blob: c1d65398498a15741cd91f088e60ee8a0677170f [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>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000036#include <signal.h>
37#include <errno.h>
38#include <sys/param.h>
39#include <fcntl.h>
40#include <sys/resource.h>
41#include <sys/wait.h>
42#include <sys/stat.h>
43#include <pwd.h>
44#include <grp.h>
45#include <string.h>
John Hughes19e49982001-10-19 08:59:12 +000046#include <limits.h>
Roland McGrath70b08532004-04-09 00:25:21 +000047#include <dirent.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000048
Roland McGrath134813a2007-06-02 00:07:33 +000049#ifdef LINUX
50# include <asm/unistd.h>
51# if defined __NR_tgkill
52# define my_tgkill(pid, tid, sig) syscall (__NR_tgkill, (pid), (tid), (sig))
53# elif defined __NR_tkill
54# define my_tgkill(pid, tid, sig) syscall (__NR_tkill, (tid), (sig))
55# else
56 /* kill() may choose arbitrarily the target task of the process group
57 while we later wait on a that specific TID. PID process waits become
58 TID task specific waits for a process under ptrace(2). */
59# warning "Neither tkill(2) nor tgkill(2) available, risk of strace hangs!"
60# define my_tgkill(pid, tid, sig) kill ((tid), (sig))
61# endif
62#endif
63
Wichert Akkerman7b3346b2001-10-09 23:47:38 +000064#if defined(IA64) && defined(LINUX)
65# include <asm/ptrace_offsets.h>
66#endif
67
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +000068#ifdef USE_PROCFS
69#include <poll.h>
70#endif
71
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000072#ifdef SVR4
73#include <sys/stropts.h>
Wichert Akkermanea78f0f1999-11-29 15:34:02 +000074#ifdef HAVE_MP_PROCFS
John Hughes1d08dcf2001-07-10 13:48:44 +000075#ifdef HAVE_SYS_UIO_H
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000076#include <sys/uio.h>
77#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000078#endif
John Hughes1d08dcf2001-07-10 13:48:44 +000079#endif
Denys Vlasenko96d5a762008-12-29 19:13:27 +000080extern char **environ;
Denys Vlasenko418d66a2009-01-17 01:52:54 +000081extern int optind;
82extern char *optarg;
Denys Vlasenko96d5a762008-12-29 19:13:27 +000083
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000084
Roland McGrath41c48222008-07-18 00:25:10 +000085int debug = 0, followfork = 0;
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +000086int dtime = 0, xflag = 0, qflag = 0;
87cflag_t cflag = CFLAG_NONE;
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +000088static int iflag = 0, interactive = 0, pflag_seen = 0, rflag = 0, tflag = 0;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +000089/*
90 * daemonized_tracer supports -D option.
91 * With this option, strace forks twice.
92 * Unlike normal case, with -D *grandparent* process exec's,
93 * becoming a traced process. Child exits (this prevents traced process
94 * from having children it doesn't expect to have), and grandchild
95 * attaches to grandparent similarly to strace -p PID.
96 * This allows for more transparent interaction in cases
97 * when process and its parent are communicating via signals,
98 * wait() etc. Without -D, strace process gets lodged in between,
99 * disrupting parent<->child link.
100 */
101static bool daemonized_tracer = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000102
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000103/* Sometimes we want to print only succeeding syscalls. */
104int not_failing_only = 0;
105
Dmitry V. Levina6809652008-11-10 17:14:58 +0000106static int exit_code = 0;
107static int strace_child = 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -0700108
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000109static char *username = NULL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000110uid_t run_uid;
111gid_t run_gid;
112
113int acolumn = DEFAULT_ACOLUMN;
114int max_strlen = DEFAULT_STRLEN;
Dmitry V. Levinb9fe0112006-12-13 16:59:44 +0000115static char *outfname = NULL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000116FILE *outf;
Andreas Schwabccdff482009-10-27 16:27:13 +0100117static int curcol;
Roland McGrathee9d4352002-12-18 04:16:10 +0000118struct tcb **tcbtab;
119unsigned int nprocs, tcbtabsize;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000120char *progname;
Roland McGratheb9e2e82009-06-02 16:49:22 -0700121extern char **environ;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000122
Andreas Schwabe5355de2009-10-27 16:56:43 +0100123static int detach(struct tcb *tcp, int sig);
124static int trace(void);
125static void cleanup(void);
126static void interrupt(int sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000127static sigset_t empty_set, blocked_set;
128
129#ifdef HAVE_SIG_ATOMIC_T
130static volatile sig_atomic_t interrupted;
131#else /* !HAVE_SIG_ATOMIC_T */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000132static volatile int interrupted;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000133#endif /* !HAVE_SIG_ATOMIC_T */
134
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000135#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000136
Andreas Schwabe5355de2009-10-27 16:56:43 +0100137static struct tcb *pfd2tcb(int pfd);
138static void reaper(int sig);
139static void rebuild_pollv(void);
Roland McGrathee9d4352002-12-18 04:16:10 +0000140static struct pollfd *pollv;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000141
142#ifndef HAVE_POLLABLE_PROCFS
143
Andreas Schwabe5355de2009-10-27 16:56:43 +0100144static void proc_poll_open(void);
145static void proc_poller(int pfd);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000146
147struct proc_pollfd {
148 int fd;
149 int revents;
150 int pid;
151};
152
153static int poller_pid;
154static int proc_poll_pipe[2] = { -1, -1 };
155
156#endif /* !HAVE_POLLABLE_PROCFS */
157
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000158#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000159#define POLLWANT POLLWRNORM
160#else
161#define POLLWANT POLLPRI
162#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000163#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000164
165static void
166usage(ofp, exitval)
167FILE *ofp;
168int exitval;
169{
170 fprintf(ofp, "\
Andreas Schwabb87d30c2010-06-11 15:49:36 +0200171usage: strace [-CdDffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000172 [-p pid] ... [-s strsize] [-u username] [-E var=val] ...\n\
173 [command [arg ...]]\n\
Andreas Schwabb87d30c2010-06-11 15:49:36 +0200174 or: strace -c [-D] [-e expr] ... [-O overhead] [-S sortby] [-E var=val] ...\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000175 [command [arg ...]]\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000176-c -- count time, calls, and errors for each syscall and report summary\n\
Andreas Schwabb87d30c2010-06-11 15:49:36 +0200177-C -- like -c but also print regular output while processes are running\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000178-f -- follow forks, -ff -- with output into separate files\n\
179-F -- attempt to follow vforks, -h -- print help message\n\
180-i -- print instruction pointer at time of syscall\n\
181-q -- suppress messages about attaching, detaching, etc.\n\
182-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
183-T -- print time spent in each syscall, -V -- print version\n\
184-v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\
185-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
186-a column -- alignment COLUMN for printing syscall results (default %d)\n\
187-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
188 options: trace, abbrev, verbose, raw, signal, read, or write\n\
189-o file -- send trace output to FILE instead of stderr\n\
190-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
191-p pid -- trace process with process id PID, may be repeated\n\
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000192-D -- run tracer process as a detached grandchild, not as parent\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000193-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
194-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
195-u username -- run command as username handling setuid and/or setgid\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000196-E var=val -- put var=val in the environment for command\n\
197-E var -- remove var from the environment for command\n\
198" /* this is broken, so don't document it
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000199-z -- print only succeeding syscalls\n\
Roland McGrathde6e5332003-01-24 04:31:23 +0000200 */
201, DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000202 exit(exitval);
203}
204
205#ifdef SVR4
206#ifdef MIPS
207void
208foobar()
209{
210}
211#endif /* MIPS */
212#endif /* SVR4 */
213
Mike Frysingerc1a5b7e2009-10-07 20:41:29 -0400214/* Glue for systems without a MMU that cannot provide fork() */
215#ifdef HAVE_FORK
216# define strace_vforked 0
217#else
218# define strace_vforked 1
219# define fork() vfork()
220#endif
221
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000222static int
223set_cloexec_flag(int fd)
224{
225 int flags, newflags;
226
227 if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
228 {
229 fprintf(stderr, "%s: fcntl F_GETFD: %s\n",
230 progname, strerror(errno));
231 return -1;
232 }
233
234 newflags = flags | FD_CLOEXEC;
235 if (flags == newflags)
236 return 0;
237
238 if (fcntl(fd, F_SETFD, newflags) < 0)
239 {
240 fprintf(stderr, "%s: fcntl F_SETFD: %s\n",
241 progname, strerror(errno));
242 return -1;
243 }
244
245 return 0;
246}
247
248/*
249 * When strace is setuid executable, we have to swap uids
250 * before and after filesystem and process management operations.
251 */
252static void
253swap_uid(void)
254{
255#ifndef SVR4
256 int euid = geteuid(), uid = getuid();
257
258 if (euid != uid && setreuid(euid, uid) < 0)
259 {
260 fprintf(stderr, "%s: setreuid: %s\n",
261 progname, strerror(errno));
262 exit(1);
263 }
264#endif
265}
266
Roland McGrath4bfa6262007-07-05 20:03:16 +0000267#if _LFS64_LARGEFILE
268# define fopen_for_output fopen64
269#else
270# define fopen_for_output fopen
271#endif
272
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000273static FILE *
274strace_fopen(const char *path, const char *mode)
275{
276 FILE *fp;
277
278 swap_uid();
Roland McGrath4bfa6262007-07-05 20:03:16 +0000279 if ((fp = fopen_for_output(path, mode)) == NULL)
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000280 fprintf(stderr, "%s: can't fopen '%s': %s\n",
281 progname, path, strerror(errno));
282 swap_uid();
283 if (fp && set_cloexec_flag(fileno(fp)) < 0)
284 {
285 fclose(fp);
286 fp = NULL;
287 }
288 return fp;
289}
290
291static int popen_pid = -1;
292
293#ifndef _PATH_BSHELL
294# define _PATH_BSHELL "/bin/sh"
295#endif
296
297/*
298 * We cannot use standard popen(3) here because we have to distinguish
299 * popen child process from other processes we trace, and standard popen(3)
300 * does not export its child's pid.
301 */
302static FILE *
303strace_popen(const char *command)
304{
305 int fds[2];
306
307 swap_uid();
308 if (pipe(fds) < 0)
309 {
310 fprintf(stderr, "%s: pipe: %s\n",
311 progname, strerror(errno));
312 swap_uid();
313 return NULL;
314 }
315
316 if (set_cloexec_flag(fds[1]) < 0)
317 {
318 close(fds[0]);
319 close(fds[1]);
320 swap_uid();
321 return NULL;
322 }
323
324 if ((popen_pid = fork()) == -1)
325 {
326 fprintf(stderr, "%s: fork: %s\n",
327 progname, strerror(errno));
328 close(fds[0]);
329 close(fds[1]);
330 swap_uid();
331 return NULL;
332 }
333
334 if (popen_pid)
335 {
336 /* parent */
337 close(fds[0]);
338 swap_uid();
339 return fdopen(fds[1], "w");
340 } else
341 {
342 /* child */
343 close(fds[1]);
344 if (fds[0] && (dup2(fds[0], 0) || close(fds[0])))
345 {
346 fprintf(stderr, "%s: dup2: %s\n",
347 progname, strerror(errno));
348 _exit(1);
349 }
350 execl(_PATH_BSHELL, "sh", "-c", command, NULL);
351 fprintf(stderr, "%s: execl: %s: %s\n",
352 progname, _PATH_BSHELL, strerror(errno));
353 _exit(1);
354 }
355}
356
357static int
358newoutf(struct tcb *tcp)
359{
360 if (outfname && followfork > 1) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000361 char name[520 + sizeof(int) * 3];
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000362 FILE *fp;
363
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000364 sprintf(name, "%.512s.%u", outfname, tcp->pid);
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000365 if ((fp = strace_fopen(name, "w")) == NULL)
366 return -1;
367 tcp->outf = fp;
368 }
369 return 0;
370}
371
Roland McGrath02203312007-06-11 22:06:31 +0000372static void
373startup_attach(void)
374{
375 int tcbi;
376 struct tcb *tcp;
377
378 /*
379 * Block user interruptions as we would leave the traced
380 * process stopped (process state T) if we would terminate in
381 * between PTRACE_ATTACH and wait4 () on SIGSTOP.
382 * We rely on cleanup () from this point on.
383 */
384 if (interactive)
385 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
386
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000387 if (daemonized_tracer) {
388 pid_t pid = fork();
389 if (pid < 0) {
390 _exit(1);
391 }
392 if (pid) { /* parent */
393 /*
394 * Wait for child to attach to straced process
395 * (our parent). Child SIGKILLs us after it attached.
396 * Parent's wait() is unblocked by our death,
397 * it proceeds to exec the straced program.
398 */
399 pause();
400 _exit(0); /* paranoia */
401 }
402 }
403
Roland McGrath02203312007-06-11 22:06:31 +0000404 for (tcbi = 0; tcbi < tcbtabsize; tcbi++) {
405 tcp = tcbtab[tcbi];
406 if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
407 continue;
408#ifdef LINUX
409 if (tcp->flags & TCB_CLONE_THREAD)
410 continue;
411#endif
412 /* Reinitialize the output since it may have changed. */
413 tcp->outf = outf;
414 if (newoutf(tcp) < 0)
415 exit(1);
416
417#ifdef USE_PROCFS
418 if (proc_open(tcp, 1) < 0) {
419 fprintf(stderr, "trouble opening proc file\n");
420 droptcb(tcp);
421 continue;
422 }
423#else /* !USE_PROCFS */
424# ifdef LINUX
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000425 if (followfork && !daemonized_tracer) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000426 char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3];
Roland McGrath02203312007-06-11 22:06:31 +0000427 DIR *dir;
428
429 sprintf(procdir, "/proc/%d/task", tcp->pid);
430 dir = opendir(procdir);
431 if (dir != NULL) {
432 unsigned int ntid = 0, nerr = 0;
433 struct dirent *de;
434 int tid;
435 while ((de = readdir(dir)) != NULL) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000436 if (de->d_fileno == 0)
Roland McGrath02203312007-06-11 22:06:31 +0000437 continue;
438 tid = atoi(de->d_name);
439 if (tid <= 0)
440 continue;
441 ++ntid;
Denys Vlasenkoaab52ca2009-03-17 14:46:54 +0000442 if (ptrace(PTRACE_ATTACH, tid, (char *) 1, 0) < 0)
Roland McGrath02203312007-06-11 22:06:31 +0000443 ++nerr;
Denys Vlasenkoaab52ca2009-03-17 14:46:54 +0000444 else if (tid != tcbtab[tcbi]->pid) {
Denys Vlasenko418d66a2009-01-17 01:52:54 +0000445 tcp = alloctcb(tid);
Wang Chao21b8db42010-08-27 17:43:16 +0800446 tcp->flags |= TCB_ATTACHED|TCB_CLONE_THREAD|TCB_FOLLOWFORK;
Roland McGrath02203312007-06-11 22:06:31 +0000447 tcbtab[tcbi]->nchildren++;
448 tcbtab[tcbi]->nclone_threads++;
Roland McGrath02203312007-06-11 22:06:31 +0000449 tcp->parent = tcbtab[tcbi];
450 }
Denys Vlasenkoaab52ca2009-03-17 14:46:54 +0000451 if (interactive) {
452 sigprocmask(SIG_SETMASK, &empty_set, NULL);
453 if (interrupted)
454 return;
455 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
456 }
Roland McGrath02203312007-06-11 22:06:31 +0000457 }
458 closedir(dir);
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000459 ntid -= nerr;
460 if (ntid == 0) {
Roland McGrath02203312007-06-11 22:06:31 +0000461 perror("attach: ptrace(PTRACE_ATTACH, ...)");
462 droptcb(tcp);
463 continue;
464 }
465 if (!qflag) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000466 fprintf(stderr, ntid > 1
467? "Process %u attached with %u threads - interrupt to quit\n"
468: "Process %u attached - interrupt to quit\n",
469 tcbtab[tcbi]->pid, ntid);
Roland McGrath02203312007-06-11 22:06:31 +0000470 }
471 continue;
Denys Vlasenko7a8bf062009-01-29 20:38:20 +0000472 } /* if (opendir worked) */
473 } /* if (-f) */
Roland McGrath02203312007-06-11 22:06:31 +0000474# endif
475 if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
476 perror("attach: ptrace(PTRACE_ATTACH, ...)");
477 droptcb(tcp);
478 continue;
479 }
480 /* INTERRUPTED is going to be checked at the top of TRACE. */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000481
482 if (daemonized_tracer) {
483 /*
484 * It is our grandparent we trace, not a -p PID.
485 * Don't want to just detach on exit, so...
486 */
487 tcp->flags &= ~TCB_ATTACHED;
488 /*
489 * Make parent go away.
490 * Also makes grandparent's wait() unblock.
491 */
492 kill(getppid(), SIGKILL);
493 }
494
Roland McGrath02203312007-06-11 22:06:31 +0000495#endif /* !USE_PROCFS */
496 if (!qflag)
497 fprintf(stderr,
498 "Process %u attached - interrupt to quit\n",
499 tcp->pid);
500 }
501
502 if (interactive)
503 sigprocmask(SIG_SETMASK, &empty_set, NULL);
504}
505
506static void
507startup_child (char **argv)
508{
509 struct stat statbuf;
510 const char *filename;
511 char pathname[MAXPATHLEN];
512 int pid = 0;
513 struct tcb *tcp;
514
515 filename = argv[0];
516 if (strchr(filename, '/')) {
517 if (strlen(filename) > sizeof pathname - 1) {
518 errno = ENAMETOOLONG;
519 perror("strace: exec");
520 exit(1);
521 }
522 strcpy(pathname, filename);
523 }
524#ifdef USE_DEBUGGING_EXEC
525 /*
526 * Debuggers customarily check the current directory
527 * first regardless of the path but doing that gives
528 * security geeks a panic attack.
529 */
530 else if (stat(filename, &statbuf) == 0)
531 strcpy(pathname, filename);
532#endif /* USE_DEBUGGING_EXEC */
533 else {
534 char *path;
535 int m, n, len;
536
537 for (path = getenv("PATH"); path && *path; path += m) {
538 if (strchr(path, ':')) {
539 n = strchr(path, ':') - path;
540 m = n + 1;
541 }
542 else
543 m = n = strlen(path);
544 if (n == 0) {
545 if (!getcwd(pathname, MAXPATHLEN))
546 continue;
547 len = strlen(pathname);
548 }
549 else if (n > sizeof pathname - 1)
550 continue;
551 else {
552 strncpy(pathname, path, n);
553 len = n;
554 }
555 if (len && pathname[len - 1] != '/')
556 pathname[len++] = '/';
557 strcpy(pathname + len, filename);
558 if (stat(pathname, &statbuf) == 0 &&
559 /* Accept only regular files
560 with some execute bits set.
561 XXX not perfect, might still fail */
562 S_ISREG(statbuf.st_mode) &&
563 (statbuf.st_mode & 0111))
564 break;
565 }
566 }
567 if (stat(pathname, &statbuf) < 0) {
568 fprintf(stderr, "%s: %s: command not found\n",
569 progname, filename);
570 exit(1);
571 }
Dmitry V. Levina6809652008-11-10 17:14:58 +0000572 strace_child = pid = fork();
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000573 if (pid < 0) {
Roland McGrath02203312007-06-11 22:06:31 +0000574 perror("strace: fork");
575 cleanup();
576 exit(1);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000577 }
578 if ((pid != 0 && daemonized_tracer) /* parent: to become a traced process */
579 || (pid == 0 && !daemonized_tracer) /* child: to become a traced process */
580 ) {
581 pid = getpid();
Roland McGrath02203312007-06-11 22:06:31 +0000582#ifdef USE_PROCFS
583 if (outf != stderr) close (fileno (outf));
584#ifdef MIPS
585 /* Kludge for SGI, see proc_open for details. */
586 sa.sa_handler = foobar;
587 sa.sa_flags = 0;
588 sigemptyset(&sa.sa_mask);
589 sigaction(SIGINT, &sa, NULL);
590#endif /* MIPS */
591#ifndef FREEBSD
592 pause();
593#else /* FREEBSD */
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000594 kill(pid, SIGSTOP); /* stop HERE */
Roland McGrath02203312007-06-11 22:06:31 +0000595#endif /* FREEBSD */
596#else /* !USE_PROCFS */
597 if (outf!=stderr)
598 close(fileno (outf));
599
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000600 if (!daemonized_tracer) {
601 if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
602 perror("strace: ptrace(PTRACE_TRACEME, ...)");
603 exit(1);
604 }
605 if (debug)
606 kill(pid, SIGSTOP);
Roland McGrath02203312007-06-11 22:06:31 +0000607 }
Roland McGrath02203312007-06-11 22:06:31 +0000608
609 if (username != NULL || geteuid() == 0) {
610 uid_t run_euid = run_uid;
611 gid_t run_egid = run_gid;
612
613 if (statbuf.st_mode & S_ISUID)
614 run_euid = statbuf.st_uid;
615 if (statbuf.st_mode & S_ISGID)
616 run_egid = statbuf.st_gid;
617
618 /*
619 * It is important to set groups before we
620 * lose privileges on setuid.
621 */
622 if (username != NULL) {
623 if (initgroups(username, run_gid) < 0) {
624 perror("initgroups");
625 exit(1);
626 }
627 if (setregid(run_gid, run_egid) < 0) {
628 perror("setregid");
629 exit(1);
630 }
631 if (setreuid(run_uid, run_euid) < 0) {
632 perror("setreuid");
633 exit(1);
634 }
635 }
636 }
637 else
638 setreuid(run_uid, run_uid);
639
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000640 if (!daemonized_tracer) {
641 /*
642 * Induce an immediate stop so that the parent
643 * will resume us with PTRACE_SYSCALL and display
644 * this execve call normally.
Mike Frysingerc1a5b7e2009-10-07 20:41:29 -0400645 * Unless of course we're on a no-MMU system where
646 * we vfork()-ed, so we cannot stop the child.
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000647 */
Mike Frysingerc1a5b7e2009-10-07 20:41:29 -0400648 if (!strace_vforked)
649 kill(getpid(), SIGSTOP);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000650 } else {
651 struct sigaction sv_sigchld;
652 sigaction(SIGCHLD, NULL, &sv_sigchld);
653 /*
654 * Make sure it is not SIG_IGN, otherwise wait
655 * will not block.
656 */
657 signal(SIGCHLD, SIG_DFL);
658 /*
659 * Wait for grandchild to attach to us.
660 * It kills child after that, and wait() unblocks.
661 */
662 alarm(3);
663 wait(NULL);
664 alarm(0);
665 sigaction(SIGCHLD, &sv_sigchld, NULL);
666 }
Roland McGrath02203312007-06-11 22:06:31 +0000667#endif /* !USE_PROCFS */
668
669 execv(pathname, argv);
670 perror("strace: exec");
671 _exit(1);
Roland McGrath02203312007-06-11 22:06:31 +0000672 }
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000673
674 /* We are the tracer. */
675 tcp = alloctcb(daemonized_tracer ? getppid() : pid);
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000676 if (daemonized_tracer) {
677 /* We want subsequent startup_attach() to attach to it. */
678 tcp->flags |= TCB_ATTACHED;
679 }
Roland McGrath02203312007-06-11 22:06:31 +0000680#ifdef USE_PROCFS
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000681 if (proc_open(tcp, 0) < 0) {
682 fprintf(stderr, "trouble opening proc file\n");
683 cleanup();
684 exit(1);
Roland McGrath02203312007-06-11 22:06:31 +0000685 }
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000686#endif /* USE_PROCFS */
Roland McGrath02203312007-06-11 22:06:31 +0000687}
688
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000689int
Dmitry V. Levin08b623e2007-10-08 21:04:41 +0000690main(int argc, char *argv[])
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000691{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000692 struct tcb *tcp;
693 int c, pid = 0;
Dmitry V. Levin06350db2008-07-25 15:42:34 +0000694 int optF = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000695 struct sigaction sa;
696
697 static char buf[BUFSIZ];
698
Dmitry V. Levin08b623e2007-10-08 21:04:41 +0000699 progname = argv[0] ? argv[0] : "strace";
700
Roland McGrathee9d4352002-12-18 04:16:10 +0000701 /* Allocate the initial tcbtab. */
702 tcbtabsize = argc; /* Surely enough for all -p args. */
Denys Vlasenko418d66a2009-01-17 01:52:54 +0000703 if ((tcbtab = calloc(tcbtabsize, sizeof tcbtab[0])) == NULL) {
Dmitry V. Levin08b623e2007-10-08 21:04:41 +0000704 fprintf(stderr, "%s: out of memory\n", progname);
705 exit(1);
706 }
Denys Vlasenko418d66a2009-01-17 01:52:54 +0000707 if ((tcbtab[0] = calloc(tcbtabsize, sizeof tcbtab[0][0])) == NULL) {
Dmitry V. Levin08b623e2007-10-08 21:04:41 +0000708 fprintf(stderr, "%s: out of memory\n", progname);
709 exit(1);
710 }
Roland McGrathee9d4352002-12-18 04:16:10 +0000711 for (tcp = tcbtab[0]; tcp < &tcbtab[0][tcbtabsize]; ++tcp)
712 tcbtab[tcp - tcbtab[0]] = &tcbtab[0][tcp - tcbtab[0]];
713
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000714 outf = stderr;
715 interactive = 1;
Roland McGrath138c6a32006-01-12 09:50:49 +0000716 set_sortby(DEFAULT_SORTBY);
717 set_personality(DEFAULT_PERSONALITY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000718 qualify("trace=all");
719 qualify("abbrev=all");
720 qualify("verbose=all");
721 qualify("signal=all");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000722 while ((c = getopt(argc, argv,
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +0000723 "+cCdfFhiqrtTvVxz"
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000724#ifndef USE_PROCFS
725 "D"
726#endif
727 "a:e:o:O:p:s:S:u:E:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000728 switch (c) {
729 case 'c':
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +0000730 if (cflag == CFLAG_BOTH) {
731 fprintf(stderr, "%s: -c and -C are mutually exclusive options\n",
732 progname);
733 exit(1);
734 }
735 cflag = CFLAG_ONLY_STATS;
736 break;
737 case 'C':
738 if (cflag == CFLAG_ONLY_STATS) {
739 fprintf(stderr, "%s: -c and -C are mutually exclusive options\n",
740 progname);
741 exit(1);
742 }
743 cflag = CFLAG_BOTH;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000744 break;
745 case 'd':
746 debug++;
747 break;
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000748#ifndef USE_PROCFS
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000749 case 'D':
750 daemonized_tracer = 1;
751 break;
752#endif
Roland McGrath41c48222008-07-18 00:25:10 +0000753 case 'F':
Dmitry V. Levin06350db2008-07-25 15:42:34 +0000754 optF = 1;
755 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000756 case 'f':
757 followfork++;
758 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000759 case 'h':
760 usage(stdout, 0);
761 break;
762 case 'i':
763 iflag++;
764 break;
765 case 'q':
766 qflag++;
767 break;
768 case 'r':
769 rflag++;
770 tflag++;
771 break;
772 case 't':
773 tflag++;
774 break;
775 case 'T':
776 dtime++;
777 break;
778 case 'x':
779 xflag++;
780 break;
781 case 'v':
782 qualify("abbrev=none");
783 break;
784 case 'V':
Roland McGrath9c9a2532003-02-20 02:56:29 +0000785 printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000786 exit(0);
787 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000788 case 'z':
789 not_failing_only = 1;
790 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000791 case 'a':
792 acolumn = atoi(optarg);
793 break;
794 case 'e':
795 qualify(optarg);
796 break;
797 case 'o':
798 outfname = strdup(optarg);
799 break;
800 case 'O':
801 set_overhead(atoi(optarg));
802 break;
803 case 'p':
Roland McGrathde6e5332003-01-24 04:31:23 +0000804 if ((pid = atoi(optarg)) <= 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000805 fprintf(stderr, "%s: Invalid process id: %s\n",
806 progname, optarg);
807 break;
808 }
809 if (pid == getpid()) {
Wichert Akkerman54a47671999-10-17 00:57:34 +0000810 fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000811 break;
812 }
Denys Vlasenko418d66a2009-01-17 01:52:54 +0000813 tcp = alloc_tcb(pid, 0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000814 tcp->flags |= TCB_ATTACHED;
815 pflag_seen++;
816 break;
817 case 's':
818 max_strlen = atoi(optarg);
Roland McGrathdccec722005-05-09 07:45:47 +0000819 if (max_strlen < 0) {
820 fprintf(stderr,
821 "%s: invalid -s argument: %s\n",
822 progname, optarg);
823 exit(1);
824 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000825 break;
826 case 'S':
827 set_sortby(optarg);
828 break;
829 case 'u':
830 username = strdup(optarg);
831 break;
Roland McGrathde6e5332003-01-24 04:31:23 +0000832 case 'E':
833 if (putenv(optarg) < 0) {
834 fprintf(stderr, "%s: out of memory\n",
835 progname);
836 exit(1);
837 }
838 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000839 default:
840 usage(stderr, 1);
841 break;
842 }
843 }
844
Roland McGrathd0c4c0c2006-04-25 07:39:40 +0000845 if ((optind == argc) == !pflag_seen)
Roland McGrathce0d1542003-11-11 21:24:23 +0000846 usage(stderr, 1);
847
Wang Chaod322a4b2010-08-05 14:30:11 +0800848 if (pflag_seen && daemonized_tracer) {
849 fprintf(stderr,
850 "%s: -D and -p are mutually exclusive options\n",
851 progname);
852 exit(1);
853 }
854
Dmitry V. Levin06350db2008-07-25 15:42:34 +0000855 if (!followfork)
856 followfork = optF;
857
Roland McGrathcb9def62006-04-25 07:48:03 +0000858 if (followfork > 1 && cflag) {
859 fprintf(stderr,
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +0000860 "%s: (-c or -C) and -ff are mutually exclusive options\n",
Roland McGrathcb9def62006-04-25 07:48:03 +0000861 progname);
862 exit(1);
863 }
864
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000865 /* See if they want to run as another user. */
866 if (username != NULL) {
867 struct passwd *pent;
868
869 if (getuid() != 0 || geteuid() != 0) {
870 fprintf(stderr,
871 "%s: you must be root to use the -u option\n",
872 progname);
873 exit(1);
874 }
875 if ((pent = getpwnam(username)) == NULL) {
876 fprintf(stderr, "%s: cannot find user `%s'\n",
Roland McGrath09553f82007-07-05 19:31:49 +0000877 progname, username);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000878 exit(1);
879 }
880 run_uid = pent->pw_uid;
881 run_gid = pent->pw_gid;
882 }
883 else {
884 run_uid = getuid();
885 run_gid = getgid();
886 }
887
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000888 /* Check if they want to redirect the output. */
889 if (outfname) {
Roland McGrath37b9a662003-11-07 02:26:54 +0000890 /* See if they want to pipe the output. */
891 if (outfname[0] == '|' || outfname[0] == '!') {
892 /*
893 * We can't do the <outfname>.PID funny business
894 * when using popen, so prohibit it.
895 */
896 if (followfork > 1) {
897 fprintf(stderr, "\
898%s: piping the output and -ff are mutually exclusive options\n",
899 progname);
900 exit(1);
901 }
902
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000903 if ((outf = strace_popen(outfname + 1)) == NULL)
Roland McGrath37b9a662003-11-07 02:26:54 +0000904 exit(1);
Roland McGrath37b9a662003-11-07 02:26:54 +0000905 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +0000906 else if (followfork <= 1 &&
907 (outf = strace_fopen(outfname, "w")) == NULL)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000908 exit(1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000909 }
910
Roland McGrath37b9a662003-11-07 02:26:54 +0000911 if (!outfname || outfname[0] == '|' || outfname[0] == '!')
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000912 setvbuf(outf, buf, _IOLBF, BUFSIZ);
Roland McGrath37b9a662003-11-07 02:26:54 +0000913 if (outfname && optind < argc) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000914 interactive = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000915 qflag = 1;
Roland McGrath36931052003-06-03 01:35:20 +0000916 }
Roland McGrath54cc1c82007-11-03 23:34:11 +0000917 /* Valid states here:
918 optind < argc pflag_seen outfname interactive
919 1 0 0 1
920 0 1 0 1
921 1 0 1 0
922 0 1 1 1
923 */
924
925 /* STARTUP_CHILD must be called before the signal handlers get
926 installed below as they are inherited into the spawned process.
927 Also we do not need to be protected by them as during interruption
928 in the STARTUP_CHILD mode we kill the spawned process anyway. */
929 if (!pflag_seen)
930 startup_child(&argv[optind]);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000931
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000932 sigemptyset(&empty_set);
933 sigemptyset(&blocked_set);
934 sa.sa_handler = SIG_IGN;
935 sigemptyset(&sa.sa_mask);
936 sa.sa_flags = 0;
937 sigaction(SIGTTOU, &sa, NULL);
938 sigaction(SIGTTIN, &sa, NULL);
939 if (interactive) {
940 sigaddset(&blocked_set, SIGHUP);
941 sigaddset(&blocked_set, SIGINT);
942 sigaddset(&blocked_set, SIGQUIT);
943 sigaddset(&blocked_set, SIGPIPE);
944 sigaddset(&blocked_set, SIGTERM);
945 sa.sa_handler = interrupt;
946#ifdef SUNOS4
947 /* POSIX signals on sunos4.1 are a little broken. */
948 sa.sa_flags = SA_INTERRUPT;
949#endif /* SUNOS4 */
950 }
951 sigaction(SIGHUP, &sa, NULL);
952 sigaction(SIGINT, &sa, NULL);
953 sigaction(SIGQUIT, &sa, NULL);
954 sigaction(SIGPIPE, &sa, NULL);
955 sigaction(SIGTERM, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000956#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000957 sa.sa_handler = reaper;
958 sigaction(SIGCHLD, &sa, NULL);
Roland McGrath553a6092002-12-16 20:40:39 +0000959#else
960 /* Make sure SIGCHLD has the default action so that waitpid
961 definitely works without losing track of children. The user
962 should not have given us a bogus state to inherit, but he might
963 have. Arguably we should detect SIG_IGN here and pass it on
964 to children, but probably noone really needs that. */
965 sa.sa_handler = SIG_DFL;
966 sigaction(SIGCHLD, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000967#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000968
Denys Vlasenkoecfe2f12008-12-30 20:51:30 +0000969 if (pflag_seen || daemonized_tracer)
Roland McGrath02203312007-06-11 22:06:31 +0000970 startup_attach();
Roland McGrath02203312007-06-11 22:06:31 +0000971
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000972 if (trace() < 0)
973 exit(1);
974 cleanup();
Dmitry V. Levina6809652008-11-10 17:14:58 +0000975 fflush(NULL);
976 if (exit_code > 0xff) {
977 /* Child was killed by a signal, mimic that. */
978 exit_code &= 0xff;
979 signal(exit_code, SIG_DFL);
980 raise(exit_code);
981 /* Paranoia - what if this signal is not fatal?
982 Exit with 128 + signo then. */
983 exit_code += 128;
984 }
985 exit(exit_code);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000986}
987
Denys Vlasenko418d66a2009-01-17 01:52:54 +0000988void
989expand_tcbtab(void)
Roland McGrath7b54a7a2004-06-04 01:50:45 +0000990{
991 /* Allocate some more TCBs and expand the table.
992 We don't want to relocate the TCBs because our
993 callers have pointers and it would be a pain.
994 So tcbtab is a table of pointers. Since we never
995 free the TCBs, we allocate a single chunk of many. */
996 struct tcb **newtab = (struct tcb **)
997 realloc(tcbtab, 2 * tcbtabsize * sizeof tcbtab[0]);
998 struct tcb *newtcbs = (struct tcb *) calloc(tcbtabsize,
999 sizeof *newtcbs);
1000 int i;
1001 if (newtab == NULL || newtcbs == NULL) {
Dmitry V. Levin76860f62006-10-11 22:55:25 +00001002 fprintf(stderr, "%s: expand_tcbtab: out of memory\n",
1003 progname);
Denys Vlasenko418d66a2009-01-17 01:52:54 +00001004 cleanup();
1005 exit(1);
Roland McGrath7b54a7a2004-06-04 01:50:45 +00001006 }
1007 for (i = tcbtabsize; i < 2 * tcbtabsize; ++i)
1008 newtab[i] = &newtcbs[i - tcbtabsize];
1009 tcbtabsize *= 2;
1010 tcbtab = newtab;
Roland McGrath7b54a7a2004-06-04 01:50:45 +00001011}
1012
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001013struct tcb *
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001014alloc_tcb(int pid, int command_options_parsed)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001015{
1016 int i;
1017 struct tcb *tcp;
1018
Denys Vlasenko418d66a2009-01-17 01:52:54 +00001019 if (nprocs == tcbtabsize)
1020 expand_tcbtab();
1021
Roland McGrathee9d4352002-12-18 04:16:10 +00001022 for (i = 0; i < tcbtabsize; i++) {
1023 tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001024 if ((tcp->flags & TCB_INUSE) == 0) {
1025 tcp->pid = pid;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001026 tcp->parent = NULL;
1027 tcp->nchildren = 0;
1028 tcp->nzombies = 0;
1029#ifdef TCB_CLONE_THREAD
Wang Chao21b8db42010-08-27 17:43:16 +08001030 tcp->nclone_threads = 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001031 tcp->nclone_waiting = 0;
1032#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001033 tcp->flags = TCB_INUSE | TCB_STARTUP;
1034 tcp->outf = outf; /* Initialise to current out file */
Andreas Schwabccdff482009-10-27 16:27:13 +01001035 tcp->curcol = 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -07001036 tcp->stime.tv_sec = 0;
1037 tcp->stime.tv_usec = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001038 tcp->pfd = -1;
1039 nprocs++;
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001040 if (command_options_parsed)
1041 newoutf(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001042 return tcp;
1043 }
1044 }
Denys Vlasenko418d66a2009-01-17 01:52:54 +00001045 fprintf(stderr, "%s: bug in alloc_tcb\n", progname);
1046 cleanup();
1047 exit(1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001048}
1049
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001050#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001051int
Denys Vlasenko418d66a2009-01-17 01:52:54 +00001052proc_open(struct tcb *tcp, int attaching)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001053{
1054 char proc[32];
1055 long arg;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001056#ifdef SVR4
John Hughes19e49982001-10-19 08:59:12 +00001057 int i;
1058 sysset_t syscalls;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001059 sigset_t signals;
1060 fltset_t faults;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001061#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001062#ifndef HAVE_POLLABLE_PROCFS
1063 static int last_pfd;
1064#endif
1065
Wichert Akkermanea78f0f1999-11-29 15:34:02 +00001066#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001067 /* Open the process pseudo-files in /proc. */
1068 sprintf(proc, "/proc/%d/ctl", tcp->pid);
1069 if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001070 perror("strace: open(\"/proc/...\", ...)");
1071 return -1;
1072 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001073 if (set_cloexec_flag(tcp->pfd) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001074 return -1;
1075 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001076 sprintf(proc, "/proc/%d/status", tcp->pid);
1077 if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
1078 perror("strace: open(\"/proc/...\", ...)");
1079 return -1;
1080 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001081 if (set_cloexec_flag(tcp->pfd_stat) < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001082 return -1;
1083 }
1084 sprintf(proc, "/proc/%d/as", tcp->pid);
1085 if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
1086 perror("strace: open(\"/proc/...\", ...)");
1087 return -1;
1088 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001089 if (set_cloexec_flag(tcp->pfd_as) < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001090 return -1;
1091 }
1092#else
1093 /* Open the process pseudo-file in /proc. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001094#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001095 sprintf(proc, "/proc/%d", tcp->pid);
Andreas Schwab372cc842010-07-09 11:49:27 +02001096 tcp->pfd = open(proc, O_RDWR|O_EXCL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001097#else /* FREEBSD */
1098 sprintf(proc, "/proc/%d/mem", tcp->pid);
Andreas Schwab372cc842010-07-09 11:49:27 +02001099 tcp->pfd = open(proc, O_RDWR);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001100#endif /* FREEBSD */
Andreas Schwab372cc842010-07-09 11:49:27 +02001101 if (tcp->pfd < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001102 perror("strace: open(\"/proc/...\", ...)");
1103 return -1;
1104 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001105 if (set_cloexec_flag(tcp->pfd) < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001106 return -1;
1107 }
1108#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001109#ifdef FREEBSD
1110 sprintf(proc, "/proc/%d/regs", tcp->pid);
1111 if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) {
1112 perror("strace: open(\"/proc/.../regs\", ...)");
1113 return -1;
1114 }
1115 if (cflag) {
1116 sprintf(proc, "/proc/%d/status", tcp->pid);
1117 if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) {
1118 perror("strace: open(\"/proc/.../status\", ...)");
1119 return -1;
1120 }
1121 } else
1122 tcp->pfd_status = -1;
1123#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001124 rebuild_pollv();
1125 if (!attaching) {
1126 /*
1127 * Wait for the child to pause. Because of a race
1128 * condition we have to poll for the event.
1129 */
1130 for (;;) {
1131 if (IOCTL_STATUS (tcp) < 0) {
1132 perror("strace: PIOCSTATUS");
1133 return -1;
1134 }
1135 if (tcp->status.PR_FLAGS & PR_ASLEEP)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001136 break;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001137 }
1138 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001139#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001140 /* Stop the process so that we own the stop. */
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001141 if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001142 perror("strace: PIOCSTOP");
1143 return -1;
1144 }
Roland McGrath553a6092002-12-16 20:40:39 +00001145#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001146#ifdef PIOCSET
1147 /* Set Run-on-Last-Close. */
1148 arg = PR_RLC;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001149 if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001150 perror("PIOCSET PR_RLC");
1151 return -1;
1152 }
1153 /* Set or Reset Inherit-on-Fork. */
1154 arg = PR_FORK;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001155 if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001156 perror("PIOC{SET,RESET} PR_FORK");
1157 return -1;
1158 }
1159#else /* !PIOCSET */
Roland McGrath553a6092002-12-16 20:40:39 +00001160#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001161 if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
1162 perror("PIOCSRLC");
1163 return -1;
1164 }
1165 if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
1166 perror("PIOC{S,R}FORK");
1167 return -1;
1168 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001169#else /* FREEBSD */
1170 /* just unset the PF_LINGER flag for the Run-on-Last-Close. */
1171 if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) {
1172 perror("PIOCGFL");
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001173 return -1;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001174 }
1175 arg &= ~PF_LINGER;
1176 if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001177 perror("PIOCSFL");
1178 return -1;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001179 }
1180#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001181#endif /* !PIOCSET */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001182#ifndef FREEBSD
John Hughes19e49982001-10-19 08:59:12 +00001183 /* Enable all syscall entries we care about. */
1184 premptyset(&syscalls);
1185 for (i = 1; i < MAX_QUALS; ++i) {
1186 if (i > (sizeof syscalls) * CHAR_BIT) break;
1187 if (qual_flags [i] & QUAL_TRACE) praddset (&syscalls, i);
1188 }
1189 praddset (&syscalls, SYS_execve);
1190 if (followfork) {
1191 praddset (&syscalls, SYS_fork);
1192#ifdef SYS_forkall
1193 praddset (&syscalls, SYS_forkall);
1194#endif
Roland McGrath553a6092002-12-16 20:40:39 +00001195#ifdef SYS_fork1
John Hughes19e49982001-10-19 08:59:12 +00001196 praddset (&syscalls, SYS_fork1);
1197#endif
1198#ifdef SYS_rfork1
1199 praddset (&syscalls, SYS_rfork1);
1200#endif
1201#ifdef SYS_rforkall
1202 praddset (&syscalls, SYS_rforkall);
1203#endif
1204 }
1205 if (IOCTL(tcp->pfd, PIOCSENTRY, &syscalls) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001206 perror("PIOCSENTRY");
1207 return -1;
1208 }
John Hughes19e49982001-10-19 08:59:12 +00001209 /* Enable the syscall exits. */
1210 if (IOCTL(tcp->pfd, PIOCSEXIT, &syscalls) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001211 perror("PIOSEXIT");
1212 return -1;
1213 }
John Hughes19e49982001-10-19 08:59:12 +00001214 /* Enable signals we care about. */
1215 premptyset(&signals);
1216 for (i = 1; i < MAX_QUALS; ++i) {
1217 if (i > (sizeof signals) * CHAR_BIT) break;
1218 if (qual_flags [i] & QUAL_SIGNAL) praddset (&signals, i);
1219 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001220 if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001221 perror("PIOCSTRACE");
1222 return -1;
1223 }
John Hughes19e49982001-10-19 08:59:12 +00001224 /* Enable faults we care about */
1225 premptyset(&faults);
1226 for (i = 1; i < MAX_QUALS; ++i) {
1227 if (i > (sizeof faults) * CHAR_BIT) break;
1228 if (qual_flags [i] & QUAL_FAULT) praddset (&faults, i);
1229 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001230 if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001231 perror("PIOCSFAULT");
1232 return -1;
1233 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001234#else /* FREEBSD */
1235 /* set events flags. */
1236 arg = S_SIG | S_SCE | S_SCX ;
1237 if(ioctl(tcp->pfd, PIOCBIS, arg) < 0) {
1238 perror("PIOCBIS");
1239 return -1;
1240 }
1241#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001242 if (!attaching) {
1243#ifdef MIPS
1244 /*
1245 * The SGI PRSABORT doesn't work for pause() so
1246 * we send it a caught signal to wake it up.
1247 */
1248 kill(tcp->pid, SIGINT);
1249#else /* !MIPS */
Roland McGrath553a6092002-12-16 20:40:39 +00001250#ifdef PRSABORT
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001251 /* The child is in a pause(), abort it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001252 arg = PRSABORT;
1253 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001254 perror("PIOCRUN");
1255 return -1;
1256 }
Roland McGrath553a6092002-12-16 20:40:39 +00001257#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001258#endif /* !MIPS*/
1259#ifdef FREEBSD
1260 /* wake up the child if it received the SIGSTOP */
1261 kill(tcp->pid, SIGCONT);
Roland McGrath553a6092002-12-16 20:40:39 +00001262#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001263 for (;;) {
1264 /* Wait for the child to do something. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001265 if (IOCTL_WSTOP (tcp) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001266 perror("PIOCWSTOP");
1267 return -1;
1268 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001269 if (tcp->status.PR_WHY == PR_SYSENTRY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001270 tcp->flags &= ~TCB_INSYSCALL;
1271 get_scno(tcp);
Roland McGrath76989d72005-06-07 23:21:31 +00001272 if (known_scno(tcp) == SYS_execve)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001273 break;
1274 }
1275 /* Set it running: maybe execve will be next. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001276#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001277 arg = 0;
1278 if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001279#else /* FREEBSD */
1280 if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) {
Roland McGrath553a6092002-12-16 20:40:39 +00001281#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001282 perror("PIOCRUN");
1283 return -1;
1284 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001285#ifdef FREEBSD
1286 /* handle the case where we "opened" the child before
1287 it did the kill -STOP */
1288 if (tcp->status.PR_WHY == PR_SIGNALLED &&
1289 tcp->status.PR_WHAT == SIGSTOP)
1290 kill(tcp->pid, SIGCONT);
Roland McGrath553a6092002-12-16 20:40:39 +00001291#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001292 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001293#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001294 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001295#else /* FREEBSD */
1296 } else {
Roland McGrath553a6092002-12-16 20:40:39 +00001297 if (attaching < 2) {
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +00001298 /* We are attaching to an already running process.
1299 * Try to figure out the state of the process in syscalls,
1300 * to handle the first event well.
1301 * This is done by having a look at the "wchan" property of the
1302 * process, which tells where it is stopped (if it is). */
1303 FILE * status;
1304 char wchan[20]; /* should be enough */
Roland McGrath553a6092002-12-16 20:40:39 +00001305
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +00001306 sprintf(proc, "/proc/%d/status", tcp->pid);
1307 status = fopen(proc, "r");
1308 if (status &&
1309 (fscanf(status, "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d"
1310 "%*d,%*d %*d,%*d %19s", wchan) == 1) &&
1311 strcmp(wchan, "nochan") && strcmp(wchan, "spread") &&
1312 strcmp(wchan, "stopevent")) {
1313 /* The process is asleep in the middle of a syscall.
1314 Fake the syscall entry event */
1315 tcp->flags &= ~(TCB_INSYSCALL|TCB_STARTUP);
1316 tcp->status.PR_WHY = PR_SYSENTRY;
1317 trace_syscall(tcp);
1318 }
1319 if (status)
1320 fclose(status);
1321 } /* otherwise it's a fork being followed */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001322 }
1323#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001324#ifndef HAVE_POLLABLE_PROCFS
1325 if (proc_poll_pipe[0] != -1)
1326 proc_poller(tcp->pfd);
1327 else if (nprocs > 1) {
1328 proc_poll_open();
1329 proc_poller(last_pfd);
1330 proc_poller(tcp->pfd);
1331 }
1332 last_pfd = tcp->pfd;
1333#endif /* !HAVE_POLLABLE_PROCFS */
1334 return 0;
1335}
1336
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001337#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001338
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001339struct tcb *
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001340pid2tcb(pid)
1341int pid;
1342{
1343 int i;
1344 struct tcb *tcp;
1345
Roland McGrathee9d4352002-12-18 04:16:10 +00001346 for (i = 0; i < tcbtabsize; i++) {
1347 tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001348 if (pid && tcp->pid != pid)
1349 continue;
1350 if (tcp->flags & TCB_INUSE)
1351 return tcp;
1352 }
1353 return NULL;
1354}
1355
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001356#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001357
1358static struct tcb *
1359pfd2tcb(pfd)
1360int pfd;
1361{
1362 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001363
Roland McGrathca16be82003-01-10 19:55:28 +00001364 for (i = 0; i < tcbtabsize; i++) {
1365 struct tcb *tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001366 if (tcp->pfd != pfd)
1367 continue;
1368 if (tcp->flags & TCB_INUSE)
1369 return tcp;
1370 }
1371 return NULL;
1372}
1373
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001374#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001375
1376void
1377droptcb(tcp)
1378struct tcb *tcp;
1379{
1380 if (tcp->pid == 0)
1381 return;
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001382#ifdef TCB_CLONE_THREAD
1383 if (tcp->nclone_threads > 0) {
1384 /* There are other threads left in this process, but this
1385 is the one whose PID represents the whole process.
1386 We need to keep this record around as a zombie until
1387 all the threads die. */
1388 tcp->flags |= TCB_EXITING;
1389 return;
1390 }
1391#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001392 nprocs--;
1393 tcp->pid = 0;
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +00001394
Roland McGrathe29341c2003-01-10 20:14:20 +00001395 if (tcp->parent != NULL) {
1396 tcp->parent->nchildren--;
1397#ifdef TCB_CLONE_THREAD
Roland McGrathe29341c2003-01-10 20:14:20 +00001398 if (tcp->flags & TCB_CLONE_THREAD)
1399 tcp->parent->nclone_threads--;
1400#endif
Wang Chao21b8db42010-08-27 17:43:16 +08001401 tcp->parent->nzombies++;
Roland McGrath276ceb32007-11-13 08:12:12 +00001402#ifdef LINUX
1403 /* Update `tcp->parent->parent->nchildren' and the other fields
1404 like NCLONE_DETACHED, only for zombie group leader that has
1405 already reported and been short-circuited at the top of this
1406 function. The same condition as at the top of DETACH. */
1407 if ((tcp->flags & TCB_CLONE_THREAD) &&
1408 tcp->parent->nclone_threads == 0 &&
1409 (tcp->parent->flags & TCB_EXITING))
1410 droptcb(tcp->parent);
1411#endif
Roland McGrathe29341c2003-01-10 20:14:20 +00001412 tcp->parent = NULL;
1413 }
1414
1415 tcp->flags = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001416 if (tcp->pfd != -1) {
1417 close(tcp->pfd);
1418 tcp->pfd = -1;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001419#ifdef FREEBSD
1420 if (tcp->pfd_reg != -1) {
1421 close(tcp->pfd_reg);
1422 tcp->pfd_reg = -1;
1423 }
1424 if (tcp->pfd_status != -1) {
1425 close(tcp->pfd_status);
1426 tcp->pfd_status = -1;
1427 }
Roland McGrath553a6092002-12-16 20:40:39 +00001428#endif /* !FREEBSD */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001429#ifdef USE_PROCFS
Roland McGrathe29341c2003-01-10 20:14:20 +00001430 rebuild_pollv(); /* Note, flags needs to be cleared by now. */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001431#endif
1432 }
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +00001433
Wichert Akkerman822f0c92002-04-03 10:55:14 +00001434 if (outfname && followfork > 1 && tcp->outf)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001435 fclose(tcp->outf);
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +00001436
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001437 tcp->outf = 0;
1438}
1439
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001440#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001441
1442static int
1443resume(tcp)
1444struct tcb *tcp;
1445{
1446 if (tcp == NULL)
1447 return -1;
1448
1449 if (!(tcp->flags & TCB_SUSPENDED)) {
1450 fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid);
1451 return -1;
1452 }
1453 tcp->flags &= ~TCB_SUSPENDED;
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001454#ifdef TCB_CLONE_THREAD
1455 if (tcp->flags & TCB_CLONE_THREAD)
1456 tcp->parent->nclone_waiting--;
1457#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001458
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00001459 if (ptrace_restart(PTRACE_SYSCALL, tcp, 0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001460 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001461
1462 if (!qflag)
1463 fprintf(stderr, "Process %u resumed\n", tcp->pid);
1464 return 0;
1465}
1466
Roland McGrath1bfd3102007-08-03 10:02:00 +00001467static int
1468resume_from_tcp (struct tcb *tcp)
1469{
1470 int error = 0;
1471 int resumed = 0;
1472
1473 /* XXX This won't always be quite right (but it never was).
1474 A waiter with argument 0 or < -1 is waiting for any pid in
1475 a particular pgrp, which this child might or might not be
1476 in. The waiter will only wake up if it's argument is -1
1477 or if it's waiting for tcp->pid's pgrp. It makes a
1478 difference to wake up a waiter when there might be more
1479 traced children, because it could get a false ECHILD
1480 error. OTOH, if this was the last child in the pgrp, then
1481 it ought to wake up and get ECHILD. We would have to
1482 search the system for all pid's in the pgrp to be sure.
1483
1484 && (t->waitpid == -1 ||
1485 (t->waitpid == 0 && getpgid (tcp->pid) == getpgid (t->pid))
1486 || (t->waitpid < 0 && t->waitpid == -getpid (t->pid)))
1487 */
1488
1489 if (tcp->parent &&
1490 (tcp->parent->flags & TCB_SUSPENDED) &&
1491 (tcp->parent->waitpid <= 0 || tcp->parent->waitpid == tcp->pid)) {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001492 error = resume(tcp->parent);
Roland McGrath1bfd3102007-08-03 10:02:00 +00001493 ++resumed;
1494 }
1495#ifdef TCB_CLONE_THREAD
1496 if (tcp->parent && tcp->parent->nclone_waiting > 0) {
1497 /* Some other threads of our parent are waiting too. */
1498 unsigned int i;
1499
1500 /* Resume all the threads that were waiting for this PID. */
1501 for (i = 0; i < tcbtabsize; i++) {
1502 struct tcb *t = tcbtab[i];
1503 if (t->parent == tcp->parent && t != tcp
1504 && ((t->flags & (TCB_CLONE_THREAD|TCB_SUSPENDED))
1505 == (TCB_CLONE_THREAD|TCB_SUSPENDED))
1506 && t->waitpid == tcp->pid) {
1507 error |= resume (t);
1508 ++resumed;
1509 }
1510 }
1511 if (resumed == 0)
1512 /* Noone was waiting for this PID in particular,
1513 so now we might need to resume some wildcarders. */
1514 for (i = 0; i < tcbtabsize; i++) {
1515 struct tcb *t = tcbtab[i];
1516 if (t->parent == tcp->parent && t != tcp
1517 && ((t->flags
1518 & (TCB_CLONE_THREAD|TCB_SUSPENDED))
1519 == (TCB_CLONE_THREAD|TCB_SUSPENDED))
1520 && t->waitpid <= 0
1521 ) {
1522 error |= resume (t);
1523 break;
1524 }
1525 }
1526 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001527#endif
Roland McGrath1bfd3102007-08-03 10:02:00 +00001528
1529 return error;
1530}
Roland McGrath1bfd3102007-08-03 10:02:00 +00001531
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001532#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001533
Roland McGrath0a463882007-07-05 18:43:16 +00001534/* detach traced process; continue with sig
1535 Never call DETACH twice on the same process as both unattached and
1536 attached-unstopped processes give the same ESRCH. For unattached process we
1537 would SIGSTOP it and wait for its SIGSTOP notification forever. */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001538
1539static int
1540detach(tcp, sig)
1541struct tcb *tcp;
1542int sig;
1543{
1544 int error = 0;
Roland McGrathca16be82003-01-10 19:55:28 +00001545#ifdef LINUX
Roland McGrath1bfd3102007-08-03 10:02:00 +00001546 int status, catch_sigstop;
Roland McGratha08a97e2005-08-03 11:23:46 +00001547 struct tcb *zombie = NULL;
1548
1549 /* If the group leader is lingering only because of this other
1550 thread now dying, then detach the leader as well. */
1551 if ((tcp->flags & TCB_CLONE_THREAD) &&
1552 tcp->parent->nclone_threads == 1 &&
1553 (tcp->parent->flags & TCB_EXITING))
1554 zombie = tcp->parent;
Roland McGrathca16be82003-01-10 19:55:28 +00001555#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001556
1557 if (tcp->flags & TCB_BPTSET)
Andreas Schwab840d85b2010-01-12 11:16:32 +01001558 clearbpt(tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001559
1560#ifdef LINUX
1561 /*
1562 * Linux wrongly insists the child be stopped
Roland McGrath7bf10472002-12-16 20:42:50 +00001563 * before detaching. Arghh. We go through hoops
1564 * to make a clean break of things.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001565 */
Roland McGrath7bf10472002-12-16 20:42:50 +00001566#if defined(SPARC)
1567#undef PTRACE_DETACH
1568#define PTRACE_DETACH PTRACE_SUNDETACH
1569#endif
Roland McGrath02203312007-06-11 22:06:31 +00001570 /*
1571 * On TCB_STARTUP we did PTRACE_ATTACH but still did not get the
1572 * expected SIGSTOP. We must catch exactly one as otherwise the
1573 * detached process would be left stopped (process state T).
1574 */
1575 catch_sigstop = (tcp->flags & TCB_STARTUP);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001576 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
1577 /* On a clear day, you can see forever. */
Roland McGrath7bf10472002-12-16 20:42:50 +00001578 }
1579 else if (errno != ESRCH) {
1580 /* Shouldn't happen. */
1581 perror("detach: ptrace(PTRACE_DETACH, ...)");
1582 }
Roland McGrath134813a2007-06-02 00:07:33 +00001583 else if (my_tgkill((tcp->flags & TCB_CLONE_THREAD ? tcp->parent->pid
1584 : tcp->pid),
1585 tcp->pid, 0) < 0) {
Roland McGrath7bf10472002-12-16 20:42:50 +00001586 if (errno != ESRCH)
1587 perror("detach: checking sanity");
1588 }
Roland McGrath02203312007-06-11 22:06:31 +00001589 else if (!catch_sigstop && my_tgkill((tcp->flags & TCB_CLONE_THREAD
1590 ? tcp->parent->pid : tcp->pid),
1591 tcp->pid, SIGSTOP) < 0) {
Roland McGrath7bf10472002-12-16 20:42:50 +00001592 if (errno != ESRCH)
1593 perror("detach: stopping child");
1594 }
Roland McGrath02203312007-06-11 22:06:31 +00001595 else
1596 catch_sigstop = 1;
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00001597 if (catch_sigstop) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001598 for (;;) {
Roland McGrath7508cb42002-12-17 10:48:05 +00001599#ifdef __WALL
1600 if (wait4(tcp->pid, &status, __WALL, NULL) < 0) {
1601 if (errno == ECHILD) /* Already gone. */
1602 break;
1603 if (errno != EINVAL) {
Roland McGrath553a6092002-12-16 20:40:39 +00001604 perror("detach: waiting");
Roland McGrath7508cb42002-12-17 10:48:05 +00001605 break;
1606 }
1607#endif /* __WALL */
1608 /* No __WALL here. */
1609 if (waitpid(tcp->pid, &status, 0) < 0) {
1610 if (errno != ECHILD) {
1611 perror("detach: waiting");
1612 break;
1613 }
1614#ifdef __WCLONE
1615 /* If no processes, try clones. */
1616 if (wait4(tcp->pid, &status, __WCLONE,
1617 NULL) < 0) {
1618 if (errno != ECHILD)
1619 perror("detach: waiting");
1620 break;
1621 }
1622#endif /* __WCLONE */
1623 }
1624#ifdef __WALL
Roland McGrath553a6092002-12-16 20:40:39 +00001625 }
Roland McGrath7508cb42002-12-17 10:48:05 +00001626#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001627 if (!WIFSTOPPED(status)) {
1628 /* Au revoir, mon ami. */
1629 break;
1630 }
1631 if (WSTOPSIG(status) == SIGSTOP) {
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00001632 ptrace_restart(PTRACE_DETACH, tcp, sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001633 break;
1634 }
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00001635 error = ptrace_restart(PTRACE_CONT, tcp,
Roland McGratheb9e2e82009-06-02 16:49:22 -07001636 WSTOPSIG(status) == SIGTRAP ? 0
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00001637 : WSTOPSIG(status));
1638 if (error < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001639 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001640 }
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00001641 }
Roland McGrath7bf10472002-12-16 20:42:50 +00001642#endif /* LINUX */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001643
1644#if defined(SUNOS4)
1645 /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
1646 if (sig && kill(tcp->pid, sig) < 0)
1647 perror("detach: kill");
1648 sig = 0;
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00001649 error = ptrace_restart(PTRACE_DETACH, tcp, sig);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001650#endif /* SUNOS4 */
1651
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001652#ifndef USE_PROCFS
Roland McGrath1bfd3102007-08-03 10:02:00 +00001653 error |= resume_from_tcp (tcp);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00001654#endif
1655
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001656 if (!qflag)
1657 fprintf(stderr, "Process %u detached\n", tcp->pid);
1658
1659 droptcb(tcp);
Roland McGratha08a97e2005-08-03 11:23:46 +00001660
1661#ifdef LINUX
Roland McGrath0a463882007-07-05 18:43:16 +00001662 if (zombie != NULL) {
1663 /* TCP no longer exists therefore you must not detach () it. */
1664 droptcb(zombie);
1665 }
Roland McGratha08a97e2005-08-03 11:23:46 +00001666#endif
1667
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001668 return error;
1669}
1670
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001671#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001672
Dmitry V. Levine5e60852009-12-31 22:50:49 +00001673static void reaper(int sig)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001674{
1675 int pid;
1676 int status;
1677
1678 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001679 }
1680}
1681
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001682#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001683
1684static void
1685cleanup()
1686{
1687 int i;
1688 struct tcb *tcp;
1689
Roland McGrathee9d4352002-12-18 04:16:10 +00001690 for (i = 0; i < tcbtabsize; i++) {
1691 tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001692 if (!(tcp->flags & TCB_INUSE))
1693 continue;
1694 if (debug)
1695 fprintf(stderr,
1696 "cleanup: looking at pid %u\n", tcp->pid);
1697 if (tcp_last &&
1698 (!outfname || followfork < 2 || tcp_last == tcp)) {
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00001699 tprintf(" <unfinished ...>");
1700 printtrailer();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001701 }
1702 if (tcp->flags & TCB_ATTACHED)
1703 detach(tcp, 0);
1704 else {
1705 kill(tcp->pid, SIGCONT);
1706 kill(tcp->pid, SIGTERM);
1707 }
1708 }
1709 if (cflag)
1710 call_summary(outf);
1711}
1712
1713static void
1714interrupt(sig)
1715int sig;
1716{
1717 interrupted = 1;
1718}
1719
1720#ifndef HAVE_STRERROR
1721
Roland McGrath6d2b3492002-12-30 00:51:30 +00001722#if !HAVE_DECL_SYS_ERRLIST
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001723extern int sys_nerr;
1724extern char *sys_errlist[];
Roland McGrath6d2b3492002-12-30 00:51:30 +00001725#endif /* HAVE_DECL_SYS_ERRLIST */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001726
1727const char *
1728strerror(errno)
1729int errno;
1730{
1731 static char buf[64];
1732
1733 if (errno < 1 || errno >= sys_nerr) {
1734 sprintf(buf, "Unknown error %d", errno);
1735 return buf;
1736 }
1737 return sys_errlist[errno];
1738}
1739
1740#endif /* HAVE_STERRROR */
1741
1742#ifndef HAVE_STRSIGNAL
1743
Roland McGrath8f474e02003-01-14 07:53:33 +00001744#if defined HAVE_SYS_SIGLIST && !defined HAVE_DECL_SYS_SIGLIST
Roland McGrath6d2b3492002-12-30 00:51:30 +00001745extern char *sys_siglist[];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001746#endif
Roland McGrath8f474e02003-01-14 07:53:33 +00001747#if defined HAVE_SYS__SIGLIST && !defined HAVE_DECL__SYS_SIGLIST
1748extern char *_sys_siglist[];
1749#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001750
1751const char *
1752strsignal(sig)
1753int sig;
1754{
1755 static char buf[64];
1756
1757 if (sig < 1 || sig >= NSIG) {
1758 sprintf(buf, "Unknown signal %d", sig);
1759 return buf;
1760 }
1761#ifdef HAVE__SYS_SIGLIST
1762 return _sys_siglist[sig];
1763#else
1764 return sys_siglist[sig];
1765#endif
1766}
1767
1768#endif /* HAVE_STRSIGNAL */
1769
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001770#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001771
1772static void
1773rebuild_pollv()
1774{
1775 int i, j;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001776
Roland McGrathee9d4352002-12-18 04:16:10 +00001777 if (pollv != NULL)
1778 free (pollv);
Roland McGrathc012d222003-01-10 20:05:56 +00001779 pollv = (struct pollfd *) malloc(nprocs * sizeof pollv[0]);
Roland McGrathee9d4352002-12-18 04:16:10 +00001780 if (pollv == NULL) {
Roland McGrath46100d02005-06-01 18:55:42 +00001781 fprintf(stderr, "%s: out of memory\n", progname);
Roland McGrathee9d4352002-12-18 04:16:10 +00001782 exit(1);
1783 }
1784
Roland McGrathca16be82003-01-10 19:55:28 +00001785 for (i = j = 0; i < tcbtabsize; i++) {
1786 struct tcb *tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001787 if (!(tcp->flags & TCB_INUSE))
1788 continue;
1789 pollv[j].fd = tcp->pfd;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001790 pollv[j].events = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001791 j++;
1792 }
1793 if (j != nprocs) {
1794 fprintf(stderr, "strace: proc miscount\n");
1795 exit(1);
1796 }
1797}
1798
1799#ifndef HAVE_POLLABLE_PROCFS
1800
1801static void
1802proc_poll_open()
1803{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001804 int i;
1805
1806 if (pipe(proc_poll_pipe) < 0) {
1807 perror("pipe");
1808 exit(1);
1809 }
1810 for (i = 0; i < 2; i++) {
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00001811 if (set_cloexec_flag(proc_poll_pipe[i]) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001812 exit(1);
1813 }
1814 }
1815}
1816
1817static int
1818proc_poll(pollv, nfds, timeout)
1819struct pollfd *pollv;
1820int nfds;
1821int timeout;
1822{
1823 int i;
1824 int n;
1825 struct proc_pollfd pollinfo;
1826
1827 if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
1828 return n;
1829 if (n != sizeof(struct proc_pollfd)) {
1830 fprintf(stderr, "panic: short read: %d\n", n);
1831 exit(1);
1832 }
1833 for (i = 0; i < nprocs; i++) {
1834 if (pollv[i].fd == pollinfo.fd)
1835 pollv[i].revents = pollinfo.revents;
1836 else
1837 pollv[i].revents = 0;
1838 }
1839 poller_pid = pollinfo.pid;
1840 return 1;
1841}
1842
1843static void
1844wakeup_handler(sig)
1845int sig;
1846{
1847}
1848
1849static void
1850proc_poller(pfd)
1851int pfd;
1852{
1853 struct proc_pollfd pollinfo;
1854 struct sigaction sa;
1855 sigset_t blocked_set, empty_set;
1856 int i;
1857 int n;
1858 struct rlimit rl;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001859#ifdef FREEBSD
1860 struct procfs_status pfs;
1861#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001862
1863 switch (fork()) {
1864 case -1:
1865 perror("fork");
Dmitry V. Levina6809652008-11-10 17:14:58 +00001866 _exit(1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001867 case 0:
1868 break;
1869 default:
1870 return;
1871 }
1872
1873 sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
1874 sa.sa_flags = 0;
1875 sigemptyset(&sa.sa_mask);
1876 sigaction(SIGHUP, &sa, NULL);
1877 sigaction(SIGINT, &sa, NULL);
1878 sigaction(SIGQUIT, &sa, NULL);
1879 sigaction(SIGPIPE, &sa, NULL);
1880 sigaction(SIGTERM, &sa, NULL);
1881 sa.sa_handler = wakeup_handler;
1882 sigaction(SIGUSR1, &sa, NULL);
1883 sigemptyset(&blocked_set);
1884 sigaddset(&blocked_set, SIGUSR1);
1885 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1886 sigemptyset(&empty_set);
1887
1888 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
1889 perror("getrlimit(RLIMIT_NOFILE, ...)");
Dmitry V. Levina6809652008-11-10 17:14:58 +00001890 _exit(1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001891 }
1892 n = rl.rlim_cur;
1893 for (i = 0; i < n; i++) {
1894 if (i != pfd && i != proc_poll_pipe[1])
1895 close(i);
1896 }
1897
1898 pollinfo.fd = pfd;
1899 pollinfo.pid = getpid();
1900 for (;;) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001901#ifndef FREEBSD
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001902 if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
1903#else
1904 if (ioctl(pfd, PIOCWSTOP, &pfs) < 0)
1905#endif
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001906 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001907 switch (errno) {
1908 case EINTR:
1909 continue;
1910 case EBADF:
1911 pollinfo.revents = POLLERR;
1912 break;
1913 case ENOENT:
1914 pollinfo.revents = POLLHUP;
1915 break;
1916 default:
1917 perror("proc_poller: PIOCWSTOP");
1918 }
1919 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1920 _exit(0);
1921 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001922 pollinfo.revents = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001923 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1924 sigsuspend(&empty_set);
1925 }
1926}
1927
1928#endif /* !HAVE_POLLABLE_PROCFS */
1929
1930static int
1931choose_pfd()
1932{
1933 int i, j;
1934 struct tcb *tcp;
1935
1936 static int last;
1937
1938 if (followfork < 2 &&
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001939 last < nprocs && (pollv[last].revents & POLLWANT)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001940 /*
1941 * The previous process is ready to run again. We'll
1942 * let it do so if it is currently in a syscall. This
1943 * heuristic improves the readability of the trace.
1944 */
1945 tcp = pfd2tcb(pollv[last].fd);
1946 if (tcp && (tcp->flags & TCB_INSYSCALL))
1947 return pollv[last].fd;
1948 }
1949
1950 for (i = 0; i < nprocs; i++) {
1951 /* Let competing children run round robin. */
1952 j = (i + last + 1) % nprocs;
1953 if (pollv[j].revents & (POLLHUP | POLLERR)) {
1954 tcp = pfd2tcb(pollv[j].fd);
1955 if (!tcp) {
1956 fprintf(stderr, "strace: lost proc\n");
1957 exit(1);
1958 }
1959 droptcb(tcp);
1960 return -1;
1961 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001962 if (pollv[j].revents & POLLWANT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001963 last = j;
1964 return pollv[j].fd;
1965 }
1966 }
1967 fprintf(stderr, "strace: nothing ready\n");
1968 exit(1);
1969}
1970
1971static int
1972trace()
1973{
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001974#ifdef POLL_HACK
John Hughesd870b3c2002-05-21 11:24:18 +00001975 struct tcb *in_syscall = NULL;
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001976#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001977 struct tcb *tcp;
1978 int pfd;
1979 int what;
1980 int ioctl_result = 0, ioctl_errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001981 long arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001982
1983 for (;;) {
1984 if (interactive)
1985 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1986
1987 if (nprocs == 0)
1988 break;
1989
1990 switch (nprocs) {
1991 case 1:
1992#ifndef HAVE_POLLABLE_PROCFS
1993 if (proc_poll_pipe[0] == -1) {
1994#endif
1995 tcp = pid2tcb(0);
1996 if (!tcp)
1997 continue;
1998 pfd = tcp->pfd;
1999 if (pfd == -1)
2000 continue;
2001 break;
2002#ifndef HAVE_POLLABLE_PROCFS
2003 }
2004 /* fall through ... */
2005#endif /* !HAVE_POLLABLE_PROCFS */
2006 default:
2007#ifdef HAVE_POLLABLE_PROCFS
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00002008#ifdef POLL_HACK
2009 /* On some systems (e.g. UnixWare) we get too much ugly
2010 "unfinished..." stuff when multiple proceses are in
2011 syscalls. Here's a nasty hack */
Roland McGrath553a6092002-12-16 20:40:39 +00002012
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00002013 if (in_syscall) {
2014 struct pollfd pv;
2015 tcp = in_syscall;
2016 in_syscall = NULL;
2017 pv.fd = tcp->pfd;
2018 pv.events = POLLWANT;
2019 if ((what = poll (&pv, 1, 1)) < 0) {
2020 if (interrupted)
2021 return 0;
2022 continue;
2023 }
2024 else if (what == 1 && pv.revents & POLLWANT) {
2025 goto FOUND;
2026 }
2027 }
2028#endif
2029
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002030 if (poll(pollv, nprocs, INFTIM) < 0) {
2031 if (interrupted)
2032 return 0;
2033 continue;
2034 }
2035#else /* !HAVE_POLLABLE_PROCFS */
2036 if (proc_poll(pollv, nprocs, INFTIM) < 0) {
2037 if (interrupted)
2038 return 0;
2039 continue;
2040 }
2041#endif /* !HAVE_POLLABLE_PROCFS */
2042 pfd = choose_pfd();
2043 if (pfd == -1)
2044 continue;
2045 break;
2046 }
2047
2048 /* Look up `pfd' in our table. */
2049 if ((tcp = pfd2tcb(pfd)) == NULL) {
2050 fprintf(stderr, "unknown pfd: %u\n", pfd);
2051 exit(1);
2052 }
John Hughesb6643082002-05-23 11:02:22 +00002053#ifdef POLL_HACK
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00002054 FOUND:
John Hughesb6643082002-05-23 11:02:22 +00002055#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002056 /* Get the status of the process. */
2057 if (!interrupted) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002058#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002059 ioctl_result = IOCTL_WSTOP (tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002060#else /* FREEBSD */
2061 /* Thanks to some scheduling mystery, the first poller
2062 sometimes waits for the already processed end of fork
2063 event. Doing a non blocking poll here solves the problem. */
2064 if (proc_poll_pipe[0] != -1)
2065 ioctl_result = IOCTL_STATUS (tcp);
2066 else
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00002067 ioctl_result = IOCTL_WSTOP (tcp);
Roland McGrath553a6092002-12-16 20:40:39 +00002068#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002069 ioctl_errno = errno;
2070#ifndef HAVE_POLLABLE_PROCFS
2071 if (proc_poll_pipe[0] != -1) {
2072 if (ioctl_result < 0)
2073 kill(poller_pid, SIGKILL);
2074 else
2075 kill(poller_pid, SIGUSR1);
2076 }
2077#endif /* !HAVE_POLLABLE_PROCFS */
2078 }
2079 if (interrupted)
2080 return 0;
2081
2082 if (interactive)
2083 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
2084
2085 if (ioctl_result < 0) {
2086 /* Find out what happened if it failed. */
2087 switch (ioctl_errno) {
2088 case EINTR:
2089 case EBADF:
2090 continue;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002091#ifdef FREEBSD
2092 case ENOTTY:
Roland McGrath553a6092002-12-16 20:40:39 +00002093#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002094 case ENOENT:
2095 droptcb(tcp);
2096 continue;
2097 default:
2098 perror("PIOCWSTOP");
2099 exit(1);
2100 }
2101 }
2102
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +00002103#ifdef FREEBSD
2104 if ((tcp->flags & TCB_STARTUP) && (tcp->status.PR_WHY == PR_SYSEXIT)) {
2105 /* discard first event for a syscall we never entered */
2106 IOCTL (tcp->pfd, PIOCRUN, 0);
2107 continue;
2108 }
Roland McGrath553a6092002-12-16 20:40:39 +00002109#endif
2110
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002111 /* clear the just started flag */
2112 tcp->flags &= ~TCB_STARTUP;
2113
2114 /* set current output file */
2115 outf = tcp->outf;
Andreas Schwabccdff482009-10-27 16:27:13 +01002116 curcol = tcp->curcol;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002117
2118 if (cflag) {
2119 struct timeval stime;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002120#ifdef FREEBSD
2121 char buf[1024];
2122 int len;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002123
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002124 if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) {
2125 buf[len] = '\0';
2126 sscanf(buf,
2127 "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld",
2128 &stime.tv_sec, &stime.tv_usec);
2129 } else
2130 stime.tv_sec = stime.tv_usec = 0;
Roland McGrath553a6092002-12-16 20:40:39 +00002131#else /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002132 stime.tv_sec = tcp->status.pr_stime.tv_sec;
2133 stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002134#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002135 tv_sub(&tcp->dtime, &stime, &tcp->stime);
2136 tcp->stime = stime;
2137 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002138 what = tcp->status.PR_WHAT;
2139 switch (tcp->status.PR_WHY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002140#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002141 case PR_REQUESTED:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002142 if (tcp->status.PR_FLAGS & PR_ASLEEP) {
2143 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002144 if (trace_syscall(tcp) < 0) {
2145 fprintf(stderr, "syscall trouble\n");
2146 exit(1);
2147 }
2148 }
2149 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002150#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002151 case PR_SYSENTRY:
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00002152#ifdef POLL_HACK
2153 in_syscall = tcp;
2154#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002155 case PR_SYSEXIT:
2156 if (trace_syscall(tcp) < 0) {
2157 fprintf(stderr, "syscall trouble\n");
2158 exit(1);
2159 }
2160 break;
2161 case PR_SIGNALLED:
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002162 if (cflag != CFLAG_ONLY_STATS
2163 && (qual_flags[what] & QUAL_SIGNAL)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002164 printleader(tcp);
2165 tprintf("--- %s (%s) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00002166 signame(what), strsignal(what));
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002167 printtrailer();
John Hughes58265892001-10-18 15:13:53 +00002168#ifdef PR_INFO
2169 if (tcp->status.PR_INFO.si_signo == what) {
2170 printleader(tcp);
2171 tprintf(" siginfo=");
2172 printsiginfo(&tcp->status.PR_INFO, 1);
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002173 printtrailer();
John Hughes58265892001-10-18 15:13:53 +00002174 }
2175#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002176 }
2177 break;
2178 case PR_FAULTED:
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002179 if (cflag != CFLAGS_ONLY_STATS
2180 && (qual_flags[what] & QUAL_FAULT)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002181 printleader(tcp);
2182 tprintf("=== FAULT %d ===", what);
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002183 printtrailer();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002184 }
2185 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002186#ifdef FREEBSD
2187 case 0: /* handle case we polled for nothing */
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00002188 continue;
Roland McGrath553a6092002-12-16 20:40:39 +00002189#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002190 default:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002191 fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002192 exit(1);
2193 break;
2194 }
Andreas Schwabccdff482009-10-27 16:27:13 +01002195 /* Remember current print column before continuing. */
2196 tcp->curcol = curcol;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002197 arg = 0;
Roland McGrath553a6092002-12-16 20:40:39 +00002198#ifndef FREEBSD
Andreas Schwab372cc842010-07-09 11:49:27 +02002199 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0)
Roland McGrath553a6092002-12-16 20:40:39 +00002200#else
Andreas Schwab372cc842010-07-09 11:49:27 +02002201 if (IOCTL (tcp->pfd, PIOCRUN, 0) < 0)
Roland McGrath553a6092002-12-16 20:40:39 +00002202#endif
Andreas Schwab372cc842010-07-09 11:49:27 +02002203 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002204 perror("PIOCRUN");
2205 exit(1);
2206 }
2207 }
2208 return 0;
2209}
2210
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002211#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002212
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002213#ifdef TCB_GROUP_EXITING
2214/* Handle an exit detach or death signal that is taking all the
2215 related clone threads with it. This is called in three circumstances:
2216 SIG == -1 TCP has already died (TCB_ATTACHED is clear, strace is parent).
2217 SIG == 0 Continuing TCP will perform an exit_group syscall.
2218 SIG == other Continuing TCP with SIG will kill the process.
2219*/
2220static int
2221handle_group_exit(struct tcb *tcp, int sig)
2222{
2223 /* We need to locate our records of all the clone threads
2224 related to TCP, either its children or siblings. */
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002225 struct tcb *leader = NULL;
2226
2227 if (tcp->flags & TCB_CLONE_THREAD)
2228 leader = tcp->parent;
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002229
2230 if (sig < 0) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002231 if (leader != NULL && leader != tcp
2232 && !(leader->flags & TCB_GROUP_EXITING)
2233 && !(tcp->flags & TCB_STARTUP)
2234 ) {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002235 fprintf(stderr,
2236 "PANIC: handle_group_exit: %d leader %d\n",
2237 tcp->pid, leader ? leader->pid : -1);
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002238 }
2239 /* TCP no longer exists therefore you must not detach() it. */
Roland McGrath1bfd3102007-08-03 10:02:00 +00002240#ifndef USE_PROCFS
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002241 resume_from_tcp(tcp);
Roland McGrath1bfd3102007-08-03 10:02:00 +00002242#endif
Roland McGrath0a463882007-07-05 18:43:16 +00002243 droptcb(tcp); /* Already died. */
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002244 }
2245 else {
Roland McGratha08a97e2005-08-03 11:23:46 +00002246 /* Mark that we are taking the process down. */
2247 tcp->flags |= TCB_EXITING | TCB_GROUP_EXITING;
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002248 if (tcp->flags & TCB_ATTACHED) {
Roland McGrathd6a32f12007-07-11 08:35:11 +00002249 detach(tcp, sig);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00002250 if (leader != NULL && leader != tcp)
Roland McGrath1bfd3102007-08-03 10:02:00 +00002251 leader->flags |= TCB_GROUP_EXITING;
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002252 } else {
2253 if (ptrace_restart(PTRACE_CONT, tcp, sig) < 0) {
2254 cleanup();
2255 return -1;
2256 }
2257 if (leader != NULL) {
Roland McGrath05690952004-10-20 01:00:27 +00002258 leader->flags |= TCB_GROUP_EXITING;
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002259 if (leader != tcp)
2260 droptcb(tcp);
2261 }
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002262 /* The leader will report to us as parent now,
2263 and then we'll get to the SIG==-1 case. */
2264 return 0;
2265 }
2266 }
2267
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002268 return 0;
2269}
2270#endif
2271
Roland McGratheb9e2e82009-06-02 16:49:22 -07002272static int
2273trace()
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002274{
2275 int pid;
2276 int wait_errno;
2277 int status;
2278 struct tcb *tcp;
2279#ifdef LINUX
2280 struct rusage ru;
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00002281#ifdef __WALL
Roland McGratheb9e2e82009-06-02 16:49:22 -07002282 static int wait4_options = __WALL;
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00002283#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002284#endif /* LINUX */
2285
Roland McGratheb9e2e82009-06-02 16:49:22 -07002286 while (nprocs != 0) {
Denys Vlasenko222713a2009-03-17 14:29:59 +00002287 if (interrupted)
Roland McGratheb9e2e82009-06-02 16:49:22 -07002288 return 0;
2289 if (interactive)
2290 sigprocmask(SIG_SETMASK, &empty_set, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002291#ifdef LINUX
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00002292#ifdef __WALL
Roland McGratheb9e2e82009-06-02 16:49:22 -07002293 pid = wait4(-1, &status, wait4_options, cflag ? &ru : NULL);
Roland McGrath5bc05552002-12-17 04:50:47 +00002294 if (pid < 0 && (wait4_options & __WALL) && errno == EINVAL) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00002295 /* this kernel does not support __WALL */
2296 wait4_options &= ~__WALL;
2297 errno = 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -07002298 pid = wait4(-1, &status, wait4_options,
2299 cflag ? &ru : NULL);
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00002300 }
Roland McGrath5bc05552002-12-17 04:50:47 +00002301 if (pid < 0 && !(wait4_options & __WALL) && errno == ECHILD) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00002302 /* most likely a "cloned" process */
Roland McGratheb9e2e82009-06-02 16:49:22 -07002303 pid = wait4(-1, &status, __WCLONE,
2304 cflag ? &ru : NULL);
2305 if (pid == -1) {
2306 fprintf(stderr, "strace: clone wait4 "
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00002307 "failed: %s\n", strerror(errno));
2308 }
2309 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002310#else
2311 pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
2312#endif /* __WALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002313#endif /* LINUX */
2314#ifdef SUNOS4
2315 pid = wait(&status);
2316#endif /* SUNOS4 */
2317 wait_errno = errno;
Roland McGratheb9e2e82009-06-02 16:49:22 -07002318 if (interactive)
2319 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002320
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002321 if (pid == -1) {
Roland McGratheb9e2e82009-06-02 16:49:22 -07002322 switch (wait_errno) {
2323 case EINTR:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002324 continue;
Roland McGratheb9e2e82009-06-02 16:49:22 -07002325 case ECHILD:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002326 /*
2327 * We would like to verify this case
2328 * but sometimes a race in Solbourne's
2329 * version of SunOS sometimes reports
2330 * ECHILD before sending us SIGCHILD.
2331 */
Roland McGratheb9e2e82009-06-02 16:49:22 -07002332 return 0;
2333 default:
2334 errno = wait_errno;
2335 perror("strace: wait");
2336 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002337 }
2338 }
Dmitry V. Levin10de62b2006-12-13 21:45:31 +00002339 if (pid == popen_pid) {
2340 if (WIFEXITED(status) || WIFSIGNALED(status))
2341 popen_pid = -1;
2342 continue;
2343 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002344 if (debug)
2345 fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
2346
2347 /* Look up `pid' in our table. */
2348 if ((tcp = pid2tcb(pid)) == NULL) {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002349#ifdef LINUX
Roland McGrath41c48222008-07-18 00:25:10 +00002350 if (followfork) {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002351 /* This is needed to go with the CLONE_PTRACE
2352 changes in process.c/util.c: we might see
2353 the child's initial trap before we see the
2354 parent return from the clone syscall.
2355 Leave the child suspended until the parent
2356 returns from its system call. Only then
2357 will we have the association of parent and
2358 child so that we know how to do clearbpt
2359 in the child. */
Denys Vlasenko418d66a2009-01-17 01:52:54 +00002360 tcp = alloctcb(pid);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002361 tcp->flags |= TCB_ATTACHED | TCB_SUSPENDED;
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002362 if (!qflag)
2363 fprintf(stderr, "\
2364Process %d attached (waiting for parent)\n",
2365 pid);
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00002366 }
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002367 else
2368 /* This can happen if a clone call used
2369 CLONE_PTRACE itself. */
Roland McGratheb9e2e82009-06-02 16:49:22 -07002370#endif
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002371 {
2372 fprintf(stderr, "unknown pid: %u\n", pid);
2373 if (WIFSTOPPED(status))
2374 ptrace(PTRACE_CONT, pid, (char *) 1, 0);
2375 exit(1);
2376 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002377 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002378 /* set current output file */
2379 outf = tcp->outf;
Andreas Schwabccdff482009-10-27 16:27:13 +01002380 curcol = tcp->curcol;
Denys Vlasenko84e20af2009-02-10 16:03:20 +00002381 if (cflag) {
Roland McGratheb9e2e82009-06-02 16:49:22 -07002382#ifdef LINUX
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002383 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
2384 tcp->stime = ru.ru_stime;
Roland McGratheb9e2e82009-06-02 16:49:22 -07002385#endif /* !LINUX */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002386 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002387
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002388 if (tcp->flags & TCB_SUSPENDED) {
2389 /*
2390 * Apparently, doing any ptrace() call on a stopped
2391 * process, provokes the kernel to report the process
2392 * status again on a subsequent wait(), even if the
2393 * process has not been actually restarted.
2394 * Since we have inspected the arguments of suspended
2395 * processes we end up here testing for this case.
2396 */
2397 continue;
2398 }
2399 if (WIFSIGNALED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002400 if (pid == strace_child)
2401 exit_code = 0x100 | WTERMSIG(status);
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002402 if (cflag != CFLAG_ONLY_STATS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002403 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
2404 printleader(tcp);
Roland McGrath2efe8792004-01-13 09:59:45 +00002405 tprintf("+++ killed by %s %s+++",
2406 signame(WTERMSIG(status)),
2407#ifdef WCOREDUMP
2408 WCOREDUMP(status) ? "(core dumped) " :
2409#endif
2410 "");
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002411 printtrailer();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002412 }
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002413#ifdef TCB_GROUP_EXITING
2414 handle_group_exit(tcp, -1);
2415#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002416 droptcb(tcp);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002417#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002418 continue;
2419 }
2420 if (WIFEXITED(status)) {
Dmitry V. Levina6809652008-11-10 17:14:58 +00002421 if (pid == strace_child)
2422 exit_code = WEXITSTATUS(status);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002423 if (debug)
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002424 fprintf(stderr, "pid %u exited with %d\n", pid, WEXITSTATUS(status));
2425 if ((tcp->flags & (TCB_ATTACHED|TCB_STARTUP)) == TCB_ATTACHED
Roland McGrath05690952004-10-20 01:00:27 +00002426#ifdef TCB_GROUP_EXITING
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002427 && !(tcp->parent && (tcp->parent->flags & TCB_GROUP_EXITING))
Roland McGrath1bfd3102007-08-03 10:02:00 +00002428 && !(tcp->flags & TCB_GROUP_EXITING)
Roland McGrath05690952004-10-20 01:00:27 +00002429#endif
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002430 ) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002431 fprintf(stderr,
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002432 "PANIC: attached pid %u exited with %d\n",
2433 pid, WEXITSTATUS(status));
2434 }
Roland McGrath0a396902003-06-10 03:05:53 +00002435 if (tcp == tcp_last) {
Denys Vlasenko7a8bf062009-01-29 20:38:20 +00002436 if ((tcp->flags & (TCB_INSYSCALL|TCB_REPRINT)) == TCB_INSYSCALL)
Roland McGrath0a396902003-06-10 03:05:53 +00002437 tprintf(" <unfinished ... exit status %d>\n",
2438 WEXITSTATUS(status));
2439 tcp_last = NULL;
2440 }
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002441#ifdef TCB_GROUP_EXITING
2442 handle_group_exit(tcp, -1);
2443#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002444 droptcb(tcp);
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002445#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002446 continue;
2447 }
2448 if (!WIFSTOPPED(status)) {
2449 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
2450 droptcb(tcp);
2451 continue;
2452 }
2453 if (debug)
2454 fprintf(stderr, "pid %u stopped, [%s]\n",
Nate Sammonsce780fc1999-03-29 23:23:13 +00002455 pid, signame(WSTOPSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002456
Roland McGrath02203312007-06-11 22:06:31 +00002457 /*
2458 * Interestingly, the process may stop
2459 * with STOPSIG equal to some other signal
Roland McGratheb9e2e82009-06-02 16:49:22 -07002460 * than SIGSTOP if we happend to attach
Roland McGrath02203312007-06-11 22:06:31 +00002461 * just before the process takes a signal.
Mike Frysingerc1a5b7e2009-10-07 20:41:29 -04002462 * A no-MMU vforked child won't send up a signal,
2463 * so skip the first (lost) execve notification.
Roland McGrath02203312007-06-11 22:06:31 +00002464 */
Mike Frysingerc1a5b7e2009-10-07 20:41:29 -04002465 if ((tcp->flags & TCB_STARTUP) &&
2466 (WSTOPSIG(status) == SIGSTOP || strace_vforked)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002467 /*
2468 * This flag is there to keep us in sync.
2469 * Next time this process stops it should
2470 * really be entering a system call.
2471 */
2472 tcp->flags &= ~TCB_STARTUP;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002473 if (tcp->flags & TCB_BPTSET) {
Roland McGrath02203312007-06-11 22:06:31 +00002474 /*
2475 * One example is a breakpoint inherited from
2476 * parent through fork ().
2477 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002478 if (clearbpt(tcp) < 0) /* Pretty fatal */ {
2479 droptcb(tcp);
2480 cleanup();
2481 return -1;
2482 }
2483 }
2484 goto tracing;
2485 }
2486
Roland McGratheb9e2e82009-06-02 16:49:22 -07002487 if (WSTOPSIG(status) != SIGTRAP) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002488 if (WSTOPSIG(status) == SIGSTOP &&
2489 (tcp->flags & TCB_SIGTRAPPED)) {
2490 /*
2491 * Trapped attempt to block SIGTRAP
2492 * Hope we are back in control now.
2493 */
2494 tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002495 if (ptrace_restart(PTRACE_SYSCALL, tcp, 0) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002496 cleanup();
2497 return -1;
2498 }
2499 continue;
2500 }
Dmitry V. Levine3a7ef52010-03-28 19:24:54 +00002501 if (cflag != CFLAG_ONLY_STATS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002502 && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
Jan Kratochvil1f942712008-08-06 21:38:52 +00002503 unsigned long addr = 0;
2504 long pc = 0;
Dmitry V. Levin96339422006-10-11 23:11:43 +00002505#if defined(PT_CR_IPSR) && defined(PT_CR_IIP) && defined(PT_GETSIGINFO)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002506# define PSR_RI 41
2507 struct siginfo si;
Jan Kratochvil1f942712008-08-06 21:38:52 +00002508 long psr;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002509
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002510 upeek(tcp, PT_CR_IPSR, &psr);
2511 upeek(tcp, PT_CR_IIP, &pc);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002512
2513 pc += (psr >> PSR_RI) & 0x3;
2514 ptrace(PT_GETSIGINFO, pid, 0, (long) &si);
2515 addr = (unsigned long) si.si_addr;
Roland McGrath3a055d72005-03-06 22:24:29 +00002516#elif defined PTRACE_GETSIGINFO
2517 if (WSTOPSIG(status) == SIGSEGV ||
2518 WSTOPSIG(status) == SIGBUS) {
2519 siginfo_t si;
2520 if (ptrace(PTRACE_GETSIGINFO, pid,
2521 0, &si) == 0)
2522 addr = (unsigned long)
2523 si.si_addr;
2524 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002525#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002526 printleader(tcp);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002527 tprintf("--- %s (%s) @ %lx (%lx) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00002528 signame(WSTOPSIG(status)),
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002529 strsignal(WSTOPSIG(status)), pc, addr);
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002530 printtrailer();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002531 }
Roland McGrath05690952004-10-20 01:00:27 +00002532 if (((tcp->flags & TCB_ATTACHED) ||
2533 tcp->nclone_threads > 0) &&
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002534 !sigishandled(tcp, WSTOPSIG(status))) {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002535#ifdef TCB_GROUP_EXITING
2536 handle_group_exit(tcp, WSTOPSIG(status));
2537#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002538 detach(tcp, WSTOPSIG(status));
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002539#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002540 continue;
2541 }
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002542 if (ptrace_restart(PTRACE_SYSCALL, tcp, WSTOPSIG(status)) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002543 cleanup();
2544 return -1;
2545 }
2546 tcp->flags &= ~TCB_SUSPENDED;
2547 continue;
2548 }
Roland McGrath02203312007-06-11 22:06:31 +00002549 /* we handled the STATUS, we are permitted to interrupt now. */
2550 if (interrupted)
2551 return 0;
Roland McGratheb9e2e82009-06-02 16:49:22 -07002552 if (trace_syscall(tcp) < 0 && !tcp->ptrace_errno) {
2553 /* ptrace() failed in trace_syscall() with ESRCH.
2554 * Likely a result of process disappearing mid-flight.
2555 * Observed case: exit_group() terminating
2556 * all processes in thread group. In this case, threads
2557 * "disappear" in an unpredictable moment without any
2558 * notification to strace via wait().
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002559 */
2560 if (tcp->flags & TCB_ATTACHED) {
Roland McGratheb9e2e82009-06-02 16:49:22 -07002561 if (tcp_last) {
2562 /* Do we have dangling line "syscall(param, param"?
2563 * Finish the line then. We cannot
2564 */
2565 tcp_last->flags |= TCB_REPRINT;
2566 tprintf(" <unfinished ...>");
2567 printtrailer();
2568 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002569 detach(tcp, 0);
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002570 } else {
Roland McGratheb9e2e82009-06-02 16:49:22 -07002571 ptrace(PTRACE_KILL,
2572 tcp->pid, (char *) 1, SIGTERM);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002573 droptcb(tcp);
2574 }
2575 continue;
2576 }
2577 if (tcp->flags & TCB_EXITING) {
Roland McGrathe85bbfe2003-01-09 06:53:31 +00002578#ifdef TCB_GROUP_EXITING
2579 if (tcp->flags & TCB_GROUP_EXITING) {
2580 if (handle_group_exit(tcp, 0) < 0)
2581 return -1;
2582 continue;
2583 }
2584#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002585 if (tcp->flags & TCB_ATTACHED)
2586 detach(tcp, 0);
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002587 else if (ptrace_restart(PTRACE_CONT, tcp, 0) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002588 cleanup();
2589 return -1;
2590 }
2591 continue;
2592 }
2593 if (tcp->flags & TCB_SUSPENDED) {
2594 if (!qflag)
2595 fprintf(stderr, "Process %u suspended\n", pid);
2596 continue;
2597 }
2598 tracing:
Andreas Schwabccdff482009-10-27 16:27:13 +01002599 /* Remember current print column before continuing. */
2600 tcp->curcol = curcol;
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002601 if (ptrace_restart(PTRACE_SYSCALL, tcp, 0) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002602 cleanup();
2603 return -1;
2604 }
2605 }
2606 return 0;
2607}
2608
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002609#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002610
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002611#include <stdarg.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002612
2613void
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002614tprintf(const char *fmt, ...)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002615{
2616 va_list args;
2617
Andreas Schwabe5355de2009-10-27 16:56:43 +01002618 va_start(args, fmt);
Roland McGrathb310a0c2003-11-06 23:41:22 +00002619 if (outf) {
2620 int n = vfprintf(outf, fmt, args);
Andreas Schwabccdff482009-10-27 16:27:13 +01002621 if (n < 0) {
2622 if (outf != stderr)
2623 perror(outfname == NULL
2624 ? "<writing to pipe>" : outfname);
2625 } else
Roland McGrathb310a0c2003-11-06 23:41:22 +00002626 curcol += n;
2627 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002628 va_end(args);
2629 return;
2630}
2631
2632void
Roland McGratheb9e2e82009-06-02 16:49:22 -07002633printleader(tcp)
2634struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002635{
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002636 if (tcp_last) {
Roland McGratheb9e2e82009-06-02 16:49:22 -07002637 if (tcp_last->ptrace_errno) {
Denys Vlasenko7e0615f2009-01-28 19:00:54 +00002638 if (tcp_last->flags & TCB_INSYSCALL) {
Roland McGratheb9e2e82009-06-02 16:49:22 -07002639 tprintf(" <unavailable>)");
2640 tabto(acolumn);
Denys Vlasenko7e0615f2009-01-28 19:00:54 +00002641 }
Roland McGratheb9e2e82009-06-02 16:49:22 -07002642 tprintf("= ? <unavailable>\n");
2643 tcp_last->ptrace_errno = 0;
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002644 } else if (!outfname || followfork < 2 || tcp_last == tcp) {
Denys Vlasenko7e0615f2009-01-28 19:00:54 +00002645 tcp_last->flags |= TCB_REPRINT;
Roland McGratheb9e2e82009-06-02 16:49:22 -07002646 tprintf(" <unfinished ...>\n");
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00002647 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002648 }
2649 curcol = 0;
2650 if ((followfork == 1 || pflag_seen > 1) && outfname)
2651 tprintf("%-5d ", tcp->pid);
2652 else if (nprocs > 1 && !outfname)
2653 tprintf("[pid %5u] ", tcp->pid);
2654 if (tflag) {
2655 char str[sizeof("HH:MM:SS")];
2656 struct timeval tv, dtv;
2657 static struct timeval otv;
2658
2659 gettimeofday(&tv, NULL);
2660 if (rflag) {
2661 if (otv.tv_sec == 0)
2662 otv = tv;
2663 tv_sub(&dtv, &tv, &otv);
2664 tprintf("%6ld.%06ld ",
2665 (long) dtv.tv_sec, (long) dtv.tv_usec);
2666 otv = tv;
2667 }
2668 else if (tflag > 2) {
2669 tprintf("%ld.%06ld ",
2670 (long) tv.tv_sec, (long) tv.tv_usec);
2671 }
2672 else {
2673 time_t local = tv.tv_sec;
2674 strftime(str, sizeof(str), "%T", localtime(&local));
2675 if (tflag > 1)
2676 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
2677 else
2678 tprintf("%s ", str);
2679 }
2680 }
2681 if (iflag)
2682 printcall(tcp);
2683}
2684
2685void
2686tabto(col)
2687int col;
2688{
2689 if (curcol < col)
2690 tprintf("%*s", col - curcol, "");
2691}
2692
2693void
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002694printtrailer(void)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002695{
2696 tprintf("\n");
2697 tcp_last = NULL;
2698}
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002699
Wichert Akkermanea78f0f1999-11-29 15:34:02 +00002700#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002701
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002702int
2703mp_ioctl(int fd, int cmd, void *arg, int size)
2704{
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002705 struct iovec iov[2];
2706 int n = 1;
Roland McGrath553a6092002-12-16 20:40:39 +00002707
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002708 iov[0].iov_base = &cmd;
2709 iov[0].iov_len = sizeof cmd;
2710 if (arg) {
2711 ++n;
2712 iov[1].iov_base = arg;
2713 iov[1].iov_len = size;
2714 }
Roland McGrath553a6092002-12-16 20:40:39 +00002715
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +00002716 return writev(fd, iov, n);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002717}
2718
2719#endif