]> granicus.if.org Git - strace/blob - strace.c
2006-12-10 Dmitry V. Levin <ldv@altlinux.org>
[strace] / strace.c
1 /*
2  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  *      $Id$
31  */
32
33 #include "defs.h"
34
35 #include <sys/types.h>
36 #include <signal.h>
37 #include <errno.h>
38 #include <sys/param.h>
39 #include <fcntl.h>
40 #include <sys/resource.h>
41 #include <sys/wait.h>
42 #include <sys/stat.h>
43 #include <pwd.h>
44 #include <grp.h>
45 #include <string.h>
46 #include <limits.h>
47 #include <dirent.h>
48
49 #if defined(IA64) && defined(LINUX)
50 # include <asm/ptrace_offsets.h>
51 #endif
52
53 #ifdef USE_PROCFS
54 #include <poll.h>
55 #endif
56
57 #ifdef SVR4
58 #include <sys/stropts.h>
59 #ifdef HAVE_MP_PROCFS
60 #ifdef HAVE_SYS_UIO_H
61 #include <sys/uio.h>
62 #endif
63 #endif
64 #endif
65
66 int debug = 0, followfork = 0, followvfork = 0;
67 int dtime = 0, cflag = 0, xflag = 0, qflag = 0;
68 static int iflag = 0, interactive = 0, pflag_seen = 0, rflag = 0, tflag = 0;
69
70 /* Sometimes we want to print only succeeding syscalls. */
71 int not_failing_only = 0;
72
73 static char *username = NULL;
74 uid_t run_uid;
75 gid_t run_gid;
76
77 int acolumn = DEFAULT_ACOLUMN;
78 int max_strlen = DEFAULT_STRLEN;
79 static char *outfname = NULL;
80 FILE *outf;
81 struct tcb **tcbtab;
82 unsigned int nprocs, tcbtabsize;
83 char *progname;
84 extern char **environ;
85
86 static int trace P((void));
87 static void cleanup P((void));
88 static void interrupt P((int sig));
89 static sigset_t empty_set, blocked_set;
90
91 #ifdef HAVE_SIG_ATOMIC_T
92 static volatile sig_atomic_t interrupted;
93 #else /* !HAVE_SIG_ATOMIC_T */
94 #ifdef __STDC__
95 static volatile int interrupted;
96 #else /* !__STDC__ */
97 static int interrupted;
98 #endif /* !__STDC__ */
99 #endif /* !HAVE_SIG_ATOMIC_T */
100
101 #ifdef USE_PROCFS
102
103 static struct tcb *pfd2tcb P((int pfd));
104 static void reaper P((int sig));
105 static void rebuild_pollv P((void));
106 static struct pollfd *pollv;
107
108 #ifndef HAVE_POLLABLE_PROCFS
109
110 static void proc_poll_open P((void));
111 static void proc_poller P((int pfd));
112
113 struct proc_pollfd {
114         int fd;
115         int revents;
116         int pid;
117 };
118
119 static int poller_pid;
120 static int proc_poll_pipe[2] = { -1, -1 };
121
122 #endif /* !HAVE_POLLABLE_PROCFS */
123
124 #ifdef HAVE_MP_PROCFS
125 #define POLLWANT        POLLWRNORM
126 #else
127 #define POLLWANT        POLLPRI
128 #endif
129 #endif /* USE_PROCFS */
130
131 static void
132 usage(ofp, exitval)
133 FILE *ofp;
134 int exitval;
135 {
136         fprintf(ofp, "\
137 usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\
138               [-p pid] ... [-s strsize] [-u username] [-E var=val] ...\n\
139               [command [arg ...]]\n\
140    or: strace -c [-e expr] ... [-O overhead] [-S sortby] [-E var=val] ...\n\
141               [command [arg ...]]\n\
142 -c -- count time, calls, and errors for each syscall and report summary\n\
143 -f -- follow forks, -ff -- with output into separate files\n\
144 -F -- attempt to follow vforks, -h -- print help message\n\
145 -i -- print instruction pointer at time of syscall\n\
146 -q -- suppress messages about attaching, detaching, etc.\n\
147 -r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
148 -T -- print time spent in each syscall, -V -- print version\n\
149 -v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\
150 -x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
151 -a column -- alignment COLUMN for printing syscall results (default %d)\n\
152 -e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
153    options: trace, abbrev, verbose, raw, signal, read, or write\n\
154 -o file -- send trace output to FILE instead of stderr\n\
155 -O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
156 -p pid -- trace process with process id PID, may be repeated\n\
157 -s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
158 -S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
159 -u username -- run command as username handling setuid and/or setgid\n\
160 -E var=val -- put var=val in the environment for command\n\
161 -E var -- remove var from the environment for command\n\
162 " /* this is broken, so don't document it
163 -z -- print only succeeding syscalls\n\
164   */
165 , DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
166         exit(exitval);
167 }
168
169 #ifdef SVR4
170 #ifdef MIPS
171 void
172 foobar()
173 {
174 }
175 #endif /* MIPS */
176 #endif /* SVR4 */
177
178 int
179 main(argc, argv)
180 int argc;
181 char *argv[];
182 {
183         extern int optind;
184         extern char *optarg;
185         struct tcb *tcp;
186         int c, pid = 0;
187         struct sigaction sa;
188
189         static char buf[BUFSIZ];
190
191         /* Allocate the initial tcbtab.  */
192         tcbtabsize = argc;      /* Surely enough for all -p args.  */
193         tcbtab = (struct tcb **) malloc (tcbtabsize * sizeof tcbtab[0]);
194         tcbtab[0] = (struct tcb *) calloc (tcbtabsize, sizeof *tcbtab[0]);
195         for (tcp = tcbtab[0]; tcp < &tcbtab[0][tcbtabsize]; ++tcp)
196                 tcbtab[tcp - tcbtab[0]] = &tcbtab[0][tcp - tcbtab[0]];
197
198         progname = argv[0];
199         outf = stderr;
200         interactive = 1;
201         set_sortby(DEFAULT_SORTBY);
202         set_personality(DEFAULT_PERSONALITY);
203         qualify("trace=all");
204         qualify("abbrev=all");
205         qualify("verbose=all");
206         qualify("signal=all");
207         while ((c = getopt(argc, argv,
208                 "+cdfFhiqrtTvVxza:e:o:O:p:s:S:u:E:")) != EOF) {
209                 switch (c) {
210                 case 'c':
211                         cflag++;
212                         dtime++;
213                         break;
214                 case 'd':
215                         debug++;
216                         break;
217                 case 'f':
218                         followfork++;
219                         break;
220                 case 'F':
221                         followvfork++;
222                         break;
223                 case 'h':
224                         usage(stdout, 0);
225                         break;
226                 case 'i':
227                         iflag++;
228                         break;
229                 case 'q':
230                         qflag++;
231                         break;
232                 case 'r':
233                         rflag++;
234                         tflag++;
235                         break;
236                 case 't':
237                         tflag++;
238                         break;
239                 case 'T':
240                         dtime++;
241                         break;
242                 case 'x':
243                         xflag++;
244                         break;
245                 case 'v':
246                         qualify("abbrev=none");
247                         break;
248                 case 'V':
249                         printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
250                         exit(0);
251                         break;
252                 case 'z':
253                         not_failing_only = 1;
254                         break;
255                 case 'a':
256                         acolumn = atoi(optarg);
257                         break;
258                 case 'e':
259                         qualify(optarg);
260                         break;
261                 case 'o':
262                         outfname = strdup(optarg);
263                         break;
264                 case 'O':
265                         set_overhead(atoi(optarg));
266                         break;
267                 case 'p':
268                         if ((pid = atoi(optarg)) <= 0) {
269                                 fprintf(stderr, "%s: Invalid process id: %s\n",
270                                         progname, optarg);
271                                 break;
272                         }
273                         if (pid == getpid()) {
274                                 fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
275                                 break;
276                         }
277                         if ((tcp = alloctcb(pid)) == NULL) {
278                                 fprintf(stderr, "%s: out of memory\n",
279                                         progname);
280                                 exit(1);
281                         }
282                         tcp->flags |= TCB_ATTACHED;
283                         pflag_seen++;
284                         break;
285                 case 's':
286                         max_strlen = atoi(optarg);
287                         if (max_strlen < 0) {
288                                 fprintf(stderr,
289                                         "%s: invalid -s argument: %s\n",
290                                         progname, optarg);
291                                 exit(1);
292                         }
293                         break;
294                 case 'S':
295                         set_sortby(optarg);
296                         break;
297                 case 'u':
298                         username = strdup(optarg);
299                         break;
300                 case 'E':
301                         if (putenv(optarg) < 0) {
302                                 fprintf(stderr, "%s: out of memory\n",
303                                         progname);
304                                 exit(1);
305                         }
306                         break;
307                 default:
308                         usage(stderr, 1);
309                         break;
310                 }
311         }
312
313         if ((optind == argc) == !pflag_seen)
314                 usage(stderr, 1);
315
316         if (followfork > 1 && cflag) {
317                 fprintf(stderr,
318                         "%s: -c and -ff are mutually exclusive options\n",
319                         progname);
320                 exit(1);
321         }
322
323         /* See if they want to run as another user. */
324         if (username != NULL) {
325                 struct passwd *pent;
326
327                 if (getuid() != 0 || geteuid() != 0) {
328                         fprintf(stderr,
329                                 "%s: you must be root to use the -u option\n",
330                                 progname);
331                         exit(1);
332                 }
333                 if ((pent = getpwnam(username)) == NULL) {
334                         fprintf(stderr, "%s: cannot find user `%s'\n",
335                                 progname, optarg);
336                         exit(1);
337                 }
338                 run_uid = pent->pw_uid;
339                 run_gid = pent->pw_gid;
340         }
341         else {
342                 run_uid = getuid();
343                 run_gid = getgid();
344         }
345
346 #ifndef SVR4
347         setreuid(geteuid(), getuid());
348 #endif
349
350         /* Check if they want to redirect the output. */
351         if (outfname) {
352                 long f;
353
354                 /* See if they want to pipe the output. */
355                 if (outfname[0] == '|' || outfname[0] == '!') {
356                         /*
357                          * We can't do the <outfname>.PID funny business
358                          * when using popen, so prohibit it.
359                          */
360                         if (followfork > 1) {
361                                 fprintf(stderr, "\
362 %s: piping the output and -ff are mutually exclusive options\n",
363                                         progname);
364                                 exit(1);
365                         }
366
367                         if ((outf = popen(outfname + 1, "w")) == NULL) {
368                                 fprintf(stderr, "%s: can't popen '%s': %s\n",
369                                         progname, outfname + 1,
370                                         strerror(errno));
371                                 exit(1);
372                         }
373                 }
374                 else if ((outf = fopen(outfname, "w")) == NULL) {
375                         fprintf(stderr, "%s: can't fopen '%s': %s\n",
376                                 progname, outfname, strerror(errno));
377                         exit(1);
378                 }
379
380                 if ((f=fcntl(fileno(outf), F_GETFD)) < 0 ) {
381                         perror("failed to get flags for outputfile");
382                         exit(1);
383                 }
384
385                 if (fcntl(fileno(outf), F_SETFD, f|FD_CLOEXEC) < 0 ) {
386                         perror("failed to set flags for outputfile");
387                         exit(1);
388                 }
389         }
390
391 #ifndef SVR4
392         setreuid(geteuid(), getuid());
393 #endif
394
395         if (!outfname || outfname[0] == '|' || outfname[0] == '!')
396                 setvbuf(outf, buf, _IOLBF, BUFSIZ);
397         if (outfname && optind < argc) {
398                 interactive = 0;
399                 qflag = 1;
400         }
401
402         for (c = 0; c < tcbtabsize; c++) {
403                 tcp = tcbtab[c];
404                 /* Reinitialize the output since it may have changed. */
405                 tcp->outf = outf;
406                 if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
407                         continue;
408 #ifdef USE_PROCFS
409                 if (proc_open(tcp, 1) < 0) {
410                         fprintf(stderr, "trouble opening proc file\n");
411                         droptcb(tcp);
412                         continue;
413                 }
414 #else /* !USE_PROCFS */
415 # ifdef LINUX
416                 if (tcp->flags & TCB_CLONE_THREAD)
417                         continue;
418                 if (followfork) {
419                         char procdir[MAXPATHLEN];
420                         DIR *dir;
421                         sprintf(procdir, "/proc/%d/task", tcp->pid);
422                         dir = opendir(procdir);
423                         if (dir != NULL) {
424                                 unsigned int ntid = 0, nerr = 0;
425                                 struct dirent *de;
426                                 int tid;
427                                 while ((de = readdir(dir)) != NULL) {
428                                         if (de->d_fileno == 0 ||
429                                             de->d_name[0] == '.')
430                                                 continue;
431                                         tid = atoi(de->d_name);
432                                         if (tid <= 0)
433                                                 continue;
434                                         ++ntid;
435                                         if (ptrace(PTRACE_ATTACH, tid,
436                                                    (char *) 1, 0) < 0)
437                                                 ++nerr;
438                                         else if (tid != tcbtab[c]->pid) {
439                                                 if (nprocs == tcbtabsize &&
440                                                     expand_tcbtab())
441                                                         tcp = NULL;
442                                                 else
443                                                         tcp = alloctcb(tid);
444                                                 if (tcp == NULL)
445                                                         exit(1);
446                                                 tcp->flags |= TCB_ATTACHED|TCB_CLONE_THREAD|TCB_CLONE_DETACHED|TCB_FOLLOWFORK;
447                                                 tcbtab[c]->nchildren++;
448                                                 tcbtab[c]->nclone_threads++;
449                                                 tcbtab[c]->nclone_detached++;
450                                                 tcp->parent = tcbtab[c];
451                                         }
452                                 }
453                                 closedir(dir);
454                                 if (nerr == ntid) {
455                                         perror("attach: ptrace(PTRACE_ATTACH, ...)");
456                                         droptcb(tcp);
457                                         continue;
458                                 }
459                                 if (!qflag) {
460                                         ntid -= nerr;
461                                         if (ntid > 1)
462                                                 fprintf(stderr, "\
463 Process %u attached with %u threads - interrupt to quit\n",
464                                                         tcp->pid, ntid);
465                                         else
466                                                 fprintf(stderr, "\
467 Process %u attached - interrupt to quit\n",
468                                                         tcp->pid);
469                                 }
470                                 continue;
471                         }
472                 }
473 # endif
474                 if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
475                         perror("attach: ptrace(PTRACE_ATTACH, ...)");
476                         droptcb(tcp);
477                         continue;
478                 }
479 #endif /* !USE_PROCFS */
480                 if (!qflag)
481                         fprintf(stderr,
482                                 "Process %u attached - interrupt to quit\n",
483                                 tcp->pid);
484         }
485
486         if (!pflag_seen) {
487                 struct stat statbuf;
488                 char *filename;
489                 char pathname[MAXPATHLEN];
490
491                 filename = argv[optind];
492                 if (strchr(filename, '/')) {
493                         if (strlen(filename) > sizeof pathname - 1) {
494                                 errno = ENAMETOOLONG;
495                                 perror("strace: exec");
496                                 exit(1);
497                         }
498                         strcpy(pathname, filename);
499                 }
500 #ifdef USE_DEBUGGING_EXEC
501                 /*
502                  * Debuggers customarily check the current directory
503                  * first regardless of the path but doing that gives
504                  * security geeks a panic attack.
505                  */
506                 else if (stat(filename, &statbuf) == 0)
507                         strcpy(pathname, filename);
508 #endif /* USE_DEBUGGING_EXEC */
509                 else {
510                         char *path;
511                         int m, n, len;
512
513                         for (path = getenv("PATH"); path && *path; path += m) {
514                                 if (strchr(path, ':')) {
515                                         n = strchr(path, ':') - path;
516                                         m = n + 1;
517                                 }
518                                 else
519                                         m = n = strlen(path);
520                                 if (n == 0) {
521                                         if (!getcwd(pathname, MAXPATHLEN))
522                                                 continue;
523                                         len = strlen(pathname);
524                                 }
525                                 else if (n > sizeof pathname - 1)
526                                         continue;
527                                 else {
528                                         strncpy(pathname, path, n);
529                                         len = n;
530                                 }
531                                 if (len && pathname[len - 1] != '/')
532                                         pathname[len++] = '/';
533                                 strcpy(pathname + len, filename);
534                                 if (stat(pathname, &statbuf) == 0 &&
535                                     /* Accept only regular files
536                                        with some execute bits set.
537                                        XXX not perfect, might still fail */
538                                     S_ISREG(statbuf.st_mode) &&
539                                     (statbuf.st_mode & 0111))
540                                         break;
541                         }
542                 }
543                 if (stat(pathname, &statbuf) < 0) {
544                         fprintf(stderr, "%s: %s: command not found\n",
545                                 progname, filename);
546                         exit(1);
547                 }
548                 switch (pid = fork()) {
549                 case -1:
550                         perror("strace: fork");
551                         cleanup();
552                         exit(1);
553                         break;
554                 case 0: {
555 #ifdef USE_PROCFS
556                         if (outf != stderr) close (fileno (outf));
557 #ifdef MIPS
558                         /* Kludge for SGI, see proc_open for details. */
559                         sa.sa_handler = foobar;
560                         sa.sa_flags = 0;
561                         sigemptyset(&sa.sa_mask);
562                         sigaction(SIGINT, &sa, NULL);
563 #endif /* MIPS */
564 #ifndef FREEBSD
565                         pause();
566 #else /* FREEBSD */
567                         kill(getpid(), SIGSTOP); /* stop HERE */
568 #endif /* FREEBSD */
569 #else /* !USE_PROCFS */
570                         if (outf!=stderr)
571                                 close(fileno (outf));
572
573                         if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
574                                 perror("strace: ptrace(PTRACE_TRACEME, ...)");
575                                 return -1;
576                         }
577                         if (debug)
578                                 kill(getpid(), SIGSTOP);
579
580                         if (username != NULL || geteuid() == 0) {
581                                 uid_t run_euid = run_uid;
582                                 gid_t run_egid = run_gid;
583
584                                 if (statbuf.st_mode & S_ISUID)
585                                         run_euid = statbuf.st_uid;
586                                 if (statbuf.st_mode & S_ISGID)
587                                         run_egid = statbuf.st_gid;
588
589                                 /*
590                                  * It is important to set groups before we
591                                  * lose privileges on setuid.
592                                  */
593                                 if (username != NULL) {
594                                         if (initgroups(username, run_gid) < 0) {
595                                                 perror("initgroups");
596                                                 exit(1);
597                                         }
598                                         if (setregid(run_gid, run_egid) < 0) {
599                                                 perror("setregid");
600                                                 exit(1);
601                                         }
602                                         if (setreuid(run_uid, run_euid) < 0) {
603                                                 perror("setreuid");
604                                                 exit(1);
605                                         }
606                                 }
607                         }
608                         else
609                                 setreuid(run_uid, run_uid);
610
611                         /*
612                          * Induce an immediate stop so that the parent
613                          * will resume us with PTRACE_SYSCALL and display
614                          * this execve call normally.
615                          */
616                         kill(getpid(), SIGSTOP);
617 #endif /* !USE_PROCFS */
618
619                         execv(pathname, &argv[optind]);
620                         perror("strace: exec");
621                         _exit(1);
622                         break;
623                 }
624                 default:
625                         if ((tcp = alloctcb(pid)) == NULL) {
626                                 cleanup();
627                                 exit(1);
628                         }
629 #ifdef USE_PROCFS
630                         if (proc_open(tcp, 0) < 0) {
631                                 fprintf(stderr, "trouble opening proc file\n");
632                                 cleanup();
633                                 exit(1);
634                         }
635 #endif /* USE_PROCFS */
636                         break;
637                 }
638         }
639
640         sigemptyset(&empty_set);
641         sigemptyset(&blocked_set);
642         sa.sa_handler = SIG_IGN;
643         sigemptyset(&sa.sa_mask);
644         sa.sa_flags = 0;
645         sigaction(SIGTTOU, &sa, NULL);
646         sigaction(SIGTTIN, &sa, NULL);
647         if (interactive) {
648                 sigaddset(&blocked_set, SIGHUP);
649                 sigaddset(&blocked_set, SIGINT);
650                 sigaddset(&blocked_set, SIGQUIT);
651                 sigaddset(&blocked_set, SIGPIPE);
652                 sigaddset(&blocked_set, SIGTERM);
653                 sa.sa_handler = interrupt;
654 #ifdef SUNOS4
655                 /* POSIX signals on sunos4.1 are a little broken. */
656                 sa.sa_flags = SA_INTERRUPT;
657 #endif /* SUNOS4 */
658         }
659         sigaction(SIGHUP, &sa, NULL);
660         sigaction(SIGINT, &sa, NULL);
661         sigaction(SIGQUIT, &sa, NULL);
662         sigaction(SIGPIPE, &sa, NULL);
663         sigaction(SIGTERM, &sa, NULL);
664 #ifdef USE_PROCFS
665         sa.sa_handler = reaper;
666         sigaction(SIGCHLD, &sa, NULL);
667 #else
668         /* Make sure SIGCHLD has the default action so that waitpid
669            definitely works without losing track of children.  The user
670            should not have given us a bogus state to inherit, but he might
671            have.  Arguably we should detect SIG_IGN here and pass it on
672            to children, but probably noone really needs that.  */
673         sa.sa_handler = SIG_DFL;
674         sigaction(SIGCHLD, &sa, NULL);
675 #endif /* USE_PROCFS */
676
677         if (trace() < 0)
678                 exit(1);
679         cleanup();
680         exit(0);
681 }
682
683 void
684 newoutf(tcp)
685 struct tcb *tcp;
686 {
687         char name[MAXPATHLEN];
688         FILE *fp;
689
690         if (outfname && followfork > 1) {
691                 sprintf(name, "%s.%u", outfname, tcp->pid);
692 #ifndef SVR4
693                 setreuid(geteuid(), getuid());
694 #endif
695                 fp = fopen(name, "w");
696 #ifndef SVR4
697                 setreuid(geteuid(), getuid());
698 #endif
699                 if (fp == NULL) {
700                         perror("fopen");
701                         return;
702                 }
703                 tcp->outf = fp;
704         }
705         return;
706 }
707
708 int
709 expand_tcbtab()
710 {
711         /* Allocate some more TCBs and expand the table.
712            We don't want to relocate the TCBs because our
713            callers have pointers and it would be a pain.
714            So tcbtab is a table of pointers.  Since we never
715            free the TCBs, we allocate a single chunk of many.  */
716         struct tcb **newtab = (struct tcb **)
717                 realloc(tcbtab, 2 * tcbtabsize * sizeof tcbtab[0]);
718         struct tcb *newtcbs = (struct tcb *) calloc(tcbtabsize,
719                                                     sizeof *newtcbs);
720         int i;
721         if (newtab == NULL || newtcbs == NULL) {
722                 if (newtab != NULL)
723                         free(newtab);
724                 fprintf(stderr, "%s: expand_tcbtab: out of memory\n",
725                         progname);
726                 return 1;
727         }
728         for (i = tcbtabsize; i < 2 * tcbtabsize; ++i)
729                 newtab[i] = &newtcbs[i - tcbtabsize];
730         tcbtabsize *= 2;
731         tcbtab = newtab;
732
733         return 0;
734 }
735
736
737 struct tcb *
738 alloctcb(pid)
739 int pid;
740 {
741         int i;
742         struct tcb *tcp;
743
744         for (i = 0; i < tcbtabsize; i++) {
745                 tcp = tcbtab[i];
746                 if ((tcp->flags & TCB_INUSE) == 0) {
747                         tcp->pid = pid;
748                         tcp->parent = NULL;
749                         tcp->nchildren = 0;
750                         tcp->nzombies = 0;
751 #ifdef TCB_CLONE_THREAD
752                         tcp->nclone_threads = tcp->nclone_detached = 0;
753                         tcp->nclone_waiting = 0;
754 #endif
755                         tcp->flags = TCB_INUSE | TCB_STARTUP;
756                         tcp->outf = outf; /* Initialise to current out file */
757                         tcp->stime.tv_sec = 0;
758                         tcp->stime.tv_usec = 0;
759                         tcp->pfd = -1;
760                         nprocs++;
761                         return tcp;
762                 }
763         }
764         fprintf(stderr, "%s: alloctcb: tcb table full\n", progname);
765         return NULL;
766 }
767
768 #ifdef USE_PROCFS
769 int
770 proc_open(tcp, attaching)
771 struct tcb *tcp;
772 int attaching;
773 {
774         char proc[32];
775         long arg;
776 #ifdef SVR4
777         int i;
778         sysset_t syscalls;
779         sigset_t signals;
780         fltset_t faults;
781 #endif
782 #ifndef HAVE_POLLABLE_PROCFS
783         static int last_pfd;
784 #endif
785
786 #ifdef HAVE_MP_PROCFS
787         /* Open the process pseudo-files in /proc. */
788         sprintf(proc, "/proc/%d/ctl", tcp->pid);
789         if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
790                 perror("strace: open(\"/proc/...\", ...)");
791                 return -1;
792         }
793         if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
794                 perror("F_GETFD");
795                 return -1;
796         }
797         if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
798                 perror("F_SETFD");
799                 return -1;
800         }
801         sprintf(proc, "/proc/%d/status", tcp->pid);
802         if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
803                 perror("strace: open(\"/proc/...\", ...)");
804                 return -1;
805         }
806         if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) {
807                 perror("F_GETFD");
808                 return -1;
809         }
810         if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) {
811                 perror("F_SETFD");
812                 return -1;
813         }
814         sprintf(proc, "/proc/%d/as", tcp->pid);
815         if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
816                 perror("strace: open(\"/proc/...\", ...)");
817                 return -1;
818         }
819         if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) {
820                 perror("F_GETFD");
821                 return -1;
822         }
823         if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) {
824                 perror("F_SETFD");
825                 return -1;
826         }
827 #else
828         /* Open the process pseudo-file in /proc. */
829 #ifndef FREEBSD
830         sprintf(proc, "/proc/%d", tcp->pid);
831         if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) {
832 #else /* FREEBSD */
833         sprintf(proc, "/proc/%d/mem", tcp->pid);
834         if ((tcp->pfd = open(proc, O_RDWR)) < 0) {
835 #endif /* FREEBSD */
836                 perror("strace: open(\"/proc/...\", ...)");
837                 return -1;
838         }
839         if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
840                 perror("F_GETFD");
841                 return -1;
842         }
843         if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
844                 perror("F_SETFD");
845                 return -1;
846         }
847 #endif
848 #ifdef FREEBSD
849         sprintf(proc, "/proc/%d/regs", tcp->pid);
850         if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) {
851                 perror("strace: open(\"/proc/.../regs\", ...)");
852                 return -1;
853         }
854         if (cflag) {
855                 sprintf(proc, "/proc/%d/status", tcp->pid);
856                 if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) {
857                         perror("strace: open(\"/proc/.../status\", ...)");
858                         return -1;
859                 }
860         } else
861                 tcp->pfd_status = -1;
862 #endif /* FREEBSD */
863         rebuild_pollv();
864         if (!attaching) {
865                 /*
866                  * Wait for the child to pause.  Because of a race
867                  * condition we have to poll for the event.
868                  */
869                 for (;;) {
870                         if (IOCTL_STATUS (tcp) < 0) {
871                                 perror("strace: PIOCSTATUS");
872                                 return -1;
873                         }
874                         if (tcp->status.PR_FLAGS & PR_ASLEEP)
875                             break;
876                 }
877         }
878 #ifndef FREEBSD
879         /* Stop the process so that we own the stop. */
880         if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) {
881                 perror("strace: PIOCSTOP");
882                 return -1;
883         }
884 #endif
885 #ifdef PIOCSET
886         /* Set Run-on-Last-Close. */
887         arg = PR_RLC;
888         if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
889                 perror("PIOCSET PR_RLC");
890                 return -1;
891         }
892         /* Set or Reset Inherit-on-Fork. */
893         arg = PR_FORK;
894         if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
895                 perror("PIOC{SET,RESET} PR_FORK");
896                 return -1;
897         }
898 #else  /* !PIOCSET */
899 #ifndef FREEBSD
900         if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
901                 perror("PIOCSRLC");
902                 return -1;
903         }
904         if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
905                 perror("PIOC{S,R}FORK");
906                 return -1;
907         }
908 #else /* FREEBSD */
909         /* just unset the PF_LINGER flag for the Run-on-Last-Close. */
910         if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) {
911                 perror("PIOCGFL");
912                 return -1;
913         }
914         arg &= ~PF_LINGER;
915         if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) {
916                 perror("PIOCSFL");
917                 return -1;
918         }
919 #endif /* FREEBSD */
920 #endif /* !PIOCSET */
921 #ifndef FREEBSD
922         /* Enable all syscall entries we care about. */
923         premptyset(&syscalls);
924         for (i = 1; i < MAX_QUALS; ++i) {
925                 if (i > (sizeof syscalls) * CHAR_BIT) break;
926                 if (qual_flags [i] & QUAL_TRACE) praddset (&syscalls, i);
927         }
928         praddset (&syscalls, SYS_execve);
929         if (followfork) {
930                 praddset (&syscalls, SYS_fork);
931 #ifdef SYS_forkall
932                 praddset (&syscalls, SYS_forkall);
933 #endif
934 #ifdef SYS_fork1
935                 praddset (&syscalls, SYS_fork1);
936 #endif
937 #ifdef SYS_rfork1
938                 praddset (&syscalls, SYS_rfork1);
939 #endif
940 #ifdef SYS_rforkall
941                 praddset (&syscalls, SYS_rforkall);
942 #endif
943         }
944         if (IOCTL(tcp->pfd, PIOCSENTRY, &syscalls) < 0) {
945                 perror("PIOCSENTRY");
946                 return -1;
947         }
948         /* Enable the syscall exits. */
949         if (IOCTL(tcp->pfd, PIOCSEXIT, &syscalls) < 0) {
950                 perror("PIOSEXIT");
951                 return -1;
952         }
953         /* Enable signals we care about. */
954         premptyset(&signals);
955         for (i = 1; i < MAX_QUALS; ++i) {
956                 if (i > (sizeof signals) * CHAR_BIT) break;
957                 if (qual_flags [i] & QUAL_SIGNAL) praddset (&signals, i);
958         }
959         if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
960                 perror("PIOCSTRACE");
961                 return -1;
962         }
963         /* Enable faults we care about */
964         premptyset(&faults);
965         for (i = 1; i < MAX_QUALS; ++i) {
966                 if (i > (sizeof faults) * CHAR_BIT) break;
967                 if (qual_flags [i] & QUAL_FAULT) praddset (&faults, i);
968         }
969         if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
970                 perror("PIOCSFAULT");
971                 return -1;
972         }
973 #else /* FREEBSD */
974         /* set events flags. */
975         arg = S_SIG | S_SCE | S_SCX ;
976         if(ioctl(tcp->pfd, PIOCBIS, arg) < 0) {
977                 perror("PIOCBIS");
978                 return -1;
979         }
980 #endif /* FREEBSD */
981         if (!attaching) {
982 #ifdef MIPS
983                 /*
984                  * The SGI PRSABORT doesn't work for pause() so
985                  * we send it a caught signal to wake it up.
986                  */
987                 kill(tcp->pid, SIGINT);
988 #else /* !MIPS */
989 #ifdef PRSABORT
990                 /* The child is in a pause(), abort it. */
991                 arg = PRSABORT;
992                 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
993                         perror("PIOCRUN");
994                         return -1;
995                 }
996 #endif
997 #endif /* !MIPS*/
998 #ifdef FREEBSD
999                 /* wake up the child if it received the SIGSTOP */
1000                 kill(tcp->pid, SIGCONT);
1001 #endif
1002                 for (;;) {
1003                         /* Wait for the child to do something. */
1004                         if (IOCTL_WSTOP (tcp) < 0) {
1005                                 perror("PIOCWSTOP");
1006                                 return -1;
1007                         }
1008                         if (tcp->status.PR_WHY == PR_SYSENTRY) {
1009                                 tcp->flags &= ~TCB_INSYSCALL;
1010                                 get_scno(tcp);
1011                                 if (known_scno(tcp) == SYS_execve)
1012                                         break;
1013                         }
1014                         /* Set it running: maybe execve will be next. */
1015 #ifndef FREEBSD
1016                         arg = 0;
1017                         if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
1018 #else /* FREEBSD */
1019                         if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) {
1020 #endif /* FREEBSD */
1021                                 perror("PIOCRUN");
1022                                 return -1;
1023                         }
1024 #ifdef FREEBSD
1025                         /* handle the case where we "opened" the child before
1026                            it did the kill -STOP */
1027                         if (tcp->status.PR_WHY == PR_SIGNALLED &&
1028                             tcp->status.PR_WHAT == SIGSTOP)
1029                                 kill(tcp->pid, SIGCONT);
1030 #endif
1031                 }
1032 #ifndef FREEBSD
1033         }
1034 #else /* FREEBSD */
1035         } else {
1036                 if (attaching < 2) {
1037                         /* We are attaching to an already running process.
1038                          * Try to figure out the state of the process in syscalls,
1039                          * to handle the first event well.
1040                          * This is done by having a look at the "wchan" property of the
1041                          * process, which tells where it is stopped (if it is). */
1042                         FILE * status;
1043                         char wchan[20]; /* should be enough */
1044
1045                         sprintf(proc, "/proc/%d/status", tcp->pid);
1046                         status = fopen(proc, "r");
1047                         if (status &&
1048                             (fscanf(status, "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d"
1049                                     "%*d,%*d %*d,%*d %19s", wchan) == 1) &&
1050                             strcmp(wchan, "nochan") && strcmp(wchan, "spread") &&
1051                             strcmp(wchan, "stopevent")) {
1052                                 /* The process is asleep in the middle of a syscall.
1053                                    Fake the syscall entry event */
1054                                 tcp->flags &= ~(TCB_INSYSCALL|TCB_STARTUP);
1055                                 tcp->status.PR_WHY = PR_SYSENTRY;
1056                                 trace_syscall(tcp);
1057                         }
1058                         if (status)
1059                                 fclose(status);
1060                 } /* otherwise it's a fork being followed */
1061         }
1062 #endif /* FREEBSD */
1063 #ifndef HAVE_POLLABLE_PROCFS
1064         if (proc_poll_pipe[0] != -1)
1065                 proc_poller(tcp->pfd);
1066         else if (nprocs > 1) {
1067                 proc_poll_open();
1068                 proc_poller(last_pfd);
1069                 proc_poller(tcp->pfd);
1070         }
1071         last_pfd = tcp->pfd;
1072 #endif /* !HAVE_POLLABLE_PROCFS */
1073         return 0;
1074 }
1075
1076 #endif /* USE_PROCFS */
1077
1078 struct tcb *
1079 pid2tcb(pid)
1080 int pid;
1081 {
1082         int i;
1083         struct tcb *tcp;
1084
1085         for (i = 0; i < tcbtabsize; i++) {
1086                 tcp = tcbtab[i];
1087                 if (pid && tcp->pid != pid)
1088                         continue;
1089                 if (tcp->flags & TCB_INUSE)
1090                         return tcp;
1091         }
1092         return NULL;
1093 }
1094
1095 #ifdef USE_PROCFS
1096
1097 static struct tcb *
1098 pfd2tcb(pfd)
1099 int pfd;
1100 {
1101         int i;
1102
1103         for (i = 0; i < tcbtabsize; i++) {
1104                 struct tcb *tcp = tcbtab[i];
1105                 if (tcp->pfd != pfd)
1106                         continue;
1107                 if (tcp->flags & TCB_INUSE)
1108                         return tcp;
1109         }
1110         return NULL;
1111 }
1112
1113 #endif /* USE_PROCFS */
1114
1115 void
1116 droptcb(tcp)
1117 struct tcb *tcp;
1118 {
1119         if (tcp->pid == 0)
1120                 return;
1121 #ifdef TCB_CLONE_THREAD
1122         if (tcp->nclone_threads > 0) {
1123                 /* There are other threads left in this process, but this
1124                    is the one whose PID represents the whole process.
1125                    We need to keep this record around as a zombie until
1126                    all the threads die.  */
1127                 tcp->flags |= TCB_EXITING;
1128                 return;
1129         }
1130 #endif
1131         nprocs--;
1132         tcp->pid = 0;
1133
1134         if (tcp->parent != NULL) {
1135                 tcp->parent->nchildren--;
1136 #ifdef TCB_CLONE_THREAD
1137                 if (tcp->flags & TCB_CLONE_DETACHED)
1138                         tcp->parent->nclone_detached--;
1139                 if (tcp->flags & TCB_CLONE_THREAD)
1140                         tcp->parent->nclone_threads--;
1141 #endif
1142 #ifdef TCB_CLONE_DETACHED
1143                 if (!(tcp->flags & TCB_CLONE_DETACHED))
1144 #endif
1145                         tcp->parent->nzombies++;
1146                 tcp->parent = NULL;
1147         }
1148
1149         tcp->flags = 0;
1150         if (tcp->pfd != -1) {
1151                 close(tcp->pfd);
1152                 tcp->pfd = -1;
1153 #ifdef FREEBSD
1154                 if (tcp->pfd_reg != -1) {
1155                         close(tcp->pfd_reg);
1156                         tcp->pfd_reg = -1;
1157                 }
1158                 if (tcp->pfd_status != -1) {
1159                         close(tcp->pfd_status);
1160                         tcp->pfd_status = -1;
1161                 }
1162 #endif /* !FREEBSD */
1163 #ifdef USE_PROCFS
1164                 rebuild_pollv(); /* Note, flags needs to be cleared by now.  */
1165 #endif
1166         }
1167
1168         if (outfname && followfork > 1 && tcp->outf)
1169                 fclose(tcp->outf);
1170
1171         tcp->outf = 0;
1172 }
1173
1174 #ifndef USE_PROCFS
1175
1176 static int
1177 resume(tcp)
1178 struct tcb *tcp;
1179 {
1180         if (tcp == NULL)
1181                 return -1;
1182
1183         if (!(tcp->flags & TCB_SUSPENDED)) {
1184                 fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid);
1185                 return -1;
1186         }
1187         tcp->flags &= ~TCB_SUSPENDED;
1188 #ifdef TCB_CLONE_THREAD
1189         if (tcp->flags & TCB_CLONE_THREAD)
1190                 tcp->parent->nclone_waiting--;
1191 #endif
1192
1193         if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) {
1194                 perror("resume: ptrace(PTRACE_SYSCALL, ...)");
1195                 return -1;
1196         }
1197
1198         if (!qflag)
1199                 fprintf(stderr, "Process %u resumed\n", tcp->pid);
1200         return 0;
1201 }
1202
1203 #endif /* !USE_PROCFS */
1204
1205 /* detach traced process; continue with sig */
1206
1207 static int
1208 detach(tcp, sig)
1209 struct tcb *tcp;
1210 int sig;
1211 {
1212         int error = 0;
1213 #ifdef LINUX
1214         int status, resumed;
1215         struct tcb *zombie = NULL;
1216
1217         /* If the group leader is lingering only because of this other
1218            thread now dying, then detach the leader as well.  */
1219         if ((tcp->flags & TCB_CLONE_THREAD) &&
1220             tcp->parent->nclone_threads == 1 &&
1221             (tcp->parent->flags & TCB_EXITING))
1222                 zombie = tcp->parent;
1223 #endif
1224
1225         if (tcp->flags & TCB_BPTSET)
1226                 sig = SIGKILL;
1227
1228 #ifdef LINUX
1229         /*
1230          * Linux wrongly insists the child be stopped
1231          * before detaching.  Arghh.  We go through hoops
1232          * to make a clean break of things.
1233          */
1234 #if defined(SPARC)
1235 #undef PTRACE_DETACH
1236 #define PTRACE_DETACH PTRACE_SUNDETACH
1237 #endif
1238         if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
1239                 /* On a clear day, you can see forever. */
1240         }
1241         else if (errno != ESRCH) {
1242                 /* Shouldn't happen. */
1243                 perror("detach: ptrace(PTRACE_DETACH, ...)");
1244         }
1245         else if (kill(tcp->pid, 0) < 0) {
1246                 if (errno != ESRCH)
1247                         perror("detach: checking sanity");
1248         }
1249         else if (kill(tcp->pid, SIGSTOP) < 0) {
1250                 if (errno != ESRCH)
1251                         perror("detach: stopping child");
1252         }
1253         else {
1254                 for (;;) {
1255 #ifdef __WALL
1256                         if (wait4(tcp->pid, &status, __WALL, NULL) < 0) {
1257                                 if (errno == ECHILD) /* Already gone.  */
1258                                         break;
1259                                 if (errno != EINVAL) {
1260                                         perror("detach: waiting");
1261                                         break;
1262                                 }
1263 #endif /* __WALL */
1264                                 /* No __WALL here.  */
1265                                 if (waitpid(tcp->pid, &status, 0) < 0) {
1266                                         if (errno != ECHILD) {
1267                                                 perror("detach: waiting");
1268                                                 break;
1269                                         }
1270 #ifdef __WCLONE
1271                                         /* If no processes, try clones.  */
1272                                         if (wait4(tcp->pid, &status, __WCLONE,
1273                                                   NULL) < 0) {
1274                                                 if (errno != ECHILD)
1275                                                         perror("detach: waiting");
1276                                                 break;
1277                                         }
1278 #endif /* __WCLONE */
1279                                 }
1280 #ifdef __WALL
1281                         }
1282 #endif
1283                         if (!WIFSTOPPED(status)) {
1284                                 /* Au revoir, mon ami. */
1285                                 break;
1286                         }
1287                         if (WSTOPSIG(status) == SIGSTOP) {
1288                                 if ((error = ptrace(PTRACE_DETACH,
1289                                     tcp->pid, (char *) 1, sig)) < 0) {
1290                                         if (errno != ESRCH)
1291                                                 perror("detach: ptrace(PTRACE_DETACH, ...)");
1292                                         /* I died trying. */
1293                                 }
1294                                 break;
1295                         }
1296                         if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1,
1297                             WSTOPSIG(status) == SIGTRAP ?
1298                             0 : WSTOPSIG(status))) < 0) {
1299                                 if (errno != ESRCH)
1300                                         perror("detach: ptrace(PTRACE_CONT, ...)");
1301                                 break;
1302                         }
1303                 }
1304         }
1305 #endif /* LINUX */
1306
1307 #if defined(SUNOS4)
1308         /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
1309         if (sig && kill(tcp->pid, sig) < 0)
1310                 perror("detach: kill");
1311         sig = 0;
1312         if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0)
1313                 perror("detach: ptrace(PTRACE_DETACH, ...)");
1314 #endif /* SUNOS4 */
1315
1316 #ifndef USE_PROCFS
1317         resumed = 0;
1318
1319         /* XXX This won't always be quite right (but it never was).
1320            A waiter with argument 0 or < -1 is waiting for any pid in
1321            a particular pgrp, which this child might or might not be
1322            in.  The waiter will only wake up if it's argument is -1
1323            or if it's waiting for tcp->pid's pgrp.  It makes a
1324            difference to wake up a waiter when there might be more
1325            traced children, because it could get a false ECHILD
1326            error.  OTOH, if this was the last child in the pgrp, then
1327            it ought to wake up and get ECHILD.  We would have to
1328            search the system for all pid's in the pgrp to be sure.
1329
1330              && (t->waitpid == -1 ||
1331                  (t->waitpid == 0 && getpgid (tcp->pid) == getpgid (t->pid))
1332                  || (t->waitpid < 0 && t->waitpid == -getpid (t->pid)))
1333         */
1334
1335         if (tcp->parent &&
1336             (tcp->parent->flags & TCB_SUSPENDED) &&
1337             (tcp->parent->waitpid <= 0 || tcp->parent->waitpid == tcp->pid)) {
1338                 error = resume(tcp->parent);
1339                 ++resumed;
1340         }
1341 #ifdef TCB_CLONE_THREAD
1342         if (tcp->parent && tcp->parent->nclone_waiting > 0) {
1343                 /* Some other threads of our parent are waiting too.  */
1344                 unsigned int i;
1345
1346                 /* Resume all the threads that were waiting for this PID.  */
1347                 for (i = 0; i < tcbtabsize; i++) {
1348                         struct tcb *t = tcbtab[i];
1349                         if (t->parent == tcp->parent && t != tcp
1350                             && ((t->flags & (TCB_CLONE_THREAD|TCB_SUSPENDED))
1351                                 == (TCB_CLONE_THREAD|TCB_SUSPENDED))
1352                             && t->waitpid == tcp->pid) {
1353                                 error |= resume (t);
1354                                 ++resumed;
1355                         }
1356                 }
1357                 if (resumed == 0)
1358                         /* Noone was waiting for this PID in particular,
1359                            so now we might need to resume some wildcarders.  */
1360                         for (i = 0; i < tcbtabsize; i++) {
1361                                 struct tcb *t = tcbtab[i];
1362                                 if (t->parent == tcp->parent && t != tcp
1363                                     && ((t->flags
1364                                          & (TCB_CLONE_THREAD|TCB_SUSPENDED))
1365                                         == (TCB_CLONE_THREAD|TCB_SUSPENDED))
1366                                     && t->waitpid <= 0
1367                                         ) {
1368                                         error |= resume (t);
1369                                         break;
1370                                 }
1371                         }
1372         }
1373 #endif
1374
1375 #endif /* !USE_PROCFS */
1376
1377         if (!qflag)
1378                 fprintf(stderr, "Process %u detached\n", tcp->pid);
1379
1380         droptcb(tcp);
1381
1382 #ifdef LINUX
1383         if (zombie != NULL)
1384                 error = detach(zombie) || error;
1385 #endif
1386
1387         return error;
1388 }
1389
1390 #ifdef USE_PROCFS
1391
1392 static void
1393 reaper(sig)
1394 int sig;
1395 {
1396         int pid;
1397         int status;
1398
1399         while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
1400 #if 0
1401                 struct tcb *tcp;
1402
1403                 tcp = pid2tcb(pid);
1404                 if (tcp)
1405                         droptcb(tcp);
1406 #endif
1407         }
1408 }
1409
1410 #endif /* USE_PROCFS */
1411
1412 static void
1413 cleanup()
1414 {
1415         int i;
1416         struct tcb *tcp;
1417
1418         for (i = 0; i < tcbtabsize; i++) {
1419                 tcp = tcbtab[i];
1420                 if (!(tcp->flags & TCB_INUSE))
1421                         continue;
1422                 if (debug)
1423                         fprintf(stderr,
1424                                 "cleanup: looking at pid %u\n", tcp->pid);
1425                 if (tcp_last &&
1426                     (!outfname || followfork < 2 || tcp_last == tcp)) {
1427                         tprintf(" <unfinished ...>\n");
1428                         tcp_last = NULL;
1429                 }
1430                 if (tcp->flags & TCB_ATTACHED)
1431                         detach(tcp, 0);
1432                 else {
1433                         kill(tcp->pid, SIGCONT);
1434                         kill(tcp->pid, SIGTERM);
1435                 }
1436         }
1437         if (cflag)
1438                 call_summary(outf);
1439 }
1440
1441 static void
1442 interrupt(sig)
1443 int sig;
1444 {
1445         interrupted = 1;
1446 }
1447
1448 #ifndef HAVE_STRERROR
1449
1450 #if !HAVE_DECL_SYS_ERRLIST
1451 extern int sys_nerr;
1452 extern char *sys_errlist[];
1453 #endif /* HAVE_DECL_SYS_ERRLIST */
1454
1455 const char *
1456 strerror(errno)
1457 int errno;
1458 {
1459         static char buf[64];
1460
1461         if (errno < 1 || errno >= sys_nerr) {
1462                 sprintf(buf, "Unknown error %d", errno);
1463                 return buf;
1464         }
1465         return sys_errlist[errno];
1466 }
1467
1468 #endif /* HAVE_STERRROR */
1469
1470 #ifndef HAVE_STRSIGNAL
1471
1472 #if defined HAVE_SYS_SIGLIST && !defined HAVE_DECL_SYS_SIGLIST
1473 extern char *sys_siglist[];
1474 #endif
1475 #if defined HAVE_SYS__SIGLIST && !defined HAVE_DECL__SYS_SIGLIST
1476 extern char *_sys_siglist[];
1477 #endif
1478
1479 const char *
1480 strsignal(sig)
1481 int sig;
1482 {
1483         static char buf[64];
1484
1485         if (sig < 1 || sig >= NSIG) {
1486                 sprintf(buf, "Unknown signal %d", sig);
1487                 return buf;
1488         }
1489 #ifdef HAVE__SYS_SIGLIST
1490         return _sys_siglist[sig];
1491 #else
1492         return sys_siglist[sig];
1493 #endif
1494 }
1495
1496 #endif /* HAVE_STRSIGNAL */
1497
1498 #ifdef USE_PROCFS
1499
1500 static void
1501 rebuild_pollv()
1502 {
1503         int i, j;
1504
1505         if (pollv != NULL)
1506                 free (pollv);
1507         pollv = (struct pollfd *) malloc(nprocs * sizeof pollv[0]);
1508         if (pollv == NULL) {
1509                 fprintf(stderr, "%s: out of memory\n", progname);
1510                 exit(1);
1511         }
1512
1513         for (i = j = 0; i < tcbtabsize; i++) {
1514                 struct tcb *tcp = tcbtab[i];
1515                 if (!(tcp->flags & TCB_INUSE))
1516                         continue;
1517                 pollv[j].fd = tcp->pfd;
1518                 pollv[j].events = POLLWANT;
1519                 j++;
1520         }
1521         if (j != nprocs) {
1522                 fprintf(stderr, "strace: proc miscount\n");
1523                 exit(1);
1524         }
1525 }
1526
1527 #ifndef HAVE_POLLABLE_PROCFS
1528
1529 static void
1530 proc_poll_open()
1531 {
1532         int arg;
1533         int i;
1534
1535         if (pipe(proc_poll_pipe) < 0) {
1536                 perror("pipe");
1537                 exit(1);
1538         }
1539         for (i = 0; i < 2; i++) {
1540                 if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) {
1541                         perror("F_GETFD");
1542                         exit(1);
1543                 }
1544                 if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) {
1545                         perror("F_SETFD");
1546                         exit(1);
1547                 }
1548         }
1549 }
1550
1551 static int
1552 proc_poll(pollv, nfds, timeout)
1553 struct pollfd *pollv;
1554 int nfds;
1555 int timeout;
1556 {
1557         int i;
1558         int n;
1559         struct proc_pollfd pollinfo;
1560
1561         if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
1562                 return n;
1563         if (n != sizeof(struct proc_pollfd)) {
1564                 fprintf(stderr, "panic: short read: %d\n", n);
1565                 exit(1);
1566         }
1567         for (i = 0; i < nprocs; i++) {
1568                 if (pollv[i].fd == pollinfo.fd)
1569                         pollv[i].revents = pollinfo.revents;
1570                 else
1571                         pollv[i].revents = 0;
1572         }
1573         poller_pid = pollinfo.pid;
1574         return 1;
1575 }
1576
1577 static void
1578 wakeup_handler(sig)
1579 int sig;
1580 {
1581 }
1582
1583 static void
1584 proc_poller(pfd)
1585 int pfd;
1586 {
1587         struct proc_pollfd pollinfo;
1588         struct sigaction sa;
1589         sigset_t blocked_set, empty_set;
1590         int i;
1591         int n;
1592         struct rlimit rl;
1593 #ifdef FREEBSD
1594         struct procfs_status pfs;
1595 #endif /* FREEBSD */
1596
1597         switch (fork()) {
1598         case -1:
1599                 perror("fork");
1600                 _exit(0);
1601         case 0:
1602                 break;
1603         default:
1604                 return;
1605         }
1606
1607         sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
1608         sa.sa_flags = 0;
1609         sigemptyset(&sa.sa_mask);
1610         sigaction(SIGHUP, &sa, NULL);
1611         sigaction(SIGINT, &sa, NULL);
1612         sigaction(SIGQUIT, &sa, NULL);
1613         sigaction(SIGPIPE, &sa, NULL);
1614         sigaction(SIGTERM, &sa, NULL);
1615         sa.sa_handler = wakeup_handler;
1616         sigaction(SIGUSR1, &sa, NULL);
1617         sigemptyset(&blocked_set);
1618         sigaddset(&blocked_set, SIGUSR1);
1619         sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1620         sigemptyset(&empty_set);
1621
1622         if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
1623                 perror("getrlimit(RLIMIT_NOFILE, ...)");
1624                 _exit(0);
1625         }
1626         n = rl.rlim_cur;
1627         for (i = 0; i < n; i++) {
1628                 if (i != pfd && i != proc_poll_pipe[1])
1629                         close(i);
1630         }
1631
1632         pollinfo.fd = pfd;
1633         pollinfo.pid = getpid();
1634         for (;;) {
1635 #ifndef FREEBSD
1636                 if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
1637 #else /* FREEBSD */
1638                 if (ioctl(pfd, PIOCWSTOP, &pfs) < 0)
1639 #endif /* FREEBSD */
1640                 {
1641                         switch (errno) {
1642                         case EINTR:
1643                                 continue;
1644                         case EBADF:
1645                                 pollinfo.revents = POLLERR;
1646                                 break;
1647                         case ENOENT:
1648                                 pollinfo.revents = POLLHUP;
1649                                 break;
1650                         default:
1651                                 perror("proc_poller: PIOCWSTOP");
1652                         }
1653                         write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1654                         _exit(0);
1655                 }
1656                 pollinfo.revents = POLLWANT;
1657                 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1658                 sigsuspend(&empty_set);
1659         }
1660 }
1661
1662 #endif /* !HAVE_POLLABLE_PROCFS */
1663
1664 static int
1665 choose_pfd()
1666 {
1667         int i, j;
1668         struct tcb *tcp;
1669
1670         static int last;
1671
1672         if (followfork < 2 &&
1673             last < nprocs && (pollv[last].revents & POLLWANT)) {
1674                 /*
1675                  * The previous process is ready to run again.  We'll
1676                  * let it do so if it is currently in a syscall.  This
1677                  * heuristic improves the readability of the trace.
1678                  */
1679                 tcp = pfd2tcb(pollv[last].fd);
1680                 if (tcp && (tcp->flags & TCB_INSYSCALL))
1681                         return pollv[last].fd;
1682         }
1683
1684         for (i = 0; i < nprocs; i++) {
1685                 /* Let competing children run round robin. */
1686                 j = (i + last + 1) % nprocs;
1687                 if (pollv[j].revents & (POLLHUP | POLLERR)) {
1688                         tcp = pfd2tcb(pollv[j].fd);
1689                         if (!tcp) {
1690                                 fprintf(stderr, "strace: lost proc\n");
1691                                 exit(1);
1692                         }
1693                         droptcb(tcp);
1694                         return -1;
1695                 }
1696                 if (pollv[j].revents & POLLWANT) {
1697                         last = j;
1698                         return pollv[j].fd;
1699                 }
1700         }
1701         fprintf(stderr, "strace: nothing ready\n");
1702         exit(1);
1703 }
1704
1705 static int
1706 trace()
1707 {
1708 #ifdef POLL_HACK
1709         struct tcb *in_syscall = NULL;
1710 #endif
1711         struct tcb *tcp;
1712         int pfd;
1713         int what;
1714         int ioctl_result = 0, ioctl_errno = 0;
1715         long arg;
1716
1717         for (;;) {
1718                 if (interactive)
1719                         sigprocmask(SIG_SETMASK, &empty_set, NULL);
1720
1721                 if (nprocs == 0)
1722                         break;
1723
1724                 switch (nprocs) {
1725                 case 1:
1726 #ifndef HAVE_POLLABLE_PROCFS
1727                         if (proc_poll_pipe[0] == -1) {
1728 #endif
1729                                 tcp = pid2tcb(0);
1730                                 if (!tcp)
1731                                         continue;
1732                                 pfd = tcp->pfd;
1733                                 if (pfd == -1)
1734                                         continue;
1735                                 break;
1736 #ifndef HAVE_POLLABLE_PROCFS
1737                         }
1738                         /* fall through ... */
1739 #endif /* !HAVE_POLLABLE_PROCFS */
1740                 default:
1741 #ifdef HAVE_POLLABLE_PROCFS
1742 #ifdef POLL_HACK
1743                         /* On some systems (e.g. UnixWare) we get too much ugly
1744                            "unfinished..." stuff when multiple proceses are in
1745                            syscalls.  Here's a nasty hack */
1746
1747                         if (in_syscall) {
1748                                 struct pollfd pv;
1749                                 tcp = in_syscall;
1750                                 in_syscall = NULL;
1751                                 pv.fd = tcp->pfd;
1752                                 pv.events = POLLWANT;
1753                                 if ((what = poll (&pv, 1, 1)) < 0) {
1754                                         if (interrupted)
1755                                                 return 0;
1756                                         continue;
1757                                 }
1758                                 else if (what == 1 && pv.revents & POLLWANT) {
1759                                         goto FOUND;
1760                                 }
1761                         }
1762 #endif
1763
1764                         if (poll(pollv, nprocs, INFTIM) < 0) {
1765                                 if (interrupted)
1766                                         return 0;
1767                                 continue;
1768                         }
1769 #else /* !HAVE_POLLABLE_PROCFS */
1770                         if (proc_poll(pollv, nprocs, INFTIM) < 0) {
1771                                 if (interrupted)
1772                                         return 0;
1773                                 continue;
1774                         }
1775 #endif /* !HAVE_POLLABLE_PROCFS */
1776                         pfd = choose_pfd();
1777                         if (pfd == -1)
1778                                 continue;
1779                         break;
1780                 }
1781
1782                 /* Look up `pfd' in our table. */
1783                 if ((tcp = pfd2tcb(pfd)) == NULL) {
1784                         fprintf(stderr, "unknown pfd: %u\n", pfd);
1785                         exit(1);
1786                 }
1787 #ifdef POLL_HACK
1788         FOUND:
1789 #endif
1790                 /* Get the status of the process. */
1791                 if (!interrupted) {
1792 #ifndef FREEBSD
1793                         ioctl_result = IOCTL_WSTOP (tcp);
1794 #else /* FREEBSD */
1795                         /* Thanks to some scheduling mystery, the first poller
1796                            sometimes waits for the already processed end of fork
1797                            event. Doing a non blocking poll here solves the problem. */
1798                         if (proc_poll_pipe[0] != -1)
1799                                 ioctl_result = IOCTL_STATUS (tcp);
1800                         else
1801                                 ioctl_result = IOCTL_WSTOP (tcp);
1802 #endif /* FREEBSD */
1803                         ioctl_errno = errno;
1804 #ifndef HAVE_POLLABLE_PROCFS
1805                         if (proc_poll_pipe[0] != -1) {
1806                                 if (ioctl_result < 0)
1807                                         kill(poller_pid, SIGKILL);
1808                                 else
1809                                         kill(poller_pid, SIGUSR1);
1810                         }
1811 #endif /* !HAVE_POLLABLE_PROCFS */
1812                 }
1813                 if (interrupted)
1814                         return 0;
1815
1816                 if (interactive)
1817                         sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1818
1819                 if (ioctl_result < 0) {
1820                         /* Find out what happened if it failed. */
1821                         switch (ioctl_errno) {
1822                         case EINTR:
1823                         case EBADF:
1824                                 continue;
1825 #ifdef FREEBSD
1826                         case ENOTTY:
1827 #endif
1828                         case ENOENT:
1829                                 droptcb(tcp);
1830                                 continue;
1831                         default:
1832                                 perror("PIOCWSTOP");
1833                                 exit(1);
1834                         }
1835                 }
1836
1837 #ifdef FREEBSD
1838                 if ((tcp->flags & TCB_STARTUP) && (tcp->status.PR_WHY == PR_SYSEXIT)) {
1839                         /* discard first event for a syscall we never entered */
1840                         IOCTL (tcp->pfd, PIOCRUN, 0);
1841                         continue;
1842                 }
1843 #endif
1844
1845                 /* clear the just started flag */
1846                 tcp->flags &= ~TCB_STARTUP;
1847
1848                 /* set current output file */
1849                 outf = tcp->outf;
1850
1851                 if (cflag) {
1852                         struct timeval stime;
1853 #ifdef FREEBSD
1854                         char buf[1024];
1855                         int len;
1856
1857                         if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) {
1858                                 buf[len] = '\0';
1859                                 sscanf(buf,
1860                                        "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld",
1861                                        &stime.tv_sec, &stime.tv_usec);
1862                         } else
1863                                 stime.tv_sec = stime.tv_usec = 0;
1864 #else /* !FREEBSD */
1865                         stime.tv_sec = tcp->status.pr_stime.tv_sec;
1866                         stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
1867 #endif /* !FREEBSD */
1868                         tv_sub(&tcp->dtime, &stime, &tcp->stime);
1869                         tcp->stime = stime;
1870                 }
1871                 what = tcp->status.PR_WHAT;
1872                 switch (tcp->status.PR_WHY) {
1873 #ifndef FREEBSD
1874                 case PR_REQUESTED:
1875                         if (tcp->status.PR_FLAGS & PR_ASLEEP) {
1876                                 tcp->status.PR_WHY = PR_SYSENTRY;
1877                                 if (trace_syscall(tcp) < 0) {
1878                                         fprintf(stderr, "syscall trouble\n");
1879                                         exit(1);
1880                                 }
1881                         }
1882                         break;
1883 #endif /* !FREEBSD */
1884                 case PR_SYSENTRY:
1885 #ifdef POLL_HACK
1886                         in_syscall = tcp;
1887 #endif
1888                 case PR_SYSEXIT:
1889                         if (trace_syscall(tcp) < 0) {
1890                                 fprintf(stderr, "syscall trouble\n");
1891                                 exit(1);
1892                         }
1893                         break;
1894                 case PR_SIGNALLED:
1895                         if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) {
1896                                 printleader(tcp);
1897                                 tprintf("--- %s (%s) ---",
1898                                         signame(what), strsignal(what));
1899                                 printtrailer(tcp);
1900 #ifdef PR_INFO
1901                                 if (tcp->status.PR_INFO.si_signo == what) {
1902                                         printleader(tcp);
1903                                         tprintf("    siginfo=");
1904                                         printsiginfo(&tcp->status.PR_INFO, 1);
1905                                         printtrailer(tcp);
1906                                 }
1907 #endif
1908                         }
1909                         break;
1910                 case PR_FAULTED:
1911                         if (!cflag && (qual_flags[what] & QUAL_FAULT)) {
1912                                 printleader(tcp);
1913                                 tprintf("=== FAULT %d ===", what);
1914                                 printtrailer(tcp);
1915                         }
1916                         break;
1917 #ifdef FREEBSD
1918                 case 0: /* handle case we polled for nothing */
1919                         continue;
1920 #endif
1921                 default:
1922                         fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
1923                         exit(1);
1924                         break;
1925                 }
1926                 arg = 0;
1927 #ifndef FREEBSD
1928                 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
1929 #else
1930                 if (IOCTL (tcp->pfd, PIOCRUN, 0) < 0) {
1931 #endif
1932                         perror("PIOCRUN");
1933                         exit(1);
1934                 }
1935         }
1936         return 0;
1937 }
1938
1939 #else /* !USE_PROCFS */
1940
1941 #ifdef TCB_GROUP_EXITING
1942 /* Handle an exit detach or death signal that is taking all the
1943    related clone threads with it.  This is called in three circumstances:
1944    SIG == -1    TCP has already died (TCB_ATTACHED is clear, strace is parent).
1945    SIG == 0     Continuing TCP will perform an exit_group syscall.
1946    SIG == other Continuing TCP with SIG will kill the process.
1947 */
1948 static int
1949 handle_group_exit(struct tcb *tcp, int sig)
1950 {
1951         /* We need to locate our records of all the clone threads
1952            related to TCP, either its children or siblings.  */
1953         struct tcb *leader = ((tcp->flags & TCB_CLONE_THREAD)
1954                               ? tcp->parent
1955                               : tcp->nclone_detached > 0
1956                               ? tcp : NULL);
1957
1958         if (sig < 0) {
1959                 if (leader != NULL && leader != tcp &&
1960                     !(leader->flags & TCB_GROUP_EXITING))
1961                         fprintf(stderr,
1962                                 "PANIC: handle_group_exit: %d leader %d\n",
1963                                 tcp->pid, leader ? leader->pid : -1);
1964                 detach(tcp);    /* Already died.  */
1965         }
1966         else {
1967                 /* Mark that we are taking the process down.  */
1968                 tcp->flags |= TCB_EXITING | TCB_GROUP_EXITING;
1969                 if (tcp->flags & TCB_ATTACHED) {
1970                         if (leader != NULL && leader != tcp) {
1971                                 if (leader->flags & TCB_ATTACHED) {
1972                                         /* We need to detach the leader so
1973                                            that the process death will be
1974                                            reported to its real parent.
1975                                            But we kill it first to prevent
1976                                            it doing anything before we kill
1977                                            the whole process in a moment.
1978                                            We can use PTRACE_KILL on a
1979                                            thread that's not already
1980                                            stopped.  Then the value we pass
1981                                            in PTRACE_DETACH just sets the
1982                                            death signal reported to the
1983                                            real parent.  */
1984                                         ptrace(PTRACE_KILL, leader->pid, 0, 0);
1985                                         if (debug)
1986                                                 fprintf(stderr,
1987                                                         " [%d exit %d kills %d]\n",
1988                                                         tcp->pid, sig, leader->pid);
1989                                         detach(leader, sig);
1990                                 }
1991                                 else
1992                                         leader->flags |= TCB_GROUP_EXITING;
1993                         }
1994                         detach(tcp, sig);
1995                 }
1996                 else if (ptrace(PTRACE_CONT, tcp->pid, (char *) 1, sig) < 0) {
1997                         perror("strace: ptrace(PTRACE_CONT, ...)");
1998                         cleanup();
1999                         return -1;
2000                 }
2001                 else {
2002                         if (leader != NULL)
2003                                 leader->flags |= TCB_GROUP_EXITING;
2004                         if (leader != NULL && leader != tcp)
2005                                 droptcb(tcp);
2006                         /* The leader will report to us as parent now,
2007                            and then we'll get to the SIG==-1 case.  */
2008                         return 0;
2009                 }
2010         }
2011
2012         return 0;
2013 }
2014 #endif
2015
2016 static int
2017 trace()
2018 {
2019         int pid;
2020         int wait_errno;
2021         int status;
2022         struct tcb *tcp;
2023 #ifdef LINUX
2024         struct rusage ru;
2025 #ifdef __WALL
2026         static int wait4_options = __WALL;
2027 #endif
2028 #endif /* LINUX */
2029
2030         while (nprocs != 0) {
2031                 if (interactive)
2032                         sigprocmask(SIG_SETMASK, &empty_set, NULL);
2033 #ifdef LINUX
2034 #ifdef __WALL
2035                 pid = wait4(-1, &status, wait4_options, cflag ? &ru : NULL);
2036                 if (pid < 0 && (wait4_options & __WALL) && errno == EINVAL) {
2037                         /* this kernel does not support __WALL */
2038                         wait4_options &= ~__WALL;
2039                         errno = 0;
2040                         pid = wait4(-1, &status, wait4_options,
2041                                         cflag ? &ru : NULL);
2042                 }
2043                 if (pid < 0 && !(wait4_options & __WALL) && errno == ECHILD) {
2044                         /* most likely a "cloned" process */
2045                         pid = wait4(-1, &status, __WCLONE,
2046                                         cflag ? &ru : NULL);
2047                         if (pid == -1) {
2048                                 fprintf(stderr, "strace: clone wait4 "
2049                                                 "failed: %s\n", strerror(errno));
2050                         }
2051                 }
2052 #else
2053                 pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
2054 #endif /* __WALL */
2055 #endif /* LINUX */
2056 #ifdef SUNOS4
2057                 pid = wait(&status);
2058 #endif /* SUNOS4 */
2059                 wait_errno = errno;
2060                 if (interactive)
2061                         sigprocmask(SIG_BLOCK, &blocked_set, NULL);
2062
2063                 if (interrupted)
2064                         return 0;
2065
2066                 if (pid == -1) {
2067                         switch (wait_errno) {
2068                         case EINTR:
2069                                 continue;
2070                         case ECHILD:
2071                                 /*
2072                                  * We would like to verify this case
2073                                  * but sometimes a race in Solbourne's
2074                                  * version of SunOS sometimes reports
2075                                  * ECHILD before sending us SIGCHILD.
2076                                  */
2077 #if 0
2078                                 if (nprocs == 0)
2079                                         return 0;
2080                                 fprintf(stderr, "strace: proc miscount\n");
2081                                 exit(1);
2082 #endif
2083                                 return 0;
2084                         default:
2085                                 errno = wait_errno;
2086                                 perror("strace: wait");
2087                                 return -1;
2088                         }
2089                 }
2090                 if (debug)
2091                         fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
2092
2093                 /* Look up `pid' in our table. */
2094                 if ((tcp = pid2tcb(pid)) == NULL) {
2095 #ifdef LINUX
2096                         if (followfork || followvfork) {
2097                                 /* This is needed to go with the CLONE_PTRACE
2098                                    changes in process.c/util.c: we might see
2099                                    the child's initial trap before we see the
2100                                    parent return from the clone syscall.
2101                                    Leave the child suspended until the parent
2102                                    returns from its system call.  Only then
2103                                    will we have the association of parent and
2104                                    child so that we know how to do clearbpt
2105                                    in the child.  */
2106                                 if (nprocs == tcbtabsize &&
2107                                     expand_tcbtab())
2108                                         tcp = NULL;
2109                                 else
2110                                         tcp = alloctcb(pid);
2111                                 if (tcp == NULL) {
2112                                         kill(pid, SIGKILL); /* XXX */
2113                                         return 0;
2114                                 }
2115                                 tcp->flags |= TCB_ATTACHED | TCB_SUSPENDED;
2116                                 newoutf(tcp);
2117                                 if (!qflag)
2118                                         fprintf(stderr, "\
2119 Process %d attached (waiting for parent)\n",
2120                                                 pid);
2121                         }
2122                         else
2123                                 /* This can happen if a clone call used
2124                                    CLONE_PTRACE itself.  */
2125 #endif
2126                         {
2127                                 fprintf(stderr, "unknown pid: %u\n", pid);
2128                                 if (WIFSTOPPED(status))
2129                                         ptrace(PTRACE_CONT, pid, (char *) 1, 0);
2130                                 exit(1);
2131                         }
2132                 }
2133                 /* set current output file */
2134                 outf = tcp->outf;
2135                 if (cflag) {
2136 #ifdef LINUX
2137                         tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2138                         tcp->stime = ru.ru_stime;
2139 #endif /* !LINUX */
2140                 }
2141
2142                 if (tcp->flags & TCB_SUSPENDED) {
2143                         /*
2144                          * Apparently, doing any ptrace() call on a stopped
2145                          * process, provokes the kernel to report the process
2146                          * status again on a subsequent wait(), even if the
2147                          * process has not been actually restarted.
2148                          * Since we have inspected the arguments of suspended
2149                          * processes we end up here testing for this case.
2150                          */
2151                         continue;
2152                 }
2153                 if (WIFSIGNALED(status)) {
2154                         if (!cflag
2155                             && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
2156                                 printleader(tcp);
2157                                 tprintf("+++ killed by %s %s+++",
2158                                         signame(WTERMSIG(status)),
2159 #ifdef WCOREDUMP
2160                                         WCOREDUMP(status) ? "(core dumped) " :
2161 #endif
2162                                         "");
2163                                 printtrailer(tcp);
2164                         }
2165 #ifdef TCB_GROUP_EXITING
2166                         handle_group_exit(tcp, -1);
2167 #else
2168                         droptcb(tcp);
2169 #endif
2170                         continue;
2171                 }
2172                 if (WIFEXITED(status)) {
2173                         if (debug)
2174                                 fprintf(stderr, "pid %u exited\n", pid);
2175                         if ((tcp->flags & TCB_ATTACHED)
2176 #ifdef TCB_GROUP_EXITING
2177                             && !(tcp->parent && (tcp->parent->flags &
2178                                                  TCB_GROUP_EXITING))
2179 #endif
2180                                 )
2181                                 fprintf(stderr,
2182                                         "PANIC: attached pid %u exited\n",
2183                                         pid);
2184                         if (tcp == tcp_last) {
2185                                 if ((tcp->flags & (TCB_INSYSCALL|TCB_REPRINT))
2186                                     == TCB_INSYSCALL)
2187                                         tprintf(" <unfinished ... exit status %d>\n",
2188                                                 WEXITSTATUS(status));
2189                                 tcp_last = NULL;
2190                         }
2191 #ifdef TCB_GROUP_EXITING
2192                         handle_group_exit(tcp, -1);
2193 #else
2194                         droptcb(tcp);
2195 #endif
2196                         continue;
2197                 }
2198                 if (!WIFSTOPPED(status)) {
2199                         fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
2200                         droptcb(tcp);
2201                         continue;
2202                 }
2203                 if (debug)
2204                         fprintf(stderr, "pid %u stopped, [%s]\n",
2205                                 pid, signame(WSTOPSIG(status)));
2206
2207                 if (tcp->flags & TCB_STARTUP) {
2208                         /*
2209                          * This flag is there to keep us in sync.
2210                          * Next time this process stops it should
2211                          * really be entering a system call.
2212                          */
2213                         tcp->flags &= ~TCB_STARTUP;
2214                         if (tcp->flags & TCB_ATTACHED) {
2215                                 /*
2216                                  * Interestingly, the process may stop
2217                                  * with STOPSIG equal to some other signal
2218                                  * than SIGSTOP if we happend to attach
2219                                  * just before the process takes a signal.
2220                                  */
2221                                 if (!WIFSTOPPED(status)) {
2222                                         fprintf(stderr,
2223                                                 "pid %u not stopped\n", pid);
2224                                         detach(tcp, WSTOPSIG(status));
2225                                         continue;
2226                                 }
2227                         }
2228                         else {
2229 #ifdef SUNOS4
2230                                 /* A child of us stopped at exec */
2231                                 if (WSTOPSIG(status) == SIGTRAP && followvfork)
2232                                         fixvfork(tcp);
2233 #endif /* SUNOS4 */
2234                         }
2235                         if (tcp->flags & TCB_BPTSET) {
2236                                 if (clearbpt(tcp) < 0) /* Pretty fatal */ {
2237                                         droptcb(tcp);
2238                                         cleanup();
2239                                         return -1;
2240                                 }
2241                         }
2242                         goto tracing;
2243                 }
2244
2245                 if (WSTOPSIG(status) != SIGTRAP) {
2246                         if (WSTOPSIG(status) == SIGSTOP &&
2247                                         (tcp->flags & TCB_SIGTRAPPED)) {
2248                                 /*
2249                                  * Trapped attempt to block SIGTRAP
2250                                  * Hope we are back in control now.
2251                                  */
2252                                 tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
2253                                 if (ptrace(PTRACE_SYSCALL,
2254                                                 pid, (char *) 1, 0) < 0) {
2255                                         perror("trace: ptrace(PTRACE_SYSCALL, ...)");
2256                                         cleanup();
2257                                         return -1;
2258                                 }
2259                                 continue;
2260                         }
2261                         if (!cflag
2262                             && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
2263                                 unsigned long addr = 0, pc = 0;
2264 #if defined(PT_CR_IPSR) && defined(PT_CR_IIP) && defined(PT_GETSIGINFO)
2265 #                               define PSR_RI   41
2266                                 struct siginfo si;
2267                                 unsigned long psr;
2268
2269                                 upeek(pid, PT_CR_IPSR, &psr);
2270                                 upeek(pid, PT_CR_IIP, &pc);
2271
2272                                 pc += (psr >> PSR_RI) & 0x3;
2273                                 ptrace(PT_GETSIGINFO, pid, 0, (long) &si);
2274                                 addr = (unsigned long) si.si_addr;
2275 #elif defined PTRACE_GETSIGINFO
2276                                 if (WSTOPSIG(status) == SIGSEGV ||
2277                                     WSTOPSIG(status) == SIGBUS) {
2278                                         siginfo_t si;
2279                                         if (ptrace(PTRACE_GETSIGINFO, pid,
2280                                                    0, &si) == 0)
2281                                                 addr = (unsigned long)
2282                                                         si.si_addr;
2283                                 }
2284 #endif
2285                                 printleader(tcp);
2286                                 tprintf("--- %s (%s) @ %lx (%lx) ---",
2287                                         signame(WSTOPSIG(status)),
2288                                         strsignal(WSTOPSIG(status)), pc, addr);
2289                                 printtrailer(tcp);
2290                         }
2291                         if (((tcp->flags & TCB_ATTACHED) ||
2292                              tcp->nclone_threads > 0) &&
2293                                 !sigishandled(tcp, WSTOPSIG(status))) {
2294 #ifdef TCB_GROUP_EXITING
2295                                 handle_group_exit(tcp, WSTOPSIG(status));
2296 #else
2297                                 detach(tcp, WSTOPSIG(status));
2298 #endif
2299                                 continue;
2300                         }
2301                         if (ptrace(PTRACE_SYSCALL, pid, (char *) 1,
2302                                    WSTOPSIG(status)) < 0) {
2303                                 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
2304                                 cleanup();
2305                                 return -1;
2306                         }
2307                         tcp->flags &= ~TCB_SUSPENDED;
2308                         continue;
2309                 }
2310                 if (trace_syscall(tcp) < 0) {
2311                         if (tcp->flags & TCB_ATTACHED)
2312                                 detach(tcp, 0);
2313                         else {
2314                                 ptrace(PTRACE_KILL,
2315                                         tcp->pid, (char *) 1, SIGTERM);
2316                                 droptcb(tcp);
2317                         }
2318                         continue;
2319                 }
2320                 if (tcp->flags & TCB_EXITING) {
2321 #ifdef TCB_GROUP_EXITING
2322                         if (tcp->flags & TCB_GROUP_EXITING) {
2323                                 if (handle_group_exit(tcp, 0) < 0)
2324                                         return -1;
2325                                 continue;
2326                         }
2327 #endif
2328                         if (tcp->flags & TCB_ATTACHED)
2329                                 detach(tcp, 0);
2330                         else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) {
2331                                 perror("strace: ptrace(PTRACE_CONT, ...)");
2332                                 cleanup();
2333                                 return -1;
2334                         }
2335                         continue;
2336                 }
2337                 if (tcp->flags & TCB_SUSPENDED) {
2338                         if (!qflag)
2339                                 fprintf(stderr, "Process %u suspended\n", pid);
2340                         continue;
2341                 }
2342         tracing:
2343                 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) {
2344                         perror("trace: ptrace(PTRACE_SYSCALL, ...)");
2345                         cleanup();
2346                         return -1;
2347                 }
2348         }
2349         return 0;
2350 }
2351
2352 #endif /* !USE_PROCFS */
2353
2354 static int curcol;
2355
2356 #ifdef __STDC__
2357 #include <stdarg.h>
2358 #define VA_START(a, b) va_start(a, b)
2359 #else
2360 #include <varargs.h>
2361 #define VA_START(a, b) va_start(a)
2362 #endif
2363
2364 void
2365 #ifdef __STDC__
2366 tprintf(const char *fmt, ...)
2367 #else
2368 tprintf(fmt, va_alist)
2369 char *fmt;
2370 va_dcl
2371 #endif
2372 {
2373         va_list args;
2374
2375         VA_START(args, fmt);
2376         if (outf) {
2377                 int n = vfprintf(outf, fmt, args);
2378                 if (n < 0 && outf != stderr)
2379                         perror(outfname == NULL
2380                                ? "<writing to pipe>" : outfname);
2381                 else
2382                         curcol += n;
2383         }
2384         va_end(args);
2385         return;
2386 }
2387
2388 void
2389 printleader(tcp)
2390 struct tcb *tcp;
2391 {
2392         if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) {
2393                 tcp_last->flags |= TCB_REPRINT;
2394                 tprintf(" <unfinished ...>\n");
2395         }
2396         curcol = 0;
2397         if ((followfork == 1 || pflag_seen > 1) && outfname)
2398                 tprintf("%-5d ", tcp->pid);
2399         else if (nprocs > 1 && !outfname)
2400                 tprintf("[pid %5u] ", tcp->pid);
2401         if (tflag) {
2402                 char str[sizeof("HH:MM:SS")];
2403                 struct timeval tv, dtv;
2404                 static struct timeval otv;
2405
2406                 gettimeofday(&tv, NULL);
2407                 if (rflag) {
2408                         if (otv.tv_sec == 0)
2409                                 otv = tv;
2410                         tv_sub(&dtv, &tv, &otv);
2411                         tprintf("%6ld.%06ld ",
2412                                 (long) dtv.tv_sec, (long) dtv.tv_usec);
2413                         otv = tv;
2414                 }
2415                 else if (tflag > 2) {
2416                         tprintf("%ld.%06ld ",
2417                                 (long) tv.tv_sec, (long) tv.tv_usec);
2418                 }
2419                 else {
2420                         time_t local = tv.tv_sec;
2421                         strftime(str, sizeof(str), "%T", localtime(&local));
2422                         if (tflag > 1)
2423                                 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
2424                         else
2425                                 tprintf("%s ", str);
2426                 }
2427         }
2428         if (iflag)
2429                 printcall(tcp);
2430 }
2431
2432 void
2433 tabto(col)
2434 int col;
2435 {
2436         if (curcol < col)
2437                 tprintf("%*s", col - curcol, "");
2438 }
2439
2440 void
2441 printtrailer(tcp)
2442 struct tcb *tcp;
2443 {
2444         tprintf("\n");
2445         tcp_last = NULL;
2446 }
2447
2448 #ifdef HAVE_MP_PROCFS
2449
2450 int mp_ioctl (int fd, int cmd, void *arg, int size) {
2451
2452         struct iovec iov[2];
2453         int n = 1;
2454
2455         iov[0].iov_base = &cmd;
2456         iov[0].iov_len = sizeof cmd;
2457         if (arg) {
2458                 ++n;
2459                 iov[1].iov_base = arg;
2460                 iov[1].iov_len = size;
2461         }
2462
2463         return writev (fd, iov, n);
2464 }
2465
2466 #endif