]> granicus.if.org Git - strace/blob - strace.c
Linux/MIPS uses syscalls up to >4k, so set MAX_QUALS to 4999
[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
47 #ifdef SVR4
48 #include <sys/stropts.h>
49 #include <poll.h>
50 #ifdef HAVE_MP_PROCFS
51 #include <sys/uio.h>
52 #endif
53 #endif
54
55 int debug = 0, followfork = 0, followvfork = 0, interactive = 0;
56 int rflag = 0, tflag = 0, dtime = 0, cflag = 0;
57 int iflag = 0, xflag = 0, qflag = 0;
58 int pflag_seen = 0;
59
60 char *username = NULL;
61 uid_t run_uid;
62 gid_t run_gid;
63
64 int acolumn = DEFAULT_ACOLUMN;
65 int max_strlen = DEFAULT_STRLEN;
66 char *outfname = NULL;
67 FILE *outf;
68 struct tcb tcbtab[MAX_PROCS];
69 int nprocs;
70 char *progname;
71 extern char version[];
72 extern char **environ;
73
74 static struct tcb *pid2tcb P((int pid));
75 static int trace P((void));
76 static void cleanup P((void));
77 static void interrupt P((int sig));
78 static sigset_t empty_set, blocked_set;
79
80 #ifdef HAVE_SIG_ATOMIC_T
81 static volatile sig_atomic_t interrupted;
82 #else /* !HAVE_SIG_ATOMIC_T */
83 #ifdef __STDC__
84 static volatile int interrupted;
85 #else /* !__STDC__ */
86 static int interrupted;
87 #endif /* !__STDC__ */
88 #endif /* !HAVE_SIG_ATOMIC_T */
89
90 #ifdef SVR4
91
92 static struct tcb *pfd2tcb P((int pfd));
93 static void reaper P((int sig));
94 static void rebuild_pollv P((void));
95 struct pollfd pollv[MAX_PROCS];
96
97 #ifndef HAVE_POLLABLE_PROCFS
98
99 static void proc_poll_open P((void));
100 static void proc_poller P((int pfd));
101
102 struct proc_pollfd {
103         int fd;
104         int revents;
105         int pid;
106 };
107
108 static int poller_pid;
109 static int proc_poll_pipe[2] = { -1, -1 };
110
111 #endif /* !HAVE_POLLABLE_PROCFS */
112
113 #ifdef HAVE_MP_PROCFS
114 #define POLLWANT        POLLWRNORM
115 #else
116 #define POLLWANT        POLLPRI
117 #endif
118 #endif /* SVR4 */
119
120 static void
121 usage(ofp, exitval)
122 FILE *ofp;
123 int exitval;
124 {
125         fprintf(ofp, "\
126 usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\
127               [-p pid] ... [-s strsize] [-u username] [command [arg ...]]\n\
128    or: strace -c [-e expr] ... [-O overhead] [-S sortby] [command [arg ...]]\n\
129 -c -- count time, calls, and errors for each syscall and report summary\n\
130 -f -- follow forks, -ff -- with output into separate files\n\
131 -F -- attempt to follow vforks, -h -- print help message\n\
132 -i -- print instruction pointer at time of syscall\n\
133 -q -- suppress messages about attaching, detaching, etc.\n\
134 -r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
135 -T -- print time spent in each syscall, -V -- print version\n\
136 -v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\
137 -x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
138 -a column -- alignment COLUMN for printing syscall results (default %d)\n\
139 -e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
140    options: trace, abbrev, verbose, raw, signal, read, or write\n\
141 -o file -- send trace output to FILE instead of stderr\n\
142 -O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
143 -p pid -- trace process with process id PID, may be repeated\n\
144 -s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
145 -S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
146 -u username -- run command as username handling setuid and/or setgid\n\
147 ", DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
148         exit(exitval);
149 }
150
151 #ifdef SVR4
152 #ifdef MIPS
153 void
154 foobar()
155 {
156 }
157 #endif /* MIPS */
158 #endif /* SVR4 */
159
160 int
161 main(argc, argv)
162 int argc;
163 char *argv[];
164 {
165         extern int optind;
166         extern char *optarg;
167         struct tcb *tcp;
168         int c, pid = 0;
169         struct sigaction sa;
170
171         static char buf[BUFSIZ];
172
173         progname = argv[0];
174         outf = stderr;
175         interactive = 1;
176         qualify("trace=all");
177         qualify("abbrev=all");
178         qualify("verbose=all");
179         qualify("signal=all");
180         set_sortby(DEFAULT_SORTBY);
181         set_personality(DEFAULT_PERSONALITY);
182         while ((c = getopt(argc, argv,
183                 "+cdfFhiqrtTvVxa:e:o:O:p:s:S:u:")) != EOF) {
184                 switch (c) {
185                 case 'c':
186                         cflag++;
187                         dtime++;
188                         break;
189                 case 'd':
190                         debug++;
191                         break;
192                 case 'f':
193                         followfork++;
194                         break;
195                 case 'F':
196                         followvfork++;
197                         break;
198                 case 'h':
199                         usage(stdout, 0);
200                         break;
201                 case 'i':
202                         iflag++;
203                         break;
204                 case 'q':
205                         qflag++;
206                         break;
207                 case 'r':
208                         rflag++;
209                         tflag++;
210                         break;
211                 case 't':
212                         tflag++;
213                         break;
214                 case 'T':
215                         dtime++;
216                         break;
217                 case 'x':
218                         xflag++;
219                         break;
220                 case 'v':
221                         qualify("abbrev=none");
222                         break;
223                 case 'V':
224                         printf("%s\n", version);
225                         exit(0);
226                         break;
227                 case 'a':
228                         acolumn = atoi(optarg);
229                         break;
230                 case 'e':
231                         qualify(optarg);
232                         break;
233                 case 'o':
234                         outfname = strdup(optarg);
235                         break;
236                 case 'O':
237                         set_overhead(atoi(optarg));
238                         break;
239                 case 'p':
240                         if ((pid = atoi(optarg)) == 0) {
241                                 fprintf(stderr, "%s: Invalid process id: %s\n",
242                                         progname, optarg);
243                                 break;
244                         }
245                         if (pid == getpid()) {
246                                 fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
247                                 break;
248                         }
249                         if ((tcp = alloctcb(pid)) == NULL) {
250                                 fprintf(stderr, "%s: tcb table full, please recompile strace\n",
251                                         progname);
252                                 exit(1);
253                         }
254                         tcp->flags |= TCB_ATTACHED;
255                         pflag_seen++;
256                         break;
257                 case 's':
258                         max_strlen = atoi(optarg);
259                         break;
260                 case 'S':
261                         set_sortby(optarg);
262                         break;
263                 case 'u':
264                         username = strdup(optarg);
265                         break;
266                 default:
267                         usage(stderr, 1);
268                         break;
269                 }
270         }
271
272         /* See if they want to run as another user. */
273         if (username != NULL) {
274                 struct passwd *pent;
275
276                 if (getuid() != 0 || geteuid() != 0) {
277                         fprintf(stderr,
278                                 "%s: you must be root to use the -u option\n",
279                                 progname);
280                         exit(1);
281                 }
282                 if ((pent = getpwnam(username)) == NULL) {
283                         fprintf(stderr, "%s: cannot find user `%s'\n",
284                                 progname, optarg);
285                         exit(1);
286                 }
287                 run_uid = pent->pw_uid;
288                 run_gid = pent->pw_gid;
289         }
290         else {
291                 run_uid = getuid();
292                 run_gid = getgid();
293         }
294
295 #ifndef SVR4
296         setreuid(geteuid(), getuid());
297 #endif
298
299         /* See if they want to pipe the output. */
300         if (outfname && (outfname[0] == '|' || outfname[0] == '!')) {
301                 if ((outf = popen(outfname + 1, "w")) == NULL) {
302                         fprintf(stderr, "%s: can't popen '%s': %s\n",
303                                 progname, outfname + 1, strerror(errno));
304                         exit(1);
305                 }
306                 free(outfname);
307                 outfname = NULL;
308         }
309
310         /* Check if they want to redirect the output. */
311         if (outfname) {
312                 if ((outf = fopen(outfname, "w")) == NULL) {
313                         fprintf(stderr, "%s: can't fopen '%s': %s\n",
314                                 progname, outfname, strerror(errno));
315                         exit(1);
316                 }
317         }
318
319 #ifndef SVR4
320         setreuid(geteuid(), getuid());
321 #endif
322
323         if (!outfname) {
324                 qflag = 1;
325                 setvbuf(outf, buf, _IOLBF, BUFSIZ);
326         }
327         else if (optind < argc)
328                 interactive = 0;
329         else
330                 qflag = 1;
331
332         for (c = 0, tcp = tcbtab; c < MAX_PROCS; c++, tcp++) {
333                 /* Reinitialize the output since it may have changed. */
334                 tcp->outf = outf;
335                 if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
336                         continue;
337 #ifdef SVR4
338                 if (proc_open(tcp, 1) < 0) {
339                         fprintf(stderr, "trouble opening proc file\n");
340                         droptcb(tcp);
341                         continue;
342                 }
343 #else /* !SVR4 */
344                 if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
345                         perror("attach: ptrace(PTRACE_ATTACH, ...)");
346                         droptcb(tcp);
347                         continue;
348                 }
349 #endif /* !SVR4 */
350                 if (!qflag)
351                         fprintf(stderr,
352                                 "Process %u attached - interrupt to quit\n",
353                                 pid);
354         }
355
356         if (optind < argc) {
357                 struct stat statbuf;
358                 char *filename;
359                 char pathname[MAXPATHLEN];
360
361                 filename = argv[optind];
362                 if (strchr(filename, '/'))
363                         strcpy(pathname, filename);
364 #ifdef USE_DEBUGGING_EXEC
365                 /*
366                  * Debuggers customarily check the current directory
367                  * first regardless of the path but doing that gives
368                  * security geeks a panic attack.
369                  */
370                 else if (stat(filename, &statbuf) == 0)
371                         strcpy(pathname, filename);
372 #endif /* USE_DEBUGGING_EXEC */
373                 else {
374                         char *path;
375                         int m, n, len;
376
377                         for (path = getenv("PATH"); path && *path; path += m) {
378                                 if (strchr(path, ':')) {
379                                         n = strchr(path, ':') - path;
380                                         m = n + 1;
381                                 }
382                                 else
383                                         m = n = strlen(path);
384                                 if (n == 0) {
385                                         getcwd(pathname, MAXPATHLEN);
386                                         len = strlen(pathname);
387                                 }
388                                 else {
389                                         strncpy(pathname, path, n);
390                                         len = n;
391                                 }
392                                 if (len && pathname[len - 1] != '/')
393                                         pathname[len++] = '/';
394                                 strcpy(pathname + len, filename);
395                                 if (stat(pathname, &statbuf) == 0)
396                                         break;
397                         }
398                 }
399                 if (stat(pathname, &statbuf) < 0) {
400                         fprintf(stderr, "%s: %s: command not found\n",
401                                 progname, filename);
402                         exit(1);
403                 }
404                 switch (pid = fork()) {
405                 case -1:
406                         perror("strace: fork");
407                         cleanup();
408                         exit(1);
409                         break;
410                 case 0: {
411 #ifdef SVR4
412                         if (outf != stderr) close (fileno (outf));
413 #ifdef MIPS
414                         /* Kludge for SGI, see proc_open for details. */
415                         sa.sa_handler = foobar;
416                         sa.sa_flags = 0;
417                         sigemptyset(&sa.sa_mask);
418                         sigaction(SIGINT, &sa, NULL);
419 #endif /* MIPS */
420                         pause();
421 #else /* !SVR4 */
422                         if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
423                                 perror("strace: ptrace(PTRACE_TRACEME, ...)");
424                                 return -1;
425                         }
426                         if (debug)
427                                 kill(getpid(), SIGSTOP);
428
429                         if (username != NULL || geteuid() == 0) {
430                                 uid_t run_euid = run_uid;
431                                 gid_t run_egid = run_gid;
432
433                                 if (statbuf.st_mode & S_ISUID)
434                                         run_euid = statbuf.st_uid;
435                                 if (statbuf.st_mode & S_ISGID)
436                                         run_egid = statbuf.st_gid;
437
438                                 /*
439                                  * It is important to set groups before we
440                                  * lose privileges on setuid.
441                                  */
442                                 if (username != NULL
443                                     && initgroups(username, run_gid) < 0) {
444                                         perror("initgroups");
445                                         exit(1);
446                                 }
447                                 if (setregid(run_gid, run_egid) < 0) {
448                                         perror("setregid");
449                                         exit(1);
450                                 }
451                                 if (setreuid(run_uid, run_euid) < 0) {
452                                         perror("setreuid");
453                                         exit(1);
454                                 }
455                         }
456                         else
457                                 setreuid(run_uid, run_uid);
458 #endif /* !SVR4 */
459
460                         execv(pathname, &argv[optind]);
461                         perror("strace: exec");
462                         _exit(1);
463                         break;
464                 }
465                 default:
466                         if ((tcp = alloctcb(pid)) == NULL) {
467                                 fprintf(stderr, "tcb table full\n");
468                                 cleanup();
469                                 exit(1);
470                         }
471 #ifdef SVR4
472                         if (proc_open(tcp, 0) < 0) {
473                                 fprintf(stderr, "trouble opening proc file\n");
474                                 cleanup();
475                                 exit(1);
476                         }
477 #endif /* SVR4 */
478 #ifndef SVR4
479                         fake_execve(tcp, pathname, &argv[optind], environ);
480 #endif
481                         break;
482                 }
483         }
484         else if (pflag_seen == 0)
485                 usage(stderr, 1);
486
487         sigemptyset(&empty_set);
488         sigemptyset(&blocked_set);
489         sa.sa_handler = SIG_IGN;
490         sigemptyset(&sa.sa_mask);
491         sa.sa_flags = 0;
492         sigaction(SIGTTOU, &sa, NULL);
493         sigaction(SIGTTIN, &sa, NULL);
494         if (interactive) {
495                 sigaddset(&blocked_set, SIGHUP);
496                 sigaddset(&blocked_set, SIGINT);
497                 sigaddset(&blocked_set, SIGQUIT);
498                 sigaddset(&blocked_set, SIGPIPE);
499                 sigaddset(&blocked_set, SIGTERM);
500                 sa.sa_handler = interrupt;
501 #ifdef SUNOS4
502                 /* POSIX signals on sunos4.1 are a little broken. */
503                 sa.sa_flags = SA_INTERRUPT;
504 #endif /* SUNOS4 */
505         }
506         sigaction(SIGHUP, &sa, NULL);
507         sigaction(SIGINT, &sa, NULL);
508         sigaction(SIGQUIT, &sa, NULL);
509         sigaction(SIGPIPE, &sa, NULL);
510         sigaction(SIGTERM, &sa, NULL);
511 #ifdef SVR4
512         sa.sa_handler = reaper;
513         sigaction(SIGCHLD, &sa, NULL);
514 #endif /* SVR4 */
515
516         if (trace() < 0)
517                 exit(1);
518         cleanup();
519         exit(0);
520 }
521
522 void
523 newoutf(tcp)
524 struct tcb *tcp;
525 {
526         char name[MAXPATHLEN];
527         FILE *fp;
528
529         if (outfname && followfork > 1) {
530                 sprintf(name, "%s.%u", outfname, tcp->pid);
531 #ifndef SVR4
532                 setreuid(geteuid(), getuid());
533 #endif
534                 fp = fopen(name, "w");
535 #ifndef SVR4
536                 setreuid(geteuid(), getuid());
537 #endif
538                 if (fp == NULL) {
539                         perror("fopen");
540                         return;
541                 }
542                 tcp->outf = fp;
543         }
544         return;
545 }
546
547 struct tcb *
548 alloctcb(pid)
549 int pid;
550 {
551         int i;
552         struct tcb *tcp;
553
554         for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
555                 if ((tcp->flags & TCB_INUSE) == 0) {
556                         tcp->pid = pid;
557                         tcp->parent = NULL;
558                         tcp->nchildren = 0;
559                         tcp->flags = TCB_INUSE | TCB_STARTUP;
560                         tcp->outf = outf; /* Initialise to current out file */
561                         tcp->stime.tv_sec = 0;
562                         tcp->stime.tv_usec = 0;
563                         tcp->pfd = -1;
564                         nprocs++;
565                         return tcp;
566                 }
567         }
568         return NULL;
569 }
570
571 #ifdef SVR4
572 int
573 proc_open(tcp, attaching)
574 struct tcb *tcp;
575 int attaching;
576 {
577         char proc[32];
578         long arg;
579         sysset_t sc_enter, sc_exit;
580         sigset_t signals;
581         fltset_t faults;
582 #ifndef HAVE_POLLABLE_PROCFS
583         static int last_pfd;
584 #endif
585
586 #ifdef HAVE_MP_PROCFS
587         /* Open the process pseudo-files in /proc. */
588         sprintf(proc, "/proc/%d/ctl", tcp->pid);
589         if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
590                 perror("strace: open(\"/proc/...\", ...)");
591                 return -1;
592         }
593         if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
594                 perror("F_GETFD");
595                 return -1;
596         }
597         if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
598                 perror("F_SETFD");
599                 return -1;
600         }
601         sprintf(proc, "/proc/%d/status", tcp->pid);
602         if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
603                 perror("strace: open(\"/proc/...\", ...)");
604                 return -1;
605         }
606         if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) {
607                 perror("F_GETFD");
608                 return -1;
609         }
610         if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) {
611                 perror("F_SETFD");
612                 return -1;
613         }
614         sprintf(proc, "/proc/%d/as", tcp->pid);
615         if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
616                 perror("strace: open(\"/proc/...\", ...)");
617                 return -1;
618         }
619         if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) {
620                 perror("F_GETFD");
621                 return -1;
622         }
623         if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) {
624                 perror("F_SETFD");
625                 return -1;
626         }
627 #else
628         /* Open the process pseudo-file in /proc. */
629         sprintf(proc, "/proc/%d", tcp->pid);
630         if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) {
631                 perror("strace: open(\"/proc/...\", ...)");
632                 return -1;
633         }
634         if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
635                 perror("F_GETFD");
636                 return -1;
637         }
638         if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
639                 perror("F_SETFD");
640                 return -1;
641         }
642 #endif
643         rebuild_pollv();
644         if (!attaching) {
645                 /*
646                  * Wait for the child to pause.  Because of a race
647                  * condition we have to poll for the event.
648                  */
649                 for (;;) {
650                         if (IOCTL_STATUS (tcp) < 0) {
651                                 perror("strace: PIOCSTATUS");
652                                 return -1;
653                         }
654                         if (tcp->status.PR_FLAGS & PR_ASLEEP)
655                                 break;
656                 }
657         }
658         /* Stop the process so that we own the stop. */
659         if (IOCTL(tcp->pfd, PIOCSTOP, NULL) < 0) {
660                 perror("strace: PIOCSTOP");
661                 return -1;
662         }
663 #ifdef PIOCSET
664         /* Set Run-on-Last-Close. */
665         arg = PR_RLC;
666         if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
667                 perror("PIOCSET PR_RLC");
668                 return -1;
669         }
670         /* Set or Reset Inherit-on-Fork. */
671         arg = PR_FORK;
672         if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
673                 perror("PIOC{SET,RESET} PR_FORK");
674                 return -1;
675         }
676 #else  /* !PIOCSET */
677         if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
678                 perror("PIOCSRLC");
679                 return -1;
680         }
681         if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
682                 perror("PIOC{S,R}FORK");
683                 return -1;
684         }
685 #endif /* !PIOCSET */
686         /* Enable all syscall entries. */
687         prfillset(&sc_enter);
688         if (IOCTL(tcp->pfd, PIOCSENTRY, &sc_enter) < 0) {
689                 perror("PIOCSENTRY");
690                 return -1;
691         }
692         /* Enable all syscall exits. */
693         prfillset(&sc_exit);
694         if (IOCTL(tcp->pfd, PIOCSEXIT, &sc_exit) < 0) {
695                 perror("PIOSEXIT");
696                 return -1;
697         }
698         /* Enable all signals. */
699         prfillset(&signals);
700         if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
701                 perror("PIOCSTRACE");
702                 return -1;
703         }
704         /* Enable all faults. */
705         prfillset(&faults);
706         if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
707                 perror("PIOCSFAULT");
708                 return -1;
709         }
710         if (!attaching) {
711 #ifdef MIPS
712                 /*
713                  * The SGI PRSABORT doesn't work for pause() so
714                  * we send it a caught signal to wake it up.
715                  */
716                 kill(tcp->pid, SIGINT);
717 #else /* !MIPS */
718                 /* The child is in a pause(), abort it. */
719                 arg = PRSABORT;
720                 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
721                         perror("PIOCRUN");
722                         return -1;
723                 }
724 #endif /* !MIPS */
725                 for (;;) {
726                         /* Wait for the child to do something. */
727                         if (IOCTL_WSTOP (tcp) < 0) {
728                                 perror("PIOCWSTOP");
729                                 return -1;
730                         }
731                         if (tcp->status.PR_WHY == PR_SYSENTRY) {
732 #ifdef HAVE_PR_SYSCALL
733                                 int scno = tcp->status.pr_syscall;
734 #else /* !HAVE_PR_SYSCALL */
735                                 int scno = tcp->status.PR_WHAT;
736 #endif /* !HAVE_PR_SYSCALL */
737                                 if (scno == SYS_execve)
738                                         break;
739                         }
740                         /* Set it running: maybe execve will be next. */
741                         arg = 0;
742                         if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
743                                 perror("PIOCRUN");
744                                 return -1;
745                         }
746                 }
747         }
748 #ifndef HAVE_POLLABLE_PROCFS
749         if (proc_poll_pipe[0] != -1)
750                 proc_poller(tcp->pfd);
751         else if (nprocs > 1) {
752                 proc_poll_open();
753                 proc_poller(last_pfd);
754                 proc_poller(tcp->pfd);
755         }
756         last_pfd = tcp->pfd;
757 #endif /* !HAVE_POLLABLE_PROCFS */
758         return 0;
759 }
760
761 #endif /* SVR4 */
762
763 static struct tcb *
764 pid2tcb(pid)
765 int pid;
766 {
767         int i;
768         struct tcb *tcp;
769
770         for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
771                 if (pid && tcp->pid != pid)
772                         continue;
773                 if (tcp->flags & TCB_INUSE)
774                         return tcp;
775         }
776         return NULL;
777 }
778
779 #ifdef SVR4
780
781 static struct tcb *
782 pfd2tcb(pfd)
783 int pfd;
784 {
785         int i;
786         struct tcb *tcp;
787
788         for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
789                 if (tcp->pfd != pfd)
790                         continue;
791                 if (tcp->flags & TCB_INUSE)
792                         return tcp;
793         }
794         return NULL;
795 }
796
797 #endif /* SVR4 */
798
799 void
800 droptcb(tcp)
801 struct tcb *tcp;
802 {
803         if (tcp->pid == 0)
804                 return;
805         nprocs--;
806         tcp->pid = 0;
807         tcp->flags = 0;
808         if (tcp->pfd != -1) {
809                 close(tcp->pfd);
810                 tcp->pfd = -1;
811 #ifdef SVR4
812                 rebuild_pollv();
813 #endif
814         }
815         if (tcp->parent != NULL) {
816                 tcp->parent->nchildren--;
817                 tcp->parent = NULL;
818         }
819 #if 0
820         if (tcp->outf != stderr)
821                 fclose(tcp->outf);
822 #endif
823         tcp->outf = 0;
824 }
825
826 #ifndef SVR4
827
828 static int
829 resume(tcp)
830 struct tcb *tcp;
831 {
832         if (tcp == NULL)
833                 return -1;
834
835         if (!(tcp->flags & TCB_SUSPENDED)) {
836                 fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid);
837                 return -1;
838         }
839         tcp->flags &= ~TCB_SUSPENDED;
840
841         if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) {
842                 perror("resume: ptrace(PTRACE_SYSCALL, ...)");
843                 return -1;
844         }
845
846         if (!qflag)
847                 fprintf(stderr, "Process %u resumed\n", tcp->pid);
848         return 0;
849 }
850
851 #endif /* !SVR4 */
852
853 /* detach traced process; continue with sig */
854
855 static int
856 detach(tcp, sig)
857 struct tcb *tcp;
858 int sig;
859 {
860         int error = 0;
861 #ifdef LINUX
862         int status;
863 #endif
864
865         if (tcp->flags & TCB_BPTSET)
866                 sig = SIGKILL;
867
868 #ifdef LINUX
869         /*
870          * Linux wrongly insists the child be stopped
871          * before detaching.  Arghh.  We go through hoops
872          * to make a clean break of things.
873          */
874 #if defined(SPARC)
875 #undef PTRACE_DETACH
876 #define PTRACE_DETACH PTRACE_SUNDETACH
877 #endif
878         if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
879                 /* On a clear day, you can see forever. */
880         }
881         else if (errno != ESRCH) {
882                 /* Shouldn't happen. */
883                 perror("detach: ptrace(PTRACE_DETACH, ...)");
884         }
885         else if (kill(tcp->pid, 0) < 0) {
886                 if (errno != ESRCH)
887                         perror("detach: checking sanity");
888         }
889         else if (kill(tcp->pid, SIGSTOP) < 0) {
890                 if (errno != ESRCH)
891                         perror("detach: stopping child");
892         }
893         else {
894                 for (;;) {
895                         if (waitpid(tcp->pid, &status, 0) < 0) {
896                                 if (errno != ECHILD)
897                                         perror("detach: waiting");
898                                 break;
899                         }
900                         if (!WIFSTOPPED(status)) {
901                                 /* Au revoir, mon ami. */
902                                 break;
903                         }
904                         if (WSTOPSIG(status) == SIGSTOP) {
905                                 if ((error = ptrace(PTRACE_DETACH,
906                                     tcp->pid, (char *) 1, sig)) < 0) {
907                                         if (errno != ESRCH)
908                                                 perror("detach: ptrace(PTRACE_DETACH, ...)");
909                                         /* I died trying. */
910                                 }
911                                 break;
912                         }
913                         if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1,
914                             WSTOPSIG(status) == SIGTRAP ?
915                             0 : WSTOPSIG(status))) < 0) {
916                                 if (errno != ESRCH)
917                                         perror("detach: ptrace(PTRACE_CONT, ...)");
918                                 break;
919                         }
920                 }
921         }
922 #endif /* LINUX */
923
924 #if defined(SUNOS4)
925         /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
926         if (sig && kill(tcp->pid, sig) < 0)
927                 perror("detach: kill");
928         sig = 0;
929         if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0)
930                 perror("detach: ptrace(PTRACE_DETACH, ...)");
931 #endif /* SUNOS4 */
932
933 #ifndef SVR4
934         if (waiting_parent(tcp))
935                 error = resume(tcp->parent);
936 #endif /* !SVR4 */
937
938         if (!qflag)
939                 fprintf(stderr, "Process %u detached\n", tcp->pid);
940
941         droptcb(tcp);
942         return error;
943 }
944
945 #ifdef SVR4
946
947 static void
948 reaper(sig)
949 int sig;
950 {
951         int pid;
952         int status;
953
954         while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
955 #if 0
956                 struct tcb *tcp;
957
958                 tcp = pid2tcb(pid);
959                 if (tcp)
960                         droptcb(tcp);
961 #endif
962         }
963 }
964
965 #endif /* SVR4 */
966
967 static void
968 cleanup()
969 {
970         int i;
971         struct tcb *tcp;
972
973         for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
974                 if (!(tcp->flags & TCB_INUSE))
975                         continue;
976                 if (debug)
977                         fprintf(stderr,
978                                 "cleanup: looking at pid %u\n", tcp->pid);
979                 if (tcp_last &&
980                     (!outfname || followfork < 2 || tcp_last == tcp)) {
981                         tprintf(" <unfinished ...>\n");
982                         tcp_last = NULL;
983                 }
984                 if (tcp->flags & TCB_ATTACHED)
985                         detach(tcp, 0);
986                 else {
987                         kill(tcp->pid, SIGCONT);
988                         kill(tcp->pid, SIGTERM);
989                 }
990         }
991         if (cflag)
992                 call_summary(outf);
993 }
994
995 static void
996 interrupt(sig)
997 int sig;
998 {
999         interrupted = 1;
1000 }
1001
1002 #ifndef HAVE_STRERROR
1003
1004 #ifndef SYS_ERRLIST_DECLARED
1005 extern int sys_nerr;
1006 extern char *sys_errlist[];
1007 #endif /* SYS_ERRLIST_DECLARED */
1008
1009 const char *
1010 strerror(errno)
1011 int errno;
1012 {
1013         static char buf[64];
1014
1015         if (errno < 1 || errno >= sys_nerr) {
1016                 sprintf(buf, "Unknown error %d", errno);
1017                 return buf;
1018         }
1019         return sys_errlist[errno];
1020 }
1021
1022 #endif /* HAVE_STERRROR */
1023
1024 #ifndef HAVE_STRSIGNAL
1025
1026 #ifndef SYS_SIGLIST_DECLARED
1027 #ifdef HAVE__SYS_SIGLIST
1028         extern char *_sys_siglist[];
1029 #else
1030         extern char *sys_siglist[];
1031 #endif
1032 #endif /* SYS_SIGLIST_DECLARED */
1033
1034 const char *
1035 strsignal(sig)
1036 int sig;
1037 {
1038         static char buf[64];
1039
1040         if (sig < 1 || sig >= NSIG) {
1041                 sprintf(buf, "Unknown signal %d", sig);
1042                 return buf;
1043         }
1044 #ifdef HAVE__SYS_SIGLIST
1045         return _sys_siglist[sig];
1046 #else
1047         return sys_siglist[sig];
1048 #endif
1049 }
1050
1051 #endif /* HAVE_STRSIGNAL */
1052
1053 #ifdef SVR4
1054
1055 static void
1056 rebuild_pollv()
1057 {
1058         int i, j;
1059         struct tcb *tcp;
1060
1061         for (i = j = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1062                 if (!(tcp->flags & TCB_INUSE))
1063                         continue;
1064                 pollv[j].fd = tcp->pfd;
1065                 pollv[j].events = POLLWANT;
1066                 j++;
1067         }
1068         if (j != nprocs) {
1069                 fprintf(stderr, "strace: proc miscount\n");
1070                 exit(1);
1071         }
1072 }
1073
1074 #ifndef HAVE_POLLABLE_PROCFS
1075
1076 static void
1077 proc_poll_open()
1078 {
1079         int arg;
1080         int i;
1081
1082         if (pipe(proc_poll_pipe) < 0) {
1083                 perror("pipe");
1084                 exit(1);
1085         }
1086         for (i = 0; i < 2; i++) {
1087                 if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) {
1088                         perror("F_GETFD");
1089                         exit(1);
1090                 }
1091                 if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) {
1092                         perror("F_SETFD");
1093                         exit(1);
1094                 }
1095         }
1096 }
1097
1098 static int
1099 proc_poll(pollv, nfds, timeout)
1100 struct pollfd *pollv;
1101 int nfds;
1102 int timeout;
1103 {
1104         int i;
1105         int n;
1106         struct proc_pollfd pollinfo;
1107
1108         if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
1109                 return n;
1110         if (n != sizeof(struct proc_pollfd)) {
1111                 fprintf(stderr, "panic: short read: %d\n", n);
1112                 exit(1);
1113         }
1114         for (i = 0; i < nprocs; i++) {
1115                 if (pollv[i].fd == pollinfo.fd)
1116                         pollv[i].revents = pollinfo.revents;
1117                 else
1118                         pollv[i].revents = 0;
1119         }
1120         poller_pid = pollinfo.pid;
1121         return 1;
1122 }
1123
1124 static void
1125 wakeup_handler(sig)
1126 int sig;
1127 {
1128 }
1129
1130 static void
1131 proc_poller(pfd)
1132 int pfd;
1133 {
1134         struct proc_pollfd pollinfo;
1135         struct sigaction sa;
1136         sigset_t blocked_set, empty_set;
1137         int i;
1138         int n;
1139         struct rlimit rl;
1140
1141         switch (fork()) {
1142         case -1:
1143                 perror("fork");
1144                 _exit(0);
1145         case 0:
1146                 break;
1147         default:
1148                 return;
1149         }
1150
1151         sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
1152         sa.sa_flags = 0;
1153         sigemptyset(&sa.sa_mask);
1154         sigaction(SIGHUP, &sa, NULL);
1155         sigaction(SIGINT, &sa, NULL);
1156         sigaction(SIGQUIT, &sa, NULL);
1157         sigaction(SIGPIPE, &sa, NULL);
1158         sigaction(SIGTERM, &sa, NULL);
1159         sa.sa_handler = wakeup_handler;
1160         sigaction(SIGUSR1, &sa, NULL);
1161         sigemptyset(&blocked_set);
1162         sigaddset(&blocked_set, SIGUSR1);
1163         sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1164         sigemptyset(&empty_set);
1165
1166         if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
1167                 perror("getrlimit(RLIMIT_NOFILE, ...)");
1168                 _exit(0);
1169         }
1170         n = rl.rlim_cur;
1171         for (i = 0; i < n; i++) {
1172                 if (i != pfd && i != proc_poll_pipe[1])
1173                         close(i);
1174         }
1175
1176         pollinfo.fd = pfd;
1177         pollinfo.pid = getpid();
1178         for (;;) {
1179                 if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
1180                 {
1181                         switch (errno) {
1182                         case EINTR:
1183                                 continue;
1184                         case EBADF:
1185                                 pollinfo.revents = POLLERR;
1186                                 break;
1187                         case ENOENT:
1188                                 pollinfo.revents = POLLHUP;
1189                                 break;
1190                         default:
1191                                 perror("proc_poller: PIOCWSTOP");
1192                         }
1193                         write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1194                         _exit(0);
1195                 }
1196                 pollinfo.revents = POLLWANT;
1197                 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1198                 sigsuspend(&empty_set);
1199         }
1200 }
1201
1202 #endif /* !HAVE_POLLABLE_PROCFS */
1203
1204 static int
1205 choose_pfd()
1206 {
1207         int i, j;
1208         struct tcb *tcp;
1209
1210         static int last;
1211
1212         if (followfork < 2 &&
1213             last < nprocs && (pollv[last].revents & POLLWANT)) {
1214                 /*
1215                  * The previous process is ready to run again.  We'll
1216                  * let it do so if it is currently in a syscall.  This
1217                  * heuristic improves the readability of the trace.
1218                  */
1219                 tcp = pfd2tcb(pollv[last].fd);
1220                 if (tcp && (tcp->flags & TCB_INSYSCALL))
1221                         return pollv[last].fd;
1222         }
1223
1224         for (i = 0; i < nprocs; i++) {
1225                 /* Let competing children run round robin. */
1226                 j = (i + last + 1) % nprocs;
1227                 if (pollv[j].revents & (POLLHUP | POLLERR)) {
1228                         tcp = pfd2tcb(pollv[j].fd);
1229                         if (!tcp) {
1230                                 fprintf(stderr, "strace: lost proc\n");
1231                                 exit(1);
1232                         }
1233                         droptcb(tcp);
1234                         return -1;
1235                 }
1236                 if (pollv[j].revents & POLLWANT) {
1237                         last = j;
1238                         return pollv[j].fd;
1239                 }
1240         }
1241         fprintf(stderr, "strace: nothing ready\n");
1242         exit(1);
1243 }
1244
1245 static int
1246 trace()
1247 {
1248 #ifdef POLL_HACK
1249         struct tcb *in_syscall;
1250 #endif
1251         struct tcb *tcp;
1252         int pfd;
1253         int what;
1254         int ioctl_result = 0, ioctl_errno = 0;
1255         long arg;
1256
1257         for (;;) {
1258                 if (interactive)
1259                         sigprocmask(SIG_SETMASK, &empty_set, NULL);
1260
1261                 if (nprocs == 0)
1262                         break;
1263
1264                 switch (nprocs) {
1265                 case 1:
1266 #ifndef HAVE_POLLABLE_PROCFS
1267                         if (proc_poll_pipe[0] == -1) {
1268 #endif
1269                                 tcp = pid2tcb(0);
1270                                 if (!tcp)
1271                                         continue;
1272                                 pfd = tcp->pfd;
1273                                 if (pfd == -1)
1274                                         continue;
1275                                 break;
1276 #ifndef HAVE_POLLABLE_PROCFS
1277                         }
1278                         /* fall through ... */
1279 #endif /* !HAVE_POLLABLE_PROCFS */
1280                 default:
1281 #ifdef HAVE_POLLABLE_PROCFS
1282 #ifdef POLL_HACK
1283                         /* On some systems (e.g. UnixWare) we get too much ugly
1284                            "unfinished..." stuff when multiple proceses are in
1285                            syscalls.  Here's a nasty hack */
1286                     
1287                         if (in_syscall) {
1288                                 struct pollfd pv;
1289                                 tcp = in_syscall;
1290                                 in_syscall = NULL;
1291                                 pv.fd = tcp->pfd;
1292                                 pv.events = POLLWANT;
1293                                 if ((what = poll (&pv, 1, 1)) < 0) {
1294                                         if (interrupted)
1295                                                 return 0;
1296                                         continue;
1297                                 }
1298                                 else if (what == 1 && pv.revents & POLLWANT) {
1299                                         goto FOUND;
1300                                 }
1301                         }
1302 #endif
1303
1304                         if (poll(pollv, nprocs, INFTIM) < 0) {
1305                                 if (interrupted)
1306                                         return 0;
1307                                 continue;
1308                         }
1309 #else /* !HAVE_POLLABLE_PROCFS */
1310                         if (proc_poll(pollv, nprocs, INFTIM) < 0) {
1311                                 if (interrupted)
1312                                         return 0;
1313                                 continue;
1314                         }
1315 #endif /* !HAVE_POLLABLE_PROCFS */
1316                         pfd = choose_pfd();
1317                         if (pfd == -1)
1318                                 continue;
1319                         break;
1320                 }
1321
1322                 /* Look up `pfd' in our table. */
1323                 if ((tcp = pfd2tcb(pfd)) == NULL) {
1324                         fprintf(stderr, "unknown pfd: %u\n", pfd);
1325                         exit(1);
1326                 }
1327         FOUND:
1328                 /* Get the status of the process. */
1329                 if (!interrupted) {
1330                         ioctl_result = IOCTL_WSTOP (tcp);
1331                         ioctl_errno = errno;
1332 #ifndef HAVE_POLLABLE_PROCFS
1333                         if (proc_poll_pipe[0] != -1) {
1334                                 if (ioctl_result < 0)
1335                                         kill(poller_pid, SIGKILL);
1336                                 else
1337                                         kill(poller_pid, SIGUSR1);
1338                         }
1339 #endif /* !HAVE_POLLABLE_PROCFS */
1340                 }
1341                 if (interrupted)
1342                         return 0;
1343
1344                 if (interactive)
1345                         sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1346
1347                 if (ioctl_result < 0) {
1348                         /* Find out what happened if it failed. */
1349                         switch (ioctl_errno) {
1350                         case EINTR:
1351                         case EBADF:
1352                                 continue;
1353                         case ENOENT:
1354                                 droptcb(tcp);
1355                                 continue;
1356                         default:
1357                                 perror("PIOCWSTOP");
1358                                 exit(1);
1359                         }
1360                 }
1361
1362                 /* clear the just started flag */
1363                 tcp->flags &= ~TCB_STARTUP;
1364
1365                 /* set current output file */
1366                 outf = tcp->outf;
1367
1368                 if (cflag) {
1369                         struct timeval stime;
1370
1371                         stime.tv_sec = tcp->status.pr_stime.tv_sec;
1372                         stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
1373                         tv_sub(&tcp->dtime, &stime, &tcp->stime);
1374                         tcp->stime = stime;
1375                 }
1376
1377                 what = tcp->status.PR_WHAT;
1378                 switch (tcp->status.PR_WHY) {
1379                 case PR_REQUESTED:
1380                         if (tcp->status.PR_FLAGS & PR_ASLEEP) {
1381                                 tcp->status.PR_WHY = PR_SYSENTRY;
1382                                 if (trace_syscall(tcp) < 0) {
1383                                         fprintf(stderr, "syscall trouble\n");
1384                                         exit(1);
1385                                 }
1386                         }
1387                         break;
1388                 case PR_SYSENTRY:
1389 #ifdef POLL_HACK
1390                         in_syscall = tcp;
1391 #endif
1392                 case PR_SYSEXIT:
1393                         if (trace_syscall(tcp) < 0) {
1394                                 fprintf(stderr, "syscall trouble\n");
1395                                 exit(1);
1396                         }
1397                         break;
1398                 case PR_SIGNALLED:
1399                         if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) {
1400                                 printleader(tcp);
1401                                 tprintf("--- %s (%s) ---",
1402                                         signame(what), strsignal(what));
1403                                 printtrailer(tcp);
1404                         }
1405                         break;
1406                 case PR_FAULTED:
1407                         if (!cflag && (qual_flags[what] & QUAL_FAULT)) {
1408                                 printleader(tcp);
1409                                 tprintf("=== FAULT %d ===", what);
1410                                 printtrailer(tcp);
1411                         }
1412                         break;
1413                 default:
1414                         fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
1415                         exit(1);
1416                         break;
1417                 }
1418                 arg = 0;
1419                 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
1420                         perror("PIOCRUN");
1421                         exit(1);
1422                 }
1423         }
1424         return 0;
1425 }
1426
1427 #else /* !SVR4 */
1428
1429 static int
1430 trace()
1431 {
1432         int pid;
1433         int wait_errno;
1434         int status;
1435         struct tcb *tcp;
1436 #ifdef LINUX
1437         struct rusage ru;
1438 #endif /* LINUX */
1439
1440         while (nprocs != 0) {
1441                 if (interactive)
1442                         sigprocmask(SIG_SETMASK, &empty_set, NULL);
1443 #ifdef LINUX
1444                 pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
1445 #endif /* LINUX */
1446 #ifdef SUNOS4
1447                 pid = wait(&status);
1448 #endif /* SUNOS4 */
1449                 wait_errno = errno;
1450                 if (interactive)
1451                         sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1452
1453                 if (interrupted)
1454                         return 0;
1455
1456                 if (pid == -1) {
1457                         switch (wait_errno) {
1458                         case EINTR:
1459                                 continue;
1460                         case ECHILD:
1461                                 /*
1462                                  * We would like to verify this case
1463                                  * but sometimes a race in Solbourne's
1464                                  * version of SunOS sometimes reports
1465                                  * ECHILD before sending us SIGCHILD.
1466                                  */
1467 #if 0
1468                                 if (nprocs == 0)
1469                                         return 0;
1470                                 fprintf(stderr, "strace: proc miscount\n");
1471                                 exit(1);
1472 #endif
1473                                 return 0;
1474                         default:
1475                                 errno = wait_errno;
1476                                 perror("strace: wait");
1477                                 return -1;
1478                         }
1479                 }
1480                 if (debug)
1481                         fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
1482
1483                 /* Look up `pid' in our table. */
1484                 if ((tcp = pid2tcb(pid)) == NULL) {
1485 #if 0 /* XXX davidm */ /* WTA: disabled again */
1486                         struct tcb *tcpchild;
1487
1488                         if ((tcpchild = alloctcb(pid)) == NULL) {
1489                                 fprintf(stderr, " [tcb table full]\n");
1490                                 kill(pid, SIGKILL); /* XXX */
1491                                 return 0;
1492                         }
1493                         tcpchild->flags |= TCB_ATTACHED;
1494                         newoutf(tcpchild);
1495                         tcp->nchildren++;
1496                         if (!qflag)
1497                                 fprintf(stderr, "Process %d attached\n", pid);
1498 #else
1499                         fprintf(stderr, "unknown pid: %u\n", pid);
1500                         if (WIFSTOPPED(status))
1501                                 ptrace(PTRACE_CONT, pid, (char *) 1, 0);
1502                         exit(1);
1503 #endif
1504                 }
1505                 /* set current output file */
1506                 outf = tcp->outf;
1507                 if (cflag) {
1508 #ifdef LINUX
1509                         tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
1510                         tcp->stime = ru.ru_stime;
1511 #endif /* !LINUX */
1512                 }
1513
1514                 if (tcp->flags & TCB_SUSPENDED) {
1515                         /*
1516                          * Apparently, doing any ptrace() call on a stopped
1517                          * process, provokes the kernel to report the process
1518                          * status again on a subsequent wait(), even if the
1519                          * process has not been actually restarted.
1520                          * Since we have inspected the arguments of suspended
1521                          * processes we end up here testing for this case.
1522                          */
1523                         continue;
1524                 }
1525                 if (WIFSIGNALED(status)) {
1526                         if (!cflag
1527                             && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
1528                                 printleader(tcp);
1529                                 tprintf("+++ killed by %s +++",
1530                                         signame(WTERMSIG(status)));
1531                                 printtrailer(tcp);
1532                         }
1533                         droptcb(tcp);
1534                         continue;
1535                 }
1536                 if (WIFEXITED(status)) {
1537                         if (debug)
1538                                 fprintf(stderr, "pid %u exited\n", pid);
1539                         if (tcp->flags & TCB_ATTACHED)
1540                                 fprintf(stderr,
1541                                         "PANIC: attached pid %u exited\n",
1542                                         pid);
1543                         droptcb(tcp);
1544                         continue;
1545                 }
1546                 if (!WIFSTOPPED(status)) {
1547                         fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
1548                         droptcb(tcp);
1549                         continue;
1550                 }
1551                 if (debug)
1552                         fprintf(stderr, "pid %u stopped, [%s]\n",
1553                                 pid, signame(WSTOPSIG(status)));
1554
1555                 if (tcp->flags & TCB_STARTUP) {
1556                         /*
1557                          * This flag is there to keep us in sync.
1558                          * Next time this process stops it should
1559                          * really be entering a system call.
1560                          */
1561                         tcp->flags &= ~TCB_STARTUP;
1562                         if (tcp->flags & TCB_ATTACHED) {
1563                                 /*
1564                                  * Interestingly, the process may stop
1565                                  * with STOPSIG equal to some other signal
1566                                  * than SIGSTOP if we happend to attach
1567                                  * just before the process takes a signal.
1568                                  */
1569                                 if (!WIFSTOPPED(status)) {
1570                                         fprintf(stderr,
1571                                                 "pid %u not stopped\n", pid);
1572                                         detach(tcp, WSTOPSIG(status));
1573                                         continue;
1574                                 }
1575                         }
1576                         else {
1577 #ifdef SUNOS4
1578                                 /* A child of us stopped at exec */
1579                                 if (WSTOPSIG(status) == SIGTRAP && followvfork)
1580                                         fixvfork(tcp);
1581 #endif /* SUNOS4 */
1582                         }
1583                         if (tcp->flags & TCB_BPTSET) {
1584                                 if (clearbpt(tcp) < 0) /* Pretty fatal */ {
1585                                         droptcb(tcp);
1586                                         cleanup();
1587                                         return -1;
1588                                 }
1589                         }
1590                         goto tracing;
1591                 }
1592
1593                 if (WSTOPSIG(status) != SIGTRAP) {
1594                         if (WSTOPSIG(status) == SIGSTOP &&
1595                                         (tcp->flags & TCB_SIGTRAPPED)) {
1596                                 /*
1597                                  * Trapped attempt to block SIGTRAP
1598                                  * Hope we are back in control now.
1599                                  */
1600                                 tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
1601                                 if (ptrace(PTRACE_SYSCALL,
1602                                                 pid, (char *) 1, 0) < 0) {
1603                                         perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1604                                         cleanup();
1605                                         return -1;
1606                                 }
1607                                 continue;
1608                         }
1609                         if (!cflag
1610                             && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
1611                                 printleader(tcp);
1612                                 tprintf("--- %s (%s) ---",
1613                                         signame(WSTOPSIG(status)),
1614                                         strsignal(WSTOPSIG(status)));
1615                                 printtrailer(tcp);
1616                         }
1617                         if ((tcp->flags & TCB_ATTACHED) &&
1618                                 !sigishandled(tcp, WSTOPSIG(status))) {
1619                                 detach(tcp, WSTOPSIG(status));
1620                                 continue;
1621                         }
1622                         if (ptrace(PTRACE_SYSCALL, pid, (char *) 1,
1623                                    WSTOPSIG(status)) < 0) {
1624                                 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1625                                 cleanup();
1626                                 return -1;
1627                         }
1628                         tcp->flags &= ~TCB_SUSPENDED;
1629                         continue;
1630                 }
1631                 if (trace_syscall(tcp) < 0) {
1632                         if (tcp->flags & TCB_ATTACHED)
1633                                 detach(tcp, 0);
1634                         else {
1635                                 ptrace(PTRACE_KILL,
1636                                         tcp->pid, (char *) 1, SIGTERM);
1637                                 droptcb(tcp);
1638                         }
1639                         continue;
1640                 }
1641                 if (tcp->flags & TCB_EXITING) {
1642                         if (tcp->flags & TCB_ATTACHED)
1643                                 detach(tcp, 0);
1644                         else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) {
1645                                 perror("strace: ptrace(PTRACE_CONT, ...)");
1646                                 cleanup();
1647                                 return -1;
1648                         }
1649                         continue;
1650                 }
1651                 if (tcp->flags & TCB_SUSPENDED) {
1652                         if (!qflag)
1653                                 fprintf(stderr, "Process %u suspended\n", pid);
1654                         continue;
1655                 }
1656         tracing:
1657                 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) {
1658                         perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1659                         cleanup();
1660                         return -1;
1661                 }
1662         }
1663         return 0;
1664 }
1665
1666 #endif /* !SVR4 */
1667
1668 static int curcol;
1669
1670 #ifdef __STDC__
1671 #include <stdarg.h>
1672 #define VA_START(a, b) va_start(a, b)
1673 #else
1674 #include <varargs.h>
1675 #define VA_START(a, b) va_start(a)
1676 #endif
1677
1678 void
1679 #ifdef __STDC__
1680 tprintf(const char *fmt, ...)
1681 #else
1682 tprintf(fmt, va_alist)
1683 char *fmt;
1684 va_dcl
1685 #endif
1686 {
1687         va_list args;
1688
1689         VA_START(args, fmt);
1690         if (outf)
1691                 curcol += vfprintf(outf, fmt, args);
1692         va_end(args);
1693         return;
1694 }
1695
1696 void
1697 printleader(tcp)
1698 struct tcb *tcp;
1699 {
1700         if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) {
1701                 tcp_last->flags |= TCB_REPRINT;
1702                 tprintf(" <unfinished ...>\n");
1703         }
1704         curcol = 0;
1705         if ((followfork == 1 || pflag_seen > 1) && outfname)
1706                 tprintf("%-5d ", tcp->pid);
1707         else if (nprocs > 1 && !outfname)
1708                 tprintf("[pid %5u] ", tcp->pid);
1709         if (tflag) {
1710                 char str[sizeof("HH:MM:SS")];
1711                 struct timeval tv, dtv;
1712                 static struct timeval otv;
1713
1714                 gettimeofday(&tv, NULL);
1715                 if (rflag) {
1716                         if (otv.tv_sec == 0)
1717                                 otv = tv;
1718                         tv_sub(&dtv, &tv, &otv);
1719                         tprintf("%6ld.%06ld ",
1720                                 (long) dtv.tv_sec, (long) dtv.tv_usec);
1721                         otv = tv;
1722                 }
1723                 else if (tflag > 2) {
1724                         tprintf("%ld.%06ld ",
1725                                 (long) tv.tv_sec, (long) tv.tv_usec);
1726                 }
1727                 else {
1728                         time_t local = tv.tv_sec;
1729                         strftime(str, sizeof(str), "%T", localtime(&local));
1730                         if (tflag > 1)
1731                                 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
1732                         else
1733                                 tprintf("%s ", str);
1734                 }
1735         }
1736         if (iflag)
1737                 printcall(tcp);
1738 }
1739
1740 void
1741 tabto(col)
1742 int col;
1743 {
1744         if (curcol < col)
1745                 tprintf("%*s", col - curcol, "");
1746 }
1747
1748 void
1749 printtrailer(tcp)
1750 struct tcb *tcp;
1751 {
1752         tprintf("\n");
1753         tcp_last = NULL;
1754 }
1755
1756 #ifdef HAVE_MP_PROCFS
1757
1758 int mp_ioctl (int fd, int cmd, void *arg, int size) {
1759
1760         struct iovec iov[2];
1761         int n = 1;
1762         
1763         iov[0].iov_base = &cmd;
1764         iov[0].iov_len = sizeof cmd;
1765         if (arg) {
1766                 ++n;
1767                 iov[1].iov_base = arg;
1768                 iov[1].iov_len = size;
1769         }
1770         
1771         return writev (fd, iov, n);
1772 }
1773
1774 #endif