blob: a02ab040cb209341fca69af5884bfe0251e77118 [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
Dmitry V. Levinfadf3792015-02-13 00:26:38 +000048
49#include "ptrace.h"
50
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010051/* In some libc, these aren't declared. Do it ourself: */
Denys Vlasenko96d5a762008-12-29 19:13:27 +000052extern char **environ;
Denys Vlasenko418d66a2009-01-17 01:52:54 +000053extern int optind;
54extern char *optarg;
Denys Vlasenko96d5a762008-12-29 19:13:27 +000055
Luca Clementi327064b2013-07-23 00:11:35 -070056#ifdef USE_LIBUNWIND
57/* if this is true do the stack trace for every system call */
58bool stack_trace_enabled = false;
59#endif
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010060
61#if defined __NR_tkill
62# define my_tkill(tid, sig) syscall(__NR_tkill, (tid), (sig))
63#else
64 /* kill() may choose arbitrarily the target task of the process group
65 while we later wait on a that specific TID. PID process waits become
66 TID task specific waits for a process under ptrace(2). */
Denys Vlasenko978fbc92012-09-13 10:28:43 +020067# warning "tkill(2) not available, risk of strace hangs!"
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010068# define my_tkill(tid, sig) kill((tid), (sig))
69#endif
70
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +010071/* Glue for systems without a MMU that cannot provide fork() */
72#if !defined(HAVE_FORK)
73# undef NOMMU_SYSTEM
74# define NOMMU_SYSTEM 1
75#endif
76#if NOMMU_SYSTEM
77# define fork() vfork()
78#endif
79
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010080cflag_t cflag = CFLAG_NONE;
81unsigned int followfork = 0;
Denys Vlasenkof44cce42011-06-21 14:34:10 +020082unsigned int ptrace_setoptions = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010083unsigned int xflag = 0;
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +010084bool need_fork_exec_workarounds = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010085bool debug_flag = 0;
86bool Tflag = 0;
Anton Blancharda34dead2013-07-12 12:22:06 +020087bool iflag = 0;
Mark Hillse53bf232014-05-28 17:52:40 +010088bool count_wallclock = 0;
Daniel P. Berrange01997cf2013-05-13 11:30:55 +010089unsigned int qflag = 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020090/* Which WSTOPSIG(status) value marks syscall traps? */
Denys Vlasenko75422762011-05-27 14:36:01 +020091static unsigned int syscall_trap_sig = SIGTRAP;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010092static unsigned int tflag = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010093static bool rflag = 0;
94static bool print_pid_pfx = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +010095
96/* -I n */
97enum {
98 INTR_NOT_SET = 0,
99 INTR_ANYWHERE = 1, /* don't block/ignore any signals */
100 INTR_WHILE_WAIT = 2, /* block fatal signals while decoding syscall. default */
101 INTR_NEVER = 3, /* block fatal signals. default if '-o FILE PROG' */
102 INTR_BLOCK_TSTP_TOO = 4, /* block fatal signals and SIGTSTP (^Z) */
103 NUM_INTR_OPTS
104};
105static int opt_intr;
106/* We play with signal mask only if this mode is active: */
107#define interactive (opt_intr == INTR_WHILE_WAIT)
108
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000109/*
110 * daemonized_tracer supports -D option.
111 * With this option, strace forks twice.
112 * Unlike normal case, with -D *grandparent* process exec's,
113 * becoming a traced process. Child exits (this prevents traced process
114 * from having children it doesn't expect to have), and grandchild
115 * attaches to grandparent similarly to strace -p PID.
116 * This allows for more transparent interaction in cases
117 * when process and its parent are communicating via signals,
118 * wait() etc. Without -D, strace process gets lodged in between,
119 * disrupting parent<->child link.
120 */
121static bool daemonized_tracer = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000122
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100123#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100124static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP;
125# define use_seize (post_attach_sigstop == 0)
126#else
127# define post_attach_sigstop TCB_IGNORE_ONE_SIGSTOP
128# define use_seize 0
129#endif
130
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000131/* Sometimes we want to print only succeeding syscalls. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100132bool not_failing_only = 0;
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000133
Grant Edwards8a082772011-04-07 20:25:40 +0000134/* Show path associated with fd arguments */
Dmitry V. Levin45e7b182014-08-08 23:38:26 +0000135unsigned int show_fd_path = 0;
Grant Edwards8a082772011-04-07 20:25:40 +0000136
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100137static bool detach_on_execve = 0;
Denys Vlasenko2a3d2752013-05-14 16:07:46 +0200138/* Are we "strace PROG" and need to skip detach on first execve? */
139static bool skip_one_b_execve = 0;
140/* Are we "strace PROG" and need to hide everything until execve? */
141bool hide_log_until_execve = 0;
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100142
Dmitry V. Levina6809652008-11-10 17:14:58 +0000143static int exit_code = 0;
144static int strace_child = 0;
Denys Vlasenko75422762011-05-27 14:36:01 +0200145static int strace_tracer_pid = 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -0700146
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000147static char *username = NULL;
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200148static uid_t run_uid;
149static gid_t run_gid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000150
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100151unsigned int max_strlen = DEFAULT_STRLEN;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000152static int acolumn = DEFAULT_ACOLUMN;
Denys Vlasenko102ec492011-08-25 01:27:59 +0200153static char *acolumn_spaces;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100154
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000155static char *outfname = NULL;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100156/* If -ff, points to stderr. Else, it's our common output log */
157static FILE *shared_log;
158
Denys Vlasenko000b6012012-01-28 01:25:03 +0100159struct tcb *printing_tcp = NULL;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100160static struct tcb *current_tcp;
161
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200162static struct tcb **tcbtab;
Denys Vlasenko2b60c352011-06-22 12:45:25 +0200163static unsigned int nprocs, tcbtabsize;
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200164static const char *progname;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000165
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100166unsigned os_release; /* generated from uname()'s u.release */
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +0100167
Denys Vlasenko4a9ba982013-06-20 11:20:23 +0200168static void detach(struct tcb *tcp);
Andreas Schwabe5355de2009-10-27 16:56:43 +0100169static void cleanup(void);
170static void interrupt(int sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000171static sigset_t empty_set, blocked_set;
172
173#ifdef HAVE_SIG_ATOMIC_T
174static volatile sig_atomic_t interrupted;
Denys Vlasenkoa3559252012-01-29 16:43:51 +0100175#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000176static volatile int interrupted;
Denys Vlasenkoa3559252012-01-29 16:43:51 +0100177#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000178
Denys Vlasenko2c4fb902012-03-15 17:24:49 +0100179#ifndef HAVE_STRERROR
180
181#if !HAVE_DECL_SYS_ERRLIST
182extern int sys_nerr;
183extern char *sys_errlist[];
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +0100184#endif
Denys Vlasenko2c4fb902012-03-15 17:24:49 +0100185
186const char *
187strerror(int err_no)
188{
189 static char buf[sizeof("Unknown error %d") + sizeof(int)*3];
190
191 if (err_no < 1 || err_no >= sys_nerr) {
192 sprintf(buf, "Unknown error %d", err_no);
193 return buf;
194 }
195 return sys_errlist[err_no];
196}
197
198#endif /* HAVE_STERRROR */
199
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000200static void
Denys Vlasenkocb2ad002011-06-23 13:05:29 +0200201usage(FILE *ofp, int exitval)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000202{
203 fprintf(ofp, "\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200204usage: strace [-CdffhiqrtttTvVxxy] [-I n] [-e expr]...\n\
205 [-a column] [-o file] [-s strsize] [-P path]...\n\
206 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
207 or: strace -c[df] [-I n] [-e expr]... [-O overhead] [-S sortby]\n\
208 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000209-c -- count time, calls, and errors for each syscall and report summary\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200210-C -- like -c but also print regular output\n\
Mark Hillse53bf232014-05-28 17:52:40 +0100211-w -- summarise syscall latency (default is system time)\n\
Denys Vlasenko3e084ac2012-03-15 13:39:05 +0100212-d -- enable debug output to stderr\n\
Denys Vlasenkob51581e2012-01-29 16:53:03 +0100213-D -- run tracer process as a detached grandchild, not as parent\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000214-f -- follow forks, -ff -- with output into separate files\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000215-i -- print instruction pointer at time of syscall\n\
216-q -- suppress messages about attaching, detaching, etc.\n\
217-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
Denys Vlasenkocdab1be2012-02-03 12:17:57 +0100218-T -- print time spent in each syscall\n\
219-v -- verbose mode: print unabbreviated argv, stat, termios, etc. args\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000220-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000221-y -- print paths associated with file descriptor arguments\n\
Dmitry V. Levin2f6510c2014-08-21 03:17:48 +0000222-yy -- print ip:port pairs associated with socket file descriptors\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200223-h -- print help message, -V -- print version\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000224-a column -- alignment COLUMN for printing syscall results (default %d)\n\
Denys Vlasenko22efaf02013-02-27 12:15:19 +0100225-b execve -- detach on this syscall\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000226-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
Denys Vlasenko38e79bb2013-02-26 11:33:54 +0100227 options: trace, abbrev, verbose, raw, signal, read, write\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200228-I interruptible --\n\
229 1: no signals are blocked\n\
230 2: fatal signals are blocked while decoding syscall (default)\n\
231 3: fatal signals are always blocked (default if '-o FILE PROG')\n\
232 4: fatal signals and SIGTSTP (^Z) are always blocked\n\
233 (useful to make 'strace -o FILE PROG' not stop on ^Z)\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000234-o file -- send trace output to FILE instead of stderr\n\
235-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
236-p pid -- trace process with process id PID, may be repeated\n\
237-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
238-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
239-u username -- run command as username handling setuid and/or setgid\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000240-E var=val -- put var=val in the environment for command\n\
241-E var -- remove var from the environment for command\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000242-P path -- trace accesses to path\n\
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100243"
Luca Clementi327064b2013-07-23 00:11:35 -0700244#ifdef USE_LIBUNWIND
Dmitry V. Levin2734a702014-06-18 15:34:27 +0000245"-k obtain stack trace between each syscall (experimental)\n\
Luca Clementi327064b2013-07-23 00:11:35 -0700246"
247#endif
Denys Vlasenko38e79bb2013-02-26 11:33:54 +0100248/* ancient, no one should use it
249-F -- attempt to follow vforks (deprecated, use -f)\n\
250 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100251/* this is broken, so don't document it
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000252-z -- print only succeeding syscalls\n\
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100253 */
Roland McGrathde6e5332003-01-24 04:31:23 +0000254, DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000255 exit(exitval);
256}
257
Denys Vlasenko75422762011-05-27 14:36:01 +0200258static void die(void) __attribute__ ((noreturn));
259static void die(void)
260{
261 if (strace_tracer_pid == getpid()) {
262 cflag = 0;
263 cleanup();
264 }
265 exit(1);
266}
267
268static void verror_msg(int err_no, const char *fmt, va_list p)
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200269{
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100270 char *msg;
271
Dmitry V. Levin44d05322011-06-09 15:50:41 +0000272 fflush(NULL);
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100273
274 /* We want to print entire message with single fprintf to ensure
275 * message integrity if stderr is shared with other programs.
276 * Thus we use vasprintf + single fprintf.
277 */
278 msg = NULL;
Denys Vlasenkocfad5432012-01-24 12:48:02 +0100279 if (vasprintf(&msg, fmt, p) >= 0) {
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100280 if (err_no)
281 fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(err_no));
282 else
283 fprintf(stderr, "%s: %s\n", progname, msg);
284 free(msg);
285 } else {
286 /* malloc in vasprintf failed, try it without malloc */
287 fprintf(stderr, "%s: ", progname);
288 vfprintf(stderr, fmt, p);
289 if (err_no)
290 fprintf(stderr, ": %s\n", strerror(err_no));
291 else
292 putc('\n', stderr);
293 }
294 /* We don't switch stderr to buffered, thus fprintf(stderr)
295 * always flushes its output and this is not necessary: */
296 /* fflush(stderr); */
Denys Vlasenko75422762011-05-27 14:36:01 +0200297}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200298
Denys Vlasenko75422762011-05-27 14:36:01 +0200299void error_msg(const char *fmt, ...)
300{
301 va_list p;
302 va_start(p, fmt);
303 verror_msg(0, fmt, p);
304 va_end(p);
305}
306
307void error_msg_and_die(const char *fmt, ...)
308{
309 va_list p;
310 va_start(p, fmt);
311 verror_msg(0, fmt, p);
312 die();
313}
314
315void perror_msg(const char *fmt, ...)
316{
317 va_list p;
318 va_start(p, fmt);
319 verror_msg(errno, fmt, p);
320 va_end(p);
321}
322
323void perror_msg_and_die(const char *fmt, ...)
324{
325 va_list p;
326 va_start(p, fmt);
327 verror_msg(errno, fmt, p);
328 die();
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200329}
330
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200331void die_out_of_memory(void)
332{
333 static bool recursed = 0;
334 if (recursed)
335 exit(1);
336 recursed = 1;
337 error_msg_and_die("Out of memory");
338}
339
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000340static void
341error_opt_arg(int opt, const char *arg)
342{
343 error_msg_and_die("Invalid -%c argument: '%s'", opt, arg);
344}
345
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100346#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100347static int
348ptrace_attach_or_seize(int pid)
349{
350 int r;
351 if (!use_seize)
Denys Vlasenkoc169d942013-07-10 14:33:05 +0200352 return ptrace(PTRACE_ATTACH, pid, 0L, 0L);
353 r = ptrace(PTRACE_SEIZE, pid, 0L, (unsigned long)ptrace_setoptions);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100354 if (r)
355 return r;
Denys Vlasenkoc169d942013-07-10 14:33:05 +0200356 r = ptrace(PTRACE_INTERRUPT, pid, 0L, 0L);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100357 return r;
358}
359#else
360# define ptrace_attach_or_seize(pid) ptrace(PTRACE_ATTACH, (pid), 0, 0)
361#endif
362
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100363/*
364 * Used when we want to unblock stopped traced process.
365 * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL.
366 * Returns 0 on success or if error was ESRCH
367 * (presumably process was killed while we talk to it).
368 * Otherwise prints error message and returns -1.
369 */
370static int
371ptrace_restart(int op, struct tcb *tcp, int sig)
372{
373 int err;
374 const char *msg;
375
376 errno = 0;
377 ptrace(op, tcp->pid, (void *) 0, (long) sig);
378 err = errno;
Denys Vlasenko23506752012-03-21 10:32:49 +0100379 if (!err)
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100380 return 0;
381
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100382 msg = "SYSCALL";
383 if (op == PTRACE_CONT)
384 msg = "CONT";
385 if (op == PTRACE_DETACH)
386 msg = "DETACH";
387#ifdef PTRACE_LISTEN
388 if (op == PTRACE_LISTEN)
389 msg = "LISTEN";
390#endif
Denys Vlasenko23506752012-03-21 10:32:49 +0100391 /*
392 * Why curcol != 0? Otherwise sometimes we get this:
393 *
394 * 10252 kill(10253, SIGKILL) = 0
395 * <ptrace(SYSCALL,10252):No such process>10253 ...next decode...
396 *
397 * 10252 died after we retrieved syscall exit data,
398 * but before we tried to restart it. Log looks ugly.
399 */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100400 if (current_tcp && current_tcp->curcol != 0) {
Denys Vlasenko23506752012-03-21 10:32:49 +0100401 tprintf(" <ptrace(%s):%s>\n", msg, strerror(err));
402 line_ended();
403 }
404 if (err == ESRCH)
405 return 0;
406 errno = err;
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100407 perror_msg("ptrace(PTRACE_%s,pid:%d,sig:%d)", msg, tcp->pid, sig);
408 return -1;
409}
410
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200411static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000412set_cloexec_flag(int fd)
413{
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200414 int flags, newflags;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000415
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200416 flags = fcntl(fd, F_GETFD);
417 if (flags < 0) {
418 /* Can happen only if fd is bad.
419 * Should never happen: if it does, we have a bug
420 * in the caller. Therefore we just abort
421 * instead of propagating the error.
422 */
423 perror_msg_and_die("fcntl(%d, F_GETFD)", fd);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000424 }
425
426 newflags = flags | FD_CLOEXEC;
427 if (flags == newflags)
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200428 return;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000429
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200430 fcntl(fd, F_SETFD, newflags); /* never fails */
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000431}
432
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100433static void kill_save_errno(pid_t pid, int sig)
434{
435 int saved_errno = errno;
436
437 (void) kill(pid, sig);
438 errno = saved_errno;
439}
440
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100441/*
442 * When strace is setuid executable, we have to swap uids
443 * before and after filesystem and process management operations.
444 */
445static void
446swap_uid(void)
447{
448 int euid = geteuid(), uid = getuid();
449
450 if (euid != uid && setreuid(euid, uid) < 0) {
451 perror_msg_and_die("setreuid");
452 }
453}
454
Dmitry V. Levin0506f0f2013-11-12 22:34:42 +0000455#ifdef _LARGEFILE64_SOURCE
Dmitry V. Levind3541302014-02-26 00:01:00 +0000456# ifdef HAVE_FOPEN64
457# define fopen_for_output fopen64
458# else
459# define fopen_for_output fopen
460# endif
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000461# define struct_stat struct stat64
462# define stat_file stat64
463# define struct_dirent struct dirent64
464# define read_dir readdir64
465# define struct_rlimit struct rlimit64
466# define set_rlimit setrlimit64
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100467#else
468# define fopen_for_output fopen
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000469# define struct_stat struct stat
470# define stat_file stat
471# define struct_dirent struct dirent
472# define read_dir readdir
473# define struct_rlimit struct rlimit
474# define set_rlimit setrlimit
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100475#endif
476
477static FILE *
478strace_fopen(const char *path)
479{
480 FILE *fp;
481
482 swap_uid();
483 fp = fopen_for_output(path, "w");
484 if (!fp)
485 perror_msg_and_die("Can't fopen '%s'", path);
486 swap_uid();
487 set_cloexec_flag(fileno(fp));
488 return fp;
489}
490
491static int popen_pid = 0;
492
493#ifndef _PATH_BSHELL
494# define _PATH_BSHELL "/bin/sh"
495#endif
496
497/*
498 * We cannot use standard popen(3) here because we have to distinguish
499 * popen child process from other processes we trace, and standard popen(3)
500 * does not export its child's pid.
501 */
502static FILE *
503strace_popen(const char *command)
504{
505 FILE *fp;
Denys Vlasenko519af5a2013-07-02 11:31:24 +0200506 int pid;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100507 int fds[2];
508
509 swap_uid();
510 if (pipe(fds) < 0)
511 perror_msg_and_die("pipe");
512
513 set_cloexec_flag(fds[1]); /* never fails */
514
Denys Vlasenko519af5a2013-07-02 11:31:24 +0200515 pid = vfork();
516 if (pid < 0)
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100517 perror_msg_and_die("vfork");
518
Denys Vlasenko519af5a2013-07-02 11:31:24 +0200519 if (pid == 0) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100520 /* child */
521 close(fds[1]);
522 if (fds[0] != 0) {
523 if (dup2(fds[0], 0))
524 perror_msg_and_die("dup2");
525 close(fds[0]);
526 }
527 execl(_PATH_BSHELL, "sh", "-c", command, NULL);
528 perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL);
529 }
530
531 /* parent */
Denys Vlasenko519af5a2013-07-02 11:31:24 +0200532 popen_pid = pid;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100533 close(fds[0]);
534 swap_uid();
535 fp = fdopen(fds[1], "w");
536 if (!fp)
537 die_out_of_memory();
538 return fp;
539}
540
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100541void
542tprintf(const char *fmt, ...)
543{
544 va_list args;
545
546 va_start(args, fmt);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100547 if (current_tcp) {
Denys Vlasenko6e4f3c12012-04-16 18:22:19 +0200548 int n = strace_vfprintf(current_tcp->outf, fmt, args);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100549 if (n < 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100550 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000551 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100552 } else
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100553 current_tcp->curcol += n;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100554 }
555 va_end(args);
556}
557
Dmitry V. Levind3541302014-02-26 00:01:00 +0000558#ifndef HAVE_FPUTS_UNLOCKED
559# define fputs_unlocked fputs
560#endif
561
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100562void
563tprints(const char *str)
564{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100565 if (current_tcp) {
Denys Vlasenko142aee02012-04-16 18:10:15 +0200566 int n = fputs_unlocked(str, current_tcp->outf);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100567 if (n >= 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100568 current_tcp->curcol += strlen(str);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100569 return;
570 }
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100571 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000572 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100573 }
574}
575
576void
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100577line_ended(void)
578{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100579 if (current_tcp) {
580 current_tcp->curcol = 0;
581 fflush(current_tcp->outf);
582 }
583 if (printing_tcp) {
584 printing_tcp->curcol = 0;
585 printing_tcp = NULL;
586 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100587}
588
589void
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100590printleader(struct tcb *tcp)
591{
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100592 /* If -ff, "previous tcb we printed" is always the same as current,
593 * because we have per-tcb output files.
594 */
595 if (followfork >= 2)
596 printing_tcp = tcp;
597
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100598 if (printing_tcp) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100599 current_tcp = printing_tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100600 if (printing_tcp->curcol != 0 && (followfork < 2 || printing_tcp == tcp)) {
601 /*
602 * case 1: we have a shared log (i.e. not -ff), and last line
603 * wasn't finished (same or different tcb, doesn't matter).
604 * case 2: split log, we are the same tcb, but our last line
605 * didn't finish ("SIGKILL nuked us after syscall entry" etc).
606 */
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100607 tprints(" <unfinished ...>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100608 printing_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100609 }
610 }
611
612 printing_tcp = tcp;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100613 current_tcp = tcp;
614 current_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100615
616 if (print_pid_pfx)
617 tprintf("%-5d ", tcp->pid);
618 else if (nprocs > 1 && !outfname)
619 tprintf("[pid %5u] ", tcp->pid);
620
621 if (tflag) {
622 char str[sizeof("HH:MM:SS")];
623 struct timeval tv, dtv;
624 static struct timeval otv;
625
626 gettimeofday(&tv, NULL);
627 if (rflag) {
628 if (otv.tv_sec == 0)
629 otv = tv;
630 tv_sub(&dtv, &tv, &otv);
631 tprintf("%6ld.%06ld ",
632 (long) dtv.tv_sec, (long) dtv.tv_usec);
633 otv = tv;
634 }
635 else if (tflag > 2) {
636 tprintf("%ld.%06ld ",
637 (long) tv.tv_sec, (long) tv.tv_usec);
638 }
639 else {
640 time_t local = tv.tv_sec;
641 strftime(str, sizeof(str), "%T", localtime(&local));
642 if (tflag > 1)
643 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
644 else
645 tprintf("%s ", str);
646 }
647 }
648 if (iflag)
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200649 print_pc(tcp);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100650}
651
652void
653tabto(void)
654{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100655 if (current_tcp->curcol < acolumn)
656 tprints(acolumn_spaces + current_tcp->curcol);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100657}
658
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100659/* Should be only called directly *after successful attach* to a tracee.
660 * Otherwise, "strace -oFILE -ff -p<nonexistant_pid>"
661 * may create bogus empty FILE.<nonexistant_pid>, and then die.
662 */
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200663static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000664newoutf(struct tcb *tcp)
665{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100666 tcp->outf = shared_log; /* if not -ff mode, the same file is for all */
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100667 if (followfork >= 2) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000668 char name[520 + sizeof(int) * 3];
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000669 sprintf(name, "%.512s.%u", outfname, tcp->pid);
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200670 tcp->outf = strace_fopen(name);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000671 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000672}
673
Denys Vlasenko558e5122012-03-12 23:32:16 +0100674static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100675expand_tcbtab(void)
676{
677 /* Allocate some more TCBs and expand the table.
678 We don't want to relocate the TCBs because our
679 callers have pointers and it would be a pain.
680 So tcbtab is a table of pointers. Since we never
681 free the TCBs, we allocate a single chunk of many. */
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000682 unsigned int i = tcbtabsize;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100683 struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0]));
684 struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0]));
685 if (!newtab || !newtcbs)
686 die_out_of_memory();
687 tcbtabsize *= 2;
688 tcbtab = newtab;
689 while (i < tcbtabsize)
690 tcbtab[i++] = newtcbs++;
691}
692
693static struct tcb *
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100694alloctcb(int pid)
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100695{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000696 unsigned int i;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100697 struct tcb *tcp;
698
699 if (nprocs == tcbtabsize)
700 expand_tcbtab();
701
702 for (i = 0; i < tcbtabsize; i++) {
703 tcp = tcbtab[i];
Denys Vlasenkofadbf662013-06-26 14:14:29 +0200704 if (!tcp->pid) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100705 memset(tcp, 0, sizeof(*tcp));
706 tcp->pid = pid;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100707#if SUPPORTED_PERSONALITIES > 1
708 tcp->currpers = current_personality;
709#endif
Luca Clementi327064b2013-07-23 00:11:35 -0700710
711#ifdef USE_LIBUNWIND
712 if (stack_trace_enabled)
Masatake YAMATO61413922014-04-16 15:33:02 +0900713 unwind_tcb_init(tcp);
Luca Clementi327064b2013-07-23 00:11:35 -0700714#endif
715
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100716 nprocs++;
717 if (debug_flag)
718 fprintf(stderr, "new tcb for pid %d, active tcbs:%d\n", tcp->pid, nprocs);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100719 return tcp;
720 }
721 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100722 error_msg_and_die("bug in alloctcb");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100723}
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100724
725static void
726droptcb(struct tcb *tcp)
727{
728 if (tcp->pid == 0)
729 return;
730
Masatake YAMATO2b09df92014-04-16 15:33:08 +0900731#ifdef USE_LIBUNWIND
732 if (stack_trace_enabled) {
733 unwind_tcb_fin(tcp);
734 }
735#endif
736
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100737 nprocs--;
738 if (debug_flag)
739 fprintf(stderr, "dropped tcb for pid %d, %d remain\n", tcp->pid, nprocs);
740
741 if (tcp->outf) {
Denys Vlasenko8511f2a2012-03-22 09:35:51 +0100742 if (followfork >= 2) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100743 if (tcp->curcol != 0)
744 fprintf(tcp->outf, " <detached ...>\n");
745 fclose(tcp->outf);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100746 } else {
747 if (printing_tcp == tcp && tcp->curcol != 0)
748 fprintf(tcp->outf, " <detached ...>\n");
749 fflush(tcp->outf);
750 }
751 }
752
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100753 if (current_tcp == tcp)
754 current_tcp = NULL;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100755 if (printing_tcp == tcp)
756 printing_tcp = NULL;
757
758 memset(tcp, 0, sizeof(*tcp));
759}
760
Denys Vlasenkof1669e72013-06-18 18:09:39 +0200761/* Detach traced process.
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100762 * Never call DETACH twice on the same process as both unattached and
763 * attached-unstopped processes give the same ESRCH. For unattached process we
764 * would SIGSTOP it and wait for its SIGSTOP notification forever.
765 */
Denys Vlasenko4a9ba982013-06-20 11:20:23 +0200766static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100767detach(struct tcb *tcp)
768{
769 int error;
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200770 int status;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100771
772 if (tcp->flags & TCB_BPTSET)
773 clearbpt(tcp);
774
775 /*
776 * Linux wrongly insists the child be stopped
777 * before detaching. Arghh. We go through hoops
778 * to make a clean break of things.
779 */
780#if defined(SPARC)
Denys Vlasenko978fbc92012-09-13 10:28:43 +0200781# undef PTRACE_DETACH
782# define PTRACE_DETACH PTRACE_SUNDETACH
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100783#endif
784
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200785 if (!(tcp->flags & TCB_ATTACHED))
786 goto drop;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100787
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200788 /* We attached but possibly didn't see the expected SIGSTOP.
789 * We must catch exactly one as otherwise the detached process
790 * would be left stopped (process state T).
791 */
792 if (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)
793 goto wait_loop;
794
795 error = ptrace(PTRACE_DETACH, tcp->pid, 0, 0);
796 if (!error) {
797 /* On a clear day, you can see forever. */
798 goto drop;
799 }
800 if (errno != ESRCH) {
801 /* Shouldn't happen. */
802 perror_msg("detach: ptrace(PTRACE_DETACH,%u)", tcp->pid);
803 goto drop;
804 }
805 /* ESRCH: process is either not stopped or doesn't exist. */
806 if (my_tkill(tcp->pid, 0) < 0) {
807 if (errno != ESRCH)
808 /* Shouldn't happen. */
809 perror_msg("detach: tkill(%u,0)", tcp->pid);
810 /* else: process doesn't exist. */
811 goto drop;
812 }
813 /* Process is not stopped, need to stop it. */
814 if (use_seize) {
815 /*
816 * With SEIZE, tracee can be in group-stop already.
817 * In this state sending it another SIGSTOP does nothing.
818 * Need to use INTERRUPT.
819 * Testcase: trying to ^C a "strace -p <stopped_process>".
820 */
821 error = ptrace(PTRACE_INTERRUPT, tcp->pid, 0, 0);
822 if (!error)
823 goto wait_loop;
824 if (errno != ESRCH)
825 perror_msg("detach: ptrace(PTRACE_INTERRUPT,%u)", tcp->pid);
826 }
827 else {
828 error = my_tkill(tcp->pid, SIGSTOP);
829 if (!error)
830 goto wait_loop;
831 if (errno != ESRCH)
832 perror_msg("detach: tkill(%u,SIGSTOP)", tcp->pid);
833 }
834 /* Either process doesn't exist, or some weird error. */
835 goto drop;
836
Denys Vlasenkoa2de9da2013-06-21 15:50:41 +0200837 wait_loop:
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200838 /* We end up here in three cases:
839 * 1. We sent PTRACE_INTERRUPT (use_seize case)
840 * 2. We sent SIGSTOP (!use_seize)
841 * 3. Attach SIGSTOP was already pending (TCB_IGNORE_ONE_SIGSTOP set)
842 */
843 for (;;) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000844 unsigned int sig;
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200845 if (waitpid(tcp->pid, &status, __WALL) < 0) {
846 if (errno == EINTR)
847 continue;
848 /*
849 * if (errno == ECHILD) break;
850 * ^^^ WRONG! We expect this PID to exist,
851 * and want to emit a message otherwise:
852 */
853 perror_msg("detach: waitpid(%u)", tcp->pid);
854 break;
855 }
856 if (!WIFSTOPPED(status)) {
857 /*
858 * Tracee exited or was killed by signal.
859 * We shouldn't normally reach this place:
860 * we don't want to consume exit status.
861 * Consider "strace -p PID" being ^C-ed:
862 * we want merely to detach from PID.
863 *
864 * However, we _can_ end up here if tracee
865 * was SIGKILLed.
866 */
867 break;
868 }
869 sig = WSTOPSIG(status);
870 if (debug_flag)
871 fprintf(stderr, "detach wait: event:%d sig:%d\n",
872 (unsigned)status >> 16, sig);
873 if (use_seize) {
874 unsigned event = (unsigned)status >> 16;
875 if (event == PTRACE_EVENT_STOP /*&& sig == SIGTRAP*/) {
Denys Vlasenkofdfa47a2013-06-20 12:10:21 +0200876 /*
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200877 * sig == SIGTRAP: PTRACE_INTERRUPT stop.
878 * sig == other: process was already stopped
879 * with this stopping sig (see tests/detach-stopped).
880 * Looks like re-injecting this sig is not necessary
881 * in DETACH for the tracee to remain stopped.
Denys Vlasenkofdfa47a2013-06-20 12:10:21 +0200882 */
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200883 sig = 0;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100884 }
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200885 /*
886 * PTRACE_INTERRUPT is not guaranteed to produce
887 * the above event if other ptrace-stop is pending.
888 * See tests/detach-sleeping testcase:
889 * strace got SIGINT while tracee is sleeping.
890 * We sent PTRACE_INTERRUPT.
891 * We see syscall exit, not PTRACE_INTERRUPT stop.
892 * We won't get PTRACE_INTERRUPT stop
893 * if we would CONT now. Need to DETACH.
894 */
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200895 if (sig == syscall_trap_sig)
896 sig = 0;
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200897 /* else: not sure in which case we can be here.
898 * Signal stop? Inject it while detaching.
899 */
900 ptrace_restart(PTRACE_DETACH, tcp, sig);
901 break;
902 }
903 /* Note: this check has to be after use_seize check */
904 /* (else, in use_seize case SIGSTOP will be mistreated) */
905 if (sig == SIGSTOP) {
906 /* Detach, suppressing SIGSTOP */
907 ptrace_restart(PTRACE_DETACH, tcp, 0);
908 break;
909 }
910 if (sig == syscall_trap_sig)
911 sig = 0;
912 /* Can't detach just yet, may need to wait for SIGSTOP */
913 error = ptrace_restart(PTRACE_CONT, tcp, sig);
914 if (error < 0) {
915 /* Should not happen.
916 * Note: ptrace_restart returns 0 on ESRCH, so it's not it.
917 * ptrace_restart already emitted error message.
918 */
919 break;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100920 }
921 }
922
Denys Vlasenkoe2567d52013-06-21 16:11:10 +0200923 drop:
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100924 if (!qflag && (tcp->flags & TCB_ATTACHED))
925 fprintf(stderr, "Process %u detached\n", tcp->pid);
926
927 droptcb(tcp);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100928}
929
930static void
Denys Vlasenko558e5122012-03-12 23:32:16 +0100931process_opt_p_list(char *opt)
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100932{
933 while (*opt) {
934 /*
935 * We accept -p PID,PID; -p "`pidof PROG`"; -p "`pgrep PROG`".
936 * pidof uses space as delim, pgrep uses newline. :(
937 */
938 int pid;
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100939 char *delim = opt + strcspn(opt, ", \n\t");
940 char c = *delim;
941
942 *delim = '\0';
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000943 pid = string_to_uint(opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100944 if (pid <= 0) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000945 error_msg_and_die("Invalid process id: '%s'", opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100946 }
947 if (pid == strace_tracer_pid) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000948 error_msg_and_die("I'm sorry, I can't let you do that, Dave.");
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100949 }
950 *delim = c;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100951 alloctcb(pid);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100952 if (c == '\0')
953 break;
954 opt = delim + 1;
955 }
956}
957
Roland McGrath02203312007-06-11 22:06:31 +0000958static void
959startup_attach(void)
960{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000961 unsigned int tcbi;
Roland McGrath02203312007-06-11 22:06:31 +0000962 struct tcb *tcp;
963
964 /*
965 * Block user interruptions as we would leave the traced
966 * process stopped (process state T) if we would terminate in
Denys Vlasenko2e968c02011-09-01 10:23:09 +0200967 * between PTRACE_ATTACH and wait4() on SIGSTOP.
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200968 * We rely on cleanup() from this point on.
Roland McGrath02203312007-06-11 22:06:31 +0000969 */
970 if (interactive)
971 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
972
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000973 if (daemonized_tracer) {
974 pid_t pid = fork();
975 if (pid < 0) {
Denys Vlasenko014ca3a2011-09-02 16:19:30 +0200976 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000977 }
978 if (pid) { /* parent */
979 /*
Denys Vlasenko75422762011-05-27 14:36:01 +0200980 * Wait for grandchild to attach to straced process
981 * (grandparent). Grandchild SIGKILLs us after it attached.
982 * Grandparent's wait() is unblocked by our death,
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000983 * it proceeds to exec the straced program.
984 */
985 pause();
986 _exit(0); /* paranoia */
987 }
Denys Vlasenko75422762011-05-27 14:36:01 +0200988 /* grandchild */
989 /* We will be the tracer process. Remember our new pid: */
990 strace_tracer_pid = getpid();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000991 }
992
Roland McGrath02203312007-06-11 22:06:31 +0000993 for (tcbi = 0; tcbi < tcbtabsize; tcbi++) {
994 tcp = tcbtab[tcbi];
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200995
Denys Vlasenkofadbf662013-06-26 14:14:29 +0200996 if (!tcp->pid)
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100997 continue;
998
Denys Vlasenkod116a732011-09-05 14:01:33 +0200999 /* Is this a process we should attach to, but not yet attached? */
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001000 if (tcp->flags & TCB_ATTACHED)
1001 continue; /* no, we already attached it */
Denys Vlasenkod116a732011-09-05 14:01:33 +02001002
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001003 if (followfork && !daemonized_tracer) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001004 char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3];
Roland McGrath02203312007-06-11 22:06:31 +00001005 DIR *dir;
1006
1007 sprintf(procdir, "/proc/%d/task", tcp->pid);
1008 dir = opendir(procdir);
1009 if (dir != NULL) {
1010 unsigned int ntid = 0, nerr = 0;
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001011 struct_dirent *de;
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001012
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001013 while ((de = read_dir(dir)) != NULL) {
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001014 struct tcb *cur_tcp;
1015 int tid;
1016
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001017 if (de->d_fileno == 0)
Roland McGrath02203312007-06-11 22:06:31 +00001018 continue;
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001019 /* we trust /proc filesystem */
Roland McGrath02203312007-06-11 22:06:31 +00001020 tid = atoi(de->d_name);
1021 if (tid <= 0)
1022 continue;
1023 ++ntid;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001024 if (ptrace_attach_or_seize(tid) < 0) {
Roland McGrath02203312007-06-11 22:06:31 +00001025 ++nerr;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001026 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001027 fprintf(stderr, "attach to pid %d failed\n", tid);
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001028 continue;
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001029 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001030 if (debug_flag)
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001031 fprintf(stderr, "attach to pid %d succeeded\n", tid);
1032 cur_tcp = tcp;
1033 if (tid != tcp->pid)
1034 cur_tcp = alloctcb(tid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001035 cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001036 newoutf(cur_tcp);
Roland McGrath02203312007-06-11 22:06:31 +00001037 }
1038 closedir(dir);
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001039 if (interactive) {
1040 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1041 if (interrupted)
1042 goto ret;
1043 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1044 }
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001045 ntid -= nerr;
1046 if (ntid == 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001047 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +00001048 droptcb(tcp);
1049 continue;
1050 }
1051 if (!qflag) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001052 fprintf(stderr, ntid > 1
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001053? "Process %u attached with %u threads\n"
1054: "Process %u attached\n",
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02001055 tcp->pid, ntid);
Roland McGrath02203312007-06-11 22:06:31 +00001056 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001057 if (!(tcp->flags & TCB_ATTACHED)) {
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001058 /* -p PID, we failed to attach to PID itself
1059 * but did attach to some of its sibling threads.
1060 * Drop PID's tcp.
1061 */
1062 droptcb(tcp);
1063 }
Roland McGrath02203312007-06-11 22:06:31 +00001064 continue;
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001065 } /* if (opendir worked) */
1066 } /* if (-f) */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001067 if (ptrace_attach_or_seize(tcp->pid) < 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001068 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +00001069 droptcb(tcp);
1070 continue;
1071 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001072 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001073 newoutf(tcp);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001074 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001075 fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001076
1077 if (daemonized_tracer) {
1078 /*
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001079 * Make parent go away.
1080 * Also makes grandparent's wait() unblock.
1081 */
1082 kill(getppid(), SIGKILL);
1083 }
1084
Roland McGrath02203312007-06-11 22:06:31 +00001085 if (!qflag)
1086 fprintf(stderr,
Denys Vlasenko9c3861d2012-03-16 15:21:49 +01001087 "Process %u attached\n",
Roland McGrath02203312007-06-11 22:06:31 +00001088 tcp->pid);
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001089 } /* for each tcbtab[] */
Roland McGrath02203312007-06-11 22:06:31 +00001090
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02001091 ret:
Roland McGrath02203312007-06-11 22:06:31 +00001092 if (interactive)
1093 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1094}
1095
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001096/* Stack-o-phobic exec helper, in the hope to work around
1097 * NOMMU + "daemonized tracer" difficulty.
1098 */
1099struct exec_params {
1100 int fd_to_close;
1101 uid_t run_euid;
1102 gid_t run_egid;
1103 char **argv;
1104 char *pathname;
1105};
1106static struct exec_params params_for_tracee;
1107static void __attribute__ ((noinline, noreturn))
1108exec_or_die(void)
1109{
1110 struct exec_params *params = &params_for_tracee;
1111
1112 if (params->fd_to_close >= 0)
1113 close(params->fd_to_close);
1114 if (!daemonized_tracer && !use_seize) {
1115 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) {
1116 perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)");
1117 }
1118 }
1119
1120 if (username != NULL) {
1121 /*
1122 * It is important to set groups before we
1123 * lose privileges on setuid.
1124 */
1125 if (initgroups(username, run_gid) < 0) {
1126 perror_msg_and_die("initgroups");
1127 }
1128 if (setregid(run_gid, params->run_egid) < 0) {
1129 perror_msg_and_die("setregid");
1130 }
1131 if (setreuid(run_uid, params->run_euid) < 0) {
1132 perror_msg_and_die("setreuid");
1133 }
1134 }
1135 else if (geteuid() != 0)
1136 if (setreuid(run_uid, run_uid) < 0) {
1137 perror_msg_and_die("setreuid");
1138 }
1139
1140 if (!daemonized_tracer) {
1141 /*
1142 * Induce a ptrace stop. Tracer (our parent)
1143 * will resume us with PTRACE_SYSCALL and display
1144 * the immediately following execve syscall.
1145 * Can't do this on NOMMU systems, we are after
1146 * vfork: parent is blocked, stopping would deadlock.
1147 */
1148 if (!NOMMU_SYSTEM)
1149 kill(getpid(), SIGSTOP);
1150 } else {
1151 alarm(3);
1152 /* we depend on SIGCHLD set to SIG_DFL by init code */
1153 /* if it happens to be SIG_IGN'ed, wait won't block */
1154 wait(NULL);
1155 alarm(0);
1156 }
1157
1158 execv(params->pathname, params->argv);
1159 perror_msg_and_die("exec");
1160}
1161
Roland McGrath02203312007-06-11 22:06:31 +00001162static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001163startup_child(char **argv)
Roland McGrath02203312007-06-11 22:06:31 +00001164{
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001165 struct_stat statbuf;
Roland McGrath02203312007-06-11 22:06:31 +00001166 const char *filename;
Dmitry V. Levin025b3582014-11-21 22:28:34 +00001167 char pathname[PATH_MAX];
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001168 int pid;
Roland McGrath02203312007-06-11 22:06:31 +00001169 struct tcb *tcp;
1170
1171 filename = argv[0];
1172 if (strchr(filename, '/')) {
1173 if (strlen(filename) > sizeof pathname - 1) {
1174 errno = ENAMETOOLONG;
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001175 perror_msg_and_die("exec");
Roland McGrath02203312007-06-11 22:06:31 +00001176 }
1177 strcpy(pathname, filename);
1178 }
1179#ifdef USE_DEBUGGING_EXEC
1180 /*
1181 * Debuggers customarily check the current directory
1182 * first regardless of the path but doing that gives
1183 * security geeks a panic attack.
1184 */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001185 else if (stat_file(filename, &statbuf) == 0)
Roland McGrath02203312007-06-11 22:06:31 +00001186 strcpy(pathname, filename);
1187#endif /* USE_DEBUGGING_EXEC */
1188 else {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +00001189 const char *path;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001190 size_t m, n, len;
Roland McGrath02203312007-06-11 22:06:31 +00001191
1192 for (path = getenv("PATH"); path && *path; path += m) {
Denys Vlasenko4f3df072012-01-29 22:38:35 +01001193 const char *colon = strchr(path, ':');
1194 if (colon) {
1195 n = colon - path;
Roland McGrath02203312007-06-11 22:06:31 +00001196 m = n + 1;
1197 }
1198 else
1199 m = n = strlen(path);
1200 if (n == 0) {
Dmitry V. Levin025b3582014-11-21 22:28:34 +00001201 if (!getcwd(pathname, PATH_MAX))
Roland McGrath02203312007-06-11 22:06:31 +00001202 continue;
1203 len = strlen(pathname);
1204 }
1205 else if (n > sizeof pathname - 1)
1206 continue;
1207 else {
1208 strncpy(pathname, path, n);
1209 len = n;
1210 }
1211 if (len && pathname[len - 1] != '/')
1212 pathname[len++] = '/';
1213 strcpy(pathname + len, filename);
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001214 if (stat_file(pathname, &statbuf) == 0 &&
Roland McGrath02203312007-06-11 22:06:31 +00001215 /* Accept only regular files
1216 with some execute bits set.
1217 XXX not perfect, might still fail */
1218 S_ISREG(statbuf.st_mode) &&
1219 (statbuf.st_mode & 0111))
1220 break;
1221 }
1222 }
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001223 if (stat_file(pathname, &statbuf) < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001224 perror_msg_and_die("Can't stat '%s'", filename);
Roland McGrath02203312007-06-11 22:06:31 +00001225 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001226
1227 params_for_tracee.fd_to_close = (shared_log != stderr) ? fileno(shared_log) : -1;
1228 params_for_tracee.run_euid = (statbuf.st_mode & S_ISUID) ? statbuf.st_uid : run_uid;
1229 params_for_tracee.run_egid = (statbuf.st_mode & S_ISGID) ? statbuf.st_gid : run_gid;
1230 params_for_tracee.argv = argv;
1231 /*
1232 * On NOMMU, can be safely freed only after execve in tracee.
1233 * It's hard to know when that happens, so we just leak it.
1234 */
Denys Vlasenko05f32512013-02-26 12:00:34 +01001235 params_for_tracee.pathname = NOMMU_SYSTEM ? strdup(pathname) : pathname;
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001236
Dmitry V. Levin882478a2013-05-13 18:43:28 +00001237#if defined HAVE_PRCTL && defined PR_SET_PTRACER && defined PR_SET_PTRACER_ANY
1238 if (daemonized_tracer)
1239 prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY);
1240#endif
1241
Denys Vlasenkoe8681c92013-06-26 14:27:11 +02001242 pid = fork();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001243 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001244 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001245 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001246 if ((pid != 0 && daemonized_tracer)
1247 || (pid == 0 && !daemonized_tracer)
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001248 ) {
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001249 /* We are to become the tracee. Two cases:
1250 * -D: we are parent
1251 * not -D: we are child
1252 */
1253 exec_or_die();
Roland McGrath02203312007-06-11 22:06:31 +00001254 }
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001255
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001256 /* We are the tracer */
Denys Vlasenko75422762011-05-27 14:36:01 +02001257
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001258 if (!daemonized_tracer) {
Denys Vlasenkoe8681c92013-06-26 14:27:11 +02001259 strace_child = pid;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001260 if (!use_seize) {
1261 /* child did PTRACE_TRACEME, nothing to do in parent */
1262 } else {
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001263 if (!NOMMU_SYSTEM) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001264 /* Wait until child stopped itself */
1265 int status;
1266 while (waitpid(pid, &status, WSTOPPED) < 0) {
1267 if (errno == EINTR)
1268 continue;
1269 perror_msg_and_die("waitpid");
1270 }
1271 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001272 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001273 perror_msg_and_die("Unexpected wait status %x", status);
1274 }
1275 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001276 /* Else: NOMMU case, we have no way to sync.
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001277 * Just attach to it as soon as possible.
1278 * This means that we may miss a few first syscalls...
1279 */
1280
1281 if (ptrace_attach_or_seize(pid)) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001282 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001283 perror_msg_and_die("Can't attach to %d", pid);
1284 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001285 if (!NOMMU_SYSTEM)
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001286 kill(pid, SIGCONT);
1287 }
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001288 tcp = alloctcb(pid);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001289 if (!NOMMU_SYSTEM)
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001290 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001291 else
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001292 tcp->flags |= TCB_ATTACHED | TCB_STARTUP;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001293 newoutf(tcp);
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001294 }
1295 else {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001296 /* With -D, we are *child* here, IOW: different pid. Fetch it: */
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001297 strace_tracer_pid = getpid();
1298 /* The tracee is our parent: */
1299 pid = getppid();
Denys Vlasenkof2025022012-03-09 15:29:45 +01001300 alloctcb(pid);
1301 /* attaching will be done later, by startup_attach */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001302 /* note: we don't do newoutf(tcp) here either! */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001303
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001304 /* NOMMU BUG! -D mode is active, we (child) return,
1305 * and we will scribble over parent's stack!
1306 * When parent later unpauses, it segfaults.
1307 *
1308 * We work around it
1309 * (1) by declaring exec_or_die() NORETURN,
1310 * hopefully compiler will just jump to it
1311 * instead of call (won't push anything to stack),
1312 * (2) by trying very hard in exec_or_die()
1313 * to not use any stack,
Dmitry V. Levin025b3582014-11-21 22:28:34 +00001314 * (3) having a really big (PATH_MAX) stack object
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001315 * in this function, which creates a "buffer" between
1316 * child's and parent's stack pointers.
1317 * This may save us if (1) and (2) failed
1318 * and compiler decided to use stack in exec_or_die() anyway
1319 * (happens on i386 because of stack parameter passing).
Denys Vlasenko05f32512013-02-26 12:00:34 +01001320 *
1321 * A cleaner solution is to use makecontext + setcontext
1322 * to create a genuine separate stack and execute on it.
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001323 */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001324 }
Roland McGrath02203312007-06-11 22:06:31 +00001325}
1326
Wang Chaob13c0de2010-11-12 17:25:19 +08001327/*
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001328 * Test whether the kernel support PTRACE_O_TRACECLONE et al options.
Wang Chaob13c0de2010-11-12 17:25:19 +08001329 * First fork a new child, call ptrace with PTRACE_SETOPTIONS on it,
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001330 * and then see which options are supported by the kernel.
Wang Chaob13c0de2010-11-12 17:25:19 +08001331 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001332static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001333test_ptrace_setoptions_followfork(void)
Wang Chaob13c0de2010-11-12 17:25:19 +08001334{
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001335 int pid, expected_grandchild = 0, found_grandchild = 0;
1336 const unsigned int test_options = PTRACE_O_TRACECLONE |
1337 PTRACE_O_TRACEFORK |
1338 PTRACE_O_TRACEVFORK;
Wang Chaob13c0de2010-11-12 17:25:19 +08001339
Denys Vlasenko05f32512013-02-26 12:00:34 +01001340 /* Need fork for test. NOMMU has no forks */
1341 if (NOMMU_SYSTEM)
1342 goto worked; /* be bold, and pretend that test succeeded */
1343
Denys Vlasenko5d645812011-08-20 12:48:18 +02001344 pid = fork();
1345 if (pid < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001346 perror_msg_and_die("fork");
Denys Vlasenko5d645812011-08-20 12:48:18 +02001347 if (pid == 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001348 pid = getpid();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001349 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001350 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1351 __func__);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001352 kill_save_errno(pid, SIGSTOP);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001353 if (fork() < 0)
1354 perror_msg_and_die("fork");
1355 _exit(0);
Wang Chaob13c0de2010-11-12 17:25:19 +08001356 }
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001357
1358 while (1) {
1359 int status, tracee_pid;
1360
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001361 errno = 0;
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001362 tracee_pid = wait(&status);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001363 if (tracee_pid <= 0) {
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001364 if (errno == EINTR)
1365 continue;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001366 if (errno == ECHILD)
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001367 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001368 kill_save_errno(pid, SIGKILL);
1369 perror_msg_and_die("%s: unexpected wait result %d",
1370 __func__, tracee_pid);
1371 }
1372 if (WIFEXITED(status)) {
1373 if (WEXITSTATUS(status)) {
1374 if (tracee_pid != pid)
1375 kill_save_errno(pid, SIGKILL);
1376 error_msg_and_die("%s: unexpected exit status %u",
1377 __func__, WEXITSTATUS(status));
1378 }
1379 continue;
1380 }
1381 if (WIFSIGNALED(status)) {
1382 if (tracee_pid != pid)
1383 kill_save_errno(pid, SIGKILL);
1384 error_msg_and_die("%s: unexpected signal %u",
1385 __func__, WTERMSIG(status));
1386 }
1387 if (!WIFSTOPPED(status)) {
1388 if (tracee_pid != pid)
1389 kill_save_errno(tracee_pid, SIGKILL);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001390 kill_save_errno(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001391 error_msg_and_die("%s: unexpected wait status %x",
1392 __func__, status);
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001393 }
1394 if (tracee_pid != pid) {
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001395 found_grandchild = tracee_pid;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001396 if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0) {
1397 kill_save_errno(tracee_pid, SIGKILL);
1398 kill_save_errno(pid, SIGKILL);
1399 perror_msg_and_die("PTRACE_CONT doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001400 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001401 continue;
1402 }
1403 switch (WSTOPSIG(status)) {
1404 case SIGSTOP:
1405 if (ptrace(PTRACE_SETOPTIONS, pid, 0, test_options) < 0
1406 && errno != EINVAL && errno != EIO)
1407 perror_msg("PTRACE_SETOPTIONS");
1408 break;
1409 case SIGTRAP:
1410 if (status >> 16 == PTRACE_EVENT_FORK) {
1411 long msg = 0;
1412
1413 if (ptrace(PTRACE_GETEVENTMSG, pid,
1414 NULL, (long) &msg) == 0)
1415 expected_grandchild = msg;
1416 }
1417 break;
1418 }
1419 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) {
1420 kill_save_errno(pid, SIGKILL);
1421 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001422 }
1423 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001424 if (expected_grandchild && expected_grandchild == found_grandchild) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001425 worked:
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001426 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001427 if (debug_flag)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001428 fprintf(stderr, "ptrace_setoptions = %#x\n",
1429 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001430 return 0;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001431 }
1432 error_msg("Test for PTRACE_O_TRACECLONE failed, "
1433 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001434 return 1;
Wang Chaob13c0de2010-11-12 17:25:19 +08001435}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001436
1437/*
1438 * Test whether the kernel support PTRACE_O_TRACESYSGOOD.
1439 * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it,
1440 * and then see whether it will stop with (SIGTRAP | 0x80).
1441 *
1442 * Use of this option enables correct handling of user-generated SIGTRAPs,
1443 * and SIGTRAPs generated by special instructions such as int3 on x86:
Denys Vlasenko329fa392014-04-10 09:57:17 +02001444
1445# compile with: gcc -nostartfiles -nostdlib -o int3 int3.S
1446_start: .globl _start
1447 int3
1448 movl $42, %ebx
1449 movl $1, %eax
1450 int $0x80
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001451 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001452static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001453test_ptrace_setoptions_for_all(void)
1454{
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001455 const unsigned int test_options = PTRACE_O_TRACESYSGOOD |
1456 PTRACE_O_TRACEEXEC;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001457 int pid;
1458 int it_worked = 0;
1459
Denys Vlasenko05f32512013-02-26 12:00:34 +01001460 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001461 if (NOMMU_SYSTEM)
Denys Vlasenko05f32512013-02-26 12:00:34 +01001462 goto worked; /* be bold, and pretend that test succeeded */
Mike Frysinger7ff5ed92012-04-05 01:52:25 -04001463
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001464 pid = fork();
1465 if (pid < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001466 perror_msg_and_die("fork");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001467
1468 if (pid == 0) {
1469 pid = getpid();
1470 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001471 /* Note: exits with exitcode 1 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001472 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1473 __func__);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001474 kill(pid, SIGSTOP);
1475 _exit(0); /* parent should see entry into this syscall */
1476 }
1477
1478 while (1) {
1479 int status, tracee_pid;
1480
1481 errno = 0;
1482 tracee_pid = wait(&status);
1483 if (tracee_pid <= 0) {
1484 if (errno == EINTR)
1485 continue;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001486 kill_save_errno(pid, SIGKILL);
1487 perror_msg_and_die("%s: unexpected wait result %d",
1488 __func__, tracee_pid);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001489 }
Denys Vlasenko75422762011-05-27 14:36:01 +02001490 if (WIFEXITED(status)) {
1491 if (WEXITSTATUS(status) == 0)
1492 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001493 error_msg_and_die("%s: unexpected exit status %u",
1494 __func__, WEXITSTATUS(status));
1495 }
1496 if (WIFSIGNALED(status)) {
1497 error_msg_and_die("%s: unexpected signal %u",
1498 __func__, WTERMSIG(status));
Denys Vlasenko75422762011-05-27 14:36:01 +02001499 }
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001500 if (!WIFSTOPPED(status)) {
1501 kill(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001502 error_msg_and_die("%s: unexpected wait status %x",
1503 __func__, status);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001504 }
1505 if (WSTOPSIG(status) == SIGSTOP) {
1506 /*
1507 * We don't check "options aren't accepted" error.
1508 * If it happens, we'll never get (SIGTRAP | 0x80),
1509 * and thus will decide to not use the option.
1510 * IOW: the outcome of the test will be correct.
1511 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001512 if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0
1513 && errno != EINVAL && errno != EIO)
1514 perror_msg("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001515 }
1516 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
1517 it_worked = 1;
1518 }
1519 if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001520 kill_save_errno(pid, SIGKILL);
Denys Vlasenko75422762011-05-27 14:36:01 +02001521 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001522 }
1523 }
1524
1525 if (it_worked) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001526 worked:
Denys Vlasenko75422762011-05-27 14:36:01 +02001527 syscall_trap_sig = (SIGTRAP | 0x80);
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001528 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001529 if (debug_flag)
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001530 fprintf(stderr, "ptrace_setoptions = %#x\n",
1531 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001532 return 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001533 }
1534
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001535 error_msg("Test for PTRACE_O_TRACESYSGOOD failed, "
1536 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001537 return 1;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001538}
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001539
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001540#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001541static void
1542test_ptrace_seize(void)
1543{
1544 int pid;
1545
Denys Vlasenko05f32512013-02-26 12:00:34 +01001546 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001547 if (NOMMU_SYSTEM) {
1548 post_attach_sigstop = 0; /* this sets use_seize to 1 */
1549 return;
1550 }
1551
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001552 pid = fork();
1553 if (pid < 0)
1554 perror_msg_and_die("fork");
1555
1556 if (pid == 0) {
1557 pause();
1558 _exit(0);
1559 }
1560
1561 /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After
1562 * attaching tracee continues to run unless a trap condition occurs.
1563 * PTRACE_SEIZE doesn't affect signal or group stop state.
1564 */
Denys Vlasenko26bc0602012-07-10 16:36:32 +02001565 if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001566 post_attach_sigstop = 0; /* this sets use_seize to 1 */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001567 } else if (debug_flag) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001568 fprintf(stderr, "PTRACE_SEIZE doesn't work\n");
1569 }
1570
1571 kill(pid, SIGKILL);
1572
1573 while (1) {
1574 int status, tracee_pid;
1575
1576 errno = 0;
1577 tracee_pid = waitpid(pid, &status, 0);
1578 if (tracee_pid <= 0) {
1579 if (errno == EINTR)
1580 continue;
1581 perror_msg_and_die("%s: unexpected wait result %d",
1582 __func__, tracee_pid);
1583 }
1584 if (WIFSIGNALED(status)) {
1585 return;
1586 }
1587 error_msg_and_die("%s: unexpected wait status %x",
1588 __func__, status);
1589 }
1590}
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001591#else /* !USE_SEIZE */
1592# define test_ptrace_seize() ((void)0)
1593#endif
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001594
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001595static unsigned
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001596get_os_release(void)
1597{
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001598 unsigned rel;
1599 const char *p;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001600 struct utsname u;
1601 if (uname(&u) < 0)
1602 perror_msg_and_die("uname");
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001603 /* u.release has this form: "3.2.9[-some-garbage]" */
1604 rel = 0;
1605 p = u.release;
1606 for (;;) {
1607 if (!(*p >= '0' && *p <= '9'))
1608 error_msg_and_die("Bad OS release string: '%s'", u.release);
1609 /* Note: this open-codes KERNEL_VERSION(): */
1610 rel = (rel << 8) | atoi(p);
1611 if (rel >= KERNEL_VERSION(1,0,0))
1612 break;
1613 while (*p >= '0' && *p <= '9')
1614 p++;
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001615 if (*p != '.') {
1616 if (rel >= KERNEL_VERSION(0,1,0)) {
1617 /* "X.Y-something" means "X.Y.0" */
1618 rel <<= 8;
1619 break;
1620 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001621 error_msg_and_die("Bad OS release string: '%s'", u.release);
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001622 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001623 p++;
1624 }
1625 return rel;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001626}
1627
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01001628/*
1629 * Initialization part of main() was eating much stack (~0.5k),
1630 * which was unused after init.
1631 * We can reuse it if we move init code into a separate function.
1632 *
1633 * Don't want main() to inline us and defeat the reason
1634 * we have a separate function.
1635 */
1636static void __attribute__ ((noinline))
1637init(int argc, char *argv[])
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001638{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001639 struct tcb *tcp;
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001640 int c, i;
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001641 int optF = 0;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001642 unsigned int tcbi;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001643 struct sigaction sa;
1644
Dmitry V. Levin08b623e2007-10-08 21:04:41 +00001645 progname = argv[0] ? argv[0] : "strace";
1646
Denys Vlasenkoa5090542012-03-15 17:27:49 +01001647 /* Make sure SIGCHLD has the default action so that waitpid
1648 definitely works without losing track of children. The user
1649 should not have given us a bogus state to inherit, but he might
1650 have. Arguably we should detect SIG_IGN here and pass it on
1651 to children, but probably noone really needs that. */
1652 signal(SIGCHLD, SIG_DFL);
1653
Denys Vlasenko75422762011-05-27 14:36:01 +02001654 strace_tracer_pid = getpid();
1655
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001656 os_release = get_os_release();
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001657
Roland McGrathee9d4352002-12-18 04:16:10 +00001658 /* Allocate the initial tcbtab. */
1659 tcbtabsize = argc; /* Surely enough for all -p args. */
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001660 tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0]));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001661 if (!tcbtab)
1662 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001663 tcp = calloc(tcbtabsize, sizeof(*tcp));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001664 if (!tcp)
1665 die_out_of_memory();
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001666 for (tcbi = 0; tcbi < tcbtabsize; tcbi++)
1667 tcbtab[tcbi] = tcp++;
Roland McGrathee9d4352002-12-18 04:16:10 +00001668
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001669 shared_log = stderr;
Roland McGrath138c6a32006-01-12 09:50:49 +00001670 set_sortby(DEFAULT_SORTBY);
1671 set_personality(DEFAULT_PERSONALITY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001672 qualify("trace=all");
1673 qualify("abbrev=all");
1674 qualify("verbose=all");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001675#if DEFAULT_QUAL_FLAGS != (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
1676# error Bug in DEFAULT_QUAL_FLAGS
1677#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001678 qualify("signal=all");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001679 while ((c = getopt(argc, argv,
Mark Hillse53bf232014-05-28 17:52:40 +01001680 "+b:cCdfFhiqrtTvVwxyz"
Luca Clementi327064b2013-07-23 00:11:35 -07001681#ifdef USE_LIBUNWIND
1682 "k"
1683#endif
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001684 "D"
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001685 "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001686 switch (c) {
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001687 case 'b':
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001688 if (strcmp(optarg, "execve") != 0)
1689 error_msg_and_die("Syscall '%s' for -b isn't supported",
1690 optarg);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001691 detach_on_execve = 1;
1692 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001693 case 'c':
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001694 if (cflag == CFLAG_BOTH) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001695 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001696 }
1697 cflag = CFLAG_ONLY_STATS;
1698 break;
1699 case 'C':
1700 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001701 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001702 }
1703 cflag = CFLAG_BOTH;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001704 break;
1705 case 'd':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001706 debug_flag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001707 break;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001708 case 'D':
1709 daemonized_tracer = 1;
1710 break;
Roland McGrath41c48222008-07-18 00:25:10 +00001711 case 'F':
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001712 optF = 1;
1713 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001714 case 'f':
1715 followfork++;
1716 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001717 case 'h':
1718 usage(stdout, 0);
1719 break;
1720 case 'i':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001721 iflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001722 break;
1723 case 'q':
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01001724 qflag++;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001725 break;
1726 case 'r':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001727 rflag = 1;
1728 /* fall through to tflag++ */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001729 case 't':
1730 tflag++;
1731 break;
1732 case 'T':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001733 Tflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001734 break;
Mark Hillse53bf232014-05-28 17:52:40 +01001735 case 'w':
1736 count_wallclock = 1;
1737 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001738 case 'x':
1739 xflag++;
1740 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001741 case 'y':
Dmitry V. Levin45e7b182014-08-08 23:38:26 +00001742 show_fd_path++;
Grant Edwards8a082772011-04-07 20:25:40 +00001743 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001744 case 'v':
1745 qualify("abbrev=none");
1746 break;
1747 case 'V':
Roland McGrath9c9a2532003-02-20 02:56:29 +00001748 printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001749 exit(0);
1750 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001751 case 'z':
1752 not_failing_only = 1;
1753 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001754 case 'a':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001755 acolumn = string_to_uint(optarg);
Denys Vlasenko102ec492011-08-25 01:27:59 +02001756 if (acolumn < 0)
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001757 error_opt_arg(c, optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001758 break;
1759 case 'e':
1760 qualify(optarg);
1761 break;
1762 case 'o':
1763 outfname = strdup(optarg);
1764 break;
1765 case 'O':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001766 i = string_to_uint(optarg);
1767 if (i < 0)
1768 error_opt_arg(c, optarg);
1769 set_overhead(i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001770 break;
1771 case 'p':
Denys Vlasenkoe8172b72012-03-09 13:01:04 +01001772 process_opt_p_list(optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001773 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001774 case 'P':
Denys Vlasenko7239dbc2013-03-05 15:46:34 +01001775 pathtrace_select(optarg);
Grant Edwards8a082772011-04-07 20:25:40 +00001776 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001777 case 's':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001778 i = string_to_uint(optarg);
1779 if (i < 0)
1780 error_opt_arg(c, optarg);
1781 max_strlen = i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001782 break;
1783 case 'S':
1784 set_sortby(optarg);
1785 break;
1786 case 'u':
1787 username = strdup(optarg);
1788 break;
Luca Clementi327064b2013-07-23 00:11:35 -07001789#ifdef USE_LIBUNWIND
1790 case 'k':
1791 stack_trace_enabled = true;
1792 break;
1793#endif
Roland McGrathde6e5332003-01-24 04:31:23 +00001794 case 'E':
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001795 if (putenv(optarg) < 0)
1796 die_out_of_memory();
Roland McGrathde6e5332003-01-24 04:31:23 +00001797 break;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001798 case 'I':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001799 opt_intr = string_to_uint(optarg);
1800 if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS)
1801 error_opt_arg(c, optarg);
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001802 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001803 default:
1804 usage(stderr, 1);
1805 break;
1806 }
1807 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001808 argv += optind;
1809 /* argc -= optind; - no need, argc is not used below */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001810
Denys Vlasenko102ec492011-08-25 01:27:59 +02001811 acolumn_spaces = malloc(acolumn + 1);
1812 if (!acolumn_spaces)
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001813 die_out_of_memory();
Denys Vlasenko102ec492011-08-25 01:27:59 +02001814 memset(acolumn_spaces, ' ', acolumn);
1815 acolumn_spaces[acolumn] = '\0';
1816
Denys Vlasenko837399a2012-01-24 11:37:03 +01001817 /* Must have PROG [ARGS], or -p PID. Not both. */
Denys Vlasenkofd883382012-03-09 13:03:41 +01001818 if (!argv[0] == !nprocs)
Roland McGrathce0d1542003-11-11 21:24:23 +00001819 usage(stderr, 1);
1820
Denys Vlasenkofd883382012-03-09 13:03:41 +01001821 if (nprocs != 0 && daemonized_tracer) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001822 error_msg_and_die("-D and -p are mutually exclusive");
Wang Chaod322a4b2010-08-05 14:30:11 +08001823 }
1824
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001825 if (!followfork)
1826 followfork = optF;
1827
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001828 if (followfork >= 2 && cflag) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001829 error_msg_and_die("(-c or -C) and -ff are mutually exclusive");
Roland McGrathcb9def62006-04-25 07:48:03 +00001830 }
1831
Mark Hillse53bf232014-05-28 17:52:40 +01001832 if (count_wallclock && !cflag) {
1833 error_msg_and_die("-w must be given with (-c or -C)");
1834 }
1835
Dmitry V. Levin2727aae2014-06-03 13:20:05 +00001836 if (cflag == CFLAG_ONLY_STATS) {
1837 if (iflag)
1838 error_msg("-%c has no effect with -c", 'i');
1839#ifdef USE_LIBUNWIND
1840 if (stack_trace_enabled)
1841 error_msg("-%c has no effect with -c", 'k');
1842#endif
1843 if (rflag)
1844 error_msg("-%c has no effect with -c", 'r');
1845 if (tflag)
1846 error_msg("-%c has no effect with -c", 't');
1847 if (Tflag)
1848 error_msg("-%c has no effect with -c", 'T');
1849 if (show_fd_path)
1850 error_msg("-%c has no effect with -c", 'y');
1851 }
1852
1853#ifdef USE_LIBUNWIND
1854 if (stack_trace_enabled)
1855 unwind_init();
1856#endif
1857
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001858 /* See if they want to run as another user. */
1859 if (username != NULL) {
1860 struct passwd *pent;
1861
1862 if (getuid() != 0 || geteuid() != 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001863 error_msg_and_die("You must be root to use the -u option");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001864 }
Denys Vlasenko5d645812011-08-20 12:48:18 +02001865 pent = getpwnam(username);
1866 if (pent == NULL) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001867 error_msg_and_die("Cannot find user '%s'", username);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001868 }
1869 run_uid = pent->pw_uid;
1870 run_gid = pent->pw_gid;
1871 }
1872 else {
1873 run_uid = getuid();
1874 run_gid = getgid();
1875 }
1876
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001877 /*
1878 * On any reasonably recent Linux kernel (circa about 2.5.46)
1879 * need_fork_exec_workarounds should stay 0 after these tests:
1880 */
1881 /*need_fork_exec_workarounds = 0; - already is */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001882 if (followfork)
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001883 need_fork_exec_workarounds = test_ptrace_setoptions_followfork();
1884 need_fork_exec_workarounds |= test_ptrace_setoptions_for_all();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001885 test_ptrace_seize();
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001886
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001887 /* Check if they want to redirect the output. */
1888 if (outfname) {
Roland McGrath37b9a662003-11-07 02:26:54 +00001889 /* See if they want to pipe the output. */
1890 if (outfname[0] == '|' || outfname[0] == '!') {
1891 /*
1892 * We can't do the <outfname>.PID funny business
1893 * when using popen, so prohibit it.
1894 */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001895 if (followfork >= 2)
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001896 error_msg_and_die("Piping the output and -ff are mutually exclusive");
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001897 shared_log = strace_popen(outfname + 1);
Roland McGrath37b9a662003-11-07 02:26:54 +00001898 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001899 else if (followfork < 2)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001900 shared_log = strace_fopen(outfname);
Denys Vlasenko328bf252012-03-12 23:34:13 +01001901 } else {
1902 /* -ff without -o FILE is the same as single -f */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001903 if (followfork >= 2)
Denys Vlasenko328bf252012-03-12 23:34:13 +01001904 followfork = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001905 }
1906
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001907 if (!outfname || outfname[0] == '|' || outfname[0] == '!') {
Denys Vlasenkoa677da52012-01-24 11:31:51 +01001908 char *buf = malloc(BUFSIZ);
1909 if (!buf)
1910 die_out_of_memory();
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001911 setvbuf(shared_log, buf, _IOLBF, BUFSIZ);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001912 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001913 if (outfname && argv[0]) {
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001914 if (!opt_intr)
1915 opt_intr = INTR_NEVER;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001916 qflag = 1;
Roland McGrath36931052003-06-03 01:35:20 +00001917 }
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001918 if (!opt_intr)
1919 opt_intr = INTR_WHILE_WAIT;
Wang Chaob13c0de2010-11-12 17:25:19 +08001920
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001921 /* argv[0] -pPID -oFILE Default interactive setting
1922 * yes 0 0 INTR_WHILE_WAIT
1923 * no 1 0 INTR_WHILE_WAIT
1924 * yes 0 1 INTR_NEVER
1925 * no 1 1 INTR_WHILE_WAIT
Roland McGrath54cc1c82007-11-03 23:34:11 +00001926 */
1927
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001928 sigemptyset(&empty_set);
1929 sigemptyset(&blocked_set);
1930
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001931 /* startup_child() must be called before the signal handlers get
1932 * installed below as they are inherited into the spawned process.
1933 * Also we do not need to be protected by them as during interruption
1934 * in the startup_child() mode we kill the spawned process anyway.
1935 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001936 if (argv[0]) {
Dmitry V. Levin1d2435b2013-05-14 22:35:46 +00001937 if (!NOMMU_SYSTEM || daemonized_tracer)
1938 hide_log_until_execve = 1;
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001939 skip_one_b_execve = 1;
Denys Vlasenko837399a2012-01-24 11:37:03 +01001940 startup_child(argv);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001941 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001942
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001943 sa.sa_handler = SIG_IGN;
1944 sigemptyset(&sa.sa_mask);
1945 sa.sa_flags = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001946 sigaction(SIGTTOU, &sa, NULL); /* SIG_IGN */
1947 sigaction(SIGTTIN, &sa, NULL); /* SIG_IGN */
1948 if (opt_intr != INTR_ANYWHERE) {
1949 if (opt_intr == INTR_BLOCK_TSTP_TOO)
1950 sigaction(SIGTSTP, &sa, NULL); /* SIG_IGN */
1951 /*
1952 * In interactive mode (if no -o OUTFILE, or -p PID is used),
1953 * fatal signals are blocked while syscall stop is processed,
1954 * and acted on in between, when waiting for new syscall stops.
1955 * In non-interactive mode, signals are ignored.
1956 */
1957 if (opt_intr == INTR_WHILE_WAIT) {
1958 sigaddset(&blocked_set, SIGHUP);
1959 sigaddset(&blocked_set, SIGINT);
1960 sigaddset(&blocked_set, SIGQUIT);
1961 sigaddset(&blocked_set, SIGPIPE);
1962 sigaddset(&blocked_set, SIGTERM);
1963 sa.sa_handler = interrupt;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001964 }
1965 /* SIG_IGN, or set handler for these */
1966 sigaction(SIGHUP, &sa, NULL);
1967 sigaction(SIGINT, &sa, NULL);
1968 sigaction(SIGQUIT, &sa, NULL);
1969 sigaction(SIGPIPE, &sa, NULL);
1970 sigaction(SIGTERM, &sa, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001971 }
Denys Vlasenkofd883382012-03-09 13:03:41 +01001972 if (nprocs != 0 || daemonized_tracer)
Roland McGrath02203312007-06-11 22:06:31 +00001973 startup_attach();
Roland McGrath02203312007-06-11 22:06:31 +00001974
Denys Vlasenkofd883382012-03-09 13:03:41 +01001975 /* Do we want pids printed in our -o OUTFILE?
1976 * -ff: no (every pid has its own file); or
1977 * -f: yes (there can be more pids in the future); or
1978 * -p PID1,PID2: yes (there are already more than one pid)
1979 */
1980 print_pid_pfx = (outfname && followfork < 2 && (followfork == 1 || nprocs > 1));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001981}
1982
Denys Vlasenkoeebb04d2012-01-27 15:24:48 +01001983static struct tcb *
Roland McGrath54e931f2010-09-14 18:59:20 -07001984pid2tcb(int pid)
1985{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001986 unsigned int i;
Roland McGrath54e931f2010-09-14 18:59:20 -07001987
1988 if (pid <= 0)
1989 return NULL;
1990
1991 for (i = 0; i < tcbtabsize; i++) {
1992 struct tcb *tcp = tcbtab[i];
Denys Vlasenkofadbf662013-06-26 14:14:29 +02001993 if (tcp->pid == pid)
Roland McGrath54e931f2010-09-14 18:59:20 -07001994 return tcp;
1995 }
1996
1997 return NULL;
1998}
1999
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002000static void
Denys Vlasenko12014262011-05-30 14:00:14 +02002001cleanup(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002002{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00002003 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002004 struct tcb *tcp;
Denys Vlasenko35218842012-01-29 21:17:56 +01002005 int fatal_sig;
2006
2007 /* 'interrupted' is a volatile object, fetch it only once */
2008 fatal_sig = interrupted;
2009 if (!fatal_sig)
2010 fatal_sig = SIGTERM;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002011
Roland McGrathee9d4352002-12-18 04:16:10 +00002012 for (i = 0; i < tcbtabsize; i++) {
2013 tcp = tcbtab[i];
Denys Vlasenkofadbf662013-06-26 14:14:29 +02002014 if (!tcp->pid)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002015 continue;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002016 if (debug_flag)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002017 fprintf(stderr,
2018 "cleanup: looking at pid %u\n", tcp->pid);
Denys Vlasenkofadbf662013-06-26 14:14:29 +02002019 if (tcp->pid == strace_child) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002020 kill(tcp->pid, SIGCONT);
Denys Vlasenkoa3559252012-01-29 16:43:51 +01002021 kill(tcp->pid, fatal_sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002022 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002023 detach(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002024 }
2025 if (cflag)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002026 call_summary(shared_log);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002027}
2028
2029static void
Denys Vlasenko12014262011-05-30 14:00:14 +02002030interrupt(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002031{
Denys Vlasenkoa3559252012-01-29 16:43:51 +01002032 interrupted = sig;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002033}
2034
Denys Vlasenkod0ffdf42013-07-01 13:02:33 +02002035static void
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002036print_debug_info(const int pid, const int status)
2037{
2038 const unsigned int event = (unsigned int) status >> 16;
2039 char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16];
2040 char evbuf[sizeof(",EVENT_VFORK_DONE (%u)") + sizeof(int)*3 /*paranoia:*/ + 16];
2041
2042 strcpy(buf, "???");
2043 if (WIFSIGNALED(status))
2044#ifdef WCOREDUMP
2045 sprintf(buf, "WIFSIGNALED,%ssig=%s",
2046 WCOREDUMP(status) ? "core," : "",
2047 signame(WTERMSIG(status)));
2048#else
2049 sprintf(buf, "WIFSIGNALED,sig=%s",
2050 signame(WTERMSIG(status)));
2051#endif
2052 if (WIFEXITED(status))
2053 sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status));
2054 if (WIFSTOPPED(status))
2055 sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status)));
2056#ifdef WIFCONTINUED
2057 /* Should never be seen */
2058 if (WIFCONTINUED(status))
2059 strcpy(buf, "WIFCONTINUED");
2060#endif
2061 evbuf[0] = '\0';
2062 if (event != 0) {
2063 static const char *const event_names[] = {
2064 [PTRACE_EVENT_CLONE] = "CLONE",
2065 [PTRACE_EVENT_FORK] = "FORK",
2066 [PTRACE_EVENT_VFORK] = "VFORK",
2067 [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE",
2068 [PTRACE_EVENT_EXEC] = "EXEC",
2069 [PTRACE_EVENT_EXIT] = "EXIT",
2070 /* [PTRACE_EVENT_STOP (=128)] would make biggish array */
2071 };
2072 const char *e = "??";
2073 if (event < ARRAY_SIZE(event_names))
2074 e = event_names[event];
2075 else if (event == PTRACE_EVENT_STOP)
2076 e = "STOP";
2077 sprintf(evbuf, ",EVENT_%s (%u)", e, event);
2078 }
2079 fprintf(stderr, " [wait(0x%06x) = %u] %s%s\n", status, pid, buf, evbuf);
2080}
2081
2082static struct tcb *
2083maybe_allocate_tcb(const int pid, const int status)
2084{
2085 if (!WIFSTOPPED(status)) {
Dmitry V. Levinaa801922015-02-07 18:48:55 +00002086 if (detach_on_execve && pid == strace_child) {
2087 /* example: strace -bexecve sh -c 'exec true' */
2088 strace_child = 0;
2089 return NULL;
2090 }
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002091 /*
2092 * This can happen if we inherited an unknown child.
Dmitry V. Levinaa801922015-02-07 18:48:55 +00002093 * Example: (sleep 1 & exec strace true)
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002094 */
2095 error_msg("Exit of unknown pid %u ignored", pid);
2096 return NULL;
2097 }
2098 if (followfork) {
2099 /* We assume it's a fork/vfork/clone child */
2100 struct tcb *tcp = alloctcb(pid);
2101 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
2102 newoutf(tcp);
2103 if (!qflag)
2104 fprintf(stderr, "Process %d attached\n", pid);
2105 return tcp;
2106 } else {
2107 /* This can happen if a clone call used
2108 * CLONE_PTRACE itself.
2109 */
2110 ptrace(PTRACE_CONT, pid, (char *) 0, 0);
2111 error_msg("Stop of unknown pid %u seen, PTRACE_CONTed it", pid);
2112 return NULL;
2113 }
2114}
2115
2116static struct tcb *
2117maybe_switch_tcbs(struct tcb *tcp, const int pid)
2118{
2119 FILE *fp;
2120 struct tcb *execve_thread;
2121 long old_pid = 0;
2122
2123 if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) < 0)
2124 return tcp;
2125 /* Avoid truncation in pid2tcb() param passing */
2126 if (old_pid <= 0 || old_pid == pid)
2127 return tcp;
2128 if ((unsigned long) old_pid > UINT_MAX)
2129 return tcp;
2130 execve_thread = pid2tcb(old_pid);
2131 /* It should be !NULL, but I feel paranoid */
2132 if (!execve_thread)
2133 return tcp;
2134
2135 if (execve_thread->curcol != 0) {
2136 /*
2137 * One case we are here is -ff:
2138 * try "strace -oLOG -ff test/threaded_execve"
2139 */
2140 fprintf(execve_thread->outf, " <pid changed to %d ...>\n", pid);
2141 /*execve_thread->curcol = 0; - no need, see code below */
2142 }
2143 /* Swap output FILEs (needed for -ff) */
2144 fp = execve_thread->outf;
2145 execve_thread->outf = tcp->outf;
2146 tcp->outf = fp;
2147 /* And their column positions */
2148 execve_thread->curcol = tcp->curcol;
2149 tcp->curcol = 0;
2150 /* Drop leader, but close execve'd thread outfile (if -ff) */
2151 droptcb(tcp);
2152 /* Switch to the thread, reusing leader's outfile and pid */
2153 tcp = execve_thread;
2154 tcp->pid = pid;
2155 if (cflag != CFLAG_ONLY_STATS) {
2156 printleader(tcp);
2157 tprintf("+++ superseded by execve in pid %lu +++\n", old_pid);
2158 line_ended();
2159 tcp->flags |= TCB_REPRINT;
2160 }
2161
2162 return tcp;
2163}
2164
2165static void
2166print_signalled(struct tcb *tcp, const int pid, const int status)
2167{
Dmitry V. Levin2cd488b2015-02-07 19:41:48 +00002168 if (pid == strace_child) {
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002169 exit_code = 0x100 | WTERMSIG(status);
Dmitry V. Levin2cd488b2015-02-07 19:41:48 +00002170 strace_child = 0;
2171 }
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002172
2173 if (cflag != CFLAG_ONLY_STATS
2174 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)
2175 ) {
2176 printleader(tcp);
2177#ifdef WCOREDUMP
2178 tprintf("+++ killed by %s %s+++\n",
2179 signame(WTERMSIG(status)),
2180 WCOREDUMP(status) ? "(core dumped) " : "");
2181#else
2182 tprintf("+++ killed by %s +++\n",
2183 signame(WTERMSIG(status)));
2184#endif
2185 line_ended();
2186 }
2187}
2188
2189static void
2190print_exited(struct tcb *tcp, const int pid, const int status)
2191{
Dmitry V. Levin2cd488b2015-02-07 19:41:48 +00002192 if (pid == strace_child) {
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002193 exit_code = WEXITSTATUS(status);
Dmitry V. Levin2cd488b2015-02-07 19:41:48 +00002194 strace_child = 0;
2195 }
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002196
2197 if (cflag != CFLAG_ONLY_STATS &&
2198 qflag < 2) {
2199 printleader(tcp);
2200 tprintf("+++ exited with %d +++\n", WEXITSTATUS(status));
2201 line_ended();
2202 }
2203}
2204
2205static void
2206print_stopped(struct tcb *tcp, const siginfo_t *si, const unsigned int sig)
2207{
2208 if (cflag != CFLAG_ONLY_STATS
2209 && !hide_log_until_execve
2210 && (qual_flags[sig] & QUAL_SIGNAL)
2211 ) {
2212 printleader(tcp);
2213 if (si) {
2214 tprintf("--- %s ", signame(sig));
2215 printsiginfo(si, verbose(tcp));
2216 tprints(" ---\n");
2217 } else
2218 tprintf("--- stopped by %s ---\n", signame(sig));
2219 line_ended();
2220 }
2221}
2222
2223static bool
2224startup_tcb(struct tcb *tcp)
2225{
2226 if (debug_flag)
2227 fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n",
2228 tcp->pid);
2229
2230 tcp->flags &= ~TCB_STARTUP;
2231
2232 if (tcp->flags & TCB_BPTSET) {
2233 /*
2234 * One example is a breakpoint inherited from
2235 * parent through fork().
2236 */
2237 if (clearbpt(tcp) < 0) {
2238 /* Pretty fatal */
2239 droptcb(tcp);
2240 exit_code = 1;
2241 return false;
2242 }
2243 }
2244
2245 if (!use_seize && ptrace_setoptions) {
2246 if (debug_flag)
2247 fprintf(stderr, "setting opts 0x%x on pid %d\n",
2248 ptrace_setoptions, tcp->pid);
2249 if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) {
2250 if (errno != ESRCH) {
2251 /* Should never happen, really */
2252 perror_msg_and_die("PTRACE_SETOPTIONS");
2253 }
2254 }
2255 }
2256
2257 return true;
2258}
2259
2260/* Returns true iff the main trace loop has to continue. */
2261static bool
Denys Vlasenko12014262011-05-30 14:00:14 +02002262trace(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002263{
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002264 int pid;
2265 int wait_errno;
2266 int status;
2267 bool stopped;
2268 unsigned int sig;
2269 unsigned int event;
2270 struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002271 struct rusage ru;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002272
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002273 if (interrupted)
2274 return false;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002275
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002276 if (popen_pid != 0 && nprocs == 0)
2277 return false;
Denys Vlasenko725dd422013-06-20 11:06:58 +02002278
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002279 if (interactive)
2280 sigprocmask(SIG_SETMASK, &empty_set, NULL);
2281 pid = wait4(-1, &status, __WALL, (cflag ? &ru : NULL));
2282 wait_errno = errno;
2283 if (interactive)
2284 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
Denys Vlasenko7c41ce22013-07-08 13:55:04 +02002285
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002286 if (pid < 0) {
2287 if (wait_errno == EINTR)
2288 return true;
2289 if (nprocs == 0 && wait_errno == ECHILD)
2290 return false;
2291 /*
2292 * If nprocs > 0, ECHILD is not expected,
2293 * treat it as any other error here:
2294 */
2295 errno = wait_errno;
2296 perror_msg_and_die("wait4(__WALL)");
2297 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002298
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002299 if (pid == popen_pid) {
2300 if (!WIFSTOPPED(status))
2301 popen_pid = 0;
2302 return true;
2303 }
Denys Vlasenko725dd422013-06-20 11:06:58 +02002304
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002305 if (debug_flag)
2306 print_debug_info(pid, status);
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002307
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002308 /* Look up 'pid' in our table. */
2309 tcp = pid2tcb(pid);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002310
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002311 if (!tcp) {
2312 tcp = maybe_allocate_tcb(pid, status);
2313 if (!tcp)
2314 return true;
2315 }
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002316
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002317 clear_regs();
2318 if (WIFSTOPPED(status))
2319 get_regs(pid);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002320
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002321 event = (unsigned int) status >> 16;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002322
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002323 if (event == PTRACE_EVENT_EXEC) {
2324 /*
2325 * Under Linux, execve changes pid to thread leader's pid,
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002326 * and we see this changed pid on EVENT_EXEC and later,
2327 * execve sysexit. Leader "disappears" without exit
2328 * notification. Let user know that, drop leader's tcb,
2329 * and fix up pid in execve thread's tcb.
2330 * Effectively, execve thread's tcb replaces leader's tcb.
2331 *
2332 * BTW, leader is 'stuck undead' (doesn't report WIFEXITED
2333 * on exit syscall) in multithreaded programs exactly
2334 * in order to handle this case.
2335 *
2336 * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0.
2337 * On 2.6 and earlier, it can return garbage.
2338 */
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002339 if (os_release >= KERNEL_VERSION(3,0,0))
2340 tcp = maybe_switch_tcbs(tcp, pid);
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002341
Dmitry V. Levine6908132015-02-07 17:47:53 +00002342 if (detach_on_execve && !skip_one_b_execve) {
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002343 detach(tcp); /* do "-b execve" thingy */
Dmitry V. Levine6908132015-02-07 17:47:53 +00002344 return true;
2345 }
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002346 skip_one_b_execve = 0;
2347 }
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002348
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002349 /* Set current output file */
2350 current_tcp = tcp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002351
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002352 if (cflag) {
2353 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2354 tcp->stime = ru.ru_stime;
2355 }
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002356
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002357 if (WIFSIGNALED(status)) {
2358 print_signalled(tcp, pid, status);
2359 droptcb(tcp);
2360 return true;
2361 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002362
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002363 if (WIFEXITED(status)) {
2364 print_exited(tcp, pid, status);
2365 droptcb(tcp);
2366 return true;
2367 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002368
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002369 if (!WIFSTOPPED(status)) {
2370 /*
2371 * Neither signalled, exited or stopped.
2372 * How could that be?
2373 */
2374 error_msg("pid %u not stopped!", pid);
2375 droptcb(tcp);
2376 return true;
2377 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002378
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002379 /* Is this the very first time we see this tracee stopped? */
2380 if (tcp->flags & TCB_STARTUP) {
2381 if (!startup_tcb(tcp))
2382 return false;
2383 }
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002384
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002385 sig = WSTOPSIG(status);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002386
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002387 if (event != 0) {
2388 /* Ptrace event */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002389#if USE_SEIZE
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002390 if (event == PTRACE_EVENT_STOP) {
2391 /*
2392 * PTRACE_INTERRUPT-stop or group-stop.
2393 * PTRACE_INTERRUPT-stop has sig == SIGTRAP here.
2394 */
2395 switch (sig) {
2396 case SIGSTOP:
2397 case SIGTSTP:
2398 case SIGTTIN:
2399 case SIGTTOU:
2400 stopped = true;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002401 goto show_stopsig;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002402 }
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002403 }
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002404#endif
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002405 goto restart_tracee_with_sig_0;
2406 }
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002407
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002408 /*
2409 * Is this post-attach SIGSTOP?
2410 * Interestingly, the process may stop
2411 * with STOPSIG equal to some other signal
2412 * than SIGSTOP if we happend to attach
2413 * just before the process takes a signal.
2414 */
2415 if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
2416 if (debug_flag)
2417 fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid);
2418 tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
2419 goto restart_tracee_with_sig_0;
2420 }
2421
2422 if (sig != syscall_trap_sig) {
2423 siginfo_t si;
2424
2425 /*
2426 * True if tracee is stopped by signal
2427 * (as opposed to "tracee received signal").
2428 * TODO: shouldn't we check for errno == EINVAL too?
2429 * We can get ESRCH instead, you know...
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002430 */
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002431 stopped = ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0;
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002432#if USE_SEIZE
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002433show_stopsig:
Denys Vlasenko67038162012-01-29 16:46:46 +01002434#endif
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002435 print_stopped(tcp, stopped ? NULL : &si, sig);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002436
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002437 if (!stopped)
2438 /* It's signal-delivery-stop. Inject the signal */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002439 goto restart_tracee;
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002440
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002441 /* It's group-stop */
2442 if (use_seize) {
2443 /*
2444 * This ends ptrace-stop, but does *not* end group-stop.
2445 * This makes stopping signals work properly on straced process
2446 * (that is, process really stops. It used to continue to run).
Denys Vlasenkof2025022012-03-09 15:29:45 +01002447 */
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002448 if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) {
2449 /* Note: ptrace_restart emitted error message */
2450 exit_code = 1;
2451 return false;
2452 }
2453 return true;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002454 }
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002455 /* We don't have PTRACE_LISTEN support... */
2456 goto restart_tracee;
2457 }
2458
2459 /* We handled quick cases, we are permitted to interrupt now. */
2460 if (interrupted)
2461 return false;
2462
2463 /*
2464 * This should be syscall entry or exit.
2465 * (Or it still can be that pesky post-execve SIGTRAP!)
2466 * Handle it.
2467 */
2468 if (trace_syscall(tcp) < 0) {
2469 /*
2470 * ptrace() failed in trace_syscall().
2471 * Likely a result of process disappearing mid-flight.
2472 * Observed case: exit_group() or SIGKILL terminating
2473 * all processes in thread group.
2474 * We assume that ptrace error was caused by process death.
2475 * We used to detach(tcp) here, but since we no longer
2476 * implement "detach before death" policy/hack,
2477 * we can let this process to report its death to us
2478 * normally, via WIFEXITED or WIFSIGNALED wait status.
2479 */
2480 return true;
2481 }
2482
2483restart_tracee_with_sig_0:
2484 sig = 0;
2485
2486restart_tracee:
2487 if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) {
2488 /* Note: ptrace_restart emitted error message */
2489 exit_code = 1;
2490 return false;
2491 }
2492
2493 return true;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002494}
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002495
2496int
2497main(int argc, char *argv[])
2498{
2499 init(argc, argv);
2500
Dmitry V. Levin4b4ec122015-02-07 17:31:54 +00002501 /*
2502 * Run main tracing loop.
2503 *
2504 * Used to be "while (nprocs != 0)", but in this testcase:
2505 * int main() { _exit(!!fork()); }
2506 * under strace -f, parent sometimes (rarely) manages
2507 * to exit before we see the first stop of the child,
2508 * and we are losing track of it:
2509 * 19923 clone(...) = 19924
2510 * 19923 exit_group(1) = ?
2511 * 19923 +++ exited with 1 +++
2512 * Waiting for ECHILD works better.
2513 * (However, if -o|logger is in use, we can't do that.
2514 * Can work around that by double-forking the logger,
2515 * but that loses the ability to wait for its completion on exit.
2516 * Oh well...)
2517 */
2518 while (trace())
2519 ;
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002520
2521 cleanup();
2522 fflush(NULL);
Dmitry V. Levincf534362012-07-12 20:54:46 +00002523 if (shared_log != stderr)
2524 fclose(shared_log);
2525 if (popen_pid) {
2526 while (waitpid(popen_pid, NULL, 0) < 0 && errno == EINTR)
2527 ;
2528 }
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002529 if (exit_code > 0xff) {
2530 /* Avoid potential core file clobbering. */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00002531 struct_rlimit rlim = {0, 0};
2532 set_rlimit(RLIMIT_CORE, &rlim);
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002533
2534 /* Child was killed by a signal, mimic that. */
2535 exit_code &= 0xff;
2536 signal(exit_code, SIG_DFL);
2537 raise(exit_code);
2538 /* Paranoia - what if this signal is not fatal?
2539 Exit with 128 + signo then. */
2540 exit_code += 128;
2541 }
2542
2543 return exit_code;
2544}