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