blob: 84a443c07ff6c5a0fce8d96937feda9a5f363784 [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00006 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000029 */
30
31#include "defs.h"
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020032#include <stdarg.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000033#include <sys/param.h>
34#include <fcntl.h>
35#include <sys/resource.h>
36#include <sys/wait.h>
37#include <sys/stat.h>
38#include <pwd.h>
39#include <grp.h>
Roland McGrath70b08532004-04-09 00:25:21 +000040#include <dirent.h>
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +010041#include <sys/utsname.h>
Denys Vlasenko84703742012-02-25 02:38:52 +010042#if defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +000043# include <asm/ptrace_offsets.h>
44#endif
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010045/* In some libc, these aren't declared. Do it ourself: */
Denys Vlasenko96d5a762008-12-29 19:13:27 +000046extern char **environ;
Denys Vlasenko418d66a2009-01-17 01:52:54 +000047extern int optind;
48extern char *optarg;
Denys Vlasenko96d5a762008-12-29 19:13:27 +000049
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010050
51#if defined __NR_tkill
52# define my_tkill(tid, sig) syscall(__NR_tkill, (tid), (sig))
53#else
54 /* kill() may choose arbitrarily the target task of the process group
55 while we later wait on a that specific TID. PID process waits become
56 TID task specific waits for a process under ptrace(2). */
Denys Vlasenko978fbc92012-09-13 10:28:43 +020057# warning "tkill(2) not available, risk of strace hangs!"
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010058# define my_tkill(tid, sig) kill((tid), (sig))
59#endif
60
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +010061/* Glue for systems without a MMU that cannot provide fork() */
62#if !defined(HAVE_FORK)
63# undef NOMMU_SYSTEM
64# define NOMMU_SYSTEM 1
65#endif
66#if NOMMU_SYSTEM
67# define fork() vfork()
68#endif
69
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010070cflag_t cflag = CFLAG_NONE;
71unsigned int followfork = 0;
Denys Vlasenkof44cce42011-06-21 14:34:10 +020072unsigned int ptrace_setoptions = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010073unsigned int xflag = 0;
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +010074bool need_fork_exec_workarounds = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010075bool debug_flag = 0;
76bool Tflag = 0;
Daniel P. Berrange01997cf2013-05-13 11:30:55 +010077unsigned int qflag = 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020078/* Which WSTOPSIG(status) value marks syscall traps? */
Denys Vlasenko75422762011-05-27 14:36:01 +020079static unsigned int syscall_trap_sig = SIGTRAP;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010080static unsigned int tflag = 0;
81static bool iflag = 0;
82static bool rflag = 0;
83static bool print_pid_pfx = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +010084
85/* -I n */
86enum {
87 INTR_NOT_SET = 0,
88 INTR_ANYWHERE = 1, /* don't block/ignore any signals */
89 INTR_WHILE_WAIT = 2, /* block fatal signals while decoding syscall. default */
90 INTR_NEVER = 3, /* block fatal signals. default if '-o FILE PROG' */
91 INTR_BLOCK_TSTP_TOO = 4, /* block fatal signals and SIGTSTP (^Z) */
92 NUM_INTR_OPTS
93};
94static int opt_intr;
95/* We play with signal mask only if this mode is active: */
96#define interactive (opt_intr == INTR_WHILE_WAIT)
97
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +000098/*
99 * daemonized_tracer supports -D option.
100 * With this option, strace forks twice.
101 * Unlike normal case, with -D *grandparent* process exec's,
102 * becoming a traced process. Child exits (this prevents traced process
103 * from having children it doesn't expect to have), and grandchild
104 * attaches to grandparent similarly to strace -p PID.
105 * This allows for more transparent interaction in cases
106 * when process and its parent are communicating via signals,
107 * wait() etc. Without -D, strace process gets lodged in between,
108 * disrupting parent<->child link.
109 */
110static bool daemonized_tracer = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000111
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100112#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100113static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP;
114# define use_seize (post_attach_sigstop == 0)
115#else
116# define post_attach_sigstop TCB_IGNORE_ONE_SIGSTOP
117# define use_seize 0
118#endif
119
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000120/* Sometimes we want to print only succeeding syscalls. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100121bool not_failing_only = 0;
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000122
Grant Edwards8a082772011-04-07 20:25:40 +0000123/* Show path associated with fd arguments */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100124bool show_fd_path = 0;
Grant Edwards8a082772011-04-07 20:25:40 +0000125
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100126static bool detach_on_execve = 0;
Denys Vlasenko2a3d2752013-05-14 16:07:46 +0200127/* Are we "strace PROG" and need to skip detach on first execve? */
128static bool skip_one_b_execve = 0;
129/* Are we "strace PROG" and need to hide everything until execve? */
130bool hide_log_until_execve = 0;
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100131
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
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000441# define struct_stat struct stat64
442# define stat_file stat64
443# define struct_dirent struct dirent64
444# define read_dir readdir64
445# define struct_rlimit struct rlimit64
446# define set_rlimit setrlimit64
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100447#else
448# define fopen_for_output fopen
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000449# define struct_stat struct stat
450# define stat_file stat
451# define struct_dirent struct dirent
452# define read_dir readdir
453# define struct_rlimit struct rlimit
454# define set_rlimit setrlimit
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100455#endif
456
457static FILE *
458strace_fopen(const char *path)
459{
460 FILE *fp;
461
462 swap_uid();
463 fp = fopen_for_output(path, "w");
464 if (!fp)
465 perror_msg_and_die("Can't fopen '%s'", path);
466 swap_uid();
467 set_cloexec_flag(fileno(fp));
468 return fp;
469}
470
471static int popen_pid = 0;
472
473#ifndef _PATH_BSHELL
474# define _PATH_BSHELL "/bin/sh"
475#endif
476
477/*
478 * We cannot use standard popen(3) here because we have to distinguish
479 * popen child process from other processes we trace, and standard popen(3)
480 * does not export its child's pid.
481 */
482static FILE *
483strace_popen(const char *command)
484{
485 FILE *fp;
486 int fds[2];
487
488 swap_uid();
489 if (pipe(fds) < 0)
490 perror_msg_and_die("pipe");
491
492 set_cloexec_flag(fds[1]); /* never fails */
493
494 popen_pid = vfork();
495 if (popen_pid == -1)
496 perror_msg_and_die("vfork");
497
498 if (popen_pid == 0) {
499 /* child */
500 close(fds[1]);
501 if (fds[0] != 0) {
502 if (dup2(fds[0], 0))
503 perror_msg_and_die("dup2");
504 close(fds[0]);
505 }
506 execl(_PATH_BSHELL, "sh", "-c", command, NULL);
507 perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL);
508 }
509
510 /* parent */
511 close(fds[0]);
512 swap_uid();
513 fp = fdopen(fds[1], "w");
514 if (!fp)
515 die_out_of_memory();
516 return fp;
517}
518
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100519void
520tprintf(const char *fmt, ...)
521{
522 va_list args;
523
524 va_start(args, fmt);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100525 if (current_tcp) {
Denys Vlasenko6e4f3c12012-04-16 18:22:19 +0200526 int n = strace_vfprintf(current_tcp->outf, fmt, args);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100527 if (n < 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100528 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000529 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100530 } else
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100531 current_tcp->curcol += n;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100532 }
533 va_end(args);
534}
535
536void
537tprints(const char *str)
538{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100539 if (current_tcp) {
Denys Vlasenko142aee02012-04-16 18:10:15 +0200540 int n = fputs_unlocked(str, current_tcp->outf);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100541 if (n >= 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100542 current_tcp->curcol += strlen(str);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100543 return;
544 }
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100545 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000546 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100547 }
548}
549
550void
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100551line_ended(void)
552{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100553 if (current_tcp) {
554 current_tcp->curcol = 0;
555 fflush(current_tcp->outf);
556 }
557 if (printing_tcp) {
558 printing_tcp->curcol = 0;
559 printing_tcp = NULL;
560 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100561}
562
563void
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100564printleader(struct tcb *tcp)
565{
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100566 /* If -ff, "previous tcb we printed" is always the same as current,
567 * because we have per-tcb output files.
568 */
569 if (followfork >= 2)
570 printing_tcp = tcp;
571
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100572 if (printing_tcp) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100573 current_tcp = printing_tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100574 if (printing_tcp->curcol != 0 && (followfork < 2 || printing_tcp == tcp)) {
575 /*
576 * case 1: we have a shared log (i.e. not -ff), and last line
577 * wasn't finished (same or different tcb, doesn't matter).
578 * case 2: split log, we are the same tcb, but our last line
579 * didn't finish ("SIGKILL nuked us after syscall entry" etc).
580 */
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100581 tprints(" <unfinished ...>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100582 printing_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100583 }
584 }
585
586 printing_tcp = tcp;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100587 current_tcp = tcp;
588 current_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100589
590 if (print_pid_pfx)
591 tprintf("%-5d ", tcp->pid);
592 else if (nprocs > 1 && !outfname)
593 tprintf("[pid %5u] ", tcp->pid);
594
595 if (tflag) {
596 char str[sizeof("HH:MM:SS")];
597 struct timeval tv, dtv;
598 static struct timeval otv;
599
600 gettimeofday(&tv, NULL);
601 if (rflag) {
602 if (otv.tv_sec == 0)
603 otv = tv;
604 tv_sub(&dtv, &tv, &otv);
605 tprintf("%6ld.%06ld ",
606 (long) dtv.tv_sec, (long) dtv.tv_usec);
607 otv = tv;
608 }
609 else if (tflag > 2) {
610 tprintf("%ld.%06ld ",
611 (long) tv.tv_sec, (long) tv.tv_usec);
612 }
613 else {
614 time_t local = tv.tv_sec;
615 strftime(str, sizeof(str), "%T", localtime(&local));
616 if (tflag > 1)
617 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
618 else
619 tprintf("%s ", str);
620 }
621 }
622 if (iflag)
623 printcall(tcp);
624}
625
626void
627tabto(void)
628{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100629 if (current_tcp->curcol < acolumn)
630 tprints(acolumn_spaces + current_tcp->curcol);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100631}
632
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100633/* Should be only called directly *after successful attach* to a tracee.
634 * Otherwise, "strace -oFILE -ff -p<nonexistant_pid>"
635 * may create bogus empty FILE.<nonexistant_pid>, and then die.
636 */
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200637static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000638newoutf(struct tcb *tcp)
639{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100640 tcp->outf = shared_log; /* if not -ff mode, the same file is for all */
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100641 if (followfork >= 2) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000642 char name[520 + sizeof(int) * 3];
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000643 sprintf(name, "%.512s.%u", outfname, tcp->pid);
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200644 tcp->outf = strace_fopen(name);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000645 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000646}
647
Denys Vlasenko558e5122012-03-12 23:32:16 +0100648static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100649expand_tcbtab(void)
650{
651 /* Allocate some more TCBs and expand the table.
652 We don't want to relocate the TCBs because our
653 callers have pointers and it would be a pain.
654 So tcbtab is a table of pointers. Since we never
655 free the TCBs, we allocate a single chunk of many. */
656 int i = tcbtabsize;
657 struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0]));
658 struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0]));
659 if (!newtab || !newtcbs)
660 die_out_of_memory();
661 tcbtabsize *= 2;
662 tcbtab = newtab;
663 while (i < tcbtabsize)
664 tcbtab[i++] = newtcbs++;
665}
666
667static struct tcb *
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100668alloctcb(int pid)
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100669{
670 int i;
671 struct tcb *tcp;
672
673 if (nprocs == tcbtabsize)
674 expand_tcbtab();
675
676 for (i = 0; i < tcbtabsize; i++) {
677 tcp = tcbtab[i];
678 if ((tcp->flags & TCB_INUSE) == 0) {
679 memset(tcp, 0, sizeof(*tcp));
680 tcp->pid = pid;
681 tcp->flags = TCB_INUSE;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100682#if SUPPORTED_PERSONALITIES > 1
683 tcp->currpers = current_personality;
684#endif
685 nprocs++;
686 if (debug_flag)
687 fprintf(stderr, "new tcb for pid %d, active tcbs:%d\n", tcp->pid, nprocs);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100688 return tcp;
689 }
690 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100691 error_msg_and_die("bug in alloctcb");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100692}
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100693
694static void
695droptcb(struct tcb *tcp)
696{
697 if (tcp->pid == 0)
698 return;
699
700 nprocs--;
701 if (debug_flag)
702 fprintf(stderr, "dropped tcb for pid %d, %d remain\n", tcp->pid, nprocs);
703
704 if (tcp->outf) {
Denys Vlasenko8511f2a2012-03-22 09:35:51 +0100705 if (followfork >= 2) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100706 if (tcp->curcol != 0)
707 fprintf(tcp->outf, " <detached ...>\n");
708 fclose(tcp->outf);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100709 } else {
710 if (printing_tcp == tcp && tcp->curcol != 0)
711 fprintf(tcp->outf, " <detached ...>\n");
712 fflush(tcp->outf);
713 }
714 }
715
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100716 if (current_tcp == tcp)
717 current_tcp = NULL;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100718 if (printing_tcp == tcp)
719 printing_tcp = NULL;
720
721 memset(tcp, 0, sizeof(*tcp));
722}
723
724/* detach traced process; continue with sig
725 * Never call DETACH twice on the same process as both unattached and
726 * attached-unstopped processes give the same ESRCH. For unattached process we
727 * would SIGSTOP it and wait for its SIGSTOP notification forever.
728 */
729static int
730detach(struct tcb *tcp)
731{
732 int error;
733 int status, sigstop_expected;
734
735 if (tcp->flags & TCB_BPTSET)
736 clearbpt(tcp);
737
738 /*
739 * Linux wrongly insists the child be stopped
740 * before detaching. Arghh. We go through hoops
741 * to make a clean break of things.
742 */
743#if defined(SPARC)
Denys Vlasenko978fbc92012-09-13 10:28:43 +0200744# undef PTRACE_DETACH
745# define PTRACE_DETACH PTRACE_SUNDETACH
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100746#endif
747
748 error = 0;
749 sigstop_expected = 0;
750 if (tcp->flags & TCB_ATTACHED) {
751 /*
752 * We attached but possibly didn't see the expected SIGSTOP.
753 * We must catch exactly one as otherwise the detached process
754 * would be left stopped (process state T).
755 */
756 sigstop_expected = (tcp->flags & TCB_IGNORE_ONE_SIGSTOP);
757 error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, 0);
758 if (error == 0) {
759 /* On a clear day, you can see forever. */
760 }
761 else if (errno != ESRCH) {
762 /* Shouldn't happen. */
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100763 perror_msg("detach: ptrace(PTRACE_DETACH, ...)");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100764 }
765 else if (my_tkill(tcp->pid, 0) < 0) {
766 if (errno != ESRCH)
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100767 perror_msg("detach: checking sanity");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100768 }
769 else if (!sigstop_expected && my_tkill(tcp->pid, SIGSTOP) < 0) {
770 if (errno != ESRCH)
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100771 perror_msg("detach: stopping child");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100772 }
773 else
774 sigstop_expected = 1;
775 }
776
777 if (sigstop_expected) {
778 for (;;) {
779#ifdef __WALL
780 if (waitpid(tcp->pid, &status, __WALL) < 0) {
781 if (errno == ECHILD) /* Already gone. */
782 break;
783 if (errno != EINVAL) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100784 perror_msg("detach: waiting");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100785 break;
786 }
787#endif /* __WALL */
788 /* No __WALL here. */
789 if (waitpid(tcp->pid, &status, 0) < 0) {
790 if (errno != ECHILD) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100791 perror_msg("detach: waiting");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100792 break;
793 }
794#ifdef __WCLONE
795 /* If no processes, try clones. */
796 if (waitpid(tcp->pid, &status, __WCLONE) < 0) {
797 if (errno != ECHILD)
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100798 perror_msg("detach: waiting");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100799 break;
800 }
801#endif /* __WCLONE */
802 }
803#ifdef __WALL
804 }
805#endif
806 if (!WIFSTOPPED(status)) {
807 /* Au revoir, mon ami. */
808 break;
809 }
810 if (WSTOPSIG(status) == SIGSTOP) {
811 ptrace_restart(PTRACE_DETACH, tcp, 0);
812 break;
813 }
814 error = ptrace_restart(PTRACE_CONT, tcp,
815 WSTOPSIG(status) == syscall_trap_sig ? 0
816 : WSTOPSIG(status));
817 if (error < 0)
818 break;
819 }
820 }
821
822 if (!qflag && (tcp->flags & TCB_ATTACHED))
823 fprintf(stderr, "Process %u detached\n", tcp->pid);
824
825 droptcb(tcp);
826
827 return error;
828}
829
830static void
Denys Vlasenko558e5122012-03-12 23:32:16 +0100831process_opt_p_list(char *opt)
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100832{
833 while (*opt) {
834 /*
835 * We accept -p PID,PID; -p "`pidof PROG`"; -p "`pgrep PROG`".
836 * pidof uses space as delim, pgrep uses newline. :(
837 */
838 int pid;
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100839 char *delim = opt + strcspn(opt, ", \n\t");
840 char c = *delim;
841
842 *delim = '\0';
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000843 pid = string_to_uint(opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100844 if (pid <= 0) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000845 error_msg_and_die("Invalid process id: '%s'", opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100846 }
847 if (pid == strace_tracer_pid) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000848 error_msg_and_die("I'm sorry, I can't let you do that, Dave.");
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100849 }
850 *delim = c;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100851 alloctcb(pid);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100852 if (c == '\0')
853 break;
854 opt = delim + 1;
855 }
856}
857
Roland McGrath02203312007-06-11 22:06:31 +0000858static void
859startup_attach(void)
860{
861 int tcbi;
862 struct tcb *tcp;
863
864 /*
865 * Block user interruptions as we would leave the traced
866 * process stopped (process state T) if we would terminate in
Denys Vlasenko2e968c02011-09-01 10:23:09 +0200867 * between PTRACE_ATTACH and wait4() on SIGSTOP.
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200868 * We rely on cleanup() from this point on.
Roland McGrath02203312007-06-11 22:06:31 +0000869 */
870 if (interactive)
871 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
872
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000873 if (daemonized_tracer) {
874 pid_t pid = fork();
875 if (pid < 0) {
Denys Vlasenko014ca3a2011-09-02 16:19:30 +0200876 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000877 }
878 if (pid) { /* parent */
879 /*
Denys Vlasenko75422762011-05-27 14:36:01 +0200880 * Wait for grandchild to attach to straced process
881 * (grandparent). Grandchild SIGKILLs us after it attached.
882 * Grandparent's wait() is unblocked by our death,
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000883 * it proceeds to exec the straced program.
884 */
885 pause();
886 _exit(0); /* paranoia */
887 }
Denys Vlasenko75422762011-05-27 14:36:01 +0200888 /* grandchild */
889 /* We will be the tracer process. Remember our new pid: */
890 strace_tracer_pid = getpid();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000891 }
892
Roland McGrath02203312007-06-11 22:06:31 +0000893 for (tcbi = 0; tcbi < tcbtabsize; tcbi++) {
894 tcp = tcbtab[tcbi];
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200895
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100896 if (!(tcp->flags & TCB_INUSE))
897 continue;
898
Denys Vlasenkod116a732011-09-05 14:01:33 +0200899 /* Is this a process we should attach to, but not yet attached? */
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100900 if (tcp->flags & TCB_ATTACHED)
901 continue; /* no, we already attached it */
Denys Vlasenkod116a732011-09-05 14:01:33 +0200902
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000903 if (followfork && !daemonized_tracer) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000904 char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3];
Roland McGrath02203312007-06-11 22:06:31 +0000905 DIR *dir;
906
907 sprintf(procdir, "/proc/%d/task", tcp->pid);
908 dir = opendir(procdir);
909 if (dir != NULL) {
910 unsigned int ntid = 0, nerr = 0;
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000911 struct_dirent *de;
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200912
Dmitry V. Levinc8938e02013-03-20 21:40:15 +0000913 while ((de = read_dir(dir)) != NULL) {
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200914 struct tcb *cur_tcp;
915 int tid;
916
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000917 if (de->d_fileno == 0)
Roland McGrath02203312007-06-11 22:06:31 +0000918 continue;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000919 /* we trust /proc filesystem */
Roland McGrath02203312007-06-11 22:06:31 +0000920 tid = atoi(de->d_name);
921 if (tid <= 0)
922 continue;
923 ++ntid;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100924 if (ptrace_attach_or_seize(tid) < 0) {
Roland McGrath02203312007-06-11 22:06:31 +0000925 ++nerr;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100926 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200927 fprintf(stderr, "attach to pid %d failed\n", tid);
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200928 continue;
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200929 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100930 if (debug_flag)
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200931 fprintf(stderr, "attach to pid %d succeeded\n", tid);
932 cur_tcp = tcp;
933 if (tid != tcp->pid)
934 cur_tcp = alloctcb(tid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100935 cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100936 newoutf(cur_tcp);
Roland McGrath02203312007-06-11 22:06:31 +0000937 }
938 closedir(dir);
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200939 if (interactive) {
940 sigprocmask(SIG_SETMASK, &empty_set, NULL);
941 if (interrupted)
942 goto ret;
943 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
944 }
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000945 ntid -= nerr;
946 if (ntid == 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100947 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +0000948 droptcb(tcp);
949 continue;
950 }
951 if (!qflag) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000952 fprintf(stderr, ntid > 1
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100953? "Process %u attached with %u threads\n"
954: "Process %u attached\n",
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200955 tcp->pid, ntid);
Roland McGrath02203312007-06-11 22:06:31 +0000956 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100957 if (!(tcp->flags & TCB_ATTACHED)) {
Denys Vlasenkof88837a2011-09-05 14:05:46 +0200958 /* -p PID, we failed to attach to PID itself
959 * but did attach to some of its sibling threads.
960 * Drop PID's tcp.
961 */
962 droptcb(tcp);
963 }
Roland McGrath02203312007-06-11 22:06:31 +0000964 continue;
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000965 } /* if (opendir worked) */
966 } /* if (-f) */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100967 if (ptrace_attach_or_seize(tcp->pid) < 0) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +0100968 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +0000969 droptcb(tcp);
970 continue;
971 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100972 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100973 newoutf(tcp);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100974 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200975 fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000976
977 if (daemonized_tracer) {
978 /*
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000979 * Make parent go away.
980 * Also makes grandparent's wait() unblock.
981 */
982 kill(getppid(), SIGKILL);
983 }
984
Roland McGrath02203312007-06-11 22:06:31 +0000985 if (!qflag)
986 fprintf(stderr,
Denys Vlasenko9c3861d2012-03-16 15:21:49 +0100987 "Process %u attached\n",
Roland McGrath02203312007-06-11 22:06:31 +0000988 tcp->pid);
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200989 } /* for each tcbtab[] */
Roland McGrath02203312007-06-11 22:06:31 +0000990
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200991 ret:
Roland McGrath02203312007-06-11 22:06:31 +0000992 if (interactive)
993 sigprocmask(SIG_SETMASK, &empty_set, NULL);
994}
995
Denys Vlasenkof909c8d2013-02-19 16:30:31 +0100996/* Stack-o-phobic exec helper, in the hope to work around
997 * NOMMU + "daemonized tracer" difficulty.
998 */
999struct exec_params {
1000 int fd_to_close;
1001 uid_t run_euid;
1002 gid_t run_egid;
1003 char **argv;
1004 char *pathname;
1005};
1006static struct exec_params params_for_tracee;
1007static void __attribute__ ((noinline, noreturn))
1008exec_or_die(void)
1009{
1010 struct exec_params *params = &params_for_tracee;
1011
1012 if (params->fd_to_close >= 0)
1013 close(params->fd_to_close);
1014 if (!daemonized_tracer && !use_seize) {
1015 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) {
1016 perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)");
1017 }
1018 }
1019
1020 if (username != NULL) {
1021 /*
1022 * It is important to set groups before we
1023 * lose privileges on setuid.
1024 */
1025 if (initgroups(username, run_gid) < 0) {
1026 perror_msg_and_die("initgroups");
1027 }
1028 if (setregid(run_gid, params->run_egid) < 0) {
1029 perror_msg_and_die("setregid");
1030 }
1031 if (setreuid(run_uid, params->run_euid) < 0) {
1032 perror_msg_and_die("setreuid");
1033 }
1034 }
1035 else if (geteuid() != 0)
1036 if (setreuid(run_uid, run_uid) < 0) {
1037 perror_msg_and_die("setreuid");
1038 }
1039
1040 if (!daemonized_tracer) {
1041 /*
1042 * Induce a ptrace stop. Tracer (our parent)
1043 * will resume us with PTRACE_SYSCALL and display
1044 * the immediately following execve syscall.
1045 * Can't do this on NOMMU systems, we are after
1046 * vfork: parent is blocked, stopping would deadlock.
1047 */
1048 if (!NOMMU_SYSTEM)
1049 kill(getpid(), SIGSTOP);
1050 } else {
1051 alarm(3);
1052 /* we depend on SIGCHLD set to SIG_DFL by init code */
1053 /* if it happens to be SIG_IGN'ed, wait won't block */
1054 wait(NULL);
1055 alarm(0);
1056 }
1057
1058 execv(params->pathname, params->argv);
1059 perror_msg_and_die("exec");
1060}
1061
Roland McGrath02203312007-06-11 22:06:31 +00001062static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001063startup_child(char **argv)
Roland McGrath02203312007-06-11 22:06:31 +00001064{
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001065 struct_stat statbuf;
Roland McGrath02203312007-06-11 22:06:31 +00001066 const char *filename;
1067 char pathname[MAXPATHLEN];
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001068 int pid;
Roland McGrath02203312007-06-11 22:06:31 +00001069 struct tcb *tcp;
1070
1071 filename = argv[0];
1072 if (strchr(filename, '/')) {
1073 if (strlen(filename) > sizeof pathname - 1) {
1074 errno = ENAMETOOLONG;
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001075 perror_msg_and_die("exec");
Roland McGrath02203312007-06-11 22:06:31 +00001076 }
1077 strcpy(pathname, filename);
1078 }
1079#ifdef USE_DEBUGGING_EXEC
1080 /*
1081 * Debuggers customarily check the current directory
1082 * first regardless of the path but doing that gives
1083 * security geeks a panic attack.
1084 */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001085 else if (stat_file(filename, &statbuf) == 0)
Roland McGrath02203312007-06-11 22:06:31 +00001086 strcpy(pathname, filename);
1087#endif /* USE_DEBUGGING_EXEC */
1088 else {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +00001089 const char *path;
Roland McGrath02203312007-06-11 22:06:31 +00001090 int m, n, len;
1091
1092 for (path = getenv("PATH"); path && *path; path += m) {
Denys Vlasenko4f3df072012-01-29 22:38:35 +01001093 const char *colon = strchr(path, ':');
1094 if (colon) {
1095 n = colon - path;
Roland McGrath02203312007-06-11 22:06:31 +00001096 m = n + 1;
1097 }
1098 else
1099 m = n = strlen(path);
1100 if (n == 0) {
1101 if (!getcwd(pathname, MAXPATHLEN))
1102 continue;
1103 len = strlen(pathname);
1104 }
1105 else if (n > sizeof pathname - 1)
1106 continue;
1107 else {
1108 strncpy(pathname, path, n);
1109 len = n;
1110 }
1111 if (len && pathname[len - 1] != '/')
1112 pathname[len++] = '/';
1113 strcpy(pathname + len, filename);
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001114 if (stat_file(pathname, &statbuf) == 0 &&
Roland McGrath02203312007-06-11 22:06:31 +00001115 /* Accept only regular files
1116 with some execute bits set.
1117 XXX not perfect, might still fail */
1118 S_ISREG(statbuf.st_mode) &&
1119 (statbuf.st_mode & 0111))
1120 break;
1121 }
1122 }
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00001123 if (stat_file(pathname, &statbuf) < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001124 perror_msg_and_die("Can't stat '%s'", filename);
Roland McGrath02203312007-06-11 22:06:31 +00001125 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001126
1127 params_for_tracee.fd_to_close = (shared_log != stderr) ? fileno(shared_log) : -1;
1128 params_for_tracee.run_euid = (statbuf.st_mode & S_ISUID) ? statbuf.st_uid : run_uid;
1129 params_for_tracee.run_egid = (statbuf.st_mode & S_ISGID) ? statbuf.st_gid : run_gid;
1130 params_for_tracee.argv = argv;
1131 /*
1132 * On NOMMU, can be safely freed only after execve in tracee.
1133 * It's hard to know when that happens, so we just leak it.
1134 */
Denys Vlasenko05f32512013-02-26 12:00:34 +01001135 params_for_tracee.pathname = NOMMU_SYSTEM ? strdup(pathname) : pathname;
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001136
Dmitry V. Levina6809652008-11-10 17:14:58 +00001137 strace_child = pid = fork();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001138 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001139 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001140 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001141 if ((pid != 0 && daemonized_tracer)
1142 || (pid == 0 && !daemonized_tracer)
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001143 ) {
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001144 /* We are to become the tracee. Two cases:
1145 * -D: we are parent
1146 * not -D: we are child
1147 */
1148 exec_or_die();
Roland McGrath02203312007-06-11 22:06:31 +00001149 }
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001150
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001151 /* We are the tracer */
Denys Vlasenko75422762011-05-27 14:36:01 +02001152
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001153 if (!daemonized_tracer) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001154 if (!use_seize) {
1155 /* child did PTRACE_TRACEME, nothing to do in parent */
1156 } else {
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001157 if (!NOMMU_SYSTEM) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001158 /* Wait until child stopped itself */
1159 int status;
1160 while (waitpid(pid, &status, WSTOPPED) < 0) {
1161 if (errno == EINTR)
1162 continue;
1163 perror_msg_and_die("waitpid");
1164 }
1165 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001166 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001167 perror_msg_and_die("Unexpected wait status %x", status);
1168 }
1169 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001170 /* Else: NOMMU case, we have no way to sync.
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001171 * Just attach to it as soon as possible.
1172 * This means that we may miss a few first syscalls...
1173 */
1174
1175 if (ptrace_attach_or_seize(pid)) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001176 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001177 perror_msg_and_die("Can't attach to %d", pid);
1178 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001179 if (!NOMMU_SYSTEM)
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001180 kill(pid, SIGCONT);
1181 }
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001182 tcp = alloctcb(pid);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001183 if (!NOMMU_SYSTEM)
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001184 tcp->flags |= TCB_ATTACHED | TCB_STRACE_CHILD | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001185 else
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001186 tcp->flags |= TCB_ATTACHED | TCB_STRACE_CHILD | TCB_STARTUP;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001187 newoutf(tcp);
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001188 }
1189 else {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001190 /* With -D, we are *child* here, IOW: different pid. Fetch it: */
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001191 strace_tracer_pid = getpid();
1192 /* The tracee is our parent: */
1193 pid = getppid();
Denys Vlasenkof2025022012-03-09 15:29:45 +01001194 alloctcb(pid);
1195 /* attaching will be done later, by startup_attach */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001196 /* note: we don't do newoutf(tcp) here either! */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001197
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001198 /* NOMMU BUG! -D mode is active, we (child) return,
1199 * and we will scribble over parent's stack!
1200 * When parent later unpauses, it segfaults.
1201 *
1202 * We work around it
1203 * (1) by declaring exec_or_die() NORETURN,
1204 * hopefully compiler will just jump to it
1205 * instead of call (won't push anything to stack),
1206 * (2) by trying very hard in exec_or_die()
1207 * to not use any stack,
1208 * (3) having a really big (MAXPATHLEN) stack object
1209 * in this function, which creates a "buffer" between
1210 * child's and parent's stack pointers.
1211 * This may save us if (1) and (2) failed
1212 * and compiler decided to use stack in exec_or_die() anyway
1213 * (happens on i386 because of stack parameter passing).
Denys Vlasenko05f32512013-02-26 12:00:34 +01001214 *
1215 * A cleaner solution is to use makecontext + setcontext
1216 * to create a genuine separate stack and execute on it.
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001217 */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001218 }
Roland McGrath02203312007-06-11 22:06:31 +00001219}
1220
Wang Chaob13c0de2010-11-12 17:25:19 +08001221/*
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001222 * Test whether the kernel support PTRACE_O_TRACECLONE et al options.
Wang Chaob13c0de2010-11-12 17:25:19 +08001223 * First fork a new child, call ptrace with PTRACE_SETOPTIONS on it,
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001224 * and then see which options are supported by the kernel.
Wang Chaob13c0de2010-11-12 17:25:19 +08001225 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001226static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001227test_ptrace_setoptions_followfork(void)
Wang Chaob13c0de2010-11-12 17:25:19 +08001228{
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001229 int pid, expected_grandchild = 0, found_grandchild = 0;
1230 const unsigned int test_options = PTRACE_O_TRACECLONE |
1231 PTRACE_O_TRACEFORK |
1232 PTRACE_O_TRACEVFORK;
Wang Chaob13c0de2010-11-12 17:25:19 +08001233
Denys Vlasenko05f32512013-02-26 12:00:34 +01001234 /* Need fork for test. NOMMU has no forks */
1235 if (NOMMU_SYSTEM)
1236 goto worked; /* be bold, and pretend that test succeeded */
1237
Denys Vlasenko5d645812011-08-20 12:48:18 +02001238 pid = fork();
1239 if (pid < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001240 perror_msg_and_die("fork");
Denys Vlasenko5d645812011-08-20 12:48:18 +02001241 if (pid == 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001242 pid = getpid();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001243 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001244 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1245 __func__);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001246 kill_save_errno(pid, SIGSTOP);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001247 if (fork() < 0)
1248 perror_msg_and_die("fork");
1249 _exit(0);
Wang Chaob13c0de2010-11-12 17:25:19 +08001250 }
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001251
1252 while (1) {
1253 int status, tracee_pid;
1254
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001255 errno = 0;
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001256 tracee_pid = wait(&status);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001257 if (tracee_pid <= 0) {
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001258 if (errno == EINTR)
1259 continue;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001260 if (errno == ECHILD)
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001261 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001262 kill_save_errno(pid, SIGKILL);
1263 perror_msg_and_die("%s: unexpected wait result %d",
1264 __func__, tracee_pid);
1265 }
1266 if (WIFEXITED(status)) {
1267 if (WEXITSTATUS(status)) {
1268 if (tracee_pid != pid)
1269 kill_save_errno(pid, SIGKILL);
1270 error_msg_and_die("%s: unexpected exit status %u",
1271 __func__, WEXITSTATUS(status));
1272 }
1273 continue;
1274 }
1275 if (WIFSIGNALED(status)) {
1276 if (tracee_pid != pid)
1277 kill_save_errno(pid, SIGKILL);
1278 error_msg_and_die("%s: unexpected signal %u",
1279 __func__, WTERMSIG(status));
1280 }
1281 if (!WIFSTOPPED(status)) {
1282 if (tracee_pid != pid)
1283 kill_save_errno(tracee_pid, SIGKILL);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001284 kill_save_errno(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001285 error_msg_and_die("%s: unexpected wait status %x",
1286 __func__, status);
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001287 }
1288 if (tracee_pid != pid) {
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001289 found_grandchild = tracee_pid;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001290 if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0) {
1291 kill_save_errno(tracee_pid, SIGKILL);
1292 kill_save_errno(pid, SIGKILL);
1293 perror_msg_and_die("PTRACE_CONT doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001294 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001295 continue;
1296 }
1297 switch (WSTOPSIG(status)) {
1298 case SIGSTOP:
1299 if (ptrace(PTRACE_SETOPTIONS, pid, 0, test_options) < 0
1300 && errno != EINVAL && errno != EIO)
1301 perror_msg("PTRACE_SETOPTIONS");
1302 break;
1303 case SIGTRAP:
1304 if (status >> 16 == PTRACE_EVENT_FORK) {
1305 long msg = 0;
1306
1307 if (ptrace(PTRACE_GETEVENTMSG, pid,
1308 NULL, (long) &msg) == 0)
1309 expected_grandchild = msg;
1310 }
1311 break;
1312 }
1313 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) {
1314 kill_save_errno(pid, SIGKILL);
1315 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001316 }
1317 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001318 if (expected_grandchild && expected_grandchild == found_grandchild) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001319 worked:
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001320 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001321 if (debug_flag)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001322 fprintf(stderr, "ptrace_setoptions = %#x\n",
1323 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001324 return 0;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001325 }
1326 error_msg("Test for PTRACE_O_TRACECLONE failed, "
1327 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001328 return 1;
Wang Chaob13c0de2010-11-12 17:25:19 +08001329}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001330
1331/*
1332 * Test whether the kernel support PTRACE_O_TRACESYSGOOD.
1333 * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it,
1334 * and then see whether it will stop with (SIGTRAP | 0x80).
1335 *
1336 * Use of this option enables correct handling of user-generated SIGTRAPs,
1337 * and SIGTRAPs generated by special instructions such as int3 on x86:
1338 * _start: .globl _start
1339 * int3
1340 * movl $42, %ebx
1341 * movl $1, %eax
1342 * int $0x80
1343 * (compile with: "gcc -nostartfiles -nostdlib -o int3 int3.S")
1344 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001345static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001346test_ptrace_setoptions_for_all(void)
1347{
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001348 const unsigned int test_options = PTRACE_O_TRACESYSGOOD |
1349 PTRACE_O_TRACEEXEC;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001350 int pid;
1351 int it_worked = 0;
1352
Denys Vlasenko05f32512013-02-26 12:00:34 +01001353 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001354 if (NOMMU_SYSTEM)
Denys Vlasenko05f32512013-02-26 12:00:34 +01001355 goto worked; /* be bold, and pretend that test succeeded */
Mike Frysinger7ff5ed92012-04-05 01:52:25 -04001356
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001357 pid = fork();
1358 if (pid < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001359 perror_msg_and_die("fork");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001360
1361 if (pid == 0) {
1362 pid = getpid();
1363 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001364 /* Note: exits with exitcode 1 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001365 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1366 __func__);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001367 kill(pid, SIGSTOP);
1368 _exit(0); /* parent should see entry into this syscall */
1369 }
1370
1371 while (1) {
1372 int status, tracee_pid;
1373
1374 errno = 0;
1375 tracee_pid = wait(&status);
1376 if (tracee_pid <= 0) {
1377 if (errno == EINTR)
1378 continue;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001379 kill_save_errno(pid, SIGKILL);
1380 perror_msg_and_die("%s: unexpected wait result %d",
1381 __func__, tracee_pid);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001382 }
Denys Vlasenko75422762011-05-27 14:36:01 +02001383 if (WIFEXITED(status)) {
1384 if (WEXITSTATUS(status) == 0)
1385 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001386 error_msg_and_die("%s: unexpected exit status %u",
1387 __func__, WEXITSTATUS(status));
1388 }
1389 if (WIFSIGNALED(status)) {
1390 error_msg_and_die("%s: unexpected signal %u",
1391 __func__, WTERMSIG(status));
Denys Vlasenko75422762011-05-27 14:36:01 +02001392 }
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001393 if (!WIFSTOPPED(status)) {
1394 kill(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001395 error_msg_and_die("%s: unexpected wait status %x",
1396 __func__, status);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001397 }
1398 if (WSTOPSIG(status) == SIGSTOP) {
1399 /*
1400 * We don't check "options aren't accepted" error.
1401 * If it happens, we'll never get (SIGTRAP | 0x80),
1402 * and thus will decide to not use the option.
1403 * IOW: the outcome of the test will be correct.
1404 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001405 if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0
1406 && errno != EINVAL && errno != EIO)
1407 perror_msg("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001408 }
1409 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
1410 it_worked = 1;
1411 }
1412 if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001413 kill_save_errno(pid, SIGKILL);
Denys Vlasenko75422762011-05-27 14:36:01 +02001414 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001415 }
1416 }
1417
1418 if (it_worked) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001419 worked:
Denys Vlasenko75422762011-05-27 14:36:01 +02001420 syscall_trap_sig = (SIGTRAP | 0x80);
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001421 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001422 if (debug_flag)
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001423 fprintf(stderr, "ptrace_setoptions = %#x\n",
1424 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001425 return 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001426 }
1427
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001428 error_msg("Test for PTRACE_O_TRACESYSGOOD failed, "
1429 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001430 return 1;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001431}
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001432
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001433#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001434static void
1435test_ptrace_seize(void)
1436{
1437 int pid;
1438
Denys Vlasenko05f32512013-02-26 12:00:34 +01001439 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001440 if (NOMMU_SYSTEM) {
1441 post_attach_sigstop = 0; /* this sets use_seize to 1 */
1442 return;
1443 }
1444
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001445 pid = fork();
1446 if (pid < 0)
1447 perror_msg_and_die("fork");
1448
1449 if (pid == 0) {
1450 pause();
1451 _exit(0);
1452 }
1453
1454 /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After
1455 * attaching tracee continues to run unless a trap condition occurs.
1456 * PTRACE_SEIZE doesn't affect signal or group stop state.
1457 */
Denys Vlasenko26bc0602012-07-10 16:36:32 +02001458 if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001459 post_attach_sigstop = 0; /* this sets use_seize to 1 */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001460 } else if (debug_flag) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001461 fprintf(stderr, "PTRACE_SEIZE doesn't work\n");
1462 }
1463
1464 kill(pid, SIGKILL);
1465
1466 while (1) {
1467 int status, tracee_pid;
1468
1469 errno = 0;
1470 tracee_pid = waitpid(pid, &status, 0);
1471 if (tracee_pid <= 0) {
1472 if (errno == EINTR)
1473 continue;
1474 perror_msg_and_die("%s: unexpected wait result %d",
1475 __func__, tracee_pid);
1476 }
1477 if (WIFSIGNALED(status)) {
1478 return;
1479 }
1480 error_msg_and_die("%s: unexpected wait status %x",
1481 __func__, status);
1482 }
1483}
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001484#else /* !USE_SEIZE */
1485# define test_ptrace_seize() ((void)0)
1486#endif
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001487
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001488static unsigned
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001489get_os_release(void)
1490{
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001491 unsigned rel;
1492 const char *p;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001493 struct utsname u;
1494 if (uname(&u) < 0)
1495 perror_msg_and_die("uname");
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001496 /* u.release has this form: "3.2.9[-some-garbage]" */
1497 rel = 0;
1498 p = u.release;
1499 for (;;) {
1500 if (!(*p >= '0' && *p <= '9'))
1501 error_msg_and_die("Bad OS release string: '%s'", u.release);
1502 /* Note: this open-codes KERNEL_VERSION(): */
1503 rel = (rel << 8) | atoi(p);
1504 if (rel >= KERNEL_VERSION(1,0,0))
1505 break;
1506 while (*p >= '0' && *p <= '9')
1507 p++;
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001508 if (*p != '.') {
1509 if (rel >= KERNEL_VERSION(0,1,0)) {
1510 /* "X.Y-something" means "X.Y.0" */
1511 rel <<= 8;
1512 break;
1513 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001514 error_msg_and_die("Bad OS release string: '%s'", u.release);
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001515 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001516 p++;
1517 }
1518 return rel;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001519}
1520
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01001521/*
1522 * Initialization part of main() was eating much stack (~0.5k),
1523 * which was unused after init.
1524 * We can reuse it if we move init code into a separate function.
1525 *
1526 * Don't want main() to inline us and defeat the reason
1527 * we have a separate function.
1528 */
1529static void __attribute__ ((noinline))
1530init(int argc, char *argv[])
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001531{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001532 struct tcb *tcp;
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001533 int c, i;
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001534 int optF = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001535 struct sigaction sa;
1536
Dmitry V. Levin08b623e2007-10-08 21:04:41 +00001537 progname = argv[0] ? argv[0] : "strace";
1538
Denys Vlasenkoa5090542012-03-15 17:27:49 +01001539 /* Make sure SIGCHLD has the default action so that waitpid
1540 definitely works without losing track of children. The user
1541 should not have given us a bogus state to inherit, but he might
1542 have. Arguably we should detect SIG_IGN here and pass it on
1543 to children, but probably noone really needs that. */
1544 signal(SIGCHLD, SIG_DFL);
1545
Denys Vlasenko75422762011-05-27 14:36:01 +02001546 strace_tracer_pid = getpid();
1547
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001548 os_release = get_os_release();
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001549
Roland McGrathee9d4352002-12-18 04:16:10 +00001550 /* Allocate the initial tcbtab. */
1551 tcbtabsize = argc; /* Surely enough for all -p args. */
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001552 tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0]));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001553 if (!tcbtab)
1554 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001555 tcp = calloc(tcbtabsize, sizeof(*tcp));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001556 if (!tcp)
1557 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001558 for (c = 0; c < tcbtabsize; c++)
1559 tcbtab[c] = tcp++;
Roland McGrathee9d4352002-12-18 04:16:10 +00001560
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001561 shared_log = stderr;
Roland McGrath138c6a32006-01-12 09:50:49 +00001562 set_sortby(DEFAULT_SORTBY);
1563 set_personality(DEFAULT_PERSONALITY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001564 qualify("trace=all");
1565 qualify("abbrev=all");
1566 qualify("verbose=all");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001567#if DEFAULT_QUAL_FLAGS != (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
1568# error Bug in DEFAULT_QUAL_FLAGS
1569#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001570 qualify("signal=all");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001571 while ((c = getopt(argc, argv,
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001572 "+b:cCdfFhiqrtTvVxyz"
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001573 "D"
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001574 "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001575 switch (c) {
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001576 case 'b':
Denys Vlasenko22efaf02013-02-27 12:15:19 +01001577 if (strcmp(optarg, "execve") != 0)
1578 error_msg_and_die("Syscall '%s' for -b isn't supported",
1579 optarg);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001580 detach_on_execve = 1;
1581 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001582 case 'c':
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001583 if (cflag == CFLAG_BOTH) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001584 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001585 }
1586 cflag = CFLAG_ONLY_STATS;
1587 break;
1588 case 'C':
1589 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001590 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001591 }
1592 cflag = CFLAG_BOTH;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001593 break;
1594 case 'd':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001595 debug_flag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001596 break;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001597 case 'D':
1598 daemonized_tracer = 1;
1599 break;
Roland McGrath41c48222008-07-18 00:25:10 +00001600 case 'F':
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001601 optF = 1;
1602 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001603 case 'f':
1604 followfork++;
1605 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001606 case 'h':
1607 usage(stdout, 0);
1608 break;
1609 case 'i':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001610 iflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001611 break;
1612 case 'q':
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01001613 qflag++;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001614 break;
1615 case 'r':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001616 rflag = 1;
1617 /* fall through to tflag++ */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001618 case 't':
1619 tflag++;
1620 break;
1621 case 'T':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001622 Tflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001623 break;
1624 case 'x':
1625 xflag++;
1626 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001627 case 'y':
1628 show_fd_path = 1;
1629 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001630 case 'v':
1631 qualify("abbrev=none");
1632 break;
1633 case 'V':
Roland McGrath9c9a2532003-02-20 02:56:29 +00001634 printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001635 exit(0);
1636 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001637 case 'z':
1638 not_failing_only = 1;
1639 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001640 case 'a':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001641 acolumn = string_to_uint(optarg);
Denys Vlasenko102ec492011-08-25 01:27:59 +02001642 if (acolumn < 0)
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001643 error_opt_arg(c, optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001644 break;
1645 case 'e':
1646 qualify(optarg);
1647 break;
1648 case 'o':
1649 outfname = strdup(optarg);
1650 break;
1651 case 'O':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001652 i = string_to_uint(optarg);
1653 if (i < 0)
1654 error_opt_arg(c, optarg);
1655 set_overhead(i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001656 break;
1657 case 'p':
Denys Vlasenkoe8172b72012-03-09 13:01:04 +01001658 process_opt_p_list(optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001659 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001660 case 'P':
Denys Vlasenko7239dbc2013-03-05 15:46:34 +01001661 pathtrace_select(optarg);
Grant Edwards8a082772011-04-07 20:25:40 +00001662 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001663 case 's':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001664 i = string_to_uint(optarg);
1665 if (i < 0)
1666 error_opt_arg(c, optarg);
1667 max_strlen = i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001668 break;
1669 case 'S':
1670 set_sortby(optarg);
1671 break;
1672 case 'u':
1673 username = strdup(optarg);
1674 break;
Roland McGrathde6e5332003-01-24 04:31:23 +00001675 case 'E':
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001676 if (putenv(optarg) < 0)
1677 die_out_of_memory();
Roland McGrathde6e5332003-01-24 04:31:23 +00001678 break;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001679 case 'I':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001680 opt_intr = string_to_uint(optarg);
1681 if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS)
1682 error_opt_arg(c, optarg);
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001683 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001684 default:
1685 usage(stderr, 1);
1686 break;
1687 }
1688 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001689 argv += optind;
1690 /* argc -= optind; - no need, argc is not used below */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001691
Denys Vlasenko102ec492011-08-25 01:27:59 +02001692 acolumn_spaces = malloc(acolumn + 1);
1693 if (!acolumn_spaces)
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001694 die_out_of_memory();
Denys Vlasenko102ec492011-08-25 01:27:59 +02001695 memset(acolumn_spaces, ' ', acolumn);
1696 acolumn_spaces[acolumn] = '\0';
1697
Denys Vlasenko837399a2012-01-24 11:37:03 +01001698 /* Must have PROG [ARGS], or -p PID. Not both. */
Denys Vlasenkofd883382012-03-09 13:03:41 +01001699 if (!argv[0] == !nprocs)
Roland McGrathce0d1542003-11-11 21:24:23 +00001700 usage(stderr, 1);
1701
Denys Vlasenkofd883382012-03-09 13:03:41 +01001702 if (nprocs != 0 && daemonized_tracer) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001703 error_msg_and_die("-D and -p are mutually exclusive");
Wang Chaod322a4b2010-08-05 14:30:11 +08001704 }
1705
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001706 if (!followfork)
1707 followfork = optF;
1708
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001709 if (followfork >= 2 && cflag) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001710 error_msg_and_die("(-c or -C) and -ff are mutually exclusive");
Roland McGrathcb9def62006-04-25 07:48:03 +00001711 }
1712
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001713 /* See if they want to run as another user. */
1714 if (username != NULL) {
1715 struct passwd *pent;
1716
1717 if (getuid() != 0 || geteuid() != 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001718 error_msg_and_die("You must be root to use the -u option");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001719 }
Denys Vlasenko5d645812011-08-20 12:48:18 +02001720 pent = getpwnam(username);
1721 if (pent == NULL) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001722 error_msg_and_die("Cannot find user '%s'", username);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001723 }
1724 run_uid = pent->pw_uid;
1725 run_gid = pent->pw_gid;
1726 }
1727 else {
1728 run_uid = getuid();
1729 run_gid = getgid();
1730 }
1731
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001732 /*
1733 * On any reasonably recent Linux kernel (circa about 2.5.46)
1734 * need_fork_exec_workarounds should stay 0 after these tests:
1735 */
1736 /*need_fork_exec_workarounds = 0; - already is */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001737 if (followfork)
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001738 need_fork_exec_workarounds = test_ptrace_setoptions_followfork();
1739 need_fork_exec_workarounds |= test_ptrace_setoptions_for_all();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001740 test_ptrace_seize();
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001741
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001742 /* Check if they want to redirect the output. */
1743 if (outfname) {
Roland McGrath37b9a662003-11-07 02:26:54 +00001744 /* See if they want to pipe the output. */
1745 if (outfname[0] == '|' || outfname[0] == '!') {
1746 /*
1747 * We can't do the <outfname>.PID funny business
1748 * when using popen, so prohibit it.
1749 */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001750 if (followfork >= 2)
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001751 error_msg_and_die("Piping the output and -ff are mutually exclusive");
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001752 shared_log = strace_popen(outfname + 1);
Roland McGrath37b9a662003-11-07 02:26:54 +00001753 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001754 else if (followfork < 2)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001755 shared_log = strace_fopen(outfname);
Denys Vlasenko328bf252012-03-12 23:34:13 +01001756 } else {
1757 /* -ff without -o FILE is the same as single -f */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001758 if (followfork >= 2)
Denys Vlasenko328bf252012-03-12 23:34:13 +01001759 followfork = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001760 }
1761
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001762 if (!outfname || outfname[0] == '|' || outfname[0] == '!') {
Denys Vlasenkoa677da52012-01-24 11:31:51 +01001763 char *buf = malloc(BUFSIZ);
1764 if (!buf)
1765 die_out_of_memory();
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001766 setvbuf(shared_log, buf, _IOLBF, BUFSIZ);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001767 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001768 if (outfname && argv[0]) {
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001769 if (!opt_intr)
1770 opt_intr = INTR_NEVER;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001771 qflag = 1;
Roland McGrath36931052003-06-03 01:35:20 +00001772 }
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001773 if (!opt_intr)
1774 opt_intr = INTR_WHILE_WAIT;
Wang Chaob13c0de2010-11-12 17:25:19 +08001775
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001776 /* argv[0] -pPID -oFILE Default interactive setting
1777 * yes 0 0 INTR_WHILE_WAIT
1778 * no 1 0 INTR_WHILE_WAIT
1779 * yes 0 1 INTR_NEVER
1780 * no 1 1 INTR_WHILE_WAIT
Roland McGrath54cc1c82007-11-03 23:34:11 +00001781 */
1782
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001783 sigemptyset(&empty_set);
1784 sigemptyset(&blocked_set);
1785
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001786 /* startup_child() must be called before the signal handlers get
1787 * installed below as they are inherited into the spawned process.
1788 * Also we do not need to be protected by them as during interruption
1789 * in the startup_child() mode we kill the spawned process anyway.
1790 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001791 if (argv[0]) {
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001792 hide_log_until_execve = 1;
1793 skip_one_b_execve = 1;
Denys Vlasenko837399a2012-01-24 11:37:03 +01001794 startup_child(argv);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001795 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001796
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001797 sa.sa_handler = SIG_IGN;
1798 sigemptyset(&sa.sa_mask);
1799 sa.sa_flags = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001800 sigaction(SIGTTOU, &sa, NULL); /* SIG_IGN */
1801 sigaction(SIGTTIN, &sa, NULL); /* SIG_IGN */
1802 if (opt_intr != INTR_ANYWHERE) {
1803 if (opt_intr == INTR_BLOCK_TSTP_TOO)
1804 sigaction(SIGTSTP, &sa, NULL); /* SIG_IGN */
1805 /*
1806 * In interactive mode (if no -o OUTFILE, or -p PID is used),
1807 * fatal signals are blocked while syscall stop is processed,
1808 * and acted on in between, when waiting for new syscall stops.
1809 * In non-interactive mode, signals are ignored.
1810 */
1811 if (opt_intr == INTR_WHILE_WAIT) {
1812 sigaddset(&blocked_set, SIGHUP);
1813 sigaddset(&blocked_set, SIGINT);
1814 sigaddset(&blocked_set, SIGQUIT);
1815 sigaddset(&blocked_set, SIGPIPE);
1816 sigaddset(&blocked_set, SIGTERM);
1817 sa.sa_handler = interrupt;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001818 }
1819 /* SIG_IGN, or set handler for these */
1820 sigaction(SIGHUP, &sa, NULL);
1821 sigaction(SIGINT, &sa, NULL);
1822 sigaction(SIGQUIT, &sa, NULL);
1823 sigaction(SIGPIPE, &sa, NULL);
1824 sigaction(SIGTERM, &sa, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001825 }
Denys Vlasenkofd883382012-03-09 13:03:41 +01001826 if (nprocs != 0 || daemonized_tracer)
Roland McGrath02203312007-06-11 22:06:31 +00001827 startup_attach();
Roland McGrath02203312007-06-11 22:06:31 +00001828
Denys Vlasenkofd883382012-03-09 13:03:41 +01001829 /* Do we want pids printed in our -o OUTFILE?
1830 * -ff: no (every pid has its own file); or
1831 * -f: yes (there can be more pids in the future); or
1832 * -p PID1,PID2: yes (there are already more than one pid)
1833 */
1834 print_pid_pfx = (outfname && followfork < 2 && (followfork == 1 || nprocs > 1));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001835}
1836
Denys Vlasenkoeebb04d2012-01-27 15:24:48 +01001837static struct tcb *
Roland McGrath54e931f2010-09-14 18:59:20 -07001838pid2tcb(int pid)
1839{
1840 int i;
1841
1842 if (pid <= 0)
1843 return NULL;
1844
1845 for (i = 0; i < tcbtabsize; i++) {
1846 struct tcb *tcp = tcbtab[i];
1847 if (tcp->pid == pid && (tcp->flags & TCB_INUSE))
1848 return tcp;
1849 }
1850
1851 return NULL;
1852}
1853
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001854static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001855cleanup(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001856{
1857 int i;
1858 struct tcb *tcp;
Denys Vlasenko35218842012-01-29 21:17:56 +01001859 int fatal_sig;
1860
1861 /* 'interrupted' is a volatile object, fetch it only once */
1862 fatal_sig = interrupted;
1863 if (!fatal_sig)
1864 fatal_sig = SIGTERM;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001865
Roland McGrathee9d4352002-12-18 04:16:10 +00001866 for (i = 0; i < tcbtabsize; i++) {
1867 tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001868 if (!(tcp->flags & TCB_INUSE))
1869 continue;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001870 if (debug_flag)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001871 fprintf(stderr,
1872 "cleanup: looking at pid %u\n", tcp->pid);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001873 if (tcp->flags & TCB_STRACE_CHILD) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001874 kill(tcp->pid, SIGCONT);
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001875 kill(tcp->pid, fatal_sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001876 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001877 detach(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001878 }
1879 if (cflag)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001880 call_summary(shared_log);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001881}
1882
1883static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001884interrupt(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001885{
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001886 interrupted = sig;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001887}
1888
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001889static int
Denys Vlasenko12014262011-05-30 14:00:14 +02001890trace(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001891{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001892 struct rusage ru;
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001893 struct rusage *rup = cflag ? &ru : NULL;
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001894#ifdef __WALL
Roland McGratheb9e2e82009-06-02 16:49:22 -07001895 static int wait4_options = __WALL;
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001896#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001897
Roland McGratheb9e2e82009-06-02 16:49:22 -07001898 while (nprocs != 0) {
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001899 int pid;
1900 int wait_errno;
1901 int status, sig;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001902 int stopped;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001903 struct tcb *tcp;
1904 unsigned event;
1905
Denys Vlasenko222713a2009-03-17 14:29:59 +00001906 if (interrupted)
Roland McGratheb9e2e82009-06-02 16:49:22 -07001907 return 0;
1908 if (interactive)
1909 sigprocmask(SIG_SETMASK, &empty_set, NULL);
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001910#ifdef __WALL
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001911 pid = wait4(-1, &status, wait4_options, rup);
Roland McGrath5bc05552002-12-17 04:50:47 +00001912 if (pid < 0 && (wait4_options & __WALL) && errno == EINVAL) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001913 /* this kernel does not support __WALL */
1914 wait4_options &= ~__WALL;
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001915 pid = wait4(-1, &status, wait4_options, rup);
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001916 }
Roland McGrath5bc05552002-12-17 04:50:47 +00001917 if (pid < 0 && !(wait4_options & __WALL) && errno == ECHILD) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001918 /* most likely a "cloned" process */
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001919 pid = wait4(-1, &status, __WCLONE, rup);
1920 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001921 perror_msg("wait4(__WCLONE) failed");
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001922 }
1923 }
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001924#else
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001925 pid = wait4(-1, &status, 0, rup);
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001926#endif /* __WALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001927 wait_errno = errno;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001928 if (interactive)
1929 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001930
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001931 if (pid < 0) {
Roland McGratheb9e2e82009-06-02 16:49:22 -07001932 switch (wait_errno) {
1933 case EINTR:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001934 continue;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001935 case ECHILD:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001936 /*
1937 * We would like to verify this case
1938 * but sometimes a race in Solbourne's
1939 * version of SunOS sometimes reports
1940 * ECHILD before sending us SIGCHILD.
1941 */
Roland McGratheb9e2e82009-06-02 16:49:22 -07001942 return 0;
1943 default:
1944 errno = wait_errno;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001945 perror_msg("wait");
Roland McGratheb9e2e82009-06-02 16:49:22 -07001946 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001947 }
1948 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001949 if (pid == popen_pid) {
1950 if (WIFEXITED(status) || WIFSIGNALED(status))
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001951 popen_pid = 0;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001952 continue;
1953 }
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001954
1955 event = ((unsigned)status >> 16);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001956 if (debug_flag) {
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001957 char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko67559ad2012-03-13 12:05:27 +01001958 char evbuf[sizeof(",PTRACE_EVENT_?? (%u)") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001959 strcpy(buf, "???");
1960 if (WIFSIGNALED(status))
1961#ifdef WCOREDUMP
1962 sprintf(buf, "WIFSIGNALED,%ssig=%s",
1963 WCOREDUMP(status) ? "core," : "",
1964 signame(WTERMSIG(status)));
1965#else
1966 sprintf(buf, "WIFSIGNALED,sig=%s",
1967 signame(WTERMSIG(status)));
1968#endif
1969 if (WIFEXITED(status))
1970 sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status));
1971 if (WIFSTOPPED(status))
1972 sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status)));
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02001973#ifdef WIFCONTINUED
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001974 if (WIFCONTINUED(status))
1975 strcpy(buf, "WIFCONTINUED");
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02001976#endif
Denys Vlasenko67559ad2012-03-13 12:05:27 +01001977 evbuf[0] = '\0';
1978 if (event != 0) {
1979 static const char *const event_names[] = {
1980 [PTRACE_EVENT_CLONE] = "CLONE",
1981 [PTRACE_EVENT_FORK] = "FORK",
1982 [PTRACE_EVENT_VFORK] = "VFORK",
1983 [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE",
1984 [PTRACE_EVENT_EXEC] = "EXEC",
1985 [PTRACE_EVENT_EXIT] = "EXIT",
1986 };
1987 const char *e;
1988 if (event < ARRAY_SIZE(event_names))
1989 e = event_names[event];
1990 else {
1991 sprintf(buf, "?? (%u)", event);
1992 e = buf;
1993 }
1994 sprintf(evbuf, ",PTRACE_EVENT_%s", e);
1995 }
1996 fprintf(stderr, " [wait(0x%04x) = %u] %s%s\n", status, pid, buf, evbuf);
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001997 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001998
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001999 /* Look up 'pid' in our table. */
Denys Vlasenko5d645812011-08-20 12:48:18 +02002000 tcp = pid2tcb(pid);
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002001
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002002 if (!tcp) {
Roland McGrath41c48222008-07-18 00:25:10 +00002003 if (followfork) {
Denys Vlasenko418d66a2009-01-17 01:52:54 +00002004 tcp = alloctcb(pid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002005 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01002006 newoutf(tcp);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002007 if (!qflag)
Denys Vlasenko833fb132011-08-17 11:30:56 +02002008 fprintf(stderr, "Process %d attached\n",
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002009 pid);
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002010 } else {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002011 /* This can happen if a clone call used
2012 CLONE_PTRACE itself. */
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002013 if (WIFSTOPPED(status))
Denys Vlasenko97c503f2012-03-09 15:11:21 +01002014 ptrace(PTRACE_CONT, pid, (char *) 0, 0);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02002015 error_msg_and_die("Unknown pid: %u", pid);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002016 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002017 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002018
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002019 clear_regs();
2020 if (WIFSTOPPED(status))
2021 get_regs(pid);
2022
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002023 /* Under Linux, execve changes pid to thread leader's pid,
2024 * and we see this changed pid on EVENT_EXEC and later,
2025 * execve sysexit. Leader "disappears" without exit
2026 * notification. Let user know that, drop leader's tcb,
2027 * and fix up pid in execve thread's tcb.
2028 * Effectively, execve thread's tcb replaces leader's tcb.
2029 *
2030 * BTW, leader is 'stuck undead' (doesn't report WIFEXITED
2031 * on exit syscall) in multithreaded programs exactly
2032 * in order to handle this case.
2033 *
2034 * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0.
2035 * On 2.6 and earlier, it can return garbage.
2036 */
2037 if (event == PTRACE_EVENT_EXEC && os_release >= KERNEL_VERSION(3,0,0)) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002038 FILE *fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002039 struct tcb *execve_thread;
2040 long old_pid = 0;
2041
2042 if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) < 0)
2043 goto dont_switch_tcbs;
2044 if (old_pid <= 0 || old_pid == pid)
2045 goto dont_switch_tcbs;
2046 execve_thread = pid2tcb(old_pid);
2047 /* It should be !NULL, but I feel paranoid */
2048 if (!execve_thread)
2049 goto dont_switch_tcbs;
2050
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002051 if (execve_thread->curcol != 0) {
2052 /*
2053 * One case we are here is -ff:
2054 * try "strace -oLOG -ff test/threaded_execve"
2055 */
2056 fprintf(execve_thread->outf, " <pid changed to %d ...>\n", pid);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002057 /*execve_thread->curcol = 0; - no need, see code below */
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002058 }
2059 /* Swap output FILEs (needed for -ff) */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002060 fp = execve_thread->outf;
2061 execve_thread->outf = tcp->outf;
2062 tcp->outf = fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002063 /* And their column positions */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002064 execve_thread->curcol = tcp->curcol;
2065 tcp->curcol = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002066 /* Drop leader, but close execve'd thread outfile (if -ff) */
2067 droptcb(tcp);
2068 /* Switch to the thread, reusing leader's outfile and pid */
2069 tcp = execve_thread;
2070 tcp->pid = pid;
2071 if (cflag != CFLAG_ONLY_STATS) {
2072 printleader(tcp);
2073 tprintf("+++ superseded by execve in pid %lu +++\n", old_pid);
2074 line_ended();
2075 tcp->flags |= TCB_REPRINT;
2076 }
2077 }
2078 dont_switch_tcbs:
2079
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002080 if (event == PTRACE_EVENT_EXEC) {
2081 if (detach_on_execve && !skip_one_b_execve)
2082 detach(tcp); /* do "-b execve" thingy */
2083 skip_one_b_execve = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002084 }
2085
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002086 /* Set current output file */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002087 current_tcp = tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002088
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002089 if (cflag) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002090 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2091 tcp->stime = ru.ru_stime;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002092 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002093
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002094 if (WIFSIGNALED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002095 if (pid == strace_child)
2096 exit_code = 0x100 | WTERMSIG(status);
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002097 if (cflag != CFLAG_ONLY_STATS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01002098 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)
2099 ) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002100 printleader(tcp);
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002101#ifdef WCOREDUMP
Denys Vlasenko000b6012012-01-28 01:25:03 +01002102 tprintf("+++ killed by %s %s+++\n",
Roland McGrath2efe8792004-01-13 09:59:45 +00002103 signame(WTERMSIG(status)),
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002104 WCOREDUMP(status) ? "(core dumped) " : "");
2105#else
Denys Vlasenko000b6012012-01-28 01:25:03 +01002106 tprintf("+++ killed by %s +++\n",
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002107 signame(WTERMSIG(status)));
Roland McGrath2efe8792004-01-13 09:59:45 +00002108#endif
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002109 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002110 }
2111 droptcb(tcp);
2112 continue;
2113 }
2114 if (WIFEXITED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002115 if (pid == strace_child)
2116 exit_code = WEXITSTATUS(status);
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01002117 if (cflag != CFLAG_ONLY_STATS &&
2118 qflag < 2) {
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002119 printleader(tcp);
Denys Vlasenko000b6012012-01-28 01:25:03 +01002120 tprintf("+++ exited with %d +++\n", WEXITSTATUS(status));
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002121 line_ended();
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002122 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002123 droptcb(tcp);
2124 continue;
2125 }
2126 if (!WIFSTOPPED(status)) {
2127 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
2128 droptcb(tcp);
2129 continue;
2130 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002131
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002132 /* Is this the very first time we see this tracee stopped? */
2133 if (tcp->flags & TCB_STARTUP) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002134 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002135 fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n", tcp->pid);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002136 tcp->flags &= ~TCB_STARTUP;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002137 if (tcp->flags & TCB_BPTSET) {
Roland McGrath02203312007-06-11 22:06:31 +00002138 /*
2139 * One example is a breakpoint inherited from
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002140 * parent through fork().
Roland McGrath02203312007-06-11 22:06:31 +00002141 */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002142 if (clearbpt(tcp) < 0) {
2143 /* Pretty fatal */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002144 droptcb(tcp);
2145 cleanup();
2146 return -1;
2147 }
2148 }
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002149 if (ptrace_setoptions) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002150 if (debug_flag)
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002151 fprintf(stderr, "setting opts %x on pid %d\n", ptrace_setoptions, tcp->pid);
2152 if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) {
2153 if (errno != ESRCH) {
2154 /* Should never happen, really */
2155 perror_msg_and_die("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02002156 }
2157 }
2158 }
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002159 }
2160
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002161 sig = WSTOPSIG(status);
2162
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002163 if (event != 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002164 /* Ptrace event */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002165#if USE_SEIZE
Denys Vlasenko26bc0602012-07-10 16:36:32 +02002166 if (event == PTRACE_EVENT_STOP) {
Denys Vlasenko67038162012-01-29 16:46:46 +01002167 /*
2168 * PTRACE_INTERRUPT-stop or group-stop.
2169 * PTRACE_INTERRUPT-stop has sig == SIGTRAP here.
2170 */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002171 if (sig == SIGSTOP
2172 || sig == SIGTSTP
2173 || sig == SIGTTIN
2174 || sig == SIGTTOU
2175 ) {
2176 stopped = 1;
2177 goto show_stopsig;
2178 }
2179 }
2180#endif
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002181 goto restart_tracee_with_sig_0;
2182 }
2183
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002184 /* Is this post-attach SIGSTOP?
2185 * Interestingly, the process may stop
2186 * with STOPSIG equal to some other signal
2187 * than SIGSTOP if we happend to attach
2188 * just before the process takes a signal.
2189 */
2190 if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002191 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002192 fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid);
2193 tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002194 goto restart_tracee_with_sig_0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002195 }
2196
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002197 if (sig != syscall_trap_sig) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002198 siginfo_t si;
2199
2200 /* Nonzero (true) if tracee is stopped by signal
2201 * (as opposed to "tracee received signal").
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002202 * TODO: shouldn't we check for errno == EINVAL too?
2203 * We can get ESRCH instead, you know...
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002204 */
2205 stopped = (ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002206#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002207 show_stopsig:
Denys Vlasenko67038162012-01-29 16:46:46 +01002208#endif
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002209 if (cflag != CFLAG_ONLY_STATS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01002210 && (qual_flags[sig] & QUAL_SIGNAL)
2211 ) {
Dmitry V. Levinc15dfc72011-03-10 14:44:45 +00002212#if defined(PT_CR_IPSR) && defined(PT_CR_IIP)
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002213 long pc = 0;
2214 long psr = 0;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002215
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002216 upeek(tcp, PT_CR_IPSR, &psr);
2217 upeek(tcp, PT_CR_IIP, &pc);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002218
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002219# define PSR_RI 41
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002220 pc += (psr >> PSR_RI) & 0x3;
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002221# define PC_FORMAT_STR " @ %lx"
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002222# define PC_FORMAT_ARG , pc
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002223#else
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002224# define PC_FORMAT_STR ""
2225# define PC_FORMAT_ARG /* nothing */
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002226#endif
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002227 if (qflag < 2 && !hide_log_until_execve) {
Daniel P. Berrange01997cf2013-05-13 11:30:55 +01002228 printleader(tcp);
2229 if (!stopped) {
2230 tprintf("--- %s ", signame(sig));
2231 printsiginfo(&si, verbose(tcp));
2232 tprintf(PC_FORMAT_STR " ---\n"
2233 PC_FORMAT_ARG);
2234 } else
2235 tprintf("--- stopped by %s" PC_FORMAT_STR " ---\n",
2236 signame(sig)
2237 PC_FORMAT_ARG);
2238 line_ended();
2239 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002240 }
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002241
2242 if (!stopped)
2243 /* It's signal-delivery-stop. Inject the signal */
2244 goto restart_tracee;
2245
2246 /* It's group-stop */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002247#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002248 if (use_seize) {
2249 /*
2250 * This ends ptrace-stop, but does *not* end group-stop.
2251 * This makes stopping signals work properly on straced process
2252 * (that is, process really stops. It used to continue to run).
2253 */
2254 if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) {
2255 cleanup();
2256 return -1;
2257 }
2258 continue;
2259 }
2260 /* We don't have PTRACE_LISTEN support... */
2261#endif
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002262 goto restart_tracee;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002263 }
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002264
2265 /* We handled quick cases, we are permitted to interrupt now. */
Roland McGrath02203312007-06-11 22:06:31 +00002266 if (interrupted)
2267 return 0;
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002268
2269 /* This should be syscall entry or exit.
2270 * (Or it still can be that pesky post-execve SIGTRAP!)
2271 * Handle it.
2272 */
Denys Vlasenko23506752012-03-21 10:32:49 +01002273 if (trace_syscall(tcp) < 0) {
2274 /* ptrace() failed in trace_syscall().
Roland McGratheb9e2e82009-06-02 16:49:22 -07002275 * Likely a result of process disappearing mid-flight.
Denys Vlasenko23506752012-03-21 10:32:49 +01002276 * Observed case: exit_group() or SIGKILL terminating
Denys Vlasenkof1e69032012-01-04 15:15:26 +01002277 * all processes in thread group.
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002278 * We assume that ptrace error was caused by process death.
Denys Vlasenkof2025022012-03-09 15:29:45 +01002279 * We used to detach(tcp) here, but since we no longer
2280 * implement "detach before death" policy/hack,
2281 * we can let this process to report its death to us
2282 * normally, via WIFEXITED or WIFSIGNALED wait status.
2283 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002284 continue;
2285 }
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002286 restart_tracee_with_sig_0:
2287 sig = 0;
2288 restart_tracee:
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002289 if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002290 cleanup();
2291 return -1;
2292 }
2293 }
2294 return 0;
2295}
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002296
2297int
2298main(int argc, char *argv[])
2299{
2300 init(argc, argv);
2301
2302 /* Run main tracing loop */
2303 if (trace() < 0)
2304 return 1;
2305
2306 cleanup();
2307 fflush(NULL);
Dmitry V. Levincf534362012-07-12 20:54:46 +00002308 if (shared_log != stderr)
2309 fclose(shared_log);
2310 if (popen_pid) {
2311 while (waitpid(popen_pid, NULL, 0) < 0 && errno == EINTR)
2312 ;
2313 }
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002314 if (exit_code > 0xff) {
2315 /* Avoid potential core file clobbering. */
Dmitry V. Levinc8938e02013-03-20 21:40:15 +00002316 struct_rlimit rlim = {0, 0};
2317 set_rlimit(RLIMIT_CORE, &rlim);
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002318
2319 /* Child was killed by a signal, mimic that. */
2320 exit_code &= 0xff;
2321 signal(exit_code, SIG_DFL);
2322 raise(exit_code);
2323 /* Paranoia - what if this signal is not fatal?
2324 Exit with 128 + signo then. */
2325 exit_code += 128;
2326 }
2327
2328 return exit_code;
2329}