-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProcessTracer.cpp
454 lines (409 loc) · 10.2 KB
/
ProcessTracer.cpp
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
NAME
ProcessTracer
DESCRIPTION
About the simplest Linux debugger which is useful!
COPYRIGHT
Copyright (C) 2012 by Roger Orr <[email protected]>
This software is distributed in the hope that it will be useful, but
without WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Permission is granted to anyone to make or distribute verbatim
copies of this software provided that the copyright notice and
this permission notice are preserved, and that the distributor
grants the recipent permission for further distribution as permitted
by this notice.
Comments and suggestions are always welcome.
Please report bugs to [email protected].
*/
static char const szRCSID[] = "$Id: ProcessTracer.cpp 256 2020-04-09 21:35:25Z Roger $";
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>
#include <asm/unistd.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
namespace
{
std::runtime_error make_error(std::string const &action)
{
std::string const what(action + " failed: " + strerror(errno));
return std::runtime_error(what);
}
static struct
{
int code;
char const *name;
} signals[] = {
{ SIGHUP, "hangup" },
{ SIGINT, "interrupt" },
{ SIGQUIT, "quit" },
{ SIGILL, "illegal instruction" },
{ SIGTRAP, "trap" },
{ SIGABRT, "abort" },
{ SIGBUS, "bus error" },
{ SIGFPE, "floating point exception" },
{ SIGKILL, "kill" },
{ SIGUSR1, "user 1" },
{ SIGSEGV, "segmentation violation" },
{ SIGUSR2, "user 2" },
{ SIGPIPE, "broken pipe" },
{ SIGALRM, "alarm" },
{ SIGTERM, "terminate" },
{ SIGSTKFLT, "stack fault" },
{ SIGCHLD, "child" },
{ SIGCONT, "continue" },
{ SIGSTOP, "stop" },
{ SIGTSTP, "tty stop" },
{ SIGTTIN, "tty in" },
{ SIGTTOU, "tty out" },
{ SIGURG, "urgent" },
{ SIGXCPU, "exceeded CPU" },
{ SIGXFSZ, "exceeded file size"},
{ SIGVTALRM, "virtual alarm" },
{ SIGPROF, "profiling" },
{ SIGWINCH, "window size change" },
{ SIGPOLL, "poll" },
};
/** Strema helper for signals */
class sigstrm
{
int const signal;
public:
sigstrm(int signal) : signal(signal) {}
friend std::ostream & operator<<(std::ostream& os, sigstrm const &rhs)
{
int idx = 0;
for (; idx != sizeof(signals)/sizeof(signals[0]); ++idx)
{
if (signals[idx].code == rhs.signal)
{
os << signals[idx].name;
break;
}
}
if (idx == sizeof(signals)/sizeof(signals[0]))
{
os << "signal " << rhs.signal;
}
return os;
}
};
} // namespace
/** Simple ptrace user */
class ProcessTracer
{
private:
pid_t pid;
std::ostream &os;
bool initialised;
public:
/** Create */
ProcessTracer(pid_t pid, std::ostream &os)
: pid(pid), os(os), initialised(false) {}
/** Run the debug loop */
void run();
private:
/** Stop received */
int OnStop(int signal, int event);
/** PTrace event received */
void OnEvent(int event);
/** System trap received */
void OnTrap();
/** System call received */
void OnSysCall();
/** Check if specified system call is of interest */
bool SelectedCall(int func);
/** System call 'func' being entered */
void OnCallEntry(int func, long int args[]);
/** Sytem call 'func' being exited */
void OnCallExit(int func, int rc);
/** Signal received */
bool OnSignal(int signal);
/** Read a string from the taget process */
std::string readString(long addr);
};
/////////////////////////////////////////////////////////////////////////////////////////////////
void ProcessTracer::run()
{
int status(0); // Status of the stopped child process
// This is the main tracing loop. When the child stops,
// we examine the system call and its arguments
while ((pid = waitpid(-1, &status, __WALL)) != -1)
{
int send_signal(0);
if (WIFSTOPPED(status))
{
send_signal = OnStop(WSTOPSIG(status), status >> 16);
}
else if (WIFEXITED(status))
{
os << "Exit(" << WEXITSTATUS(status) << ")" << std::endl;
}
else if (WIFSIGNALED(status))
{
os << "Terminated: " << sigstrm(WTERMSIG(status)) << std::endl;
}
else if (WIFCONTINUED(status))
{
os << "Continued" << std::endl;
}
else
{
os << "Unexpected status: " << status << std::endl;
}
ptrace(PTRACE_SYSCALL, pid, 0, send_signal);
}
if (errno != ECHILD)
{
throw make_error("wait");
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
int ProcessTracer::OnStop(int signal, int event)
{
if (!initialised)
{
initialised = true;
long const options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK |
PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE |
PTRACE_O_TRACEEXEC;
if (ptrace(PTRACE_SETOPTIONS, pid, 0, options) == -1)
{
throw make_error("PTRACE_SETOPTIONS");
}
}
else if (signal == SIGTRAP)
{
if (event)
{
OnEvent(event);
}
else
{
OnTrap();
}
}
else if (signal == (SIGTRAP | 0x80))
{
OnSysCall();
}
else
{
if (OnSignal(signal))
{
return signal;
}
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void ProcessTracer::OnEvent(int event)
{
unsigned long message(0);
switch (event)
{
case PTRACE_EVENT_CLONE:
case PTRACE_EVENT_FORK:
case PTRACE_EVENT_VFORK:
if (ptrace(PTRACE_GETEVENTMSG, pid, 0, &message) == 0)
{
os << "New pid: " << message << std::endl;
}
break;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void ProcessTracer::OnTrap()
{
siginfo_t siginfo;
if (ptrace(PTRACE_GETSIGINFO, pid, 0, &siginfo) == -1)
{
throw make_error("ptrace(PTRACE_GETSIGINFO)");
}
if (siginfo.si_code == SIGTRAP)
{
OnSysCall();
}
else
{
os << "Breakpoint" << std::endl;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void ProcessTracer::OnSysCall()
{
struct user_regs_struct regs;
if (ptrace(PTRACE_GETREGS, pid, 0, ®s) == -1)
{
throw make_error("ptrace(PTRACE_GETREGS)");
}
#if __x86_64__
int const rc = regs.rax;
int const func = regs.orig_rax;
long int args[] = { regs.rdi, regs.rsi, regs.rdx, regs.r10, regs.r8, regs.r9 };
#elif __i386__
int const rc = regs.eax;
int const func = regs.orig_eax;
long int args[] = { regs.ebx, regs.ecx, regs.edx, regs.esi, regs.edi, regs.ebp };
#else
#error Unknown target architecture
#endif // __x86_64__
if (SelectedCall(func))
{
if (rc == -ENOSYS)
{
OnCallEntry(func, args);
}
else
{
OnCallExit(func, rc);
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
bool ProcessTracer::SelectedCall(int func)
{
switch (func)
{
case __NR_open:
case __NR_close:
return true;
}
return false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void ProcessTracer::OnCallEntry(int func, long int args[])
{
if (func == __NR_open)
{
os << "open(\"" << readString(args[0]) << "\") = " << std::flush;
}
else if (func == __NR_close)
{
os << "close(" << args[0] << ") = " << std::flush;
}
else
{
os << '#' << func << "(" << args[0] << ") = " << std::flush;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void ProcessTracer::OnCallExit(int func, int rc)
{
if (rc < 0)
{
os << rc << "(" << strerror(-rc) << ")" << std::endl;
}
else
{
os << std::hex << rc << std::dec << std::endl;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
bool ProcessTracer::OnSignal(int signal)
{
os << "Signal: " << sigstrm(signal) << std::endl;
bool bDeliver(true);
switch (signal)
{
case SIGCHLD:
case SIGSTOP:
bDeliver = false;
break;
}
return bDeliver;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
std::string ProcessTracer::readString(long addr)
{
std::string result;
#if 1
int offset = addr % sizeof(long);
char *peekAddr = (char *)addr - offset;
// Loop round to find the end of the string
bool stringFound = false;
do
{
long const peekWord =
ptrace( PTRACE_PEEKDATA, pid, peekAddr, NULL );
if ( -1 == peekWord )
{
throw make_error("ptrace(PTRACE_PEEKDATA)");
}
char const * const tmpString = (char const *)&peekWord;
for (unsigned int i = offset; i != sizeof(long); i++)
{
if (tmpString[i] == '\0')
{
stringFound = true;
break;
}
result.push_back(tmpString[i]);
}
peekAddr += sizeof(long);
offset = 0;
} while ( !stringFound );
#else
std::ostringstream os;
os << "/proc/" << pid << "/mem";
std::ifstream mem(os.str().c_str(), std::ios::binary);
mem.exceptions(std::ios::failbit);
mem.seekg((std::streampos)addr);
std::getline(mem, result, '\0');
#endif // 0
return result;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
// Fork a child process, running under ptrace, and return its pid
pid_t CreateProcess(int argc, char **argv)
{
pid_t const cpid = fork();
if (cpid > 0)
{
// In the parent
return cpid;
}
else if (cpid == 0)
{
// In the child
if (ptrace(PTRACE_TRACEME, 0, 0, 0) == -1)
{
throw make_error("ptrace(PTRACE_TRACEME)");
}
execv(argv[0], argv);
throw make_error("execv");
}
else
{
throw make_error("fork");
}
}
int main(int argc, char **argv)
{
int rc(1);
if (argc <= 1)
{
std::cout << "Syntax: ProcessTracer command_line" << std::endl;
return 1;
}
++argv;
--argc;
try
{
pid_t pid = CreateProcess(argc, argv);
ProcessTracer(pid, std::cerr).run();
rc = 0;
}
catch ( std::exception &ex)
{
std::cerr << "Unexpected exception: " << ex.what() << std::endl;
}
return rc;
}