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