blob: 55089bf3be744b755110a67dc5e8eb417200910d [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00006 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000029 */
30
31#include "defs.h"
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020032#include <stdarg.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000033#include <sys/param.h>
34#include <fcntl.h>
35#include <sys/resource.h>
36#include <sys/wait.h>
37#include <sys/stat.h>
38#include <pwd.h>
39#include <grp.h>
Roland McGrath70b08532004-04-09 00:25:21 +000040#include <dirent.h>
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +010041#include <sys/utsname.h>
Dmitry V. Levin882478a2013-05-13 18:43:28 +000042#ifdef HAVE_PRCTL
43# include <sys/prctl.h>
44#endif
Denys Vlasenko84703742012-02-25 02:38:52 +010045#if defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +000046# include <asm/ptrace_offsets.h>
47#endif
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010048/* In some libc, these aren't declared. Do it ourself: */
Denys Vlasenko96d5a762008-12-29 19:13:27 +000049extern char **environ;
Denys Vlasenko418d66a2009-01-17 01:52:54 +000050extern int optind;
51extern char *optarg;
Denys Vlasenko96d5a762008-12-29 19:13:27 +000052
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010053
54#if defined __NR_tkill
55# define my_tkill(tid, sig) syscall(__NR_tkill, (tid), (sig))
56#else
57 /* kill() may choose arbitrarily the target task of the process group
58 while we later wait on a that specific TID. PID process waits become
59 TID task specific waits for a process under ptrace(2). */
Denys Vlasenko978fbc92012-09-13 10:28:43 +020060# warning "tkill(2) not available, risk of strace hangs!"
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010061# define my_tkill(tid, sig) kill((tid), (sig))
62#endif
63
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +010064/* Glue for systems without a MMU that cannot provide fork() */
65#if !defined(HAVE_FORK)
66# undef NOMMU_SYSTEM
67# define NOMMU_SYSTEM 1
68#endif
69#if NOMMU_SYSTEM
70# define fork() vfork()
71#endif
72
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010073cflag_t cflag = CFLAG_NONE;
74unsigned int followfork = 0;
Denys Vlasenkof44cce42011-06-21 14:34:10 +020075unsigned int ptrace_setoptions = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010076unsigned int xflag = 0;
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +010077bool need_fork_exec_workarounds = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010078bool debug_flag = 0;
79bool Tflag = 0;
Daniel P. Berrange01997cf2013-05-13 11:30:55 +010080unsigned int qflag = 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020081/* Which WSTOPSIG(status) value marks syscall traps? */
Denys Vlasenko75422762011-05-27 14:36:01 +020082static unsigned int syscall_trap_sig = SIGTRAP;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010083static unsigned int tflag = 0;
84static bool iflag = 0;
85static bool rflag = 0;
86static bool print_pid_pfx = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +010087
88/* -I n */
89enum {
90 INTR_NOT_SET = 0,
91 INTR_ANYWHERE = 1, /* don't block/ignore any signals */
92 INTR_WHILE_WAIT = 2, /* block fatal signals while decoding syscall. default */
93 INTR_NEVER = 3, /* block fatal signals. default if '-o FILE PROG' */
94 INTR_BLOCK_TSTP_TOO = 4, /* block fatal signals and SIGTSTP (^Z) */
95 NUM_INTR_OPTS
96};
97static int opt_intr;
98/* We play with signal mask only if this mode is active: */
99#define interactive (opt_intr == INTR_WHILE_WAIT)
100
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000101/*
102 * daemonized_tracer supports -D option.
103 * With this option, strace forks twice.
104 * Unlike normal case, with -D *grandparent* process exec's,
105 * becoming a traced process. Child exits (this prevents traced process
106 * from having children it doesn't expect to have), and grandchild
107 * attaches to grandparent similarly to strace -p PID.
108 * This allows for more transparent interaction in cases
109 * when process and its parent are communicating via signals,
110 * wait() etc. Without -D, strace process gets lodged in between,
111 * disrupting parent<->child link.
112 */
113static bool daemonized_tracer = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000114
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100115#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100116static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP;
117# define use_seize (post_attach_sigstop == 0)
118#else
119# define post_attach_sigstop TCB_IGNORE_ONE_SIGSTOP
120# define use_seize 0
121#endif
122
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000123/* Sometimes we want to print only succeeding syscalls. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100124bool not_failing_only = 0;
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000125
Grant Edwards8a082772011-04-07 20:25:40 +0000126/* Show path associated with fd arguments */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100127bool show_fd_path = 0;
Grant Edwards8a082772011-04-07 20:25:40 +0000128
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100129static bool detach_on_execve = 0;
Denys Vlasenko2a3d2752013-05-14 16:07:46 +0200130/* Are we "strace PROG" and need to skip detach on first execve? */
131static bool skip_one_b_execve = 0;
132/* Are we "strace PROG" and need to hide everything until execve? */
133bool hide_log_until_execve = 0;
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100134
Dmitry V. Levina6809652008-11-10 17:14:58 +0000135static int exit_code = 0;
136static int strace_child = 0;
Denys Vlasenko75422762011-05-27 14:36:01 +0200137static int strace_tracer_pid = 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -0700138
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000139static char *username = NULL;
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200140static uid_t run_uid;
141static gid_t run_gid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000142
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100143unsigned int max_strlen = DEFAULT_STRLEN;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000144static int acolumn = DEFAULT_ACOLUMN;
Denys Vlasenko102ec492011-08-25 01:27:59 +0200145static char *acolumn_spaces;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100146
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000147static char *outfname = NULL;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100148/* If -ff, points to stderr. Else, it's our common output log */
149static FILE *shared_log;
150
Denys Vlasenko000b6012012-01-28 01:25:03 +0100151struct tcb *printing_tcp = NULL;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100152static struct tcb *current_tcp;
153
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200154static struct tcb **tcbtab;
Denys Vlasenko2b60c352011-06-22 12:45:25 +0200155static unsigned int nprocs, tcbtabsize;
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200156static const char *progname;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000157
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100158unsigned os_release; /* generated from uname()'s u.release */
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +0100159
Denys Vlasenko4a9ba982013-06-20 11:20:23 +0200160static void detach(struct tcb *tcp);
Andreas Schwabe5355de2009-10-27 16:56:43 +0100161static int trace(void);
162static void cleanup(void);
163static void interrupt(int sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000164static sigset_t empty_set, blocked_set;
165
166#ifdef HAVE_SIG_ATOMIC_T
167static volatile sig_atomic_t interrupted;
Denys Vlasenkoa3559252012-01-29 16:43:51 +0100168#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000169static volatile int interrupted;
Denys Vlasenkoa3559252012-01-29 16:43:51 +0100170#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000171
Denys Vlasenko2c4fb902012-03-15 17:24:49 +0100172#ifndef HAVE_STRERROR
173
174#if !HAVE_DECL_SYS_ERRLIST
175extern int sys_nerr;
176extern char *sys_errlist[];
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +0100177#endif
Denys Vlasenko2c4fb902012-03-15 17:24:49 +0100178
179const char *
180strerror(int err_no)
181{
182 static char buf[sizeof("Unknown error %d") + sizeof(int)*3];
183
184 if (err_no < 1 || err_no >= sys_nerr) {
185 sprintf(buf, "Unknown error %d", err_no);
186 return buf;
187 }
188 return sys_errlist[err_no];
189}
190
191#endif /* HAVE_STERRROR */
192
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000193static void
Denys Vlasenkocb2ad002011-06-23 13:05:29 +0200194usage(FILE *ofp, int exitval)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000195{
196 fprintf(ofp, "\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200197usage: strace [-CdffhiqrtttTvVxxy] [-I n] [-e expr]...\n\
198 [-a column] [-o file] [-s strsize] [-P path]...\n\
199 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
200 or: strace -c[df] [-I n] [-e expr]... [-O overhead] [-S sortby]\n\
201 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000202-c -- count time, calls, and errors for each syscall and report summary\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200203-C -- like -c but also print regular output\n\
Denys Vlasenko3e084ac2012-03-15 13:39:05 +0100204-d -- enable debug output to stderr\n\
Denys Vlasenkob51581e2012-01-29 16:53:03 +0100205-D -- run tracer process as a detached grandchild, not as parent\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000206-f -- follow forks, -ff -- with output into separate files\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000207-i -- print instruction pointer at time of syscall\n\
208-q -- suppress messages about attaching, detaching, etc.\n\
209-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
Denys Vlasenkocdab1be2012-02-03 12:17:57 +0100210-T -- print time spent in each syscall\n\
211-v -- verbose mode: print unabbreviated argv, stat, termios, etc. args\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000212-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000213-y -- print paths associated with file descriptor arguments\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200214-h -- print help message, -V -- print version\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000215-a column -- alignment COLUMN for printing syscall results (default %d)\n\
Denys Vlasenko22efaf02013-02-27 12:15:19 +0100216-b execve -- detach on this syscall\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000217-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
Denys Vlasenko38e79bb2013-02-26 11:33:54 +0100218 options: trace, abbrev, verbose, raw, signal, read, write\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200219-I interruptible --\n\
220 1: no signals are blocked\n\
221 2: fatal signals are blocked while decoding syscall (default)\n\
222 3: fatal signals are always blocked (default if '-o FILE PROG')\n\
223 4: fatal signals and SIGTSTP (^Z) are always blocked\n\
224 (useful to make 'strace -o FILE PROG' not stop on ^Z)\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000225-o file -- send trace output to FILE instead of stderr\n\
226-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
227-p pid -- trace process with process id PID, may be repeated\n\
228-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
229-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
230-u username -- run command as username handling setuid and/or setgid\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000231-E var=val -- put var=val in the environment for command\n\
232-E var -- remove var from the environment for command\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000233-P path -- trace accesses to path\n\
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100234"
Denys Vlasenko38e79bb2013-02-26 11:33:54 +0100235/* ancient, no one should use it
236-F -- attempt to follow vforks (deprecated, use -f)\n\
237 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100238/* this is broken, so don't document it
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000239-z -- print only succeeding syscalls\n\
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100240 */
Roland McGrathde6e5332003-01-24 04:31:23 +0000241, DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000242 exit(exitval);
243}
244
Denys Vlasenko75422762011-05-27 14:36:01 +0200245static void die(void) __attribute__ ((noreturn));
246static void die(void)
247{
248 if (strace_tracer_pid == getpid()) {
249 cflag = 0;
250 cleanup();
251 }
252 exit(1);
253}
254
255static void verror_msg(int err_no, const char *fmt, va_list p)
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200256{
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100257 char *msg;
258
Dmitry V. Levin44d05322011-06-09 15:50:41 +0000259 fflush(NULL);
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100260
261 /* We want to print entire message with single fprintf to ensure
262 * message integrity if stderr is shared with other programs.
263 * Thus we use vasprintf + single fprintf.
264 */
265 msg = NULL;
Denys Vlasenkocfad5432012-01-24 12:48:02 +0100266 if (vasprintf(&msg, fmt, p) >= 0) {
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100267 if (err_no)
268 fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(err_no));
269 else
270 fprintf(stderr, "%s: %s\n", progname, msg);
271 free(msg);
272 } else {
273 /* malloc in vasprintf failed, try it without malloc */
274 fprintf(stderr, "%s: ", progname);
275 vfprintf(stderr, fmt, p);
276 if (err_no)
277 fprintf(stderr, ": %s\n", strerror(err_no));
278 else
279 putc('\n', stderr);
280 }
281 /* We don't switch stderr to buffered, thus fprintf(stderr)
282 * always flushes its output and this is not necessary: */
283 /* fflush(stderr); */
Denys Vlasenko75422762011-05-27 14:36:01 +0200284}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200285
Denys Vlasenko75422762011-05-27 14:36:01 +0200286void error_msg(const char *fmt, ...)
287{
288 va_list p;
289 va_start(p, fmt);
290 verror_msg(0, fmt, p);
291 va_end(p);
292}
293
294void error_msg_and_die(const char *fmt, ...)
295{
296 va_list p;
297 va_start(p, fmt);
298 verror_msg(0, fmt, p);
299 die();
300}
301
302void perror_msg(const char *fmt, ...)
303{
304 va_list p;
305 va_start(p, fmt);
306 verror_msg(errno, fmt, p);
307 va_end(p);
308}
309
310void perror_msg_and_die(const char *fmt, ...)
311{
312 va_list p;
313 va_start(p, fmt);
314 verror_msg(errno, fmt, p);
315 die();
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200316}
317
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200318void die_out_of_memory(void)
319{
320 static bool recursed = 0;
321 if (recursed)
322 exit(1);
323 recursed = 1;
324 error_msg_and_die("Out of memory");
325}
326
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000327static void
328error_opt_arg(int opt, const char *arg)
329{
330 error_msg_and_die("Invalid -%c argument: '%s'", opt, arg);
331}
332
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100333#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100334static int
335ptrace_attach_or_seize(int pid)
336{
337 int r;
338 if (!use_seize)
339 return ptrace(PTRACE_ATTACH, pid, 0, 0);
Denys Vlasenko26bc0602012-07-10 16:36:32 +0200340 r = ptrace(PTRACE_SEIZE, pid, 0, 0);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100341 if (r)
342 return r;
343 r = ptrace(PTRACE_INTERRUPT, pid, 0, 0);
344 return r;
345}
346#else
347# define ptrace_attach_or_seize(pid) ptrace(PTRACE_ATTACH, (pid), 0, 0)
348#endif
349
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100350/*
351 * Used when we want to unblock stopped traced process.
352 * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL.
353 * Returns 0 on success or if error was ESRCH
354 * (presumably process was killed while we talk to it).
355 * Otherwise prints error message and returns -1.
356 */
357static int
358ptrace_restart(int op, struct tcb *tcp, int sig)
359{
360 int err;
361 const char *msg;
362
363 errno = 0;
364 ptrace(op, tcp->pid, (void *) 0, (long) sig);
365 err = errno;
Denys Vlasenko23506752012-03-21 10:32:49 +0100366 if (!err)
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100367 return 0;
368
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100369 msg = "SYSCALL";
370 if (op == PTRACE_CONT)
371 msg = "CONT";
372 if (op == PTRACE_DETACH)
373 msg = "DETACH";
374#ifdef PTRACE_LISTEN
375 if (op == PTRACE_LISTEN)
376 msg = "LISTEN";
377#endif
Denys Vlasenko23506752012-03-21 10:32:49 +0100378 /*
379 * Why curcol != 0? Otherwise sometimes we get this:
380 *
381 * 10252 kill(10253, SIGKILL) = 0
382 * <ptrace(SYSCALL,10252):No such process>10253 ...next decode...
383 *
384 * 10252 died after we retrieved syscall exit data,
385 * but before we tried to restart it. Log looks ugly.
386 */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100387 if (current_tcp && current_tcp->curcol != 0) {
Denys Vlasenko23506752012-03-21 10:32:49 +0100388 tprintf(" <ptrace(%s):%s>\n", msg, strerror(err));
389 line_ended();
390 }
391 if (err == ESRCH)
392 return 0;
393 errno = err;
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100394 perror_msg("ptrace(PTRACE_%s,pid:%d,sig:%d)", msg, tcp->pid, sig);
395 return -1;
396}
397
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200398static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000399set_cloexec_flag(int fd)
400{
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200401 int flags, newflags;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000402
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200403 flags = fcntl(fd, F_GETFD);
404 if (flags < 0) {
405 /* Can happen only if fd is bad.
406 * Should never happen: if it does, we have a bug
407 * in the caller. Therefore we just abort
408 * instead of propagating the error.
409 */
410 perror_msg_and_die("fcntl(%d, F_GETFD)", fd);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000411 }
412
413 newflags = flags | FD_CLOEXEC;
414 if (flags == newflags)
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200415 return;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000416
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200417 fcntl(fd, F_SETFD, newflags); /* never fails */
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000418}
419
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100420static void kill_save_errno(pid_t pid, int sig)
421{
422 int saved_errno = errno;
423
424 (void) kill(pid, sig);
425 errno = saved_errno;
426}
427
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100428/*
429 * When strace is setuid executable, we have to swap uids
430 * before and after filesystem and process management operations.
431 */
432static void
433swap_uid(void)
434{
435 int euid = geteuid(), uid = getuid();
436
437 if (euid != uid && setreuid(euid, uid) < 0) {
438 perror_msg_and_die("setreuid");
439 }
440}
441
442#if _LFS64_LARGEFILE
443# define fopen_for_output fopen64
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000444# define struct_stat struct stat64
445# define stat_file stat64
446# define struct_dirent struct dirent64
447# define read_dir readdir64
448# define struct_rlimit struct rlimit64
449# define set_rlimit setrlimit64
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100450#else
451# define fopen_for_output fopen
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000452# define struct_stat struct stat
453# define stat_file stat
454# define struct_dirent struct dirent
455# define read_dir readdir
456# define struct_rlimit struct rlimit
457# define set_rlimit setrlimit
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100458#endif
459
460static FILE *
461strace_fopen(const char *path)
462{
463 FILE *fp;
464
465 swap_uid();
466 fp = fopen_for_output(path, "w");
467 if (!fp)
468 perror_msg_and_die("Can't fopen '%s'", path);
469 swap_uid();
470 set_cloexec_flag(fileno(fp));
471 return fp;
472}
473
474static int popen_pid = 0;
475
476#ifndef _PATH_BSHELL
477# define _PATH_BSHELL "/bin/sh"
478#endif
479
480/*
481 * We cannot use standard popen(3) here because we have to distinguish
482 * popen child process from other processes we trace, and standard popen(3)
483 * does not export its child's pid.
484 */
485static FILE *
486strace_popen(const char *command)
487{
488 FILE *fp;
489 int fds[2];
490
491 swap_uid();
492 if (pipe(fds) < 0)
493 perror_msg_and_die("pipe");
494
495 set_cloexec_flag(fds[1]); /* never fails */
496
497 popen_pid = vfork();
498 if (popen_pid == -1)
499 perror_msg_and_die("vfork");
500
501 if (popen_pid == 0) {
502 /* child */
503 close(fds[1]);
504 if (fds[0] != 0) {
505 if (dup2(fds[0], 0))
506 perror_msg_and_die("dup2");
507 close(fds[0]);
508 }
509 execl(_PATH_BSHELL, "sh", "-c", command, NULL);
510 perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL);
511 }
512
513 /* parent */
514 close(fds[0]);
515 swap_uid();
516 fp = fdopen(fds[1], "w");
517 if (!fp)
518 die_out_of_memory();
519 return fp;
520}
521
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100522void
523tprintf(const char *fmt, ...)
524{
525 va_list args;
526
527 va_start(args, fmt);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100528 if (current_tcp) {
Denys Vlasenko6e4f3c12012-04-16 18:22:19 +0200529 int n = strace_vfprintf(current_tcp->outf, fmt, args);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100530 if (n < 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100531 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000532 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100533 } else
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100534 current_tcp->curcol += n;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100535 }
536 va_end(args);
537}
538
539void
540tprints(const char *str)
541{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100542 if (current_tcp) {
Denys Vlasenko142aee02012-04-16 18:10:15 +0200543 int n = fputs_unlocked(str, current_tcp->outf);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100544 if (n >= 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100545 current_tcp->curcol += strlen(str);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100546 return;
547 }
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100548 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000549 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100550 }
551}
552
553void
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100554line_ended(void)
555{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100556 if (current_tcp) {
557 current_tcp->curcol = 0;
558 fflush(current_tcp->outf);
559 }
560 if (printing_tcp) {
561 printing_tcp->curcol = 0;
562 printing_tcp = NULL;
563 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100564}
565
566void
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100567printleader(struct tcb *tcp)
568{
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100569 /* If -ff, "previous tcb we printed" is always the same as current,
570 * because we have per-tcb output files.
571 */
572 if (followfork >= 2)
573 printing_tcp = tcp;
574
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100575 if (printing_tcp) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100576 current_tcp = printing_tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100577 if (printing_tcp->curcol != 0 && (followfork < 2 || printing_tcp == tcp)) {
578 /*
579 * case 1: we have a shared log (i.e. not -ff), and last line
580 * wasn't finished (same or different tcb, doesn't matter).
581 * case 2: split log, we are the same tcb, but our last line
582 * didn't finish ("SIGKILL nuked us after syscall entry" etc).
583 */
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100584 tprints(" <unfinished ...>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100585 printing_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100586 }
587 }
588
589 printing_tcp = tcp;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100590 current_tcp = tcp;
591 current_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100592
593 if (print_pid_pfx)
594 tprintf("%-5d ", tcp->pid);
595 else if (nprocs > 1 && !outfname)
596 tprintf("[pid %5u] ", tcp->pid);
597
598 if (tflag) {
599 char str[sizeof("HH:MM:SS")];
600 struct timeval tv, dtv;
601 static struct timeval otv;
602
603 gettimeofday(&tv, NULL);
604 if (rflag) {
605 if (otv.tv_sec == 0)
606 otv = tv;
607 tv_sub(&dtv, &tv, &otv);
608 tprintf("%6ld.%06ld ",
609 (long) dtv.tv_sec, (long) dtv.tv_usec);
610 otv = tv;
611 }
612 else if (tflag > 2) {
613 tprintf("%ld.%06ld ",
614 (long) tv.tv_sec, (long) tv.tv_usec);
615 }
616 else {
617 time_t local = tv.tv_sec;
618 strftime(str, sizeof(str), "%T", localtime(&local));
619 if (tflag > 1)
620 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
621 else
622 tprintf("%s ", str);
623 }
624 }
625 if (iflag)
626 printcall(tcp);
627}
628
629void
630tabto(void)
631{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100632 if (current_tcp->curcol < acolumn)
633 tprints(acolumn_spaces + current_tcp->curcol);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100634}
635
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100636/* Should be only called directly *after successful attach* to a tracee.
637 * Otherwise, "strace -oFILE -ff -p<nonexistant_pid>"
638 * may create bogus empty FILE.<nonexistant_pid>, and then die.
639 */
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200640static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000641newoutf(struct tcb *tcp)
642{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100643 tcp->outf = shared_log; /* if not -ff mode, the same file is for all */
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100644 if (followfork >= 2) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000645 char name[520 + sizeof(int) * 3];
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000646 sprintf(name, "%.512s.%u", outfname, tcp->pid);
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200647 tcp->outf = strace_fopen(name);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000648 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000649}
650
Denys Vlasenko558e5122012-03-12 23:32:16 +0100651static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100652expand_tcbtab(void)
653{
654 /* Allocate some more TCBs and expand the table.
655 We don't want to relocate the TCBs because our
656 callers have pointers and it would be a pain.
657 So tcbtab is a table of pointers. Since we never
658 free the TCBs, we allocate a single chunk of many. */
659 int i = tcbtabsize;
660 struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0]));
661 struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0]));
662 if (!newtab || !newtcbs)
663 die_out_of_memory();
664 tcbtabsize *= 2;
665 tcbtab = newtab;
666 while (i < tcbtabsize)
667 tcbtab[i++] = newtcbs++;
668}
669
670static struct tcb *
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100671alloctcb(int pid)
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100672{
673 int i;
674 struct tcb *tcp;
675
676 if (nprocs == tcbtabsize)
677 expand_tcbtab();
678
679 for (i = 0; i < tcbtabsize; i++) {
680 tcp = tcbtab[i];
681 if ((tcp->flags & TCB_INUSE) == 0) {
682 memset(tcp, 0, sizeof(*tcp));
683 tcp->pid = pid;
684 tcp->flags = TCB_INUSE;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100685#if SUPPORTED_PERSONALITIES > 1
686 tcp->currpers = current_personality;
687#endif
688 nprocs++;
689 if (debug_flag)
690 fprintf(stderr, "new tcb for pid %d, active tcbs:%d\n", tcp->pid, nprocs);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100691 return tcp;
692 }
693 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100694 error_msg_and_die("bug in alloctcb");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100695}
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100696
697static void
698droptcb(struct tcb *tcp)
699{
700 if (tcp->pid == 0)
701 return;
702
703 nprocs--;
704 if (debug_flag)
705 fprintf(stderr, "dropped tcb for pid %d, %d remain\n", tcp->pid, nprocs);
706
707 if (tcp->outf) {
Denys Vlasenko8511f2a2012-03-22 09:35:51 +0100708 if (followfork >= 2) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100709 if (tcp->curcol != 0)
710 fprintf(tcp->outf, " <detached ...>\n");
711 fclose(tcp->outf);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100712 } else {
713 if (printing_tcp == tcp && tcp->curcol != 0)
714 fprintf(tcp->outf, " <detached ...>\n");
715 fflush(tcp->outf);
716 }
717 }
718
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100719 if (current_tcp == tcp)
720 current_tcp = NULL;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100721 if (printing_tcp == tcp)
722 printing_tcp = NULL;
723
724 memset(tcp, 0, sizeof(*tcp));
725}
726
Denys Vlasenkof1669e72013-06-18 18:09:39 +0200727/* Detach traced process.
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100728 * Never call DETACH twice on the same process as both unattached and
729 * attached-unstopped processes give the same ESRCH. For unattached process we
730 * would SIGSTOP it and wait for its SIGSTOP notification forever.
731 */
Denys Vlasenko4a9ba982013-06-20 11:20:23 +0200732static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100733detach(struct tcb *tcp)
734{
735 int error;
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200736 int status, sigstop_expected, interrupt_done;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100737
738 if (tcp->flags & TCB_BPTSET)
739 clearbpt(tcp);
740
741 /*
742 * Linux wrongly insists the child be stopped
743 * before detaching. Arghh. We go through hoops
744 * to make a clean break of things.
745 */
746#if defined(SPARC)
Denys Vlasenko978fbc92012-09-13 10:28:43 +0200747# undef PTRACE_DETACH
748# define PTRACE_DETACH PTRACE_SUNDETACH
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100749#endif
750
751 error = 0;
752 sigstop_expected = 0;
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200753 interrupt_done = 0;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100754 if (tcp->flags & TCB_ATTACHED) {
755 /*
756 * We attached but possibly didn't see the expected SIGSTOP.
757 * We must catch exactly one as otherwise the detached process
758 * would be left stopped (process state T).
759 */
760 sigstop_expected = (tcp->flags & TCB_IGNORE_ONE_SIGSTOP);
Denys Vlasenkoa2de9da2013-06-21 15:50:41 +0200761 if (sigstop_expected)
762 goto wait_loop;
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200763 error = ptrace(PTRACE_DETACH, tcp->pid, 0, 0);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100764 if (error == 0) {
765 /* On a clear day, you can see forever. */
766 }
767 else if (errno != ESRCH) {
768 /* Shouldn't happen. */
Denys Vlasenkofdfa47a2013-06-20 12:10:21 +0200769 perror_msg("detach: ptrace(PTRACE_DETACH,%u)", tcp->pid);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100770 }
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200771 else
772 /* ESRCH: process is either not stopped or doesn't exist. */
773 if (my_tkill(tcp->pid, 0) < 0) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100774 if (errno != ESRCH)
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200775 /* Shouldn't happen. */
Denys Vlasenko4a9ba982013-06-20 11:20:23 +0200776 perror_msg("detach: tkill(%u,0)", tcp->pid);
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200777 /* else: process doesn't exist. */
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100778 }
779 else
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200780 /* Process is not stopped. */
781 if (!sigstop_expected) {
782 /* We need to stop it. */
783 if (use_seize) {
784 /*
785 * With SEIZE, tracee can be in group-stop already.
786 * In this state sending it another SIGSTOP does nothing.
787 * Need to use INTERRUPT.
788 * Testcase: trying to ^C a "strace -p <stopped_process>".
789 */
790 error = ptrace(PTRACE_INTERRUPT, tcp->pid, 0, 0);
791 if (!error)
792 interrupt_done = 1;
Denys Vlasenko4a9ba982013-06-20 11:20:23 +0200793 else if (errno != ESRCH)
Denys Vlasenkofdfa47a2013-06-20 12:10:21 +0200794 perror_msg("detach: ptrace(PTRACE_INTERRUPT,%u)", tcp->pid);
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200795 }
796 else {
797 error = my_tkill(tcp->pid, SIGSTOP);
798 if (!error)
799 sigstop_expected = 1;
Denys Vlasenko4a9ba982013-06-20 11:20:23 +0200800 else if (errno != ESRCH)
801 perror_msg("detach: tkill(%u,SIGSTOP)", tcp->pid);
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200802 }
803 }
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100804 }
805
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200806 if (sigstop_expected || interrupt_done) {
Denys Vlasenkoa2de9da2013-06-21 15:50:41 +0200807 wait_loop:
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100808 for (;;) {
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200809 int sig;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100810 if (waitpid(tcp->pid, &status, __WALL) < 0) {
Denys Vlasenko725dd422013-06-20 11:06:58 +0200811 if (errno == EINTR)
812 continue;
Denys Vlasenkofdfa47a2013-06-20 12:10:21 +0200813 /*
814 * if (errno == ECHILD) break;
815 * ^^^ WRONG! We expect this PID to exist,
816 * and want to emit a message otherwise:
817 */
Denys Vlasenko4a9ba982013-06-20 11:20:23 +0200818 perror_msg("detach: waitpid(%u)", tcp->pid);
Denys Vlasenko725dd422013-06-20 11:06:58 +0200819 break;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100820 }
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100821 if (!WIFSTOPPED(status)) {
Denys Vlasenkofdfa47a2013-06-20 12:10:21 +0200822 /*
823 * Tracee exited or was killed by signal.
824 * We shouldn't normally reach this place:
825 * we don't want to consume exit status.
826 * Consider "strace -p PID" being ^C-ed:
827 * we want merely to detach from PID.
828 *
829 * However, we _can_ end up here if tracee
830 * was SIGKILLed.
831 */
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100832 break;
833 }
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200834 sig = WSTOPSIG(status);
835 if (debug_flag)
836 fprintf(stderr, "detach wait: event:%d sig:%d\n",
837 (unsigned)status >> 16, sig);
838 if (sigstop_expected && sig == SIGSTOP) {
839 /* Detach, suppressing SIGSTOP */
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100840 ptrace_restart(PTRACE_DETACH, tcp, 0);
841 break;
842 }
Denys Vlasenko69e27ef2013-06-19 15:31:39 +0200843 if (interrupt_done) {
844 unsigned event = (unsigned)status >> 16;
845 if (event == PTRACE_EVENT_STOP /*&& sig == SIGTRAP*/) {
846 /*
847 * sig == SIGTRAP: PTRACE_INTERRUPT stop.
848 * sig == other: process was already stopped
849 * with this stopping sig (see tests/detach-stopped).
850 * Looks like re-injecting this sig is not necessary
851 * in DETACH for the tracee to remain stopped.
852 */
853 sig = 0;
854 }
855 /*
856 * PTRACE_INTERRUPT is not guaranteed to produce
857 * the above event if other ptrace-stop is pending.
858 * See tests/detach-sleeping testcase:
859 * strace got SIGINT while tracee is sleeping.
860 * We sent PTRACE_INTERRUPT.
861 * We see syscall exit, not PTRACE_INTERRUPT stop.
862 * We won't get PTRACE_INTERRUPT stop
863 * if we would CONT now. Need to DETACH.
864 */
865 if (sig == syscall_trap_sig)
866 sig = 0;
867 /* else: not sure in which case we can be here.
868 * Signal stop? Inject it while detaching.
869 */
870 ptrace_restart(PTRACE_DETACH, tcp, sig);
871 break;
872 }
873 if (sig == syscall_trap_sig)
874 sig = 0;
875 /* Can't detach just yet, may need to wait for SIGSTOP */
876 error = ptrace_restart(PTRACE_CONT, tcp, sig);
Denys Vlasenkofdfa47a2013-06-20 12:10:21 +0200877 if (error < 0) {
878 /* Should not happen.
879 * Note: ptrace_restart returns 0 on ESRCH, so it's not it.
880 * ptrace_restart already emitted error message.
881 */
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100882 break;
Denys Vlasenkofdfa47a2013-06-20 12:10:21 +0200883 }
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100884 }
885 }
886
887 if (!qflag && (tcp->flags & TCB_ATTACHED))
888 fprintf(stderr, "Process %u detached\n", tcp->pid);
889
890 droptcb(tcp);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100891}
892
893static void
Denys Vlasenko558e5122012-03-12 23:32:16 +0100894process_opt_p_list(char *opt)
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100895{
896 while (*opt) {
897 /*
898 * We accept -p PID,PID; -p "`pidof PROG`"; -p "`pgrep PROG`".
899 * pidof uses space as delim, pgrep uses newline. :(
900 */
901 int pid;
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100902 char *delim = opt + strcspn(opt, ", \n\t");
903 char c = *delim;
904
905 *delim = '\0';
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000906 pid = string_to_uint(opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100907 if (pid <= 0) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000908 error_msg_and_die("Invalid process id: '%s'", opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100909 }
910 if (pid == strace_tracer_pid) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000911 error_msg_and_die("I'm sorry, I can't let you do that, Dave.");
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100912 }
913 *delim = c;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100914 alloctcb(pid);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100915 if (c == '\0')
916 break;
917 opt = delim + 1;
918 }
919}
920
Roland McGrath02203312007-06-11 22:06:31 +0000921static void
922startup_attach(void)
923{
924 int tcbi;
925 struct tcb *tcp;
926
927 /*
928 * Block user interruptions as we would leave the traced
929 * process stopped (process state T) if we would terminate in
Denys Vlasenko2e968c02011-09-01 10:23:09 +0200930 * between PTRACE_ATTACH and wait4() on SIGSTOP.
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200931 * We rely on cleanup() from this point on.
Roland McGrath02203312007-06-11 22:06:31 +0000932 */
933 if (interactive)
934 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
935
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000936 if (daemonized_tracer) {
937 pid_t pid = fork();
938 if (pid < 0) {
Denys Vlasenko014ca3a2011-09-02 16:19:30 +0200939 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000940 }
941 if (pid) { /* parent */
942 /*
Denys Vlasenko75422762011-05-27 14:36:01 +0200943 * Wait for grandchild to attach to straced process
944 * (grandparent). Grandchild SIGKILLs us after it attached.
945 * Grandparent's wait() is unblocked by our death,
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000946 * it proceeds to exec the straced program.
947 */
948 pause();
949 _exit(0); /* paranoia */
950 }
Denys Vlasenko75422762011-05-27 14:36:01 +0200951 /* grandchild */
952 /* We will be the tracer process. Remember our new pid: */
953 strace_tracer_pid = getpid();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000954 }
955
Roland McGrath02203312007-06-11 22:06:31 +0000956 for (tcbi = 0; tcbi < tcbtabsize; tcbi++) {
957 tcp = tcbtab[tcbi];
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200958
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100959 if (!(tcp->flags & TCB_INUSE))
960 continue;
961
Denys Vlasenkod116a732011-09-05 14:01:33 +0200962 /* Is this a process we should attach to, but not yet attached? */
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100963 if (tcp->flags & TCB_ATTACHED)
964 continue; /* no, we already attached it */
Denys Vlasenkod116a732011-09-05 14:01:33 +0200965
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000966 if (followfork && !daemonized_tracer) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000967 char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3];
Roland McGrath02203312007-06-11 22:06:31 +0000968 DIR *dir;
969
970 sprintf(procdir, "/proc/%d/task", tcp->pid);
971 dir = opendir(procdir);
972 if (dir != NULL) {
973 unsigned int ntid = 0, nerr = 0;
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000974 struct_dirent *de;
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200975
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000976 while ((de = read_dir(dir)) != NULL) {
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200977 struct tcb *cur_tcp;
978 int tid;
979
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000980 if (de->d_fileno == 0)
Roland McGrath02203312007-06-11 22:06:31 +0000981 continue;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000982 /* we trust /proc filesystem */
Roland McGrath02203312007-06-11 22:06:31 +0000983 tid = atoi(de->d_name);
984 if (tid <= 0)
985 continue;
986 ++ntid;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100987 if (ptrace_attach_or_seize(tid) < 0) {
Roland McGrath02203312007-06-11 22:06:31 +0000988 ++nerr;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100989 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200990 fprintf(stderr, "attach to pid %d failed\n", tid);
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200991 continue;
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200992 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100993 if (debug_flag)
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200994 fprintf(stderr, "attach to pid %d succeeded\n", tid);
995 cur_tcp = tcp;
996 if (tid != tcp->pid)
997 cur_tcp = alloctcb(tid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100998 cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100999 newoutf(cur_tcp);
Roland McGrath02203312007-06-11 22:06:31 +00001000 }
1001 closedir(dir);
Denys Vlasenko381dbc22011-09-05 13:59:39 +02001002 if (interactive) {
1003 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1004 if (interrupted)
1005 goto ret;
1006 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1007 }
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001008 ntid -= nerr;
1009 if (ntid == 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001010 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +00001011 droptcb(tcp);
1012 continue;
1013 }
1014 if (!qflag) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001015 fprintf(stderr, ntid > 1
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001016? "Process %u attached with %u threads\n"
1017: "Process %u attached\n",
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02001018 tcp->pid, ntid);
Roland McGrath02203312007-06-11 22:06:31 +00001019 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001020 if (!(tcp->flags & TCB_ATTACHED)) {
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001021 /* -p PID, we failed to attach to PID itself
1022 * but did attach to some of its sibling threads.
1023 * Drop PID's tcp.
1024 */
1025 droptcb(tcp);
1026 }
Roland McGrath02203312007-06-11 22:06:31 +00001027 continue;
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00001028 } /* if (opendir worked) */
1029 } /* if (-f) */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001030 if (ptrace_attach_or_seize(tcp->pid) < 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001031 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +00001032 droptcb(tcp);
1033 continue;
1034 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001035 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001036 newoutf(tcp);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001037 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001038 fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001039
1040 if (daemonized_tracer) {
1041 /*
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001042 * Make parent go away.
1043 * Also makes grandparent's wait() unblock.
1044 */
1045 kill(getppid(), SIGKILL);
1046 }
1047
Roland McGrath02203312007-06-11 22:06:31 +00001048 if (!qflag)
1049 fprintf(stderr,
Denys Vlasenko9c3861d2012-03-16 15:21:49 +01001050 "Process %u attached\n",
Roland McGrath02203312007-06-11 22:06:31 +00001051 tcp->pid);
Denys Vlasenkof95397a2011-06-24 16:51:16 +02001052 } /* for each tcbtab[] */
Roland McGrath02203312007-06-11 22:06:31 +00001053
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02001054 ret:
Roland McGrath02203312007-06-11 22:06:31 +00001055 if (interactive)
1056 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1057}
1058
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001059/* Stack-o-phobic exec helper, in the hope to work around
1060 * NOMMU + "daemonized tracer" difficulty.
1061 */
1062struct exec_params {
1063 int fd_to_close;
1064 uid_t run_euid;
1065 gid_t run_egid;
1066 char **argv;
1067 char *pathname;
1068};
1069static struct exec_params params_for_tracee;
1070static void __attribute__ ((noinline, noreturn))
1071exec_or_die(void)
1072{
1073 struct exec_params *params = &params_for_tracee;
1074
1075 if (params->fd_to_close >= 0)
1076 close(params->fd_to_close);
1077 if (!daemonized_tracer && !use_seize) {
1078 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) {
1079 perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)");
1080 }
1081 }
1082
1083 if (username != NULL) {
1084 /*
1085 * It is important to set groups before we
1086 * lose privileges on setuid.
1087 */
1088 if (initgroups(username, run_gid) < 0) {
1089 perror_msg_and_die("initgroups");
1090 }
1091 if (setregid(run_gid, params->run_egid) < 0) {
1092 perror_msg_and_die("setregid");
1093 }
1094 if (setreuid(run_uid, params->run_euid) < 0) {
1095 perror_msg_and_die("setreuid");
1096 }
1097 }
1098 else if (geteuid() != 0)
1099 if (setreuid(run_uid, run_uid) < 0) {
1100 perror_msg_and_die("setreuid");
1101 }
1102
1103 if (!daemonized_tracer) {
1104 /*
1105 * Induce a ptrace stop. Tracer (our parent)
1106 * will resume us with PTRACE_SYSCALL and display
1107 * the immediately following execve syscall.
1108 * Can't do this on NOMMU systems, we are after
1109 * vfork: parent is blocked, stopping would deadlock.
1110 */
1111 if (!NOMMU_SYSTEM)
1112 kill(getpid(), SIGSTOP);
1113 } else {
1114 alarm(3);
1115 /* we depend on SIGCHLD set to SIG_DFL by init code */
1116 /* if it happens to be SIG_IGN'ed, wait won't block */
1117 wait(NULL);
1118 alarm(0);
1119 }
1120
1121 execv(params->pathname, params->argv);
1122 perror_msg_and_die("exec");
1123}
1124
Roland McGrath02203312007-06-11 22:06:31 +00001125static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001126startup_child(char **argv)
Roland McGrath02203312007-06-11 22:06:31 +00001127{
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001128 struct_stat statbuf;
Roland McGrath02203312007-06-11 22:06:31 +00001129 const char *filename;
1130 char pathname[MAXPATHLEN];
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001131 int pid;
Roland McGrath02203312007-06-11 22:06:31 +00001132 struct tcb *tcp;
1133
1134 filename = argv[0];
1135 if (strchr(filename, '/')) {
1136 if (strlen(filename) > sizeof pathname - 1) {
1137 errno = ENAMETOOLONG;
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001138 perror_msg_and_die("exec");
Roland McGrath02203312007-06-11 22:06:31 +00001139 }
1140 strcpy(pathname, filename);
1141 }
1142#ifdef USE_DEBUGGING_EXEC
1143 /*
1144 * Debuggers customarily check the current directory
1145 * first regardless of the path but doing that gives
1146 * security geeks a panic attack.
1147 */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001148 else if (stat_file(filename, &statbuf) == 0)
Roland McGrath02203312007-06-11 22:06:31 +00001149 strcpy(pathname, filename);
1150#endif /* USE_DEBUGGING_EXEC */
1151 else {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +00001152 const char *path;
Roland McGrath02203312007-06-11 22:06:31 +00001153 int m, n, len;
1154
1155 for (path = getenv("PATH"); path && *path; path += m) {
Denys Vlasenko4f3df072012-01-29 22:38:35 +01001156 const char *colon = strchr(path, ':');
1157 if (colon) {
1158 n = colon - path;
Roland McGrath02203312007-06-11 22:06:31 +00001159 m = n + 1;
1160 }
1161 else
1162 m = n = strlen(path);
1163 if (n == 0) {
1164 if (!getcwd(pathname, MAXPATHLEN))
1165 continue;
1166 len = strlen(pathname);
1167 }
1168 else if (n > sizeof pathname - 1)
1169 continue;
1170 else {
1171 strncpy(pathname, path, n);
1172 len = n;
1173 }
1174 if (len && pathname[len - 1] != '/')
1175 pathname[len++] = '/';
1176 strcpy(pathname + len, filename);
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001177 if (stat_file(pathname, &statbuf) == 0 &&
Roland McGrath02203312007-06-11 22:06:31 +00001178 /* Accept only regular files
1179 with some execute bits set.
1180 XXX not perfect, might still fail */
1181 S_ISREG(statbuf.st_mode) &&
1182 (statbuf.st_mode & 0111))
1183 break;
1184 }
1185 }
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001186 if (stat_file(pathname, &statbuf) < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001187 perror_msg_and_die("Can't stat '%s'", filename);
Roland McGrath02203312007-06-11 22:06:31 +00001188 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001189
1190 params_for_tracee.fd_to_close = (shared_log != stderr) ? fileno(shared_log) : -1;
1191 params_for_tracee.run_euid = (statbuf.st_mode & S_ISUID) ? statbuf.st_uid : run_uid;
1192 params_for_tracee.run_egid = (statbuf.st_mode & S_ISGID) ? statbuf.st_gid : run_gid;
1193 params_for_tracee.argv = argv;
1194 /*
1195 * On NOMMU, can be safely freed only after execve in tracee.
1196 * It's hard to know when that happens, so we just leak it.
1197 */
Denys Vlasenko05f32512013-02-26 12:00:34 +01001198 params_for_tracee.pathname = NOMMU_SYSTEM ? strdup(pathname) : pathname;
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001199
Dmitry V. Levin882478a2013-05-13 18:43:28 +00001200#if defined HAVE_PRCTL && defined PR_SET_PTRACER && defined PR_SET_PTRACER_ANY
1201 if (daemonized_tracer)
1202 prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY);
1203#endif
1204
Dmitry V. Levina6809652008-11-10 17:14:58 +00001205 strace_child = pid = fork();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001206 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001207 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001208 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001209 if ((pid != 0 && daemonized_tracer)
1210 || (pid == 0 && !daemonized_tracer)
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001211 ) {
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001212 /* We are to become the tracee. Two cases:
1213 * -D: we are parent
1214 * not -D: we are child
1215 */
1216 exec_or_die();
Roland McGrath02203312007-06-11 22:06:31 +00001217 }
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001218
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001219 /* We are the tracer */
Denys Vlasenko75422762011-05-27 14:36:01 +02001220
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001221 if (!daemonized_tracer) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001222 if (!use_seize) {
1223 /* child did PTRACE_TRACEME, nothing to do in parent */
1224 } else {
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001225 if (!NOMMU_SYSTEM) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001226 /* Wait until child stopped itself */
1227 int status;
1228 while (waitpid(pid, &status, WSTOPPED) < 0) {
1229 if (errno == EINTR)
1230 continue;
1231 perror_msg_and_die("waitpid");
1232 }
1233 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001234 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001235 perror_msg_and_die("Unexpected wait status %x", status);
1236 }
1237 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001238 /* Else: NOMMU case, we have no way to sync.
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001239 * Just attach to it as soon as possible.
1240 * This means that we may miss a few first syscalls...
1241 */
1242
1243 if (ptrace_attach_or_seize(pid)) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001244 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001245 perror_msg_and_die("Can't attach to %d", pid);
1246 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001247 if (!NOMMU_SYSTEM)
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001248 kill(pid, SIGCONT);
1249 }
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001250 tcp = alloctcb(pid);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001251 if (!NOMMU_SYSTEM)
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001252 tcp->flags |= TCB_ATTACHED | TCB_STRACE_CHILD | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001253 else
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001254 tcp->flags |= TCB_ATTACHED | TCB_STRACE_CHILD | TCB_STARTUP;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001255 newoutf(tcp);
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001256 }
1257 else {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001258 /* With -D, we are *child* here, IOW: different pid. Fetch it: */
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001259 strace_tracer_pid = getpid();
1260 /* The tracee is our parent: */
1261 pid = getppid();
Denys Vlasenkof2025022012-03-09 15:29:45 +01001262 alloctcb(pid);
1263 /* attaching will be done later, by startup_attach */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001264 /* note: we don't do newoutf(tcp) here either! */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001265
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001266 /* NOMMU BUG! -D mode is active, we (child) return,
1267 * and we will scribble over parent's stack!
1268 * When parent later unpauses, it segfaults.
1269 *
1270 * We work around it
1271 * (1) by declaring exec_or_die() NORETURN,
1272 * hopefully compiler will just jump to it
1273 * instead of call (won't push anything to stack),
1274 * (2) by trying very hard in exec_or_die()
1275 * to not use any stack,
1276 * (3) having a really big (MAXPATHLEN) stack object
1277 * in this function, which creates a "buffer" between
1278 * child's and parent's stack pointers.
1279 * This may save us if (1) and (2) failed
1280 * and compiler decided to use stack in exec_or_die() anyway
1281 * (happens on i386 because of stack parameter passing).
Denys Vlasenko05f32512013-02-26 12:00:34 +01001282 *
1283 * A cleaner solution is to use makecontext + setcontext
1284 * to create a genuine separate stack and execute on it.
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001285 */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001286 }
Roland McGrath02203312007-06-11 22:06:31 +00001287}
1288
Wang Chaob13c0de2010-11-12 17:25:19 +08001289/*
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001290 * Test whether the kernel support PTRACE_O_TRACECLONE et al options.
Wang Chaob13c0de2010-11-12 17:25:19 +08001291 * First fork a new child, call ptrace with PTRACE_SETOPTIONS on it,
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001292 * and then see which options are supported by the kernel.
Wang Chaob13c0de2010-11-12 17:25:19 +08001293 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001294static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001295test_ptrace_setoptions_followfork(void)
Wang Chaob13c0de2010-11-12 17:25:19 +08001296{
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001297 int pid, expected_grandchild = 0, found_grandchild = 0;
1298 const unsigned int test_options = PTRACE_O_TRACECLONE |
1299 PTRACE_O_TRACEFORK |
1300 PTRACE_O_TRACEVFORK;
Wang Chaob13c0de2010-11-12 17:25:19 +08001301
Denys Vlasenko05f32512013-02-26 12:00:34 +01001302 /* Need fork for test. NOMMU has no forks */
1303 if (NOMMU_SYSTEM)
1304 goto worked; /* be bold, and pretend that test succeeded */
1305
Denys Vlasenko5d645812011-08-20 12:48:18 +02001306 pid = fork();
1307 if (pid < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001308 perror_msg_and_die("fork");
Denys Vlasenko5d645812011-08-20 12:48:18 +02001309 if (pid == 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001310 pid = getpid();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001311 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001312 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1313 __func__);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001314 kill_save_errno(pid, SIGSTOP);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001315 if (fork() < 0)
1316 perror_msg_and_die("fork");
1317 _exit(0);
Wang Chaob13c0de2010-11-12 17:25:19 +08001318 }
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001319
1320 while (1) {
1321 int status, tracee_pid;
1322
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001323 errno = 0;
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001324 tracee_pid = wait(&status);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001325 if (tracee_pid <= 0) {
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001326 if (errno == EINTR)
1327 continue;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001328 if (errno == ECHILD)
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001329 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001330 kill_save_errno(pid, SIGKILL);
1331 perror_msg_and_die("%s: unexpected wait result %d",
1332 __func__, tracee_pid);
1333 }
1334 if (WIFEXITED(status)) {
1335 if (WEXITSTATUS(status)) {
1336 if (tracee_pid != pid)
1337 kill_save_errno(pid, SIGKILL);
1338 error_msg_and_die("%s: unexpected exit status %u",
1339 __func__, WEXITSTATUS(status));
1340 }
1341 continue;
1342 }
1343 if (WIFSIGNALED(status)) {
1344 if (tracee_pid != pid)
1345 kill_save_errno(pid, SIGKILL);
1346 error_msg_and_die("%s: unexpected signal %u",
1347 __func__, WTERMSIG(status));
1348 }
1349 if (!WIFSTOPPED(status)) {
1350 if (tracee_pid != pid)
1351 kill_save_errno(tracee_pid, SIGKILL);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001352 kill_save_errno(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001353 error_msg_and_die("%s: unexpected wait status %x",
1354 __func__, status);
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001355 }
1356 if (tracee_pid != pid) {
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001357 found_grandchild = tracee_pid;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001358 if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0) {
1359 kill_save_errno(tracee_pid, SIGKILL);
1360 kill_save_errno(pid, SIGKILL);
1361 perror_msg_and_die("PTRACE_CONT doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001362 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001363 continue;
1364 }
1365 switch (WSTOPSIG(status)) {
1366 case SIGSTOP:
1367 if (ptrace(PTRACE_SETOPTIONS, pid, 0, test_options) < 0
1368 && errno != EINVAL && errno != EIO)
1369 perror_msg("PTRACE_SETOPTIONS");
1370 break;
1371 case SIGTRAP:
1372 if (status >> 16 == PTRACE_EVENT_FORK) {
1373 long msg = 0;
1374
1375 if (ptrace(PTRACE_GETEVENTMSG, pid,
1376 NULL, (long) &msg) == 0)
1377 expected_grandchild = msg;
1378 }
1379 break;
1380 }
1381 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) {
1382 kill_save_errno(pid, SIGKILL);
1383 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001384 }
1385 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001386 if (expected_grandchild && expected_grandchild == found_grandchild) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001387 worked:
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001388 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001389 if (debug_flag)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001390 fprintf(stderr, "ptrace_setoptions = %#x\n",
1391 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001392 return 0;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001393 }
1394 error_msg("Test for PTRACE_O_TRACECLONE failed, "
1395 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001396 return 1;
Wang Chaob13c0de2010-11-12 17:25:19 +08001397}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001398
1399/*
1400 * Test whether the kernel support PTRACE_O_TRACESYSGOOD.
1401 * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it,
1402 * and then see whether it will stop with (SIGTRAP | 0x80).
1403 *
1404 * Use of this option enables correct handling of user-generated SIGTRAPs,
1405 * and SIGTRAPs generated by special instructions such as int3 on x86:
1406 * _start: .globl _start
1407 * int3
1408 * movl $42, %ebx
1409 * movl $1, %eax
1410 * int $0x80
1411 * (compile with: "gcc -nostartfiles -nostdlib -o int3 int3.S")
1412 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001413static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001414test_ptrace_setoptions_for_all(void)
1415{
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001416 const unsigned int test_options = PTRACE_O_TRACESYSGOOD |
1417 PTRACE_O_TRACEEXEC;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001418 int pid;
1419 int it_worked = 0;
1420
Denys Vlasenko05f32512013-02-26 12:00:34 +01001421 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001422 if (NOMMU_SYSTEM)
Denys Vlasenko05f32512013-02-26 12:00:34 +01001423 goto worked; /* be bold, and pretend that test succeeded */
Mike Frysinger7ff5ed92012-04-05 01:52:25 -04001424
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001425 pid = fork();
1426 if (pid < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001427 perror_msg_and_die("fork");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001428
1429 if (pid == 0) {
1430 pid = getpid();
1431 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001432 /* Note: exits with exitcode 1 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001433 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1434 __func__);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001435 kill(pid, SIGSTOP);
1436 _exit(0); /* parent should see entry into this syscall */
1437 }
1438
1439 while (1) {
1440 int status, tracee_pid;
1441
1442 errno = 0;
1443 tracee_pid = wait(&status);
1444 if (tracee_pid <= 0) {
1445 if (errno == EINTR)
1446 continue;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001447 kill_save_errno(pid, SIGKILL);
1448 perror_msg_and_die("%s: unexpected wait result %d",
1449 __func__, tracee_pid);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001450 }
Denys Vlasenko75422762011-05-27 14:36:01 +02001451 if (WIFEXITED(status)) {
1452 if (WEXITSTATUS(status) == 0)
1453 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001454 error_msg_and_die("%s: unexpected exit status %u",
1455 __func__, WEXITSTATUS(status));
1456 }
1457 if (WIFSIGNALED(status)) {
1458 error_msg_and_die("%s: unexpected signal %u",
1459 __func__, WTERMSIG(status));
Denys Vlasenko75422762011-05-27 14:36:01 +02001460 }
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001461 if (!WIFSTOPPED(status)) {
1462 kill(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001463 error_msg_and_die("%s: unexpected wait status %x",
1464 __func__, status);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001465 }
1466 if (WSTOPSIG(status) == SIGSTOP) {
1467 /*
1468 * We don't check "options aren't accepted" error.
1469 * If it happens, we'll never get (SIGTRAP | 0x80),
1470 * and thus will decide to not use the option.
1471 * IOW: the outcome of the test will be correct.
1472 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001473 if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0
1474 && errno != EINVAL && errno != EIO)
1475 perror_msg("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001476 }
1477 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
1478 it_worked = 1;
1479 }
1480 if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001481 kill_save_errno(pid, SIGKILL);
Denys Vlasenko75422762011-05-27 14:36:01 +02001482 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001483 }
1484 }
1485
1486 if (it_worked) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001487 worked:
Denys Vlasenko75422762011-05-27 14:36:01 +02001488 syscall_trap_sig = (SIGTRAP | 0x80);
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001489 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001490 if (debug_flag)
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001491 fprintf(stderr, "ptrace_setoptions = %#x\n",
1492 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001493 return 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001494 }
1495
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001496 error_msg("Test for PTRACE_O_TRACESYSGOOD failed, "
1497 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001498 return 1;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001499}
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001500
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001501#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001502static void
1503test_ptrace_seize(void)
1504{
1505 int pid;
1506
Denys Vlasenko05f32512013-02-26 12:00:34 +01001507 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001508 if (NOMMU_SYSTEM) {
1509 post_attach_sigstop = 0; /* this sets use_seize to 1 */
1510 return;
1511 }
1512
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001513 pid = fork();
1514 if (pid < 0)
1515 perror_msg_and_die("fork");
1516
1517 if (pid == 0) {
1518 pause();
1519 _exit(0);
1520 }
1521
1522 /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After
1523 * attaching tracee continues to run unless a trap condition occurs.
1524 * PTRACE_SEIZE doesn't affect signal or group stop state.
1525 */
Denys Vlasenko26bc0602012-07-10 16:36:32 +02001526 if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001527 post_attach_sigstop = 0; /* this sets use_seize to 1 */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001528 } else if (debug_flag) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001529 fprintf(stderr, "PTRACE_SEIZE doesn't work\n");
1530 }
1531
1532 kill(pid, SIGKILL);
1533
1534 while (1) {
1535 int status, tracee_pid;
1536
1537 errno = 0;
1538 tracee_pid = waitpid(pid, &status, 0);
1539 if (tracee_pid <= 0) {
1540 if (errno == EINTR)
1541 continue;
1542 perror_msg_and_die("%s: unexpected wait result %d",
1543 __func__, tracee_pid);
1544 }
1545 if (WIFSIGNALED(status)) {
1546 return;
1547 }
1548 error_msg_and_die("%s: unexpected wait status %x",
1549 __func__, status);
1550 }
1551}
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001552#else /* !USE_SEIZE */
1553# define test_ptrace_seize() ((void)0)
1554#endif
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001555
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001556static unsigned
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001557get_os_release(void)
1558{
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001559 unsigned rel;
1560 const char *p;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001561 struct utsname u;
1562 if (uname(&u) < 0)
1563 perror_msg_and_die("uname");
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001564 /* u.release has this form: "3.2.9[-some-garbage]" */
1565 rel = 0;
1566 p = u.release;
1567 for (;;) {
1568 if (!(*p >= '0' && *p <= '9'))
1569 error_msg_and_die("Bad OS release string: '%s'", u.release);
1570 /* Note: this open-codes KERNEL_VERSION(): */
1571 rel = (rel << 8) | atoi(p);
1572 if (rel >= KERNEL_VERSION(1,0,0))
1573 break;
1574 while (*p >= '0' && *p <= '9')
1575 p++;
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001576 if (*p != '.') {
1577 if (rel >= KERNEL_VERSION(0,1,0)) {
1578 /* "X.Y-something" means "X.Y.0" */
1579 rel <<= 8;
1580 break;
1581 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001582 error_msg_and_die("Bad OS release string: '%s'", u.release);
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001583 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001584 p++;
1585 }
1586 return rel;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001587}
1588
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01001589/*
1590 * Initialization part of main() was eating much stack (~0.5k),
1591 * which was unused after init.
1592 * We can reuse it if we move init code into a separate function.
1593 *
1594 * Don't want main() to inline us and defeat the reason
1595 * we have a separate function.
1596 */
1597static void __attribute__ ((noinline))
1598init(int argc, char *argv[])
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001599{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001600 struct tcb *tcp;
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001601 int c, i;
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001602 int optF = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001603 struct sigaction sa;
1604
Dmitry V. Levin08b623e2007-10-08 21:04:41 +00001605 progname = argv[0] ? argv[0] : "strace";
1606
Denys Vlasenkoa5090542012-03-15 17:27:49 +01001607 /* Make sure SIGCHLD has the default action so that waitpid
1608 definitely works without losing track of children. The user
1609 should not have given us a bogus state to inherit, but he might
1610 have. Arguably we should detect SIG_IGN here and pass it on
1611 to children, but probably noone really needs that. */
1612 signal(SIGCHLD, SIG_DFL);
1613
Denys Vlasenko75422762011-05-27 14:36:01 +02001614 strace_tracer_pid = getpid();
1615
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001616 os_release = get_os_release();
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001617
Roland McGrathee9d4352002-12-18 04:16:10 +00001618 /* Allocate the initial tcbtab. */
1619 tcbtabsize = argc; /* Surely enough for all -p args. */
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001620 tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0]));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001621 if (!tcbtab)
1622 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001623 tcp = calloc(tcbtabsize, sizeof(*tcp));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001624 if (!tcp)
1625 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001626 for (c = 0; c < tcbtabsize; c++)
1627 tcbtab[c] = tcp++;
Roland McGrathee9d4352002-12-18 04:16:10 +00001628
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001629 shared_log = stderr;
Roland McGrath138c6a32006-01-12 09:50:49 +00001630 set_sortby(DEFAULT_SORTBY);
1631 set_personality(DEFAULT_PERSONALITY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001632 qualify("trace=all");
1633 qualify("abbrev=all");
1634 qualify("verbose=all");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001635#if DEFAULT_QUAL_FLAGS != (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
1636# error Bug in DEFAULT_QUAL_FLAGS
1637#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001638 qualify("signal=all");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001639 while ((c = getopt(argc, argv,
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001640 "+b:cCdfFhiqrtTvVxyz"
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001641 "D"
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001642 "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001643 switch (c) {
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001644 case 'b':
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001645 if (strcmp(optarg, "execve") != 0)
1646 error_msg_and_die("Syscall '%s' for -b isn't supported",
1647 optarg);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001648 detach_on_execve = 1;
1649 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001650 case 'c':
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001651 if (cflag == CFLAG_BOTH) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001652 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001653 }
1654 cflag = CFLAG_ONLY_STATS;
1655 break;
1656 case 'C':
1657 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001658 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001659 }
1660 cflag = CFLAG_BOTH;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001661 break;
1662 case 'd':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001663 debug_flag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001664 break;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001665 case 'D':
1666 daemonized_tracer = 1;
1667 break;
Roland McGrath41c48222008-07-18 00:25:10 +00001668 case 'F':
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001669 optF = 1;
1670 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001671 case 'f':
1672 followfork++;
1673 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001674 case 'h':
1675 usage(stdout, 0);
1676 break;
1677 case 'i':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001678 iflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001679 break;
1680 case 'q':
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01001681 qflag++;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001682 break;
1683 case 'r':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001684 rflag = 1;
1685 /* fall through to tflag++ */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001686 case 't':
1687 tflag++;
1688 break;
1689 case 'T':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001690 Tflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001691 break;
1692 case 'x':
1693 xflag++;
1694 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001695 case 'y':
1696 show_fd_path = 1;
1697 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001698 case 'v':
1699 qualify("abbrev=none");
1700 break;
1701 case 'V':
Roland McGrath9c9a2532003-02-20 02:56:29 +00001702 printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001703 exit(0);
1704 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001705 case 'z':
1706 not_failing_only = 1;
1707 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001708 case 'a':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001709 acolumn = string_to_uint(optarg);
Denys Vlasenko102ec492011-08-25 01:27:59 +02001710 if (acolumn < 0)
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001711 error_opt_arg(c, optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001712 break;
1713 case 'e':
1714 qualify(optarg);
1715 break;
1716 case 'o':
1717 outfname = strdup(optarg);
1718 break;
1719 case 'O':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001720 i = string_to_uint(optarg);
1721 if (i < 0)
1722 error_opt_arg(c, optarg);
1723 set_overhead(i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001724 break;
1725 case 'p':
Denys Vlasenkoe8172b72012-03-09 13:01:04 +01001726 process_opt_p_list(optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001727 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001728 case 'P':
Denys Vlasenko7239dbc2013-03-05 15:46:34 +01001729 pathtrace_select(optarg);
Grant Edwards8a082772011-04-07 20:25:40 +00001730 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001731 case 's':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001732 i = string_to_uint(optarg);
1733 if (i < 0)
1734 error_opt_arg(c, optarg);
1735 max_strlen = i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001736 break;
1737 case 'S':
1738 set_sortby(optarg);
1739 break;
1740 case 'u':
1741 username = strdup(optarg);
1742 break;
Roland McGrathde6e5332003-01-24 04:31:23 +00001743 case 'E':
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001744 if (putenv(optarg) < 0)
1745 die_out_of_memory();
Roland McGrathde6e5332003-01-24 04:31:23 +00001746 break;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001747 case 'I':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001748 opt_intr = string_to_uint(optarg);
1749 if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS)
1750 error_opt_arg(c, optarg);
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001751 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001752 default:
1753 usage(stderr, 1);
1754 break;
1755 }
1756 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001757 argv += optind;
1758 /* argc -= optind; - no need, argc is not used below */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001759
Denys Vlasenko102ec492011-08-25 01:27:59 +02001760 acolumn_spaces = malloc(acolumn + 1);
1761 if (!acolumn_spaces)
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001762 die_out_of_memory();
Denys Vlasenko102ec492011-08-25 01:27:59 +02001763 memset(acolumn_spaces, ' ', acolumn);
1764 acolumn_spaces[acolumn] = '\0';
1765
Denys Vlasenko837399a2012-01-24 11:37:03 +01001766 /* Must have PROG [ARGS], or -p PID. Not both. */
Denys Vlasenkofd883382012-03-09 13:03:41 +01001767 if (!argv[0] == !nprocs)
Roland McGrathce0d1542003-11-11 21:24:23 +00001768 usage(stderr, 1);
1769
Denys Vlasenkofd883382012-03-09 13:03:41 +01001770 if (nprocs != 0 && daemonized_tracer) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001771 error_msg_and_die("-D and -p are mutually exclusive");
Wang Chaod322a4b2010-08-05 14:30:11 +08001772 }
1773
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001774 if (!followfork)
1775 followfork = optF;
1776
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001777 if (followfork >= 2 && cflag) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001778 error_msg_and_die("(-c or -C) and -ff are mutually exclusive");
Roland McGrathcb9def62006-04-25 07:48:03 +00001779 }
1780
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001781 /* See if they want to run as another user. */
1782 if (username != NULL) {
1783 struct passwd *pent;
1784
1785 if (getuid() != 0 || geteuid() != 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001786 error_msg_and_die("You must be root to use the -u option");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001787 }
Denys Vlasenko5d645812011-08-20 12:48:18 +02001788 pent = getpwnam(username);
1789 if (pent == NULL) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001790 error_msg_and_die("Cannot find user '%s'", username);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001791 }
1792 run_uid = pent->pw_uid;
1793 run_gid = pent->pw_gid;
1794 }
1795 else {
1796 run_uid = getuid();
1797 run_gid = getgid();
1798 }
1799
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001800 /*
1801 * On any reasonably recent Linux kernel (circa about 2.5.46)
1802 * need_fork_exec_workarounds should stay 0 after these tests:
1803 */
1804 /*need_fork_exec_workarounds = 0; - already is */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001805 if (followfork)
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001806 need_fork_exec_workarounds = test_ptrace_setoptions_followfork();
1807 need_fork_exec_workarounds |= test_ptrace_setoptions_for_all();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001808 test_ptrace_seize();
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001809
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001810 /* Check if they want to redirect the output. */
1811 if (outfname) {
Roland McGrath37b9a662003-11-07 02:26:54 +00001812 /* See if they want to pipe the output. */
1813 if (outfname[0] == '|' || outfname[0] == '!') {
1814 /*
1815 * We can't do the <outfname>.PID funny business
1816 * when using popen, so prohibit it.
1817 */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001818 if (followfork >= 2)
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001819 error_msg_and_die("Piping the output and -ff are mutually exclusive");
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001820 shared_log = strace_popen(outfname + 1);
Roland McGrath37b9a662003-11-07 02:26:54 +00001821 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001822 else if (followfork < 2)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001823 shared_log = strace_fopen(outfname);
Denys Vlasenko328bf252012-03-12 23:34:13 +01001824 } else {
1825 /* -ff without -o FILE is the same as single -f */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001826 if (followfork >= 2)
Denys Vlasenko328bf252012-03-12 23:34:13 +01001827 followfork = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001828 }
1829
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001830 if (!outfname || outfname[0] == '|' || outfname[0] == '!') {
Denys Vlasenkoa677da52012-01-24 11:31:51 +01001831 char *buf = malloc(BUFSIZ);
1832 if (!buf)
1833 die_out_of_memory();
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001834 setvbuf(shared_log, buf, _IOLBF, BUFSIZ);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001835 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001836 if (outfname && argv[0]) {
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001837 if (!opt_intr)
1838 opt_intr = INTR_NEVER;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001839 qflag = 1;
Roland McGrath36931052003-06-03 01:35:20 +00001840 }
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001841 if (!opt_intr)
1842 opt_intr = INTR_WHILE_WAIT;
Wang Chaob13c0de2010-11-12 17:25:19 +08001843
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001844 /* argv[0] -pPID -oFILE Default interactive setting
1845 * yes 0 0 INTR_WHILE_WAIT
1846 * no 1 0 INTR_WHILE_WAIT
1847 * yes 0 1 INTR_NEVER
1848 * no 1 1 INTR_WHILE_WAIT
Roland McGrath54cc1c82007-11-03 23:34:11 +00001849 */
1850
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001851 sigemptyset(&empty_set);
1852 sigemptyset(&blocked_set);
1853
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001854 /* startup_child() must be called before the signal handlers get
1855 * installed below as they are inherited into the spawned process.
1856 * Also we do not need to be protected by them as during interruption
1857 * in the startup_child() mode we kill the spawned process anyway.
1858 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001859 if (argv[0]) {
Dmitry V. Levin1d2435b2013-05-14 22:35:46 +00001860 if (!NOMMU_SYSTEM || daemonized_tracer)
1861 hide_log_until_execve = 1;
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001862 skip_one_b_execve = 1;
Denys Vlasenko837399a2012-01-24 11:37:03 +01001863 startup_child(argv);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001864 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001865
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001866 sa.sa_handler = SIG_IGN;
1867 sigemptyset(&sa.sa_mask);
1868 sa.sa_flags = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001869 sigaction(SIGTTOU, &sa, NULL); /* SIG_IGN */
1870 sigaction(SIGTTIN, &sa, NULL); /* SIG_IGN */
1871 if (opt_intr != INTR_ANYWHERE) {
1872 if (opt_intr == INTR_BLOCK_TSTP_TOO)
1873 sigaction(SIGTSTP, &sa, NULL); /* SIG_IGN */
1874 /*
1875 * In interactive mode (if no -o OUTFILE, or -p PID is used),
1876 * fatal signals are blocked while syscall stop is processed,
1877 * and acted on in between, when waiting for new syscall stops.
1878 * In non-interactive mode, signals are ignored.
1879 */
1880 if (opt_intr == INTR_WHILE_WAIT) {
1881 sigaddset(&blocked_set, SIGHUP);
1882 sigaddset(&blocked_set, SIGINT);
1883 sigaddset(&blocked_set, SIGQUIT);
1884 sigaddset(&blocked_set, SIGPIPE);
1885 sigaddset(&blocked_set, SIGTERM);
1886 sa.sa_handler = interrupt;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001887 }
1888 /* SIG_IGN, or set handler for these */
1889 sigaction(SIGHUP, &sa, NULL);
1890 sigaction(SIGINT, &sa, NULL);
1891 sigaction(SIGQUIT, &sa, NULL);
1892 sigaction(SIGPIPE, &sa, NULL);
1893 sigaction(SIGTERM, &sa, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001894 }
Denys Vlasenkofd883382012-03-09 13:03:41 +01001895 if (nprocs != 0 || daemonized_tracer)
Roland McGrath02203312007-06-11 22:06:31 +00001896 startup_attach();
Roland McGrath02203312007-06-11 22:06:31 +00001897
Denys Vlasenkofd883382012-03-09 13:03:41 +01001898 /* Do we want pids printed in our -o OUTFILE?
1899 * -ff: no (every pid has its own file); or
1900 * -f: yes (there can be more pids in the future); or
1901 * -p PID1,PID2: yes (there are already more than one pid)
1902 */
1903 print_pid_pfx = (outfname && followfork < 2 && (followfork == 1 || nprocs > 1));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001904}
1905
Denys Vlasenkoeebb04d2012-01-27 15:24:48 +01001906static struct tcb *
Roland McGrath54e931f2010-09-14 18:59:20 -07001907pid2tcb(int pid)
1908{
1909 int i;
1910
1911 if (pid <= 0)
1912 return NULL;
1913
1914 for (i = 0; i < tcbtabsize; i++) {
1915 struct tcb *tcp = tcbtab[i];
1916 if (tcp->pid == pid && (tcp->flags & TCB_INUSE))
1917 return tcp;
1918 }
1919
1920 return NULL;
1921}
1922
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001923static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001924cleanup(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001925{
1926 int i;
1927 struct tcb *tcp;
Denys Vlasenko35218842012-01-29 21:17:56 +01001928 int fatal_sig;
1929
1930 /* 'interrupted' is a volatile object, fetch it only once */
1931 fatal_sig = interrupted;
1932 if (!fatal_sig)
1933 fatal_sig = SIGTERM;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001934
Roland McGrathee9d4352002-12-18 04:16:10 +00001935 for (i = 0; i < tcbtabsize; i++) {
1936 tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001937 if (!(tcp->flags & TCB_INUSE))
1938 continue;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001939 if (debug_flag)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001940 fprintf(stderr,
1941 "cleanup: looking at pid %u\n", tcp->pid);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001942 if (tcp->flags & TCB_STRACE_CHILD) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001943 kill(tcp->pid, SIGCONT);
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001944 kill(tcp->pid, fatal_sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001945 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001946 detach(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001947 }
1948 if (cflag)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001949 call_summary(shared_log);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001950}
1951
1952static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001953interrupt(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001954{
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001955 interrupted = sig;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001956}
1957
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001958static int
Denys Vlasenko12014262011-05-30 14:00:14 +02001959trace(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001960{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001961 struct rusage ru;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001962
Roland McGratheb9e2e82009-06-02 16:49:22 -07001963 while (nprocs != 0) {
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001964 int pid;
1965 int wait_errno;
1966 int status, sig;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001967 int stopped;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001968 struct tcb *tcp;
1969 unsigned event;
1970
Denys Vlasenko222713a2009-03-17 14:29:59 +00001971 if (interrupted)
Roland McGratheb9e2e82009-06-02 16:49:22 -07001972 return 0;
Denys Vlasenko725dd422013-06-20 11:06:58 +02001973
Roland McGratheb9e2e82009-06-02 16:49:22 -07001974 if (interactive)
1975 sigprocmask(SIG_SETMASK, &empty_set, NULL);
Denys Vlasenko725dd422013-06-20 11:06:58 +02001976 pid = wait4(-1, &status, __WALL, (cflag ? &ru : NULL));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001977 wait_errno = errno;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001978 if (interactive)
1979 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001980
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001981 if (pid < 0) {
Denys Vlasenko725dd422013-06-20 11:06:58 +02001982 if (wait_errno == EINTR)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001983 continue;
Denys Vlasenko725dd422013-06-20 11:06:58 +02001984 if (wait_errno == ECHILD)
1985 /* Should not happen since nprocs > 0 */
Roland McGratheb9e2e82009-06-02 16:49:22 -07001986 return 0;
Denys Vlasenko725dd422013-06-20 11:06:58 +02001987 errno = wait_errno;
1988 perror_msg("wait4(__WALL)");
1989 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001990 }
Denys Vlasenko725dd422013-06-20 11:06:58 +02001991
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001992 if (pid == popen_pid) {
1993 if (WIFEXITED(status) || WIFSIGNALED(status))
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001994 popen_pid = 0;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001995 continue;
1996 }
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001997
1998 event = ((unsigned)status >> 16);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001999 if (debug_flag) {
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002000 char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002001 char evbuf[sizeof(",PTRACE_EVENT_?? (%u)") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002002 strcpy(buf, "???");
2003 if (WIFSIGNALED(status))
2004#ifdef WCOREDUMP
2005 sprintf(buf, "WIFSIGNALED,%ssig=%s",
2006 WCOREDUMP(status) ? "core," : "",
2007 signame(WTERMSIG(status)));
2008#else
2009 sprintf(buf, "WIFSIGNALED,sig=%s",
2010 signame(WTERMSIG(status)));
2011#endif
2012 if (WIFEXITED(status))
2013 sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status));
2014 if (WIFSTOPPED(status))
2015 sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status)));
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02002016#ifdef WIFCONTINUED
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002017 if (WIFCONTINUED(status))
2018 strcpy(buf, "WIFCONTINUED");
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02002019#endif
Denys Vlasenko67559ad2012-03-13 12:05:27 +01002020 evbuf[0] = '\0';
2021 if (event != 0) {
2022 static const char *const event_names[] = {
2023 [PTRACE_EVENT_CLONE] = "CLONE",
2024 [PTRACE_EVENT_FORK] = "FORK",
2025 [PTRACE_EVENT_VFORK] = "VFORK",
2026 [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE",
2027 [PTRACE_EVENT_EXEC] = "EXEC",
2028 [PTRACE_EVENT_EXIT] = "EXIT",
2029 };
2030 const char *e;
2031 if (event < ARRAY_SIZE(event_names))
2032 e = event_names[event];
2033 else {
2034 sprintf(buf, "?? (%u)", event);
2035 e = buf;
2036 }
2037 sprintf(evbuf, ",PTRACE_EVENT_%s", e);
2038 }
2039 fprintf(stderr, " [wait(0x%04x) = %u] %s%s\n", status, pid, buf, evbuf);
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02002040 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002041
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002042 /* Look up 'pid' in our table. */
Denys Vlasenko5d645812011-08-20 12:48:18 +02002043 tcp = pid2tcb(pid);
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002044
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002045 if (!tcp) {
Roland McGrath41c48222008-07-18 00:25:10 +00002046 if (followfork) {
Denys Vlasenko418d66a2009-01-17 01:52:54 +00002047 tcp = alloctcb(pid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002048 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01002049 newoutf(tcp);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002050 if (!qflag)
Denys Vlasenko833fb132011-08-17 11:30:56 +02002051 fprintf(stderr, "Process %d attached\n",
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002052 pid);
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002053 } else {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002054 /* This can happen if a clone call used
2055 CLONE_PTRACE itself. */
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002056 if (WIFSTOPPED(status))
Denys Vlasenko97c503f2012-03-09 15:11:21 +01002057 ptrace(PTRACE_CONT, pid, (char *) 0, 0);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02002058 error_msg_and_die("Unknown pid: %u", pid);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002059 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002060 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002061
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002062 clear_regs();
2063 if (WIFSTOPPED(status))
2064 get_regs(pid);
2065
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002066 /* Under Linux, execve changes pid to thread leader's pid,
2067 * and we see this changed pid on EVENT_EXEC and later,
2068 * execve sysexit. Leader "disappears" without exit
2069 * notification. Let user know that, drop leader's tcb,
2070 * and fix up pid in execve thread's tcb.
2071 * Effectively, execve thread's tcb replaces leader's tcb.
2072 *
2073 * BTW, leader is 'stuck undead' (doesn't report WIFEXITED
2074 * on exit syscall) in multithreaded programs exactly
2075 * in order to handle this case.
2076 *
2077 * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0.
2078 * On 2.6 and earlier, it can return garbage.
2079 */
2080 if (event == PTRACE_EVENT_EXEC && os_release >= KERNEL_VERSION(3,0,0)) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002081 FILE *fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002082 struct tcb *execve_thread;
2083 long old_pid = 0;
2084
2085 if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) < 0)
2086 goto dont_switch_tcbs;
2087 if (old_pid <= 0 || old_pid == pid)
2088 goto dont_switch_tcbs;
2089 execve_thread = pid2tcb(old_pid);
2090 /* It should be !NULL, but I feel paranoid */
2091 if (!execve_thread)
2092 goto dont_switch_tcbs;
2093
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002094 if (execve_thread->curcol != 0) {
2095 /*
2096 * One case we are here is -ff:
2097 * try "strace -oLOG -ff test/threaded_execve"
2098 */
2099 fprintf(execve_thread->outf, " <pid changed to %d ...>\n", pid);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002100 /*execve_thread->curcol = 0; - no need, see code below */
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002101 }
2102 /* Swap output FILEs (needed for -ff) */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002103 fp = execve_thread->outf;
2104 execve_thread->outf = tcp->outf;
2105 tcp->outf = fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002106 /* And their column positions */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002107 execve_thread->curcol = tcp->curcol;
2108 tcp->curcol = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002109 /* Drop leader, but close execve'd thread outfile (if -ff) */
2110 droptcb(tcp);
2111 /* Switch to the thread, reusing leader's outfile and pid */
2112 tcp = execve_thread;
2113 tcp->pid = pid;
2114 if (cflag != CFLAG_ONLY_STATS) {
2115 printleader(tcp);
2116 tprintf("+++ superseded by execve in pid %lu +++\n", old_pid);
2117 line_ended();
2118 tcp->flags |= TCB_REPRINT;
2119 }
2120 }
2121 dont_switch_tcbs:
2122
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002123 if (event == PTRACE_EVENT_EXEC) {
2124 if (detach_on_execve && !skip_one_b_execve)
2125 detach(tcp); /* do "-b execve" thingy */
2126 skip_one_b_execve = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002127 }
2128
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002129 /* Set current output file */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002130 current_tcp = tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002131
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002132 if (cflag) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002133 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2134 tcp->stime = ru.ru_stime;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002135 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002136
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002137 if (WIFSIGNALED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002138 if (pid == strace_child)
2139 exit_code = 0x100 | WTERMSIG(status);
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002140 if (cflag != CFLAG_ONLY_STATS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01002141 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)
2142 ) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002143 printleader(tcp);
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002144#ifdef WCOREDUMP
Denys Vlasenko000b6012012-01-28 01:25:03 +01002145 tprintf("+++ killed by %s %s+++\n",
Roland McGrath2efe8792004-01-13 09:59:45 +00002146 signame(WTERMSIG(status)),
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002147 WCOREDUMP(status) ? "(core dumped) " : "");
2148#else
Denys Vlasenko000b6012012-01-28 01:25:03 +01002149 tprintf("+++ killed by %s +++\n",
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002150 signame(WTERMSIG(status)));
Roland McGrath2efe8792004-01-13 09:59:45 +00002151#endif
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002152 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002153 }
2154 droptcb(tcp);
2155 continue;
2156 }
2157 if (WIFEXITED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002158 if (pid == strace_child)
2159 exit_code = WEXITSTATUS(status);
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01002160 if (cflag != CFLAG_ONLY_STATS &&
2161 qflag < 2) {
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002162 printleader(tcp);
Denys Vlasenko000b6012012-01-28 01:25:03 +01002163 tprintf("+++ exited with %d +++\n", WEXITSTATUS(status));
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002164 line_ended();
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002165 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002166 droptcb(tcp);
2167 continue;
2168 }
2169 if (!WIFSTOPPED(status)) {
2170 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
2171 droptcb(tcp);
2172 continue;
2173 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002174
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002175 /* Is this the very first time we see this tracee stopped? */
2176 if (tcp->flags & TCB_STARTUP) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002177 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002178 fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n", tcp->pid);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002179 tcp->flags &= ~TCB_STARTUP;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002180 if (tcp->flags & TCB_BPTSET) {
Roland McGrath02203312007-06-11 22:06:31 +00002181 /*
2182 * One example is a breakpoint inherited from
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002183 * parent through fork().
Roland McGrath02203312007-06-11 22:06:31 +00002184 */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002185 if (clearbpt(tcp) < 0) {
2186 /* Pretty fatal */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002187 droptcb(tcp);
2188 cleanup();
2189 return -1;
2190 }
2191 }
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002192 if (ptrace_setoptions) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002193 if (debug_flag)
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002194 fprintf(stderr, "setting opts %x on pid %d\n", ptrace_setoptions, tcp->pid);
2195 if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) {
2196 if (errno != ESRCH) {
2197 /* Should never happen, really */
2198 perror_msg_and_die("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02002199 }
2200 }
2201 }
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002202 }
2203
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002204 sig = WSTOPSIG(status);
2205
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002206 if (event != 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002207 /* Ptrace event */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002208#if USE_SEIZE
Denys Vlasenko26bc0602012-07-10 16:36:32 +02002209 if (event == PTRACE_EVENT_STOP) {
Denys Vlasenko67038162012-01-29 16:46:46 +01002210 /*
2211 * PTRACE_INTERRUPT-stop or group-stop.
2212 * PTRACE_INTERRUPT-stop has sig == SIGTRAP here.
2213 */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002214 if (sig == SIGSTOP
2215 || sig == SIGTSTP
2216 || sig == SIGTTIN
2217 || sig == SIGTTOU
2218 ) {
2219 stopped = 1;
2220 goto show_stopsig;
2221 }
2222 }
2223#endif
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002224 goto restart_tracee_with_sig_0;
2225 }
2226
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002227 /* Is this post-attach SIGSTOP?
2228 * Interestingly, the process may stop
2229 * with STOPSIG equal to some other signal
2230 * than SIGSTOP if we happend to attach
2231 * just before the process takes a signal.
2232 */
2233 if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002234 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002235 fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid);
2236 tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002237 goto restart_tracee_with_sig_0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002238 }
2239
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002240 if (sig != syscall_trap_sig) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002241 siginfo_t si;
2242
2243 /* Nonzero (true) if tracee is stopped by signal
2244 * (as opposed to "tracee received signal").
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002245 * TODO: shouldn't we check for errno == EINVAL too?
2246 * We can get ESRCH instead, you know...
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002247 */
2248 stopped = (ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002249#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002250 show_stopsig:
Denys Vlasenko67038162012-01-29 16:46:46 +01002251#endif
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002252 if (cflag != CFLAG_ONLY_STATS
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002253 && !hide_log_until_execve
2254 && (qual_flags[sig] & QUAL_SIGNAL)
2255 ) {
Dmitry V. Levinc15dfc72011-03-10 14:44:45 +00002256#if defined(PT_CR_IPSR) && defined(PT_CR_IIP)
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002257 long pc = 0;
2258 long psr = 0;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002259
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002260 upeek(tcp, PT_CR_IPSR, &psr);
2261 upeek(tcp, PT_CR_IIP, &pc);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002262
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002263# define PSR_RI 41
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002264 pc += (psr >> PSR_RI) & 0x3;
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002265# define PC_FORMAT_STR " @ %lx"
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002266# define PC_FORMAT_ARG , pc
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002267#else
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002268# define PC_FORMAT_STR ""
2269# define PC_FORMAT_ARG /* nothing */
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002270#endif
Dmitry V. Levine8ff4c62013-05-28 20:27:10 +00002271 printleader(tcp);
2272 if (!stopped) {
2273 tprintf("--- %s ", signame(sig));
2274 printsiginfo(&si, verbose(tcp));
2275 tprintf(PC_FORMAT_STR " ---\n"
2276 PC_FORMAT_ARG);
2277 } else
2278 tprintf("--- stopped by %s" PC_FORMAT_STR " ---\n",
2279 signame(sig)
2280 PC_FORMAT_ARG);
2281 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002282 }
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002283
2284 if (!stopped)
2285 /* It's signal-delivery-stop. Inject the signal */
2286 goto restart_tracee;
2287
2288 /* It's group-stop */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002289 if (use_seize) {
2290 /*
2291 * This ends ptrace-stop, but does *not* end group-stop.
2292 * This makes stopping signals work properly on straced process
2293 * (that is, process really stops. It used to continue to run).
2294 */
2295 if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) {
2296 cleanup();
2297 return -1;
2298 }
2299 continue;
2300 }
2301 /* We don't have PTRACE_LISTEN support... */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002302 goto restart_tracee;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002303 }
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002304
2305 /* We handled quick cases, we are permitted to interrupt now. */
Roland McGrath02203312007-06-11 22:06:31 +00002306 if (interrupted)
2307 return 0;
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002308
2309 /* This should be syscall entry or exit.
2310 * (Or it still can be that pesky post-execve SIGTRAP!)
2311 * Handle it.
2312 */
Denys Vlasenko23506752012-03-21 10:32:49 +01002313 if (trace_syscall(tcp) < 0) {
2314 /* ptrace() failed in trace_syscall().
Roland McGratheb9e2e82009-06-02 16:49:22 -07002315 * Likely a result of process disappearing mid-flight.
Denys Vlasenko23506752012-03-21 10:32:49 +01002316 * Observed case: exit_group() or SIGKILL terminating
Denys Vlasenkof1e69032012-01-04 15:15:26 +01002317 * all processes in thread group.
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002318 * We assume that ptrace error was caused by process death.
Denys Vlasenkof2025022012-03-09 15:29:45 +01002319 * We used to detach(tcp) here, but since we no longer
2320 * implement "detach before death" policy/hack,
2321 * we can let this process to report its death to us
2322 * normally, via WIFEXITED or WIFSIGNALED wait status.
2323 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002324 continue;
2325 }
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002326 restart_tracee_with_sig_0:
2327 sig = 0;
2328 restart_tracee:
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002329 if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002330 cleanup();
2331 return -1;
2332 }
2333 }
2334 return 0;
2335}
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002336
2337int
2338main(int argc, char *argv[])
2339{
2340 init(argc, argv);
2341
2342 /* Run main tracing loop */
2343 if (trace() < 0)
2344 return 1;
2345
2346 cleanup();
2347 fflush(NULL);
Dmitry V. Levincf534362012-07-12 20:54:46 +00002348 if (shared_log != stderr)
2349 fclose(shared_log);
2350 if (popen_pid) {
2351 while (waitpid(popen_pid, NULL, 0) < 0 && errno == EINTR)
2352 ;
2353 }
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002354 if (exit_code > 0xff) {
2355 /* Avoid potential core file clobbering. */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00002356 struct_rlimit rlim = {0, 0};
2357 set_rlimit(RLIMIT_CORE, &rlim);
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002358
2359 /* Child was killed by a signal, mimic that. */
2360 exit_code &= 0xff;
2361 signal(exit_code, SIG_DFL);
2362 raise(exit_code);
2363 /* Paranoia - what if this signal is not fatal?
2364 Exit with 128 + signo then. */
2365 exit_code += 128;
2366 }
2367
2368 return exit_code;
2369}