utrace: Deliver signals to traced processes
[project/procd.git] / trace / trace.c
index 5941f90..cad2d37 100644 (file)
 } while (0)
 
 #define ERROR(fmt, ...) do { \
-       syslog(0, "utrace: "fmt, ## __VA_ARGS__); \
+       syslog(LOG_ERR, "utrace: "fmt, ## __VA_ARGS__); \
        fprintf(stderr, "utrace: "fmt, ## __VA_ARGS__); \
 } while (0)
 
-static struct uloop_process tracer;
+struct tracee {
+       struct uloop_process proc;
+       int in_syscall;
+};
+
+static struct tracee tracer;
 static int *syscall_count;
 static struct blob_buf b;
 static int syscall_max;
-static int in_syscall;
 static int debug;
 
 static int max_syscall = ARRAY_SIZE(syscall_names);
@@ -77,6 +81,16 @@ static void set_syscall(const char *name, int val)
                }
 }
 
+struct syscall {
+       int syscall;
+       int count;
+};
+
+static int cmp_count(const void *a, const void *b)
+{
+       return ((struct syscall*)b)->count - ((struct syscall*)a)->count;
+}
+
 static void print_syscalls(int policy, const char *json)
 {
        void *c;
@@ -88,19 +102,29 @@ static void print_syscalls(int policy, const char *json)
        set_syscall("exit_group", 1);
        set_syscall("exit", 1);
 
+       struct syscall sorted[ARRAY_SIZE(syscall_names)];
+
+       for (i = 0; i < ARRAY_SIZE(syscall_names); i++) {
+               sorted[i].syscall = i;
+               sorted[i].count = syscall_count[i];
+       }
+
+       qsort(sorted, ARRAY_SIZE(syscall_names), sizeof(sorted[0]), cmp_count);
+
        blob_buf_init(&b, 0);
        c = blobmsg_open_array(&b, "whitelist");
 
        for (i = 0; i < ARRAY_SIZE(syscall_names); i++) {
-               if (!syscall_count[i])
-                       continue;
-               if (syscall_names[i]) {
+               int sc = sorted[i].syscall;
+               if (!sorted[i].count)
+                       break;
+               if (syscall_names[sc]) {
                        if (debug)
                                printf("syscall %d (%s) was called %d times\n",
-                                       i, syscall_names[i], syscall_count[i]);
-                       blobmsg_add_string(&b, NULL, syscall_names[i]);
+                                       sc, syscall_names[sc], sorted[i].count);
+                       blobmsg_add_string(&b, NULL, syscall_names[sc]);
                } else {
-                       ERROR("no name found for syscall(%d)\n", i);
+                       ERROR("no name found for syscall(%d)\n", sc);
                }
        }
        blobmsg_close_array(&b, c);
@@ -123,25 +147,53 @@ static void print_syscalls(int policy, const char *json)
 
 static void tracer_cb(struct uloop_process *c, int ret)
 {
-       if (WIFSTOPPED(ret) && WSTOPSIG(ret) & 0x80) {
-               if (!in_syscall) {
-                       int syscall = ptrace(PTRACE_PEEKUSER, c->pid, reg_syscall_nr);
-
-                       if (syscall < syscall_max) {
-                               syscall_count[syscall]++;
-                               if (debug)
-                                       fprintf(stderr, "%s()\n", syscall_names[syscall]);
-                       } else if (debug) {
-                               fprintf(stderr, "syscal(%d)\n", syscall);
+       struct tracee *tracee = container_of(c, struct tracee, proc);
+       int inject_signal = 0;
+
+       if (WIFSTOPPED(ret)) {
+               if (WSTOPSIG(ret) & 0x80) {
+                       if (!tracee->in_syscall) {
+                               int syscall = ptrace(PTRACE_PEEKUSER, c->pid, reg_syscall_nr);
+
+                               if (syscall < syscall_max) {
+                                       syscall_count[syscall]++;
+                                       if (debug)
+                                               fprintf(stderr, "%s()\n", syscall_names[syscall]);
+                               } else if (debug) {
+                                       fprintf(stderr, "syscal(%d)\n", syscall);
+                               }
                        }
+                       tracee->in_syscall = !tracee->in_syscall;
+               } else if ((ret >> 8) == (SIGTRAP | (PTRACE_EVENT_FORK << 8)) ||
+                          (ret >> 8) == (SIGTRAP | (PTRACE_EVENT_VFORK << 8)) ||
+                          (ret >> 8) == (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) {
+                       struct tracee *child = calloc(1, sizeof(struct tracee));
+
+                       ptrace(PTRACE_GETEVENTMSG, c->pid, 0, &child->proc.pid);
+                       child->proc.cb = tracer_cb;
+                       ptrace(PTRACE_SYSCALL, child->proc.pid, 0, 0);
+                       uloop_process_add(&child->proc);
+                       if (debug)
+                               fprintf(stderr, "Tracing new child %d\n", child->proc.pid);
+               } else {
+                       inject_signal = WSTOPSIG(ret);
+                       if (debug)
+                               fprintf(stderr, "Injecting signal %d into pid %d\n",
+                                       inject_signal, tracee->proc.pid);
+               }
+       } else if (WIFEXITED(ret) || (WIFSIGNALED(ret) && WTERMSIG(ret))) {
+               if (tracee == &tracer) {
+                       uloop_end(); /* Main process exit */
+               } else {
+                       if (debug)
+                               fprintf(stderr, "Child %d exited\n", tracee->proc.pid);
+                       free(tracee);
                }
-               in_syscall = !in_syscall;
-       } else if (WIFEXITED(ret)) {
-               uloop_end();
                return;
        }
-       ptrace(PTRACE_SYSCALL, c->pid, 0, 0);
-       uloop_process_add(&tracer);
+
+       ptrace(PTRACE_SYSCALL, c->pid, 0, inject_signal);
+       uloop_process_add(c);
 }
 
 int main(int argc, char **argv, char **envp)
@@ -176,8 +228,8 @@ int main(int argc, char **argv, char **envp)
        if (child == 0) {
                char **_argv = calloc(argc + 1, sizeof(char *));
                char **_envp;
-               char preload[] = "LD_PRELOAD=/lib/libpreload-trace.so";
-               int envc = 1;
+               char *preload = "LD_PRELOAD=/lib/libpreload-trace.so";
+               int envc = 0;
                int ret;
 
                memcpy(_argv, argv, argc * sizeof(char *));
@@ -185,12 +237,15 @@ int main(int argc, char **argv, char **envp)
                while (envp[envc++])
                        ;
 
-               _envp = calloc(envc, sizeof(char *));
-               memcpy(&_envp[1], _envp, envc * sizeof(char *));
-               *envp = preload;
+               _envp = calloc(envc + 1, sizeof(char *));
+               memcpy(&_envp[1], envp, envc * sizeof(char *));
+               *_envp = preload;
 
-               ret = execve(_argv[0], _argv, envp);
+               ret = execve(_argv[0], _argv, _envp);
                ERROR("failed to exec %s: %s\n", _argv[0], strerror(errno));
+
+               free(_argv);
+               free(_envp);
                return ret;
        }
 
@@ -205,13 +260,17 @@ int main(int argc, char **argv, char **envp)
                return -1;
        }
 
-       ptrace(PTRACE_SETOPTIONS, child, 0, PTRACE_O_TRACESYSGOOD);
+       ptrace(PTRACE_SETOPTIONS, child, 0,
+              PTRACE_O_TRACESYSGOOD |
+              PTRACE_O_TRACEFORK |
+              PTRACE_O_TRACEVFORK |
+              PTRACE_O_TRACECLONE);
        ptrace(PTRACE_SYSCALL, child, 0, 0);
 
        uloop_init();
-       tracer.pid = child;
-       tracer.cb = tracer_cb;
-       uloop_process_add(&tracer);
+       tracer.proc.pid = child;
+       tracer.proc.cb = tracer_cb;
+       uloop_process_add(&tracer.proc);
        uloop_run();
        uloop_done();