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