-
Notifications
You must be signed in to change notification settings - Fork 0
/
strace.c
2451 lines (2280 loc) · 55 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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1991, 1992 Paul Kranenburg <[email protected]>
* Copyright (c) 1993 Branko Lankester <[email protected]>
* Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <[email protected]>
* Copyright (c) 1996-1999 Wichert Akkerman <[email protected]>
* All rights reserved.
*
* 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.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*
* $Id: strace.c,v 1.66 2005/06/07 23:21:31 roland Exp $
*/
#include "defs.h"
#include <sys/types.h>
#include <signal.h>
#include <errno.h>
#include <sys/param.h>
#include <fcntl.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <limits.h>
#include <dirent.h>
#if defined(IA64) && defined(LINUX)
# include <asm/ptrace_offsets.h>
#endif
#ifdef USE_PROCFS
#include <poll.h>
#endif
#ifdef SVR4
#include <sys/stropts.h>
#ifdef HAVE_MP_PROCFS
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#endif
#endif
#ifdef HAVE_ANDROID_OS
#define wait4 __wait4
#endif
int debug = 0, followfork = 0, followvfork = 0, interactive = 0;
int rflag = 0, tflag = 0, dtime = 0, cflag = 0;
int iflag = 0, xflag = 0, qflag = 0;
int pflag_seen = 0;
/* Sometimes we want to print only succeeding syscalls. */
int not_failing_only = 0;
char *username = NULL;
uid_t run_uid;
gid_t run_gid;
int acolumn = DEFAULT_ACOLUMN;
int max_strlen = DEFAULT_STRLEN;
char *outfname = NULL;
FILE *outf;
struct tcb **tcbtab;
unsigned int nprocs, tcbtabsize;
char *progname;
extern char **environ;
static int trace P((void));
static void cleanup P((void));
static void interrupt P((int sig));
static sigset_t empty_set, blocked_set;
#ifdef HAVE_SIG_ATOMIC_T
static volatile sig_atomic_t interrupted;
#else /* !HAVE_SIG_ATOMIC_T */
#ifdef __STDC__
static volatile int interrupted;
#else /* !__STDC__ */
static int interrupted;
#endif /* !__STDC__ */
#endif /* !HAVE_SIG_ATOMIC_T */
#ifdef USE_PROCFS
static struct tcb *pfd2tcb P((int pfd));
static void reaper P((int sig));
static void rebuild_pollv P((void));
static struct pollfd *pollv;
#ifndef HAVE_POLLABLE_PROCFS
static void proc_poll_open P((void));
static void proc_poller P((int pfd));
struct proc_pollfd {
int fd;
int revents;
int pid;
};
static int poller_pid;
static int proc_poll_pipe[2] = { -1, -1 };
#endif /* !HAVE_POLLABLE_PROCFS */
#ifdef HAVE_MP_PROCFS
#define POLLWANT POLLWRNORM
#else
#define POLLWANT POLLPRI
#endif
#endif /* USE_PROCFS */
static void
usage(ofp, exitval)
FILE *ofp;
int exitval;
{
fprintf(ofp, "\
usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\
[-p pid] ... [-s strsize] [-u username] [-E var=val] ...\n\
[command [arg ...]]\n\
or: strace -c [-e expr] ... [-O overhead] [-S sortby] [-E var=val] ...\n\
[command [arg ...]]\n\
-c -- count time, calls, and errors for each syscall and report summary\n\
-f -- follow forks, -ff -- with output into separate files\n\
-F -- attempt to follow vforks, -h -- print help message\n\
-i -- print instruction pointer at time of syscall\n\
-q -- suppress messages about attaching, detaching, etc.\n\
-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
-T -- print time spent in each syscall, -V -- print version\n\
-v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\
-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
-a column -- alignment COLUMN for printing syscall results (default %d)\n\
-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
options: trace, abbrev, verbose, raw, signal, read, or write\n\
-o file -- send trace output to FILE instead of stderr\n\
-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
-p pid -- trace process with process id PID, may be repeated\n\
-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
-u username -- run command as username handling setuid and/or setgid\n\
-E var=val -- put var=val in the environment for command\n\
-E var -- remove var from the environment for command\n\
" /* this is broken, so don't document it
-z -- print only succeeding syscalls\n\
*/
, DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
exit(exitval);
}
#ifdef SVR4
#ifdef MIPS
void
foobar()
{
}
#endif /* MIPS */
#endif /* SVR4 */
int
main(argc, argv)
int argc;
char *argv[];
{
extern int optind;
extern char *optarg;
struct tcb *tcp;
int c, pid = 0;
struct sigaction sa;
static char buf[BUFSIZ];
/* Allocate the initial tcbtab. */
tcbtabsize = argc; /* Surely enough for all -p args. */
tcbtab = (struct tcb **) malloc (tcbtabsize * sizeof tcbtab[0]);
tcbtab[0] = (struct tcb *) calloc (tcbtabsize, sizeof *tcbtab[0]);
for (tcp = tcbtab[0]; tcp < &tcbtab[0][tcbtabsize]; ++tcp)
tcbtab[tcp - tcbtab[0]] = &tcbtab[0][tcp - tcbtab[0]];
progname = argv[0];
outf = stderr;
interactive = 1;
qualify("trace=all");
qualify("abbrev=all");
qualify("verbose=all");
qualify("signal=all");
set_sortby(DEFAULT_SORTBY);
set_personality(DEFAULT_PERSONALITY);
while ((c = getopt(argc, argv,
"+cdfFhiqrtTvVxza:e:o:O:p:s:S:u:E:")) != EOF) {
switch (c) {
case 'c':
cflag++;
dtime++;
break;
case 'd':
debug++;
break;
case 'f':
followfork++;
break;
case 'F':
followvfork++;
break;
case 'h':
usage(stdout, 0);
break;
case 'i':
iflag++;
break;
case 'q':
qflag++;
break;
case 'r':
rflag++;
tflag++;
break;
case 't':
tflag++;
break;
case 'T':
dtime++;
break;
case 'x':
xflag++;
break;
case 'v':
qualify("abbrev=none");
break;
case 'V':
printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
exit(0);
break;
case 'z':
not_failing_only = 1;
break;
case 'a':
acolumn = atoi(optarg);
break;
case 'e':
qualify(optarg);
break;
case 'o':
outfname = strdup(optarg);
break;
case 'O':
set_overhead(atoi(optarg));
break;
case 'p':
if ((pid = atoi(optarg)) <= 0) {
fprintf(stderr, "%s: Invalid process id: %s\n",
progname, optarg);
break;
}
if (pid == getpid()) {
fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
break;
}
if ((tcp = alloctcb(pid)) == NULL) {
fprintf(stderr, "%s: out of memory\n",
progname);
exit(1);
}
tcp->flags |= TCB_ATTACHED;
pflag_seen++;
break;
case 's':
max_strlen = atoi(optarg);
if (max_strlen < 0) {
fprintf(stderr,
"%s: invalid -s argument: %s\n",
progname, optarg);
exit(1);
}
break;
case 'S':
set_sortby(optarg);
break;
case 'u':
username = strdup(optarg);
break;
case 'E':
if (putenv(optarg) < 0) {
fprintf(stderr, "%s: out of memory\n",
progname);
exit(1);
}
break;
default:
usage(stderr, 1);
break;
}
}
if (optind == argc && !pflag_seen)
usage(stderr, 1);
/* See if they want to run as another user. */
if (username != NULL) {
struct passwd *pent;
if (getuid() != 0 || geteuid() != 0) {
fprintf(stderr,
"%s: you must be root to use the -u option\n",
progname);
exit(1);
}
if ((pent = getpwnam(username)) == NULL) {
fprintf(stderr, "%s: cannot find user `%s'\n",
progname, optarg);
exit(1);
}
run_uid = pent->pw_uid;
run_gid = pent->pw_gid;
}
else {
run_uid = getuid();
run_gid = getgid();
}
#ifndef SVR4
setreuid(geteuid(), getuid());
#endif
/* Check if they want to redirect the output. */
if (outfname) {
long f;
/* See if they want to pipe the output. */
if (outfname[0] == '|' || outfname[0] == '!') {
/*
* We can't do the <outfname>.PID funny business
* when using popen, so prohibit it.
*/
if (followfork > 1) {
fprintf(stderr, "\
%s: piping the output and -ff are mutually exclusive options\n",
progname);
exit(1);
}
#ifdef HAVE_ANDROID_OS
fprintf(stderr,"output to a pipe not supported on android.\n");
exit(-1);
#else
if ((outf = popen(outfname + 1, "w")) == NULL) {
fprintf(stderr, "%s: can't popen '%s': %s\n",
progname, outfname + 1,
strerror(errno));
exit(1);
}
#endif
}
else if ((outf = fopen(outfname, "w")) == NULL) {
fprintf(stderr, "%s: can't fopen '%s': %s\n",
progname, outfname, strerror(errno));
exit(1);
}
if ((f=fcntl(fileno(outf), F_GETFD)) < 0 ) {
perror("failed to get flags for outputfile");
exit(1);
}
if (fcntl(fileno(outf), F_SETFD, f|FD_CLOEXEC) < 0 ) {
perror("failed to set flags for outputfile");
exit(1);
}
}
#ifndef SVR4
setreuid(geteuid(), getuid());
#endif
if (!outfname || outfname[0] == '|' || outfname[0] == '!')
setvbuf(outf, buf, _IOLBF, BUFSIZ);
if (outfname && optind < argc) {
interactive = 0;
qflag = 1;
}
for (c = 0; c < tcbtabsize; c++) {
tcp = tcbtab[c];
/* Reinitialize the output since it may have changed. */
tcp->outf = outf;
if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
continue;
#ifdef USE_PROCFS
if (proc_open(tcp, 1) < 0) {
fprintf(stderr, "trouble opening proc file\n");
droptcb(tcp);
continue;
}
#else /* !USE_PROCFS */
# ifdef LINUX
if (tcp->flags & TCB_CLONE_THREAD)
continue;
if (followfork) {
char procdir[MAXPATHLEN];
DIR *dir;
sprintf(procdir, "/proc/%d/task", tcp->pid);
dir = opendir(procdir);
if (dir != NULL) {
unsigned int ntid = 0, nerr = 0;
struct dirent *de;
int tid;
while ((de = readdir(dir)) != NULL) {
if (de->d_fileno == 0 ||
de->d_name[0] == '.')
continue;
tid = atoi(de->d_name);
if (tid <= 0)
continue;
++ntid;
if (ptrace(PTRACE_ATTACH, tid,
(char *) 1, 0) < 0)
++nerr;
else if (tid != tcbtab[c]->pid) {
if (nprocs == tcbtabsize &&
expand_tcbtab())
tcp = NULL;
else
tcp = alloctcb(tid);
if (tcp == NULL) {
fprintf(stderr, "%s: out of memory\n",
progname);
exit(1);
}
tcp->flags |= TCB_ATTACHED|TCB_CLONE_THREAD|TCB_CLONE_DETACHED|TCB_FOLLOWFORK;
tcbtab[c]->nchildren++;
tcbtab[c]->nclone_threads++;
tcbtab[c]->nclone_detached++;
tcp->parent = tcbtab[c];
}
}
closedir(dir);
if (nerr == ntid) {
perror("attach: ptrace(PTRACE_ATTACH, ...)");
droptcb(tcp);
continue;
}
if (!qflag) {
ntid -= nerr;
if (ntid > 1)
fprintf(stderr, "\
Process %u attached with %u threads - interrupt to quit\n",
tcp->pid, ntid);
else
fprintf(stderr, "\
Process %u attached - interrupt to quit\n",
tcp->pid);
}
continue;
}
}
# endif
if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
perror("attach: ptrace(PTRACE_ATTACH, ...)");
droptcb(tcp);
continue;
}
#endif /* !USE_PROCFS */
if (!qflag)
fprintf(stderr,
"Process %u attached - interrupt to quit\n",
tcp->pid);
}
if (!pflag_seen) {
struct stat statbuf;
char *filename;
char pathname[MAXPATHLEN];
filename = argv[optind];
if (strchr(filename, '/')) {
if (strlen(filename) > sizeof pathname - 1) {
errno = ENAMETOOLONG;
perror("strace: exec");
exit(1);
}
strcpy(pathname, filename);
}
#ifdef USE_DEBUGGING_EXEC
/*
* Debuggers customarily check the current directory
* first regardless of the path but doing that gives
* security geeks a panic attack.
*/
else if (stat(filename, &statbuf) == 0)
strcpy(pathname, filename);
#endif /* USE_DEBUGGING_EXEC */
else {
char *path;
int m, n, len;
for (path = getenv("PATH"); path && *path; path += m) {
if (strchr(path, ':')) {
n = strchr(path, ':') - path;
m = n + 1;
}
else
m = n = strlen(path);
if (n == 0) {
getcwd(pathname, MAXPATHLEN);
len = strlen(pathname);
}
else if (n > sizeof pathname - 1)
continue;
else {
strncpy(pathname, path, n);
len = n;
}
if (len && pathname[len - 1] != '/')
pathname[len++] = '/';
strcpy(pathname + len, filename);
if (stat(pathname, &statbuf) == 0 &&
/* Accept only regular files
with some execute bits set.
XXX not perfect, might still fail */
S_ISREG(statbuf.st_mode) &&
(statbuf.st_mode & 0111))
break;
}
}
if (stat(pathname, &statbuf) < 0) {
fprintf(stderr, "%s: %s: command not found\n",
progname, filename);
exit(1);
}
switch (pid = fork()) {
case -1:
perror("strace: fork");
cleanup();
exit(1);
break;
case 0: {
#ifdef USE_PROCFS
if (outf != stderr) close (fileno (outf));
#ifdef MIPS
/* Kludge for SGI, see proc_open for details. */
sa.sa_handler = foobar;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
#endif /* MIPS */
#ifndef FREEBSD
pause();
#else /* FREEBSD */
kill(getpid(), SIGSTOP); /* stop HERE */
#endif /* FREEBSD */
#else /* !USE_PROCFS */
if (outf!=stderr)
close(fileno (outf));
if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
perror("strace: ptrace(PTRACE_TRACEME, ...)");
return -1;
}
if (debug)
kill(getpid(), SIGSTOP);
if (username != NULL || geteuid() == 0) {
uid_t run_euid = run_uid;
gid_t run_egid = run_gid;
if (statbuf.st_mode & S_ISUID)
run_euid = statbuf.st_uid;
if (statbuf.st_mode & S_ISGID)
run_egid = statbuf.st_gid;
/*
* It is important to set groups before we
* lose privileges on setuid.
*/
if (username != NULL) {
#ifndef HAVE_ANDROID_OS
if (initgroups(username, run_gid) < 0) {
perror("initgroups");
exit(1);
}
#endif /* HAVE_ANDROID_OS */
if (setregid(run_gid, run_egid) < 0) {
perror("setregid");
exit(1);
}
if (setreuid(run_uid, run_euid) < 0) {
perror("setreuid");
exit(1);
}
}
}
else
setreuid(run_uid, run_uid);
/*
* Induce an immediate stop so that the parent
* will resume us with PTRACE_SYSCALL and display
* this execve call normally.
*/
kill(getpid(), SIGSTOP);
#endif /* !USE_PROCFS */
execv(pathname, &argv[optind]);
perror("strace: exec");
_exit(1);
break;
}
default:
if ((tcp = alloctcb(pid)) == NULL) {
fprintf(stderr, "tcb table full\n");
cleanup();
exit(1);
}
#ifdef USE_PROCFS
if (proc_open(tcp, 0) < 0) {
fprintf(stderr, "trouble opening proc file\n");
cleanup();
exit(1);
}
#endif /* USE_PROCFS */
break;
}
}
sigemptyset(&empty_set);
sigemptyset(&blocked_set);
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGTTOU, &sa, NULL);
sigaction(SIGTTIN, &sa, NULL);
if (interactive) {
sigaddset(&blocked_set, SIGHUP);
sigaddset(&blocked_set, SIGINT);
sigaddset(&blocked_set, SIGQUIT);
sigaddset(&blocked_set, SIGPIPE);
sigaddset(&blocked_set, SIGTERM);
sa.sa_handler = interrupt;
#ifdef SUNOS4
/* POSIX signals on sunos4.1 are a little broken. */
sa.sa_flags = SA_INTERRUPT;
#endif /* SUNOS4 */
}
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGPIPE, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
#ifdef USE_PROCFS
sa.sa_handler = reaper;
sigaction(SIGCHLD, &sa, NULL);
#else
/* Make sure SIGCHLD has the default action so that waitpid
definitely works without losing track of children. The user
should not have given us a bogus state to inherit, but he might
have. Arguably we should detect SIG_IGN here and pass it on
to children, but probably noone really needs that. */
sa.sa_handler = SIG_DFL;
sigaction(SIGCHLD, &sa, NULL);
#endif /* USE_PROCFS */
if (trace() < 0)
exit(1);
cleanup();
exit(0);
}
void
newoutf(tcp)
struct tcb *tcp;
{
char name[MAXPATHLEN];
FILE *fp;
if (outfname && followfork > 1) {
sprintf(name, "%s.%u", outfname, tcp->pid);
#ifndef SVR4
setreuid(geteuid(), getuid());
#endif
fp = fopen(name, "w");
#ifndef SVR4
setreuid(geteuid(), getuid());
#endif
if (fp == NULL) {
perror("fopen");
return;
}
tcp->outf = fp;
}
return;
}
int
expand_tcbtab()
{
/* Allocate some more TCBs and expand the table.
We don't want to relocate the TCBs because our
callers have pointers and it would be a pain.
So tcbtab is a table of pointers. Since we never
free the TCBs, we allocate a single chunk of many. */
struct tcb **newtab = (struct tcb **)
realloc(tcbtab, 2 * tcbtabsize * sizeof tcbtab[0]);
struct tcb *newtcbs = (struct tcb *) calloc(tcbtabsize,
sizeof *newtcbs);
int i;
if (newtab == NULL || newtcbs == NULL) {
if (newtab != NULL)
free(newtab);
return 1;
}
for (i = tcbtabsize; i < 2 * tcbtabsize; ++i)
newtab[i] = &newtcbs[i - tcbtabsize];
tcbtabsize *= 2;
tcbtab = newtab;
return 0;
}
struct tcb *
alloctcb(pid)
int pid;
{
int i;
struct tcb *tcp;
for (i = 0; i < tcbtabsize; i++) {
tcp = tcbtab[i];
if ((tcp->flags & TCB_INUSE) == 0) {
tcp->pid = pid;
tcp->parent = NULL;
tcp->nchildren = 0;
tcp->nzombies = 0;
#ifdef TCB_CLONE_THREAD
tcp->nclone_threads = tcp->nclone_detached = 0;
tcp->nclone_waiting = 0;
#endif
tcp->flags = TCB_INUSE | TCB_STARTUP;
tcp->outf = outf; /* Initialise to current out file */
tcp->stime.tv_sec = 0;
tcp->stime.tv_usec = 0;
tcp->pfd = -1;
nprocs++;
return tcp;
}
}
return NULL;
}
#ifdef USE_PROCFS
int
proc_open(tcp, attaching)
struct tcb *tcp;
int attaching;
{
char proc[32];
long arg;
#ifdef SVR4
int i;
sysset_t syscalls;
sigset_t signals;
fltset_t faults;
#endif
#ifndef HAVE_POLLABLE_PROCFS
static int last_pfd;
#endif
#ifdef HAVE_MP_PROCFS
/* Open the process pseudo-files in /proc. */
sprintf(proc, "/proc/%d/ctl", tcp->pid);
if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
perror("strace: open(\"/proc/...\", ...)");
return -1;
}
if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
perror("F_GETFD");
return -1;
}
if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
perror("F_SETFD");
return -1;
}
sprintf(proc, "/proc/%d/status", tcp->pid);
if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
perror("strace: open(\"/proc/...\", ...)");
return -1;
}
if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) {
perror("F_GETFD");
return -1;
}
if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) {
perror("F_SETFD");
return -1;
}
sprintf(proc, "/proc/%d/as", tcp->pid);
if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
perror("strace: open(\"/proc/...\", ...)");
return -1;
}
if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) {
perror("F_GETFD");
return -1;
}
if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) {
perror("F_SETFD");
return -1;
}
#else
/* Open the process pseudo-file in /proc. */
#ifndef FREEBSD
sprintf(proc, "/proc/%d", tcp->pid);
if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) {
#else /* FREEBSD */
sprintf(proc, "/proc/%d/mem", tcp->pid);
if ((tcp->pfd = open(proc, O_RDWR)) < 0) {
#endif /* FREEBSD */
perror("strace: open(\"/proc/...\", ...)");
return -1;
}
if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
perror("F_GETFD");
return -1;
}
if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
perror("F_SETFD");
return -1;
}
#endif
#ifdef FREEBSD
sprintf(proc, "/proc/%d/regs", tcp->pid);
if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) {
perror("strace: open(\"/proc/.../regs\", ...)");
return -1;
}
if (cflag) {
sprintf(proc, "/proc/%d/status", tcp->pid);
if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) {
perror("strace: open(\"/proc/.../status\", ...)");
return -1;
}
} else
tcp->pfd_status = -1;
#endif /* FREEBSD */
rebuild_pollv();
if (!attaching) {
/*
* Wait for the child to pause. Because of a race
* condition we have to poll for the event.
*/
for (;;) {
if (IOCTL_STATUS (tcp) < 0) {
perror("strace: PIOCSTATUS");
return -1;
}
if (tcp->status.PR_FLAGS & PR_ASLEEP)
break;
}
}
#ifndef FREEBSD
/* Stop the process so that we own the stop. */
if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) {
perror("strace: PIOCSTOP");
return -1;
}
#endif
#ifdef PIOCSET
/* Set Run-on-Last-Close. */
arg = PR_RLC;
if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
perror("PIOCSET PR_RLC");
return -1;
}
/* Set or Reset Inherit-on-Fork. */
arg = PR_FORK;
if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
perror("PIOC{SET,RESET} PR_FORK");
return -1;
}
#else /* !PIOCSET */
#ifndef FREEBSD
if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
perror("PIOCSRLC");
return -1;
}
if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
perror("PIOC{S,R}FORK");
return -1;
}
#else /* FREEBSD */
/* just unset the PF_LINGER flag for the Run-on-Last-Close. */
if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) {
perror("PIOCGFL");
return -1;
}
arg &= ~PF_LINGER;
if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) {
perror("PIOCSFL");
return -1;
}
#endif /* FREEBSD */
#endif /* !PIOCSET */
#ifndef FREEBSD
/* Enable all syscall entries we care about. */
premptyset(&syscalls);
for (i = 1; i < MAX_QUALS; ++i) {
if (i > (sizeof syscalls) * CHAR_BIT) break;
if (qual_flags [i] & QUAL_TRACE) praddset (&syscalls, i);
}
praddset (&syscalls, SYS_execve);
if (followfork) {
praddset (&syscalls, SYS_fork);
#ifdef SYS_forkall
praddset (&syscalls, SYS_forkall);
#endif
#ifdef SYS_fork1
praddset (&syscalls, SYS_fork1);
#endif
#ifdef SYS_rfork1
praddset (&syscalls, SYS_rfork1);
#endif
#ifdef SYS_rforkall
praddset (&syscalls, SYS_rforkall);
#endif
}
if (IOCTL(tcp->pfd, PIOCSENTRY, &syscalls) < 0) {
perror("PIOCSENTRY");
return -1;
}
/* Enable the syscall exits. */
if (IOCTL(tcp->pfd, PIOCSEXIT, &syscalls) < 0) {
perror("PIOSEXIT");
return -1;
}
/* Enable signals we care about. */
premptyset(&signals);
for (i = 1; i < MAX_QUALS; ++i) {
if (i > (sizeof signals) * CHAR_BIT) break;
if (qual_flags [i] & QUAL_SIGNAL) praddset (&signals, i);
}
if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
perror("PIOCSTRACE");
return -1;
}
/* Enable faults we care about */
premptyset(&faults);
for (i = 1; i < MAX_QUALS; ++i) {
if (i > (sizeof faults) * CHAR_BIT) break;
if (qual_flags [i] & QUAL_FAULT) praddset (&faults, i);
}
if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
perror("PIOCSFAULT");
return -1;
}
#else /* FREEBSD */
/* set events flags. */
arg = S_SIG | S_SCE | S_SCX ;
if(ioctl(tcp->pfd, PIOCBIS, arg) < 0) {
perror("PIOCBIS");
return -1;
}
#endif /* FREEBSD */
if (!attaching) {
#ifdef MIPS
/*
* The SGI PRSABORT doesn't work for pause() so
* we send it a caught signal to wake it up.
*/
kill(tcp->pid, SIGINT);
#else /* !MIPS */
#ifdef PRSABORT
/* The child is in a pause(), abort it. */
arg = PRSABORT;
if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
perror("PIOCRUN");
return -1;
}