blob: 8d7ee74d6486cca8ce5a0071bd8bbee3a0ff4c54 [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00006 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000029 */
30
31#include "defs.h"
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020032#include <stdarg.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000033#include <sys/param.h>
34#include <fcntl.h>
35#include <sys/resource.h>
36#include <sys/wait.h>
37#include <sys/stat.h>
38#include <pwd.h>
39#include <grp.h>
Roland McGrath70b08532004-04-09 00:25:21 +000040#include <dirent.h>
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +010041#include <sys/utsname.h>
Denys Vlasenko84703742012-02-25 02:38:52 +010042#if defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +000043# include <asm/ptrace_offsets.h>
44#endif
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010045/* In some libc, these aren't declared. Do it ourself: */
Denys Vlasenko96d5a762008-12-29 19:13:27 +000046extern char **environ;
Denys Vlasenko418d66a2009-01-17 01:52:54 +000047extern int optind;
48extern char *optarg;
Denys Vlasenko96d5a762008-12-29 19:13:27 +000049
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010050
51#if defined __NR_tkill
52# define my_tkill(tid, sig) syscall(__NR_tkill, (tid), (sig))
53#else
54 /* kill() may choose arbitrarily the target task of the process group
55 while we later wait on a that specific TID. PID process waits become
56 TID task specific waits for a process under ptrace(2). */
Denys Vlasenko978fbc92012-09-13 10:28:43 +020057# warning "tkill(2) not available, risk of strace hangs!"
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010058# define my_tkill(tid, sig) kill((tid), (sig))
59#endif
60
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +010061/* Glue for systems without a MMU that cannot provide fork() */
62#if !defined(HAVE_FORK)
63# undef NOMMU_SYSTEM
64# define NOMMU_SYSTEM 1
65#endif
66#if NOMMU_SYSTEM
67# define fork() vfork()
68#endif
69
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010070cflag_t cflag = CFLAG_NONE;
71unsigned int followfork = 0;
Denys Vlasenkof44cce42011-06-21 14:34:10 +020072unsigned int ptrace_setoptions = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010073unsigned int xflag = 0;
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +010074bool need_fork_exec_workarounds = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010075bool debug_flag = 0;
76bool Tflag = 0;
77bool qflag = 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020078/* Which WSTOPSIG(status) value marks syscall traps? */
Denys Vlasenko75422762011-05-27 14:36:01 +020079static unsigned int syscall_trap_sig = SIGTRAP;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010080static unsigned int tflag = 0;
81static bool iflag = 0;
82static bool rflag = 0;
83static bool print_pid_pfx = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +010084
85/* -I n */
86enum {
87 INTR_NOT_SET = 0,
88 INTR_ANYWHERE = 1, /* don't block/ignore any signals */
89 INTR_WHILE_WAIT = 2, /* block fatal signals while decoding syscall. default */
90 INTR_NEVER = 3, /* block fatal signals. default if '-o FILE PROG' */
91 INTR_BLOCK_TSTP_TOO = 4, /* block fatal signals and SIGTSTP (^Z) */
92 NUM_INTR_OPTS
93};
94static int opt_intr;
95/* We play with signal mask only if this mode is active: */
96#define interactive (opt_intr == INTR_WHILE_WAIT)
97
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +000098/*
99 * daemonized_tracer supports -D option.
100 * With this option, strace forks twice.
101 * Unlike normal case, with -D *grandparent* process exec's,
102 * becoming a traced process. Child exits (this prevents traced process
103 * from having children it doesn't expect to have), and grandchild
104 * attaches to grandparent similarly to strace -p PID.
105 * This allows for more transparent interaction in cases
106 * when process and its parent are communicating via signals,
107 * wait() etc. Without -D, strace process gets lodged in between,
108 * disrupting parent<->child link.
109 */
110static bool daemonized_tracer = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000111
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100112#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100113static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP;
114# define use_seize (post_attach_sigstop == 0)
115#else
116# define post_attach_sigstop TCB_IGNORE_ONE_SIGSTOP
117# define use_seize 0
118#endif
119
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000120/* Sometimes we want to print only succeeding syscalls. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100121bool not_failing_only = 0;
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000122
Grant Edwards8a082772011-04-07 20:25:40 +0000123/* Show path associated with fd arguments */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100124bool show_fd_path = 0;
Grant Edwards8a082772011-04-07 20:25:40 +0000125
126/* are we filtering traces based on paths? */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100127bool tracing_paths = 0;
Grant Edwards8a082772011-04-07 20:25:40 +0000128
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100129static bool detach_on_execve = 0;
130static bool skip_startup_execve = 0;
131
Dmitry V. Levina6809652008-11-10 17:14:58 +0000132static int exit_code = 0;
133static int strace_child = 0;
Denys Vlasenko75422762011-05-27 14:36:01 +0200134static int strace_tracer_pid = 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -0700135
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000136static char *username = NULL;
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200137static uid_t run_uid;
138static gid_t run_gid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000139
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100140unsigned int max_strlen = DEFAULT_STRLEN;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000141static int acolumn = DEFAULT_ACOLUMN;
Denys Vlasenko102ec492011-08-25 01:27:59 +0200142static char *acolumn_spaces;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100143
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000144static char *outfname = NULL;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100145/* If -ff, points to stderr. Else, it's our common output log */
146static FILE *shared_log;
147
Denys Vlasenko000b6012012-01-28 01:25:03 +0100148struct tcb *printing_tcp = NULL;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100149static struct tcb *current_tcp;
150
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200151static struct tcb **tcbtab;
Denys Vlasenko2b60c352011-06-22 12:45:25 +0200152static unsigned int nprocs, tcbtabsize;
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200153static const char *progname;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000154
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100155unsigned os_release; /* generated from uname()'s u.release */
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +0100156
Denys Vlasenko4c196382012-01-04 15:11:09 +0100157static int detach(struct tcb *tcp);
Andreas Schwabe5355de2009-10-27 16:56:43 +0100158static int trace(void);
159static void cleanup(void);
160static void interrupt(int sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000161static sigset_t empty_set, blocked_set;
162
163#ifdef HAVE_SIG_ATOMIC_T
164static volatile sig_atomic_t interrupted;
Denys Vlasenkoa3559252012-01-29 16:43:51 +0100165#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000166static volatile int interrupted;
Denys Vlasenkoa3559252012-01-29 16:43:51 +0100167#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000168
Denys Vlasenko2c4fb902012-03-15 17:24:49 +0100169#ifndef HAVE_STRERROR
170
171#if !HAVE_DECL_SYS_ERRLIST
172extern int sys_nerr;
173extern char *sys_errlist[];
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +0100174#endif
Denys Vlasenko2c4fb902012-03-15 17:24:49 +0100175
176const char *
177strerror(int err_no)
178{
179 static char buf[sizeof("Unknown error %d") + sizeof(int)*3];
180
181 if (err_no < 1 || err_no >= sys_nerr) {
182 sprintf(buf, "Unknown error %d", err_no);
183 return buf;
184 }
185 return sys_errlist[err_no];
186}
187
188#endif /* HAVE_STERRROR */
189
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000190static void
Denys Vlasenkocb2ad002011-06-23 13:05:29 +0200191usage(FILE *ofp, int exitval)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000192{
193 fprintf(ofp, "\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200194usage: strace [-CdffhiqrtttTvVxxy] [-I n] [-e expr]...\n\
195 [-a column] [-o file] [-s strsize] [-P path]...\n\
196 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
197 or: strace -c[df] [-I n] [-e expr]... [-O overhead] [-S sortby]\n\
198 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000199-c -- count time, calls, and errors for each syscall and report summary\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200200-C -- like -c but also print regular output\n\
Denys Vlasenko3e084ac2012-03-15 13:39:05 +0100201-d -- enable debug output to stderr\n\
Denys Vlasenkob51581e2012-01-29 16:53:03 +0100202-D -- run tracer process as a detached grandchild, not as parent\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000203-f -- follow forks, -ff -- with output into separate files\n\
Denys Vlasenko3e084ac2012-03-15 13:39:05 +0100204-F -- attempt to follow vforks (deprecated, use -f)\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000205-i -- print instruction pointer at time of syscall\n\
206-q -- suppress messages about attaching, detaching, etc.\n\
207-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
Denys Vlasenkocdab1be2012-02-03 12:17:57 +0100208-T -- print time spent in each syscall\n\
209-v -- verbose mode: print unabbreviated argv, stat, termios, etc. args\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000210-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000211-y -- print paths associated with file descriptor arguments\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200212-h -- print help message, -V -- print version\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000213-a column -- alignment COLUMN for printing syscall results (default %d)\n\
214-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
215 options: trace, abbrev, verbose, raw, signal, read, or write\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200216-I interruptible --\n\
217 1: no signals are blocked\n\
218 2: fatal signals are blocked while decoding syscall (default)\n\
219 3: fatal signals are always blocked (default if '-o FILE PROG')\n\
220 4: fatal signals and SIGTSTP (^Z) are always blocked\n\
221 (useful to make 'strace -o FILE PROG' not stop on ^Z)\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000222-o file -- send trace output to FILE instead of stderr\n\
223-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
224-p pid -- trace process with process id PID, may be repeated\n\
225-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
226-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
227-u username -- run command as username handling setuid and/or setgid\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000228-E var=val -- put var=val in the environment for command\n\
229-E var -- remove var from the environment for command\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000230-P path -- trace accesses to path\n\
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100231"
232/* 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 */
235/* experimental, don't document it yet (option letter may change in the future!)
236-b -- detach on successful execve\n\
237 */
Roland McGrathde6e5332003-01-24 04:31:23 +0000238, DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000239 exit(exitval);
240}
241
Denys Vlasenko75422762011-05-27 14:36:01 +0200242static void die(void) __attribute__ ((noreturn));
243static void die(void)
244{
245 if (strace_tracer_pid == getpid()) {
246 cflag = 0;
247 cleanup();
248 }
249 exit(1);
250}
251
252static void verror_msg(int err_no, const char *fmt, va_list p)
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200253{
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100254 char *msg;
255
Dmitry V. Levin44d05322011-06-09 15:50:41 +0000256 fflush(NULL);
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100257
258 /* We want to print entire message with single fprintf to ensure
259 * message integrity if stderr is shared with other programs.
260 * Thus we use vasprintf + single fprintf.
261 */
262 msg = NULL;
Denys Vlasenkocfad5432012-01-24 12:48:02 +0100263 if (vasprintf(&msg, fmt, p) >= 0) {
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100264 if (err_no)
265 fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(err_no));
266 else
267 fprintf(stderr, "%s: %s\n", progname, msg);
268 free(msg);
269 } else {
270 /* malloc in vasprintf failed, try it without malloc */
271 fprintf(stderr, "%s: ", progname);
272 vfprintf(stderr, fmt, p);
273 if (err_no)
274 fprintf(stderr, ": %s\n", strerror(err_no));
275 else
276 putc('\n', stderr);
277 }
278 /* We don't switch stderr to buffered, thus fprintf(stderr)
279 * always flushes its output and this is not necessary: */
280 /* fflush(stderr); */
Denys Vlasenko75422762011-05-27 14:36:01 +0200281}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200282
Denys Vlasenko75422762011-05-27 14:36:01 +0200283void error_msg(const char *fmt, ...)
284{
285 va_list p;
286 va_start(p, fmt);
287 verror_msg(0, fmt, p);
288 va_end(p);
289}
290
291void error_msg_and_die(const char *fmt, ...)
292{
293 va_list p;
294 va_start(p, fmt);
295 verror_msg(0, fmt, p);
296 die();
297}
298
299void perror_msg(const char *fmt, ...)
300{
301 va_list p;
302 va_start(p, fmt);
303 verror_msg(errno, fmt, p);
304 va_end(p);
305}
306
307void perror_msg_and_die(const char *fmt, ...)
308{
309 va_list p;
310 va_start(p, fmt);
311 verror_msg(errno, fmt, p);
312 die();
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200313}
314
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200315void die_out_of_memory(void)
316{
317 static bool recursed = 0;
318 if (recursed)
319 exit(1);
320 recursed = 1;
321 error_msg_and_die("Out of memory");
322}
323
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000324static void
325error_opt_arg(int opt, const char *arg)
326{
327 error_msg_and_die("Invalid -%c argument: '%s'", opt, arg);
328}
329
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100330#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100331static int
332ptrace_attach_or_seize(int pid)
333{
334 int r;
335 if (!use_seize)
336 return ptrace(PTRACE_ATTACH, pid, 0, 0);
Denys Vlasenko26bc0602012-07-10 16:36:32 +0200337 r = ptrace(PTRACE_SEIZE, pid, 0, 0);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100338 if (r)
339 return r;
340 r = ptrace(PTRACE_INTERRUPT, pid, 0, 0);
341 return r;
342}
343#else
344# define ptrace_attach_or_seize(pid) ptrace(PTRACE_ATTACH, (pid), 0, 0)
345#endif
346
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100347/*
348 * Used when we want to unblock stopped traced process.
349 * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL.
350 * Returns 0 on success or if error was ESRCH
351 * (presumably process was killed while we talk to it).
352 * Otherwise prints error message and returns -1.
353 */
354static int
355ptrace_restart(int op, struct tcb *tcp, int sig)
356{
357 int err;
358 const char *msg;
359
360 errno = 0;
361 ptrace(op, tcp->pid, (void *) 0, (long) sig);
362 err = errno;
Denys Vlasenko23506752012-03-21 10:32:49 +0100363 if (!err)
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100364 return 0;
365
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100366 msg = "SYSCALL";
367 if (op == PTRACE_CONT)
368 msg = "CONT";
369 if (op == PTRACE_DETACH)
370 msg = "DETACH";
371#ifdef PTRACE_LISTEN
372 if (op == PTRACE_LISTEN)
373 msg = "LISTEN";
374#endif
Denys Vlasenko23506752012-03-21 10:32:49 +0100375 /*
376 * Why curcol != 0? Otherwise sometimes we get this:
377 *
378 * 10252 kill(10253, SIGKILL) = 0
379 * <ptrace(SYSCALL,10252):No such process>10253 ...next decode...
380 *
381 * 10252 died after we retrieved syscall exit data,
382 * but before we tried to restart it. Log looks ugly.
383 */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100384 if (current_tcp && current_tcp->curcol != 0) {
Denys Vlasenko23506752012-03-21 10:32:49 +0100385 tprintf(" <ptrace(%s):%s>\n", msg, strerror(err));
386 line_ended();
387 }
388 if (err == ESRCH)
389 return 0;
390 errno = err;
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100391 perror_msg("ptrace(PTRACE_%s,pid:%d,sig:%d)", msg, tcp->pid, sig);
392 return -1;
393}
394
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200395static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000396set_cloexec_flag(int fd)
397{
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200398 int flags, newflags;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000399
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200400 flags = fcntl(fd, F_GETFD);
401 if (flags < 0) {
402 /* Can happen only if fd is bad.
403 * Should never happen: if it does, we have a bug
404 * in the caller. Therefore we just abort
405 * instead of propagating the error.
406 */
407 perror_msg_and_die("fcntl(%d, F_GETFD)", fd);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000408 }
409
410 newflags = flags | FD_CLOEXEC;
411 if (flags == newflags)
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200412 return;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000413
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200414 fcntl(fd, F_SETFD, newflags); /* never fails */
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000415}
416
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100417static void kill_save_errno(pid_t pid, int sig)
418{
419 int saved_errno = errno;
420
421 (void) kill(pid, sig);
422 errno = saved_errno;
423}
424
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100425/*
426 * When strace is setuid executable, we have to swap uids
427 * before and after filesystem and process management operations.
428 */
429static void
430swap_uid(void)
431{
432 int euid = geteuid(), uid = getuid();
433
434 if (euid != uid && setreuid(euid, uid) < 0) {
435 perror_msg_and_die("setreuid");
436 }
437}
438
439#if _LFS64_LARGEFILE
440# define fopen_for_output fopen64
441#else
442# define fopen_for_output fopen
443#endif
444
445static FILE *
446strace_fopen(const char *path)
447{
448 FILE *fp;
449
450 swap_uid();
451 fp = fopen_for_output(path, "w");
452 if (!fp)
453 perror_msg_and_die("Can't fopen '%s'", path);
454 swap_uid();
455 set_cloexec_flag(fileno(fp));
456 return fp;
457}
458
459static int popen_pid = 0;
460
461#ifndef _PATH_BSHELL
462# define _PATH_BSHELL "/bin/sh"
463#endif
464
465/*
466 * We cannot use standard popen(3) here because we have to distinguish
467 * popen child process from other processes we trace, and standard popen(3)
468 * does not export its child's pid.
469 */
470static FILE *
471strace_popen(const char *command)
472{
473 FILE *fp;
474 int fds[2];
475
476 swap_uid();
477 if (pipe(fds) < 0)
478 perror_msg_and_die("pipe");
479
480 set_cloexec_flag(fds[1]); /* never fails */
481
482 popen_pid = vfork();
483 if (popen_pid == -1)
484 perror_msg_and_die("vfork");
485
486 if (popen_pid == 0) {
487 /* child */
488 close(fds[1]);
489 if (fds[0] != 0) {
490 if (dup2(fds[0], 0))
491 perror_msg_and_die("dup2");
492 close(fds[0]);
493 }
494 execl(_PATH_BSHELL, "sh", "-c", command, NULL);
495 perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL);
496 }
497
498 /* parent */
499 close(fds[0]);
500 swap_uid();
501 fp = fdopen(fds[1], "w");
502 if (!fp)
503 die_out_of_memory();
504 return fp;
505}
506
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100507void
508tprintf(const char *fmt, ...)
509{
510 va_list args;
511
512 va_start(args, fmt);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100513 if (current_tcp) {
Denys Vlasenko6e4f3c12012-04-16 18:22:19 +0200514 int n = strace_vfprintf(current_tcp->outf, fmt, args);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100515 if (n < 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100516 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000517 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100518 } else
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100519 current_tcp->curcol += n;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100520 }
521 va_end(args);
522}
523
524void
525tprints(const char *str)
526{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100527 if (current_tcp) {
Denys Vlasenko142aee02012-04-16 18:10:15 +0200528 int n = fputs_unlocked(str, current_tcp->outf);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100529 if (n >= 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100530 current_tcp->curcol += strlen(str);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100531 return;
532 }
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100533 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000534 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100535 }
536}
537
538void
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100539line_ended(void)
540{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100541 if (current_tcp) {
542 current_tcp->curcol = 0;
543 fflush(current_tcp->outf);
544 }
545 if (printing_tcp) {
546 printing_tcp->curcol = 0;
547 printing_tcp = NULL;
548 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100549}
550
551void
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100552printleader(struct tcb *tcp)
553{
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100554 /* If -ff, "previous tcb we printed" is always the same as current,
555 * because we have per-tcb output files.
556 */
557 if (followfork >= 2)
558 printing_tcp = tcp;
559
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100560 if (printing_tcp) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100561 current_tcp = printing_tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100562 if (printing_tcp->curcol != 0 && (followfork < 2 || printing_tcp == tcp)) {
563 /*
564 * case 1: we have a shared log (i.e. not -ff), and last line
565 * wasn't finished (same or different tcb, doesn't matter).
566 * case 2: split log, we are the same tcb, but our last line
567 * didn't finish ("SIGKILL nuked us after syscall entry" etc).
568 */
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100569 tprints(" <unfinished ...>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100570 printing_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100571 }
572 }
573
574 printing_tcp = tcp;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100575 current_tcp = tcp;
576 current_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100577
578 if (print_pid_pfx)
579 tprintf("%-5d ", tcp->pid);
580 else if (nprocs > 1 && !outfname)
581 tprintf("[pid %5u] ", tcp->pid);
582
583 if (tflag) {
584 char str[sizeof("HH:MM:SS")];
585 struct timeval tv, dtv;
586 static struct timeval otv;
587
588 gettimeofday(&tv, NULL);
589 if (rflag) {
590 if (otv.tv_sec == 0)
591 otv = tv;
592 tv_sub(&dtv, &tv, &otv);
593 tprintf("%6ld.%06ld ",
594 (long) dtv.tv_sec, (long) dtv.tv_usec);
595 otv = tv;
596 }
597 else if (tflag > 2) {
598 tprintf("%ld.%06ld ",
599 (long) tv.tv_sec, (long) tv.tv_usec);
600 }
601 else {
602 time_t local = tv.tv_sec;
603 strftime(str, sizeof(str), "%T", localtime(&local));
604 if (tflag > 1)
605 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
606 else
607 tprintf("%s ", str);
608 }
609 }
610 if (iflag)
611 printcall(tcp);
612}
613
614void
615tabto(void)
616{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100617 if (current_tcp->curcol < acolumn)
618 tprints(acolumn_spaces + current_tcp->curcol);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100619}
620
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100621/* Should be only called directly *after successful attach* to a tracee.
622 * Otherwise, "strace -oFILE -ff -p<nonexistant_pid>"
623 * may create bogus empty FILE.<nonexistant_pid>, and then die.
624 */
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200625static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000626newoutf(struct tcb *tcp)
627{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100628 tcp->outf = shared_log; /* if not -ff mode, the same file is for all */
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100629 if (followfork >= 2) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000630 char name[520 + sizeof(int) * 3];
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000631 sprintf(name, "%.512s.%u", outfname, tcp->pid);
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200632 tcp->outf = strace_fopen(name);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000633 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000634}
635
Denys Vlasenko558e5122012-03-12 23:32:16 +0100636static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100637expand_tcbtab(void)
638{
639 /* Allocate some more TCBs and expand the table.
640 We don't want to relocate the TCBs because our
641 callers have pointers and it would be a pain.
642 So tcbtab is a table of pointers. Since we never
643 free the TCBs, we allocate a single chunk of many. */
644 int i = tcbtabsize;
645 struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0]));
646 struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0]));
647 if (!newtab || !newtcbs)
648 die_out_of_memory();
649 tcbtabsize *= 2;
650 tcbtab = newtab;
651 while (i < tcbtabsize)
652 tcbtab[i++] = newtcbs++;
653}
654
655static struct tcb *
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100656alloctcb(int pid)
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100657{
658 int i;
659 struct tcb *tcp;
660
661 if (nprocs == tcbtabsize)
662 expand_tcbtab();
663
664 for (i = 0; i < tcbtabsize; i++) {
665 tcp = tcbtab[i];
666 if ((tcp->flags & TCB_INUSE) == 0) {
667 memset(tcp, 0, sizeof(*tcp));
668 tcp->pid = pid;
669 tcp->flags = TCB_INUSE;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100670#if SUPPORTED_PERSONALITIES > 1
671 tcp->currpers = current_personality;
672#endif
673 nprocs++;
674 if (debug_flag)
675 fprintf(stderr, "new tcb for pid %d, active tcbs:%d\n", tcp->pid, nprocs);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100676 return tcp;
677 }
678 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100679 error_msg_and_die("bug in alloctcb");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100680}
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100681
682static void
683droptcb(struct tcb *tcp)
684{
685 if (tcp->pid == 0)
686 return;
687
688 nprocs--;
689 if (debug_flag)
690 fprintf(stderr, "dropped tcb for pid %d, %d remain\n", tcp->pid, nprocs);
691
692 if (tcp->outf) {
Denys Vlasenko8511f2a2012-03-22 09:35:51 +0100693 if (followfork >= 2) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100694 if (tcp->curcol != 0)
695 fprintf(tcp->outf, " <detached ...>\n");
696 fclose(tcp->outf);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100697 } else {
698 if (printing_tcp == tcp && tcp->curcol != 0)
699 fprintf(tcp->outf, " <detached ...>\n");
700 fflush(tcp->outf);
701 }
702 }
703
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100704 if (current_tcp == tcp)
705 current_tcp = NULL;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100706 if (printing_tcp == tcp)
707 printing_tcp = NULL;
708
709 memset(tcp, 0, sizeof(*tcp));
710}
711
712/* detach traced process; continue with sig
713 * Never call DETACH twice on the same process as both unattached and
714 * attached-unstopped processes give the same ESRCH. For unattached process we
715 * would SIGSTOP it and wait for its SIGSTOP notification forever.
716 */
717static int
718detach(struct tcb *tcp)
719{
720 int error;
721 int status, sigstop_expected;
722
723 if (tcp->flags & TCB_BPTSET)
724 clearbpt(tcp);
725
726 /*
727 * Linux wrongly insists the child be stopped
728 * before detaching. Arghh. We go through hoops
729 * to make a clean break of things.
730 */
731#if defined(SPARC)
Denys Vlasenko978fbc92012-09-13 10:28:43 +0200732# undef PTRACE_DETACH
733# define PTRACE_DETACH PTRACE_SUNDETACH
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100734#endif
735
736 error = 0;
737 sigstop_expected = 0;
738 if (tcp->flags & TCB_ATTACHED) {
739 /*
740 * We attached but possibly didn't see the expected SIGSTOP.
741 * We must catch exactly one as otherwise the detached process
742 * would be left stopped (process state T).
743 */
744 sigstop_expected = (tcp->flags & TCB_IGNORE_ONE_SIGSTOP);
745 error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, 0);
746 if (error == 0) {
747 /* On a clear day, you can see forever. */
748 }
749 else if (errno != ESRCH) {
750 /* Shouldn't happen. */
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000751 perror_msg("%s", "detach: ptrace(PTRACE_DETACH, ...)");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100752 }
753 else if (my_tkill(tcp->pid, 0) < 0) {
754 if (errno != ESRCH)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000755 perror_msg("%s", "detach: checking sanity");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100756 }
757 else if (!sigstop_expected && my_tkill(tcp->pid, SIGSTOP) < 0) {
758 if (errno != ESRCH)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000759 perror_msg("%s", "detach: stopping child");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100760 }
761 else
762 sigstop_expected = 1;
763 }
764
765 if (sigstop_expected) {
766 for (;;) {
767#ifdef __WALL
768 if (waitpid(tcp->pid, &status, __WALL) < 0) {
769 if (errno == ECHILD) /* Already gone. */
770 break;
771 if (errno != EINVAL) {
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000772 perror_msg("%s", "detach: waiting");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100773 break;
774 }
775#endif /* __WALL */
776 /* No __WALL here. */
777 if (waitpid(tcp->pid, &status, 0) < 0) {
778 if (errno != ECHILD) {
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000779 perror_msg("%s", "detach: waiting");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100780 break;
781 }
782#ifdef __WCLONE
783 /* If no processes, try clones. */
784 if (waitpid(tcp->pid, &status, __WCLONE) < 0) {
785 if (errno != ECHILD)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000786 perror_msg("%s", "detach: waiting");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100787 break;
788 }
789#endif /* __WCLONE */
790 }
791#ifdef __WALL
792 }
793#endif
794 if (!WIFSTOPPED(status)) {
795 /* Au revoir, mon ami. */
796 break;
797 }
798 if (WSTOPSIG(status) == SIGSTOP) {
799 ptrace_restart(PTRACE_DETACH, tcp, 0);
800 break;
801 }
802 error = ptrace_restart(PTRACE_CONT, tcp,
803 WSTOPSIG(status) == syscall_trap_sig ? 0
804 : WSTOPSIG(status));
805 if (error < 0)
806 break;
807 }
808 }
809
810 if (!qflag && (tcp->flags & TCB_ATTACHED))
811 fprintf(stderr, "Process %u detached\n", tcp->pid);
812
813 droptcb(tcp);
814
815 return error;
816}
817
818static void
Denys Vlasenko558e5122012-03-12 23:32:16 +0100819process_opt_p_list(char *opt)
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100820{
821 while (*opt) {
822 /*
823 * We accept -p PID,PID; -p "`pidof PROG`"; -p "`pgrep PROG`".
824 * pidof uses space as delim, pgrep uses newline. :(
825 */
826 int pid;
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100827 char *delim = opt + strcspn(opt, ", \n\t");
828 char c = *delim;
829
830 *delim = '\0';
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000831 pid = string_to_uint(opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100832 if (pid <= 0) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000833 error_msg_and_die("Invalid process id: '%s'", opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100834 }
835 if (pid == strace_tracer_pid) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000836 error_msg_and_die("I'm sorry, I can't let you do that, Dave.");
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100837 }
838 *delim = c;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100839 alloctcb(pid);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100840 if (c == '\0')
841 break;
842 opt = delim + 1;
843 }
844}
845
Roland McGrath02203312007-06-11 22:06:31 +0000846static void
847startup_attach(void)
848{
849 int tcbi;
850 struct tcb *tcp;
851
852 /*
853 * Block user interruptions as we would leave the traced
854 * process stopped (process state T) if we would terminate in
Denys Vlasenko2e968c02011-09-01 10:23:09 +0200855 * between PTRACE_ATTACH and wait4() on SIGSTOP.
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200856 * We rely on cleanup() from this point on.
Roland McGrath02203312007-06-11 22:06:31 +0000857 */
858 if (interactive)
859 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
860
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000861 if (daemonized_tracer) {
862 pid_t pid = fork();
863 if (pid < 0) {
Denys Vlasenko014ca3a2011-09-02 16:19:30 +0200864 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000865 }
866 if (pid) { /* parent */
867 /*
Denys Vlasenko75422762011-05-27 14:36:01 +0200868 * Wait for grandchild to attach to straced process
869 * (grandparent). Grandchild SIGKILLs us after it attached.
870 * Grandparent's wait() is unblocked by our death,
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000871 * it proceeds to exec the straced program.
872 */
873 pause();
874 _exit(0); /* paranoia */
875 }
Denys Vlasenko75422762011-05-27 14:36:01 +0200876 /* grandchild */
877 /* We will be the tracer process. Remember our new pid: */
878 strace_tracer_pid = getpid();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000879 }
880
Roland McGrath02203312007-06-11 22:06:31 +0000881 for (tcbi = 0; tcbi < tcbtabsize; tcbi++) {
882 tcp = tcbtab[tcbi];
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200883
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100884 if (!(tcp->flags & TCB_INUSE))
885 continue;
886
Denys Vlasenkod116a732011-09-05 14:01:33 +0200887 /* Is this a process we should attach to, but not yet attached? */
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100888 if (tcp->flags & TCB_ATTACHED)
889 continue; /* no, we already attached it */
Denys Vlasenkod116a732011-09-05 14:01:33 +0200890
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000891 if (followfork && !daemonized_tracer) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000892 char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3];
Roland McGrath02203312007-06-11 22:06:31 +0000893 DIR *dir;
894
895 sprintf(procdir, "/proc/%d/task", tcp->pid);
896 dir = opendir(procdir);
897 if (dir != NULL) {
898 unsigned int ntid = 0, nerr = 0;
899 struct dirent *de;
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200900
Roland McGrath02203312007-06-11 22:06:31 +0000901 while ((de = readdir(dir)) != NULL) {
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200902 struct tcb *cur_tcp;
903 int tid;
904
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000905 if (de->d_fileno == 0)
Roland McGrath02203312007-06-11 22:06:31 +0000906 continue;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000907 /* we trust /proc filesystem */
Roland McGrath02203312007-06-11 22:06:31 +0000908 tid = atoi(de->d_name);
909 if (tid <= 0)
910 continue;
911 ++ntid;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100912 if (ptrace_attach_or_seize(tid) < 0) {
Roland McGrath02203312007-06-11 22:06:31 +0000913 ++nerr;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100914 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200915 fprintf(stderr, "attach to pid %d failed\n", tid);
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200916 continue;
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200917 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100918 if (debug_flag)
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200919 fprintf(stderr, "attach to pid %d succeeded\n", tid);
920 cur_tcp = tcp;
921 if (tid != tcp->pid)
922 cur_tcp = alloctcb(tid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100923 cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100924 newoutf(cur_tcp);
Roland McGrath02203312007-06-11 22:06:31 +0000925 }
926 closedir(dir);
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200927 if (interactive) {
928 sigprocmask(SIG_SETMASK, &empty_set, NULL);
929 if (interrupted)
930 goto ret;
931 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
932 }
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000933 ntid -= nerr;
934 if (ntid == 0) {
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000935 perror_msg("%s", "attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +0000936 droptcb(tcp);
937 continue;
938 }
939 if (!qflag) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000940 fprintf(stderr, ntid > 1
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100941? "Process %u attached with %u threads\n"
942: "Process %u attached\n",
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200943 tcp->pid, ntid);
Roland McGrath02203312007-06-11 22:06:31 +0000944 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100945 if (!(tcp->flags & TCB_ATTACHED)) {
Denys Vlasenkof88837a2011-09-05 14:05:46 +0200946 /* -p PID, we failed to attach to PID itself
947 * but did attach to some of its sibling threads.
948 * Drop PID's tcp.
949 */
950 droptcb(tcp);
951 }
Roland McGrath02203312007-06-11 22:06:31 +0000952 continue;
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000953 } /* if (opendir worked) */
954 } /* if (-f) */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100955 if (ptrace_attach_or_seize(tcp->pid) < 0) {
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000956 perror_msg("%s", "attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +0000957 droptcb(tcp);
958 continue;
959 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100960 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100961 newoutf(tcp);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100962 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200963 fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000964
965 if (daemonized_tracer) {
966 /*
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000967 * Make parent go away.
968 * Also makes grandparent's wait() unblock.
969 */
970 kill(getppid(), SIGKILL);
971 }
972
Roland McGrath02203312007-06-11 22:06:31 +0000973 if (!qflag)
974 fprintf(stderr,
Denys Vlasenko9c3861d2012-03-16 15:21:49 +0100975 "Process %u attached\n",
Roland McGrath02203312007-06-11 22:06:31 +0000976 tcp->pid);
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200977 } /* for each tcbtab[] */
Roland McGrath02203312007-06-11 22:06:31 +0000978
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200979 ret:
Roland McGrath02203312007-06-11 22:06:31 +0000980 if (interactive)
981 sigprocmask(SIG_SETMASK, &empty_set, NULL);
982}
983
Denys Vlasenkof909c8d2013-02-19 16:30:31 +0100984/* Stack-o-phobic exec helper, in the hope to work around
985 * NOMMU + "daemonized tracer" difficulty.
986 */
987struct exec_params {
988 int fd_to_close;
989 uid_t run_euid;
990 gid_t run_egid;
991 char **argv;
992 char *pathname;
993};
994static struct exec_params params_for_tracee;
995static void __attribute__ ((noinline, noreturn))
996exec_or_die(void)
997{
998 struct exec_params *params = &params_for_tracee;
999
1000 if (params->fd_to_close >= 0)
1001 close(params->fd_to_close);
1002 if (!daemonized_tracer && !use_seize) {
1003 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) {
1004 perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)");
1005 }
1006 }
1007
1008 if (username != NULL) {
1009 /*
1010 * It is important to set groups before we
1011 * lose privileges on setuid.
1012 */
1013 if (initgroups(username, run_gid) < 0) {
1014 perror_msg_and_die("initgroups");
1015 }
1016 if (setregid(run_gid, params->run_egid) < 0) {
1017 perror_msg_and_die("setregid");
1018 }
1019 if (setreuid(run_uid, params->run_euid) < 0) {
1020 perror_msg_and_die("setreuid");
1021 }
1022 }
1023 else if (geteuid() != 0)
1024 if (setreuid(run_uid, run_uid) < 0) {
1025 perror_msg_and_die("setreuid");
1026 }
1027
1028 if (!daemonized_tracer) {
1029 /*
1030 * Induce a ptrace stop. Tracer (our parent)
1031 * will resume us with PTRACE_SYSCALL and display
1032 * the immediately following execve syscall.
1033 * Can't do this on NOMMU systems, we are after
1034 * vfork: parent is blocked, stopping would deadlock.
1035 */
1036 if (!NOMMU_SYSTEM)
1037 kill(getpid(), SIGSTOP);
1038 } else {
1039 alarm(3);
1040 /* we depend on SIGCHLD set to SIG_DFL by init code */
1041 /* if it happens to be SIG_IGN'ed, wait won't block */
1042 wait(NULL);
1043 alarm(0);
1044 }
1045
1046 execv(params->pathname, params->argv);
1047 perror_msg_and_die("exec");
1048}
1049
Roland McGrath02203312007-06-11 22:06:31 +00001050static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001051startup_child(char **argv)
Roland McGrath02203312007-06-11 22:06:31 +00001052{
1053 struct stat statbuf;
1054 const char *filename;
1055 char pathname[MAXPATHLEN];
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001056 int pid;
Roland McGrath02203312007-06-11 22:06:31 +00001057 struct tcb *tcp;
1058
1059 filename = argv[0];
1060 if (strchr(filename, '/')) {
1061 if (strlen(filename) > sizeof pathname - 1) {
1062 errno = ENAMETOOLONG;
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001063 perror_msg_and_die("exec");
Roland McGrath02203312007-06-11 22:06:31 +00001064 }
1065 strcpy(pathname, filename);
1066 }
1067#ifdef USE_DEBUGGING_EXEC
1068 /*
1069 * Debuggers customarily check the current directory
1070 * first regardless of the path but doing that gives
1071 * security geeks a panic attack.
1072 */
1073 else if (stat(filename, &statbuf) == 0)
1074 strcpy(pathname, filename);
1075#endif /* USE_DEBUGGING_EXEC */
1076 else {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +00001077 const char *path;
Roland McGrath02203312007-06-11 22:06:31 +00001078 int m, n, len;
1079
1080 for (path = getenv("PATH"); path && *path; path += m) {
Denys Vlasenko4f3df072012-01-29 22:38:35 +01001081 const char *colon = strchr(path, ':');
1082 if (colon) {
1083 n = colon - path;
Roland McGrath02203312007-06-11 22:06:31 +00001084 m = n + 1;
1085 }
1086 else
1087 m = n = strlen(path);
1088 if (n == 0) {
1089 if (!getcwd(pathname, MAXPATHLEN))
1090 continue;
1091 len = strlen(pathname);
1092 }
1093 else if (n > sizeof pathname - 1)
1094 continue;
1095 else {
1096 strncpy(pathname, path, n);
1097 len = n;
1098 }
1099 if (len && pathname[len - 1] != '/')
1100 pathname[len++] = '/';
1101 strcpy(pathname + len, filename);
1102 if (stat(pathname, &statbuf) == 0 &&
1103 /* Accept only regular files
1104 with some execute bits set.
1105 XXX not perfect, might still fail */
1106 S_ISREG(statbuf.st_mode) &&
1107 (statbuf.st_mode & 0111))
1108 break;
1109 }
1110 }
1111 if (stat(pathname, &statbuf) < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001112 perror_msg_and_die("Can't stat '%s'", filename);
Roland McGrath02203312007-06-11 22:06:31 +00001113 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001114
1115 params_for_tracee.fd_to_close = (shared_log != stderr) ? fileno(shared_log) : -1;
1116 params_for_tracee.run_euid = (statbuf.st_mode & S_ISUID) ? statbuf.st_uid : run_uid;
1117 params_for_tracee.run_egid = (statbuf.st_mode & S_ISGID) ? statbuf.st_gid : run_gid;
1118 params_for_tracee.argv = argv;
1119 /*
1120 * On NOMMU, can be safely freed only after execve in tracee.
1121 * It's hard to know when that happens, so we just leak it.
1122 */
1123 params_for_tracee.pathname = strdup(pathname);
1124
Dmitry V. Levina6809652008-11-10 17:14:58 +00001125 strace_child = pid = fork();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001126 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001127 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001128 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001129 if ((pid != 0 && daemonized_tracer)
1130 || (pid == 0 && !daemonized_tracer)
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001131 ) {
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001132 /* We are to become the tracee. Two cases:
1133 * -D: we are parent
1134 * not -D: we are child
1135 */
1136 exec_or_die();
Roland McGrath02203312007-06-11 22:06:31 +00001137 }
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001138
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001139 /* We are the tracer */
Denys Vlasenko75422762011-05-27 14:36:01 +02001140
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001141 if (!daemonized_tracer) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001142 if (!use_seize) {
1143 /* child did PTRACE_TRACEME, nothing to do in parent */
1144 } else {
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001145 if (!NOMMU_SYSTEM) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001146 /* Wait until child stopped itself */
1147 int status;
1148 while (waitpid(pid, &status, WSTOPPED) < 0) {
1149 if (errno == EINTR)
1150 continue;
1151 perror_msg_and_die("waitpid");
1152 }
1153 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001154 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001155 perror_msg_and_die("Unexpected wait status %x", status);
1156 }
1157 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001158 /* Else: NOMMU case, we have no way to sync.
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001159 * Just attach to it as soon as possible.
1160 * This means that we may miss a few first syscalls...
1161 */
1162
1163 if (ptrace_attach_or_seize(pid)) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001164 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001165 perror_msg_and_die("Can't attach to %d", pid);
1166 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001167 if (!NOMMU_SYSTEM)
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001168 kill(pid, SIGCONT);
1169 }
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001170 tcp = alloctcb(pid);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001171 if (!NOMMU_SYSTEM)
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001172 tcp->flags |= TCB_ATTACHED | TCB_STRACE_CHILD | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001173 else
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001174 tcp->flags |= TCB_ATTACHED | TCB_STRACE_CHILD | TCB_STARTUP;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001175 newoutf(tcp);
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001176 }
1177 else {
1178 /* With -D, *we* are child here, IOW: different pid. Fetch it: */
1179 strace_tracer_pid = getpid();
1180 /* The tracee is our parent: */
1181 pid = getppid();
Denys Vlasenkof2025022012-03-09 15:29:45 +01001182 alloctcb(pid);
1183 /* attaching will be done later, by startup_attach */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001184 /* note: we don't do newoutf(tcp) here either! */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001185
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001186 /* NOMMU BUG! -D mode is active, we (child) return,
1187 * and we will scribble over parent's stack!
1188 * When parent later unpauses, it segfaults.
1189 *
1190 * We work around it
1191 * (1) by declaring exec_or_die() NORETURN,
1192 * hopefully compiler will just jump to it
1193 * instead of call (won't push anything to stack),
1194 * (2) by trying very hard in exec_or_die()
1195 * to not use any stack,
1196 * (3) having a really big (MAXPATHLEN) stack object
1197 * in this function, which creates a "buffer" between
1198 * child's and parent's stack pointers.
1199 * This may save us if (1) and (2) failed
1200 * and compiler decided to use stack in exec_or_die() anyway
1201 * (happens on i386 because of stack parameter passing).
1202 */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001203 }
Roland McGrath02203312007-06-11 22:06:31 +00001204}
1205
Wang Chaob13c0de2010-11-12 17:25:19 +08001206/*
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001207 * Test whether the kernel support PTRACE_O_TRACECLONE et al options.
Wang Chaob13c0de2010-11-12 17:25:19 +08001208 * First fork a new child, call ptrace with PTRACE_SETOPTIONS on it,
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001209 * and then see which options are supported by the kernel.
Wang Chaob13c0de2010-11-12 17:25:19 +08001210 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001211static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001212test_ptrace_setoptions_followfork(void)
Wang Chaob13c0de2010-11-12 17:25:19 +08001213{
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001214 int pid, expected_grandchild = 0, found_grandchild = 0;
1215 const unsigned int test_options = PTRACE_O_TRACECLONE |
1216 PTRACE_O_TRACEFORK |
1217 PTRACE_O_TRACEVFORK;
Wang Chaob13c0de2010-11-12 17:25:19 +08001218
Denys Vlasenko5d645812011-08-20 12:48:18 +02001219 pid = fork();
1220 if (pid < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001221 perror_msg_and_die("fork");
Denys Vlasenko5d645812011-08-20 12:48:18 +02001222 if (pid == 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001223 pid = getpid();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001224 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001225 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1226 __func__);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001227 kill_save_errno(pid, SIGSTOP);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001228 if (fork() < 0)
1229 perror_msg_and_die("fork");
1230 _exit(0);
Wang Chaob13c0de2010-11-12 17:25:19 +08001231 }
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001232
1233 while (1) {
1234 int status, tracee_pid;
1235
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001236 errno = 0;
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001237 tracee_pid = wait(&status);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001238 if (tracee_pid <= 0) {
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001239 if (errno == EINTR)
1240 continue;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001241 if (errno == ECHILD)
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001242 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001243 kill_save_errno(pid, SIGKILL);
1244 perror_msg_and_die("%s: unexpected wait result %d",
1245 __func__, tracee_pid);
1246 }
1247 if (WIFEXITED(status)) {
1248 if (WEXITSTATUS(status)) {
1249 if (tracee_pid != pid)
1250 kill_save_errno(pid, SIGKILL);
1251 error_msg_and_die("%s: unexpected exit status %u",
1252 __func__, WEXITSTATUS(status));
1253 }
1254 continue;
1255 }
1256 if (WIFSIGNALED(status)) {
1257 if (tracee_pid != pid)
1258 kill_save_errno(pid, SIGKILL);
1259 error_msg_and_die("%s: unexpected signal %u",
1260 __func__, WTERMSIG(status));
1261 }
1262 if (!WIFSTOPPED(status)) {
1263 if (tracee_pid != pid)
1264 kill_save_errno(tracee_pid, SIGKILL);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001265 kill_save_errno(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001266 error_msg_and_die("%s: unexpected wait status %x",
1267 __func__, status);
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001268 }
1269 if (tracee_pid != pid) {
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001270 found_grandchild = tracee_pid;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001271 if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0) {
1272 kill_save_errno(tracee_pid, SIGKILL);
1273 kill_save_errno(pid, SIGKILL);
1274 perror_msg_and_die("PTRACE_CONT doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001275 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001276 continue;
1277 }
1278 switch (WSTOPSIG(status)) {
1279 case SIGSTOP:
1280 if (ptrace(PTRACE_SETOPTIONS, pid, 0, test_options) < 0
1281 && errno != EINVAL && errno != EIO)
1282 perror_msg("PTRACE_SETOPTIONS");
1283 break;
1284 case SIGTRAP:
1285 if (status >> 16 == PTRACE_EVENT_FORK) {
1286 long msg = 0;
1287
1288 if (ptrace(PTRACE_GETEVENTMSG, pid,
1289 NULL, (long) &msg) == 0)
1290 expected_grandchild = msg;
1291 }
1292 break;
1293 }
1294 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) {
1295 kill_save_errno(pid, SIGKILL);
1296 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001297 }
1298 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001299 if (expected_grandchild && expected_grandchild == found_grandchild) {
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001300 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001301 if (debug_flag)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001302 fprintf(stderr, "ptrace_setoptions = %#x\n",
1303 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001304 return 0;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001305 }
1306 error_msg("Test for PTRACE_O_TRACECLONE failed, "
1307 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001308 return 1;
Wang Chaob13c0de2010-11-12 17:25:19 +08001309}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001310
1311/*
1312 * Test whether the kernel support PTRACE_O_TRACESYSGOOD.
1313 * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it,
1314 * and then see whether it will stop with (SIGTRAP | 0x80).
1315 *
1316 * Use of this option enables correct handling of user-generated SIGTRAPs,
1317 * and SIGTRAPs generated by special instructions such as int3 on x86:
1318 * _start: .globl _start
1319 * int3
1320 * movl $42, %ebx
1321 * movl $1, %eax
1322 * int $0x80
1323 * (compile with: "gcc -nostartfiles -nostdlib -o int3 int3.S")
1324 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001325static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001326test_ptrace_setoptions_for_all(void)
1327{
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001328 const unsigned int test_options = PTRACE_O_TRACESYSGOOD |
1329 PTRACE_O_TRACEEXEC;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001330 int pid;
1331 int it_worked = 0;
1332
Mike Frysinger7ff5ed92012-04-05 01:52:25 -04001333 /* this fork test doesn't work on no-mmu systems */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001334 if (NOMMU_SYSTEM)
1335 return 0; /* be bold, and pretend that test succeeded */
Mike Frysinger7ff5ed92012-04-05 01:52:25 -04001336
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001337 pid = fork();
1338 if (pid < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001339 perror_msg_and_die("fork");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001340
1341 if (pid == 0) {
1342 pid = getpid();
1343 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001344 /* Note: exits with exitcode 1 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001345 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1346 __func__);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001347 kill(pid, SIGSTOP);
1348 _exit(0); /* parent should see entry into this syscall */
1349 }
1350
1351 while (1) {
1352 int status, tracee_pid;
1353
1354 errno = 0;
1355 tracee_pid = wait(&status);
1356 if (tracee_pid <= 0) {
1357 if (errno == EINTR)
1358 continue;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001359 kill_save_errno(pid, SIGKILL);
1360 perror_msg_and_die("%s: unexpected wait result %d",
1361 __func__, tracee_pid);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001362 }
Denys Vlasenko75422762011-05-27 14:36:01 +02001363 if (WIFEXITED(status)) {
1364 if (WEXITSTATUS(status) == 0)
1365 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001366 error_msg_and_die("%s: unexpected exit status %u",
1367 __func__, WEXITSTATUS(status));
1368 }
1369 if (WIFSIGNALED(status)) {
1370 error_msg_and_die("%s: unexpected signal %u",
1371 __func__, WTERMSIG(status));
Denys Vlasenko75422762011-05-27 14:36:01 +02001372 }
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001373 if (!WIFSTOPPED(status)) {
1374 kill(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001375 error_msg_and_die("%s: unexpected wait status %x",
1376 __func__, status);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001377 }
1378 if (WSTOPSIG(status) == SIGSTOP) {
1379 /*
1380 * We don't check "options aren't accepted" error.
1381 * If it happens, we'll never get (SIGTRAP | 0x80),
1382 * and thus will decide to not use the option.
1383 * IOW: the outcome of the test will be correct.
1384 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001385 if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0
1386 && errno != EINVAL && errno != EIO)
1387 perror_msg("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001388 }
1389 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
1390 it_worked = 1;
1391 }
1392 if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001393 kill_save_errno(pid, SIGKILL);
Denys Vlasenko75422762011-05-27 14:36:01 +02001394 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001395 }
1396 }
1397
1398 if (it_worked) {
Denys Vlasenko75422762011-05-27 14:36:01 +02001399 syscall_trap_sig = (SIGTRAP | 0x80);
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001400 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001401 if (debug_flag)
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001402 fprintf(stderr, "ptrace_setoptions = %#x\n",
1403 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001404 return 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001405 }
1406
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001407 error_msg("Test for PTRACE_O_TRACESYSGOOD failed, "
1408 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001409 return 1;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001410}
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001411
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001412#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001413static void
1414test_ptrace_seize(void)
1415{
1416 int pid;
1417
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001418 /* this fork test doesn't work on no-mmu systems */
1419 if (NOMMU_SYSTEM) {
1420 post_attach_sigstop = 0; /* this sets use_seize to 1 */
1421 return;
1422 }
1423
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001424 pid = fork();
1425 if (pid < 0)
1426 perror_msg_and_die("fork");
1427
1428 if (pid == 0) {
1429 pause();
1430 _exit(0);
1431 }
1432
1433 /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After
1434 * attaching tracee continues to run unless a trap condition occurs.
1435 * PTRACE_SEIZE doesn't affect signal or group stop state.
1436 */
Denys Vlasenko26bc0602012-07-10 16:36:32 +02001437 if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001438 post_attach_sigstop = 0; /* this sets use_seize to 1 */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001439 } else if (debug_flag) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001440 fprintf(stderr, "PTRACE_SEIZE doesn't work\n");
1441 }
1442
1443 kill(pid, SIGKILL);
1444
1445 while (1) {
1446 int status, tracee_pid;
1447
1448 errno = 0;
1449 tracee_pid = waitpid(pid, &status, 0);
1450 if (tracee_pid <= 0) {
1451 if (errno == EINTR)
1452 continue;
1453 perror_msg_and_die("%s: unexpected wait result %d",
1454 __func__, tracee_pid);
1455 }
1456 if (WIFSIGNALED(status)) {
1457 return;
1458 }
1459 error_msg_and_die("%s: unexpected wait status %x",
1460 __func__, status);
1461 }
1462}
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001463#else /* !USE_SEIZE */
1464# define test_ptrace_seize() ((void)0)
1465#endif
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001466
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001467static unsigned
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001468get_os_release(void)
1469{
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001470 unsigned rel;
1471 const char *p;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001472 struct utsname u;
1473 if (uname(&u) < 0)
1474 perror_msg_and_die("uname");
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001475 /* u.release has this form: "3.2.9[-some-garbage]" */
1476 rel = 0;
1477 p = u.release;
1478 for (;;) {
1479 if (!(*p >= '0' && *p <= '9'))
1480 error_msg_and_die("Bad OS release string: '%s'", u.release);
1481 /* Note: this open-codes KERNEL_VERSION(): */
1482 rel = (rel << 8) | atoi(p);
1483 if (rel >= KERNEL_VERSION(1,0,0))
1484 break;
1485 while (*p >= '0' && *p <= '9')
1486 p++;
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001487 if (*p != '.') {
1488 if (rel >= KERNEL_VERSION(0,1,0)) {
1489 /* "X.Y-something" means "X.Y.0" */
1490 rel <<= 8;
1491 break;
1492 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001493 error_msg_and_die("Bad OS release string: '%s'", u.release);
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001494 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001495 p++;
1496 }
1497 return rel;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001498}
1499
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01001500/*
1501 * Initialization part of main() was eating much stack (~0.5k),
1502 * which was unused after init.
1503 * We can reuse it if we move init code into a separate function.
1504 *
1505 * Don't want main() to inline us and defeat the reason
1506 * we have a separate function.
1507 */
1508static void __attribute__ ((noinline))
1509init(int argc, char *argv[])
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001510{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001511 struct tcb *tcp;
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001512 int c, i;
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001513 int optF = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001514 struct sigaction sa;
1515
Dmitry V. Levin08b623e2007-10-08 21:04:41 +00001516 progname = argv[0] ? argv[0] : "strace";
1517
Denys Vlasenkoa5090542012-03-15 17:27:49 +01001518 /* Make sure SIGCHLD has the default action so that waitpid
1519 definitely works without losing track of children. The user
1520 should not have given us a bogus state to inherit, but he might
1521 have. Arguably we should detect SIG_IGN here and pass it on
1522 to children, but probably noone really needs that. */
1523 signal(SIGCHLD, SIG_DFL);
1524
Denys Vlasenko75422762011-05-27 14:36:01 +02001525 strace_tracer_pid = getpid();
1526
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001527 os_release = get_os_release();
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001528
Roland McGrathee9d4352002-12-18 04:16:10 +00001529 /* Allocate the initial tcbtab. */
1530 tcbtabsize = argc; /* Surely enough for all -p args. */
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001531 tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0]));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001532 if (!tcbtab)
1533 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001534 tcp = calloc(tcbtabsize, sizeof(*tcp));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001535 if (!tcp)
1536 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001537 for (c = 0; c < tcbtabsize; c++)
1538 tcbtab[c] = tcp++;
Roland McGrathee9d4352002-12-18 04:16:10 +00001539
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001540 shared_log = stderr;
Roland McGrath138c6a32006-01-12 09:50:49 +00001541 set_sortby(DEFAULT_SORTBY);
1542 set_personality(DEFAULT_PERSONALITY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001543 qualify("trace=all");
1544 qualify("abbrev=all");
1545 qualify("verbose=all");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001546#if DEFAULT_QUAL_FLAGS != (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
1547# error Bug in DEFAULT_QUAL_FLAGS
1548#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001549 qualify("signal=all");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001550 while ((c = getopt(argc, argv,
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001551 "+bcCdfFhiqrtTvVxyz"
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001552 "D"
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001553 "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001554 switch (c) {
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001555 case 'b':
1556 detach_on_execve = 1;
1557 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001558 case 'c':
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001559 if (cflag == CFLAG_BOTH) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001560 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001561 }
1562 cflag = CFLAG_ONLY_STATS;
1563 break;
1564 case 'C':
1565 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001566 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001567 }
1568 cflag = CFLAG_BOTH;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001569 break;
1570 case 'd':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001571 debug_flag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001572 break;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001573 case 'D':
1574 daemonized_tracer = 1;
1575 break;
Roland McGrath41c48222008-07-18 00:25:10 +00001576 case 'F':
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001577 optF = 1;
1578 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001579 case 'f':
1580 followfork++;
1581 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001582 case 'h':
1583 usage(stdout, 0);
1584 break;
1585 case 'i':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001586 iflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001587 break;
1588 case 'q':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001589 qflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001590 break;
1591 case 'r':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001592 rflag = 1;
1593 /* fall through to tflag++ */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001594 case 't':
1595 tflag++;
1596 break;
1597 case 'T':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001598 Tflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001599 break;
1600 case 'x':
1601 xflag++;
1602 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001603 case 'y':
1604 show_fd_path = 1;
1605 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001606 case 'v':
1607 qualify("abbrev=none");
1608 break;
1609 case 'V':
Roland McGrath9c9a2532003-02-20 02:56:29 +00001610 printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001611 exit(0);
1612 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001613 case 'z':
1614 not_failing_only = 1;
1615 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001616 case 'a':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001617 acolumn = string_to_uint(optarg);
Denys Vlasenko102ec492011-08-25 01:27:59 +02001618 if (acolumn < 0)
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001619 error_opt_arg(c, optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001620 break;
1621 case 'e':
1622 qualify(optarg);
1623 break;
1624 case 'o':
1625 outfname = strdup(optarg);
1626 break;
1627 case 'O':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001628 i = string_to_uint(optarg);
1629 if (i < 0)
1630 error_opt_arg(c, optarg);
1631 set_overhead(i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001632 break;
1633 case 'p':
Denys Vlasenkoe8172b72012-03-09 13:01:04 +01001634 process_opt_p_list(optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001635 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001636 case 'P':
1637 tracing_paths = 1;
1638 if (pathtrace_select(optarg)) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001639 error_msg_and_die("Failed to select path '%s'", optarg);
Grant Edwards8a082772011-04-07 20:25:40 +00001640 }
1641 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001642 case 's':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001643 i = string_to_uint(optarg);
1644 if (i < 0)
1645 error_opt_arg(c, optarg);
1646 max_strlen = i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001647 break;
1648 case 'S':
1649 set_sortby(optarg);
1650 break;
1651 case 'u':
1652 username = strdup(optarg);
1653 break;
Roland McGrathde6e5332003-01-24 04:31:23 +00001654 case 'E':
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001655 if (putenv(optarg) < 0)
1656 die_out_of_memory();
Roland McGrathde6e5332003-01-24 04:31:23 +00001657 break;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001658 case 'I':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001659 opt_intr = string_to_uint(optarg);
1660 if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS)
1661 error_opt_arg(c, optarg);
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001662 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001663 default:
1664 usage(stderr, 1);
1665 break;
1666 }
1667 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001668 argv += optind;
1669 /* argc -= optind; - no need, argc is not used below */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001670
Denys Vlasenko102ec492011-08-25 01:27:59 +02001671 acolumn_spaces = malloc(acolumn + 1);
1672 if (!acolumn_spaces)
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001673 die_out_of_memory();
Denys Vlasenko102ec492011-08-25 01:27:59 +02001674 memset(acolumn_spaces, ' ', acolumn);
1675 acolumn_spaces[acolumn] = '\0';
1676
Denys Vlasenko837399a2012-01-24 11:37:03 +01001677 /* Must have PROG [ARGS], or -p PID. Not both. */
Denys Vlasenkofd883382012-03-09 13:03:41 +01001678 if (!argv[0] == !nprocs)
Roland McGrathce0d1542003-11-11 21:24:23 +00001679 usage(stderr, 1);
1680
Denys Vlasenkofd883382012-03-09 13:03:41 +01001681 if (nprocs != 0 && daemonized_tracer) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001682 error_msg_and_die("-D and -p are mutually exclusive");
Wang Chaod322a4b2010-08-05 14:30:11 +08001683 }
1684
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001685 if (!followfork)
1686 followfork = optF;
1687
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001688 if (followfork >= 2 && cflag) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001689 error_msg_and_die("(-c or -C) and -ff are mutually exclusive");
Roland McGrathcb9def62006-04-25 07:48:03 +00001690 }
1691
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001692 /* See if they want to run as another user. */
1693 if (username != NULL) {
1694 struct passwd *pent;
1695
1696 if (getuid() != 0 || geteuid() != 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001697 error_msg_and_die("You must be root to use the -u option");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001698 }
Denys Vlasenko5d645812011-08-20 12:48:18 +02001699 pent = getpwnam(username);
1700 if (pent == NULL) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001701 error_msg_and_die("Cannot find user '%s'", username);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001702 }
1703 run_uid = pent->pw_uid;
1704 run_gid = pent->pw_gid;
1705 }
1706 else {
1707 run_uid = getuid();
1708 run_gid = getgid();
1709 }
1710
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001711 /*
1712 * On any reasonably recent Linux kernel (circa about 2.5.46)
1713 * need_fork_exec_workarounds should stay 0 after these tests:
1714 */
1715 /*need_fork_exec_workarounds = 0; - already is */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001716 if (followfork)
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001717 need_fork_exec_workarounds = test_ptrace_setoptions_followfork();
1718 need_fork_exec_workarounds |= test_ptrace_setoptions_for_all();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001719 test_ptrace_seize();
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001720
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001721 /* Check if they want to redirect the output. */
1722 if (outfname) {
Roland McGrath37b9a662003-11-07 02:26:54 +00001723 /* See if they want to pipe the output. */
1724 if (outfname[0] == '|' || outfname[0] == '!') {
1725 /*
1726 * We can't do the <outfname>.PID funny business
1727 * when using popen, so prohibit it.
1728 */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001729 if (followfork >= 2)
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001730 error_msg_and_die("Piping the output and -ff are mutually exclusive");
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001731 shared_log = strace_popen(outfname + 1);
Roland McGrath37b9a662003-11-07 02:26:54 +00001732 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001733 else if (followfork < 2)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001734 shared_log = strace_fopen(outfname);
Denys Vlasenko328bf252012-03-12 23:34:13 +01001735 } else {
1736 /* -ff without -o FILE is the same as single -f */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001737 if (followfork >= 2)
Denys Vlasenko328bf252012-03-12 23:34:13 +01001738 followfork = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001739 }
1740
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001741 if (!outfname || outfname[0] == '|' || outfname[0] == '!') {
Denys Vlasenkoa677da52012-01-24 11:31:51 +01001742 char *buf = malloc(BUFSIZ);
1743 if (!buf)
1744 die_out_of_memory();
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001745 setvbuf(shared_log, buf, _IOLBF, BUFSIZ);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001746 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001747 if (outfname && argv[0]) {
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001748 if (!opt_intr)
1749 opt_intr = INTR_NEVER;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001750 qflag = 1;
Roland McGrath36931052003-06-03 01:35:20 +00001751 }
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001752 if (!opt_intr)
1753 opt_intr = INTR_WHILE_WAIT;
Wang Chaob13c0de2010-11-12 17:25:19 +08001754
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001755 /* argv[0] -pPID -oFILE Default interactive setting
1756 * yes 0 0 INTR_WHILE_WAIT
1757 * no 1 0 INTR_WHILE_WAIT
1758 * yes 0 1 INTR_NEVER
1759 * no 1 1 INTR_WHILE_WAIT
Roland McGrath54cc1c82007-11-03 23:34:11 +00001760 */
1761
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001762 sigemptyset(&empty_set);
1763 sigemptyset(&blocked_set);
1764
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001765 /* startup_child() must be called before the signal handlers get
1766 * installed below as they are inherited into the spawned process.
1767 * Also we do not need to be protected by them as during interruption
1768 * in the startup_child() mode we kill the spawned process anyway.
1769 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001770 if (argv[0]) {
1771 skip_startup_execve = 1;
Denys Vlasenko837399a2012-01-24 11:37:03 +01001772 startup_child(argv);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001773 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001774
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001775 sa.sa_handler = SIG_IGN;
1776 sigemptyset(&sa.sa_mask);
1777 sa.sa_flags = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001778 sigaction(SIGTTOU, &sa, NULL); /* SIG_IGN */
1779 sigaction(SIGTTIN, &sa, NULL); /* SIG_IGN */
1780 if (opt_intr != INTR_ANYWHERE) {
1781 if (opt_intr == INTR_BLOCK_TSTP_TOO)
1782 sigaction(SIGTSTP, &sa, NULL); /* SIG_IGN */
1783 /*
1784 * In interactive mode (if no -o OUTFILE, or -p PID is used),
1785 * fatal signals are blocked while syscall stop is processed,
1786 * and acted on in between, when waiting for new syscall stops.
1787 * In non-interactive mode, signals are ignored.
1788 */
1789 if (opt_intr == INTR_WHILE_WAIT) {
1790 sigaddset(&blocked_set, SIGHUP);
1791 sigaddset(&blocked_set, SIGINT);
1792 sigaddset(&blocked_set, SIGQUIT);
1793 sigaddset(&blocked_set, SIGPIPE);
1794 sigaddset(&blocked_set, SIGTERM);
1795 sa.sa_handler = interrupt;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001796 }
1797 /* SIG_IGN, or set handler for these */
1798 sigaction(SIGHUP, &sa, NULL);
1799 sigaction(SIGINT, &sa, NULL);
1800 sigaction(SIGQUIT, &sa, NULL);
1801 sigaction(SIGPIPE, &sa, NULL);
1802 sigaction(SIGTERM, &sa, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001803 }
Denys Vlasenkofd883382012-03-09 13:03:41 +01001804 if (nprocs != 0 || daemonized_tracer)
Roland McGrath02203312007-06-11 22:06:31 +00001805 startup_attach();
Roland McGrath02203312007-06-11 22:06:31 +00001806
Denys Vlasenkofd883382012-03-09 13:03:41 +01001807 /* Do we want pids printed in our -o OUTFILE?
1808 * -ff: no (every pid has its own file); or
1809 * -f: yes (there can be more pids in the future); or
1810 * -p PID1,PID2: yes (there are already more than one pid)
1811 */
1812 print_pid_pfx = (outfname && followfork < 2 && (followfork == 1 || nprocs > 1));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001813}
1814
Denys Vlasenkoeebb04d2012-01-27 15:24:48 +01001815static struct tcb *
Roland McGrath54e931f2010-09-14 18:59:20 -07001816pid2tcb(int pid)
1817{
1818 int i;
1819
1820 if (pid <= 0)
1821 return NULL;
1822
1823 for (i = 0; i < tcbtabsize; i++) {
1824 struct tcb *tcp = tcbtab[i];
1825 if (tcp->pid == pid && (tcp->flags & TCB_INUSE))
1826 return tcp;
1827 }
1828
1829 return NULL;
1830}
1831
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001832static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001833cleanup(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001834{
1835 int i;
1836 struct tcb *tcp;
Denys Vlasenko35218842012-01-29 21:17:56 +01001837 int fatal_sig;
1838
1839 /* 'interrupted' is a volatile object, fetch it only once */
1840 fatal_sig = interrupted;
1841 if (!fatal_sig)
1842 fatal_sig = SIGTERM;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001843
Roland McGrathee9d4352002-12-18 04:16:10 +00001844 for (i = 0; i < tcbtabsize; i++) {
1845 tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001846 if (!(tcp->flags & TCB_INUSE))
1847 continue;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001848 if (debug_flag)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001849 fprintf(stderr,
1850 "cleanup: looking at pid %u\n", tcp->pid);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001851 if (tcp->flags & TCB_STRACE_CHILD) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001852 kill(tcp->pid, SIGCONT);
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001853 kill(tcp->pid, fatal_sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001854 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001855 detach(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001856 }
1857 if (cflag)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001858 call_summary(shared_log);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001859}
1860
1861static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001862interrupt(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001863{
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001864 interrupted = sig;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001865}
1866
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001867static int
Denys Vlasenko12014262011-05-30 14:00:14 +02001868trace(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001869{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001870 struct rusage ru;
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001871 struct rusage *rup = cflag ? &ru : NULL;
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001872#ifdef __WALL
Roland McGratheb9e2e82009-06-02 16:49:22 -07001873 static int wait4_options = __WALL;
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001874#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001875
Roland McGratheb9e2e82009-06-02 16:49:22 -07001876 while (nprocs != 0) {
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001877 int pid;
1878 int wait_errno;
1879 int status, sig;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001880 int stopped;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001881 struct tcb *tcp;
1882 unsigned event;
1883
Denys Vlasenko222713a2009-03-17 14:29:59 +00001884 if (interrupted)
Roland McGratheb9e2e82009-06-02 16:49:22 -07001885 return 0;
1886 if (interactive)
1887 sigprocmask(SIG_SETMASK, &empty_set, NULL);
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001888#ifdef __WALL
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001889 pid = wait4(-1, &status, wait4_options, rup);
Roland McGrath5bc05552002-12-17 04:50:47 +00001890 if (pid < 0 && (wait4_options & __WALL) && errno == EINVAL) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001891 /* this kernel does not support __WALL */
1892 wait4_options &= ~__WALL;
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001893 pid = wait4(-1, &status, wait4_options, rup);
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001894 }
Roland McGrath5bc05552002-12-17 04:50:47 +00001895 if (pid < 0 && !(wait4_options & __WALL) && errno == ECHILD) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001896 /* most likely a "cloned" process */
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001897 pid = wait4(-1, &status, __WCLONE, rup);
1898 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001899 perror_msg("wait4(__WCLONE) failed");
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001900 }
1901 }
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001902#else
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001903 pid = wait4(-1, &status, 0, rup);
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001904#endif /* __WALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001905 wait_errno = errno;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001906 if (interactive)
1907 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001908
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001909 if (pid < 0) {
Roland McGratheb9e2e82009-06-02 16:49:22 -07001910 switch (wait_errno) {
1911 case EINTR:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001912 continue;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001913 case ECHILD:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001914 /*
1915 * We would like to verify this case
1916 * but sometimes a race in Solbourne's
1917 * version of SunOS sometimes reports
1918 * ECHILD before sending us SIGCHILD.
1919 */
Roland McGratheb9e2e82009-06-02 16:49:22 -07001920 return 0;
1921 default:
1922 errno = wait_errno;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001923 perror_msg("wait");
Roland McGratheb9e2e82009-06-02 16:49:22 -07001924 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001925 }
1926 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001927 if (pid == popen_pid) {
1928 if (WIFEXITED(status) || WIFSIGNALED(status))
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001929 popen_pid = 0;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001930 continue;
1931 }
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001932
1933 event = ((unsigned)status >> 16);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001934 if (debug_flag) {
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001935 char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko67559ad2012-03-13 12:05:27 +01001936 char evbuf[sizeof(",PTRACE_EVENT_?? (%u)") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001937 strcpy(buf, "???");
1938 if (WIFSIGNALED(status))
1939#ifdef WCOREDUMP
1940 sprintf(buf, "WIFSIGNALED,%ssig=%s",
1941 WCOREDUMP(status) ? "core," : "",
1942 signame(WTERMSIG(status)));
1943#else
1944 sprintf(buf, "WIFSIGNALED,sig=%s",
1945 signame(WTERMSIG(status)));
1946#endif
1947 if (WIFEXITED(status))
1948 sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status));
1949 if (WIFSTOPPED(status))
1950 sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status)));
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02001951#ifdef WIFCONTINUED
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001952 if (WIFCONTINUED(status))
1953 strcpy(buf, "WIFCONTINUED");
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02001954#endif
Denys Vlasenko67559ad2012-03-13 12:05:27 +01001955 evbuf[0] = '\0';
1956 if (event != 0) {
1957 static const char *const event_names[] = {
1958 [PTRACE_EVENT_CLONE] = "CLONE",
1959 [PTRACE_EVENT_FORK] = "FORK",
1960 [PTRACE_EVENT_VFORK] = "VFORK",
1961 [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE",
1962 [PTRACE_EVENT_EXEC] = "EXEC",
1963 [PTRACE_EVENT_EXIT] = "EXIT",
1964 };
1965 const char *e;
1966 if (event < ARRAY_SIZE(event_names))
1967 e = event_names[event];
1968 else {
1969 sprintf(buf, "?? (%u)", event);
1970 e = buf;
1971 }
1972 sprintf(evbuf, ",PTRACE_EVENT_%s", e);
1973 }
1974 fprintf(stderr, " [wait(0x%04x) = %u] %s%s\n", status, pid, buf, evbuf);
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001975 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001976
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001977 /* Look up 'pid' in our table. */
Denys Vlasenko5d645812011-08-20 12:48:18 +02001978 tcp = pid2tcb(pid);
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001979
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01001980 if (!tcp) {
Roland McGrath41c48222008-07-18 00:25:10 +00001981 if (followfork) {
Denys Vlasenko418d66a2009-01-17 01:52:54 +00001982 tcp = alloctcb(pid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001983 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001984 newoutf(tcp);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001985 if (!qflag)
Denys Vlasenko833fb132011-08-17 11:30:56 +02001986 fprintf(stderr, "Process %d attached\n",
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001987 pid);
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01001988 } else {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001989 /* This can happen if a clone call used
1990 CLONE_PTRACE itself. */
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001991 if (WIFSTOPPED(status))
Denys Vlasenko97c503f2012-03-09 15:11:21 +01001992 ptrace(PTRACE_CONT, pid, (char *) 0, 0);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001993 error_msg_and_die("Unknown pid: %u", pid);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001994 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001995 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001996
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001997 clear_regs();
1998 if (WIFSTOPPED(status))
1999 get_regs(pid);
2000
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002001 /* Under Linux, execve changes pid to thread leader's pid,
2002 * and we see this changed pid on EVENT_EXEC and later,
2003 * execve sysexit. Leader "disappears" without exit
2004 * notification. Let user know that, drop leader's tcb,
2005 * and fix up pid in execve thread's tcb.
2006 * Effectively, execve thread's tcb replaces leader's tcb.
2007 *
2008 * BTW, leader is 'stuck undead' (doesn't report WIFEXITED
2009 * on exit syscall) in multithreaded programs exactly
2010 * in order to handle this case.
2011 *
2012 * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0.
2013 * On 2.6 and earlier, it can return garbage.
2014 */
2015 if (event == PTRACE_EVENT_EXEC && os_release >= KERNEL_VERSION(3,0,0)) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002016 FILE *fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002017 struct tcb *execve_thread;
2018 long old_pid = 0;
2019
2020 if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) < 0)
2021 goto dont_switch_tcbs;
2022 if (old_pid <= 0 || old_pid == pid)
2023 goto dont_switch_tcbs;
2024 execve_thread = pid2tcb(old_pid);
2025 /* It should be !NULL, but I feel paranoid */
2026 if (!execve_thread)
2027 goto dont_switch_tcbs;
2028
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002029 if (execve_thread->curcol != 0) {
2030 /*
2031 * One case we are here is -ff:
2032 * try "strace -oLOG -ff test/threaded_execve"
2033 */
2034 fprintf(execve_thread->outf, " <pid changed to %d ...>\n", pid);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002035 /*execve_thread->curcol = 0; - no need, see code below */
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002036 }
2037 /* Swap output FILEs (needed for -ff) */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002038 fp = execve_thread->outf;
2039 execve_thread->outf = tcp->outf;
2040 tcp->outf = fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002041 /* And their column positions */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002042 execve_thread->curcol = tcp->curcol;
2043 tcp->curcol = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002044 /* Drop leader, but close execve'd thread outfile (if -ff) */
2045 droptcb(tcp);
2046 /* Switch to the thread, reusing leader's outfile and pid */
2047 tcp = execve_thread;
2048 tcp->pid = pid;
2049 if (cflag != CFLAG_ONLY_STATS) {
2050 printleader(tcp);
2051 tprintf("+++ superseded by execve in pid %lu +++\n", old_pid);
2052 line_ended();
2053 tcp->flags |= TCB_REPRINT;
2054 }
2055 }
2056 dont_switch_tcbs:
2057
2058 if (event == PTRACE_EVENT_EXEC && detach_on_execve) {
2059 if (!skip_startup_execve)
2060 detach(tcp);
2061 /* This was initial execve for "strace PROG". Skip. */
2062 skip_startup_execve = 0;
2063 }
2064
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002065 /* Set current output file */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002066 current_tcp = tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002067
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002068 if (cflag) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002069 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2070 tcp->stime = ru.ru_stime;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002071 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002072
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002073 if (WIFSIGNALED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002074 if (pid == strace_child)
2075 exit_code = 0x100 | WTERMSIG(status);
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002076 if (cflag != CFLAG_ONLY_STATS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002077 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
2078 printleader(tcp);
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002079#ifdef WCOREDUMP
Denys Vlasenko000b6012012-01-28 01:25:03 +01002080 tprintf("+++ killed by %s %s+++\n",
Roland McGrath2efe8792004-01-13 09:59:45 +00002081 signame(WTERMSIG(status)),
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002082 WCOREDUMP(status) ? "(core dumped) " : "");
2083#else
Denys Vlasenko000b6012012-01-28 01:25:03 +01002084 tprintf("+++ killed by %s +++\n",
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002085 signame(WTERMSIG(status)));
Roland McGrath2efe8792004-01-13 09:59:45 +00002086#endif
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002087 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002088 }
2089 droptcb(tcp);
2090 continue;
2091 }
2092 if (WIFEXITED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002093 if (pid == strace_child)
2094 exit_code = WEXITSTATUS(status);
Denys Vlasenkob5e09082012-03-21 14:27:40 +01002095 if (cflag != CFLAG_ONLY_STATS) {
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002096 printleader(tcp);
Denys Vlasenko000b6012012-01-28 01:25:03 +01002097 tprintf("+++ exited with %d +++\n", WEXITSTATUS(status));
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002098 line_ended();
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002099 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002100 droptcb(tcp);
2101 continue;
2102 }
2103 if (!WIFSTOPPED(status)) {
2104 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
2105 droptcb(tcp);
2106 continue;
2107 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002108
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002109 /* Is this the very first time we see this tracee stopped? */
2110 if (tcp->flags & TCB_STARTUP) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002111 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002112 fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n", tcp->pid);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002113 tcp->flags &= ~TCB_STARTUP;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002114 if (tcp->flags & TCB_BPTSET) {
Roland McGrath02203312007-06-11 22:06:31 +00002115 /*
2116 * One example is a breakpoint inherited from
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002117 * parent through fork().
Roland McGrath02203312007-06-11 22:06:31 +00002118 */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002119 if (clearbpt(tcp) < 0) {
2120 /* Pretty fatal */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002121 droptcb(tcp);
2122 cleanup();
2123 return -1;
2124 }
2125 }
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002126 if (ptrace_setoptions) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002127 if (debug_flag)
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002128 fprintf(stderr, "setting opts %x on pid %d\n", ptrace_setoptions, tcp->pid);
2129 if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) {
2130 if (errno != ESRCH) {
2131 /* Should never happen, really */
2132 perror_msg_and_die("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02002133 }
2134 }
2135 }
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002136 }
2137
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002138 sig = WSTOPSIG(status);
2139
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002140 if (event != 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002141 /* Ptrace event */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002142#if USE_SEIZE
Denys Vlasenko26bc0602012-07-10 16:36:32 +02002143 if (event == PTRACE_EVENT_STOP) {
Denys Vlasenko67038162012-01-29 16:46:46 +01002144 /*
2145 * PTRACE_INTERRUPT-stop or group-stop.
2146 * PTRACE_INTERRUPT-stop has sig == SIGTRAP here.
2147 */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002148 if (sig == SIGSTOP
2149 || sig == SIGTSTP
2150 || sig == SIGTTIN
2151 || sig == SIGTTOU
2152 ) {
2153 stopped = 1;
2154 goto show_stopsig;
2155 }
2156 }
2157#endif
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002158 goto restart_tracee_with_sig_0;
2159 }
2160
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002161 /* Is this post-attach SIGSTOP?
2162 * Interestingly, the process may stop
2163 * with STOPSIG equal to some other signal
2164 * than SIGSTOP if we happend to attach
2165 * just before the process takes a signal.
2166 */
2167 if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002168 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002169 fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid);
2170 tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002171 goto restart_tracee_with_sig_0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002172 }
2173
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002174 if (sig != syscall_trap_sig) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002175 siginfo_t si;
2176
2177 /* Nonzero (true) if tracee is stopped by signal
2178 * (as opposed to "tracee received signal").
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002179 * TODO: shouldn't we check for errno == EINVAL too?
2180 * We can get ESRCH instead, you know...
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002181 */
2182 stopped = (ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002183#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002184 show_stopsig:
Denys Vlasenko67038162012-01-29 16:46:46 +01002185#endif
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002186 if (cflag != CFLAG_ONLY_STATS
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002187 && (qual_flags[sig] & QUAL_SIGNAL)) {
Dmitry V. Levinc15dfc72011-03-10 14:44:45 +00002188#if defined(PT_CR_IPSR) && defined(PT_CR_IIP)
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002189 long pc = 0;
2190 long psr = 0;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002191
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002192 upeek(tcp, PT_CR_IPSR, &psr);
2193 upeek(tcp, PT_CR_IIP, &pc);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002194
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002195# define PSR_RI 41
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002196 pc += (psr >> PSR_RI) & 0x3;
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002197# define PC_FORMAT_STR " @ %lx"
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002198# define PC_FORMAT_ARG , pc
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002199#else
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002200# define PC_FORMAT_STR ""
2201# define PC_FORMAT_ARG /* nothing */
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002202#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002203 printleader(tcp);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002204 if (!stopped) {
Denys Vlasenko2c4fb902012-03-15 17:24:49 +01002205 tprintf("--- %s ", signame(sig));
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002206 printsiginfo(&si, verbose(tcp));
Denys Vlasenko2c4fb902012-03-15 17:24:49 +01002207 tprintf(PC_FORMAT_STR " ---\n"
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002208 PC_FORMAT_ARG);
2209 } else
Denys Vlasenko2c4fb902012-03-15 17:24:49 +01002210 tprintf("--- stopped by %s" PC_FORMAT_STR " ---\n",
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002211 signame(sig)
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002212 PC_FORMAT_ARG);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002213 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002214 }
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002215
2216 if (!stopped)
2217 /* It's signal-delivery-stop. Inject the signal */
2218 goto restart_tracee;
2219
2220 /* It's group-stop */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002221#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002222 if (use_seize) {
2223 /*
2224 * This ends ptrace-stop, but does *not* end group-stop.
2225 * This makes stopping signals work properly on straced process
2226 * (that is, process really stops. It used to continue to run).
2227 */
2228 if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) {
2229 cleanup();
2230 return -1;
2231 }
2232 continue;
2233 }
2234 /* We don't have PTRACE_LISTEN support... */
2235#endif
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002236 goto restart_tracee;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002237 }
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002238
2239 /* We handled quick cases, we are permitted to interrupt now. */
Roland McGrath02203312007-06-11 22:06:31 +00002240 if (interrupted)
2241 return 0;
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002242
2243 /* This should be syscall entry or exit.
2244 * (Or it still can be that pesky post-execve SIGTRAP!)
2245 * Handle it.
2246 */
Denys Vlasenko23506752012-03-21 10:32:49 +01002247 if (trace_syscall(tcp) < 0) {
2248 /* ptrace() failed in trace_syscall().
Roland McGratheb9e2e82009-06-02 16:49:22 -07002249 * Likely a result of process disappearing mid-flight.
Denys Vlasenko23506752012-03-21 10:32:49 +01002250 * Observed case: exit_group() or SIGKILL terminating
Denys Vlasenkof1e69032012-01-04 15:15:26 +01002251 * all processes in thread group.
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002252 * We assume that ptrace error was caused by process death.
Denys Vlasenkof2025022012-03-09 15:29:45 +01002253 * We used to detach(tcp) here, but since we no longer
2254 * implement "detach before death" policy/hack,
2255 * we can let this process to report its death to us
2256 * normally, via WIFEXITED or WIFSIGNALED wait status.
2257 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002258 continue;
2259 }
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002260 restart_tracee_with_sig_0:
2261 sig = 0;
2262 restart_tracee:
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002263 if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002264 cleanup();
2265 return -1;
2266 }
2267 }
2268 return 0;
2269}
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002270
2271int
2272main(int argc, char *argv[])
2273{
2274 init(argc, argv);
2275
2276 /* Run main tracing loop */
2277 if (trace() < 0)
2278 return 1;
2279
2280 cleanup();
2281 fflush(NULL);
Dmitry V. Levincf534362012-07-12 20:54:46 +00002282 if (shared_log != stderr)
2283 fclose(shared_log);
2284 if (popen_pid) {
2285 while (waitpid(popen_pid, NULL, 0) < 0 && errno == EINTR)
2286 ;
2287 }
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002288 if (exit_code > 0xff) {
2289 /* Avoid potential core file clobbering. */
2290 struct rlimit rlim = {0, 0};
2291 setrlimit(RLIMIT_CORE, &rlim);
2292
2293 /* Child was killed by a signal, mimic that. */
2294 exit_code &= 0xff;
2295 signal(exit_code, SIG_DFL);
2296 raise(exit_code);
2297 /* Paranoia - what if this signal is not fatal?
2298 Exit with 128 + signo then. */
2299 exit_code += 128;
2300 }
2301
2302 return exit_code;
2303}