2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
16 #include <sys/ptrace.h>
17 #include <sys/types.h>
27 #include <libubox/uloop.h>
28 #include <libubox/blobmsg.h>
29 #include <libubox/blobmsg_json.h>
31 #include "../syscall-names.h"
33 #define _offsetof(a, b) __builtin_offsetof(a,b)
34 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
37 #define reg_syscall_nr _offsetof(struct user, regs.orig_rax)
38 #elif defined(__i386__)
39 #define reg_syscall_nr _offsetof(struct user, regs.orig_eax)
44 #define reg_syscall_nr (EF_REG2 / 4)
45 #elif defined(__arm__)
46 #define reg_syscall_nr _offsetof(struct user, regs.uregs[7])
48 #error tracing is not supported on this architecture
51 #define INFO(fmt, ...) do { \
52 fprintf(stderr, "utrace: "fmt, ## __VA_ARGS__); \
55 #define ERROR(fmt, ...) do { \
56 syslog(LOG_ERR, "utrace: "fmt, ## __VA_ARGS__); \
57 fprintf(stderr, "utrace: "fmt, ## __VA_ARGS__); \
60 static struct uloop_process tracer;
61 static int *syscall_count;
62 static struct blob_buf b;
63 static int syscall_max;
64 static int in_syscall;
67 static int max_syscall = ARRAY_SIZE(syscall_names);
69 static void set_syscall(const char *name, int val)
73 for (i = 0; i < max_syscall; i++)
74 if (syscall_names[i] && !strcmp(syscall_names[i], name)) {
75 syscall_count[i] = val;
80 static void print_syscalls(int policy, const char *json)
85 set_syscall("rt_sigaction", 1);
86 set_syscall("sigreturn", 1);
87 set_syscall("rt_sigreturn", 1);
88 set_syscall("exit_group", 1);
89 set_syscall("exit", 1);
92 c = blobmsg_open_array(&b, "whitelist");
94 for (i = 0; i < ARRAY_SIZE(syscall_names); i++) {
95 if (!syscall_count[i])
97 if (syscall_names[i]) {
99 printf("syscall %d (%s) was called %d times\n",
100 i, syscall_names[i], syscall_count[i]);
101 blobmsg_add_string(&b, NULL, syscall_names[i]);
103 ERROR("no name found for syscall(%d)\n", i);
106 blobmsg_close_array(&b, c);
107 blobmsg_add_u32(&b, "policy", policy);
109 FILE *fp = fopen(json, "w");
111 fprintf(fp, "%s", blobmsg_format_json_indent(b.head, true, 0));
113 INFO("saving syscall trace to %s\n", json);
115 ERROR("failed to open %s\n", json);
119 blobmsg_format_json_indent(b.head, true, 0));
124 static void tracer_cb(struct uloop_process *c, int ret)
126 if (WIFSTOPPED(ret) && WSTOPSIG(ret) & 0x80) {
128 int syscall = ptrace(PTRACE_PEEKUSER, c->pid, reg_syscall_nr);
130 if (syscall < syscall_max) {
131 syscall_count[syscall]++;
133 fprintf(stderr, "%s()\n", syscall_names[syscall]);
135 fprintf(stderr, "syscal(%d)\n", syscall);
138 in_syscall = !in_syscall;
139 } else if (WIFEXITED(ret)) {
143 ptrace(PTRACE_SYSCALL, c->pid, 0, 0);
144 uloop_process_add(&tracer);
147 int main(int argc, char **argv, char **envp)
150 int status, ch, policy = EPERM;
153 while ((ch = getopt(argc, argv, "f:p:")) != -1) {
159 policy = atoi(optarg);
170 if (getenv("TRACE_DEBUG"))
172 unsetenv("TRACE_DEBUG");
177 char **_argv = calloc(argc + 1, sizeof(char *));
179 char preload[] = "LD_PRELOAD=/lib/libpreload-trace.so";
183 memcpy(_argv, argv, argc * sizeof(char *));
188 _envp = calloc(envc, sizeof(char *));
189 memcpy(&_envp[1], _envp, envc * sizeof(char *));
192 ret = execve(_argv[0], _argv, envp);
193 ERROR("failed to exec %s: %s\n", _argv[0], strerror(errno));
200 syscall_max = ARRAY_SIZE(syscall_names);
201 syscall_count = calloc(syscall_max, sizeof(int));
202 waitpid(child, &status, 0);
203 if (!WIFSTOPPED(status)) {
204 ERROR("failed to start %s\n", *argv);
208 ptrace(PTRACE_SETOPTIONS, child, 0, PTRACE_O_TRACESYSGOOD);
209 ptrace(PTRACE_SYSCALL, child, 0, 0);
213 tracer.cb = tracer_cb;
214 uloop_process_add(&tracer);
219 if (asprintf(&json, "/tmp/%s.%u.json", basename(*argv), child) < 0)
220 ERROR("failed to allocate output path: %s\n", strerror(errno));
222 print_syscalls(policy, json);