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