blob: 0440832f0cfb127e7b010e434fa912f97f917d35 [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>
Denys Vlasenko84703742012-02-25 02:38:52 +010042#if defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +000043# include <asm/ptrace_offsets.h>
44#endif
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010045/* In some libc, these aren't declared. Do it ourself: */
Denys Vlasenko96d5a762008-12-29 19:13:27 +000046extern char **environ;
Denys Vlasenko418d66a2009-01-17 01:52:54 +000047extern int optind;
48extern char *optarg;
Denys Vlasenko96d5a762008-12-29 19:13:27 +000049
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010050
51#if defined __NR_tkill
52# define my_tkill(tid, sig) syscall(__NR_tkill, (tid), (sig))
53#else
54 /* kill() may choose arbitrarily the target task of the process group
55 while we later wait on a that specific TID. PID process waits become
56 TID task specific waits for a process under ptrace(2). */
Denys Vlasenko978fbc92012-09-13 10:28:43 +020057# warning "tkill(2) not available, risk of strace hangs!"
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010058# define my_tkill(tid, sig) kill((tid), (sig))
59#endif
60
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +010061/* Glue for systems without a MMU that cannot provide fork() */
62#if !defined(HAVE_FORK)
63# undef NOMMU_SYSTEM
64# define NOMMU_SYSTEM 1
65#endif
66#if NOMMU_SYSTEM
67# define fork() vfork()
68#endif
69
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010070cflag_t cflag = CFLAG_NONE;
71unsigned int followfork = 0;
Denys Vlasenkof44cce42011-06-21 14:34:10 +020072unsigned int ptrace_setoptions = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010073unsigned int xflag = 0;
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +010074bool need_fork_exec_workarounds = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010075bool debug_flag = 0;
76bool Tflag = 0;
77bool qflag = 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020078/* Which WSTOPSIG(status) value marks syscall traps? */
Denys Vlasenko75422762011-05-27 14:36:01 +020079static unsigned int syscall_trap_sig = SIGTRAP;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010080static unsigned int tflag = 0;
81static bool iflag = 0;
82static bool rflag = 0;
83static bool print_pid_pfx = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +010084
85/* -I n */
86enum {
87 INTR_NOT_SET = 0,
88 INTR_ANYWHERE = 1, /* don't block/ignore any signals */
89 INTR_WHILE_WAIT = 2, /* block fatal signals while decoding syscall. default */
90 INTR_NEVER = 3, /* block fatal signals. default if '-o FILE PROG' */
91 INTR_BLOCK_TSTP_TOO = 4, /* block fatal signals and SIGTSTP (^Z) */
92 NUM_INTR_OPTS
93};
94static int opt_intr;
95/* We play with signal mask only if this mode is active: */
96#define interactive (opt_intr == INTR_WHILE_WAIT)
97
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +000098/*
99 * daemonized_tracer supports -D option.
100 * With this option, strace forks twice.
101 * Unlike normal case, with -D *grandparent* process exec's,
102 * becoming a traced process. Child exits (this prevents traced process
103 * from having children it doesn't expect to have), and grandchild
104 * attaches to grandparent similarly to strace -p PID.
105 * This allows for more transparent interaction in cases
106 * when process and its parent are communicating via signals,
107 * wait() etc. Without -D, strace process gets lodged in between,
108 * disrupting parent<->child link.
109 */
110static bool daemonized_tracer = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000111
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100112#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100113static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP;
114# define use_seize (post_attach_sigstop == 0)
115#else
116# define post_attach_sigstop TCB_IGNORE_ONE_SIGSTOP
117# define use_seize 0
118#endif
119
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000120/* Sometimes we want to print only succeeding syscalls. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100121bool not_failing_only = 0;
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000122
Grant Edwards8a082772011-04-07 20:25:40 +0000123/* Show path associated with fd arguments */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100124bool show_fd_path = 0;
Grant Edwards8a082772011-04-07 20:25:40 +0000125
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100126static bool detach_on_execve = 0;
127static bool skip_startup_execve = 0;
128
Dmitry V. Levina6809652008-11-10 17:14:58 +0000129static int exit_code = 0;
130static int strace_child = 0;
Denys Vlasenko75422762011-05-27 14:36:01 +0200131static int strace_tracer_pid = 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -0700132
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000133static char *username = NULL;
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200134static uid_t run_uid;
135static gid_t run_gid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000136
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100137unsigned int max_strlen = DEFAULT_STRLEN;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000138static int acolumn = DEFAULT_ACOLUMN;
Denys Vlasenko102ec492011-08-25 01:27:59 +0200139static char *acolumn_spaces;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100140
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000141static char *outfname = NULL;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100142/* If -ff, points to stderr. Else, it's our common output log */
143static FILE *shared_log;
144
Denys Vlasenko000b6012012-01-28 01:25:03 +0100145struct tcb *printing_tcp = NULL;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100146static struct tcb *current_tcp;
147
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200148static struct tcb **tcbtab;
Denys Vlasenko2b60c352011-06-22 12:45:25 +0200149static unsigned int nprocs, tcbtabsize;
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200150static const char *progname;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000151
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100152unsigned os_release; /* generated from uname()'s u.release */
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +0100153
Denys Vlasenko4c196382012-01-04 15:11:09 +0100154static int detach(struct tcb *tcp);
Andreas Schwabe5355de2009-10-27 16:56:43 +0100155static int trace(void);
156static void cleanup(void);
157static void interrupt(int sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000158static sigset_t empty_set, blocked_set;
159
160#ifdef HAVE_SIG_ATOMIC_T
161static volatile sig_atomic_t interrupted;
Denys Vlasenkoa3559252012-01-29 16:43:51 +0100162#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000163static volatile int interrupted;
Denys Vlasenkoa3559252012-01-29 16:43:51 +0100164#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000165
Denys Vlasenko2c4fb902012-03-15 17:24:49 +0100166#ifndef HAVE_STRERROR
167
168#if !HAVE_DECL_SYS_ERRLIST
169extern int sys_nerr;
170extern char *sys_errlist[];
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +0100171#endif
Denys Vlasenko2c4fb902012-03-15 17:24:49 +0100172
173const char *
174strerror(int err_no)
175{
176 static char buf[sizeof("Unknown error %d") + sizeof(int)*3];
177
178 if (err_no < 1 || err_no >= sys_nerr) {
179 sprintf(buf, "Unknown error %d", err_no);
180 return buf;
181 }
182 return sys_errlist[err_no];
183}
184
185#endif /* HAVE_STERRROR */
186
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000187static void
Denys Vlasenkocb2ad002011-06-23 13:05:29 +0200188usage(FILE *ofp, int exitval)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000189{
190 fprintf(ofp, "\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200191usage: strace [-CdffhiqrtttTvVxxy] [-I n] [-e expr]...\n\
192 [-a column] [-o file] [-s strsize] [-P path]...\n\
193 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
194 or: strace -c[df] [-I n] [-e expr]... [-O overhead] [-S sortby]\n\
195 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000196-c -- count time, calls, and errors for each syscall and report summary\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200197-C -- like -c but also print regular output\n\
Denys Vlasenko3e084ac2012-03-15 13:39:05 +0100198-d -- enable debug output to stderr\n\
Denys Vlasenkob51581e2012-01-29 16:53:03 +0100199-D -- run tracer process as a detached grandchild, not as parent\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000200-f -- follow forks, -ff -- with output into separate files\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000201-i -- print instruction pointer at time of syscall\n\
202-q -- suppress messages about attaching, detaching, etc.\n\
203-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
Denys Vlasenkocdab1be2012-02-03 12:17:57 +0100204-T -- print time spent in each syscall\n\
205-v -- verbose mode: print unabbreviated argv, stat, termios, etc. args\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000206-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000207-y -- print paths associated with file descriptor arguments\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200208-h -- print help message, -V -- print version\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000209-a column -- alignment COLUMN for printing syscall results (default %d)\n\
Denys Vlasenko22efaf02013-02-27 12:15:19 +0100210-b execve -- detach on this syscall\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000211-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
Denys Vlasenko38e79bb2013-02-26 11:33:54 +0100212 options: trace, abbrev, verbose, raw, signal, read, write\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200213-I interruptible --\n\
214 1: no signals are blocked\n\
215 2: fatal signals are blocked while decoding syscall (default)\n\
216 3: fatal signals are always blocked (default if '-o FILE PROG')\n\
217 4: fatal signals and SIGTSTP (^Z) are always blocked\n\
218 (useful to make 'strace -o FILE PROG' not stop on ^Z)\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000219-o file -- send trace output to FILE instead of stderr\n\
220-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
221-p pid -- trace process with process id PID, may be repeated\n\
222-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
223-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
224-u username -- run command as username handling setuid and/or setgid\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000225-E var=val -- put var=val in the environment for command\n\
226-E var -- remove var from the environment for command\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000227-P path -- trace accesses to path\n\
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100228"
Denys Vlasenko38e79bb2013-02-26 11:33:54 +0100229/* ancient, no one should use it
230-F -- attempt to follow vforks (deprecated, use -f)\n\
231 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100232/* this is broken, so don't document it
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000233-z -- print only succeeding syscalls\n\
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100234 */
Roland McGrathde6e5332003-01-24 04:31:23 +0000235, DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000236 exit(exitval);
237}
238
Denys Vlasenko75422762011-05-27 14:36:01 +0200239static void die(void) __attribute__ ((noreturn));
240static void die(void)
241{
242 if (strace_tracer_pid == getpid()) {
243 cflag = 0;
244 cleanup();
245 }
246 exit(1);
247}
248
249static void verror_msg(int err_no, const char *fmt, va_list p)
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200250{
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100251 char *msg;
252
Dmitry V. Levin44d05322011-06-09 15:50:41 +0000253 fflush(NULL);
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100254
255 /* We want to print entire message with single fprintf to ensure
256 * message integrity if stderr is shared with other programs.
257 * Thus we use vasprintf + single fprintf.
258 */
259 msg = NULL;
Denys Vlasenkocfad5432012-01-24 12:48:02 +0100260 if (vasprintf(&msg, fmt, p) >= 0) {
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100261 if (err_no)
262 fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(err_no));
263 else
264 fprintf(stderr, "%s: %s\n", progname, msg);
265 free(msg);
266 } else {
267 /* malloc in vasprintf failed, try it without malloc */
268 fprintf(stderr, "%s: ", progname);
269 vfprintf(stderr, fmt, p);
270 if (err_no)
271 fprintf(stderr, ": %s\n", strerror(err_no));
272 else
273 putc('\n', stderr);
274 }
275 /* We don't switch stderr to buffered, thus fprintf(stderr)
276 * always flushes its output and this is not necessary: */
277 /* fflush(stderr); */
Denys Vlasenko75422762011-05-27 14:36:01 +0200278}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200279
Denys Vlasenko75422762011-05-27 14:36:01 +0200280void error_msg(const char *fmt, ...)
281{
282 va_list p;
283 va_start(p, fmt);
284 verror_msg(0, fmt, p);
285 va_end(p);
286}
287
288void error_msg_and_die(const char *fmt, ...)
289{
290 va_list p;
291 va_start(p, fmt);
292 verror_msg(0, fmt, p);
293 die();
294}
295
296void perror_msg(const char *fmt, ...)
297{
298 va_list p;
299 va_start(p, fmt);
300 verror_msg(errno, fmt, p);
301 va_end(p);
302}
303
304void perror_msg_and_die(const char *fmt, ...)
305{
306 va_list p;
307 va_start(p, fmt);
308 verror_msg(errno, fmt, p);
309 die();
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200310}
311
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200312void die_out_of_memory(void)
313{
314 static bool recursed = 0;
315 if (recursed)
316 exit(1);
317 recursed = 1;
318 error_msg_and_die("Out of memory");
319}
320
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000321static void
322error_opt_arg(int opt, const char *arg)
323{
324 error_msg_and_die("Invalid -%c argument: '%s'", opt, arg);
325}
326
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100327#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100328static int
329ptrace_attach_or_seize(int pid)
330{
331 int r;
332 if (!use_seize)
333 return ptrace(PTRACE_ATTACH, pid, 0, 0);
Denys Vlasenko26bc0602012-07-10 16:36:32 +0200334 r = ptrace(PTRACE_SEIZE, pid, 0, 0);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100335 if (r)
336 return r;
337 r = ptrace(PTRACE_INTERRUPT, pid, 0, 0);
338 return r;
339}
340#else
341# define ptrace_attach_or_seize(pid) ptrace(PTRACE_ATTACH, (pid), 0, 0)
342#endif
343
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100344/*
345 * Used when we want to unblock stopped traced process.
346 * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL.
347 * Returns 0 on success or if error was ESRCH
348 * (presumably process was killed while we talk to it).
349 * Otherwise prints error message and returns -1.
350 */
351static int
352ptrace_restart(int op, struct tcb *tcp, int sig)
353{
354 int err;
355 const char *msg;
356
357 errno = 0;
358 ptrace(op, tcp->pid, (void *) 0, (long) sig);
359 err = errno;
Denys Vlasenko23506752012-03-21 10:32:49 +0100360 if (!err)
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100361 return 0;
362
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100363 msg = "SYSCALL";
364 if (op == PTRACE_CONT)
365 msg = "CONT";
366 if (op == PTRACE_DETACH)
367 msg = "DETACH";
368#ifdef PTRACE_LISTEN
369 if (op == PTRACE_LISTEN)
370 msg = "LISTEN";
371#endif
Denys Vlasenko23506752012-03-21 10:32:49 +0100372 /*
373 * Why curcol != 0? Otherwise sometimes we get this:
374 *
375 * 10252 kill(10253, SIGKILL) = 0
376 * <ptrace(SYSCALL,10252):No such process>10253 ...next decode...
377 *
378 * 10252 died after we retrieved syscall exit data,
379 * but before we tried to restart it. Log looks ugly.
380 */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100381 if (current_tcp && current_tcp->curcol != 0) {
Denys Vlasenko23506752012-03-21 10:32:49 +0100382 tprintf(" <ptrace(%s):%s>\n", msg, strerror(err));
383 line_ended();
384 }
385 if (err == ESRCH)
386 return 0;
387 errno = err;
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100388 perror_msg("ptrace(PTRACE_%s,pid:%d,sig:%d)", msg, tcp->pid, sig);
389 return -1;
390}
391
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200392static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000393set_cloexec_flag(int fd)
394{
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200395 int flags, newflags;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000396
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200397 flags = fcntl(fd, F_GETFD);
398 if (flags < 0) {
399 /* Can happen only if fd is bad.
400 * Should never happen: if it does, we have a bug
401 * in the caller. Therefore we just abort
402 * instead of propagating the error.
403 */
404 perror_msg_and_die("fcntl(%d, F_GETFD)", fd);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000405 }
406
407 newflags = flags | FD_CLOEXEC;
408 if (flags == newflags)
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200409 return;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000410
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200411 fcntl(fd, F_SETFD, newflags); /* never fails */
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000412}
413
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100414static void kill_save_errno(pid_t pid, int sig)
415{
416 int saved_errno = errno;
417
418 (void) kill(pid, sig);
419 errno = saved_errno;
420}
421
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100422/*
423 * When strace is setuid executable, we have to swap uids
424 * before and after filesystem and process management operations.
425 */
426static void
427swap_uid(void)
428{
429 int euid = geteuid(), uid = getuid();
430
431 if (euid != uid && setreuid(euid, uid) < 0) {
432 perror_msg_and_die("setreuid");
433 }
434}
435
436#if _LFS64_LARGEFILE
437# define fopen_for_output fopen64
438#else
439# define fopen_for_output fopen
440#endif
441
442static FILE *
443strace_fopen(const char *path)
444{
445 FILE *fp;
446
447 swap_uid();
448 fp = fopen_for_output(path, "w");
449 if (!fp)
450 perror_msg_and_die("Can't fopen '%s'", path);
451 swap_uid();
452 set_cloexec_flag(fileno(fp));
453 return fp;
454}
455
456static int popen_pid = 0;
457
458#ifndef _PATH_BSHELL
459# define _PATH_BSHELL "/bin/sh"
460#endif
461
462/*
463 * We cannot use standard popen(3) here because we have to distinguish
464 * popen child process from other processes we trace, and standard popen(3)
465 * does not export its child's pid.
466 */
467static FILE *
468strace_popen(const char *command)
469{
470 FILE *fp;
471 int fds[2];
472
473 swap_uid();
474 if (pipe(fds) < 0)
475 perror_msg_and_die("pipe");
476
477 set_cloexec_flag(fds[1]); /* never fails */
478
479 popen_pid = vfork();
480 if (popen_pid == -1)
481 perror_msg_and_die("vfork");
482
483 if (popen_pid == 0) {
484 /* child */
485 close(fds[1]);
486 if (fds[0] != 0) {
487 if (dup2(fds[0], 0))
488 perror_msg_and_die("dup2");
489 close(fds[0]);
490 }
491 execl(_PATH_BSHELL, "sh", "-c", command, NULL);
492 perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL);
493 }
494
495 /* parent */
496 close(fds[0]);
497 swap_uid();
498 fp = fdopen(fds[1], "w");
499 if (!fp)
500 die_out_of_memory();
501 return fp;
502}
503
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100504void
505tprintf(const char *fmt, ...)
506{
507 va_list args;
508
509 va_start(args, fmt);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100510 if (current_tcp) {
Denys Vlasenko6e4f3c12012-04-16 18:22:19 +0200511 int n = strace_vfprintf(current_tcp->outf, fmt, args);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100512 if (n < 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100513 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000514 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100515 } else
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100516 current_tcp->curcol += n;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100517 }
518 va_end(args);
519}
520
521void
522tprints(const char *str)
523{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100524 if (current_tcp) {
Denys Vlasenko142aee02012-04-16 18:10:15 +0200525 int n = fputs_unlocked(str, current_tcp->outf);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100526 if (n >= 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100527 current_tcp->curcol += strlen(str);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100528 return;
529 }
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100530 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000531 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100532 }
533}
534
535void
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100536line_ended(void)
537{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100538 if (current_tcp) {
539 current_tcp->curcol = 0;
540 fflush(current_tcp->outf);
541 }
542 if (printing_tcp) {
543 printing_tcp->curcol = 0;
544 printing_tcp = NULL;
545 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100546}
547
548void
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100549printleader(struct tcb *tcp)
550{
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100551 /* If -ff, "previous tcb we printed" is always the same as current,
552 * because we have per-tcb output files.
553 */
554 if (followfork >= 2)
555 printing_tcp = tcp;
556
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100557 if (printing_tcp) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100558 current_tcp = printing_tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100559 if (printing_tcp->curcol != 0 && (followfork < 2 || printing_tcp == tcp)) {
560 /*
561 * case 1: we have a shared log (i.e. not -ff), and last line
562 * wasn't finished (same or different tcb, doesn't matter).
563 * case 2: split log, we are the same tcb, but our last line
564 * didn't finish ("SIGKILL nuked us after syscall entry" etc).
565 */
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100566 tprints(" <unfinished ...>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100567 printing_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100568 }
569 }
570
571 printing_tcp = tcp;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100572 current_tcp = tcp;
573 current_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100574
575 if (print_pid_pfx)
576 tprintf("%-5d ", tcp->pid);
577 else if (nprocs > 1 && !outfname)
578 tprintf("[pid %5u] ", tcp->pid);
579
580 if (tflag) {
581 char str[sizeof("HH:MM:SS")];
582 struct timeval tv, dtv;
583 static struct timeval otv;
584
585 gettimeofday(&tv, NULL);
586 if (rflag) {
587 if (otv.tv_sec == 0)
588 otv = tv;
589 tv_sub(&dtv, &tv, &otv);
590 tprintf("%6ld.%06ld ",
591 (long) dtv.tv_sec, (long) dtv.tv_usec);
592 otv = tv;
593 }
594 else if (tflag > 2) {
595 tprintf("%ld.%06ld ",
596 (long) tv.tv_sec, (long) tv.tv_usec);
597 }
598 else {
599 time_t local = tv.tv_sec;
600 strftime(str, sizeof(str), "%T", localtime(&local));
601 if (tflag > 1)
602 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
603 else
604 tprintf("%s ", str);
605 }
606 }
607 if (iflag)
608 printcall(tcp);
609}
610
611void
612tabto(void)
613{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100614 if (current_tcp->curcol < acolumn)
615 tprints(acolumn_spaces + current_tcp->curcol);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100616}
617
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100618/* Should be only called directly *after successful attach* to a tracee.
619 * Otherwise, "strace -oFILE -ff -p<nonexistant_pid>"
620 * may create bogus empty FILE.<nonexistant_pid>, and then die.
621 */
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200622static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000623newoutf(struct tcb *tcp)
624{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100625 tcp->outf = shared_log; /* if not -ff mode, the same file is for all */
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100626 if (followfork >= 2) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000627 char name[520 + sizeof(int) * 3];
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000628 sprintf(name, "%.512s.%u", outfname, tcp->pid);
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200629 tcp->outf = strace_fopen(name);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000630 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000631}
632
Denys Vlasenko558e5122012-03-12 23:32:16 +0100633static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100634expand_tcbtab(void)
635{
636 /* Allocate some more TCBs and expand the table.
637 We don't want to relocate the TCBs because our
638 callers have pointers and it would be a pain.
639 So tcbtab is a table of pointers. Since we never
640 free the TCBs, we allocate a single chunk of many. */
641 int i = tcbtabsize;
642 struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0]));
643 struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0]));
644 if (!newtab || !newtcbs)
645 die_out_of_memory();
646 tcbtabsize *= 2;
647 tcbtab = newtab;
648 while (i < tcbtabsize)
649 tcbtab[i++] = newtcbs++;
650}
651
652static struct tcb *
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100653alloctcb(int pid)
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100654{
655 int i;
656 struct tcb *tcp;
657
658 if (nprocs == tcbtabsize)
659 expand_tcbtab();
660
661 for (i = 0; i < tcbtabsize; i++) {
662 tcp = tcbtab[i];
663 if ((tcp->flags & TCB_INUSE) == 0) {
664 memset(tcp, 0, sizeof(*tcp));
665 tcp->pid = pid;
666 tcp->flags = TCB_INUSE;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100667#if SUPPORTED_PERSONALITIES > 1
668 tcp->currpers = current_personality;
669#endif
670 nprocs++;
671 if (debug_flag)
672 fprintf(stderr, "new tcb for pid %d, active tcbs:%d\n", tcp->pid, nprocs);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100673 return tcp;
674 }
675 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100676 error_msg_and_die("bug in alloctcb");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100677}
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100678
679static void
680droptcb(struct tcb *tcp)
681{
682 if (tcp->pid == 0)
683 return;
684
685 nprocs--;
686 if (debug_flag)
687 fprintf(stderr, "dropped tcb for pid %d, %d remain\n", tcp->pid, nprocs);
688
689 if (tcp->outf) {
Denys Vlasenko8511f2a2012-03-22 09:35:51 +0100690 if (followfork >= 2) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100691 if (tcp->curcol != 0)
692 fprintf(tcp->outf, " <detached ...>\n");
693 fclose(tcp->outf);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100694 } else {
695 if (printing_tcp == tcp && tcp->curcol != 0)
696 fprintf(tcp->outf, " <detached ...>\n");
697 fflush(tcp->outf);
698 }
699 }
700
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100701 if (current_tcp == tcp)
702 current_tcp = NULL;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100703 if (printing_tcp == tcp)
704 printing_tcp = NULL;
705
706 memset(tcp, 0, sizeof(*tcp));
707}
708
709/* detach traced process; continue with sig
710 * Never call DETACH twice on the same process as both unattached and
711 * attached-unstopped processes give the same ESRCH. For unattached process we
712 * would SIGSTOP it and wait for its SIGSTOP notification forever.
713 */
714static int
715detach(struct tcb *tcp)
716{
717 int error;
718 int status, sigstop_expected;
719
720 if (tcp->flags & TCB_BPTSET)
721 clearbpt(tcp);
722
723 /*
724 * Linux wrongly insists the child be stopped
725 * before detaching. Arghh. We go through hoops
726 * to make a clean break of things.
727 */
728#if defined(SPARC)
Denys Vlasenko978fbc92012-09-13 10:28:43 +0200729# undef PTRACE_DETACH
730# define PTRACE_DETACH PTRACE_SUNDETACH
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100731#endif
732
733 error = 0;
734 sigstop_expected = 0;
735 if (tcp->flags & TCB_ATTACHED) {
736 /*
737 * We attached but possibly didn't see the expected SIGSTOP.
738 * We must catch exactly one as otherwise the detached process
739 * would be left stopped (process state T).
740 */
741 sigstop_expected = (tcp->flags & TCB_IGNORE_ONE_SIGSTOP);
742 error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, 0);
743 if (error == 0) {
744 /* On a clear day, you can see forever. */
745 }
746 else if (errno != ESRCH) {
747 /* Shouldn't happen. */
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100748 perror_msg("detach: ptrace(PTRACE_DETACH, ...)");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100749 }
750 else if (my_tkill(tcp->pid, 0) < 0) {
751 if (errno != ESRCH)
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100752 perror_msg("detach: checking sanity");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100753 }
754 else if (!sigstop_expected && my_tkill(tcp->pid, SIGSTOP) < 0) {
755 if (errno != ESRCH)
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100756 perror_msg("detach: stopping child");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100757 }
758 else
759 sigstop_expected = 1;
760 }
761
762 if (sigstop_expected) {
763 for (;;) {
764#ifdef __WALL
765 if (waitpid(tcp->pid, &status, __WALL) < 0) {
766 if (errno == ECHILD) /* Already gone. */
767 break;
768 if (errno != EINVAL) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100769 perror_msg("detach: waiting");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100770 break;
771 }
772#endif /* __WALL */
773 /* No __WALL here. */
774 if (waitpid(tcp->pid, &status, 0) < 0) {
775 if (errno != ECHILD) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100776 perror_msg("detach: waiting");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100777 break;
778 }
779#ifdef __WCLONE
780 /* If no processes, try clones. */
781 if (waitpid(tcp->pid, &status, __WCLONE) < 0) {
782 if (errno != ECHILD)
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100783 perror_msg("detach: waiting");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100784 break;
785 }
786#endif /* __WCLONE */
787 }
788#ifdef __WALL
789 }
790#endif
791 if (!WIFSTOPPED(status)) {
792 /* Au revoir, mon ami. */
793 break;
794 }
795 if (WSTOPSIG(status) == SIGSTOP) {
796 ptrace_restart(PTRACE_DETACH, tcp, 0);
797 break;
798 }
799 error = ptrace_restart(PTRACE_CONT, tcp,
800 WSTOPSIG(status) == syscall_trap_sig ? 0
801 : WSTOPSIG(status));
802 if (error < 0)
803 break;
804 }
805 }
806
807 if (!qflag && (tcp->flags & TCB_ATTACHED))
808 fprintf(stderr, "Process %u detached\n", tcp->pid);
809
810 droptcb(tcp);
811
812 return error;
813}
814
815static void
Denys Vlasenko558e5122012-03-12 23:32:16 +0100816process_opt_p_list(char *opt)
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100817{
818 while (*opt) {
819 /*
820 * We accept -p PID,PID; -p "`pidof PROG`"; -p "`pgrep PROG`".
821 * pidof uses space as delim, pgrep uses newline. :(
822 */
823 int pid;
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100824 char *delim = opt + strcspn(opt, ", \n\t");
825 char c = *delim;
826
827 *delim = '\0';
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000828 pid = string_to_uint(opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100829 if (pid <= 0) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000830 error_msg_and_die("Invalid process id: '%s'", opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100831 }
832 if (pid == strace_tracer_pid) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000833 error_msg_and_die("I'm sorry, I can't let you do that, Dave.");
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100834 }
835 *delim = c;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100836 alloctcb(pid);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100837 if (c == '\0')
838 break;
839 opt = delim + 1;
840 }
841}
842
Roland McGrath02203312007-06-11 22:06:31 +0000843static void
844startup_attach(void)
845{
846 int tcbi;
847 struct tcb *tcp;
848
849 /*
850 * Block user interruptions as we would leave the traced
851 * process stopped (process state T) if we would terminate in
Denys Vlasenko2e968c02011-09-01 10:23:09 +0200852 * between PTRACE_ATTACH and wait4() on SIGSTOP.
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200853 * We rely on cleanup() from this point on.
Roland McGrath02203312007-06-11 22:06:31 +0000854 */
855 if (interactive)
856 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
857
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000858 if (daemonized_tracer) {
859 pid_t pid = fork();
860 if (pid < 0) {
Denys Vlasenko014ca3a2011-09-02 16:19:30 +0200861 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000862 }
863 if (pid) { /* parent */
864 /*
Denys Vlasenko75422762011-05-27 14:36:01 +0200865 * Wait for grandchild to attach to straced process
866 * (grandparent). Grandchild SIGKILLs us after it attached.
867 * Grandparent's wait() is unblocked by our death,
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000868 * it proceeds to exec the straced program.
869 */
870 pause();
871 _exit(0); /* paranoia */
872 }
Denys Vlasenko75422762011-05-27 14:36:01 +0200873 /* grandchild */
874 /* We will be the tracer process. Remember our new pid: */
875 strace_tracer_pid = getpid();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000876 }
877
Roland McGrath02203312007-06-11 22:06:31 +0000878 for (tcbi = 0; tcbi < tcbtabsize; tcbi++) {
879 tcp = tcbtab[tcbi];
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200880
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100881 if (!(tcp->flags & TCB_INUSE))
882 continue;
883
Denys Vlasenkod116a732011-09-05 14:01:33 +0200884 /* Is this a process we should attach to, but not yet attached? */
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100885 if (tcp->flags & TCB_ATTACHED)
886 continue; /* no, we already attached it */
Denys Vlasenkod116a732011-09-05 14:01:33 +0200887
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000888 if (followfork && !daemonized_tracer) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000889 char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3];
Roland McGrath02203312007-06-11 22:06:31 +0000890 DIR *dir;
891
892 sprintf(procdir, "/proc/%d/task", tcp->pid);
893 dir = opendir(procdir);
894 if (dir != NULL) {
895 unsigned int ntid = 0, nerr = 0;
896 struct dirent *de;
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200897
Roland McGrath02203312007-06-11 22:06:31 +0000898 while ((de = readdir(dir)) != NULL) {
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200899 struct tcb *cur_tcp;
900 int tid;
901
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000902 if (de->d_fileno == 0)
Roland McGrath02203312007-06-11 22:06:31 +0000903 continue;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000904 /* we trust /proc filesystem */
Roland McGrath02203312007-06-11 22:06:31 +0000905 tid = atoi(de->d_name);
906 if (tid <= 0)
907 continue;
908 ++ntid;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100909 if (ptrace_attach_or_seize(tid) < 0) {
Roland McGrath02203312007-06-11 22:06:31 +0000910 ++nerr;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100911 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200912 fprintf(stderr, "attach to pid %d failed\n", tid);
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200913 continue;
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200914 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100915 if (debug_flag)
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200916 fprintf(stderr, "attach to pid %d succeeded\n", tid);
917 cur_tcp = tcp;
918 if (tid != tcp->pid)
919 cur_tcp = alloctcb(tid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100920 cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100921 newoutf(cur_tcp);
Roland McGrath02203312007-06-11 22:06:31 +0000922 }
923 closedir(dir);
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200924 if (interactive) {
925 sigprocmask(SIG_SETMASK, &empty_set, NULL);
926 if (interrupted)
927 goto ret;
928 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
929 }
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000930 ntid -= nerr;
931 if (ntid == 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100932 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +0000933 droptcb(tcp);
934 continue;
935 }
936 if (!qflag) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000937 fprintf(stderr, ntid > 1
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100938? "Process %u attached with %u threads\n"
939: "Process %u attached\n",
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200940 tcp->pid, ntid);
Roland McGrath02203312007-06-11 22:06:31 +0000941 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100942 if (!(tcp->flags & TCB_ATTACHED)) {
Denys Vlasenkof88837a2011-09-05 14:05:46 +0200943 /* -p PID, we failed to attach to PID itself
944 * but did attach to some of its sibling threads.
945 * Drop PID's tcp.
946 */
947 droptcb(tcp);
948 }
Roland McGrath02203312007-06-11 22:06:31 +0000949 continue;
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000950 } /* if (opendir worked) */
951 } /* if (-f) */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100952 if (ptrace_attach_or_seize(tcp->pid) < 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100953 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +0000954 droptcb(tcp);
955 continue;
956 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100957 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100958 newoutf(tcp);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100959 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200960 fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000961
962 if (daemonized_tracer) {
963 /*
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000964 * Make parent go away.
965 * Also makes grandparent's wait() unblock.
966 */
967 kill(getppid(), SIGKILL);
968 }
969
Roland McGrath02203312007-06-11 22:06:31 +0000970 if (!qflag)
971 fprintf(stderr,
Denys Vlasenko9c3861d2012-03-16 15:21:49 +0100972 "Process %u attached\n",
Roland McGrath02203312007-06-11 22:06:31 +0000973 tcp->pid);
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200974 } /* for each tcbtab[] */
Roland McGrath02203312007-06-11 22:06:31 +0000975
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200976 ret:
Roland McGrath02203312007-06-11 22:06:31 +0000977 if (interactive)
978 sigprocmask(SIG_SETMASK, &empty_set, NULL);
979}
980
Denys Vlasenkof909c8d2013-02-19 16:30:31 +0100981/* Stack-o-phobic exec helper, in the hope to work around
982 * NOMMU + "daemonized tracer" difficulty.
983 */
984struct exec_params {
985 int fd_to_close;
986 uid_t run_euid;
987 gid_t run_egid;
988 char **argv;
989 char *pathname;
990};
991static struct exec_params params_for_tracee;
992static void __attribute__ ((noinline, noreturn))
993exec_or_die(void)
994{
995 struct exec_params *params = &params_for_tracee;
996
997 if (params->fd_to_close >= 0)
998 close(params->fd_to_close);
999 if (!daemonized_tracer && !use_seize) {
1000 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) {
1001 perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)");
1002 }
1003 }
1004
1005 if (username != NULL) {
1006 /*
1007 * It is important to set groups before we
1008 * lose privileges on setuid.
1009 */
1010 if (initgroups(username, run_gid) < 0) {
1011 perror_msg_and_die("initgroups");
1012 }
1013 if (setregid(run_gid, params->run_egid) < 0) {
1014 perror_msg_and_die("setregid");
1015 }
1016 if (setreuid(run_uid, params->run_euid) < 0) {
1017 perror_msg_and_die("setreuid");
1018 }
1019 }
1020 else if (geteuid() != 0)
1021 if (setreuid(run_uid, run_uid) < 0) {
1022 perror_msg_and_die("setreuid");
1023 }
1024
1025 if (!daemonized_tracer) {
1026 /*
1027 * Induce a ptrace stop. Tracer (our parent)
1028 * will resume us with PTRACE_SYSCALL and display
1029 * the immediately following execve syscall.
1030 * Can't do this on NOMMU systems, we are after
1031 * vfork: parent is blocked, stopping would deadlock.
1032 */
1033 if (!NOMMU_SYSTEM)
1034 kill(getpid(), SIGSTOP);
1035 } else {
1036 alarm(3);
1037 /* we depend on SIGCHLD set to SIG_DFL by init code */
1038 /* if it happens to be SIG_IGN'ed, wait won't block */
1039 wait(NULL);
1040 alarm(0);
1041 }
1042
1043 execv(params->pathname, params->argv);
1044 perror_msg_and_die("exec");
1045}
1046
Roland McGrath02203312007-06-11 22:06:31 +00001047static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001048startup_child(char **argv)
Roland McGrath02203312007-06-11 22:06:31 +00001049{
1050 struct stat statbuf;
1051 const char *filename;
1052 char pathname[MAXPATHLEN];
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001053 int pid;
Roland McGrath02203312007-06-11 22:06:31 +00001054 struct tcb *tcp;
1055
1056 filename = argv[0];
1057 if (strchr(filename, '/')) {
1058 if (strlen(filename) > sizeof pathname - 1) {
1059 errno = ENAMETOOLONG;
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001060 perror_msg_and_die("exec");
Roland McGrath02203312007-06-11 22:06:31 +00001061 }
1062 strcpy(pathname, filename);
1063 }
1064#ifdef USE_DEBUGGING_EXEC
1065 /*
1066 * Debuggers customarily check the current directory
1067 * first regardless of the path but doing that gives
1068 * security geeks a panic attack.
1069 */
1070 else if (stat(filename, &statbuf) == 0)
1071 strcpy(pathname, filename);
1072#endif /* USE_DEBUGGING_EXEC */
1073 else {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +00001074 const char *path;
Roland McGrath02203312007-06-11 22:06:31 +00001075 int m, n, len;
1076
1077 for (path = getenv("PATH"); path && *path; path += m) {
Denys Vlasenko4f3df072012-01-29 22:38:35 +01001078 const char *colon = strchr(path, ':');
1079 if (colon) {
1080 n = colon - path;
Roland McGrath02203312007-06-11 22:06:31 +00001081 m = n + 1;
1082 }
1083 else
1084 m = n = strlen(path);
1085 if (n == 0) {
1086 if (!getcwd(pathname, MAXPATHLEN))
1087 continue;
1088 len = strlen(pathname);
1089 }
1090 else if (n > sizeof pathname - 1)
1091 continue;
1092 else {
1093 strncpy(pathname, path, n);
1094 len = n;
1095 }
1096 if (len && pathname[len - 1] != '/')
1097 pathname[len++] = '/';
1098 strcpy(pathname + len, filename);
1099 if (stat(pathname, &statbuf) == 0 &&
1100 /* Accept only regular files
1101 with some execute bits set.
1102 XXX not perfect, might still fail */
1103 S_ISREG(statbuf.st_mode) &&
1104 (statbuf.st_mode & 0111))
1105 break;
1106 }
1107 }
1108 if (stat(pathname, &statbuf) < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001109 perror_msg_and_die("Can't stat '%s'", filename);
Roland McGrath02203312007-06-11 22:06:31 +00001110 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001111
1112 params_for_tracee.fd_to_close = (shared_log != stderr) ? fileno(shared_log) : -1;
1113 params_for_tracee.run_euid = (statbuf.st_mode & S_ISUID) ? statbuf.st_uid : run_uid;
1114 params_for_tracee.run_egid = (statbuf.st_mode & S_ISGID) ? statbuf.st_gid : run_gid;
1115 params_for_tracee.argv = argv;
1116 /*
1117 * On NOMMU, can be safely freed only after execve in tracee.
1118 * It's hard to know when that happens, so we just leak it.
1119 */
Denys Vlasenko05f32512013-02-26 12:00:34 +01001120 params_for_tracee.pathname = NOMMU_SYSTEM ? strdup(pathname) : pathname;
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001121
Dmitry V. Levina6809652008-11-10 17:14:58 +00001122 strace_child = pid = fork();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001123 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001124 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001125 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001126 if ((pid != 0 && daemonized_tracer)
1127 || (pid == 0 && !daemonized_tracer)
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001128 ) {
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001129 /* We are to become the tracee. Two cases:
1130 * -D: we are parent
1131 * not -D: we are child
1132 */
1133 exec_or_die();
Roland McGrath02203312007-06-11 22:06:31 +00001134 }
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001135
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001136 /* We are the tracer */
Denys Vlasenko75422762011-05-27 14:36:01 +02001137
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001138 if (!daemonized_tracer) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001139 if (!use_seize) {
1140 /* child did PTRACE_TRACEME, nothing to do in parent */
1141 } else {
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001142 if (!NOMMU_SYSTEM) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001143 /* Wait until child stopped itself */
1144 int status;
1145 while (waitpid(pid, &status, WSTOPPED) < 0) {
1146 if (errno == EINTR)
1147 continue;
1148 perror_msg_and_die("waitpid");
1149 }
1150 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001151 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001152 perror_msg_and_die("Unexpected wait status %x", status);
1153 }
Denys Vlasenko05f32512013-02-26 12:00:34 +01001154 skip_startup_execve = 1;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001155 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001156 /* Else: NOMMU case, we have no way to sync.
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001157 * Just attach to it as soon as possible.
1158 * This means that we may miss a few first syscalls...
1159 */
1160
1161 if (ptrace_attach_or_seize(pid)) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001162 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001163 perror_msg_and_die("Can't attach to %d", pid);
1164 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001165 if (!NOMMU_SYSTEM)
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001166 kill(pid, SIGCONT);
1167 }
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001168 tcp = alloctcb(pid);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001169 if (!NOMMU_SYSTEM)
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001170 tcp->flags |= TCB_ATTACHED | TCB_STRACE_CHILD | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001171 else
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001172 tcp->flags |= TCB_ATTACHED | TCB_STRACE_CHILD | TCB_STARTUP;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001173 newoutf(tcp);
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001174 }
1175 else {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001176 /* With -D, we are *child* here, IOW: different pid. Fetch it: */
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001177 strace_tracer_pid = getpid();
1178 /* The tracee is our parent: */
1179 pid = getppid();
Denys Vlasenkof2025022012-03-09 15:29:45 +01001180 alloctcb(pid);
1181 /* attaching will be done later, by startup_attach */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001182 /* note: we don't do newoutf(tcp) here either! */
Denys Vlasenko05f32512013-02-26 12:00:34 +01001183 skip_startup_execve = 1;
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001184
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001185 /* NOMMU BUG! -D mode is active, we (child) return,
1186 * and we will scribble over parent's stack!
1187 * When parent later unpauses, it segfaults.
1188 *
1189 * We work around it
1190 * (1) by declaring exec_or_die() NORETURN,
1191 * hopefully compiler will just jump to it
1192 * instead of call (won't push anything to stack),
1193 * (2) by trying very hard in exec_or_die()
1194 * to not use any stack,
1195 * (3) having a really big (MAXPATHLEN) stack object
1196 * in this function, which creates a "buffer" between
1197 * child's and parent's stack pointers.
1198 * This may save us if (1) and (2) failed
1199 * and compiler decided to use stack in exec_or_die() anyway
1200 * (happens on i386 because of stack parameter passing).
Denys Vlasenko05f32512013-02-26 12:00:34 +01001201 *
1202 * A cleaner solution is to use makecontext + setcontext
1203 * to create a genuine separate stack and execute on it.
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001204 */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001205 }
Roland McGrath02203312007-06-11 22:06:31 +00001206}
1207
Wang Chaob13c0de2010-11-12 17:25:19 +08001208/*
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001209 * Test whether the kernel support PTRACE_O_TRACECLONE et al options.
Wang Chaob13c0de2010-11-12 17:25:19 +08001210 * First fork a new child, call ptrace with PTRACE_SETOPTIONS on it,
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001211 * and then see which options are supported by the kernel.
Wang Chaob13c0de2010-11-12 17:25:19 +08001212 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001213static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001214test_ptrace_setoptions_followfork(void)
Wang Chaob13c0de2010-11-12 17:25:19 +08001215{
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001216 int pid, expected_grandchild = 0, found_grandchild = 0;
1217 const unsigned int test_options = PTRACE_O_TRACECLONE |
1218 PTRACE_O_TRACEFORK |
1219 PTRACE_O_TRACEVFORK;
Wang Chaob13c0de2010-11-12 17:25:19 +08001220
Denys Vlasenko05f32512013-02-26 12:00:34 +01001221 /* Need fork for test. NOMMU has no forks */
1222 if (NOMMU_SYSTEM)
1223 goto worked; /* be bold, and pretend that test succeeded */
1224
Denys Vlasenko5d645812011-08-20 12:48:18 +02001225 pid = fork();
1226 if (pid < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001227 perror_msg_and_die("fork");
Denys Vlasenko5d645812011-08-20 12:48:18 +02001228 if (pid == 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001229 pid = getpid();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001230 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001231 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1232 __func__);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001233 kill_save_errno(pid, SIGSTOP);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001234 if (fork() < 0)
1235 perror_msg_and_die("fork");
1236 _exit(0);
Wang Chaob13c0de2010-11-12 17:25:19 +08001237 }
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001238
1239 while (1) {
1240 int status, tracee_pid;
1241
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001242 errno = 0;
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001243 tracee_pid = wait(&status);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001244 if (tracee_pid <= 0) {
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001245 if (errno == EINTR)
1246 continue;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001247 if (errno == ECHILD)
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001248 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001249 kill_save_errno(pid, SIGKILL);
1250 perror_msg_and_die("%s: unexpected wait result %d",
1251 __func__, tracee_pid);
1252 }
1253 if (WIFEXITED(status)) {
1254 if (WEXITSTATUS(status)) {
1255 if (tracee_pid != pid)
1256 kill_save_errno(pid, SIGKILL);
1257 error_msg_and_die("%s: unexpected exit status %u",
1258 __func__, WEXITSTATUS(status));
1259 }
1260 continue;
1261 }
1262 if (WIFSIGNALED(status)) {
1263 if (tracee_pid != pid)
1264 kill_save_errno(pid, SIGKILL);
1265 error_msg_and_die("%s: unexpected signal %u",
1266 __func__, WTERMSIG(status));
1267 }
1268 if (!WIFSTOPPED(status)) {
1269 if (tracee_pid != pid)
1270 kill_save_errno(tracee_pid, SIGKILL);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001271 kill_save_errno(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001272 error_msg_and_die("%s: unexpected wait status %x",
1273 __func__, status);
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001274 }
1275 if (tracee_pid != pid) {
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001276 found_grandchild = tracee_pid;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001277 if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0) {
1278 kill_save_errno(tracee_pid, SIGKILL);
1279 kill_save_errno(pid, SIGKILL);
1280 perror_msg_and_die("PTRACE_CONT doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001281 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001282 continue;
1283 }
1284 switch (WSTOPSIG(status)) {
1285 case SIGSTOP:
1286 if (ptrace(PTRACE_SETOPTIONS, pid, 0, test_options) < 0
1287 && errno != EINVAL && errno != EIO)
1288 perror_msg("PTRACE_SETOPTIONS");
1289 break;
1290 case SIGTRAP:
1291 if (status >> 16 == PTRACE_EVENT_FORK) {
1292 long msg = 0;
1293
1294 if (ptrace(PTRACE_GETEVENTMSG, pid,
1295 NULL, (long) &msg) == 0)
1296 expected_grandchild = msg;
1297 }
1298 break;
1299 }
1300 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) {
1301 kill_save_errno(pid, SIGKILL);
1302 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001303 }
1304 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001305 if (expected_grandchild && expected_grandchild == found_grandchild) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001306 worked:
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001307 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001308 if (debug_flag)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001309 fprintf(stderr, "ptrace_setoptions = %#x\n",
1310 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001311 return 0;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001312 }
1313 error_msg("Test for PTRACE_O_TRACECLONE failed, "
1314 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001315 return 1;
Wang Chaob13c0de2010-11-12 17:25:19 +08001316}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001317
1318/*
1319 * Test whether the kernel support PTRACE_O_TRACESYSGOOD.
1320 * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it,
1321 * and then see whether it will stop with (SIGTRAP | 0x80).
1322 *
1323 * Use of this option enables correct handling of user-generated SIGTRAPs,
1324 * and SIGTRAPs generated by special instructions such as int3 on x86:
1325 * _start: .globl _start
1326 * int3
1327 * movl $42, %ebx
1328 * movl $1, %eax
1329 * int $0x80
1330 * (compile with: "gcc -nostartfiles -nostdlib -o int3 int3.S")
1331 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001332static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001333test_ptrace_setoptions_for_all(void)
1334{
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001335 const unsigned int test_options = PTRACE_O_TRACESYSGOOD |
1336 PTRACE_O_TRACEEXEC;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001337 int pid;
1338 int it_worked = 0;
1339
Denys Vlasenko05f32512013-02-26 12:00:34 +01001340 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001341 if (NOMMU_SYSTEM)
Denys Vlasenko05f32512013-02-26 12:00:34 +01001342 goto worked; /* be bold, and pretend that test succeeded */
Mike Frysinger7ff5ed92012-04-05 01:52:25 -04001343
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001344 pid = fork();
1345 if (pid < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001346 perror_msg_and_die("fork");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001347
1348 if (pid == 0) {
1349 pid = getpid();
1350 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001351 /* Note: exits with exitcode 1 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001352 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1353 __func__);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001354 kill(pid, SIGSTOP);
1355 _exit(0); /* parent should see entry into this syscall */
1356 }
1357
1358 while (1) {
1359 int status, tracee_pid;
1360
1361 errno = 0;
1362 tracee_pid = wait(&status);
1363 if (tracee_pid <= 0) {
1364 if (errno == EINTR)
1365 continue;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001366 kill_save_errno(pid, SIGKILL);
1367 perror_msg_and_die("%s: unexpected wait result %d",
1368 __func__, tracee_pid);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001369 }
Denys Vlasenko75422762011-05-27 14:36:01 +02001370 if (WIFEXITED(status)) {
1371 if (WEXITSTATUS(status) == 0)
1372 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001373 error_msg_and_die("%s: unexpected exit status %u",
1374 __func__, WEXITSTATUS(status));
1375 }
1376 if (WIFSIGNALED(status)) {
1377 error_msg_and_die("%s: unexpected signal %u",
1378 __func__, WTERMSIG(status));
Denys Vlasenko75422762011-05-27 14:36:01 +02001379 }
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001380 if (!WIFSTOPPED(status)) {
1381 kill(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001382 error_msg_and_die("%s: unexpected wait status %x",
1383 __func__, status);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001384 }
1385 if (WSTOPSIG(status) == SIGSTOP) {
1386 /*
1387 * We don't check "options aren't accepted" error.
1388 * If it happens, we'll never get (SIGTRAP | 0x80),
1389 * and thus will decide to not use the option.
1390 * IOW: the outcome of the test will be correct.
1391 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001392 if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0
1393 && errno != EINVAL && errno != EIO)
1394 perror_msg("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001395 }
1396 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
1397 it_worked = 1;
1398 }
1399 if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001400 kill_save_errno(pid, SIGKILL);
Denys Vlasenko75422762011-05-27 14:36:01 +02001401 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001402 }
1403 }
1404
1405 if (it_worked) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001406 worked:
Denys Vlasenko75422762011-05-27 14:36:01 +02001407 syscall_trap_sig = (SIGTRAP | 0x80);
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001408 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001409 if (debug_flag)
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001410 fprintf(stderr, "ptrace_setoptions = %#x\n",
1411 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001412 return 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001413 }
1414
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001415 error_msg("Test for PTRACE_O_TRACESYSGOOD failed, "
1416 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001417 return 1;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001418}
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001419
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001420#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001421static void
1422test_ptrace_seize(void)
1423{
1424 int pid;
1425
Denys Vlasenko05f32512013-02-26 12:00:34 +01001426 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001427 if (NOMMU_SYSTEM) {
1428 post_attach_sigstop = 0; /* this sets use_seize to 1 */
1429 return;
1430 }
1431
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001432 pid = fork();
1433 if (pid < 0)
1434 perror_msg_and_die("fork");
1435
1436 if (pid == 0) {
1437 pause();
1438 _exit(0);
1439 }
1440
1441 /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After
1442 * attaching tracee continues to run unless a trap condition occurs.
1443 * PTRACE_SEIZE doesn't affect signal or group stop state.
1444 */
Denys Vlasenko26bc0602012-07-10 16:36:32 +02001445 if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001446 post_attach_sigstop = 0; /* this sets use_seize to 1 */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001447 } else if (debug_flag) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001448 fprintf(stderr, "PTRACE_SEIZE doesn't work\n");
1449 }
1450
1451 kill(pid, SIGKILL);
1452
1453 while (1) {
1454 int status, tracee_pid;
1455
1456 errno = 0;
1457 tracee_pid = waitpid(pid, &status, 0);
1458 if (tracee_pid <= 0) {
1459 if (errno == EINTR)
1460 continue;
1461 perror_msg_and_die("%s: unexpected wait result %d",
1462 __func__, tracee_pid);
1463 }
1464 if (WIFSIGNALED(status)) {
1465 return;
1466 }
1467 error_msg_and_die("%s: unexpected wait status %x",
1468 __func__, status);
1469 }
1470}
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001471#else /* !USE_SEIZE */
1472# define test_ptrace_seize() ((void)0)
1473#endif
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001474
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001475static unsigned
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001476get_os_release(void)
1477{
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001478 unsigned rel;
1479 const char *p;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001480 struct utsname u;
1481 if (uname(&u) < 0)
1482 perror_msg_and_die("uname");
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001483 /* u.release has this form: "3.2.9[-some-garbage]" */
1484 rel = 0;
1485 p = u.release;
1486 for (;;) {
1487 if (!(*p >= '0' && *p <= '9'))
1488 error_msg_and_die("Bad OS release string: '%s'", u.release);
1489 /* Note: this open-codes KERNEL_VERSION(): */
1490 rel = (rel << 8) | atoi(p);
1491 if (rel >= KERNEL_VERSION(1,0,0))
1492 break;
1493 while (*p >= '0' && *p <= '9')
1494 p++;
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001495 if (*p != '.') {
1496 if (rel >= KERNEL_VERSION(0,1,0)) {
1497 /* "X.Y-something" means "X.Y.0" */
1498 rel <<= 8;
1499 break;
1500 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001501 error_msg_and_die("Bad OS release string: '%s'", u.release);
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001502 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001503 p++;
1504 }
1505 return rel;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001506}
1507
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01001508/*
1509 * Initialization part of main() was eating much stack (~0.5k),
1510 * which was unused after init.
1511 * We can reuse it if we move init code into a separate function.
1512 *
1513 * Don't want main() to inline us and defeat the reason
1514 * we have a separate function.
1515 */
1516static void __attribute__ ((noinline))
1517init(int argc, char *argv[])
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001518{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001519 struct tcb *tcp;
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001520 int c, i;
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001521 int optF = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001522 struct sigaction sa;
1523
Dmitry V. Levin08b623e2007-10-08 21:04:41 +00001524 progname = argv[0] ? argv[0] : "strace";
1525
Denys Vlasenkoa5090542012-03-15 17:27:49 +01001526 /* Make sure SIGCHLD has the default action so that waitpid
1527 definitely works without losing track of children. The user
1528 should not have given us a bogus state to inherit, but he might
1529 have. Arguably we should detect SIG_IGN here and pass it on
1530 to children, but probably noone really needs that. */
1531 signal(SIGCHLD, SIG_DFL);
1532
Denys Vlasenko75422762011-05-27 14:36:01 +02001533 strace_tracer_pid = getpid();
1534
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001535 os_release = get_os_release();
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001536
Roland McGrathee9d4352002-12-18 04:16:10 +00001537 /* Allocate the initial tcbtab. */
1538 tcbtabsize = argc; /* Surely enough for all -p args. */
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001539 tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0]));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001540 if (!tcbtab)
1541 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001542 tcp = calloc(tcbtabsize, sizeof(*tcp));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001543 if (!tcp)
1544 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001545 for (c = 0; c < tcbtabsize; c++)
1546 tcbtab[c] = tcp++;
Roland McGrathee9d4352002-12-18 04:16:10 +00001547
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001548 shared_log = stderr;
Roland McGrath138c6a32006-01-12 09:50:49 +00001549 set_sortby(DEFAULT_SORTBY);
1550 set_personality(DEFAULT_PERSONALITY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001551 qualify("trace=all");
1552 qualify("abbrev=all");
1553 qualify("verbose=all");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001554#if DEFAULT_QUAL_FLAGS != (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
1555# error Bug in DEFAULT_QUAL_FLAGS
1556#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001557 qualify("signal=all");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001558 while ((c = getopt(argc, argv,
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001559 "+b:cCdfFhiqrtTvVxyz"
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001560 "D"
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001561 "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001562 switch (c) {
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001563 case 'b':
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001564 if (strcmp(optarg, "execve") != 0)
1565 error_msg_and_die("Syscall '%s' for -b isn't supported",
1566 optarg);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001567 detach_on_execve = 1;
1568 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001569 case 'c':
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001570 if (cflag == CFLAG_BOTH) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001571 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001572 }
1573 cflag = CFLAG_ONLY_STATS;
1574 break;
1575 case 'C':
1576 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001577 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001578 }
1579 cflag = CFLAG_BOTH;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001580 break;
1581 case 'd':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001582 debug_flag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001583 break;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001584 case 'D':
1585 daemonized_tracer = 1;
1586 break;
Roland McGrath41c48222008-07-18 00:25:10 +00001587 case 'F':
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001588 optF = 1;
1589 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001590 case 'f':
1591 followfork++;
1592 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001593 case 'h':
1594 usage(stdout, 0);
1595 break;
1596 case 'i':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001597 iflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001598 break;
1599 case 'q':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001600 qflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001601 break;
1602 case 'r':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001603 rflag = 1;
1604 /* fall through to tflag++ */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001605 case 't':
1606 tflag++;
1607 break;
1608 case 'T':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001609 Tflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001610 break;
1611 case 'x':
1612 xflag++;
1613 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001614 case 'y':
1615 show_fd_path = 1;
1616 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001617 case 'v':
1618 qualify("abbrev=none");
1619 break;
1620 case 'V':
Roland McGrath9c9a2532003-02-20 02:56:29 +00001621 printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001622 exit(0);
1623 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001624 case 'z':
1625 not_failing_only = 1;
1626 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001627 case 'a':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001628 acolumn = string_to_uint(optarg);
Denys Vlasenko102ec492011-08-25 01:27:59 +02001629 if (acolumn < 0)
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001630 error_opt_arg(c, optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001631 break;
1632 case 'e':
1633 qualify(optarg);
1634 break;
1635 case 'o':
1636 outfname = strdup(optarg);
1637 break;
1638 case 'O':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001639 i = string_to_uint(optarg);
1640 if (i < 0)
1641 error_opt_arg(c, optarg);
1642 set_overhead(i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001643 break;
1644 case 'p':
Denys Vlasenkoe8172b72012-03-09 13:01:04 +01001645 process_opt_p_list(optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001646 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001647 case 'P':
Denys Vlasenko7239dbc2013-03-05 15:46:34 +01001648 pathtrace_select(optarg);
Grant Edwards8a082772011-04-07 20:25:40 +00001649 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001650 case 's':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001651 i = string_to_uint(optarg);
1652 if (i < 0)
1653 error_opt_arg(c, optarg);
1654 max_strlen = i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001655 break;
1656 case 'S':
1657 set_sortby(optarg);
1658 break;
1659 case 'u':
1660 username = strdup(optarg);
1661 break;
Roland McGrathde6e5332003-01-24 04:31:23 +00001662 case 'E':
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001663 if (putenv(optarg) < 0)
1664 die_out_of_memory();
Roland McGrathde6e5332003-01-24 04:31:23 +00001665 break;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001666 case 'I':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001667 opt_intr = string_to_uint(optarg);
1668 if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS)
1669 error_opt_arg(c, optarg);
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001670 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001671 default:
1672 usage(stderr, 1);
1673 break;
1674 }
1675 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001676 argv += optind;
1677 /* argc -= optind; - no need, argc is not used below */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001678
Denys Vlasenko102ec492011-08-25 01:27:59 +02001679 acolumn_spaces = malloc(acolumn + 1);
1680 if (!acolumn_spaces)
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001681 die_out_of_memory();
Denys Vlasenko102ec492011-08-25 01:27:59 +02001682 memset(acolumn_spaces, ' ', acolumn);
1683 acolumn_spaces[acolumn] = '\0';
1684
Denys Vlasenko837399a2012-01-24 11:37:03 +01001685 /* Must have PROG [ARGS], or -p PID. Not both. */
Denys Vlasenkofd883382012-03-09 13:03:41 +01001686 if (!argv[0] == !nprocs)
Roland McGrathce0d1542003-11-11 21:24:23 +00001687 usage(stderr, 1);
1688
Denys Vlasenkofd883382012-03-09 13:03:41 +01001689 if (nprocs != 0 && daemonized_tracer) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001690 error_msg_and_die("-D and -p are mutually exclusive");
Wang Chaod322a4b2010-08-05 14:30:11 +08001691 }
1692
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001693 if (!followfork)
1694 followfork = optF;
1695
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001696 if (followfork >= 2 && cflag) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001697 error_msg_and_die("(-c or -C) and -ff are mutually exclusive");
Roland McGrathcb9def62006-04-25 07:48:03 +00001698 }
1699
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001700 /* See if they want to run as another user. */
1701 if (username != NULL) {
1702 struct passwd *pent;
1703
1704 if (getuid() != 0 || geteuid() != 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001705 error_msg_and_die("You must be root to use the -u option");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001706 }
Denys Vlasenko5d645812011-08-20 12:48:18 +02001707 pent = getpwnam(username);
1708 if (pent == NULL) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001709 error_msg_and_die("Cannot find user '%s'", username);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001710 }
1711 run_uid = pent->pw_uid;
1712 run_gid = pent->pw_gid;
1713 }
1714 else {
1715 run_uid = getuid();
1716 run_gid = getgid();
1717 }
1718
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001719 /*
1720 * On any reasonably recent Linux kernel (circa about 2.5.46)
1721 * need_fork_exec_workarounds should stay 0 after these tests:
1722 */
1723 /*need_fork_exec_workarounds = 0; - already is */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001724 if (followfork)
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001725 need_fork_exec_workarounds = test_ptrace_setoptions_followfork();
1726 need_fork_exec_workarounds |= test_ptrace_setoptions_for_all();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001727 test_ptrace_seize();
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001728
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001729 /* Check if they want to redirect the output. */
1730 if (outfname) {
Roland McGrath37b9a662003-11-07 02:26:54 +00001731 /* See if they want to pipe the output. */
1732 if (outfname[0] == '|' || outfname[0] == '!') {
1733 /*
1734 * We can't do the <outfname>.PID funny business
1735 * when using popen, so prohibit it.
1736 */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001737 if (followfork >= 2)
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001738 error_msg_and_die("Piping the output and -ff are mutually exclusive");
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001739 shared_log = strace_popen(outfname + 1);
Roland McGrath37b9a662003-11-07 02:26:54 +00001740 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001741 else if (followfork < 2)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001742 shared_log = strace_fopen(outfname);
Denys Vlasenko328bf252012-03-12 23:34:13 +01001743 } else {
1744 /* -ff without -o FILE is the same as single -f */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001745 if (followfork >= 2)
Denys Vlasenko328bf252012-03-12 23:34:13 +01001746 followfork = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001747 }
1748
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001749 if (!outfname || outfname[0] == '|' || outfname[0] == '!') {
Denys Vlasenkoa677da52012-01-24 11:31:51 +01001750 char *buf = malloc(BUFSIZ);
1751 if (!buf)
1752 die_out_of_memory();
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001753 setvbuf(shared_log, buf, _IOLBF, BUFSIZ);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001754 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001755 if (outfname && argv[0]) {
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001756 if (!opt_intr)
1757 opt_intr = INTR_NEVER;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001758 qflag = 1;
Roland McGrath36931052003-06-03 01:35:20 +00001759 }
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001760 if (!opt_intr)
1761 opt_intr = INTR_WHILE_WAIT;
Wang Chaob13c0de2010-11-12 17:25:19 +08001762
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001763 /* argv[0] -pPID -oFILE Default interactive setting
1764 * yes 0 0 INTR_WHILE_WAIT
1765 * no 1 0 INTR_WHILE_WAIT
1766 * yes 0 1 INTR_NEVER
1767 * no 1 1 INTR_WHILE_WAIT
Roland McGrath54cc1c82007-11-03 23:34:11 +00001768 */
1769
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001770 sigemptyset(&empty_set);
1771 sigemptyset(&blocked_set);
1772
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001773 /* startup_child() must be called before the signal handlers get
1774 * installed below as they are inherited into the spawned process.
1775 * Also we do not need to be protected by them as during interruption
1776 * in the startup_child() mode we kill the spawned process anyway.
1777 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001778 if (argv[0]) {
Denys Vlasenko837399a2012-01-24 11:37:03 +01001779 startup_child(argv);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001780 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001781
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001782 sa.sa_handler = SIG_IGN;
1783 sigemptyset(&sa.sa_mask);
1784 sa.sa_flags = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001785 sigaction(SIGTTOU, &sa, NULL); /* SIG_IGN */
1786 sigaction(SIGTTIN, &sa, NULL); /* SIG_IGN */
1787 if (opt_intr != INTR_ANYWHERE) {
1788 if (opt_intr == INTR_BLOCK_TSTP_TOO)
1789 sigaction(SIGTSTP, &sa, NULL); /* SIG_IGN */
1790 /*
1791 * In interactive mode (if no -o OUTFILE, or -p PID is used),
1792 * fatal signals are blocked while syscall stop is processed,
1793 * and acted on in between, when waiting for new syscall stops.
1794 * In non-interactive mode, signals are ignored.
1795 */
1796 if (opt_intr == INTR_WHILE_WAIT) {
1797 sigaddset(&blocked_set, SIGHUP);
1798 sigaddset(&blocked_set, SIGINT);
1799 sigaddset(&blocked_set, SIGQUIT);
1800 sigaddset(&blocked_set, SIGPIPE);
1801 sigaddset(&blocked_set, SIGTERM);
1802 sa.sa_handler = interrupt;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001803 }
1804 /* SIG_IGN, or set handler for these */
1805 sigaction(SIGHUP, &sa, NULL);
1806 sigaction(SIGINT, &sa, NULL);
1807 sigaction(SIGQUIT, &sa, NULL);
1808 sigaction(SIGPIPE, &sa, NULL);
1809 sigaction(SIGTERM, &sa, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001810 }
Denys Vlasenkofd883382012-03-09 13:03:41 +01001811 if (nprocs != 0 || daemonized_tracer)
Roland McGrath02203312007-06-11 22:06:31 +00001812 startup_attach();
Roland McGrath02203312007-06-11 22:06:31 +00001813
Denys Vlasenkofd883382012-03-09 13:03:41 +01001814 /* Do we want pids printed in our -o OUTFILE?
1815 * -ff: no (every pid has its own file); or
1816 * -f: yes (there can be more pids in the future); or
1817 * -p PID1,PID2: yes (there are already more than one pid)
1818 */
1819 print_pid_pfx = (outfname && followfork < 2 && (followfork == 1 || nprocs > 1));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001820}
1821
Denys Vlasenkoeebb04d2012-01-27 15:24:48 +01001822static struct tcb *
Roland McGrath54e931f2010-09-14 18:59:20 -07001823pid2tcb(int pid)
1824{
1825 int i;
1826
1827 if (pid <= 0)
1828 return NULL;
1829
1830 for (i = 0; i < tcbtabsize; i++) {
1831 struct tcb *tcp = tcbtab[i];
1832 if (tcp->pid == pid && (tcp->flags & TCB_INUSE))
1833 return tcp;
1834 }
1835
1836 return NULL;
1837}
1838
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001839static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001840cleanup(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001841{
1842 int i;
1843 struct tcb *tcp;
Denys Vlasenko35218842012-01-29 21:17:56 +01001844 int fatal_sig;
1845
1846 /* 'interrupted' is a volatile object, fetch it only once */
1847 fatal_sig = interrupted;
1848 if (!fatal_sig)
1849 fatal_sig = SIGTERM;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001850
Roland McGrathee9d4352002-12-18 04:16:10 +00001851 for (i = 0; i < tcbtabsize; i++) {
1852 tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001853 if (!(tcp->flags & TCB_INUSE))
1854 continue;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001855 if (debug_flag)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001856 fprintf(stderr,
1857 "cleanup: looking at pid %u\n", tcp->pid);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001858 if (tcp->flags & TCB_STRACE_CHILD) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001859 kill(tcp->pid, SIGCONT);
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001860 kill(tcp->pid, fatal_sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001861 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001862 detach(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001863 }
1864 if (cflag)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001865 call_summary(shared_log);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001866}
1867
1868static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001869interrupt(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001870{
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001871 interrupted = sig;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001872}
1873
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001874static int
Denys Vlasenko12014262011-05-30 14:00:14 +02001875trace(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001876{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001877 struct rusage ru;
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001878 struct rusage *rup = cflag ? &ru : NULL;
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001879#ifdef __WALL
Roland McGratheb9e2e82009-06-02 16:49:22 -07001880 static int wait4_options = __WALL;
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001881#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001882
Roland McGratheb9e2e82009-06-02 16:49:22 -07001883 while (nprocs != 0) {
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001884 int pid;
1885 int wait_errno;
1886 int status, sig;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001887 int stopped;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001888 struct tcb *tcp;
1889 unsigned event;
1890
Denys Vlasenko222713a2009-03-17 14:29:59 +00001891 if (interrupted)
Roland McGratheb9e2e82009-06-02 16:49:22 -07001892 return 0;
1893 if (interactive)
1894 sigprocmask(SIG_SETMASK, &empty_set, NULL);
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001895#ifdef __WALL
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001896 pid = wait4(-1, &status, wait4_options, rup);
Roland McGrath5bc05552002-12-17 04:50:47 +00001897 if (pid < 0 && (wait4_options & __WALL) && errno == EINVAL) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001898 /* this kernel does not support __WALL */
1899 wait4_options &= ~__WALL;
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001900 pid = wait4(-1, &status, wait4_options, rup);
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001901 }
Roland McGrath5bc05552002-12-17 04:50:47 +00001902 if (pid < 0 && !(wait4_options & __WALL) && errno == ECHILD) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001903 /* most likely a "cloned" process */
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001904 pid = wait4(-1, &status, __WCLONE, rup);
1905 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001906 perror_msg("wait4(__WCLONE) failed");
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001907 }
1908 }
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001909#else
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001910 pid = wait4(-1, &status, 0, rup);
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001911#endif /* __WALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001912 wait_errno = errno;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001913 if (interactive)
1914 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001915
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001916 if (pid < 0) {
Roland McGratheb9e2e82009-06-02 16:49:22 -07001917 switch (wait_errno) {
1918 case EINTR:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001919 continue;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001920 case ECHILD:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001921 /*
1922 * We would like to verify this case
1923 * but sometimes a race in Solbourne's
1924 * version of SunOS sometimes reports
1925 * ECHILD before sending us SIGCHILD.
1926 */
Roland McGratheb9e2e82009-06-02 16:49:22 -07001927 return 0;
1928 default:
1929 errno = wait_errno;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001930 perror_msg("wait");
Roland McGratheb9e2e82009-06-02 16:49:22 -07001931 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001932 }
1933 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001934 if (pid == popen_pid) {
1935 if (WIFEXITED(status) || WIFSIGNALED(status))
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001936 popen_pid = 0;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001937 continue;
1938 }
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001939
1940 event = ((unsigned)status >> 16);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001941 if (debug_flag) {
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001942 char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko67559ad2012-03-13 12:05:27 +01001943 char evbuf[sizeof(",PTRACE_EVENT_?? (%u)") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001944 strcpy(buf, "???");
1945 if (WIFSIGNALED(status))
1946#ifdef WCOREDUMP
1947 sprintf(buf, "WIFSIGNALED,%ssig=%s",
1948 WCOREDUMP(status) ? "core," : "",
1949 signame(WTERMSIG(status)));
1950#else
1951 sprintf(buf, "WIFSIGNALED,sig=%s",
1952 signame(WTERMSIG(status)));
1953#endif
1954 if (WIFEXITED(status))
1955 sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status));
1956 if (WIFSTOPPED(status))
1957 sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status)));
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02001958#ifdef WIFCONTINUED
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001959 if (WIFCONTINUED(status))
1960 strcpy(buf, "WIFCONTINUED");
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02001961#endif
Denys Vlasenko67559ad2012-03-13 12:05:27 +01001962 evbuf[0] = '\0';
1963 if (event != 0) {
1964 static const char *const event_names[] = {
1965 [PTRACE_EVENT_CLONE] = "CLONE",
1966 [PTRACE_EVENT_FORK] = "FORK",
1967 [PTRACE_EVENT_VFORK] = "VFORK",
1968 [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE",
1969 [PTRACE_EVENT_EXEC] = "EXEC",
1970 [PTRACE_EVENT_EXIT] = "EXIT",
1971 };
1972 const char *e;
1973 if (event < ARRAY_SIZE(event_names))
1974 e = event_names[event];
1975 else {
1976 sprintf(buf, "?? (%u)", event);
1977 e = buf;
1978 }
1979 sprintf(evbuf, ",PTRACE_EVENT_%s", e);
1980 }
1981 fprintf(stderr, " [wait(0x%04x) = %u] %s%s\n", status, pid, buf, evbuf);
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001982 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001983
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001984 /* Look up 'pid' in our table. */
Denys Vlasenko5d645812011-08-20 12:48:18 +02001985 tcp = pid2tcb(pid);
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001986
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01001987 if (!tcp) {
Roland McGrath41c48222008-07-18 00:25:10 +00001988 if (followfork) {
Denys Vlasenko418d66a2009-01-17 01:52:54 +00001989 tcp = alloctcb(pid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001990 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001991 newoutf(tcp);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001992 if (!qflag)
Denys Vlasenko833fb132011-08-17 11:30:56 +02001993 fprintf(stderr, "Process %d attached\n",
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001994 pid);
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01001995 } else {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001996 /* This can happen if a clone call used
1997 CLONE_PTRACE itself. */
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001998 if (WIFSTOPPED(status))
Denys Vlasenko97c503f2012-03-09 15:11:21 +01001999 ptrace(PTRACE_CONT, pid, (char *) 0, 0);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02002000 error_msg_and_die("Unknown pid: %u", pid);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002001 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002002 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002003
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002004 clear_regs();
2005 if (WIFSTOPPED(status))
2006 get_regs(pid);
2007
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002008 /* Under Linux, execve changes pid to thread leader's pid,
2009 * and we see this changed pid on EVENT_EXEC and later,
2010 * execve sysexit. Leader "disappears" without exit
2011 * notification. Let user know that, drop leader's tcb,
2012 * and fix up pid in execve thread's tcb.
2013 * Effectively, execve thread's tcb replaces leader's tcb.
2014 *
2015 * BTW, leader is 'stuck undead' (doesn't report WIFEXITED
2016 * on exit syscall) in multithreaded programs exactly
2017 * in order to handle this case.
2018 *
2019 * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0.
2020 * On 2.6 and earlier, it can return garbage.
2021 */
2022 if (event == PTRACE_EVENT_EXEC && os_release >= KERNEL_VERSION(3,0,0)) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002023 FILE *fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002024 struct tcb *execve_thread;
2025 long old_pid = 0;
2026
2027 if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) < 0)
2028 goto dont_switch_tcbs;
2029 if (old_pid <= 0 || old_pid == pid)
2030 goto dont_switch_tcbs;
2031 execve_thread = pid2tcb(old_pid);
2032 /* It should be !NULL, but I feel paranoid */
2033 if (!execve_thread)
2034 goto dont_switch_tcbs;
2035
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002036 if (execve_thread->curcol != 0) {
2037 /*
2038 * One case we are here is -ff:
2039 * try "strace -oLOG -ff test/threaded_execve"
2040 */
2041 fprintf(execve_thread->outf, " <pid changed to %d ...>\n", pid);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002042 /*execve_thread->curcol = 0; - no need, see code below */
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002043 }
2044 /* Swap output FILEs (needed for -ff) */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002045 fp = execve_thread->outf;
2046 execve_thread->outf = tcp->outf;
2047 tcp->outf = fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002048 /* And their column positions */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002049 execve_thread->curcol = tcp->curcol;
2050 tcp->curcol = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002051 /* Drop leader, but close execve'd thread outfile (if -ff) */
2052 droptcb(tcp);
2053 /* Switch to the thread, reusing leader's outfile and pid */
2054 tcp = execve_thread;
2055 tcp->pid = pid;
2056 if (cflag != CFLAG_ONLY_STATS) {
2057 printleader(tcp);
2058 tprintf("+++ superseded by execve in pid %lu +++\n", old_pid);
2059 line_ended();
2060 tcp->flags |= TCB_REPRINT;
2061 }
2062 }
2063 dont_switch_tcbs:
2064
2065 if (event == PTRACE_EVENT_EXEC && detach_on_execve) {
2066 if (!skip_startup_execve)
2067 detach(tcp);
2068 /* This was initial execve for "strace PROG". Skip. */
2069 skip_startup_execve = 0;
2070 }
2071
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002072 /* Set current output file */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002073 current_tcp = tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002074
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002075 if (cflag) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002076 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2077 tcp->stime = ru.ru_stime;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002078 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002079
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002080 if (WIFSIGNALED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002081 if (pid == strace_child)
2082 exit_code = 0x100 | WTERMSIG(status);
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002083 if (cflag != CFLAG_ONLY_STATS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01002084 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)
2085 ) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002086 printleader(tcp);
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002087#ifdef WCOREDUMP
Denys Vlasenko000b6012012-01-28 01:25:03 +01002088 tprintf("+++ killed by %s %s+++\n",
Roland McGrath2efe8792004-01-13 09:59:45 +00002089 signame(WTERMSIG(status)),
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002090 WCOREDUMP(status) ? "(core dumped) " : "");
2091#else
Denys Vlasenko000b6012012-01-28 01:25:03 +01002092 tprintf("+++ killed by %s +++\n",
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002093 signame(WTERMSIG(status)));
Roland McGrath2efe8792004-01-13 09:59:45 +00002094#endif
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002095 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002096 }
2097 droptcb(tcp);
2098 continue;
2099 }
2100 if (WIFEXITED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002101 if (pid == strace_child)
2102 exit_code = WEXITSTATUS(status);
Denys Vlasenkob5e09082012-03-21 14:27:40 +01002103 if (cflag != CFLAG_ONLY_STATS) {
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002104 printleader(tcp);
Denys Vlasenko000b6012012-01-28 01:25:03 +01002105 tprintf("+++ exited with %d +++\n", WEXITSTATUS(status));
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002106 line_ended();
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002107 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002108 droptcb(tcp);
2109 continue;
2110 }
2111 if (!WIFSTOPPED(status)) {
2112 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
2113 droptcb(tcp);
2114 continue;
2115 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002116
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002117 /* Is this the very first time we see this tracee stopped? */
2118 if (tcp->flags & TCB_STARTUP) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002119 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002120 fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n", tcp->pid);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002121 tcp->flags &= ~TCB_STARTUP;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002122 if (tcp->flags & TCB_BPTSET) {
Roland McGrath02203312007-06-11 22:06:31 +00002123 /*
2124 * One example is a breakpoint inherited from
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002125 * parent through fork().
Roland McGrath02203312007-06-11 22:06:31 +00002126 */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002127 if (clearbpt(tcp) < 0) {
2128 /* Pretty fatal */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002129 droptcb(tcp);
2130 cleanup();
2131 return -1;
2132 }
2133 }
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002134 if (ptrace_setoptions) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002135 if (debug_flag)
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002136 fprintf(stderr, "setting opts %x on pid %d\n", ptrace_setoptions, tcp->pid);
2137 if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) {
2138 if (errno != ESRCH) {
2139 /* Should never happen, really */
2140 perror_msg_and_die("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02002141 }
2142 }
2143 }
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002144 }
2145
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002146 sig = WSTOPSIG(status);
2147
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002148 if (event != 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002149 /* Ptrace event */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002150#if USE_SEIZE
Denys Vlasenko26bc0602012-07-10 16:36:32 +02002151 if (event == PTRACE_EVENT_STOP) {
Denys Vlasenko67038162012-01-29 16:46:46 +01002152 /*
2153 * PTRACE_INTERRUPT-stop or group-stop.
2154 * PTRACE_INTERRUPT-stop has sig == SIGTRAP here.
2155 */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002156 if (sig == SIGSTOP
2157 || sig == SIGTSTP
2158 || sig == SIGTTIN
2159 || sig == SIGTTOU
2160 ) {
2161 stopped = 1;
2162 goto show_stopsig;
2163 }
2164 }
2165#endif
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002166 goto restart_tracee_with_sig_0;
2167 }
2168
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002169 /* Is this post-attach SIGSTOP?
2170 * Interestingly, the process may stop
2171 * with STOPSIG equal to some other signal
2172 * than SIGSTOP if we happend to attach
2173 * just before the process takes a signal.
2174 */
2175 if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002176 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002177 fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid);
2178 tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002179 goto restart_tracee_with_sig_0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002180 }
2181
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002182 if (sig != syscall_trap_sig) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002183 siginfo_t si;
2184
2185 /* Nonzero (true) if tracee is stopped by signal
2186 * (as opposed to "tracee received signal").
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002187 * TODO: shouldn't we check for errno == EINVAL too?
2188 * We can get ESRCH instead, you know...
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002189 */
2190 stopped = (ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002191#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002192 show_stopsig:
Denys Vlasenko67038162012-01-29 16:46:46 +01002193#endif
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002194 if (cflag != CFLAG_ONLY_STATS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01002195 && (qual_flags[sig] & QUAL_SIGNAL)
2196 ) {
Dmitry V. Levinc15dfc72011-03-10 14:44:45 +00002197#if defined(PT_CR_IPSR) && defined(PT_CR_IIP)
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002198 long pc = 0;
2199 long psr = 0;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002200
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002201 upeek(tcp, PT_CR_IPSR, &psr);
2202 upeek(tcp, PT_CR_IIP, &pc);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002203
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002204# define PSR_RI 41
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002205 pc += (psr >> PSR_RI) & 0x3;
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002206# define PC_FORMAT_STR " @ %lx"
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002207# define PC_FORMAT_ARG , pc
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002208#else
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002209# define PC_FORMAT_STR ""
2210# define PC_FORMAT_ARG /* nothing */
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002211#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002212 printleader(tcp);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002213 if (!stopped) {
Denys Vlasenko2c4fb902012-03-15 17:24:49 +01002214 tprintf("--- %s ", signame(sig));
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002215 printsiginfo(&si, verbose(tcp));
Denys Vlasenko2c4fb902012-03-15 17:24:49 +01002216 tprintf(PC_FORMAT_STR " ---\n"
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002217 PC_FORMAT_ARG);
2218 } else
Denys Vlasenko2c4fb902012-03-15 17:24:49 +01002219 tprintf("--- stopped by %s" PC_FORMAT_STR " ---\n",
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002220 signame(sig)
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002221 PC_FORMAT_ARG);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002222 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002223 }
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002224
2225 if (!stopped)
2226 /* It's signal-delivery-stop. Inject the signal */
2227 goto restart_tracee;
2228
2229 /* It's group-stop */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002230#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002231 if (use_seize) {
2232 /*
2233 * This ends ptrace-stop, but does *not* end group-stop.
2234 * This makes stopping signals work properly on straced process
2235 * (that is, process really stops. It used to continue to run).
2236 */
2237 if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) {
2238 cleanup();
2239 return -1;
2240 }
2241 continue;
2242 }
2243 /* We don't have PTRACE_LISTEN support... */
2244#endif
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002245 goto restart_tracee;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002246 }
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002247
2248 /* We handled quick cases, we are permitted to interrupt now. */
Roland McGrath02203312007-06-11 22:06:31 +00002249 if (interrupted)
2250 return 0;
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002251
2252 /* This should be syscall entry or exit.
2253 * (Or it still can be that pesky post-execve SIGTRAP!)
2254 * Handle it.
2255 */
Denys Vlasenko23506752012-03-21 10:32:49 +01002256 if (trace_syscall(tcp) < 0) {
2257 /* ptrace() failed in trace_syscall().
Roland McGratheb9e2e82009-06-02 16:49:22 -07002258 * Likely a result of process disappearing mid-flight.
Denys Vlasenko23506752012-03-21 10:32:49 +01002259 * Observed case: exit_group() or SIGKILL terminating
Denys Vlasenkof1e69032012-01-04 15:15:26 +01002260 * all processes in thread group.
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002261 * We assume that ptrace error was caused by process death.
Denys Vlasenkof2025022012-03-09 15:29:45 +01002262 * We used to detach(tcp) here, but since we no longer
2263 * implement "detach before death" policy/hack,
2264 * we can let this process to report its death to us
2265 * normally, via WIFEXITED or WIFSIGNALED wait status.
2266 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002267 continue;
2268 }
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002269 restart_tracee_with_sig_0:
2270 sig = 0;
2271 restart_tracee:
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002272 if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002273 cleanup();
2274 return -1;
2275 }
2276 }
2277 return 0;
2278}
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002279
2280int
2281main(int argc, char *argv[])
2282{
2283 init(argc, argv);
2284
2285 /* Run main tracing loop */
2286 if (trace() < 0)
2287 return 1;
2288
2289 cleanup();
2290 fflush(NULL);
Dmitry V. Levincf534362012-07-12 20:54:46 +00002291 if (shared_log != stderr)
2292 fclose(shared_log);
2293 if (popen_pid) {
2294 while (waitpid(popen_pid, NULL, 0) < 0 && errno == EINTR)
2295 ;
2296 }
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002297 if (exit_code > 0xff) {
2298 /* Avoid potential core file clobbering. */
2299 struct rlimit rlim = {0, 0};
2300 setrlimit(RLIMIT_CORE, &rlim);
2301
2302 /* Child was killed by a signal, mimic that. */
2303 exit_code &= 0xff;
2304 signal(exit_code, SIG_DFL);
2305 raise(exit_code);
2306 /* Paranoia - what if this signal is not fatal?
2307 Exit with 128 + signo then. */
2308 exit_code += 128;
2309 }
2310
2311 return exit_code;
2312}