utrace: Sort syscalls by number of invocations
[project/procd.git] / trace / trace.c
index d70957d..35bc548 100644 (file)
@@ -42,6 +42,8 @@
 # define EF_REG2       8
 # endif
 #define reg_syscall_nr (EF_REG2 / 4)
+#elif defined(__arm__)
+#define reg_syscall_nr _offsetof(struct user, regs.uregs[7])
 #else
 #error tracing is not supported on this architecture
 #endif
@@ -51,7 +53,7 @@
 } 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)
 
@@ -75,6 +77,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;
@@ -86,19 +98,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);
@@ -174,8 +196,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 *));
@@ -183,12 +205,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;
        }