blob: 9383625da8155e091cb75d19ebc2f3a45f56316b [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
Luca Clementi327064b2013-07-23 00:11:35 -070053#ifdef USE_LIBUNWIND
54/* if this is true do the stack trace for every system call */
55bool stack_trace_enabled = false;
56#endif
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010057
58#if defined __NR_tkill
59# define my_tkill(tid, sig) syscall(__NR_tkill, (tid), (sig))
60#else
61 /* kill() may choose arbitrarily the target task of the process group
62 while we later wait on a that specific TID. PID process waits become
63 TID task specific waits for a process under ptrace(2). */
Denys Vlasenko978fbc92012-09-13 10:28:43 +020064# warning "tkill(2) not available, risk of strace hangs!"
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010065# define my_tkill(tid, sig) kill((tid), (sig))
66#endif
67
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +010068/* Glue for systems without a MMU that cannot provide fork() */
69#if !defined(HAVE_FORK)
70# undef NOMMU_SYSTEM
71# define NOMMU_SYSTEM 1
72#endif
73#if NOMMU_SYSTEM
74# define fork() vfork()
75#endif
76
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010077cflag_t cflag = CFLAG_NONE;
78unsigned int followfork = 0;
Denys Vlasenkof44cce42011-06-21 14:34:10 +020079unsigned int ptrace_setoptions = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010080unsigned int xflag = 0;
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +010081bool need_fork_exec_workarounds = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010082bool debug_flag = 0;
83bool Tflag = 0;
Anton Blancharda34dead2013-07-12 12:22:06 +020084bool iflag = 0;
Mark Hillse53bf232014-05-28 17:52:40 +010085bool count_wallclock = 0;
Daniel P. Berrange01997cf2013-05-13 11:30:55 +010086unsigned int qflag = 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020087/* Which WSTOPSIG(status) value marks syscall traps? */
Denys Vlasenko75422762011-05-27 14:36:01 +020088static unsigned int syscall_trap_sig = SIGTRAP;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010089static unsigned int tflag = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010090static bool rflag = 0;
91static bool print_pid_pfx = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +010092
93/* -I n */
94enum {
95 INTR_NOT_SET = 0,
96 INTR_ANYWHERE = 1, /* don't block/ignore any signals */
97 INTR_WHILE_WAIT = 2, /* block fatal signals while decoding syscall. default */
98 INTR_NEVER = 3, /* block fatal signals. default if '-o FILE PROG' */
99 INTR_BLOCK_TSTP_TOO = 4, /* block fatal signals and SIGTSTP (^Z) */
100 NUM_INTR_OPTS
101};
102static int opt_intr;
103/* We play with signal mask only if this mode is active: */
104#define interactive (opt_intr == INTR_WHILE_WAIT)
105
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000106/*
107 * daemonized_tracer supports -D option.
108 * With this option, strace forks twice.
109 * Unlike normal case, with -D *grandparent* process exec's,
110 * becoming a traced process. Child exits (this prevents traced process
111 * from having children it doesn't expect to have), and grandchild
112 * attaches to grandparent similarly to strace -p PID.
113 * This allows for more transparent interaction in cases
114 * when process and its parent are communicating via signals,
115 * wait() etc. Without -D, strace process gets lodged in between,
116 * disrupting parent<->child link.
117 */
118static bool daemonized_tracer = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000119
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100120#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100121static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP;
122# define use_seize (post_attach_sigstop == 0)
123#else
124# define post_attach_sigstop TCB_IGNORE_ONE_SIGSTOP
125# define use_seize 0
126#endif
127
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000128/* Sometimes we want to print only succeeding syscalls. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100129bool not_failing_only = 0;
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000130
Grant Edwards8a082772011-04-07 20:25:40 +0000131/* Show path associated with fd arguments */
Dmitry V. Levin45e7b182014-08-08 23:38:26 +0000132unsigned int show_fd_path = 0;
Grant Edwards8a082772011-04-07 20:25:40 +0000133
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100134static bool detach_on_execve = 0;
Denys Vlasenko2a3d2752013-05-14 16:07:46 +0200135/* Are we "strace PROG" and need to skip detach on first execve? */
136static bool skip_one_b_execve = 0;
137/* Are we "strace PROG" and need to hide everything until execve? */
138bool hide_log_until_execve = 0;
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100139
Dmitry V. Levina6809652008-11-10 17:14:58 +0000140static int exit_code = 0;
141static int strace_child = 0;
Denys Vlasenko75422762011-05-27 14:36:01 +0200142static int strace_tracer_pid = 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -0700143
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000144static char *username = NULL;
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200145static uid_t run_uid;
146static gid_t run_gid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000147
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100148unsigned int max_strlen = DEFAULT_STRLEN;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000149static int acolumn = DEFAULT_ACOLUMN;
Denys Vlasenko102ec492011-08-25 01:27:59 +0200150static char *acolumn_spaces;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100151
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000152static char *outfname = NULL;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100153/* If -ff, points to stderr. Else, it's our common output log */
154static FILE *shared_log;
155
Denys Vlasenko000b6012012-01-28 01:25:03 +0100156struct tcb *printing_tcp = NULL;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100157static struct tcb *current_tcp;
158
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200159static struct tcb **tcbtab;
Denys Vlasenko2b60c352011-06-22 12:45:25 +0200160static unsigned int nprocs, tcbtabsize;
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200161static const char *progname;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000162
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100163unsigned os_release; /* generated from uname()'s u.release */
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +0100164
Denys Vlasenko4a9ba982013-06-20 11:20:23 +0200165static void detach(struct tcb *tcp);
Andreas Schwabe5355de2009-10-27 16:56:43 +0100166static void cleanup(void);
167static void interrupt(int sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000168static sigset_t empty_set, blocked_set;
169
170#ifdef HAVE_SIG_ATOMIC_T
171static volatile sig_atomic_t interrupted;
Denys Vlasenkoa3559252012-01-29 16:43:51 +0100172#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000173static volatile int interrupted;
Denys Vlasenkoa3559252012-01-29 16:43:51 +0100174#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000175
Denys Vlasenko2c4fb902012-03-15 17:24:49 +0100176#ifndef HAVE_STRERROR
177
178#if !HAVE_DECL_SYS_ERRLIST
179extern int sys_nerr;
180extern char *sys_errlist[];
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +0100181#endif
Denys Vlasenko2c4fb902012-03-15 17:24:49 +0100182
183const char *
184strerror(int err_no)
185{
186 static char buf[sizeof("Unknown error %d") + sizeof(int)*3];
187
188 if (err_no < 1 || err_no >= sys_nerr) {
189 sprintf(buf, "Unknown error %d", err_no);
190 return buf;
191 }
192 return sys_errlist[err_no];
193}
194
195#endif /* HAVE_STERRROR */
196
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000197static void
Denys Vlasenkocb2ad002011-06-23 13:05:29 +0200198usage(FILE *ofp, int exitval)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000199{
200 fprintf(ofp, "\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200201usage: strace [-CdffhiqrtttTvVxxy] [-I n] [-e expr]...\n\
202 [-a column] [-o file] [-s strsize] [-P path]...\n\
203 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
204 or: strace -c[df] [-I n] [-e expr]... [-O overhead] [-S sortby]\n\
205 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000206-c -- count time, calls, and errors for each syscall and report summary\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200207-C -- like -c but also print regular output\n\
Mark Hillse53bf232014-05-28 17:52:40 +0100208-w -- summarise syscall latency (default is system time)\n\
Denys Vlasenko3e084ac2012-03-15 13:39:05 +0100209-d -- enable debug output to stderr\n\
Denys Vlasenkob51581e2012-01-29 16:53:03 +0100210-D -- run tracer process as a detached grandchild, not as parent\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000211-f -- follow forks, -ff -- with output into separate files\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000212-i -- print instruction pointer at time of syscall\n\
213-q -- suppress messages about attaching, detaching, etc.\n\
214-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
Denys Vlasenkocdab1be2012-02-03 12:17:57 +0100215-T -- print time spent in each syscall\n\
216-v -- verbose mode: print unabbreviated argv, stat, termios, etc. args\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000217-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000218-y -- print paths associated with file descriptor arguments\n\
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000219-yy -- print ip:port pairs associated with socket file descriptors\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200220-h -- print help message, -V -- print version\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000221-a column -- alignment COLUMN for printing syscall results (default %d)\n\
Denys Vlasenko22efaf02013-02-27 12:15:19 +0100222-b execve -- detach on this syscall\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000223-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
Denys Vlasenko38e79bb2013-02-26 11:33:54 +0100224 options: trace, abbrev, verbose, raw, signal, read, write\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200225-I interruptible --\n\
226 1: no signals are blocked\n\
227 2: fatal signals are blocked while decoding syscall (default)\n\
228 3: fatal signals are always blocked (default if '-o FILE PROG')\n\
229 4: fatal signals and SIGTSTP (^Z) are always blocked\n\
230 (useful to make 'strace -o FILE PROG' not stop on ^Z)\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000231-o file -- send trace output to FILE instead of stderr\n\
232-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
233-p pid -- trace process with process id PID, may be repeated\n\
234-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
235-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
236-u username -- run command as username handling setuid and/or setgid\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000237-E var=val -- put var=val in the environment for command\n\
238-E var -- remove var from the environment for command\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000239-P path -- trace accesses to path\n\
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100240"
Luca Clementi327064b2013-07-23 00:11:35 -0700241#ifdef USE_LIBUNWIND
Dmitry V. Levin2734a702014-06-18 15:34:27 +0000242"-k obtain stack trace between each syscall (experimental)\n\
Luca Clementi327064b2013-07-23 00:11:35 -0700243"
244#endif
Denys Vlasenko38e79bb2013-02-26 11:33:54 +0100245/* ancient, no one should use it
246-F -- attempt to follow vforks (deprecated, use -f)\n\
247 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100248/* this is broken, so don't document it
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000249-z -- print only succeeding syscalls\n\
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100250 */
Roland McGrathde6e5332003-01-24 04:31:23 +0000251, DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000252 exit(exitval);
253}
254
Denys Vlasenko75422762011-05-27 14:36:01 +0200255static void die(void) __attribute__ ((noreturn));
256static void die(void)
257{
258 if (strace_tracer_pid == getpid()) {
259 cflag = 0;
260 cleanup();
261 }
262 exit(1);
263}
264
265static void verror_msg(int err_no, const char *fmt, va_list p)
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200266{
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100267 char *msg;
268
Dmitry V. Levin44d05322011-06-09 15:50:41 +0000269 fflush(NULL);
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100270
271 /* We want to print entire message with single fprintf to ensure
272 * message integrity if stderr is shared with other programs.
273 * Thus we use vasprintf + single fprintf.
274 */
275 msg = NULL;
Denys Vlasenkocfad5432012-01-24 12:48:02 +0100276 if (vasprintf(&msg, fmt, p) >= 0) {
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100277 if (err_no)
278 fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(err_no));
279 else
280 fprintf(stderr, "%s: %s\n", progname, msg);
281 free(msg);
282 } else {
283 /* malloc in vasprintf failed, try it without malloc */
284 fprintf(stderr, "%s: ", progname);
285 vfprintf(stderr, fmt, p);
286 if (err_no)
287 fprintf(stderr, ": %s\n", strerror(err_no));
288 else
289 putc('\n', stderr);
290 }
291 /* We don't switch stderr to buffered, thus fprintf(stderr)
292 * always flushes its output and this is not necessary: */
293 /* fflush(stderr); */
Denys Vlasenko75422762011-05-27 14:36:01 +0200294}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200295
Denys Vlasenko75422762011-05-27 14:36:01 +0200296void error_msg(const char *fmt, ...)
297{
298 va_list p;
299 va_start(p, fmt);
300 verror_msg(0, fmt, p);
301 va_end(p);
302}
303
304void error_msg_and_die(const char *fmt, ...)
305{
306 va_list p;
307 va_start(p, fmt);
308 verror_msg(0, fmt, p);
309 die();
310}
311
312void perror_msg(const char *fmt, ...)
313{
314 va_list p;
315 va_start(p, fmt);
316 verror_msg(errno, fmt, p);
317 va_end(p);
318}
319
320void perror_msg_and_die(const char *fmt, ...)
321{
322 va_list p;
323 va_start(p, fmt);
324 verror_msg(errno, fmt, p);
325 die();
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200326}
327
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200328void die_out_of_memory(void)
329{
330 static bool recursed = 0;
331 if (recursed)
332 exit(1);
333 recursed = 1;
334 error_msg_and_die("Out of memory");
335}
336
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000337static void
338error_opt_arg(int opt, const char *arg)
339{
340 error_msg_and_die("Invalid -%c argument: '%s'", opt, arg);
341}
342
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100343#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100344static int
345ptrace_attach_or_seize(int pid)
346{
347 int r;
348 if (!use_seize)
Denys Vlasenkoc169d942013-07-10 14:33:05 +0200349 return ptrace(PTRACE_ATTACH, pid, 0L, 0L);
350 r = ptrace(PTRACE_SEIZE, pid, 0L, (unsigned long)ptrace_setoptions);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100351 if (r)
352 return r;
Denys Vlasenkoc169d942013-07-10 14:33:05 +0200353 r = ptrace(PTRACE_INTERRUPT, pid, 0L, 0L);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100354 return r;
355}
356#else
357# define ptrace_attach_or_seize(pid) ptrace(PTRACE_ATTACH, (pid), 0, 0)
358#endif
359
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100360/*
361 * Used when we want to unblock stopped traced process.
362 * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL.
363 * Returns 0 on success or if error was ESRCH
364 * (presumably process was killed while we talk to it).
365 * Otherwise prints error message and returns -1.
366 */
367static int
368ptrace_restart(int op, struct tcb *tcp, int sig)
369{
370 int err;
371 const char *msg;
372
373 errno = 0;
374 ptrace(op, tcp->pid, (void *) 0, (long) sig);
375 err = errno;
Denys Vlasenko23506752012-03-21 10:32:49 +0100376 if (!err)
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100377 return 0;
378
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100379 msg = "SYSCALL";
380 if (op == PTRACE_CONT)
381 msg = "CONT";
382 if (op == PTRACE_DETACH)
383 msg = "DETACH";
384#ifdef PTRACE_LISTEN
385 if (op == PTRACE_LISTEN)
386 msg = "LISTEN";
387#endif
Denys Vlasenko23506752012-03-21 10:32:49 +0100388 /*
389 * Why curcol != 0? Otherwise sometimes we get this:
390 *
391 * 10252 kill(10253, SIGKILL) = 0
392 * <ptrace(SYSCALL,10252):No such process>10253 ...next decode...
393 *
394 * 10252 died after we retrieved syscall exit data,
395 * but before we tried to restart it. Log looks ugly.
396 */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100397 if (current_tcp && current_tcp->curcol != 0) {
Denys Vlasenko23506752012-03-21 10:32:49 +0100398 tprintf(" <ptrace(%s):%s>\n", msg, strerror(err));
399 line_ended();
400 }
401 if (err == ESRCH)
402 return 0;
403 errno = err;
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100404 perror_msg("ptrace(PTRACE_%s,pid:%d,sig:%d)", msg, tcp->pid, sig);
405 return -1;
406}
407
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200408static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000409set_cloexec_flag(int fd)
410{
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200411 int flags, newflags;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000412
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200413 flags = fcntl(fd, F_GETFD);
414 if (flags < 0) {
415 /* Can happen only if fd is bad.
416 * Should never happen: if it does, we have a bug
417 * in the caller. Therefore we just abort
418 * instead of propagating the error.
419 */
420 perror_msg_and_die("fcntl(%d, F_GETFD)", fd);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000421 }
422
423 newflags = flags | FD_CLOEXEC;
424 if (flags == newflags)
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200425 return;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000426
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200427 fcntl(fd, F_SETFD, newflags); /* never fails */
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000428}
429
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100430static void kill_save_errno(pid_t pid, int sig)
431{
432 int saved_errno = errno;
433
434 (void) kill(pid, sig);
435 errno = saved_errno;
436}
437
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100438/*
439 * When strace is setuid executable, we have to swap uids
440 * before and after filesystem and process management operations.
441 */
442static void
443swap_uid(void)
444{
445 int euid = geteuid(), uid = getuid();
446
447 if (euid != uid && setreuid(euid, uid) < 0) {
448 perror_msg_and_die("setreuid");
449 }
450}
451
Dmitry V. Levin0506f0f2013-11-12 22:34:42 +0000452#ifdef _LARGEFILE64_SOURCE
Dmitry V. Levind3541302014-02-26 00:01:00 +0000453# ifdef HAVE_FOPEN64
454# define fopen_for_output fopen64
455# else
456# define fopen_for_output fopen
457# endif
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000458# define struct_stat struct stat64
459# define stat_file stat64
460# define struct_dirent struct dirent64
461# define read_dir readdir64
462# define struct_rlimit struct rlimit64
463# define set_rlimit setrlimit64
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100464#else
465# define fopen_for_output fopen
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000466# define struct_stat struct stat
467# define stat_file stat
468# define struct_dirent struct dirent
469# define read_dir readdir
470# define struct_rlimit struct rlimit
471# define set_rlimit setrlimit
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100472#endif
473
474static FILE *
475strace_fopen(const char *path)
476{
477 FILE *fp;
478
479 swap_uid();
480 fp = fopen_for_output(path, "w");
481 if (!fp)
482 perror_msg_and_die("Can't fopen '%s'", path);
483 swap_uid();
484 set_cloexec_flag(fileno(fp));
485 return fp;
486}
487
488static int popen_pid = 0;
489
490#ifndef _PATH_BSHELL
491# define _PATH_BSHELL "/bin/sh"
492#endif
493
494/*
495 * We cannot use standard popen(3) here because we have to distinguish
496 * popen child process from other processes we trace, and standard popen(3)
497 * does not export its child's pid.
498 */
499static FILE *
500strace_popen(const char *command)
501{
502 FILE *fp;
Denys Vlasenko519af5a2013-07-02 11:31:24 +0200503 int pid;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100504 int fds[2];
505
506 swap_uid();
507 if (pipe(fds) < 0)
508 perror_msg_and_die("pipe");
509
510 set_cloexec_flag(fds[1]); /* never fails */
511
Denys Vlasenko519af5a2013-07-02 11:31:24 +0200512 pid = vfork();
513 if (pid < 0)
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100514 perror_msg_and_die("vfork");
515
Denys Vlasenko519af5a2013-07-02 11:31:24 +0200516 if (pid == 0) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100517 /* child */
518 close(fds[1]);
519 if (fds[0] != 0) {
520 if (dup2(fds[0], 0))
521 perror_msg_and_die("dup2");
522 close(fds[0]);
523 }
524 execl(_PATH_BSHELL, "sh", "-c", command, NULL);
525 perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL);
526 }
527
528 /* parent */
Denys Vlasenko519af5a2013-07-02 11:31:24 +0200529 popen_pid = pid;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100530 close(fds[0]);
531 swap_uid();
532 fp = fdopen(fds[1], "w");
533 if (!fp)
534 die_out_of_memory();
535 return fp;
536}
537
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100538void
539tprintf(const char *fmt, ...)
540{
541 va_list args;
542
543 va_start(args, fmt);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100544 if (current_tcp) {
Denys Vlasenko6e4f3c12012-04-16 18:22:19 +0200545 int n = strace_vfprintf(current_tcp->outf, fmt, args);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100546 if (n < 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100547 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000548 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100549 } else
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100550 current_tcp->curcol += n;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100551 }
552 va_end(args);
553}
554
Dmitry V. Levind3541302014-02-26 00:01:00 +0000555#ifndef HAVE_FPUTS_UNLOCKED
556# define fputs_unlocked fputs
557#endif
558
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100559void
560tprints(const char *str)
561{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100562 if (current_tcp) {
Denys Vlasenko142aee02012-04-16 18:10:15 +0200563 int n = fputs_unlocked(str, current_tcp->outf);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100564 if (n >= 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100565 current_tcp->curcol += strlen(str);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100566 return;
567 }
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100568 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000569 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100570 }
571}
572
573void
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100574line_ended(void)
575{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100576 if (current_tcp) {
577 current_tcp->curcol = 0;
578 fflush(current_tcp->outf);
579 }
580 if (printing_tcp) {
581 printing_tcp->curcol = 0;
582 printing_tcp = NULL;
583 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100584}
585
586void
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100587printleader(struct tcb *tcp)
588{
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100589 /* If -ff, "previous tcb we printed" is always the same as current,
590 * because we have per-tcb output files.
591 */
592 if (followfork >= 2)
593 printing_tcp = tcp;
594
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100595 if (printing_tcp) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100596 current_tcp = printing_tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100597 if (printing_tcp->curcol != 0 && (followfork < 2 || printing_tcp == tcp)) {
598 /*
599 * case 1: we have a shared log (i.e. not -ff), and last line
600 * wasn't finished (same or different tcb, doesn't matter).
601 * case 2: split log, we are the same tcb, but our last line
602 * didn't finish ("SIGKILL nuked us after syscall entry" etc).
603 */
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100604 tprints(" <unfinished ...>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100605 printing_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100606 }
607 }
608
609 printing_tcp = tcp;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100610 current_tcp = tcp;
611 current_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100612
613 if (print_pid_pfx)
614 tprintf("%-5d ", tcp->pid);
615 else if (nprocs > 1 && !outfname)
616 tprintf("[pid %5u] ", tcp->pid);
617
618 if (tflag) {
619 char str[sizeof("HH:MM:SS")];
620 struct timeval tv, dtv;
621 static struct timeval otv;
622
623 gettimeofday(&tv, NULL);
624 if (rflag) {
625 if (otv.tv_sec == 0)
626 otv = tv;
627 tv_sub(&dtv, &tv, &otv);
628 tprintf("%6ld.%06ld ",
629 (long) dtv.tv_sec, (long) dtv.tv_usec);
630 otv = tv;
631 }
632 else if (tflag > 2) {
633 tprintf("%ld.%06ld ",
634 (long) tv.tv_sec, (long) tv.tv_usec);
635 }
636 else {
637 time_t local = tv.tv_sec;
638 strftime(str, sizeof(str), "%T", localtime(&local));
639 if (tflag > 1)
640 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
641 else
642 tprintf("%s ", str);
643 }
644 }
645 if (iflag)
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200646 print_pc(tcp);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100647}
648
649void
650tabto(void)
651{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100652 if (current_tcp->curcol < acolumn)
653 tprints(acolumn_spaces + current_tcp->curcol);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100654}
655
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100656/* Should be only called directly *after successful attach* to a tracee.
657 * Otherwise, "strace -oFILE -ff -p<nonexistant_pid>"
658 * may create bogus empty FILE.<nonexistant_pid>, and then die.
659 */
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200660static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000661newoutf(struct tcb *tcp)
662{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100663 tcp->outf = shared_log; /* if not -ff mode, the same file is for all */
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100664 if (followfork >= 2) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000665 char name[520 + sizeof(int) * 3];
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000666 sprintf(name, "%.512s.%u", outfname, tcp->pid);
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200667 tcp->outf = strace_fopen(name);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000668 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000669}
670
Denys Vlasenko558e5122012-03-12 23:32:16 +0100671static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100672expand_tcbtab(void)
673{
674 /* Allocate some more TCBs and expand the table.
675 We don't want to relocate the TCBs because our
676 callers have pointers and it would be a pain.
677 So tcbtab is a table of pointers. Since we never
678 free the TCBs, we allocate a single chunk of many. */
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000679 unsigned int i = tcbtabsize;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100680 struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0]));
681 struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0]));
682 if (!newtab || !newtcbs)
683 die_out_of_memory();
684 tcbtabsize *= 2;
685 tcbtab = newtab;
686 while (i < tcbtabsize)
687 tcbtab[i++] = newtcbs++;
688}
689
690static struct tcb *
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100691alloctcb(int pid)
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100692{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000693 unsigned int i;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100694 struct tcb *tcp;
695
696 if (nprocs == tcbtabsize)
697 expand_tcbtab();
698
699 for (i = 0; i < tcbtabsize; i++) {
700 tcp = tcbtab[i];
Denys Vlasenkofadbf662013-06-26 14:14:29 +0200701 if (!tcp->pid) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100702 memset(tcp, 0, sizeof(*tcp));
703 tcp->pid = pid;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100704#if SUPPORTED_PERSONALITIES > 1
705 tcp->currpers = current_personality;
706#endif
Luca Clementi327064b2013-07-23 00:11:35 -0700707
708#ifdef USE_LIBUNWIND
709 if (stack_trace_enabled)
Masatake YAMATO61413922014-04-16 15:33:02 +0900710 unwind_tcb_init(tcp);
Luca Clementi327064b2013-07-23 00:11:35 -0700711#endif
712
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100713 nprocs++;
714 if (debug_flag)
715 fprintf(stderr, "new tcb for pid %d, active tcbs:%d\n", tcp->pid, nprocs);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100716 return tcp;
717 }
718 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100719 error_msg_and_die("bug in alloctcb");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100720}
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100721
722static void
723droptcb(struct tcb *tcp)
724{
725 if (tcp->pid == 0)
726 return;
727
Masatake YAMATO2b09df92014-04-16 15:33:08 +0900728#ifdef USE_LIBUNWIND
729 if (stack_trace_enabled) {
730 unwind_tcb_fin(tcp);
731 }
732#endif
733
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100734 nprocs--;
735 if (debug_flag)
736 fprintf(stderr, "dropped tcb for pid %d, %d remain\n", tcp->pid, nprocs);
737
738 if (tcp->outf) {
Denys Vlasenko8511f2a2012-03-22 09:35:51 +0100739 if (followfork >= 2) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100740 if (tcp->curcol != 0)
741 fprintf(tcp->outf, " <detached ...>\n");
742 fclose(tcp->outf);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100743 } else {
744 if (printing_tcp == tcp && tcp->curcol != 0)
745 fprintf(tcp->outf, " <detached ...>\n");
746 fflush(tcp->outf);
747 }
748 }
749
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100750 if (current_tcp == tcp)
751 current_tcp = NULL;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100752 if (printing_tcp == tcp)
753 printing_tcp = NULL;
754
755 memset(tcp, 0, sizeof(*tcp));
756}
757
Denys Vlasenkof1669e72013-06-18 18:09:39 +0200758/* Detach traced process.
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100759 * Never call DETACH twice on the same process as both unattached and
760 * attached-unstopped processes give the same ESRCH. For unattached process we
761 * would SIGSTOP it and wait for its SIGSTOP notification forever.
762 */
Denys Vlasenko4a9ba982013-06-20 11:20:23 +0200763static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100764detach(struct tcb *tcp)
765{
766 int error;
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200767 int status;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100768
769 if (tcp->flags & TCB_BPTSET)
770 clearbpt(tcp);
771
772 /*
773 * Linux wrongly insists the child be stopped
774 * before detaching. Arghh. We go through hoops
775 * to make a clean break of things.
776 */
777#if defined(SPARC)
Denys Vlasenko978fbc92012-09-13 10:28:43 +0200778# undef PTRACE_DETACH
779# define PTRACE_DETACH PTRACE_SUNDETACH
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100780#endif
781
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200782 if (!(tcp->flags & TCB_ATTACHED))
783 goto drop;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100784
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200785 /* We attached but possibly didn't see the expected SIGSTOP.
786 * We must catch exactly one as otherwise the detached process
787 * would be left stopped (process state T).
788 */
789 if (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)
790 goto wait_loop;
791
792 error = ptrace(PTRACE_DETACH, tcp->pid, 0, 0);
793 if (!error) {
794 /* On a clear day, you can see forever. */
795 goto drop;
796 }
797 if (errno != ESRCH) {
798 /* Shouldn't happen. */
799 perror_msg("detach: ptrace(PTRACE_DETACH,%u)", tcp->pid);
800 goto drop;
801 }
802 /* ESRCH: process is either not stopped or doesn't exist. */
803 if (my_tkill(tcp->pid, 0) < 0) {
804 if (errno != ESRCH)
805 /* Shouldn't happen. */
806 perror_msg("detach: tkill(%u,0)", tcp->pid);
807 /* else: process doesn't exist. */
808 goto drop;
809 }
810 /* Process is not stopped, need to stop it. */
811 if (use_seize) {
812 /*
813 * With SEIZE, tracee can be in group-stop already.
814 * In this state sending it another SIGSTOP does nothing.
815 * Need to use INTERRUPT.
816 * Testcase: trying to ^C a "strace -p <stopped_process>".
817 */
818 error = ptrace(PTRACE_INTERRUPT, tcp->pid, 0, 0);
819 if (!error)
820 goto wait_loop;
821 if (errno != ESRCH)
822 perror_msg("detach: ptrace(PTRACE_INTERRUPT,%u)", tcp->pid);
823 }
824 else {
825 error = my_tkill(tcp->pid, SIGSTOP);
826 if (!error)
827 goto wait_loop;
828 if (errno != ESRCH)
829 perror_msg("detach: tkill(%u,SIGSTOP)", tcp->pid);
830 }
831 /* Either process doesn't exist, or some weird error. */
832 goto drop;
833
Denys Vlasenkoa2de9da2013-06-21 15:50:41 +0200834 wait_loop:
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200835 /* We end up here in three cases:
836 * 1. We sent PTRACE_INTERRUPT (use_seize case)
837 * 2. We sent SIGSTOP (!use_seize)
838 * 3. Attach SIGSTOP was already pending (TCB_IGNORE_ONE_SIGSTOP set)
839 */
840 for (;;) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000841 unsigned int sig;
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200842 if (waitpid(tcp->pid, &status, __WALL) < 0) {
843 if (errno == EINTR)
844 continue;
845 /*
846 * if (errno == ECHILD) break;
847 * ^^^ WRONG! We expect this PID to exist,
848 * and want to emit a message otherwise:
849 */
850 perror_msg("detach: waitpid(%u)", tcp->pid);
851 break;
852 }
853 if (!WIFSTOPPED(status)) {
854 /*
855 * Tracee exited or was killed by signal.
856 * We shouldn't normally reach this place:
857 * we don't want to consume exit status.
858 * Consider "strace -p PID" being ^C-ed:
859 * we want merely to detach from PID.
860 *
861 * However, we _can_ end up here if tracee
862 * was SIGKILLed.
863 */
864 break;
865 }
866 sig = WSTOPSIG(status);
867 if (debug_flag)
868 fprintf(stderr, "detach wait: event:%d sig:%d\n",
869 (unsigned)status >> 16, sig);
870 if (use_seize) {
871 unsigned event = (unsigned)status >> 16;
872 if (event == PTRACE_EVENT_STOP /*&& sig == SIGTRAP*/) {
Denys Vlasenkofdfa47a2013-06-20 12:10:21 +0200873 /*
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200874 * sig == SIGTRAP: PTRACE_INTERRUPT stop.
875 * sig == other: process was already stopped
876 * with this stopping sig (see tests/detach-stopped).
877 * Looks like re-injecting this sig is not necessary
878 * in DETACH for the tracee to remain stopped.
Denys Vlasenkofdfa47a2013-06-20 12:10:21 +0200879 */
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200880 sig = 0;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100881 }
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200882 /*
883 * PTRACE_INTERRUPT is not guaranteed to produce
884 * the above event if other ptrace-stop is pending.
885 * See tests/detach-sleeping testcase:
886 * strace got SIGINT while tracee is sleeping.
887 * We sent PTRACE_INTERRUPT.
888 * We see syscall exit, not PTRACE_INTERRUPT stop.
889 * We won't get PTRACE_INTERRUPT stop
890 * if we would CONT now. Need to DETACH.
891 */
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200892 if (sig == syscall_trap_sig)
893 sig = 0;
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200894 /* else: not sure in which case we can be here.
895 * Signal stop? Inject it while detaching.
896 */
897 ptrace_restart(PTRACE_DETACH, tcp, sig);
898 break;
899 }
900 /* Note: this check has to be after use_seize check */
901 /* (else, in use_seize case SIGSTOP will be mistreated) */
902 if (sig == SIGSTOP) {
903 /* Detach, suppressing SIGSTOP */
904 ptrace_restart(PTRACE_DETACH, tcp, 0);
905 break;
906 }
907 if (sig == syscall_trap_sig)
908 sig = 0;
909 /* Can't detach just yet, may need to wait for SIGSTOP */
910 error = ptrace_restart(PTRACE_CONT, tcp, sig);
911 if (error < 0) {
912 /* Should not happen.
913 * Note: ptrace_restart returns 0 on ESRCH, so it's not it.
914 * ptrace_restart already emitted error message.
915 */
916 break;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100917 }
918 }
919
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200920 drop:
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100921 if (!qflag && (tcp->flags & TCB_ATTACHED))
922 fprintf(stderr, "Process %u detached\n", tcp->pid);
923
924 droptcb(tcp);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100925}
926
927static void
Denys Vlasenko558e5122012-03-12 23:32:16 +0100928process_opt_p_list(char *opt)
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100929{
930 while (*opt) {
931 /*
932 * We accept -p PID,PID; -p "`pidof PROG`"; -p "`pgrep PROG`".
933 * pidof uses space as delim, pgrep uses newline. :(
934 */
935 int pid;
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100936 char *delim = opt + strcspn(opt, ", \n\t");
937 char c = *delim;
938
939 *delim = '\0';
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000940 pid = string_to_uint(opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100941 if (pid <= 0) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000942 error_msg_and_die("Invalid process id: '%s'", opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100943 }
944 if (pid == strace_tracer_pid) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000945 error_msg_and_die("I'm sorry, I can't let you do that, Dave.");
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100946 }
947 *delim = c;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100948 alloctcb(pid);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100949 if (c == '\0')
950 break;
951 opt = delim + 1;
952 }
953}
954
Roland McGrath02203312007-06-11 22:06:31 +0000955static void
956startup_attach(void)
957{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000958 unsigned int tcbi;
Roland McGrath02203312007-06-11 22:06:31 +0000959 struct tcb *tcp;
960
961 /*
962 * Block user interruptions as we would leave the traced
963 * process stopped (process state T) if we would terminate in
Denys Vlasenko2e968c02011-09-01 10:23:09 +0200964 * between PTRACE_ATTACH and wait4() on SIGSTOP.
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200965 * We rely on cleanup() from this point on.
Roland McGrath02203312007-06-11 22:06:31 +0000966 */
967 if (interactive)
968 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
969
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000970 if (daemonized_tracer) {
971 pid_t pid = fork();
972 if (pid < 0) {
Denys Vlasenko014ca3a2011-09-02 16:19:30 +0200973 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000974 }
975 if (pid) { /* parent */
976 /*
Denys Vlasenko75422762011-05-27 14:36:01 +0200977 * Wait for grandchild to attach to straced process
978 * (grandparent). Grandchild SIGKILLs us after it attached.
979 * Grandparent's wait() is unblocked by our death,
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000980 * it proceeds to exec the straced program.
981 */
982 pause();
983 _exit(0); /* paranoia */
984 }
Denys Vlasenko75422762011-05-27 14:36:01 +0200985 /* grandchild */
986 /* We will be the tracer process. Remember our new pid: */
987 strace_tracer_pid = getpid();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000988 }
989
Roland McGrath02203312007-06-11 22:06:31 +0000990 for (tcbi = 0; tcbi < tcbtabsize; tcbi++) {
991 tcp = tcbtab[tcbi];
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200992
Denys Vlasenkofadbf662013-06-26 14:14:29 +0200993 if (!tcp->pid)
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100994 continue;
995
Denys Vlasenkod116a732011-09-05 14:01:33 +0200996 /* Is this a process we should attach to, but not yet attached? */
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100997 if (tcp->flags & TCB_ATTACHED)
998 continue; /* no, we already attached it */
Denys Vlasenkod116a732011-09-05 14:01:33 +0200999
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001000 if (followfork && !daemonized_tracer) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001001 char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3];
Roland McGrath02203312007-06-11 22:06:31 +00001002 DIR *dir;
1003
1004 sprintf(procdir, "/proc/%d/task", tcp->pid);
1005 dir = opendir(procdir);
1006 if (dir != NULL) {
1007 unsigned int ntid = 0, nerr = 0;
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001008 struct_dirent *de;
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001009
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001010 while ((de = read_dir(dir)) != NULL) {
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001011 struct tcb *cur_tcp;
1012 int tid;
1013
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001014 if (de->d_fileno == 0)
Roland McGrath02203312007-06-11 22:06:31 +00001015 continue;
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001016 /* we trust /proc filesystem */
Roland McGrath02203312007-06-11 22:06:31 +00001017 tid = atoi(de->d_name);
1018 if (tid <= 0)
1019 continue;
1020 ++ntid;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001021 if (ptrace_attach_or_seize(tid) < 0) {
Roland McGrath02203312007-06-11 22:06:31 +00001022 ++nerr;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001023 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001024 fprintf(stderr, "attach to pid %d failed\n", tid);
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001025 continue;
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001026 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001027 if (debug_flag)
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001028 fprintf(stderr, "attach to pid %d succeeded\n", tid);
1029 cur_tcp = tcp;
1030 if (tid != tcp->pid)
1031 cur_tcp = alloctcb(tid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001032 cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001033 newoutf(cur_tcp);
Roland McGrath02203312007-06-11 22:06:31 +00001034 }
1035 closedir(dir);
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001036 if (interactive) {
1037 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1038 if (interrupted)
1039 goto ret;
1040 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1041 }
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001042 ntid -= nerr;
1043 if (ntid == 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 }
1048 if (!qflag) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001049 fprintf(stderr, ntid > 1
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001050? "Process %u attached with %u threads\n"
1051: "Process %u attached\n",
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02001052 tcp->pid, ntid);
Roland McGrath02203312007-06-11 22:06:31 +00001053 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001054 if (!(tcp->flags & TCB_ATTACHED)) {
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001055 /* -p PID, we failed to attach to PID itself
1056 * but did attach to some of its sibling threads.
1057 * Drop PID's tcp.
1058 */
1059 droptcb(tcp);
1060 }
Roland McGrath02203312007-06-11 22:06:31 +00001061 continue;
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001062 } /* if (opendir worked) */
1063 } /* if (-f) */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001064 if (ptrace_attach_or_seize(tcp->pid) < 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001065 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +00001066 droptcb(tcp);
1067 continue;
1068 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001069 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001070 newoutf(tcp);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001071 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001072 fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001073
1074 if (daemonized_tracer) {
1075 /*
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001076 * Make parent go away.
1077 * Also makes grandparent's wait() unblock.
1078 */
1079 kill(getppid(), SIGKILL);
1080 }
1081
Roland McGrath02203312007-06-11 22:06:31 +00001082 if (!qflag)
1083 fprintf(stderr,
Denys Vlasenko9c3861d2012-03-16 15:21:49 +01001084 "Process %u attached\n",
Roland McGrath02203312007-06-11 22:06:31 +00001085 tcp->pid);
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001086 } /* for each tcbtab[] */
Roland McGrath02203312007-06-11 22:06:31 +00001087
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02001088 ret:
Roland McGrath02203312007-06-11 22:06:31 +00001089 if (interactive)
1090 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1091}
1092
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001093/* Stack-o-phobic exec helper, in the hope to work around
1094 * NOMMU + "daemonized tracer" difficulty.
1095 */
1096struct exec_params {
1097 int fd_to_close;
1098 uid_t run_euid;
1099 gid_t run_egid;
1100 char **argv;
1101 char *pathname;
1102};
1103static struct exec_params params_for_tracee;
1104static void __attribute__ ((noinline, noreturn))
1105exec_or_die(void)
1106{
1107 struct exec_params *params = &params_for_tracee;
1108
1109 if (params->fd_to_close >= 0)
1110 close(params->fd_to_close);
1111 if (!daemonized_tracer && !use_seize) {
1112 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) {
1113 perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)");
1114 }
1115 }
1116
1117 if (username != NULL) {
1118 /*
1119 * It is important to set groups before we
1120 * lose privileges on setuid.
1121 */
1122 if (initgroups(username, run_gid) < 0) {
1123 perror_msg_and_die("initgroups");
1124 }
1125 if (setregid(run_gid, params->run_egid) < 0) {
1126 perror_msg_and_die("setregid");
1127 }
1128 if (setreuid(run_uid, params->run_euid) < 0) {
1129 perror_msg_and_die("setreuid");
1130 }
1131 }
1132 else if (geteuid() != 0)
1133 if (setreuid(run_uid, run_uid) < 0) {
1134 perror_msg_and_die("setreuid");
1135 }
1136
1137 if (!daemonized_tracer) {
1138 /*
1139 * Induce a ptrace stop. Tracer (our parent)
1140 * will resume us with PTRACE_SYSCALL and display
1141 * the immediately following execve syscall.
1142 * Can't do this on NOMMU systems, we are after
1143 * vfork: parent is blocked, stopping would deadlock.
1144 */
1145 if (!NOMMU_SYSTEM)
1146 kill(getpid(), SIGSTOP);
1147 } else {
1148 alarm(3);
1149 /* we depend on SIGCHLD set to SIG_DFL by init code */
1150 /* if it happens to be SIG_IGN'ed, wait won't block */
1151 wait(NULL);
1152 alarm(0);
1153 }
1154
1155 execv(params->pathname, params->argv);
1156 perror_msg_and_die("exec");
1157}
1158
Roland McGrath02203312007-06-11 22:06:31 +00001159static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001160startup_child(char **argv)
Roland McGrath02203312007-06-11 22:06:31 +00001161{
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001162 struct_stat statbuf;
Roland McGrath02203312007-06-11 22:06:31 +00001163 const char *filename;
Dmitry V. Levin025b3582014-11-21 22:28:34 +00001164 char pathname[PATH_MAX];
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001165 int pid;
Roland McGrath02203312007-06-11 22:06:31 +00001166 struct tcb *tcp;
1167
1168 filename = argv[0];
1169 if (strchr(filename, '/')) {
1170 if (strlen(filename) > sizeof pathname - 1) {
1171 errno = ENAMETOOLONG;
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001172 perror_msg_and_die("exec");
Roland McGrath02203312007-06-11 22:06:31 +00001173 }
1174 strcpy(pathname, filename);
1175 }
1176#ifdef USE_DEBUGGING_EXEC
1177 /*
1178 * Debuggers customarily check the current directory
1179 * first regardless of the path but doing that gives
1180 * security geeks a panic attack.
1181 */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001182 else if (stat_file(filename, &statbuf) == 0)
Roland McGrath02203312007-06-11 22:06:31 +00001183 strcpy(pathname, filename);
1184#endif /* USE_DEBUGGING_EXEC */
1185 else {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +00001186 const char *path;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001187 size_t m, n, len;
Roland McGrath02203312007-06-11 22:06:31 +00001188
1189 for (path = getenv("PATH"); path && *path; path += m) {
Denys Vlasenko4f3df072012-01-29 22:38:35 +01001190 const char *colon = strchr(path, ':');
1191 if (colon) {
1192 n = colon - path;
Roland McGrath02203312007-06-11 22:06:31 +00001193 m = n + 1;
1194 }
1195 else
1196 m = n = strlen(path);
1197 if (n == 0) {
Dmitry V. Levin025b3582014-11-21 22:28:34 +00001198 if (!getcwd(pathname, PATH_MAX))
Roland McGrath02203312007-06-11 22:06:31 +00001199 continue;
1200 len = strlen(pathname);
1201 }
1202 else if (n > sizeof pathname - 1)
1203 continue;
1204 else {
1205 strncpy(pathname, path, n);
1206 len = n;
1207 }
1208 if (len && pathname[len - 1] != '/')
1209 pathname[len++] = '/';
1210 strcpy(pathname + len, filename);
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001211 if (stat_file(pathname, &statbuf) == 0 &&
Roland McGrath02203312007-06-11 22:06:31 +00001212 /* Accept only regular files
1213 with some execute bits set.
1214 XXX not perfect, might still fail */
1215 S_ISREG(statbuf.st_mode) &&
1216 (statbuf.st_mode & 0111))
1217 break;
1218 }
1219 }
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001220 if (stat_file(pathname, &statbuf) < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001221 perror_msg_and_die("Can't stat '%s'", filename);
Roland McGrath02203312007-06-11 22:06:31 +00001222 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001223
1224 params_for_tracee.fd_to_close = (shared_log != stderr) ? fileno(shared_log) : -1;
1225 params_for_tracee.run_euid = (statbuf.st_mode & S_ISUID) ? statbuf.st_uid : run_uid;
1226 params_for_tracee.run_egid = (statbuf.st_mode & S_ISGID) ? statbuf.st_gid : run_gid;
1227 params_for_tracee.argv = argv;
1228 /*
1229 * On NOMMU, can be safely freed only after execve in tracee.
1230 * It's hard to know when that happens, so we just leak it.
1231 */
Denys Vlasenko05f32512013-02-26 12:00:34 +01001232 params_for_tracee.pathname = NOMMU_SYSTEM ? strdup(pathname) : pathname;
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001233
Dmitry V. Levin882478a2013-05-13 18:43:28 +00001234#if defined HAVE_PRCTL && defined PR_SET_PTRACER && defined PR_SET_PTRACER_ANY
1235 if (daemonized_tracer)
1236 prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY);
1237#endif
1238
Denys Vlasenkoe8681c92013-06-26 14:27:11 +02001239 pid = fork();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001240 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001241 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001242 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001243 if ((pid != 0 && daemonized_tracer)
1244 || (pid == 0 && !daemonized_tracer)
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001245 ) {
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001246 /* We are to become the tracee. Two cases:
1247 * -D: we are parent
1248 * not -D: we are child
1249 */
1250 exec_or_die();
Roland McGrath02203312007-06-11 22:06:31 +00001251 }
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001252
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001253 /* We are the tracer */
Denys Vlasenko75422762011-05-27 14:36:01 +02001254
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001255 if (!daemonized_tracer) {
Denys Vlasenkoe8681c92013-06-26 14:27:11 +02001256 strace_child = pid;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001257 if (!use_seize) {
1258 /* child did PTRACE_TRACEME, nothing to do in parent */
1259 } else {
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001260 if (!NOMMU_SYSTEM) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001261 /* Wait until child stopped itself */
1262 int status;
1263 while (waitpid(pid, &status, WSTOPPED) < 0) {
1264 if (errno == EINTR)
1265 continue;
1266 perror_msg_and_die("waitpid");
1267 }
1268 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001269 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001270 perror_msg_and_die("Unexpected wait status %x", status);
1271 }
1272 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001273 /* Else: NOMMU case, we have no way to sync.
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001274 * Just attach to it as soon as possible.
1275 * This means that we may miss a few first syscalls...
1276 */
1277
1278 if (ptrace_attach_or_seize(pid)) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001279 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001280 perror_msg_and_die("Can't attach to %d", pid);
1281 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001282 if (!NOMMU_SYSTEM)
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001283 kill(pid, SIGCONT);
1284 }
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001285 tcp = alloctcb(pid);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001286 if (!NOMMU_SYSTEM)
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001287 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001288 else
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001289 tcp->flags |= TCB_ATTACHED | TCB_STARTUP;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001290 newoutf(tcp);
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001291 }
1292 else {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001293 /* With -D, we are *child* here, IOW: different pid. Fetch it: */
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001294 strace_tracer_pid = getpid();
1295 /* The tracee is our parent: */
1296 pid = getppid();
Denys Vlasenkof2025022012-03-09 15:29:45 +01001297 alloctcb(pid);
1298 /* attaching will be done later, by startup_attach */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001299 /* note: we don't do newoutf(tcp) here either! */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001300
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001301 /* NOMMU BUG! -D mode is active, we (child) return,
1302 * and we will scribble over parent's stack!
1303 * When parent later unpauses, it segfaults.
1304 *
1305 * We work around it
1306 * (1) by declaring exec_or_die() NORETURN,
1307 * hopefully compiler will just jump to it
1308 * instead of call (won't push anything to stack),
1309 * (2) by trying very hard in exec_or_die()
1310 * to not use any stack,
Dmitry V. Levin025b3582014-11-21 22:28:34 +00001311 * (3) having a really big (PATH_MAX) stack object
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001312 * in this function, which creates a "buffer" between
1313 * child's and parent's stack pointers.
1314 * This may save us if (1) and (2) failed
1315 * and compiler decided to use stack in exec_or_die() anyway
1316 * (happens on i386 because of stack parameter passing).
Denys Vlasenko05f32512013-02-26 12:00:34 +01001317 *
1318 * A cleaner solution is to use makecontext + setcontext
1319 * to create a genuine separate stack and execute on it.
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001320 */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001321 }
Roland McGrath02203312007-06-11 22:06:31 +00001322}
1323
Wang Chaob13c0de2010-11-12 17:25:19 +08001324/*
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001325 * Test whether the kernel support PTRACE_O_TRACECLONE et al options.
Wang Chaob13c0de2010-11-12 17:25:19 +08001326 * First fork a new child, call ptrace with PTRACE_SETOPTIONS on it,
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001327 * and then see which options are supported by the kernel.
Wang Chaob13c0de2010-11-12 17:25:19 +08001328 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001329static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001330test_ptrace_setoptions_followfork(void)
Wang Chaob13c0de2010-11-12 17:25:19 +08001331{
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001332 int pid, expected_grandchild = 0, found_grandchild = 0;
1333 const unsigned int test_options = PTRACE_O_TRACECLONE |
1334 PTRACE_O_TRACEFORK |
1335 PTRACE_O_TRACEVFORK;
Wang Chaob13c0de2010-11-12 17:25:19 +08001336
Denys Vlasenko05f32512013-02-26 12:00:34 +01001337 /* Need fork for test. NOMMU has no forks */
1338 if (NOMMU_SYSTEM)
1339 goto worked; /* be bold, and pretend that test succeeded */
1340
Denys Vlasenko5d645812011-08-20 12:48:18 +02001341 pid = fork();
1342 if (pid < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001343 perror_msg_and_die("fork");
Denys Vlasenko5d645812011-08-20 12:48:18 +02001344 if (pid == 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001345 pid = getpid();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001346 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001347 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1348 __func__);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001349 kill_save_errno(pid, SIGSTOP);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001350 if (fork() < 0)
1351 perror_msg_and_die("fork");
1352 _exit(0);
Wang Chaob13c0de2010-11-12 17:25:19 +08001353 }
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001354
1355 while (1) {
1356 int status, tracee_pid;
1357
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001358 errno = 0;
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001359 tracee_pid = wait(&status);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001360 if (tracee_pid <= 0) {
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001361 if (errno == EINTR)
1362 continue;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001363 if (errno == ECHILD)
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001364 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001365 kill_save_errno(pid, SIGKILL);
1366 perror_msg_and_die("%s: unexpected wait result %d",
1367 __func__, tracee_pid);
1368 }
1369 if (WIFEXITED(status)) {
1370 if (WEXITSTATUS(status)) {
1371 if (tracee_pid != pid)
1372 kill_save_errno(pid, SIGKILL);
1373 error_msg_and_die("%s: unexpected exit status %u",
1374 __func__, WEXITSTATUS(status));
1375 }
1376 continue;
1377 }
1378 if (WIFSIGNALED(status)) {
1379 if (tracee_pid != pid)
1380 kill_save_errno(pid, SIGKILL);
1381 error_msg_and_die("%s: unexpected signal %u",
1382 __func__, WTERMSIG(status));
1383 }
1384 if (!WIFSTOPPED(status)) {
1385 if (tracee_pid != pid)
1386 kill_save_errno(tracee_pid, SIGKILL);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001387 kill_save_errno(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001388 error_msg_and_die("%s: unexpected wait status %x",
1389 __func__, status);
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001390 }
1391 if (tracee_pid != pid) {
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001392 found_grandchild = tracee_pid;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001393 if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0) {
1394 kill_save_errno(tracee_pid, SIGKILL);
1395 kill_save_errno(pid, SIGKILL);
1396 perror_msg_and_die("PTRACE_CONT doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001397 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001398 continue;
1399 }
1400 switch (WSTOPSIG(status)) {
1401 case SIGSTOP:
1402 if (ptrace(PTRACE_SETOPTIONS, pid, 0, test_options) < 0
1403 && errno != EINVAL && errno != EIO)
1404 perror_msg("PTRACE_SETOPTIONS");
1405 break;
1406 case SIGTRAP:
1407 if (status >> 16 == PTRACE_EVENT_FORK) {
1408 long msg = 0;
1409
1410 if (ptrace(PTRACE_GETEVENTMSG, pid,
1411 NULL, (long) &msg) == 0)
1412 expected_grandchild = msg;
1413 }
1414 break;
1415 }
1416 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) {
1417 kill_save_errno(pid, SIGKILL);
1418 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001419 }
1420 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001421 if (expected_grandchild && expected_grandchild == found_grandchild) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001422 worked:
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001423 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001424 if (debug_flag)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001425 fprintf(stderr, "ptrace_setoptions = %#x\n",
1426 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001427 return 0;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001428 }
1429 error_msg("Test for PTRACE_O_TRACECLONE failed, "
1430 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001431 return 1;
Wang Chaob13c0de2010-11-12 17:25:19 +08001432}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001433
1434/*
1435 * Test whether the kernel support PTRACE_O_TRACESYSGOOD.
1436 * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it,
1437 * and then see whether it will stop with (SIGTRAP | 0x80).
1438 *
1439 * Use of this option enables correct handling of user-generated SIGTRAPs,
1440 * and SIGTRAPs generated by special instructions such as int3 on x86:
Denys Vlasenko329fa392014-04-10 09:57:17 +02001441
1442# compile with: gcc -nostartfiles -nostdlib -o int3 int3.S
1443_start: .globl _start
1444 int3
1445 movl $42, %ebx
1446 movl $1, %eax
1447 int $0x80
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001448 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001449static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001450test_ptrace_setoptions_for_all(void)
1451{
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001452 const unsigned int test_options = PTRACE_O_TRACESYSGOOD |
1453 PTRACE_O_TRACEEXEC;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001454 int pid;
1455 int it_worked = 0;
1456
Denys Vlasenko05f32512013-02-26 12:00:34 +01001457 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001458 if (NOMMU_SYSTEM)
Denys Vlasenko05f32512013-02-26 12:00:34 +01001459 goto worked; /* be bold, and pretend that test succeeded */
Mike Frysinger7ff5ed92012-04-05 01:52:25 -04001460
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001461 pid = fork();
1462 if (pid < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001463 perror_msg_and_die("fork");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001464
1465 if (pid == 0) {
1466 pid = getpid();
1467 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001468 /* Note: exits with exitcode 1 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001469 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1470 __func__);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001471 kill(pid, SIGSTOP);
1472 _exit(0); /* parent should see entry into this syscall */
1473 }
1474
1475 while (1) {
1476 int status, tracee_pid;
1477
1478 errno = 0;
1479 tracee_pid = wait(&status);
1480 if (tracee_pid <= 0) {
1481 if (errno == EINTR)
1482 continue;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001483 kill_save_errno(pid, SIGKILL);
1484 perror_msg_and_die("%s: unexpected wait result %d",
1485 __func__, tracee_pid);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001486 }
Denys Vlasenko75422762011-05-27 14:36:01 +02001487 if (WIFEXITED(status)) {
1488 if (WEXITSTATUS(status) == 0)
1489 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001490 error_msg_and_die("%s: unexpected exit status %u",
1491 __func__, WEXITSTATUS(status));
1492 }
1493 if (WIFSIGNALED(status)) {
1494 error_msg_and_die("%s: unexpected signal %u",
1495 __func__, WTERMSIG(status));
Denys Vlasenko75422762011-05-27 14:36:01 +02001496 }
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001497 if (!WIFSTOPPED(status)) {
1498 kill(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001499 error_msg_and_die("%s: unexpected wait status %x",
1500 __func__, status);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001501 }
1502 if (WSTOPSIG(status) == SIGSTOP) {
1503 /*
1504 * We don't check "options aren't accepted" error.
1505 * If it happens, we'll never get (SIGTRAP | 0x80),
1506 * and thus will decide to not use the option.
1507 * IOW: the outcome of the test will be correct.
1508 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001509 if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0
1510 && errno != EINVAL && errno != EIO)
1511 perror_msg("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001512 }
1513 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
1514 it_worked = 1;
1515 }
1516 if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001517 kill_save_errno(pid, SIGKILL);
Denys Vlasenko75422762011-05-27 14:36:01 +02001518 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001519 }
1520 }
1521
1522 if (it_worked) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001523 worked:
Denys Vlasenko75422762011-05-27 14:36:01 +02001524 syscall_trap_sig = (SIGTRAP | 0x80);
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001525 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001526 if (debug_flag)
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001527 fprintf(stderr, "ptrace_setoptions = %#x\n",
1528 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001529 return 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001530 }
1531
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001532 error_msg("Test for PTRACE_O_TRACESYSGOOD failed, "
1533 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001534 return 1;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001535}
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001536
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001537#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001538static void
1539test_ptrace_seize(void)
1540{
1541 int pid;
1542
Denys Vlasenko05f32512013-02-26 12:00:34 +01001543 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001544 if (NOMMU_SYSTEM) {
1545 post_attach_sigstop = 0; /* this sets use_seize to 1 */
1546 return;
1547 }
1548
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001549 pid = fork();
1550 if (pid < 0)
1551 perror_msg_and_die("fork");
1552
1553 if (pid == 0) {
1554 pause();
1555 _exit(0);
1556 }
1557
1558 /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After
1559 * attaching tracee continues to run unless a trap condition occurs.
1560 * PTRACE_SEIZE doesn't affect signal or group stop state.
1561 */
Denys Vlasenko26bc0602012-07-10 16:36:32 +02001562 if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001563 post_attach_sigstop = 0; /* this sets use_seize to 1 */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001564 } else if (debug_flag) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001565 fprintf(stderr, "PTRACE_SEIZE doesn't work\n");
1566 }
1567
1568 kill(pid, SIGKILL);
1569
1570 while (1) {
1571 int status, tracee_pid;
1572
1573 errno = 0;
1574 tracee_pid = waitpid(pid, &status, 0);
1575 if (tracee_pid <= 0) {
1576 if (errno == EINTR)
1577 continue;
1578 perror_msg_and_die("%s: unexpected wait result %d",
1579 __func__, tracee_pid);
1580 }
1581 if (WIFSIGNALED(status)) {
1582 return;
1583 }
1584 error_msg_and_die("%s: unexpected wait status %x",
1585 __func__, status);
1586 }
1587}
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001588#else /* !USE_SEIZE */
1589# define test_ptrace_seize() ((void)0)
1590#endif
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001591
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001592static unsigned
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001593get_os_release(void)
1594{
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001595 unsigned rel;
1596 const char *p;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001597 struct utsname u;
1598 if (uname(&u) < 0)
1599 perror_msg_and_die("uname");
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001600 /* u.release has this form: "3.2.9[-some-garbage]" */
1601 rel = 0;
1602 p = u.release;
1603 for (;;) {
1604 if (!(*p >= '0' && *p <= '9'))
1605 error_msg_and_die("Bad OS release string: '%s'", u.release);
1606 /* Note: this open-codes KERNEL_VERSION(): */
1607 rel = (rel << 8) | atoi(p);
1608 if (rel >= KERNEL_VERSION(1,0,0))
1609 break;
1610 while (*p >= '0' && *p <= '9')
1611 p++;
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001612 if (*p != '.') {
1613 if (rel >= KERNEL_VERSION(0,1,0)) {
1614 /* "X.Y-something" means "X.Y.0" */
1615 rel <<= 8;
1616 break;
1617 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001618 error_msg_and_die("Bad OS release string: '%s'", u.release);
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001619 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001620 p++;
1621 }
1622 return rel;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001623}
1624
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01001625/*
1626 * Initialization part of main() was eating much stack (~0.5k),
1627 * which was unused after init.
1628 * We can reuse it if we move init code into a separate function.
1629 *
1630 * Don't want main() to inline us and defeat the reason
1631 * we have a separate function.
1632 */
1633static void __attribute__ ((noinline))
1634init(int argc, char *argv[])
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001635{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001636 struct tcb *tcp;
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001637 int c, i;
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001638 int optF = 0;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001639 unsigned int tcbi;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001640 struct sigaction sa;
1641
Dmitry V. Levin08b623e2007-10-08 21:04:41 +00001642 progname = argv[0] ? argv[0] : "strace";
1643
Denys Vlasenkoa5090542012-03-15 17:27:49 +01001644 /* Make sure SIGCHLD has the default action so that waitpid
1645 definitely works without losing track of children. The user
1646 should not have given us a bogus state to inherit, but he might
1647 have. Arguably we should detect SIG_IGN here and pass it on
1648 to children, but probably noone really needs that. */
1649 signal(SIGCHLD, SIG_DFL);
1650
Denys Vlasenko75422762011-05-27 14:36:01 +02001651 strace_tracer_pid = getpid();
1652
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001653 os_release = get_os_release();
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001654
Roland McGrathee9d4352002-12-18 04:16:10 +00001655 /* Allocate the initial tcbtab. */
1656 tcbtabsize = argc; /* Surely enough for all -p args. */
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001657 tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0]));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001658 if (!tcbtab)
1659 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001660 tcp = calloc(tcbtabsize, sizeof(*tcp));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001661 if (!tcp)
1662 die_out_of_memory();
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001663 for (tcbi = 0; tcbi < tcbtabsize; tcbi++)
1664 tcbtab[tcbi] = tcp++;
Roland McGrathee9d4352002-12-18 04:16:10 +00001665
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001666 shared_log = stderr;
Roland McGrath138c6a32006-01-12 09:50:49 +00001667 set_sortby(DEFAULT_SORTBY);
1668 set_personality(DEFAULT_PERSONALITY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001669 qualify("trace=all");
1670 qualify("abbrev=all");
1671 qualify("verbose=all");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001672#if DEFAULT_QUAL_FLAGS != (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
1673# error Bug in DEFAULT_QUAL_FLAGS
1674#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001675 qualify("signal=all");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001676 while ((c = getopt(argc, argv,
Mark Hillse53bf232014-05-28 17:52:40 +01001677 "+b:cCdfFhiqrtTvVwxyz"
Luca Clementi327064b2013-07-23 00:11:35 -07001678#ifdef USE_LIBUNWIND
1679 "k"
1680#endif
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001681 "D"
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001682 "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001683 switch (c) {
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001684 case 'b':
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001685 if (strcmp(optarg, "execve") != 0)
1686 error_msg_and_die("Syscall '%s' for -b isn't supported",
1687 optarg);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001688 detach_on_execve = 1;
1689 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001690 case 'c':
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001691 if (cflag == CFLAG_BOTH) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001692 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001693 }
1694 cflag = CFLAG_ONLY_STATS;
1695 break;
1696 case 'C':
1697 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001698 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001699 }
1700 cflag = CFLAG_BOTH;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001701 break;
1702 case 'd':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001703 debug_flag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001704 break;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001705 case 'D':
1706 daemonized_tracer = 1;
1707 break;
Roland McGrath41c48222008-07-18 00:25:10 +00001708 case 'F':
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001709 optF = 1;
1710 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001711 case 'f':
1712 followfork++;
1713 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001714 case 'h':
1715 usage(stdout, 0);
1716 break;
1717 case 'i':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001718 iflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001719 break;
1720 case 'q':
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01001721 qflag++;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001722 break;
1723 case 'r':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001724 rflag = 1;
1725 /* fall through to tflag++ */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001726 case 't':
1727 tflag++;
1728 break;
1729 case 'T':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001730 Tflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001731 break;
Mark Hillse53bf232014-05-28 17:52:40 +01001732 case 'w':
1733 count_wallclock = 1;
1734 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001735 case 'x':
1736 xflag++;
1737 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001738 case 'y':
Dmitry V. Levin45e7b182014-08-08 23:38:26 +00001739 show_fd_path++;
Grant Edwards8a082772011-04-07 20:25:40 +00001740 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001741 case 'v':
1742 qualify("abbrev=none");
1743 break;
1744 case 'V':
Roland McGrath9c9a2532003-02-20 02:56:29 +00001745 printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001746 exit(0);
1747 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001748 case 'z':
1749 not_failing_only = 1;
1750 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001751 case 'a':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001752 acolumn = string_to_uint(optarg);
Denys Vlasenko102ec492011-08-25 01:27:59 +02001753 if (acolumn < 0)
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001754 error_opt_arg(c, optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001755 break;
1756 case 'e':
1757 qualify(optarg);
1758 break;
1759 case 'o':
1760 outfname = strdup(optarg);
1761 break;
1762 case 'O':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001763 i = string_to_uint(optarg);
1764 if (i < 0)
1765 error_opt_arg(c, optarg);
1766 set_overhead(i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001767 break;
1768 case 'p':
Denys Vlasenkoe8172b72012-03-09 13:01:04 +01001769 process_opt_p_list(optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001770 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001771 case 'P':
Denys Vlasenko7239dbc2013-03-05 15:46:34 +01001772 pathtrace_select(optarg);
Grant Edwards8a082772011-04-07 20:25:40 +00001773 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001774 case 's':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001775 i = string_to_uint(optarg);
1776 if (i < 0)
1777 error_opt_arg(c, optarg);
1778 max_strlen = i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001779 break;
1780 case 'S':
1781 set_sortby(optarg);
1782 break;
1783 case 'u':
1784 username = strdup(optarg);
1785 break;
Luca Clementi327064b2013-07-23 00:11:35 -07001786#ifdef USE_LIBUNWIND
1787 case 'k':
1788 stack_trace_enabled = true;
1789 break;
1790#endif
Roland McGrathde6e5332003-01-24 04:31:23 +00001791 case 'E':
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001792 if (putenv(optarg) < 0)
1793 die_out_of_memory();
Roland McGrathde6e5332003-01-24 04:31:23 +00001794 break;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001795 case 'I':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001796 opt_intr = string_to_uint(optarg);
1797 if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS)
1798 error_opt_arg(c, optarg);
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001799 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001800 default:
1801 usage(stderr, 1);
1802 break;
1803 }
1804 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001805 argv += optind;
1806 /* argc -= optind; - no need, argc is not used below */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001807
Denys Vlasenko102ec492011-08-25 01:27:59 +02001808 acolumn_spaces = malloc(acolumn + 1);
1809 if (!acolumn_spaces)
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001810 die_out_of_memory();
Denys Vlasenko102ec492011-08-25 01:27:59 +02001811 memset(acolumn_spaces, ' ', acolumn);
1812 acolumn_spaces[acolumn] = '\0';
1813
Denys Vlasenko837399a2012-01-24 11:37:03 +01001814 /* Must have PROG [ARGS], or -p PID. Not both. */
Denys Vlasenkofd883382012-03-09 13:03:41 +01001815 if (!argv[0] == !nprocs)
Roland McGrathce0d1542003-11-11 21:24:23 +00001816 usage(stderr, 1);
1817
Denys Vlasenkofd883382012-03-09 13:03:41 +01001818 if (nprocs != 0 && daemonized_tracer) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001819 error_msg_and_die("-D and -p are mutually exclusive");
Wang Chaod322a4b2010-08-05 14:30:11 +08001820 }
1821
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001822 if (!followfork)
1823 followfork = optF;
1824
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001825 if (followfork >= 2 && cflag) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001826 error_msg_and_die("(-c or -C) and -ff are mutually exclusive");
Roland McGrathcb9def62006-04-25 07:48:03 +00001827 }
1828
Mark Hillse53bf232014-05-28 17:52:40 +01001829 if (count_wallclock && !cflag) {
1830 error_msg_and_die("-w must be given with (-c or -C)");
1831 }
1832
Dmitry V. Levin2727aae2014-06-03 13:20:05 +00001833 if (cflag == CFLAG_ONLY_STATS) {
1834 if (iflag)
1835 error_msg("-%c has no effect with -c", 'i');
1836#ifdef USE_LIBUNWIND
1837 if (stack_trace_enabled)
1838 error_msg("-%c has no effect with -c", 'k');
1839#endif
1840 if (rflag)
1841 error_msg("-%c has no effect with -c", 'r');
1842 if (tflag)
1843 error_msg("-%c has no effect with -c", 't');
1844 if (Tflag)
1845 error_msg("-%c has no effect with -c", 'T');
1846 if (show_fd_path)
1847 error_msg("-%c has no effect with -c", 'y');
1848 }
1849
1850#ifdef USE_LIBUNWIND
1851 if (stack_trace_enabled)
1852 unwind_init();
1853#endif
1854
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001855 /* See if they want to run as another user. */
1856 if (username != NULL) {
1857 struct passwd *pent;
1858
1859 if (getuid() != 0 || geteuid() != 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001860 error_msg_and_die("You must be root to use the -u option");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001861 }
Denys Vlasenko5d645812011-08-20 12:48:18 +02001862 pent = getpwnam(username);
1863 if (pent == NULL) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001864 error_msg_and_die("Cannot find user '%s'", username);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001865 }
1866 run_uid = pent->pw_uid;
1867 run_gid = pent->pw_gid;
1868 }
1869 else {
1870 run_uid = getuid();
1871 run_gid = getgid();
1872 }
1873
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001874 /*
1875 * On any reasonably recent Linux kernel (circa about 2.5.46)
1876 * need_fork_exec_workarounds should stay 0 after these tests:
1877 */
1878 /*need_fork_exec_workarounds = 0; - already is */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001879 if (followfork)
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001880 need_fork_exec_workarounds = test_ptrace_setoptions_followfork();
1881 need_fork_exec_workarounds |= test_ptrace_setoptions_for_all();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001882 test_ptrace_seize();
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001883
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001884 /* Check if they want to redirect the output. */
1885 if (outfname) {
Roland McGrath37b9a662003-11-07 02:26:54 +00001886 /* See if they want to pipe the output. */
1887 if (outfname[0] == '|' || outfname[0] == '!') {
1888 /*
1889 * We can't do the <outfname>.PID funny business
1890 * when using popen, so prohibit it.
1891 */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001892 if (followfork >= 2)
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001893 error_msg_and_die("Piping the output and -ff are mutually exclusive");
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001894 shared_log = strace_popen(outfname + 1);
Roland McGrath37b9a662003-11-07 02:26:54 +00001895 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001896 else if (followfork < 2)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001897 shared_log = strace_fopen(outfname);
Denys Vlasenko328bf252012-03-12 23:34:13 +01001898 } else {
1899 /* -ff without -o FILE is the same as single -f */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001900 if (followfork >= 2)
Denys Vlasenko328bf252012-03-12 23:34:13 +01001901 followfork = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001902 }
1903
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001904 if (!outfname || outfname[0] == '|' || outfname[0] == '!') {
Denys Vlasenkoa677da52012-01-24 11:31:51 +01001905 char *buf = malloc(BUFSIZ);
1906 if (!buf)
1907 die_out_of_memory();
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001908 setvbuf(shared_log, buf, _IOLBF, BUFSIZ);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001909 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001910 if (outfname && argv[0]) {
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001911 if (!opt_intr)
1912 opt_intr = INTR_NEVER;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001913 qflag = 1;
Roland McGrath36931052003-06-03 01:35:20 +00001914 }
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001915 if (!opt_intr)
1916 opt_intr = INTR_WHILE_WAIT;
Wang Chaob13c0de2010-11-12 17:25:19 +08001917
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001918 /* argv[0] -pPID -oFILE Default interactive setting
1919 * yes 0 0 INTR_WHILE_WAIT
1920 * no 1 0 INTR_WHILE_WAIT
1921 * yes 0 1 INTR_NEVER
1922 * no 1 1 INTR_WHILE_WAIT
Roland McGrath54cc1c82007-11-03 23:34:11 +00001923 */
1924
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001925 sigemptyset(&empty_set);
1926 sigemptyset(&blocked_set);
1927
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001928 /* startup_child() must be called before the signal handlers get
1929 * installed below as they are inherited into the spawned process.
1930 * Also we do not need to be protected by them as during interruption
1931 * in the startup_child() mode we kill the spawned process anyway.
1932 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001933 if (argv[0]) {
Dmitry V. Levin1d2435b2013-05-14 22:35:46 +00001934 if (!NOMMU_SYSTEM || daemonized_tracer)
1935 hide_log_until_execve = 1;
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001936 skip_one_b_execve = 1;
Denys Vlasenko837399a2012-01-24 11:37:03 +01001937 startup_child(argv);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001938 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001939
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001940 sa.sa_handler = SIG_IGN;
1941 sigemptyset(&sa.sa_mask);
1942 sa.sa_flags = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001943 sigaction(SIGTTOU, &sa, NULL); /* SIG_IGN */
1944 sigaction(SIGTTIN, &sa, NULL); /* SIG_IGN */
1945 if (opt_intr != INTR_ANYWHERE) {
1946 if (opt_intr == INTR_BLOCK_TSTP_TOO)
1947 sigaction(SIGTSTP, &sa, NULL); /* SIG_IGN */
1948 /*
1949 * In interactive mode (if no -o OUTFILE, or -p PID is used),
1950 * fatal signals are blocked while syscall stop is processed,
1951 * and acted on in between, when waiting for new syscall stops.
1952 * In non-interactive mode, signals are ignored.
1953 */
1954 if (opt_intr == INTR_WHILE_WAIT) {
1955 sigaddset(&blocked_set, SIGHUP);
1956 sigaddset(&blocked_set, SIGINT);
1957 sigaddset(&blocked_set, SIGQUIT);
1958 sigaddset(&blocked_set, SIGPIPE);
1959 sigaddset(&blocked_set, SIGTERM);
1960 sa.sa_handler = interrupt;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001961 }
1962 /* SIG_IGN, or set handler for these */
1963 sigaction(SIGHUP, &sa, NULL);
1964 sigaction(SIGINT, &sa, NULL);
1965 sigaction(SIGQUIT, &sa, NULL);
1966 sigaction(SIGPIPE, &sa, NULL);
1967 sigaction(SIGTERM, &sa, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001968 }
Denys Vlasenkofd883382012-03-09 13:03:41 +01001969 if (nprocs != 0 || daemonized_tracer)
Roland McGrath02203312007-06-11 22:06:31 +00001970 startup_attach();
Roland McGrath02203312007-06-11 22:06:31 +00001971
Denys Vlasenkofd883382012-03-09 13:03:41 +01001972 /* Do we want pids printed in our -o OUTFILE?
1973 * -ff: no (every pid has its own file); or
1974 * -f: yes (there can be more pids in the future); or
1975 * -p PID1,PID2: yes (there are already more than one pid)
1976 */
1977 print_pid_pfx = (outfname && followfork < 2 && (followfork == 1 || nprocs > 1));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001978}
1979
Denys Vlasenkoeebb04d2012-01-27 15:24:48 +01001980static struct tcb *
Roland McGrath54e931f2010-09-14 18:59:20 -07001981pid2tcb(int pid)
1982{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001983 unsigned int i;
Roland McGrath54e931f2010-09-14 18:59:20 -07001984
1985 if (pid <= 0)
1986 return NULL;
1987
1988 for (i = 0; i < tcbtabsize; i++) {
1989 struct tcb *tcp = tcbtab[i];
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001990 if (tcp->pid == pid)
Roland McGrath54e931f2010-09-14 18:59:20 -07001991 return tcp;
1992 }
1993
1994 return NULL;
1995}
1996
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001997static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001998cleanup(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001999{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00002000 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002001 struct tcb *tcp;
Denys Vlasenko35218842012-01-29 21:17:56 +01002002 int fatal_sig;
2003
2004 /* 'interrupted' is a volatile object, fetch it only once */
2005 fatal_sig = interrupted;
2006 if (!fatal_sig)
2007 fatal_sig = SIGTERM;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002008
Roland McGrathee9d4352002-12-18 04:16:10 +00002009 for (i = 0; i < tcbtabsize; i++) {
2010 tcp = tcbtab[i];
Denys Vlasenkofadbf662013-06-26 14:14:29 +02002011 if (!tcp->pid)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002012 continue;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002013 if (debug_flag)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002014 fprintf(stderr,
2015 "cleanup: looking at pid %u\n", tcp->pid);
Denys Vlasenkofadbf662013-06-26 14:14:29 +02002016 if (tcp->pid == strace_child) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002017 kill(tcp->pid, SIGCONT);
Denys Vlasenkoa3559252012-01-29 16:43:51 +01002018 kill(tcp->pid, fatal_sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002019 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002020 detach(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002021 }
2022 if (cflag)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002023 call_summary(shared_log);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002024}
2025
2026static void
Denys Vlasenko12014262011-05-30 14:00:14 +02002027interrupt(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002028{
Denys Vlasenkoa3559252012-01-29 16:43:51 +01002029 interrupted = sig;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002030}
2031
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002032static void
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002033print_debug_info(const int pid, const int status)
2034{
2035 const unsigned int event = (unsigned int) status >> 16;
2036 char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16];
2037 char evbuf[sizeof(",EVENT_VFORK_DONE (%u)") + sizeof(int)*3 /*paranoia:*/ + 16];
2038
2039 strcpy(buf, "???");
2040 if (WIFSIGNALED(status))
2041#ifdef WCOREDUMP
2042 sprintf(buf, "WIFSIGNALED,%ssig=%s",
2043 WCOREDUMP(status) ? "core," : "",
2044 signame(WTERMSIG(status)));
2045#else
2046 sprintf(buf, "WIFSIGNALED,sig=%s",
2047 signame(WTERMSIG(status)));
2048#endif
2049 if (WIFEXITED(status))
2050 sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status));
2051 if (WIFSTOPPED(status))
2052 sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status)));
2053#ifdef WIFCONTINUED
2054 /* Should never be seen */
2055 if (WIFCONTINUED(status))
2056 strcpy(buf, "WIFCONTINUED");
2057#endif
2058 evbuf[0] = '\0';
2059 if (event != 0) {
2060 static const char *const event_names[] = {
2061 [PTRACE_EVENT_CLONE] = "CLONE",
2062 [PTRACE_EVENT_FORK] = "FORK",
2063 [PTRACE_EVENT_VFORK] = "VFORK",
2064 [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE",
2065 [PTRACE_EVENT_EXEC] = "EXEC",
2066 [PTRACE_EVENT_EXIT] = "EXIT",
2067 /* [PTRACE_EVENT_STOP (=128)] would make biggish array */
2068 };
2069 const char *e = "??";
2070 if (event < ARRAY_SIZE(event_names))
2071 e = event_names[event];
2072 else if (event == PTRACE_EVENT_STOP)
2073 e = "STOP";
2074 sprintf(evbuf, ",EVENT_%s (%u)", e, event);
2075 }
2076 fprintf(stderr, " [wait(0x%06x) = %u] %s%s\n", status, pid, buf, evbuf);
2077}
2078
2079static struct tcb *
2080maybe_allocate_tcb(const int pid, const int status)
2081{
2082 if (!WIFSTOPPED(status)) {
Dmitry V. Levinaa801922015-02-07 18:48:55 +00002083 if (detach_on_execve && pid == strace_child) {
2084 /* example: strace -bexecve sh -c 'exec true' */
2085 strace_child = 0;
2086 return NULL;
2087 }
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002088 /*
2089 * This can happen if we inherited an unknown child.
Dmitry V. Levinaa801922015-02-07 18:48:55 +00002090 * Example: (sleep 1 & exec strace true)
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002091 */
2092 error_msg("Exit of unknown pid %u ignored", pid);
2093 return NULL;
2094 }
2095 if (followfork) {
2096 /* We assume it's a fork/vfork/clone child */
2097 struct tcb *tcp = alloctcb(pid);
2098 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
2099 newoutf(tcp);
2100 if (!qflag)
2101 fprintf(stderr, "Process %d attached\n", pid);
2102 return tcp;
2103 } else {
2104 /* This can happen if a clone call used
2105 * CLONE_PTRACE itself.
2106 */
2107 ptrace(PTRACE_CONT, pid, (char *) 0, 0);
2108 error_msg("Stop of unknown pid %u seen, PTRACE_CONTed it", pid);
2109 return NULL;
2110 }
2111}
2112
2113static struct tcb *
2114maybe_switch_tcbs(struct tcb *tcp, const int pid)
2115{
2116 FILE *fp;
2117 struct tcb *execve_thread;
2118 long old_pid = 0;
2119
2120 if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) < 0)
2121 return tcp;
2122 /* Avoid truncation in pid2tcb() param passing */
2123 if (old_pid <= 0 || old_pid == pid)
2124 return tcp;
2125 if ((unsigned long) old_pid > UINT_MAX)
2126 return tcp;
2127 execve_thread = pid2tcb(old_pid);
2128 /* It should be !NULL, but I feel paranoid */
2129 if (!execve_thread)
2130 return tcp;
2131
2132 if (execve_thread->curcol != 0) {
2133 /*
2134 * One case we are here is -ff:
2135 * try "strace -oLOG -ff test/threaded_execve"
2136 */
2137 fprintf(execve_thread->outf, " <pid changed to %d ...>\n", pid);
2138 /*execve_thread->curcol = 0; - no need, see code below */
2139 }
2140 /* Swap output FILEs (needed for -ff) */
2141 fp = execve_thread->outf;
2142 execve_thread->outf = tcp->outf;
2143 tcp->outf = fp;
2144 /* And their column positions */
2145 execve_thread->curcol = tcp->curcol;
2146 tcp->curcol = 0;
2147 /* Drop leader, but close execve'd thread outfile (if -ff) */
2148 droptcb(tcp);
2149 /* Switch to the thread, reusing leader's outfile and pid */
2150 tcp = execve_thread;
2151 tcp->pid = pid;
2152 if (cflag != CFLAG_ONLY_STATS) {
2153 printleader(tcp);
2154 tprintf("+++ superseded by execve in pid %lu +++\n", old_pid);
2155 line_ended();
2156 tcp->flags |= TCB_REPRINT;
2157 }
2158
2159 return tcp;
2160}
2161
2162static void
2163print_signalled(struct tcb *tcp, const int pid, const int status)
2164{
Dmitry V. Levin2cd488b2015-02-07 19:41:48 +00002165 if (pid == strace_child) {
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002166 exit_code = 0x100 | WTERMSIG(status);
Dmitry V. Levin2cd488b2015-02-07 19:41:48 +00002167 strace_child = 0;
2168 }
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002169
2170 if (cflag != CFLAG_ONLY_STATS
2171 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)
2172 ) {
2173 printleader(tcp);
2174#ifdef WCOREDUMP
2175 tprintf("+++ killed by %s %s+++\n",
2176 signame(WTERMSIG(status)),
2177 WCOREDUMP(status) ? "(core dumped) " : "");
2178#else
2179 tprintf("+++ killed by %s +++\n",
2180 signame(WTERMSIG(status)));
2181#endif
2182 line_ended();
2183 }
2184}
2185
2186static void
2187print_exited(struct tcb *tcp, const int pid, const int status)
2188{
Dmitry V. Levin2cd488b2015-02-07 19:41:48 +00002189 if (pid == strace_child) {
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002190 exit_code = WEXITSTATUS(status);
Dmitry V. Levin2cd488b2015-02-07 19:41:48 +00002191 strace_child = 0;
2192 }
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002193
2194 if (cflag != CFLAG_ONLY_STATS &&
2195 qflag < 2) {
2196 printleader(tcp);
2197 tprintf("+++ exited with %d +++\n", WEXITSTATUS(status));
2198 line_ended();
2199 }
2200}
2201
2202static void
2203print_stopped(struct tcb *tcp, const siginfo_t *si, const unsigned int sig)
2204{
2205 if (cflag != CFLAG_ONLY_STATS
2206 && !hide_log_until_execve
2207 && (qual_flags[sig] & QUAL_SIGNAL)
2208 ) {
2209 printleader(tcp);
2210 if (si) {
2211 tprintf("--- %s ", signame(sig));
2212 printsiginfo(si, verbose(tcp));
2213 tprints(" ---\n");
2214 } else
2215 tprintf("--- stopped by %s ---\n", signame(sig));
2216 line_ended();
2217 }
2218}
2219
2220static bool
2221startup_tcb(struct tcb *tcp)
2222{
2223 if (debug_flag)
2224 fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n",
2225 tcp->pid);
2226
2227 tcp->flags &= ~TCB_STARTUP;
2228
2229 if (tcp->flags & TCB_BPTSET) {
2230 /*
2231 * One example is a breakpoint inherited from
2232 * parent through fork().
2233 */
2234 if (clearbpt(tcp) < 0) {
2235 /* Pretty fatal */
2236 droptcb(tcp);
2237 exit_code = 1;
2238 return false;
2239 }
2240 }
2241
2242 if (!use_seize && ptrace_setoptions) {
2243 if (debug_flag)
2244 fprintf(stderr, "setting opts 0x%x on pid %d\n",
2245 ptrace_setoptions, tcp->pid);
2246 if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) {
2247 if (errno != ESRCH) {
2248 /* Should never happen, really */
2249 perror_msg_and_die("PTRACE_SETOPTIONS");
2250 }
2251 }
2252 }
2253
2254 return true;
2255}
2256
2257/* Returns true iff the main trace loop has to continue. */
2258static bool
Denys Vlasenko12014262011-05-30 14:00:14 +02002259trace(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002260{
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002261 int pid;
2262 int wait_errno;
2263 int status;
2264 bool stopped;
2265 unsigned int sig;
2266 unsigned int event;
2267 struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002268 struct rusage ru;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002269
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002270 if (interrupted)
2271 return false;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002272
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002273 if (popen_pid != 0 && nprocs == 0)
2274 return false;
Denys Vlasenko725dd422013-06-20 11:06:58 +02002275
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002276 if (interactive)
2277 sigprocmask(SIG_SETMASK, &empty_set, NULL);
2278 pid = wait4(-1, &status, __WALL, (cflag ? &ru : NULL));
2279 wait_errno = errno;
2280 if (interactive)
2281 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
Denys Vlasenko7c41ce22013-07-08 13:55:04 +02002282
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002283 if (pid < 0) {
2284 if (wait_errno == EINTR)
2285 return true;
2286 if (nprocs == 0 && wait_errno == ECHILD)
2287 return false;
2288 /*
2289 * If nprocs > 0, ECHILD is not expected,
2290 * treat it as any other error here:
2291 */
2292 errno = wait_errno;
2293 perror_msg_and_die("wait4(__WALL)");
2294 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002295
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002296 if (pid == popen_pid) {
2297 if (!WIFSTOPPED(status))
2298 popen_pid = 0;
2299 return true;
2300 }
Denys Vlasenko725dd422013-06-20 11:06:58 +02002301
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002302 if (debug_flag)
2303 print_debug_info(pid, status);
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002304
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002305 /* Look up 'pid' in our table. */
2306 tcp = pid2tcb(pid);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002307
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002308 if (!tcp) {
2309 tcp = maybe_allocate_tcb(pid, status);
2310 if (!tcp)
2311 return true;
2312 }
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002313
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002314 clear_regs();
2315 if (WIFSTOPPED(status))
2316 get_regs(pid);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002317
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002318 event = (unsigned int) status >> 16;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002319
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002320 if (event == PTRACE_EVENT_EXEC) {
2321 /*
2322 * Under Linux, execve changes pid to thread leader's pid,
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002323 * and we see this changed pid on EVENT_EXEC and later,
2324 * execve sysexit. Leader "disappears" without exit
2325 * notification. Let user know that, drop leader's tcb,
2326 * and fix up pid in execve thread's tcb.
2327 * Effectively, execve thread's tcb replaces leader's tcb.
2328 *
2329 * BTW, leader is 'stuck undead' (doesn't report WIFEXITED
2330 * on exit syscall) in multithreaded programs exactly
2331 * in order to handle this case.
2332 *
2333 * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0.
2334 * On 2.6 and earlier, it can return garbage.
2335 */
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002336 if (os_release >= KERNEL_VERSION(3,0,0))
2337 tcp = maybe_switch_tcbs(tcp, pid);
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002338
Dmitry V. Levine6908132015-02-07 17:47:53 +00002339 if (detach_on_execve && !skip_one_b_execve) {
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002340 detach(tcp); /* do "-b execve" thingy */
Dmitry V. Levine6908132015-02-07 17:47:53 +00002341 return true;
2342 }
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002343 skip_one_b_execve = 0;
2344 }
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002345
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002346 /* Set current output file */
2347 current_tcp = tcp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002348
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002349 if (cflag) {
2350 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2351 tcp->stime = ru.ru_stime;
2352 }
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002353
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002354 if (WIFSIGNALED(status)) {
2355 print_signalled(tcp, pid, status);
2356 droptcb(tcp);
2357 return true;
2358 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002359
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002360 if (WIFEXITED(status)) {
2361 print_exited(tcp, pid, status);
2362 droptcb(tcp);
2363 return true;
2364 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002365
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002366 if (!WIFSTOPPED(status)) {
2367 /*
2368 * Neither signalled, exited or stopped.
2369 * How could that be?
2370 */
2371 error_msg("pid %u not stopped!", pid);
2372 droptcb(tcp);
2373 return true;
2374 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002375
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002376 /* Is this the very first time we see this tracee stopped? */
2377 if (tcp->flags & TCB_STARTUP) {
2378 if (!startup_tcb(tcp))
2379 return false;
2380 }
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002381
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002382 sig = WSTOPSIG(status);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002383
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002384 if (event != 0) {
2385 /* Ptrace event */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002386#if USE_SEIZE
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002387 if (event == PTRACE_EVENT_STOP) {
2388 /*
2389 * PTRACE_INTERRUPT-stop or group-stop.
2390 * PTRACE_INTERRUPT-stop has sig == SIGTRAP here.
2391 */
2392 switch (sig) {
2393 case SIGSTOP:
2394 case SIGTSTP:
2395 case SIGTTIN:
2396 case SIGTTOU:
2397 stopped = true;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002398 goto show_stopsig;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002399 }
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002400 }
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002401#endif
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002402 goto restart_tracee_with_sig_0;
2403 }
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002404
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002405 /*
2406 * Is this post-attach SIGSTOP?
2407 * Interestingly, the process may stop
2408 * with STOPSIG equal to some other signal
2409 * than SIGSTOP if we happend to attach
2410 * just before the process takes a signal.
2411 */
2412 if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
2413 if (debug_flag)
2414 fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid);
2415 tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
2416 goto restart_tracee_with_sig_0;
2417 }
2418
2419 if (sig != syscall_trap_sig) {
2420 siginfo_t si;
2421
2422 /*
2423 * True if tracee is stopped by signal
2424 * (as opposed to "tracee received signal").
2425 * TODO: shouldn't we check for errno == EINVAL too?
2426 * We can get ESRCH instead, you know...
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002427 */
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002428 stopped = ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0;
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002429#if USE_SEIZE
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002430show_stopsig:
Denys Vlasenko67038162012-01-29 16:46:46 +01002431#endif
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002432 print_stopped(tcp, stopped ? NULL : &si, sig);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002433
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002434 if (!stopped)
2435 /* It's signal-delivery-stop. Inject the signal */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002436 goto restart_tracee;
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002437
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002438 /* It's group-stop */
2439 if (use_seize) {
2440 /*
2441 * This ends ptrace-stop, but does *not* end group-stop.
2442 * This makes stopping signals work properly on straced process
2443 * (that is, process really stops. It used to continue to run).
Denys Vlasenkof2025022012-03-09 15:29:45 +01002444 */
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002445 if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) {
2446 /* Note: ptrace_restart emitted error message */
2447 exit_code = 1;
2448 return false;
2449 }
2450 return true;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002451 }
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002452 /* We don't have PTRACE_LISTEN support... */
2453 goto restart_tracee;
2454 }
2455
2456 /* We handled quick cases, we are permitted to interrupt now. */
2457 if (interrupted)
2458 return false;
2459
2460 /*
2461 * This should be syscall entry or exit.
2462 * (Or it still can be that pesky post-execve SIGTRAP!)
2463 * Handle it.
2464 */
2465 if (trace_syscall(tcp) < 0) {
2466 /*
2467 * ptrace() failed in trace_syscall().
2468 * Likely a result of process disappearing mid-flight.
2469 * Observed case: exit_group() or SIGKILL terminating
2470 * all processes in thread group.
2471 * We assume that ptrace error was caused by process death.
2472 * We used to detach(tcp) here, but since we no longer
2473 * implement "detach before death" policy/hack,
2474 * we can let this process to report its death to us
2475 * normally, via WIFEXITED or WIFSIGNALED wait status.
2476 */
2477 return true;
2478 }
2479
2480restart_tracee_with_sig_0:
2481 sig = 0;
2482
2483restart_tracee:
2484 if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) {
2485 /* Note: ptrace_restart emitted error message */
2486 exit_code = 1;
2487 return false;
2488 }
2489
2490 return true;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002491}
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002492
2493int
2494main(int argc, char *argv[])
2495{
2496 init(argc, argv);
2497
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002498 /*
2499 * Run main tracing loop.
2500 *
2501 * Used to be "while (nprocs != 0)", but in this testcase:
2502 * int main() { _exit(!!fork()); }
2503 * under strace -f, parent sometimes (rarely) manages
2504 * to exit before we see the first stop of the child,
2505 * and we are losing track of it:
2506 * 19923 clone(...) = 19924
2507 * 19923 exit_group(1) = ?
2508 * 19923 +++ exited with 1 +++
2509 * Waiting for ECHILD works better.
2510 * (However, if -o|logger is in use, we can't do that.
2511 * Can work around that by double-forking the logger,
2512 * but that loses the ability to wait for its completion on exit.
2513 * Oh well...)
2514 */
2515 while (trace())
2516 ;
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002517
2518 cleanup();
2519 fflush(NULL);
Dmitry V. Levincf534362012-07-12 20:54:46 +00002520 if (shared_log != stderr)
2521 fclose(shared_log);
2522 if (popen_pid) {
2523 while (waitpid(popen_pid, NULL, 0) < 0 && errno == EINTR)
2524 ;
2525 }
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002526 if (exit_code > 0xff) {
2527 /* Avoid potential core file clobbering. */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00002528 struct_rlimit rlim = {0, 0};
2529 set_rlimit(RLIMIT_CORE, &rlim);
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002530
2531 /* Child was killed by a signal, mimic that. */
2532 exit_code &= 0xff;
2533 signal(exit_code, SIG_DFL);
2534 raise(exit_code);
2535 /* Paranoia - what if this signal is not fatal?
2536 Exit with 128 + signo then. */
2537 exit_code += 128;
2538 }
2539
2540 return exit_code;
2541}