fix include order
[project/procd.git] / trace / trace.c
1 /*
2 * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
3 *
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
7 *
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.
12 */
13
14 #define _GNU_SOURCE
15 #include <stddef.h>
16 #include <sys/ptrace.h>
17 #include <sys/types.h>
18 #include <sys/user.h>
19 #include <sys/wait.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <syslog.h>
26
27 #include <libubox/uloop.h>
28 #include <libubox/blobmsg.h>
29 #include <libubox/blobmsg_json.h>
30
31 #include "../syscall-names.h"
32
33 #define _offsetof(a, b) __builtin_offsetof(a,b)
34 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
35
36 #ifdef __amd64__
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)
40 #elif defined(__mips)
41 # ifndef EF_REG2
42 # define EF_REG2 8
43 # endif
44 #define reg_syscall_nr (EF_REG2 / 4)
45 #else
46 #error tracing is not supported on this architecture
47 #endif
48
49 #define INFO(fmt, ...) do { \
50 fprintf(stderr, "utrace: "fmt, ## __VA_ARGS__); \
51 } while (0)
52
53 #define ERROR(fmt, ...) do { \
54 syslog(0, "utrace: "fmt, ## __VA_ARGS__); \
55 fprintf(stderr, "utrace: "fmt, ## __VA_ARGS__); \
56 } while (0)
57
58 static struct uloop_process tracer;
59 static int *syscall_count;
60 static struct blob_buf b;
61 static int syscall_max;
62 static int in_syscall;
63 static int debug;
64
65 static int max_syscall = ARRAY_SIZE(syscall_names);
66
67 static void set_syscall(const char *name, int val)
68 {
69 int i;
70
71 for (i = 0; i < max_syscall; i++)
72 if (syscall_names[i] && !strcmp(syscall_names[i], name)) {
73 syscall_count[i] = val;
74 return;
75 }
76 }
77
78 static void print_syscalls(int policy, const char *json)
79 {
80 void *c;
81 int i;
82
83 set_syscall("rt_sigaction", 1);
84 set_syscall("sigreturn", 1);
85 set_syscall("rt_sigreturn", 1);
86 set_syscall("exit_group", 1);
87 set_syscall("exit", 1);
88
89 blob_buf_init(&b, 0);
90 c = blobmsg_open_array(&b, "whitelist");
91
92 for (i = 0; i < ARRAY_SIZE(syscall_names); i++) {
93 if (!syscall_count[i])
94 continue;
95 if (syscall_names[i]) {
96 if (debug)
97 printf("syscall %d (%s) was called %d times\n",
98 i, syscall_names[i], syscall_count[i]);
99 blobmsg_add_string(&b, NULL, syscall_names[i]);
100 } else {
101 ERROR("no name found for syscall(%d)\n", i);
102 }
103 }
104 blobmsg_close_array(&b, c);
105 blobmsg_add_u32(&b, "policy", policy);
106 if (json) {
107 FILE *fp = fopen(json, "w");
108 if (fp) {
109 fprintf(fp, "%s", blobmsg_format_json_indent(b.head, true, 0));
110 fclose(fp);
111 INFO("saving syscall trace to %s\n", json);
112 } else {
113 ERROR("failed to open %s\n", json);
114 }
115 } else {
116 printf("%s\n",
117 blobmsg_format_json_indent(b.head, true, 0));
118 }
119
120 }
121
122 static void tracer_cb(struct uloop_process *c, int ret)
123 {
124 if (WIFSTOPPED(ret) && WSTOPSIG(ret) & 0x80) {
125 if (!in_syscall) {
126 int syscall = ptrace(PTRACE_PEEKUSER, c->pid, reg_syscall_nr);
127
128 if (syscall < syscall_max) {
129 syscall_count[syscall]++;
130 if (debug)
131 fprintf(stderr, "%s()\n", syscall_names[syscall]);
132 } else if (debug) {
133 fprintf(stderr, "syscal(%d)\n", syscall);
134 }
135 }
136 in_syscall = !in_syscall;
137 } else if (WIFEXITED(ret)) {
138 uloop_end();
139 return;
140 }
141 ptrace(PTRACE_SYSCALL, c->pid, 0, 0);
142 uloop_process_add(&tracer);
143 }
144
145 int main(int argc, char **argv, char **envp)
146 {
147 char *json = NULL;
148 int status, ch, policy = EPERM;
149 pid_t child;
150
151 while ((ch = getopt(argc, argv, "f:p:")) != -1) {
152 switch (ch) {
153 case 'f':
154 json = optarg;
155 break;
156 case 'p':
157 policy = atoi(optarg);
158 break;
159 }
160 }
161
162 argc -= optind;
163 argv += optind;
164
165 if (!argc)
166 return -1;
167
168 if (getenv("TRACE_DEBUG"))
169 debug = 1;
170 unsetenv("TRACE_DEBUG");
171
172 child = fork();
173
174 if (child == 0) {
175 char **_argv = calloc(argc + 1, sizeof(char *));
176 char **_envp;
177 char preload[] = "LD_PRELOAD=/lib/libpreload-trace.so";
178 int envc = 1;
179 int ret;
180
181 memcpy(_argv, argv, argc * sizeof(char *));
182
183 while (envp[envc++])
184 ;
185
186 _envp = calloc(envc, sizeof(char *));
187 memcpy(&_envp[1], _envp, envc * sizeof(char *));
188 *envp = preload;
189
190 ret = execve(_argv[0], _argv, envp);
191 ERROR("failed to exec %s: %s\n", _argv[0], strerror(errno));
192 return ret;
193 }
194
195 if (child < 0)
196 return -1;
197
198 syscall_max = ARRAY_SIZE(syscall_names);
199 syscall_count = calloc(syscall_max, sizeof(int));
200 waitpid(child, &status, 0);
201 if (!WIFSTOPPED(status)) {
202 ERROR("failed to start %s\n", *argv);
203 return -1;
204 }
205
206 ptrace(PTRACE_SETOPTIONS, child, 0, PTRACE_O_TRACESYSGOOD);
207 ptrace(PTRACE_SYSCALL, child, 0, 0);
208
209 uloop_init();
210 tracer.pid = child;
211 tracer.cb = tracer_cb;
212 uloop_process_add(&tracer);
213 uloop_run();
214 uloop_done();
215
216 if (!json)
217 if (asprintf(&json, "/tmp/%s.%u.json", basename(*argv), child) < 0)
218 ERROR("failed to allocate output path: %s\n", strerror(errno));
219
220 print_syscalls(policy, json);
221
222 return 0;
223 }