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