blob: f6f6e89814234ccb5fe3857b31ce36094722dd45 [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;
Daniel P. Berrange01997cf2013-05-13 11:30:55 +010077unsigned int 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
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000438# define struct_stat struct stat64
439# define stat_file stat64
440# define struct_dirent struct dirent64
441# define read_dir readdir64
442# define struct_rlimit struct rlimit64
443# define set_rlimit setrlimit64
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100444#else
445# define fopen_for_output fopen
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000446# define struct_stat struct stat
447# define stat_file stat
448# define struct_dirent struct dirent
449# define read_dir readdir
450# define struct_rlimit struct rlimit
451# define set_rlimit setrlimit
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100452#endif
453
454static FILE *
455strace_fopen(const char *path)
456{
457 FILE *fp;
458
459 swap_uid();
460 fp = fopen_for_output(path, "w");
461 if (!fp)
462 perror_msg_and_die("Can't fopen '%s'", path);
463 swap_uid();
464 set_cloexec_flag(fileno(fp));
465 return fp;
466}
467
468static int popen_pid = 0;
469
470#ifndef _PATH_BSHELL
471# define _PATH_BSHELL "/bin/sh"
472#endif
473
474/*
475 * We cannot use standard popen(3) here because we have to distinguish
476 * popen child process from other processes we trace, and standard popen(3)
477 * does not export its child's pid.
478 */
479static FILE *
480strace_popen(const char *command)
481{
482 FILE *fp;
483 int fds[2];
484
485 swap_uid();
486 if (pipe(fds) < 0)
487 perror_msg_and_die("pipe");
488
489 set_cloexec_flag(fds[1]); /* never fails */
490
491 popen_pid = vfork();
492 if (popen_pid == -1)
493 perror_msg_and_die("vfork");
494
495 if (popen_pid == 0) {
496 /* child */
497 close(fds[1]);
498 if (fds[0] != 0) {
499 if (dup2(fds[0], 0))
500 perror_msg_and_die("dup2");
501 close(fds[0]);
502 }
503 execl(_PATH_BSHELL, "sh", "-c", command, NULL);
504 perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL);
505 }
506
507 /* parent */
508 close(fds[0]);
509 swap_uid();
510 fp = fdopen(fds[1], "w");
511 if (!fp)
512 die_out_of_memory();
513 return fp;
514}
515
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100516void
517tprintf(const char *fmt, ...)
518{
519 va_list args;
520
521 va_start(args, fmt);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100522 if (current_tcp) {
Denys Vlasenko6e4f3c12012-04-16 18:22:19 +0200523 int n = strace_vfprintf(current_tcp->outf, fmt, args);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100524 if (n < 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100525 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000526 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100527 } else
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100528 current_tcp->curcol += n;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100529 }
530 va_end(args);
531}
532
533void
534tprints(const char *str)
535{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100536 if (current_tcp) {
Denys Vlasenko142aee02012-04-16 18:10:15 +0200537 int n = fputs_unlocked(str, current_tcp->outf);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100538 if (n >= 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100539 current_tcp->curcol += strlen(str);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100540 return;
541 }
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100542 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000543 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100544 }
545}
546
547void
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100548line_ended(void)
549{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100550 if (current_tcp) {
551 current_tcp->curcol = 0;
552 fflush(current_tcp->outf);
553 }
554 if (printing_tcp) {
555 printing_tcp->curcol = 0;
556 printing_tcp = NULL;
557 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100558}
559
560void
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100561printleader(struct tcb *tcp)
562{
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100563 /* If -ff, "previous tcb we printed" is always the same as current,
564 * because we have per-tcb output files.
565 */
566 if (followfork >= 2)
567 printing_tcp = tcp;
568
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100569 if (printing_tcp) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100570 current_tcp = printing_tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100571 if (printing_tcp->curcol != 0 && (followfork < 2 || printing_tcp == tcp)) {
572 /*
573 * case 1: we have a shared log (i.e. not -ff), and last line
574 * wasn't finished (same or different tcb, doesn't matter).
575 * case 2: split log, we are the same tcb, but our last line
576 * didn't finish ("SIGKILL nuked us after syscall entry" etc).
577 */
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100578 tprints(" <unfinished ...>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100579 printing_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100580 }
581 }
582
583 printing_tcp = tcp;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100584 current_tcp = tcp;
585 current_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100586
587 if (print_pid_pfx)
588 tprintf("%-5d ", tcp->pid);
589 else if (nprocs > 1 && !outfname)
590 tprintf("[pid %5u] ", tcp->pid);
591
592 if (tflag) {
593 char str[sizeof("HH:MM:SS")];
594 struct timeval tv, dtv;
595 static struct timeval otv;
596
597 gettimeofday(&tv, NULL);
598 if (rflag) {
599 if (otv.tv_sec == 0)
600 otv = tv;
601 tv_sub(&dtv, &tv, &otv);
602 tprintf("%6ld.%06ld ",
603 (long) dtv.tv_sec, (long) dtv.tv_usec);
604 otv = tv;
605 }
606 else if (tflag > 2) {
607 tprintf("%ld.%06ld ",
608 (long) tv.tv_sec, (long) tv.tv_usec);
609 }
610 else {
611 time_t local = tv.tv_sec;
612 strftime(str, sizeof(str), "%T", localtime(&local));
613 if (tflag > 1)
614 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
615 else
616 tprintf("%s ", str);
617 }
618 }
619 if (iflag)
620 printcall(tcp);
621}
622
623void
624tabto(void)
625{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100626 if (current_tcp->curcol < acolumn)
627 tprints(acolumn_spaces + current_tcp->curcol);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100628}
629
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100630/* Should be only called directly *after successful attach* to a tracee.
631 * Otherwise, "strace -oFILE -ff -p<nonexistant_pid>"
632 * may create bogus empty FILE.<nonexistant_pid>, and then die.
633 */
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200634static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000635newoutf(struct tcb *tcp)
636{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100637 tcp->outf = shared_log; /* if not -ff mode, the same file is for all */
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100638 if (followfork >= 2) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000639 char name[520 + sizeof(int) * 3];
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000640 sprintf(name, "%.512s.%u", outfname, tcp->pid);
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200641 tcp->outf = strace_fopen(name);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000642 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000643}
644
Denys Vlasenko558e5122012-03-12 23:32:16 +0100645static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100646expand_tcbtab(void)
647{
648 /* Allocate some more TCBs and expand the table.
649 We don't want to relocate the TCBs because our
650 callers have pointers and it would be a pain.
651 So tcbtab is a table of pointers. Since we never
652 free the TCBs, we allocate a single chunk of many. */
653 int i = tcbtabsize;
654 struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0]));
655 struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0]));
656 if (!newtab || !newtcbs)
657 die_out_of_memory();
658 tcbtabsize *= 2;
659 tcbtab = newtab;
660 while (i < tcbtabsize)
661 tcbtab[i++] = newtcbs++;
662}
663
664static struct tcb *
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100665alloctcb(int pid)
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100666{
667 int i;
668 struct tcb *tcp;
669
670 if (nprocs == tcbtabsize)
671 expand_tcbtab();
672
673 for (i = 0; i < tcbtabsize; i++) {
674 tcp = tcbtab[i];
675 if ((tcp->flags & TCB_INUSE) == 0) {
676 memset(tcp, 0, sizeof(*tcp));
677 tcp->pid = pid;
678 tcp->flags = TCB_INUSE;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100679#if SUPPORTED_PERSONALITIES > 1
680 tcp->currpers = current_personality;
681#endif
682 nprocs++;
683 if (debug_flag)
684 fprintf(stderr, "new tcb for pid %d, active tcbs:%d\n", tcp->pid, nprocs);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100685 return tcp;
686 }
687 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100688 error_msg_and_die("bug in alloctcb");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100689}
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100690
691static void
692droptcb(struct tcb *tcp)
693{
694 if (tcp->pid == 0)
695 return;
696
697 nprocs--;
698 if (debug_flag)
699 fprintf(stderr, "dropped tcb for pid %d, %d remain\n", tcp->pid, nprocs);
700
701 if (tcp->outf) {
Denys Vlasenko8511f2a2012-03-22 09:35:51 +0100702 if (followfork >= 2) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100703 if (tcp->curcol != 0)
704 fprintf(tcp->outf, " <detached ...>\n");
705 fclose(tcp->outf);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100706 } else {
707 if (printing_tcp == tcp && tcp->curcol != 0)
708 fprintf(tcp->outf, " <detached ...>\n");
709 fflush(tcp->outf);
710 }
711 }
712
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100713 if (current_tcp == tcp)
714 current_tcp = NULL;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100715 if (printing_tcp == tcp)
716 printing_tcp = NULL;
717
718 memset(tcp, 0, sizeof(*tcp));
719}
720
721/* detach traced process; continue with sig
722 * Never call DETACH twice on the same process as both unattached and
723 * attached-unstopped processes give the same ESRCH. For unattached process we
724 * would SIGSTOP it and wait for its SIGSTOP notification forever.
725 */
726static int
727detach(struct tcb *tcp)
728{
729 int error;
730 int status, sigstop_expected;
731
732 if (tcp->flags & TCB_BPTSET)
733 clearbpt(tcp);
734
735 /*
736 * Linux wrongly insists the child be stopped
737 * before detaching. Arghh. We go through hoops
738 * to make a clean break of things.
739 */
740#if defined(SPARC)
Denys Vlasenko978fbc92012-09-13 10:28:43 +0200741# undef PTRACE_DETACH
742# define PTRACE_DETACH PTRACE_SUNDETACH
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100743#endif
744
745 error = 0;
746 sigstop_expected = 0;
747 if (tcp->flags & TCB_ATTACHED) {
748 /*
749 * We attached but possibly didn't see the expected SIGSTOP.
750 * We must catch exactly one as otherwise the detached process
751 * would be left stopped (process state T).
752 */
753 sigstop_expected = (tcp->flags & TCB_IGNORE_ONE_SIGSTOP);
754 error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, 0);
755 if (error == 0) {
756 /* On a clear day, you can see forever. */
757 }
758 else if (errno != ESRCH) {
759 /* Shouldn't happen. */
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100760 perror_msg("detach: ptrace(PTRACE_DETACH, ...)");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100761 }
762 else if (my_tkill(tcp->pid, 0) < 0) {
763 if (errno != ESRCH)
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100764 perror_msg("detach: checking sanity");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100765 }
766 else if (!sigstop_expected && my_tkill(tcp->pid, SIGSTOP) < 0) {
767 if (errno != ESRCH)
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100768 perror_msg("detach: stopping child");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100769 }
770 else
771 sigstop_expected = 1;
772 }
773
774 if (sigstop_expected) {
775 for (;;) {
776#ifdef __WALL
777 if (waitpid(tcp->pid, &status, __WALL) < 0) {
778 if (errno == ECHILD) /* Already gone. */
779 break;
780 if (errno != EINVAL) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100781 perror_msg("detach: waiting");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100782 break;
783 }
784#endif /* __WALL */
785 /* No __WALL here. */
786 if (waitpid(tcp->pid, &status, 0) < 0) {
787 if (errno != ECHILD) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100788 perror_msg("detach: waiting");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100789 break;
790 }
791#ifdef __WCLONE
792 /* If no processes, try clones. */
793 if (waitpid(tcp->pid, &status, __WCLONE) < 0) {
794 if (errno != ECHILD)
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100795 perror_msg("detach: waiting");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100796 break;
797 }
798#endif /* __WCLONE */
799 }
800#ifdef __WALL
801 }
802#endif
803 if (!WIFSTOPPED(status)) {
804 /* Au revoir, mon ami. */
805 break;
806 }
807 if (WSTOPSIG(status) == SIGSTOP) {
808 ptrace_restart(PTRACE_DETACH, tcp, 0);
809 break;
810 }
811 error = ptrace_restart(PTRACE_CONT, tcp,
812 WSTOPSIG(status) == syscall_trap_sig ? 0
813 : WSTOPSIG(status));
814 if (error < 0)
815 break;
816 }
817 }
818
819 if (!qflag && (tcp->flags & TCB_ATTACHED))
820 fprintf(stderr, "Process %u detached\n", tcp->pid);
821
822 droptcb(tcp);
823
824 return error;
825}
826
827static void
Denys Vlasenko558e5122012-03-12 23:32:16 +0100828process_opt_p_list(char *opt)
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100829{
830 while (*opt) {
831 /*
832 * We accept -p PID,PID; -p "`pidof PROG`"; -p "`pgrep PROG`".
833 * pidof uses space as delim, pgrep uses newline. :(
834 */
835 int pid;
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100836 char *delim = opt + strcspn(opt, ", \n\t");
837 char c = *delim;
838
839 *delim = '\0';
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000840 pid = string_to_uint(opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100841 if (pid <= 0) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000842 error_msg_and_die("Invalid process id: '%s'", opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100843 }
844 if (pid == strace_tracer_pid) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000845 error_msg_and_die("I'm sorry, I can't let you do that, Dave.");
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100846 }
847 *delim = c;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100848 alloctcb(pid);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100849 if (c == '\0')
850 break;
851 opt = delim + 1;
852 }
853}
854
Roland McGrath02203312007-06-11 22:06:31 +0000855static void
856startup_attach(void)
857{
858 int tcbi;
859 struct tcb *tcp;
860
861 /*
862 * Block user interruptions as we would leave the traced
863 * process stopped (process state T) if we would terminate in
Denys Vlasenko2e968c02011-09-01 10:23:09 +0200864 * between PTRACE_ATTACH and wait4() on SIGSTOP.
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200865 * We rely on cleanup() from this point on.
Roland McGrath02203312007-06-11 22:06:31 +0000866 */
867 if (interactive)
868 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
869
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000870 if (daemonized_tracer) {
871 pid_t pid = fork();
872 if (pid < 0) {
Denys Vlasenko014ca3a2011-09-02 16:19:30 +0200873 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000874 }
875 if (pid) { /* parent */
876 /*
Denys Vlasenko75422762011-05-27 14:36:01 +0200877 * Wait for grandchild to attach to straced process
878 * (grandparent). Grandchild SIGKILLs us after it attached.
879 * Grandparent's wait() is unblocked by our death,
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000880 * it proceeds to exec the straced program.
881 */
882 pause();
883 _exit(0); /* paranoia */
884 }
Denys Vlasenko75422762011-05-27 14:36:01 +0200885 /* grandchild */
886 /* We will be the tracer process. Remember our new pid: */
887 strace_tracer_pid = getpid();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000888 }
889
Roland McGrath02203312007-06-11 22:06:31 +0000890 for (tcbi = 0; tcbi < tcbtabsize; tcbi++) {
891 tcp = tcbtab[tcbi];
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200892
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100893 if (!(tcp->flags & TCB_INUSE))
894 continue;
895
Denys Vlasenkod116a732011-09-05 14:01:33 +0200896 /* Is this a process we should attach to, but not yet attached? */
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100897 if (tcp->flags & TCB_ATTACHED)
898 continue; /* no, we already attached it */
Denys Vlasenkod116a732011-09-05 14:01:33 +0200899
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000900 if (followfork && !daemonized_tracer) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000901 char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3];
Roland McGrath02203312007-06-11 22:06:31 +0000902 DIR *dir;
903
904 sprintf(procdir, "/proc/%d/task", tcp->pid);
905 dir = opendir(procdir);
906 if (dir != NULL) {
907 unsigned int ntid = 0, nerr = 0;
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000908 struct_dirent *de;
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200909
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000910 while ((de = read_dir(dir)) != NULL) {
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200911 struct tcb *cur_tcp;
912 int tid;
913
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000914 if (de->d_fileno == 0)
Roland McGrath02203312007-06-11 22:06:31 +0000915 continue;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000916 /* we trust /proc filesystem */
Roland McGrath02203312007-06-11 22:06:31 +0000917 tid = atoi(de->d_name);
918 if (tid <= 0)
919 continue;
920 ++ntid;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100921 if (ptrace_attach_or_seize(tid) < 0) {
Roland McGrath02203312007-06-11 22:06:31 +0000922 ++nerr;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100923 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200924 fprintf(stderr, "attach to pid %d failed\n", tid);
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200925 continue;
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200926 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100927 if (debug_flag)
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200928 fprintf(stderr, "attach to pid %d succeeded\n", tid);
929 cur_tcp = tcp;
930 if (tid != tcp->pid)
931 cur_tcp = alloctcb(tid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100932 cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100933 newoutf(cur_tcp);
Roland McGrath02203312007-06-11 22:06:31 +0000934 }
935 closedir(dir);
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200936 if (interactive) {
937 sigprocmask(SIG_SETMASK, &empty_set, NULL);
938 if (interrupted)
939 goto ret;
940 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
941 }
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000942 ntid -= nerr;
943 if (ntid == 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100944 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +0000945 droptcb(tcp);
946 continue;
947 }
948 if (!qflag) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000949 fprintf(stderr, ntid > 1
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100950? "Process %u attached with %u threads\n"
951: "Process %u attached\n",
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200952 tcp->pid, ntid);
Roland McGrath02203312007-06-11 22:06:31 +0000953 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100954 if (!(tcp->flags & TCB_ATTACHED)) {
Denys Vlasenkof88837a2011-09-05 14:05:46 +0200955 /* -p PID, we failed to attach to PID itself
956 * but did attach to some of its sibling threads.
957 * Drop PID's tcp.
958 */
959 droptcb(tcp);
960 }
Roland McGrath02203312007-06-11 22:06:31 +0000961 continue;
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000962 } /* if (opendir worked) */
963 } /* if (-f) */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100964 if (ptrace_attach_or_seize(tcp->pid) < 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100965 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +0000966 droptcb(tcp);
967 continue;
968 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100969 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100970 newoutf(tcp);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100971 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200972 fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000973
974 if (daemonized_tracer) {
975 /*
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000976 * Make parent go away.
977 * Also makes grandparent's wait() unblock.
978 */
979 kill(getppid(), SIGKILL);
980 }
981
Roland McGrath02203312007-06-11 22:06:31 +0000982 if (!qflag)
983 fprintf(stderr,
Denys Vlasenko9c3861d2012-03-16 15:21:49 +0100984 "Process %u attached\n",
Roland McGrath02203312007-06-11 22:06:31 +0000985 tcp->pid);
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200986 } /* for each tcbtab[] */
Roland McGrath02203312007-06-11 22:06:31 +0000987
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200988 ret:
Roland McGrath02203312007-06-11 22:06:31 +0000989 if (interactive)
990 sigprocmask(SIG_SETMASK, &empty_set, NULL);
991}
992
Denys Vlasenkof909c8d2013-02-19 16:30:31 +0100993/* Stack-o-phobic exec helper, in the hope to work around
994 * NOMMU + "daemonized tracer" difficulty.
995 */
996struct exec_params {
997 int fd_to_close;
998 uid_t run_euid;
999 gid_t run_egid;
1000 char **argv;
1001 char *pathname;
1002};
1003static struct exec_params params_for_tracee;
1004static void __attribute__ ((noinline, noreturn))
1005exec_or_die(void)
1006{
1007 struct exec_params *params = &params_for_tracee;
1008
1009 if (params->fd_to_close >= 0)
1010 close(params->fd_to_close);
1011 if (!daemonized_tracer && !use_seize) {
1012 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) {
1013 perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)");
1014 }
1015 }
1016
1017 if (username != NULL) {
1018 /*
1019 * It is important to set groups before we
1020 * lose privileges on setuid.
1021 */
1022 if (initgroups(username, run_gid) < 0) {
1023 perror_msg_and_die("initgroups");
1024 }
1025 if (setregid(run_gid, params->run_egid) < 0) {
1026 perror_msg_and_die("setregid");
1027 }
1028 if (setreuid(run_uid, params->run_euid) < 0) {
1029 perror_msg_and_die("setreuid");
1030 }
1031 }
1032 else if (geteuid() != 0)
1033 if (setreuid(run_uid, run_uid) < 0) {
1034 perror_msg_and_die("setreuid");
1035 }
1036
1037 if (!daemonized_tracer) {
1038 /*
1039 * Induce a ptrace stop. Tracer (our parent)
1040 * will resume us with PTRACE_SYSCALL and display
1041 * the immediately following execve syscall.
1042 * Can't do this on NOMMU systems, we are after
1043 * vfork: parent is blocked, stopping would deadlock.
1044 */
1045 if (!NOMMU_SYSTEM)
1046 kill(getpid(), SIGSTOP);
1047 } else {
1048 alarm(3);
1049 /* we depend on SIGCHLD set to SIG_DFL by init code */
1050 /* if it happens to be SIG_IGN'ed, wait won't block */
1051 wait(NULL);
1052 alarm(0);
1053 }
1054
1055 execv(params->pathname, params->argv);
1056 perror_msg_and_die("exec");
1057}
1058
Roland McGrath02203312007-06-11 22:06:31 +00001059static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001060startup_child(char **argv)
Roland McGrath02203312007-06-11 22:06:31 +00001061{
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001062 struct_stat statbuf;
Roland McGrath02203312007-06-11 22:06:31 +00001063 const char *filename;
1064 char pathname[MAXPATHLEN];
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001065 int pid;
Roland McGrath02203312007-06-11 22:06:31 +00001066 struct tcb *tcp;
1067
1068 filename = argv[0];
1069 if (strchr(filename, '/')) {
1070 if (strlen(filename) > sizeof pathname - 1) {
1071 errno = ENAMETOOLONG;
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001072 perror_msg_and_die("exec");
Roland McGrath02203312007-06-11 22:06:31 +00001073 }
1074 strcpy(pathname, filename);
1075 }
1076#ifdef USE_DEBUGGING_EXEC
1077 /*
1078 * Debuggers customarily check the current directory
1079 * first regardless of the path but doing that gives
1080 * security geeks a panic attack.
1081 */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001082 else if (stat_file(filename, &statbuf) == 0)
Roland McGrath02203312007-06-11 22:06:31 +00001083 strcpy(pathname, filename);
1084#endif /* USE_DEBUGGING_EXEC */
1085 else {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +00001086 const char *path;
Roland McGrath02203312007-06-11 22:06:31 +00001087 int m, n, len;
1088
1089 for (path = getenv("PATH"); path && *path; path += m) {
Denys Vlasenko4f3df072012-01-29 22:38:35 +01001090 const char *colon = strchr(path, ':');
1091 if (colon) {
1092 n = colon - path;
Roland McGrath02203312007-06-11 22:06:31 +00001093 m = n + 1;
1094 }
1095 else
1096 m = n = strlen(path);
1097 if (n == 0) {
1098 if (!getcwd(pathname, MAXPATHLEN))
1099 continue;
1100 len = strlen(pathname);
1101 }
1102 else if (n > sizeof pathname - 1)
1103 continue;
1104 else {
1105 strncpy(pathname, path, n);
1106 len = n;
1107 }
1108 if (len && pathname[len - 1] != '/')
1109 pathname[len++] = '/';
1110 strcpy(pathname + len, filename);
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001111 if (stat_file(pathname, &statbuf) == 0 &&
Roland McGrath02203312007-06-11 22:06:31 +00001112 /* Accept only regular files
1113 with some execute bits set.
1114 XXX not perfect, might still fail */
1115 S_ISREG(statbuf.st_mode) &&
1116 (statbuf.st_mode & 0111))
1117 break;
1118 }
1119 }
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001120 if (stat_file(pathname, &statbuf) < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001121 perror_msg_and_die("Can't stat '%s'", filename);
Roland McGrath02203312007-06-11 22:06:31 +00001122 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001123
1124 params_for_tracee.fd_to_close = (shared_log != stderr) ? fileno(shared_log) : -1;
1125 params_for_tracee.run_euid = (statbuf.st_mode & S_ISUID) ? statbuf.st_uid : run_uid;
1126 params_for_tracee.run_egid = (statbuf.st_mode & S_ISGID) ? statbuf.st_gid : run_gid;
1127 params_for_tracee.argv = argv;
1128 /*
1129 * On NOMMU, can be safely freed only after execve in tracee.
1130 * It's hard to know when that happens, so we just leak it.
1131 */
Denys Vlasenko05f32512013-02-26 12:00:34 +01001132 params_for_tracee.pathname = NOMMU_SYSTEM ? strdup(pathname) : pathname;
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001133
Dmitry V. Levina6809652008-11-10 17:14:58 +00001134 strace_child = pid = fork();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001135 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001136 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001137 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001138 if ((pid != 0 && daemonized_tracer)
1139 || (pid == 0 && !daemonized_tracer)
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001140 ) {
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001141 /* We are to become the tracee. Two cases:
1142 * -D: we are parent
1143 * not -D: we are child
1144 */
1145 exec_or_die();
Roland McGrath02203312007-06-11 22:06:31 +00001146 }
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001147
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001148 /* We are the tracer */
Denys Vlasenko75422762011-05-27 14:36:01 +02001149
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001150 if (!daemonized_tracer) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001151 if (!use_seize) {
1152 /* child did PTRACE_TRACEME, nothing to do in parent */
1153 } else {
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001154 if (!NOMMU_SYSTEM) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001155 /* Wait until child stopped itself */
1156 int status;
1157 while (waitpid(pid, &status, WSTOPPED) < 0) {
1158 if (errno == EINTR)
1159 continue;
1160 perror_msg_and_die("waitpid");
1161 }
1162 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001163 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001164 perror_msg_and_die("Unexpected wait status %x", status);
1165 }
Denys Vlasenko05f32512013-02-26 12:00:34 +01001166 skip_startup_execve = 1;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001167 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001168 /* Else: NOMMU case, we have no way to sync.
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001169 * Just attach to it as soon as possible.
1170 * This means that we may miss a few first syscalls...
1171 */
1172
1173 if (ptrace_attach_or_seize(pid)) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001174 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001175 perror_msg_and_die("Can't attach to %d", pid);
1176 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001177 if (!NOMMU_SYSTEM)
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001178 kill(pid, SIGCONT);
1179 }
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001180 tcp = alloctcb(pid);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001181 if (!NOMMU_SYSTEM)
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001182 tcp->flags |= TCB_ATTACHED | TCB_STRACE_CHILD | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001183 else
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001184 tcp->flags |= TCB_ATTACHED | TCB_STRACE_CHILD | TCB_STARTUP;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001185 newoutf(tcp);
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001186 }
1187 else {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001188 /* With -D, we are *child* here, IOW: different pid. Fetch it: */
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001189 strace_tracer_pid = getpid();
1190 /* The tracee is our parent: */
1191 pid = getppid();
Denys Vlasenkof2025022012-03-09 15:29:45 +01001192 alloctcb(pid);
1193 /* attaching will be done later, by startup_attach */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001194 /* note: we don't do newoutf(tcp) here either! */
Denys Vlasenko05f32512013-02-26 12:00:34 +01001195 skip_startup_execve = 1;
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001196
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001197 /* NOMMU BUG! -D mode is active, we (child) return,
1198 * and we will scribble over parent's stack!
1199 * When parent later unpauses, it segfaults.
1200 *
1201 * We work around it
1202 * (1) by declaring exec_or_die() NORETURN,
1203 * hopefully compiler will just jump to it
1204 * instead of call (won't push anything to stack),
1205 * (2) by trying very hard in exec_or_die()
1206 * to not use any stack,
1207 * (3) having a really big (MAXPATHLEN) stack object
1208 * in this function, which creates a "buffer" between
1209 * child's and parent's stack pointers.
1210 * This may save us if (1) and (2) failed
1211 * and compiler decided to use stack in exec_or_die() anyway
1212 * (happens on i386 because of stack parameter passing).
Denys Vlasenko05f32512013-02-26 12:00:34 +01001213 *
1214 * A cleaner solution is to use makecontext + setcontext
1215 * to create a genuine separate stack and execute on it.
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001216 */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001217 }
Roland McGrath02203312007-06-11 22:06:31 +00001218}
1219
Wang Chaob13c0de2010-11-12 17:25:19 +08001220/*
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001221 * Test whether the kernel support PTRACE_O_TRACECLONE et al options.
Wang Chaob13c0de2010-11-12 17:25:19 +08001222 * First fork a new child, call ptrace with PTRACE_SETOPTIONS on it,
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001223 * and then see which options are supported by the kernel.
Wang Chaob13c0de2010-11-12 17:25:19 +08001224 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001225static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001226test_ptrace_setoptions_followfork(void)
Wang Chaob13c0de2010-11-12 17:25:19 +08001227{
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001228 int pid, expected_grandchild = 0, found_grandchild = 0;
1229 const unsigned int test_options = PTRACE_O_TRACECLONE |
1230 PTRACE_O_TRACEFORK |
1231 PTRACE_O_TRACEVFORK;
Wang Chaob13c0de2010-11-12 17:25:19 +08001232
Denys Vlasenko05f32512013-02-26 12:00:34 +01001233 /* Need fork for test. NOMMU has no forks */
1234 if (NOMMU_SYSTEM)
1235 goto worked; /* be bold, and pretend that test succeeded */
1236
Denys Vlasenko5d645812011-08-20 12:48:18 +02001237 pid = fork();
1238 if (pid < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001239 perror_msg_and_die("fork");
Denys Vlasenko5d645812011-08-20 12:48:18 +02001240 if (pid == 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001241 pid = getpid();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001242 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001243 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1244 __func__);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001245 kill_save_errno(pid, SIGSTOP);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001246 if (fork() < 0)
1247 perror_msg_and_die("fork");
1248 _exit(0);
Wang Chaob13c0de2010-11-12 17:25:19 +08001249 }
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001250
1251 while (1) {
1252 int status, tracee_pid;
1253
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001254 errno = 0;
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001255 tracee_pid = wait(&status);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001256 if (tracee_pid <= 0) {
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001257 if (errno == EINTR)
1258 continue;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001259 if (errno == ECHILD)
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001260 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001261 kill_save_errno(pid, SIGKILL);
1262 perror_msg_and_die("%s: unexpected wait result %d",
1263 __func__, tracee_pid);
1264 }
1265 if (WIFEXITED(status)) {
1266 if (WEXITSTATUS(status)) {
1267 if (tracee_pid != pid)
1268 kill_save_errno(pid, SIGKILL);
1269 error_msg_and_die("%s: unexpected exit status %u",
1270 __func__, WEXITSTATUS(status));
1271 }
1272 continue;
1273 }
1274 if (WIFSIGNALED(status)) {
1275 if (tracee_pid != pid)
1276 kill_save_errno(pid, SIGKILL);
1277 error_msg_and_die("%s: unexpected signal %u",
1278 __func__, WTERMSIG(status));
1279 }
1280 if (!WIFSTOPPED(status)) {
1281 if (tracee_pid != pid)
1282 kill_save_errno(tracee_pid, SIGKILL);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001283 kill_save_errno(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001284 error_msg_and_die("%s: unexpected wait status %x",
1285 __func__, status);
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001286 }
1287 if (tracee_pid != pid) {
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001288 found_grandchild = tracee_pid;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001289 if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0) {
1290 kill_save_errno(tracee_pid, SIGKILL);
1291 kill_save_errno(pid, SIGKILL);
1292 perror_msg_and_die("PTRACE_CONT doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001293 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001294 continue;
1295 }
1296 switch (WSTOPSIG(status)) {
1297 case SIGSTOP:
1298 if (ptrace(PTRACE_SETOPTIONS, pid, 0, test_options) < 0
1299 && errno != EINVAL && errno != EIO)
1300 perror_msg("PTRACE_SETOPTIONS");
1301 break;
1302 case SIGTRAP:
1303 if (status >> 16 == PTRACE_EVENT_FORK) {
1304 long msg = 0;
1305
1306 if (ptrace(PTRACE_GETEVENTMSG, pid,
1307 NULL, (long) &msg) == 0)
1308 expected_grandchild = msg;
1309 }
1310 break;
1311 }
1312 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) {
1313 kill_save_errno(pid, SIGKILL);
1314 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001315 }
1316 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001317 if (expected_grandchild && expected_grandchild == found_grandchild) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001318 worked:
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001319 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001320 if (debug_flag)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001321 fprintf(stderr, "ptrace_setoptions = %#x\n",
1322 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001323 return 0;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001324 }
1325 error_msg("Test for PTRACE_O_TRACECLONE failed, "
1326 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001327 return 1;
Wang Chaob13c0de2010-11-12 17:25:19 +08001328}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001329
1330/*
1331 * Test whether the kernel support PTRACE_O_TRACESYSGOOD.
1332 * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it,
1333 * and then see whether it will stop with (SIGTRAP | 0x80).
1334 *
1335 * Use of this option enables correct handling of user-generated SIGTRAPs,
1336 * and SIGTRAPs generated by special instructions such as int3 on x86:
1337 * _start: .globl _start
1338 * int3
1339 * movl $42, %ebx
1340 * movl $1, %eax
1341 * int $0x80
1342 * (compile with: "gcc -nostartfiles -nostdlib -o int3 int3.S")
1343 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001344static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001345test_ptrace_setoptions_for_all(void)
1346{
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001347 const unsigned int test_options = PTRACE_O_TRACESYSGOOD |
1348 PTRACE_O_TRACEEXEC;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001349 int pid;
1350 int it_worked = 0;
1351
Denys Vlasenko05f32512013-02-26 12:00:34 +01001352 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001353 if (NOMMU_SYSTEM)
Denys Vlasenko05f32512013-02-26 12:00:34 +01001354 goto worked; /* be bold, and pretend that test succeeded */
Mike Frysinger7ff5ed92012-04-05 01:52:25 -04001355
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001356 pid = fork();
1357 if (pid < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001358 perror_msg_and_die("fork");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001359
1360 if (pid == 0) {
1361 pid = getpid();
1362 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001363 /* Note: exits with exitcode 1 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001364 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1365 __func__);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001366 kill(pid, SIGSTOP);
1367 _exit(0); /* parent should see entry into this syscall */
1368 }
1369
1370 while (1) {
1371 int status, tracee_pid;
1372
1373 errno = 0;
1374 tracee_pid = wait(&status);
1375 if (tracee_pid <= 0) {
1376 if (errno == EINTR)
1377 continue;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001378 kill_save_errno(pid, SIGKILL);
1379 perror_msg_and_die("%s: unexpected wait result %d",
1380 __func__, tracee_pid);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001381 }
Denys Vlasenko75422762011-05-27 14:36:01 +02001382 if (WIFEXITED(status)) {
1383 if (WEXITSTATUS(status) == 0)
1384 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001385 error_msg_and_die("%s: unexpected exit status %u",
1386 __func__, WEXITSTATUS(status));
1387 }
1388 if (WIFSIGNALED(status)) {
1389 error_msg_and_die("%s: unexpected signal %u",
1390 __func__, WTERMSIG(status));
Denys Vlasenko75422762011-05-27 14:36:01 +02001391 }
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001392 if (!WIFSTOPPED(status)) {
1393 kill(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001394 error_msg_and_die("%s: unexpected wait status %x",
1395 __func__, status);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001396 }
1397 if (WSTOPSIG(status) == SIGSTOP) {
1398 /*
1399 * We don't check "options aren't accepted" error.
1400 * If it happens, we'll never get (SIGTRAP | 0x80),
1401 * and thus will decide to not use the option.
1402 * IOW: the outcome of the test will be correct.
1403 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001404 if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0
1405 && errno != EINVAL && errno != EIO)
1406 perror_msg("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001407 }
1408 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
1409 it_worked = 1;
1410 }
1411 if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001412 kill_save_errno(pid, SIGKILL);
Denys Vlasenko75422762011-05-27 14:36:01 +02001413 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001414 }
1415 }
1416
1417 if (it_worked) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001418 worked:
Denys Vlasenko75422762011-05-27 14:36:01 +02001419 syscall_trap_sig = (SIGTRAP | 0x80);
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001420 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001421 if (debug_flag)
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001422 fprintf(stderr, "ptrace_setoptions = %#x\n",
1423 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001424 return 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001425 }
1426
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001427 error_msg("Test for PTRACE_O_TRACESYSGOOD failed, "
1428 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001429 return 1;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001430}
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001431
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001432#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001433static void
1434test_ptrace_seize(void)
1435{
1436 int pid;
1437
Denys Vlasenko05f32512013-02-26 12:00:34 +01001438 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001439 if (NOMMU_SYSTEM) {
1440 post_attach_sigstop = 0; /* this sets use_seize to 1 */
1441 return;
1442 }
1443
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001444 pid = fork();
1445 if (pid < 0)
1446 perror_msg_and_die("fork");
1447
1448 if (pid == 0) {
1449 pause();
1450 _exit(0);
1451 }
1452
1453 /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After
1454 * attaching tracee continues to run unless a trap condition occurs.
1455 * PTRACE_SEIZE doesn't affect signal or group stop state.
1456 */
Denys Vlasenko26bc0602012-07-10 16:36:32 +02001457 if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001458 post_attach_sigstop = 0; /* this sets use_seize to 1 */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001459 } else if (debug_flag) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001460 fprintf(stderr, "PTRACE_SEIZE doesn't work\n");
1461 }
1462
1463 kill(pid, SIGKILL);
1464
1465 while (1) {
1466 int status, tracee_pid;
1467
1468 errno = 0;
1469 tracee_pid = waitpid(pid, &status, 0);
1470 if (tracee_pid <= 0) {
1471 if (errno == EINTR)
1472 continue;
1473 perror_msg_and_die("%s: unexpected wait result %d",
1474 __func__, tracee_pid);
1475 }
1476 if (WIFSIGNALED(status)) {
1477 return;
1478 }
1479 error_msg_and_die("%s: unexpected wait status %x",
1480 __func__, status);
1481 }
1482}
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001483#else /* !USE_SEIZE */
1484# define test_ptrace_seize() ((void)0)
1485#endif
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001486
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001487static unsigned
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001488get_os_release(void)
1489{
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001490 unsigned rel;
1491 const char *p;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001492 struct utsname u;
1493 if (uname(&u) < 0)
1494 perror_msg_and_die("uname");
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001495 /* u.release has this form: "3.2.9[-some-garbage]" */
1496 rel = 0;
1497 p = u.release;
1498 for (;;) {
1499 if (!(*p >= '0' && *p <= '9'))
1500 error_msg_and_die("Bad OS release string: '%s'", u.release);
1501 /* Note: this open-codes KERNEL_VERSION(): */
1502 rel = (rel << 8) | atoi(p);
1503 if (rel >= KERNEL_VERSION(1,0,0))
1504 break;
1505 while (*p >= '0' && *p <= '9')
1506 p++;
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001507 if (*p != '.') {
1508 if (rel >= KERNEL_VERSION(0,1,0)) {
1509 /* "X.Y-something" means "X.Y.0" */
1510 rel <<= 8;
1511 break;
1512 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001513 error_msg_and_die("Bad OS release string: '%s'", u.release);
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001514 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001515 p++;
1516 }
1517 return rel;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001518}
1519
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01001520/*
1521 * Initialization part of main() was eating much stack (~0.5k),
1522 * which was unused after init.
1523 * We can reuse it if we move init code into a separate function.
1524 *
1525 * Don't want main() to inline us and defeat the reason
1526 * we have a separate function.
1527 */
1528static void __attribute__ ((noinline))
1529init(int argc, char *argv[])
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001530{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001531 struct tcb *tcp;
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001532 int c, i;
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001533 int optF = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001534 struct sigaction sa;
1535
Dmitry V. Levin08b623e2007-10-08 21:04:41 +00001536 progname = argv[0] ? argv[0] : "strace";
1537
Denys Vlasenkoa5090542012-03-15 17:27:49 +01001538 /* Make sure SIGCHLD has the default action so that waitpid
1539 definitely works without losing track of children. The user
1540 should not have given us a bogus state to inherit, but he might
1541 have. Arguably we should detect SIG_IGN here and pass it on
1542 to children, but probably noone really needs that. */
1543 signal(SIGCHLD, SIG_DFL);
1544
Denys Vlasenko75422762011-05-27 14:36:01 +02001545 strace_tracer_pid = getpid();
1546
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001547 os_release = get_os_release();
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001548
Roland McGrathee9d4352002-12-18 04:16:10 +00001549 /* Allocate the initial tcbtab. */
1550 tcbtabsize = argc; /* Surely enough for all -p args. */
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001551 tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0]));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001552 if (!tcbtab)
1553 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001554 tcp = calloc(tcbtabsize, sizeof(*tcp));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001555 if (!tcp)
1556 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001557 for (c = 0; c < tcbtabsize; c++)
1558 tcbtab[c] = tcp++;
Roland McGrathee9d4352002-12-18 04:16:10 +00001559
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001560 shared_log = stderr;
Roland McGrath138c6a32006-01-12 09:50:49 +00001561 set_sortby(DEFAULT_SORTBY);
1562 set_personality(DEFAULT_PERSONALITY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001563 qualify("trace=all");
1564 qualify("abbrev=all");
1565 qualify("verbose=all");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001566#if DEFAULT_QUAL_FLAGS != (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
1567# error Bug in DEFAULT_QUAL_FLAGS
1568#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001569 qualify("signal=all");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001570 while ((c = getopt(argc, argv,
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001571 "+b:cCdfFhiqrtTvVxyz"
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001572 "D"
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001573 "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001574 switch (c) {
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001575 case 'b':
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001576 if (strcmp(optarg, "execve") != 0)
1577 error_msg_and_die("Syscall '%s' for -b isn't supported",
1578 optarg);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001579 detach_on_execve = 1;
1580 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001581 case 'c':
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001582 if (cflag == CFLAG_BOTH) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001583 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001584 }
1585 cflag = CFLAG_ONLY_STATS;
1586 break;
1587 case 'C':
1588 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001589 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001590 }
1591 cflag = CFLAG_BOTH;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001592 break;
1593 case 'd':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001594 debug_flag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001595 break;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001596 case 'D':
1597 daemonized_tracer = 1;
1598 break;
Roland McGrath41c48222008-07-18 00:25:10 +00001599 case 'F':
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001600 optF = 1;
1601 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001602 case 'f':
1603 followfork++;
1604 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001605 case 'h':
1606 usage(stdout, 0);
1607 break;
1608 case 'i':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001609 iflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001610 break;
1611 case 'q':
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01001612 qflag++;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001613 break;
1614 case 'r':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001615 rflag = 1;
1616 /* fall through to tflag++ */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001617 case 't':
1618 tflag++;
1619 break;
1620 case 'T':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001621 Tflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001622 break;
1623 case 'x':
1624 xflag++;
1625 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001626 case 'y':
1627 show_fd_path = 1;
1628 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001629 case 'v':
1630 qualify("abbrev=none");
1631 break;
1632 case 'V':
Roland McGrath9c9a2532003-02-20 02:56:29 +00001633 printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001634 exit(0);
1635 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001636 case 'z':
1637 not_failing_only = 1;
1638 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001639 case 'a':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001640 acolumn = string_to_uint(optarg);
Denys Vlasenko102ec492011-08-25 01:27:59 +02001641 if (acolumn < 0)
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001642 error_opt_arg(c, optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001643 break;
1644 case 'e':
1645 qualify(optarg);
1646 break;
1647 case 'o':
1648 outfname = strdup(optarg);
1649 break;
1650 case 'O':
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 set_overhead(i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001655 break;
1656 case 'p':
Denys Vlasenkoe8172b72012-03-09 13:01:04 +01001657 process_opt_p_list(optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001658 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001659 case 'P':
Denys Vlasenko7239dbc2013-03-05 15:46:34 +01001660 pathtrace_select(optarg);
Grant Edwards8a082772011-04-07 20:25:40 +00001661 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001662 case 's':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001663 i = string_to_uint(optarg);
1664 if (i < 0)
1665 error_opt_arg(c, optarg);
1666 max_strlen = i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001667 break;
1668 case 'S':
1669 set_sortby(optarg);
1670 break;
1671 case 'u':
1672 username = strdup(optarg);
1673 break;
Roland McGrathde6e5332003-01-24 04:31:23 +00001674 case 'E':
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001675 if (putenv(optarg) < 0)
1676 die_out_of_memory();
Roland McGrathde6e5332003-01-24 04:31:23 +00001677 break;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001678 case 'I':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001679 opt_intr = string_to_uint(optarg);
1680 if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS)
1681 error_opt_arg(c, optarg);
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001682 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001683 default:
1684 usage(stderr, 1);
1685 break;
1686 }
1687 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001688 argv += optind;
1689 /* argc -= optind; - no need, argc is not used below */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001690
Denys Vlasenko102ec492011-08-25 01:27:59 +02001691 acolumn_spaces = malloc(acolumn + 1);
1692 if (!acolumn_spaces)
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001693 die_out_of_memory();
Denys Vlasenko102ec492011-08-25 01:27:59 +02001694 memset(acolumn_spaces, ' ', acolumn);
1695 acolumn_spaces[acolumn] = '\0';
1696
Denys Vlasenko837399a2012-01-24 11:37:03 +01001697 /* Must have PROG [ARGS], or -p PID. Not both. */
Denys Vlasenkofd883382012-03-09 13:03:41 +01001698 if (!argv[0] == !nprocs)
Roland McGrathce0d1542003-11-11 21:24:23 +00001699 usage(stderr, 1);
1700
Denys Vlasenkofd883382012-03-09 13:03:41 +01001701 if (nprocs != 0 && daemonized_tracer) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001702 error_msg_and_die("-D and -p are mutually exclusive");
Wang Chaod322a4b2010-08-05 14:30:11 +08001703 }
1704
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001705 if (!followfork)
1706 followfork = optF;
1707
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001708 if (followfork >= 2 && cflag) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001709 error_msg_and_die("(-c or -C) and -ff are mutually exclusive");
Roland McGrathcb9def62006-04-25 07:48:03 +00001710 }
1711
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001712 /* See if they want to run as another user. */
1713 if (username != NULL) {
1714 struct passwd *pent;
1715
1716 if (getuid() != 0 || geteuid() != 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001717 error_msg_and_die("You must be root to use the -u option");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001718 }
Denys Vlasenko5d645812011-08-20 12:48:18 +02001719 pent = getpwnam(username);
1720 if (pent == NULL) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001721 error_msg_and_die("Cannot find user '%s'", username);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001722 }
1723 run_uid = pent->pw_uid;
1724 run_gid = pent->pw_gid;
1725 }
1726 else {
1727 run_uid = getuid();
1728 run_gid = getgid();
1729 }
1730
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001731 /*
1732 * On any reasonably recent Linux kernel (circa about 2.5.46)
1733 * need_fork_exec_workarounds should stay 0 after these tests:
1734 */
1735 /*need_fork_exec_workarounds = 0; - already is */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001736 if (followfork)
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001737 need_fork_exec_workarounds = test_ptrace_setoptions_followfork();
1738 need_fork_exec_workarounds |= test_ptrace_setoptions_for_all();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001739 test_ptrace_seize();
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001740
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001741 /* Check if they want to redirect the output. */
1742 if (outfname) {
Roland McGrath37b9a662003-11-07 02:26:54 +00001743 /* See if they want to pipe the output. */
1744 if (outfname[0] == '|' || outfname[0] == '!') {
1745 /*
1746 * We can't do the <outfname>.PID funny business
1747 * when using popen, so prohibit it.
1748 */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001749 if (followfork >= 2)
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001750 error_msg_and_die("Piping the output and -ff are mutually exclusive");
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001751 shared_log = strace_popen(outfname + 1);
Roland McGrath37b9a662003-11-07 02:26:54 +00001752 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001753 else if (followfork < 2)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001754 shared_log = strace_fopen(outfname);
Denys Vlasenko328bf252012-03-12 23:34:13 +01001755 } else {
1756 /* -ff without -o FILE is the same as single -f */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001757 if (followfork >= 2)
Denys Vlasenko328bf252012-03-12 23:34:13 +01001758 followfork = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001759 }
1760
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001761 if (!outfname || outfname[0] == '|' || outfname[0] == '!') {
Denys Vlasenkoa677da52012-01-24 11:31:51 +01001762 char *buf = malloc(BUFSIZ);
1763 if (!buf)
1764 die_out_of_memory();
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001765 setvbuf(shared_log, buf, _IOLBF, BUFSIZ);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001766 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001767 if (outfname && argv[0]) {
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001768 if (!opt_intr)
1769 opt_intr = INTR_NEVER;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001770 qflag = 1;
Roland McGrath36931052003-06-03 01:35:20 +00001771 }
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001772 if (!opt_intr)
1773 opt_intr = INTR_WHILE_WAIT;
Wang Chaob13c0de2010-11-12 17:25:19 +08001774
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001775 /* argv[0] -pPID -oFILE Default interactive setting
1776 * yes 0 0 INTR_WHILE_WAIT
1777 * no 1 0 INTR_WHILE_WAIT
1778 * yes 0 1 INTR_NEVER
1779 * no 1 1 INTR_WHILE_WAIT
Roland McGrath54cc1c82007-11-03 23:34:11 +00001780 */
1781
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001782 sigemptyset(&empty_set);
1783 sigemptyset(&blocked_set);
1784
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001785 /* startup_child() must be called before the signal handlers get
1786 * installed below as they are inherited into the spawned process.
1787 * Also we do not need to be protected by them as during interruption
1788 * in the startup_child() mode we kill the spawned process anyway.
1789 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001790 if (argv[0]) {
Denys Vlasenko837399a2012-01-24 11:37:03 +01001791 startup_child(argv);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001792 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001793
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001794 sa.sa_handler = SIG_IGN;
1795 sigemptyset(&sa.sa_mask);
1796 sa.sa_flags = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001797 sigaction(SIGTTOU, &sa, NULL); /* SIG_IGN */
1798 sigaction(SIGTTIN, &sa, NULL); /* SIG_IGN */
1799 if (opt_intr != INTR_ANYWHERE) {
1800 if (opt_intr == INTR_BLOCK_TSTP_TOO)
1801 sigaction(SIGTSTP, &sa, NULL); /* SIG_IGN */
1802 /*
1803 * In interactive mode (if no -o OUTFILE, or -p PID is used),
1804 * fatal signals are blocked while syscall stop is processed,
1805 * and acted on in between, when waiting for new syscall stops.
1806 * In non-interactive mode, signals are ignored.
1807 */
1808 if (opt_intr == INTR_WHILE_WAIT) {
1809 sigaddset(&blocked_set, SIGHUP);
1810 sigaddset(&blocked_set, SIGINT);
1811 sigaddset(&blocked_set, SIGQUIT);
1812 sigaddset(&blocked_set, SIGPIPE);
1813 sigaddset(&blocked_set, SIGTERM);
1814 sa.sa_handler = interrupt;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001815 }
1816 /* SIG_IGN, or set handler for these */
1817 sigaction(SIGHUP, &sa, NULL);
1818 sigaction(SIGINT, &sa, NULL);
1819 sigaction(SIGQUIT, &sa, NULL);
1820 sigaction(SIGPIPE, &sa, NULL);
1821 sigaction(SIGTERM, &sa, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001822 }
Denys Vlasenkofd883382012-03-09 13:03:41 +01001823 if (nprocs != 0 || daemonized_tracer)
Roland McGrath02203312007-06-11 22:06:31 +00001824 startup_attach();
Roland McGrath02203312007-06-11 22:06:31 +00001825
Denys Vlasenkofd883382012-03-09 13:03:41 +01001826 /* Do we want pids printed in our -o OUTFILE?
1827 * -ff: no (every pid has its own file); or
1828 * -f: yes (there can be more pids in the future); or
1829 * -p PID1,PID2: yes (there are already more than one pid)
1830 */
1831 print_pid_pfx = (outfname && followfork < 2 && (followfork == 1 || nprocs > 1));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001832}
1833
Denys Vlasenkoeebb04d2012-01-27 15:24:48 +01001834static struct tcb *
Roland McGrath54e931f2010-09-14 18:59:20 -07001835pid2tcb(int pid)
1836{
1837 int i;
1838
1839 if (pid <= 0)
1840 return NULL;
1841
1842 for (i = 0; i < tcbtabsize; i++) {
1843 struct tcb *tcp = tcbtab[i];
1844 if (tcp->pid == pid && (tcp->flags & TCB_INUSE))
1845 return tcp;
1846 }
1847
1848 return NULL;
1849}
1850
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001851static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001852cleanup(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001853{
1854 int i;
1855 struct tcb *tcp;
Denys Vlasenko35218842012-01-29 21:17:56 +01001856 int fatal_sig;
1857
1858 /* 'interrupted' is a volatile object, fetch it only once */
1859 fatal_sig = interrupted;
1860 if (!fatal_sig)
1861 fatal_sig = SIGTERM;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001862
Roland McGrathee9d4352002-12-18 04:16:10 +00001863 for (i = 0; i < tcbtabsize; i++) {
1864 tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001865 if (!(tcp->flags & TCB_INUSE))
1866 continue;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001867 if (debug_flag)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001868 fprintf(stderr,
1869 "cleanup: looking at pid %u\n", tcp->pid);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001870 if (tcp->flags & TCB_STRACE_CHILD) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001871 kill(tcp->pid, SIGCONT);
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001872 kill(tcp->pid, fatal_sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001873 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001874 detach(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001875 }
1876 if (cflag)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001877 call_summary(shared_log);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001878}
1879
1880static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001881interrupt(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001882{
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001883 interrupted = sig;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001884}
1885
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001886static int
Denys Vlasenko12014262011-05-30 14:00:14 +02001887trace(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001888{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001889 struct rusage ru;
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001890 struct rusage *rup = cflag ? &ru : NULL;
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001891#ifdef __WALL
Roland McGratheb9e2e82009-06-02 16:49:22 -07001892 static int wait4_options = __WALL;
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001893#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001894
Roland McGratheb9e2e82009-06-02 16:49:22 -07001895 while (nprocs != 0) {
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001896 int pid;
1897 int wait_errno;
1898 int status, sig;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001899 int stopped;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001900 struct tcb *tcp;
1901 unsigned event;
1902
Denys Vlasenko222713a2009-03-17 14:29:59 +00001903 if (interrupted)
Roland McGratheb9e2e82009-06-02 16:49:22 -07001904 return 0;
1905 if (interactive)
1906 sigprocmask(SIG_SETMASK, &empty_set, NULL);
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001907#ifdef __WALL
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001908 pid = wait4(-1, &status, wait4_options, rup);
Roland McGrath5bc05552002-12-17 04:50:47 +00001909 if (pid < 0 && (wait4_options & __WALL) && errno == EINVAL) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001910 /* this kernel does not support __WALL */
1911 wait4_options &= ~__WALL;
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001912 pid = wait4(-1, &status, wait4_options, rup);
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001913 }
Roland McGrath5bc05552002-12-17 04:50:47 +00001914 if (pid < 0 && !(wait4_options & __WALL) && errno == ECHILD) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001915 /* most likely a "cloned" process */
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001916 pid = wait4(-1, &status, __WCLONE, rup);
1917 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001918 perror_msg("wait4(__WCLONE) failed");
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001919 }
1920 }
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001921#else
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001922 pid = wait4(-1, &status, 0, rup);
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001923#endif /* __WALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001924 wait_errno = errno;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001925 if (interactive)
1926 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001927
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001928 if (pid < 0) {
Roland McGratheb9e2e82009-06-02 16:49:22 -07001929 switch (wait_errno) {
1930 case EINTR:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001931 continue;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001932 case ECHILD:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001933 /*
1934 * We would like to verify this case
1935 * but sometimes a race in Solbourne's
1936 * version of SunOS sometimes reports
1937 * ECHILD before sending us SIGCHILD.
1938 */
Roland McGratheb9e2e82009-06-02 16:49:22 -07001939 return 0;
1940 default:
1941 errno = wait_errno;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001942 perror_msg("wait");
Roland McGratheb9e2e82009-06-02 16:49:22 -07001943 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001944 }
1945 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001946 if (pid == popen_pid) {
1947 if (WIFEXITED(status) || WIFSIGNALED(status))
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001948 popen_pid = 0;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001949 continue;
1950 }
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001951
1952 event = ((unsigned)status >> 16);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001953 if (debug_flag) {
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001954 char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko67559ad2012-03-13 12:05:27 +01001955 char evbuf[sizeof(",PTRACE_EVENT_?? (%u)") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001956 strcpy(buf, "???");
1957 if (WIFSIGNALED(status))
1958#ifdef WCOREDUMP
1959 sprintf(buf, "WIFSIGNALED,%ssig=%s",
1960 WCOREDUMP(status) ? "core," : "",
1961 signame(WTERMSIG(status)));
1962#else
1963 sprintf(buf, "WIFSIGNALED,sig=%s",
1964 signame(WTERMSIG(status)));
1965#endif
1966 if (WIFEXITED(status))
1967 sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status));
1968 if (WIFSTOPPED(status))
1969 sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status)));
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02001970#ifdef WIFCONTINUED
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001971 if (WIFCONTINUED(status))
1972 strcpy(buf, "WIFCONTINUED");
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02001973#endif
Denys Vlasenko67559ad2012-03-13 12:05:27 +01001974 evbuf[0] = '\0';
1975 if (event != 0) {
1976 static const char *const event_names[] = {
1977 [PTRACE_EVENT_CLONE] = "CLONE",
1978 [PTRACE_EVENT_FORK] = "FORK",
1979 [PTRACE_EVENT_VFORK] = "VFORK",
1980 [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE",
1981 [PTRACE_EVENT_EXEC] = "EXEC",
1982 [PTRACE_EVENT_EXIT] = "EXIT",
1983 };
1984 const char *e;
1985 if (event < ARRAY_SIZE(event_names))
1986 e = event_names[event];
1987 else {
1988 sprintf(buf, "?? (%u)", event);
1989 e = buf;
1990 }
1991 sprintf(evbuf, ",PTRACE_EVENT_%s", e);
1992 }
1993 fprintf(stderr, " [wait(0x%04x) = %u] %s%s\n", status, pid, buf, evbuf);
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001994 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001995
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001996 /* Look up 'pid' in our table. */
Denys Vlasenko5d645812011-08-20 12:48:18 +02001997 tcp = pid2tcb(pid);
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001998
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01001999 if (!tcp) {
Roland McGrath41c48222008-07-18 00:25:10 +00002000 if (followfork) {
Denys Vlasenko418d66a2009-01-17 01:52:54 +00002001 tcp = alloctcb(pid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002002 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01002003 newoutf(tcp);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002004 if (!qflag)
Denys Vlasenko833fb132011-08-17 11:30:56 +02002005 fprintf(stderr, "Process %d attached\n",
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002006 pid);
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002007 } else {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002008 /* This can happen if a clone call used
2009 CLONE_PTRACE itself. */
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002010 if (WIFSTOPPED(status))
Denys Vlasenko97c503f2012-03-09 15:11:21 +01002011 ptrace(PTRACE_CONT, pid, (char *) 0, 0);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02002012 error_msg_and_die("Unknown pid: %u", pid);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002013 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002014 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002015
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002016 clear_regs();
2017 if (WIFSTOPPED(status))
2018 get_regs(pid);
2019
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002020 /* Under Linux, execve changes pid to thread leader's pid,
2021 * and we see this changed pid on EVENT_EXEC and later,
2022 * execve sysexit. Leader "disappears" without exit
2023 * notification. Let user know that, drop leader's tcb,
2024 * and fix up pid in execve thread's tcb.
2025 * Effectively, execve thread's tcb replaces leader's tcb.
2026 *
2027 * BTW, leader is 'stuck undead' (doesn't report WIFEXITED
2028 * on exit syscall) in multithreaded programs exactly
2029 * in order to handle this case.
2030 *
2031 * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0.
2032 * On 2.6 and earlier, it can return garbage.
2033 */
2034 if (event == PTRACE_EVENT_EXEC && os_release >= KERNEL_VERSION(3,0,0)) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002035 FILE *fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002036 struct tcb *execve_thread;
2037 long old_pid = 0;
2038
2039 if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) < 0)
2040 goto dont_switch_tcbs;
2041 if (old_pid <= 0 || old_pid == pid)
2042 goto dont_switch_tcbs;
2043 execve_thread = pid2tcb(old_pid);
2044 /* It should be !NULL, but I feel paranoid */
2045 if (!execve_thread)
2046 goto dont_switch_tcbs;
2047
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002048 if (execve_thread->curcol != 0) {
2049 /*
2050 * One case we are here is -ff:
2051 * try "strace -oLOG -ff test/threaded_execve"
2052 */
2053 fprintf(execve_thread->outf, " <pid changed to %d ...>\n", pid);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002054 /*execve_thread->curcol = 0; - no need, see code below */
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002055 }
2056 /* Swap output FILEs (needed for -ff) */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002057 fp = execve_thread->outf;
2058 execve_thread->outf = tcp->outf;
2059 tcp->outf = fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002060 /* And their column positions */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002061 execve_thread->curcol = tcp->curcol;
2062 tcp->curcol = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002063 /* Drop leader, but close execve'd thread outfile (if -ff) */
2064 droptcb(tcp);
2065 /* Switch to the thread, reusing leader's outfile and pid */
2066 tcp = execve_thread;
2067 tcp->pid = pid;
2068 if (cflag != CFLAG_ONLY_STATS) {
2069 printleader(tcp);
2070 tprintf("+++ superseded by execve in pid %lu +++\n", old_pid);
2071 line_ended();
2072 tcp->flags |= TCB_REPRINT;
2073 }
2074 }
2075 dont_switch_tcbs:
2076
2077 if (event == PTRACE_EVENT_EXEC && detach_on_execve) {
2078 if (!skip_startup_execve)
2079 detach(tcp);
2080 /* This was initial execve for "strace PROG". Skip. */
2081 skip_startup_execve = 0;
2082 }
2083
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002084 /* Set current output file */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002085 current_tcp = tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002086
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002087 if (cflag) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002088 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2089 tcp->stime = ru.ru_stime;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002090 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002091
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002092 if (WIFSIGNALED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002093 if (pid == strace_child)
2094 exit_code = 0x100 | WTERMSIG(status);
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002095 if (cflag != CFLAG_ONLY_STATS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01002096 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)
2097 ) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002098 printleader(tcp);
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002099#ifdef WCOREDUMP
Denys Vlasenko000b6012012-01-28 01:25:03 +01002100 tprintf("+++ killed by %s %s+++\n",
Roland McGrath2efe8792004-01-13 09:59:45 +00002101 signame(WTERMSIG(status)),
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002102 WCOREDUMP(status) ? "(core dumped) " : "");
2103#else
Denys Vlasenko000b6012012-01-28 01:25:03 +01002104 tprintf("+++ killed by %s +++\n",
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002105 signame(WTERMSIG(status)));
Roland McGrath2efe8792004-01-13 09:59:45 +00002106#endif
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002107 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002108 }
2109 droptcb(tcp);
2110 continue;
2111 }
2112 if (WIFEXITED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002113 if (pid == strace_child)
2114 exit_code = WEXITSTATUS(status);
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01002115 if (cflag != CFLAG_ONLY_STATS &&
2116 qflag < 2) {
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002117 printleader(tcp);
Denys Vlasenko000b6012012-01-28 01:25:03 +01002118 tprintf("+++ exited with %d +++\n", WEXITSTATUS(status));
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002119 line_ended();
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002120 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002121 droptcb(tcp);
2122 continue;
2123 }
2124 if (!WIFSTOPPED(status)) {
2125 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
2126 droptcb(tcp);
2127 continue;
2128 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002129
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002130 /* Is this the very first time we see this tracee stopped? */
2131 if (tcp->flags & TCB_STARTUP) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002132 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002133 fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n", tcp->pid);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002134 tcp->flags &= ~TCB_STARTUP;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002135 if (tcp->flags & TCB_BPTSET) {
Roland McGrath02203312007-06-11 22:06:31 +00002136 /*
2137 * One example is a breakpoint inherited from
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002138 * parent through fork().
Roland McGrath02203312007-06-11 22:06:31 +00002139 */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002140 if (clearbpt(tcp) < 0) {
2141 /* Pretty fatal */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002142 droptcb(tcp);
2143 cleanup();
2144 return -1;
2145 }
2146 }
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002147 if (ptrace_setoptions) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002148 if (debug_flag)
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002149 fprintf(stderr, "setting opts %x on pid %d\n", ptrace_setoptions, tcp->pid);
2150 if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) {
2151 if (errno != ESRCH) {
2152 /* Should never happen, really */
2153 perror_msg_and_die("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02002154 }
2155 }
2156 }
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002157 }
2158
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002159 sig = WSTOPSIG(status);
2160
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002161 if (event != 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002162 /* Ptrace event */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002163#if USE_SEIZE
Denys Vlasenko26bc0602012-07-10 16:36:32 +02002164 if (event == PTRACE_EVENT_STOP) {
Denys Vlasenko67038162012-01-29 16:46:46 +01002165 /*
2166 * PTRACE_INTERRUPT-stop or group-stop.
2167 * PTRACE_INTERRUPT-stop has sig == SIGTRAP here.
2168 */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002169 if (sig == SIGSTOP
2170 || sig == SIGTSTP
2171 || sig == SIGTTIN
2172 || sig == SIGTTOU
2173 ) {
2174 stopped = 1;
2175 goto show_stopsig;
2176 }
2177 }
2178#endif
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002179 goto restart_tracee_with_sig_0;
2180 }
2181
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002182 /* Is this post-attach SIGSTOP?
2183 * Interestingly, the process may stop
2184 * with STOPSIG equal to some other signal
2185 * than SIGSTOP if we happend to attach
2186 * just before the process takes a signal.
2187 */
2188 if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002189 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002190 fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid);
2191 tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002192 goto restart_tracee_with_sig_0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002193 }
2194
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002195 if (sig != syscall_trap_sig) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002196 siginfo_t si;
2197
2198 /* Nonzero (true) if tracee is stopped by signal
2199 * (as opposed to "tracee received signal").
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002200 * TODO: shouldn't we check for errno == EINVAL too?
2201 * We can get ESRCH instead, you know...
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002202 */
2203 stopped = (ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002204#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002205 show_stopsig:
Denys Vlasenko67038162012-01-29 16:46:46 +01002206#endif
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002207 if (cflag != CFLAG_ONLY_STATS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01002208 && (qual_flags[sig] & QUAL_SIGNAL)
2209 ) {
Dmitry V. Levinc15dfc72011-03-10 14:44:45 +00002210#if defined(PT_CR_IPSR) && defined(PT_CR_IIP)
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002211 long pc = 0;
2212 long psr = 0;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002213
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002214 upeek(tcp, PT_CR_IPSR, &psr);
2215 upeek(tcp, PT_CR_IIP, &pc);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002216
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002217# define PSR_RI 41
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002218 pc += (psr >> PSR_RI) & 0x3;
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002219# define PC_FORMAT_STR " @ %lx"
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002220# define PC_FORMAT_ARG , pc
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002221#else
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002222# define PC_FORMAT_STR ""
2223# define PC_FORMAT_ARG /* nothing */
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002224#endif
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01002225 if (qflag < 2) {
2226 printleader(tcp);
2227 if (!stopped) {
2228 tprintf("--- %s ", signame(sig));
2229 printsiginfo(&si, verbose(tcp));
2230 tprintf(PC_FORMAT_STR " ---\n"
2231 PC_FORMAT_ARG);
2232 } else
2233 tprintf("--- stopped by %s" PC_FORMAT_STR " ---\n",
2234 signame(sig)
2235 PC_FORMAT_ARG);
2236 line_ended();
2237 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002238 }
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002239
2240 if (!stopped)
2241 /* It's signal-delivery-stop. Inject the signal */
2242 goto restart_tracee;
2243
2244 /* It's group-stop */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002245#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002246 if (use_seize) {
2247 /*
2248 * This ends ptrace-stop, but does *not* end group-stop.
2249 * This makes stopping signals work properly on straced process
2250 * (that is, process really stops. It used to continue to run).
2251 */
2252 if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) {
2253 cleanup();
2254 return -1;
2255 }
2256 continue;
2257 }
2258 /* We don't have PTRACE_LISTEN support... */
2259#endif
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002260 goto restart_tracee;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002261 }
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002262
2263 /* We handled quick cases, we are permitted to interrupt now. */
Roland McGrath02203312007-06-11 22:06:31 +00002264 if (interrupted)
2265 return 0;
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002266
2267 /* This should be syscall entry or exit.
2268 * (Or it still can be that pesky post-execve SIGTRAP!)
2269 * Handle it.
2270 */
Denys Vlasenko23506752012-03-21 10:32:49 +01002271 if (trace_syscall(tcp) < 0) {
2272 /* ptrace() failed in trace_syscall().
Roland McGratheb9e2e82009-06-02 16:49:22 -07002273 * Likely a result of process disappearing mid-flight.
Denys Vlasenko23506752012-03-21 10:32:49 +01002274 * Observed case: exit_group() or SIGKILL terminating
Denys Vlasenkof1e69032012-01-04 15:15:26 +01002275 * all processes in thread group.
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002276 * We assume that ptrace error was caused by process death.
Denys Vlasenkof2025022012-03-09 15:29:45 +01002277 * We used to detach(tcp) here, but since we no longer
2278 * implement "detach before death" policy/hack,
2279 * we can let this process to report its death to us
2280 * normally, via WIFEXITED or WIFSIGNALED wait status.
2281 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002282 continue;
2283 }
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002284 restart_tracee_with_sig_0:
2285 sig = 0;
2286 restart_tracee:
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002287 if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002288 cleanup();
2289 return -1;
2290 }
2291 }
2292 return 0;
2293}
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002294
2295int
2296main(int argc, char *argv[])
2297{
2298 init(argc, argv);
2299
2300 /* Run main tracing loop */
2301 if (trace() < 0)
2302 return 1;
2303
2304 cleanup();
2305 fflush(NULL);
Dmitry V. Levincf534362012-07-12 20:54:46 +00002306 if (shared_log != stderr)
2307 fclose(shared_log);
2308 if (popen_pid) {
2309 while (waitpid(popen_pid, NULL, 0) < 0 && errno == EINTR)
2310 ;
2311 }
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002312 if (exit_code > 0xff) {
2313 /* Avoid potential core file clobbering. */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00002314 struct_rlimit rlim = {0, 0};
2315 set_rlimit(RLIMIT_CORE, &rlim);
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002316
2317 /* Child was killed by a signal, mimic that. */
2318 exit_code &= 0xff;
2319 signal(exit_code, SIG_DFL);
2320 raise(exit_code);
2321 /* Paranoia - what if this signal is not fatal?
2322 Exit with 128 + signo then. */
2323 exit_code += 128;
2324 }
2325
2326 return exit_code;
2327}