blob: 35ee7bed5629b12ee95d3be5b490de89fcc72f60 [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;
Mark Hillse53bf232014-05-28 17:52:40 +010081bool count_wallclock = 0;
Daniel P. Berrange01997cf2013-05-13 11:30:55 +010082unsigned int qflag = 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020083/* Which WSTOPSIG(status) value marks syscall traps? */
Denys Vlasenko75422762011-05-27 14:36:01 +020084static unsigned int syscall_trap_sig = SIGTRAP;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010085static unsigned int tflag = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010086static bool rflag = 0;
87static bool print_pid_pfx = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +010088
89/* -I n */
90enum {
91 INTR_NOT_SET = 0,
92 INTR_ANYWHERE = 1, /* don't block/ignore any signals */
93 INTR_WHILE_WAIT = 2, /* block fatal signals while decoding syscall. default */
94 INTR_NEVER = 3, /* block fatal signals. default if '-o FILE PROG' */
95 INTR_BLOCK_TSTP_TOO = 4, /* block fatal signals and SIGTSTP (^Z) */
96 NUM_INTR_OPTS
97};
98static int opt_intr;
99/* We play with signal mask only if this mode is active: */
100#define interactive (opt_intr == INTR_WHILE_WAIT)
101
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000102/*
103 * daemonized_tracer supports -D option.
104 * With this option, strace forks twice.
105 * Unlike normal case, with -D *grandparent* process exec's,
106 * becoming a traced process. Child exits (this prevents traced process
107 * from having children it doesn't expect to have), and grandchild
108 * attaches to grandparent similarly to strace -p PID.
109 * This allows for more transparent interaction in cases
110 * when process and its parent are communicating via signals,
111 * wait() etc. Without -D, strace process gets lodged in between,
112 * disrupting parent<->child link.
113 */
114static bool daemonized_tracer = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000115
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100116#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100117static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP;
118# define use_seize (post_attach_sigstop == 0)
119#else
120# define post_attach_sigstop TCB_IGNORE_ONE_SIGSTOP
121# define use_seize 0
122#endif
123
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000124/* Sometimes we want to print only succeeding syscalls. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100125bool not_failing_only = 0;
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000126
Grant Edwards8a082772011-04-07 20:25:40 +0000127/* Show path associated with fd arguments */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100128bool show_fd_path = 0;
Grant Edwards8a082772011-04-07 20:25:40 +0000129
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100130static bool detach_on_execve = 0;
Denys Vlasenko2a3d2752013-05-14 16:07:46 +0200131/* Are we "strace PROG" and need to skip detach on first execve? */
132static bool skip_one_b_execve = 0;
133/* Are we "strace PROG" and need to hide everything until execve? */
134bool hide_log_until_execve = 0;
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100135
Dmitry V. Levina6809652008-11-10 17:14:58 +0000136static int exit_code = 0;
137static int strace_child = 0;
Denys Vlasenko75422762011-05-27 14:36:01 +0200138static int strace_tracer_pid = 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -0700139
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000140static char *username = NULL;
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200141static uid_t run_uid;
142static gid_t run_gid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000143
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100144unsigned int max_strlen = DEFAULT_STRLEN;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000145static int acolumn = DEFAULT_ACOLUMN;
Denys Vlasenko102ec492011-08-25 01:27:59 +0200146static char *acolumn_spaces;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100147
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000148static char *outfname = NULL;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100149/* If -ff, points to stderr. Else, it's our common output log */
150static FILE *shared_log;
151
Denys Vlasenko000b6012012-01-28 01:25:03 +0100152struct tcb *printing_tcp = NULL;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100153static struct tcb *current_tcp;
154
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200155static struct tcb **tcbtab;
Denys Vlasenko2b60c352011-06-22 12:45:25 +0200156static unsigned int nprocs, tcbtabsize;
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200157static const char *progname;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000158
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100159unsigned os_release; /* generated from uname()'s u.release */
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +0100160
Denys Vlasenko4a9ba982013-06-20 11:20:23 +0200161static void detach(struct tcb *tcp);
Andreas Schwabe5355de2009-10-27 16:56:43 +0100162static void cleanup(void);
163static void interrupt(int sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000164static sigset_t empty_set, blocked_set;
165
166#ifdef HAVE_SIG_ATOMIC_T
167static volatile sig_atomic_t interrupted;
Denys Vlasenkoa3559252012-01-29 16:43:51 +0100168#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000169static volatile int interrupted;
Denys Vlasenkoa3559252012-01-29 16:43:51 +0100170#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000171
Denys Vlasenko2c4fb902012-03-15 17:24:49 +0100172#ifndef HAVE_STRERROR
173
174#if !HAVE_DECL_SYS_ERRLIST
175extern int sys_nerr;
176extern char *sys_errlist[];
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +0100177#endif
Denys Vlasenko2c4fb902012-03-15 17:24:49 +0100178
179const char *
180strerror(int err_no)
181{
182 static char buf[sizeof("Unknown error %d") + sizeof(int)*3];
183
184 if (err_no < 1 || err_no >= sys_nerr) {
185 sprintf(buf, "Unknown error %d", err_no);
186 return buf;
187 }
188 return sys_errlist[err_no];
189}
190
191#endif /* HAVE_STERRROR */
192
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000193static void
Denys Vlasenkocb2ad002011-06-23 13:05:29 +0200194usage(FILE *ofp, int exitval)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000195{
196 fprintf(ofp, "\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200197usage: strace [-CdffhiqrtttTvVxxy] [-I n] [-e expr]...\n\
198 [-a column] [-o file] [-s strsize] [-P path]...\n\
199 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
200 or: strace -c[df] [-I n] [-e expr]... [-O overhead] [-S sortby]\n\
201 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000202-c -- count time, calls, and errors for each syscall and report summary\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200203-C -- like -c but also print regular output\n\
Mark Hillse53bf232014-05-28 17:52:40 +0100204-w -- summarise syscall latency (default is system time)\n\
Denys Vlasenko3e084ac2012-03-15 13:39:05 +0100205-d -- enable debug output to stderr\n\
Denys Vlasenkob51581e2012-01-29 16:53:03 +0100206-D -- run tracer process as a detached grandchild, not as parent\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000207-f -- follow forks, -ff -- with output into separate files\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000208-i -- print instruction pointer at time of syscall\n\
209-q -- suppress messages about attaching, detaching, etc.\n\
210-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
Denys Vlasenkocdab1be2012-02-03 12:17:57 +0100211-T -- print time spent in each syscall\n\
212-v -- verbose mode: print unabbreviated argv, stat, termios, etc. args\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000213-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000214-y -- print paths associated with file descriptor arguments\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200215-h -- print help message, -V -- print version\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000216-a column -- alignment COLUMN for printing syscall results (default %d)\n\
Denys Vlasenko22efaf02013-02-27 12:15:19 +0100217-b execve -- detach on this syscall\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000218-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
Denys Vlasenko38e79bb2013-02-26 11:33:54 +0100219 options: trace, abbrev, verbose, raw, signal, read, write\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200220-I interruptible --\n\
221 1: no signals are blocked\n\
222 2: fatal signals are blocked while decoding syscall (default)\n\
223 3: fatal signals are always blocked (default if '-o FILE PROG')\n\
224 4: fatal signals and SIGTSTP (^Z) are always blocked\n\
225 (useful to make 'strace -o FILE PROG' not stop on ^Z)\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000226-o file -- send trace output to FILE instead of stderr\n\
227-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
228-p pid -- trace process with process id PID, may be repeated\n\
229-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
230-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
231-u username -- run command as username handling setuid and/or setgid\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000232-E var=val -- put var=val in the environment for command\n\
233-E var -- remove var from the environment for command\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000234-P path -- trace accesses to path\n\
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100235"
Denys Vlasenko38e79bb2013-02-26 11:33:54 +0100236/* ancient, no one should use it
237-F -- attempt to follow vforks (deprecated, use -f)\n\
238 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100239/* this is broken, so don't document it
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000240-z -- print only succeeding syscalls\n\
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100241 */
Roland McGrathde6e5332003-01-24 04:31:23 +0000242, DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000243 exit(exitval);
244}
245
Denys Vlasenko75422762011-05-27 14:36:01 +0200246static void die(void) __attribute__ ((noreturn));
247static void die(void)
248{
249 if (strace_tracer_pid == getpid()) {
250 cflag = 0;
251 cleanup();
252 }
253 exit(1);
254}
255
256static void verror_msg(int err_no, const char *fmt, va_list p)
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200257{
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100258 char *msg;
259
Dmitry V. Levin44d05322011-06-09 15:50:41 +0000260 fflush(NULL);
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100261
262 /* We want to print entire message with single fprintf to ensure
263 * message integrity if stderr is shared with other programs.
264 * Thus we use vasprintf + single fprintf.
265 */
266 msg = NULL;
Denys Vlasenkocfad5432012-01-24 12:48:02 +0100267 if (vasprintf(&msg, fmt, p) >= 0) {
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100268 if (err_no)
269 fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(err_no));
270 else
271 fprintf(stderr, "%s: %s\n", progname, msg);
272 free(msg);
273 } else {
274 /* malloc in vasprintf failed, try it without malloc */
275 fprintf(stderr, "%s: ", progname);
276 vfprintf(stderr, fmt, p);
277 if (err_no)
278 fprintf(stderr, ": %s\n", strerror(err_no));
279 else
280 putc('\n', stderr);
281 }
282 /* We don't switch stderr to buffered, thus fprintf(stderr)
283 * always flushes its output and this is not necessary: */
284 /* fflush(stderr); */
Denys Vlasenko75422762011-05-27 14:36:01 +0200285}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200286
Denys Vlasenko75422762011-05-27 14:36:01 +0200287void error_msg(const char *fmt, ...)
288{
289 va_list p;
290 va_start(p, fmt);
291 verror_msg(0, fmt, p);
292 va_end(p);
293}
294
295void error_msg_and_die(const char *fmt, ...)
296{
297 va_list p;
298 va_start(p, fmt);
299 verror_msg(0, fmt, p);
300 die();
301}
302
303void perror_msg(const char *fmt, ...)
304{
305 va_list p;
306 va_start(p, fmt);
307 verror_msg(errno, fmt, p);
308 va_end(p);
309}
310
311void perror_msg_and_die(const char *fmt, ...)
312{
313 va_list p;
314 va_start(p, fmt);
315 verror_msg(errno, fmt, p);
316 die();
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200317}
318
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200319void die_out_of_memory(void)
320{
321 static bool recursed = 0;
322 if (recursed)
323 exit(1);
324 recursed = 1;
325 error_msg_and_die("Out of memory");
326}
327
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000328static void
329error_opt_arg(int opt, const char *arg)
330{
331 error_msg_and_die("Invalid -%c argument: '%s'", opt, arg);
332}
333
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100334#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100335static int
336ptrace_attach_or_seize(int pid)
337{
338 int r;
339 if (!use_seize)
Denys Vlasenkoc169d942013-07-10 14:33:05 +0200340 return ptrace(PTRACE_ATTACH, pid, 0L, 0L);
341 r = ptrace(PTRACE_SEIZE, pid, 0L, (unsigned long)ptrace_setoptions);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100342 if (r)
343 return r;
Denys Vlasenkoc169d942013-07-10 14:33:05 +0200344 r = ptrace(PTRACE_INTERRUPT, pid, 0L, 0L);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100345 return r;
346}
347#else
348# define ptrace_attach_or_seize(pid) ptrace(PTRACE_ATTACH, (pid), 0, 0)
349#endif
350
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100351/*
352 * Used when we want to unblock stopped traced process.
353 * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL.
354 * Returns 0 on success or if error was ESRCH
355 * (presumably process was killed while we talk to it).
356 * Otherwise prints error message and returns -1.
357 */
358static int
359ptrace_restart(int op, struct tcb *tcp, int sig)
360{
361 int err;
362 const char *msg;
363
364 errno = 0;
365 ptrace(op, tcp->pid, (void *) 0, (long) sig);
366 err = errno;
Denys Vlasenko23506752012-03-21 10:32:49 +0100367 if (!err)
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100368 return 0;
369
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100370 msg = "SYSCALL";
371 if (op == PTRACE_CONT)
372 msg = "CONT";
373 if (op == PTRACE_DETACH)
374 msg = "DETACH";
375#ifdef PTRACE_LISTEN
376 if (op == PTRACE_LISTEN)
377 msg = "LISTEN";
378#endif
Denys Vlasenko23506752012-03-21 10:32:49 +0100379 /*
380 * Why curcol != 0? Otherwise sometimes we get this:
381 *
382 * 10252 kill(10253, SIGKILL) = 0
383 * <ptrace(SYSCALL,10252):No such process>10253 ...next decode...
384 *
385 * 10252 died after we retrieved syscall exit data,
386 * but before we tried to restart it. Log looks ugly.
387 */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100388 if (current_tcp && current_tcp->curcol != 0) {
Denys Vlasenko23506752012-03-21 10:32:49 +0100389 tprintf(" <ptrace(%s):%s>\n", msg, strerror(err));
390 line_ended();
391 }
392 if (err == ESRCH)
393 return 0;
394 errno = err;
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100395 perror_msg("ptrace(PTRACE_%s,pid:%d,sig:%d)", msg, tcp->pid, sig);
396 return -1;
397}
398
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200399static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000400set_cloexec_flag(int fd)
401{
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200402 int flags, newflags;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000403
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200404 flags = fcntl(fd, F_GETFD);
405 if (flags < 0) {
406 /* Can happen only if fd is bad.
407 * Should never happen: if it does, we have a bug
408 * in the caller. Therefore we just abort
409 * instead of propagating the error.
410 */
411 perror_msg_and_die("fcntl(%d, F_GETFD)", fd);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000412 }
413
414 newflags = flags | FD_CLOEXEC;
415 if (flags == newflags)
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200416 return;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000417
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200418 fcntl(fd, F_SETFD, newflags); /* never fails */
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000419}
420
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100421static void kill_save_errno(pid_t pid, int sig)
422{
423 int saved_errno = errno;
424
425 (void) kill(pid, sig);
426 errno = saved_errno;
427}
428
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100429/*
430 * When strace is setuid executable, we have to swap uids
431 * before and after filesystem and process management operations.
432 */
433static void
434swap_uid(void)
435{
436 int euid = geteuid(), uid = getuid();
437
438 if (euid != uid && setreuid(euid, uid) < 0) {
439 perror_msg_and_die("setreuid");
440 }
441}
442
Dmitry V. Levin0506f0f2013-11-12 22:34:42 +0000443#ifdef _LARGEFILE64_SOURCE
Dmitry V. Levind3541302014-02-26 00:01:00 +0000444# ifdef HAVE_FOPEN64
445# define fopen_for_output fopen64
446# else
447# define fopen_for_output fopen
448# endif
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000449# define struct_stat struct stat64
450# define stat_file stat64
451# define struct_dirent struct dirent64
452# define read_dir readdir64
453# define struct_rlimit struct rlimit64
454# define set_rlimit setrlimit64
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100455#else
456# define fopen_for_output fopen
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000457# define struct_stat struct stat
458# define stat_file stat
459# define struct_dirent struct dirent
460# define read_dir readdir
461# define struct_rlimit struct rlimit
462# define set_rlimit setrlimit
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100463#endif
464
465static FILE *
466strace_fopen(const char *path)
467{
468 FILE *fp;
469
470 swap_uid();
471 fp = fopen_for_output(path, "w");
472 if (!fp)
473 perror_msg_and_die("Can't fopen '%s'", path);
474 swap_uid();
475 set_cloexec_flag(fileno(fp));
476 return fp;
477}
478
479static int popen_pid = 0;
480
481#ifndef _PATH_BSHELL
482# define _PATH_BSHELL "/bin/sh"
483#endif
484
485/*
486 * We cannot use standard popen(3) here because we have to distinguish
487 * popen child process from other processes we trace, and standard popen(3)
488 * does not export its child's pid.
489 */
490static FILE *
491strace_popen(const char *command)
492{
493 FILE *fp;
Denys Vlasenko519af5a2013-07-02 11:31:24 +0200494 int pid;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100495 int fds[2];
496
497 swap_uid();
498 if (pipe(fds) < 0)
499 perror_msg_and_die("pipe");
500
501 set_cloexec_flag(fds[1]); /* never fails */
502
Denys Vlasenko519af5a2013-07-02 11:31:24 +0200503 pid = vfork();
504 if (pid < 0)
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100505 perror_msg_and_die("vfork");
506
Denys Vlasenko519af5a2013-07-02 11:31:24 +0200507 if (pid == 0) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100508 /* child */
509 close(fds[1]);
510 if (fds[0] != 0) {
511 if (dup2(fds[0], 0))
512 perror_msg_and_die("dup2");
513 close(fds[0]);
514 }
515 execl(_PATH_BSHELL, "sh", "-c", command, NULL);
516 perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL);
517 }
518
519 /* parent */
Denys Vlasenko519af5a2013-07-02 11:31:24 +0200520 popen_pid = pid;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100521 close(fds[0]);
522 swap_uid();
523 fp = fdopen(fds[1], "w");
524 if (!fp)
525 die_out_of_memory();
526 return fp;
527}
528
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100529void
530tprintf(const char *fmt, ...)
531{
532 va_list args;
533
534 va_start(args, fmt);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100535 if (current_tcp) {
Denys Vlasenko6e4f3c12012-04-16 18:22:19 +0200536 int n = strace_vfprintf(current_tcp->outf, fmt, args);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100537 if (n < 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100538 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000539 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100540 } else
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100541 current_tcp->curcol += n;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100542 }
543 va_end(args);
544}
545
Dmitry V. Levind3541302014-02-26 00:01:00 +0000546#ifndef HAVE_FPUTS_UNLOCKED
547# define fputs_unlocked fputs
548#endif
549
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100550void
551tprints(const char *str)
552{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100553 if (current_tcp) {
Denys Vlasenko142aee02012-04-16 18:10:15 +0200554 int n = fputs_unlocked(str, current_tcp->outf);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100555 if (n >= 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100556 current_tcp->curcol += strlen(str);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100557 return;
558 }
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100559 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000560 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100561 }
562}
563
564void
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100565line_ended(void)
566{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100567 if (current_tcp) {
568 current_tcp->curcol = 0;
569 fflush(current_tcp->outf);
570 }
571 if (printing_tcp) {
572 printing_tcp->curcol = 0;
573 printing_tcp = NULL;
574 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100575}
576
577void
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100578printleader(struct tcb *tcp)
579{
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100580 /* If -ff, "previous tcb we printed" is always the same as current,
581 * because we have per-tcb output files.
582 */
583 if (followfork >= 2)
584 printing_tcp = tcp;
585
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100586 if (printing_tcp) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100587 current_tcp = printing_tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100588 if (printing_tcp->curcol != 0 && (followfork < 2 || printing_tcp == tcp)) {
589 /*
590 * case 1: we have a shared log (i.e. not -ff), and last line
591 * wasn't finished (same or different tcb, doesn't matter).
592 * case 2: split log, we are the same tcb, but our last line
593 * didn't finish ("SIGKILL nuked us after syscall entry" etc).
594 */
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100595 tprints(" <unfinished ...>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100596 printing_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100597 }
598 }
599
600 printing_tcp = tcp;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100601 current_tcp = tcp;
602 current_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100603
604 if (print_pid_pfx)
605 tprintf("%-5d ", tcp->pid);
606 else if (nprocs > 1 && !outfname)
607 tprintf("[pid %5u] ", tcp->pid);
608
609 if (tflag) {
610 char str[sizeof("HH:MM:SS")];
611 struct timeval tv, dtv;
612 static struct timeval otv;
613
614 gettimeofday(&tv, NULL);
615 if (rflag) {
616 if (otv.tv_sec == 0)
617 otv = tv;
618 tv_sub(&dtv, &tv, &otv);
619 tprintf("%6ld.%06ld ",
620 (long) dtv.tv_sec, (long) dtv.tv_usec);
621 otv = tv;
622 }
623 else if (tflag > 2) {
624 tprintf("%ld.%06ld ",
625 (long) tv.tv_sec, (long) tv.tv_usec);
626 }
627 else {
628 time_t local = tv.tv_sec;
629 strftime(str, sizeof(str), "%T", localtime(&local));
630 if (tflag > 1)
631 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
632 else
633 tprintf("%s ", str);
634 }
635 }
636 if (iflag)
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200637 print_pc(tcp);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100638}
639
640void
641tabto(void)
642{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100643 if (current_tcp->curcol < acolumn)
644 tprints(acolumn_spaces + current_tcp->curcol);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100645}
646
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100647/* Should be only called directly *after successful attach* to a tracee.
648 * Otherwise, "strace -oFILE -ff -p<nonexistant_pid>"
649 * may create bogus empty FILE.<nonexistant_pid>, and then die.
650 */
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200651static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000652newoutf(struct tcb *tcp)
653{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100654 tcp->outf = shared_log; /* if not -ff mode, the same file is for all */
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100655 if (followfork >= 2) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000656 char name[520 + sizeof(int) * 3];
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000657 sprintf(name, "%.512s.%u", outfname, tcp->pid);
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200658 tcp->outf = strace_fopen(name);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000659 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000660}
661
Denys Vlasenko558e5122012-03-12 23:32:16 +0100662static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100663expand_tcbtab(void)
664{
665 /* Allocate some more TCBs and expand the table.
666 We don't want to relocate the TCBs because our
667 callers have pointers and it would be a pain.
668 So tcbtab is a table of pointers. Since we never
669 free the TCBs, we allocate a single chunk of many. */
670 int i = tcbtabsize;
671 struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0]));
672 struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0]));
673 if (!newtab || !newtcbs)
674 die_out_of_memory();
675 tcbtabsize *= 2;
676 tcbtab = newtab;
677 while (i < tcbtabsize)
678 tcbtab[i++] = newtcbs++;
679}
680
681static struct tcb *
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100682alloctcb(int pid)
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100683{
684 int i;
685 struct tcb *tcp;
686
687 if (nprocs == tcbtabsize)
688 expand_tcbtab();
689
690 for (i = 0; i < tcbtabsize; i++) {
691 tcp = tcbtab[i];
Denys Vlasenkofadbf662013-06-26 14:14:29 +0200692 if (!tcp->pid) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100693 memset(tcp, 0, sizeof(*tcp));
694 tcp->pid = pid;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100695#if SUPPORTED_PERSONALITIES > 1
696 tcp->currpers = current_personality;
697#endif
698 nprocs++;
699 if (debug_flag)
700 fprintf(stderr, "new tcb for pid %d, active tcbs:%d\n", tcp->pid, nprocs);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100701 return tcp;
702 }
703 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100704 error_msg_and_die("bug in alloctcb");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100705}
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100706
707static void
708droptcb(struct tcb *tcp)
709{
710 if (tcp->pid == 0)
711 return;
712
713 nprocs--;
714 if (debug_flag)
715 fprintf(stderr, "dropped tcb for pid %d, %d remain\n", tcp->pid, nprocs);
716
717 if (tcp->outf) {
Denys Vlasenko8511f2a2012-03-22 09:35:51 +0100718 if (followfork >= 2) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100719 if (tcp->curcol != 0)
720 fprintf(tcp->outf, " <detached ...>\n");
721 fclose(tcp->outf);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100722 } else {
723 if (printing_tcp == tcp && tcp->curcol != 0)
724 fprintf(tcp->outf, " <detached ...>\n");
725 fflush(tcp->outf);
726 }
727 }
728
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100729 if (current_tcp == tcp)
730 current_tcp = NULL;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100731 if (printing_tcp == tcp)
732 printing_tcp = NULL;
733
734 memset(tcp, 0, sizeof(*tcp));
735}
736
Denys Vlasenkof1669e72013-06-18 18:09:39 +0200737/* Detach traced process.
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100738 * Never call DETACH twice on the same process as both unattached and
739 * attached-unstopped processes give the same ESRCH. For unattached process we
740 * would SIGSTOP it and wait for its SIGSTOP notification forever.
741 */
Denys Vlasenko4a9ba982013-06-20 11:20:23 +0200742static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100743detach(struct tcb *tcp)
744{
745 int error;
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200746 int status;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100747
748 if (tcp->flags & TCB_BPTSET)
749 clearbpt(tcp);
750
751 /*
752 * Linux wrongly insists the child be stopped
753 * before detaching. Arghh. We go through hoops
754 * to make a clean break of things.
755 */
756#if defined(SPARC)
Denys Vlasenko978fbc92012-09-13 10:28:43 +0200757# undef PTRACE_DETACH
758# define PTRACE_DETACH PTRACE_SUNDETACH
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100759#endif
760
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200761 if (!(tcp->flags & TCB_ATTACHED))
762 goto drop;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100763
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200764 /* We attached but possibly didn't see the expected SIGSTOP.
765 * We must catch exactly one as otherwise the detached process
766 * would be left stopped (process state T).
767 */
768 if (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)
769 goto wait_loop;
770
771 error = ptrace(PTRACE_DETACH, tcp->pid, 0, 0);
772 if (!error) {
773 /* On a clear day, you can see forever. */
774 goto drop;
775 }
776 if (errno != ESRCH) {
777 /* Shouldn't happen. */
778 perror_msg("detach: ptrace(PTRACE_DETACH,%u)", tcp->pid);
779 goto drop;
780 }
781 /* ESRCH: process is either not stopped or doesn't exist. */
782 if (my_tkill(tcp->pid, 0) < 0) {
783 if (errno != ESRCH)
784 /* Shouldn't happen. */
785 perror_msg("detach: tkill(%u,0)", tcp->pid);
786 /* else: process doesn't exist. */
787 goto drop;
788 }
789 /* Process is not stopped, need to stop it. */
790 if (use_seize) {
791 /*
792 * With SEIZE, tracee can be in group-stop already.
793 * In this state sending it another SIGSTOP does nothing.
794 * Need to use INTERRUPT.
795 * Testcase: trying to ^C a "strace -p <stopped_process>".
796 */
797 error = ptrace(PTRACE_INTERRUPT, tcp->pid, 0, 0);
798 if (!error)
799 goto wait_loop;
800 if (errno != ESRCH)
801 perror_msg("detach: ptrace(PTRACE_INTERRUPT,%u)", tcp->pid);
802 }
803 else {
804 error = my_tkill(tcp->pid, SIGSTOP);
805 if (!error)
806 goto wait_loop;
807 if (errno != ESRCH)
808 perror_msg("detach: tkill(%u,SIGSTOP)", tcp->pid);
809 }
810 /* Either process doesn't exist, or some weird error. */
811 goto drop;
812
Denys Vlasenkoa2de9da2013-06-21 15:50:41 +0200813 wait_loop:
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200814 /* We end up here in three cases:
815 * 1. We sent PTRACE_INTERRUPT (use_seize case)
816 * 2. We sent SIGSTOP (!use_seize)
817 * 3. Attach SIGSTOP was already pending (TCB_IGNORE_ONE_SIGSTOP set)
818 */
819 for (;;) {
820 int sig;
821 if (waitpid(tcp->pid, &status, __WALL) < 0) {
822 if (errno == EINTR)
823 continue;
824 /*
825 * if (errno == ECHILD) break;
826 * ^^^ WRONG! We expect this PID to exist,
827 * and want to emit a message otherwise:
828 */
829 perror_msg("detach: waitpid(%u)", tcp->pid);
830 break;
831 }
832 if (!WIFSTOPPED(status)) {
833 /*
834 * Tracee exited or was killed by signal.
835 * We shouldn't normally reach this place:
836 * we don't want to consume exit status.
837 * Consider "strace -p PID" being ^C-ed:
838 * we want merely to detach from PID.
839 *
840 * However, we _can_ end up here if tracee
841 * was SIGKILLed.
842 */
843 break;
844 }
845 sig = WSTOPSIG(status);
846 if (debug_flag)
847 fprintf(stderr, "detach wait: event:%d sig:%d\n",
848 (unsigned)status >> 16, sig);
849 if (use_seize) {
850 unsigned event = (unsigned)status >> 16;
851 if (event == PTRACE_EVENT_STOP /*&& sig == SIGTRAP*/) {
Denys Vlasenkofdfa47a2013-06-20 12:10:21 +0200852 /*
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200853 * sig == SIGTRAP: PTRACE_INTERRUPT stop.
854 * sig == other: process was already stopped
855 * with this stopping sig (see tests/detach-stopped).
856 * Looks like re-injecting this sig is not necessary
857 * in DETACH for the tracee to remain stopped.
Denys Vlasenkofdfa47a2013-06-20 12:10:21 +0200858 */
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200859 sig = 0;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100860 }
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200861 /*
862 * PTRACE_INTERRUPT is not guaranteed to produce
863 * the above event if other ptrace-stop is pending.
864 * See tests/detach-sleeping testcase:
865 * strace got SIGINT while tracee is sleeping.
866 * We sent PTRACE_INTERRUPT.
867 * We see syscall exit, not PTRACE_INTERRUPT stop.
868 * We won't get PTRACE_INTERRUPT stop
869 * if we would CONT now. Need to DETACH.
870 */
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200871 if (sig == syscall_trap_sig)
872 sig = 0;
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200873 /* else: not sure in which case we can be here.
874 * Signal stop? Inject it while detaching.
875 */
876 ptrace_restart(PTRACE_DETACH, tcp, sig);
877 break;
878 }
879 /* Note: this check has to be after use_seize check */
880 /* (else, in use_seize case SIGSTOP will be mistreated) */
881 if (sig == SIGSTOP) {
882 /* Detach, suppressing SIGSTOP */
883 ptrace_restart(PTRACE_DETACH, tcp, 0);
884 break;
885 }
886 if (sig == syscall_trap_sig)
887 sig = 0;
888 /* Can't detach just yet, may need to wait for SIGSTOP */
889 error = ptrace_restart(PTRACE_CONT, tcp, sig);
890 if (error < 0) {
891 /* Should not happen.
892 * Note: ptrace_restart returns 0 on ESRCH, so it's not it.
893 * ptrace_restart already emitted error message.
894 */
895 break;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100896 }
897 }
898
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200899 drop:
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100900 if (!qflag && (tcp->flags & TCB_ATTACHED))
901 fprintf(stderr, "Process %u detached\n", tcp->pid);
902
903 droptcb(tcp);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100904}
905
906static void
Denys Vlasenko558e5122012-03-12 23:32:16 +0100907process_opt_p_list(char *opt)
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100908{
909 while (*opt) {
910 /*
911 * We accept -p PID,PID; -p "`pidof PROG`"; -p "`pgrep PROG`".
912 * pidof uses space as delim, pgrep uses newline. :(
913 */
914 int pid;
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100915 char *delim = opt + strcspn(opt, ", \n\t");
916 char c = *delim;
917
918 *delim = '\0';
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000919 pid = string_to_uint(opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100920 if (pid <= 0) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000921 error_msg_and_die("Invalid process id: '%s'", opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100922 }
923 if (pid == strace_tracer_pid) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000924 error_msg_and_die("I'm sorry, I can't let you do that, Dave.");
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100925 }
926 *delim = c;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100927 alloctcb(pid);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100928 if (c == '\0')
929 break;
930 opt = delim + 1;
931 }
932}
933
Roland McGrath02203312007-06-11 22:06:31 +0000934static void
935startup_attach(void)
936{
937 int tcbi;
938 struct tcb *tcp;
939
940 /*
941 * Block user interruptions as we would leave the traced
942 * process stopped (process state T) if we would terminate in
Denys Vlasenko2e968c02011-09-01 10:23:09 +0200943 * between PTRACE_ATTACH and wait4() on SIGSTOP.
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200944 * We rely on cleanup() from this point on.
Roland McGrath02203312007-06-11 22:06:31 +0000945 */
946 if (interactive)
947 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
948
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000949 if (daemonized_tracer) {
950 pid_t pid = fork();
951 if (pid < 0) {
Denys Vlasenko014ca3a2011-09-02 16:19:30 +0200952 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000953 }
954 if (pid) { /* parent */
955 /*
Denys Vlasenko75422762011-05-27 14:36:01 +0200956 * Wait for grandchild to attach to straced process
957 * (grandparent). Grandchild SIGKILLs us after it attached.
958 * Grandparent's wait() is unblocked by our death,
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000959 * it proceeds to exec the straced program.
960 */
961 pause();
962 _exit(0); /* paranoia */
963 }
Denys Vlasenko75422762011-05-27 14:36:01 +0200964 /* grandchild */
965 /* We will be the tracer process. Remember our new pid: */
966 strace_tracer_pid = getpid();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000967 }
968
Roland McGrath02203312007-06-11 22:06:31 +0000969 for (tcbi = 0; tcbi < tcbtabsize; tcbi++) {
970 tcp = tcbtab[tcbi];
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200971
Denys Vlasenkofadbf662013-06-26 14:14:29 +0200972 if (!tcp->pid)
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100973 continue;
974
Denys Vlasenkod116a732011-09-05 14:01:33 +0200975 /* Is this a process we should attach to, but not yet attached? */
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100976 if (tcp->flags & TCB_ATTACHED)
977 continue; /* no, we already attached it */
Denys Vlasenkod116a732011-09-05 14:01:33 +0200978
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000979 if (followfork && !daemonized_tracer) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000980 char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3];
Roland McGrath02203312007-06-11 22:06:31 +0000981 DIR *dir;
982
983 sprintf(procdir, "/proc/%d/task", tcp->pid);
984 dir = opendir(procdir);
985 if (dir != NULL) {
986 unsigned int ntid = 0, nerr = 0;
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000987 struct_dirent *de;
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200988
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000989 while ((de = read_dir(dir)) != NULL) {
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200990 struct tcb *cur_tcp;
991 int tid;
992
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000993 if (de->d_fileno == 0)
Roland McGrath02203312007-06-11 22:06:31 +0000994 continue;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000995 /* we trust /proc filesystem */
Roland McGrath02203312007-06-11 22:06:31 +0000996 tid = atoi(de->d_name);
997 if (tid <= 0)
998 continue;
999 ++ntid;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001000 if (ptrace_attach_or_seize(tid) < 0) {
Roland McGrath02203312007-06-11 22:06:31 +00001001 ++nerr;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001002 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001003 fprintf(stderr, "attach to pid %d failed\n", tid);
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001004 continue;
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001005 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001006 if (debug_flag)
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001007 fprintf(stderr, "attach to pid %d succeeded\n", tid);
1008 cur_tcp = tcp;
1009 if (tid != tcp->pid)
1010 cur_tcp = alloctcb(tid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001011 cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001012 newoutf(cur_tcp);
Roland McGrath02203312007-06-11 22:06:31 +00001013 }
1014 closedir(dir);
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001015 if (interactive) {
1016 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1017 if (interrupted)
1018 goto ret;
1019 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1020 }
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001021 ntid -= nerr;
1022 if (ntid == 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001023 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +00001024 droptcb(tcp);
1025 continue;
1026 }
1027 if (!qflag) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001028 fprintf(stderr, ntid > 1
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001029? "Process %u attached with %u threads\n"
1030: "Process %u attached\n",
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02001031 tcp->pid, ntid);
Roland McGrath02203312007-06-11 22:06:31 +00001032 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001033 if (!(tcp->flags & TCB_ATTACHED)) {
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001034 /* -p PID, we failed to attach to PID itself
1035 * but did attach to some of its sibling threads.
1036 * Drop PID's tcp.
1037 */
1038 droptcb(tcp);
1039 }
Roland McGrath02203312007-06-11 22:06:31 +00001040 continue;
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001041 } /* if (opendir worked) */
1042 } /* if (-f) */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001043 if (ptrace_attach_or_seize(tcp->pid) < 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001044 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +00001045 droptcb(tcp);
1046 continue;
1047 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001048 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001049 newoutf(tcp);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001050 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001051 fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001052
1053 if (daemonized_tracer) {
1054 /*
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001055 * Make parent go away.
1056 * Also makes grandparent's wait() unblock.
1057 */
1058 kill(getppid(), SIGKILL);
1059 }
1060
Roland McGrath02203312007-06-11 22:06:31 +00001061 if (!qflag)
1062 fprintf(stderr,
Denys Vlasenko9c3861d2012-03-16 15:21:49 +01001063 "Process %u attached\n",
Roland McGrath02203312007-06-11 22:06:31 +00001064 tcp->pid);
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001065 } /* for each tcbtab[] */
Roland McGrath02203312007-06-11 22:06:31 +00001066
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02001067 ret:
Roland McGrath02203312007-06-11 22:06:31 +00001068 if (interactive)
1069 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1070}
1071
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001072/* Stack-o-phobic exec helper, in the hope to work around
1073 * NOMMU + "daemonized tracer" difficulty.
1074 */
1075struct exec_params {
1076 int fd_to_close;
1077 uid_t run_euid;
1078 gid_t run_egid;
1079 char **argv;
1080 char *pathname;
1081};
1082static struct exec_params params_for_tracee;
1083static void __attribute__ ((noinline, noreturn))
1084exec_or_die(void)
1085{
1086 struct exec_params *params = &params_for_tracee;
1087
1088 if (params->fd_to_close >= 0)
1089 close(params->fd_to_close);
1090 if (!daemonized_tracer && !use_seize) {
1091 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) {
1092 perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)");
1093 }
1094 }
1095
1096 if (username != NULL) {
1097 /*
1098 * It is important to set groups before we
1099 * lose privileges on setuid.
1100 */
1101 if (initgroups(username, run_gid) < 0) {
1102 perror_msg_and_die("initgroups");
1103 }
1104 if (setregid(run_gid, params->run_egid) < 0) {
1105 perror_msg_and_die("setregid");
1106 }
1107 if (setreuid(run_uid, params->run_euid) < 0) {
1108 perror_msg_and_die("setreuid");
1109 }
1110 }
1111 else if (geteuid() != 0)
1112 if (setreuid(run_uid, run_uid) < 0) {
1113 perror_msg_and_die("setreuid");
1114 }
1115
1116 if (!daemonized_tracer) {
1117 /*
1118 * Induce a ptrace stop. Tracer (our parent)
1119 * will resume us with PTRACE_SYSCALL and display
1120 * the immediately following execve syscall.
1121 * Can't do this on NOMMU systems, we are after
1122 * vfork: parent is blocked, stopping would deadlock.
1123 */
1124 if (!NOMMU_SYSTEM)
1125 kill(getpid(), SIGSTOP);
1126 } else {
1127 alarm(3);
1128 /* we depend on SIGCHLD set to SIG_DFL by init code */
1129 /* if it happens to be SIG_IGN'ed, wait won't block */
1130 wait(NULL);
1131 alarm(0);
1132 }
1133
1134 execv(params->pathname, params->argv);
1135 perror_msg_and_die("exec");
1136}
1137
Roland McGrath02203312007-06-11 22:06:31 +00001138static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001139startup_child(char **argv)
Roland McGrath02203312007-06-11 22:06:31 +00001140{
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001141 struct_stat statbuf;
Roland McGrath02203312007-06-11 22:06:31 +00001142 const char *filename;
1143 char pathname[MAXPATHLEN];
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001144 int pid;
Roland McGrath02203312007-06-11 22:06:31 +00001145 struct tcb *tcp;
1146
1147 filename = argv[0];
1148 if (strchr(filename, '/')) {
1149 if (strlen(filename) > sizeof pathname - 1) {
1150 errno = ENAMETOOLONG;
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001151 perror_msg_and_die("exec");
Roland McGrath02203312007-06-11 22:06:31 +00001152 }
1153 strcpy(pathname, filename);
1154 }
1155#ifdef USE_DEBUGGING_EXEC
1156 /*
1157 * Debuggers customarily check the current directory
1158 * first regardless of the path but doing that gives
1159 * security geeks a panic attack.
1160 */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001161 else if (stat_file(filename, &statbuf) == 0)
Roland McGrath02203312007-06-11 22:06:31 +00001162 strcpy(pathname, filename);
1163#endif /* USE_DEBUGGING_EXEC */
1164 else {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +00001165 const char *path;
Roland McGrath02203312007-06-11 22:06:31 +00001166 int m, n, len;
1167
1168 for (path = getenv("PATH"); path && *path; path += m) {
Denys Vlasenko4f3df072012-01-29 22:38:35 +01001169 const char *colon = strchr(path, ':');
1170 if (colon) {
1171 n = colon - path;
Roland McGrath02203312007-06-11 22:06:31 +00001172 m = n + 1;
1173 }
1174 else
1175 m = n = strlen(path);
1176 if (n == 0) {
1177 if (!getcwd(pathname, MAXPATHLEN))
1178 continue;
1179 len = strlen(pathname);
1180 }
1181 else if (n > sizeof pathname - 1)
1182 continue;
1183 else {
1184 strncpy(pathname, path, n);
1185 len = n;
1186 }
1187 if (len && pathname[len - 1] != '/')
1188 pathname[len++] = '/';
1189 strcpy(pathname + len, filename);
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001190 if (stat_file(pathname, &statbuf) == 0 &&
Roland McGrath02203312007-06-11 22:06:31 +00001191 /* Accept only regular files
1192 with some execute bits set.
1193 XXX not perfect, might still fail */
1194 S_ISREG(statbuf.st_mode) &&
1195 (statbuf.st_mode & 0111))
1196 break;
1197 }
1198 }
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001199 if (stat_file(pathname, &statbuf) < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001200 perror_msg_and_die("Can't stat '%s'", filename);
Roland McGrath02203312007-06-11 22:06:31 +00001201 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001202
1203 params_for_tracee.fd_to_close = (shared_log != stderr) ? fileno(shared_log) : -1;
1204 params_for_tracee.run_euid = (statbuf.st_mode & S_ISUID) ? statbuf.st_uid : run_uid;
1205 params_for_tracee.run_egid = (statbuf.st_mode & S_ISGID) ? statbuf.st_gid : run_gid;
1206 params_for_tracee.argv = argv;
1207 /*
1208 * On NOMMU, can be safely freed only after execve in tracee.
1209 * It's hard to know when that happens, so we just leak it.
1210 */
Denys Vlasenko05f32512013-02-26 12:00:34 +01001211 params_for_tracee.pathname = NOMMU_SYSTEM ? strdup(pathname) : pathname;
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001212
Dmitry V. Levin882478a2013-05-13 18:43:28 +00001213#if defined HAVE_PRCTL && defined PR_SET_PTRACER && defined PR_SET_PTRACER_ANY
1214 if (daemonized_tracer)
1215 prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY);
1216#endif
1217
Denys Vlasenkoe8681c92013-06-26 14:27:11 +02001218 pid = fork();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001219 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001220 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001221 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001222 if ((pid != 0 && daemonized_tracer)
1223 || (pid == 0 && !daemonized_tracer)
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001224 ) {
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001225 /* We are to become the tracee. Two cases:
1226 * -D: we are parent
1227 * not -D: we are child
1228 */
1229 exec_or_die();
Roland McGrath02203312007-06-11 22:06:31 +00001230 }
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001231
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001232 /* We are the tracer */
Denys Vlasenko75422762011-05-27 14:36:01 +02001233
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001234 if (!daemonized_tracer) {
Denys Vlasenkoe8681c92013-06-26 14:27:11 +02001235 strace_child = pid;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001236 if (!use_seize) {
1237 /* child did PTRACE_TRACEME, nothing to do in parent */
1238 } else {
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001239 if (!NOMMU_SYSTEM) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001240 /* Wait until child stopped itself */
1241 int status;
1242 while (waitpid(pid, &status, WSTOPPED) < 0) {
1243 if (errno == EINTR)
1244 continue;
1245 perror_msg_and_die("waitpid");
1246 }
1247 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001248 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001249 perror_msg_and_die("Unexpected wait status %x", status);
1250 }
1251 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001252 /* Else: NOMMU case, we have no way to sync.
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001253 * Just attach to it as soon as possible.
1254 * This means that we may miss a few first syscalls...
1255 */
1256
1257 if (ptrace_attach_or_seize(pid)) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001258 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001259 perror_msg_and_die("Can't attach to %d", pid);
1260 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001261 if (!NOMMU_SYSTEM)
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001262 kill(pid, SIGCONT);
1263 }
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001264 tcp = alloctcb(pid);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001265 if (!NOMMU_SYSTEM)
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001266 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001267 else
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001268 tcp->flags |= TCB_ATTACHED | TCB_STARTUP;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001269 newoutf(tcp);
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001270 }
1271 else {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001272 /* With -D, we are *child* here, IOW: different pid. Fetch it: */
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001273 strace_tracer_pid = getpid();
1274 /* The tracee is our parent: */
1275 pid = getppid();
Denys Vlasenkof2025022012-03-09 15:29:45 +01001276 alloctcb(pid);
1277 /* attaching will be done later, by startup_attach */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001278 /* note: we don't do newoutf(tcp) here either! */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001279
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001280 /* NOMMU BUG! -D mode is active, we (child) return,
1281 * and we will scribble over parent's stack!
1282 * When parent later unpauses, it segfaults.
1283 *
1284 * We work around it
1285 * (1) by declaring exec_or_die() NORETURN,
1286 * hopefully compiler will just jump to it
1287 * instead of call (won't push anything to stack),
1288 * (2) by trying very hard in exec_or_die()
1289 * to not use any stack,
1290 * (3) having a really big (MAXPATHLEN) stack object
1291 * in this function, which creates a "buffer" between
1292 * child's and parent's stack pointers.
1293 * This may save us if (1) and (2) failed
1294 * and compiler decided to use stack in exec_or_die() anyway
1295 * (happens on i386 because of stack parameter passing).
Denys Vlasenko05f32512013-02-26 12:00:34 +01001296 *
1297 * A cleaner solution is to use makecontext + setcontext
1298 * to create a genuine separate stack and execute on it.
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001299 */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001300 }
Roland McGrath02203312007-06-11 22:06:31 +00001301}
1302
Wang Chaob13c0de2010-11-12 17:25:19 +08001303/*
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001304 * Test whether the kernel support PTRACE_O_TRACECLONE et al options.
Wang Chaob13c0de2010-11-12 17:25:19 +08001305 * First fork a new child, call ptrace with PTRACE_SETOPTIONS on it,
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001306 * and then see which options are supported by the kernel.
Wang Chaob13c0de2010-11-12 17:25:19 +08001307 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001308static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001309test_ptrace_setoptions_followfork(void)
Wang Chaob13c0de2010-11-12 17:25:19 +08001310{
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001311 int pid, expected_grandchild = 0, found_grandchild = 0;
1312 const unsigned int test_options = PTRACE_O_TRACECLONE |
1313 PTRACE_O_TRACEFORK |
1314 PTRACE_O_TRACEVFORK;
Wang Chaob13c0de2010-11-12 17:25:19 +08001315
Denys Vlasenko05f32512013-02-26 12:00:34 +01001316 /* Need fork for test. NOMMU has no forks */
1317 if (NOMMU_SYSTEM)
1318 goto worked; /* be bold, and pretend that test succeeded */
1319
Denys Vlasenko5d645812011-08-20 12:48:18 +02001320 pid = fork();
1321 if (pid < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001322 perror_msg_and_die("fork");
Denys Vlasenko5d645812011-08-20 12:48:18 +02001323 if (pid == 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001324 pid = getpid();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001325 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001326 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1327 __func__);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001328 kill_save_errno(pid, SIGSTOP);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001329 if (fork() < 0)
1330 perror_msg_and_die("fork");
1331 _exit(0);
Wang Chaob13c0de2010-11-12 17:25:19 +08001332 }
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001333
1334 while (1) {
1335 int status, tracee_pid;
1336
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001337 errno = 0;
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001338 tracee_pid = wait(&status);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001339 if (tracee_pid <= 0) {
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001340 if (errno == EINTR)
1341 continue;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001342 if (errno == ECHILD)
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001343 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001344 kill_save_errno(pid, SIGKILL);
1345 perror_msg_and_die("%s: unexpected wait result %d",
1346 __func__, tracee_pid);
1347 }
1348 if (WIFEXITED(status)) {
1349 if (WEXITSTATUS(status)) {
1350 if (tracee_pid != pid)
1351 kill_save_errno(pid, SIGKILL);
1352 error_msg_and_die("%s: unexpected exit status %u",
1353 __func__, WEXITSTATUS(status));
1354 }
1355 continue;
1356 }
1357 if (WIFSIGNALED(status)) {
1358 if (tracee_pid != pid)
1359 kill_save_errno(pid, SIGKILL);
1360 error_msg_and_die("%s: unexpected signal %u",
1361 __func__, WTERMSIG(status));
1362 }
1363 if (!WIFSTOPPED(status)) {
1364 if (tracee_pid != pid)
1365 kill_save_errno(tracee_pid, SIGKILL);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001366 kill_save_errno(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001367 error_msg_and_die("%s: unexpected wait status %x",
1368 __func__, status);
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001369 }
1370 if (tracee_pid != pid) {
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001371 found_grandchild = tracee_pid;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001372 if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0) {
1373 kill_save_errno(tracee_pid, SIGKILL);
1374 kill_save_errno(pid, SIGKILL);
1375 perror_msg_and_die("PTRACE_CONT doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001376 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001377 continue;
1378 }
1379 switch (WSTOPSIG(status)) {
1380 case SIGSTOP:
1381 if (ptrace(PTRACE_SETOPTIONS, pid, 0, test_options) < 0
1382 && errno != EINVAL && errno != EIO)
1383 perror_msg("PTRACE_SETOPTIONS");
1384 break;
1385 case SIGTRAP:
1386 if (status >> 16 == PTRACE_EVENT_FORK) {
1387 long msg = 0;
1388
1389 if (ptrace(PTRACE_GETEVENTMSG, pid,
1390 NULL, (long) &msg) == 0)
1391 expected_grandchild = msg;
1392 }
1393 break;
1394 }
1395 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) {
1396 kill_save_errno(pid, SIGKILL);
1397 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001398 }
1399 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001400 if (expected_grandchild && expected_grandchild == found_grandchild) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001401 worked:
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001402 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001403 if (debug_flag)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001404 fprintf(stderr, "ptrace_setoptions = %#x\n",
1405 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001406 return 0;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001407 }
1408 error_msg("Test for PTRACE_O_TRACECLONE failed, "
1409 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001410 return 1;
Wang Chaob13c0de2010-11-12 17:25:19 +08001411}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001412
1413/*
1414 * Test whether the kernel support PTRACE_O_TRACESYSGOOD.
1415 * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it,
1416 * and then see whether it will stop with (SIGTRAP | 0x80).
1417 *
1418 * Use of this option enables correct handling of user-generated SIGTRAPs,
1419 * and SIGTRAPs generated by special instructions such as int3 on x86:
Denys Vlasenko329fa392014-04-10 09:57:17 +02001420
1421# compile with: gcc -nostartfiles -nostdlib -o int3 int3.S
1422_start: .globl _start
1423 int3
1424 movl $42, %ebx
1425 movl $1, %eax
1426 int $0x80
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001427 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001428static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001429test_ptrace_setoptions_for_all(void)
1430{
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001431 const unsigned int test_options = PTRACE_O_TRACESYSGOOD |
1432 PTRACE_O_TRACEEXEC;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001433 int pid;
1434 int it_worked = 0;
1435
Denys Vlasenko05f32512013-02-26 12:00:34 +01001436 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001437 if (NOMMU_SYSTEM)
Denys Vlasenko05f32512013-02-26 12:00:34 +01001438 goto worked; /* be bold, and pretend that test succeeded */
Mike Frysinger7ff5ed92012-04-05 01:52:25 -04001439
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001440 pid = fork();
1441 if (pid < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001442 perror_msg_and_die("fork");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001443
1444 if (pid == 0) {
1445 pid = getpid();
1446 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001447 /* Note: exits with exitcode 1 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001448 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1449 __func__);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001450 kill(pid, SIGSTOP);
1451 _exit(0); /* parent should see entry into this syscall */
1452 }
1453
1454 while (1) {
1455 int status, tracee_pid;
1456
1457 errno = 0;
1458 tracee_pid = wait(&status);
1459 if (tracee_pid <= 0) {
1460 if (errno == EINTR)
1461 continue;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001462 kill_save_errno(pid, SIGKILL);
1463 perror_msg_and_die("%s: unexpected wait result %d",
1464 __func__, tracee_pid);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001465 }
Denys Vlasenko75422762011-05-27 14:36:01 +02001466 if (WIFEXITED(status)) {
1467 if (WEXITSTATUS(status) == 0)
1468 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001469 error_msg_and_die("%s: unexpected exit status %u",
1470 __func__, WEXITSTATUS(status));
1471 }
1472 if (WIFSIGNALED(status)) {
1473 error_msg_and_die("%s: unexpected signal %u",
1474 __func__, WTERMSIG(status));
Denys Vlasenko75422762011-05-27 14:36:01 +02001475 }
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001476 if (!WIFSTOPPED(status)) {
1477 kill(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001478 error_msg_and_die("%s: unexpected wait status %x",
1479 __func__, status);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001480 }
1481 if (WSTOPSIG(status) == SIGSTOP) {
1482 /*
1483 * We don't check "options aren't accepted" error.
1484 * If it happens, we'll never get (SIGTRAP | 0x80),
1485 * and thus will decide to not use the option.
1486 * IOW: the outcome of the test will be correct.
1487 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001488 if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0
1489 && errno != EINVAL && errno != EIO)
1490 perror_msg("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001491 }
1492 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
1493 it_worked = 1;
1494 }
1495 if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001496 kill_save_errno(pid, SIGKILL);
Denys Vlasenko75422762011-05-27 14:36:01 +02001497 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001498 }
1499 }
1500
1501 if (it_worked) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001502 worked:
Denys Vlasenko75422762011-05-27 14:36:01 +02001503 syscall_trap_sig = (SIGTRAP | 0x80);
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001504 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001505 if (debug_flag)
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001506 fprintf(stderr, "ptrace_setoptions = %#x\n",
1507 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001508 return 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001509 }
1510
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001511 error_msg("Test for PTRACE_O_TRACESYSGOOD failed, "
1512 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001513 return 1;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001514}
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001515
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001516#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001517static void
1518test_ptrace_seize(void)
1519{
1520 int pid;
1521
Denys Vlasenko05f32512013-02-26 12:00:34 +01001522 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001523 if (NOMMU_SYSTEM) {
1524 post_attach_sigstop = 0; /* this sets use_seize to 1 */
1525 return;
1526 }
1527
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001528 pid = fork();
1529 if (pid < 0)
1530 perror_msg_and_die("fork");
1531
1532 if (pid == 0) {
1533 pause();
1534 _exit(0);
1535 }
1536
1537 /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After
1538 * attaching tracee continues to run unless a trap condition occurs.
1539 * PTRACE_SEIZE doesn't affect signal or group stop state.
1540 */
Denys Vlasenko26bc0602012-07-10 16:36:32 +02001541 if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001542 post_attach_sigstop = 0; /* this sets use_seize to 1 */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001543 } else if (debug_flag) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001544 fprintf(stderr, "PTRACE_SEIZE doesn't work\n");
1545 }
1546
1547 kill(pid, SIGKILL);
1548
1549 while (1) {
1550 int status, tracee_pid;
1551
1552 errno = 0;
1553 tracee_pid = waitpid(pid, &status, 0);
1554 if (tracee_pid <= 0) {
1555 if (errno == EINTR)
1556 continue;
1557 perror_msg_and_die("%s: unexpected wait result %d",
1558 __func__, tracee_pid);
1559 }
1560 if (WIFSIGNALED(status)) {
1561 return;
1562 }
1563 error_msg_and_die("%s: unexpected wait status %x",
1564 __func__, status);
1565 }
1566}
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001567#else /* !USE_SEIZE */
1568# define test_ptrace_seize() ((void)0)
1569#endif
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001570
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001571static unsigned
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001572get_os_release(void)
1573{
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001574 unsigned rel;
1575 const char *p;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001576 struct utsname u;
1577 if (uname(&u) < 0)
1578 perror_msg_and_die("uname");
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001579 /* u.release has this form: "3.2.9[-some-garbage]" */
1580 rel = 0;
1581 p = u.release;
1582 for (;;) {
1583 if (!(*p >= '0' && *p <= '9'))
1584 error_msg_and_die("Bad OS release string: '%s'", u.release);
1585 /* Note: this open-codes KERNEL_VERSION(): */
1586 rel = (rel << 8) | atoi(p);
1587 if (rel >= KERNEL_VERSION(1,0,0))
1588 break;
1589 while (*p >= '0' && *p <= '9')
1590 p++;
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001591 if (*p != '.') {
1592 if (rel >= KERNEL_VERSION(0,1,0)) {
1593 /* "X.Y-something" means "X.Y.0" */
1594 rel <<= 8;
1595 break;
1596 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001597 error_msg_and_die("Bad OS release string: '%s'", u.release);
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001598 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001599 p++;
1600 }
1601 return rel;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001602}
1603
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01001604/*
1605 * Initialization part of main() was eating much stack (~0.5k),
1606 * which was unused after init.
1607 * We can reuse it if we move init code into a separate function.
1608 *
1609 * Don't want main() to inline us and defeat the reason
1610 * we have a separate function.
1611 */
1612static void __attribute__ ((noinline))
1613init(int argc, char *argv[])
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001614{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001615 struct tcb *tcp;
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001616 int c, i;
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001617 int optF = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001618 struct sigaction sa;
1619
Dmitry V. Levin08b623e2007-10-08 21:04:41 +00001620 progname = argv[0] ? argv[0] : "strace";
1621
Denys Vlasenkoa5090542012-03-15 17:27:49 +01001622 /* Make sure SIGCHLD has the default action so that waitpid
1623 definitely works without losing track of children. The user
1624 should not have given us a bogus state to inherit, but he might
1625 have. Arguably we should detect SIG_IGN here and pass it on
1626 to children, but probably noone really needs that. */
1627 signal(SIGCHLD, SIG_DFL);
1628
Denys Vlasenko75422762011-05-27 14:36:01 +02001629 strace_tracer_pid = getpid();
1630
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001631 os_release = get_os_release();
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001632
Roland McGrathee9d4352002-12-18 04:16:10 +00001633 /* Allocate the initial tcbtab. */
1634 tcbtabsize = argc; /* Surely enough for all -p args. */
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001635 tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0]));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001636 if (!tcbtab)
1637 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001638 tcp = calloc(tcbtabsize, sizeof(*tcp));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001639 if (!tcp)
1640 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001641 for (c = 0; c < tcbtabsize; c++)
1642 tcbtab[c] = tcp++;
Roland McGrathee9d4352002-12-18 04:16:10 +00001643
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001644 shared_log = stderr;
Roland McGrath138c6a32006-01-12 09:50:49 +00001645 set_sortby(DEFAULT_SORTBY);
1646 set_personality(DEFAULT_PERSONALITY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001647 qualify("trace=all");
1648 qualify("abbrev=all");
1649 qualify("verbose=all");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001650#if DEFAULT_QUAL_FLAGS != (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
1651# error Bug in DEFAULT_QUAL_FLAGS
1652#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001653 qualify("signal=all");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001654 while ((c = getopt(argc, argv,
Mark Hillse53bf232014-05-28 17:52:40 +01001655 "+b:cCdfFhiqrtTvVwxyz"
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001656 "D"
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001657 "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001658 switch (c) {
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001659 case 'b':
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001660 if (strcmp(optarg, "execve") != 0)
1661 error_msg_and_die("Syscall '%s' for -b isn't supported",
1662 optarg);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001663 detach_on_execve = 1;
1664 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001665 case 'c':
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001666 if (cflag == CFLAG_BOTH) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001667 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001668 }
1669 cflag = CFLAG_ONLY_STATS;
1670 break;
1671 case 'C':
1672 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001673 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001674 }
1675 cflag = CFLAG_BOTH;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001676 break;
1677 case 'd':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001678 debug_flag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001679 break;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001680 case 'D':
1681 daemonized_tracer = 1;
1682 break;
Roland McGrath41c48222008-07-18 00:25:10 +00001683 case 'F':
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001684 optF = 1;
1685 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001686 case 'f':
1687 followfork++;
1688 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001689 case 'h':
1690 usage(stdout, 0);
1691 break;
1692 case 'i':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001693 iflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001694 break;
1695 case 'q':
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01001696 qflag++;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001697 break;
1698 case 'r':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001699 rflag = 1;
1700 /* fall through to tflag++ */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001701 case 't':
1702 tflag++;
1703 break;
1704 case 'T':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001705 Tflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001706 break;
Mark Hillse53bf232014-05-28 17:52:40 +01001707 case 'w':
1708 count_wallclock = 1;
1709 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001710 case 'x':
1711 xflag++;
1712 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001713 case 'y':
1714 show_fd_path = 1;
1715 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001716 case 'v':
1717 qualify("abbrev=none");
1718 break;
1719 case 'V':
Roland McGrath9c9a2532003-02-20 02:56:29 +00001720 printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001721 exit(0);
1722 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001723 case 'z':
1724 not_failing_only = 1;
1725 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001726 case 'a':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001727 acolumn = string_to_uint(optarg);
Denys Vlasenko102ec492011-08-25 01:27:59 +02001728 if (acolumn < 0)
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001729 error_opt_arg(c, optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001730 break;
1731 case 'e':
1732 qualify(optarg);
1733 break;
1734 case 'o':
1735 outfname = strdup(optarg);
1736 break;
1737 case 'O':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001738 i = string_to_uint(optarg);
1739 if (i < 0)
1740 error_opt_arg(c, optarg);
1741 set_overhead(i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001742 break;
1743 case 'p':
Denys Vlasenkoe8172b72012-03-09 13:01:04 +01001744 process_opt_p_list(optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001745 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001746 case 'P':
Denys Vlasenko7239dbc2013-03-05 15:46:34 +01001747 pathtrace_select(optarg);
Grant Edwards8a082772011-04-07 20:25:40 +00001748 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001749 case 's':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001750 i = string_to_uint(optarg);
1751 if (i < 0)
1752 error_opt_arg(c, optarg);
1753 max_strlen = i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001754 break;
1755 case 'S':
1756 set_sortby(optarg);
1757 break;
1758 case 'u':
1759 username = strdup(optarg);
1760 break;
Roland McGrathde6e5332003-01-24 04:31:23 +00001761 case 'E':
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001762 if (putenv(optarg) < 0)
1763 die_out_of_memory();
Roland McGrathde6e5332003-01-24 04:31:23 +00001764 break;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001765 case 'I':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001766 opt_intr = string_to_uint(optarg);
1767 if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS)
1768 error_opt_arg(c, optarg);
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001769 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001770 default:
1771 usage(stderr, 1);
1772 break;
1773 }
1774 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001775 argv += optind;
1776 /* argc -= optind; - no need, argc is not used below */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001777
Denys Vlasenko102ec492011-08-25 01:27:59 +02001778 acolumn_spaces = malloc(acolumn + 1);
1779 if (!acolumn_spaces)
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001780 die_out_of_memory();
Denys Vlasenko102ec492011-08-25 01:27:59 +02001781 memset(acolumn_spaces, ' ', acolumn);
1782 acolumn_spaces[acolumn] = '\0';
1783
Denys Vlasenko837399a2012-01-24 11:37:03 +01001784 /* Must have PROG [ARGS], or -p PID. Not both. */
Denys Vlasenkofd883382012-03-09 13:03:41 +01001785 if (!argv[0] == !nprocs)
Roland McGrathce0d1542003-11-11 21:24:23 +00001786 usage(stderr, 1);
1787
Denys Vlasenkofd883382012-03-09 13:03:41 +01001788 if (nprocs != 0 && daemonized_tracer) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001789 error_msg_and_die("-D and -p are mutually exclusive");
Wang Chaod322a4b2010-08-05 14:30:11 +08001790 }
1791
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001792 if (!followfork)
1793 followfork = optF;
1794
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001795 if (followfork >= 2 && cflag) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001796 error_msg_and_die("(-c or -C) and -ff are mutually exclusive");
Roland McGrathcb9def62006-04-25 07:48:03 +00001797 }
1798
Mark Hillse53bf232014-05-28 17:52:40 +01001799 if (count_wallclock && !cflag) {
1800 error_msg_and_die("-w must be given with (-c or -C)");
1801 }
1802
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001803 /* See if they want to run as another user. */
1804 if (username != NULL) {
1805 struct passwd *pent;
1806
1807 if (getuid() != 0 || geteuid() != 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001808 error_msg_and_die("You must be root to use the -u option");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001809 }
Denys Vlasenko5d645812011-08-20 12:48:18 +02001810 pent = getpwnam(username);
1811 if (pent == NULL) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001812 error_msg_and_die("Cannot find user '%s'", username);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001813 }
1814 run_uid = pent->pw_uid;
1815 run_gid = pent->pw_gid;
1816 }
1817 else {
1818 run_uid = getuid();
1819 run_gid = getgid();
1820 }
1821
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001822 /*
1823 * On any reasonably recent Linux kernel (circa about 2.5.46)
1824 * need_fork_exec_workarounds should stay 0 after these tests:
1825 */
1826 /*need_fork_exec_workarounds = 0; - already is */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001827 if (followfork)
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001828 need_fork_exec_workarounds = test_ptrace_setoptions_followfork();
1829 need_fork_exec_workarounds |= test_ptrace_setoptions_for_all();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001830 test_ptrace_seize();
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001831
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001832 /* Check if they want to redirect the output. */
1833 if (outfname) {
Roland McGrath37b9a662003-11-07 02:26:54 +00001834 /* See if they want to pipe the output. */
1835 if (outfname[0] == '|' || outfname[0] == '!') {
1836 /*
1837 * We can't do the <outfname>.PID funny business
1838 * when using popen, so prohibit it.
1839 */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001840 if (followfork >= 2)
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001841 error_msg_and_die("Piping the output and -ff are mutually exclusive");
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001842 shared_log = strace_popen(outfname + 1);
Roland McGrath37b9a662003-11-07 02:26:54 +00001843 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001844 else if (followfork < 2)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001845 shared_log = strace_fopen(outfname);
Denys Vlasenko328bf252012-03-12 23:34:13 +01001846 } else {
1847 /* -ff without -o FILE is the same as single -f */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001848 if (followfork >= 2)
Denys Vlasenko328bf252012-03-12 23:34:13 +01001849 followfork = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001850 }
1851
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001852 if (!outfname || outfname[0] == '|' || outfname[0] == '!') {
Denys Vlasenkoa677da52012-01-24 11:31:51 +01001853 char *buf = malloc(BUFSIZ);
1854 if (!buf)
1855 die_out_of_memory();
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001856 setvbuf(shared_log, buf, _IOLBF, BUFSIZ);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001857 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001858 if (outfname && argv[0]) {
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001859 if (!opt_intr)
1860 opt_intr = INTR_NEVER;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001861 qflag = 1;
Roland McGrath36931052003-06-03 01:35:20 +00001862 }
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001863 if (!opt_intr)
1864 opt_intr = INTR_WHILE_WAIT;
Wang Chaob13c0de2010-11-12 17:25:19 +08001865
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001866 /* argv[0] -pPID -oFILE Default interactive setting
1867 * yes 0 0 INTR_WHILE_WAIT
1868 * no 1 0 INTR_WHILE_WAIT
1869 * yes 0 1 INTR_NEVER
1870 * no 1 1 INTR_WHILE_WAIT
Roland McGrath54cc1c82007-11-03 23:34:11 +00001871 */
1872
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001873 sigemptyset(&empty_set);
1874 sigemptyset(&blocked_set);
1875
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001876 /* startup_child() must be called before the signal handlers get
1877 * installed below as they are inherited into the spawned process.
1878 * Also we do not need to be protected by them as during interruption
1879 * in the startup_child() mode we kill the spawned process anyway.
1880 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001881 if (argv[0]) {
Dmitry V. Levin1d2435b2013-05-14 22:35:46 +00001882 if (!NOMMU_SYSTEM || daemonized_tracer)
1883 hide_log_until_execve = 1;
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001884 skip_one_b_execve = 1;
Denys Vlasenko837399a2012-01-24 11:37:03 +01001885 startup_child(argv);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001886 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001887
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001888 sa.sa_handler = SIG_IGN;
1889 sigemptyset(&sa.sa_mask);
1890 sa.sa_flags = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001891 sigaction(SIGTTOU, &sa, NULL); /* SIG_IGN */
1892 sigaction(SIGTTIN, &sa, NULL); /* SIG_IGN */
1893 if (opt_intr != INTR_ANYWHERE) {
1894 if (opt_intr == INTR_BLOCK_TSTP_TOO)
1895 sigaction(SIGTSTP, &sa, NULL); /* SIG_IGN */
1896 /*
1897 * In interactive mode (if no -o OUTFILE, or -p PID is used),
1898 * fatal signals are blocked while syscall stop is processed,
1899 * and acted on in between, when waiting for new syscall stops.
1900 * In non-interactive mode, signals are ignored.
1901 */
1902 if (opt_intr == INTR_WHILE_WAIT) {
1903 sigaddset(&blocked_set, SIGHUP);
1904 sigaddset(&blocked_set, SIGINT);
1905 sigaddset(&blocked_set, SIGQUIT);
1906 sigaddset(&blocked_set, SIGPIPE);
1907 sigaddset(&blocked_set, SIGTERM);
1908 sa.sa_handler = interrupt;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001909 }
1910 /* SIG_IGN, or set handler for these */
1911 sigaction(SIGHUP, &sa, NULL);
1912 sigaction(SIGINT, &sa, NULL);
1913 sigaction(SIGQUIT, &sa, NULL);
1914 sigaction(SIGPIPE, &sa, NULL);
1915 sigaction(SIGTERM, &sa, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001916 }
Denys Vlasenkofd883382012-03-09 13:03:41 +01001917 if (nprocs != 0 || daemonized_tracer)
Roland McGrath02203312007-06-11 22:06:31 +00001918 startup_attach();
Roland McGrath02203312007-06-11 22:06:31 +00001919
Denys Vlasenkofd883382012-03-09 13:03:41 +01001920 /* Do we want pids printed in our -o OUTFILE?
1921 * -ff: no (every pid has its own file); or
1922 * -f: yes (there can be more pids in the future); or
1923 * -p PID1,PID2: yes (there are already more than one pid)
1924 */
1925 print_pid_pfx = (outfname && followfork < 2 && (followfork == 1 || nprocs > 1));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001926}
1927
Denys Vlasenkoeebb04d2012-01-27 15:24:48 +01001928static struct tcb *
Roland McGrath54e931f2010-09-14 18:59:20 -07001929pid2tcb(int pid)
1930{
1931 int i;
1932
1933 if (pid <= 0)
1934 return NULL;
1935
1936 for (i = 0; i < tcbtabsize; i++) {
1937 struct tcb *tcp = tcbtab[i];
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001938 if (tcp->pid == pid)
Roland McGrath54e931f2010-09-14 18:59:20 -07001939 return tcp;
1940 }
1941
1942 return NULL;
1943}
1944
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001945static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001946cleanup(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001947{
1948 int i;
1949 struct tcb *tcp;
Denys Vlasenko35218842012-01-29 21:17:56 +01001950 int fatal_sig;
1951
1952 /* 'interrupted' is a volatile object, fetch it only once */
1953 fatal_sig = interrupted;
1954 if (!fatal_sig)
1955 fatal_sig = SIGTERM;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001956
Roland McGrathee9d4352002-12-18 04:16:10 +00001957 for (i = 0; i < tcbtabsize; i++) {
1958 tcp = tcbtab[i];
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001959 if (!tcp->pid)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001960 continue;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001961 if (debug_flag)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001962 fprintf(stderr,
1963 "cleanup: looking at pid %u\n", tcp->pid);
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001964 if (tcp->pid == strace_child) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001965 kill(tcp->pid, SIGCONT);
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001966 kill(tcp->pid, fatal_sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001967 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001968 detach(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001969 }
1970 if (cflag)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001971 call_summary(shared_log);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001972}
1973
1974static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001975interrupt(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001976{
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001977 interrupted = sig;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001978}
1979
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02001980static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001981trace(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001982{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001983 struct rusage ru;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001984
Denys Vlasenkod2e1f422013-07-08 11:28:27 +02001985 /* Used to be "while (nprocs != 0)", but in this testcase:
1986 * int main() { _exit(!!fork()); }
1987 * under strace -f, parent sometimes (rarely) manages
1988 * to exit before we see the first stop of the child,
1989 * and we are losing track of it:
1990 * 19923 clone(...) = 19924
1991 * 19923 exit_group(1) = ?
1992 * 19923 +++ exited with 1 +++
1993 * Waiting for ECHILD works better.
Denys Vlasenko7c41ce22013-07-08 13:55:04 +02001994 * (However, if -o|logger is in use, we can't do that.
1995 * Can work around that by double-forking the logger,
1996 * but that loses the ability to wait for its completion on exit.
1997 * Oh well...)
Denys Vlasenkod2e1f422013-07-08 11:28:27 +02001998 */
1999 while (1) {
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002000 int pid;
2001 int wait_errno;
2002 int status, sig;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002003 int stopped;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002004 struct tcb *tcp;
2005 unsigned event;
2006
Denys Vlasenko222713a2009-03-17 14:29:59 +00002007 if (interrupted)
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002008 return;
Denys Vlasenko725dd422013-06-20 11:06:58 +02002009
Denys Vlasenko7c41ce22013-07-08 13:55:04 +02002010 if (popen_pid != 0 && nprocs == 0)
2011 return;
2012
Roland McGratheb9e2e82009-06-02 16:49:22 -07002013 if (interactive)
2014 sigprocmask(SIG_SETMASK, &empty_set, NULL);
Denys Vlasenko725dd422013-06-20 11:06:58 +02002015 pid = wait4(-1, &status, __WALL, (cflag ? &ru : NULL));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002016 wait_errno = errno;
Roland McGratheb9e2e82009-06-02 16:49:22 -07002017 if (interactive)
2018 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002019
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02002020 if (pid < 0) {
Denys Vlasenko725dd422013-06-20 11:06:58 +02002021 if (wait_errno == EINTR)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002022 continue;
Denys Vlasenkod2e1f422013-07-08 11:28:27 +02002023 if (nprocs == 0 && wait_errno == ECHILD)
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002024 return;
Denys Vlasenkod2e1f422013-07-08 11:28:27 +02002025 /* If nprocs > 0, ECHILD is not expected,
2026 * treat it as any other error here:
2027 */
Denys Vlasenko725dd422013-06-20 11:06:58 +02002028 errno = wait_errno;
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002029 perror_msg_and_die("wait4(__WALL)");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002030 }
Denys Vlasenko725dd422013-06-20 11:06:58 +02002031
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00002032 if (pid == popen_pid) {
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002033 if (!WIFSTOPPED(status))
Denys Vlasenko7dd23382011-06-22 13:03:56 +02002034 popen_pid = 0;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00002035 continue;
2036 }
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002037
2038 event = ((unsigned)status >> 16);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002039 if (debug_flag) {
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002040 char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko1b2bfbc2013-06-21 16:41:50 +02002041 char evbuf[sizeof(",EVENT_VFORK_DONE (%u)") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002042 strcpy(buf, "???");
2043 if (WIFSIGNALED(status))
2044#ifdef WCOREDUMP
2045 sprintf(buf, "WIFSIGNALED,%ssig=%s",
2046 WCOREDUMP(status) ? "core," : "",
2047 signame(WTERMSIG(status)));
2048#else
2049 sprintf(buf, "WIFSIGNALED,sig=%s",
2050 signame(WTERMSIG(status)));
2051#endif
2052 if (WIFEXITED(status))
2053 sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status));
2054 if (WIFSTOPPED(status))
2055 sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status)));
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02002056#ifdef WIFCONTINUED
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002057 /* Should never be seen */
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002058 if (WIFCONTINUED(status))
2059 strcpy(buf, "WIFCONTINUED");
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02002060#endif
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002061 evbuf[0] = '\0';
2062 if (event != 0) {
2063 static const char *const event_names[] = {
2064 [PTRACE_EVENT_CLONE] = "CLONE",
2065 [PTRACE_EVENT_FORK] = "FORK",
2066 [PTRACE_EVENT_VFORK] = "VFORK",
2067 [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE",
2068 [PTRACE_EVENT_EXEC] = "EXEC",
2069 [PTRACE_EVENT_EXIT] = "EXIT",
Denys Vlasenko4e020c02013-06-21 16:33:56 +02002070 /* [PTRACE_EVENT_STOP (=128)] would make biggish array */
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002071 };
Denys Vlasenko1b2bfbc2013-06-21 16:41:50 +02002072 const char *e = "??";
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002073 if (event < ARRAY_SIZE(event_names))
2074 e = event_names[event];
Denys Vlasenko4e020c02013-06-21 16:33:56 +02002075 else if (event == PTRACE_EVENT_STOP)
2076 e = "STOP";
Denys Vlasenko1b2bfbc2013-06-21 16:41:50 +02002077 sprintf(evbuf, ",EVENT_%s (%u)", e, event);
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002078 }
Denys Vlasenko38eab5d2013-07-02 12:18:22 +02002079 fprintf(stderr, " [wait(0x%06x) = %u] %s%s\n", status, pid, buf, evbuf);
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002080 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002081
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002082 /* Look up 'pid' in our table. */
Denys Vlasenko5d645812011-08-20 12:48:18 +02002083 tcp = pid2tcb(pid);
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002084
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002085 if (!tcp) {
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002086 if (!WIFSTOPPED(status)) {
2087 /* This can happen if we inherited
2088 * an unknown child. Example:
2089 * (sleep 1 & exec strace sleep 2)
2090 */
2091 error_msg("Exit of unknown pid %u seen", pid);
2092 continue;
2093 }
Roland McGrath41c48222008-07-18 00:25:10 +00002094 if (followfork) {
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002095 /* We assume it's a fork/vfork/clone child */
Denys Vlasenko418d66a2009-01-17 01:52:54 +00002096 tcp = alloctcb(pid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002097 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01002098 newoutf(tcp);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002099 if (!qflag)
Denys Vlasenko833fb132011-08-17 11:30:56 +02002100 fprintf(stderr, "Process %d attached\n",
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002101 pid);
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002102 } else {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002103 /* This can happen if a clone call used
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002104 * CLONE_PTRACE itself.
Denys Vlasenko71d3d292013-06-21 16:19:46 +02002105 */
Denys Vlasenkoc8511f02013-06-26 14:29:19 +02002106 ptrace(PTRACE_CONT, pid, (char *) 0, 0);
2107 error_msg("Stop of unknown pid %u seen, PTRACE_CONTed it", pid);
Denys Vlasenko71d3d292013-06-21 16:19:46 +02002108 continue;
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002109 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002110 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002111
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002112 clear_regs();
2113 if (WIFSTOPPED(status))
2114 get_regs(pid);
2115
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002116 /* Under Linux, execve changes pid to thread leader's pid,
2117 * and we see this changed pid on EVENT_EXEC and later,
2118 * execve sysexit. Leader "disappears" without exit
2119 * notification. Let user know that, drop leader's tcb,
2120 * and fix up pid in execve thread's tcb.
2121 * Effectively, execve thread's tcb replaces leader's tcb.
2122 *
2123 * BTW, leader is 'stuck undead' (doesn't report WIFEXITED
2124 * on exit syscall) in multithreaded programs exactly
2125 * in order to handle this case.
2126 *
2127 * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0.
2128 * On 2.6 and earlier, it can return garbage.
2129 */
2130 if (event == PTRACE_EVENT_EXEC && os_release >= KERNEL_VERSION(3,0,0)) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002131 FILE *fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002132 struct tcb *execve_thread;
2133 long old_pid = 0;
2134
2135 if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) < 0)
2136 goto dont_switch_tcbs;
Denys Vlasenko6162a3f2013-07-04 09:26:24 +02002137 /* Avoid truncation in pid2tcb() param passing */
2138 if (old_pid > UINT_MAX)
2139 goto dont_switch_tcbs;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002140 if (old_pid <= 0 || old_pid == pid)
2141 goto dont_switch_tcbs;
2142 execve_thread = pid2tcb(old_pid);
2143 /* It should be !NULL, but I feel paranoid */
2144 if (!execve_thread)
2145 goto dont_switch_tcbs;
2146
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002147 if (execve_thread->curcol != 0) {
2148 /*
2149 * One case we are here is -ff:
2150 * try "strace -oLOG -ff test/threaded_execve"
2151 */
2152 fprintf(execve_thread->outf, " <pid changed to %d ...>\n", pid);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002153 /*execve_thread->curcol = 0; - no need, see code below */
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002154 }
2155 /* Swap output FILEs (needed for -ff) */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002156 fp = execve_thread->outf;
2157 execve_thread->outf = tcp->outf;
2158 tcp->outf = fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002159 /* And their column positions */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002160 execve_thread->curcol = tcp->curcol;
2161 tcp->curcol = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002162 /* Drop leader, but close execve'd thread outfile (if -ff) */
2163 droptcb(tcp);
2164 /* Switch to the thread, reusing leader's outfile and pid */
2165 tcp = execve_thread;
2166 tcp->pid = pid;
2167 if (cflag != CFLAG_ONLY_STATS) {
2168 printleader(tcp);
2169 tprintf("+++ superseded by execve in pid %lu +++\n", old_pid);
2170 line_ended();
2171 tcp->flags |= TCB_REPRINT;
2172 }
2173 }
2174 dont_switch_tcbs:
2175
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002176 if (event == PTRACE_EVENT_EXEC) {
2177 if (detach_on_execve && !skip_one_b_execve)
2178 detach(tcp); /* do "-b execve" thingy */
2179 skip_one_b_execve = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002180 }
2181
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002182 /* Set current output file */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002183 current_tcp = tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002184
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002185 if (cflag) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002186 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2187 tcp->stime = ru.ru_stime;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002188 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002189
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002190 if (WIFSIGNALED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002191 if (pid == strace_child)
2192 exit_code = 0x100 | WTERMSIG(status);
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002193 if (cflag != CFLAG_ONLY_STATS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01002194 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)
2195 ) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002196 printleader(tcp);
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002197#ifdef WCOREDUMP
Denys Vlasenko000b6012012-01-28 01:25:03 +01002198 tprintf("+++ killed by %s %s+++\n",
Roland McGrath2efe8792004-01-13 09:59:45 +00002199 signame(WTERMSIG(status)),
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002200 WCOREDUMP(status) ? "(core dumped) " : "");
2201#else
Denys Vlasenko000b6012012-01-28 01:25:03 +01002202 tprintf("+++ killed by %s +++\n",
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002203 signame(WTERMSIG(status)));
Roland McGrath2efe8792004-01-13 09:59:45 +00002204#endif
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002205 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002206 }
2207 droptcb(tcp);
2208 continue;
2209 }
2210 if (WIFEXITED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002211 if (pid == strace_child)
2212 exit_code = WEXITSTATUS(status);
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01002213 if (cflag != CFLAG_ONLY_STATS &&
2214 qflag < 2) {
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002215 printleader(tcp);
Denys Vlasenko000b6012012-01-28 01:25:03 +01002216 tprintf("+++ exited with %d +++\n", WEXITSTATUS(status));
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002217 line_ended();
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002218 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002219 droptcb(tcp);
2220 continue;
2221 }
2222 if (!WIFSTOPPED(status)) {
2223 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
2224 droptcb(tcp);
2225 continue;
2226 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002227
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002228 /* Is this the very first time we see this tracee stopped? */
2229 if (tcp->flags & TCB_STARTUP) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002230 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002231 fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n", tcp->pid);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002232 tcp->flags &= ~TCB_STARTUP;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002233 if (tcp->flags & TCB_BPTSET) {
Roland McGrath02203312007-06-11 22:06:31 +00002234 /*
2235 * One example is a breakpoint inherited from
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002236 * parent through fork().
Roland McGrath02203312007-06-11 22:06:31 +00002237 */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002238 if (clearbpt(tcp) < 0) {
2239 /* Pretty fatal */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002240 droptcb(tcp);
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002241 exit_code = 1;
2242 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002243 }
2244 }
Denys Vlasenkoc169d942013-07-10 14:33:05 +02002245 if (!use_seize && ptrace_setoptions) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002246 if (debug_flag)
Denys Vlasenkoc169d942013-07-10 14:33:05 +02002247 fprintf(stderr, "setting opts 0x%x on pid %d\n", ptrace_setoptions, tcp->pid);
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002248 if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) {
2249 if (errno != ESRCH) {
2250 /* Should never happen, really */
2251 perror_msg_and_die("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02002252 }
2253 }
2254 }
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002255 }
2256
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002257 sig = WSTOPSIG(status);
2258
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002259 if (event != 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002260 /* Ptrace event */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002261#if USE_SEIZE
Denys Vlasenko26bc0602012-07-10 16:36:32 +02002262 if (event == PTRACE_EVENT_STOP) {
Denys Vlasenko67038162012-01-29 16:46:46 +01002263 /*
2264 * PTRACE_INTERRUPT-stop or group-stop.
2265 * PTRACE_INTERRUPT-stop has sig == SIGTRAP here.
2266 */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002267 if (sig == SIGSTOP
2268 || sig == SIGTSTP
2269 || sig == SIGTTIN
2270 || sig == SIGTTOU
2271 ) {
2272 stopped = 1;
2273 goto show_stopsig;
2274 }
2275 }
2276#endif
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002277 goto restart_tracee_with_sig_0;
2278 }
2279
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002280 /* Is this post-attach SIGSTOP?
2281 * Interestingly, the process may stop
2282 * with STOPSIG equal to some other signal
2283 * than SIGSTOP if we happend to attach
2284 * just before the process takes a signal.
2285 */
2286 if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002287 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002288 fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid);
2289 tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002290 goto restart_tracee_with_sig_0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002291 }
2292
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002293 if (sig != syscall_trap_sig) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002294 siginfo_t si;
2295
2296 /* Nonzero (true) if tracee is stopped by signal
2297 * (as opposed to "tracee received signal").
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002298 * TODO: shouldn't we check for errno == EINVAL too?
2299 * We can get ESRCH instead, you know...
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002300 */
2301 stopped = (ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002302#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002303 show_stopsig:
Denys Vlasenko67038162012-01-29 16:46:46 +01002304#endif
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002305 if (cflag != CFLAG_ONLY_STATS
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002306 && !hide_log_until_execve
2307 && (qual_flags[sig] & QUAL_SIGNAL)
2308 ) {
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002309 printleader(tcp);
2310 if (!stopped) {
2311 tprintf("--- %s ", signame(sig));
2312 printsiginfo(&si, verbose(tcp));
Denys Vlasenko5a2483b2013-07-01 12:49:14 +02002313 tprints(" ---\n");
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002314 } else
Denys Vlasenko5a2483b2013-07-01 12:49:14 +02002315 tprintf("--- stopped by %s ---\n",
2316 signame(sig));
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002317 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002318 }
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002319
2320 if (!stopped)
2321 /* It's signal-delivery-stop. Inject the signal */
2322 goto restart_tracee;
2323
2324 /* It's group-stop */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002325 if (use_seize) {
2326 /*
2327 * This ends ptrace-stop, but does *not* end group-stop.
2328 * This makes stopping signals work properly on straced process
2329 * (that is, process really stops. It used to continue to run).
2330 */
2331 if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) {
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002332 /* Note: ptrace_restart emitted error message */
2333 exit_code = 1;
2334 return;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002335 }
2336 continue;
2337 }
2338 /* We don't have PTRACE_LISTEN support... */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002339 goto restart_tracee;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002340 }
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002341
2342 /* We handled quick cases, we are permitted to interrupt now. */
Roland McGrath02203312007-06-11 22:06:31 +00002343 if (interrupted)
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002344 return;
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002345
2346 /* This should be syscall entry or exit.
2347 * (Or it still can be that pesky post-execve SIGTRAP!)
2348 * Handle it.
2349 */
Denys Vlasenko23506752012-03-21 10:32:49 +01002350 if (trace_syscall(tcp) < 0) {
2351 /* ptrace() failed in trace_syscall().
Roland McGratheb9e2e82009-06-02 16:49:22 -07002352 * Likely a result of process disappearing mid-flight.
Denys Vlasenko23506752012-03-21 10:32:49 +01002353 * Observed case: exit_group() or SIGKILL terminating
Denys Vlasenkof1e69032012-01-04 15:15:26 +01002354 * all processes in thread group.
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002355 * We assume that ptrace error was caused by process death.
Denys Vlasenkof2025022012-03-09 15:29:45 +01002356 * We used to detach(tcp) here, but since we no longer
2357 * implement "detach before death" policy/hack,
2358 * we can let this process to report its death to us
2359 * normally, via WIFEXITED or WIFSIGNALED wait status.
2360 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002361 continue;
2362 }
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002363 restart_tracee_with_sig_0:
2364 sig = 0;
2365 restart_tracee:
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002366 if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) {
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002367 /* Note: ptrace_restart emitted error message */
2368 exit_code = 1;
2369 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002370 }
Denys Vlasenko7c41ce22013-07-08 13:55:04 +02002371 } /* while (1) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002372}
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002373
2374int
2375main(int argc, char *argv[])
2376{
2377 init(argc, argv);
2378
2379 /* Run main tracing loop */
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002380 trace();
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002381
2382 cleanup();
2383 fflush(NULL);
Dmitry V. Levincf534362012-07-12 20:54:46 +00002384 if (shared_log != stderr)
2385 fclose(shared_log);
2386 if (popen_pid) {
2387 while (waitpid(popen_pid, NULL, 0) < 0 && errno == EINTR)
2388 ;
2389 }
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002390 if (exit_code > 0xff) {
2391 /* Avoid potential core file clobbering. */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00002392 struct_rlimit rlim = {0, 0};
2393 set_rlimit(RLIMIT_CORE, &rlim);
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002394
2395 /* Child was killed by a signal, mimic that. */
2396 exit_code &= 0xff;
2397 signal(exit_code, SIG_DFL);
2398 raise(exit_code);
2399 /* Paranoia - what if this signal is not fatal?
2400 Exit with 128 + signo then. */
2401 exit_code += 128;
2402 }
2403
2404 return exit_code;
2405}