blob: 4c70b988ea743e23f98923772b22aadf4e7a3e20 [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
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>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00006 * 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.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000029 */
30
31#include "defs.h"
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020032#include <stdarg.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000033#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>
Roland McGrath70b08532004-04-09 00:25:21 +000040#include <dirent.h>
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +010041#include <sys/utsname.h>
Dmitry V. Levin882478a2013-05-13 18:43:28 +000042#ifdef HAVE_PRCTL
43# include <sys/prctl.h>
44#endif
Denys Vlasenko84703742012-02-25 02:38:52 +010045#if defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +000046# include <asm/ptrace_offsets.h>
47#endif
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010048/* In some libc, these aren't declared. Do it ourself: */
Denys Vlasenko96d5a762008-12-29 19:13:27 +000049extern char **environ;
Denys Vlasenko418d66a2009-01-17 01:52:54 +000050extern int optind;
51extern char *optarg;
Denys Vlasenko96d5a762008-12-29 19:13:27 +000052
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010053
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). */
Denys Vlasenko978fbc92012-09-13 10:28:43 +020060# warning "tkill(2) not available, risk of strace hangs!"
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010061# define my_tkill(tid, sig) kill((tid), (sig))
62#endif
63
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +010064/* 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
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010073cflag_t cflag = CFLAG_NONE;
74unsigned int followfork = 0;
Denys Vlasenkof44cce42011-06-21 14:34:10 +020075unsigned int ptrace_setoptions = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010076unsigned int xflag = 0;
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +010077bool need_fork_exec_workarounds = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010078bool debug_flag = 0;
79bool Tflag = 0;
Daniel P. Berrange01997cf2013-05-13 11:30:55 +010080unsigned int qflag = 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020081/* Which WSTOPSIG(status) value marks syscall traps? */
Denys Vlasenko75422762011-05-27 14:36:01 +020082static unsigned int syscall_trap_sig = SIGTRAP;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010083static unsigned int tflag = 0;
84static bool iflag = 0;
85static bool rflag = 0;
86static bool print_pid_pfx = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +010087
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
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000101/*
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;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000114
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100115#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100116static 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
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000123/* Sometimes we want to print only succeeding syscalls. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100124bool not_failing_only = 0;
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000125
Grant Edwards8a082772011-04-07 20:25:40 +0000126/* Show path associated with fd arguments */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100127bool show_fd_path = 0;
Grant Edwards8a082772011-04-07 20:25:40 +0000128
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100129static bool detach_on_execve = 0;
Denys Vlasenko2a3d2752013-05-14 16:07:46 +0200130/* 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;
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100134
Dmitry V. Levina6809652008-11-10 17:14:58 +0000135static int exit_code = 0;
136static int strace_child = 0;
Denys Vlasenko75422762011-05-27 14:36:01 +0200137static int strace_tracer_pid = 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -0700138
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000139static char *username = NULL;
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200140static uid_t run_uid;
141static gid_t run_gid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000142
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100143unsigned int max_strlen = DEFAULT_STRLEN;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000144static int acolumn = DEFAULT_ACOLUMN;
Denys Vlasenko102ec492011-08-25 01:27:59 +0200145static char *acolumn_spaces;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100146
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000147static char *outfname = NULL;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100148/* If -ff, points to stderr. Else, it's our common output log */
149static FILE *shared_log;
150
Denys Vlasenko000b6012012-01-28 01:25:03 +0100151struct tcb *printing_tcp = NULL;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100152static struct tcb *current_tcp;
153
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200154static struct tcb **tcbtab;
Denys Vlasenko2b60c352011-06-22 12:45:25 +0200155static unsigned int nprocs, tcbtabsize;
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200156static const char *progname;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000157
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100158unsigned os_release; /* generated from uname()'s u.release */
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +0100159
Denys Vlasenko4a9ba982013-06-20 11:20:23 +0200160static void detach(struct tcb *tcp);
Andreas Schwabe5355de2009-10-27 16:56:43 +0100161static void cleanup(void);
162static void interrupt(int sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000163static sigset_t empty_set, blocked_set;
164
165#ifdef HAVE_SIG_ATOMIC_T
166static volatile sig_atomic_t interrupted;
Denys Vlasenkoa3559252012-01-29 16:43:51 +0100167#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000168static volatile int interrupted;
Denys Vlasenkoa3559252012-01-29 16:43:51 +0100169#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000170
Denys Vlasenko2c4fb902012-03-15 17:24:49 +0100171#ifndef HAVE_STRERROR
172
173#if !HAVE_DECL_SYS_ERRLIST
174extern int sys_nerr;
175extern char *sys_errlist[];
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +0100176#endif
Denys Vlasenko2c4fb902012-03-15 17:24:49 +0100177
178const char *
179strerror(int err_no)
180{
181 static char buf[sizeof("Unknown error %d") + sizeof(int)*3];
182
183 if (err_no < 1 || err_no >= sys_nerr) {
184 sprintf(buf, "Unknown error %d", err_no);
185 return buf;
186 }
187 return sys_errlist[err_no];
188}
189
190#endif /* HAVE_STERRROR */
191
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000192static void
Denys Vlasenkocb2ad002011-06-23 13:05:29 +0200193usage(FILE *ofp, int exitval)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000194{
195 fprintf(ofp, "\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200196usage: strace [-CdffhiqrtttTvVxxy] [-I n] [-e expr]...\n\
197 [-a column] [-o file] [-s strsize] [-P path]...\n\
198 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
199 or: strace -c[df] [-I n] [-e expr]... [-O overhead] [-S sortby]\n\
200 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000201-c -- count time, calls, and errors for each syscall and report summary\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200202-C -- like -c but also print regular output\n\
Denys Vlasenko3e084ac2012-03-15 13:39:05 +0100203-d -- enable debug output to stderr\n\
Denys Vlasenkob51581e2012-01-29 16:53:03 +0100204-D -- run tracer process as a detached grandchild, not as parent\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000205-f -- follow forks, -ff -- with output into separate files\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000206-i -- print instruction pointer at time of syscall\n\
207-q -- suppress messages about attaching, detaching, etc.\n\
208-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
Denys Vlasenkocdab1be2012-02-03 12:17:57 +0100209-T -- print time spent in each syscall\n\
210-v -- verbose mode: print unabbreviated argv, stat, termios, etc. args\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000211-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000212-y -- print paths associated with file descriptor arguments\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200213-h -- print help message, -V -- print version\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000214-a column -- alignment COLUMN for printing syscall results (default %d)\n\
Denys Vlasenko22efaf02013-02-27 12:15:19 +0100215-b execve -- detach on this syscall\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000216-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
Denys Vlasenko38e79bb2013-02-26 11:33:54 +0100217 options: trace, abbrev, verbose, raw, signal, read, write\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200218-I interruptible --\n\
219 1: no signals are blocked\n\
220 2: fatal signals are blocked while decoding syscall (default)\n\
221 3: fatal signals are always blocked (default if '-o FILE PROG')\n\
222 4: fatal signals and SIGTSTP (^Z) are always blocked\n\
223 (useful to make 'strace -o FILE PROG' not stop on ^Z)\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000224-o file -- send trace output to FILE instead of stderr\n\
225-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
226-p pid -- trace process with process id PID, may be repeated\n\
227-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
228-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
229-u username -- run command as username handling setuid and/or setgid\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000230-E var=val -- put var=val in the environment for command\n\
231-E var -- remove var from the environment for command\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000232-P path -- trace accesses to path\n\
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100233"
Denys Vlasenko38e79bb2013-02-26 11:33:54 +0100234/* ancient, no one should use it
235-F -- attempt to follow vforks (deprecated, use -f)\n\
236 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100237/* this is broken, so don't document it
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000238-z -- print only succeeding syscalls\n\
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100239 */
Roland McGrathde6e5332003-01-24 04:31:23 +0000240, DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000241 exit(exitval);
242}
243
Denys Vlasenko75422762011-05-27 14:36:01 +0200244static void die(void) __attribute__ ((noreturn));
245static void die(void)
246{
247 if (strace_tracer_pid == getpid()) {
248 cflag = 0;
249 cleanup();
250 }
251 exit(1);
252}
253
254static void verror_msg(int err_no, const char *fmt, va_list p)
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200255{
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100256 char *msg;
257
Dmitry V. Levin44d05322011-06-09 15:50:41 +0000258 fflush(NULL);
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100259
260 /* We want to print entire message with single fprintf to ensure
261 * message integrity if stderr is shared with other programs.
262 * Thus we use vasprintf + single fprintf.
263 */
264 msg = NULL;
Denys Vlasenkocfad5432012-01-24 12:48:02 +0100265 if (vasprintf(&msg, fmt, p) >= 0) {
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100266 if (err_no)
267 fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(err_no));
268 else
269 fprintf(stderr, "%s: %s\n", progname, msg);
270 free(msg);
271 } else {
272 /* malloc in vasprintf failed, try it without malloc */
273 fprintf(stderr, "%s: ", progname);
274 vfprintf(stderr, fmt, p);
275 if (err_no)
276 fprintf(stderr, ": %s\n", strerror(err_no));
277 else
278 putc('\n', stderr);
279 }
280 /* We don't switch stderr to buffered, thus fprintf(stderr)
281 * always flushes its output and this is not necessary: */
282 /* fflush(stderr); */
Denys Vlasenko75422762011-05-27 14:36:01 +0200283}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200284
Denys Vlasenko75422762011-05-27 14:36:01 +0200285void error_msg(const char *fmt, ...)
286{
287 va_list p;
288 va_start(p, fmt);
289 verror_msg(0, fmt, p);
290 va_end(p);
291}
292
293void error_msg_and_die(const char *fmt, ...)
294{
295 va_list p;
296 va_start(p, fmt);
297 verror_msg(0, fmt, p);
298 die();
299}
300
301void perror_msg(const char *fmt, ...)
302{
303 va_list p;
304 va_start(p, fmt);
305 verror_msg(errno, fmt, p);
306 va_end(p);
307}
308
309void perror_msg_and_die(const char *fmt, ...)
310{
311 va_list p;
312 va_start(p, fmt);
313 verror_msg(errno, fmt, p);
314 die();
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200315}
316
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200317void die_out_of_memory(void)
318{
319 static bool recursed = 0;
320 if (recursed)
321 exit(1);
322 recursed = 1;
323 error_msg_and_die("Out of memory");
324}
325
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000326static void
327error_opt_arg(int opt, const char *arg)
328{
329 error_msg_and_die("Invalid -%c argument: '%s'", opt, arg);
330}
331
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100332#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100333static int
334ptrace_attach_or_seize(int pid)
335{
336 int r;
337 if (!use_seize)
338 return ptrace(PTRACE_ATTACH, pid, 0, 0);
Denys Vlasenko26bc0602012-07-10 16:36:32 +0200339 r = ptrace(PTRACE_SEIZE, pid, 0, 0);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100340 if (r)
341 return r;
342 r = ptrace(PTRACE_INTERRUPT, pid, 0, 0);
343 return r;
344}
345#else
346# define ptrace_attach_or_seize(pid) ptrace(PTRACE_ATTACH, (pid), 0, 0)
347#endif
348
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100349/*
350 * Used when we want to unblock stopped traced process.
351 * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL.
352 * Returns 0 on success or if error was ESRCH
353 * (presumably process was killed while we talk to it).
354 * Otherwise prints error message and returns -1.
355 */
356static int
357ptrace_restart(int op, struct tcb *tcp, int sig)
358{
359 int err;
360 const char *msg;
361
362 errno = 0;
363 ptrace(op, tcp->pid, (void *) 0, (long) sig);
364 err = errno;
Denys Vlasenko23506752012-03-21 10:32:49 +0100365 if (!err)
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100366 return 0;
367
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100368 msg = "SYSCALL";
369 if (op == PTRACE_CONT)
370 msg = "CONT";
371 if (op == PTRACE_DETACH)
372 msg = "DETACH";
373#ifdef PTRACE_LISTEN
374 if (op == PTRACE_LISTEN)
375 msg = "LISTEN";
376#endif
Denys Vlasenko23506752012-03-21 10:32:49 +0100377 /*
378 * Why curcol != 0? Otherwise sometimes we get this:
379 *
380 * 10252 kill(10253, SIGKILL) = 0
381 * <ptrace(SYSCALL,10252):No such process>10253 ...next decode...
382 *
383 * 10252 died after we retrieved syscall exit data,
384 * but before we tried to restart it. Log looks ugly.
385 */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100386 if (current_tcp && current_tcp->curcol != 0) {
Denys Vlasenko23506752012-03-21 10:32:49 +0100387 tprintf(" <ptrace(%s):%s>\n", msg, strerror(err));
388 line_ended();
389 }
390 if (err == ESRCH)
391 return 0;
392 errno = err;
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100393 perror_msg("ptrace(PTRACE_%s,pid:%d,sig:%d)", msg, tcp->pid, sig);
394 return -1;
395}
396
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200397static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000398set_cloexec_flag(int fd)
399{
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200400 int flags, newflags;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000401
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200402 flags = fcntl(fd, F_GETFD);
403 if (flags < 0) {
404 /* Can happen only if fd is bad.
405 * Should never happen: if it does, we have a bug
406 * in the caller. Therefore we just abort
407 * instead of propagating the error.
408 */
409 perror_msg_and_die("fcntl(%d, F_GETFD)", fd);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000410 }
411
412 newflags = flags | FD_CLOEXEC;
413 if (flags == newflags)
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200414 return;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000415
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200416 fcntl(fd, F_SETFD, newflags); /* never fails */
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000417}
418
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100419static void kill_save_errno(pid_t pid, int sig)
420{
421 int saved_errno = errno;
422
423 (void) kill(pid, sig);
424 errno = saved_errno;
425}
426
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100427/*
428 * When strace is setuid executable, we have to swap uids
429 * before and after filesystem and process management operations.
430 */
431static void
432swap_uid(void)
433{
434 int euid = geteuid(), uid = getuid();
435
436 if (euid != uid && setreuid(euid, uid) < 0) {
437 perror_msg_and_die("setreuid");
438 }
439}
440
441#if _LFS64_LARGEFILE
442# define fopen_for_output fopen64
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000443# define struct_stat struct stat64
444# define stat_file stat64
445# define struct_dirent struct dirent64
446# define read_dir readdir64
447# define struct_rlimit struct rlimit64
448# define set_rlimit setrlimit64
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100449#else
450# define fopen_for_output fopen
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000451# define struct_stat struct stat
452# define stat_file stat
453# define struct_dirent struct dirent
454# define read_dir readdir
455# define struct_rlimit struct rlimit
456# define set_rlimit setrlimit
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100457#endif
458
459static FILE *
460strace_fopen(const char *path)
461{
462 FILE *fp;
463
464 swap_uid();
465 fp = fopen_for_output(path, "w");
466 if (!fp)
467 perror_msg_and_die("Can't fopen '%s'", path);
468 swap_uid();
469 set_cloexec_flag(fileno(fp));
470 return fp;
471}
472
473static int popen_pid = 0;
474
475#ifndef _PATH_BSHELL
476# define _PATH_BSHELL "/bin/sh"
477#endif
478
479/*
480 * We cannot use standard popen(3) here because we have to distinguish
481 * popen child process from other processes we trace, and standard popen(3)
482 * does not export its child's pid.
483 */
484static FILE *
485strace_popen(const char *command)
486{
487 FILE *fp;
488 int fds[2];
489
490 swap_uid();
491 if (pipe(fds) < 0)
492 perror_msg_and_die("pipe");
493
494 set_cloexec_flag(fds[1]); /* never fails */
495
496 popen_pid = vfork();
497 if (popen_pid == -1)
498 perror_msg_and_die("vfork");
499
500 if (popen_pid == 0) {
501 /* child */
502 close(fds[1]);
503 if (fds[0] != 0) {
504 if (dup2(fds[0], 0))
505 perror_msg_and_die("dup2");
506 close(fds[0]);
507 }
508 execl(_PATH_BSHELL, "sh", "-c", command, NULL);
509 perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL);
510 }
511
512 /* parent */
513 close(fds[0]);
514 swap_uid();
515 fp = fdopen(fds[1], "w");
516 if (!fp)
517 die_out_of_memory();
518 return fp;
519}
520
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100521void
522tprintf(const char *fmt, ...)
523{
524 va_list args;
525
526 va_start(args, fmt);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100527 if (current_tcp) {
Denys Vlasenko6e4f3c12012-04-16 18:22:19 +0200528 int n = strace_vfprintf(current_tcp->outf, fmt, args);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100529 if (n < 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100530 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000531 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100532 } else
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100533 current_tcp->curcol += n;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100534 }
535 va_end(args);
536}
537
538void
539tprints(const char *str)
540{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100541 if (current_tcp) {
Denys Vlasenko142aee02012-04-16 18:10:15 +0200542 int n = fputs_unlocked(str, current_tcp->outf);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100543 if (n >= 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100544 current_tcp->curcol += strlen(str);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100545 return;
546 }
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100547 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000548 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100549 }
550}
551
552void
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100553line_ended(void)
554{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100555 if (current_tcp) {
556 current_tcp->curcol = 0;
557 fflush(current_tcp->outf);
558 }
559 if (printing_tcp) {
560 printing_tcp->curcol = 0;
561 printing_tcp = NULL;
562 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100563}
564
565void
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100566printleader(struct tcb *tcp)
567{
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100568 /* If -ff, "previous tcb we printed" is always the same as current,
569 * because we have per-tcb output files.
570 */
571 if (followfork >= 2)
572 printing_tcp = tcp;
573
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100574 if (printing_tcp) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100575 current_tcp = printing_tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100576 if (printing_tcp->curcol != 0 && (followfork < 2 || printing_tcp == tcp)) {
577 /*
578 * case 1: we have a shared log (i.e. not -ff), and last line
579 * wasn't finished (same or different tcb, doesn't matter).
580 * case 2: split log, we are the same tcb, but our last line
581 * didn't finish ("SIGKILL nuked us after syscall entry" etc).
582 */
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100583 tprints(" <unfinished ...>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100584 printing_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100585 }
586 }
587
588 printing_tcp = tcp;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100589 current_tcp = tcp;
590 current_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100591
592 if (print_pid_pfx)
593 tprintf("%-5d ", tcp->pid);
594 else if (nprocs > 1 && !outfname)
595 tprintf("[pid %5u] ", tcp->pid);
596
597 if (tflag) {
598 char str[sizeof("HH:MM:SS")];
599 struct timeval tv, dtv;
600 static struct timeval otv;
601
602 gettimeofday(&tv, NULL);
603 if (rflag) {
604 if (otv.tv_sec == 0)
605 otv = tv;
606 tv_sub(&dtv, &tv, &otv);
607 tprintf("%6ld.%06ld ",
608 (long) dtv.tv_sec, (long) dtv.tv_usec);
609 otv = tv;
610 }
611 else if (tflag > 2) {
612 tprintf("%ld.%06ld ",
613 (long) tv.tv_sec, (long) tv.tv_usec);
614 }
615 else {
616 time_t local = tv.tv_sec;
617 strftime(str, sizeof(str), "%T", localtime(&local));
618 if (tflag > 1)
619 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
620 else
621 tprintf("%s ", str);
622 }
623 }
624 if (iflag)
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200625 print_pc(tcp);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100626}
627
628void
629tabto(void)
630{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100631 if (current_tcp->curcol < acolumn)
632 tprints(acolumn_spaces + current_tcp->curcol);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100633}
634
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100635/* Should be only called directly *after successful attach* to a tracee.
636 * Otherwise, "strace -oFILE -ff -p<nonexistant_pid>"
637 * may create bogus empty FILE.<nonexistant_pid>, and then die.
638 */
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200639static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000640newoutf(struct tcb *tcp)
641{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100642 tcp->outf = shared_log; /* if not -ff mode, the same file is for all */
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100643 if (followfork >= 2) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000644 char name[520 + sizeof(int) * 3];
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000645 sprintf(name, "%.512s.%u", outfname, tcp->pid);
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200646 tcp->outf = strace_fopen(name);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000647 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000648}
649
Denys Vlasenko558e5122012-03-12 23:32:16 +0100650static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100651expand_tcbtab(void)
652{
653 /* Allocate some more TCBs and expand the table.
654 We don't want to relocate the TCBs because our
655 callers have pointers and it would be a pain.
656 So tcbtab is a table of pointers. Since we never
657 free the TCBs, we allocate a single chunk of many. */
658 int i = tcbtabsize;
659 struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0]));
660 struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0]));
661 if (!newtab || !newtcbs)
662 die_out_of_memory();
663 tcbtabsize *= 2;
664 tcbtab = newtab;
665 while (i < tcbtabsize)
666 tcbtab[i++] = newtcbs++;
667}
668
669static struct tcb *
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100670alloctcb(int pid)
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100671{
672 int i;
673 struct tcb *tcp;
674
675 if (nprocs == tcbtabsize)
676 expand_tcbtab();
677
678 for (i = 0; i < tcbtabsize; i++) {
679 tcp = tcbtab[i];
Denys Vlasenkofadbf662013-06-26 14:14:29 +0200680 if (!tcp->pid) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100681 memset(tcp, 0, sizeof(*tcp));
682 tcp->pid = pid;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100683#if SUPPORTED_PERSONALITIES > 1
684 tcp->currpers = current_personality;
685#endif
686 nprocs++;
687 if (debug_flag)
688 fprintf(stderr, "new tcb for pid %d, active tcbs:%d\n", tcp->pid, nprocs);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100689 return tcp;
690 }
691 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100692 error_msg_and_die("bug in alloctcb");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100693}
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100694
695static void
696droptcb(struct tcb *tcp)
697{
698 if (tcp->pid == 0)
699 return;
700
701 nprocs--;
702 if (debug_flag)
703 fprintf(stderr, "dropped tcb for pid %d, %d remain\n", tcp->pid, nprocs);
704
705 if (tcp->outf) {
Denys Vlasenko8511f2a2012-03-22 09:35:51 +0100706 if (followfork >= 2) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100707 if (tcp->curcol != 0)
708 fprintf(tcp->outf, " <detached ...>\n");
709 fclose(tcp->outf);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100710 } else {
711 if (printing_tcp == tcp && tcp->curcol != 0)
712 fprintf(tcp->outf, " <detached ...>\n");
713 fflush(tcp->outf);
714 }
715 }
716
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100717 if (current_tcp == tcp)
718 current_tcp = NULL;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100719 if (printing_tcp == tcp)
720 printing_tcp = NULL;
721
722 memset(tcp, 0, sizeof(*tcp));
723}
724
Denys Vlasenkof1669e72013-06-18 18:09:39 +0200725/* Detach traced process.
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100726 * Never call DETACH twice on the same process as both unattached and
727 * attached-unstopped processes give the same ESRCH. For unattached process we
728 * would SIGSTOP it and wait for its SIGSTOP notification forever.
729 */
Denys Vlasenko4a9ba982013-06-20 11:20:23 +0200730static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100731detach(struct tcb *tcp)
732{
733 int error;
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200734 int status;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100735
736 if (tcp->flags & TCB_BPTSET)
737 clearbpt(tcp);
738
739 /*
740 * Linux wrongly insists the child be stopped
741 * before detaching. Arghh. We go through hoops
742 * to make a clean break of things.
743 */
744#if defined(SPARC)
Denys Vlasenko978fbc92012-09-13 10:28:43 +0200745# undef PTRACE_DETACH
746# define PTRACE_DETACH PTRACE_SUNDETACH
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100747#endif
748
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200749 if (!(tcp->flags & TCB_ATTACHED))
750 goto drop;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100751
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200752 /* We attached but possibly didn't see the expected SIGSTOP.
753 * We must catch exactly one as otherwise the detached process
754 * would be left stopped (process state T).
755 */
756 if (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)
757 goto wait_loop;
758
759 error = ptrace(PTRACE_DETACH, tcp->pid, 0, 0);
760 if (!error) {
761 /* On a clear day, you can see forever. */
762 goto drop;
763 }
764 if (errno != ESRCH) {
765 /* Shouldn't happen. */
766 perror_msg("detach: ptrace(PTRACE_DETACH,%u)", tcp->pid);
767 goto drop;
768 }
769 /* ESRCH: process is either not stopped or doesn't exist. */
770 if (my_tkill(tcp->pid, 0) < 0) {
771 if (errno != ESRCH)
772 /* Shouldn't happen. */
773 perror_msg("detach: tkill(%u,0)", tcp->pid);
774 /* else: process doesn't exist. */
775 goto drop;
776 }
777 /* Process is not stopped, need to stop it. */
778 if (use_seize) {
779 /*
780 * With SEIZE, tracee can be in group-stop already.
781 * In this state sending it another SIGSTOP does nothing.
782 * Need to use INTERRUPT.
783 * Testcase: trying to ^C a "strace -p <stopped_process>".
784 */
785 error = ptrace(PTRACE_INTERRUPT, tcp->pid, 0, 0);
786 if (!error)
787 goto wait_loop;
788 if (errno != ESRCH)
789 perror_msg("detach: ptrace(PTRACE_INTERRUPT,%u)", tcp->pid);
790 }
791 else {
792 error = my_tkill(tcp->pid, SIGSTOP);
793 if (!error)
794 goto wait_loop;
795 if (errno != ESRCH)
796 perror_msg("detach: tkill(%u,SIGSTOP)", tcp->pid);
797 }
798 /* Either process doesn't exist, or some weird error. */
799 goto drop;
800
Denys Vlasenkoa2de9da2013-06-21 15:50:41 +0200801 wait_loop:
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200802 /* We end up here in three cases:
803 * 1. We sent PTRACE_INTERRUPT (use_seize case)
804 * 2. We sent SIGSTOP (!use_seize)
805 * 3. Attach SIGSTOP was already pending (TCB_IGNORE_ONE_SIGSTOP set)
806 */
807 for (;;) {
808 int sig;
809 if (waitpid(tcp->pid, &status, __WALL) < 0) {
810 if (errno == EINTR)
811 continue;
812 /*
813 * if (errno == ECHILD) break;
814 * ^^^ WRONG! We expect this PID to exist,
815 * and want to emit a message otherwise:
816 */
817 perror_msg("detach: waitpid(%u)", tcp->pid);
818 break;
819 }
820 if (!WIFSTOPPED(status)) {
821 /*
822 * Tracee exited or was killed by signal.
823 * We shouldn't normally reach this place:
824 * we don't want to consume exit status.
825 * Consider "strace -p PID" being ^C-ed:
826 * we want merely to detach from PID.
827 *
828 * However, we _can_ end up here if tracee
829 * was SIGKILLed.
830 */
831 break;
832 }
833 sig = WSTOPSIG(status);
834 if (debug_flag)
835 fprintf(stderr, "detach wait: event:%d sig:%d\n",
836 (unsigned)status >> 16, sig);
837 if (use_seize) {
838 unsigned event = (unsigned)status >> 16;
839 if (event == PTRACE_EVENT_STOP /*&& sig == SIGTRAP*/) {
Denys Vlasenkofdfa47a2013-06-20 12:10:21 +0200840 /*
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200841 * sig == SIGTRAP: PTRACE_INTERRUPT stop.
842 * sig == other: process was already stopped
843 * with this stopping sig (see tests/detach-stopped).
844 * Looks like re-injecting this sig is not necessary
845 * in DETACH for the tracee to remain stopped.
Denys Vlasenkofdfa47a2013-06-20 12:10:21 +0200846 */
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200847 sig = 0;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100848 }
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200849 /*
850 * PTRACE_INTERRUPT is not guaranteed to produce
851 * the above event if other ptrace-stop is pending.
852 * See tests/detach-sleeping testcase:
853 * strace got SIGINT while tracee is sleeping.
854 * We sent PTRACE_INTERRUPT.
855 * We see syscall exit, not PTRACE_INTERRUPT stop.
856 * We won't get PTRACE_INTERRUPT stop
857 * if we would CONT now. Need to DETACH.
858 */
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200859 if (sig == syscall_trap_sig)
860 sig = 0;
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200861 /* else: not sure in which case we can be here.
862 * Signal stop? Inject it while detaching.
863 */
864 ptrace_restart(PTRACE_DETACH, tcp, sig);
865 break;
866 }
867 /* Note: this check has to be after use_seize check */
868 /* (else, in use_seize case SIGSTOP will be mistreated) */
869 if (sig == SIGSTOP) {
870 /* Detach, suppressing SIGSTOP */
871 ptrace_restart(PTRACE_DETACH, tcp, 0);
872 break;
873 }
874 if (sig == syscall_trap_sig)
875 sig = 0;
876 /* Can't detach just yet, may need to wait for SIGSTOP */
877 error = ptrace_restart(PTRACE_CONT, tcp, sig);
878 if (error < 0) {
879 /* Should not happen.
880 * Note: ptrace_restart returns 0 on ESRCH, so it's not it.
881 * ptrace_restart already emitted error message.
882 */
883 break;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100884 }
885 }
886
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200887 drop:
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100888 if (!qflag && (tcp->flags & TCB_ATTACHED))
889 fprintf(stderr, "Process %u detached\n", tcp->pid);
890
891 droptcb(tcp);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100892}
893
894static void
Denys Vlasenko558e5122012-03-12 23:32:16 +0100895process_opt_p_list(char *opt)
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100896{
897 while (*opt) {
898 /*
899 * We accept -p PID,PID; -p "`pidof PROG`"; -p "`pgrep PROG`".
900 * pidof uses space as delim, pgrep uses newline. :(
901 */
902 int pid;
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100903 char *delim = opt + strcspn(opt, ", \n\t");
904 char c = *delim;
905
906 *delim = '\0';
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000907 pid = string_to_uint(opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100908 if (pid <= 0) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000909 error_msg_and_die("Invalid process id: '%s'", opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100910 }
911 if (pid == strace_tracer_pid) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000912 error_msg_and_die("I'm sorry, I can't let you do that, Dave.");
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100913 }
914 *delim = c;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100915 alloctcb(pid);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100916 if (c == '\0')
917 break;
918 opt = delim + 1;
919 }
920}
921
Roland McGrath02203312007-06-11 22:06:31 +0000922static void
923startup_attach(void)
924{
925 int tcbi;
926 struct tcb *tcp;
927
928 /*
929 * Block user interruptions as we would leave the traced
930 * process stopped (process state T) if we would terminate in
Denys Vlasenko2e968c02011-09-01 10:23:09 +0200931 * between PTRACE_ATTACH and wait4() on SIGSTOP.
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200932 * We rely on cleanup() from this point on.
Roland McGrath02203312007-06-11 22:06:31 +0000933 */
934 if (interactive)
935 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
936
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000937 if (daemonized_tracer) {
938 pid_t pid = fork();
939 if (pid < 0) {
Denys Vlasenko014ca3a2011-09-02 16:19:30 +0200940 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000941 }
942 if (pid) { /* parent */
943 /*
Denys Vlasenko75422762011-05-27 14:36:01 +0200944 * Wait for grandchild to attach to straced process
945 * (grandparent). Grandchild SIGKILLs us after it attached.
946 * Grandparent's wait() is unblocked by our death,
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000947 * it proceeds to exec the straced program.
948 */
949 pause();
950 _exit(0); /* paranoia */
951 }
Denys Vlasenko75422762011-05-27 14:36:01 +0200952 /* grandchild */
953 /* We will be the tracer process. Remember our new pid: */
954 strace_tracer_pid = getpid();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000955 }
956
Roland McGrath02203312007-06-11 22:06:31 +0000957 for (tcbi = 0; tcbi < tcbtabsize; tcbi++) {
958 tcp = tcbtab[tcbi];
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200959
Denys Vlasenkofadbf662013-06-26 14:14:29 +0200960 if (!tcp->pid)
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100961 continue;
962
Denys Vlasenkod116a732011-09-05 14:01:33 +0200963 /* Is this a process we should attach to, but not yet attached? */
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100964 if (tcp->flags & TCB_ATTACHED)
965 continue; /* no, we already attached it */
Denys Vlasenkod116a732011-09-05 14:01:33 +0200966
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000967 if (followfork && !daemonized_tracer) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000968 char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3];
Roland McGrath02203312007-06-11 22:06:31 +0000969 DIR *dir;
970
971 sprintf(procdir, "/proc/%d/task", tcp->pid);
972 dir = opendir(procdir);
973 if (dir != NULL) {
974 unsigned int ntid = 0, nerr = 0;
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000975 struct_dirent *de;
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200976
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000977 while ((de = read_dir(dir)) != NULL) {
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200978 struct tcb *cur_tcp;
979 int tid;
980
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000981 if (de->d_fileno == 0)
Roland McGrath02203312007-06-11 22:06:31 +0000982 continue;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000983 /* we trust /proc filesystem */
Roland McGrath02203312007-06-11 22:06:31 +0000984 tid = atoi(de->d_name);
985 if (tid <= 0)
986 continue;
987 ++ntid;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100988 if (ptrace_attach_or_seize(tid) < 0) {
Roland McGrath02203312007-06-11 22:06:31 +0000989 ++nerr;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100990 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200991 fprintf(stderr, "attach to pid %d failed\n", tid);
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200992 continue;
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200993 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100994 if (debug_flag)
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200995 fprintf(stderr, "attach to pid %d succeeded\n", tid);
996 cur_tcp = tcp;
997 if (tid != tcp->pid)
998 cur_tcp = alloctcb(tid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100999 cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001000 newoutf(cur_tcp);
Roland McGrath02203312007-06-11 22:06:31 +00001001 }
1002 closedir(dir);
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001003 if (interactive) {
1004 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1005 if (interrupted)
1006 goto ret;
1007 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1008 }
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001009 ntid -= nerr;
1010 if (ntid == 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001011 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +00001012 droptcb(tcp);
1013 continue;
1014 }
1015 if (!qflag) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001016 fprintf(stderr, ntid > 1
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001017? "Process %u attached with %u threads\n"
1018: "Process %u attached\n",
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02001019 tcp->pid, ntid);
Roland McGrath02203312007-06-11 22:06:31 +00001020 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001021 if (!(tcp->flags & TCB_ATTACHED)) {
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001022 /* -p PID, we failed to attach to PID itself
1023 * but did attach to some of its sibling threads.
1024 * Drop PID's tcp.
1025 */
1026 droptcb(tcp);
1027 }
Roland McGrath02203312007-06-11 22:06:31 +00001028 continue;
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001029 } /* if (opendir worked) */
1030 } /* if (-f) */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001031 if (ptrace_attach_or_seize(tcp->pid) < 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001032 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +00001033 droptcb(tcp);
1034 continue;
1035 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001036 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001037 newoutf(tcp);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001038 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001039 fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001040
1041 if (daemonized_tracer) {
1042 /*
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001043 * Make parent go away.
1044 * Also makes grandparent's wait() unblock.
1045 */
1046 kill(getppid(), SIGKILL);
1047 }
1048
Roland McGrath02203312007-06-11 22:06:31 +00001049 if (!qflag)
1050 fprintf(stderr,
Denys Vlasenko9c3861d2012-03-16 15:21:49 +01001051 "Process %u attached\n",
Roland McGrath02203312007-06-11 22:06:31 +00001052 tcp->pid);
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001053 } /* for each tcbtab[] */
Roland McGrath02203312007-06-11 22:06:31 +00001054
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02001055 ret:
Roland McGrath02203312007-06-11 22:06:31 +00001056 if (interactive)
1057 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1058}
1059
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001060/* Stack-o-phobic exec helper, in the hope to work around
1061 * NOMMU + "daemonized tracer" difficulty.
1062 */
1063struct exec_params {
1064 int fd_to_close;
1065 uid_t run_euid;
1066 gid_t run_egid;
1067 char **argv;
1068 char *pathname;
1069};
1070static struct exec_params params_for_tracee;
1071static void __attribute__ ((noinline, noreturn))
1072exec_or_die(void)
1073{
1074 struct exec_params *params = &params_for_tracee;
1075
1076 if (params->fd_to_close >= 0)
1077 close(params->fd_to_close);
1078 if (!daemonized_tracer && !use_seize) {
1079 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) {
1080 perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)");
1081 }
1082 }
1083
1084 if (username != NULL) {
1085 /*
1086 * It is important to set groups before we
1087 * lose privileges on setuid.
1088 */
1089 if (initgroups(username, run_gid) < 0) {
1090 perror_msg_and_die("initgroups");
1091 }
1092 if (setregid(run_gid, params->run_egid) < 0) {
1093 perror_msg_and_die("setregid");
1094 }
1095 if (setreuid(run_uid, params->run_euid) < 0) {
1096 perror_msg_and_die("setreuid");
1097 }
1098 }
1099 else if (geteuid() != 0)
1100 if (setreuid(run_uid, run_uid) < 0) {
1101 perror_msg_and_die("setreuid");
1102 }
1103
1104 if (!daemonized_tracer) {
1105 /*
1106 * Induce a ptrace stop. Tracer (our parent)
1107 * will resume us with PTRACE_SYSCALL and display
1108 * the immediately following execve syscall.
1109 * Can't do this on NOMMU systems, we are after
1110 * vfork: parent is blocked, stopping would deadlock.
1111 */
1112 if (!NOMMU_SYSTEM)
1113 kill(getpid(), SIGSTOP);
1114 } else {
1115 alarm(3);
1116 /* we depend on SIGCHLD set to SIG_DFL by init code */
1117 /* if it happens to be SIG_IGN'ed, wait won't block */
1118 wait(NULL);
1119 alarm(0);
1120 }
1121
1122 execv(params->pathname, params->argv);
1123 perror_msg_and_die("exec");
1124}
1125
Roland McGrath02203312007-06-11 22:06:31 +00001126static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001127startup_child(char **argv)
Roland McGrath02203312007-06-11 22:06:31 +00001128{
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001129 struct_stat statbuf;
Roland McGrath02203312007-06-11 22:06:31 +00001130 const char *filename;
1131 char pathname[MAXPATHLEN];
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001132 int pid;
Roland McGrath02203312007-06-11 22:06:31 +00001133 struct tcb *tcp;
1134
1135 filename = argv[0];
1136 if (strchr(filename, '/')) {
1137 if (strlen(filename) > sizeof pathname - 1) {
1138 errno = ENAMETOOLONG;
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001139 perror_msg_and_die("exec");
Roland McGrath02203312007-06-11 22:06:31 +00001140 }
1141 strcpy(pathname, filename);
1142 }
1143#ifdef USE_DEBUGGING_EXEC
1144 /*
1145 * Debuggers customarily check the current directory
1146 * first regardless of the path but doing that gives
1147 * security geeks a panic attack.
1148 */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001149 else if (stat_file(filename, &statbuf) == 0)
Roland McGrath02203312007-06-11 22:06:31 +00001150 strcpy(pathname, filename);
1151#endif /* USE_DEBUGGING_EXEC */
1152 else {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +00001153 const char *path;
Roland McGrath02203312007-06-11 22:06:31 +00001154 int m, n, len;
1155
1156 for (path = getenv("PATH"); path && *path; path += m) {
Denys Vlasenko4f3df072012-01-29 22:38:35 +01001157 const char *colon = strchr(path, ':');
1158 if (colon) {
1159 n = colon - path;
Roland McGrath02203312007-06-11 22:06:31 +00001160 m = n + 1;
1161 }
1162 else
1163 m = n = strlen(path);
1164 if (n == 0) {
1165 if (!getcwd(pathname, MAXPATHLEN))
1166 continue;
1167 len = strlen(pathname);
1168 }
1169 else if (n > sizeof pathname - 1)
1170 continue;
1171 else {
1172 strncpy(pathname, path, n);
1173 len = n;
1174 }
1175 if (len && pathname[len - 1] != '/')
1176 pathname[len++] = '/';
1177 strcpy(pathname + len, filename);
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001178 if (stat_file(pathname, &statbuf) == 0 &&
Roland McGrath02203312007-06-11 22:06:31 +00001179 /* Accept only regular files
1180 with some execute bits set.
1181 XXX not perfect, might still fail */
1182 S_ISREG(statbuf.st_mode) &&
1183 (statbuf.st_mode & 0111))
1184 break;
1185 }
1186 }
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001187 if (stat_file(pathname, &statbuf) < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001188 perror_msg_and_die("Can't stat '%s'", filename);
Roland McGrath02203312007-06-11 22:06:31 +00001189 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001190
1191 params_for_tracee.fd_to_close = (shared_log != stderr) ? fileno(shared_log) : -1;
1192 params_for_tracee.run_euid = (statbuf.st_mode & S_ISUID) ? statbuf.st_uid : run_uid;
1193 params_for_tracee.run_egid = (statbuf.st_mode & S_ISGID) ? statbuf.st_gid : run_gid;
1194 params_for_tracee.argv = argv;
1195 /*
1196 * On NOMMU, can be safely freed only after execve in tracee.
1197 * It's hard to know when that happens, so we just leak it.
1198 */
Denys Vlasenko05f32512013-02-26 12:00:34 +01001199 params_for_tracee.pathname = NOMMU_SYSTEM ? strdup(pathname) : pathname;
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001200
Dmitry V. Levin882478a2013-05-13 18:43:28 +00001201#if defined HAVE_PRCTL && defined PR_SET_PTRACER && defined PR_SET_PTRACER_ANY
1202 if (daemonized_tracer)
1203 prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY);
1204#endif
1205
Denys Vlasenkoe8681c92013-06-26 14:27:11 +02001206 pid = fork();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001207 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001208 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001209 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001210 if ((pid != 0 && daemonized_tracer)
1211 || (pid == 0 && !daemonized_tracer)
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001212 ) {
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001213 /* We are to become the tracee. Two cases:
1214 * -D: we are parent
1215 * not -D: we are child
1216 */
1217 exec_or_die();
Roland McGrath02203312007-06-11 22:06:31 +00001218 }
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001219
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001220 /* We are the tracer */
Denys Vlasenko75422762011-05-27 14:36:01 +02001221
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001222 if (!daemonized_tracer) {
Denys Vlasenkoe8681c92013-06-26 14:27:11 +02001223 strace_child = pid;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001224 if (!use_seize) {
1225 /* child did PTRACE_TRACEME, nothing to do in parent */
1226 } else {
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001227 if (!NOMMU_SYSTEM) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001228 /* 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) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001236 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001237 perror_msg_and_die("Unexpected wait status %x", status);
1238 }
1239 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001240 /* Else: NOMMU case, we have no way to sync.
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001241 * 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)) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001246 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001247 perror_msg_and_die("Can't attach to %d", pid);
1248 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001249 if (!NOMMU_SYSTEM)
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001250 kill(pid, SIGCONT);
1251 }
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001252 tcp = alloctcb(pid);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001253 if (!NOMMU_SYSTEM)
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001254 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001255 else
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001256 tcp->flags |= TCB_ATTACHED | TCB_STARTUP;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001257 newoutf(tcp);
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001258 }
1259 else {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001260 /* With -D, we are *child* here, IOW: different pid. Fetch it: */
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001261 strace_tracer_pid = getpid();
1262 /* The tracee is our parent: */
1263 pid = getppid();
Denys Vlasenkof2025022012-03-09 15:29:45 +01001264 alloctcb(pid);
1265 /* attaching will be done later, by startup_attach */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001266 /* note: we don't do newoutf(tcp) here either! */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001267
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001268 /* 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).
Denys Vlasenko05f32512013-02-26 12:00:34 +01001284 *
1285 * A cleaner solution is to use makecontext + setcontext
1286 * to create a genuine separate stack and execute on it.
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001287 */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001288 }
Roland McGrath02203312007-06-11 22:06:31 +00001289}
1290
Wang Chaob13c0de2010-11-12 17:25:19 +08001291/*
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001292 * Test whether the kernel support PTRACE_O_TRACECLONE et al options.
Wang Chaob13c0de2010-11-12 17:25:19 +08001293 * First fork a new child, call ptrace with PTRACE_SETOPTIONS on it,
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001294 * and then see which options are supported by the kernel.
Wang Chaob13c0de2010-11-12 17:25:19 +08001295 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001296static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001297test_ptrace_setoptions_followfork(void)
Wang Chaob13c0de2010-11-12 17:25:19 +08001298{
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001299 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;
Wang Chaob13c0de2010-11-12 17:25:19 +08001303
Denys Vlasenko05f32512013-02-26 12:00:34 +01001304 /* Need fork for test. NOMMU has no forks */
1305 if (NOMMU_SYSTEM)
1306 goto worked; /* be bold, and pretend that test succeeded */
1307
Denys Vlasenko5d645812011-08-20 12:48:18 +02001308 pid = fork();
1309 if (pid < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001310 perror_msg_and_die("fork");
Denys Vlasenko5d645812011-08-20 12:48:18 +02001311 if (pid == 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001312 pid = getpid();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001313 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001314 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1315 __func__);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001316 kill_save_errno(pid, SIGSTOP);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001317 if (fork() < 0)
1318 perror_msg_and_die("fork");
1319 _exit(0);
Wang Chaob13c0de2010-11-12 17:25:19 +08001320 }
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001321
1322 while (1) {
1323 int status, tracee_pid;
1324
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001325 errno = 0;
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001326 tracee_pid = wait(&status);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001327 if (tracee_pid <= 0) {
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001328 if (errno == EINTR)
1329 continue;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001330 if (errno == ECHILD)
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001331 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001332 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);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001354 kill_save_errno(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001355 error_msg_and_die("%s: unexpected wait status %x",
1356 __func__, status);
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001357 }
1358 if (tracee_pid != pid) {
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001359 found_grandchild = tracee_pid;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001360 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");
Wang Chaob13c0de2010-11-12 17:25:19 +08001364 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001365 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");
Wang Chaob13c0de2010-11-12 17:25:19 +08001386 }
1387 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001388 if (expected_grandchild && expected_grandchild == found_grandchild) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001389 worked:
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001390 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001391 if (debug_flag)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001392 fprintf(stderr, "ptrace_setoptions = %#x\n",
1393 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001394 return 0;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001395 }
1396 error_msg("Test for PTRACE_O_TRACECLONE failed, "
1397 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001398 return 1;
Wang Chaob13c0de2010-11-12 17:25:19 +08001399}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001400
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 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001415static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001416test_ptrace_setoptions_for_all(void)
1417{
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001418 const unsigned int test_options = PTRACE_O_TRACESYSGOOD |
1419 PTRACE_O_TRACEEXEC;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001420 int pid;
1421 int it_worked = 0;
1422
Denys Vlasenko05f32512013-02-26 12:00:34 +01001423 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001424 if (NOMMU_SYSTEM)
Denys Vlasenko05f32512013-02-26 12:00:34 +01001425 goto worked; /* be bold, and pretend that test succeeded */
Mike Frysinger7ff5ed92012-04-05 01:52:25 -04001426
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001427 pid = fork();
1428 if (pid < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001429 perror_msg_and_die("fork");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001430
1431 if (pid == 0) {
1432 pid = getpid();
1433 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001434 /* Note: exits with exitcode 1 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001435 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1436 __func__);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001437 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;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001449 kill_save_errno(pid, SIGKILL);
1450 perror_msg_and_die("%s: unexpected wait result %d",
1451 __func__, tracee_pid);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001452 }
Denys Vlasenko75422762011-05-27 14:36:01 +02001453 if (WIFEXITED(status)) {
1454 if (WEXITSTATUS(status) == 0)
1455 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001456 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));
Denys Vlasenko75422762011-05-27 14:36:01 +02001462 }
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001463 if (!WIFSTOPPED(status)) {
1464 kill(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001465 error_msg_and_die("%s: unexpected wait status %x",
1466 __func__, status);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001467 }
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 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001475 if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0
1476 && errno != EINVAL && errno != EIO)
1477 perror_msg("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001478 }
1479 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
1480 it_worked = 1;
1481 }
1482 if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001483 kill_save_errno(pid, SIGKILL);
Denys Vlasenko75422762011-05-27 14:36:01 +02001484 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001485 }
1486 }
1487
1488 if (it_worked) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001489 worked:
Denys Vlasenko75422762011-05-27 14:36:01 +02001490 syscall_trap_sig = (SIGTRAP | 0x80);
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001491 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001492 if (debug_flag)
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001493 fprintf(stderr, "ptrace_setoptions = %#x\n",
1494 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001495 return 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001496 }
1497
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001498 error_msg("Test for PTRACE_O_TRACESYSGOOD failed, "
1499 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001500 return 1;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001501}
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001502
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001503#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001504static void
1505test_ptrace_seize(void)
1506{
1507 int pid;
1508
Denys Vlasenko05f32512013-02-26 12:00:34 +01001509 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001510 if (NOMMU_SYSTEM) {
1511 post_attach_sigstop = 0; /* this sets use_seize to 1 */
1512 return;
1513 }
1514
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001515 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 */
Denys Vlasenko26bc0602012-07-10 16:36:32 +02001528 if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001529 post_attach_sigstop = 0; /* this sets use_seize to 1 */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001530 } else if (debug_flag) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001531 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}
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001554#else /* !USE_SEIZE */
1555# define test_ptrace_seize() ((void)0)
1556#endif
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001557
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001558static unsigned
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001559get_os_release(void)
1560{
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001561 unsigned rel;
1562 const char *p;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001563 struct utsname u;
1564 if (uname(&u) < 0)
1565 perror_msg_and_die("uname");
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001566 /* 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++;
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001578 if (*p != '.') {
1579 if (rel >= KERNEL_VERSION(0,1,0)) {
1580 /* "X.Y-something" means "X.Y.0" */
1581 rel <<= 8;
1582 break;
1583 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001584 error_msg_and_die("Bad OS release string: '%s'", u.release);
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001585 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001586 p++;
1587 }
1588 return rel;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001589}
1590
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01001591/*
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[])
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001601{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001602 struct tcb *tcp;
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001603 int c, i;
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001604 int optF = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001605 struct sigaction sa;
1606
Dmitry V. Levin08b623e2007-10-08 21:04:41 +00001607 progname = argv[0] ? argv[0] : "strace";
1608
Denys Vlasenkoa5090542012-03-15 17:27:49 +01001609 /* 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
Denys Vlasenko75422762011-05-27 14:36:01 +02001616 strace_tracer_pid = getpid();
1617
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001618 os_release = get_os_release();
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001619
Roland McGrathee9d4352002-12-18 04:16:10 +00001620 /* Allocate the initial tcbtab. */
1621 tcbtabsize = argc; /* Surely enough for all -p args. */
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001622 tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0]));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001623 if (!tcbtab)
1624 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001625 tcp = calloc(tcbtabsize, sizeof(*tcp));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001626 if (!tcp)
1627 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001628 for (c = 0; c < tcbtabsize; c++)
1629 tcbtab[c] = tcp++;
Roland McGrathee9d4352002-12-18 04:16:10 +00001630
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001631 shared_log = stderr;
Roland McGrath138c6a32006-01-12 09:50:49 +00001632 set_sortby(DEFAULT_SORTBY);
1633 set_personality(DEFAULT_PERSONALITY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001634 qualify("trace=all");
1635 qualify("abbrev=all");
1636 qualify("verbose=all");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001637#if DEFAULT_QUAL_FLAGS != (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
1638# error Bug in DEFAULT_QUAL_FLAGS
1639#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001640 qualify("signal=all");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001641 while ((c = getopt(argc, argv,
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001642 "+b:cCdfFhiqrtTvVxyz"
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001643 "D"
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001644 "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001645 switch (c) {
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001646 case 'b':
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001647 if (strcmp(optarg, "execve") != 0)
1648 error_msg_and_die("Syscall '%s' for -b isn't supported",
1649 optarg);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001650 detach_on_execve = 1;
1651 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001652 case 'c':
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001653 if (cflag == CFLAG_BOTH) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001654 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001655 }
1656 cflag = CFLAG_ONLY_STATS;
1657 break;
1658 case 'C':
1659 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001660 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001661 }
1662 cflag = CFLAG_BOTH;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001663 break;
1664 case 'd':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001665 debug_flag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001666 break;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001667 case 'D':
1668 daemonized_tracer = 1;
1669 break;
Roland McGrath41c48222008-07-18 00:25:10 +00001670 case 'F':
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001671 optF = 1;
1672 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001673 case 'f':
1674 followfork++;
1675 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001676 case 'h':
1677 usage(stdout, 0);
1678 break;
1679 case 'i':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001680 iflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001681 break;
1682 case 'q':
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01001683 qflag++;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001684 break;
1685 case 'r':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001686 rflag = 1;
1687 /* fall through to tflag++ */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001688 case 't':
1689 tflag++;
1690 break;
1691 case 'T':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001692 Tflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001693 break;
1694 case 'x':
1695 xflag++;
1696 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001697 case 'y':
1698 show_fd_path = 1;
1699 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001700 case 'v':
1701 qualify("abbrev=none");
1702 break;
1703 case 'V':
Roland McGrath9c9a2532003-02-20 02:56:29 +00001704 printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001705 exit(0);
1706 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001707 case 'z':
1708 not_failing_only = 1;
1709 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001710 case 'a':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001711 acolumn = string_to_uint(optarg);
Denys Vlasenko102ec492011-08-25 01:27:59 +02001712 if (acolumn < 0)
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001713 error_opt_arg(c, optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001714 break;
1715 case 'e':
1716 qualify(optarg);
1717 break;
1718 case 'o':
1719 outfname = strdup(optarg);
1720 break;
1721 case 'O':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001722 i = string_to_uint(optarg);
1723 if (i < 0)
1724 error_opt_arg(c, optarg);
1725 set_overhead(i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001726 break;
1727 case 'p':
Denys Vlasenkoe8172b72012-03-09 13:01:04 +01001728 process_opt_p_list(optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001729 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001730 case 'P':
Denys Vlasenko7239dbc2013-03-05 15:46:34 +01001731 pathtrace_select(optarg);
Grant Edwards8a082772011-04-07 20:25:40 +00001732 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001733 case 's':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001734 i = string_to_uint(optarg);
1735 if (i < 0)
1736 error_opt_arg(c, optarg);
1737 max_strlen = i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001738 break;
1739 case 'S':
1740 set_sortby(optarg);
1741 break;
1742 case 'u':
1743 username = strdup(optarg);
1744 break;
Roland McGrathde6e5332003-01-24 04:31:23 +00001745 case 'E':
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001746 if (putenv(optarg) < 0)
1747 die_out_of_memory();
Roland McGrathde6e5332003-01-24 04:31:23 +00001748 break;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001749 case 'I':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001750 opt_intr = string_to_uint(optarg);
1751 if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS)
1752 error_opt_arg(c, optarg);
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001753 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001754 default:
1755 usage(stderr, 1);
1756 break;
1757 }
1758 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001759 argv += optind;
1760 /* argc -= optind; - no need, argc is not used below */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001761
Denys Vlasenko102ec492011-08-25 01:27:59 +02001762 acolumn_spaces = malloc(acolumn + 1);
1763 if (!acolumn_spaces)
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001764 die_out_of_memory();
Denys Vlasenko102ec492011-08-25 01:27:59 +02001765 memset(acolumn_spaces, ' ', acolumn);
1766 acolumn_spaces[acolumn] = '\0';
1767
Denys Vlasenko837399a2012-01-24 11:37:03 +01001768 /* Must have PROG [ARGS], or -p PID. Not both. */
Denys Vlasenkofd883382012-03-09 13:03:41 +01001769 if (!argv[0] == !nprocs)
Roland McGrathce0d1542003-11-11 21:24:23 +00001770 usage(stderr, 1);
1771
Denys Vlasenkofd883382012-03-09 13:03:41 +01001772 if (nprocs != 0 && daemonized_tracer) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001773 error_msg_and_die("-D and -p are mutually exclusive");
Wang Chaod322a4b2010-08-05 14:30:11 +08001774 }
1775
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001776 if (!followfork)
1777 followfork = optF;
1778
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001779 if (followfork >= 2 && cflag) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001780 error_msg_and_die("(-c or -C) and -ff are mutually exclusive");
Roland McGrathcb9def62006-04-25 07:48:03 +00001781 }
1782
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001783 /* See if they want to run as another user. */
1784 if (username != NULL) {
1785 struct passwd *pent;
1786
1787 if (getuid() != 0 || geteuid() != 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001788 error_msg_and_die("You must be root to use the -u option");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001789 }
Denys Vlasenko5d645812011-08-20 12:48:18 +02001790 pent = getpwnam(username);
1791 if (pent == NULL) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001792 error_msg_and_die("Cannot find user '%s'", username);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001793 }
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
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001802 /*
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 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001807 if (followfork)
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001808 need_fork_exec_workarounds = test_ptrace_setoptions_followfork();
1809 need_fork_exec_workarounds |= test_ptrace_setoptions_for_all();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001810 test_ptrace_seize();
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001811
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001812 /* Check if they want to redirect the output. */
1813 if (outfname) {
Roland McGrath37b9a662003-11-07 02:26:54 +00001814 /* 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 */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001820 if (followfork >= 2)
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001821 error_msg_and_die("Piping the output and -ff are mutually exclusive");
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001822 shared_log = strace_popen(outfname + 1);
Roland McGrath37b9a662003-11-07 02:26:54 +00001823 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001824 else if (followfork < 2)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001825 shared_log = strace_fopen(outfname);
Denys Vlasenko328bf252012-03-12 23:34:13 +01001826 } else {
1827 /* -ff without -o FILE is the same as single -f */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001828 if (followfork >= 2)
Denys Vlasenko328bf252012-03-12 23:34:13 +01001829 followfork = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001830 }
1831
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001832 if (!outfname || outfname[0] == '|' || outfname[0] == '!') {
Denys Vlasenkoa677da52012-01-24 11:31:51 +01001833 char *buf = malloc(BUFSIZ);
1834 if (!buf)
1835 die_out_of_memory();
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001836 setvbuf(shared_log, buf, _IOLBF, BUFSIZ);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001837 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001838 if (outfname && argv[0]) {
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001839 if (!opt_intr)
1840 opt_intr = INTR_NEVER;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001841 qflag = 1;
Roland McGrath36931052003-06-03 01:35:20 +00001842 }
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001843 if (!opt_intr)
1844 opt_intr = INTR_WHILE_WAIT;
Wang Chaob13c0de2010-11-12 17:25:19 +08001845
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001846 /* 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
Roland McGrath54cc1c82007-11-03 23:34:11 +00001851 */
1852
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001853 sigemptyset(&empty_set);
1854 sigemptyset(&blocked_set);
1855
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001856 /* 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 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001861 if (argv[0]) {
Dmitry V. Levin1d2435b2013-05-14 22:35:46 +00001862 if (!NOMMU_SYSTEM || daemonized_tracer)
1863 hide_log_until_execve = 1;
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001864 skip_one_b_execve = 1;
Denys Vlasenko837399a2012-01-24 11:37:03 +01001865 startup_child(argv);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001866 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001867
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001868 sa.sa_handler = SIG_IGN;
1869 sigemptyset(&sa.sa_mask);
1870 sa.sa_flags = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001871 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;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001889 }
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);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001896 }
Denys Vlasenkofd883382012-03-09 13:03:41 +01001897 if (nprocs != 0 || daemonized_tracer)
Roland McGrath02203312007-06-11 22:06:31 +00001898 startup_attach();
Roland McGrath02203312007-06-11 22:06:31 +00001899
Denys Vlasenkofd883382012-03-09 13:03:41 +01001900 /* 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));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001906}
1907
Denys Vlasenkoeebb04d2012-01-27 15:24:48 +01001908static struct tcb *
Roland McGrath54e931f2010-09-14 18:59:20 -07001909pid2tcb(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];
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001918 if (tcp->pid == pid)
Roland McGrath54e931f2010-09-14 18:59:20 -07001919 return tcp;
1920 }
1921
1922 return NULL;
1923}
1924
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001925static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001926cleanup(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001927{
1928 int i;
1929 struct tcb *tcp;
Denys Vlasenko35218842012-01-29 21:17:56 +01001930 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;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001936
Roland McGrathee9d4352002-12-18 04:16:10 +00001937 for (i = 0; i < tcbtabsize; i++) {
1938 tcp = tcbtab[i];
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001939 if (!tcp->pid)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001940 continue;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001941 if (debug_flag)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001942 fprintf(stderr,
1943 "cleanup: looking at pid %u\n", tcp->pid);
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001944 if (tcp->pid == strace_child) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001945 kill(tcp->pid, SIGCONT);
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001946 kill(tcp->pid, fatal_sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001947 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001948 detach(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001949 }
1950 if (cflag)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001951 call_summary(shared_log);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001952}
1953
1954static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001955interrupt(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001956{
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001957 interrupted = sig;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001958}
1959
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02001960static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001961trace(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001962{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001963 struct rusage ru;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001964
Roland McGratheb9e2e82009-06-02 16:49:22 -07001965 while (nprocs != 0) {
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001966 int pid;
1967 int wait_errno;
1968 int status, sig;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001969 int stopped;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001970 struct tcb *tcp;
1971 unsigned event;
1972
Denys Vlasenko222713a2009-03-17 14:29:59 +00001973 if (interrupted)
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02001974 return;
Denys Vlasenko725dd422013-06-20 11:06:58 +02001975
Roland McGratheb9e2e82009-06-02 16:49:22 -07001976 if (interactive)
1977 sigprocmask(SIG_SETMASK, &empty_set, NULL);
Denys Vlasenko725dd422013-06-20 11:06:58 +02001978 pid = wait4(-1, &status, __WALL, (cflag ? &ru : NULL));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001979 wait_errno = errno;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001980 if (interactive)
1981 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001982
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001983 if (pid < 0) {
Denys Vlasenko725dd422013-06-20 11:06:58 +02001984 if (wait_errno == EINTR)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001985 continue;
Denys Vlasenko725dd422013-06-20 11:06:58 +02001986 if (wait_errno == ECHILD)
1987 /* Should not happen since nprocs > 0 */
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02001988 return;
Denys Vlasenko725dd422013-06-20 11:06:58 +02001989 errno = wait_errno;
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02001990 perror_msg_and_die("wait4(__WALL)");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001991 }
Denys Vlasenko725dd422013-06-20 11:06:58 +02001992
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001993 if (pid == popen_pid) {
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02001994 if (!WIFSTOPPED(status))
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001995 popen_pid = 0;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001996 continue;
1997 }
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001998
1999 event = ((unsigned)status >> 16);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002000 if (debug_flag) {
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002001 char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko1b2bfbc2013-06-21 16:41:50 +02002002 char evbuf[sizeof(",EVENT_VFORK_DONE (%u)") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002003 strcpy(buf, "???");
2004 if (WIFSIGNALED(status))
2005#ifdef WCOREDUMP
2006 sprintf(buf, "WIFSIGNALED,%ssig=%s",
2007 WCOREDUMP(status) ? "core," : "",
2008 signame(WTERMSIG(status)));
2009#else
2010 sprintf(buf, "WIFSIGNALED,sig=%s",
2011 signame(WTERMSIG(status)));
2012#endif
2013 if (WIFEXITED(status))
2014 sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status));
2015 if (WIFSTOPPED(status))
2016 sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status)));
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02002017#ifdef WIFCONTINUED
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002018 /* Should never be seen */
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002019 if (WIFCONTINUED(status))
2020 strcpy(buf, "WIFCONTINUED");
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02002021#endif
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002022 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",
Denys Vlasenko4e020c02013-06-21 16:33:56 +02002031 /* [PTRACE_EVENT_STOP (=128)] would make biggish array */
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002032 };
Denys Vlasenko1b2bfbc2013-06-21 16:41:50 +02002033 const char *e = "??";
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002034 if (event < ARRAY_SIZE(event_names))
2035 e = event_names[event];
Denys Vlasenko4e020c02013-06-21 16:33:56 +02002036 else if (event == PTRACE_EVENT_STOP)
2037 e = "STOP";
Denys Vlasenko1b2bfbc2013-06-21 16:41:50 +02002038 sprintf(evbuf, ",EVENT_%s (%u)", e, event);
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002039 }
2040 fprintf(stderr, " [wait(0x%04x) = %u] %s%s\n", status, pid, buf, evbuf);
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002041 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002042
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002043 /* Look up 'pid' in our table. */
Denys Vlasenko5d645812011-08-20 12:48:18 +02002044 tcp = pid2tcb(pid);
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002045
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002046 if (!tcp) {
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002047 if (!WIFSTOPPED(status)) {
2048 /* This can happen if we inherited
2049 * an unknown child. Example:
2050 * (sleep 1 & exec strace sleep 2)
2051 */
2052 error_msg("Exit of unknown pid %u seen", pid);
2053 continue;
2054 }
Roland McGrath41c48222008-07-18 00:25:10 +00002055 if (followfork) {
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002056 /* We assume it's a fork/vfork/clone child */
Denys Vlasenko418d66a2009-01-17 01:52:54 +00002057 tcp = alloctcb(pid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002058 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01002059 newoutf(tcp);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002060 if (!qflag)
Denys Vlasenko833fb132011-08-17 11:30:56 +02002061 fprintf(stderr, "Process %d attached\n",
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002062 pid);
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002063 } else {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002064 /* This can happen if a clone call used
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002065 * CLONE_PTRACE itself.
Denys Vlasenko71d3d292013-06-21 16:19:46 +02002066 */
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002067 ptrace(PTRACE_CONT, pid, (char *) 0, 0);
2068 error_msg("Stop of unknown pid %u seen, PTRACE_CONTed it", pid);
Denys Vlasenko71d3d292013-06-21 16:19:46 +02002069 continue;
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002070 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002071 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002072
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002073 clear_regs();
2074 if (WIFSTOPPED(status))
2075 get_regs(pid);
2076
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002077 /* Under Linux, execve changes pid to thread leader's pid,
2078 * and we see this changed pid on EVENT_EXEC and later,
2079 * execve sysexit. Leader "disappears" without exit
2080 * notification. Let user know that, drop leader's tcb,
2081 * and fix up pid in execve thread's tcb.
2082 * Effectively, execve thread's tcb replaces leader's tcb.
2083 *
2084 * BTW, leader is 'stuck undead' (doesn't report WIFEXITED
2085 * on exit syscall) in multithreaded programs exactly
2086 * in order to handle this case.
2087 *
2088 * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0.
2089 * On 2.6 and earlier, it can return garbage.
2090 */
2091 if (event == PTRACE_EVENT_EXEC && os_release >= KERNEL_VERSION(3,0,0)) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002092 FILE *fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002093 struct tcb *execve_thread;
2094 long old_pid = 0;
2095
2096 if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) < 0)
2097 goto dont_switch_tcbs;
2098 if (old_pid <= 0 || old_pid == pid)
2099 goto dont_switch_tcbs;
2100 execve_thread = pid2tcb(old_pid);
2101 /* It should be !NULL, but I feel paranoid */
2102 if (!execve_thread)
2103 goto dont_switch_tcbs;
2104
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002105 if (execve_thread->curcol != 0) {
2106 /*
2107 * One case we are here is -ff:
2108 * try "strace -oLOG -ff test/threaded_execve"
2109 */
2110 fprintf(execve_thread->outf, " <pid changed to %d ...>\n", pid);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002111 /*execve_thread->curcol = 0; - no need, see code below */
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002112 }
2113 /* Swap output FILEs (needed for -ff) */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002114 fp = execve_thread->outf;
2115 execve_thread->outf = tcp->outf;
2116 tcp->outf = fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002117 /* And their column positions */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002118 execve_thread->curcol = tcp->curcol;
2119 tcp->curcol = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002120 /* Drop leader, but close execve'd thread outfile (if -ff) */
2121 droptcb(tcp);
2122 /* Switch to the thread, reusing leader's outfile and pid */
2123 tcp = execve_thread;
2124 tcp->pid = pid;
2125 if (cflag != CFLAG_ONLY_STATS) {
2126 printleader(tcp);
2127 tprintf("+++ superseded by execve in pid %lu +++\n", old_pid);
2128 line_ended();
2129 tcp->flags |= TCB_REPRINT;
2130 }
2131 }
2132 dont_switch_tcbs:
2133
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002134 if (event == PTRACE_EVENT_EXEC) {
2135 if (detach_on_execve && !skip_one_b_execve)
2136 detach(tcp); /* do "-b execve" thingy */
2137 skip_one_b_execve = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002138 }
2139
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002140 /* Set current output file */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002141 current_tcp = tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002142
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002143 if (cflag) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002144 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2145 tcp->stime = ru.ru_stime;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002146 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002147
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002148 if (WIFSIGNALED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002149 if (pid == strace_child)
2150 exit_code = 0x100 | WTERMSIG(status);
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002151 if (cflag != CFLAG_ONLY_STATS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01002152 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)
2153 ) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002154 printleader(tcp);
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002155#ifdef WCOREDUMP
Denys Vlasenko000b6012012-01-28 01:25:03 +01002156 tprintf("+++ killed by %s %s+++\n",
Roland McGrath2efe8792004-01-13 09:59:45 +00002157 signame(WTERMSIG(status)),
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002158 WCOREDUMP(status) ? "(core dumped) " : "");
2159#else
Denys Vlasenko000b6012012-01-28 01:25:03 +01002160 tprintf("+++ killed by %s +++\n",
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002161 signame(WTERMSIG(status)));
Roland McGrath2efe8792004-01-13 09:59:45 +00002162#endif
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002163 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002164 }
2165 droptcb(tcp);
2166 continue;
2167 }
2168 if (WIFEXITED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002169 if (pid == strace_child)
2170 exit_code = WEXITSTATUS(status);
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01002171 if (cflag != CFLAG_ONLY_STATS &&
2172 qflag < 2) {
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002173 printleader(tcp);
Denys Vlasenko000b6012012-01-28 01:25:03 +01002174 tprintf("+++ exited with %d +++\n", WEXITSTATUS(status));
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002175 line_ended();
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002176 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002177 droptcb(tcp);
2178 continue;
2179 }
2180 if (!WIFSTOPPED(status)) {
2181 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
2182 droptcb(tcp);
2183 continue;
2184 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002185
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002186 /* Is this the very first time we see this tracee stopped? */
2187 if (tcp->flags & TCB_STARTUP) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002188 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002189 fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n", tcp->pid);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002190 tcp->flags &= ~TCB_STARTUP;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002191 if (tcp->flags & TCB_BPTSET) {
Roland McGrath02203312007-06-11 22:06:31 +00002192 /*
2193 * One example is a breakpoint inherited from
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002194 * parent through fork().
Roland McGrath02203312007-06-11 22:06:31 +00002195 */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002196 if (clearbpt(tcp) < 0) {
2197 /* Pretty fatal */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002198 droptcb(tcp);
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002199 exit_code = 1;
2200 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002201 }
2202 }
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002203 if (ptrace_setoptions) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002204 if (debug_flag)
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002205 fprintf(stderr, "setting opts %x on pid %d\n", ptrace_setoptions, tcp->pid);
2206 if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) {
2207 if (errno != ESRCH) {
2208 /* Should never happen, really */
2209 perror_msg_and_die("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02002210 }
2211 }
2212 }
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002213 }
2214
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002215 sig = WSTOPSIG(status);
2216
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002217 if (event != 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002218 /* Ptrace event */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002219#if USE_SEIZE
Denys Vlasenko26bc0602012-07-10 16:36:32 +02002220 if (event == PTRACE_EVENT_STOP) {
Denys Vlasenko67038162012-01-29 16:46:46 +01002221 /*
2222 * PTRACE_INTERRUPT-stop or group-stop.
2223 * PTRACE_INTERRUPT-stop has sig == SIGTRAP here.
2224 */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002225 if (sig == SIGSTOP
2226 || sig == SIGTSTP
2227 || sig == SIGTTIN
2228 || sig == SIGTTOU
2229 ) {
2230 stopped = 1;
2231 goto show_stopsig;
2232 }
2233 }
2234#endif
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002235 goto restart_tracee_with_sig_0;
2236 }
2237
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002238 /* Is this post-attach SIGSTOP?
2239 * Interestingly, the process may stop
2240 * with STOPSIG equal to some other signal
2241 * than SIGSTOP if we happend to attach
2242 * just before the process takes a signal.
2243 */
2244 if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002245 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002246 fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid);
2247 tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002248 goto restart_tracee_with_sig_0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002249 }
2250
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002251 if (sig != syscall_trap_sig) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002252 siginfo_t si;
2253
2254 /* Nonzero (true) if tracee is stopped by signal
2255 * (as opposed to "tracee received signal").
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002256 * TODO: shouldn't we check for errno == EINVAL too?
2257 * We can get ESRCH instead, you know...
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002258 */
2259 stopped = (ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002260#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002261 show_stopsig:
Denys Vlasenko67038162012-01-29 16:46:46 +01002262#endif
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002263 if (cflag != CFLAG_ONLY_STATS
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002264 && !hide_log_until_execve
2265 && (qual_flags[sig] & QUAL_SIGNAL)
2266 ) {
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002267 printleader(tcp);
2268 if (!stopped) {
2269 tprintf("--- %s ", signame(sig));
2270 printsiginfo(&si, verbose(tcp));
Denys Vlasenko5a2483b2013-07-01 12:49:14 +02002271 tprints(" ---\n");
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002272 } else
Denys Vlasenko5a2483b2013-07-01 12:49:14 +02002273 tprintf("--- stopped by %s ---\n",
2274 signame(sig));
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002275 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002276 }
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002277
2278 if (!stopped)
2279 /* It's signal-delivery-stop. Inject the signal */
2280 goto restart_tracee;
2281
2282 /* It's group-stop */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002283 if (use_seize) {
2284 /*
2285 * This ends ptrace-stop, but does *not* end group-stop.
2286 * This makes stopping signals work properly on straced process
2287 * (that is, process really stops. It used to continue to run).
2288 */
2289 if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) {
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002290 /* Note: ptrace_restart emitted error message */
2291 exit_code = 1;
2292 return;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002293 }
2294 continue;
2295 }
2296 /* We don't have PTRACE_LISTEN support... */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002297 goto restart_tracee;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002298 }
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002299
2300 /* We handled quick cases, we are permitted to interrupt now. */
Roland McGrath02203312007-06-11 22:06:31 +00002301 if (interrupted)
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002302 return;
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002303
2304 /* This should be syscall entry or exit.
2305 * (Or it still can be that pesky post-execve SIGTRAP!)
2306 * Handle it.
2307 */
Denys Vlasenko23506752012-03-21 10:32:49 +01002308 if (trace_syscall(tcp) < 0) {
2309 /* ptrace() failed in trace_syscall().
Roland McGratheb9e2e82009-06-02 16:49:22 -07002310 * Likely a result of process disappearing mid-flight.
Denys Vlasenko23506752012-03-21 10:32:49 +01002311 * Observed case: exit_group() or SIGKILL terminating
Denys Vlasenkof1e69032012-01-04 15:15:26 +01002312 * all processes in thread group.
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002313 * We assume that ptrace error was caused by process death.
Denys Vlasenkof2025022012-03-09 15:29:45 +01002314 * We used to detach(tcp) here, but since we no longer
2315 * implement "detach before death" policy/hack,
2316 * we can let this process to report its death to us
2317 * normally, via WIFEXITED or WIFSIGNALED wait status.
2318 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002319 continue;
2320 }
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002321 restart_tracee_with_sig_0:
2322 sig = 0;
2323 restart_tracee:
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002324 if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) {
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002325 /* Note: ptrace_restart emitted error message */
2326 exit_code = 1;
2327 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002328 }
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002329 } /* while (nprocs != 0) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002330}
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002331
2332int
2333main(int argc, char *argv[])
2334{
2335 init(argc, argv);
2336
2337 /* Run main tracing loop */
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002338 trace();
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002339
2340 cleanup();
2341 fflush(NULL);
Dmitry V. Levincf534362012-07-12 20:54:46 +00002342 if (shared_log != stderr)
2343 fclose(shared_log);
2344 if (popen_pid) {
2345 while (waitpid(popen_pid, NULL, 0) < 0 && errno == EINTR)
2346 ;
2347 }
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002348 if (exit_code > 0xff) {
2349 /* Avoid potential core file clobbering. */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00002350 struct_rlimit rlim = {0, 0};
2351 set_rlimit(RLIMIT_CORE, &rlim);
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002352
2353 /* Child was killed by a signal, mimic that. */
2354 exit_code &= 0xff;
2355 signal(exit_code, SIG_DFL);
2356 raise(exit_code);
2357 /* Paranoia - what if this signal is not fatal?
2358 Exit with 128 + signo then. */
2359 exit_code += 128;
2360 }
2361
2362 return exit_code;
2363}