blob: de5b08a06c25ccb2d52f77fdf265d9973acb21d6 [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00006 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000029 */
30
31#include "defs.h"
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020032#include <stdarg.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000033#include <sys/param.h>
34#include <fcntl.h>
35#include <sys/resource.h>
36#include <sys/wait.h>
37#include <sys/stat.h>
38#include <pwd.h>
39#include <grp.h>
Roland McGrath70b08532004-04-09 00:25:21 +000040#include <dirent.h>
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +010041#include <sys/utsname.h>
Denys Vlasenko84703742012-02-25 02:38:52 +010042#if defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +000043# include <asm/ptrace_offsets.h>
44#endif
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010045/* In some libc, these aren't declared. Do it ourself: */
Denys Vlasenko96d5a762008-12-29 19:13:27 +000046extern char **environ;
Denys Vlasenko418d66a2009-01-17 01:52:54 +000047extern int optind;
48extern char *optarg;
Denys Vlasenko96d5a762008-12-29 19:13:27 +000049
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010050
51#if defined __NR_tkill
52# define my_tkill(tid, sig) syscall(__NR_tkill, (tid), (sig))
53#else
54 /* kill() may choose arbitrarily the target task of the process group
55 while we later wait on a that specific TID. PID process waits become
56 TID task specific waits for a process under ptrace(2). */
Denys Vlasenko978fbc92012-09-13 10:28:43 +020057# warning "tkill(2) not available, risk of strace hangs!"
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010058# define my_tkill(tid, sig) kill((tid), (sig))
59#endif
60
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +010061/* Glue for systems without a MMU that cannot provide fork() */
62#if !defined(HAVE_FORK)
63# undef NOMMU_SYSTEM
64# define NOMMU_SYSTEM 1
65#endif
66#if NOMMU_SYSTEM
67# define fork() vfork()
68#endif
69
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010070cflag_t cflag = CFLAG_NONE;
71unsigned int followfork = 0;
Denys Vlasenkof44cce42011-06-21 14:34:10 +020072unsigned int ptrace_setoptions = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010073unsigned int xflag = 0;
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +010074bool need_fork_exec_workarounds = 0;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010075bool debug_flag = 0;
76bool Tflag = 0;
77bool qflag = 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020078/* Which WSTOPSIG(status) value marks syscall traps? */
Denys Vlasenko75422762011-05-27 14:36:01 +020079static unsigned int syscall_trap_sig = SIGTRAP;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +010080static unsigned int tflag = 0;
81static bool iflag = 0;
82static bool rflag = 0;
83static bool print_pid_pfx = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +010084
85/* -I n */
86enum {
87 INTR_NOT_SET = 0,
88 INTR_ANYWHERE = 1, /* don't block/ignore any signals */
89 INTR_WHILE_WAIT = 2, /* block fatal signals while decoding syscall. default */
90 INTR_NEVER = 3, /* block fatal signals. default if '-o FILE PROG' */
91 INTR_BLOCK_TSTP_TOO = 4, /* block fatal signals and SIGTSTP (^Z) */
92 NUM_INTR_OPTS
93};
94static int opt_intr;
95/* We play with signal mask only if this mode is active: */
96#define interactive (opt_intr == INTR_WHILE_WAIT)
97
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +000098/*
99 * daemonized_tracer supports -D option.
100 * With this option, strace forks twice.
101 * Unlike normal case, with -D *grandparent* process exec's,
102 * becoming a traced process. Child exits (this prevents traced process
103 * from having children it doesn't expect to have), and grandchild
104 * attaches to grandparent similarly to strace -p PID.
105 * This allows for more transparent interaction in cases
106 * when process and its parent are communicating via signals,
107 * wait() etc. Without -D, strace process gets lodged in between,
108 * disrupting parent<->child link.
109 */
110static bool daemonized_tracer = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000111
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100112#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100113static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP;
114# define use_seize (post_attach_sigstop == 0)
115#else
116# define post_attach_sigstop TCB_IGNORE_ONE_SIGSTOP
117# define use_seize 0
118#endif
119
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000120/* Sometimes we want to print only succeeding syscalls. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100121bool not_failing_only = 0;
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000122
Grant Edwards8a082772011-04-07 20:25:40 +0000123/* Show path associated with fd arguments */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100124bool show_fd_path = 0;
Grant Edwards8a082772011-04-07 20:25:40 +0000125
126/* are we filtering traces based on paths? */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100127bool tracing_paths = 0;
Grant Edwards8a082772011-04-07 20:25:40 +0000128
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100129static bool detach_on_execve = 0;
130static bool skip_startup_execve = 0;
131
Dmitry V. Levina6809652008-11-10 17:14:58 +0000132static int exit_code = 0;
133static int strace_child = 0;
Denys Vlasenko75422762011-05-27 14:36:01 +0200134static int strace_tracer_pid = 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -0700135
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000136static char *username = NULL;
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200137static uid_t run_uid;
138static gid_t run_gid;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000139
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100140unsigned int max_strlen = DEFAULT_STRLEN;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000141static int acolumn = DEFAULT_ACOLUMN;
Denys Vlasenko102ec492011-08-25 01:27:59 +0200142static char *acolumn_spaces;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100143
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000144static char *outfname = NULL;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100145/* If -ff, points to stderr. Else, it's our common output log */
146static FILE *shared_log;
147
Denys Vlasenko000b6012012-01-28 01:25:03 +0100148struct tcb *printing_tcp = NULL;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100149static struct tcb *current_tcp;
150
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200151static struct tcb **tcbtab;
Denys Vlasenko2b60c352011-06-22 12:45:25 +0200152static unsigned int nprocs, tcbtabsize;
Denys Vlasenkoead73bd2011-06-24 22:49:58 +0200153static const char *progname;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000154
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100155unsigned os_release; /* generated from uname()'s u.release */
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +0100156
Denys Vlasenko4c196382012-01-04 15:11:09 +0100157static int detach(struct tcb *tcp);
Andreas Schwabe5355de2009-10-27 16:56:43 +0100158static int trace(void);
159static void cleanup(void);
160static void interrupt(int sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000161static sigset_t empty_set, blocked_set;
162
163#ifdef HAVE_SIG_ATOMIC_T
164static volatile sig_atomic_t interrupted;
Denys Vlasenkoa3559252012-01-29 16:43:51 +0100165#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000166static volatile int interrupted;
Denys Vlasenkoa3559252012-01-29 16:43:51 +0100167#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000168
Denys Vlasenko2c4fb902012-03-15 17:24:49 +0100169#ifndef HAVE_STRERROR
170
171#if !HAVE_DECL_SYS_ERRLIST
172extern int sys_nerr;
173extern char *sys_errlist[];
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +0100174#endif
Denys Vlasenko2c4fb902012-03-15 17:24:49 +0100175
176const char *
177strerror(int err_no)
178{
179 static char buf[sizeof("Unknown error %d") + sizeof(int)*3];
180
181 if (err_no < 1 || err_no >= sys_nerr) {
182 sprintf(buf, "Unknown error %d", err_no);
183 return buf;
184 }
185 return sys_errlist[err_no];
186}
187
188#endif /* HAVE_STERRROR */
189
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000190static void
Denys Vlasenkocb2ad002011-06-23 13:05:29 +0200191usage(FILE *ofp, int exitval)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000192{
193 fprintf(ofp, "\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200194usage: strace [-CdffhiqrtttTvVxxy] [-I n] [-e expr]...\n\
195 [-a column] [-o file] [-s strsize] [-P path]...\n\
196 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
197 or: strace -c[df] [-I n] [-e expr]... [-O overhead] [-S sortby]\n\
198 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000199-c -- count time, calls, and errors for each syscall and report summary\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200200-C -- like -c but also print regular output\n\
Denys Vlasenko3e084ac2012-03-15 13:39:05 +0100201-d -- enable debug output to stderr\n\
Denys Vlasenkob51581e2012-01-29 16:53:03 +0100202-D -- run tracer process as a detached grandchild, not as parent\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000203-f -- follow forks, -ff -- with output into separate files\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000204-i -- print instruction pointer at time of syscall\n\
205-q -- suppress messages about attaching, detaching, etc.\n\
206-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
Denys Vlasenkocdab1be2012-02-03 12:17:57 +0100207-T -- print time spent in each syscall\n\
208-v -- verbose mode: print unabbreviated argv, stat, termios, etc. args\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000209-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000210-y -- print paths associated with file descriptor arguments\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200211-h -- print help message, -V -- print version\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000212-a column -- alignment COLUMN for printing syscall results (default %d)\n\
213-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
Denys Vlasenko38e79bb2013-02-26 11:33:54 +0100214 options: trace, abbrev, verbose, raw, signal, read, write\n\
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +0200215-I interruptible --\n\
216 1: no signals are blocked\n\
217 2: fatal signals are blocked while decoding syscall (default)\n\
218 3: fatal signals are always blocked (default if '-o FILE PROG')\n\
219 4: fatal signals and SIGTSTP (^Z) are always blocked\n\
220 (useful to make 'strace -o FILE PROG' not stop on ^Z)\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000221-o file -- send trace output to FILE instead of stderr\n\
222-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
223-p pid -- trace process with process id PID, may be repeated\n\
224-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
225-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
226-u username -- run command as username handling setuid and/or setgid\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000227-E var=val -- put var=val in the environment for command\n\
228-E var -- remove var from the environment for command\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000229-P path -- trace accesses to path\n\
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100230"
Denys Vlasenko38e79bb2013-02-26 11:33:54 +0100231/* ancient, no one should use it
232-F -- attempt to follow vforks (deprecated, use -f)\n\
233 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100234/* this is broken, so don't document it
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000235-z -- print only succeeding syscalls\n\
Denys Vlasenko61e7aad2012-03-15 13:44:17 +0100236 */
237/* experimental, don't document it yet (option letter may change in the future!)
238-b -- detach on successful execve\n\
239 */
Roland McGrathde6e5332003-01-24 04:31:23 +0000240, DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000241 exit(exitval);
242}
243
Denys Vlasenko75422762011-05-27 14:36:01 +0200244static void die(void) __attribute__ ((noreturn));
245static void die(void)
246{
247 if (strace_tracer_pid == getpid()) {
248 cflag = 0;
249 cleanup();
250 }
251 exit(1);
252}
253
254static void verror_msg(int err_no, const char *fmt, va_list p)
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200255{
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100256 char *msg;
257
Dmitry V. Levin44d05322011-06-09 15:50:41 +0000258 fflush(NULL);
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100259
260 /* We want to print entire message with single fprintf to ensure
261 * message integrity if stderr is shared with other programs.
262 * Thus we use vasprintf + single fprintf.
263 */
264 msg = NULL;
Denys Vlasenkocfad5432012-01-24 12:48:02 +0100265 if (vasprintf(&msg, fmt, p) >= 0) {
Denys Vlasenko82bb78c2012-01-24 10:17:18 +0100266 if (err_no)
267 fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(err_no));
268 else
269 fprintf(stderr, "%s: %s\n", progname, msg);
270 free(msg);
271 } else {
272 /* malloc in vasprintf failed, try it without malloc */
273 fprintf(stderr, "%s: ", progname);
274 vfprintf(stderr, fmt, p);
275 if (err_no)
276 fprintf(stderr, ": %s\n", strerror(err_no));
277 else
278 putc('\n', stderr);
279 }
280 /* We don't switch stderr to buffered, thus fprintf(stderr)
281 * always flushes its output and this is not necessary: */
282 /* fflush(stderr); */
Denys Vlasenko75422762011-05-27 14:36:01 +0200283}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200284
Denys Vlasenko75422762011-05-27 14:36:01 +0200285void error_msg(const char *fmt, ...)
286{
287 va_list p;
288 va_start(p, fmt);
289 verror_msg(0, fmt, p);
290 va_end(p);
291}
292
293void error_msg_and_die(const char *fmt, ...)
294{
295 va_list p;
296 va_start(p, fmt);
297 verror_msg(0, fmt, p);
298 die();
299}
300
301void perror_msg(const char *fmt, ...)
302{
303 va_list p;
304 va_start(p, fmt);
305 verror_msg(errno, fmt, p);
306 va_end(p);
307}
308
309void perror_msg_and_die(const char *fmt, ...)
310{
311 va_list p;
312 va_start(p, fmt);
313 verror_msg(errno, fmt, p);
314 die();
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200315}
316
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200317void die_out_of_memory(void)
318{
319 static bool recursed = 0;
320 if (recursed)
321 exit(1);
322 recursed = 1;
323 error_msg_and_die("Out of memory");
324}
325
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000326static void
327error_opt_arg(int opt, const char *arg)
328{
329 error_msg_and_die("Invalid -%c argument: '%s'", opt, arg);
330}
331
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +0100332#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100333static int
334ptrace_attach_or_seize(int pid)
335{
336 int r;
337 if (!use_seize)
338 return ptrace(PTRACE_ATTACH, pid, 0, 0);
Denys Vlasenko26bc0602012-07-10 16:36:32 +0200339 r = ptrace(PTRACE_SEIZE, pid, 0, 0);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100340 if (r)
341 return r;
342 r = ptrace(PTRACE_INTERRUPT, pid, 0, 0);
343 return r;
344}
345#else
346# define ptrace_attach_or_seize(pid) ptrace(PTRACE_ATTACH, (pid), 0, 0)
347#endif
348
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100349/*
350 * Used when we want to unblock stopped traced process.
351 * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL.
352 * Returns 0 on success or if error was ESRCH
353 * (presumably process was killed while we talk to it).
354 * Otherwise prints error message and returns -1.
355 */
356static int
357ptrace_restart(int op, struct tcb *tcp, int sig)
358{
359 int err;
360 const char *msg;
361
362 errno = 0;
363 ptrace(op, tcp->pid, (void *) 0, (long) sig);
364 err = errno;
Denys Vlasenko23506752012-03-21 10:32:49 +0100365 if (!err)
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100366 return 0;
367
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100368 msg = "SYSCALL";
369 if (op == PTRACE_CONT)
370 msg = "CONT";
371 if (op == PTRACE_DETACH)
372 msg = "DETACH";
373#ifdef PTRACE_LISTEN
374 if (op == PTRACE_LISTEN)
375 msg = "LISTEN";
376#endif
Denys Vlasenko23506752012-03-21 10:32:49 +0100377 /*
378 * Why curcol != 0? Otherwise sometimes we get this:
379 *
380 * 10252 kill(10253, SIGKILL) = 0
381 * <ptrace(SYSCALL,10252):No such process>10253 ...next decode...
382 *
383 * 10252 died after we retrieved syscall exit data,
384 * but before we tried to restart it. Log looks ugly.
385 */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100386 if (current_tcp && current_tcp->curcol != 0) {
Denys Vlasenko23506752012-03-21 10:32:49 +0100387 tprintf(" <ptrace(%s):%s>\n", msg, strerror(err));
388 line_ended();
389 }
390 if (err == ESRCH)
391 return 0;
392 errno = err;
Denys Vlasenko852f98a2012-03-20 16:26:25 +0100393 perror_msg("ptrace(PTRACE_%s,pid:%d,sig:%d)", msg, tcp->pid, sig);
394 return -1;
395}
396
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200397static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000398set_cloexec_flag(int fd)
399{
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200400 int flags, newflags;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000401
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200402 flags = fcntl(fd, F_GETFD);
403 if (flags < 0) {
404 /* Can happen only if fd is bad.
405 * Should never happen: if it does, we have a bug
406 * in the caller. Therefore we just abort
407 * instead of propagating the error.
408 */
409 perror_msg_and_die("fcntl(%d, F_GETFD)", fd);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000410 }
411
412 newflags = flags | FD_CLOEXEC;
413 if (flags == newflags)
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200414 return;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000415
Denys Vlasenko1f532ab2011-06-22 13:11:23 +0200416 fcntl(fd, F_SETFD, newflags); /* never fails */
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000417}
418
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100419static void kill_save_errno(pid_t pid, int sig)
420{
421 int saved_errno = errno;
422
423 (void) kill(pid, sig);
424 errno = saved_errno;
425}
426
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100427/*
428 * When strace is setuid executable, we have to swap uids
429 * before and after filesystem and process management operations.
430 */
431static void
432swap_uid(void)
433{
434 int euid = geteuid(), uid = getuid();
435
436 if (euid != uid && setreuid(euid, uid) < 0) {
437 perror_msg_and_die("setreuid");
438 }
439}
440
441#if _LFS64_LARGEFILE
442# define fopen_for_output fopen64
443#else
444# define fopen_for_output fopen
445#endif
446
447static FILE *
448strace_fopen(const char *path)
449{
450 FILE *fp;
451
452 swap_uid();
453 fp = fopen_for_output(path, "w");
454 if (!fp)
455 perror_msg_and_die("Can't fopen '%s'", path);
456 swap_uid();
457 set_cloexec_flag(fileno(fp));
458 return fp;
459}
460
461static int popen_pid = 0;
462
463#ifndef _PATH_BSHELL
464# define _PATH_BSHELL "/bin/sh"
465#endif
466
467/*
468 * We cannot use standard popen(3) here because we have to distinguish
469 * popen child process from other processes we trace, and standard popen(3)
470 * does not export its child's pid.
471 */
472static FILE *
473strace_popen(const char *command)
474{
475 FILE *fp;
476 int fds[2];
477
478 swap_uid();
479 if (pipe(fds) < 0)
480 perror_msg_and_die("pipe");
481
482 set_cloexec_flag(fds[1]); /* never fails */
483
484 popen_pid = vfork();
485 if (popen_pid == -1)
486 perror_msg_and_die("vfork");
487
488 if (popen_pid == 0) {
489 /* child */
490 close(fds[1]);
491 if (fds[0] != 0) {
492 if (dup2(fds[0], 0))
493 perror_msg_and_die("dup2");
494 close(fds[0]);
495 }
496 execl(_PATH_BSHELL, "sh", "-c", command, NULL);
497 perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL);
498 }
499
500 /* parent */
501 close(fds[0]);
502 swap_uid();
503 fp = fdopen(fds[1], "w");
504 if (!fp)
505 die_out_of_memory();
506 return fp;
507}
508
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100509void
510tprintf(const char *fmt, ...)
511{
512 va_list args;
513
514 va_start(args, fmt);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100515 if (current_tcp) {
Denys Vlasenko6e4f3c12012-04-16 18:22:19 +0200516 int n = strace_vfprintf(current_tcp->outf, fmt, args);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100517 if (n < 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100518 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000519 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100520 } else
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100521 current_tcp->curcol += n;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100522 }
523 va_end(args);
524}
525
526void
527tprints(const char *str)
528{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100529 if (current_tcp) {
Denys Vlasenko142aee02012-04-16 18:10:15 +0200530 int n = fputs_unlocked(str, current_tcp->outf);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100531 if (n >= 0) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100532 current_tcp->curcol += strlen(str);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100533 return;
534 }
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100535 if (current_tcp->outf != stderr)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000536 perror_msg("%s", outfname);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100537 }
538}
539
540void
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100541line_ended(void)
542{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100543 if (current_tcp) {
544 current_tcp->curcol = 0;
545 fflush(current_tcp->outf);
546 }
547 if (printing_tcp) {
548 printing_tcp->curcol = 0;
549 printing_tcp = NULL;
550 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100551}
552
553void
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100554printleader(struct tcb *tcp)
555{
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100556 /* If -ff, "previous tcb we printed" is always the same as current,
557 * because we have per-tcb output files.
558 */
559 if (followfork >= 2)
560 printing_tcp = tcp;
561
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100562 if (printing_tcp) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100563 current_tcp = printing_tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100564 if (printing_tcp->curcol != 0 && (followfork < 2 || printing_tcp == tcp)) {
565 /*
566 * case 1: we have a shared log (i.e. not -ff), and last line
567 * wasn't finished (same or different tcb, doesn't matter).
568 * case 2: split log, we are the same tcb, but our last line
569 * didn't finish ("SIGKILL nuked us after syscall entry" etc).
570 */
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100571 tprints(" <unfinished ...>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +0100572 printing_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100573 }
574 }
575
576 printing_tcp = tcp;
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100577 current_tcp = tcp;
578 current_tcp->curcol = 0;
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100579
580 if (print_pid_pfx)
581 tprintf("%-5d ", tcp->pid);
582 else if (nprocs > 1 && !outfname)
583 tprintf("[pid %5u] ", tcp->pid);
584
585 if (tflag) {
586 char str[sizeof("HH:MM:SS")];
587 struct timeval tv, dtv;
588 static struct timeval otv;
589
590 gettimeofday(&tv, NULL);
591 if (rflag) {
592 if (otv.tv_sec == 0)
593 otv = tv;
594 tv_sub(&dtv, &tv, &otv);
595 tprintf("%6ld.%06ld ",
596 (long) dtv.tv_sec, (long) dtv.tv_usec);
597 otv = tv;
598 }
599 else if (tflag > 2) {
600 tprintf("%ld.%06ld ",
601 (long) tv.tv_sec, (long) tv.tv_usec);
602 }
603 else {
604 time_t local = tv.tv_sec;
605 strftime(str, sizeof(str), "%T", localtime(&local));
606 if (tflag > 1)
607 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
608 else
609 tprintf("%s ", str);
610 }
611 }
612 if (iflag)
613 printcall(tcp);
614}
615
616void
617tabto(void)
618{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100619 if (current_tcp->curcol < acolumn)
620 tprints(acolumn_spaces + current_tcp->curcol);
Denys Vlasenko2e856a12012-03-12 23:02:26 +0100621}
622
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100623/* Should be only called directly *after successful attach* to a tracee.
624 * Otherwise, "strace -oFILE -ff -p<nonexistant_pid>"
625 * may create bogus empty FILE.<nonexistant_pid>, and then die.
626 */
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200627static void
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000628newoutf(struct tcb *tcp)
629{
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100630 tcp->outf = shared_log; /* if not -ff mode, the same file is for all */
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100631 if (followfork >= 2) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000632 char name[520 + sizeof(int) * 3];
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000633 sprintf(name, "%.512s.%u", outfname, tcp->pid);
Denys Vlasenko3d5ed412011-06-22 13:17:16 +0200634 tcp->outf = strace_fopen(name);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000635 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000636}
637
Denys Vlasenko558e5122012-03-12 23:32:16 +0100638static void
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100639expand_tcbtab(void)
640{
641 /* Allocate some more TCBs and expand the table.
642 We don't want to relocate the TCBs because our
643 callers have pointers and it would be a pain.
644 So tcbtab is a table of pointers. Since we never
645 free the TCBs, we allocate a single chunk of many. */
646 int i = tcbtabsize;
647 struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0]));
648 struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0]));
649 if (!newtab || !newtcbs)
650 die_out_of_memory();
651 tcbtabsize *= 2;
652 tcbtab = newtab;
653 while (i < tcbtabsize)
654 tcbtab[i++] = newtcbs++;
655}
656
657static struct tcb *
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100658alloctcb(int pid)
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100659{
660 int i;
661 struct tcb *tcp;
662
663 if (nprocs == tcbtabsize)
664 expand_tcbtab();
665
666 for (i = 0; i < tcbtabsize; i++) {
667 tcp = tcbtab[i];
668 if ((tcp->flags & TCB_INUSE) == 0) {
669 memset(tcp, 0, sizeof(*tcp));
670 tcp->pid = pid;
671 tcp->flags = TCB_INUSE;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100672#if SUPPORTED_PERSONALITIES > 1
673 tcp->currpers = current_personality;
674#endif
675 nprocs++;
676 if (debug_flag)
677 fprintf(stderr, "new tcb for pid %d, active tcbs:%d\n", tcp->pid, nprocs);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100678 return tcp;
679 }
680 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100681 error_msg_and_die("bug in alloctcb");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100682}
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100683
684static void
685droptcb(struct tcb *tcp)
686{
687 if (tcp->pid == 0)
688 return;
689
690 nprocs--;
691 if (debug_flag)
692 fprintf(stderr, "dropped tcb for pid %d, %d remain\n", tcp->pid, nprocs);
693
694 if (tcp->outf) {
Denys Vlasenko8511f2a2012-03-22 09:35:51 +0100695 if (followfork >= 2) {
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100696 if (tcp->curcol != 0)
697 fprintf(tcp->outf, " <detached ...>\n");
698 fclose(tcp->outf);
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100699 } else {
700 if (printing_tcp == tcp && tcp->curcol != 0)
701 fprintf(tcp->outf, " <detached ...>\n");
702 fflush(tcp->outf);
703 }
704 }
705
Denys Vlasenko6764f8f2012-03-22 09:56:20 +0100706 if (current_tcp == tcp)
707 current_tcp = NULL;
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100708 if (printing_tcp == tcp)
709 printing_tcp = NULL;
710
711 memset(tcp, 0, sizeof(*tcp));
712}
713
714/* detach traced process; continue with sig
715 * Never call DETACH twice on the same process as both unattached and
716 * attached-unstopped processes give the same ESRCH. For unattached process we
717 * would SIGSTOP it and wait for its SIGSTOP notification forever.
718 */
719static int
720detach(struct tcb *tcp)
721{
722 int error;
723 int status, sigstop_expected;
724
725 if (tcp->flags & TCB_BPTSET)
726 clearbpt(tcp);
727
728 /*
729 * Linux wrongly insists the child be stopped
730 * before detaching. Arghh. We go through hoops
731 * to make a clean break of things.
732 */
733#if defined(SPARC)
Denys Vlasenko978fbc92012-09-13 10:28:43 +0200734# undef PTRACE_DETACH
735# define PTRACE_DETACH PTRACE_SUNDETACH
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100736#endif
737
738 error = 0;
739 sigstop_expected = 0;
740 if (tcp->flags & TCB_ATTACHED) {
741 /*
742 * We attached but possibly didn't see the expected SIGSTOP.
743 * We must catch exactly one as otherwise the detached process
744 * would be left stopped (process state T).
745 */
746 sigstop_expected = (tcp->flags & TCB_IGNORE_ONE_SIGSTOP);
747 error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, 0);
748 if (error == 0) {
749 /* On a clear day, you can see forever. */
750 }
751 else if (errno != ESRCH) {
752 /* Shouldn't happen. */
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000753 perror_msg("%s", "detach: ptrace(PTRACE_DETACH, ...)");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100754 }
755 else if (my_tkill(tcp->pid, 0) < 0) {
756 if (errno != ESRCH)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000757 perror_msg("%s", "detach: checking sanity");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100758 }
759 else if (!sigstop_expected && my_tkill(tcp->pid, SIGSTOP) < 0) {
760 if (errno != ESRCH)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000761 perror_msg("%s", "detach: stopping child");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100762 }
763 else
764 sigstop_expected = 1;
765 }
766
767 if (sigstop_expected) {
768 for (;;) {
769#ifdef __WALL
770 if (waitpid(tcp->pid, &status, __WALL) < 0) {
771 if (errno == ECHILD) /* Already gone. */
772 break;
773 if (errno != EINVAL) {
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000774 perror_msg("%s", "detach: waiting");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100775 break;
776 }
777#endif /* __WALL */
778 /* No __WALL here. */
779 if (waitpid(tcp->pid, &status, 0) < 0) {
780 if (errno != ECHILD) {
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000781 perror_msg("%s", "detach: waiting");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100782 break;
783 }
784#ifdef __WCLONE
785 /* If no processes, try clones. */
786 if (waitpid(tcp->pid, &status, __WCLONE) < 0) {
787 if (errno != ECHILD)
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000788 perror_msg("%s", "detach: waiting");
Denys Vlasenko800ec8f2012-03-16 15:11:34 +0100789 break;
790 }
791#endif /* __WCLONE */
792 }
793#ifdef __WALL
794 }
795#endif
796 if (!WIFSTOPPED(status)) {
797 /* Au revoir, mon ami. */
798 break;
799 }
800 if (WSTOPSIG(status) == SIGSTOP) {
801 ptrace_restart(PTRACE_DETACH, tcp, 0);
802 break;
803 }
804 error = ptrace_restart(PTRACE_CONT, tcp,
805 WSTOPSIG(status) == syscall_trap_sig ? 0
806 : WSTOPSIG(status));
807 if (error < 0)
808 break;
809 }
810 }
811
812 if (!qflag && (tcp->flags & TCB_ATTACHED))
813 fprintf(stderr, "Process %u detached\n", tcp->pid);
814
815 droptcb(tcp);
816
817 return error;
818}
819
820static void
Denys Vlasenko558e5122012-03-12 23:32:16 +0100821process_opt_p_list(char *opt)
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100822{
823 while (*opt) {
824 /*
825 * We accept -p PID,PID; -p "`pidof PROG`"; -p "`pgrep PROG`".
826 * pidof uses space as delim, pgrep uses newline. :(
827 */
828 int pid;
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100829 char *delim = opt + strcspn(opt, ", \n\t");
830 char c = *delim;
831
832 *delim = '\0';
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000833 pid = string_to_uint(opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100834 if (pid <= 0) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000835 error_msg_and_die("Invalid process id: '%s'", opt);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100836 }
837 if (pid == strace_tracer_pid) {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000838 error_msg_and_die("I'm sorry, I can't let you do that, Dave.");
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100839 }
840 *delim = c;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100841 alloctcb(pid);
Denys Vlasenkoe8172b72012-03-09 13:01:04 +0100842 if (c == '\0')
843 break;
844 opt = delim + 1;
845 }
846}
847
Roland McGrath02203312007-06-11 22:06:31 +0000848static void
849startup_attach(void)
850{
851 int tcbi;
852 struct tcb *tcp;
853
854 /*
855 * Block user interruptions as we would leave the traced
856 * process stopped (process state T) if we would terminate in
Denys Vlasenko2e968c02011-09-01 10:23:09 +0200857 * between PTRACE_ATTACH and wait4() on SIGSTOP.
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200858 * We rely on cleanup() from this point on.
Roland McGrath02203312007-06-11 22:06:31 +0000859 */
860 if (interactive)
861 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
862
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000863 if (daemonized_tracer) {
864 pid_t pid = fork();
865 if (pid < 0) {
Denys Vlasenko014ca3a2011-09-02 16:19:30 +0200866 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000867 }
868 if (pid) { /* parent */
869 /*
Denys Vlasenko75422762011-05-27 14:36:01 +0200870 * Wait for grandchild to attach to straced process
871 * (grandparent). Grandchild SIGKILLs us after it attached.
872 * Grandparent's wait() is unblocked by our death,
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000873 * it proceeds to exec the straced program.
874 */
875 pause();
876 _exit(0); /* paranoia */
877 }
Denys Vlasenko75422762011-05-27 14:36:01 +0200878 /* grandchild */
879 /* We will be the tracer process. Remember our new pid: */
880 strace_tracer_pid = getpid();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000881 }
882
Roland McGrath02203312007-06-11 22:06:31 +0000883 for (tcbi = 0; tcbi < tcbtabsize; tcbi++) {
884 tcp = tcbtab[tcbi];
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200885
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100886 if (!(tcp->flags & TCB_INUSE))
887 continue;
888
Denys Vlasenkod116a732011-09-05 14:01:33 +0200889 /* Is this a process we should attach to, but not yet attached? */
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100890 if (tcp->flags & TCB_ATTACHED)
891 continue; /* no, we already attached it */
Denys Vlasenkod116a732011-09-05 14:01:33 +0200892
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000893 if (followfork && !daemonized_tracer) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000894 char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3];
Roland McGrath02203312007-06-11 22:06:31 +0000895 DIR *dir;
896
897 sprintf(procdir, "/proc/%d/task", tcp->pid);
898 dir = opendir(procdir);
899 if (dir != NULL) {
900 unsigned int ntid = 0, nerr = 0;
901 struct dirent *de;
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200902
Roland McGrath02203312007-06-11 22:06:31 +0000903 while ((de = readdir(dir)) != NULL) {
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200904 struct tcb *cur_tcp;
905 int tid;
906
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000907 if (de->d_fileno == 0)
Roland McGrath02203312007-06-11 22:06:31 +0000908 continue;
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000909 /* we trust /proc filesystem */
Roland McGrath02203312007-06-11 22:06:31 +0000910 tid = atoi(de->d_name);
911 if (tid <= 0)
912 continue;
913 ++ntid;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100914 if (ptrace_attach_or_seize(tid) < 0) {
Roland McGrath02203312007-06-11 22:06:31 +0000915 ++nerr;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100916 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200917 fprintf(stderr, "attach to pid %d failed\n", tid);
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200918 continue;
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200919 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100920 if (debug_flag)
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200921 fprintf(stderr, "attach to pid %d succeeded\n", tid);
922 cur_tcp = tcp;
923 if (tid != tcp->pid)
924 cur_tcp = alloctcb(tid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100925 cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100926 newoutf(cur_tcp);
Roland McGrath02203312007-06-11 22:06:31 +0000927 }
928 closedir(dir);
Denys Vlasenko381dbc22011-09-05 13:59:39 +0200929 if (interactive) {
930 sigprocmask(SIG_SETMASK, &empty_set, NULL);
931 if (interrupted)
932 goto ret;
933 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
934 }
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000935 ntid -= nerr;
936 if (ntid == 0) {
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000937 perror_msg("%s", "attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +0000938 droptcb(tcp);
939 continue;
940 }
941 if (!qflag) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000942 fprintf(stderr, ntid > 1
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100943? "Process %u attached with %u threads\n"
944: "Process %u attached\n",
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200945 tcp->pid, ntid);
Roland McGrath02203312007-06-11 22:06:31 +0000946 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100947 if (!(tcp->flags & TCB_ATTACHED)) {
Denys Vlasenkof88837a2011-09-05 14:05:46 +0200948 /* -p PID, we failed to attach to PID itself
949 * but did attach to some of its sibling threads.
950 * Drop PID's tcp.
951 */
952 droptcb(tcp);
953 }
Roland McGrath02203312007-06-11 22:06:31 +0000954 continue;
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000955 } /* if (opendir worked) */
956 } /* if (-f) */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +0100957 if (ptrace_attach_or_seize(tcp->pid) < 0) {
Dmitry V. Levin9a71bcd2012-09-17 23:20:54 +0000958 perror_msg("%s", "attach: ptrace(PTRACE_ATTACH, ...)");
Roland McGrath02203312007-06-11 22:06:31 +0000959 droptcb(tcp);
960 continue;
961 }
Denys Vlasenko75fe85c2012-03-09 15:15:24 +0100962 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +0100963 newoutf(tcp);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +0100964 if (debug_flag)
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200965 fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000966
967 if (daemonized_tracer) {
968 /*
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000969 * Make parent go away.
970 * Also makes grandparent's wait() unblock.
971 */
972 kill(getppid(), SIGKILL);
973 }
974
Roland McGrath02203312007-06-11 22:06:31 +0000975 if (!qflag)
976 fprintf(stderr,
Denys Vlasenko9c3861d2012-03-16 15:21:49 +0100977 "Process %u attached\n",
Roland McGrath02203312007-06-11 22:06:31 +0000978 tcp->pid);
Denys Vlasenkof95397a2011-06-24 16:51:16 +0200979 } /* for each tcbtab[] */
Roland McGrath02203312007-06-11 22:06:31 +0000980
Denys Vlasenko44f87ef2011-08-17 15:18:21 +0200981 ret:
Roland McGrath02203312007-06-11 22:06:31 +0000982 if (interactive)
983 sigprocmask(SIG_SETMASK, &empty_set, NULL);
984}
985
Denys Vlasenkof909c8d2013-02-19 16:30:31 +0100986/* Stack-o-phobic exec helper, in the hope to work around
987 * NOMMU + "daemonized tracer" difficulty.
988 */
989struct exec_params {
990 int fd_to_close;
991 uid_t run_euid;
992 gid_t run_egid;
993 char **argv;
994 char *pathname;
995};
996static struct exec_params params_for_tracee;
997static void __attribute__ ((noinline, noreturn))
998exec_or_die(void)
999{
1000 struct exec_params *params = &params_for_tracee;
1001
1002 if (params->fd_to_close >= 0)
1003 close(params->fd_to_close);
1004 if (!daemonized_tracer && !use_seize) {
1005 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) {
1006 perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)");
1007 }
1008 }
1009
1010 if (username != NULL) {
1011 /*
1012 * It is important to set groups before we
1013 * lose privileges on setuid.
1014 */
1015 if (initgroups(username, run_gid) < 0) {
1016 perror_msg_and_die("initgroups");
1017 }
1018 if (setregid(run_gid, params->run_egid) < 0) {
1019 perror_msg_and_die("setregid");
1020 }
1021 if (setreuid(run_uid, params->run_euid) < 0) {
1022 perror_msg_and_die("setreuid");
1023 }
1024 }
1025 else if (geteuid() != 0)
1026 if (setreuid(run_uid, run_uid) < 0) {
1027 perror_msg_and_die("setreuid");
1028 }
1029
1030 if (!daemonized_tracer) {
1031 /*
1032 * Induce a ptrace stop. Tracer (our parent)
1033 * will resume us with PTRACE_SYSCALL and display
1034 * the immediately following execve syscall.
1035 * Can't do this on NOMMU systems, we are after
1036 * vfork: parent is blocked, stopping would deadlock.
1037 */
1038 if (!NOMMU_SYSTEM)
1039 kill(getpid(), SIGSTOP);
1040 } else {
1041 alarm(3);
1042 /* we depend on SIGCHLD set to SIG_DFL by init code */
1043 /* if it happens to be SIG_IGN'ed, wait won't block */
1044 wait(NULL);
1045 alarm(0);
1046 }
1047
1048 execv(params->pathname, params->argv);
1049 perror_msg_and_die("exec");
1050}
1051
Roland McGrath02203312007-06-11 22:06:31 +00001052static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001053startup_child(char **argv)
Roland McGrath02203312007-06-11 22:06:31 +00001054{
1055 struct stat statbuf;
1056 const char *filename;
1057 char pathname[MAXPATHLEN];
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001058 int pid;
Roland McGrath02203312007-06-11 22:06:31 +00001059 struct tcb *tcp;
1060
1061 filename = argv[0];
1062 if (strchr(filename, '/')) {
1063 if (strlen(filename) > sizeof pathname - 1) {
1064 errno = ENAMETOOLONG;
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001065 perror_msg_and_die("exec");
Roland McGrath02203312007-06-11 22:06:31 +00001066 }
1067 strcpy(pathname, filename);
1068 }
1069#ifdef USE_DEBUGGING_EXEC
1070 /*
1071 * Debuggers customarily check the current directory
1072 * first regardless of the path but doing that gives
1073 * security geeks a panic attack.
1074 */
1075 else if (stat(filename, &statbuf) == 0)
1076 strcpy(pathname, filename);
1077#endif /* USE_DEBUGGING_EXEC */
1078 else {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +00001079 const char *path;
Roland McGrath02203312007-06-11 22:06:31 +00001080 int m, n, len;
1081
1082 for (path = getenv("PATH"); path && *path; path += m) {
Denys Vlasenko4f3df072012-01-29 22:38:35 +01001083 const char *colon = strchr(path, ':');
1084 if (colon) {
1085 n = colon - path;
Roland McGrath02203312007-06-11 22:06:31 +00001086 m = n + 1;
1087 }
1088 else
1089 m = n = strlen(path);
1090 if (n == 0) {
1091 if (!getcwd(pathname, MAXPATHLEN))
1092 continue;
1093 len = strlen(pathname);
1094 }
1095 else if (n > sizeof pathname - 1)
1096 continue;
1097 else {
1098 strncpy(pathname, path, n);
1099 len = n;
1100 }
1101 if (len && pathname[len - 1] != '/')
1102 pathname[len++] = '/';
1103 strcpy(pathname + len, filename);
1104 if (stat(pathname, &statbuf) == 0 &&
1105 /* Accept only regular files
1106 with some execute bits set.
1107 XXX not perfect, might still fail */
1108 S_ISREG(statbuf.st_mode) &&
1109 (statbuf.st_mode & 0111))
1110 break;
1111 }
1112 }
1113 if (stat(pathname, &statbuf) < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001114 perror_msg_and_die("Can't stat '%s'", filename);
Roland McGrath02203312007-06-11 22:06:31 +00001115 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001116
1117 params_for_tracee.fd_to_close = (shared_log != stderr) ? fileno(shared_log) : -1;
1118 params_for_tracee.run_euid = (statbuf.st_mode & S_ISUID) ? statbuf.st_uid : run_uid;
1119 params_for_tracee.run_egid = (statbuf.st_mode & S_ISGID) ? statbuf.st_gid : run_gid;
1120 params_for_tracee.argv = argv;
1121 /*
1122 * On NOMMU, can be safely freed only after execve in tracee.
1123 * It's hard to know when that happens, so we just leak it.
1124 */
Denys Vlasenko05f32512013-02-26 12:00:34 +01001125 params_for_tracee.pathname = NOMMU_SYSTEM ? strdup(pathname) : pathname;
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001126
Dmitry V. Levina6809652008-11-10 17:14:58 +00001127 strace_child = pid = fork();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001128 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001129 perror_msg_and_die("fork");
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001130 }
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001131 if ((pid != 0 && daemonized_tracer)
1132 || (pid == 0 && !daemonized_tracer)
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001133 ) {
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001134 /* We are to become the tracee. Two cases:
1135 * -D: we are parent
1136 * not -D: we are child
1137 */
1138 exec_or_die();
Roland McGrath02203312007-06-11 22:06:31 +00001139 }
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001140
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001141 /* We are the tracer */
Denys Vlasenko75422762011-05-27 14:36:01 +02001142
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001143 if (!daemonized_tracer) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001144 if (!use_seize) {
1145 /* child did PTRACE_TRACEME, nothing to do in parent */
1146 } else {
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001147 if (!NOMMU_SYSTEM) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001148 /* Wait until child stopped itself */
1149 int status;
1150 while (waitpid(pid, &status, WSTOPPED) < 0) {
1151 if (errno == EINTR)
1152 continue;
1153 perror_msg_and_die("waitpid");
1154 }
1155 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001156 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001157 perror_msg_and_die("Unexpected wait status %x", status);
1158 }
Denys Vlasenko05f32512013-02-26 12:00:34 +01001159 skip_startup_execve = 1;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001160 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001161 /* Else: NOMMU case, we have no way to sync.
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001162 * Just attach to it as soon as possible.
1163 * This means that we may miss a few first syscalls...
1164 */
1165
1166 if (ptrace_attach_or_seize(pid)) {
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001167 kill_save_errno(pid, SIGKILL);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001168 perror_msg_and_die("Can't attach to %d", pid);
1169 }
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001170 if (!NOMMU_SYSTEM)
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001171 kill(pid, SIGCONT);
1172 }
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001173 tcp = alloctcb(pid);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001174 if (!NOMMU_SYSTEM)
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001175 tcp->flags |= TCB_ATTACHED | TCB_STRACE_CHILD | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenkof88837a2011-09-05 14:05:46 +02001176 else
Denys Vlasenko75fe85c2012-03-09 15:15:24 +01001177 tcp->flags |= TCB_ATTACHED | TCB_STRACE_CHILD | TCB_STARTUP;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001178 newoutf(tcp);
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001179 }
1180 else {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001181 /* With -D, we are *child* here, IOW: different pid. Fetch it: */
Denys Vlasenko2e968c02011-09-01 10:23:09 +02001182 strace_tracer_pid = getpid();
1183 /* The tracee is our parent: */
1184 pid = getppid();
Denys Vlasenkof2025022012-03-09 15:29:45 +01001185 alloctcb(pid);
1186 /* attaching will be done later, by startup_attach */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001187 /* note: we don't do newoutf(tcp) here either! */
Denys Vlasenko05f32512013-02-26 12:00:34 +01001188 skip_startup_execve = 1;
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001189
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001190 /* NOMMU BUG! -D mode is active, we (child) return,
1191 * and we will scribble over parent's stack!
1192 * When parent later unpauses, it segfaults.
1193 *
1194 * We work around it
1195 * (1) by declaring exec_or_die() NORETURN,
1196 * hopefully compiler will just jump to it
1197 * instead of call (won't push anything to stack),
1198 * (2) by trying very hard in exec_or_die()
1199 * to not use any stack,
1200 * (3) having a really big (MAXPATHLEN) stack object
1201 * in this function, which creates a "buffer" between
1202 * child's and parent's stack pointers.
1203 * This may save us if (1) and (2) failed
1204 * and compiler decided to use stack in exec_or_die() anyway
1205 * (happens on i386 because of stack parameter passing).
Denys Vlasenko05f32512013-02-26 12:00:34 +01001206 *
1207 * A cleaner solution is to use makecontext + setcontext
1208 * to create a genuine separate stack and execute on it.
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001209 */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001210 }
Roland McGrath02203312007-06-11 22:06:31 +00001211}
1212
Wang Chaob13c0de2010-11-12 17:25:19 +08001213/*
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001214 * Test whether the kernel support PTRACE_O_TRACECLONE et al options.
Wang Chaob13c0de2010-11-12 17:25:19 +08001215 * First fork a new child, call ptrace with PTRACE_SETOPTIONS on it,
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001216 * and then see which options are supported by the kernel.
Wang Chaob13c0de2010-11-12 17:25:19 +08001217 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001218static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001219test_ptrace_setoptions_followfork(void)
Wang Chaob13c0de2010-11-12 17:25:19 +08001220{
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001221 int pid, expected_grandchild = 0, found_grandchild = 0;
1222 const unsigned int test_options = PTRACE_O_TRACECLONE |
1223 PTRACE_O_TRACEFORK |
1224 PTRACE_O_TRACEVFORK;
Wang Chaob13c0de2010-11-12 17:25:19 +08001225
Denys Vlasenko05f32512013-02-26 12:00:34 +01001226 /* Need fork for test. NOMMU has no forks */
1227 if (NOMMU_SYSTEM)
1228 goto worked; /* be bold, and pretend that test succeeded */
1229
Denys Vlasenko5d645812011-08-20 12:48:18 +02001230 pid = fork();
1231 if (pid < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001232 perror_msg_and_die("fork");
Denys Vlasenko5d645812011-08-20 12:48:18 +02001233 if (pid == 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001234 pid = getpid();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001235 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001236 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1237 __func__);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001238 kill_save_errno(pid, SIGSTOP);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001239 if (fork() < 0)
1240 perror_msg_and_die("fork");
1241 _exit(0);
Wang Chaob13c0de2010-11-12 17:25:19 +08001242 }
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001243
1244 while (1) {
1245 int status, tracee_pid;
1246
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001247 errno = 0;
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001248 tracee_pid = wait(&status);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001249 if (tracee_pid <= 0) {
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001250 if (errno == EINTR)
1251 continue;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001252 if (errno == ECHILD)
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001253 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001254 kill_save_errno(pid, SIGKILL);
1255 perror_msg_and_die("%s: unexpected wait result %d",
1256 __func__, tracee_pid);
1257 }
1258 if (WIFEXITED(status)) {
1259 if (WEXITSTATUS(status)) {
1260 if (tracee_pid != pid)
1261 kill_save_errno(pid, SIGKILL);
1262 error_msg_and_die("%s: unexpected exit status %u",
1263 __func__, WEXITSTATUS(status));
1264 }
1265 continue;
1266 }
1267 if (WIFSIGNALED(status)) {
1268 if (tracee_pid != pid)
1269 kill_save_errno(pid, SIGKILL);
1270 error_msg_and_die("%s: unexpected signal %u",
1271 __func__, WTERMSIG(status));
1272 }
1273 if (!WIFSTOPPED(status)) {
1274 if (tracee_pid != pid)
1275 kill_save_errno(tracee_pid, SIGKILL);
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001276 kill_save_errno(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001277 error_msg_and_die("%s: unexpected wait status %x",
1278 __func__, status);
Dmitry V. Levinb1467442010-12-02 20:56:43 +00001279 }
1280 if (tracee_pid != pid) {
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00001281 found_grandchild = tracee_pid;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001282 if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0) {
1283 kill_save_errno(tracee_pid, SIGKILL);
1284 kill_save_errno(pid, SIGKILL);
1285 perror_msg_and_die("PTRACE_CONT doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001286 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001287 continue;
1288 }
1289 switch (WSTOPSIG(status)) {
1290 case SIGSTOP:
1291 if (ptrace(PTRACE_SETOPTIONS, pid, 0, test_options) < 0
1292 && errno != EINVAL && errno != EIO)
1293 perror_msg("PTRACE_SETOPTIONS");
1294 break;
1295 case SIGTRAP:
1296 if (status >> 16 == PTRACE_EVENT_FORK) {
1297 long msg = 0;
1298
1299 if (ptrace(PTRACE_GETEVENTMSG, pid,
1300 NULL, (long) &msg) == 0)
1301 expected_grandchild = msg;
1302 }
1303 break;
1304 }
1305 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) {
1306 kill_save_errno(pid, SIGKILL);
1307 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Wang Chaob13c0de2010-11-12 17:25:19 +08001308 }
1309 }
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001310 if (expected_grandchild && expected_grandchild == found_grandchild) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001311 worked:
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001312 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001313 if (debug_flag)
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001314 fprintf(stderr, "ptrace_setoptions = %#x\n",
1315 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001316 return 0;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001317 }
1318 error_msg("Test for PTRACE_O_TRACECLONE failed, "
1319 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001320 return 1;
Wang Chaob13c0de2010-11-12 17:25:19 +08001321}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001322
1323/*
1324 * Test whether the kernel support PTRACE_O_TRACESYSGOOD.
1325 * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it,
1326 * and then see whether it will stop with (SIGTRAP | 0x80).
1327 *
1328 * Use of this option enables correct handling of user-generated SIGTRAPs,
1329 * and SIGTRAPs generated by special instructions such as int3 on x86:
1330 * _start: .globl _start
1331 * int3
1332 * movl $42, %ebx
1333 * movl $1, %eax
1334 * int $0x80
1335 * (compile with: "gcc -nostartfiles -nostdlib -o int3 int3.S")
1336 */
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001337static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001338test_ptrace_setoptions_for_all(void)
1339{
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001340 const unsigned int test_options = PTRACE_O_TRACESYSGOOD |
1341 PTRACE_O_TRACEEXEC;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001342 int pid;
1343 int it_worked = 0;
1344
Denys Vlasenko05f32512013-02-26 12:00:34 +01001345 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001346 if (NOMMU_SYSTEM)
Denys Vlasenko05f32512013-02-26 12:00:34 +01001347 goto worked; /* be bold, and pretend that test succeeded */
Mike Frysinger7ff5ed92012-04-05 01:52:25 -04001348
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001349 pid = fork();
1350 if (pid < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001351 perror_msg_and_die("fork");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001352
1353 if (pid == 0) {
1354 pid = getpid();
1355 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +02001356 /* Note: exits with exitcode 1 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001357 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
1358 __func__);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001359 kill(pid, SIGSTOP);
1360 _exit(0); /* parent should see entry into this syscall */
1361 }
1362
1363 while (1) {
1364 int status, tracee_pid;
1365
1366 errno = 0;
1367 tracee_pid = wait(&status);
1368 if (tracee_pid <= 0) {
1369 if (errno == EINTR)
1370 continue;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001371 kill_save_errno(pid, SIGKILL);
1372 perror_msg_and_die("%s: unexpected wait result %d",
1373 __func__, tracee_pid);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001374 }
Denys Vlasenko75422762011-05-27 14:36:01 +02001375 if (WIFEXITED(status)) {
1376 if (WEXITSTATUS(status) == 0)
1377 break;
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001378 error_msg_and_die("%s: unexpected exit status %u",
1379 __func__, WEXITSTATUS(status));
1380 }
1381 if (WIFSIGNALED(status)) {
1382 error_msg_and_die("%s: unexpected signal %u",
1383 __func__, WTERMSIG(status));
Denys Vlasenko75422762011-05-27 14:36:01 +02001384 }
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001385 if (!WIFSTOPPED(status)) {
1386 kill(pid, SIGKILL);
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001387 error_msg_and_die("%s: unexpected wait status %x",
1388 __func__, status);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001389 }
1390 if (WSTOPSIG(status) == SIGSTOP) {
1391 /*
1392 * We don't check "options aren't accepted" error.
1393 * If it happens, we'll never get (SIGTRAP | 0x80),
1394 * and thus will decide to not use the option.
1395 * IOW: the outcome of the test will be correct.
1396 */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001397 if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0
1398 && errno != EINVAL && errno != EIO)
1399 perror_msg("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001400 }
1401 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
1402 it_worked = 1;
1403 }
1404 if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) {
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001405 kill_save_errno(pid, SIGKILL);
Denys Vlasenko75422762011-05-27 14:36:01 +02001406 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001407 }
1408 }
1409
1410 if (it_worked) {
Denys Vlasenko05f32512013-02-26 12:00:34 +01001411 worked:
Denys Vlasenko75422762011-05-27 14:36:01 +02001412 syscall_trap_sig = (SIGTRAP | 0x80);
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001413 ptrace_setoptions |= test_options;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001414 if (debug_flag)
Denys Vlasenkof44cce42011-06-21 14:34:10 +02001415 fprintf(stderr, "ptrace_setoptions = %#x\n",
1416 ptrace_setoptions);
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001417 return 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001418 }
1419
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001420 error_msg("Test for PTRACE_O_TRACESYSGOOD failed, "
1421 "giving up using this feature.");
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001422 return 1;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001423}
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001424
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001425#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001426static void
1427test_ptrace_seize(void)
1428{
1429 int pid;
1430
Denys Vlasenko05f32512013-02-26 12:00:34 +01001431 /* Need fork for test. NOMMU has no forks */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001432 if (NOMMU_SYSTEM) {
1433 post_attach_sigstop = 0; /* this sets use_seize to 1 */
1434 return;
1435 }
1436
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001437 pid = fork();
1438 if (pid < 0)
1439 perror_msg_and_die("fork");
1440
1441 if (pid == 0) {
1442 pause();
1443 _exit(0);
1444 }
1445
1446 /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After
1447 * attaching tracee continues to run unless a trap condition occurs.
1448 * PTRACE_SEIZE doesn't affect signal or group stop state.
1449 */
Denys Vlasenko26bc0602012-07-10 16:36:32 +02001450 if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001451 post_attach_sigstop = 0; /* this sets use_seize to 1 */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001452 } else if (debug_flag) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001453 fprintf(stderr, "PTRACE_SEIZE doesn't work\n");
1454 }
1455
1456 kill(pid, SIGKILL);
1457
1458 while (1) {
1459 int status, tracee_pid;
1460
1461 errno = 0;
1462 tracee_pid = waitpid(pid, &status, 0);
1463 if (tracee_pid <= 0) {
1464 if (errno == EINTR)
1465 continue;
1466 perror_msg_and_die("%s: unexpected wait result %d",
1467 __func__, tracee_pid);
1468 }
1469 if (WIFSIGNALED(status)) {
1470 return;
1471 }
1472 error_msg_and_die("%s: unexpected wait status %x",
1473 __func__, status);
1474 }
1475}
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001476#else /* !USE_SEIZE */
1477# define test_ptrace_seize() ((void)0)
1478#endif
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001479
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001480static unsigned
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001481get_os_release(void)
1482{
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001483 unsigned rel;
1484 const char *p;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001485 struct utsname u;
1486 if (uname(&u) < 0)
1487 perror_msg_and_die("uname");
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001488 /* u.release has this form: "3.2.9[-some-garbage]" */
1489 rel = 0;
1490 p = u.release;
1491 for (;;) {
1492 if (!(*p >= '0' && *p <= '9'))
1493 error_msg_and_die("Bad OS release string: '%s'", u.release);
1494 /* Note: this open-codes KERNEL_VERSION(): */
1495 rel = (rel << 8) | atoi(p);
1496 if (rel >= KERNEL_VERSION(1,0,0))
1497 break;
1498 while (*p >= '0' && *p <= '9')
1499 p++;
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001500 if (*p != '.') {
1501 if (rel >= KERNEL_VERSION(0,1,0)) {
1502 /* "X.Y-something" means "X.Y.0" */
1503 rel <<= 8;
1504 break;
1505 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001506 error_msg_and_die("Bad OS release string: '%s'", u.release);
Dmitry V. Levin0dbc80d2012-05-14 23:42:10 +00001507 }
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001508 p++;
1509 }
1510 return rel;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001511}
1512
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01001513/*
1514 * Initialization part of main() was eating much stack (~0.5k),
1515 * which was unused after init.
1516 * We can reuse it if we move init code into a separate function.
1517 *
1518 * Don't want main() to inline us and defeat the reason
1519 * we have a separate function.
1520 */
1521static void __attribute__ ((noinline))
1522init(int argc, char *argv[])
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001523{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001524 struct tcb *tcp;
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001525 int c, i;
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001526 int optF = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001527 struct sigaction sa;
1528
Dmitry V. Levin08b623e2007-10-08 21:04:41 +00001529 progname = argv[0] ? argv[0] : "strace";
1530
Denys Vlasenkoa5090542012-03-15 17:27:49 +01001531 /* Make sure SIGCHLD has the default action so that waitpid
1532 definitely works without losing track of children. The user
1533 should not have given us a bogus state to inherit, but he might
1534 have. Arguably we should detect SIG_IGN here and pass it on
1535 to children, but probably noone really needs that. */
1536 signal(SIGCHLD, SIG_DFL);
1537
Denys Vlasenko75422762011-05-27 14:36:01 +02001538 strace_tracer_pid = getpid();
1539
Denys Vlasenko6e0bfd12012-03-15 14:36:28 +01001540 os_release = get_os_release();
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001541
Roland McGrathee9d4352002-12-18 04:16:10 +00001542 /* Allocate the initial tcbtab. */
1543 tcbtabsize = argc; /* Surely enough for all -p args. */
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001544 tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0]));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001545 if (!tcbtab)
1546 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001547 tcp = calloc(tcbtabsize, sizeof(*tcp));
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001548 if (!tcp)
1549 die_out_of_memory();
Denys Vlasenko4f12af22011-06-23 13:16:23 +02001550 for (c = 0; c < tcbtabsize; c++)
1551 tcbtab[c] = tcp++;
Roland McGrathee9d4352002-12-18 04:16:10 +00001552
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001553 shared_log = stderr;
Roland McGrath138c6a32006-01-12 09:50:49 +00001554 set_sortby(DEFAULT_SORTBY);
1555 set_personality(DEFAULT_PERSONALITY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001556 qualify("trace=all");
1557 qualify("abbrev=all");
1558 qualify("verbose=all");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001559#if DEFAULT_QUAL_FLAGS != (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
1560# error Bug in DEFAULT_QUAL_FLAGS
1561#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001562 qualify("signal=all");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001563 while ((c = getopt(argc, argv,
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001564 "+bcCdfFhiqrtTvVxyz"
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001565 "D"
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001566 "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001567 switch (c) {
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001568 case 'b':
1569 detach_on_execve = 1;
1570 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001571 case 'c':
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001572 if (cflag == CFLAG_BOTH) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001573 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001574 }
1575 cflag = CFLAG_ONLY_STATS;
1576 break;
1577 case 'C':
1578 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001579 error_msg_and_die("-c and -C are mutually exclusive");
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001580 }
1581 cflag = CFLAG_BOTH;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001582 break;
1583 case 'd':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001584 debug_flag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001585 break;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001586 case 'D':
1587 daemonized_tracer = 1;
1588 break;
Roland McGrath41c48222008-07-18 00:25:10 +00001589 case 'F':
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001590 optF = 1;
1591 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001592 case 'f':
1593 followfork++;
1594 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001595 case 'h':
1596 usage(stdout, 0);
1597 break;
1598 case 'i':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001599 iflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001600 break;
1601 case 'q':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001602 qflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001603 break;
1604 case 'r':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001605 rflag = 1;
1606 /* fall through to tflag++ */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001607 case 't':
1608 tflag++;
1609 break;
1610 case 'T':
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001611 Tflag = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001612 break;
1613 case 'x':
1614 xflag++;
1615 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001616 case 'y':
1617 show_fd_path = 1;
1618 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001619 case 'v':
1620 qualify("abbrev=none");
1621 break;
1622 case 'V':
Roland McGrath9c9a2532003-02-20 02:56:29 +00001623 printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001624 exit(0);
1625 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001626 case 'z':
1627 not_failing_only = 1;
1628 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001629 case 'a':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001630 acolumn = string_to_uint(optarg);
Denys Vlasenko102ec492011-08-25 01:27:59 +02001631 if (acolumn < 0)
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001632 error_opt_arg(c, optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001633 break;
1634 case 'e':
1635 qualify(optarg);
1636 break;
1637 case 'o':
1638 outfname = strdup(optarg);
1639 break;
1640 case 'O':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001641 i = string_to_uint(optarg);
1642 if (i < 0)
1643 error_opt_arg(c, optarg);
1644 set_overhead(i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001645 break;
1646 case 'p':
Denys Vlasenkoe8172b72012-03-09 13:01:04 +01001647 process_opt_p_list(optarg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001648 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001649 case 'P':
1650 tracing_paths = 1;
1651 if (pathtrace_select(optarg)) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001652 error_msg_and_die("Failed to select path '%s'", optarg);
Grant Edwards8a082772011-04-07 20:25:40 +00001653 }
1654 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001655 case 's':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001656 i = string_to_uint(optarg);
1657 if (i < 0)
1658 error_opt_arg(c, optarg);
1659 max_strlen = i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001660 break;
1661 case 'S':
1662 set_sortby(optarg);
1663 break;
1664 case 'u':
1665 username = strdup(optarg);
1666 break;
Roland McGrathde6e5332003-01-24 04:31:23 +00001667 case 'E':
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001668 if (putenv(optarg) < 0)
1669 die_out_of_memory();
Roland McGrathde6e5332003-01-24 04:31:23 +00001670 break;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001671 case 'I':
Dmitry V. Levinccee1692012-03-25 21:49:48 +00001672 opt_intr = string_to_uint(optarg);
1673 if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS)
1674 error_opt_arg(c, optarg);
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001675 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001676 default:
1677 usage(stderr, 1);
1678 break;
1679 }
1680 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001681 argv += optind;
1682 /* argc -= optind; - no need, argc is not used below */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001683
Denys Vlasenko102ec492011-08-25 01:27:59 +02001684 acolumn_spaces = malloc(acolumn + 1);
1685 if (!acolumn_spaces)
Denys Vlasenko1d46ba52011-08-31 14:00:02 +02001686 die_out_of_memory();
Denys Vlasenko102ec492011-08-25 01:27:59 +02001687 memset(acolumn_spaces, ' ', acolumn);
1688 acolumn_spaces[acolumn] = '\0';
1689
Denys Vlasenko837399a2012-01-24 11:37:03 +01001690 /* Must have PROG [ARGS], or -p PID. Not both. */
Denys Vlasenkofd883382012-03-09 13:03:41 +01001691 if (!argv[0] == !nprocs)
Roland McGrathce0d1542003-11-11 21:24:23 +00001692 usage(stderr, 1);
1693
Denys Vlasenkofd883382012-03-09 13:03:41 +01001694 if (nprocs != 0 && daemonized_tracer) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001695 error_msg_and_die("-D and -p are mutually exclusive");
Wang Chaod322a4b2010-08-05 14:30:11 +08001696 }
1697
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001698 if (!followfork)
1699 followfork = optF;
1700
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001701 if (followfork >= 2 && cflag) {
Denys Vlasenkoc5ccfa42012-03-26 13:10:50 +02001702 error_msg_and_die("(-c or -C) and -ff are mutually exclusive");
Roland McGrathcb9def62006-04-25 07:48:03 +00001703 }
1704
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001705 /* See if they want to run as another user. */
1706 if (username != NULL) {
1707 struct passwd *pent;
1708
1709 if (getuid() != 0 || geteuid() != 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001710 error_msg_and_die("You must be root to use the -u option");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001711 }
Denys Vlasenko5d645812011-08-20 12:48:18 +02001712 pent = getpwnam(username);
1713 if (pent == NULL) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001714 error_msg_and_die("Cannot find user '%s'", username);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001715 }
1716 run_uid = pent->pw_uid;
1717 run_gid = pent->pw_gid;
1718 }
1719 else {
1720 run_uid = getuid();
1721 run_gid = getgid();
1722 }
1723
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001724 /*
1725 * On any reasonably recent Linux kernel (circa about 2.5.46)
1726 * need_fork_exec_workarounds should stay 0 after these tests:
1727 */
1728 /*need_fork_exec_workarounds = 0; - already is */
Dmitry V. Levin04f8b482011-08-16 18:57:29 +00001729 if (followfork)
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001730 need_fork_exec_workarounds = test_ptrace_setoptions_followfork();
1731 need_fork_exec_workarounds |= test_ptrace_setoptions_for_all();
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001732 test_ptrace_seize();
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001733
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001734 /* Check if they want to redirect the output. */
1735 if (outfname) {
Roland McGrath37b9a662003-11-07 02:26:54 +00001736 /* See if they want to pipe the output. */
1737 if (outfname[0] == '|' || outfname[0] == '!') {
1738 /*
1739 * We can't do the <outfname>.PID funny business
1740 * when using popen, so prohibit it.
1741 */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001742 if (followfork >= 2)
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001743 error_msg_and_die("Piping the output and -ff are mutually exclusive");
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001744 shared_log = strace_popen(outfname + 1);
Roland McGrath37b9a662003-11-07 02:26:54 +00001745 }
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001746 else if (followfork < 2)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001747 shared_log = strace_fopen(outfname);
Denys Vlasenko328bf252012-03-12 23:34:13 +01001748 } else {
1749 /* -ff without -o FILE is the same as single -f */
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001750 if (followfork >= 2)
Denys Vlasenko328bf252012-03-12 23:34:13 +01001751 followfork = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001752 }
1753
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001754 if (!outfname || outfname[0] == '|' || outfname[0] == '!') {
Denys Vlasenkoa677da52012-01-24 11:31:51 +01001755 char *buf = malloc(BUFSIZ);
1756 if (!buf)
1757 die_out_of_memory();
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001758 setvbuf(shared_log, buf, _IOLBF, BUFSIZ);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001759 }
Denys Vlasenko837399a2012-01-24 11:37:03 +01001760 if (outfname && argv[0]) {
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001761 if (!opt_intr)
1762 opt_intr = INTR_NEVER;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001763 qflag = 1;
Roland McGrath36931052003-06-03 01:35:20 +00001764 }
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001765 if (!opt_intr)
1766 opt_intr = INTR_WHILE_WAIT;
Wang Chaob13c0de2010-11-12 17:25:19 +08001767
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001768 /* argv[0] -pPID -oFILE Default interactive setting
1769 * yes 0 0 INTR_WHILE_WAIT
1770 * no 1 0 INTR_WHILE_WAIT
1771 * yes 0 1 INTR_NEVER
1772 * no 1 1 INTR_WHILE_WAIT
Roland McGrath54cc1c82007-11-03 23:34:11 +00001773 */
1774
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01001775 sigemptyset(&empty_set);
1776 sigemptyset(&blocked_set);
1777
Denys Vlasenkof909c8d2013-02-19 16:30:31 +01001778 /* startup_child() must be called before the signal handlers get
1779 * installed below as they are inherited into the spawned process.
1780 * Also we do not need to be protected by them as during interruption
1781 * in the startup_child() mode we kill the spawned process anyway.
1782 */
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001783 if (argv[0]) {
Denys Vlasenko837399a2012-01-24 11:37:03 +01001784 startup_child(argv);
Denys Vlasenko61e7aad2012-03-15 13:44:17 +01001785 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001786
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001787 sa.sa_handler = SIG_IGN;
1788 sigemptyset(&sa.sa_mask);
1789 sa.sa_flags = 0;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001790 sigaction(SIGTTOU, &sa, NULL); /* SIG_IGN */
1791 sigaction(SIGTTIN, &sa, NULL); /* SIG_IGN */
1792 if (opt_intr != INTR_ANYWHERE) {
1793 if (opt_intr == INTR_BLOCK_TSTP_TOO)
1794 sigaction(SIGTSTP, &sa, NULL); /* SIG_IGN */
1795 /*
1796 * In interactive mode (if no -o OUTFILE, or -p PID is used),
1797 * fatal signals are blocked while syscall stop is processed,
1798 * and acted on in between, when waiting for new syscall stops.
1799 * In non-interactive mode, signals are ignored.
1800 */
1801 if (opt_intr == INTR_WHILE_WAIT) {
1802 sigaddset(&blocked_set, SIGHUP);
1803 sigaddset(&blocked_set, SIGINT);
1804 sigaddset(&blocked_set, SIGQUIT);
1805 sigaddset(&blocked_set, SIGPIPE);
1806 sigaddset(&blocked_set, SIGTERM);
1807 sa.sa_handler = interrupt;
Denys Vlasenkob51581e2012-01-29 16:53:03 +01001808 }
1809 /* SIG_IGN, or set handler for these */
1810 sigaction(SIGHUP, &sa, NULL);
1811 sigaction(SIGINT, &sa, NULL);
1812 sigaction(SIGQUIT, &sa, NULL);
1813 sigaction(SIGPIPE, &sa, NULL);
1814 sigaction(SIGTERM, &sa, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001815 }
Denys Vlasenkofd883382012-03-09 13:03:41 +01001816 if (nprocs != 0 || daemonized_tracer)
Roland McGrath02203312007-06-11 22:06:31 +00001817 startup_attach();
Roland McGrath02203312007-06-11 22:06:31 +00001818
Denys Vlasenkofd883382012-03-09 13:03:41 +01001819 /* Do we want pids printed in our -o OUTFILE?
1820 * -ff: no (every pid has its own file); or
1821 * -f: yes (there can be more pids in the future); or
1822 * -p PID1,PID2: yes (there are already more than one pid)
1823 */
1824 print_pid_pfx = (outfname && followfork < 2 && (followfork == 1 || nprocs > 1));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001825}
1826
Denys Vlasenkoeebb04d2012-01-27 15:24:48 +01001827static struct tcb *
Roland McGrath54e931f2010-09-14 18:59:20 -07001828pid2tcb(int pid)
1829{
1830 int i;
1831
1832 if (pid <= 0)
1833 return NULL;
1834
1835 for (i = 0; i < tcbtabsize; i++) {
1836 struct tcb *tcp = tcbtab[i];
1837 if (tcp->pid == pid && (tcp->flags & TCB_INUSE))
1838 return tcp;
1839 }
1840
1841 return NULL;
1842}
1843
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001844static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001845cleanup(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001846{
1847 int i;
1848 struct tcb *tcp;
Denys Vlasenko35218842012-01-29 21:17:56 +01001849 int fatal_sig;
1850
1851 /* 'interrupted' is a volatile object, fetch it only once */
1852 fatal_sig = interrupted;
1853 if (!fatal_sig)
1854 fatal_sig = SIGTERM;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001855
Roland McGrathee9d4352002-12-18 04:16:10 +00001856 for (i = 0; i < tcbtabsize; i++) {
1857 tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001858 if (!(tcp->flags & TCB_INUSE))
1859 continue;
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001860 if (debug_flag)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001861 fprintf(stderr,
1862 "cleanup: looking at pid %u\n", tcp->pid);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001863 if (tcp->flags & TCB_STRACE_CHILD) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001864 kill(tcp->pid, SIGCONT);
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001865 kill(tcp->pid, fatal_sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001866 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01001867 detach(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001868 }
1869 if (cflag)
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01001870 call_summary(shared_log);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001871}
1872
1873static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001874interrupt(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001875{
Denys Vlasenkoa3559252012-01-29 16:43:51 +01001876 interrupted = sig;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001877}
1878
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001879static int
Denys Vlasenko12014262011-05-30 14:00:14 +02001880trace(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001881{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001882 struct rusage ru;
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001883 struct rusage *rup = cflag ? &ru : NULL;
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001884#ifdef __WALL
Roland McGratheb9e2e82009-06-02 16:49:22 -07001885 static int wait4_options = __WALL;
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001886#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001887
Roland McGratheb9e2e82009-06-02 16:49:22 -07001888 while (nprocs != 0) {
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001889 int pid;
1890 int wait_errno;
1891 int status, sig;
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001892 int stopped;
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001893 struct tcb *tcp;
1894 unsigned event;
1895
Denys Vlasenko222713a2009-03-17 14:29:59 +00001896 if (interrupted)
Roland McGratheb9e2e82009-06-02 16:49:22 -07001897 return 0;
1898 if (interactive)
1899 sigprocmask(SIG_SETMASK, &empty_set, NULL);
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001900#ifdef __WALL
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001901 pid = wait4(-1, &status, wait4_options, rup);
Roland McGrath5bc05552002-12-17 04:50:47 +00001902 if (pid < 0 && (wait4_options & __WALL) && errno == EINVAL) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001903 /* this kernel does not support __WALL */
1904 wait4_options &= ~__WALL;
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001905 pid = wait4(-1, &status, wait4_options, rup);
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001906 }
Roland McGrath5bc05552002-12-17 04:50:47 +00001907 if (pid < 0 && !(wait4_options & __WALL) && errno == ECHILD) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001908 /* most likely a "cloned" process */
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001909 pid = wait4(-1, &status, __WCLONE, rup);
1910 if (pid < 0) {
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02001911 perror_msg("wait4(__WCLONE) failed");
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001912 }
1913 }
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001914#else
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001915 pid = wait4(-1, &status, 0, rup);
Denys Vlasenko978fbc92012-09-13 10:28:43 +02001916#endif /* __WALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001917 wait_errno = errno;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001918 if (interactive)
1919 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001920
Denys Vlasenko26d1b1e2011-08-15 12:24:14 +02001921 if (pid < 0) {
Roland McGratheb9e2e82009-06-02 16:49:22 -07001922 switch (wait_errno) {
1923 case EINTR:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001924 continue;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001925 case ECHILD:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001926 /*
1927 * We would like to verify this case
1928 * but sometimes a race in Solbourne's
1929 * version of SunOS sometimes reports
1930 * ECHILD before sending us SIGCHILD.
1931 */
Roland McGratheb9e2e82009-06-02 16:49:22 -07001932 return 0;
1933 default:
1934 errno = wait_errno;
Denys Vlasenko4c65c442012-03-08 11:54:10 +01001935 perror_msg("wait");
Roland McGratheb9e2e82009-06-02 16:49:22 -07001936 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001937 }
1938 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001939 if (pid == popen_pid) {
1940 if (WIFEXITED(status) || WIFSIGNALED(status))
Denys Vlasenko7dd23382011-06-22 13:03:56 +02001941 popen_pid = 0;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001942 continue;
1943 }
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001944
1945 event = ((unsigned)status >> 16);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001946 if (debug_flag) {
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001947 char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko67559ad2012-03-13 12:05:27 +01001948 char evbuf[sizeof(",PTRACE_EVENT_?? (%u)") + sizeof(int)*3 /*paranoia:*/ + 16];
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001949 strcpy(buf, "???");
1950 if (WIFSIGNALED(status))
1951#ifdef WCOREDUMP
1952 sprintf(buf, "WIFSIGNALED,%ssig=%s",
1953 WCOREDUMP(status) ? "core," : "",
1954 signame(WTERMSIG(status)));
1955#else
1956 sprintf(buf, "WIFSIGNALED,sig=%s",
1957 signame(WTERMSIG(status)));
1958#endif
1959 if (WIFEXITED(status))
1960 sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status));
1961 if (WIFSTOPPED(status))
1962 sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status)));
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02001963#ifdef WIFCONTINUED
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001964 if (WIFCONTINUED(status))
1965 strcpy(buf, "WIFCONTINUED");
Denys Vlasenko5bd67c82011-08-15 11:36:09 +02001966#endif
Denys Vlasenko67559ad2012-03-13 12:05:27 +01001967 evbuf[0] = '\0';
1968 if (event != 0) {
1969 static const char *const event_names[] = {
1970 [PTRACE_EVENT_CLONE] = "CLONE",
1971 [PTRACE_EVENT_FORK] = "FORK",
1972 [PTRACE_EVENT_VFORK] = "VFORK",
1973 [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE",
1974 [PTRACE_EVENT_EXEC] = "EXEC",
1975 [PTRACE_EVENT_EXIT] = "EXIT",
1976 };
1977 const char *e;
1978 if (event < ARRAY_SIZE(event_names))
1979 e = event_names[event];
1980 else {
1981 sprintf(buf, "?? (%u)", event);
1982 e = buf;
1983 }
1984 sprintf(evbuf, ",PTRACE_EVENT_%s", e);
1985 }
1986 fprintf(stderr, " [wait(0x%04x) = %u] %s%s\n", status, pid, buf, evbuf);
Denys Vlasenko1d5f12e2011-06-24 16:41:35 +02001987 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001988
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001989 /* Look up 'pid' in our table. */
Denys Vlasenko5d645812011-08-20 12:48:18 +02001990 tcp = pid2tcb(pid);
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01001991
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01001992 if (!tcp) {
Roland McGrath41c48222008-07-18 00:25:10 +00001993 if (followfork) {
Denys Vlasenko418d66a2009-01-17 01:52:54 +00001994 tcp = alloctcb(pid);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01001995 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop;
Denys Vlasenko3db3b262012-03-16 15:15:14 +01001996 newoutf(tcp);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001997 if (!qflag)
Denys Vlasenko833fb132011-08-17 11:30:56 +02001998 fprintf(stderr, "Process %d attached\n",
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001999 pid);
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002000 } else {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002001 /* This can happen if a clone call used
2002 CLONE_PTRACE itself. */
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002003 if (WIFSTOPPED(status))
Denys Vlasenko97c503f2012-03-09 15:11:21 +01002004 ptrace(PTRACE_CONT, pid, (char *) 0, 0);
Denys Vlasenkocb2ad002011-06-23 13:05:29 +02002005 error_msg_and_die("Unknown pid: %u", pid);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002006 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002007 }
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002008
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002009 clear_regs();
2010 if (WIFSTOPPED(status))
2011 get_regs(pid);
2012
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002013 /* Under Linux, execve changes pid to thread leader's pid,
2014 * and we see this changed pid on EVENT_EXEC and later,
2015 * execve sysexit. Leader "disappears" without exit
2016 * notification. Let user know that, drop leader's tcb,
2017 * and fix up pid in execve thread's tcb.
2018 * Effectively, execve thread's tcb replaces leader's tcb.
2019 *
2020 * BTW, leader is 'stuck undead' (doesn't report WIFEXITED
2021 * on exit syscall) in multithreaded programs exactly
2022 * in order to handle this case.
2023 *
2024 * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0.
2025 * On 2.6 and earlier, it can return garbage.
2026 */
2027 if (event == PTRACE_EVENT_EXEC && os_release >= KERNEL_VERSION(3,0,0)) {
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002028 FILE *fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002029 struct tcb *execve_thread;
2030 long old_pid = 0;
2031
2032 if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) < 0)
2033 goto dont_switch_tcbs;
2034 if (old_pid <= 0 || old_pid == pid)
2035 goto dont_switch_tcbs;
2036 execve_thread = pid2tcb(old_pid);
2037 /* It should be !NULL, but I feel paranoid */
2038 if (!execve_thread)
2039 goto dont_switch_tcbs;
2040
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002041 if (execve_thread->curcol != 0) {
2042 /*
2043 * One case we are here is -ff:
2044 * try "strace -oLOG -ff test/threaded_execve"
2045 */
2046 fprintf(execve_thread->outf, " <pid changed to %d ...>\n", pid);
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002047 /*execve_thread->curcol = 0; - no need, see code below */
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002048 }
2049 /* Swap output FILEs (needed for -ff) */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002050 fp = execve_thread->outf;
2051 execve_thread->outf = tcp->outf;
2052 tcp->outf = fp;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002053 /* And their column positions */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002054 execve_thread->curcol = tcp->curcol;
2055 tcp->curcol = 0;
Denys Vlasenko8511f2a2012-03-22 09:35:51 +01002056 /* Drop leader, but close execve'd thread outfile (if -ff) */
2057 droptcb(tcp);
2058 /* Switch to the thread, reusing leader's outfile and pid */
2059 tcp = execve_thread;
2060 tcp->pid = pid;
2061 if (cflag != CFLAG_ONLY_STATS) {
2062 printleader(tcp);
2063 tprintf("+++ superseded by execve in pid %lu +++\n", old_pid);
2064 line_ended();
2065 tcp->flags |= TCB_REPRINT;
2066 }
2067 }
2068 dont_switch_tcbs:
2069
2070 if (event == PTRACE_EVENT_EXEC && detach_on_execve) {
2071 if (!skip_startup_execve)
2072 detach(tcp);
2073 /* This was initial execve for "strace PROG". Skip. */
2074 skip_startup_execve = 0;
2075 }
2076
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002077 /* Set current output file */
Denys Vlasenko6764f8f2012-03-22 09:56:20 +01002078 current_tcp = tcp;
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002079
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002080 if (cflag) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002081 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2082 tcp->stime = ru.ru_stime;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002083 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002084
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002085 if (WIFSIGNALED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002086 if (pid == strace_child)
2087 exit_code = 0x100 | WTERMSIG(status);
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002088 if (cflag != CFLAG_ONLY_STATS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01002089 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)
2090 ) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002091 printleader(tcp);
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002092#ifdef WCOREDUMP
Denys Vlasenko000b6012012-01-28 01:25:03 +01002093 tprintf("+++ killed by %s %s+++\n",
Roland McGrath2efe8792004-01-13 09:59:45 +00002094 signame(WTERMSIG(status)),
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002095 WCOREDUMP(status) ? "(core dumped) " : "");
2096#else
Denys Vlasenko000b6012012-01-28 01:25:03 +01002097 tprintf("+++ killed by %s +++\n",
Denys Vlasenko13d22f12011-06-24 23:01:57 +02002098 signame(WTERMSIG(status)));
Roland McGrath2efe8792004-01-13 09:59:45 +00002099#endif
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002100 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002101 }
2102 droptcb(tcp);
2103 continue;
2104 }
2105 if (WIFEXITED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002106 if (pid == strace_child)
2107 exit_code = WEXITSTATUS(status);
Denys Vlasenkob5e09082012-03-21 14:27:40 +01002108 if (cflag != CFLAG_ONLY_STATS) {
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002109 printleader(tcp);
Denys Vlasenko000b6012012-01-28 01:25:03 +01002110 tprintf("+++ exited with %d +++\n", WEXITSTATUS(status));
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002111 line_ended();
Denys Vlasenko19cdada2011-08-17 10:45:32 +02002112 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002113 droptcb(tcp);
2114 continue;
2115 }
2116 if (!WIFSTOPPED(status)) {
2117 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
2118 droptcb(tcp);
2119 continue;
2120 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002121
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002122 /* Is this the very first time we see this tracee stopped? */
2123 if (tcp->flags & TCB_STARTUP) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002124 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002125 fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n", tcp->pid);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002126 tcp->flags &= ~TCB_STARTUP;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002127 if (tcp->flags & TCB_BPTSET) {
Roland McGrath02203312007-06-11 22:06:31 +00002128 /*
2129 * One example is a breakpoint inherited from
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002130 * parent through fork().
Roland McGrath02203312007-06-11 22:06:31 +00002131 */
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002132 if (clearbpt(tcp) < 0) {
2133 /* Pretty fatal */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002134 droptcb(tcp);
2135 cleanup();
2136 return -1;
2137 }
2138 }
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002139 if (ptrace_setoptions) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002140 if (debug_flag)
Denys Vlasenko44f87ef2011-08-17 15:18:21 +02002141 fprintf(stderr, "setting opts %x on pid %d\n", ptrace_setoptions, tcp->pid);
2142 if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) {
2143 if (errno != ESRCH) {
2144 /* Should never happen, really */
2145 perror_msg_and_die("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02002146 }
2147 }
2148 }
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002149 }
2150
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002151 sig = WSTOPSIG(status);
2152
Denys Vlasenkof7db5dd2012-01-28 01:16:02 +01002153 if (event != 0) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002154 /* Ptrace event */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002155#if USE_SEIZE
Denys Vlasenko26bc0602012-07-10 16:36:32 +02002156 if (event == PTRACE_EVENT_STOP) {
Denys Vlasenko67038162012-01-29 16:46:46 +01002157 /*
2158 * PTRACE_INTERRUPT-stop or group-stop.
2159 * PTRACE_INTERRUPT-stop has sig == SIGTRAP here.
2160 */
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002161 if (sig == SIGSTOP
2162 || sig == SIGTSTP
2163 || sig == SIGTTIN
2164 || sig == SIGTTOU
2165 ) {
2166 stopped = 1;
2167 goto show_stopsig;
2168 }
2169 }
2170#endif
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002171 goto restart_tracee_with_sig_0;
2172 }
2173
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002174 /* Is this post-attach SIGSTOP?
2175 * Interestingly, the process may stop
2176 * with STOPSIG equal to some other signal
2177 * than SIGSTOP if we happend to attach
2178 * just before the process takes a signal.
2179 */
2180 if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002181 if (debug_flag)
Denys Vlasenkof88837a2011-09-05 14:05:46 +02002182 fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid);
2183 tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP;
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002184 goto restart_tracee_with_sig_0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002185 }
2186
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002187 if (sig != syscall_trap_sig) {
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002188 siginfo_t si;
2189
2190 /* Nonzero (true) if tracee is stopped by signal
2191 * (as opposed to "tracee received signal").
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002192 * TODO: shouldn't we check for errno == EINVAL too?
2193 * We can get ESRCH instead, you know...
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002194 */
2195 stopped = (ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0);
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002196#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002197 show_stopsig:
Denys Vlasenko67038162012-01-29 16:46:46 +01002198#endif
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002199 if (cflag != CFLAG_ONLY_STATS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01002200 && (qual_flags[sig] & QUAL_SIGNAL)
2201 ) {
Dmitry V. Levinc15dfc72011-03-10 14:44:45 +00002202#if defined(PT_CR_IPSR) && defined(PT_CR_IIP)
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002203 long pc = 0;
2204 long psr = 0;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002205
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002206 upeek(tcp, PT_CR_IPSR, &psr);
2207 upeek(tcp, PT_CR_IIP, &pc);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002208
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002209# define PSR_RI 41
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002210 pc += (psr >> PSR_RI) & 0x3;
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002211# define PC_FORMAT_STR " @ %lx"
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002212# define PC_FORMAT_ARG , pc
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002213#else
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002214# define PC_FORMAT_STR ""
2215# define PC_FORMAT_ARG /* nothing */
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002216#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002217 printleader(tcp);
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002218 if (!stopped) {
Denys Vlasenko2c4fb902012-03-15 17:24:49 +01002219 tprintf("--- %s ", signame(sig));
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002220 printsiginfo(&si, verbose(tcp));
Denys Vlasenko2c4fb902012-03-15 17:24:49 +01002221 tprintf(PC_FORMAT_STR " ---\n"
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002222 PC_FORMAT_ARG);
2223 } else
Denys Vlasenko2c4fb902012-03-15 17:24:49 +01002224 tprintf("--- stopped by %s" PC_FORMAT_STR " ---\n",
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002225 signame(sig)
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002226 PC_FORMAT_ARG);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002227 line_ended();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002228 }
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002229
2230 if (!stopped)
2231 /* It's signal-delivery-stop. Inject the signal */
2232 goto restart_tracee;
2233
2234 /* It's group-stop */
Denys Vlasenko5c9d8f42013-02-19 15:30:12 +01002235#if USE_SEIZE
Denys Vlasenko31fa8a22012-01-29 02:01:44 +01002236 if (use_seize) {
2237 /*
2238 * This ends ptrace-stop, but does *not* end group-stop.
2239 * This makes stopping signals work properly on straced process
2240 * (that is, process really stops. It used to continue to run).
2241 */
2242 if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) {
2243 cleanup();
2244 return -1;
2245 }
2246 continue;
2247 }
2248 /* We don't have PTRACE_LISTEN support... */
2249#endif
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002250 goto restart_tracee;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002251 }
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002252
2253 /* We handled quick cases, we are permitted to interrupt now. */
Roland McGrath02203312007-06-11 22:06:31 +00002254 if (interrupted)
2255 return 0;
Denys Vlasenko2ecba322011-08-21 17:35:39 +02002256
2257 /* This should be syscall entry or exit.
2258 * (Or it still can be that pesky post-execve SIGTRAP!)
2259 * Handle it.
2260 */
Denys Vlasenko23506752012-03-21 10:32:49 +01002261 if (trace_syscall(tcp) < 0) {
2262 /* ptrace() failed in trace_syscall().
Roland McGratheb9e2e82009-06-02 16:49:22 -07002263 * Likely a result of process disappearing mid-flight.
Denys Vlasenko23506752012-03-21 10:32:49 +01002264 * Observed case: exit_group() or SIGKILL terminating
Denys Vlasenkof1e69032012-01-04 15:15:26 +01002265 * all processes in thread group.
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002266 * We assume that ptrace error was caused by process death.
Denys Vlasenkof2025022012-03-09 15:29:45 +01002267 * We used to detach(tcp) here, but since we no longer
2268 * implement "detach before death" policy/hack,
2269 * we can let this process to report its death to us
2270 * normally, via WIFEXITED or WIFSIGNALED wait status.
2271 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002272 continue;
2273 }
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002274 restart_tracee_with_sig_0:
2275 sig = 0;
2276 restart_tracee:
Denys Vlasenko6cda73f2011-09-02 16:23:53 +02002277 if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002278 cleanup();
2279 return -1;
2280 }
2281 }
2282 return 0;
2283}
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002284
2285int
2286main(int argc, char *argv[])
2287{
2288 init(argc, argv);
2289
2290 /* Run main tracing loop */
2291 if (trace() < 0)
2292 return 1;
2293
2294 cleanup();
2295 fflush(NULL);
Dmitry V. Levincf534362012-07-12 20:54:46 +00002296 if (shared_log != stderr)
2297 fclose(shared_log);
2298 if (popen_pid) {
2299 while (waitpid(popen_pid, NULL, 0) < 0 && errno == EINTR)
2300 ;
2301 }
Denys Vlasenkoecc8b972012-03-12 23:05:25 +01002302 if (exit_code > 0xff) {
2303 /* Avoid potential core file clobbering. */
2304 struct rlimit rlim = {0, 0};
2305 setrlimit(RLIMIT_CORE, &rlim);
2306
2307 /* Child was killed by a signal, mimic that. */
2308 exit_code &= 0xff;
2309 signal(exit_code, SIG_DFL);
2310 raise(exit_code);
2311 /* Paranoia - what if this signal is not fatal?
2312 Exit with 128 + signo then. */
2313 exit_code += 128;
2314 }
2315
2316 return exit_code;
2317}