blob: 319dff2fd33c22c911dc3fca42e2a7ebf1585347 [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\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000204-i -- print instruction pointer at time of syscall\n\
205-q -- suppress messages about attaching, detaching, etc.\n\
206-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
Denys Vlasenkocdab1be2012-02-03 12:17:57 +0100207-T -- print time spent in each syscall\n\
208-v -- verbose mode: print unabbreviated argv, stat, termios, etc. args\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000209-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000210-y -- print paths associated with file descriptor arguments\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200211-h -- print help message, -V -- print version\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000212-a column -- alignment COLUMN for printing syscall results (default %d)\n\
Denys Vlasenko22efaf02013-02-27 12:15:19 +0100213-b execve -- detach on this syscall\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000214-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
Denys Vlasenko38e79bb2013-02-26 11:33:54 +0100215 options: trace, abbrev, verbose, raw, signal, read, 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"
Denys Vlasenko38e79bb2013-02-26 11:33:54 +0100232/* ancient, no one should use it
233-F -- attempt to follow vforks (deprecated, use -f)\n\
234 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100235/* this is broken, so don't document it
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000236-z -- print only succeeding syscalls\n\
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100237 */
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. */
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100751 perror_msg("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)
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100755 perror_msg("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)
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100759 perror_msg("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) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100772 perror_msg("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) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100779 perror_msg("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)
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100786 perror_msg("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) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100935 perror_msg("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) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100956 perror_msg("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 */
Denys Vlasenko05f32512013-02-26 12:00:34 +01001123 params_for_tracee.pathname = NOMMU_SYSTEM ? strdup(pathname) : pathname;
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001124
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 }
Denys Vlasenko05f32512013-02-26 12:00:34 +01001157 skip_startup_execve = 1;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001158 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001159 /* Else: NOMMU case, we have no way to sync.
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001160 * Just attach to it as soon as possible.
1161 * This means that we may miss a few first syscalls...
1162 */
1163
1164 if (ptrace_attach_or_seize(pid)) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001165 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001166 perror_msg_and_die("Can't attach to %d", pid);
1167 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001168 if (!NOMMU_SYSTEM)
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001169 kill(pid, SIGCONT);
1170 }
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001171 tcp = alloctcb(pid);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001172 if (!NOMMU_SYSTEM)
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001173 tcp->flags |= TCB_ATTACHED | TCB_STRACE_CHILD | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001174 else
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001175 tcp->flags |= TCB_ATTACHED | TCB_STRACE_CHILD | TCB_STARTUP;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001176 newoutf(tcp);
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001177 }
1178 else {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001179 /* With -D, we are *child* here, IOW: different pid. Fetch it: */
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001180 strace_tracer_pid = getpid();
1181 /* The tracee is our parent: */
1182 pid = getppid();
Denys Vlasenkof2025022012-03-09 15:29:45 +01001183 alloctcb(pid);
1184 /* attaching will be done later, by startup_attach */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001185 /* note: we don't do newoutf(tcp) here either! */
Denys Vlasenko05f32512013-02-26 12:00:34 +01001186 skip_startup_execve = 1;
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001187
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001188 /* NOMMU BUG! -D mode is active, we (child) return,
1189 * and we will scribble over parent's stack!
1190 * When parent later unpauses, it segfaults.
1191 *
1192 * We work around it
1193 * (1) by declaring exec_or_die() NORETURN,
1194 * hopefully compiler will just jump to it
1195 * instead of call (won't push anything to stack),
1196 * (2) by trying very hard in exec_or_die()
1197 * to not use any stack,
1198 * (3) having a really big (MAXPATHLEN) stack object
1199 * in this function, which creates a "buffer" between
1200 * child's and parent's stack pointers.
1201 * This may save us if (1) and (2) failed
1202 * and compiler decided to use stack in exec_or_die() anyway
1203 * (happens on i386 because of stack parameter passing).
Denys Vlasenko05f32512013-02-26 12:00:34 +01001204 *
1205 * A cleaner solution is to use makecontext + setcontext
1206 * to create a genuine separate stack and execute on it.
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001207 */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001208 }
Roland McGrath02203312007-06-11 22:06:31 +00001209}
1210
Wang Chaob13c0de2010-11-12 17:25:19 +08001211/*
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001212 * Test whether the kernel support PTRACE_O_TRACECLONE et al options.
Wang Chaob13c0de2010-11-12 17:25:19 +08001213 * First fork a new child, call ptrace with PTRACE_SETOPTIONS on it,
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001214 * and then see which options are supported by the kernel.
Wang Chaob13c0de2010-11-12 17:25:19 +08001215 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001216static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001217test_ptrace_setoptions_followfork(void)
Wang Chaob13c0de2010-11-12 17:25:19 +08001218{
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001219 int pid, expected_grandchild = 0, found_grandchild = 0;
1220 const unsigned int test_options = PTRACE_O_TRACECLONE |
1221 PTRACE_O_TRACEFORK |
1222 PTRACE_O_TRACEVFORK;
Wang Chaob13c0de2010-11-12 17:25:19 +08001223
Denys Vlasenko05f32512013-02-26 12:00:34 +01001224 /* Need fork for test. NOMMU has no forks */
1225 if (NOMMU_SYSTEM)
1226 goto worked; /* be bold, and pretend that test succeeded */
1227
Denys Vlasenko5d645812011-08-20 12:48:18 +02001228 pid = fork();
1229 if (pid < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001230 perror_msg_and_die("fork");
Denys Vlasenko5d645812011-08-20 12:48:18 +02001231 if (pid == 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001232 pid = getpid();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001233 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001234 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1235 __func__);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001236 kill_save_errno(pid, SIGSTOP);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001237 if (fork() < 0)
1238 perror_msg_and_die("fork");
1239 _exit(0);
Wang Chaob13c0de2010-11-12 17:25:19 +08001240 }
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001241
1242 while (1) {
1243 int status, tracee_pid;
1244
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001245 errno = 0;
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001246 tracee_pid = wait(&status);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001247 if (tracee_pid <= 0) {
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001248 if (errno == EINTR)
1249 continue;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001250 if (errno == ECHILD)
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001251 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001252 kill_save_errno(pid, SIGKILL);
1253 perror_msg_and_die("%s: unexpected wait result %d",
1254 __func__, tracee_pid);
1255 }
1256 if (WIFEXITED(status)) {
1257 if (WEXITSTATUS(status)) {
1258 if (tracee_pid != pid)
1259 kill_save_errno(pid, SIGKILL);
1260 error_msg_and_die("%s: unexpected exit status %u",
1261 __func__, WEXITSTATUS(status));
1262 }
1263 continue;
1264 }
1265 if (WIFSIGNALED(status)) {
1266 if (tracee_pid != pid)
1267 kill_save_errno(pid, SIGKILL);
1268 error_msg_and_die("%s: unexpected signal %u",
1269 __func__, WTERMSIG(status));
1270 }
1271 if (!WIFSTOPPED(status)) {
1272 if (tracee_pid != pid)
1273 kill_save_errno(tracee_pid, SIGKILL);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001274 kill_save_errno(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001275 error_msg_and_die("%s: unexpected wait status %x",
1276 __func__, status);
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001277 }
1278 if (tracee_pid != pid) {
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001279 found_grandchild = tracee_pid;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001280 if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0) {
1281 kill_save_errno(tracee_pid, SIGKILL);
1282 kill_save_errno(pid, SIGKILL);
1283 perror_msg_and_die("PTRACE_CONT doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001284 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001285 continue;
1286 }
1287 switch (WSTOPSIG(status)) {
1288 case SIGSTOP:
1289 if (ptrace(PTRACE_SETOPTIONS, pid, 0, test_options) < 0
1290 && errno != EINVAL && errno != EIO)
1291 perror_msg("PTRACE_SETOPTIONS");
1292 break;
1293 case SIGTRAP:
1294 if (status >> 16 == PTRACE_EVENT_FORK) {
1295 long msg = 0;
1296
1297 if (ptrace(PTRACE_GETEVENTMSG, pid,
1298 NULL, (long) &msg) == 0)
1299 expected_grandchild = msg;
1300 }
1301 break;
1302 }
1303 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) {
1304 kill_save_errno(pid, SIGKILL);
1305 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001306 }
1307 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001308 if (expected_grandchild && expected_grandchild == found_grandchild) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001309 worked:
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001310 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001311 if (debug_flag)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001312 fprintf(stderr, "ptrace_setoptions = %#x\n",
1313 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001314 return 0;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001315 }
1316 error_msg("Test for PTRACE_O_TRACECLONE failed, "
1317 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001318 return 1;
Wang Chaob13c0de2010-11-12 17:25:19 +08001319}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001320
1321/*
1322 * Test whether the kernel support PTRACE_O_TRACESYSGOOD.
1323 * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it,
1324 * and then see whether it will stop with (SIGTRAP | 0x80).
1325 *
1326 * Use of this option enables correct handling of user-generated SIGTRAPs,
1327 * and SIGTRAPs generated by special instructions such as int3 on x86:
1328 * _start: .globl _start
1329 * int3
1330 * movl $42, %ebx
1331 * movl $1, %eax
1332 * int $0x80
1333 * (compile with: "gcc -nostartfiles -nostdlib -o int3 int3.S")
1334 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001335static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001336test_ptrace_setoptions_for_all(void)
1337{
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001338 const unsigned int test_options = PTRACE_O_TRACESYSGOOD |
1339 PTRACE_O_TRACEEXEC;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001340 int pid;
1341 int it_worked = 0;
1342
Denys Vlasenko05f32512013-02-26 12:00:34 +01001343 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001344 if (NOMMU_SYSTEM)
Denys Vlasenko05f32512013-02-26 12:00:34 +01001345 goto worked; /* be bold, and pretend that test succeeded */
Mike Frysinger7ff5ed92012-04-05 01:52:25 -04001346
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001347 pid = fork();
1348 if (pid < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001349 perror_msg_and_die("fork");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001350
1351 if (pid == 0) {
1352 pid = getpid();
1353 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001354 /* Note: exits with exitcode 1 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001355 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1356 __func__);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001357 kill(pid, SIGSTOP);
1358 _exit(0); /* parent should see entry into this syscall */
1359 }
1360
1361 while (1) {
1362 int status, tracee_pid;
1363
1364 errno = 0;
1365 tracee_pid = wait(&status);
1366 if (tracee_pid <= 0) {
1367 if (errno == EINTR)
1368 continue;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001369 kill_save_errno(pid, SIGKILL);
1370 perror_msg_and_die("%s: unexpected wait result %d",
1371 __func__, tracee_pid);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001372 }
Denys Vlasenko75422762011-05-27 14:36:01 +02001373 if (WIFEXITED(status)) {
1374 if (WEXITSTATUS(status) == 0)
1375 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001376 error_msg_and_die("%s: unexpected exit status %u",
1377 __func__, WEXITSTATUS(status));
1378 }
1379 if (WIFSIGNALED(status)) {
1380 error_msg_and_die("%s: unexpected signal %u",
1381 __func__, WTERMSIG(status));
Denys Vlasenko75422762011-05-27 14:36:01 +02001382 }
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001383 if (!WIFSTOPPED(status)) {
1384 kill(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001385 error_msg_and_die("%s: unexpected wait status %x",
1386 __func__, status);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001387 }
1388 if (WSTOPSIG(status) == SIGSTOP) {
1389 /*
1390 * We don't check "options aren't accepted" error.
1391 * If it happens, we'll never get (SIGTRAP | 0x80),
1392 * and thus will decide to not use the option.
1393 * IOW: the outcome of the test will be correct.
1394 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001395 if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0
1396 && errno != EINVAL && errno != EIO)
1397 perror_msg("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001398 }
1399 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
1400 it_worked = 1;
1401 }
1402 if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001403 kill_save_errno(pid, SIGKILL);
Denys Vlasenko75422762011-05-27 14:36:01 +02001404 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001405 }
1406 }
1407
1408 if (it_worked) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001409 worked:
Denys Vlasenko75422762011-05-27 14:36:01 +02001410 syscall_trap_sig = (SIGTRAP | 0x80);
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001411 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001412 if (debug_flag)
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001413 fprintf(stderr, "ptrace_setoptions = %#x\n",
1414 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001415 return 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001416 }
1417
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001418 error_msg("Test for PTRACE_O_TRACESYSGOOD failed, "
1419 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001420 return 1;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001421}
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001422
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001423#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001424static void
1425test_ptrace_seize(void)
1426{
1427 int pid;
1428
Denys Vlasenko05f32512013-02-26 12:00:34 +01001429 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001430 if (NOMMU_SYSTEM) {
1431 post_attach_sigstop = 0; /* this sets use_seize to 1 */
1432 return;
1433 }
1434
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001435 pid = fork();
1436 if (pid < 0)
1437 perror_msg_and_die("fork");
1438
1439 if (pid == 0) {
1440 pause();
1441 _exit(0);
1442 }
1443
1444 /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After
1445 * attaching tracee continues to run unless a trap condition occurs.
1446 * PTRACE_SEIZE doesn't affect signal or group stop state.
1447 */
Denys Vlasenko26bc0602012-07-10 16:36:32 +02001448 if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001449 post_attach_sigstop = 0; /* this sets use_seize to 1 */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001450 } else if (debug_flag) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001451 fprintf(stderr, "PTRACE_SEIZE doesn't work\n");
1452 }
1453
1454 kill(pid, SIGKILL);
1455
1456 while (1) {
1457 int status, tracee_pid;
1458
1459 errno = 0;
1460 tracee_pid = waitpid(pid, &status, 0);
1461 if (tracee_pid <= 0) {
1462 if (errno == EINTR)
1463 continue;
1464 perror_msg_and_die("%s: unexpected wait result %d",
1465 __func__, tracee_pid);
1466 }
1467 if (WIFSIGNALED(status)) {
1468 return;
1469 }
1470 error_msg_and_die("%s: unexpected wait status %x",
1471 __func__, status);
1472 }
1473}
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001474#else /* !USE_SEIZE */
1475# define test_ptrace_seize() ((void)0)
1476#endif
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001477
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001478static unsigned
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001479get_os_release(void)
1480{
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001481 unsigned rel;
1482 const char *p;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001483 struct utsname u;
1484 if (uname(&u) < 0)
1485 perror_msg_and_die("uname");
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001486 /* u.release has this form: "3.2.9[-some-garbage]" */
1487 rel = 0;
1488 p = u.release;
1489 for (;;) {
1490 if (!(*p >= '0' && *p <= '9'))
1491 error_msg_and_die("Bad OS release string: '%s'", u.release);
1492 /* Note: this open-codes KERNEL_VERSION(): */
1493 rel = (rel << 8) | atoi(p);
1494 if (rel >= KERNEL_VERSION(1,0,0))
1495 break;
1496 while (*p >= '0' && *p <= '9')
1497 p++;
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001498 if (*p != '.') {
1499 if (rel >= KERNEL_VERSION(0,1,0)) {
1500 /* "X.Y-something" means "X.Y.0" */
1501 rel <<= 8;
1502 break;
1503 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001504 error_msg_and_die("Bad OS release string: '%s'", u.release);
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001505 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001506 p++;
1507 }
1508 return rel;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001509}
1510
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01001511/*
1512 * Initialization part of main() was eating much stack (~0.5k),
1513 * which was unused after init.
1514 * We can reuse it if we move init code into a separate function.
1515 *
1516 * Don't want main() to inline us and defeat the reason
1517 * we have a separate function.
1518 */
1519static void __attribute__ ((noinline))
1520init(int argc, char *argv[])
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001521{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001522 struct tcb *tcp;
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001523 int c, i;
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001524 int optF = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001525 struct sigaction sa;
1526
Dmitry V. Levin08b623e2007-10-08 21:04:41 +00001527 progname = argv[0] ? argv[0] : "strace";
1528
Denys Vlasenkoa5090542012-03-15 17:27:49 +01001529 /* Make sure SIGCHLD has the default action so that waitpid
1530 definitely works without losing track of children. The user
1531 should not have given us a bogus state to inherit, but he might
1532 have. Arguably we should detect SIG_IGN here and pass it on
1533 to children, but probably noone really needs that. */
1534 signal(SIGCHLD, SIG_DFL);
1535
Denys Vlasenko75422762011-05-27 14:36:01 +02001536 strace_tracer_pid = getpid();
1537
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001538 os_release = get_os_release();
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001539
Roland McGrathee9d4352002-12-18 04:16:10 +00001540 /* Allocate the initial tcbtab. */
1541 tcbtabsize = argc; /* Surely enough for all -p args. */
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001542 tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0]));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001543 if (!tcbtab)
1544 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001545 tcp = calloc(tcbtabsize, sizeof(*tcp));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001546 if (!tcp)
1547 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001548 for (c = 0; c < tcbtabsize; c++)
1549 tcbtab[c] = tcp++;
Roland McGrathee9d4352002-12-18 04:16:10 +00001550
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001551 shared_log = stderr;
Roland McGrath138c6a32006-01-12 09:50:49 +00001552 set_sortby(DEFAULT_SORTBY);
1553 set_personality(DEFAULT_PERSONALITY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001554 qualify("trace=all");
1555 qualify("abbrev=all");
1556 qualify("verbose=all");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001557#if DEFAULT_QUAL_FLAGS != (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
1558# error Bug in DEFAULT_QUAL_FLAGS
1559#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001560 qualify("signal=all");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001561 while ((c = getopt(argc, argv,
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001562 "+b:cCdfFhiqrtTvVxyz"
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001563 "D"
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001564 "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001565 switch (c) {
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001566 case 'b':
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001567 if (strcmp(optarg, "execve") != 0)
1568 error_msg_and_die("Syscall '%s' for -b isn't supported",
1569 optarg);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001570 detach_on_execve = 1;
1571 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001572 case 'c':
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001573 if (cflag == CFLAG_BOTH) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001574 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001575 }
1576 cflag = CFLAG_ONLY_STATS;
1577 break;
1578 case 'C':
1579 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001580 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001581 }
1582 cflag = CFLAG_BOTH;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001583 break;
1584 case 'd':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001585 debug_flag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001586 break;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001587 case 'D':
1588 daemonized_tracer = 1;
1589 break;
Roland McGrath41c48222008-07-18 00:25:10 +00001590 case 'F':
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001591 optF = 1;
1592 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001593 case 'f':
1594 followfork++;
1595 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001596 case 'h':
1597 usage(stdout, 0);
1598 break;
1599 case 'i':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001600 iflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001601 break;
1602 case 'q':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001603 qflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001604 break;
1605 case 'r':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001606 rflag = 1;
1607 /* fall through to tflag++ */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001608 case 't':
1609 tflag++;
1610 break;
1611 case 'T':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001612 Tflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001613 break;
1614 case 'x':
1615 xflag++;
1616 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001617 case 'y':
1618 show_fd_path = 1;
1619 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001620 case 'v':
1621 qualify("abbrev=none");
1622 break;
1623 case 'V':
Roland McGrath9c9a2532003-02-20 02:56:29 +00001624 printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001625 exit(0);
1626 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001627 case 'z':
1628 not_failing_only = 1;
1629 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001630 case 'a':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001631 acolumn = string_to_uint(optarg);
Denys Vlasenko102ec492011-08-25 01:27:59 +02001632 if (acolumn < 0)
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001633 error_opt_arg(c, optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001634 break;
1635 case 'e':
1636 qualify(optarg);
1637 break;
1638 case 'o':
1639 outfname = strdup(optarg);
1640 break;
1641 case 'O':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001642 i = string_to_uint(optarg);
1643 if (i < 0)
1644 error_opt_arg(c, optarg);
1645 set_overhead(i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001646 break;
1647 case 'p':
Denys Vlasenkoe8172b72012-03-09 13:01:04 +01001648 process_opt_p_list(optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001649 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001650 case 'P':
1651 tracing_paths = 1;
1652 if (pathtrace_select(optarg)) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001653 error_msg_and_die("Failed to select path '%s'", optarg);
Grant Edwards8a082772011-04-07 20:25:40 +00001654 }
1655 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001656 case 's':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001657 i = string_to_uint(optarg);
1658 if (i < 0)
1659 error_opt_arg(c, optarg);
1660 max_strlen = i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001661 break;
1662 case 'S':
1663 set_sortby(optarg);
1664 break;
1665 case 'u':
1666 username = strdup(optarg);
1667 break;
Roland McGrathde6e5332003-01-24 04:31:23 +00001668 case 'E':
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001669 if (putenv(optarg) < 0)
1670 die_out_of_memory();
Roland McGrathde6e5332003-01-24 04:31:23 +00001671 break;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001672 case 'I':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001673 opt_intr = string_to_uint(optarg);
1674 if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS)
1675 error_opt_arg(c, optarg);
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001676 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001677 default:
1678 usage(stderr, 1);
1679 break;
1680 }
1681 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001682 argv += optind;
1683 /* argc -= optind; - no need, argc is not used below */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001684
Denys Vlasenko102ec492011-08-25 01:27:59 +02001685 acolumn_spaces = malloc(acolumn + 1);
1686 if (!acolumn_spaces)
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001687 die_out_of_memory();
Denys Vlasenko102ec492011-08-25 01:27:59 +02001688 memset(acolumn_spaces, ' ', acolumn);
1689 acolumn_spaces[acolumn] = '\0';
1690
Denys Vlasenko837399a2012-01-24 11:37:03 +01001691 /* Must have PROG [ARGS], or -p PID. Not both. */
Denys Vlasenkofd883382012-03-09 13:03:41 +01001692 if (!argv[0] == !nprocs)
Roland McGrathce0d1542003-11-11 21:24:23 +00001693 usage(stderr, 1);
1694
Denys Vlasenkofd883382012-03-09 13:03:41 +01001695 if (nprocs != 0 && daemonized_tracer) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001696 error_msg_and_die("-D and -p are mutually exclusive");
Wang Chaod322a4b2010-08-05 14:30:11 +08001697 }
1698
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001699 if (!followfork)
1700 followfork = optF;
1701
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001702 if (followfork >= 2 && cflag) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001703 error_msg_and_die("(-c or -C) and -ff are mutually exclusive");
Roland McGrathcb9def62006-04-25 07:48:03 +00001704 }
1705
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001706 /* See if they want to run as another user. */
1707 if (username != NULL) {
1708 struct passwd *pent;
1709
1710 if (getuid() != 0 || geteuid() != 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001711 error_msg_and_die("You must be root to use the -u option");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001712 }
Denys Vlasenko5d645812011-08-20 12:48:18 +02001713 pent = getpwnam(username);
1714 if (pent == NULL) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001715 error_msg_and_die("Cannot find user '%s'", username);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001716 }
1717 run_uid = pent->pw_uid;
1718 run_gid = pent->pw_gid;
1719 }
1720 else {
1721 run_uid = getuid();
1722 run_gid = getgid();
1723 }
1724
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001725 /*
1726 * On any reasonably recent Linux kernel (circa about 2.5.46)
1727 * need_fork_exec_workarounds should stay 0 after these tests:
1728 */
1729 /*need_fork_exec_workarounds = 0; - already is */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001730 if (followfork)
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001731 need_fork_exec_workarounds = test_ptrace_setoptions_followfork();
1732 need_fork_exec_workarounds |= test_ptrace_setoptions_for_all();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001733 test_ptrace_seize();
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001734
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001735 /* Check if they want to redirect the output. */
1736 if (outfname) {
Roland McGrath37b9a662003-11-07 02:26:54 +00001737 /* See if they want to pipe the output. */
1738 if (outfname[0] == '|' || outfname[0] == '!') {
1739 /*
1740 * We can't do the <outfname>.PID funny business
1741 * when using popen, so prohibit it.
1742 */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001743 if (followfork >= 2)
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001744 error_msg_and_die("Piping the output and -ff are mutually exclusive");
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001745 shared_log = strace_popen(outfname + 1);
Roland McGrath37b9a662003-11-07 02:26:54 +00001746 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001747 else if (followfork < 2)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001748 shared_log = strace_fopen(outfname);
Denys Vlasenko328bf252012-03-12 23:34:13 +01001749 } else {
1750 /* -ff without -o FILE is the same as single -f */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001751 if (followfork >= 2)
Denys Vlasenko328bf252012-03-12 23:34:13 +01001752 followfork = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001753 }
1754
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001755 if (!outfname || outfname[0] == '|' || outfname[0] == '!') {
Denys Vlasenkoa677da52012-01-24 11:31:51 +01001756 char *buf = malloc(BUFSIZ);
1757 if (!buf)
1758 die_out_of_memory();
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001759 setvbuf(shared_log, buf, _IOLBF, BUFSIZ);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001760 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001761 if (outfname && argv[0]) {
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001762 if (!opt_intr)
1763 opt_intr = INTR_NEVER;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001764 qflag = 1;
Roland McGrath36931052003-06-03 01:35:20 +00001765 }
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001766 if (!opt_intr)
1767 opt_intr = INTR_WHILE_WAIT;
Wang Chaob13c0de2010-11-12 17:25:19 +08001768
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001769 /* argv[0] -pPID -oFILE Default interactive setting
1770 * yes 0 0 INTR_WHILE_WAIT
1771 * no 1 0 INTR_WHILE_WAIT
1772 * yes 0 1 INTR_NEVER
1773 * no 1 1 INTR_WHILE_WAIT
Roland McGrath54cc1c82007-11-03 23:34:11 +00001774 */
1775
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001776 sigemptyset(&empty_set);
1777 sigemptyset(&blocked_set);
1778
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001779 /* startup_child() must be called before the signal handlers get
1780 * installed below as they are inherited into the spawned process.
1781 * Also we do not need to be protected by them as during interruption
1782 * in the startup_child() mode we kill the spawned process anyway.
1783 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001784 if (argv[0]) {
Denys Vlasenko837399a2012-01-24 11:37:03 +01001785 startup_child(argv);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001786 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001787
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001788 sa.sa_handler = SIG_IGN;
1789 sigemptyset(&sa.sa_mask);
1790 sa.sa_flags = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001791 sigaction(SIGTTOU, &sa, NULL); /* SIG_IGN */
1792 sigaction(SIGTTIN, &sa, NULL); /* SIG_IGN */
1793 if (opt_intr != INTR_ANYWHERE) {
1794 if (opt_intr == INTR_BLOCK_TSTP_TOO)
1795 sigaction(SIGTSTP, &sa, NULL); /* SIG_IGN */
1796 /*
1797 * In interactive mode (if no -o OUTFILE, or -p PID is used),
1798 * fatal signals are blocked while syscall stop is processed,
1799 * and acted on in between, when waiting for new syscall stops.
1800 * In non-interactive mode, signals are ignored.
1801 */
1802 if (opt_intr == INTR_WHILE_WAIT) {
1803 sigaddset(&blocked_set, SIGHUP);
1804 sigaddset(&blocked_set, SIGINT);
1805 sigaddset(&blocked_set, SIGQUIT);
1806 sigaddset(&blocked_set, SIGPIPE);
1807 sigaddset(&blocked_set, SIGTERM);
1808 sa.sa_handler = interrupt;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001809 }
1810 /* SIG_IGN, or set handler for these */
1811 sigaction(SIGHUP, &sa, NULL);
1812 sigaction(SIGINT, &sa, NULL);
1813 sigaction(SIGQUIT, &sa, NULL);
1814 sigaction(SIGPIPE, &sa, NULL);
1815 sigaction(SIGTERM, &sa, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001816 }
Denys Vlasenkofd883382012-03-09 13:03:41 +01001817 if (nprocs != 0 || daemonized_tracer)
Roland McGrath02203312007-06-11 22:06:31 +00001818 startup_attach();
Roland McGrath02203312007-06-11 22:06:31 +00001819
Denys Vlasenkofd883382012-03-09 13:03:41 +01001820 /* Do we want pids printed in our -o OUTFILE?
1821 * -ff: no (every pid has its own file); or
1822 * -f: yes (there can be more pids in the future); or
1823 * -p PID1,PID2: yes (there are already more than one pid)
1824 */
1825 print_pid_pfx = (outfname && followfork < 2 && (followfork == 1 || nprocs > 1));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001826}
1827
Denys Vlasenkoeebb04d2012-01-27 15:24:48 +01001828static struct tcb *
Roland McGrath54e931f2010-09-14 18:59:20 -07001829pid2tcb(int pid)
1830{
1831 int i;
1832
1833 if (pid <= 0)
1834 return NULL;
1835
1836 for (i = 0; i < tcbtabsize; i++) {
1837 struct tcb *tcp = tcbtab[i];
1838 if (tcp->pid == pid && (tcp->flags & TCB_INUSE))
1839 return tcp;
1840 }
1841
1842 return NULL;
1843}
1844
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001845static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001846cleanup(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001847{
1848 int i;
1849 struct tcb *tcp;
Denys Vlasenko35218842012-01-29 21:17:56 +01001850 int fatal_sig;
1851
1852 /* 'interrupted' is a volatile object, fetch it only once */
1853 fatal_sig = interrupted;
1854 if (!fatal_sig)
1855 fatal_sig = SIGTERM;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001856
Roland McGrathee9d4352002-12-18 04:16:10 +00001857 for (i = 0; i < tcbtabsize; i++) {
1858 tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001859 if (!(tcp->flags & TCB_INUSE))
1860 continue;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001861 if (debug_flag)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001862 fprintf(stderr,
1863 "cleanup: looking at pid %u\n", tcp->pid);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001864 if (tcp->flags & TCB_STRACE_CHILD) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001865 kill(tcp->pid, SIGCONT);
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001866 kill(tcp->pid, fatal_sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001867 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001868 detach(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001869 }
1870 if (cflag)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001871 call_summary(shared_log);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001872}
1873
1874static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001875interrupt(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001876{
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001877 interrupted = sig;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001878}
1879
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001880static int
Denys Vlasenko12014262011-05-30 14:00:14 +02001881trace(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001882{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001883 struct rusage ru;
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001884 struct rusage *rup = cflag ? &ru : NULL;
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001885#ifdef __WALL
Roland McGratheb9e2e82009-06-02 16:49:22 -07001886 static int wait4_options = __WALL;
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001887#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001888
Roland McGratheb9e2e82009-06-02 16:49:22 -07001889 while (nprocs != 0) {
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001890 int pid;
1891 int wait_errno;
1892 int status, sig;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001893 int stopped;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001894 struct tcb *tcp;
1895 unsigned event;
1896
Denys Vlasenko222713a2009-03-17 14:29:59 +00001897 if (interrupted)
Roland McGratheb9e2e82009-06-02 16:49:22 -07001898 return 0;
1899 if (interactive)
1900 sigprocmask(SIG_SETMASK, &empty_set, NULL);
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001901#ifdef __WALL
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001902 pid = wait4(-1, &status, wait4_options, rup);
Roland McGrath5bc05552002-12-17 04:50:47 +00001903 if (pid < 0 && (wait4_options & __WALL) && errno == EINVAL) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001904 /* this kernel does not support __WALL */
1905 wait4_options &= ~__WALL;
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001906 pid = wait4(-1, &status, wait4_options, rup);
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001907 }
Roland McGrath5bc05552002-12-17 04:50:47 +00001908 if (pid < 0 && !(wait4_options & __WALL) && errno == ECHILD) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001909 /* most likely a "cloned" process */
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001910 pid = wait4(-1, &status, __WCLONE, rup);
1911 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001912 perror_msg("wait4(__WCLONE) failed");
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001913 }
1914 }
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001915#else
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001916 pid = wait4(-1, &status, 0, rup);
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001917#endif /* __WALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001918 wait_errno = errno;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001919 if (interactive)
1920 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001921
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001922 if (pid < 0) {
Roland McGratheb9e2e82009-06-02 16:49:22 -07001923 switch (wait_errno) {
1924 case EINTR:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001925 continue;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001926 case ECHILD:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001927 /*
1928 * We would like to verify this case
1929 * but sometimes a race in Solbourne's
1930 * version of SunOS sometimes reports
1931 * ECHILD before sending us SIGCHILD.
1932 */
Roland McGratheb9e2e82009-06-02 16:49:22 -07001933 return 0;
1934 default:
1935 errno = wait_errno;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001936 perror_msg("wait");
Roland McGratheb9e2e82009-06-02 16:49:22 -07001937 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001938 }
1939 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001940 if (pid == popen_pid) {
1941 if (WIFEXITED(status) || WIFSIGNALED(status))
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001942 popen_pid = 0;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001943 continue;
1944 }
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001945
1946 event = ((unsigned)status >> 16);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001947 if (debug_flag) {
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001948 char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko67559ad2012-03-13 12:05:27 +01001949 char evbuf[sizeof(",PTRACE_EVENT_?? (%u)") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001950 strcpy(buf, "???");
1951 if (WIFSIGNALED(status))
1952#ifdef WCOREDUMP
1953 sprintf(buf, "WIFSIGNALED,%ssig=%s",
1954 WCOREDUMP(status) ? "core," : "",
1955 signame(WTERMSIG(status)));
1956#else
1957 sprintf(buf, "WIFSIGNALED,sig=%s",
1958 signame(WTERMSIG(status)));
1959#endif
1960 if (WIFEXITED(status))
1961 sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status));
1962 if (WIFSTOPPED(status))
1963 sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status)));
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02001964#ifdef WIFCONTINUED
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001965 if (WIFCONTINUED(status))
1966 strcpy(buf, "WIFCONTINUED");
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02001967#endif
Denys Vlasenko67559ad2012-03-13 12:05:27 +01001968 evbuf[0] = '\0';
1969 if (event != 0) {
1970 static const char *const event_names[] = {
1971 [PTRACE_EVENT_CLONE] = "CLONE",
1972 [PTRACE_EVENT_FORK] = "FORK",
1973 [PTRACE_EVENT_VFORK] = "VFORK",
1974 [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE",
1975 [PTRACE_EVENT_EXEC] = "EXEC",
1976 [PTRACE_EVENT_EXIT] = "EXIT",
1977 };
1978 const char *e;
1979 if (event < ARRAY_SIZE(event_names))
1980 e = event_names[event];
1981 else {
1982 sprintf(buf, "?? (%u)", event);
1983 e = buf;
1984 }
1985 sprintf(evbuf, ",PTRACE_EVENT_%s", e);
1986 }
1987 fprintf(stderr, " [wait(0x%04x) = %u] %s%s\n", status, pid, buf, evbuf);
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001988 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001989
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001990 /* Look up 'pid' in our table. */
Denys Vlasenko5d645812011-08-20 12:48:18 +02001991 tcp = pid2tcb(pid);
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001992
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01001993 if (!tcp) {
Roland McGrath41c48222008-07-18 00:25:10 +00001994 if (followfork) {
Denys Vlasenko418d66a2009-01-17 01:52:54 +00001995 tcp = alloctcb(pid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001996 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001997 newoutf(tcp);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001998 if (!qflag)
Denys Vlasenko833fb132011-08-17 11:30:56 +02001999 fprintf(stderr, "Process %d attached\n",
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002000 pid);
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002001 } else {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002002 /* This can happen if a clone call used
2003 CLONE_PTRACE itself. */
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002004 if (WIFSTOPPED(status))
Denys Vlasenko97c503f2012-03-09 15:11:21 +01002005 ptrace(PTRACE_CONT, pid, (char *) 0, 0);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02002006 error_msg_and_die("Unknown pid: %u", pid);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002007 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002008 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002009
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002010 clear_regs();
2011 if (WIFSTOPPED(status))
2012 get_regs(pid);
2013
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002014 /* Under Linux, execve changes pid to thread leader's pid,
2015 * and we see this changed pid on EVENT_EXEC and later,
2016 * execve sysexit. Leader "disappears" without exit
2017 * notification. Let user know that, drop leader's tcb,
2018 * and fix up pid in execve thread's tcb.
2019 * Effectively, execve thread's tcb replaces leader's tcb.
2020 *
2021 * BTW, leader is 'stuck undead' (doesn't report WIFEXITED
2022 * on exit syscall) in multithreaded programs exactly
2023 * in order to handle this case.
2024 *
2025 * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0.
2026 * On 2.6 and earlier, it can return garbage.
2027 */
2028 if (event == PTRACE_EVENT_EXEC && os_release >= KERNEL_VERSION(3,0,0)) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002029 FILE *fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002030 struct tcb *execve_thread;
2031 long old_pid = 0;
2032
2033 if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) < 0)
2034 goto dont_switch_tcbs;
2035 if (old_pid <= 0 || old_pid == pid)
2036 goto dont_switch_tcbs;
2037 execve_thread = pid2tcb(old_pid);
2038 /* It should be !NULL, but I feel paranoid */
2039 if (!execve_thread)
2040 goto dont_switch_tcbs;
2041
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002042 if (execve_thread->curcol != 0) {
2043 /*
2044 * One case we are here is -ff:
2045 * try "strace -oLOG -ff test/threaded_execve"
2046 */
2047 fprintf(execve_thread->outf, " <pid changed to %d ...>\n", pid);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002048 /*execve_thread->curcol = 0; - no need, see code below */
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002049 }
2050 /* Swap output FILEs (needed for -ff) */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002051 fp = execve_thread->outf;
2052 execve_thread->outf = tcp->outf;
2053 tcp->outf = fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002054 /* And their column positions */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002055 execve_thread->curcol = tcp->curcol;
2056 tcp->curcol = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002057 /* Drop leader, but close execve'd thread outfile (if -ff) */
2058 droptcb(tcp);
2059 /* Switch to the thread, reusing leader's outfile and pid */
2060 tcp = execve_thread;
2061 tcp->pid = pid;
2062 if (cflag != CFLAG_ONLY_STATS) {
2063 printleader(tcp);
2064 tprintf("+++ superseded by execve in pid %lu +++\n", old_pid);
2065 line_ended();
2066 tcp->flags |= TCB_REPRINT;
2067 }
2068 }
2069 dont_switch_tcbs:
2070
2071 if (event == PTRACE_EVENT_EXEC && detach_on_execve) {
2072 if (!skip_startup_execve)
2073 detach(tcp);
2074 /* This was initial execve for "strace PROG". Skip. */
2075 skip_startup_execve = 0;
2076 }
2077
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002078 /* Set current output file */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002079 current_tcp = tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002080
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002081 if (cflag) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002082 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2083 tcp->stime = ru.ru_stime;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002084 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002085
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002086 if (WIFSIGNALED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002087 if (pid == strace_child)
2088 exit_code = 0x100 | WTERMSIG(status);
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002089 if (cflag != CFLAG_ONLY_STATS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01002090 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)
2091 ) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002092 printleader(tcp);
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002093#ifdef WCOREDUMP
Denys Vlasenko000b6012012-01-28 01:25:03 +01002094 tprintf("+++ killed by %s %s+++\n",
Roland McGrath2efe8792004-01-13 09:59:45 +00002095 signame(WTERMSIG(status)),
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002096 WCOREDUMP(status) ? "(core dumped) " : "");
2097#else
Denys Vlasenko000b6012012-01-28 01:25:03 +01002098 tprintf("+++ killed by %s +++\n",
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002099 signame(WTERMSIG(status)));
Roland McGrath2efe8792004-01-13 09:59:45 +00002100#endif
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002101 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002102 }
2103 droptcb(tcp);
2104 continue;
2105 }
2106 if (WIFEXITED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002107 if (pid == strace_child)
2108 exit_code = WEXITSTATUS(status);
Denys Vlasenkob5e09082012-03-21 14:27:40 +01002109 if (cflag != CFLAG_ONLY_STATS) {
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002110 printleader(tcp);
Denys Vlasenko000b6012012-01-28 01:25:03 +01002111 tprintf("+++ exited with %d +++\n", WEXITSTATUS(status));
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002112 line_ended();
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002113 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002114 droptcb(tcp);
2115 continue;
2116 }
2117 if (!WIFSTOPPED(status)) {
2118 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
2119 droptcb(tcp);
2120 continue;
2121 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002122
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002123 /* Is this the very first time we see this tracee stopped? */
2124 if (tcp->flags & TCB_STARTUP) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002125 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002126 fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n", tcp->pid);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002127 tcp->flags &= ~TCB_STARTUP;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002128 if (tcp->flags & TCB_BPTSET) {
Roland McGrath02203312007-06-11 22:06:31 +00002129 /*
2130 * One example is a breakpoint inherited from
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002131 * parent through fork().
Roland McGrath02203312007-06-11 22:06:31 +00002132 */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002133 if (clearbpt(tcp) < 0) {
2134 /* Pretty fatal */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002135 droptcb(tcp);
2136 cleanup();
2137 return -1;
2138 }
2139 }
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002140 if (ptrace_setoptions) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002141 if (debug_flag)
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002142 fprintf(stderr, "setting opts %x on pid %d\n", ptrace_setoptions, tcp->pid);
2143 if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) {
2144 if (errno != ESRCH) {
2145 /* Should never happen, really */
2146 perror_msg_and_die("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02002147 }
2148 }
2149 }
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002150 }
2151
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002152 sig = WSTOPSIG(status);
2153
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002154 if (event != 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002155 /* Ptrace event */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002156#if USE_SEIZE
Denys Vlasenko26bc0602012-07-10 16:36:32 +02002157 if (event == PTRACE_EVENT_STOP) {
Denys Vlasenko67038162012-01-29 16:46:46 +01002158 /*
2159 * PTRACE_INTERRUPT-stop or group-stop.
2160 * PTRACE_INTERRUPT-stop has sig == SIGTRAP here.
2161 */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002162 if (sig == SIGSTOP
2163 || sig == SIGTSTP
2164 || sig == SIGTTIN
2165 || sig == SIGTTOU
2166 ) {
2167 stopped = 1;
2168 goto show_stopsig;
2169 }
2170 }
2171#endif
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002172 goto restart_tracee_with_sig_0;
2173 }
2174
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002175 /* Is this post-attach SIGSTOP?
2176 * Interestingly, the process may stop
2177 * with STOPSIG equal to some other signal
2178 * than SIGSTOP if we happend to attach
2179 * just before the process takes a signal.
2180 */
2181 if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002182 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002183 fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid);
2184 tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002185 goto restart_tracee_with_sig_0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002186 }
2187
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002188 if (sig != syscall_trap_sig) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002189 siginfo_t si;
2190
2191 /* Nonzero (true) if tracee is stopped by signal
2192 * (as opposed to "tracee received signal").
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002193 * TODO: shouldn't we check for errno == EINVAL too?
2194 * We can get ESRCH instead, you know...
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002195 */
2196 stopped = (ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002197#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002198 show_stopsig:
Denys Vlasenko67038162012-01-29 16:46:46 +01002199#endif
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002200 if (cflag != CFLAG_ONLY_STATS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01002201 && (qual_flags[sig] & QUAL_SIGNAL)
2202 ) {
Dmitry V. Levinc15dfc72011-03-10 14:44:45 +00002203#if defined(PT_CR_IPSR) && defined(PT_CR_IIP)
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002204 long pc = 0;
2205 long psr = 0;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002206
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002207 upeek(tcp, PT_CR_IPSR, &psr);
2208 upeek(tcp, PT_CR_IIP, &pc);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002209
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002210# define PSR_RI 41
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002211 pc += (psr >> PSR_RI) & 0x3;
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002212# define PC_FORMAT_STR " @ %lx"
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002213# define PC_FORMAT_ARG , pc
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002214#else
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002215# define PC_FORMAT_STR ""
2216# define PC_FORMAT_ARG /* nothing */
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002217#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002218 printleader(tcp);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002219 if (!stopped) {
Denys Vlasenko2c4fb902012-03-15 17:24:49 +01002220 tprintf("--- %s ", signame(sig));
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002221 printsiginfo(&si, verbose(tcp));
Denys Vlasenko2c4fb902012-03-15 17:24:49 +01002222 tprintf(PC_FORMAT_STR " ---\n"
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002223 PC_FORMAT_ARG);
2224 } else
Denys Vlasenko2c4fb902012-03-15 17:24:49 +01002225 tprintf("--- stopped by %s" PC_FORMAT_STR " ---\n",
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002226 signame(sig)
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002227 PC_FORMAT_ARG);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002228 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002229 }
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002230
2231 if (!stopped)
2232 /* It's signal-delivery-stop. Inject the signal */
2233 goto restart_tracee;
2234
2235 /* It's group-stop */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002236#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002237 if (use_seize) {
2238 /*
2239 * This ends ptrace-stop, but does *not* end group-stop.
2240 * This makes stopping signals work properly on straced process
2241 * (that is, process really stops. It used to continue to run).
2242 */
2243 if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) {
2244 cleanup();
2245 return -1;
2246 }
2247 continue;
2248 }
2249 /* We don't have PTRACE_LISTEN support... */
2250#endif
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002251 goto restart_tracee;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002252 }
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002253
2254 /* We handled quick cases, we are permitted to interrupt now. */
Roland McGrath02203312007-06-11 22:06:31 +00002255 if (interrupted)
2256 return 0;
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002257
2258 /* This should be syscall entry or exit.
2259 * (Or it still can be that pesky post-execve SIGTRAP!)
2260 * Handle it.
2261 */
Denys Vlasenko23506752012-03-21 10:32:49 +01002262 if (trace_syscall(tcp) < 0) {
2263 /* ptrace() failed in trace_syscall().
Roland McGratheb9e2e82009-06-02 16:49:22 -07002264 * Likely a result of process disappearing mid-flight.
Denys Vlasenko23506752012-03-21 10:32:49 +01002265 * Observed case: exit_group() or SIGKILL terminating
Denys Vlasenkof1e69032012-01-04 15:15:26 +01002266 * all processes in thread group.
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002267 * We assume that ptrace error was caused by process death.
Denys Vlasenkof2025022012-03-09 15:29:45 +01002268 * We used to detach(tcp) here, but since we no longer
2269 * implement "detach before death" policy/hack,
2270 * we can let this process to report its death to us
2271 * normally, via WIFEXITED or WIFSIGNALED wait status.
2272 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002273 continue;
2274 }
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002275 restart_tracee_with_sig_0:
2276 sig = 0;
2277 restart_tracee:
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002278 if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002279 cleanup();
2280 return -1;
2281 }
2282 }
2283 return 0;
2284}
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002285
2286int
2287main(int argc, char *argv[])
2288{
2289 init(argc, argv);
2290
2291 /* Run main tracing loop */
2292 if (trace() < 0)
2293 return 1;
2294
2295 cleanup();
2296 fflush(NULL);
Dmitry V. Levincf534362012-07-12 20:54:46 +00002297 if (shared_log != stderr)
2298 fclose(shared_log);
2299 if (popen_pid) {
2300 while (waitpid(popen_pid, NULL, 0) < 0 && errno == EINTR)
2301 ;
2302 }
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002303 if (exit_code > 0xff) {
2304 /* Avoid potential core file clobbering. */
2305 struct rlimit rlim = {0, 0};
2306 setrlimit(RLIMIT_CORE, &rlim);
2307
2308 /* Child was killed by a signal, mimic that. */
2309 exit_code &= 0xff;
2310 signal(exit_code, SIG_DFL);
2311 raise(exit_code);
2312 /* Paranoia - what if this signal is not fatal?
2313 Exit with 128 + signo then. */
2314 exit_code += 128;
2315 }
2316
2317 return exit_code;
2318}