forked from krytarowski/picotrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace.c
286 lines (225 loc) · 6.51 KB
/
trace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* $NetBSD$ */
/*-
* Copyright (c) 2019 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Kamil Rytarowski.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <threads.h>
#include <util.h>
#include "trace.h"
#include "trace_utils.h"
static int worker(void *);
static void monitor_sigtrap(pid_t);
static void monitor_crash(pid_t);
static void monitor_signal(pid_t);
static mtx_t mtx;
static cnd_t cnd;
static int workers;
static struct trace_ops *ops;
#define TRACE_MAIN(argc, argv) (*ops->main)(argc, argv)
#define TRACE_END() (*ops->end)()
#define TRACE_STARTUP(pid) (*ops->startup)(pid)
#define TRACE_UNSTOP(pid) (*ops->unstop)(pid)
#define TRACE_CONTINUED(pid) (*ops->continued)(pid)
#define TRACE_SIGNALED(pid, sig, core) (*ops->signaled)(pid, sig, core)
#define TRACE_EXITED(pid, status) (*ops->exited)(pid, status)
#define TRACE_CLEANUP(pid) (*ops->cleanup)(pid)
#define TRACE_DEBUGREGISTER(pid, lid) (*ops->debugregister)(pid, lid)
#define TRACE_SINGLESTEP(pid, lid) (*ops->singlestep)(pid, lid)
#define TRACE_BREAKPOINT(pid, lid) (*ops->breakpoint)(pid, lid)
#define TRACE_SYSCALLENTRY(pid, lid, si) (*ops->syscallentry)(pid, lid, si)
#define TRACE_SYSCALLEXIT(pid, lid, si) (*ops->syscallexit)(pid, lid, si)
#define TRACE_EXEC(pid, lid) (*ops->exec)(pid, lid)
#define TRACE_FORKED(pid, lid, child) (*ops->forked)(pid, lid, child)
#define TRACE_VFORKED(pid, lid, child) (*ops->vforked)(pid, lid, child)
#define TRACE_VFORKDONE(pid, lid, child) (*ops->vforkdone)(pid, lid, child)
#define TRACE_LWPCREATED(pid, lid, lwp) (*ops->lwpcreated)(pid, lid, lwp)
#define TRACE_LWPEXITED(pid, lid, lwp) (*ops->lwpexited)(pid, lid, lwp)
#define TRACE_CRASHED(pid, lid, si) (*ops->crashed)(pid, lid, si)
#define TRACE_STOPPED(pid, lid, si) (*ops->stopped)(pid, lid, si)
int
main(int argc, char **argv)
{
const char *p;
setprogname(argv[0]);
p = getprogname();
if (strcmp(p, "picotrace") == 0)
ops = &trace_ops_picotrace;
else
errx(EXIT_FAILURE, "unrecognized program name: '%s'", p);
trace_cnd_init(&cnd);
trace_mtx_init(&mtx, mtx_plain);
TRACE_MAIN(argc, argv);
trace_mtx_lock(&mtx);
while (workers > 0)
trace_cnd_wait(&cnd, &mtx);
trace_mtx_unlock(&mtx);
TRACE_END();
return EXIT_SUCCESS;
}
void
launch_worker(pid_t pid)
{
thrd_t t;
trace_mtx_lock(&mtx);
++workers;
trace_mtx_unlock(&mtx);
/* The t thread is not waited as the thread will perform selfdetach. */
thrd_create(&t, worker, (void *)(intptr_t)pid);
}
static int
worker(void *arg)
{
ptrace_siginfo_t psi;
ptrace_event_t pe;
pid_t pid;
int status;
assert(arg != NULL);
pid = (pid_t)(intptr_t)arg;
TRACE_STARTUP(pid);
while (true) {
TRACE_UNSTOP(pid);
trace_waitpid(pid, &status, 0);
/* Tracee terminating event */
if (WIFSTOPPED(status)) {
switch (WSTOPSIG(status)) {
case SIGTRAP:
monitor_sigtrap(pid);
break;
case SIGSEGV:
/* FALLTHROUGH */
case SIGILL:
/* FALLTHROUGH */
case SIGFPE:
/* FALLTHROUGH */
case SIGBUS:
monitor_crash(pid);
break;
default:
monitor_signal(pid);
}
}
if (WIFCONTINUED(status)) {
TRACE_CONTINUED(pid);
}
/* Tracee terminating event */
if (WIFSIGNALED(status)) {
TRACE_SIGNALED(pid, WTERMSIG(status), WCOREDUMP(status));
break;
}
if (WIFEXITED(status)) {
TRACE_EXITED(pid, status);
break;
}
};
TRACE_CLEANUP(pid);
trace_mtx_lock(&mtx);
--workers;
trace_mtx_unlock(&mtx);
trace_cnd_signal(&cnd);
trace_thrd_detach(thrd_current());
thrd_exit(0);
}
static void
monitor_sigtrap(pid_t pid)
{
char buf[1024];
ptrace_state_t pst;
ptrace_siginfo_t psi;
lwpid_t lid;
int status;
trace_ptrace(PT_GET_SIGINFO, pid, &psi, sizeof(psi));
lid = psi.psi_lwpid;
switch (psi.psi_siginfo.si_code) {
case TRAP_DBREG:
TRACE_DEBUGREGISTER(pid, lid);
break;
case TRAP_TRACE:
TRACE_SINGLESTEP(pid, lid);
break;
case TRAP_BRKPT:
TRACE_BREAKPOINT(pid, lid);
break;
case TRAP_SCE:
TRACE_SYSCALLENTRY(pid, lid, &psi.psi_siginfo);
break;
case TRAP_SCX:
TRACE_SYSCALLEXIT(pid, lid, &psi.psi_siginfo);
break;
case TRAP_EXEC:
TRACE_EXEC(pid, lid);
break;
case TRAP_LWP:
/* FALLTHROUGH */
case TRAP_CHLD:
trace_ptrace(PT_GET_PROCESS_STATE, pid, &pst, sizeof(pst));
switch (pst.pe_report_event) {
case PTRACE_FORK:
TRACE_FORKED(pid, lid, pst.pe_other_pid);
break;
case PTRACE_VFORK:
TRACE_VFORKED(pid, lid, pst.pe_other_pid);
break;
case PTRACE_VFORK_DONE:
TRACE_VFORKDONE(pid, lid, pst.pe_other_pid);
break;
case PTRACE_LWP_CREATE:
TRACE_LWPCREATED(pid, lid, pst.pe_lwp);
break;
case PTRACE_LWP_EXIT:
TRACE_LWPEXITED(pid, lid, pst.pe_lwp);
break;
}
default:
/* Fallback to regular crash/signal. */
if (psi.psi_siginfo.si_code <= SI_USER)
monitor_crash(pid);
else
monitor_signal(pid);
break;
}
}
static void
monitor_crash(pid_t pid)
{
ptrace_siginfo_t psi;
lwpid_t lid;
trace_ptrace(PT_GET_SIGINFO, pid, &psi, sizeof(psi));
lid = psi.psi_lwpid;
TRACE_CRASHED(pid, lid, &psi.psi_siginfo);
}
static void
monitor_signal(pid_t pid)
{
ptrace_siginfo_t psi;
lwpid_t lid;
trace_ptrace(PT_GET_SIGINFO, pid, &psi, sizeof(psi));
lid = psi.psi_lwpid;
TRACE_STOPPED(pid, lid, &psi.psi_siginfo);
}