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