system: always support staged sysupgrade
[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 #elif defined(__arm__)
46 #define reg_syscall_nr  _offsetof(struct user, regs.uregs[7])
47 #else
48 #error tracing is not supported on this architecture
49 #endif
50
51 #define INFO(fmt, ...) do { \
52         fprintf(stderr, "utrace: "fmt, ## __VA_ARGS__); \
53 } while (0)
54
55 #define ERROR(fmt, ...) do { \
56         syslog(LOG_ERR, "utrace: "fmt, ## __VA_ARGS__); \
57         fprintf(stderr, "utrace: "fmt, ## __VA_ARGS__); \
58 } while (0)
59
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;
65 static int debug;
66
67 static int max_syscall = ARRAY_SIZE(syscall_names);
68
69 static void set_syscall(const char *name, int val)
70 {
71         int i;
72
73         for (i = 0; i < max_syscall; i++)
74                 if (syscall_names[i] && !strcmp(syscall_names[i], name)) {
75                         syscall_count[i] = val;
76                         return;
77                 }
78 }
79
80 static void print_syscalls(int policy, const char *json)
81 {
82         void *c;
83         int i;
84
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);
90
91         blob_buf_init(&b, 0);
92         c = blobmsg_open_array(&b, "whitelist");
93
94         for (i = 0; i < ARRAY_SIZE(syscall_names); i++) {
95                 if (!syscall_count[i])
96                         continue;
97                 if (syscall_names[i]) {
98                         if (debug)
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]);
102                 } else {
103                         ERROR("no name found for syscall(%d)\n", i);
104                 }
105         }
106         blobmsg_close_array(&b, c);
107         blobmsg_add_u32(&b, "policy", policy);
108         if (json) {
109                 FILE *fp = fopen(json, "w");
110                 if (fp) {
111                         fprintf(fp, "%s", blobmsg_format_json_indent(b.head, true, 0));
112                         fclose(fp);
113                         INFO("saving syscall trace to %s\n", json);
114                 } else {
115                         ERROR("failed to open %s\n", json);
116                 }
117         } else {
118                 printf("%s\n",
119                         blobmsg_format_json_indent(b.head, true, 0));
120         }
121
122 }
123
124 static void tracer_cb(struct uloop_process *c, int ret)
125 {
126         if (WIFSTOPPED(ret) && WSTOPSIG(ret) & 0x80) {
127                 if (!in_syscall) {
128                         int syscall = ptrace(PTRACE_PEEKUSER, c->pid, reg_syscall_nr);
129
130                         if (syscall < syscall_max) {
131                                 syscall_count[syscall]++;
132                                 if (debug)
133                                         fprintf(stderr, "%s()\n", syscall_names[syscall]);
134                         } else if (debug) {
135                                 fprintf(stderr, "syscal(%d)\n", syscall);
136                         }
137                 }
138                 in_syscall = !in_syscall;
139         } else if (WIFEXITED(ret)) {
140                 uloop_end();
141                 return;
142         }
143         ptrace(PTRACE_SYSCALL, c->pid, 0, 0);
144         uloop_process_add(&tracer);
145 }
146
147 int main(int argc, char **argv, char **envp)
148 {
149         char *json = NULL;
150         int status, ch, policy = EPERM;
151         pid_t child;
152
153         while ((ch = getopt(argc, argv, "f:p:")) != -1) {
154                 switch (ch) {
155                 case 'f':
156                         json = optarg;
157                         break;
158                 case 'p':
159                         policy = atoi(optarg);
160                         break;
161                 }
162         }
163
164         argc -= optind;
165         argv += optind;
166
167         if (!argc)
168                 return -1;
169
170         if (getenv("TRACE_DEBUG"))
171                 debug = 1;
172         unsetenv("TRACE_DEBUG");
173
174         child = fork();
175
176         if (child == 0) {
177                 char **_argv = calloc(argc + 1, sizeof(char *));
178                 char **_envp;
179                 char *preload = "LD_PRELOAD=/lib/libpreload-trace.so";
180                 int envc = 1;
181                 int ret;
182
183                 memcpy(_argv, argv, argc * sizeof(char *));
184
185                 while (envp[envc++])
186                         ;
187
188                 _envp = calloc(envc, sizeof(char *));
189                 memcpy(&_envp[1], _envp, envc * sizeof(char *));
190                 *_envp = preload;
191
192                 ret = execve(_argv[0], _argv, _envp);
193                 ERROR("failed to exec %s: %s\n", _argv[0], strerror(errno));
194
195                 free(_argv);
196                 free(_envp);
197                 return ret;
198         }
199
200         if (child < 0)
201                 return -1;
202
203         syscall_max = ARRAY_SIZE(syscall_names);
204         syscall_count = calloc(syscall_max, sizeof(int));
205         waitpid(child, &status, 0);
206         if (!WIFSTOPPED(status)) {
207                 ERROR("failed to start %s\n", *argv);
208                 return -1;
209         }
210
211         ptrace(PTRACE_SETOPTIONS, child, 0, PTRACE_O_TRACESYSGOOD);
212         ptrace(PTRACE_SYSCALL, child, 0, 0);
213
214         uloop_init();
215         tracer.pid = child;
216         tracer.cb = tracer_cb;
217         uloop_process_add(&tracer);
218         uloop_run();
219         uloop_done();
220
221         if (!json)
222                 if (asprintf(&json, "/tmp/%s.%u.json", basename(*argv), child) < 0)
223                         ERROR("failed to allocate output path: %s\n", strerror(errno));
224
225         print_syscalls(policy, json);
226
227         return 0;
228 }