Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 44ec5822 authored by Dan Albert's avatar Dan Albert Committed by Gerrit Code Review
Browse files

Merge "Use text based trace masks for adbd."

parents a16b0f4c ea2175ab
Loading
Loading
Loading
Loading
+83 −23
Original line number Diff line number Diff line
@@ -19,16 +19,18 @@
#include "sysdeps.h"
#include "adb.h"

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include <errno.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <stdint.h>
#include <time.h>

#include <string>

#include "adb_auth.h"
#include "adb_io.h"
@@ -75,17 +77,71 @@ void fatal_errno(const char *fmt, ...)
    exit(-1);
}

#if !ADB_HOST
void start_device_log(void) {
    adb_mkdir("/data/adb", 0775);

    struct tm now;
    time_t t;
    tzset();
    time(&t);
    localtime_r(&t, &now);

    char path[PATH_MAX];
    strftime(path, sizeof(path), "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt", &now);

    int fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
    if (fd == -1) {
        return;
    }

    // redirect stdout and stderr to the log file
    dup2(fd, STDOUT_FILENO);
    dup2(fd, STDERR_FILENO);
    fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
    adb_close(fd);

    fd = unix_open("/dev/null", O_RDONLY);
    dup2(fd, 0);
    adb_close(fd);
}
#endif

int adb_trace_mask;

/* read a comma/space/colum/semi-column separated list of tags
 * from the ADB_TRACE environment variable and build the trace
 * mask from it. note that '1' and 'all' are special cases to
 * enable all tracing
 */
void  adb_trace_init(void)
{
    const char*  p = getenv("ADB_TRACE");
    const char*  q;
std::string get_trace_setting_from_env() {
    const char* setting = getenv("ADB_TRACE");
    if (setting == nullptr) {
        setting = "";
    }

    return std::string(setting);
}

#if !ADB_HOST
std::string get_trace_setting_from_prop() {
    char buf[PROPERTY_VALUE_MAX];
    property_get("persist.adb.trace_mask", buf, "");
    return std::string(buf);
}
#endif

std::string get_trace_setting() {
#if ADB_HOST
    return get_trace_setting_from_env();
#else
    return get_trace_setting_from_prop();
#endif
}

// Split the comma/space/colum/semi-column separated list of tags from the trace
// setting and build the trace mask from it. note that '1' and 'all' are special
// cases to enable all tracing.
//
// adb's trace setting comes from the ADB_TRACE environment variable, whereas
// adbd's comes from the system property persist.adb.trace_mask.
void adb_trace_init() {
    const std::string trace_setting = get_trace_setting();

    static const struct {
        const char*  tag;
@@ -107,25 +163,25 @@ void adb_trace_init(void)
        { NULL, 0 }
    };

    if (p == NULL)
    if (trace_setting.empty()) {
        return;
    }

    /* use a comma/column/semi-colum/space separated list */
    // Use a comma/colon/semi-colon/space separated list
    const char* p = trace_setting.c_str();
    while (*p) {
        int  len, tagn;

        q = strpbrk(p, " ,:;");
        const char* q = strpbrk(p, " ,:;");
        if (q == NULL) {
            q = p + strlen(p);
        }
        len = q - p;

        for (tagn = 0; tags[tagn].tag != NULL; tagn++)
        {
        for (tagn = 0; tags[tagn].tag != NULL; tagn++) {
            int  taglen = strlen(tags[tagn].tag);

            if (len == taglen && !memcmp(tags[tagn].tag, p, len) )
            {
            if (len == taglen && !memcmp(tags[tagn].tag, p, len)) {
                int  flag = tags[tagn].flag;
                if (flag == 0) {
                    adb_trace_mask = ~0;
@@ -139,6 +195,10 @@ void adb_trace_init(void)
        if (*p)
            p++;
    }

#if !ADB_HOST
    start_device_log();
#endif
}

apacket* get_apacket(void)
+5 −40
Original line number Diff line number Diff line
@@ -148,42 +148,6 @@ static bool should_drop_privileges() {
    return true; // "adb root" not allowed, always drop privileges.
#endif /* ALLOW_ADBD_ROOT */
}

void start_device_log(void)
{
    int fd;
    char    path[PATH_MAX];
    struct tm now;
    time_t t;
    char value[PROPERTY_VALUE_MAX];

    // read the trace mask from persistent property persist.adb.trace_mask
    // give up if the property is not set or cannot be parsed
    property_get("persist.adb.trace_mask", value, "");
    if (sscanf(value, "%x", &adb_trace_mask) != 1)
        return;

    adb_mkdir("/data/adb", 0775);
    tzset();
    time(&t);
    localtime_r(&t, &now);
    strftime(path, sizeof(path),
                "/data/adb/adb-%Y-%m-%d-%H-%M-%S.txt",
                &now);
    fd = unix_open(path, O_WRONLY | O_CREAT | O_TRUNC, 0640);
    if (fd < 0)
        return;

    // redirect stdout and stderr to the log file
    dup2(fd, 1);
    dup2(fd, 2);
    fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
    adb_close(fd);

    fd = unix_open("/dev/null", O_RDONLY);
    dup2(fd, 0);
    adb_close(fd);
}
#endif /* ADB_HOST */

/* Constructs a local name of form tcp:port.
@@ -385,11 +349,13 @@ int adb_main(int is_daemon, int server_port)
    return 0;
}

int main(int argc, char **argv)
{
int main(int argc, char **argv) {
#if ADB_HOST
    adb_sysdeps_init();
#endif
    adb_trace_init();

#if ADB_HOST
    D("Handling commandline()\n");
    return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
#else
@@ -418,7 +384,6 @@ int main(int argc, char **argv)
        }
    }

    start_device_log();
    D("Handling main()\n");
    return adb_main(0, DEFAULT_ADB_PORT);
#endif