blob: 8a49340f76ddf4065aeb1925e8e54e385e2bb906 [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:
Denys Vlasenko329fa392014-04-10 09:57:17 +02001418
1419# compile with: gcc -nostartfiles -nostdlib -o int3 int3.S
1420_start: .globl _start
1421 int3
1422 movl $42, %ebx
1423 movl $1, %eax
1424 int $0x80
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001425 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001426static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001427test_ptrace_setoptions_for_all(void)
1428{
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001429 const unsigned int test_options = PTRACE_O_TRACESYSGOOD |
1430 PTRACE_O_TRACEEXEC;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001431 int pid;
1432 int it_worked = 0;
1433
Denys Vlasenko05f32512013-02-26 12:00:34 +01001434 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001435 if (NOMMU_SYSTEM)
Denys Vlasenko05f32512013-02-26 12:00:34 +01001436 goto worked; /* be bold, and pretend that test succeeded */
Mike Frysinger7ff5ed92012-04-05 01:52:25 -04001437
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001438 pid = fork();
1439 if (pid < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001440 perror_msg_and_die("fork");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001441
1442 if (pid == 0) {
1443 pid = getpid();
1444 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001445 /* Note: exits with exitcode 1 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001446 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1447 __func__);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001448 kill(pid, SIGSTOP);
1449 _exit(0); /* parent should see entry into this syscall */
1450 }
1451
1452 while (1) {
1453 int status, tracee_pid;
1454
1455 errno = 0;
1456 tracee_pid = wait(&status);
1457 if (tracee_pid <= 0) {
1458 if (errno == EINTR)
1459 continue;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001460 kill_save_errno(pid, SIGKILL);
1461 perror_msg_and_die("%s: unexpected wait result %d",
1462 __func__, tracee_pid);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001463 }
Denys Vlasenko75422762011-05-27 14:36:01 +02001464 if (WIFEXITED(status)) {
1465 if (WEXITSTATUS(status) == 0)
1466 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001467 error_msg_and_die("%s: unexpected exit status %u",
1468 __func__, WEXITSTATUS(status));
1469 }
1470 if (WIFSIGNALED(status)) {
1471 error_msg_and_die("%s: unexpected signal %u",
1472 __func__, WTERMSIG(status));
Denys Vlasenko75422762011-05-27 14:36:01 +02001473 }
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001474 if (!WIFSTOPPED(status)) {
1475 kill(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001476 error_msg_and_die("%s: unexpected wait status %x",
1477 __func__, status);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001478 }
1479 if (WSTOPSIG(status) == SIGSTOP) {
1480 /*
1481 * We don't check "options aren't accepted" error.
1482 * If it happens, we'll never get (SIGTRAP | 0x80),
1483 * and thus will decide to not use the option.
1484 * IOW: the outcome of the test will be correct.
1485 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001486 if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0
1487 && errno != EINVAL && errno != EIO)
1488 perror_msg("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001489 }
1490 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
1491 it_worked = 1;
1492 }
1493 if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001494 kill_save_errno(pid, SIGKILL);
Denys Vlasenko75422762011-05-27 14:36:01 +02001495 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001496 }
1497 }
1498
1499 if (it_worked) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001500 worked:
Denys Vlasenko75422762011-05-27 14:36:01 +02001501 syscall_trap_sig = (SIGTRAP | 0x80);
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001502 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001503 if (debug_flag)
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001504 fprintf(stderr, "ptrace_setoptions = %#x\n",
1505 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001506 return 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001507 }
1508
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001509 error_msg("Test for PTRACE_O_TRACESYSGOOD failed, "
1510 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001511 return 1;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001512}
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001513
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001514#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001515static void
1516test_ptrace_seize(void)
1517{
1518 int pid;
1519
Denys Vlasenko05f32512013-02-26 12:00:34 +01001520 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001521 if (NOMMU_SYSTEM) {
1522 post_attach_sigstop = 0; /* this sets use_seize to 1 */
1523 return;
1524 }
1525
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001526 pid = fork();
1527 if (pid < 0)
1528 perror_msg_and_die("fork");
1529
1530 if (pid == 0) {
1531 pause();
1532 _exit(0);
1533 }
1534
1535 /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After
1536 * attaching tracee continues to run unless a trap condition occurs.
1537 * PTRACE_SEIZE doesn't affect signal or group stop state.
1538 */
Denys Vlasenko26bc0602012-07-10 16:36:32 +02001539 if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001540 post_attach_sigstop = 0; /* this sets use_seize to 1 */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001541 } else if (debug_flag) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001542 fprintf(stderr, "PTRACE_SEIZE doesn't work\n");
1543 }
1544
1545 kill(pid, SIGKILL);
1546
1547 while (1) {
1548 int status, tracee_pid;
1549
1550 errno = 0;
1551 tracee_pid = waitpid(pid, &status, 0);
1552 if (tracee_pid <= 0) {
1553 if (errno == EINTR)
1554 continue;
1555 perror_msg_and_die("%s: unexpected wait result %d",
1556 __func__, tracee_pid);
1557 }
1558 if (WIFSIGNALED(status)) {
1559 return;
1560 }
1561 error_msg_and_die("%s: unexpected wait status %x",
1562 __func__, status);
1563 }
1564}
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001565#else /* !USE_SEIZE */
1566# define test_ptrace_seize() ((void)0)
1567#endif
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001568
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001569static unsigned
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001570get_os_release(void)
1571{
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001572 unsigned rel;
1573 const char *p;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001574 struct utsname u;
1575 if (uname(&u) < 0)
1576 perror_msg_and_die("uname");
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001577 /* u.release has this form: "3.2.9[-some-garbage]" */
1578 rel = 0;
1579 p = u.release;
1580 for (;;) {
1581 if (!(*p >= '0' && *p <= '9'))
1582 error_msg_and_die("Bad OS release string: '%s'", u.release);
1583 /* Note: this open-codes KERNEL_VERSION(): */
1584 rel = (rel << 8) | atoi(p);
1585 if (rel >= KERNEL_VERSION(1,0,0))
1586 break;
1587 while (*p >= '0' && *p <= '9')
1588 p++;
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001589 if (*p != '.') {
1590 if (rel >= KERNEL_VERSION(0,1,0)) {
1591 /* "X.Y-something" means "X.Y.0" */
1592 rel <<= 8;
1593 break;
1594 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001595 error_msg_and_die("Bad OS release string: '%s'", u.release);
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001596 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001597 p++;
1598 }
1599 return rel;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001600}
1601
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01001602/*
1603 * Initialization part of main() was eating much stack (~0.5k),
1604 * which was unused after init.
1605 * We can reuse it if we move init code into a separate function.
1606 *
1607 * Don't want main() to inline us and defeat the reason
1608 * we have a separate function.
1609 */
1610static void __attribute__ ((noinline))
1611init(int argc, char *argv[])
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001612{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001613 struct tcb *tcp;
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001614 int c, i;
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001615 int optF = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001616 struct sigaction sa;
1617
Dmitry V. Levin08b623e2007-10-08 21:04:41 +00001618 progname = argv[0] ? argv[0] : "strace";
1619
Denys Vlasenkoa5090542012-03-15 17:27:49 +01001620 /* Make sure SIGCHLD has the default action so that waitpid
1621 definitely works without losing track of children. The user
1622 should not have given us a bogus state to inherit, but he might
1623 have. Arguably we should detect SIG_IGN here and pass it on
1624 to children, but probably noone really needs that. */
1625 signal(SIGCHLD, SIG_DFL);
1626
Denys Vlasenko75422762011-05-27 14:36:01 +02001627 strace_tracer_pid = getpid();
1628
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001629 os_release = get_os_release();
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001630
Roland McGrathee9d4352002-12-18 04:16:10 +00001631 /* Allocate the initial tcbtab. */
1632 tcbtabsize = argc; /* Surely enough for all -p args. */
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001633 tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0]));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001634 if (!tcbtab)
1635 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001636 tcp = calloc(tcbtabsize, sizeof(*tcp));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001637 if (!tcp)
1638 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001639 for (c = 0; c < tcbtabsize; c++)
1640 tcbtab[c] = tcp++;
Roland McGrathee9d4352002-12-18 04:16:10 +00001641
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001642 shared_log = stderr;
Roland McGrath138c6a32006-01-12 09:50:49 +00001643 set_sortby(DEFAULT_SORTBY);
1644 set_personality(DEFAULT_PERSONALITY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001645 qualify("trace=all");
1646 qualify("abbrev=all");
1647 qualify("verbose=all");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001648#if DEFAULT_QUAL_FLAGS != (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
1649# error Bug in DEFAULT_QUAL_FLAGS
1650#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001651 qualify("signal=all");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001652 while ((c = getopt(argc, argv,
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001653 "+b:cCdfFhiqrtTvVxyz"
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001654 "D"
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001655 "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001656 switch (c) {
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001657 case 'b':
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001658 if (strcmp(optarg, "execve") != 0)
1659 error_msg_and_die("Syscall '%s' for -b isn't supported",
1660 optarg);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001661 detach_on_execve = 1;
1662 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001663 case 'c':
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001664 if (cflag == CFLAG_BOTH) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001665 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001666 }
1667 cflag = CFLAG_ONLY_STATS;
1668 break;
1669 case 'C':
1670 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001671 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001672 }
1673 cflag = CFLAG_BOTH;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001674 break;
1675 case 'd':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001676 debug_flag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001677 break;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001678 case 'D':
1679 daemonized_tracer = 1;
1680 break;
Roland McGrath41c48222008-07-18 00:25:10 +00001681 case 'F':
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001682 optF = 1;
1683 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001684 case 'f':
1685 followfork++;
1686 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001687 case 'h':
1688 usage(stdout, 0);
1689 break;
1690 case 'i':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001691 iflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001692 break;
1693 case 'q':
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01001694 qflag++;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001695 break;
1696 case 'r':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001697 rflag = 1;
1698 /* fall through to tflag++ */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001699 case 't':
1700 tflag++;
1701 break;
1702 case 'T':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001703 Tflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001704 break;
1705 case 'x':
1706 xflag++;
1707 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001708 case 'y':
1709 show_fd_path = 1;
1710 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001711 case 'v':
1712 qualify("abbrev=none");
1713 break;
1714 case 'V':
Roland McGrath9c9a2532003-02-20 02:56:29 +00001715 printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001716 exit(0);
1717 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001718 case 'z':
1719 not_failing_only = 1;
1720 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001721 case 'a':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001722 acolumn = string_to_uint(optarg);
Denys Vlasenko102ec492011-08-25 01:27:59 +02001723 if (acolumn < 0)
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001724 error_opt_arg(c, optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001725 break;
1726 case 'e':
1727 qualify(optarg);
1728 break;
1729 case 'o':
1730 outfname = strdup(optarg);
1731 break;
1732 case 'O':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001733 i = string_to_uint(optarg);
1734 if (i < 0)
1735 error_opt_arg(c, optarg);
1736 set_overhead(i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001737 break;
1738 case 'p':
Denys Vlasenkoe8172b72012-03-09 13:01:04 +01001739 process_opt_p_list(optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001740 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001741 case 'P':
Denys Vlasenko7239dbc2013-03-05 15:46:34 +01001742 pathtrace_select(optarg);
Grant Edwards8a082772011-04-07 20:25:40 +00001743 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001744 case 's':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001745 i = string_to_uint(optarg);
1746 if (i < 0)
1747 error_opt_arg(c, optarg);
1748 max_strlen = i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001749 break;
1750 case 'S':
1751 set_sortby(optarg);
1752 break;
1753 case 'u':
1754 username = strdup(optarg);
1755 break;
Roland McGrathde6e5332003-01-24 04:31:23 +00001756 case 'E':
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001757 if (putenv(optarg) < 0)
1758 die_out_of_memory();
Roland McGrathde6e5332003-01-24 04:31:23 +00001759 break;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001760 case 'I':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001761 opt_intr = string_to_uint(optarg);
1762 if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS)
1763 error_opt_arg(c, optarg);
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001764 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001765 default:
1766 usage(stderr, 1);
1767 break;
1768 }
1769 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001770 argv += optind;
1771 /* argc -= optind; - no need, argc is not used below */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001772
Denys Vlasenko102ec492011-08-25 01:27:59 +02001773 acolumn_spaces = malloc(acolumn + 1);
1774 if (!acolumn_spaces)
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001775 die_out_of_memory();
Denys Vlasenko102ec492011-08-25 01:27:59 +02001776 memset(acolumn_spaces, ' ', acolumn);
1777 acolumn_spaces[acolumn] = '\0';
1778
Denys Vlasenko837399a2012-01-24 11:37:03 +01001779 /* Must have PROG [ARGS], or -p PID. Not both. */
Denys Vlasenkofd883382012-03-09 13:03:41 +01001780 if (!argv[0] == !nprocs)
Roland McGrathce0d1542003-11-11 21:24:23 +00001781 usage(stderr, 1);
1782
Denys Vlasenkofd883382012-03-09 13:03:41 +01001783 if (nprocs != 0 && daemonized_tracer) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001784 error_msg_and_die("-D and -p are mutually exclusive");
Wang Chaod322a4b2010-08-05 14:30:11 +08001785 }
1786
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001787 if (!followfork)
1788 followfork = optF;
1789
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001790 if (followfork >= 2 && cflag) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001791 error_msg_and_die("(-c or -C) and -ff are mutually exclusive");
Roland McGrathcb9def62006-04-25 07:48:03 +00001792 }
1793
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001794 /* See if they want to run as another user. */
1795 if (username != NULL) {
1796 struct passwd *pent;
1797
1798 if (getuid() != 0 || geteuid() != 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001799 error_msg_and_die("You must be root to use the -u option");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001800 }
Denys Vlasenko5d645812011-08-20 12:48:18 +02001801 pent = getpwnam(username);
1802 if (pent == NULL) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001803 error_msg_and_die("Cannot find user '%s'", username);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001804 }
1805 run_uid = pent->pw_uid;
1806 run_gid = pent->pw_gid;
1807 }
1808 else {
1809 run_uid = getuid();
1810 run_gid = getgid();
1811 }
1812
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001813 /*
1814 * On any reasonably recent Linux kernel (circa about 2.5.46)
1815 * need_fork_exec_workarounds should stay 0 after these tests:
1816 */
1817 /*need_fork_exec_workarounds = 0; - already is */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001818 if (followfork)
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001819 need_fork_exec_workarounds = test_ptrace_setoptions_followfork();
1820 need_fork_exec_workarounds |= test_ptrace_setoptions_for_all();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001821 test_ptrace_seize();
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001822
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001823 /* Check if they want to redirect the output. */
1824 if (outfname) {
Roland McGrath37b9a662003-11-07 02:26:54 +00001825 /* See if they want to pipe the output. */
1826 if (outfname[0] == '|' || outfname[0] == '!') {
1827 /*
1828 * We can't do the <outfname>.PID funny business
1829 * when using popen, so prohibit it.
1830 */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001831 if (followfork >= 2)
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001832 error_msg_and_die("Piping the output and -ff are mutually exclusive");
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001833 shared_log = strace_popen(outfname + 1);
Roland McGrath37b9a662003-11-07 02:26:54 +00001834 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001835 else if (followfork < 2)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001836 shared_log = strace_fopen(outfname);
Denys Vlasenko328bf252012-03-12 23:34:13 +01001837 } else {
1838 /* -ff without -o FILE is the same as single -f */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001839 if (followfork >= 2)
Denys Vlasenko328bf252012-03-12 23:34:13 +01001840 followfork = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001841 }
1842
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001843 if (!outfname || outfname[0] == '|' || outfname[0] == '!') {
Denys Vlasenkoa677da52012-01-24 11:31:51 +01001844 char *buf = malloc(BUFSIZ);
1845 if (!buf)
1846 die_out_of_memory();
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001847 setvbuf(shared_log, buf, _IOLBF, BUFSIZ);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001848 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001849 if (outfname && argv[0]) {
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001850 if (!opt_intr)
1851 opt_intr = INTR_NEVER;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001852 qflag = 1;
Roland McGrath36931052003-06-03 01:35:20 +00001853 }
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001854 if (!opt_intr)
1855 opt_intr = INTR_WHILE_WAIT;
Wang Chaob13c0de2010-11-12 17:25:19 +08001856
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001857 /* argv[0] -pPID -oFILE Default interactive setting
1858 * yes 0 0 INTR_WHILE_WAIT
1859 * no 1 0 INTR_WHILE_WAIT
1860 * yes 0 1 INTR_NEVER
1861 * no 1 1 INTR_WHILE_WAIT
Roland McGrath54cc1c82007-11-03 23:34:11 +00001862 */
1863
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001864 sigemptyset(&empty_set);
1865 sigemptyset(&blocked_set);
1866
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001867 /* startup_child() must be called before the signal handlers get
1868 * installed below as they are inherited into the spawned process.
1869 * Also we do not need to be protected by them as during interruption
1870 * in the startup_child() mode we kill the spawned process anyway.
1871 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001872 if (argv[0]) {
Dmitry V. Levin1d2435b2013-05-14 22:35:46 +00001873 if (!NOMMU_SYSTEM || daemonized_tracer)
1874 hide_log_until_execve = 1;
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001875 skip_one_b_execve = 1;
Denys Vlasenko837399a2012-01-24 11:37:03 +01001876 startup_child(argv);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001877 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001878
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001879 sa.sa_handler = SIG_IGN;
1880 sigemptyset(&sa.sa_mask);
1881 sa.sa_flags = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001882 sigaction(SIGTTOU, &sa, NULL); /* SIG_IGN */
1883 sigaction(SIGTTIN, &sa, NULL); /* SIG_IGN */
1884 if (opt_intr != INTR_ANYWHERE) {
1885 if (opt_intr == INTR_BLOCK_TSTP_TOO)
1886 sigaction(SIGTSTP, &sa, NULL); /* SIG_IGN */
1887 /*
1888 * In interactive mode (if no -o OUTFILE, or -p PID is used),
1889 * fatal signals are blocked while syscall stop is processed,
1890 * and acted on in between, when waiting for new syscall stops.
1891 * In non-interactive mode, signals are ignored.
1892 */
1893 if (opt_intr == INTR_WHILE_WAIT) {
1894 sigaddset(&blocked_set, SIGHUP);
1895 sigaddset(&blocked_set, SIGINT);
1896 sigaddset(&blocked_set, SIGQUIT);
1897 sigaddset(&blocked_set, SIGPIPE);
1898 sigaddset(&blocked_set, SIGTERM);
1899 sa.sa_handler = interrupt;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001900 }
1901 /* SIG_IGN, or set handler for these */
1902 sigaction(SIGHUP, &sa, NULL);
1903 sigaction(SIGINT, &sa, NULL);
1904 sigaction(SIGQUIT, &sa, NULL);
1905 sigaction(SIGPIPE, &sa, NULL);
1906 sigaction(SIGTERM, &sa, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001907 }
Denys Vlasenkofd883382012-03-09 13:03:41 +01001908 if (nprocs != 0 || daemonized_tracer)
Roland McGrath02203312007-06-11 22:06:31 +00001909 startup_attach();
Roland McGrath02203312007-06-11 22:06:31 +00001910
Denys Vlasenkofd883382012-03-09 13:03:41 +01001911 /* Do we want pids printed in our -o OUTFILE?
1912 * -ff: no (every pid has its own file); or
1913 * -f: yes (there can be more pids in the future); or
1914 * -p PID1,PID2: yes (there are already more than one pid)
1915 */
1916 print_pid_pfx = (outfname && followfork < 2 && (followfork == 1 || nprocs > 1));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001917}
1918
Denys Vlasenkoeebb04d2012-01-27 15:24:48 +01001919static struct tcb *
Roland McGrath54e931f2010-09-14 18:59:20 -07001920pid2tcb(int pid)
1921{
1922 int i;
1923
1924 if (pid <= 0)
1925 return NULL;
1926
1927 for (i = 0; i < tcbtabsize; i++) {
1928 struct tcb *tcp = tcbtab[i];
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001929 if (tcp->pid == pid)
Roland McGrath54e931f2010-09-14 18:59:20 -07001930 return tcp;
1931 }
1932
1933 return NULL;
1934}
1935
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001936static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001937cleanup(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001938{
1939 int i;
1940 struct tcb *tcp;
Denys Vlasenko35218842012-01-29 21:17:56 +01001941 int fatal_sig;
1942
1943 /* 'interrupted' is a volatile object, fetch it only once */
1944 fatal_sig = interrupted;
1945 if (!fatal_sig)
1946 fatal_sig = SIGTERM;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001947
Roland McGrathee9d4352002-12-18 04:16:10 +00001948 for (i = 0; i < tcbtabsize; i++) {
1949 tcp = tcbtab[i];
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001950 if (!tcp->pid)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001951 continue;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001952 if (debug_flag)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001953 fprintf(stderr,
1954 "cleanup: looking at pid %u\n", tcp->pid);
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001955 if (tcp->pid == strace_child) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001956 kill(tcp->pid, SIGCONT);
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001957 kill(tcp->pid, fatal_sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001958 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001959 detach(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001960 }
1961 if (cflag)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001962 call_summary(shared_log);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001963}
1964
1965static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001966interrupt(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001967{
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001968 interrupted = sig;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001969}
1970
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02001971static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001972trace(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001973{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001974 struct rusage ru;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001975
Denys Vlasenkod2e1f422013-07-08 11:28:27 +02001976 /* Used to be "while (nprocs != 0)", but in this testcase:
1977 * int main() { _exit(!!fork()); }
1978 * under strace -f, parent sometimes (rarely) manages
1979 * to exit before we see the first stop of the child,
1980 * and we are losing track of it:
1981 * 19923 clone(...) = 19924
1982 * 19923 exit_group(1) = ?
1983 * 19923 +++ exited with 1 +++
1984 * Waiting for ECHILD works better.
Denys Vlasenko7c41ce22013-07-08 13:55:04 +02001985 * (However, if -o|logger is in use, we can't do that.
1986 * Can work around that by double-forking the logger,
1987 * but that loses the ability to wait for its completion on exit.
1988 * Oh well...)
Denys Vlasenkod2e1f422013-07-08 11:28:27 +02001989 */
1990 while (1) {
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001991 int pid;
1992 int wait_errno;
1993 int status, sig;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001994 int stopped;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001995 struct tcb *tcp;
1996 unsigned event;
1997
Denys Vlasenko222713a2009-03-17 14:29:59 +00001998 if (interrupted)
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02001999 return;
Denys Vlasenko725dd422013-06-20 11:06:58 +02002000
Denys Vlasenko7c41ce22013-07-08 13:55:04 +02002001 if (popen_pid != 0 && nprocs == 0)
2002 return;
2003
Roland McGratheb9e2e82009-06-02 16:49:22 -07002004 if (interactive)
2005 sigprocmask(SIG_SETMASK, &empty_set, NULL);
Denys Vlasenko725dd422013-06-20 11:06:58 +02002006 pid = wait4(-1, &status, __WALL, (cflag ? &ru : NULL));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002007 wait_errno = errno;
Roland McGratheb9e2e82009-06-02 16:49:22 -07002008 if (interactive)
2009 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002010
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02002011 if (pid < 0) {
Denys Vlasenko725dd422013-06-20 11:06:58 +02002012 if (wait_errno == EINTR)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002013 continue;
Denys Vlasenkod2e1f422013-07-08 11:28:27 +02002014 if (nprocs == 0 && wait_errno == ECHILD)
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002015 return;
Denys Vlasenkod2e1f422013-07-08 11:28:27 +02002016 /* If nprocs > 0, ECHILD is not expected,
2017 * treat it as any other error here:
2018 */
Denys Vlasenko725dd422013-06-20 11:06:58 +02002019 errno = wait_errno;
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002020 perror_msg_and_die("wait4(__WALL)");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002021 }
Denys Vlasenko725dd422013-06-20 11:06:58 +02002022
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00002023 if (pid == popen_pid) {
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002024 if (!WIFSTOPPED(status))
Denys Vlasenko7dd23382011-06-22 13:03:56 +02002025 popen_pid = 0;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00002026 continue;
2027 }
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002028
2029 event = ((unsigned)status >> 16);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002030 if (debug_flag) {
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002031 char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko1b2bfbc2013-06-21 16:41:50 +02002032 char evbuf[sizeof(",EVENT_VFORK_DONE (%u)") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002033 strcpy(buf, "???");
2034 if (WIFSIGNALED(status))
2035#ifdef WCOREDUMP
2036 sprintf(buf, "WIFSIGNALED,%ssig=%s",
2037 WCOREDUMP(status) ? "core," : "",
2038 signame(WTERMSIG(status)));
2039#else
2040 sprintf(buf, "WIFSIGNALED,sig=%s",
2041 signame(WTERMSIG(status)));
2042#endif
2043 if (WIFEXITED(status))
2044 sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status));
2045 if (WIFSTOPPED(status))
2046 sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status)));
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02002047#ifdef WIFCONTINUED
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002048 /* Should never be seen */
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002049 if (WIFCONTINUED(status))
2050 strcpy(buf, "WIFCONTINUED");
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02002051#endif
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002052 evbuf[0] = '\0';
2053 if (event != 0) {
2054 static const char *const event_names[] = {
2055 [PTRACE_EVENT_CLONE] = "CLONE",
2056 [PTRACE_EVENT_FORK] = "FORK",
2057 [PTRACE_EVENT_VFORK] = "VFORK",
2058 [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE",
2059 [PTRACE_EVENT_EXEC] = "EXEC",
2060 [PTRACE_EVENT_EXIT] = "EXIT",
Denys Vlasenko4e020c02013-06-21 16:33:56 +02002061 /* [PTRACE_EVENT_STOP (=128)] would make biggish array */
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002062 };
Denys Vlasenko1b2bfbc2013-06-21 16:41:50 +02002063 const char *e = "??";
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002064 if (event < ARRAY_SIZE(event_names))
2065 e = event_names[event];
Denys Vlasenko4e020c02013-06-21 16:33:56 +02002066 else if (event == PTRACE_EVENT_STOP)
2067 e = "STOP";
Denys Vlasenko1b2bfbc2013-06-21 16:41:50 +02002068 sprintf(evbuf, ",EVENT_%s (%u)", e, event);
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002069 }
Denys Vlasenko38eab5d2013-07-02 12:18:22 +02002070 fprintf(stderr, " [wait(0x%06x) = %u] %s%s\n", status, pid, buf, evbuf);
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002071 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002072
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002073 /* Look up 'pid' in our table. */
Denys Vlasenko5d645812011-08-20 12:48:18 +02002074 tcp = pid2tcb(pid);
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002075
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002076 if (!tcp) {
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002077 if (!WIFSTOPPED(status)) {
2078 /* This can happen if we inherited
2079 * an unknown child. Example:
2080 * (sleep 1 & exec strace sleep 2)
2081 */
2082 error_msg("Exit of unknown pid %u seen", pid);
2083 continue;
2084 }
Roland McGrath41c48222008-07-18 00:25:10 +00002085 if (followfork) {
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002086 /* We assume it's a fork/vfork/clone child */
Denys Vlasenko418d66a2009-01-17 01:52:54 +00002087 tcp = alloctcb(pid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002088 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01002089 newoutf(tcp);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002090 if (!qflag)
Denys Vlasenko833fb132011-08-17 11:30:56 +02002091 fprintf(stderr, "Process %d attached\n",
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002092 pid);
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002093 } else {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002094 /* This can happen if a clone call used
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002095 * CLONE_PTRACE itself.
Denys Vlasenko71d3d292013-06-21 16:19:46 +02002096 */
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002097 ptrace(PTRACE_CONT, pid, (char *) 0, 0);
2098 error_msg("Stop of unknown pid %u seen, PTRACE_CONTed it", pid);
Denys Vlasenko71d3d292013-06-21 16:19:46 +02002099 continue;
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002100 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002101 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002102
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002103 clear_regs();
2104 if (WIFSTOPPED(status))
2105 get_regs(pid);
2106
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002107 /* Under Linux, execve changes pid to thread leader's pid,
2108 * and we see this changed pid on EVENT_EXEC and later,
2109 * execve sysexit. Leader "disappears" without exit
2110 * notification. Let user know that, drop leader's tcb,
2111 * and fix up pid in execve thread's tcb.
2112 * Effectively, execve thread's tcb replaces leader's tcb.
2113 *
2114 * BTW, leader is 'stuck undead' (doesn't report WIFEXITED
2115 * on exit syscall) in multithreaded programs exactly
2116 * in order to handle this case.
2117 *
2118 * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0.
2119 * On 2.6 and earlier, it can return garbage.
2120 */
2121 if (event == PTRACE_EVENT_EXEC && os_release >= KERNEL_VERSION(3,0,0)) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002122 FILE *fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002123 struct tcb *execve_thread;
2124 long old_pid = 0;
2125
2126 if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) < 0)
2127 goto dont_switch_tcbs;
Denys Vlasenko6162a3f2013-07-04 09:26:24 +02002128 /* Avoid truncation in pid2tcb() param passing */
2129 if (old_pid > UINT_MAX)
2130 goto dont_switch_tcbs;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002131 if (old_pid <= 0 || old_pid == pid)
2132 goto dont_switch_tcbs;
2133 execve_thread = pid2tcb(old_pid);
2134 /* It should be !NULL, but I feel paranoid */
2135 if (!execve_thread)
2136 goto dont_switch_tcbs;
2137
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002138 if (execve_thread->curcol != 0) {
2139 /*
2140 * One case we are here is -ff:
2141 * try "strace -oLOG -ff test/threaded_execve"
2142 */
2143 fprintf(execve_thread->outf, " <pid changed to %d ...>\n", pid);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002144 /*execve_thread->curcol = 0; - no need, see code below */
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002145 }
2146 /* Swap output FILEs (needed for -ff) */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002147 fp = execve_thread->outf;
2148 execve_thread->outf = tcp->outf;
2149 tcp->outf = fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002150 /* And their column positions */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002151 execve_thread->curcol = tcp->curcol;
2152 tcp->curcol = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002153 /* Drop leader, but close execve'd thread outfile (if -ff) */
2154 droptcb(tcp);
2155 /* Switch to the thread, reusing leader's outfile and pid */
2156 tcp = execve_thread;
2157 tcp->pid = pid;
2158 if (cflag != CFLAG_ONLY_STATS) {
2159 printleader(tcp);
2160 tprintf("+++ superseded by execve in pid %lu +++\n", old_pid);
2161 line_ended();
2162 tcp->flags |= TCB_REPRINT;
2163 }
2164 }
2165 dont_switch_tcbs:
2166
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002167 if (event == PTRACE_EVENT_EXEC) {
2168 if (detach_on_execve && !skip_one_b_execve)
2169 detach(tcp); /* do "-b execve" thingy */
2170 skip_one_b_execve = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002171 }
2172
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002173 /* Set current output file */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002174 current_tcp = tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002175
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002176 if (cflag) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002177 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2178 tcp->stime = ru.ru_stime;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002179 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002180
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002181 if (WIFSIGNALED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002182 if (pid == strace_child)
2183 exit_code = 0x100 | WTERMSIG(status);
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002184 if (cflag != CFLAG_ONLY_STATS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01002185 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)
2186 ) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002187 printleader(tcp);
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002188#ifdef WCOREDUMP
Denys Vlasenko000b6012012-01-28 01:25:03 +01002189 tprintf("+++ killed by %s %s+++\n",
Roland McGrath2efe8792004-01-13 09:59:45 +00002190 signame(WTERMSIG(status)),
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002191 WCOREDUMP(status) ? "(core dumped) " : "");
2192#else
Denys Vlasenko000b6012012-01-28 01:25:03 +01002193 tprintf("+++ killed by %s +++\n",
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002194 signame(WTERMSIG(status)));
Roland McGrath2efe8792004-01-13 09:59:45 +00002195#endif
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002196 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002197 }
2198 droptcb(tcp);
2199 continue;
2200 }
2201 if (WIFEXITED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002202 if (pid == strace_child)
2203 exit_code = WEXITSTATUS(status);
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01002204 if (cflag != CFLAG_ONLY_STATS &&
2205 qflag < 2) {
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002206 printleader(tcp);
Denys Vlasenko000b6012012-01-28 01:25:03 +01002207 tprintf("+++ exited with %d +++\n", WEXITSTATUS(status));
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002208 line_ended();
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002209 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002210 droptcb(tcp);
2211 continue;
2212 }
2213 if (!WIFSTOPPED(status)) {
2214 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
2215 droptcb(tcp);
2216 continue;
2217 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002218
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002219 /* Is this the very first time we see this tracee stopped? */
2220 if (tcp->flags & TCB_STARTUP) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002221 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002222 fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n", tcp->pid);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002223 tcp->flags &= ~TCB_STARTUP;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002224 if (tcp->flags & TCB_BPTSET) {
Roland McGrath02203312007-06-11 22:06:31 +00002225 /*
2226 * One example is a breakpoint inherited from
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002227 * parent through fork().
Roland McGrath02203312007-06-11 22:06:31 +00002228 */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002229 if (clearbpt(tcp) < 0) {
2230 /* Pretty fatal */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002231 droptcb(tcp);
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002232 exit_code = 1;
2233 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002234 }
2235 }
Denys Vlasenkoc169d942013-07-10 14:33:05 +02002236 if (!use_seize && ptrace_setoptions) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002237 if (debug_flag)
Denys Vlasenkoc169d942013-07-10 14:33:05 +02002238 fprintf(stderr, "setting opts 0x%x on pid %d\n", ptrace_setoptions, tcp->pid);
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002239 if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) {
2240 if (errno != ESRCH) {
2241 /* Should never happen, really */
2242 perror_msg_and_die("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02002243 }
2244 }
2245 }
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002246 }
2247
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002248 sig = WSTOPSIG(status);
2249
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002250 if (event != 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002251 /* Ptrace event */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002252#if USE_SEIZE
Denys Vlasenko26bc0602012-07-10 16:36:32 +02002253 if (event == PTRACE_EVENT_STOP) {
Denys Vlasenko67038162012-01-29 16:46:46 +01002254 /*
2255 * PTRACE_INTERRUPT-stop or group-stop.
2256 * PTRACE_INTERRUPT-stop has sig == SIGTRAP here.
2257 */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002258 if (sig == SIGSTOP
2259 || sig == SIGTSTP
2260 || sig == SIGTTIN
2261 || sig == SIGTTOU
2262 ) {
2263 stopped = 1;
2264 goto show_stopsig;
2265 }
2266 }
2267#endif
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002268 goto restart_tracee_with_sig_0;
2269 }
2270
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002271 /* Is this post-attach SIGSTOP?
2272 * Interestingly, the process may stop
2273 * with STOPSIG equal to some other signal
2274 * than SIGSTOP if we happend to attach
2275 * just before the process takes a signal.
2276 */
2277 if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002278 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002279 fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid);
2280 tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002281 goto restart_tracee_with_sig_0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002282 }
2283
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002284 if (sig != syscall_trap_sig) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002285 siginfo_t si;
2286
2287 /* Nonzero (true) if tracee is stopped by signal
2288 * (as opposed to "tracee received signal").
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002289 * TODO: shouldn't we check for errno == EINVAL too?
2290 * We can get ESRCH instead, you know...
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002291 */
2292 stopped = (ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002293#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002294 show_stopsig:
Denys Vlasenko67038162012-01-29 16:46:46 +01002295#endif
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002296 if (cflag != CFLAG_ONLY_STATS
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002297 && !hide_log_until_execve
2298 && (qual_flags[sig] & QUAL_SIGNAL)
2299 ) {
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002300 printleader(tcp);
2301 if (!stopped) {
2302 tprintf("--- %s ", signame(sig));
2303 printsiginfo(&si, verbose(tcp));
Denys Vlasenko5a2483b2013-07-01 12:49:14 +02002304 tprints(" ---\n");
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002305 } else
Denys Vlasenko5a2483b2013-07-01 12:49:14 +02002306 tprintf("--- stopped by %s ---\n",
2307 signame(sig));
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002308 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002309 }
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002310
2311 if (!stopped)
2312 /* It's signal-delivery-stop. Inject the signal */
2313 goto restart_tracee;
2314
2315 /* It's group-stop */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002316 if (use_seize) {
2317 /*
2318 * This ends ptrace-stop, but does *not* end group-stop.
2319 * This makes stopping signals work properly on straced process
2320 * (that is, process really stops. It used to continue to run).
2321 */
2322 if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) {
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002323 /* Note: ptrace_restart emitted error message */
2324 exit_code = 1;
2325 return;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002326 }
2327 continue;
2328 }
2329 /* We don't have PTRACE_LISTEN support... */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002330 goto restart_tracee;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002331 }
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002332
2333 /* We handled quick cases, we are permitted to interrupt now. */
Roland McGrath02203312007-06-11 22:06:31 +00002334 if (interrupted)
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002335 return;
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002336
2337 /* This should be syscall entry or exit.
2338 * (Or it still can be that pesky post-execve SIGTRAP!)
2339 * Handle it.
2340 */
Denys Vlasenko23506752012-03-21 10:32:49 +01002341 if (trace_syscall(tcp) < 0) {
2342 /* ptrace() failed in trace_syscall().
Roland McGratheb9e2e82009-06-02 16:49:22 -07002343 * Likely a result of process disappearing mid-flight.
Denys Vlasenko23506752012-03-21 10:32:49 +01002344 * Observed case: exit_group() or SIGKILL terminating
Denys Vlasenkof1e69032012-01-04 15:15:26 +01002345 * all processes in thread group.
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002346 * We assume that ptrace error was caused by process death.
Denys Vlasenkof2025022012-03-09 15:29:45 +01002347 * We used to detach(tcp) here, but since we no longer
2348 * implement "detach before death" policy/hack,
2349 * we can let this process to report its death to us
2350 * normally, via WIFEXITED or WIFSIGNALED wait status.
2351 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002352 continue;
2353 }
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002354 restart_tracee_with_sig_0:
2355 sig = 0;
2356 restart_tracee:
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002357 if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) {
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002358 /* Note: ptrace_restart emitted error message */
2359 exit_code = 1;
2360 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002361 }
Denys Vlasenko7c41ce22013-07-08 13:55:04 +02002362 } /* while (1) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002363}
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002364
2365int
2366main(int argc, char *argv[])
2367{
2368 init(argc, argv);
2369
2370 /* Run main tracing loop */
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002371 trace();
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002372
2373 cleanup();
2374 fflush(NULL);
Dmitry V. Levincf534362012-07-12 20:54:46 +00002375 if (shared_log != stderr)
2376 fclose(shared_log);
2377 if (popen_pid) {
2378 while (waitpid(popen_pid, NULL, 0) < 0 && errno == EINTR)
2379 ;
2380 }
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002381 if (exit_code > 0xff) {
2382 /* Avoid potential core file clobbering. */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00002383 struct_rlimit rlim = {0, 0};
2384 set_rlimit(RLIMIT_CORE, &rlim);
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002385
2386 /* Child was killed by a signal, mimic that. */
2387 exit_code &= 0xff;
2388 signal(exit_code, SIG_DFL);
2389 raise(exit_code);
2390 /* Paranoia - what if this signal is not fatal?
2391 Exit with 128 + signo then. */
2392 exit_code += 128;
2393 }
2394
2395 return exit_code;
2396}