]> granicus.if.org Git - strace/blob - strace.c
Fix preadv/pwritev offset decoding on bigendian architectures
[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
31 #include "defs.h"
32 #include <stdarg.h>
33 #include <sys/param.h>
34 #include <fcntl.h>
35 #include <sys/resource.h>
36 #include <sys/wait.h>
37 #include <sys/stat.h>
38 #include <pwd.h>
39 #include <grp.h>
40 #include <dirent.h>
41 #include <sys/utsname.h>
42 #ifdef HAVE_PRCTL
43 # include <sys/prctl.h>
44 #endif
45 #if defined(IA64)
46 # include <asm/ptrace_offsets.h>
47 #endif
48 /* In some libc, these aren't declared. Do it ourself: */
49 extern char **environ;
50 extern int optind;
51 extern char *optarg;
52
53 #ifdef USE_LIBUNWIND
54 /* if this is true do the stack trace for every system call */
55 bool stack_trace_enabled = false;
56 #endif
57
58 #if defined __NR_tkill
59 # define my_tkill(tid, sig) syscall(__NR_tkill, (tid), (sig))
60 #else
61    /* kill() may choose arbitrarily the target task of the process group
62       while we later wait on a that specific TID.  PID process waits become
63       TID task specific waits for a process under ptrace(2).  */
64 # warning "tkill(2) not available, risk of strace hangs!"
65 # define my_tkill(tid, sig) kill((tid), (sig))
66 #endif
67
68 /* Glue for systems without a MMU that cannot provide fork() */
69 #if !defined(HAVE_FORK)
70 # undef NOMMU_SYSTEM
71 # define NOMMU_SYSTEM 1
72 #endif
73 #if NOMMU_SYSTEM
74 # define fork() vfork()
75 #endif
76
77 cflag_t cflag = CFLAG_NONE;
78 unsigned int followfork = 0;
79 unsigned int ptrace_setoptions = 0;
80 unsigned int xflag = 0;
81 bool need_fork_exec_workarounds = 0;
82 bool debug_flag = 0;
83 bool Tflag = 0;
84 bool iflag = 0;
85 bool count_wallclock = 0;
86 unsigned int qflag = 0;
87 /* Which WSTOPSIG(status) value marks syscall traps? */
88 static unsigned int syscall_trap_sig = SIGTRAP;
89 static unsigned int tflag = 0;
90 static bool rflag = 0;
91 static bool print_pid_pfx = 0;
92
93 /* -I n */
94 enum {
95     INTR_NOT_SET        = 0,
96     INTR_ANYWHERE       = 1, /* don't block/ignore any signals */
97     INTR_WHILE_WAIT     = 2, /* block fatal signals while decoding syscall. default */
98     INTR_NEVER          = 3, /* block fatal signals. default if '-o FILE PROG' */
99     INTR_BLOCK_TSTP_TOO = 4, /* block fatal signals and SIGTSTP (^Z) */
100     NUM_INTR_OPTS
101 };
102 static int opt_intr;
103 /* We play with signal mask only if this mode is active: */
104 #define interactive (opt_intr == INTR_WHILE_WAIT)
105
106 /*
107  * daemonized_tracer supports -D option.
108  * With this option, strace forks twice.
109  * Unlike normal case, with -D *grandparent* process exec's,
110  * becoming a traced process. Child exits (this prevents traced process
111  * from having children it doesn't expect to have), and grandchild
112  * attaches to grandparent similarly to strace -p PID.
113  * This allows for more transparent interaction in cases
114  * when process and its parent are communicating via signals,
115  * wait() etc. Without -D, strace process gets lodged in between,
116  * disrupting parent<->child link.
117  */
118 static bool daemonized_tracer = 0;
119
120 #if USE_SEIZE
121 static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP;
122 # define use_seize (post_attach_sigstop == 0)
123 #else
124 # define post_attach_sigstop TCB_IGNORE_ONE_SIGSTOP
125 # define use_seize 0
126 #endif
127
128 /* Sometimes we want to print only succeeding syscalls. */
129 bool not_failing_only = 0;
130
131 /* Show path associated with fd arguments */
132 bool show_fd_path = 0;
133
134 static bool detach_on_execve = 0;
135 /* Are we "strace PROG" and need to skip detach on first execve? */
136 static bool skip_one_b_execve = 0;
137 /* Are we "strace PROG" and need to hide everything until execve? */
138 bool hide_log_until_execve = 0;
139
140 static int exit_code = 0;
141 static int strace_child = 0;
142 static int strace_tracer_pid = 0;
143
144 static char *username = NULL;
145 static uid_t run_uid;
146 static gid_t run_gid;
147
148 unsigned int max_strlen = DEFAULT_STRLEN;
149 static int acolumn = DEFAULT_ACOLUMN;
150 static char *acolumn_spaces;
151
152 static char *outfname = NULL;
153 /* If -ff, points to stderr. Else, it's our common output log */
154 static FILE *shared_log;
155
156 struct tcb *printing_tcp = NULL;
157 static struct tcb *current_tcp;
158
159 static struct tcb **tcbtab;
160 static unsigned int nprocs, tcbtabsize;
161 static const char *progname;
162
163 unsigned os_release; /* generated from uname()'s u.release */
164
165 static void detach(struct tcb *tcp);
166 static void cleanup(void);
167 static void interrupt(int sig);
168 static sigset_t empty_set, blocked_set;
169
170 #ifdef HAVE_SIG_ATOMIC_T
171 static volatile sig_atomic_t interrupted;
172 #else
173 static volatile int interrupted;
174 #endif
175
176 #ifndef HAVE_STRERROR
177
178 #if !HAVE_DECL_SYS_ERRLIST
179 extern int sys_nerr;
180 extern char *sys_errlist[];
181 #endif
182
183 const char *
184 strerror(int err_no)
185 {
186         static char buf[sizeof("Unknown error %d") + sizeof(int)*3];
187
188         if (err_no < 1 || err_no >= sys_nerr) {
189                 sprintf(buf, "Unknown error %d", err_no);
190                 return buf;
191         }
192         return sys_errlist[err_no];
193 }
194
195 #endif /* HAVE_STERRROR */
196
197 static void
198 usage(FILE *ofp, int exitval)
199 {
200         fprintf(ofp, "\
201 usage: strace [-CdffhiqrtttTvVxxy] [-I n] [-e expr]...\n\
202               [-a column] [-o file] [-s strsize] [-P path]...\n\
203               -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
204    or: strace -c[df] [-I n] [-e expr]... [-O overhead] [-S sortby]\n\
205               -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
206 -c -- count time, calls, and errors for each syscall and report summary\n\
207 -C -- like -c but also print regular output\n\
208 -w -- summarise syscall latency (default is system time)\n\
209 -d -- enable debug output to stderr\n\
210 -D -- run tracer process as a detached grandchild, not as parent\n\
211 -f -- follow forks, -ff -- with output into separate files\n\
212 -i -- print instruction pointer at time of syscall\n\
213 -q -- suppress messages about attaching, detaching, etc.\n\
214 -r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
215 -T -- print time spent in each syscall\n\
216 -v -- verbose mode: print unabbreviated argv, stat, termios, etc. args\n\
217 -x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
218 -y -- print paths associated with file descriptor arguments\n\
219 -h -- print help message, -V -- print version\n\
220 -a column -- alignment COLUMN for printing syscall results (default %d)\n\
221 -b execve -- detach on this syscall\n\
222 -e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
223    options: trace, abbrev, verbose, raw, signal, read, write\n\
224 -I interruptible --\n\
225    1: no signals are blocked\n\
226    2: fatal signals are blocked while decoding syscall (default)\n\
227    3: fatal signals are always blocked (default if '-o FILE PROG')\n\
228    4: fatal signals and SIGTSTP (^Z) are always blocked\n\
229       (useful to make 'strace -o FILE PROG' not stop on ^Z)\n\
230 -o file -- send trace output to FILE instead of stderr\n\
231 -O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
232 -p pid -- trace process with process id PID, may be repeated\n\
233 -s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
234 -S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
235 -u username -- run command as username handling setuid and/or setgid\n\
236 -E var=val -- put var=val in the environment for command\n\
237 -E var -- remove var from the environment for command\n\
238 -P path -- trace accesses to path\n\
239 "
240 #ifdef USE_LIBUNWIND
241 "-k obtain stack trace between each syscall (experimental)\n\
242 "
243 #endif
244 /* ancient, no one should use it
245 -F -- attempt to follow vforks (deprecated, use -f)\n\
246  */
247 /* this is broken, so don't document it
248 -z -- print only succeeding syscalls\n\
249  */
250 , DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
251         exit(exitval);
252 }
253
254 static void die(void) __attribute__ ((noreturn));
255 static void die(void)
256 {
257         if (strace_tracer_pid == getpid()) {
258                 cflag = 0;
259                 cleanup();
260         }
261         exit(1);
262 }
263
264 static void verror_msg(int err_no, const char *fmt, va_list p)
265 {
266         char *msg;
267
268         fflush(NULL);
269
270         /* We want to print entire message with single fprintf to ensure
271          * message integrity if stderr is shared with other programs.
272          * Thus we use vasprintf + single fprintf.
273          */
274         msg = NULL;
275         if (vasprintf(&msg, fmt, p) >= 0) {
276                 if (err_no)
277                         fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(err_no));
278                 else
279                         fprintf(stderr, "%s: %s\n", progname, msg);
280                 free(msg);
281         } else {
282                 /* malloc in vasprintf failed, try it without malloc */
283                 fprintf(stderr, "%s: ", progname);
284                 vfprintf(stderr, fmt, p);
285                 if (err_no)
286                         fprintf(stderr, ": %s\n", strerror(err_no));
287                 else
288                         putc('\n', stderr);
289         }
290         /* We don't switch stderr to buffered, thus fprintf(stderr)
291          * always flushes its output and this is not necessary: */
292         /* fflush(stderr); */
293 }
294
295 void error_msg(const char *fmt, ...)
296 {
297         va_list p;
298         va_start(p, fmt);
299         verror_msg(0, fmt, p);
300         va_end(p);
301 }
302
303 void error_msg_and_die(const char *fmt, ...)
304 {
305         va_list p;
306         va_start(p, fmt);
307         verror_msg(0, fmt, p);
308         die();
309 }
310
311 void perror_msg(const char *fmt, ...)
312 {
313         va_list p;
314         va_start(p, fmt);
315         verror_msg(errno, fmt, p);
316         va_end(p);
317 }
318
319 void perror_msg_and_die(const char *fmt, ...)
320 {
321         va_list p;
322         va_start(p, fmt);
323         verror_msg(errno, fmt, p);
324         die();
325 }
326
327 void die_out_of_memory(void)
328 {
329         static bool recursed = 0;
330         if (recursed)
331                 exit(1);
332         recursed = 1;
333         error_msg_and_die("Out of memory");
334 }
335
336 static void
337 error_opt_arg(int opt, const char *arg)
338 {
339         error_msg_and_die("Invalid -%c argument: '%s'", opt, arg);
340 }
341
342 #if USE_SEIZE
343 static int
344 ptrace_attach_or_seize(int pid)
345 {
346         int r;
347         if (!use_seize)
348                 return ptrace(PTRACE_ATTACH, pid, 0L, 0L);
349         r = ptrace(PTRACE_SEIZE, pid, 0L, (unsigned long)ptrace_setoptions);
350         if (r)
351                 return r;
352         r = ptrace(PTRACE_INTERRUPT, pid, 0L, 0L);
353         return r;
354 }
355 #else
356 # define ptrace_attach_or_seize(pid) ptrace(PTRACE_ATTACH, (pid), 0, 0)
357 #endif
358
359 /*
360  * Used when we want to unblock stopped traced process.
361  * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL.
362  * Returns 0 on success or if error was ESRCH
363  * (presumably process was killed while we talk to it).
364  * Otherwise prints error message and returns -1.
365  */
366 static int
367 ptrace_restart(int op, struct tcb *tcp, int sig)
368 {
369         int err;
370         const char *msg;
371
372         errno = 0;
373         ptrace(op, tcp->pid, (void *) 0, (long) sig);
374         err = errno;
375         if (!err)
376                 return 0;
377
378         msg = "SYSCALL";
379         if (op == PTRACE_CONT)
380                 msg = "CONT";
381         if (op == PTRACE_DETACH)
382                 msg = "DETACH";
383 #ifdef PTRACE_LISTEN
384         if (op == PTRACE_LISTEN)
385                 msg = "LISTEN";
386 #endif
387         /*
388          * Why curcol != 0? Otherwise sometimes we get this:
389          *
390          * 10252 kill(10253, SIGKILL)              = 0
391          *  <ptrace(SYSCALL,10252):No such process>10253 ...next decode...
392          *
393          * 10252 died after we retrieved syscall exit data,
394          * but before we tried to restart it. Log looks ugly.
395          */
396         if (current_tcp && current_tcp->curcol != 0) {
397                 tprintf(" <ptrace(%s):%s>\n", msg, strerror(err));
398                 line_ended();
399         }
400         if (err == ESRCH)
401                 return 0;
402         errno = err;
403         perror_msg("ptrace(PTRACE_%s,pid:%d,sig:%d)", msg, tcp->pid, sig);
404         return -1;
405 }
406
407 static void
408 set_cloexec_flag(int fd)
409 {
410         int flags, newflags;
411
412         flags = fcntl(fd, F_GETFD);
413         if (flags < 0) {
414                 /* Can happen only if fd is bad.
415                  * Should never happen: if it does, we have a bug
416                  * in the caller. Therefore we just abort
417                  * instead of propagating the error.
418                  */
419                 perror_msg_and_die("fcntl(%d, F_GETFD)", fd);
420         }
421
422         newflags = flags | FD_CLOEXEC;
423         if (flags == newflags)
424                 return;
425
426         fcntl(fd, F_SETFD, newflags); /* never fails */
427 }
428
429 static void kill_save_errno(pid_t pid, int sig)
430 {
431         int saved_errno = errno;
432
433         (void) kill(pid, sig);
434         errno = saved_errno;
435 }
436
437 /*
438  * When strace is setuid executable, we have to swap uids
439  * before and after filesystem and process management operations.
440  */
441 static void
442 swap_uid(void)
443 {
444         int euid = geteuid(), uid = getuid();
445
446         if (euid != uid && setreuid(euid, uid) < 0) {
447                 perror_msg_and_die("setreuid");
448         }
449 }
450
451 #ifdef _LARGEFILE64_SOURCE
452 # ifdef HAVE_FOPEN64
453 #  define fopen_for_output fopen64
454 # else
455 #  define fopen_for_output fopen
456 # endif
457 # define struct_stat struct stat64
458 # define stat_file stat64
459 # define struct_dirent struct dirent64
460 # define read_dir readdir64
461 # define struct_rlimit struct rlimit64
462 # define set_rlimit setrlimit64
463 #else
464 # define fopen_for_output fopen
465 # define struct_stat struct stat
466 # define stat_file stat
467 # define struct_dirent struct dirent
468 # define read_dir readdir
469 # define struct_rlimit struct rlimit
470 # define set_rlimit setrlimit
471 #endif
472
473 static FILE *
474 strace_fopen(const char *path)
475 {
476         FILE *fp;
477
478         swap_uid();
479         fp = fopen_for_output(path, "w");
480         if (!fp)
481                 perror_msg_and_die("Can't fopen '%s'", path);
482         swap_uid();
483         set_cloexec_flag(fileno(fp));
484         return fp;
485 }
486
487 static int popen_pid = 0;
488
489 #ifndef _PATH_BSHELL
490 # define _PATH_BSHELL "/bin/sh"
491 #endif
492
493 /*
494  * We cannot use standard popen(3) here because we have to distinguish
495  * popen child process from other processes we trace, and standard popen(3)
496  * does not export its child's pid.
497  */
498 static FILE *
499 strace_popen(const char *command)
500 {
501         FILE *fp;
502         int pid;
503         int fds[2];
504
505         swap_uid();
506         if (pipe(fds) < 0)
507                 perror_msg_and_die("pipe");
508
509         set_cloexec_flag(fds[1]); /* never fails */
510
511         pid = vfork();
512         if (pid < 0)
513                 perror_msg_and_die("vfork");
514
515         if (pid == 0) {
516                 /* child */
517                 close(fds[1]);
518                 if (fds[0] != 0) {
519                         if (dup2(fds[0], 0))
520                                 perror_msg_and_die("dup2");
521                         close(fds[0]);
522                 }
523                 execl(_PATH_BSHELL, "sh", "-c", command, NULL);
524                 perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL);
525         }
526
527         /* parent */
528         popen_pid = pid;
529         close(fds[0]);
530         swap_uid();
531         fp = fdopen(fds[1], "w");
532         if (!fp)
533                 die_out_of_memory();
534         return fp;
535 }
536
537 void
538 tprintf(const char *fmt, ...)
539 {
540         va_list args;
541
542         va_start(args, fmt);
543         if (current_tcp) {
544                 int n = strace_vfprintf(current_tcp->outf, fmt, args);
545                 if (n < 0) {
546                         if (current_tcp->outf != stderr)
547                                 perror_msg("%s", outfname);
548                 } else
549                         current_tcp->curcol += n;
550         }
551         va_end(args);
552 }
553
554 #ifndef HAVE_FPUTS_UNLOCKED
555 # define fputs_unlocked fputs
556 #endif
557
558 void
559 tprints(const char *str)
560 {
561         if (current_tcp) {
562                 int n = fputs_unlocked(str, current_tcp->outf);
563                 if (n >= 0) {
564                         current_tcp->curcol += strlen(str);
565                         return;
566                 }
567                 if (current_tcp->outf != stderr)
568                         perror_msg("%s", outfname);
569         }
570 }
571
572 void
573 line_ended(void)
574 {
575         if (current_tcp) {
576                 current_tcp->curcol = 0;
577                 fflush(current_tcp->outf);
578         }
579         if (printing_tcp) {
580                 printing_tcp->curcol = 0;
581                 printing_tcp = NULL;
582         }
583 }
584
585 void
586 printleader(struct tcb *tcp)
587 {
588         /* If -ff, "previous tcb we printed" is always the same as current,
589          * because we have per-tcb output files.
590          */
591         if (followfork >= 2)
592                 printing_tcp = tcp;
593
594         if (printing_tcp) {
595                 current_tcp = printing_tcp;
596                 if (printing_tcp->curcol != 0 && (followfork < 2 || printing_tcp == tcp)) {
597                         /*
598                          * case 1: we have a shared log (i.e. not -ff), and last line
599                          * wasn't finished (same or different tcb, doesn't matter).
600                          * case 2: split log, we are the same tcb, but our last line
601                          * didn't finish ("SIGKILL nuked us after syscall entry" etc).
602                          */
603                         tprints(" <unfinished ...>\n");
604                         printing_tcp->curcol = 0;
605                 }
606         }
607
608         printing_tcp = tcp;
609         current_tcp = tcp;
610         current_tcp->curcol = 0;
611
612         if (print_pid_pfx)
613                 tprintf("%-5d ", tcp->pid);
614         else if (nprocs > 1 && !outfname)
615                 tprintf("[pid %5u] ", tcp->pid);
616
617         if (tflag) {
618                 char str[sizeof("HH:MM:SS")];
619                 struct timeval tv, dtv;
620                 static struct timeval otv;
621
622                 gettimeofday(&tv, NULL);
623                 if (rflag) {
624                         if (otv.tv_sec == 0)
625                                 otv = tv;
626                         tv_sub(&dtv, &tv, &otv);
627                         tprintf("%6ld.%06ld ",
628                                 (long) dtv.tv_sec, (long) dtv.tv_usec);
629                         otv = tv;
630                 }
631                 else if (tflag > 2) {
632                         tprintf("%ld.%06ld ",
633                                 (long) tv.tv_sec, (long) tv.tv_usec);
634                 }
635                 else {
636                         time_t local = tv.tv_sec;
637                         strftime(str, sizeof(str), "%T", localtime(&local));
638                         if (tflag > 1)
639                                 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
640                         else
641                                 tprintf("%s ", str);
642                 }
643         }
644         if (iflag)
645                 print_pc(tcp);
646 }
647
648 void
649 tabto(void)
650 {
651         if (current_tcp->curcol < acolumn)
652                 tprints(acolumn_spaces + current_tcp->curcol);
653 }
654
655 /* Should be only called directly *after successful attach* to a tracee.
656  * Otherwise, "strace -oFILE -ff -p<nonexistant_pid>"
657  * may create bogus empty FILE.<nonexistant_pid>, and then die.
658  */
659 static void
660 newoutf(struct tcb *tcp)
661 {
662         tcp->outf = shared_log; /* if not -ff mode, the same file is for all */
663         if (followfork >= 2) {
664                 char name[520 + sizeof(int) * 3];
665                 sprintf(name, "%.512s.%u", outfname, tcp->pid);
666                 tcp->outf = strace_fopen(name);
667         }
668 }
669
670 static void
671 expand_tcbtab(void)
672 {
673         /* Allocate some more TCBs and expand the table.
674            We don't want to relocate the TCBs because our
675            callers have pointers and it would be a pain.
676            So tcbtab is a table of pointers.  Since we never
677            free the TCBs, we allocate a single chunk of many.  */
678         int i = tcbtabsize;
679         struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0]));
680         struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0]));
681         if (!newtab || !newtcbs)
682                 die_out_of_memory();
683         tcbtabsize *= 2;
684         tcbtab = newtab;
685         while (i < tcbtabsize)
686                 tcbtab[i++] = newtcbs++;
687 }
688
689 static struct tcb *
690 alloctcb(int pid)
691 {
692         int i;
693         struct tcb *tcp;
694
695         if (nprocs == tcbtabsize)
696                 expand_tcbtab();
697
698         for (i = 0; i < tcbtabsize; i++) {
699                 tcp = tcbtab[i];
700                 if (!tcp->pid) {
701                         memset(tcp, 0, sizeof(*tcp));
702                         tcp->pid = pid;
703 #if SUPPORTED_PERSONALITIES > 1
704                         tcp->currpers = current_personality;
705 #endif
706
707 #ifdef USE_LIBUNWIND
708                         if (stack_trace_enabled)
709                                 unwind_tcb_init(tcp);
710 #endif
711
712                         nprocs++;
713                         if (debug_flag)
714                                 fprintf(stderr, "new tcb for pid %d, active tcbs:%d\n", tcp->pid, nprocs);
715                         return tcp;
716                 }
717         }
718         error_msg_and_die("bug in alloctcb");
719 }
720
721 static void
722 droptcb(struct tcb *tcp)
723 {
724         if (tcp->pid == 0)
725                 return;
726
727 #ifdef USE_LIBUNWIND
728         if (stack_trace_enabled) {
729                 unwind_tcb_fin(tcp);
730         }
731 #endif
732
733         nprocs--;
734         if (debug_flag)
735                 fprintf(stderr, "dropped tcb for pid %d, %d remain\n", tcp->pid, nprocs);
736
737         if (tcp->outf) {
738                 if (followfork >= 2) {
739                         if (tcp->curcol != 0)
740                                 fprintf(tcp->outf, " <detached ...>\n");
741                         fclose(tcp->outf);
742                 } else {
743                         if (printing_tcp == tcp && tcp->curcol != 0)
744                                 fprintf(tcp->outf, " <detached ...>\n");
745                         fflush(tcp->outf);
746                 }
747         }
748
749         if (current_tcp == tcp)
750                 current_tcp = NULL;
751         if (printing_tcp == tcp)
752                 printing_tcp = NULL;
753
754         memset(tcp, 0, sizeof(*tcp));
755 }
756
757 /* Detach traced process.
758  * Never call DETACH twice on the same process as both unattached and
759  * attached-unstopped processes give the same ESRCH.  For unattached process we
760  * would SIGSTOP it and wait for its SIGSTOP notification forever.
761  */
762 static void
763 detach(struct tcb *tcp)
764 {
765         int error;
766         int status;
767
768         if (tcp->flags & TCB_BPTSET)
769                 clearbpt(tcp);
770
771         /*
772          * Linux wrongly insists the child be stopped
773          * before detaching.  Arghh.  We go through hoops
774          * to make a clean break of things.
775          */
776 #if defined(SPARC)
777 # undef PTRACE_DETACH
778 # define PTRACE_DETACH PTRACE_SUNDETACH
779 #endif
780
781         if (!(tcp->flags & TCB_ATTACHED))
782                 goto drop;
783
784         /* We attached but possibly didn't see the expected SIGSTOP.
785          * We must catch exactly one as otherwise the detached process
786          * would be left stopped (process state T).
787          */
788         if (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)
789                 goto wait_loop;
790
791         error = ptrace(PTRACE_DETACH, tcp->pid, 0, 0);
792         if (!error) {
793                 /* On a clear day, you can see forever. */
794                 goto drop;
795         }
796         if (errno != ESRCH) {
797                 /* Shouldn't happen. */
798                 perror_msg("detach: ptrace(PTRACE_DETACH,%u)", tcp->pid);
799                 goto drop;
800         }
801         /* ESRCH: process is either not stopped or doesn't exist. */
802         if (my_tkill(tcp->pid, 0) < 0) {
803                 if (errno != ESRCH)
804                         /* Shouldn't happen. */
805                         perror_msg("detach: tkill(%u,0)", tcp->pid);
806                 /* else: process doesn't exist. */
807                 goto drop;
808         }
809         /* Process is not stopped, need to stop it. */
810         if (use_seize) {
811                 /*
812                  * With SEIZE, tracee can be in group-stop already.
813                  * In this state sending it another SIGSTOP does nothing.
814                  * Need to use INTERRUPT.
815                  * Testcase: trying to ^C a "strace -p <stopped_process>".
816                  */
817                 error = ptrace(PTRACE_INTERRUPT, tcp->pid, 0, 0);
818                 if (!error)
819                         goto wait_loop;
820                 if (errno != ESRCH)
821                         perror_msg("detach: ptrace(PTRACE_INTERRUPT,%u)", tcp->pid);
822         }
823         else {
824                 error = my_tkill(tcp->pid, SIGSTOP);
825                 if (!error)
826                         goto wait_loop;
827                 if (errno != ESRCH)
828                         perror_msg("detach: tkill(%u,SIGSTOP)", tcp->pid);
829         }
830         /* Either process doesn't exist, or some weird error. */
831         goto drop;
832
833  wait_loop:
834         /* We end up here in three cases:
835          * 1. We sent PTRACE_INTERRUPT (use_seize case)
836          * 2. We sent SIGSTOP (!use_seize)
837          * 3. Attach SIGSTOP was already pending (TCB_IGNORE_ONE_SIGSTOP set)
838          */
839         for (;;) {
840                 int sig;
841                 if (waitpid(tcp->pid, &status, __WALL) < 0) {
842                         if (errno == EINTR)
843                                 continue;
844                         /*
845                          * if (errno == ECHILD) break;
846                          * ^^^  WRONG! We expect this PID to exist,
847                          * and want to emit a message otherwise:
848                          */
849                         perror_msg("detach: waitpid(%u)", tcp->pid);
850                         break;
851                 }
852                 if (!WIFSTOPPED(status)) {
853                         /*
854                          * Tracee exited or was killed by signal.
855                          * We shouldn't normally reach this place:
856                          * we don't want to consume exit status.
857                          * Consider "strace -p PID" being ^C-ed:
858                          * we want merely to detach from PID.
859                          *
860                          * However, we _can_ end up here if tracee
861                          * was SIGKILLed.
862                          */
863                         break;
864                 }
865                 sig = WSTOPSIG(status);
866                 if (debug_flag)
867                         fprintf(stderr, "detach wait: event:%d sig:%d\n",
868                                         (unsigned)status >> 16, sig);
869                 if (use_seize) {
870                         unsigned event = (unsigned)status >> 16;
871                         if (event == PTRACE_EVENT_STOP /*&& sig == SIGTRAP*/) {
872                                 /*
873                                  * sig == SIGTRAP: PTRACE_INTERRUPT stop.
874                                  * sig == other: process was already stopped
875                                  * with this stopping sig (see tests/detach-stopped).
876                                  * Looks like re-injecting this sig is not necessary
877                                  * in DETACH for the tracee to remain stopped.
878                                  */
879                                 sig = 0;
880                         }
881                         /*
882                          * PTRACE_INTERRUPT is not guaranteed to produce
883                          * the above event if other ptrace-stop is pending.
884                          * See tests/detach-sleeping testcase:
885                          * strace got SIGINT while tracee is sleeping.
886                          * We sent PTRACE_INTERRUPT.
887                          * We see syscall exit, not PTRACE_INTERRUPT stop.
888                          * We won't get PTRACE_INTERRUPT stop
889                          * if we would CONT now. Need to DETACH.
890                          */
891                         if (sig == syscall_trap_sig)
892                                 sig = 0;
893                         /* else: not sure in which case we can be here.
894                          * Signal stop? Inject it while detaching.
895                          */
896                         ptrace_restart(PTRACE_DETACH, tcp, sig);
897                         break;
898                 }
899                 /* Note: this check has to be after use_seize check */
900                 /* (else, in use_seize case SIGSTOP will be mistreated) */
901                 if (sig == SIGSTOP) {
902                         /* Detach, suppressing SIGSTOP */
903                         ptrace_restart(PTRACE_DETACH, tcp, 0);
904                         break;
905                 }
906                 if (sig == syscall_trap_sig)
907                         sig = 0;
908                 /* Can't detach just yet, may need to wait for SIGSTOP */
909                 error = ptrace_restart(PTRACE_CONT, tcp, sig);
910                 if (error < 0) {
911                         /* Should not happen.
912                          * Note: ptrace_restart returns 0 on ESRCH, so it's not it.
913                          * ptrace_restart already emitted error message.
914                          */
915                         break;
916                 }
917         }
918
919  drop:
920         if (!qflag && (tcp->flags & TCB_ATTACHED))
921                 fprintf(stderr, "Process %u detached\n", tcp->pid);
922
923         droptcb(tcp);
924 }
925
926 static void
927 process_opt_p_list(char *opt)
928 {
929         while (*opt) {
930                 /*
931                  * We accept -p PID,PID; -p "`pidof PROG`"; -p "`pgrep PROG`".
932                  * pidof uses space as delim, pgrep uses newline. :(
933                  */
934                 int pid;
935                 char *delim = opt + strcspn(opt, ", \n\t");
936                 char c = *delim;
937
938                 *delim = '\0';
939                 pid = string_to_uint(opt);
940                 if (pid <= 0) {
941                         error_msg_and_die("Invalid process id: '%s'", opt);
942                 }
943                 if (pid == strace_tracer_pid) {
944                         error_msg_and_die("I'm sorry, I can't let you do that, Dave.");
945                 }
946                 *delim = c;
947                 alloctcb(pid);
948                 if (c == '\0')
949                         break;
950                 opt = delim + 1;
951         }
952 }
953
954 static void
955 startup_attach(void)
956 {
957         int tcbi;
958         struct tcb *tcp;
959
960         /*
961          * Block user interruptions as we would leave the traced
962          * process stopped (process state T) if we would terminate in
963          * between PTRACE_ATTACH and wait4() on SIGSTOP.
964          * We rely on cleanup() from this point on.
965          */
966         if (interactive)
967                 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
968
969         if (daemonized_tracer) {
970                 pid_t pid = fork();
971                 if (pid < 0) {
972                         perror_msg_and_die("fork");
973                 }
974                 if (pid) { /* parent */
975                         /*
976                          * Wait for grandchild to attach to straced process
977                          * (grandparent). Grandchild SIGKILLs us after it attached.
978                          * Grandparent's wait() is unblocked by our death,
979                          * it proceeds to exec the straced program.
980                          */
981                         pause();
982                         _exit(0); /* paranoia */
983                 }
984                 /* grandchild */
985                 /* We will be the tracer process. Remember our new pid: */
986                 strace_tracer_pid = getpid();
987         }
988
989         for (tcbi = 0; tcbi < tcbtabsize; tcbi++) {
990                 tcp = tcbtab[tcbi];
991
992                 if (!tcp->pid)
993                         continue;
994
995                 /* Is this a process we should attach to, but not yet attached? */
996                 if (tcp->flags & TCB_ATTACHED)
997                         continue; /* no, we already attached it */
998
999                 if (followfork && !daemonized_tracer) {
1000                         char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3];
1001                         DIR *dir;
1002
1003                         sprintf(procdir, "/proc/%d/task", tcp->pid);
1004                         dir = opendir(procdir);
1005                         if (dir != NULL) {
1006                                 unsigned int ntid = 0, nerr = 0;
1007                                 struct_dirent *de;
1008
1009                                 while ((de = read_dir(dir)) != NULL) {
1010                                         struct tcb *cur_tcp;
1011                                         int tid;
1012
1013                                         if (de->d_fileno == 0)
1014                                                 continue;
1015                                         /* we trust /proc filesystem */
1016                                         tid = atoi(de->d_name);
1017                                         if (tid <= 0)
1018                                                 continue;
1019                                         ++ntid;
1020                                         if (ptrace_attach_or_seize(tid) < 0) {
1021                                                 ++nerr;
1022                                                 if (debug_flag)
1023                                                         fprintf(stderr, "attach to pid %d failed\n", tid);
1024                                                 continue;
1025                                         }
1026                                         if (debug_flag)
1027                                                 fprintf(stderr, "attach to pid %d succeeded\n", tid);
1028                                         cur_tcp = tcp;
1029                                         if (tid != tcp->pid)
1030                                                 cur_tcp = alloctcb(tid);
1031                                         cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
1032                                         newoutf(cur_tcp);
1033                                 }
1034                                 closedir(dir);
1035                                 if (interactive) {
1036                                         sigprocmask(SIG_SETMASK, &empty_set, NULL);
1037                                         if (interrupted)
1038                                                 goto ret;
1039                                         sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1040                                 }
1041                                 ntid -= nerr;
1042                                 if (ntid == 0) {
1043                                         perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
1044                                         droptcb(tcp);
1045                                         continue;
1046                                 }
1047                                 if (!qflag) {
1048                                         fprintf(stderr, ntid > 1
1049 ? "Process %u attached with %u threads\n"
1050 : "Process %u attached\n",
1051                                                 tcp->pid, ntid);
1052                                 }
1053                                 if (!(tcp->flags & TCB_ATTACHED)) {
1054                                         /* -p PID, we failed to attach to PID itself
1055                                          * but did attach to some of its sibling threads.
1056                                          * Drop PID's tcp.
1057                                          */
1058                                         droptcb(tcp);
1059                                 }
1060                                 continue;
1061                         } /* if (opendir worked) */
1062                 } /* if (-f) */
1063                 if (ptrace_attach_or_seize(tcp->pid) < 0) {
1064                         perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
1065                         droptcb(tcp);
1066                         continue;
1067                 }
1068                 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
1069                 newoutf(tcp);
1070                 if (debug_flag)
1071                         fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid);
1072
1073                 if (daemonized_tracer) {
1074                         /*
1075                          * Make parent go away.
1076                          * Also makes grandparent's wait() unblock.
1077                          */
1078                         kill(getppid(), SIGKILL);
1079                 }
1080
1081                 if (!qflag)
1082                         fprintf(stderr,
1083                                 "Process %u attached\n",
1084                                 tcp->pid);
1085         } /* for each tcbtab[] */
1086
1087  ret:
1088         if (interactive)
1089                 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1090 }
1091
1092 /* Stack-o-phobic exec helper, in the hope to work around
1093  * NOMMU + "daemonized tracer" difficulty.
1094  */
1095 struct exec_params {
1096         int fd_to_close;
1097         uid_t run_euid;
1098         gid_t run_egid;
1099         char **argv;
1100         char *pathname;
1101 };
1102 static struct exec_params params_for_tracee;
1103 static void __attribute__ ((noinline, noreturn))
1104 exec_or_die(void)
1105 {
1106         struct exec_params *params = &params_for_tracee;
1107
1108         if (params->fd_to_close >= 0)
1109                 close(params->fd_to_close);
1110         if (!daemonized_tracer && !use_seize) {
1111                 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) {
1112                         perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)");
1113                 }
1114         }
1115
1116         if (username != NULL) {
1117                 /*
1118                  * It is important to set groups before we
1119                  * lose privileges on setuid.
1120                  */
1121                 if (initgroups(username, run_gid) < 0) {
1122                         perror_msg_and_die("initgroups");
1123                 }
1124                 if (setregid(run_gid, params->run_egid) < 0) {
1125                         perror_msg_and_die("setregid");
1126                 }
1127                 if (setreuid(run_uid, params->run_euid) < 0) {
1128                         perror_msg_and_die("setreuid");
1129                 }
1130         }
1131         else if (geteuid() != 0)
1132                 if (setreuid(run_uid, run_uid) < 0) {
1133                         perror_msg_and_die("setreuid");
1134                 }
1135
1136         if (!daemonized_tracer) {
1137                 /*
1138                  * Induce a ptrace stop. Tracer (our parent)
1139                  * will resume us with PTRACE_SYSCALL and display
1140                  * the immediately following execve syscall.
1141                  * Can't do this on NOMMU systems, we are after
1142                  * vfork: parent is blocked, stopping would deadlock.
1143                  */
1144                 if (!NOMMU_SYSTEM)
1145                         kill(getpid(), SIGSTOP);
1146         } else {
1147                 alarm(3);
1148                 /* we depend on SIGCHLD set to SIG_DFL by init code */
1149                 /* if it happens to be SIG_IGN'ed, wait won't block */
1150                 wait(NULL);
1151                 alarm(0);
1152         }
1153
1154         execv(params->pathname, params->argv);
1155         perror_msg_and_die("exec");
1156 }
1157
1158 static void
1159 startup_child(char **argv)
1160 {
1161         struct_stat statbuf;
1162         const char *filename;
1163         char pathname[MAXPATHLEN];
1164         int pid;
1165         struct tcb *tcp;
1166
1167         filename = argv[0];
1168         if (strchr(filename, '/')) {
1169                 if (strlen(filename) > sizeof pathname - 1) {
1170                         errno = ENAMETOOLONG;
1171                         perror_msg_and_die("exec");
1172                 }
1173                 strcpy(pathname, filename);
1174         }
1175 #ifdef USE_DEBUGGING_EXEC
1176         /*
1177          * Debuggers customarily check the current directory
1178          * first regardless of the path but doing that gives
1179          * security geeks a panic attack.
1180          */
1181         else if (stat_file(filename, &statbuf) == 0)
1182                 strcpy(pathname, filename);
1183 #endif /* USE_DEBUGGING_EXEC */
1184         else {
1185                 const char *path;
1186                 int m, n, len;
1187
1188                 for (path = getenv("PATH"); path && *path; path += m) {
1189                         const char *colon = strchr(path, ':');
1190                         if (colon) {
1191                                 n = colon - path;
1192                                 m = n + 1;
1193                         }
1194                         else
1195                                 m = n = strlen(path);
1196                         if (n == 0) {
1197                                 if (!getcwd(pathname, MAXPATHLEN))
1198                                         continue;
1199                                 len = strlen(pathname);
1200                         }
1201                         else if (n > sizeof pathname - 1)
1202                                 continue;
1203                         else {
1204                                 strncpy(pathname, path, n);
1205                                 len = n;
1206                         }
1207                         if (len && pathname[len - 1] != '/')
1208                                 pathname[len++] = '/';
1209                         strcpy(pathname + len, filename);
1210                         if (stat_file(pathname, &statbuf) == 0 &&
1211                             /* Accept only regular files
1212                                with some execute bits set.
1213                                XXX not perfect, might still fail */
1214                             S_ISREG(statbuf.st_mode) &&
1215                             (statbuf.st_mode & 0111))
1216                                 break;
1217                 }
1218         }
1219         if (stat_file(pathname, &statbuf) < 0) {
1220                 perror_msg_and_die("Can't stat '%s'", filename);
1221         }
1222
1223         params_for_tracee.fd_to_close = (shared_log != stderr) ? fileno(shared_log) : -1;
1224         params_for_tracee.run_euid = (statbuf.st_mode & S_ISUID) ? statbuf.st_uid : run_uid;
1225         params_for_tracee.run_egid = (statbuf.st_mode & S_ISGID) ? statbuf.st_gid : run_gid;
1226         params_for_tracee.argv = argv;
1227         /*
1228          * On NOMMU, can be safely freed only after execve in tracee.
1229          * It's hard to know when that happens, so we just leak it.
1230          */
1231         params_for_tracee.pathname = NOMMU_SYSTEM ? strdup(pathname) : pathname;
1232
1233 #if defined HAVE_PRCTL && defined PR_SET_PTRACER && defined PR_SET_PTRACER_ANY
1234         if (daemonized_tracer)
1235                 prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY);
1236 #endif
1237
1238         pid = fork();
1239         if (pid < 0) {
1240                 perror_msg_and_die("fork");
1241         }
1242         if ((pid != 0 && daemonized_tracer)
1243          || (pid == 0 && !daemonized_tracer)
1244         ) {
1245                 /* We are to become the tracee. Two cases:
1246                  * -D: we are parent
1247                  * not -D: we are child
1248                  */
1249                 exec_or_die();
1250         }
1251
1252         /* We are the tracer */
1253
1254         if (!daemonized_tracer) {
1255                 strace_child = pid;
1256                 if (!use_seize) {
1257                         /* child did PTRACE_TRACEME, nothing to do in parent */
1258                 } else {
1259                         if (!NOMMU_SYSTEM) {
1260                                 /* Wait until child stopped itself */
1261                                 int status;
1262                                 while (waitpid(pid, &status, WSTOPPED) < 0) {
1263                                         if (errno == EINTR)
1264                                                 continue;
1265                                         perror_msg_and_die("waitpid");
1266                                 }
1267                                 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
1268                                         kill_save_errno(pid, SIGKILL);
1269                                         perror_msg_and_die("Unexpected wait status %x", status);
1270                                 }
1271                         }
1272                         /* Else: NOMMU case, we have no way to sync.
1273                          * Just attach to it as soon as possible.
1274                          * This means that we may miss a few first syscalls...
1275                          */
1276
1277                         if (ptrace_attach_or_seize(pid)) {
1278                                 kill_save_errno(pid, SIGKILL);
1279                                 perror_msg_and_die("Can't attach to %d", pid);
1280                         }
1281                         if (!NOMMU_SYSTEM)
1282                                 kill(pid, SIGCONT);
1283                 }
1284                 tcp = alloctcb(pid);
1285                 if (!NOMMU_SYSTEM)
1286                         tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
1287                 else
1288                         tcp->flags |= TCB_ATTACHED | TCB_STARTUP;
1289                 newoutf(tcp);
1290         }
1291         else {
1292                 /* With -D, we are *child* here, IOW: different pid. Fetch it: */
1293                 strace_tracer_pid = getpid();
1294                 /* The tracee is our parent: */
1295                 pid = getppid();
1296                 alloctcb(pid);
1297                 /* attaching will be done later, by startup_attach */
1298                 /* note: we don't do newoutf(tcp) here either! */
1299
1300                 /* NOMMU BUG! -D mode is active, we (child) return,
1301                  * and we will scribble over parent's stack!
1302                  * When parent later unpauses, it segfaults.
1303                  *
1304                  * We work around it
1305                  * (1) by declaring exec_or_die() NORETURN,
1306                  * hopefully compiler will just jump to it
1307                  * instead of call (won't push anything to stack),
1308                  * (2) by trying very hard in exec_or_die()
1309                  * to not use any stack,
1310                  * (3) having a really big (MAXPATHLEN) stack object
1311                  * in this function, which creates a "buffer" between
1312                  * child's and parent's stack pointers.
1313                  * This may save us if (1) and (2) failed
1314                  * and compiler decided to use stack in exec_or_die() anyway
1315                  * (happens on i386 because of stack parameter passing).
1316                  *
1317                  * A cleaner solution is to use makecontext + setcontext
1318                  * to create a genuine separate stack and execute on it.
1319                  */
1320         }
1321 }
1322
1323 /*
1324  * Test whether the kernel support PTRACE_O_TRACECLONE et al options.
1325  * First fork a new child, call ptrace with PTRACE_SETOPTIONS on it,
1326  * and then see which options are supported by the kernel.
1327  */
1328 static int
1329 test_ptrace_setoptions_followfork(void)
1330 {
1331         int pid, expected_grandchild = 0, found_grandchild = 0;
1332         const unsigned int test_options = PTRACE_O_TRACECLONE |
1333                                           PTRACE_O_TRACEFORK |
1334                                           PTRACE_O_TRACEVFORK;
1335
1336         /* Need fork for test. NOMMU has no forks */
1337         if (NOMMU_SYSTEM)
1338                 goto worked; /* be bold, and pretend that test succeeded */
1339
1340         pid = fork();
1341         if (pid < 0)
1342                 perror_msg_and_die("fork");
1343         if (pid == 0) {
1344                 pid = getpid();
1345                 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
1346                         perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1347                                            __func__);
1348                 kill_save_errno(pid, SIGSTOP);
1349                 if (fork() < 0)
1350                         perror_msg_and_die("fork");
1351                 _exit(0);
1352         }
1353
1354         while (1) {
1355                 int status, tracee_pid;
1356
1357                 errno = 0;
1358                 tracee_pid = wait(&status);
1359                 if (tracee_pid <= 0) {
1360                         if (errno == EINTR)
1361                                 continue;
1362                         if (errno == ECHILD)
1363                                 break;
1364                         kill_save_errno(pid, SIGKILL);
1365                         perror_msg_and_die("%s: unexpected wait result %d",
1366                                            __func__, tracee_pid);
1367                 }
1368                 if (WIFEXITED(status)) {
1369                         if (WEXITSTATUS(status)) {
1370                                 if (tracee_pid != pid)
1371                                         kill_save_errno(pid, SIGKILL);
1372                                 error_msg_and_die("%s: unexpected exit status %u",
1373                                                   __func__, WEXITSTATUS(status));
1374                         }
1375                         continue;
1376                 }
1377                 if (WIFSIGNALED(status)) {
1378                         if (tracee_pid != pid)
1379                                 kill_save_errno(pid, SIGKILL);
1380                         error_msg_and_die("%s: unexpected signal %u",
1381                                           __func__, WTERMSIG(status));
1382                 }
1383                 if (!WIFSTOPPED(status)) {
1384                         if (tracee_pid != pid)
1385                                 kill_save_errno(tracee_pid, SIGKILL);
1386                         kill_save_errno(pid, SIGKILL);
1387                         error_msg_and_die("%s: unexpected wait status %x",
1388                                           __func__, status);
1389                 }
1390                 if (tracee_pid != pid) {
1391                         found_grandchild = tracee_pid;
1392                         if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0) {
1393                                 kill_save_errno(tracee_pid, SIGKILL);
1394                                 kill_save_errno(pid, SIGKILL);
1395                                 perror_msg_and_die("PTRACE_CONT doesn't work");
1396                         }
1397                         continue;
1398                 }
1399                 switch (WSTOPSIG(status)) {
1400                 case SIGSTOP:
1401                         if (ptrace(PTRACE_SETOPTIONS, pid, 0, test_options) < 0
1402                             && errno != EINVAL && errno != EIO)
1403                                 perror_msg("PTRACE_SETOPTIONS");
1404                         break;
1405                 case SIGTRAP:
1406                         if (status >> 16 == PTRACE_EVENT_FORK) {
1407                                 long msg = 0;
1408
1409                                 if (ptrace(PTRACE_GETEVENTMSG, pid,
1410                                            NULL, (long) &msg) == 0)
1411                                         expected_grandchild = msg;
1412                         }
1413                         break;
1414                 }
1415                 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) {
1416                         kill_save_errno(pid, SIGKILL);
1417                         perror_msg_and_die("PTRACE_SYSCALL doesn't work");
1418                 }
1419         }
1420         if (expected_grandchild && expected_grandchild == found_grandchild) {
1421  worked:
1422                 ptrace_setoptions |= test_options;
1423                 if (debug_flag)
1424                         fprintf(stderr, "ptrace_setoptions = %#x\n",
1425                                 ptrace_setoptions);
1426                 return 0;
1427         }
1428         error_msg("Test for PTRACE_O_TRACECLONE failed, "
1429                   "giving up using this feature.");
1430         return 1;
1431 }
1432
1433 /*
1434  * Test whether the kernel support PTRACE_O_TRACESYSGOOD.
1435  * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it,
1436  * and then see whether it will stop with (SIGTRAP | 0x80).
1437  *
1438  * Use of this option enables correct handling of user-generated SIGTRAPs,
1439  * and SIGTRAPs generated by special instructions such as int3 on x86:
1440
1441 # compile with: gcc -nostartfiles -nostdlib -o int3 int3.S
1442 _start:         .globl  _start
1443                 int3
1444                 movl    $42, %ebx
1445                 movl    $1, %eax
1446                 int     $0x80
1447  */
1448 static int
1449 test_ptrace_setoptions_for_all(void)
1450 {
1451         const unsigned int test_options = PTRACE_O_TRACESYSGOOD |
1452                                           PTRACE_O_TRACEEXEC;
1453         int pid;
1454         int it_worked = 0;
1455
1456         /* Need fork for test. NOMMU has no forks */
1457         if (NOMMU_SYSTEM)
1458                 goto worked; /* be bold, and pretend that test succeeded */
1459
1460         pid = fork();
1461         if (pid < 0)
1462                 perror_msg_and_die("fork");
1463
1464         if (pid == 0) {
1465                 pid = getpid();
1466                 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
1467                         /* Note: exits with exitcode 1 */
1468                         perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1469                                            __func__);
1470                 kill(pid, SIGSTOP);
1471                 _exit(0); /* parent should see entry into this syscall */
1472         }
1473
1474         while (1) {
1475                 int status, tracee_pid;
1476
1477                 errno = 0;
1478                 tracee_pid = wait(&status);
1479                 if (tracee_pid <= 0) {
1480                         if (errno == EINTR)
1481                                 continue;
1482                         kill_save_errno(pid, SIGKILL);
1483                         perror_msg_and_die("%s: unexpected wait result %d",
1484                                            __func__, tracee_pid);
1485                 }
1486                 if (WIFEXITED(status)) {
1487                         if (WEXITSTATUS(status) == 0)
1488                                 break;
1489                         error_msg_and_die("%s: unexpected exit status %u",
1490                                           __func__, WEXITSTATUS(status));
1491                 }
1492                 if (WIFSIGNALED(status)) {
1493                         error_msg_and_die("%s: unexpected signal %u",
1494                                           __func__, WTERMSIG(status));
1495                 }
1496                 if (!WIFSTOPPED(status)) {
1497                         kill(pid, SIGKILL);
1498                         error_msg_and_die("%s: unexpected wait status %x",
1499                                           __func__, status);
1500                 }
1501                 if (WSTOPSIG(status) == SIGSTOP) {
1502                         /*
1503                          * We don't check "options aren't accepted" error.
1504                          * If it happens, we'll never get (SIGTRAP | 0x80),
1505                          * and thus will decide to not use the option.
1506                          * IOW: the outcome of the test will be correct.
1507                          */
1508                         if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0
1509                             && errno != EINVAL && errno != EIO)
1510                                 perror_msg("PTRACE_SETOPTIONS");
1511                 }
1512                 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
1513                         it_worked = 1;
1514                 }
1515                 if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) {
1516                         kill_save_errno(pid, SIGKILL);
1517                         perror_msg_and_die("PTRACE_SYSCALL doesn't work");
1518                 }
1519         }
1520
1521         if (it_worked) {
1522  worked:
1523                 syscall_trap_sig = (SIGTRAP | 0x80);
1524                 ptrace_setoptions |= test_options;
1525                 if (debug_flag)
1526                         fprintf(stderr, "ptrace_setoptions = %#x\n",
1527                                 ptrace_setoptions);
1528                 return 0;
1529         }
1530
1531         error_msg("Test for PTRACE_O_TRACESYSGOOD failed, "
1532                   "giving up using this feature.");
1533         return 1;
1534 }
1535
1536 #if USE_SEIZE
1537 static void
1538 test_ptrace_seize(void)
1539 {
1540         int pid;
1541
1542         /* Need fork for test. NOMMU has no forks */
1543         if (NOMMU_SYSTEM) {
1544                 post_attach_sigstop = 0; /* this sets use_seize to 1 */
1545                 return;
1546         }
1547
1548         pid = fork();
1549         if (pid < 0)
1550                 perror_msg_and_die("fork");
1551
1552         if (pid == 0) {
1553                 pause();
1554                 _exit(0);
1555         }
1556
1557         /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap.  After
1558          * attaching tracee continues to run unless a trap condition occurs.
1559          * PTRACE_SEIZE doesn't affect signal or group stop state.
1560          */
1561         if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) {
1562                 post_attach_sigstop = 0; /* this sets use_seize to 1 */
1563         } else if (debug_flag) {
1564                 fprintf(stderr, "PTRACE_SEIZE doesn't work\n");
1565         }
1566
1567         kill(pid, SIGKILL);
1568
1569         while (1) {
1570                 int status, tracee_pid;
1571
1572                 errno = 0;
1573                 tracee_pid = waitpid(pid, &status, 0);
1574                 if (tracee_pid <= 0) {
1575                         if (errno == EINTR)
1576                                 continue;
1577                         perror_msg_and_die("%s: unexpected wait result %d",
1578                                          __func__, tracee_pid);
1579                 }
1580                 if (WIFSIGNALED(status)) {
1581                         return;
1582                 }
1583                 error_msg_and_die("%s: unexpected wait status %x",
1584                                 __func__, status);
1585         }
1586 }
1587 #else /* !USE_SEIZE */
1588 # define test_ptrace_seize() ((void)0)
1589 #endif
1590
1591 static unsigned
1592 get_os_release(void)
1593 {
1594         unsigned rel;
1595         const char *p;
1596         struct utsname u;
1597         if (uname(&u) < 0)
1598                 perror_msg_and_die("uname");
1599         /* u.release has this form: "3.2.9[-some-garbage]" */
1600         rel = 0;
1601         p = u.release;
1602         for (;;) {
1603                 if (!(*p >= '0' && *p <= '9'))
1604                         error_msg_and_die("Bad OS release string: '%s'", u.release);
1605                 /* Note: this open-codes KERNEL_VERSION(): */
1606                 rel = (rel << 8) | atoi(p);
1607                 if (rel >= KERNEL_VERSION(1,0,0))
1608                         break;
1609                 while (*p >= '0' && *p <= '9')
1610                         p++;
1611                 if (*p != '.') {
1612                         if (rel >= KERNEL_VERSION(0,1,0)) {
1613                                 /* "X.Y-something" means "X.Y.0" */
1614                                 rel <<= 8;
1615                                 break;
1616                         }
1617                         error_msg_and_die("Bad OS release string: '%s'", u.release);
1618                 }
1619                 p++;
1620         }
1621         return rel;
1622 }
1623
1624 /*
1625  * Initialization part of main() was eating much stack (~0.5k),
1626  * which was unused after init.
1627  * We can reuse it if we move init code into a separate function.
1628  *
1629  * Don't want main() to inline us and defeat the reason
1630  * we have a separate function.
1631  */
1632 static void __attribute__ ((noinline))
1633 init(int argc, char *argv[])
1634 {
1635         struct tcb *tcp;
1636         int c, i;
1637         int optF = 0;
1638         struct sigaction sa;
1639
1640         progname = argv[0] ? argv[0] : "strace";
1641
1642         /* Make sure SIGCHLD has the default action so that waitpid
1643            definitely works without losing track of children.  The user
1644            should not have given us a bogus state to inherit, but he might
1645            have.  Arguably we should detect SIG_IGN here and pass it on
1646            to children, but probably noone really needs that.  */
1647         signal(SIGCHLD, SIG_DFL);
1648
1649         strace_tracer_pid = getpid();
1650
1651         os_release = get_os_release();
1652
1653         /* Allocate the initial tcbtab.  */
1654         tcbtabsize = argc;      /* Surely enough for all -p args.  */
1655         tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0]));
1656         if (!tcbtab)
1657                 die_out_of_memory();
1658         tcp = calloc(tcbtabsize, sizeof(*tcp));
1659         if (!tcp)
1660                 die_out_of_memory();
1661         for (c = 0; c < tcbtabsize; c++)
1662                 tcbtab[c] = tcp++;
1663
1664         shared_log = stderr;
1665         set_sortby(DEFAULT_SORTBY);
1666         set_personality(DEFAULT_PERSONALITY);
1667         qualify("trace=all");
1668         qualify("abbrev=all");
1669         qualify("verbose=all");
1670 #if DEFAULT_QUAL_FLAGS != (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
1671 # error Bug in DEFAULT_QUAL_FLAGS
1672 #endif
1673         qualify("signal=all");
1674         while ((c = getopt(argc, argv,
1675                 "+b:cCdfFhiqrtTvVwxyz"
1676 #ifdef USE_LIBUNWIND
1677                 "k"
1678 #endif
1679                 "D"
1680                 "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) {
1681                 switch (c) {
1682                 case 'b':
1683                         if (strcmp(optarg, "execve") != 0)
1684                                 error_msg_and_die("Syscall '%s' for -b isn't supported",
1685                                         optarg);
1686                         detach_on_execve = 1;
1687                         break;
1688                 case 'c':
1689                         if (cflag == CFLAG_BOTH) {
1690                                 error_msg_and_die("-c and -C are mutually exclusive");
1691                         }
1692                         cflag = CFLAG_ONLY_STATS;
1693                         break;
1694                 case 'C':
1695                         if (cflag == CFLAG_ONLY_STATS) {
1696                                 error_msg_and_die("-c and -C are mutually exclusive");
1697                         }
1698                         cflag = CFLAG_BOTH;
1699                         break;
1700                 case 'd':
1701                         debug_flag = 1;
1702                         break;
1703                 case 'D':
1704                         daemonized_tracer = 1;
1705                         break;
1706                 case 'F':
1707                         optF = 1;
1708                         break;
1709                 case 'f':
1710                         followfork++;
1711                         break;
1712                 case 'h':
1713                         usage(stdout, 0);
1714                         break;
1715                 case 'i':
1716                         iflag = 1;
1717                         break;
1718                 case 'q':
1719                         qflag++;
1720                         break;
1721                 case 'r':
1722                         rflag = 1;
1723                         /* fall through to tflag++ */
1724                 case 't':
1725                         tflag++;
1726                         break;
1727                 case 'T':
1728                         Tflag = 1;
1729                         break;
1730                 case 'w':
1731                         count_wallclock = 1;
1732                         break;
1733                 case 'x':
1734                         xflag++;
1735                         break;
1736                 case 'y':
1737                         show_fd_path = 1;
1738                         break;
1739                 case 'v':
1740                         qualify("abbrev=none");
1741                         break;
1742                 case 'V':
1743                         printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
1744                         exit(0);
1745                         break;
1746                 case 'z':
1747                         not_failing_only = 1;
1748                         break;
1749                 case 'a':
1750                         acolumn = string_to_uint(optarg);
1751                         if (acolumn < 0)
1752                                 error_opt_arg(c, optarg);
1753                         break;
1754                 case 'e':
1755                         qualify(optarg);
1756                         break;
1757                 case 'o':
1758                         outfname = strdup(optarg);
1759                         break;
1760                 case 'O':
1761                         i = string_to_uint(optarg);
1762                         if (i < 0)
1763                                 error_opt_arg(c, optarg);
1764                         set_overhead(i);
1765                         break;
1766                 case 'p':
1767                         process_opt_p_list(optarg);
1768                         break;
1769                 case 'P':
1770                         pathtrace_select(optarg);
1771                         break;
1772                 case 's':
1773                         i = string_to_uint(optarg);
1774                         if (i < 0)
1775                                 error_opt_arg(c, optarg);
1776                         max_strlen = i;
1777                         break;
1778                 case 'S':
1779                         set_sortby(optarg);
1780                         break;
1781                 case 'u':
1782                         username = strdup(optarg);
1783                         break;
1784 #ifdef USE_LIBUNWIND
1785                 case 'k':
1786                         stack_trace_enabled = true;
1787                         break;
1788 #endif
1789                 case 'E':
1790                         if (putenv(optarg) < 0)
1791                                 die_out_of_memory();
1792                         break;
1793                 case 'I':
1794                         opt_intr = string_to_uint(optarg);
1795                         if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS)
1796                                 error_opt_arg(c, optarg);
1797                         break;
1798                 default:
1799                         usage(stderr, 1);
1800                         break;
1801                 }
1802         }
1803         argv += optind;
1804         /* argc -= optind; - no need, argc is not used below */
1805
1806         acolumn_spaces = malloc(acolumn + 1);
1807         if (!acolumn_spaces)
1808                 die_out_of_memory();
1809         memset(acolumn_spaces, ' ', acolumn);
1810         acolumn_spaces[acolumn] = '\0';
1811
1812         /* Must have PROG [ARGS], or -p PID. Not both. */
1813         if (!argv[0] == !nprocs)
1814                 usage(stderr, 1);
1815
1816         if (nprocs != 0 && daemonized_tracer) {
1817                 error_msg_and_die("-D and -p are mutually exclusive");
1818         }
1819
1820         if (!followfork)
1821                 followfork = optF;
1822
1823         if (followfork >= 2 && cflag) {
1824                 error_msg_and_die("(-c or -C) and -ff are mutually exclusive");
1825         }
1826
1827         if (count_wallclock && !cflag) {
1828                 error_msg_and_die("-w must be given with (-c or -C)");
1829         }
1830
1831         if (cflag == CFLAG_ONLY_STATS) {
1832                 if (iflag)
1833                         error_msg("-%c has no effect with -c", 'i');
1834 #ifdef USE_LIBUNWIND
1835                 if (stack_trace_enabled)
1836                         error_msg("-%c has no effect with -c", 'k');
1837 #endif
1838                 if (rflag)
1839                         error_msg("-%c has no effect with -c", 'r');
1840                 if (tflag)
1841                         error_msg("-%c has no effect with -c", 't');
1842                 if (Tflag)
1843                         error_msg("-%c has no effect with -c", 'T');
1844                 if (show_fd_path)
1845                         error_msg("-%c has no effect with -c", 'y');
1846         }
1847
1848 #ifdef USE_LIBUNWIND
1849         if (stack_trace_enabled)
1850                 unwind_init();
1851 #endif
1852
1853         /* See if they want to run as another user. */
1854         if (username != NULL) {
1855                 struct passwd *pent;
1856
1857                 if (getuid() != 0 || geteuid() != 0) {
1858                         error_msg_and_die("You must be root to use the -u option");
1859                 }
1860                 pent = getpwnam(username);
1861                 if (pent == NULL) {
1862                         error_msg_and_die("Cannot find user '%s'", username);
1863                 }
1864                 run_uid = pent->pw_uid;
1865                 run_gid = pent->pw_gid;
1866         }
1867         else {
1868                 run_uid = getuid();
1869                 run_gid = getgid();
1870         }
1871
1872         /*
1873          * On any reasonably recent Linux kernel (circa about 2.5.46)
1874          * need_fork_exec_workarounds should stay 0 after these tests:
1875          */
1876         /*need_fork_exec_workarounds = 0; - already is */
1877         if (followfork)
1878                 need_fork_exec_workarounds = test_ptrace_setoptions_followfork();
1879         need_fork_exec_workarounds |= test_ptrace_setoptions_for_all();
1880         test_ptrace_seize();
1881
1882         /* Check if they want to redirect the output. */
1883         if (outfname) {
1884                 /* See if they want to pipe the output. */
1885                 if (outfname[0] == '|' || outfname[0] == '!') {
1886                         /*
1887                          * We can't do the <outfname>.PID funny business
1888                          * when using popen, so prohibit it.
1889                          */
1890                         if (followfork >= 2)
1891                                 error_msg_and_die("Piping the output and -ff are mutually exclusive");
1892                         shared_log = strace_popen(outfname + 1);
1893                 }
1894                 else if (followfork < 2)
1895                         shared_log = strace_fopen(outfname);
1896         } else {
1897                 /* -ff without -o FILE is the same as single -f */
1898                 if (followfork >= 2)
1899                         followfork = 1;
1900         }
1901
1902         if (!outfname || outfname[0] == '|' || outfname[0] == '!') {
1903                 char *buf = malloc(BUFSIZ);
1904                 if (!buf)
1905                         die_out_of_memory();
1906                 setvbuf(shared_log, buf, _IOLBF, BUFSIZ);
1907         }
1908         if (outfname && argv[0]) {
1909                 if (!opt_intr)
1910                         opt_intr = INTR_NEVER;
1911                 qflag = 1;
1912         }
1913         if (!opt_intr)
1914                 opt_intr = INTR_WHILE_WAIT;
1915
1916         /* argv[0]      -pPID   -oFILE  Default interactive setting
1917          * yes          0       0       INTR_WHILE_WAIT
1918          * no           1       0       INTR_WHILE_WAIT
1919          * yes          0       1       INTR_NEVER
1920          * no           1       1       INTR_WHILE_WAIT
1921          */
1922
1923         sigemptyset(&empty_set);
1924         sigemptyset(&blocked_set);
1925
1926         /* startup_child() must be called before the signal handlers get
1927          * installed below as they are inherited into the spawned process.
1928          * Also we do not need to be protected by them as during interruption
1929          * in the startup_child() mode we kill the spawned process anyway.
1930          */
1931         if (argv[0]) {
1932                 if (!NOMMU_SYSTEM || daemonized_tracer)
1933                         hide_log_until_execve = 1;
1934                 skip_one_b_execve = 1;
1935                 startup_child(argv);
1936         }
1937
1938         sa.sa_handler = SIG_IGN;
1939         sigemptyset(&sa.sa_mask);
1940         sa.sa_flags = 0;
1941         sigaction(SIGTTOU, &sa, NULL); /* SIG_IGN */
1942         sigaction(SIGTTIN, &sa, NULL); /* SIG_IGN */
1943         if (opt_intr != INTR_ANYWHERE) {
1944                 if (opt_intr == INTR_BLOCK_TSTP_TOO)
1945                         sigaction(SIGTSTP, &sa, NULL); /* SIG_IGN */
1946                 /*
1947                  * In interactive mode (if no -o OUTFILE, or -p PID is used),
1948                  * fatal signals are blocked while syscall stop is processed,
1949                  * and acted on in between, when waiting for new syscall stops.
1950                  * In non-interactive mode, signals are ignored.
1951                  */
1952                 if (opt_intr == INTR_WHILE_WAIT) {
1953                         sigaddset(&blocked_set, SIGHUP);
1954                         sigaddset(&blocked_set, SIGINT);
1955                         sigaddset(&blocked_set, SIGQUIT);
1956                         sigaddset(&blocked_set, SIGPIPE);
1957                         sigaddset(&blocked_set, SIGTERM);
1958                         sa.sa_handler = interrupt;
1959                 }
1960                 /* SIG_IGN, or set handler for these */
1961                 sigaction(SIGHUP, &sa, NULL);
1962                 sigaction(SIGINT, &sa, NULL);
1963                 sigaction(SIGQUIT, &sa, NULL);
1964                 sigaction(SIGPIPE, &sa, NULL);
1965                 sigaction(SIGTERM, &sa, NULL);
1966         }
1967         if (nprocs != 0 || daemonized_tracer)
1968                 startup_attach();
1969
1970         /* Do we want pids printed in our -o OUTFILE?
1971          * -ff: no (every pid has its own file); or
1972          * -f: yes (there can be more pids in the future); or
1973          * -p PID1,PID2: yes (there are already more than one pid)
1974          */
1975         print_pid_pfx = (outfname && followfork < 2 && (followfork == 1 || nprocs > 1));
1976 }
1977
1978 static struct tcb *
1979 pid2tcb(int pid)
1980 {
1981         int i;
1982
1983         if (pid <= 0)
1984                 return NULL;
1985
1986         for (i = 0; i < tcbtabsize; i++) {
1987                 struct tcb *tcp = tcbtab[i];
1988                 if (tcp->pid == pid)
1989                         return tcp;
1990         }
1991
1992         return NULL;
1993 }
1994
1995 static void
1996 cleanup(void)
1997 {
1998         int i;
1999         struct tcb *tcp;
2000         int fatal_sig;
2001
2002         /* 'interrupted' is a volatile object, fetch it only once */
2003         fatal_sig = interrupted;
2004         if (!fatal_sig)
2005                 fatal_sig = SIGTERM;
2006
2007         for (i = 0; i < tcbtabsize; i++) {
2008                 tcp = tcbtab[i];
2009                 if (!tcp->pid)
2010                         continue;
2011                 if (debug_flag)
2012                         fprintf(stderr,
2013                                 "cleanup: looking at pid %u\n", tcp->pid);
2014                 if (tcp->pid == strace_child) {
2015                         kill(tcp->pid, SIGCONT);
2016                         kill(tcp->pid, fatal_sig);
2017                 }
2018                 detach(tcp);
2019         }
2020         if (cflag)
2021                 call_summary(shared_log);
2022 }
2023
2024 static void
2025 interrupt(int sig)
2026 {
2027         interrupted = sig;
2028 }
2029
2030 static void
2031 trace(void)
2032 {
2033         struct rusage ru;
2034
2035         /* Used to be "while (nprocs != 0)", but in this testcase:
2036          *  int main() { _exit(!!fork()); }
2037          * under strace -f, parent sometimes (rarely) manages
2038          * to exit before we see the first stop of the child,
2039          * and we are losing track of it:
2040          *  19923 clone(...) = 19924
2041          *  19923 exit_group(1)     = ?
2042          *  19923 +++ exited with 1 +++
2043          * Waiting for ECHILD works better.
2044          * (However, if -o|logger is in use, we can't do that.
2045          * Can work around that by double-forking the logger,
2046          * but that loses the ability to wait for its completion on exit.
2047          * Oh well...)
2048          */
2049         while (1) {
2050                 int pid;
2051                 int wait_errno;
2052                 int status, sig;
2053                 int stopped;
2054                 struct tcb *tcp;
2055                 unsigned event;
2056
2057                 if (interrupted)
2058                         return;
2059
2060                 if (popen_pid != 0 && nprocs == 0)
2061                         return;
2062
2063                 if (interactive)
2064                         sigprocmask(SIG_SETMASK, &empty_set, NULL);
2065                 pid = wait4(-1, &status, __WALL, (cflag ? &ru : NULL));
2066                 wait_errno = errno;
2067                 if (interactive)
2068                         sigprocmask(SIG_BLOCK, &blocked_set, NULL);
2069
2070                 if (pid < 0) {
2071                         if (wait_errno == EINTR)
2072                                 continue;
2073                         if (nprocs == 0 && wait_errno == ECHILD)
2074                                 return;
2075                         /* If nprocs > 0, ECHILD is not expected,
2076                          * treat it as any other error here:
2077                          */
2078                         errno = wait_errno;
2079                         perror_msg_and_die("wait4(__WALL)");
2080                 }
2081
2082                 if (pid == popen_pid) {
2083                         if (!WIFSTOPPED(status))
2084                                 popen_pid = 0;
2085                         continue;
2086                 }
2087
2088                 event = ((unsigned)status >> 16);
2089                 if (debug_flag) {
2090                         char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16];
2091                         char evbuf[sizeof(",EVENT_VFORK_DONE (%u)") + sizeof(int)*3 /*paranoia:*/ + 16];
2092                         strcpy(buf, "???");
2093                         if (WIFSIGNALED(status))
2094 #ifdef WCOREDUMP
2095                                 sprintf(buf, "WIFSIGNALED,%ssig=%s",
2096                                                 WCOREDUMP(status) ? "core," : "",
2097                                                 signame(WTERMSIG(status)));
2098 #else
2099                                 sprintf(buf, "WIFSIGNALED,sig=%s",
2100                                                 signame(WTERMSIG(status)));
2101 #endif
2102                         if (WIFEXITED(status))
2103                                 sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status));
2104                         if (WIFSTOPPED(status))
2105                                 sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status)));
2106 #ifdef WIFCONTINUED
2107                         /* Should never be seen */
2108                         if (WIFCONTINUED(status))
2109                                 strcpy(buf, "WIFCONTINUED");
2110 #endif
2111                         evbuf[0] = '\0';
2112                         if (event != 0) {
2113                                 static const char *const event_names[] = {
2114                                         [PTRACE_EVENT_CLONE] = "CLONE",
2115                                         [PTRACE_EVENT_FORK]  = "FORK",
2116                                         [PTRACE_EVENT_VFORK] = "VFORK",
2117                                         [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE",
2118                                         [PTRACE_EVENT_EXEC]  = "EXEC",
2119                                         [PTRACE_EVENT_EXIT]  = "EXIT",
2120                                         /* [PTRACE_EVENT_STOP (=128)] would make biggish array */
2121                                 };
2122                                 const char *e = "??";
2123                                 if (event < ARRAY_SIZE(event_names))
2124                                         e = event_names[event];
2125                                 else if (event == PTRACE_EVENT_STOP)
2126                                         e = "STOP";
2127                                 sprintf(evbuf, ",EVENT_%s (%u)", e, event);
2128                         }
2129                         fprintf(stderr, " [wait(0x%06x) = %u] %s%s\n", status, pid, buf, evbuf);
2130                 }
2131
2132                 /* Look up 'pid' in our table. */
2133                 tcp = pid2tcb(pid);
2134
2135                 if (!tcp) {
2136                         if (!WIFSTOPPED(status)) {
2137                                 /* This can happen if we inherited
2138                                  * an unknown child. Example:
2139                                  * (sleep 1 & exec strace sleep 2)
2140                                  */
2141                                 error_msg("Exit of unknown pid %u seen", pid);
2142                                 continue;
2143                         }
2144                         if (followfork) {
2145                                 /* We assume it's a fork/vfork/clone child */
2146                                 tcp = alloctcb(pid);
2147                                 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
2148                                 newoutf(tcp);
2149                                 if (!qflag)
2150                                         fprintf(stderr, "Process %d attached\n",
2151                                                 pid);
2152                         } else {
2153                                 /* This can happen if a clone call used
2154                                  * CLONE_PTRACE itself.
2155                                  */
2156                                 ptrace(PTRACE_CONT, pid, (char *) 0, 0);
2157                                 error_msg("Stop of unknown pid %u seen, PTRACE_CONTed it", pid);
2158                                 continue;
2159                         }
2160                 }
2161
2162                 clear_regs();
2163                 if (WIFSTOPPED(status))
2164                         get_regs(pid);
2165
2166                 /* Under Linux, execve changes pid to thread leader's pid,
2167                  * and we see this changed pid on EVENT_EXEC and later,
2168                  * execve sysexit. Leader "disappears" without exit
2169                  * notification. Let user know that, drop leader's tcb,
2170                  * and fix up pid in execve thread's tcb.
2171                  * Effectively, execve thread's tcb replaces leader's tcb.
2172                  *
2173                  * BTW, leader is 'stuck undead' (doesn't report WIFEXITED
2174                  * on exit syscall) in multithreaded programs exactly
2175                  * in order to handle this case.
2176                  *
2177                  * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0.
2178                  * On 2.6 and earlier, it can return garbage.
2179                  */
2180                 if (event == PTRACE_EVENT_EXEC && os_release >= KERNEL_VERSION(3,0,0)) {
2181                         FILE *fp;
2182                         struct tcb *execve_thread;
2183                         long old_pid = 0;
2184
2185                         if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) < 0)
2186                                 goto dont_switch_tcbs;
2187                         /* Avoid truncation in pid2tcb() param passing */
2188                         if (old_pid > UINT_MAX)
2189                                 goto dont_switch_tcbs;
2190                         if (old_pid <= 0 || old_pid == pid)
2191                                 goto dont_switch_tcbs;
2192                         execve_thread = pid2tcb(old_pid);
2193                         /* It should be !NULL, but I feel paranoid */
2194                         if (!execve_thread)
2195                                 goto dont_switch_tcbs;
2196
2197                         if (execve_thread->curcol != 0) {
2198                                 /*
2199                                  * One case we are here is -ff:
2200                                  * try "strace -oLOG -ff test/threaded_execve"
2201                                  */
2202                                 fprintf(execve_thread->outf, " <pid changed to %d ...>\n", pid);
2203                                 /*execve_thread->curcol = 0; - no need, see code below */
2204                         }
2205                         /* Swap output FILEs (needed for -ff) */
2206                         fp = execve_thread->outf;
2207                         execve_thread->outf = tcp->outf;
2208                         tcp->outf = fp;
2209                         /* And their column positions */
2210                         execve_thread->curcol = tcp->curcol;
2211                         tcp->curcol = 0;
2212                         /* Drop leader, but close execve'd thread outfile (if -ff) */
2213                         droptcb(tcp);
2214                         /* Switch to the thread, reusing leader's outfile and pid */
2215                         tcp = execve_thread;
2216                         tcp->pid = pid;
2217                         if (cflag != CFLAG_ONLY_STATS) {
2218                                 printleader(tcp);
2219                                 tprintf("+++ superseded by execve in pid %lu +++\n", old_pid);
2220                                 line_ended();
2221                                 tcp->flags |= TCB_REPRINT;
2222                         }
2223                 }
2224  dont_switch_tcbs:
2225
2226                 if (event == PTRACE_EVENT_EXEC) {
2227                         if (detach_on_execve && !skip_one_b_execve)
2228                                 detach(tcp); /* do "-b execve" thingy */
2229                         skip_one_b_execve = 0;
2230                 }
2231
2232                 /* Set current output file */
2233                 current_tcp = tcp;
2234
2235                 if (cflag) {
2236                         tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2237                         tcp->stime = ru.ru_stime;
2238                 }
2239
2240                 if (WIFSIGNALED(status)) {
2241                         if (pid == strace_child)
2242                                 exit_code = 0x100 | WTERMSIG(status);
2243                         if (cflag != CFLAG_ONLY_STATS
2244                          && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)
2245                         ) {
2246                                 printleader(tcp);
2247 #ifdef WCOREDUMP
2248                                 tprintf("+++ killed by %s %s+++\n",
2249                                         signame(WTERMSIG(status)),
2250                                         WCOREDUMP(status) ? "(core dumped) " : "");
2251 #else
2252                                 tprintf("+++ killed by %s +++\n",
2253                                         signame(WTERMSIG(status)));
2254 #endif
2255                                 line_ended();
2256                         }
2257                         droptcb(tcp);
2258                         continue;
2259                 }
2260                 if (WIFEXITED(status)) {
2261                         if (pid == strace_child)
2262                                 exit_code = WEXITSTATUS(status);
2263                         if (cflag != CFLAG_ONLY_STATS &&
2264                             qflag < 2) {
2265                                 printleader(tcp);
2266                                 tprintf("+++ exited with %d +++\n", WEXITSTATUS(status));
2267                                 line_ended();
2268                         }
2269                         droptcb(tcp);
2270                         continue;
2271                 }
2272                 if (!WIFSTOPPED(status)) {
2273                         fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
2274                         droptcb(tcp);
2275                         continue;
2276                 }
2277
2278                 /* Is this the very first time we see this tracee stopped? */
2279                 if (tcp->flags & TCB_STARTUP) {
2280                         if (debug_flag)
2281                                 fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n", tcp->pid);
2282                         tcp->flags &= ~TCB_STARTUP;
2283                         if (tcp->flags & TCB_BPTSET) {
2284                                 /*
2285                                  * One example is a breakpoint inherited from
2286                                  * parent through fork().
2287                                  */
2288                                 if (clearbpt(tcp) < 0) {
2289                                         /* Pretty fatal */
2290                                         droptcb(tcp);
2291                                         exit_code = 1;
2292                                         return;
2293                                 }
2294                         }
2295                         if (!use_seize && ptrace_setoptions) {
2296                                 if (debug_flag)
2297                                         fprintf(stderr, "setting opts 0x%x on pid %d\n", ptrace_setoptions, tcp->pid);
2298                                 if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) {
2299                                         if (errno != ESRCH) {
2300                                                 /* Should never happen, really */
2301                                                 perror_msg_and_die("PTRACE_SETOPTIONS");
2302                                         }
2303                                 }
2304                         }
2305                 }
2306
2307                 sig = WSTOPSIG(status);
2308
2309                 if (event != 0) {
2310                         /* Ptrace event */
2311 #if USE_SEIZE
2312                         if (event == PTRACE_EVENT_STOP) {
2313                                 /*
2314                                  * PTRACE_INTERRUPT-stop or group-stop.
2315                                  * PTRACE_INTERRUPT-stop has sig == SIGTRAP here.
2316                                  */
2317                                 if (sig == SIGSTOP
2318                                  || sig == SIGTSTP
2319                                  || sig == SIGTTIN
2320                                  || sig == SIGTTOU
2321                                 ) {
2322                                         stopped = 1;
2323                                         goto show_stopsig;
2324                                 }
2325                         }
2326 #endif
2327                         goto restart_tracee_with_sig_0;
2328                 }
2329
2330                 /* Is this post-attach SIGSTOP?
2331                  * Interestingly, the process may stop
2332                  * with STOPSIG equal to some other signal
2333                  * than SIGSTOP if we happend to attach
2334                  * just before the process takes a signal.
2335                  */
2336                 if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
2337                         if (debug_flag)
2338                                 fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid);
2339                         tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
2340                         goto restart_tracee_with_sig_0;
2341                 }
2342
2343                 if (sig != syscall_trap_sig) {
2344                         siginfo_t si;
2345
2346                         /* Nonzero (true) if tracee is stopped by signal
2347                          * (as opposed to "tracee received signal").
2348                          * TODO: shouldn't we check for errno == EINVAL too?
2349                          * We can get ESRCH instead, you know...
2350                          */
2351                         stopped = (ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0);
2352 #if USE_SEIZE
2353  show_stopsig:
2354 #endif
2355                         if (cflag != CFLAG_ONLY_STATS
2356                             && !hide_log_until_execve
2357                             && (qual_flags[sig] & QUAL_SIGNAL)
2358                            ) {
2359                                 printleader(tcp);
2360                                 if (!stopped) {
2361                                         tprintf("--- %s ", signame(sig));
2362                                         printsiginfo(&si, verbose(tcp));
2363                                         tprints(" ---\n");
2364                                 } else
2365                                         tprintf("--- stopped by %s ---\n",
2366                                                 signame(sig));
2367                                 line_ended();
2368                         }
2369
2370                         if (!stopped)
2371                                 /* It's signal-delivery-stop. Inject the signal */
2372                                 goto restart_tracee;
2373
2374                         /* It's group-stop */
2375                         if (use_seize) {
2376                                 /*
2377                                  * This ends ptrace-stop, but does *not* end group-stop.
2378                                  * This makes stopping signals work properly on straced process
2379                                  * (that is, process really stops. It used to continue to run).
2380                                  */
2381                                 if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) {
2382                                         /* Note: ptrace_restart emitted error message */
2383                                         exit_code = 1;
2384                                         return;
2385                                 }
2386                                 continue;
2387                         }
2388                         /* We don't have PTRACE_LISTEN support... */
2389                         goto restart_tracee;
2390                 }
2391
2392                 /* We handled quick cases, we are permitted to interrupt now. */
2393                 if (interrupted)
2394                         return;
2395
2396                 /* This should be syscall entry or exit.
2397                  * (Or it still can be that pesky post-execve SIGTRAP!)
2398                  * Handle it.
2399                  */
2400                 if (trace_syscall(tcp) < 0) {
2401                         /* ptrace() failed in trace_syscall().
2402                          * Likely a result of process disappearing mid-flight.
2403                          * Observed case: exit_group() or SIGKILL terminating
2404                          * all processes in thread group.
2405                          * We assume that ptrace error was caused by process death.
2406                          * We used to detach(tcp) here, but since we no longer
2407                          * implement "detach before death" policy/hack,
2408                          * we can let this process to report its death to us
2409                          * normally, via WIFEXITED or WIFSIGNALED wait status.
2410                          */
2411                         continue;
2412                 }
2413  restart_tracee_with_sig_0:
2414                 sig = 0;
2415  restart_tracee:
2416                 if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) {
2417                         /* Note: ptrace_restart emitted error message */
2418                         exit_code = 1;
2419                         return;
2420                 }
2421         } /* while (1) */
2422 }
2423
2424 int
2425 main(int argc, char *argv[])
2426 {
2427         init(argc, argv);
2428
2429         /* Run main tracing loop */
2430         trace();
2431
2432         cleanup();
2433         fflush(NULL);
2434         if (shared_log != stderr)
2435                 fclose(shared_log);
2436         if (popen_pid) {
2437                 while (waitpid(popen_pid, NULL, 0) < 0 && errno == EINTR)
2438                         ;
2439         }
2440         if (exit_code > 0xff) {
2441                 /* Avoid potential core file clobbering.  */
2442                 struct_rlimit rlim = {0, 0};
2443                 set_rlimit(RLIMIT_CORE, &rlim);
2444
2445                 /* Child was killed by a signal, mimic that.  */
2446                 exit_code &= 0xff;
2447                 signal(exit_code, SIG_DFL);
2448                 raise(exit_code);
2449                 /* Paranoia - what if this signal is not fatal?
2450                    Exit with 128 + signo then.  */
2451                 exit_code += 128;
2452         }
2453
2454         return exit_code;
2455 }