blob: 063cb94ae2b9c1ea92ed17b3eeeee675fcf5d9d3 [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;
Anton Blancharda34dead2013-07-12 12:22:06 +020080bool iflag = 0;
Daniel P. Berrange01997cf2013-05-13 11:30:55 +010081unsigned int qflag = 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020082/* Which WSTOPSIG(status) value marks syscall traps? */
Denys Vlasenko75422762011-05-27 14:36:01 +020083static unsigned int syscall_trap_sig = SIGTRAP;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010084static unsigned int tflag = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010085static 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)
Denys Vlasenkoc169d942013-07-10 14:33:05 +0200338 return ptrace(PTRACE_ATTACH, pid, 0L, 0L);
339 r = ptrace(PTRACE_SEIZE, pid, 0L, (unsigned long)ptrace_setoptions);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100340 if (r)
341 return r;
Denys Vlasenkoc169d942013-07-10 14:33:05 +0200342 r = ptrace(PTRACE_INTERRUPT, pid, 0L, 0L);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100343 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
Dmitry V. Levin0506f0f2013-11-12 22:34:42 +0000441#ifdef _LARGEFILE64_SOURCE
Dmitry V. Levind3541302014-02-26 00:01:00 +0000442# ifdef HAVE_FOPEN64
443# define fopen_for_output fopen64
444# else
445# define fopen_for_output fopen
446# endif
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000447# define struct_stat struct stat64
448# define stat_file stat64
449# define struct_dirent struct dirent64
450# define read_dir readdir64
451# define struct_rlimit struct rlimit64
452# define set_rlimit setrlimit64
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100453#else
454# define fopen_for_output fopen
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000455# define struct_stat struct stat
456# define stat_file stat
457# define struct_dirent struct dirent
458# define read_dir readdir
459# define struct_rlimit struct rlimit
460# define set_rlimit setrlimit
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100461#endif
462
463static FILE *
464strace_fopen(const char *path)
465{
466 FILE *fp;
467
468 swap_uid();
469 fp = fopen_for_output(path, "w");
470 if (!fp)
471 perror_msg_and_die("Can't fopen '%s'", path);
472 swap_uid();
473 set_cloexec_flag(fileno(fp));
474 return fp;
475}
476
477static int popen_pid = 0;
478
479#ifndef _PATH_BSHELL
480# define _PATH_BSHELL "/bin/sh"
481#endif
482
483/*
484 * We cannot use standard popen(3) here because we have to distinguish
485 * popen child process from other processes we trace, and standard popen(3)
486 * does not export its child's pid.
487 */
488static FILE *
489strace_popen(const char *command)
490{
491 FILE *fp;
Denys Vlasenko519af5a2013-07-02 11:31:24 +0200492 int pid;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100493 int fds[2];
494
495 swap_uid();
496 if (pipe(fds) < 0)
497 perror_msg_and_die("pipe");
498
499 set_cloexec_flag(fds[1]); /* never fails */
500
Denys Vlasenko519af5a2013-07-02 11:31:24 +0200501 pid = vfork();
502 if (pid < 0)
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100503 perror_msg_and_die("vfork");
504
Denys Vlasenko519af5a2013-07-02 11:31:24 +0200505 if (pid == 0) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100506 /* child */
507 close(fds[1]);
508 if (fds[0] != 0) {
509 if (dup2(fds[0], 0))
510 perror_msg_and_die("dup2");
511 close(fds[0]);
512 }
513 execl(_PATH_BSHELL, "sh", "-c", command, NULL);
514 perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL);
515 }
516
517 /* parent */
Denys Vlasenko519af5a2013-07-02 11:31:24 +0200518 popen_pid = pid;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100519 close(fds[0]);
520 swap_uid();
521 fp = fdopen(fds[1], "w");
522 if (!fp)
523 die_out_of_memory();
524 return fp;
525}
526
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100527void
528tprintf(const char *fmt, ...)
529{
530 va_list args;
531
532 va_start(args, fmt);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100533 if (current_tcp) {
Denys Vlasenko6e4f3c12012-04-16 18:22:19 +0200534 int n = strace_vfprintf(current_tcp->outf, fmt, args);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100535 if (n < 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100536 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000537 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100538 } else
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100539 current_tcp->curcol += n;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100540 }
541 va_end(args);
542}
543
Dmitry V. Levind3541302014-02-26 00:01:00 +0000544#ifndef HAVE_FPUTS_UNLOCKED
545# define fputs_unlocked fputs
546#endif
547
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100548void
549tprints(const char *str)
550{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100551 if (current_tcp) {
Denys Vlasenko142aee02012-04-16 18:10:15 +0200552 int n = fputs_unlocked(str, current_tcp->outf);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100553 if (n >= 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100554 current_tcp->curcol += strlen(str);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100555 return;
556 }
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100557 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000558 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100559 }
560}
561
562void
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100563line_ended(void)
564{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100565 if (current_tcp) {
566 current_tcp->curcol = 0;
567 fflush(current_tcp->outf);
568 }
569 if (printing_tcp) {
570 printing_tcp->curcol = 0;
571 printing_tcp = NULL;
572 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100573}
574
575void
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100576printleader(struct tcb *tcp)
577{
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100578 /* If -ff, "previous tcb we printed" is always the same as current,
579 * because we have per-tcb output files.
580 */
581 if (followfork >= 2)
582 printing_tcp = tcp;
583
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100584 if (printing_tcp) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100585 current_tcp = printing_tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100586 if (printing_tcp->curcol != 0 && (followfork < 2 || printing_tcp == tcp)) {
587 /*
588 * case 1: we have a shared log (i.e. not -ff), and last line
589 * wasn't finished (same or different tcb, doesn't matter).
590 * case 2: split log, we are the same tcb, but our last line
591 * didn't finish ("SIGKILL nuked us after syscall entry" etc).
592 */
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100593 tprints(" <unfinished ...>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100594 printing_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100595 }
596 }
597
598 printing_tcp = tcp;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100599 current_tcp = tcp;
600 current_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100601
602 if (print_pid_pfx)
603 tprintf("%-5d ", tcp->pid);
604 else if (nprocs > 1 && !outfname)
605 tprintf("[pid %5u] ", tcp->pid);
606
607 if (tflag) {
608 char str[sizeof("HH:MM:SS")];
609 struct timeval tv, dtv;
610 static struct timeval otv;
611
612 gettimeofday(&tv, NULL);
613 if (rflag) {
614 if (otv.tv_sec == 0)
615 otv = tv;
616 tv_sub(&dtv, &tv, &otv);
617 tprintf("%6ld.%06ld ",
618 (long) dtv.tv_sec, (long) dtv.tv_usec);
619 otv = tv;
620 }
621 else if (tflag > 2) {
622 tprintf("%ld.%06ld ",
623 (long) tv.tv_sec, (long) tv.tv_usec);
624 }
625 else {
626 time_t local = tv.tv_sec;
627 strftime(str, sizeof(str), "%T", localtime(&local));
628 if (tflag > 1)
629 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
630 else
631 tprintf("%s ", str);
632 }
633 }
634 if (iflag)
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200635 print_pc(tcp);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100636}
637
638void
639tabto(void)
640{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100641 if (current_tcp->curcol < acolumn)
642 tprints(acolumn_spaces + current_tcp->curcol);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100643}
644
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100645/* Should be only called directly *after successful attach* to a tracee.
646 * Otherwise, "strace -oFILE -ff -p<nonexistant_pid>"
647 * may create bogus empty FILE.<nonexistant_pid>, and then die.
648 */
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200649static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000650newoutf(struct tcb *tcp)
651{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100652 tcp->outf = shared_log; /* if not -ff mode, the same file is for all */
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100653 if (followfork >= 2) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000654 char name[520 + sizeof(int) * 3];
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000655 sprintf(name, "%.512s.%u", outfname, tcp->pid);
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200656 tcp->outf = strace_fopen(name);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000657 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000658}
659
Denys Vlasenko558e5122012-03-12 23:32:16 +0100660static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100661expand_tcbtab(void)
662{
663 /* Allocate some more TCBs and expand the table.
664 We don't want to relocate the TCBs because our
665 callers have pointers and it would be a pain.
666 So tcbtab is a table of pointers. Since we never
667 free the TCBs, we allocate a single chunk of many. */
668 int i = tcbtabsize;
669 struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0]));
670 struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0]));
671 if (!newtab || !newtcbs)
672 die_out_of_memory();
673 tcbtabsize *= 2;
674 tcbtab = newtab;
675 while (i < tcbtabsize)
676 tcbtab[i++] = newtcbs++;
677}
678
679static struct tcb *
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100680alloctcb(int pid)
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100681{
682 int i;
683 struct tcb *tcp;
684
685 if (nprocs == tcbtabsize)
686 expand_tcbtab();
687
688 for (i = 0; i < tcbtabsize; i++) {
689 tcp = tcbtab[i];
Denys Vlasenkofadbf662013-06-26 14:14:29 +0200690 if (!tcp->pid) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100691 memset(tcp, 0, sizeof(*tcp));
692 tcp->pid = pid;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100693#if SUPPORTED_PERSONALITIES > 1
694 tcp->currpers = current_personality;
695#endif
696 nprocs++;
697 if (debug_flag)
698 fprintf(stderr, "new tcb for pid %d, active tcbs:%d\n", tcp->pid, nprocs);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100699 return tcp;
700 }
701 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100702 error_msg_and_die("bug in alloctcb");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100703}
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100704
705static void
706droptcb(struct tcb *tcp)
707{
708 if (tcp->pid == 0)
709 return;
710
711 nprocs--;
712 if (debug_flag)
713 fprintf(stderr, "dropped tcb for pid %d, %d remain\n", tcp->pid, nprocs);
714
715 if (tcp->outf) {
Denys Vlasenko8511f2a2012-03-22 09:35:51 +0100716 if (followfork >= 2) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100717 if (tcp->curcol != 0)
718 fprintf(tcp->outf, " <detached ...>\n");
719 fclose(tcp->outf);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100720 } else {
721 if (printing_tcp == tcp && tcp->curcol != 0)
722 fprintf(tcp->outf, " <detached ...>\n");
723 fflush(tcp->outf);
724 }
725 }
726
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100727 if (current_tcp == tcp)
728 current_tcp = NULL;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100729 if (printing_tcp == tcp)
730 printing_tcp = NULL;
731
732 memset(tcp, 0, sizeof(*tcp));
733}
734
Denys Vlasenkof1669e72013-06-18 18:09:39 +0200735/* Detach traced process.
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100736 * Never call DETACH twice on the same process as both unattached and
737 * attached-unstopped processes give the same ESRCH. For unattached process we
738 * would SIGSTOP it and wait for its SIGSTOP notification forever.
739 */
Denys Vlasenko4a9ba982013-06-20 11:20:23 +0200740static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100741detach(struct tcb *tcp)
742{
743 int error;
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200744 int status;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100745
746 if (tcp->flags & TCB_BPTSET)
747 clearbpt(tcp);
748
749 /*
750 * Linux wrongly insists the child be stopped
751 * before detaching. Arghh. We go through hoops
752 * to make a clean break of things.
753 */
754#if defined(SPARC)
Denys Vlasenko978fbc92012-09-13 10:28:43 +0200755# undef PTRACE_DETACH
756# define PTRACE_DETACH PTRACE_SUNDETACH
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100757#endif
758
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200759 if (!(tcp->flags & TCB_ATTACHED))
760 goto drop;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100761
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200762 /* We attached but possibly didn't see the expected SIGSTOP.
763 * We must catch exactly one as otherwise the detached process
764 * would be left stopped (process state T).
765 */
766 if (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)
767 goto wait_loop;
768
769 error = ptrace(PTRACE_DETACH, tcp->pid, 0, 0);
770 if (!error) {
771 /* On a clear day, you can see forever. */
772 goto drop;
773 }
774 if (errno != ESRCH) {
775 /* Shouldn't happen. */
776 perror_msg("detach: ptrace(PTRACE_DETACH,%u)", tcp->pid);
777 goto drop;
778 }
779 /* ESRCH: process is either not stopped or doesn't exist. */
780 if (my_tkill(tcp->pid, 0) < 0) {
781 if (errno != ESRCH)
782 /* Shouldn't happen. */
783 perror_msg("detach: tkill(%u,0)", tcp->pid);
784 /* else: process doesn't exist. */
785 goto drop;
786 }
787 /* Process is not stopped, need to stop it. */
788 if (use_seize) {
789 /*
790 * With SEIZE, tracee can be in group-stop already.
791 * In this state sending it another SIGSTOP does nothing.
792 * Need to use INTERRUPT.
793 * Testcase: trying to ^C a "strace -p <stopped_process>".
794 */
795 error = ptrace(PTRACE_INTERRUPT, tcp->pid, 0, 0);
796 if (!error)
797 goto wait_loop;
798 if (errno != ESRCH)
799 perror_msg("detach: ptrace(PTRACE_INTERRUPT,%u)", tcp->pid);
800 }
801 else {
802 error = my_tkill(tcp->pid, SIGSTOP);
803 if (!error)
804 goto wait_loop;
805 if (errno != ESRCH)
806 perror_msg("detach: tkill(%u,SIGSTOP)", tcp->pid);
807 }
808 /* Either process doesn't exist, or some weird error. */
809 goto drop;
810
Denys Vlasenkoa2de9da2013-06-21 15:50:41 +0200811 wait_loop:
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200812 /* We end up here in three cases:
813 * 1. We sent PTRACE_INTERRUPT (use_seize case)
814 * 2. We sent SIGSTOP (!use_seize)
815 * 3. Attach SIGSTOP was already pending (TCB_IGNORE_ONE_SIGSTOP set)
816 */
817 for (;;) {
818 int sig;
819 if (waitpid(tcp->pid, &status, __WALL) < 0) {
820 if (errno == EINTR)
821 continue;
822 /*
823 * if (errno == ECHILD) break;
824 * ^^^ WRONG! We expect this PID to exist,
825 * and want to emit a message otherwise:
826 */
827 perror_msg("detach: waitpid(%u)", tcp->pid);
828 break;
829 }
830 if (!WIFSTOPPED(status)) {
831 /*
832 * Tracee exited or was killed by signal.
833 * We shouldn't normally reach this place:
834 * we don't want to consume exit status.
835 * Consider "strace -p PID" being ^C-ed:
836 * we want merely to detach from PID.
837 *
838 * However, we _can_ end up here if tracee
839 * was SIGKILLed.
840 */
841 break;
842 }
843 sig = WSTOPSIG(status);
844 if (debug_flag)
845 fprintf(stderr, "detach wait: event:%d sig:%d\n",
846 (unsigned)status >> 16, sig);
847 if (use_seize) {
848 unsigned event = (unsigned)status >> 16;
849 if (event == PTRACE_EVENT_STOP /*&& sig == SIGTRAP*/) {
Denys Vlasenkofdfa47a2013-06-20 12:10:21 +0200850 /*
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200851 * sig == SIGTRAP: PTRACE_INTERRUPT stop.
852 * sig == other: process was already stopped
853 * with this stopping sig (see tests/detach-stopped).
854 * Looks like re-injecting this sig is not necessary
855 * in DETACH for the tracee to remain stopped.
Denys Vlasenkofdfa47a2013-06-20 12:10:21 +0200856 */
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200857 sig = 0;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100858 }
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200859 /*
860 * PTRACE_INTERRUPT is not guaranteed to produce
861 * the above event if other ptrace-stop is pending.
862 * See tests/detach-sleeping testcase:
863 * strace got SIGINT while tracee is sleeping.
864 * We sent PTRACE_INTERRUPT.
865 * We see syscall exit, not PTRACE_INTERRUPT stop.
866 * We won't get PTRACE_INTERRUPT stop
867 * if we would CONT now. Need to DETACH.
868 */
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200869 if (sig == syscall_trap_sig)
870 sig = 0;
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200871 /* else: not sure in which case we can be here.
872 * Signal stop? Inject it while detaching.
873 */
874 ptrace_restart(PTRACE_DETACH, tcp, sig);
875 break;
876 }
877 /* Note: this check has to be after use_seize check */
878 /* (else, in use_seize case SIGSTOP will be mistreated) */
879 if (sig == SIGSTOP) {
880 /* Detach, suppressing SIGSTOP */
881 ptrace_restart(PTRACE_DETACH, tcp, 0);
882 break;
883 }
884 if (sig == syscall_trap_sig)
885 sig = 0;
886 /* Can't detach just yet, may need to wait for SIGSTOP */
887 error = ptrace_restart(PTRACE_CONT, tcp, sig);
888 if (error < 0) {
889 /* Should not happen.
890 * Note: ptrace_restart returns 0 on ESRCH, so it's not it.
891 * ptrace_restart already emitted error message.
892 */
893 break;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100894 }
895 }
896
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200897 drop:
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100898 if (!qflag && (tcp->flags & TCB_ATTACHED))
899 fprintf(stderr, "Process %u detached\n", tcp->pid);
900
901 droptcb(tcp);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100902}
903
904static void
Denys Vlasenko558e5122012-03-12 23:32:16 +0100905process_opt_p_list(char *opt)
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100906{
907 while (*opt) {
908 /*
909 * We accept -p PID,PID; -p "`pidof PROG`"; -p "`pgrep PROG`".
910 * pidof uses space as delim, pgrep uses newline. :(
911 */
912 int pid;
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100913 char *delim = opt + strcspn(opt, ", \n\t");
914 char c = *delim;
915
916 *delim = '\0';
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000917 pid = string_to_uint(opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100918 if (pid <= 0) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000919 error_msg_and_die("Invalid process id: '%s'", opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100920 }
921 if (pid == strace_tracer_pid) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000922 error_msg_and_die("I'm sorry, I can't let you do that, Dave.");
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100923 }
924 *delim = c;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100925 alloctcb(pid);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100926 if (c == '\0')
927 break;
928 opt = delim + 1;
929 }
930}
931
Roland McGrath02203312007-06-11 22:06:31 +0000932static void
933startup_attach(void)
934{
935 int tcbi;
936 struct tcb *tcp;
937
938 /*
939 * Block user interruptions as we would leave the traced
940 * process stopped (process state T) if we would terminate in
Denys Vlasenko2e968c02011-09-01 10:23:09 +0200941 * between PTRACE_ATTACH and wait4() on SIGSTOP.
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200942 * We rely on cleanup() from this point on.
Roland McGrath02203312007-06-11 22:06:31 +0000943 */
944 if (interactive)
945 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
946
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000947 if (daemonized_tracer) {
948 pid_t pid = fork();
949 if (pid < 0) {
Denys Vlasenko014ca3a2011-09-02 16:19:30 +0200950 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000951 }
952 if (pid) { /* parent */
953 /*
Denys Vlasenko75422762011-05-27 14:36:01 +0200954 * Wait for grandchild to attach to straced process
955 * (grandparent). Grandchild SIGKILLs us after it attached.
956 * Grandparent's wait() is unblocked by our death,
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000957 * it proceeds to exec the straced program.
958 */
959 pause();
960 _exit(0); /* paranoia */
961 }
Denys Vlasenko75422762011-05-27 14:36:01 +0200962 /* grandchild */
963 /* We will be the tracer process. Remember our new pid: */
964 strace_tracer_pid = getpid();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000965 }
966
Roland McGrath02203312007-06-11 22:06:31 +0000967 for (tcbi = 0; tcbi < tcbtabsize; tcbi++) {
968 tcp = tcbtab[tcbi];
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200969
Denys Vlasenkofadbf662013-06-26 14:14:29 +0200970 if (!tcp->pid)
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100971 continue;
972
Denys Vlasenkod116a732011-09-05 14:01:33 +0200973 /* Is this a process we should attach to, but not yet attached? */
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100974 if (tcp->flags & TCB_ATTACHED)
975 continue; /* no, we already attached it */
Denys Vlasenkod116a732011-09-05 14:01:33 +0200976
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000977 if (followfork && !daemonized_tracer) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000978 char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3];
Roland McGrath02203312007-06-11 22:06:31 +0000979 DIR *dir;
980
981 sprintf(procdir, "/proc/%d/task", tcp->pid);
982 dir = opendir(procdir);
983 if (dir != NULL) {
984 unsigned int ntid = 0, nerr = 0;
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000985 struct_dirent *de;
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200986
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000987 while ((de = read_dir(dir)) != NULL) {
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200988 struct tcb *cur_tcp;
989 int tid;
990
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000991 if (de->d_fileno == 0)
Roland McGrath02203312007-06-11 22:06:31 +0000992 continue;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000993 /* we trust /proc filesystem */
Roland McGrath02203312007-06-11 22:06:31 +0000994 tid = atoi(de->d_name);
995 if (tid <= 0)
996 continue;
997 ++ntid;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100998 if (ptrace_attach_or_seize(tid) < 0) {
Roland McGrath02203312007-06-11 22:06:31 +0000999 ++nerr;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001000 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001001 fprintf(stderr, "attach to pid %d failed\n", tid);
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001002 continue;
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001003 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001004 if (debug_flag)
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001005 fprintf(stderr, "attach to pid %d succeeded\n", tid);
1006 cur_tcp = tcp;
1007 if (tid != tcp->pid)
1008 cur_tcp = alloctcb(tid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001009 cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001010 newoutf(cur_tcp);
Roland McGrath02203312007-06-11 22:06:31 +00001011 }
1012 closedir(dir);
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001013 if (interactive) {
1014 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1015 if (interrupted)
1016 goto ret;
1017 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1018 }
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001019 ntid -= nerr;
1020 if (ntid == 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001021 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +00001022 droptcb(tcp);
1023 continue;
1024 }
1025 if (!qflag) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001026 fprintf(stderr, ntid > 1
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001027? "Process %u attached with %u threads\n"
1028: "Process %u attached\n",
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02001029 tcp->pid, ntid);
Roland McGrath02203312007-06-11 22:06:31 +00001030 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001031 if (!(tcp->flags & TCB_ATTACHED)) {
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001032 /* -p PID, we failed to attach to PID itself
1033 * but did attach to some of its sibling threads.
1034 * Drop PID's tcp.
1035 */
1036 droptcb(tcp);
1037 }
Roland McGrath02203312007-06-11 22:06:31 +00001038 continue;
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001039 } /* if (opendir worked) */
1040 } /* if (-f) */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001041 if (ptrace_attach_or_seize(tcp->pid) < 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001042 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +00001043 droptcb(tcp);
1044 continue;
1045 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001046 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001047 newoutf(tcp);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001048 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001049 fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001050
1051 if (daemonized_tracer) {
1052 /*
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001053 * Make parent go away.
1054 * Also makes grandparent's wait() unblock.
1055 */
1056 kill(getppid(), SIGKILL);
1057 }
1058
Roland McGrath02203312007-06-11 22:06:31 +00001059 if (!qflag)
1060 fprintf(stderr,
Denys Vlasenko9c3861d2012-03-16 15:21:49 +01001061 "Process %u attached\n",
Roland McGrath02203312007-06-11 22:06:31 +00001062 tcp->pid);
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001063 } /* for each tcbtab[] */
Roland McGrath02203312007-06-11 22:06:31 +00001064
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02001065 ret:
Roland McGrath02203312007-06-11 22:06:31 +00001066 if (interactive)
1067 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1068}
1069
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001070/* Stack-o-phobic exec helper, in the hope to work around
1071 * NOMMU + "daemonized tracer" difficulty.
1072 */
1073struct exec_params {
1074 int fd_to_close;
1075 uid_t run_euid;
1076 gid_t run_egid;
1077 char **argv;
1078 char *pathname;
1079};
1080static struct exec_params params_for_tracee;
1081static void __attribute__ ((noinline, noreturn))
1082exec_or_die(void)
1083{
1084 struct exec_params *params = &params_for_tracee;
1085
1086 if (params->fd_to_close >= 0)
1087 close(params->fd_to_close);
1088 if (!daemonized_tracer && !use_seize) {
1089 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) {
1090 perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)");
1091 }
1092 }
1093
1094 if (username != NULL) {
1095 /*
1096 * It is important to set groups before we
1097 * lose privileges on setuid.
1098 */
1099 if (initgroups(username, run_gid) < 0) {
1100 perror_msg_and_die("initgroups");
1101 }
1102 if (setregid(run_gid, params->run_egid) < 0) {
1103 perror_msg_and_die("setregid");
1104 }
1105 if (setreuid(run_uid, params->run_euid) < 0) {
1106 perror_msg_and_die("setreuid");
1107 }
1108 }
1109 else if (geteuid() != 0)
1110 if (setreuid(run_uid, run_uid) < 0) {
1111 perror_msg_and_die("setreuid");
1112 }
1113
1114 if (!daemonized_tracer) {
1115 /*
1116 * Induce a ptrace stop. Tracer (our parent)
1117 * will resume us with PTRACE_SYSCALL and display
1118 * the immediately following execve syscall.
1119 * Can't do this on NOMMU systems, we are after
1120 * vfork: parent is blocked, stopping would deadlock.
1121 */
1122 if (!NOMMU_SYSTEM)
1123 kill(getpid(), SIGSTOP);
1124 } else {
1125 alarm(3);
1126 /* we depend on SIGCHLD set to SIG_DFL by init code */
1127 /* if it happens to be SIG_IGN'ed, wait won't block */
1128 wait(NULL);
1129 alarm(0);
1130 }
1131
1132 execv(params->pathname, params->argv);
1133 perror_msg_and_die("exec");
1134}
1135
Roland McGrath02203312007-06-11 22:06:31 +00001136static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001137startup_child(char **argv)
Roland McGrath02203312007-06-11 22:06:31 +00001138{
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001139 struct_stat statbuf;
Roland McGrath02203312007-06-11 22:06:31 +00001140 const char *filename;
1141 char pathname[MAXPATHLEN];
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001142 int pid;
Roland McGrath02203312007-06-11 22:06:31 +00001143 struct tcb *tcp;
1144
1145 filename = argv[0];
1146 if (strchr(filename, '/')) {
1147 if (strlen(filename) > sizeof pathname - 1) {
1148 errno = ENAMETOOLONG;
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001149 perror_msg_and_die("exec");
Roland McGrath02203312007-06-11 22:06:31 +00001150 }
1151 strcpy(pathname, filename);
1152 }
1153#ifdef USE_DEBUGGING_EXEC
1154 /*
1155 * Debuggers customarily check the current directory
1156 * first regardless of the path but doing that gives
1157 * security geeks a panic attack.
1158 */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001159 else if (stat_file(filename, &statbuf) == 0)
Roland McGrath02203312007-06-11 22:06:31 +00001160 strcpy(pathname, filename);
1161#endif /* USE_DEBUGGING_EXEC */
1162 else {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +00001163 const char *path;
Roland McGrath02203312007-06-11 22:06:31 +00001164 int m, n, len;
1165
1166 for (path = getenv("PATH"); path && *path; path += m) {
Denys Vlasenko4f3df072012-01-29 22:38:35 +01001167 const char *colon = strchr(path, ':');
1168 if (colon) {
1169 n = colon - path;
Roland McGrath02203312007-06-11 22:06:31 +00001170 m = n + 1;
1171 }
1172 else
1173 m = n = strlen(path);
1174 if (n == 0) {
1175 if (!getcwd(pathname, MAXPATHLEN))
1176 continue;
1177 len = strlen(pathname);
1178 }
1179 else if (n > sizeof pathname - 1)
1180 continue;
1181 else {
1182 strncpy(pathname, path, n);
1183 len = n;
1184 }
1185 if (len && pathname[len - 1] != '/')
1186 pathname[len++] = '/';
1187 strcpy(pathname + len, filename);
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001188 if (stat_file(pathname, &statbuf) == 0 &&
Roland McGrath02203312007-06-11 22:06:31 +00001189 /* Accept only regular files
1190 with some execute bits set.
1191 XXX not perfect, might still fail */
1192 S_ISREG(statbuf.st_mode) &&
1193 (statbuf.st_mode & 0111))
1194 break;
1195 }
1196 }
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001197 if (stat_file(pathname, &statbuf) < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001198 perror_msg_and_die("Can't stat '%s'", filename);
Roland McGrath02203312007-06-11 22:06:31 +00001199 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001200
1201 params_for_tracee.fd_to_close = (shared_log != stderr) ? fileno(shared_log) : -1;
1202 params_for_tracee.run_euid = (statbuf.st_mode & S_ISUID) ? statbuf.st_uid : run_uid;
1203 params_for_tracee.run_egid = (statbuf.st_mode & S_ISGID) ? statbuf.st_gid : run_gid;
1204 params_for_tracee.argv = argv;
1205 /*
1206 * On NOMMU, can be safely freed only after execve in tracee.
1207 * It's hard to know when that happens, so we just leak it.
1208 */
Denys Vlasenko05f32512013-02-26 12:00:34 +01001209 params_for_tracee.pathname = NOMMU_SYSTEM ? strdup(pathname) : pathname;
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001210
Dmitry V. Levin882478a2013-05-13 18:43:28 +00001211#if defined HAVE_PRCTL && defined PR_SET_PTRACER && defined PR_SET_PTRACER_ANY
1212 if (daemonized_tracer)
1213 prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY);
1214#endif
1215
Denys Vlasenkoe8681c92013-06-26 14:27:11 +02001216 pid = fork();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001217 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001218 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001219 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001220 if ((pid != 0 && daemonized_tracer)
1221 || (pid == 0 && !daemonized_tracer)
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001222 ) {
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001223 /* We are to become the tracee. Two cases:
1224 * -D: we are parent
1225 * not -D: we are child
1226 */
1227 exec_or_die();
Roland McGrath02203312007-06-11 22:06:31 +00001228 }
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001229
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001230 /* We are the tracer */
Denys Vlasenko75422762011-05-27 14:36:01 +02001231
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001232 if (!daemonized_tracer) {
Denys Vlasenkoe8681c92013-06-26 14:27:11 +02001233 strace_child = pid;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001234 if (!use_seize) {
1235 /* child did PTRACE_TRACEME, nothing to do in parent */
1236 } else {
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001237 if (!NOMMU_SYSTEM) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001238 /* Wait until child stopped itself */
1239 int status;
1240 while (waitpid(pid, &status, WSTOPPED) < 0) {
1241 if (errno == EINTR)
1242 continue;
1243 perror_msg_and_die("waitpid");
1244 }
1245 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
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("Unexpected wait status %x", status);
1248 }
1249 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001250 /* Else: NOMMU case, we have no way to sync.
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001251 * Just attach to it as soon as possible.
1252 * This means that we may miss a few first syscalls...
1253 */
1254
1255 if (ptrace_attach_or_seize(pid)) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001256 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001257 perror_msg_and_die("Can't attach to %d", pid);
1258 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001259 if (!NOMMU_SYSTEM)
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001260 kill(pid, SIGCONT);
1261 }
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001262 tcp = alloctcb(pid);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001263 if (!NOMMU_SYSTEM)
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001264 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001265 else
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001266 tcp->flags |= TCB_ATTACHED | TCB_STARTUP;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001267 newoutf(tcp);
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001268 }
1269 else {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001270 /* With -D, we are *child* here, IOW: different pid. Fetch it: */
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001271 strace_tracer_pid = getpid();
1272 /* The tracee is our parent: */
1273 pid = getppid();
Denys Vlasenkof2025022012-03-09 15:29:45 +01001274 alloctcb(pid);
1275 /* attaching will be done later, by startup_attach */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001276 /* note: we don't do newoutf(tcp) here either! */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001277
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001278 /* NOMMU BUG! -D mode is active, we (child) return,
1279 * and we will scribble over parent's stack!
1280 * When parent later unpauses, it segfaults.
1281 *
1282 * We work around it
1283 * (1) by declaring exec_or_die() NORETURN,
1284 * hopefully compiler will just jump to it
1285 * instead of call (won't push anything to stack),
1286 * (2) by trying very hard in exec_or_die()
1287 * to not use any stack,
1288 * (3) having a really big (MAXPATHLEN) stack object
1289 * in this function, which creates a "buffer" between
1290 * child's and parent's stack pointers.
1291 * This may save us if (1) and (2) failed
1292 * and compiler decided to use stack in exec_or_die() anyway
1293 * (happens on i386 because of stack parameter passing).
Denys Vlasenko05f32512013-02-26 12:00:34 +01001294 *
1295 * A cleaner solution is to use makecontext + setcontext
1296 * to create a genuine separate stack and execute on it.
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001297 */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001298 }
Roland McGrath02203312007-06-11 22:06:31 +00001299}
1300
Wang Chaob13c0de2010-11-12 17:25:19 +08001301/*
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001302 * Test whether the kernel support PTRACE_O_TRACECLONE et al options.
Wang Chaob13c0de2010-11-12 17:25:19 +08001303 * First fork a new child, call ptrace with PTRACE_SETOPTIONS on it,
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001304 * and then see which options are supported by the kernel.
Wang Chaob13c0de2010-11-12 17:25:19 +08001305 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001306static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001307test_ptrace_setoptions_followfork(void)
Wang Chaob13c0de2010-11-12 17:25:19 +08001308{
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001309 int pid, expected_grandchild = 0, found_grandchild = 0;
1310 const unsigned int test_options = PTRACE_O_TRACECLONE |
1311 PTRACE_O_TRACEFORK |
1312 PTRACE_O_TRACEVFORK;
Wang Chaob13c0de2010-11-12 17:25:19 +08001313
Denys Vlasenko05f32512013-02-26 12:00:34 +01001314 /* Need fork for test. NOMMU has no forks */
1315 if (NOMMU_SYSTEM)
1316 goto worked; /* be bold, and pretend that test succeeded */
1317
Denys Vlasenko5d645812011-08-20 12:48:18 +02001318 pid = fork();
1319 if (pid < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001320 perror_msg_and_die("fork");
Denys Vlasenko5d645812011-08-20 12:48:18 +02001321 if (pid == 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001322 pid = getpid();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001323 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001324 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1325 __func__);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001326 kill_save_errno(pid, SIGSTOP);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001327 if (fork() < 0)
1328 perror_msg_and_die("fork");
1329 _exit(0);
Wang Chaob13c0de2010-11-12 17:25:19 +08001330 }
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001331
1332 while (1) {
1333 int status, tracee_pid;
1334
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001335 errno = 0;
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001336 tracee_pid = wait(&status);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001337 if (tracee_pid <= 0) {
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001338 if (errno == EINTR)
1339 continue;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001340 if (errno == ECHILD)
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001341 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001342 kill_save_errno(pid, SIGKILL);
1343 perror_msg_and_die("%s: unexpected wait result %d",
1344 __func__, tracee_pid);
1345 }
1346 if (WIFEXITED(status)) {
1347 if (WEXITSTATUS(status)) {
1348 if (tracee_pid != pid)
1349 kill_save_errno(pid, SIGKILL);
1350 error_msg_and_die("%s: unexpected exit status %u",
1351 __func__, WEXITSTATUS(status));
1352 }
1353 continue;
1354 }
1355 if (WIFSIGNALED(status)) {
1356 if (tracee_pid != pid)
1357 kill_save_errno(pid, SIGKILL);
1358 error_msg_and_die("%s: unexpected signal %u",
1359 __func__, WTERMSIG(status));
1360 }
1361 if (!WIFSTOPPED(status)) {
1362 if (tracee_pid != pid)
1363 kill_save_errno(tracee_pid, SIGKILL);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001364 kill_save_errno(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001365 error_msg_and_die("%s: unexpected wait status %x",
1366 __func__, status);
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001367 }
1368 if (tracee_pid != pid) {
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001369 found_grandchild = tracee_pid;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001370 if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0) {
1371 kill_save_errno(tracee_pid, SIGKILL);
1372 kill_save_errno(pid, SIGKILL);
1373 perror_msg_and_die("PTRACE_CONT doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001374 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001375 continue;
1376 }
1377 switch (WSTOPSIG(status)) {
1378 case SIGSTOP:
1379 if (ptrace(PTRACE_SETOPTIONS, pid, 0, test_options) < 0
1380 && errno != EINVAL && errno != EIO)
1381 perror_msg("PTRACE_SETOPTIONS");
1382 break;
1383 case SIGTRAP:
1384 if (status >> 16 == PTRACE_EVENT_FORK) {
1385 long msg = 0;
1386
1387 if (ptrace(PTRACE_GETEVENTMSG, pid,
1388 NULL, (long) &msg) == 0)
1389 expected_grandchild = msg;
1390 }
1391 break;
1392 }
1393 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) {
1394 kill_save_errno(pid, SIGKILL);
1395 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001396 }
1397 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001398 if (expected_grandchild && expected_grandchild == found_grandchild) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001399 worked:
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001400 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001401 if (debug_flag)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001402 fprintf(stderr, "ptrace_setoptions = %#x\n",
1403 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001404 return 0;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001405 }
1406 error_msg("Test for PTRACE_O_TRACECLONE failed, "
1407 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001408 return 1;
Wang Chaob13c0de2010-11-12 17:25:19 +08001409}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001410
1411/*
1412 * Test whether the kernel support PTRACE_O_TRACESYSGOOD.
1413 * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it,
1414 * and then see whether it will stop with (SIGTRAP | 0x80).
1415 *
1416 * Use of this option enables correct handling of user-generated SIGTRAPs,
1417 * and SIGTRAPs generated by special instructions such as int3 on x86:
1418 * _start: .globl _start
1419 * int3
1420 * movl $42, %ebx
1421 * movl $1, %eax
1422 * int $0x80
1423 * (compile with: "gcc -nostartfiles -nostdlib -o int3 int3.S")
1424 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001425static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001426test_ptrace_setoptions_for_all(void)
1427{
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001428 const unsigned int test_options = PTRACE_O_TRACESYSGOOD |
1429 PTRACE_O_TRACEEXEC;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001430 int pid;
1431 int it_worked = 0;
1432
Denys Vlasenko05f32512013-02-26 12:00:34 +01001433 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001434 if (NOMMU_SYSTEM)
Denys Vlasenko05f32512013-02-26 12:00:34 +01001435 goto worked; /* be bold, and pretend that test succeeded */
Mike Frysinger7ff5ed92012-04-05 01:52:25 -04001436
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001437 pid = fork();
1438 if (pid < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001439 perror_msg_and_die("fork");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001440
1441 if (pid == 0) {
1442 pid = getpid();
1443 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001444 /* Note: exits with exitcode 1 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001445 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1446 __func__);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001447 kill(pid, SIGSTOP);
1448 _exit(0); /* parent should see entry into this syscall */
1449 }
1450
1451 while (1) {
1452 int status, tracee_pid;
1453
1454 errno = 0;
1455 tracee_pid = wait(&status);
1456 if (tracee_pid <= 0) {
1457 if (errno == EINTR)
1458 continue;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001459 kill_save_errno(pid, SIGKILL);
1460 perror_msg_and_die("%s: unexpected wait result %d",
1461 __func__, tracee_pid);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001462 }
Denys Vlasenko75422762011-05-27 14:36:01 +02001463 if (WIFEXITED(status)) {
1464 if (WEXITSTATUS(status) == 0)
1465 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001466 error_msg_and_die("%s: unexpected exit status %u",
1467 __func__, WEXITSTATUS(status));
1468 }
1469 if (WIFSIGNALED(status)) {
1470 error_msg_and_die("%s: unexpected signal %u",
1471 __func__, WTERMSIG(status));
Denys Vlasenko75422762011-05-27 14:36:01 +02001472 }
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001473 if (!WIFSTOPPED(status)) {
1474 kill(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001475 error_msg_and_die("%s: unexpected wait status %x",
1476 __func__, status);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001477 }
1478 if (WSTOPSIG(status) == SIGSTOP) {
1479 /*
1480 * We don't check "options aren't accepted" error.
1481 * If it happens, we'll never get (SIGTRAP | 0x80),
1482 * and thus will decide to not use the option.
1483 * IOW: the outcome of the test will be correct.
1484 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001485 if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0
1486 && errno != EINVAL && errno != EIO)
1487 perror_msg("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001488 }
1489 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
1490 it_worked = 1;
1491 }
1492 if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001493 kill_save_errno(pid, SIGKILL);
Denys Vlasenko75422762011-05-27 14:36:01 +02001494 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001495 }
1496 }
1497
1498 if (it_worked) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001499 worked:
Denys Vlasenko75422762011-05-27 14:36:01 +02001500 syscall_trap_sig = (SIGTRAP | 0x80);
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001501 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001502 if (debug_flag)
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001503 fprintf(stderr, "ptrace_setoptions = %#x\n",
1504 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001505 return 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001506 }
1507
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001508 error_msg("Test for PTRACE_O_TRACESYSGOOD failed, "
1509 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001510 return 1;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001511}
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001512
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001513#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001514static void
1515test_ptrace_seize(void)
1516{
1517 int pid;
1518
Denys Vlasenko05f32512013-02-26 12:00:34 +01001519 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001520 if (NOMMU_SYSTEM) {
1521 post_attach_sigstop = 0; /* this sets use_seize to 1 */
1522 return;
1523 }
1524
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001525 pid = fork();
1526 if (pid < 0)
1527 perror_msg_and_die("fork");
1528
1529 if (pid == 0) {
1530 pause();
1531 _exit(0);
1532 }
1533
1534 /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After
1535 * attaching tracee continues to run unless a trap condition occurs.
1536 * PTRACE_SEIZE doesn't affect signal or group stop state.
1537 */
Denys Vlasenko26bc0602012-07-10 16:36:32 +02001538 if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001539 post_attach_sigstop = 0; /* this sets use_seize to 1 */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001540 } else if (debug_flag) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001541 fprintf(stderr, "PTRACE_SEIZE doesn't work\n");
1542 }
1543
1544 kill(pid, SIGKILL);
1545
1546 while (1) {
1547 int status, tracee_pid;
1548
1549 errno = 0;
1550 tracee_pid = waitpid(pid, &status, 0);
1551 if (tracee_pid <= 0) {
1552 if (errno == EINTR)
1553 continue;
1554 perror_msg_and_die("%s: unexpected wait result %d",
1555 __func__, tracee_pid);
1556 }
1557 if (WIFSIGNALED(status)) {
1558 return;
1559 }
1560 error_msg_and_die("%s: unexpected wait status %x",
1561 __func__, status);
1562 }
1563}
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001564#else /* !USE_SEIZE */
1565# define test_ptrace_seize() ((void)0)
1566#endif
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001567
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001568static unsigned
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001569get_os_release(void)
1570{
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001571 unsigned rel;
1572 const char *p;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001573 struct utsname u;
1574 if (uname(&u) < 0)
1575 perror_msg_and_die("uname");
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001576 /* u.release has this form: "3.2.9[-some-garbage]" */
1577 rel = 0;
1578 p = u.release;
1579 for (;;) {
1580 if (!(*p >= '0' && *p <= '9'))
1581 error_msg_and_die("Bad OS release string: '%s'", u.release);
1582 /* Note: this open-codes KERNEL_VERSION(): */
1583 rel = (rel << 8) | atoi(p);
1584 if (rel >= KERNEL_VERSION(1,0,0))
1585 break;
1586 while (*p >= '0' && *p <= '9')
1587 p++;
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001588 if (*p != '.') {
1589 if (rel >= KERNEL_VERSION(0,1,0)) {
1590 /* "X.Y-something" means "X.Y.0" */
1591 rel <<= 8;
1592 break;
1593 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001594 error_msg_and_die("Bad OS release string: '%s'", u.release);
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001595 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001596 p++;
1597 }
1598 return rel;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001599}
1600
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01001601/*
1602 * Initialization part of main() was eating much stack (~0.5k),
1603 * which was unused after init.
1604 * We can reuse it if we move init code into a separate function.
1605 *
1606 * Don't want main() to inline us and defeat the reason
1607 * we have a separate function.
1608 */
1609static void __attribute__ ((noinline))
1610init(int argc, char *argv[])
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001611{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001612 struct tcb *tcp;
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001613 int c, i;
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001614 int optF = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001615 struct sigaction sa;
1616
Dmitry V. Levin08b623e2007-10-08 21:04:41 +00001617 progname = argv[0] ? argv[0] : "strace";
1618
Denys Vlasenkoa5090542012-03-15 17:27:49 +01001619 /* Make sure SIGCHLD has the default action so that waitpid
1620 definitely works without losing track of children. The user
1621 should not have given us a bogus state to inherit, but he might
1622 have. Arguably we should detect SIG_IGN here and pass it on
1623 to children, but probably noone really needs that. */
1624 signal(SIGCHLD, SIG_DFL);
1625
Denys Vlasenko75422762011-05-27 14:36:01 +02001626 strace_tracer_pid = getpid();
1627
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001628 os_release = get_os_release();
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001629
Roland McGrathee9d4352002-12-18 04:16:10 +00001630 /* Allocate the initial tcbtab. */
1631 tcbtabsize = argc; /* Surely enough for all -p args. */
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001632 tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0]));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001633 if (!tcbtab)
1634 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001635 tcp = calloc(tcbtabsize, sizeof(*tcp));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001636 if (!tcp)
1637 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001638 for (c = 0; c < tcbtabsize; c++)
1639 tcbtab[c] = tcp++;
Roland McGrathee9d4352002-12-18 04:16:10 +00001640
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001641 shared_log = stderr;
Roland McGrath138c6a32006-01-12 09:50:49 +00001642 set_sortby(DEFAULT_SORTBY);
1643 set_personality(DEFAULT_PERSONALITY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001644 qualify("trace=all");
1645 qualify("abbrev=all");
1646 qualify("verbose=all");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001647#if DEFAULT_QUAL_FLAGS != (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
1648# error Bug in DEFAULT_QUAL_FLAGS
1649#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001650 qualify("signal=all");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001651 while ((c = getopt(argc, argv,
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001652 "+b:cCdfFhiqrtTvVxyz"
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001653 "D"
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001654 "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001655 switch (c) {
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001656 case 'b':
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001657 if (strcmp(optarg, "execve") != 0)
1658 error_msg_and_die("Syscall '%s' for -b isn't supported",
1659 optarg);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001660 detach_on_execve = 1;
1661 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001662 case 'c':
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001663 if (cflag == CFLAG_BOTH) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001664 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001665 }
1666 cflag = CFLAG_ONLY_STATS;
1667 break;
1668 case 'C':
1669 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001670 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001671 }
1672 cflag = CFLAG_BOTH;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001673 break;
1674 case 'd':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001675 debug_flag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001676 break;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001677 case 'D':
1678 daemonized_tracer = 1;
1679 break;
Roland McGrath41c48222008-07-18 00:25:10 +00001680 case 'F':
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001681 optF = 1;
1682 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001683 case 'f':
1684 followfork++;
1685 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001686 case 'h':
1687 usage(stdout, 0);
1688 break;
1689 case 'i':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001690 iflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001691 break;
1692 case 'q':
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01001693 qflag++;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001694 break;
1695 case 'r':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001696 rflag = 1;
1697 /* fall through to tflag++ */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001698 case 't':
1699 tflag++;
1700 break;
1701 case 'T':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001702 Tflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001703 break;
1704 case 'x':
1705 xflag++;
1706 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001707 case 'y':
1708 show_fd_path = 1;
1709 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001710 case 'v':
1711 qualify("abbrev=none");
1712 break;
1713 case 'V':
Roland McGrath9c9a2532003-02-20 02:56:29 +00001714 printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001715 exit(0);
1716 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001717 case 'z':
1718 not_failing_only = 1;
1719 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001720 case 'a':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001721 acolumn = string_to_uint(optarg);
Denys Vlasenko102ec492011-08-25 01:27:59 +02001722 if (acolumn < 0)
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001723 error_opt_arg(c, optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001724 break;
1725 case 'e':
1726 qualify(optarg);
1727 break;
1728 case 'o':
1729 outfname = strdup(optarg);
1730 break;
1731 case 'O':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001732 i = string_to_uint(optarg);
1733 if (i < 0)
1734 error_opt_arg(c, optarg);
1735 set_overhead(i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001736 break;
1737 case 'p':
Denys Vlasenkoe8172b72012-03-09 13:01:04 +01001738 process_opt_p_list(optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001739 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001740 case 'P':
Denys Vlasenko7239dbc2013-03-05 15:46:34 +01001741 pathtrace_select(optarg);
Grant Edwards8a082772011-04-07 20:25:40 +00001742 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001743 case 's':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001744 i = string_to_uint(optarg);
1745 if (i < 0)
1746 error_opt_arg(c, optarg);
1747 max_strlen = i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001748 break;
1749 case 'S':
1750 set_sortby(optarg);
1751 break;
1752 case 'u':
1753 username = strdup(optarg);
1754 break;
Roland McGrathde6e5332003-01-24 04:31:23 +00001755 case 'E':
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001756 if (putenv(optarg) < 0)
1757 die_out_of_memory();
Roland McGrathde6e5332003-01-24 04:31:23 +00001758 break;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001759 case 'I':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001760 opt_intr = string_to_uint(optarg);
1761 if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS)
1762 error_opt_arg(c, optarg);
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001763 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001764 default:
1765 usage(stderr, 1);
1766 break;
1767 }
1768 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001769 argv += optind;
1770 /* argc -= optind; - no need, argc is not used below */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001771
Denys Vlasenko102ec492011-08-25 01:27:59 +02001772 acolumn_spaces = malloc(acolumn + 1);
1773 if (!acolumn_spaces)
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001774 die_out_of_memory();
Denys Vlasenko102ec492011-08-25 01:27:59 +02001775 memset(acolumn_spaces, ' ', acolumn);
1776 acolumn_spaces[acolumn] = '\0';
1777
Denys Vlasenko837399a2012-01-24 11:37:03 +01001778 /* Must have PROG [ARGS], or -p PID. Not both. */
Denys Vlasenkofd883382012-03-09 13:03:41 +01001779 if (!argv[0] == !nprocs)
Roland McGrathce0d1542003-11-11 21:24:23 +00001780 usage(stderr, 1);
1781
Denys Vlasenkofd883382012-03-09 13:03:41 +01001782 if (nprocs != 0 && daemonized_tracer) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001783 error_msg_and_die("-D and -p are mutually exclusive");
Wang Chaod322a4b2010-08-05 14:30:11 +08001784 }
1785
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001786 if (!followfork)
1787 followfork = optF;
1788
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001789 if (followfork >= 2 && cflag) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001790 error_msg_and_die("(-c or -C) and -ff are mutually exclusive");
Roland McGrathcb9def62006-04-25 07:48:03 +00001791 }
1792
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001793 /* See if they want to run as another user. */
1794 if (username != NULL) {
1795 struct passwd *pent;
1796
1797 if (getuid() != 0 || geteuid() != 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001798 error_msg_and_die("You must be root to use the -u option");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001799 }
Denys Vlasenko5d645812011-08-20 12:48:18 +02001800 pent = getpwnam(username);
1801 if (pent == NULL) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001802 error_msg_and_die("Cannot find user '%s'", username);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001803 }
1804 run_uid = pent->pw_uid;
1805 run_gid = pent->pw_gid;
1806 }
1807 else {
1808 run_uid = getuid();
1809 run_gid = getgid();
1810 }
1811
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001812 /*
1813 * On any reasonably recent Linux kernel (circa about 2.5.46)
1814 * need_fork_exec_workarounds should stay 0 after these tests:
1815 */
1816 /*need_fork_exec_workarounds = 0; - already is */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001817 if (followfork)
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001818 need_fork_exec_workarounds = test_ptrace_setoptions_followfork();
1819 need_fork_exec_workarounds |= test_ptrace_setoptions_for_all();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001820 test_ptrace_seize();
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001821
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001822 /* Check if they want to redirect the output. */
1823 if (outfname) {
Roland McGrath37b9a662003-11-07 02:26:54 +00001824 /* See if they want to pipe the output. */
1825 if (outfname[0] == '|' || outfname[0] == '!') {
1826 /*
1827 * We can't do the <outfname>.PID funny business
1828 * when using popen, so prohibit it.
1829 */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001830 if (followfork >= 2)
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001831 error_msg_and_die("Piping the output and -ff are mutually exclusive");
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001832 shared_log = strace_popen(outfname + 1);
Roland McGrath37b9a662003-11-07 02:26:54 +00001833 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001834 else if (followfork < 2)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001835 shared_log = strace_fopen(outfname);
Denys Vlasenko328bf252012-03-12 23:34:13 +01001836 } else {
1837 /* -ff without -o FILE is the same as single -f */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001838 if (followfork >= 2)
Denys Vlasenko328bf252012-03-12 23:34:13 +01001839 followfork = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001840 }
1841
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001842 if (!outfname || outfname[0] == '|' || outfname[0] == '!') {
Denys Vlasenkoa677da52012-01-24 11:31:51 +01001843 char *buf = malloc(BUFSIZ);
1844 if (!buf)
1845 die_out_of_memory();
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001846 setvbuf(shared_log, buf, _IOLBF, BUFSIZ);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001847 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001848 if (outfname && argv[0]) {
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001849 if (!opt_intr)
1850 opt_intr = INTR_NEVER;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001851 qflag = 1;
Roland McGrath36931052003-06-03 01:35:20 +00001852 }
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001853 if (!opt_intr)
1854 opt_intr = INTR_WHILE_WAIT;
Wang Chaob13c0de2010-11-12 17:25:19 +08001855
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001856 /* argv[0] -pPID -oFILE Default interactive setting
1857 * yes 0 0 INTR_WHILE_WAIT
1858 * no 1 0 INTR_WHILE_WAIT
1859 * yes 0 1 INTR_NEVER
1860 * no 1 1 INTR_WHILE_WAIT
Roland McGrath54cc1c82007-11-03 23:34:11 +00001861 */
1862
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001863 sigemptyset(&empty_set);
1864 sigemptyset(&blocked_set);
1865
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001866 /* startup_child() must be called before the signal handlers get
1867 * installed below as they are inherited into the spawned process.
1868 * Also we do not need to be protected by them as during interruption
1869 * in the startup_child() mode we kill the spawned process anyway.
1870 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001871 if (argv[0]) {
Dmitry V. Levin1d2435b2013-05-14 22:35:46 +00001872 if (!NOMMU_SYSTEM || daemonized_tracer)
1873 hide_log_until_execve = 1;
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001874 skip_one_b_execve = 1;
Denys Vlasenko837399a2012-01-24 11:37:03 +01001875 startup_child(argv);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001876 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001877
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001878 sa.sa_handler = SIG_IGN;
1879 sigemptyset(&sa.sa_mask);
1880 sa.sa_flags = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001881 sigaction(SIGTTOU, &sa, NULL); /* SIG_IGN */
1882 sigaction(SIGTTIN, &sa, NULL); /* SIG_IGN */
1883 if (opt_intr != INTR_ANYWHERE) {
1884 if (opt_intr == INTR_BLOCK_TSTP_TOO)
1885 sigaction(SIGTSTP, &sa, NULL); /* SIG_IGN */
1886 /*
1887 * In interactive mode (if no -o OUTFILE, or -p PID is used),
1888 * fatal signals are blocked while syscall stop is processed,
1889 * and acted on in between, when waiting for new syscall stops.
1890 * In non-interactive mode, signals are ignored.
1891 */
1892 if (opt_intr == INTR_WHILE_WAIT) {
1893 sigaddset(&blocked_set, SIGHUP);
1894 sigaddset(&blocked_set, SIGINT);
1895 sigaddset(&blocked_set, SIGQUIT);
1896 sigaddset(&blocked_set, SIGPIPE);
1897 sigaddset(&blocked_set, SIGTERM);
1898 sa.sa_handler = interrupt;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001899 }
1900 /* SIG_IGN, or set handler for these */
1901 sigaction(SIGHUP, &sa, NULL);
1902 sigaction(SIGINT, &sa, NULL);
1903 sigaction(SIGQUIT, &sa, NULL);
1904 sigaction(SIGPIPE, &sa, NULL);
1905 sigaction(SIGTERM, &sa, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001906 }
Denys Vlasenkofd883382012-03-09 13:03:41 +01001907 if (nprocs != 0 || daemonized_tracer)
Roland McGrath02203312007-06-11 22:06:31 +00001908 startup_attach();
Roland McGrath02203312007-06-11 22:06:31 +00001909
Denys Vlasenkofd883382012-03-09 13:03:41 +01001910 /* Do we want pids printed in our -o OUTFILE?
1911 * -ff: no (every pid has its own file); or
1912 * -f: yes (there can be more pids in the future); or
1913 * -p PID1,PID2: yes (there are already more than one pid)
1914 */
1915 print_pid_pfx = (outfname && followfork < 2 && (followfork == 1 || nprocs > 1));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001916}
1917
Denys Vlasenkoeebb04d2012-01-27 15:24:48 +01001918static struct tcb *
Roland McGrath54e931f2010-09-14 18:59:20 -07001919pid2tcb(int pid)
1920{
1921 int i;
1922
1923 if (pid <= 0)
1924 return NULL;
1925
1926 for (i = 0; i < tcbtabsize; i++) {
1927 struct tcb *tcp = tcbtab[i];
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001928 if (tcp->pid == pid)
Roland McGrath54e931f2010-09-14 18:59:20 -07001929 return tcp;
1930 }
1931
1932 return NULL;
1933}
1934
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001935static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001936cleanup(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001937{
1938 int i;
1939 struct tcb *tcp;
Denys Vlasenko35218842012-01-29 21:17:56 +01001940 int fatal_sig;
1941
1942 /* 'interrupted' is a volatile object, fetch it only once */
1943 fatal_sig = interrupted;
1944 if (!fatal_sig)
1945 fatal_sig = SIGTERM;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001946
Roland McGrathee9d4352002-12-18 04:16:10 +00001947 for (i = 0; i < tcbtabsize; i++) {
1948 tcp = tcbtab[i];
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001949 if (!tcp->pid)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001950 continue;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001951 if (debug_flag)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001952 fprintf(stderr,
1953 "cleanup: looking at pid %u\n", tcp->pid);
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001954 if (tcp->pid == strace_child) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001955 kill(tcp->pid, SIGCONT);
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001956 kill(tcp->pid, fatal_sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001957 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001958 detach(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001959 }
1960 if (cflag)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001961 call_summary(shared_log);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001962}
1963
1964static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001965interrupt(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001966{
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001967 interrupted = sig;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001968}
1969
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02001970static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001971trace(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001972{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001973 struct rusage ru;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001974
Denys Vlasenkod2e1f422013-07-08 11:28:27 +02001975 /* Used to be "while (nprocs != 0)", but in this testcase:
1976 * int main() { _exit(!!fork()); }
1977 * under strace -f, parent sometimes (rarely) manages
1978 * to exit before we see the first stop of the child,
1979 * and we are losing track of it:
1980 * 19923 clone(...) = 19924
1981 * 19923 exit_group(1) = ?
1982 * 19923 +++ exited with 1 +++
1983 * Waiting for ECHILD works better.
Denys Vlasenko7c41ce22013-07-08 13:55:04 +02001984 * (However, if -o|logger is in use, we can't do that.
1985 * Can work around that by double-forking the logger,
1986 * but that loses the ability to wait for its completion on exit.
1987 * Oh well...)
Denys Vlasenkod2e1f422013-07-08 11:28:27 +02001988 */
1989 while (1) {
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001990 int pid;
1991 int wait_errno;
1992 int status, sig;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001993 int stopped;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001994 struct tcb *tcp;
1995 unsigned event;
1996
Denys Vlasenko222713a2009-03-17 14:29:59 +00001997 if (interrupted)
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02001998 return;
Denys Vlasenko725dd422013-06-20 11:06:58 +02001999
Denys Vlasenko7c41ce22013-07-08 13:55:04 +02002000 if (popen_pid != 0 && nprocs == 0)
2001 return;
2002
Roland McGratheb9e2e82009-06-02 16:49:22 -07002003 if (interactive)
2004 sigprocmask(SIG_SETMASK, &empty_set, NULL);
Denys Vlasenko725dd422013-06-20 11:06:58 +02002005 pid = wait4(-1, &status, __WALL, (cflag ? &ru : NULL));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002006 wait_errno = errno;
Roland McGratheb9e2e82009-06-02 16:49:22 -07002007 if (interactive)
2008 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002009
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02002010 if (pid < 0) {
Denys Vlasenko725dd422013-06-20 11:06:58 +02002011 if (wait_errno == EINTR)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002012 continue;
Denys Vlasenkod2e1f422013-07-08 11:28:27 +02002013 if (nprocs == 0 && wait_errno == ECHILD)
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002014 return;
Denys Vlasenkod2e1f422013-07-08 11:28:27 +02002015 /* If nprocs > 0, ECHILD is not expected,
2016 * treat it as any other error here:
2017 */
Denys Vlasenko725dd422013-06-20 11:06:58 +02002018 errno = wait_errno;
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002019 perror_msg_and_die("wait4(__WALL)");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002020 }
Denys Vlasenko725dd422013-06-20 11:06:58 +02002021
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00002022 if (pid == popen_pid) {
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002023 if (!WIFSTOPPED(status))
Denys Vlasenko7dd23382011-06-22 13:03:56 +02002024 popen_pid = 0;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00002025 continue;
2026 }
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002027
2028 event = ((unsigned)status >> 16);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002029 if (debug_flag) {
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002030 char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko1b2bfbc2013-06-21 16:41:50 +02002031 char evbuf[sizeof(",EVENT_VFORK_DONE (%u)") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002032 strcpy(buf, "???");
2033 if (WIFSIGNALED(status))
2034#ifdef WCOREDUMP
2035 sprintf(buf, "WIFSIGNALED,%ssig=%s",
2036 WCOREDUMP(status) ? "core," : "",
2037 signame(WTERMSIG(status)));
2038#else
2039 sprintf(buf, "WIFSIGNALED,sig=%s",
2040 signame(WTERMSIG(status)));
2041#endif
2042 if (WIFEXITED(status))
2043 sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status));
2044 if (WIFSTOPPED(status))
2045 sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status)));
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02002046#ifdef WIFCONTINUED
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002047 /* Should never be seen */
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002048 if (WIFCONTINUED(status))
2049 strcpy(buf, "WIFCONTINUED");
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02002050#endif
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002051 evbuf[0] = '\0';
2052 if (event != 0) {
2053 static const char *const event_names[] = {
2054 [PTRACE_EVENT_CLONE] = "CLONE",
2055 [PTRACE_EVENT_FORK] = "FORK",
2056 [PTRACE_EVENT_VFORK] = "VFORK",
2057 [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE",
2058 [PTRACE_EVENT_EXEC] = "EXEC",
2059 [PTRACE_EVENT_EXIT] = "EXIT",
Denys Vlasenko4e020c02013-06-21 16:33:56 +02002060 /* [PTRACE_EVENT_STOP (=128)] would make biggish array */
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002061 };
Denys Vlasenko1b2bfbc2013-06-21 16:41:50 +02002062 const char *e = "??";
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002063 if (event < ARRAY_SIZE(event_names))
2064 e = event_names[event];
Denys Vlasenko4e020c02013-06-21 16:33:56 +02002065 else if (event == PTRACE_EVENT_STOP)
2066 e = "STOP";
Denys Vlasenko1b2bfbc2013-06-21 16:41:50 +02002067 sprintf(evbuf, ",EVENT_%s (%u)", e, event);
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002068 }
Denys Vlasenko38eab5d2013-07-02 12:18:22 +02002069 fprintf(stderr, " [wait(0x%06x) = %u] %s%s\n", status, pid, buf, evbuf);
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002070 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002071
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002072 /* Look up 'pid' in our table. */
Denys Vlasenko5d645812011-08-20 12:48:18 +02002073 tcp = pid2tcb(pid);
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002074
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002075 if (!tcp) {
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002076 if (!WIFSTOPPED(status)) {
2077 /* This can happen if we inherited
2078 * an unknown child. Example:
2079 * (sleep 1 & exec strace sleep 2)
2080 */
2081 error_msg("Exit of unknown pid %u seen", pid);
2082 continue;
2083 }
Roland McGrath41c48222008-07-18 00:25:10 +00002084 if (followfork) {
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002085 /* We assume it's a fork/vfork/clone child */
Denys Vlasenko418d66a2009-01-17 01:52:54 +00002086 tcp = alloctcb(pid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002087 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01002088 newoutf(tcp);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002089 if (!qflag)
Denys Vlasenko833fb132011-08-17 11:30:56 +02002090 fprintf(stderr, "Process %d attached\n",
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002091 pid);
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002092 } else {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002093 /* This can happen if a clone call used
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002094 * CLONE_PTRACE itself.
Denys Vlasenko71d3d292013-06-21 16:19:46 +02002095 */
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002096 ptrace(PTRACE_CONT, pid, (char *) 0, 0);
2097 error_msg("Stop of unknown pid %u seen, PTRACE_CONTed it", pid);
Denys Vlasenko71d3d292013-06-21 16:19:46 +02002098 continue;
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002099 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002100 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002101
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002102 clear_regs();
2103 if (WIFSTOPPED(status))
2104 get_regs(pid);
2105
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002106 /* Under Linux, execve changes pid to thread leader's pid,
2107 * and we see this changed pid on EVENT_EXEC and later,
2108 * execve sysexit. Leader "disappears" without exit
2109 * notification. Let user know that, drop leader's tcb,
2110 * and fix up pid in execve thread's tcb.
2111 * Effectively, execve thread's tcb replaces leader's tcb.
2112 *
2113 * BTW, leader is 'stuck undead' (doesn't report WIFEXITED
2114 * on exit syscall) in multithreaded programs exactly
2115 * in order to handle this case.
2116 *
2117 * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0.
2118 * On 2.6 and earlier, it can return garbage.
2119 */
2120 if (event == PTRACE_EVENT_EXEC && os_release >= KERNEL_VERSION(3,0,0)) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002121 FILE *fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002122 struct tcb *execve_thread;
2123 long old_pid = 0;
2124
2125 if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) < 0)
2126 goto dont_switch_tcbs;
Denys Vlasenko6162a3f2013-07-04 09:26:24 +02002127 /* Avoid truncation in pid2tcb() param passing */
2128 if (old_pid > UINT_MAX)
2129 goto dont_switch_tcbs;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002130 if (old_pid <= 0 || old_pid == pid)
2131 goto dont_switch_tcbs;
2132 execve_thread = pid2tcb(old_pid);
2133 /* It should be !NULL, but I feel paranoid */
2134 if (!execve_thread)
2135 goto dont_switch_tcbs;
2136
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002137 if (execve_thread->curcol != 0) {
2138 /*
2139 * One case we are here is -ff:
2140 * try "strace -oLOG -ff test/threaded_execve"
2141 */
2142 fprintf(execve_thread->outf, " <pid changed to %d ...>\n", pid);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002143 /*execve_thread->curcol = 0; - no need, see code below */
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002144 }
2145 /* Swap output FILEs (needed for -ff) */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002146 fp = execve_thread->outf;
2147 execve_thread->outf = tcp->outf;
2148 tcp->outf = fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002149 /* And their column positions */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002150 execve_thread->curcol = tcp->curcol;
2151 tcp->curcol = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002152 /* Drop leader, but close execve'd thread outfile (if -ff) */
2153 droptcb(tcp);
2154 /* Switch to the thread, reusing leader's outfile and pid */
2155 tcp = execve_thread;
2156 tcp->pid = pid;
2157 if (cflag != CFLAG_ONLY_STATS) {
2158 printleader(tcp);
2159 tprintf("+++ superseded by execve in pid %lu +++\n", old_pid);
2160 line_ended();
2161 tcp->flags |= TCB_REPRINT;
2162 }
2163 }
2164 dont_switch_tcbs:
2165
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002166 if (event == PTRACE_EVENT_EXEC) {
2167 if (detach_on_execve && !skip_one_b_execve)
2168 detach(tcp); /* do "-b execve" thingy */
2169 skip_one_b_execve = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002170 }
2171
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002172 /* Set current output file */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002173 current_tcp = tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002174
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002175 if (cflag) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002176 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2177 tcp->stime = ru.ru_stime;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002178 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002179
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002180 if (WIFSIGNALED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002181 if (pid == strace_child)
2182 exit_code = 0x100 | WTERMSIG(status);
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002183 if (cflag != CFLAG_ONLY_STATS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01002184 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)
2185 ) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002186 printleader(tcp);
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002187#ifdef WCOREDUMP
Denys Vlasenko000b6012012-01-28 01:25:03 +01002188 tprintf("+++ killed by %s %s+++\n",
Roland McGrath2efe8792004-01-13 09:59:45 +00002189 signame(WTERMSIG(status)),
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002190 WCOREDUMP(status) ? "(core dumped) " : "");
2191#else
Denys Vlasenko000b6012012-01-28 01:25:03 +01002192 tprintf("+++ killed by %s +++\n",
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002193 signame(WTERMSIG(status)));
Roland McGrath2efe8792004-01-13 09:59:45 +00002194#endif
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002195 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002196 }
2197 droptcb(tcp);
2198 continue;
2199 }
2200 if (WIFEXITED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002201 if (pid == strace_child)
2202 exit_code = WEXITSTATUS(status);
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01002203 if (cflag != CFLAG_ONLY_STATS &&
2204 qflag < 2) {
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002205 printleader(tcp);
Denys Vlasenko000b6012012-01-28 01:25:03 +01002206 tprintf("+++ exited with %d +++\n", WEXITSTATUS(status));
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002207 line_ended();
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002208 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002209 droptcb(tcp);
2210 continue;
2211 }
2212 if (!WIFSTOPPED(status)) {
2213 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
2214 droptcb(tcp);
2215 continue;
2216 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002217
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002218 /* Is this the very first time we see this tracee stopped? */
2219 if (tcp->flags & TCB_STARTUP) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002220 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002221 fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n", tcp->pid);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002222 tcp->flags &= ~TCB_STARTUP;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002223 if (tcp->flags & TCB_BPTSET) {
Roland McGrath02203312007-06-11 22:06:31 +00002224 /*
2225 * One example is a breakpoint inherited from
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002226 * parent through fork().
Roland McGrath02203312007-06-11 22:06:31 +00002227 */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002228 if (clearbpt(tcp) < 0) {
2229 /* Pretty fatal */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002230 droptcb(tcp);
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002231 exit_code = 1;
2232 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002233 }
2234 }
Denys Vlasenkoc169d942013-07-10 14:33:05 +02002235 if (!use_seize && ptrace_setoptions) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002236 if (debug_flag)
Denys Vlasenkoc169d942013-07-10 14:33:05 +02002237 fprintf(stderr, "setting opts 0x%x on pid %d\n", ptrace_setoptions, tcp->pid);
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002238 if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) {
2239 if (errno != ESRCH) {
2240 /* Should never happen, really */
2241 perror_msg_and_die("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02002242 }
2243 }
2244 }
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002245 }
2246
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002247 sig = WSTOPSIG(status);
2248
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002249 if (event != 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002250 /* Ptrace event */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002251#if USE_SEIZE
Denys Vlasenko26bc0602012-07-10 16:36:32 +02002252 if (event == PTRACE_EVENT_STOP) {
Denys Vlasenko67038162012-01-29 16:46:46 +01002253 /*
2254 * PTRACE_INTERRUPT-stop or group-stop.
2255 * PTRACE_INTERRUPT-stop has sig == SIGTRAP here.
2256 */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002257 if (sig == SIGSTOP
2258 || sig == SIGTSTP
2259 || sig == SIGTTIN
2260 || sig == SIGTTOU
2261 ) {
2262 stopped = 1;
2263 goto show_stopsig;
2264 }
2265 }
2266#endif
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002267 goto restart_tracee_with_sig_0;
2268 }
2269
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002270 /* Is this post-attach SIGSTOP?
2271 * Interestingly, the process may stop
2272 * with STOPSIG equal to some other signal
2273 * than SIGSTOP if we happend to attach
2274 * just before the process takes a signal.
2275 */
2276 if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002277 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002278 fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid);
2279 tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002280 goto restart_tracee_with_sig_0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002281 }
2282
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002283 if (sig != syscall_trap_sig) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002284 siginfo_t si;
2285
2286 /* Nonzero (true) if tracee is stopped by signal
2287 * (as opposed to "tracee received signal").
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002288 * TODO: shouldn't we check for errno == EINVAL too?
2289 * We can get ESRCH instead, you know...
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002290 */
2291 stopped = (ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002292#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002293 show_stopsig:
Denys Vlasenko67038162012-01-29 16:46:46 +01002294#endif
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002295 if (cflag != CFLAG_ONLY_STATS
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002296 && !hide_log_until_execve
2297 && (qual_flags[sig] & QUAL_SIGNAL)
2298 ) {
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002299 printleader(tcp);
2300 if (!stopped) {
2301 tprintf("--- %s ", signame(sig));
2302 printsiginfo(&si, verbose(tcp));
Denys Vlasenko5a2483b2013-07-01 12:49:14 +02002303 tprints(" ---\n");
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002304 } else
Denys Vlasenko5a2483b2013-07-01 12:49:14 +02002305 tprintf("--- stopped by %s ---\n",
2306 signame(sig));
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002307 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002308 }
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002309
2310 if (!stopped)
2311 /* It's signal-delivery-stop. Inject the signal */
2312 goto restart_tracee;
2313
2314 /* It's group-stop */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002315 if (use_seize) {
2316 /*
2317 * This ends ptrace-stop, but does *not* end group-stop.
2318 * This makes stopping signals work properly on straced process
2319 * (that is, process really stops. It used to continue to run).
2320 */
2321 if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) {
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002322 /* Note: ptrace_restart emitted error message */
2323 exit_code = 1;
2324 return;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002325 }
2326 continue;
2327 }
2328 /* We don't have PTRACE_LISTEN support... */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002329 goto restart_tracee;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002330 }
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002331
2332 /* We handled quick cases, we are permitted to interrupt now. */
Roland McGrath02203312007-06-11 22:06:31 +00002333 if (interrupted)
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002334 return;
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002335
2336 /* This should be syscall entry or exit.
2337 * (Or it still can be that pesky post-execve SIGTRAP!)
2338 * Handle it.
2339 */
Denys Vlasenko23506752012-03-21 10:32:49 +01002340 if (trace_syscall(tcp) < 0) {
2341 /* ptrace() failed in trace_syscall().
Roland McGratheb9e2e82009-06-02 16:49:22 -07002342 * Likely a result of process disappearing mid-flight.
Denys Vlasenko23506752012-03-21 10:32:49 +01002343 * Observed case: exit_group() or SIGKILL terminating
Denys Vlasenkof1e69032012-01-04 15:15:26 +01002344 * all processes in thread group.
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002345 * We assume that ptrace error was caused by process death.
Denys Vlasenkof2025022012-03-09 15:29:45 +01002346 * We used to detach(tcp) here, but since we no longer
2347 * implement "detach before death" policy/hack,
2348 * we can let this process to report its death to us
2349 * normally, via WIFEXITED or WIFSIGNALED wait status.
2350 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002351 continue;
2352 }
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002353 restart_tracee_with_sig_0:
2354 sig = 0;
2355 restart_tracee:
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002356 if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) {
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002357 /* Note: ptrace_restart emitted error message */
2358 exit_code = 1;
2359 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002360 }
Denys Vlasenko7c41ce22013-07-08 13:55:04 +02002361 } /* while (1) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002362}
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002363
2364int
2365main(int argc, char *argv[])
2366{
2367 init(argc, argv);
2368
2369 /* Run main tracing loop */
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002370 trace();
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002371
2372 cleanup();
2373 fflush(NULL);
Dmitry V. Levincf534362012-07-12 20:54:46 +00002374 if (shared_log != stderr)
2375 fclose(shared_log);
2376 if (popen_pid) {
2377 while (waitpid(popen_pid, NULL, 0) < 0 && errno == EINTR)
2378 ;
2379 }
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002380 if (exit_code > 0xff) {
2381 /* Avoid potential core file clobbering. */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00002382 struct_rlimit rlim = {0, 0};
2383 set_rlimit(RLIMIT_CORE, &rlim);
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002384
2385 /* Child was killed by a signal, mimic that. */
2386 exit_code &= 0xff;
2387 signal(exit_code, SIG_DFL);
2388 raise(exit_code);
2389 /* Paranoia - what if this signal is not fatal?
2390 Exit with 128 + signo then. */
2391 exit_code += 128;
2392 }
2393
2394 return exit_code;
2395}