-
Notifications
You must be signed in to change notification settings - Fork 1
/
strace.c
774 lines (749 loc) · 12.6 KB
/
strace.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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <unistd.h>
void child(char** argv);
void parent(pid_t pid);
void printerror(char* type);
void detach();
char* syscalltostring(long id);
pid_t pid;
char buffer[256];
int main(int argc, char** argv)
{
if (argc == 1)
{
printf("Usage:\n");
printf("\t%s EXECUTABLE [arg1] ... [argn]\n", argv[0]);
printf("\t%s -p PID\n", argv[0]);
}
else if (strcmp(argv[1], "-p") == 0)
{
if (argc != 3 || atoi(argv[2]) <= 0)
{
printf("Missing or invalid pid.\n");
}
else
{
pid = (pid_t)atoi(argv[2]);
if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0)
printerror("PTRACE_ATTACH");
printf("strace: Process %d attached\n", pid);
signal(SIGTERM, detach);
signal(SIGINT, detach);
parent(pid);
}
}
else
{
if ((pid = fork()) < 0)
printerror("fork()");
else if (pid == 0)
child(argv + 1);
else
parent(pid);
}
return 0;
}
void child(char** argv)
{
if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) < 0)
printerror("ptrace()");
if (execvp(argv[0], argv) < 0)
printerror("execvp()");
}
void parent(pid_t pid)
{
printf("Format: syscall_name(rdi, rsi, rdx, r10, r8, r9) = syscall_return_value\n");
int status, resp = 0;
struct user_regs_struct regs;
while (1)
{
if (waitpid(pid, &status, 0) == -1)
printerror("waitpid()");
if (WIFEXITED(status))
{
if (resp == 1)
printf(" = ?\n");
else
printf("%s(%llu, %llu, %llu, %llu, %llu, %llu) = ?\n", syscalltostring(regs.orig_rax), regs.rdi, regs.rsi, regs.rdx, regs.r10, regs.r8, regs.r9);
printf("--- exited with %llu ---\n", regs.rdi);
exit(regs.rdi);
}
if (ptrace(PTRACE_GETREGS, pid, 0, ®s) == -1)
printerror("PTRACE_GETREGS");
if (resp == 0)
printf("%s(%llu, %llu, %llu, %llu, %llu, %llu)", syscalltostring(regs.orig_rax), regs.rdi, regs.rsi, regs.rdx, regs.r10, regs.r8, regs.r9);
if ((long)regs.rax == -38)
{
resp = 1;
}
else
{
long code = (long)regs.rax;
if (code < 0)
code++;
printf(" = %ld\n", code);
resp = 0;
}
if (ptrace(PTRACE_SYSCALL, pid, 0, 0) == -1)
printerror("PTRACE_SYSCALL");
}
}
void printerror(char* type)
{
snprintf(buffer, 256, "Error %s", type);
perror(buffer);
exit(0);
}
void detach()
{
if (waitpid(pid, 0, 0) == -1)
printerror("waitpid()");
if (ptrace(PTRACE_DETACH, pid, 0, 0) == -1)
printerror("PTRACE_DETACH");
printf("\nstrace: Process %d detached\n", pid);
exit(0);
}
char* syscalltostring(long id)
{
//Generated from https://filippo.io/linux-syscall-table/
switch (id)
{
case 0:
return "read";
case 1:
return "write";
case 2:
return "open";
case 3:
return "close";
case 4:
return "stat";
case 5:
return "fstat";
case 6:
return "lstat";
case 7:
return "poll";
case 8:
return "lseek";
case 9:
return "mmap";
case 10:
return "mprotect";
case 11:
return "munmap";
case 12:
return "brk";
case 13:
return "rt_sigaction";
case 14:
return "rt_sigprocmask";
case 15:
return "rt_sigreturn";
case 16:
return "ioctl";
case 17:
return "pread64";
case 18:
return "pwrite64";
case 19:
return "readv";
case 20:
return "writev";
case 21:
return "access";
case 22:
return "pipe";
case 23:
return "select";
case 24:
return "sched_yield";
case 25:
return "mremap";
case 26:
return "msync";
case 27:
return "mincore";
case 28:
return "madvise";
case 29:
return "shmget";
case 30:
return "shmat";
case 31:
return "shmctl";
case 32:
return "dup";
case 33:
return "dup2";
case 34:
return "pause";
case 35:
return "nanosleep";
case 36:
return "getitimer";
case 37:
return "alarm";
case 38:
return "setitimer";
case 39:
return "getpid";
case 40:
return "sendfile";
case 41:
return "socket";
case 42:
return "connect";
case 43:
return "accept";
case 44:
return "sendto";
case 45:
return "recvfrom";
case 46:
return "sendmsg";
case 47:
return "recvmsg";
case 48:
return "shutdown";
case 49:
return "bind";
case 50:
return "listen";
case 51:
return "getsockname";
case 52:
return "getpeername";
case 53:
return "socketpair";
case 54:
return "setsockopt";
case 55:
return "getsockopt";
case 56:
return "clone";
case 57:
return "fork";
case 58:
return "vfork";
case 59:
return "execve";
case 60:
return "exit";
case 61:
return "wait4";
case 62:
return "kill";
case 63:
return "uname";
case 64:
return "semget";
case 65:
return "semop";
case 66:
return "semctl";
case 67:
return "shmdt";
case 68:
return "msgget";
case 69:
return "msgsnd";
case 70:
return "msgrcv";
case 71:
return "msgctl";
case 72:
return "fcntl";
case 73:
return "flock";
case 74:
return "fsync";
case 75:
return "fdatasync";
case 76:
return "truncate";
case 77:
return "ftruncate";
case 78:
return "getdents";
case 79:
return "getcwd";
case 80:
return "chdir";
case 81:
return "fchdir";
case 82:
return "rename";
case 83:
return "mkdir";
case 84:
return "rmdir";
case 85:
return "creat";
case 86:
return "link";
case 87:
return "unlink";
case 88:
return "symlink";
case 89:
return "readlink";
case 90:
return "chmod";
case 91:
return "fchmod";
case 92:
return "chown";
case 93:
return "fchown";
case 94:
return "lchown";
case 95:
return "umask";
case 96:
return "gettimeofday";
case 97:
return "getrlimit";
case 98:
return "getrusage";
case 99:
return "sysinfo";
case 100:
return "times";
case 101:
return "ptrace";
case 102:
return "getuid";
case 103:
return "syslog";
case 104:
return "getgid";
case 105:
return "setuid";
case 106:
return "setgid";
case 107:
return "geteuid";
case 108:
return "getegid";
case 109:
return "setpgid";
case 110:
return "getppid";
case 111:
return "getpgrp";
case 112:
return "setsid";
case 113:
return "setreuid";
case 114:
return "setregid";
case 115:
return "getgroups";
case 116:
return "setgroups";
case 117:
return "setresuid";
case 118:
return "getresuid";
case 119:
return "setresgid";
case 120:
return "getresgid";
case 121:
return "getpgid";
case 122:
return "setfsuid";
case 123:
return "setfsgid";
case 124:
return "getsid";
case 125:
return "capget";
case 126:
return "capset";
case 127:
return "rt_sigpending";
case 128:
return "rt_sigtimedwait";
case 129:
return "rt_sigqueueinfo";
case 130:
return "rt_sigsuspend";
case 131:
return "sigaltstack";
case 132:
return "utime";
case 133:
return "mknod";
case 134:
return "uselib";
case 135:
return "personality";
case 136:
return "ustat";
case 137:
return "statfs";
case 138:
return "fstatfs";
case 139:
return "sysfs";
case 140:
return "getpriority";
case 141:
return "setpriority";
case 142:
return "sched_setparam";
case 143:
return "sched_getparam";
case 144:
return "sched_setscheduler";
case 145:
return "sched_getscheduler";
case 146:
return "sched_get_priority_max";
case 147:
return "sched_get_priority_min";
case 148:
return "sched_rr_get_interval";
case 149:
return "mlock";
case 150:
return "munlock";
case 151:
return "mlockall";
case 152:
return "munlockall";
case 153:
return "vhangup";
case 154:
return "modify_ldt";
case 155:
return "pivot_root";
case 156:
return "_sysctl";
case 157:
return "prctl";
case 158:
return "arch_prctl";
case 159:
return "adjtimex";
case 160:
return "setrlimit";
case 161:
return "chroot";
case 162:
return "sync";
case 163:
return "acct";
case 164:
return "settimeofday";
case 165:
return "mount";
case 166:
return "umount2";
case 167:
return "swapon";
case 168:
return "swapoff";
case 169:
return "reboot";
case 170:
return "sethostname";
case 171:
return "setdomainname";
case 172:
return "iopl";
case 173:
return "ioperm";
case 174:
return "create_module";
case 175:
return "init_module";
case 176:
return "delete_module";
case 177:
return "get_kernel_syms";
case 178:
return "query_module";
case 179:
return "quotactl";
case 180:
return "nfsservctl";
case 181:
return "getpmsg";
case 182:
return "putpmsg";
case 183:
return "afs_syscall";
case 184:
return "tuxcall";
case 185:
return "security";
case 186:
return "gettid";
case 187:
return "readahead";
case 188:
return "setxattr";
case 189:
return "lsetxattr";
case 190:
return "fsetxattr";
case 191:
return "getxattr";
case 192:
return "lgetxattr";
case 193:
return "fgetxattr";
case 194:
return "listxattr";
case 195:
return "llistxattr";
case 196:
return "flistxattr";
case 197:
return "removexattr";
case 198:
return "lremovexattr";
case 199:
return "fremovexattr";
case 200:
return "tkill";
case 201:
return "time";
case 202:
return "futex";
case 203:
return "sched_setaffinity";
case 204:
return "sched_getaffinity";
case 205:
return "set_thread_area";
case 206:
return "io_setup";
case 207:
return "io_destroy";
case 208:
return "io_getevents";
case 209:
return "io_submit";
case 210:
return "io_cancel";
case 211:
return "get_thread_area";
case 212:
return "lookup_dcookie";
case 213:
return "epoll_create";
case 214:
return "epoll_ctl_old";
case 215:
return "epoll_wait_old";
case 216:
return "remap_file_pages";
case 217:
return "getdents64";
case 218:
return "set_tid_address";
case 219:
return "restart_syscall";
case 220:
return "semtimedop";
case 221:
return "fadvise64";
case 222:
return "timer_create";
case 223:
return "timer_settime";
case 224:
return "timer_gettime";
case 225:
return "timer_getoverrun";
case 226:
return "timer_delete";
case 227:
return "clock_settime";
case 228:
return "clock_gettime";
case 229:
return "clock_getres";
case 230:
return "clock_nanosleep";
case 231:
return "exit_group";
case 232:
return "epoll_wait";
case 233:
return "epoll_ctl";
case 234:
return "tgkill";
case 235:
return "utimes";
case 236:
return "vserver";
case 237:
return "mbind";
case 238:
return "set_mempolicy";
case 239:
return "get_mempolicy";
case 240:
return "mq_open";
case 241:
return "mq_unlink";
case 242:
return "mq_timedsend";
case 243:
return "mq_timedreceive";
case 244:
return "mq_notify";
case 245:
return "mq_getsetattr";
case 246:
return "kexec_load";
case 247:
return "waitid";
case 248:
return "add_key";
case 249:
return "request_key";
case 250:
return "keyctl";
case 251:
return "ioprio_set";
case 252:
return "ioprio_get";
case 253:
return "inotify_init";
case 254:
return "inotify_add_watch";
case 255:
return "inotify_rm_watch";
case 256:
return "migrate_pages";
case 257:
return "openat";
case 258:
return "mkdirat";
case 259:
return "mknodat";
case 260:
return "fchownat";
case 261:
return "futimesat";
case 262:
return "newfstatat";
case 263:
return "unlinkat";
case 264:
return "renameat";
case 265:
return "linkat";
case 266:
return "symlinkat";
case 267:
return "readlinkat";
case 268:
return "fchmodat";
case 269:
return "faccessat";
case 270:
return "pselect6";
case 271:
return "ppoll";
case 272:
return "unshare";
case 273:
return "set_robust_list";
case 274:
return "get_robust_list";
case 275:
return "splice";
case 276:
return "tee";
case 277:
return "sync_file_range";
case 278:
return "vmsplice";
case 279:
return "move_pages";
case 280:
return "utimensat";
case 281:
return "epoll_pwait";
case 282:
return "signalfd";
case 283:
return "timerfd_create";
case 284:
return "eventfd";
case 285:
return "fallocate";
case 286:
return "timerfd_settime";
case 287:
return "timerfd_gettime";
case 288:
return "accept4";
case 289:
return "signalfd4";
case 290:
return "eventfd2";
case 291:
return "epoll_create1";
case 292:
return "dup3";
case 293:
return "pipe2";
case 294:
return "inotify_init1";
case 295:
return "preadv";
case 296:
return "pwritev";
case 297:
return "rt_tgsigqueueinfo";
case 298:
return "perf_event_open";
case 299:
return "recvmmsg";
case 300:
return "fanotify_init";
case 301:
return "fanotify_mark";
case 302:
return "prlimit64";
case 303:
return "name_to_handle_at";
case 304:
return "open_by_handle_at";
case 305:
return "clock_adjtime";
case 306:
return "syncfs";
case 307:
return "sendmmsg";
case 308:
return "setns";
case 309:
return "getcpu";
case 310:
return "process_vm_readv";
case 311:
return "process_vm_writev";
case 312:
return "kcmp";
case 313:
return "finit_module";
default:
sprintf(buffer, "%ld", id);
return buffer;
}
}