blob: 3298795a19147cb163287fa574c742eaf17af2e7 [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.
29 *
30 * $Id$
31 */
32
33#include "defs.h"
34
Roland McGrath795edb12005-02-02 04:44:57 +000035#include <sys/types.h>
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020036#include <stdarg.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000037#include <signal.h>
38#include <errno.h>
39#include <sys/param.h>
40#include <fcntl.h>
41#include <sys/resource.h>
42#include <sys/wait.h>
43#include <sys/stat.h>
44#include <pwd.h>
45#include <grp.h>
46#include <string.h>
John Hughes19e49982001-10-19 08:59:12 +000047#include <limits.h>
Roland McGrath70b08532004-04-09 00:25:21 +000048#include <dirent.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000049
Roland McGrath134813a2007-06-02 00:07:33 +000050#ifdef LINUX
51# include <asm/unistd.h>
52# if defined __NR_tgkill
Denys Vlasenkob63256e2011-06-07 12:13:24 +020053# define my_tgkill(pid, tid, sig) syscall(__NR_tgkill, (pid), (tid), (sig))
Roland McGrath134813a2007-06-02 00:07:33 +000054# elif defined __NR_tkill
Denys Vlasenkob63256e2011-06-07 12:13:24 +020055# define my_tgkill(pid, tid, sig) syscall(__NR_tkill, (tid), (sig))
Roland McGrath134813a2007-06-02 00:07:33 +000056# else
57 /* kill() may choose arbitrarily the target task of the process group
58 while we later wait on a that specific TID. PID process waits become
59 TID task specific waits for a process under ptrace(2). */
60# warning "Neither tkill(2) nor tgkill(2) available, risk of strace hangs!"
Denys Vlasenkob63256e2011-06-07 12:13:24 +020061# define my_tgkill(pid, tid, sig) kill((tid), (sig))
Roland McGrath134813a2007-06-02 00:07:33 +000062# endif
63#endif
64
Wichert Akkerman7b3346b2001-10-09 23:47:38 +000065#if defined(IA64) && defined(LINUX)
66# include <asm/ptrace_offsets.h>
67#endif
68
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +000069#ifdef USE_PROCFS
70#include <poll.h>
71#endif
72
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000073#ifdef SVR4
74#include <sys/stropts.h>
Wichert Akkermanea78f0f1999-11-29 15:34:02 +000075#ifdef HAVE_MP_PROCFS
John Hughes1d08dcf2001-07-10 13:48:44 +000076#ifdef HAVE_SYS_UIO_H
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000077#include <sys/uio.h>
78#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000079#endif
John Hughes1d08dcf2001-07-10 13:48:44 +000080#endif
Denys Vlasenko96d5a762008-12-29 19:13:27 +000081extern char **environ;
Denys Vlasenko418d66a2009-01-17 01:52:54 +000082extern int optind;
83extern char *optarg;
Denys Vlasenko96d5a762008-12-29 19:13:27 +000084
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000085
Roland McGrath41c48222008-07-18 00:25:10 +000086int debug = 0, followfork = 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020087unsigned int ptrace_setoptions_followfork = 0;
Denys Vlasenkof8bc0652011-05-24 20:30:24 +020088unsigned int ptrace_setoptions_for_all = 0;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +020089/* Which WSTOPSIG(status) value marks syscall traps? */
Denys Vlasenko75422762011-05-27 14:36:01 +020090static unsigned int syscall_trap_sig = SIGTRAP;
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +000091int dtime = 0, xflag = 0, qflag = 0;
92cflag_t cflag = CFLAG_NONE;
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +000093static int iflag = 0, interactive = 0, pflag_seen = 0, rflag = 0, tflag = 0;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +000094/*
95 * daemonized_tracer supports -D option.
96 * With this option, strace forks twice.
97 * Unlike normal case, with -D *grandparent* process exec's,
98 * becoming a traced process. Child exits (this prevents traced process
99 * from having children it doesn't expect to have), and grandchild
100 * attaches to grandparent similarly to strace -p PID.
101 * This allows for more transparent interaction in cases
102 * when process and its parent are communicating via signals,
103 * wait() etc. Without -D, strace process gets lodged in between,
104 * disrupting parent<->child link.
105 */
106static bool daemonized_tracer = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000107
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000108/* Sometimes we want to print only succeeding syscalls. */
109int not_failing_only = 0;
110
Grant Edwards8a082772011-04-07 20:25:40 +0000111/* Show path associated with fd arguments */
112int show_fd_path = 0;
113
114/* are we filtering traces based on paths? */
115int tracing_paths = 0;
116
Dmitry V. Levina6809652008-11-10 17:14:58 +0000117static int exit_code = 0;
118static int strace_child = 0;
Denys Vlasenko75422762011-05-27 14:36:01 +0200119static int strace_tracer_pid = 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -0700120
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000121static char *username = NULL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000122uid_t run_uid;
123gid_t run_gid;
124
125int acolumn = DEFAULT_ACOLUMN;
126int max_strlen = DEFAULT_STRLEN;
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000127static char *outfname = NULL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000128FILE *outf;
Andreas Schwabccdff482009-10-27 16:27:13 +0100129static int curcol;
Roland McGrathee9d4352002-12-18 04:16:10 +0000130struct tcb **tcbtab;
131unsigned int nprocs, tcbtabsize;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000132const char *progname;
Roland McGratheb9e2e82009-06-02 16:49:22 -0700133extern char **environ;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000134
Andreas Schwabe5355de2009-10-27 16:56:43 +0100135static int detach(struct tcb *tcp, int sig);
136static int trace(void);
137static void cleanup(void);
138static void interrupt(int sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000139static sigset_t empty_set, blocked_set;
140
141#ifdef HAVE_SIG_ATOMIC_T
142static volatile sig_atomic_t interrupted;
143#else /* !HAVE_SIG_ATOMIC_T */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000144static volatile int interrupted;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000145#endif /* !HAVE_SIG_ATOMIC_T */
146
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000147#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000148
Andreas Schwabe5355de2009-10-27 16:56:43 +0100149static struct tcb *pfd2tcb(int pfd);
150static void reaper(int sig);
151static void rebuild_pollv(void);
Roland McGrathee9d4352002-12-18 04:16:10 +0000152static struct pollfd *pollv;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000153
154#ifndef HAVE_POLLABLE_PROCFS
155
Andreas Schwabe5355de2009-10-27 16:56:43 +0100156static void proc_poll_open(void);
157static void proc_poller(int pfd);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000158
159struct proc_pollfd {
160 int fd;
161 int revents;
162 int pid;
163};
164
165static int poller_pid;
166static int proc_poll_pipe[2] = { -1, -1 };
167
168#endif /* !HAVE_POLLABLE_PROCFS */
169
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000170#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000171#define POLLWANT POLLWRNORM
172#else
173#define POLLWANT POLLPRI
174#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000175#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000176
177static void
178usage(ofp, exitval)
179FILE *ofp;
180int exitval;
181{
182 fprintf(ofp, "\
Grant Edwards8a082772011-04-07 20:25:40 +0000183usage: strace [-CdDffhiqrtttTvVxxy] [-a column] [-e expr] ... [-o file]\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000184 [-p pid] ... [-s strsize] [-u username] [-E var=val] ...\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000185 [-P path] [command [arg ...]]\n\
Andreas Schwabb87d30c2010-06-11 15:49:36 +0200186 or: strace -c [-D] [-e expr] ... [-O overhead] [-S sortby] [-E var=val] ...\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000187 [command [arg ...]]\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000188-c -- count time, calls, and errors for each syscall and report summary\n\
Andreas Schwabb87d30c2010-06-11 15:49:36 +0200189-C -- like -c but also print regular output while processes are running\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000190-f -- follow forks, -ff -- with output into separate files\n\
191-F -- attempt to follow vforks, -h -- print help message\n\
192-i -- print instruction pointer at time of syscall\n\
193-q -- suppress messages about attaching, detaching, etc.\n\
194-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
195-T -- print time spent in each syscall, -V -- print version\n\
196-v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\
197-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000198-y -- print paths associated with file descriptor arguments\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000199-a column -- alignment COLUMN for printing syscall results (default %d)\n\
200-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
201 options: trace, abbrev, verbose, raw, signal, read, or write\n\
202-o file -- send trace output to FILE instead of stderr\n\
203-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
204-p pid -- trace process with process id PID, may be repeated\n\
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000205-D -- run tracer process as a detached grandchild, not as parent\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000206-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
207-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
208-u username -- run command as username handling setuid and/or setgid\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000209-E var=val -- put var=val in the environment for command\n\
210-E var -- remove var from the environment for command\n\
Grant Edwards8a082772011-04-07 20:25:40 +0000211-P path -- trace accesses to path\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000212" /* this is broken, so don't document it
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000213-z -- print only succeeding syscalls\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000214 */
215, DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000216 exit(exitval);
217}
218
Denys Vlasenko75422762011-05-27 14:36:01 +0200219static void die(void) __attribute__ ((noreturn));
220static void die(void)
221{
222 if (strace_tracer_pid == getpid()) {
223 cflag = 0;
224 cleanup();
225 }
226 exit(1);
227}
228
229static void verror_msg(int err_no, const char *fmt, va_list p)
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200230{
Dmitry V. Levin44d05322011-06-09 15:50:41 +0000231 fflush(NULL);
232 fprintf(stderr, "%s: ", progname);
233 vfprintf(stderr, fmt, p);
234 if (err_no)
235 fprintf(stderr, ": %s\n", strerror(err_no));
236 else
237 putc('\n', stderr);
238 fflush(stderr);
Denys Vlasenko75422762011-05-27 14:36:01 +0200239}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200240
Denys Vlasenko75422762011-05-27 14:36:01 +0200241void error_msg(const char *fmt, ...)
242{
243 va_list p;
244 va_start(p, fmt);
245 verror_msg(0, fmt, p);
246 va_end(p);
247}
248
249void error_msg_and_die(const char *fmt, ...)
250{
251 va_list p;
252 va_start(p, fmt);
253 verror_msg(0, fmt, p);
254 die();
255}
256
257void perror_msg(const char *fmt, ...)
258{
259 va_list p;
260 va_start(p, fmt);
261 verror_msg(errno, fmt, p);
262 va_end(p);
263}
264
265void perror_msg_and_die(const char *fmt, ...)
266{
267 va_list p;
268 va_start(p, fmt);
269 verror_msg(errno, fmt, p);
270 die();
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200271}
272
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000273#ifdef SVR4
274#ifdef MIPS
275void
276foobar()
277{
278}
279#endif /* MIPS */
280#endif /* SVR4 */
281
Mike Frysingerc1a5b7e2009-10-07 20:41:29 -0400282/* Glue for systems without a MMU that cannot provide fork() */
283#ifdef HAVE_FORK
284# define strace_vforked 0
285#else
286# define strace_vforked 1
287# define fork() vfork()
288#endif
289
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000290static int
291set_cloexec_flag(int fd)
292{
293 int flags, newflags;
294
295 if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
296 {
297 fprintf(stderr, "%s: fcntl F_GETFD: %s\n",
298 progname, strerror(errno));
299 return -1;
300 }
301
302 newflags = flags | FD_CLOEXEC;
303 if (flags == newflags)
304 return 0;
305
306 if (fcntl(fd, F_SETFD, newflags) < 0)
307 {
308 fprintf(stderr, "%s: fcntl F_SETFD: %s\n",
309 progname, strerror(errno));
310 return -1;
311 }
312
313 return 0;
314}
315
316/*
317 * When strace is setuid executable, we have to swap uids
318 * before and after filesystem and process management operations.
319 */
320static void
321swap_uid(void)
322{
323#ifndef SVR4
324 int euid = geteuid(), uid = getuid();
325
326 if (euid != uid && setreuid(euid, uid) < 0)
327 {
328 fprintf(stderr, "%s: setreuid: %s\n",
329 progname, strerror(errno));
330 exit(1);
331 }
332#endif
333}
334
Roland McGrath4bfa6262007-07-05 20:03:16 +0000335#if _LFS64_LARGEFILE
336# define fopen_for_output fopen64
337#else
338# define fopen_for_output fopen
339#endif
340
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000341static FILE *
342strace_fopen(const char *path, const char *mode)
343{
344 FILE *fp;
345
346 swap_uid();
Roland McGrath4bfa6262007-07-05 20:03:16 +0000347 if ((fp = fopen_for_output(path, mode)) == NULL)
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000348 fprintf(stderr, "%s: can't fopen '%s': %s\n",
349 progname, path, strerror(errno));
350 swap_uid();
351 if (fp && set_cloexec_flag(fileno(fp)) < 0)
352 {
353 fclose(fp);
354 fp = NULL;
355 }
356 return fp;
357}
358
359static int popen_pid = -1;
360
361#ifndef _PATH_BSHELL
362# define _PATH_BSHELL "/bin/sh"
363#endif
364
365/*
366 * We cannot use standard popen(3) here because we have to distinguish
367 * popen child process from other processes we trace, and standard popen(3)
368 * does not export its child's pid.
369 */
370static FILE *
371strace_popen(const char *command)
372{
373 int fds[2];
374
375 swap_uid();
376 if (pipe(fds) < 0)
377 {
378 fprintf(stderr, "%s: pipe: %s\n",
379 progname, strerror(errno));
380 swap_uid();
381 return NULL;
382 }
383
384 if (set_cloexec_flag(fds[1]) < 0)
385 {
386 close(fds[0]);
387 close(fds[1]);
388 swap_uid();
389 return NULL;
390 }
391
392 if ((popen_pid = fork()) == -1)
393 {
394 fprintf(stderr, "%s: fork: %s\n",
395 progname, strerror(errno));
396 close(fds[0]);
397 close(fds[1]);
398 swap_uid();
399 return NULL;
400 }
401
402 if (popen_pid)
403 {
404 /* parent */
405 close(fds[0]);
406 swap_uid();
407 return fdopen(fds[1], "w");
408 } else
409 {
410 /* child */
411 close(fds[1]);
412 if (fds[0] && (dup2(fds[0], 0) || close(fds[0])))
413 {
414 fprintf(stderr, "%s: dup2: %s\n",
415 progname, strerror(errno));
416 _exit(1);
417 }
418 execl(_PATH_BSHELL, "sh", "-c", command, NULL);
419 fprintf(stderr, "%s: execl: %s: %s\n",
420 progname, _PATH_BSHELL, strerror(errno));
421 _exit(1);
422 }
423}
424
425static int
426newoutf(struct tcb *tcp)
427{
428 if (outfname && followfork > 1) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000429 char name[520 + sizeof(int) * 3];
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000430 FILE *fp;
431
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000432 sprintf(name, "%.512s.%u", outfname, tcp->pid);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000433 if ((fp = strace_fopen(name, "w")) == NULL)
434 return -1;
435 tcp->outf = fp;
436 }
437 return 0;
438}
439
Roland McGrath02203312007-06-11 22:06:31 +0000440static void
441startup_attach(void)
442{
443 int tcbi;
444 struct tcb *tcp;
445
446 /*
447 * Block user interruptions as we would leave the traced
448 * process stopped (process state T) if we would terminate in
449 * between PTRACE_ATTACH and wait4 () on SIGSTOP.
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200450 * We rely on cleanup() from this point on.
Roland McGrath02203312007-06-11 22:06:31 +0000451 */
452 if (interactive)
453 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
454
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000455 if (daemonized_tracer) {
456 pid_t pid = fork();
457 if (pid < 0) {
458 _exit(1);
459 }
460 if (pid) { /* parent */
461 /*
Denys Vlasenko75422762011-05-27 14:36:01 +0200462 * Wait for grandchild to attach to straced process
463 * (grandparent). Grandchild SIGKILLs us after it attached.
464 * Grandparent's wait() is unblocked by our death,
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000465 * it proceeds to exec the straced program.
466 */
467 pause();
468 _exit(0); /* paranoia */
469 }
Denys Vlasenko75422762011-05-27 14:36:01 +0200470 /* grandchild */
471 /* We will be the tracer process. Remember our new pid: */
472 strace_tracer_pid = getpid();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000473 }
474
Roland McGrath02203312007-06-11 22:06:31 +0000475 for (tcbi = 0; tcbi < tcbtabsize; tcbi++) {
476 tcp = tcbtab[tcbi];
477 if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
478 continue;
479#ifdef LINUX
480 if (tcp->flags & TCB_CLONE_THREAD)
481 continue;
482#endif
483 /* Reinitialize the output since it may have changed. */
484 tcp->outf = outf;
485 if (newoutf(tcp) < 0)
486 exit(1);
487
488#ifdef USE_PROCFS
489 if (proc_open(tcp, 1) < 0) {
490 fprintf(stderr, "trouble opening proc file\n");
491 droptcb(tcp);
492 continue;
493 }
494#else /* !USE_PROCFS */
495# ifdef LINUX
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000496 if (followfork && !daemonized_tracer) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000497 char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3];
Roland McGrath02203312007-06-11 22:06:31 +0000498 DIR *dir;
499
500 sprintf(procdir, "/proc/%d/task", tcp->pid);
501 dir = opendir(procdir);
502 if (dir != NULL) {
503 unsigned int ntid = 0, nerr = 0;
504 struct dirent *de;
505 int tid;
506 while ((de = readdir(dir)) != NULL) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000507 if (de->d_fileno == 0)
Roland McGrath02203312007-06-11 22:06:31 +0000508 continue;
509 tid = atoi(de->d_name);
510 if (tid <= 0)
511 continue;
512 ++ntid;
Denys Vlasenkoaab52ca2009-03-17 14:46:54 +0000513 if (ptrace(PTRACE_ATTACH, tid, (char *) 1, 0) < 0)
Roland McGrath02203312007-06-11 22:06:31 +0000514 ++nerr;
Denys Vlasenkoaab52ca2009-03-17 14:46:54 +0000515 else if (tid != tcbtab[tcbi]->pid) {
Denys Vlasenko418d66a2009-01-17 01:52:54 +0000516 tcp = alloctcb(tid);
Wang Chao21b8db42010-08-27 17:43:16 +0800517 tcp->flags |= TCB_ATTACHED|TCB_CLONE_THREAD|TCB_FOLLOWFORK;
Roland McGrath02203312007-06-11 22:06:31 +0000518 tcbtab[tcbi]->nchildren++;
519 tcbtab[tcbi]->nclone_threads++;
Roland McGrath02203312007-06-11 22:06:31 +0000520 tcp->parent = tcbtab[tcbi];
521 }
Denys Vlasenkoaab52ca2009-03-17 14:46:54 +0000522 if (interactive) {
523 sigprocmask(SIG_SETMASK, &empty_set, NULL);
524 if (interrupted)
525 return;
526 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
527 }
Roland McGrath02203312007-06-11 22:06:31 +0000528 }
529 closedir(dir);
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000530 ntid -= nerr;
531 if (ntid == 0) {
Roland McGrath02203312007-06-11 22:06:31 +0000532 perror("attach: ptrace(PTRACE_ATTACH, ...)");
533 droptcb(tcp);
534 continue;
535 }
536 if (!qflag) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000537 fprintf(stderr, ntid > 1
538? "Process %u attached with %u threads - interrupt to quit\n"
539: "Process %u attached - interrupt to quit\n",
540 tcbtab[tcbi]->pid, ntid);
Roland McGrath02203312007-06-11 22:06:31 +0000541 }
542 continue;
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000543 } /* if (opendir worked) */
544 } /* if (-f) */
Roland McGrath02203312007-06-11 22:06:31 +0000545# endif
546 if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
547 perror("attach: ptrace(PTRACE_ATTACH, ...)");
548 droptcb(tcp);
549 continue;
550 }
551 /* INTERRUPTED is going to be checked at the top of TRACE. */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000552
553 if (daemonized_tracer) {
554 /*
555 * It is our grandparent we trace, not a -p PID.
556 * Don't want to just detach on exit, so...
557 */
558 tcp->flags &= ~TCB_ATTACHED;
559 /*
560 * Make parent go away.
561 * Also makes grandparent's wait() unblock.
562 */
563 kill(getppid(), SIGKILL);
564 }
565
Roland McGrath02203312007-06-11 22:06:31 +0000566#endif /* !USE_PROCFS */
567 if (!qflag)
568 fprintf(stderr,
569 "Process %u attached - interrupt to quit\n",
570 tcp->pid);
571 }
572
573 if (interactive)
574 sigprocmask(SIG_SETMASK, &empty_set, NULL);
575}
576
577static void
Denys Vlasenko12014262011-05-30 14:00:14 +0200578startup_child(char **argv)
Roland McGrath02203312007-06-11 22:06:31 +0000579{
580 struct stat statbuf;
581 const char *filename;
582 char pathname[MAXPATHLEN];
583 int pid = 0;
584 struct tcb *tcp;
585
586 filename = argv[0];
587 if (strchr(filename, '/')) {
588 if (strlen(filename) > sizeof pathname - 1) {
589 errno = ENAMETOOLONG;
590 perror("strace: exec");
591 exit(1);
592 }
593 strcpy(pathname, filename);
594 }
595#ifdef USE_DEBUGGING_EXEC
596 /*
597 * Debuggers customarily check the current directory
598 * first regardless of the path but doing that gives
599 * security geeks a panic attack.
600 */
601 else if (stat(filename, &statbuf) == 0)
602 strcpy(pathname, filename);
603#endif /* USE_DEBUGGING_EXEC */
604 else {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000605 const char *path;
Roland McGrath02203312007-06-11 22:06:31 +0000606 int m, n, len;
607
608 for (path = getenv("PATH"); path && *path; path += m) {
609 if (strchr(path, ':')) {
610 n = strchr(path, ':') - path;
611 m = n + 1;
612 }
613 else
614 m = n = strlen(path);
615 if (n == 0) {
616 if (!getcwd(pathname, MAXPATHLEN))
617 continue;
618 len = strlen(pathname);
619 }
620 else if (n > sizeof pathname - 1)
621 continue;
622 else {
623 strncpy(pathname, path, n);
624 len = n;
625 }
626 if (len && pathname[len - 1] != '/')
627 pathname[len++] = '/';
628 strcpy(pathname + len, filename);
629 if (stat(pathname, &statbuf) == 0 &&
630 /* Accept only regular files
631 with some execute bits set.
632 XXX not perfect, might still fail */
633 S_ISREG(statbuf.st_mode) &&
634 (statbuf.st_mode & 0111))
635 break;
636 }
637 }
638 if (stat(pathname, &statbuf) < 0) {
639 fprintf(stderr, "%s: %s: command not found\n",
640 progname, filename);
641 exit(1);
642 }
Dmitry V. Levina6809652008-11-10 17:14:58 +0000643 strace_child = pid = fork();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000644 if (pid < 0) {
Roland McGrath02203312007-06-11 22:06:31 +0000645 perror("strace: fork");
646 cleanup();
647 exit(1);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000648 }
Denys Vlasenko75422762011-05-27 14:36:01 +0200649 if ((pid != 0 && daemonized_tracer) /* -D: parent to become a traced process */
650 || (pid == 0 && !daemonized_tracer) /* not -D: child to become a traced process */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000651 ) {
652 pid = getpid();
Roland McGrath02203312007-06-11 22:06:31 +0000653#ifdef USE_PROCFS
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200654 if (outf != stderr) close(fileno(outf));
Roland McGrath02203312007-06-11 22:06:31 +0000655#ifdef MIPS
656 /* Kludge for SGI, see proc_open for details. */
657 sa.sa_handler = foobar;
658 sa.sa_flags = 0;
659 sigemptyset(&sa.sa_mask);
660 sigaction(SIGINT, &sa, NULL);
661#endif /* MIPS */
662#ifndef FREEBSD
663 pause();
664#else /* FREEBSD */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000665 kill(pid, SIGSTOP); /* stop HERE */
Roland McGrath02203312007-06-11 22:06:31 +0000666#endif /* FREEBSD */
667#else /* !USE_PROCFS */
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200668 if (outf != stderr)
669 close(fileno(outf));
Roland McGrath02203312007-06-11 22:06:31 +0000670
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000671 if (!daemonized_tracer) {
672 if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
673 perror("strace: ptrace(PTRACE_TRACEME, ...)");
674 exit(1);
675 }
676 if (debug)
677 kill(pid, SIGSTOP);
Roland McGrath02203312007-06-11 22:06:31 +0000678 }
Roland McGrath02203312007-06-11 22:06:31 +0000679
680 if (username != NULL || geteuid() == 0) {
681 uid_t run_euid = run_uid;
682 gid_t run_egid = run_gid;
683
684 if (statbuf.st_mode & S_ISUID)
685 run_euid = statbuf.st_uid;
686 if (statbuf.st_mode & S_ISGID)
687 run_egid = statbuf.st_gid;
688
689 /*
690 * It is important to set groups before we
691 * lose privileges on setuid.
692 */
693 if (username != NULL) {
694 if (initgroups(username, run_gid) < 0) {
695 perror("initgroups");
696 exit(1);
697 }
698 if (setregid(run_gid, run_egid) < 0) {
699 perror("setregid");
700 exit(1);
701 }
702 if (setreuid(run_uid, run_euid) < 0) {
703 perror("setreuid");
704 exit(1);
705 }
706 }
707 }
708 else
709 setreuid(run_uid, run_uid);
710
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000711 if (!daemonized_tracer) {
712 /*
713 * Induce an immediate stop so that the parent
714 * will resume us with PTRACE_SYSCALL and display
715 * this execve call normally.
Mike Frysingerc1a5b7e2009-10-07 20:41:29 -0400716 * Unless of course we're on a no-MMU system where
717 * we vfork()-ed, so we cannot stop the child.
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000718 */
Mike Frysingerc1a5b7e2009-10-07 20:41:29 -0400719 if (!strace_vforked)
720 kill(getpid(), SIGSTOP);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000721 } else {
722 struct sigaction sv_sigchld;
723 sigaction(SIGCHLD, NULL, &sv_sigchld);
724 /*
725 * Make sure it is not SIG_IGN, otherwise wait
726 * will not block.
727 */
728 signal(SIGCHLD, SIG_DFL);
729 /*
730 * Wait for grandchild to attach to us.
731 * It kills child after that, and wait() unblocks.
732 */
733 alarm(3);
734 wait(NULL);
735 alarm(0);
736 sigaction(SIGCHLD, &sv_sigchld, NULL);
737 }
Roland McGrath02203312007-06-11 22:06:31 +0000738#endif /* !USE_PROCFS */
739
740 execv(pathname, argv);
741 perror("strace: exec");
742 _exit(1);
Roland McGrath02203312007-06-11 22:06:31 +0000743 }
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000744
745 /* We are the tracer. */
Denys Vlasenko75422762011-05-27 14:36:01 +0200746 /* With -D, we are *child* here, IOW: different pid. Fetch it. */
747 strace_tracer_pid = getpid();
748
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000749 tcp = alloctcb(daemonized_tracer ? getppid() : pid);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000750 if (daemonized_tracer) {
751 /* We want subsequent startup_attach() to attach to it. */
752 tcp->flags |= TCB_ATTACHED;
753 }
Roland McGrath02203312007-06-11 22:06:31 +0000754#ifdef USE_PROCFS
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000755 if (proc_open(tcp, 0) < 0) {
756 fprintf(stderr, "trouble opening proc file\n");
757 cleanup();
758 exit(1);
Roland McGrath02203312007-06-11 22:06:31 +0000759 }
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000760#endif /* USE_PROCFS */
Roland McGrath02203312007-06-11 22:06:31 +0000761}
762
Wang Chaob13c0de2010-11-12 17:25:19 +0800763#ifdef LINUX
764/*
Dmitry V. Levinb1467442010-12-02 20:56:43 +0000765 * Test whether the kernel support PTRACE_O_TRACECLONE et al options.
Wang Chaob13c0de2010-11-12 17:25:19 +0800766 * First fork a new child, call ptrace with PTRACE_SETOPTIONS on it,
Dmitry V. Levinb1467442010-12-02 20:56:43 +0000767 * and then see which options are supported by the kernel.
Wang Chaob13c0de2010-11-12 17:25:19 +0800768 */
769static int
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200770test_ptrace_setoptions_followfork(void)
Wang Chaob13c0de2010-11-12 17:25:19 +0800771{
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +0000772 int pid, expected_grandchild = 0, found_grandchild = 0;
773 const unsigned int test_options = PTRACE_O_TRACECLONE |
774 PTRACE_O_TRACEFORK |
775 PTRACE_O_TRACEVFORK;
Wang Chaob13c0de2010-11-12 17:25:19 +0800776
777 if ((pid = fork()) < 0)
778 return -1;
779 else if (pid == 0) {
Dmitry V. Levinb1467442010-12-02 20:56:43 +0000780 if (ptrace(PTRACE_TRACEME, 0, (char *)1, 0) < 0)
Wang Chaob13c0de2010-11-12 17:25:19 +0800781 _exit(1);
Wang Chaob13c0de2010-11-12 17:25:19 +0800782 kill(getpid(), SIGSTOP);
Dmitry V. Levinb1467442010-12-02 20:56:43 +0000783 _exit(fork() < 0);
Wang Chaob13c0de2010-11-12 17:25:19 +0800784 }
Dmitry V. Levinb1467442010-12-02 20:56:43 +0000785
786 while (1) {
787 int status, tracee_pid;
788
789 tracee_pid = wait(&status);
790 if (tracee_pid == -1) {
791 if (errno == EINTR)
792 continue;
793 else if (errno == ECHILD)
794 break;
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200795 perror("test_ptrace_setoptions_followfork");
Dmitry V. Levinb1467442010-12-02 20:56:43 +0000796 return -1;
797 }
798 if (tracee_pid != pid) {
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +0000799 found_grandchild = tracee_pid;
Dmitry V. Levinb1467442010-12-02 20:56:43 +0000800 if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0 &&
801 errno != ESRCH)
802 kill(tracee_pid, SIGKILL);
803 }
804 else if (WIFSTOPPED(status)) {
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +0000805 switch (WSTOPSIG(status)) {
806 case SIGSTOP:
807 if (ptrace(PTRACE_SETOPTIONS, pid,
808 NULL, test_options) < 0) {
Dmitry V. Levinb1467442010-12-02 20:56:43 +0000809 kill(pid, SIGKILL);
Wang Chaob13c0de2010-11-12 17:25:19 +0800810 return -1;
811 }
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +0000812 break;
813 case SIGTRAP:
814 if (status >> 16 == PTRACE_EVENT_FORK) {
815 long msg = 0;
816
817 if (ptrace(PTRACE_GETEVENTMSG, pid,
818 NULL, (long) &msg) == 0)
819 expected_grandchild = msg;
820 }
821 break;
Wang Chaob13c0de2010-11-12 17:25:19 +0800822 }
Dmitry V. Levinb1467442010-12-02 20:56:43 +0000823 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0 &&
824 errno != ESRCH)
825 kill(pid, SIGKILL);
Wang Chaob13c0de2010-11-12 17:25:19 +0800826 }
827 }
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +0000828 if (expected_grandchild && expected_grandchild == found_grandchild)
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200829 ptrace_setoptions_followfork |= test_options;
Wang Chaob13c0de2010-11-12 17:25:19 +0800830 return 0;
831}
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200832
833/*
834 * Test whether the kernel support PTRACE_O_TRACESYSGOOD.
835 * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it,
836 * and then see whether it will stop with (SIGTRAP | 0x80).
837 *
838 * Use of this option enables correct handling of user-generated SIGTRAPs,
839 * and SIGTRAPs generated by special instructions such as int3 on x86:
840 * _start: .globl _start
841 * int3
842 * movl $42, %ebx
843 * movl $1, %eax
844 * int $0x80
845 * (compile with: "gcc -nostartfiles -nostdlib -o int3 int3.S")
846 */
847static void
848test_ptrace_setoptions_for_all(void)
849{
850 const unsigned int test_options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEEXEC;
851 int pid;
852 int it_worked = 0;
853
854 pid = fork();
855 if (pid < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +0200856 perror_msg_and_die("fork");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200857
858 if (pid == 0) {
859 pid = getpid();
860 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0)
Denys Vlasenko75422762011-05-27 14:36:01 +0200861 /* Note: exits with exitcode 1 */
862 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work", __func__);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200863 kill(pid, SIGSTOP);
864 _exit(0); /* parent should see entry into this syscall */
865 }
866
867 while (1) {
868 int status, tracee_pid;
869
870 errno = 0;
871 tracee_pid = wait(&status);
872 if (tracee_pid <= 0) {
873 if (errno == EINTR)
874 continue;
875 kill(pid, SIGKILL);
Denys Vlasenko75422762011-05-27 14:36:01 +0200876 perror_msg_and_die("%s: unexpected wait result %d", __func__, tracee_pid);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200877 }
Denys Vlasenko75422762011-05-27 14:36:01 +0200878 if (WIFEXITED(status)) {
879 if (WEXITSTATUS(status) == 0)
880 break;
881 /* PTRACE_TRACEME failed in child. This is fatal. */
882 exit(1);
883 }
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200884 if (!WIFSTOPPED(status)) {
885 kill(pid, SIGKILL);
886 error_msg_and_die("%s: unexpected wait status %x", __func__, status);
887 }
888 if (WSTOPSIG(status) == SIGSTOP) {
889 /*
890 * We don't check "options aren't accepted" error.
891 * If it happens, we'll never get (SIGTRAP | 0x80),
892 * and thus will decide to not use the option.
893 * IOW: the outcome of the test will be correct.
894 */
Denys Vlasenko75422762011-05-27 14:36:01 +0200895 if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0)
896 if (errno != EINVAL)
897 perror_msg("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200898 }
899 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
900 it_worked = 1;
901 }
902 if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) {
903 kill(pid, SIGKILL);
Denys Vlasenko75422762011-05-27 14:36:01 +0200904 perror_msg_and_die("PTRACE_SYSCALL doesn't work");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200905 }
906 }
907
908 if (it_worked) {
Denys Vlasenko75422762011-05-27 14:36:01 +0200909 syscall_trap_sig = (SIGTRAP | 0x80);
Denys Vlasenko3454e4b2011-05-23 21:29:03 +0200910 ptrace_setoptions_for_all = test_options;
911 if (debug)
912 fprintf(stderr, "ptrace_setoptions_for_all = %#x\n",
913 ptrace_setoptions_for_all);
914 return;
915 }
916
917 fprintf(stderr,
918 "Test for PTRACE_O_TRACESYSGOOD failed, giving up using this feature.\n");
919}
Wang Chaob13c0de2010-11-12 17:25:19 +0800920#endif
921
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000922int
Dmitry V. Levin08b623e2007-10-08 21:04:41 +0000923main(int argc, char *argv[])
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000924{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000925 struct tcb *tcp;
926 int c, pid = 0;
Dmitry V. Levin06350db2008-07-25 15:42:34 +0000927 int optF = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000928 struct sigaction sa;
929
930 static char buf[BUFSIZ];
931
Dmitry V. Levin08b623e2007-10-08 21:04:41 +0000932 progname = argv[0] ? argv[0] : "strace";
933
Denys Vlasenko75422762011-05-27 14:36:01 +0200934 strace_tracer_pid = getpid();
935
Roland McGrathee9d4352002-12-18 04:16:10 +0000936 /* Allocate the initial tcbtab. */
937 tcbtabsize = argc; /* Surely enough for all -p args. */
Denys Vlasenko418d66a2009-01-17 01:52:54 +0000938 if ((tcbtab = calloc(tcbtabsize, sizeof tcbtab[0])) == NULL) {
Dmitry V. Levin08b623e2007-10-08 21:04:41 +0000939 fprintf(stderr, "%s: out of memory\n", progname);
940 exit(1);
941 }
Denys Vlasenko418d66a2009-01-17 01:52:54 +0000942 if ((tcbtab[0] = calloc(tcbtabsize, sizeof tcbtab[0][0])) == NULL) {
Dmitry V. Levin08b623e2007-10-08 21:04:41 +0000943 fprintf(stderr, "%s: out of memory\n", progname);
944 exit(1);
945 }
Roland McGrathee9d4352002-12-18 04:16:10 +0000946 for (tcp = tcbtab[0]; tcp < &tcbtab[0][tcbtabsize]; ++tcp)
947 tcbtab[tcp - tcbtab[0]] = &tcbtab[0][tcp - tcbtab[0]];
948
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000949 outf = stderr;
950 interactive = 1;
Roland McGrath138c6a32006-01-12 09:50:49 +0000951 set_sortby(DEFAULT_SORTBY);
952 set_personality(DEFAULT_PERSONALITY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000953 qualify("trace=all");
954 qualify("abbrev=all");
955 qualify("verbose=all");
956 qualify("signal=all");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000957 while ((c = getopt(argc, argv,
Grant Edwards8a082772011-04-07 20:25:40 +0000958 "+cCdfFhiqrtTvVxyz"
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000959#ifndef USE_PROCFS
960 "D"
961#endif
Grant Edwards8a082772011-04-07 20:25:40 +0000962 "a:e:o:O:p:s:S:u:E:P:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000963 switch (c) {
964 case 'c':
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +0000965 if (cflag == CFLAG_BOTH) {
966 fprintf(stderr, "%s: -c and -C are mutually exclusive options\n",
967 progname);
968 exit(1);
969 }
970 cflag = CFLAG_ONLY_STATS;
971 break;
972 case 'C':
973 if (cflag == CFLAG_ONLY_STATS) {
974 fprintf(stderr, "%s: -c and -C are mutually exclusive options\n",
975 progname);
976 exit(1);
977 }
978 cflag = CFLAG_BOTH;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000979 break;
980 case 'd':
981 debug++;
982 break;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000983#ifndef USE_PROCFS
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000984 case 'D':
985 daemonized_tracer = 1;
986 break;
987#endif
Roland McGrath41c48222008-07-18 00:25:10 +0000988 case 'F':
Dmitry V. Levin06350db2008-07-25 15:42:34 +0000989 optF = 1;
990 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000991 case 'f':
992 followfork++;
993 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000994 case 'h':
995 usage(stdout, 0);
996 break;
997 case 'i':
998 iflag++;
999 break;
1000 case 'q':
1001 qflag++;
1002 break;
1003 case 'r':
1004 rflag++;
1005 tflag++;
1006 break;
1007 case 't':
1008 tflag++;
1009 break;
1010 case 'T':
1011 dtime++;
1012 break;
1013 case 'x':
1014 xflag++;
1015 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001016 case 'y':
1017 show_fd_path = 1;
1018 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001019 case 'v':
1020 qualify("abbrev=none");
1021 break;
1022 case 'V':
Roland McGrath9c9a2532003-02-20 02:56:29 +00001023 printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001024 exit(0);
1025 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +00001026 case 'z':
1027 not_failing_only = 1;
1028 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001029 case 'a':
1030 acolumn = atoi(optarg);
1031 break;
1032 case 'e':
1033 qualify(optarg);
1034 break;
1035 case 'o':
1036 outfname = strdup(optarg);
1037 break;
1038 case 'O':
1039 set_overhead(atoi(optarg));
1040 break;
1041 case 'p':
Roland McGrathde6e5332003-01-24 04:31:23 +00001042 if ((pid = atoi(optarg)) <= 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001043 fprintf(stderr, "%s: Invalid process id: %s\n",
1044 progname, optarg);
1045 break;
1046 }
Denys Vlasenko75422762011-05-27 14:36:01 +02001047 if (pid == strace_tracer_pid) {
Wichert Akkerman54a47671999-10-17 00:57:34 +00001048 fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001049 break;
1050 }
Denys Vlasenko418d66a2009-01-17 01:52:54 +00001051 tcp = alloc_tcb(pid, 0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001052 tcp->flags |= TCB_ATTACHED;
1053 pflag_seen++;
1054 break;
Grant Edwards8a082772011-04-07 20:25:40 +00001055 case 'P':
1056 tracing_paths = 1;
1057 if (pathtrace_select(optarg)) {
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001058 fprintf(stderr, "%s : failed to select path '%s'\n", progname, optarg);
Grant Edwards8a082772011-04-07 20:25:40 +00001059 exit(1);
1060 }
1061 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001062 case 's':
1063 max_strlen = atoi(optarg);
Roland McGrathdccec722005-05-09 07:45:47 +00001064 if (max_strlen < 0) {
1065 fprintf(stderr,
1066 "%s: invalid -s argument: %s\n",
1067 progname, optarg);
1068 exit(1);
1069 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001070 break;
1071 case 'S':
1072 set_sortby(optarg);
1073 break;
1074 case 'u':
1075 username = strdup(optarg);
1076 break;
Roland McGrathde6e5332003-01-24 04:31:23 +00001077 case 'E':
1078 if (putenv(optarg) < 0) {
1079 fprintf(stderr, "%s: out of memory\n",
1080 progname);
1081 exit(1);
1082 }
1083 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001084 default:
1085 usage(stderr, 1);
1086 break;
1087 }
1088 }
1089
Roland McGrathd0c4c0c2006-04-25 07:39:40 +00001090 if ((optind == argc) == !pflag_seen)
Roland McGrathce0d1542003-11-11 21:24:23 +00001091 usage(stderr, 1);
1092
Wang Chaod322a4b2010-08-05 14:30:11 +08001093 if (pflag_seen && daemonized_tracer) {
1094 fprintf(stderr,
1095 "%s: -D and -p are mutually exclusive options\n",
1096 progname);
1097 exit(1);
1098 }
1099
Dmitry V. Levin06350db2008-07-25 15:42:34 +00001100 if (!followfork)
1101 followfork = optF;
1102
Roland McGrathcb9def62006-04-25 07:48:03 +00001103 if (followfork > 1 && cflag) {
1104 fprintf(stderr,
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00001105 "%s: (-c or -C) and -ff are mutually exclusive options\n",
Roland McGrathcb9def62006-04-25 07:48:03 +00001106 progname);
1107 exit(1);
1108 }
1109
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001110 /* See if they want to run as another user. */
1111 if (username != NULL) {
1112 struct passwd *pent;
1113
1114 if (getuid() != 0 || geteuid() != 0) {
1115 fprintf(stderr,
1116 "%s: you must be root to use the -u option\n",
1117 progname);
1118 exit(1);
1119 }
1120 if ((pent = getpwnam(username)) == NULL) {
1121 fprintf(stderr, "%s: cannot find user `%s'\n",
Roland McGrath09553f82007-07-05 19:31:49 +00001122 progname, username);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001123 exit(1);
1124 }
1125 run_uid = pent->pw_uid;
1126 run_gid = pent->pw_gid;
1127 }
1128 else {
1129 run_uid = getuid();
1130 run_gid = getgid();
1131 }
1132
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001133#ifdef LINUX
1134 if (followfork) {
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001135 if (test_ptrace_setoptions_followfork() < 0) {
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001136 fprintf(stderr,
1137 "Test for options supported by PTRACE_SETOPTIONS "
1138 "failed, giving up using this feature.\n");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001139 ptrace_setoptions_followfork = 0;
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001140 }
1141 if (debug)
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001142 fprintf(stderr, "ptrace_setoptions_followfork = %#x\n",
1143 ptrace_setoptions_followfork);
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001144 }
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02001145 test_ptrace_setoptions_for_all();
Dmitry V. Levin8044bc12010-12-07 12:50:49 +00001146#endif
1147
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001148 /* Check if they want to redirect the output. */
1149 if (outfname) {
Roland McGrath37b9a662003-11-07 02:26:54 +00001150 /* See if they want to pipe the output. */
1151 if (outfname[0] == '|' || outfname[0] == '!') {
1152 /*
1153 * We can't do the <outfname>.PID funny business
1154 * when using popen, so prohibit it.
1155 */
1156 if (followfork > 1) {
1157 fprintf(stderr, "\
1158%s: piping the output and -ff are mutually exclusive options\n",
1159 progname);
1160 exit(1);
1161 }
1162
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001163 if ((outf = strace_popen(outfname + 1)) == NULL)
Roland McGrath37b9a662003-11-07 02:26:54 +00001164 exit(1);
Roland McGrath37b9a662003-11-07 02:26:54 +00001165 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001166 else if (followfork <= 1 &&
1167 (outf = strace_fopen(outfname, "w")) == NULL)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001168 exit(1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001169 }
1170
Roland McGrath37b9a662003-11-07 02:26:54 +00001171 if (!outfname || outfname[0] == '|' || outfname[0] == '!')
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001172 setvbuf(outf, buf, _IOLBF, BUFSIZ);
Roland McGrath37b9a662003-11-07 02:26:54 +00001173 if (outfname && optind < argc) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001174 interactive = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001175 qflag = 1;
Roland McGrath36931052003-06-03 01:35:20 +00001176 }
Wang Chaob13c0de2010-11-12 17:25:19 +08001177
Roland McGrath54cc1c82007-11-03 23:34:11 +00001178 /* Valid states here:
1179 optind < argc pflag_seen outfname interactive
1180 1 0 0 1
1181 0 1 0 1
1182 1 0 1 0
1183 0 1 1 1
1184 */
1185
1186 /* STARTUP_CHILD must be called before the signal handlers get
1187 installed below as they are inherited into the spawned process.
1188 Also we do not need to be protected by them as during interruption
1189 in the STARTUP_CHILD mode we kill the spawned process anyway. */
1190 if (!pflag_seen)
1191 startup_child(&argv[optind]);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001192
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001193 sigemptyset(&empty_set);
1194 sigemptyset(&blocked_set);
1195 sa.sa_handler = SIG_IGN;
1196 sigemptyset(&sa.sa_mask);
1197 sa.sa_flags = 0;
1198 sigaction(SIGTTOU, &sa, NULL);
1199 sigaction(SIGTTIN, &sa, NULL);
1200 if (interactive) {
1201 sigaddset(&blocked_set, SIGHUP);
1202 sigaddset(&blocked_set, SIGINT);
1203 sigaddset(&blocked_set, SIGQUIT);
1204 sigaddset(&blocked_set, SIGPIPE);
1205 sigaddset(&blocked_set, SIGTERM);
1206 sa.sa_handler = interrupt;
1207#ifdef SUNOS4
1208 /* POSIX signals on sunos4.1 are a little broken. */
1209 sa.sa_flags = SA_INTERRUPT;
1210#endif /* SUNOS4 */
1211 }
1212 sigaction(SIGHUP, &sa, NULL);
1213 sigaction(SIGINT, &sa, NULL);
1214 sigaction(SIGQUIT, &sa, NULL);
1215 sigaction(SIGPIPE, &sa, NULL);
1216 sigaction(SIGTERM, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001217#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001218 sa.sa_handler = reaper;
1219 sigaction(SIGCHLD, &sa, NULL);
Roland McGrath553a6092002-12-16 20:40:39 +00001220#else
1221 /* Make sure SIGCHLD has the default action so that waitpid
1222 definitely works without losing track of children. The user
1223 should not have given us a bogus state to inherit, but he might
1224 have. Arguably we should detect SIG_IGN here and pass it on
1225 to children, but probably noone really needs that. */
1226 sa.sa_handler = SIG_DFL;
1227 sigaction(SIGCHLD, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001228#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001229
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +00001230 if (pflag_seen || daemonized_tracer)
Roland McGrath02203312007-06-11 22:06:31 +00001231 startup_attach();
Roland McGrath02203312007-06-11 22:06:31 +00001232
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001233 if (trace() < 0)
1234 exit(1);
1235 cleanup();
Dmitry V. Levina6809652008-11-10 17:14:58 +00001236 fflush(NULL);
1237 if (exit_code > 0xff) {
1238 /* Child was killed by a signal, mimic that. */
1239 exit_code &= 0xff;
1240 signal(exit_code, SIG_DFL);
1241 raise(exit_code);
1242 /* Paranoia - what if this signal is not fatal?
1243 Exit with 128 + signo then. */
1244 exit_code += 128;
1245 }
1246 exit(exit_code);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001247}
1248
Denys Vlasenko418d66a2009-01-17 01:52:54 +00001249void
1250expand_tcbtab(void)
Roland McGrath7b54a7a2004-06-04 01:50:45 +00001251{
1252 /* Allocate some more TCBs and expand the table.
1253 We don't want to relocate the TCBs because our
1254 callers have pointers and it would be a pain.
1255 So tcbtab is a table of pointers. Since we never
1256 free the TCBs, we allocate a single chunk of many. */
1257 struct tcb **newtab = (struct tcb **)
1258 realloc(tcbtab, 2 * tcbtabsize * sizeof tcbtab[0]);
1259 struct tcb *newtcbs = (struct tcb *) calloc(tcbtabsize,
1260 sizeof *newtcbs);
1261 int i;
1262 if (newtab == NULL || newtcbs == NULL) {
Dmitry V. Levin76860f62006-10-11 22:55:25 +00001263 fprintf(stderr, "%s: expand_tcbtab: out of memory\n",
1264 progname);
Denys Vlasenko418d66a2009-01-17 01:52:54 +00001265 cleanup();
1266 exit(1);
Roland McGrath7b54a7a2004-06-04 01:50:45 +00001267 }
1268 for (i = tcbtabsize; i < 2 * tcbtabsize; ++i)
1269 newtab[i] = &newtcbs[i - tcbtabsize];
1270 tcbtabsize *= 2;
1271 tcbtab = newtab;
Roland McGrath7b54a7a2004-06-04 01:50:45 +00001272}
1273
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001274struct tcb *
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001275alloc_tcb(int pid, int command_options_parsed)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001276{
1277 int i;
1278 struct tcb *tcp;
1279
Denys Vlasenko418d66a2009-01-17 01:52:54 +00001280 if (nprocs == tcbtabsize)
1281 expand_tcbtab();
1282
Roland McGrathee9d4352002-12-18 04:16:10 +00001283 for (i = 0; i < tcbtabsize; i++) {
1284 tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001285 if ((tcp->flags & TCB_INUSE) == 0) {
1286 tcp->pid = pid;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001287 tcp->parent = NULL;
1288 tcp->nchildren = 0;
1289 tcp->nzombies = 0;
1290#ifdef TCB_CLONE_THREAD
Wang Chao21b8db42010-08-27 17:43:16 +08001291 tcp->nclone_threads = 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001292#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001293 tcp->flags = TCB_INUSE | TCB_STARTUP;
1294 tcp->outf = outf; /* Initialise to current out file */
Andreas Schwabccdff482009-10-27 16:27:13 +01001295 tcp->curcol = 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001296 tcp->stime.tv_sec = 0;
1297 tcp->stime.tv_usec = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001298 tcp->pfd = -1;
1299 nprocs++;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001300 if (command_options_parsed)
1301 newoutf(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001302 return tcp;
1303 }
1304 }
Denys Vlasenko418d66a2009-01-17 01:52:54 +00001305 fprintf(stderr, "%s: bug in alloc_tcb\n", progname);
1306 cleanup();
1307 exit(1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001308}
1309
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001310#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001311int
Denys Vlasenko418d66a2009-01-17 01:52:54 +00001312proc_open(struct tcb *tcp, int attaching)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001313{
1314 char proc[32];
1315 long arg;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001316#ifdef SVR4
John Hughes19e49982001-10-19 08:59:12 +00001317 int i;
1318 sysset_t syscalls;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001319 sigset_t signals;
1320 fltset_t faults;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001321#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001322#ifndef HAVE_POLLABLE_PROCFS
1323 static int last_pfd;
1324#endif
1325
Wichert Akkermanea78f0f1999-11-29 15:34:02 +00001326#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001327 /* Open the process pseudo-files in /proc. */
1328 sprintf(proc, "/proc/%d/ctl", tcp->pid);
1329 if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001330 perror("strace: open(\"/proc/...\", ...)");
1331 return -1;
1332 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001333 if (set_cloexec_flag(tcp->pfd) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001334 return -1;
1335 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001336 sprintf(proc, "/proc/%d/status", tcp->pid);
1337 if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
1338 perror("strace: open(\"/proc/...\", ...)");
1339 return -1;
1340 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001341 if (set_cloexec_flag(tcp->pfd_stat) < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001342 return -1;
1343 }
1344 sprintf(proc, "/proc/%d/as", tcp->pid);
1345 if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
1346 perror("strace: open(\"/proc/...\", ...)");
1347 return -1;
1348 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001349 if (set_cloexec_flag(tcp->pfd_as) < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001350 return -1;
1351 }
1352#else
1353 /* Open the process pseudo-file in /proc. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001354#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001355 sprintf(proc, "/proc/%d", tcp->pid);
Andreas Schwab372cc842010-07-09 11:49:27 +02001356 tcp->pfd = open(proc, O_RDWR|O_EXCL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001357#else /* FREEBSD */
1358 sprintf(proc, "/proc/%d/mem", tcp->pid);
Andreas Schwab372cc842010-07-09 11:49:27 +02001359 tcp->pfd = open(proc, O_RDWR);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001360#endif /* FREEBSD */
Andreas Schwab372cc842010-07-09 11:49:27 +02001361 if (tcp->pfd < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001362 perror("strace: open(\"/proc/...\", ...)");
1363 return -1;
1364 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001365 if (set_cloexec_flag(tcp->pfd) < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001366 return -1;
1367 }
1368#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001369#ifdef FREEBSD
1370 sprintf(proc, "/proc/%d/regs", tcp->pid);
1371 if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) {
1372 perror("strace: open(\"/proc/.../regs\", ...)");
1373 return -1;
1374 }
1375 if (cflag) {
1376 sprintf(proc, "/proc/%d/status", tcp->pid);
1377 if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) {
1378 perror("strace: open(\"/proc/.../status\", ...)");
1379 return -1;
1380 }
1381 } else
1382 tcp->pfd_status = -1;
1383#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001384 rebuild_pollv();
1385 if (!attaching) {
1386 /*
1387 * Wait for the child to pause. Because of a race
1388 * condition we have to poll for the event.
1389 */
1390 for (;;) {
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001391 if (IOCTL_STATUS(tcp) < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001392 perror("strace: PIOCSTATUS");
1393 return -1;
1394 }
1395 if (tcp->status.PR_FLAGS & PR_ASLEEP)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001396 break;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001397 }
1398 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001399#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001400 /* Stop the process so that we own the stop. */
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001401 if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001402 perror("strace: PIOCSTOP");
1403 return -1;
1404 }
Roland McGrath553a6092002-12-16 20:40:39 +00001405#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001406#ifdef PIOCSET
1407 /* Set Run-on-Last-Close. */
1408 arg = PR_RLC;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001409 if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001410 perror("PIOCSET PR_RLC");
1411 return -1;
1412 }
1413 /* Set or Reset Inherit-on-Fork. */
1414 arg = PR_FORK;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001415 if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001416 perror("PIOC{SET,RESET} PR_FORK");
1417 return -1;
1418 }
1419#else /* !PIOCSET */
Roland McGrath553a6092002-12-16 20:40:39 +00001420#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001421 if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
1422 perror("PIOCSRLC");
1423 return -1;
1424 }
1425 if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
1426 perror("PIOC{S,R}FORK");
1427 return -1;
1428 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001429#else /* FREEBSD */
1430 /* just unset the PF_LINGER flag for the Run-on-Last-Close. */
1431 if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) {
1432 perror("PIOCGFL");
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001433 return -1;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001434 }
1435 arg &= ~PF_LINGER;
1436 if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001437 perror("PIOCSFL");
1438 return -1;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001439 }
1440#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001441#endif /* !PIOCSET */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001442#ifndef FREEBSD
John Hughes19e49982001-10-19 08:59:12 +00001443 /* Enable all syscall entries we care about. */
1444 premptyset(&syscalls);
1445 for (i = 1; i < MAX_QUALS; ++i) {
1446 if (i > (sizeof syscalls) * CHAR_BIT) break;
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001447 if (qual_flags[i] & QUAL_TRACE) praddset(&syscalls, i);
John Hughes19e49982001-10-19 08:59:12 +00001448 }
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001449 praddset(&syscalls, SYS_execve);
John Hughes19e49982001-10-19 08:59:12 +00001450 if (followfork) {
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001451 praddset(&syscalls, SYS_fork);
John Hughes19e49982001-10-19 08:59:12 +00001452#ifdef SYS_forkall
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001453 praddset(&syscalls, SYS_forkall);
John Hughes19e49982001-10-19 08:59:12 +00001454#endif
Roland McGrath553a6092002-12-16 20:40:39 +00001455#ifdef SYS_fork1
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001456 praddset(&syscalls, SYS_fork1);
John Hughes19e49982001-10-19 08:59:12 +00001457#endif
1458#ifdef SYS_rfork1
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001459 praddset(&syscalls, SYS_rfork1);
John Hughes19e49982001-10-19 08:59:12 +00001460#endif
1461#ifdef SYS_rforkall
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001462 praddset(&syscalls, SYS_rforkall);
John Hughes19e49982001-10-19 08:59:12 +00001463#endif
1464 }
1465 if (IOCTL(tcp->pfd, PIOCSENTRY, &syscalls) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001466 perror("PIOCSENTRY");
1467 return -1;
1468 }
John Hughes19e49982001-10-19 08:59:12 +00001469 /* Enable the syscall exits. */
1470 if (IOCTL(tcp->pfd, PIOCSEXIT, &syscalls) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001471 perror("PIOSEXIT");
1472 return -1;
1473 }
John Hughes19e49982001-10-19 08:59:12 +00001474 /* Enable signals we care about. */
1475 premptyset(&signals);
1476 for (i = 1; i < MAX_QUALS; ++i) {
1477 if (i > (sizeof signals) * CHAR_BIT) break;
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001478 if (qual_flags[i] & QUAL_SIGNAL) praddset(&signals, i);
John Hughes19e49982001-10-19 08:59:12 +00001479 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001480 if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001481 perror("PIOCSTRACE");
1482 return -1;
1483 }
John Hughes19e49982001-10-19 08:59:12 +00001484 /* Enable faults we care about */
1485 premptyset(&faults);
1486 for (i = 1; i < MAX_QUALS; ++i) {
1487 if (i > (sizeof faults) * CHAR_BIT) break;
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001488 if (qual_flags[i] & QUAL_FAULT) praddset(&faults, i);
John Hughes19e49982001-10-19 08:59:12 +00001489 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001490 if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001491 perror("PIOCSFAULT");
1492 return -1;
1493 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001494#else /* FREEBSD */
1495 /* set events flags. */
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001496 arg = S_SIG | S_SCE | S_SCX;
1497 if (ioctl(tcp->pfd, PIOCBIS, arg) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001498 perror("PIOCBIS");
1499 return -1;
1500 }
1501#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001502 if (!attaching) {
1503#ifdef MIPS
1504 /*
1505 * The SGI PRSABORT doesn't work for pause() so
1506 * we send it a caught signal to wake it up.
1507 */
1508 kill(tcp->pid, SIGINT);
1509#else /* !MIPS */
Roland McGrath553a6092002-12-16 20:40:39 +00001510#ifdef PRSABORT
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001511 /* The child is in a pause(), abort it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001512 arg = PRSABORT;
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001513 if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001514 perror("PIOCRUN");
1515 return -1;
1516 }
Roland McGrath553a6092002-12-16 20:40:39 +00001517#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001518#endif /* !MIPS*/
1519#ifdef FREEBSD
1520 /* wake up the child if it received the SIGSTOP */
1521 kill(tcp->pid, SIGCONT);
Roland McGrath553a6092002-12-16 20:40:39 +00001522#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001523 for (;;) {
1524 /* Wait for the child to do something. */
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001525 if (IOCTL_WSTOP(tcp) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001526 perror("PIOCWSTOP");
1527 return -1;
1528 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001529 if (tcp->status.PR_WHY == PR_SYSENTRY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001530 tcp->flags &= ~TCB_INSYSCALL;
1531 get_scno(tcp);
Roland McGrath76989d72005-06-07 23:21:31 +00001532 if (known_scno(tcp) == SYS_execve)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001533 break;
1534 }
1535 /* Set it running: maybe execve will be next. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001536#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001537 arg = 0;
1538 if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001539#else /* FREEBSD */
1540 if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) {
Roland McGrath553a6092002-12-16 20:40:39 +00001541#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001542 perror("PIOCRUN");
1543 return -1;
1544 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001545#ifdef FREEBSD
1546 /* handle the case where we "opened" the child before
1547 it did the kill -STOP */
1548 if (tcp->status.PR_WHY == PR_SIGNALLED &&
1549 tcp->status.PR_WHAT == SIGSTOP)
1550 kill(tcp->pid, SIGCONT);
Roland McGrath553a6092002-12-16 20:40:39 +00001551#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001552 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001553#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001554 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001555#else /* FREEBSD */
1556 } else {
Roland McGrath553a6092002-12-16 20:40:39 +00001557 if (attaching < 2) {
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +00001558 /* We are attaching to an already running process.
1559 * Try to figure out the state of the process in syscalls,
1560 * to handle the first event well.
1561 * This is done by having a look at the "wchan" property of the
1562 * process, which tells where it is stopped (if it is). */
1563 FILE * status;
1564 char wchan[20]; /* should be enough */
Roland McGrath553a6092002-12-16 20:40:39 +00001565
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +00001566 sprintf(proc, "/proc/%d/status", tcp->pid);
1567 status = fopen(proc, "r");
1568 if (status &&
1569 (fscanf(status, "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d"
1570 "%*d,%*d %*d,%*d %19s", wchan) == 1) &&
1571 strcmp(wchan, "nochan") && strcmp(wchan, "spread") &&
1572 strcmp(wchan, "stopevent")) {
1573 /* The process is asleep in the middle of a syscall.
1574 Fake the syscall entry event */
1575 tcp->flags &= ~(TCB_INSYSCALL|TCB_STARTUP);
1576 tcp->status.PR_WHY = PR_SYSENTRY;
1577 trace_syscall(tcp);
1578 }
1579 if (status)
1580 fclose(status);
1581 } /* otherwise it's a fork being followed */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001582 }
1583#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001584#ifndef HAVE_POLLABLE_PROCFS
1585 if (proc_poll_pipe[0] != -1)
1586 proc_poller(tcp->pfd);
1587 else if (nprocs > 1) {
1588 proc_poll_open();
1589 proc_poller(last_pfd);
1590 proc_poller(tcp->pfd);
1591 }
1592 last_pfd = tcp->pfd;
1593#endif /* !HAVE_POLLABLE_PROCFS */
1594 return 0;
1595}
1596
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001597#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001598
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001599struct tcb *
Roland McGrath54e931f2010-09-14 18:59:20 -07001600pid2tcb(int pid)
1601{
1602 int i;
1603
1604 if (pid <= 0)
1605 return NULL;
1606
1607 for (i = 0; i < tcbtabsize; i++) {
1608 struct tcb *tcp = tcbtab[i];
1609 if (tcp->pid == pid && (tcp->flags & TCB_INUSE))
1610 return tcp;
1611 }
1612
1613 return NULL;
1614}
1615
1616#ifdef USE_PROCFS
1617
1618static struct tcb *
1619first_used_tcb(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001620{
1621 int i;
1622 struct tcb *tcp;
Roland McGrathee9d4352002-12-18 04:16:10 +00001623 for (i = 0; i < tcbtabsize; i++) {
1624 tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001625 if (tcp->flags & TCB_INUSE)
1626 return tcp;
1627 }
1628 return NULL;
1629}
1630
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001631static struct tcb *
Denys Vlasenko12014262011-05-30 14:00:14 +02001632pfd2tcb(int pfd)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001633{
1634 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001635
Roland McGrathca16be82003-01-10 19:55:28 +00001636 for (i = 0; i < tcbtabsize; i++) {
1637 struct tcb *tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001638 if (tcp->pfd != pfd)
1639 continue;
1640 if (tcp->flags & TCB_INUSE)
1641 return tcp;
1642 }
1643 return NULL;
1644}
1645
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001646#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001647
1648void
Denys Vlasenko12014262011-05-30 14:00:14 +02001649droptcb(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001650{
1651 if (tcp->pid == 0)
1652 return;
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001653#ifdef TCB_CLONE_THREAD
1654 if (tcp->nclone_threads > 0) {
1655 /* There are other threads left in this process, but this
1656 is the one whose PID represents the whole process.
1657 We need to keep this record around as a zombie until
1658 all the threads die. */
1659 tcp->flags |= TCB_EXITING;
1660 return;
1661 }
1662#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001663 nprocs--;
1664 tcp->pid = 0;
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +00001665
Roland McGrathe29341c2003-01-10 20:14:20 +00001666 if (tcp->parent != NULL) {
1667 tcp->parent->nchildren--;
1668#ifdef TCB_CLONE_THREAD
Roland McGrathe29341c2003-01-10 20:14:20 +00001669 if (tcp->flags & TCB_CLONE_THREAD)
1670 tcp->parent->nclone_threads--;
1671#endif
Wang Chao21b8db42010-08-27 17:43:16 +08001672 tcp->parent->nzombies++;
Roland McGrath276ceb32007-11-13 08:12:12 +00001673#ifdef LINUX
1674 /* Update `tcp->parent->parent->nchildren' and the other fields
1675 like NCLONE_DETACHED, only for zombie group leader that has
1676 already reported and been short-circuited at the top of this
1677 function. The same condition as at the top of DETACH. */
1678 if ((tcp->flags & TCB_CLONE_THREAD) &&
1679 tcp->parent->nclone_threads == 0 &&
1680 (tcp->parent->flags & TCB_EXITING))
1681 droptcb(tcp->parent);
1682#endif
Roland McGrathe29341c2003-01-10 20:14:20 +00001683 tcp->parent = NULL;
1684 }
1685
1686 tcp->flags = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001687 if (tcp->pfd != -1) {
1688 close(tcp->pfd);
1689 tcp->pfd = -1;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001690#ifdef FREEBSD
1691 if (tcp->pfd_reg != -1) {
1692 close(tcp->pfd_reg);
1693 tcp->pfd_reg = -1;
1694 }
1695 if (tcp->pfd_status != -1) {
1696 close(tcp->pfd_status);
1697 tcp->pfd_status = -1;
1698 }
Roland McGrath553a6092002-12-16 20:40:39 +00001699#endif /* !FREEBSD */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001700#ifdef USE_PROCFS
Roland McGrathe29341c2003-01-10 20:14:20 +00001701 rebuild_pollv(); /* Note, flags needs to be cleared by now. */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001702#endif
1703 }
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +00001704
Wichert Akkerman822f0c92002-04-03 10:55:14 +00001705 if (outfname && followfork > 1 && tcp->outf)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001706 fclose(tcp->outf);
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +00001707
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001708 tcp->outf = 0;
1709}
1710
Roland McGrath0a463882007-07-05 18:43:16 +00001711/* detach traced process; continue with sig
1712 Never call DETACH twice on the same process as both unattached and
1713 attached-unstopped processes give the same ESRCH. For unattached process we
1714 would SIGSTOP it and wait for its SIGSTOP notification forever. */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001715
1716static int
Denys Vlasenko12014262011-05-30 14:00:14 +02001717detach(struct tcb *tcp, int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001718{
1719 int error = 0;
Roland McGrathca16be82003-01-10 19:55:28 +00001720#ifdef LINUX
Roland McGrath1bfd3102007-08-03 10:02:00 +00001721 int status, catch_sigstop;
Roland McGratha08a97e2005-08-03 11:23:46 +00001722 struct tcb *zombie = NULL;
1723
1724 /* If the group leader is lingering only because of this other
1725 thread now dying, then detach the leader as well. */
1726 if ((tcp->flags & TCB_CLONE_THREAD) &&
1727 tcp->parent->nclone_threads == 1 &&
1728 (tcp->parent->flags & TCB_EXITING))
1729 zombie = tcp->parent;
Roland McGrathca16be82003-01-10 19:55:28 +00001730#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001731
1732 if (tcp->flags & TCB_BPTSET)
Andreas Schwab840d85b2010-01-12 11:16:32 +01001733 clearbpt(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001734
1735#ifdef LINUX
1736 /*
1737 * Linux wrongly insists the child be stopped
Roland McGrath7bf10472002-12-16 20:42:50 +00001738 * before detaching. Arghh. We go through hoops
1739 * to make a clean break of things.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001740 */
Roland McGrath7bf10472002-12-16 20:42:50 +00001741#if defined(SPARC)
1742#undef PTRACE_DETACH
1743#define PTRACE_DETACH PTRACE_SUNDETACH
1744#endif
Roland McGrath02203312007-06-11 22:06:31 +00001745 /*
1746 * On TCB_STARTUP we did PTRACE_ATTACH but still did not get the
1747 * expected SIGSTOP. We must catch exactly one as otherwise the
1748 * detached process would be left stopped (process state T).
1749 */
1750 catch_sigstop = (tcp->flags & TCB_STARTUP);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001751 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
1752 /* On a clear day, you can see forever. */
Roland McGrath7bf10472002-12-16 20:42:50 +00001753 }
1754 else if (errno != ESRCH) {
1755 /* Shouldn't happen. */
1756 perror("detach: ptrace(PTRACE_DETACH, ...)");
1757 }
Roland McGrath134813a2007-06-02 00:07:33 +00001758 else if (my_tgkill((tcp->flags & TCB_CLONE_THREAD ? tcp->parent->pid
1759 : tcp->pid),
1760 tcp->pid, 0) < 0) {
Roland McGrath7bf10472002-12-16 20:42:50 +00001761 if (errno != ESRCH)
1762 perror("detach: checking sanity");
1763 }
Roland McGrath02203312007-06-11 22:06:31 +00001764 else if (!catch_sigstop && my_tgkill((tcp->flags & TCB_CLONE_THREAD
1765 ? tcp->parent->pid : tcp->pid),
1766 tcp->pid, SIGSTOP) < 0) {
Roland McGrath7bf10472002-12-16 20:42:50 +00001767 if (errno != ESRCH)
1768 perror("detach: stopping child");
1769 }
Roland McGrath02203312007-06-11 22:06:31 +00001770 else
1771 catch_sigstop = 1;
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00001772 if (catch_sigstop) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001773 for (;;) {
Roland McGrath7508cb42002-12-17 10:48:05 +00001774#ifdef __WALL
1775 if (wait4(tcp->pid, &status, __WALL, NULL) < 0) {
1776 if (errno == ECHILD) /* Already gone. */
1777 break;
1778 if (errno != EINVAL) {
Roland McGrath553a6092002-12-16 20:40:39 +00001779 perror("detach: waiting");
Roland McGrath7508cb42002-12-17 10:48:05 +00001780 break;
1781 }
1782#endif /* __WALL */
1783 /* No __WALL here. */
1784 if (waitpid(tcp->pid, &status, 0) < 0) {
1785 if (errno != ECHILD) {
1786 perror("detach: waiting");
1787 break;
1788 }
1789#ifdef __WCLONE
1790 /* If no processes, try clones. */
1791 if (wait4(tcp->pid, &status, __WCLONE,
1792 NULL) < 0) {
1793 if (errno != ECHILD)
1794 perror("detach: waiting");
1795 break;
1796 }
1797#endif /* __WCLONE */
1798 }
1799#ifdef __WALL
Roland McGrath553a6092002-12-16 20:40:39 +00001800 }
Roland McGrath7508cb42002-12-17 10:48:05 +00001801#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001802 if (!WIFSTOPPED(status)) {
1803 /* Au revoir, mon ami. */
1804 break;
1805 }
1806 if (WSTOPSIG(status) == SIGSTOP) {
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00001807 ptrace_restart(PTRACE_DETACH, tcp, sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001808 break;
1809 }
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00001810 error = ptrace_restart(PTRACE_CONT, tcp,
Denys Vlasenko75422762011-05-27 14:36:01 +02001811 WSTOPSIG(status) == syscall_trap_sig ? 0
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00001812 : WSTOPSIG(status));
1813 if (error < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001814 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001815 }
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00001816 }
Roland McGrath7bf10472002-12-16 20:42:50 +00001817#endif /* LINUX */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001818
1819#if defined(SUNOS4)
1820 /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
1821 if (sig && kill(tcp->pid, sig) < 0)
1822 perror("detach: kill");
1823 sig = 0;
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00001824 error = ptrace_restart(PTRACE_DETACH, tcp, sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001825#endif /* SUNOS4 */
1826
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001827 if (!qflag)
1828 fprintf(stderr, "Process %u detached\n", tcp->pid);
1829
1830 droptcb(tcp);
Roland McGratha08a97e2005-08-03 11:23:46 +00001831
1832#ifdef LINUX
Roland McGrath0a463882007-07-05 18:43:16 +00001833 if (zombie != NULL) {
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001834 /* TCP no longer exists therefore you must not detach() it. */
Roland McGrath0a463882007-07-05 18:43:16 +00001835 droptcb(zombie);
1836 }
Roland McGratha08a97e2005-08-03 11:23:46 +00001837#endif
1838
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001839 return error;
1840}
1841
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001842#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001843
Dmitry V. Levine5e60852009-12-31 22:50:49 +00001844static void reaper(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001845{
1846 int pid;
1847 int status;
1848
1849 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001850 }
1851}
1852
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001853#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001854
1855static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001856cleanup(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001857{
1858 int i;
1859 struct tcb *tcp;
1860
Roland McGrathee9d4352002-12-18 04:16:10 +00001861 for (i = 0; i < tcbtabsize; i++) {
1862 tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001863 if (!(tcp->flags & TCB_INUSE))
1864 continue;
1865 if (debug)
1866 fprintf(stderr,
1867 "cleanup: looking at pid %u\n", tcp->pid);
1868 if (tcp_last &&
1869 (!outfname || followfork < 2 || tcp_last == tcp)) {
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00001870 tprintf(" <unfinished ...>");
1871 printtrailer();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001872 }
1873 if (tcp->flags & TCB_ATTACHED)
1874 detach(tcp, 0);
1875 else {
1876 kill(tcp->pid, SIGCONT);
1877 kill(tcp->pid, SIGTERM);
1878 }
1879 }
1880 if (cflag)
1881 call_summary(outf);
1882}
1883
1884static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001885interrupt(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001886{
1887 interrupted = 1;
1888}
1889
1890#ifndef HAVE_STRERROR
1891
Roland McGrath6d2b3492002-12-30 00:51:30 +00001892#if !HAVE_DECL_SYS_ERRLIST
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001893extern int sys_nerr;
1894extern char *sys_errlist[];
Roland McGrath6d2b3492002-12-30 00:51:30 +00001895#endif /* HAVE_DECL_SYS_ERRLIST */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001896
1897const char *
Denys Vlasenko12014262011-05-30 14:00:14 +02001898strerror(int err_no)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001899{
1900 static char buf[64];
1901
Denys Vlasenko35aba6a2011-05-25 15:33:26 +02001902 if (err_no < 1 || err_no >= sys_nerr) {
1903 sprintf(buf, "Unknown error %d", err_no);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001904 return buf;
1905 }
Denys Vlasenko35aba6a2011-05-25 15:33:26 +02001906 return sys_errlist[err_no];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001907}
1908
1909#endif /* HAVE_STERRROR */
1910
1911#ifndef HAVE_STRSIGNAL
1912
Roland McGrath8f474e02003-01-14 07:53:33 +00001913#if defined HAVE_SYS_SIGLIST && !defined HAVE_DECL_SYS_SIGLIST
Roland McGrath6d2b3492002-12-30 00:51:30 +00001914extern char *sys_siglist[];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001915#endif
Roland McGrath8f474e02003-01-14 07:53:33 +00001916#if defined HAVE_SYS__SIGLIST && !defined HAVE_DECL__SYS_SIGLIST
1917extern char *_sys_siglist[];
1918#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001919
1920const char *
Denys Vlasenko12014262011-05-30 14:00:14 +02001921strsignal(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001922{
1923 static char buf[64];
1924
1925 if (sig < 1 || sig >= NSIG) {
1926 sprintf(buf, "Unknown signal %d", sig);
1927 return buf;
1928 }
1929#ifdef HAVE__SYS_SIGLIST
1930 return _sys_siglist[sig];
1931#else
1932 return sys_siglist[sig];
1933#endif
1934}
1935
1936#endif /* HAVE_STRSIGNAL */
1937
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001938#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001939
1940static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001941rebuild_pollv(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001942{
1943 int i, j;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001944
Roland McGrathee9d4352002-12-18 04:16:10 +00001945 if (pollv != NULL)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001946 free(pollv);
Roland McGrathc012d222003-01-10 20:05:56 +00001947 pollv = (struct pollfd *) malloc(nprocs * sizeof pollv[0]);
Roland McGrathee9d4352002-12-18 04:16:10 +00001948 if (pollv == NULL) {
Roland McGrath46100d02005-06-01 18:55:42 +00001949 fprintf(stderr, "%s: out of memory\n", progname);
Roland McGrathee9d4352002-12-18 04:16:10 +00001950 exit(1);
1951 }
1952
Roland McGrathca16be82003-01-10 19:55:28 +00001953 for (i = j = 0; i < tcbtabsize; i++) {
1954 struct tcb *tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001955 if (!(tcp->flags & TCB_INUSE))
1956 continue;
1957 pollv[j].fd = tcp->pfd;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001958 pollv[j].events = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001959 j++;
1960 }
1961 if (j != nprocs) {
1962 fprintf(stderr, "strace: proc miscount\n");
1963 exit(1);
1964 }
1965}
1966
1967#ifndef HAVE_POLLABLE_PROCFS
1968
1969static void
Denys Vlasenko12014262011-05-30 14:00:14 +02001970proc_poll_open(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001971{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001972 int i;
1973
1974 if (pipe(proc_poll_pipe) < 0) {
1975 perror("pipe");
1976 exit(1);
1977 }
1978 for (i = 0; i < 2; i++) {
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001979 if (set_cloexec_flag(proc_poll_pipe[i]) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001980 exit(1);
1981 }
1982 }
1983}
1984
1985static int
Denys Vlasenko12014262011-05-30 14:00:14 +02001986proc_poll(struct pollfd *pollv, int nfds, int timeout)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001987{
1988 int i;
1989 int n;
1990 struct proc_pollfd pollinfo;
1991
1992 if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
1993 return n;
1994 if (n != sizeof(struct proc_pollfd)) {
1995 fprintf(stderr, "panic: short read: %d\n", n);
1996 exit(1);
1997 }
1998 for (i = 0; i < nprocs; i++) {
1999 if (pollv[i].fd == pollinfo.fd)
2000 pollv[i].revents = pollinfo.revents;
2001 else
2002 pollv[i].revents = 0;
2003 }
2004 poller_pid = pollinfo.pid;
2005 return 1;
2006}
2007
2008static void
Denys Vlasenko12014262011-05-30 14:00:14 +02002009wakeup_handler(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002010{
2011}
2012
2013static void
Denys Vlasenko12014262011-05-30 14:00:14 +02002014proc_poller(int pfd)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002015{
2016 struct proc_pollfd pollinfo;
2017 struct sigaction sa;
2018 sigset_t blocked_set, empty_set;
2019 int i;
2020 int n;
2021 struct rlimit rl;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002022#ifdef FREEBSD
2023 struct procfs_status pfs;
2024#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002025
2026 switch (fork()) {
2027 case -1:
2028 perror("fork");
Dmitry V. Levina6809652008-11-10 17:14:58 +00002029 _exit(1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002030 case 0:
2031 break;
2032 default:
2033 return;
2034 }
2035
2036 sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
2037 sa.sa_flags = 0;
2038 sigemptyset(&sa.sa_mask);
2039 sigaction(SIGHUP, &sa, NULL);
2040 sigaction(SIGINT, &sa, NULL);
2041 sigaction(SIGQUIT, &sa, NULL);
2042 sigaction(SIGPIPE, &sa, NULL);
2043 sigaction(SIGTERM, &sa, NULL);
2044 sa.sa_handler = wakeup_handler;
2045 sigaction(SIGUSR1, &sa, NULL);
2046 sigemptyset(&blocked_set);
2047 sigaddset(&blocked_set, SIGUSR1);
2048 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
2049 sigemptyset(&empty_set);
2050
2051 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
2052 perror("getrlimit(RLIMIT_NOFILE, ...)");
Dmitry V. Levina6809652008-11-10 17:14:58 +00002053 _exit(1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002054 }
2055 n = rl.rlim_cur;
2056 for (i = 0; i < n; i++) {
2057 if (i != pfd && i != proc_poll_pipe[1])
2058 close(i);
2059 }
2060
2061 pollinfo.fd = pfd;
2062 pollinfo.pid = getpid();
2063 for (;;) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002064#ifndef FREEBSD
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00002065 if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
2066#else
2067 if (ioctl(pfd, PIOCWSTOP, &pfs) < 0)
2068#endif
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002069 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002070 switch (errno) {
2071 case EINTR:
2072 continue;
2073 case EBADF:
2074 pollinfo.revents = POLLERR;
2075 break;
2076 case ENOENT:
2077 pollinfo.revents = POLLHUP;
2078 break;
2079 default:
2080 perror("proc_poller: PIOCWSTOP");
2081 }
2082 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
2083 _exit(0);
2084 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002085 pollinfo.revents = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002086 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
2087 sigsuspend(&empty_set);
2088 }
2089}
2090
2091#endif /* !HAVE_POLLABLE_PROCFS */
2092
2093static int
2094choose_pfd()
2095{
2096 int i, j;
2097 struct tcb *tcp;
2098
2099 static int last;
2100
2101 if (followfork < 2 &&
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002102 last < nprocs && (pollv[last].revents & POLLWANT)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002103 /*
2104 * The previous process is ready to run again. We'll
2105 * let it do so if it is currently in a syscall. This
2106 * heuristic improves the readability of the trace.
2107 */
2108 tcp = pfd2tcb(pollv[last].fd);
2109 if (tcp && (tcp->flags & TCB_INSYSCALL))
2110 return pollv[last].fd;
2111 }
2112
2113 for (i = 0; i < nprocs; i++) {
2114 /* Let competing children run round robin. */
2115 j = (i + last + 1) % nprocs;
2116 if (pollv[j].revents & (POLLHUP | POLLERR)) {
2117 tcp = pfd2tcb(pollv[j].fd);
2118 if (!tcp) {
2119 fprintf(stderr, "strace: lost proc\n");
2120 exit(1);
2121 }
2122 droptcb(tcp);
2123 return -1;
2124 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002125 if (pollv[j].revents & POLLWANT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002126 last = j;
2127 return pollv[j].fd;
2128 }
2129 }
2130 fprintf(stderr, "strace: nothing ready\n");
2131 exit(1);
2132}
2133
2134static int
Denys Vlasenko12014262011-05-30 14:00:14 +02002135trace(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002136{
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00002137#ifdef POLL_HACK
John Hughesd870b3c2002-05-21 11:24:18 +00002138 struct tcb *in_syscall = NULL;
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00002139#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002140 struct tcb *tcp;
2141 int pfd;
2142 int what;
2143 int ioctl_result = 0, ioctl_errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002144 long arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002145
2146 for (;;) {
2147 if (interactive)
2148 sigprocmask(SIG_SETMASK, &empty_set, NULL);
2149
2150 if (nprocs == 0)
2151 break;
2152
2153 switch (nprocs) {
2154 case 1:
2155#ifndef HAVE_POLLABLE_PROCFS
2156 if (proc_poll_pipe[0] == -1) {
2157#endif
Roland McGrath54e931f2010-09-14 18:59:20 -07002158 tcp = first_used_tcb();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002159 if (!tcp)
2160 continue;
2161 pfd = tcp->pfd;
2162 if (pfd == -1)
2163 continue;
2164 break;
2165#ifndef HAVE_POLLABLE_PROCFS
2166 }
2167 /* fall through ... */
2168#endif /* !HAVE_POLLABLE_PROCFS */
2169 default:
2170#ifdef HAVE_POLLABLE_PROCFS
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00002171#ifdef POLL_HACK
2172 /* On some systems (e.g. UnixWare) we get too much ugly
2173 "unfinished..." stuff when multiple proceses are in
2174 syscalls. Here's a nasty hack */
Roland McGrath553a6092002-12-16 20:40:39 +00002175
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00002176 if (in_syscall) {
2177 struct pollfd pv;
2178 tcp = in_syscall;
2179 in_syscall = NULL;
2180 pv.fd = tcp->pfd;
2181 pv.events = POLLWANT;
Denys Vlasenkob63256e2011-06-07 12:13:24 +02002182 if ((what = poll(&pv, 1, 1)) < 0) {
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00002183 if (interrupted)
2184 return 0;
2185 continue;
2186 }
2187 else if (what == 1 && pv.revents & POLLWANT) {
2188 goto FOUND;
2189 }
2190 }
2191#endif
2192
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002193 if (poll(pollv, nprocs, INFTIM) < 0) {
2194 if (interrupted)
2195 return 0;
2196 continue;
2197 }
2198#else /* !HAVE_POLLABLE_PROCFS */
2199 if (proc_poll(pollv, nprocs, INFTIM) < 0) {
2200 if (interrupted)
2201 return 0;
2202 continue;
2203 }
2204#endif /* !HAVE_POLLABLE_PROCFS */
2205 pfd = choose_pfd();
2206 if (pfd == -1)
2207 continue;
2208 break;
2209 }
2210
2211 /* Look up `pfd' in our table. */
2212 if ((tcp = pfd2tcb(pfd)) == NULL) {
2213 fprintf(stderr, "unknown pfd: %u\n", pfd);
2214 exit(1);
2215 }
John Hughesb6643082002-05-23 11:02:22 +00002216#ifdef POLL_HACK
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00002217 FOUND:
John Hughesb6643082002-05-23 11:02:22 +00002218#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002219 /* Get the status of the process. */
2220 if (!interrupted) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002221#ifndef FREEBSD
Denys Vlasenkob63256e2011-06-07 12:13:24 +02002222 ioctl_result = IOCTL_WSTOP(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002223#else /* FREEBSD */
2224 /* Thanks to some scheduling mystery, the first poller
2225 sometimes waits for the already processed end of fork
2226 event. Doing a non blocking poll here solves the problem. */
2227 if (proc_poll_pipe[0] != -1)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02002228 ioctl_result = IOCTL_STATUS(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002229 else
Denys Vlasenkob63256e2011-06-07 12:13:24 +02002230 ioctl_result = IOCTL_WSTOP(tcp);
Roland McGrath553a6092002-12-16 20:40:39 +00002231#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002232 ioctl_errno = errno;
2233#ifndef HAVE_POLLABLE_PROCFS
2234 if (proc_poll_pipe[0] != -1) {
2235 if (ioctl_result < 0)
2236 kill(poller_pid, SIGKILL);
2237 else
2238 kill(poller_pid, SIGUSR1);
2239 }
2240#endif /* !HAVE_POLLABLE_PROCFS */
2241 }
2242 if (interrupted)
2243 return 0;
2244
2245 if (interactive)
2246 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
2247
2248 if (ioctl_result < 0) {
2249 /* Find out what happened if it failed. */
2250 switch (ioctl_errno) {
2251 case EINTR:
2252 case EBADF:
2253 continue;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002254#ifdef FREEBSD
2255 case ENOTTY:
Roland McGrath553a6092002-12-16 20:40:39 +00002256#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002257 case ENOENT:
2258 droptcb(tcp);
2259 continue;
2260 default:
2261 perror("PIOCWSTOP");
2262 exit(1);
2263 }
2264 }
2265
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +00002266#ifdef FREEBSD
2267 if ((tcp->flags & TCB_STARTUP) && (tcp->status.PR_WHY == PR_SYSEXIT)) {
2268 /* discard first event for a syscall we never entered */
Denys Vlasenkob63256e2011-06-07 12:13:24 +02002269 IOCTL(tcp->pfd, PIOCRUN, 0);
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +00002270 continue;
2271 }
Roland McGrath553a6092002-12-16 20:40:39 +00002272#endif
2273
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002274 /* clear the just started flag */
2275 tcp->flags &= ~TCB_STARTUP;
2276
2277 /* set current output file */
2278 outf = tcp->outf;
Andreas Schwabccdff482009-10-27 16:27:13 +01002279 curcol = tcp->curcol;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002280
2281 if (cflag) {
2282 struct timeval stime;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002283#ifdef FREEBSD
2284 char buf[1024];
2285 int len;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002286
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002287 if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) {
2288 buf[len] = '\0';
2289 sscanf(buf,
2290 "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld",
2291 &stime.tv_sec, &stime.tv_usec);
2292 } else
2293 stime.tv_sec = stime.tv_usec = 0;
Roland McGrath553a6092002-12-16 20:40:39 +00002294#else /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002295 stime.tv_sec = tcp->status.pr_stime.tv_sec;
2296 stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002297#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002298 tv_sub(&tcp->dtime, &stime, &tcp->stime);
2299 tcp->stime = stime;
2300 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002301 what = tcp->status.PR_WHAT;
2302 switch (tcp->status.PR_WHY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002303#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002304 case PR_REQUESTED:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002305 if (tcp->status.PR_FLAGS & PR_ASLEEP) {
2306 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002307 if (trace_syscall(tcp) < 0) {
2308 fprintf(stderr, "syscall trouble\n");
2309 exit(1);
2310 }
2311 }
2312 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002313#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002314 case PR_SYSENTRY:
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00002315#ifdef POLL_HACK
2316 in_syscall = tcp;
2317#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002318 case PR_SYSEXIT:
2319 if (trace_syscall(tcp) < 0) {
2320 fprintf(stderr, "syscall trouble\n");
2321 exit(1);
2322 }
2323 break;
2324 case PR_SIGNALLED:
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002325 if (cflag != CFLAG_ONLY_STATS
2326 && (qual_flags[what] & QUAL_SIGNAL)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002327 printleader(tcp);
2328 tprintf("--- %s (%s) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00002329 signame(what), strsignal(what));
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002330 printtrailer();
John Hughes58265892001-10-18 15:13:53 +00002331#ifdef PR_INFO
2332 if (tcp->status.PR_INFO.si_signo == what) {
2333 printleader(tcp);
2334 tprintf(" siginfo=");
2335 printsiginfo(&tcp->status.PR_INFO, 1);
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002336 printtrailer();
John Hughes58265892001-10-18 15:13:53 +00002337 }
2338#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002339 }
2340 break;
2341 case PR_FAULTED:
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002342 if (cflag != CFLAGS_ONLY_STATS
2343 && (qual_flags[what] & QUAL_FAULT)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002344 printleader(tcp);
2345 tprintf("=== FAULT %d ===", what);
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002346 printtrailer();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002347 }
2348 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002349#ifdef FREEBSD
2350 case 0: /* handle case we polled for nothing */
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00002351 continue;
Roland McGrath553a6092002-12-16 20:40:39 +00002352#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002353 default:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002354 fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002355 exit(1);
2356 break;
2357 }
Andreas Schwabccdff482009-10-27 16:27:13 +01002358 /* Remember current print column before continuing. */
2359 tcp->curcol = curcol;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002360 arg = 0;
Roland McGrath553a6092002-12-16 20:40:39 +00002361#ifndef FREEBSD
Denys Vlasenkob63256e2011-06-07 12:13:24 +02002362 if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0)
Roland McGrath553a6092002-12-16 20:40:39 +00002363#else
Denys Vlasenkob63256e2011-06-07 12:13:24 +02002364 if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0)
Roland McGrath553a6092002-12-16 20:40:39 +00002365#endif
Andreas Schwab372cc842010-07-09 11:49:27 +02002366 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002367 perror("PIOCRUN");
2368 exit(1);
2369 }
2370 }
2371 return 0;
2372}
2373
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002374#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002375
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002376#ifdef TCB_GROUP_EXITING
2377/* Handle an exit detach or death signal that is taking all the
2378 related clone threads with it. This is called in three circumstances:
2379 SIG == -1 TCP has already died (TCB_ATTACHED is clear, strace is parent).
2380 SIG == 0 Continuing TCP will perform an exit_group syscall.
2381 SIG == other Continuing TCP with SIG will kill the process.
2382*/
2383static int
2384handle_group_exit(struct tcb *tcp, int sig)
2385{
2386 /* We need to locate our records of all the clone threads
2387 related to TCP, either its children or siblings. */
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002388 struct tcb *leader = NULL;
2389
2390 if (tcp->flags & TCB_CLONE_THREAD)
2391 leader = tcp->parent;
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002392
2393 if (sig < 0) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002394 if (leader != NULL && leader != tcp
2395 && !(leader->flags & TCB_GROUP_EXITING)
2396 && !(tcp->flags & TCB_STARTUP)
2397 ) {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002398 fprintf(stderr,
2399 "PANIC: handle_group_exit: %d leader %d\n",
2400 tcp->pid, leader ? leader->pid : -1);
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002401 }
2402 /* TCP no longer exists therefore you must not detach() it. */
Roland McGrath0a463882007-07-05 18:43:16 +00002403 droptcb(tcp); /* Already died. */
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002404 }
2405 else {
Roland McGratha08a97e2005-08-03 11:23:46 +00002406 /* Mark that we are taking the process down. */
2407 tcp->flags |= TCB_EXITING | TCB_GROUP_EXITING;
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002408 if (tcp->flags & TCB_ATTACHED) {
Roland McGrathd6a32f12007-07-11 08:35:11 +00002409 detach(tcp, sig);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00002410 if (leader != NULL && leader != tcp)
Roland McGrath1bfd3102007-08-03 10:02:00 +00002411 leader->flags |= TCB_GROUP_EXITING;
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002412 } else {
2413 if (ptrace_restart(PTRACE_CONT, tcp, sig) < 0) {
2414 cleanup();
2415 return -1;
2416 }
2417 if (leader != NULL) {
Roland McGrath05690952004-10-20 01:00:27 +00002418 leader->flags |= TCB_GROUP_EXITING;
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002419 if (leader != tcp)
2420 droptcb(tcp);
2421 }
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002422 /* The leader will report to us as parent now,
2423 and then we'll get to the SIG==-1 case. */
2424 return 0;
2425 }
2426 }
2427
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002428 return 0;
2429}
2430#endif
2431
Wang Chaoca8ab8d2010-11-12 17:26:08 +08002432#ifdef LINUX
2433static int
2434handle_ptrace_event(int status, struct tcb *tcp)
2435{
2436 if (status >> 16 == PTRACE_EVENT_VFORK ||
2437 status >> 16 == PTRACE_EVENT_CLONE ||
2438 status >> 16 == PTRACE_EVENT_FORK) {
Dmitry V. Levin2fabd0e2011-02-19 21:33:50 +00002439 long childpid;
Wang Chaoca8ab8d2010-11-12 17:26:08 +08002440
2441 if (do_ptrace(PTRACE_GETEVENTMSG, tcp, NULL, &childpid) < 0) {
2442 if (errno != ESRCH) {
2443 fprintf(stderr, "\
2444%s: handle_ptrace_event: ptrace cannot get new child's pid\n",
2445 progname);
2446 cleanup();
2447 exit(1);
2448 }
2449 return -1;
2450 }
2451 return handle_new_child(tcp, childpid, 0);
2452 }
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02002453 if (status >> 16 == PTRACE_EVENT_EXEC) {
2454 if (debug)
2455 fprintf(stderr, "PTRACE_EVENT_EXEC on pid %d (ignored)\n", tcp->pid);
2456 return 0;
2457 }
Denys Vlasenko75422762011-05-27 14:36:01 +02002458 /* Some PTRACE_EVENT_foo we didn't ask for?! */
2459 error_msg("Unexpected status %x on pid %d", status, tcp->pid);
Wang Chaoca8ab8d2010-11-12 17:26:08 +08002460 return 1;
2461}
2462#endif
2463
Roland McGratheb9e2e82009-06-02 16:49:22 -07002464static int
2465trace()
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002466{
2467 int pid;
2468 int wait_errno;
2469 int status;
2470 struct tcb *tcp;
2471#ifdef LINUX
2472 struct rusage ru;
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00002473#ifdef __WALL
Roland McGratheb9e2e82009-06-02 16:49:22 -07002474 static int wait4_options = __WALL;
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00002475#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002476#endif /* LINUX */
2477
Roland McGratheb9e2e82009-06-02 16:49:22 -07002478 while (nprocs != 0) {
Denys Vlasenko222713a2009-03-17 14:29:59 +00002479 if (interrupted)
Roland McGratheb9e2e82009-06-02 16:49:22 -07002480 return 0;
2481 if (interactive)
2482 sigprocmask(SIG_SETMASK, &empty_set, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002483#ifdef LINUX
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00002484#ifdef __WALL
Roland McGratheb9e2e82009-06-02 16:49:22 -07002485 pid = wait4(-1, &status, wait4_options, cflag ? &ru : NULL);
Roland McGrath5bc05552002-12-17 04:50:47 +00002486 if (pid < 0 && (wait4_options & __WALL) && errno == EINVAL) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00002487 /* this kernel does not support __WALL */
2488 wait4_options &= ~__WALL;
2489 errno = 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -07002490 pid = wait4(-1, &status, wait4_options,
2491 cflag ? &ru : NULL);
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00002492 }
Roland McGrath5bc05552002-12-17 04:50:47 +00002493 if (pid < 0 && !(wait4_options & __WALL) && errno == ECHILD) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00002494 /* most likely a "cloned" process */
Roland McGratheb9e2e82009-06-02 16:49:22 -07002495 pid = wait4(-1, &status, __WCLONE,
2496 cflag ? &ru : NULL);
2497 if (pid == -1) {
2498 fprintf(stderr, "strace: clone wait4 "
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00002499 "failed: %s\n", strerror(errno));
2500 }
2501 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002502#else
2503 pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
2504#endif /* __WALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002505#endif /* LINUX */
2506#ifdef SUNOS4
2507 pid = wait(&status);
2508#endif /* SUNOS4 */
2509 wait_errno = errno;
Roland McGratheb9e2e82009-06-02 16:49:22 -07002510 if (interactive)
2511 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002512
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002513 if (pid == -1) {
Roland McGratheb9e2e82009-06-02 16:49:22 -07002514 switch (wait_errno) {
2515 case EINTR:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002516 continue;
Roland McGratheb9e2e82009-06-02 16:49:22 -07002517 case ECHILD:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002518 /*
2519 * We would like to verify this case
2520 * but sometimes a race in Solbourne's
2521 * version of SunOS sometimes reports
2522 * ECHILD before sending us SIGCHILD.
2523 */
Roland McGratheb9e2e82009-06-02 16:49:22 -07002524 return 0;
2525 default:
2526 errno = wait_errno;
2527 perror("strace: wait");
2528 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002529 }
2530 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00002531 if (pid == popen_pid) {
2532 if (WIFEXITED(status) || WIFSIGNALED(status))
2533 popen_pid = -1;
2534 continue;
2535 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002536 if (debug)
2537 fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
2538
2539 /* Look up `pid' in our table. */
2540 if ((tcp = pid2tcb(pid)) == NULL) {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002541#ifdef LINUX
Roland McGrath41c48222008-07-18 00:25:10 +00002542 if (followfork) {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002543 /* This is needed to go with the CLONE_PTRACE
2544 changes in process.c/util.c: we might see
2545 the child's initial trap before we see the
2546 parent return from the clone syscall.
2547 Leave the child suspended until the parent
2548 returns from its system call. Only then
2549 will we have the association of parent and
2550 child so that we know how to do clearbpt
2551 in the child. */
Denys Vlasenko418d66a2009-01-17 01:52:54 +00002552 tcp = alloctcb(pid);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002553 tcp->flags |= TCB_ATTACHED | TCB_SUSPENDED;
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002554 if (!qflag)
2555 fprintf(stderr, "\
2556Process %d attached (waiting for parent)\n",
2557 pid);
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00002558 }
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002559 else
2560 /* This can happen if a clone call used
2561 CLONE_PTRACE itself. */
Roland McGratheb9e2e82009-06-02 16:49:22 -07002562#endif
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002563 {
2564 fprintf(stderr, "unknown pid: %u\n", pid);
2565 if (WIFSTOPPED(status))
2566 ptrace(PTRACE_CONT, pid, (char *) 1, 0);
2567 exit(1);
2568 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002569 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002570 /* set current output file */
2571 outf = tcp->outf;
Andreas Schwabccdff482009-10-27 16:27:13 +01002572 curcol = tcp->curcol;
Denys Vlasenko84e20af2009-02-10 16:03:20 +00002573 if (cflag) {
Roland McGratheb9e2e82009-06-02 16:49:22 -07002574#ifdef LINUX
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002575 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2576 tcp->stime = ru.ru_stime;
Roland McGratheb9e2e82009-06-02 16:49:22 -07002577#endif /* !LINUX */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002578 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002579
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002580 if (tcp->flags & TCB_SUSPENDED) {
2581 /*
2582 * Apparently, doing any ptrace() call on a stopped
2583 * process, provokes the kernel to report the process
2584 * status again on a subsequent wait(), even if the
2585 * process has not been actually restarted.
2586 * Since we have inspected the arguments of suspended
2587 * processes we end up here testing for this case.
2588 */
2589 continue;
2590 }
2591 if (WIFSIGNALED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002592 if (pid == strace_child)
2593 exit_code = 0x100 | WTERMSIG(status);
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002594 if (cflag != CFLAG_ONLY_STATS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002595 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
2596 printleader(tcp);
Roland McGrath2efe8792004-01-13 09:59:45 +00002597 tprintf("+++ killed by %s %s+++",
2598 signame(WTERMSIG(status)),
2599#ifdef WCOREDUMP
2600 WCOREDUMP(status) ? "(core dumped) " :
2601#endif
2602 "");
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002603 printtrailer();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002604 }
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002605#ifdef TCB_GROUP_EXITING
2606 handle_group_exit(tcp, -1);
2607#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002608 droptcb(tcp);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002609#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002610 continue;
2611 }
2612 if (WIFEXITED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002613 if (pid == strace_child)
2614 exit_code = WEXITSTATUS(status);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002615 if (debug)
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002616 fprintf(stderr, "pid %u exited with %d\n", pid, WEXITSTATUS(status));
2617 if ((tcp->flags & (TCB_ATTACHED|TCB_STARTUP)) == TCB_ATTACHED
Roland McGrath05690952004-10-20 01:00:27 +00002618#ifdef TCB_GROUP_EXITING
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002619 && !(tcp->parent && (tcp->parent->flags & TCB_GROUP_EXITING))
Roland McGrath1bfd3102007-08-03 10:02:00 +00002620 && !(tcp->flags & TCB_GROUP_EXITING)
Roland McGrath05690952004-10-20 01:00:27 +00002621#endif
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002622 ) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002623 fprintf(stderr,
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002624 "PANIC: attached pid %u exited with %d\n",
2625 pid, WEXITSTATUS(status));
2626 }
Roland McGrath0a396902003-06-10 03:05:53 +00002627 if (tcp == tcp_last) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002628 if ((tcp->flags & (TCB_INSYSCALL|TCB_REPRINT)) == TCB_INSYSCALL)
Roland McGrath0a396902003-06-10 03:05:53 +00002629 tprintf(" <unfinished ... exit status %d>\n",
2630 WEXITSTATUS(status));
2631 tcp_last = NULL;
2632 }
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002633#ifdef TCB_GROUP_EXITING
2634 handle_group_exit(tcp, -1);
2635#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002636 droptcb(tcp);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002637#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002638 continue;
2639 }
2640 if (!WIFSTOPPED(status)) {
2641 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
2642 droptcb(tcp);
2643 continue;
2644 }
2645 if (debug)
2646 fprintf(stderr, "pid %u stopped, [%s]\n",
Nate Sammonsce780fc1999-03-29 23:23:13 +00002647 pid, signame(WSTOPSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002648
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02002649 if (status >> 16) {
Wang Chaoca8ab8d2010-11-12 17:26:08 +08002650 if (handle_ptrace_event(status, tcp) != 1)
2651 goto tracing;
2652 }
2653
Roland McGrath02203312007-06-11 22:06:31 +00002654 /*
2655 * Interestingly, the process may stop
2656 * with STOPSIG equal to some other signal
Roland McGratheb9e2e82009-06-02 16:49:22 -07002657 * than SIGSTOP if we happend to attach
Roland McGrath02203312007-06-11 22:06:31 +00002658 * just before the process takes a signal.
Mike Frysingerc1a5b7e2009-10-07 20:41:29 -04002659 * A no-MMU vforked child won't send up a signal,
2660 * so skip the first (lost) execve notification.
Roland McGrath02203312007-06-11 22:06:31 +00002661 */
Mike Frysingerc1a5b7e2009-10-07 20:41:29 -04002662 if ((tcp->flags & TCB_STARTUP) &&
2663 (WSTOPSIG(status) == SIGSTOP || strace_vforked)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002664 /*
2665 * This flag is there to keep us in sync.
2666 * Next time this process stops it should
2667 * really be entering a system call.
2668 */
2669 tcp->flags &= ~TCB_STARTUP;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002670 if (tcp->flags & TCB_BPTSET) {
Roland McGrath02203312007-06-11 22:06:31 +00002671 /*
2672 * One example is a breakpoint inherited from
2673 * parent through fork ().
2674 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002675 if (clearbpt(tcp) < 0) /* Pretty fatal */ {
2676 droptcb(tcp);
2677 cleanup();
2678 return -1;
2679 }
2680 }
Wang Chaoca8ab8d2010-11-12 17:26:08 +08002681#ifdef LINUX
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02002682 int options = ptrace_setoptions_for_all;
2683 if (followfork && (tcp->parent == NULL))
2684 options |= ptrace_setoptions_followfork;
2685 if (options) {
2686 if (debug)
2687 fprintf(stderr, "setting opts %x on pid %d\n", options, tcp->pid);
2688 if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, options) < 0) {
2689 if (errno != ESRCH) {
2690 /* Should never happen, really */
Denys Vlasenko75422762011-05-27 14:36:01 +02002691 perror_msg_and_die("PTRACE_SETOPTIONS");
Denys Vlasenko3454e4b2011-05-23 21:29:03 +02002692 }
2693 }
2694 }
Wang Chaoca8ab8d2010-11-12 17:26:08 +08002695#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002696 goto tracing;
2697 }
2698
Denys Vlasenko75422762011-05-27 14:36:01 +02002699 if (WSTOPSIG(status) != syscall_trap_sig) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002700 if (WSTOPSIG(status) == SIGSTOP &&
2701 (tcp->flags & TCB_SIGTRAPPED)) {
2702 /*
2703 * Trapped attempt to block SIGTRAP
2704 * Hope we are back in control now.
2705 */
2706 tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002707 if (ptrace_restart(PTRACE_SYSCALL, tcp, 0) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002708 cleanup();
2709 return -1;
2710 }
2711 continue;
2712 }
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002713 if (cflag != CFLAG_ONLY_STATS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002714 && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
Dmitry V. Levinc15dfc72011-03-10 14:44:45 +00002715 siginfo_t si;
2716#if defined(PT_CR_IPSR) && defined(PT_CR_IIP)
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002717 long pc = 0;
2718 long psr = 0;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002719
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002720 upeek(tcp, PT_CR_IPSR, &psr);
2721 upeek(tcp, PT_CR_IIP, &pc);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002722
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002723# define PSR_RI 41
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002724 pc += (psr >> PSR_RI) & 0x3;
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002725# define PC_FORMAT_STR " @ %lx"
2726# define PC_FORMAT_ARG pc
2727#else
2728# define PC_FORMAT_STR "%s"
2729# define PC_FORMAT_ARG ""
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002730#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002731 printleader(tcp);
Dmitry V. Levin6b7a2612011-03-10 21:20:35 +00002732 if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) {
2733 tprintf("--- ");
2734 printsiginfo(&si, verbose(tcp));
2735 tprintf(" (%s)" PC_FORMAT_STR " ---",
2736 strsignal(WSTOPSIG(status)),
2737 PC_FORMAT_ARG);
2738 } else
2739 tprintf("--- %s by %s" PC_FORMAT_STR " ---",
2740 strsignal(WSTOPSIG(status)),
2741 signame(WSTOPSIG(status)),
2742 PC_FORMAT_ARG);
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002743 printtrailer();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002744 }
Roland McGrath05690952004-10-20 01:00:27 +00002745 if (((tcp->flags & TCB_ATTACHED) ||
2746 tcp->nclone_threads > 0) &&
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002747 !sigishandled(tcp, WSTOPSIG(status))) {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002748#ifdef TCB_GROUP_EXITING
2749 handle_group_exit(tcp, WSTOPSIG(status));
2750#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002751 detach(tcp, WSTOPSIG(status));
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002752#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002753 continue;
2754 }
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002755 if (ptrace_restart(PTRACE_SYSCALL, tcp, WSTOPSIG(status)) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002756 cleanup();
2757 return -1;
2758 }
2759 tcp->flags &= ~TCB_SUSPENDED;
2760 continue;
2761 }
Roland McGrath02203312007-06-11 22:06:31 +00002762 /* we handled the STATUS, we are permitted to interrupt now. */
2763 if (interrupted)
2764 return 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -07002765 if (trace_syscall(tcp) < 0 && !tcp->ptrace_errno) {
2766 /* ptrace() failed in trace_syscall() with ESRCH.
2767 * Likely a result of process disappearing mid-flight.
2768 * Observed case: exit_group() terminating
2769 * all processes in thread group. In this case, threads
2770 * "disappear" in an unpredictable moment without any
2771 * notification to strace via wait().
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002772 */
2773 if (tcp->flags & TCB_ATTACHED) {
Roland McGratheb9e2e82009-06-02 16:49:22 -07002774 if (tcp_last) {
2775 /* Do we have dangling line "syscall(param, param"?
2776 * Finish the line then. We cannot
2777 */
2778 tcp_last->flags |= TCB_REPRINT;
2779 tprintf(" <unfinished ...>");
2780 printtrailer();
2781 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002782 detach(tcp, 0);
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002783 } else {
Roland McGratheb9e2e82009-06-02 16:49:22 -07002784 ptrace(PTRACE_KILL,
2785 tcp->pid, (char *) 1, SIGTERM);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002786 droptcb(tcp);
2787 }
2788 continue;
2789 }
2790 if (tcp->flags & TCB_EXITING) {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002791#ifdef TCB_GROUP_EXITING
2792 if (tcp->flags & TCB_GROUP_EXITING) {
2793 if (handle_group_exit(tcp, 0) < 0)
2794 return -1;
2795 continue;
2796 }
2797#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002798 if (tcp->flags & TCB_ATTACHED)
2799 detach(tcp, 0);
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002800 else if (ptrace_restart(PTRACE_CONT, tcp, 0) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002801 cleanup();
2802 return -1;
2803 }
2804 continue;
2805 }
2806 if (tcp->flags & TCB_SUSPENDED) {
2807 if (!qflag)
2808 fprintf(stderr, "Process %u suspended\n", pid);
2809 continue;
2810 }
2811 tracing:
Andreas Schwabccdff482009-10-27 16:27:13 +01002812 /* Remember current print column before continuing. */
2813 tcp->curcol = curcol;
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002814 if (ptrace_restart(PTRACE_SYSCALL, tcp, 0) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002815 cleanup();
2816 return -1;
2817 }
2818 }
2819 return 0;
2820}
2821
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002822#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002823
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002824#include <stdarg.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002825
2826void
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002827tprintf(const char *fmt, ...)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002828{
2829 va_list args;
2830
Andreas Schwabe5355de2009-10-27 16:56:43 +01002831 va_start(args, fmt);
Roland McGrathb310a0c2003-11-06 23:41:22 +00002832 if (outf) {
2833 int n = vfprintf(outf, fmt, args);
Andreas Schwabccdff482009-10-27 16:27:13 +01002834 if (n < 0) {
2835 if (outf != stderr)
2836 perror(outfname == NULL
2837 ? "<writing to pipe>" : outfname);
2838 } else
Roland McGrathb310a0c2003-11-06 23:41:22 +00002839 curcol += n;
2840 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002841 va_end(args);
2842 return;
2843}
2844
2845void
Denys Vlasenko12014262011-05-30 14:00:14 +02002846printleader(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002847{
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002848 if (tcp_last) {
Roland McGratheb9e2e82009-06-02 16:49:22 -07002849 if (tcp_last->ptrace_errno) {
Denys Vlasenko7e0615f2009-01-28 19:00:54 +00002850 if (tcp_last->flags & TCB_INSYSCALL) {
Denys Vlasenkoe62df002011-06-08 16:15:04 +02002851 tprintf(" <unavailable>) ");
Roland McGratheb9e2e82009-06-02 16:49:22 -07002852 tabto(acolumn);
Denys Vlasenko7e0615f2009-01-28 19:00:54 +00002853 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002854 tprintf("= ? <unavailable>\n");
2855 tcp_last->ptrace_errno = 0;
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002856 } else if (!outfname || followfork < 2 || tcp_last == tcp) {
Denys Vlasenko7e0615f2009-01-28 19:00:54 +00002857 tcp_last->flags |= TCB_REPRINT;
Roland McGratheb9e2e82009-06-02 16:49:22 -07002858 tprintf(" <unfinished ...>\n");
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002859 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002860 }
2861 curcol = 0;
2862 if ((followfork == 1 || pflag_seen > 1) && outfname)
2863 tprintf("%-5d ", tcp->pid);
2864 else if (nprocs > 1 && !outfname)
2865 tprintf("[pid %5u] ", tcp->pid);
2866 if (tflag) {
2867 char str[sizeof("HH:MM:SS")];
2868 struct timeval tv, dtv;
2869 static struct timeval otv;
2870
2871 gettimeofday(&tv, NULL);
2872 if (rflag) {
2873 if (otv.tv_sec == 0)
2874 otv = tv;
2875 tv_sub(&dtv, &tv, &otv);
2876 tprintf("%6ld.%06ld ",
2877 (long) dtv.tv_sec, (long) dtv.tv_usec);
2878 otv = tv;
2879 }
2880 else if (tflag > 2) {
2881 tprintf("%ld.%06ld ",
2882 (long) tv.tv_sec, (long) tv.tv_usec);
2883 }
2884 else {
2885 time_t local = tv.tv_sec;
2886 strftime(str, sizeof(str), "%T", localtime(&local));
2887 if (tflag > 1)
2888 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
2889 else
2890 tprintf("%s ", str);
2891 }
2892 }
2893 if (iflag)
2894 printcall(tcp);
2895}
2896
2897void
Denys Vlasenko12014262011-05-30 14:00:14 +02002898tabto(int col)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002899{
2900 if (curcol < col)
2901 tprintf("%*s", col - curcol, "");
2902}
2903
2904void
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002905printtrailer(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002906{
2907 tprintf("\n");
2908 tcp_last = NULL;
2909}
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002910
Wichert Akkermanea78f0f1999-11-29 15:34:02 +00002911#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002912
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002913int
2914mp_ioctl(int fd, int cmd, void *arg, int size)
2915{
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002916 struct iovec iov[2];
2917 int n = 1;
Roland McGrath553a6092002-12-16 20:40:39 +00002918
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002919 iov[0].iov_base = &cmd;
2920 iov[0].iov_len = sizeof cmd;
2921 if (arg) {
2922 ++n;
2923 iov[1].iov_base = arg;
2924 iov[1].iov_len = size;
2925 }
Roland McGrath553a6092002-12-16 20:40:39 +00002926
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002927 return writev(fd, iov, n);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002928}
2929
2930#endif