blob: 9f2a86109ad21db7454de285edff99b5d5e96570 [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
Wichert Akkerman2ee6e452000-02-18 15:36:12 +000033#include <sys/types.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000034#include "defs.h"
35
36#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>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000047
Wichert Akkerman7b3346b2001-10-09 23:47:38 +000048#if defined(IA64) && defined(LINUX)
49# include <asm/ptrace_offsets.h>
50#endif
51
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +000052#ifdef USE_PROCFS
53#include <poll.h>
54#endif
55
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000056#ifdef SVR4
57#include <sys/stropts.h>
Wichert Akkermanea78f0f1999-11-29 15:34:02 +000058#ifdef HAVE_MP_PROCFS
John Hughes1d08dcf2001-07-10 13:48:44 +000059#ifdef HAVE_SYS_UIO_H
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000060#include <sys/uio.h>
61#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000062#endif
John Hughes1d08dcf2001-07-10 13:48:44 +000063#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000064
65int debug = 0, followfork = 0, followvfork = 0, interactive = 0;
66int rflag = 0, tflag = 0, dtime = 0, cflag = 0;
67int iflag = 0, xflag = 0, qflag = 0;
68int pflag_seen = 0;
69
Michal Ludvig17f8fb32002-11-06 13:17:21 +000070/* Sometimes we want to print only succeeding syscalls. */
71int not_failing_only = 0;
72
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000073char *username = NULL;
74uid_t run_uid;
75gid_t run_gid;
76
77int acolumn = DEFAULT_ACOLUMN;
78int max_strlen = DEFAULT_STRLEN;
79char *outfname = NULL;
80FILE *outf;
81struct tcb tcbtab[MAX_PROCS];
82int nprocs;
83char *progname;
84extern char version[];
85extern char **environ;
86
87static struct tcb *pid2tcb P((int pid));
88static int trace P((void));
89static void cleanup P((void));
90static void interrupt P((int sig));
91static sigset_t empty_set, blocked_set;
92
93#ifdef HAVE_SIG_ATOMIC_T
94static volatile sig_atomic_t interrupted;
95#else /* !HAVE_SIG_ATOMIC_T */
96#ifdef __STDC__
97static volatile int interrupted;
98#else /* !__STDC__ */
99static int interrupted;
100#endif /* !__STDC__ */
101#endif /* !HAVE_SIG_ATOMIC_T */
102
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000103#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000104
105static struct tcb *pfd2tcb P((int pfd));
106static void reaper P((int sig));
107static void rebuild_pollv P((void));
Wichert Akkermane68d61c1999-06-28 13:17:16 +0000108struct pollfd pollv[MAX_PROCS];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000109
110#ifndef HAVE_POLLABLE_PROCFS
111
112static void proc_poll_open P((void));
113static void proc_poller P((int pfd));
114
115struct proc_pollfd {
116 int fd;
117 int revents;
118 int pid;
119};
120
121static int poller_pid;
122static int proc_poll_pipe[2] = { -1, -1 };
123
124#endif /* !HAVE_POLLABLE_PROCFS */
125
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000126#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000127#define POLLWANT POLLWRNORM
128#else
129#define POLLWANT POLLPRI
130#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000131#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000132
133static void
134usage(ofp, exitval)
135FILE *ofp;
136int exitval;
137{
138 fprintf(ofp, "\
139usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\
140 [-p pid] ... [-s strsize] [-u username] [command [arg ...]]\n\
141 or: strace -c [-e expr] ... [-O overhead] [-S sortby] [command [arg ...]]\n\
142-c -- count time, calls, and errors for each syscall and report summary\n\
143-f -- follow forks, -ff -- with output into separate files\n\
144-F -- attempt to follow vforks, -h -- print help message\n\
145-i -- print instruction pointer at time of syscall\n\
146-q -- suppress messages about attaching, detaching, etc.\n\
147-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
148-T -- print time spent in each syscall, -V -- print version\n\
149-v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\
150-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
151-a column -- alignment COLUMN for printing syscall results (default %d)\n\
152-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
153 options: trace, abbrev, verbose, raw, signal, read, or write\n\
154-o file -- send trace output to FILE instead of stderr\n\
155-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
156-p pid -- trace process with process id PID, may be repeated\n\
157-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
158-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
159-u username -- run command as username handling setuid and/or setgid\n\
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000160-z -- print only succeeding syscalls\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000161", DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
162 exit(exitval);
163}
164
165#ifdef SVR4
166#ifdef MIPS
167void
168foobar()
169{
170}
171#endif /* MIPS */
172#endif /* SVR4 */
173
174int
175main(argc, argv)
176int argc;
177char *argv[];
178{
179 extern int optind;
180 extern char *optarg;
181 struct tcb *tcp;
182 int c, pid = 0;
183 struct sigaction sa;
184
185 static char buf[BUFSIZ];
186
187 progname = argv[0];
188 outf = stderr;
189 interactive = 1;
190 qualify("trace=all");
191 qualify("abbrev=all");
192 qualify("verbose=all");
193 qualify("signal=all");
194 set_sortby(DEFAULT_SORTBY);
195 set_personality(DEFAULT_PERSONALITY);
196 while ((c = getopt(argc, argv,
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000197 "+cdfFhiqrtTvVxza:e:o:O:p:s:S:u:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000198 switch (c) {
199 case 'c':
200 cflag++;
201 dtime++;
202 break;
203 case 'd':
204 debug++;
205 break;
206 case 'f':
207 followfork++;
208 break;
209 case 'F':
210 followvfork++;
211 break;
212 case 'h':
213 usage(stdout, 0);
214 break;
215 case 'i':
216 iflag++;
217 break;
218 case 'q':
219 qflag++;
220 break;
221 case 'r':
222 rflag++;
223 tflag++;
224 break;
225 case 't':
226 tflag++;
227 break;
228 case 'T':
229 dtime++;
230 break;
231 case 'x':
232 xflag++;
233 break;
234 case 'v':
235 qualify("abbrev=none");
236 break;
237 case 'V':
238 printf("%s\n", version);
239 exit(0);
240 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000241 case 'z':
242 not_failing_only = 1;
243 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000244 case 'a':
245 acolumn = atoi(optarg);
246 break;
247 case 'e':
248 qualify(optarg);
249 break;
250 case 'o':
251 outfname = strdup(optarg);
252 break;
253 case 'O':
254 set_overhead(atoi(optarg));
255 break;
256 case 'p':
257 if ((pid = atoi(optarg)) == 0) {
258 fprintf(stderr, "%s: Invalid process id: %s\n",
259 progname, optarg);
260 break;
261 }
262 if (pid == getpid()) {
Wichert Akkerman54a47671999-10-17 00:57:34 +0000263 fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000264 break;
265 }
266 if ((tcp = alloctcb(pid)) == NULL) {
267 fprintf(stderr, "%s: tcb table full, please recompile strace\n",
268 progname);
269 exit(1);
270 }
271 tcp->flags |= TCB_ATTACHED;
272 pflag_seen++;
273 break;
274 case 's':
275 max_strlen = atoi(optarg);
276 break;
277 case 'S':
278 set_sortby(optarg);
279 break;
280 case 'u':
281 username = strdup(optarg);
282 break;
283 default:
284 usage(stderr, 1);
285 break;
286 }
287 }
288
289 /* See if they want to run as another user. */
290 if (username != NULL) {
291 struct passwd *pent;
292
293 if (getuid() != 0 || geteuid() != 0) {
294 fprintf(stderr,
295 "%s: you must be root to use the -u option\n",
296 progname);
297 exit(1);
298 }
299 if ((pent = getpwnam(username)) == NULL) {
300 fprintf(stderr, "%s: cannot find user `%s'\n",
301 progname, optarg);
302 exit(1);
303 }
304 run_uid = pent->pw_uid;
305 run_gid = pent->pw_gid;
306 }
307 else {
308 run_uid = getuid();
309 run_gid = getgid();
310 }
311
312#ifndef SVR4
313 setreuid(geteuid(), getuid());
314#endif
315
316 /* See if they want to pipe the output. */
317 if (outfname && (outfname[0] == '|' || outfname[0] == '!')) {
318 if ((outf = popen(outfname + 1, "w")) == NULL) {
319 fprintf(stderr, "%s: can't popen '%s': %s\n",
320 progname, outfname + 1, strerror(errno));
321 exit(1);
322 }
323 free(outfname);
324 outfname = NULL;
325 }
326
327 /* Check if they want to redirect the output. */
328 if (outfname) {
Wichert Akkerman54b4f792001-08-03 11:43:35 +0000329 long f;
330
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000331 if ((outf = fopen(outfname, "w")) == NULL) {
332 fprintf(stderr, "%s: can't fopen '%s': %s\n",
333 progname, outfname, strerror(errno));
334 exit(1);
335 }
Wichert Akkerman54b4f792001-08-03 11:43:35 +0000336
337 if ((f=fcntl(fileno(outf), F_GETFD)) < 0 ) {
338 perror("failed to get flags for outputfile");
339 exit(1);
340 }
341
342 if (fcntl(fileno(outf), F_SETFD, f|FD_CLOEXEC) < 0 ) {
343 perror("failed to set flags for outputfile");
344 exit(1);
345 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000346 }
347
348#ifndef SVR4
349 setreuid(geteuid(), getuid());
350#endif
351
352 if (!outfname) {
353 qflag = 1;
354 setvbuf(outf, buf, _IOLBF, BUFSIZ);
355 }
356 else if (optind < argc)
357 interactive = 0;
358 else
359 qflag = 1;
360
361 for (c = 0, tcp = tcbtab; c < MAX_PROCS; c++, tcp++) {
362 /* Reinitialize the output since it may have changed. */
363 tcp->outf = outf;
364 if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
365 continue;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000366#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000367 if (proc_open(tcp, 1) < 0) {
368 fprintf(stderr, "trouble opening proc file\n");
369 droptcb(tcp);
370 continue;
371 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000372#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000373 if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
374 perror("attach: ptrace(PTRACE_ATTACH, ...)");
375 droptcb(tcp);
376 continue;
377 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000378#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000379 if (!qflag)
380 fprintf(stderr,
381 "Process %u attached - interrupt to quit\n",
382 pid);
383 }
384
385 if (optind < argc) {
386 struct stat statbuf;
387 char *filename;
388 char pathname[MAXPATHLEN];
389
390 filename = argv[optind];
391 if (strchr(filename, '/'))
392 strcpy(pathname, filename);
393#ifdef USE_DEBUGGING_EXEC
394 /*
395 * Debuggers customarily check the current directory
396 * first regardless of the path but doing that gives
397 * security geeks a panic attack.
398 */
399 else if (stat(filename, &statbuf) == 0)
400 strcpy(pathname, filename);
401#endif /* USE_DEBUGGING_EXEC */
402 else {
403 char *path;
404 int m, n, len;
405
406 for (path = getenv("PATH"); path && *path; path += m) {
407 if (strchr(path, ':')) {
408 n = strchr(path, ':') - path;
409 m = n + 1;
410 }
411 else
412 m = n = strlen(path);
413 if (n == 0) {
414 getcwd(pathname, MAXPATHLEN);
415 len = strlen(pathname);
416 }
417 else {
418 strncpy(pathname, path, n);
419 len = n;
420 }
421 if (len && pathname[len - 1] != '/')
422 pathname[len++] = '/';
423 strcpy(pathname + len, filename);
424 if (stat(pathname, &statbuf) == 0)
425 break;
426 }
427 }
428 if (stat(pathname, &statbuf) < 0) {
429 fprintf(stderr, "%s: %s: command not found\n",
430 progname, filename);
431 exit(1);
432 }
433 switch (pid = fork()) {
434 case -1:
435 perror("strace: fork");
436 cleanup();
437 exit(1);
438 break;
439 case 0: {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000440#ifdef USE_PROCFS
441 if (outf != stderr) close (fileno (outf));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000442#ifdef MIPS
443 /* Kludge for SGI, see proc_open for details. */
444 sa.sa_handler = foobar;
445 sa.sa_flags = 0;
446 sigemptyset(&sa.sa_mask);
447 sigaction(SIGINT, &sa, NULL);
448#endif /* MIPS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000449#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000450 pause();
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000451#else /* FREEBSD */
452 kill(getpid(), SIGSTOP); /* stop HERE */
Roland McGrath553a6092002-12-16 20:40:39 +0000453#endif /* FREEBSD */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000454#else /* !USE_PROCFS */
Roland McGrath553a6092002-12-16 20:40:39 +0000455 if (outf!=stderr)
Wichert Akkerman7987cdf2000-07-05 16:05:39 +0000456 close(fileno (outf));
Wichert Akkermanbd4125c2000-06-27 17:28:06 +0000457
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000458 if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
459 perror("strace: ptrace(PTRACE_TRACEME, ...)");
460 return -1;
461 }
462 if (debug)
463 kill(getpid(), SIGSTOP);
464
465 if (username != NULL || geteuid() == 0) {
466 uid_t run_euid = run_uid;
467 gid_t run_egid = run_gid;
468
469 if (statbuf.st_mode & S_ISUID)
470 run_euid = statbuf.st_uid;
471 if (statbuf.st_mode & S_ISGID)
472 run_egid = statbuf.st_gid;
473
474 /*
475 * It is important to set groups before we
476 * lose privileges on setuid.
477 */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +0000478 if (username != NULL) {
479 if (initgroups(username, run_gid) < 0) {
480 perror("initgroups");
481 exit(1);
482 }
483 if (setregid(run_gid, run_egid) < 0) {
484 perror("setregid");
485 exit(1);
486 }
487 if (setreuid(run_uid, run_euid) < 0) {
488 perror("setreuid");
489 exit(1);
490 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000491 }
492 }
493 else
494 setreuid(run_uid, run_uid);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000495#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000496
497 execv(pathname, &argv[optind]);
498 perror("strace: exec");
499 _exit(1);
500 break;
501 }
502 default:
503 if ((tcp = alloctcb(pid)) == NULL) {
504 fprintf(stderr, "tcb table full\n");
505 cleanup();
506 exit(1);
507 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000508#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000509 if (proc_open(tcp, 0) < 0) {
510 fprintf(stderr, "trouble opening proc file\n");
511 cleanup();
512 exit(1);
513 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000514#endif /* USE_PROCFS */
515#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000516 fake_execve(tcp, pathname, &argv[optind], environ);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000517#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000518 break;
519 }
520 }
521 else if (pflag_seen == 0)
522 usage(stderr, 1);
523
524 sigemptyset(&empty_set);
525 sigemptyset(&blocked_set);
526 sa.sa_handler = SIG_IGN;
527 sigemptyset(&sa.sa_mask);
528 sa.sa_flags = 0;
529 sigaction(SIGTTOU, &sa, NULL);
530 sigaction(SIGTTIN, &sa, NULL);
531 if (interactive) {
532 sigaddset(&blocked_set, SIGHUP);
533 sigaddset(&blocked_set, SIGINT);
534 sigaddset(&blocked_set, SIGQUIT);
535 sigaddset(&blocked_set, SIGPIPE);
536 sigaddset(&blocked_set, SIGTERM);
537 sa.sa_handler = interrupt;
538#ifdef SUNOS4
539 /* POSIX signals on sunos4.1 are a little broken. */
540 sa.sa_flags = SA_INTERRUPT;
541#endif /* SUNOS4 */
542 }
543 sigaction(SIGHUP, &sa, NULL);
544 sigaction(SIGINT, &sa, NULL);
545 sigaction(SIGQUIT, &sa, NULL);
546 sigaction(SIGPIPE, &sa, NULL);
547 sigaction(SIGTERM, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000548#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000549 sa.sa_handler = reaper;
550 sigaction(SIGCHLD, &sa, NULL);
Roland McGrath553a6092002-12-16 20:40:39 +0000551#else
552 /* Make sure SIGCHLD has the default action so that waitpid
553 definitely works without losing track of children. The user
554 should not have given us a bogus state to inherit, but he might
555 have. Arguably we should detect SIG_IGN here and pass it on
556 to children, but probably noone really needs that. */
557 sa.sa_handler = SIG_DFL;
558 sigaction(SIGCHLD, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000559#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000560
561 if (trace() < 0)
562 exit(1);
563 cleanup();
564 exit(0);
565}
566
567void
568newoutf(tcp)
569struct tcb *tcp;
570{
571 char name[MAXPATHLEN];
572 FILE *fp;
573
574 if (outfname && followfork > 1) {
575 sprintf(name, "%s.%u", outfname, tcp->pid);
576#ifndef SVR4
577 setreuid(geteuid(), getuid());
578#endif
579 fp = fopen(name, "w");
580#ifndef SVR4
581 setreuid(geteuid(), getuid());
582#endif
583 if (fp == NULL) {
584 perror("fopen");
585 return;
586 }
587 tcp->outf = fp;
588 }
589 return;
590}
591
592struct tcb *
593alloctcb(pid)
594int pid;
595{
596 int i;
597 struct tcb *tcp;
598
599 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
600 if ((tcp->flags & TCB_INUSE) == 0) {
601 tcp->pid = pid;
602 tcp->parent = NULL;
603 tcp->nchildren = 0;
604 tcp->flags = TCB_INUSE | TCB_STARTUP;
605 tcp->outf = outf; /* Initialise to current out file */
606 tcp->stime.tv_sec = 0;
607 tcp->stime.tv_usec = 0;
608 tcp->pfd = -1;
609 nprocs++;
610 return tcp;
611 }
612 }
613 return NULL;
614}
615
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000616#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000617int
618proc_open(tcp, attaching)
619struct tcb *tcp;
620int attaching;
621{
622 char proc[32];
623 long arg;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000624#ifdef SVR4
John Hughes19e49982001-10-19 08:59:12 +0000625 int i;
626 sysset_t syscalls;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000627 sigset_t signals;
628 fltset_t faults;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000629#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000630#ifndef HAVE_POLLABLE_PROCFS
631 static int last_pfd;
632#endif
633
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000634#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000635 /* Open the process pseudo-files in /proc. */
636 sprintf(proc, "/proc/%d/ctl", tcp->pid);
637 if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000638 perror("strace: open(\"/proc/...\", ...)");
639 return -1;
640 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000641 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
642 perror("F_GETFD");
643 return -1;
644 }
645 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
646 perror("F_SETFD");
647 return -1;
648 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000649 sprintf(proc, "/proc/%d/status", tcp->pid);
650 if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
651 perror("strace: open(\"/proc/...\", ...)");
652 return -1;
653 }
654 if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) {
655 perror("F_GETFD");
656 return -1;
657 }
658 if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) {
659 perror("F_SETFD");
660 return -1;
661 }
662 sprintf(proc, "/proc/%d/as", tcp->pid);
663 if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
664 perror("strace: open(\"/proc/...\", ...)");
665 return -1;
666 }
667 if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) {
668 perror("F_GETFD");
669 return -1;
670 }
671 if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) {
672 perror("F_SETFD");
673 return -1;
674 }
675#else
676 /* Open the process pseudo-file in /proc. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000677#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000678 sprintf(proc, "/proc/%d", tcp->pid);
679 if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000680#else /* FREEBSD */
681 sprintf(proc, "/proc/%d/mem", tcp->pid);
682 if ((tcp->pfd = open(proc, O_RDWR)) < 0) {
683#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000684 perror("strace: open(\"/proc/...\", ...)");
685 return -1;
686 }
687 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
688 perror("F_GETFD");
689 return -1;
690 }
691 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
692 perror("F_SETFD");
693 return -1;
694 }
695#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000696#ifdef FREEBSD
697 sprintf(proc, "/proc/%d/regs", tcp->pid);
698 if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) {
699 perror("strace: open(\"/proc/.../regs\", ...)");
700 return -1;
701 }
702 if (cflag) {
703 sprintf(proc, "/proc/%d/status", tcp->pid);
704 if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) {
705 perror("strace: open(\"/proc/.../status\", ...)");
706 return -1;
707 }
708 } else
709 tcp->pfd_status = -1;
710#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000711 rebuild_pollv();
712 if (!attaching) {
713 /*
714 * Wait for the child to pause. Because of a race
715 * condition we have to poll for the event.
716 */
717 for (;;) {
718 if (IOCTL_STATUS (tcp) < 0) {
719 perror("strace: PIOCSTATUS");
720 return -1;
721 }
722 if (tcp->status.PR_FLAGS & PR_ASLEEP)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000723 break;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000724 }
725 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000726#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000727 /* Stop the process so that we own the stop. */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000728 if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000729 perror("strace: PIOCSTOP");
730 return -1;
731 }
Roland McGrath553a6092002-12-16 20:40:39 +0000732#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000733#ifdef PIOCSET
734 /* Set Run-on-Last-Close. */
735 arg = PR_RLC;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000736 if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000737 perror("PIOCSET PR_RLC");
738 return -1;
739 }
740 /* Set or Reset Inherit-on-Fork. */
741 arg = PR_FORK;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000742 if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000743 perror("PIOC{SET,RESET} PR_FORK");
744 return -1;
745 }
746#else /* !PIOCSET */
Roland McGrath553a6092002-12-16 20:40:39 +0000747#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000748 if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
749 perror("PIOCSRLC");
750 return -1;
751 }
752 if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
753 perror("PIOC{S,R}FORK");
754 return -1;
755 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000756#else /* FREEBSD */
757 /* just unset the PF_LINGER flag for the Run-on-Last-Close. */
758 if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) {
759 perror("PIOCGFL");
760 return -1;
761 }
762 arg &= ~PF_LINGER;
763 if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) {
764 perror("PIOCSFL");
765 return -1;
766 }
767#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000768#endif /* !PIOCSET */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000769#ifndef FREEBSD
John Hughes19e49982001-10-19 08:59:12 +0000770 /* Enable all syscall entries we care about. */
771 premptyset(&syscalls);
772 for (i = 1; i < MAX_QUALS; ++i) {
773 if (i > (sizeof syscalls) * CHAR_BIT) break;
774 if (qual_flags [i] & QUAL_TRACE) praddset (&syscalls, i);
775 }
776 praddset (&syscalls, SYS_execve);
777 if (followfork) {
778 praddset (&syscalls, SYS_fork);
779#ifdef SYS_forkall
780 praddset (&syscalls, SYS_forkall);
781#endif
Roland McGrath553a6092002-12-16 20:40:39 +0000782#ifdef SYS_fork1
John Hughes19e49982001-10-19 08:59:12 +0000783 praddset (&syscalls, SYS_fork1);
784#endif
785#ifdef SYS_rfork1
786 praddset (&syscalls, SYS_rfork1);
787#endif
788#ifdef SYS_rforkall
789 praddset (&syscalls, SYS_rforkall);
790#endif
791 }
792 if (IOCTL(tcp->pfd, PIOCSENTRY, &syscalls) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000793 perror("PIOCSENTRY");
794 return -1;
795 }
John Hughes19e49982001-10-19 08:59:12 +0000796 /* Enable the syscall exits. */
797 if (IOCTL(tcp->pfd, PIOCSEXIT, &syscalls) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000798 perror("PIOSEXIT");
799 return -1;
800 }
John Hughes19e49982001-10-19 08:59:12 +0000801 /* Enable signals we care about. */
802 premptyset(&signals);
803 for (i = 1; i < MAX_QUALS; ++i) {
804 if (i > (sizeof signals) * CHAR_BIT) break;
805 if (qual_flags [i] & QUAL_SIGNAL) praddset (&signals, i);
806 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000807 if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000808 perror("PIOCSTRACE");
809 return -1;
810 }
John Hughes19e49982001-10-19 08:59:12 +0000811 /* Enable faults we care about */
812 premptyset(&faults);
813 for (i = 1; i < MAX_QUALS; ++i) {
814 if (i > (sizeof faults) * CHAR_BIT) break;
815 if (qual_flags [i] & QUAL_FAULT) praddset (&faults, i);
816 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000817 if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000818 perror("PIOCSFAULT");
819 return -1;
820 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000821#else /* FREEBSD */
822 /* set events flags. */
823 arg = S_SIG | S_SCE | S_SCX ;
824 if(ioctl(tcp->pfd, PIOCBIS, arg) < 0) {
825 perror("PIOCBIS");
826 return -1;
827 }
828#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000829 if (!attaching) {
830#ifdef MIPS
831 /*
832 * The SGI PRSABORT doesn't work for pause() so
833 * we send it a caught signal to wake it up.
834 */
835 kill(tcp->pid, SIGINT);
836#else /* !MIPS */
Roland McGrath553a6092002-12-16 20:40:39 +0000837#ifdef PRSABORT
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000838 /* The child is in a pause(), abort it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000839 arg = PRSABORT;
840 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000841 perror("PIOCRUN");
842 return -1;
843 }
Roland McGrath553a6092002-12-16 20:40:39 +0000844#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000845#endif /* !MIPS*/
846#ifdef FREEBSD
847 /* wake up the child if it received the SIGSTOP */
848 kill(tcp->pid, SIGCONT);
Roland McGrath553a6092002-12-16 20:40:39 +0000849#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000850 for (;;) {
851 /* Wait for the child to do something. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000852 if (IOCTL_WSTOP (tcp) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000853 perror("PIOCWSTOP");
854 return -1;
855 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000856 if (tcp->status.PR_WHY == PR_SYSENTRY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000857 tcp->flags &= ~TCB_INSYSCALL;
858 get_scno(tcp);
859 if (tcp->scno == SYS_execve)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000860 break;
861 }
862 /* Set it running: maybe execve will be next. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000863#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000864 arg = 0;
865 if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000866#else /* FREEBSD */
867 if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) {
Roland McGrath553a6092002-12-16 20:40:39 +0000868#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000869 perror("PIOCRUN");
870 return -1;
871 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000872#ifdef FREEBSD
873 /* handle the case where we "opened" the child before
874 it did the kill -STOP */
875 if (tcp->status.PR_WHY == PR_SIGNALLED &&
876 tcp->status.PR_WHAT == SIGSTOP)
877 kill(tcp->pid, SIGCONT);
Roland McGrath553a6092002-12-16 20:40:39 +0000878#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000879 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000880#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000881 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000882#else /* FREEBSD */
883 } else {
Roland McGrath553a6092002-12-16 20:40:39 +0000884 if (attaching < 2) {
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +0000885 /* We are attaching to an already running process.
886 * Try to figure out the state of the process in syscalls,
887 * to handle the first event well.
888 * This is done by having a look at the "wchan" property of the
889 * process, which tells where it is stopped (if it is). */
890 FILE * status;
891 char wchan[20]; /* should be enough */
Roland McGrath553a6092002-12-16 20:40:39 +0000892
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +0000893 sprintf(proc, "/proc/%d/status", tcp->pid);
894 status = fopen(proc, "r");
895 if (status &&
896 (fscanf(status, "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d"
897 "%*d,%*d %*d,%*d %19s", wchan) == 1) &&
898 strcmp(wchan, "nochan") && strcmp(wchan, "spread") &&
899 strcmp(wchan, "stopevent")) {
900 /* The process is asleep in the middle of a syscall.
901 Fake the syscall entry event */
902 tcp->flags &= ~(TCB_INSYSCALL|TCB_STARTUP);
903 tcp->status.PR_WHY = PR_SYSENTRY;
904 trace_syscall(tcp);
905 }
906 if (status)
907 fclose(status);
908 } /* otherwise it's a fork being followed */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000909 }
910#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000911#ifndef HAVE_POLLABLE_PROCFS
912 if (proc_poll_pipe[0] != -1)
913 proc_poller(tcp->pfd);
914 else if (nprocs > 1) {
915 proc_poll_open();
916 proc_poller(last_pfd);
917 proc_poller(tcp->pfd);
918 }
919 last_pfd = tcp->pfd;
920#endif /* !HAVE_POLLABLE_PROCFS */
921 return 0;
922}
923
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000924#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000925
926static struct tcb *
927pid2tcb(pid)
928int pid;
929{
930 int i;
931 struct tcb *tcp;
932
933 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
934 if (pid && tcp->pid != pid)
935 continue;
936 if (tcp->flags & TCB_INUSE)
937 return tcp;
938 }
939 return NULL;
940}
941
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000942#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000943
944static struct tcb *
945pfd2tcb(pfd)
946int pfd;
947{
948 int i;
949 struct tcb *tcp;
950
951 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
952 if (tcp->pfd != pfd)
953 continue;
954 if (tcp->flags & TCB_INUSE)
955 return tcp;
956 }
957 return NULL;
958}
959
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000960#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000961
962void
963droptcb(tcp)
964struct tcb *tcp;
965{
966 if (tcp->pid == 0)
967 return;
968 nprocs--;
969 tcp->pid = 0;
970 tcp->flags = 0;
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +0000971
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000972 if (tcp->pfd != -1) {
973 close(tcp->pfd);
974 tcp->pfd = -1;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000975#ifdef FREEBSD
976 if (tcp->pfd_reg != -1) {
977 close(tcp->pfd_reg);
978 tcp->pfd_reg = -1;
979 }
980 if (tcp->pfd_status != -1) {
981 close(tcp->pfd_status);
982 tcp->pfd_status = -1;
983 }
Roland McGrath553a6092002-12-16 20:40:39 +0000984#endif /* !FREEBSD */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000985#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000986 rebuild_pollv();
987#endif
988 }
989 if (tcp->parent != NULL) {
990 tcp->parent->nchildren--;
991 tcp->parent = NULL;
992 }
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +0000993
Wichert Akkerman822f0c92002-04-03 10:55:14 +0000994 if (outfname && followfork > 1 && tcp->outf)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000995 fclose(tcp->outf);
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +0000996
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000997 tcp->outf = 0;
998}
999
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001000#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001001
1002static int
1003resume(tcp)
1004struct tcb *tcp;
1005{
1006 if (tcp == NULL)
1007 return -1;
1008
1009 if (!(tcp->flags & TCB_SUSPENDED)) {
1010 fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid);
1011 return -1;
1012 }
1013 tcp->flags &= ~TCB_SUSPENDED;
1014
1015 if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) {
1016 perror("resume: ptrace(PTRACE_SYSCALL, ...)");
1017 return -1;
1018 }
1019
1020 if (!qflag)
1021 fprintf(stderr, "Process %u resumed\n", tcp->pid);
1022 return 0;
1023}
1024
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001025#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001026
1027/* detach traced process; continue with sig */
1028
1029static int
1030detach(tcp, sig)
1031struct tcb *tcp;
1032int sig;
1033{
1034 int error = 0;
1035#ifdef LINUX
1036 int status;
1037#endif
1038
1039 if (tcp->flags & TCB_BPTSET)
1040 sig = SIGKILL;
1041
1042#ifdef LINUX
1043 /*
1044 * Linux wrongly insists the child be stopped
Roland McGrath7bf10472002-12-16 20:42:50 +00001045 * before detaching. Arghh. We go through hoops
1046 * to make a clean break of things.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001047 */
Roland McGrath7bf10472002-12-16 20:42:50 +00001048#if defined(SPARC)
1049#undef PTRACE_DETACH
1050#define PTRACE_DETACH PTRACE_SUNDETACH
1051#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001052 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
1053 /* On a clear day, you can see forever. */
Roland McGrath7bf10472002-12-16 20:42:50 +00001054 }
1055 else if (errno != ESRCH) {
1056 /* Shouldn't happen. */
1057 perror("detach: ptrace(PTRACE_DETACH, ...)");
1058 }
1059 else if (kill(tcp->pid, 0) < 0) {
1060 if (errno != ESRCH)
1061 perror("detach: checking sanity");
1062 }
1063 else if (kill(tcp->pid, SIGSTOP) < 0) {
1064 if (errno != ESRCH)
1065 perror("detach: stopping child");
1066 }
1067 else {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001068 for (;;) {
Roland McGrath7508cb42002-12-17 10:48:05 +00001069#ifdef __WALL
1070 if (wait4(tcp->pid, &status, __WALL, NULL) < 0) {
1071 if (errno == ECHILD) /* Already gone. */
1072 break;
1073 if (errno != EINVAL) {
Roland McGrath553a6092002-12-16 20:40:39 +00001074 perror("detach: waiting");
Roland McGrath7508cb42002-12-17 10:48:05 +00001075 break;
1076 }
1077#endif /* __WALL */
1078 /* No __WALL here. */
1079 if (waitpid(tcp->pid, &status, 0) < 0) {
1080 if (errno != ECHILD) {
1081 perror("detach: waiting");
1082 break;
1083 }
1084#ifdef __WCLONE
1085 /* If no processes, try clones. */
1086 if (wait4(tcp->pid, &status, __WCLONE,
1087 NULL) < 0) {
1088 if (errno != ECHILD)
1089 perror("detach: waiting");
1090 break;
1091 }
1092#endif /* __WCLONE */
1093 }
1094#ifdef __WALL
Roland McGrath553a6092002-12-16 20:40:39 +00001095 }
Roland McGrath7508cb42002-12-17 10:48:05 +00001096#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001097 if (!WIFSTOPPED(status)) {
1098 /* Au revoir, mon ami. */
1099 break;
1100 }
1101 if (WSTOPSIG(status) == SIGSTOP) {
1102 if ((error = ptrace(PTRACE_DETACH,
Roland McGrath7bf10472002-12-16 20:42:50 +00001103 tcp->pid, (char *) 1, sig)) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001104 if (errno != ESRCH)
1105 perror("detach: ptrace(PTRACE_DETACH, ...)");
1106 /* I died trying. */
1107 }
1108 break;
1109 }
1110 if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1,
Roland McGrath7bf10472002-12-16 20:42:50 +00001111 WSTOPSIG(status) == SIGTRAP ?
1112 0 : WSTOPSIG(status))) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001113 if (errno != ESRCH)
1114 perror("detach: ptrace(PTRACE_CONT, ...)");
1115 break;
1116 }
1117 }
1118 }
Roland McGrath7bf10472002-12-16 20:42:50 +00001119#endif /* LINUX */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001120
1121#if defined(SUNOS4)
1122 /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
1123 if (sig && kill(tcp->pid, sig) < 0)
1124 perror("detach: kill");
1125 sig = 0;
1126 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0)
1127 perror("detach: ptrace(PTRACE_DETACH, ...)");
1128#endif /* SUNOS4 */
1129
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001130#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001131 if (waiting_parent(tcp))
1132 error = resume(tcp->parent);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001133#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001134
1135 if (!qflag)
1136 fprintf(stderr, "Process %u detached\n", tcp->pid);
1137
1138 droptcb(tcp);
1139 return error;
1140}
1141
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001142#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001143
1144static void
1145reaper(sig)
1146int sig;
1147{
1148 int pid;
1149 int status;
1150
1151 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
1152#if 0
1153 struct tcb *tcp;
1154
1155 tcp = pid2tcb(pid);
1156 if (tcp)
1157 droptcb(tcp);
1158#endif
1159 }
1160}
1161
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001162#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001163
1164static void
1165cleanup()
1166{
1167 int i;
1168 struct tcb *tcp;
1169
1170 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1171 if (!(tcp->flags & TCB_INUSE))
1172 continue;
1173 if (debug)
1174 fprintf(stderr,
1175 "cleanup: looking at pid %u\n", tcp->pid);
1176 if (tcp_last &&
1177 (!outfname || followfork < 2 || tcp_last == tcp)) {
1178 tprintf(" <unfinished ...>\n");
1179 tcp_last = NULL;
1180 }
1181 if (tcp->flags & TCB_ATTACHED)
1182 detach(tcp, 0);
1183 else {
1184 kill(tcp->pid, SIGCONT);
1185 kill(tcp->pid, SIGTERM);
1186 }
1187 }
1188 if (cflag)
1189 call_summary(outf);
1190}
1191
1192static void
1193interrupt(sig)
1194int sig;
1195{
1196 interrupted = 1;
1197}
1198
1199#ifndef HAVE_STRERROR
1200
1201#ifndef SYS_ERRLIST_DECLARED
1202extern int sys_nerr;
1203extern char *sys_errlist[];
1204#endif /* SYS_ERRLIST_DECLARED */
1205
1206const char *
1207strerror(errno)
1208int errno;
1209{
1210 static char buf[64];
1211
1212 if (errno < 1 || errno >= sys_nerr) {
1213 sprintf(buf, "Unknown error %d", errno);
1214 return buf;
1215 }
1216 return sys_errlist[errno];
1217}
1218
1219#endif /* HAVE_STERRROR */
1220
1221#ifndef HAVE_STRSIGNAL
1222
1223#ifndef SYS_SIGLIST_DECLARED
1224#ifdef HAVE__SYS_SIGLIST
1225 extern char *_sys_siglist[];
1226#else
1227 extern char *sys_siglist[];
1228#endif
1229#endif /* SYS_SIGLIST_DECLARED */
1230
1231const char *
1232strsignal(sig)
1233int sig;
1234{
1235 static char buf[64];
1236
1237 if (sig < 1 || sig >= NSIG) {
1238 sprintf(buf, "Unknown signal %d", sig);
1239 return buf;
1240 }
1241#ifdef HAVE__SYS_SIGLIST
1242 return _sys_siglist[sig];
1243#else
1244 return sys_siglist[sig];
1245#endif
1246}
1247
1248#endif /* HAVE_STRSIGNAL */
1249
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001250#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001251
1252static void
1253rebuild_pollv()
1254{
1255 int i, j;
1256 struct tcb *tcp;
1257
1258 for (i = j = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1259 if (!(tcp->flags & TCB_INUSE))
1260 continue;
1261 pollv[j].fd = tcp->pfd;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001262 pollv[j].events = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001263 j++;
1264 }
1265 if (j != nprocs) {
1266 fprintf(stderr, "strace: proc miscount\n");
1267 exit(1);
1268 }
1269}
1270
1271#ifndef HAVE_POLLABLE_PROCFS
1272
1273static void
1274proc_poll_open()
1275{
1276 int arg;
1277 int i;
1278
1279 if (pipe(proc_poll_pipe) < 0) {
1280 perror("pipe");
1281 exit(1);
1282 }
1283 for (i = 0; i < 2; i++) {
1284 if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) {
1285 perror("F_GETFD");
1286 exit(1);
1287 }
1288 if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) {
1289 perror("F_SETFD");
1290 exit(1);
1291 }
1292 }
1293}
1294
1295static int
1296proc_poll(pollv, nfds, timeout)
1297struct pollfd *pollv;
1298int nfds;
1299int timeout;
1300{
1301 int i;
1302 int n;
1303 struct proc_pollfd pollinfo;
1304
1305 if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
1306 return n;
1307 if (n != sizeof(struct proc_pollfd)) {
1308 fprintf(stderr, "panic: short read: %d\n", n);
1309 exit(1);
1310 }
1311 for (i = 0; i < nprocs; i++) {
1312 if (pollv[i].fd == pollinfo.fd)
1313 pollv[i].revents = pollinfo.revents;
1314 else
1315 pollv[i].revents = 0;
1316 }
1317 poller_pid = pollinfo.pid;
1318 return 1;
1319}
1320
1321static void
1322wakeup_handler(sig)
1323int sig;
1324{
1325}
1326
1327static void
1328proc_poller(pfd)
1329int pfd;
1330{
1331 struct proc_pollfd pollinfo;
1332 struct sigaction sa;
1333 sigset_t blocked_set, empty_set;
1334 int i;
1335 int n;
1336 struct rlimit rl;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001337#ifdef FREEBSD
1338 struct procfs_status pfs;
1339#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001340
1341 switch (fork()) {
1342 case -1:
1343 perror("fork");
1344 _exit(0);
1345 case 0:
1346 break;
1347 default:
1348 return;
1349 }
1350
1351 sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
1352 sa.sa_flags = 0;
1353 sigemptyset(&sa.sa_mask);
1354 sigaction(SIGHUP, &sa, NULL);
1355 sigaction(SIGINT, &sa, NULL);
1356 sigaction(SIGQUIT, &sa, NULL);
1357 sigaction(SIGPIPE, &sa, NULL);
1358 sigaction(SIGTERM, &sa, NULL);
1359 sa.sa_handler = wakeup_handler;
1360 sigaction(SIGUSR1, &sa, NULL);
1361 sigemptyset(&blocked_set);
1362 sigaddset(&blocked_set, SIGUSR1);
1363 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1364 sigemptyset(&empty_set);
1365
1366 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
1367 perror("getrlimit(RLIMIT_NOFILE, ...)");
1368 _exit(0);
1369 }
1370 n = rl.rlim_cur;
1371 for (i = 0; i < n; i++) {
1372 if (i != pfd && i != proc_poll_pipe[1])
1373 close(i);
1374 }
1375
1376 pollinfo.fd = pfd;
1377 pollinfo.pid = getpid();
1378 for (;;) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001379#ifndef FREEBSD
1380 if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
1381#else /* FREEBSD */
1382 if (ioctl(pfd, PIOCWSTOP, &pfs) < 0)
1383#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001384 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001385 switch (errno) {
1386 case EINTR:
1387 continue;
1388 case EBADF:
1389 pollinfo.revents = POLLERR;
1390 break;
1391 case ENOENT:
1392 pollinfo.revents = POLLHUP;
1393 break;
1394 default:
1395 perror("proc_poller: PIOCWSTOP");
1396 }
1397 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1398 _exit(0);
1399 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001400 pollinfo.revents = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001401 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1402 sigsuspend(&empty_set);
1403 }
1404}
1405
1406#endif /* !HAVE_POLLABLE_PROCFS */
1407
1408static int
1409choose_pfd()
1410{
1411 int i, j;
1412 struct tcb *tcp;
1413
1414 static int last;
1415
1416 if (followfork < 2 &&
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001417 last < nprocs && (pollv[last].revents & POLLWANT)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001418 /*
1419 * The previous process is ready to run again. We'll
1420 * let it do so if it is currently in a syscall. This
1421 * heuristic improves the readability of the trace.
1422 */
1423 tcp = pfd2tcb(pollv[last].fd);
1424 if (tcp && (tcp->flags & TCB_INSYSCALL))
1425 return pollv[last].fd;
1426 }
1427
1428 for (i = 0; i < nprocs; i++) {
1429 /* Let competing children run round robin. */
1430 j = (i + last + 1) % nprocs;
1431 if (pollv[j].revents & (POLLHUP | POLLERR)) {
1432 tcp = pfd2tcb(pollv[j].fd);
1433 if (!tcp) {
1434 fprintf(stderr, "strace: lost proc\n");
1435 exit(1);
1436 }
1437 droptcb(tcp);
1438 return -1;
1439 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001440 if (pollv[j].revents & POLLWANT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001441 last = j;
1442 return pollv[j].fd;
1443 }
1444 }
1445 fprintf(stderr, "strace: nothing ready\n");
1446 exit(1);
1447}
1448
1449static int
1450trace()
1451{
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001452#ifdef POLL_HACK
John Hughesd870b3c2002-05-21 11:24:18 +00001453 struct tcb *in_syscall = NULL;
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001454#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001455 struct tcb *tcp;
1456 int pfd;
1457 int what;
1458 int ioctl_result = 0, ioctl_errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001459 long arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001460
1461 for (;;) {
1462 if (interactive)
1463 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1464
1465 if (nprocs == 0)
1466 break;
1467
1468 switch (nprocs) {
1469 case 1:
1470#ifndef HAVE_POLLABLE_PROCFS
1471 if (proc_poll_pipe[0] == -1) {
1472#endif
1473 tcp = pid2tcb(0);
1474 if (!tcp)
1475 continue;
1476 pfd = tcp->pfd;
1477 if (pfd == -1)
1478 continue;
1479 break;
1480#ifndef HAVE_POLLABLE_PROCFS
1481 }
1482 /* fall through ... */
1483#endif /* !HAVE_POLLABLE_PROCFS */
1484 default:
1485#ifdef HAVE_POLLABLE_PROCFS
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001486#ifdef POLL_HACK
1487 /* On some systems (e.g. UnixWare) we get too much ugly
1488 "unfinished..." stuff when multiple proceses are in
1489 syscalls. Here's a nasty hack */
Roland McGrath553a6092002-12-16 20:40:39 +00001490
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001491 if (in_syscall) {
1492 struct pollfd pv;
1493 tcp = in_syscall;
1494 in_syscall = NULL;
1495 pv.fd = tcp->pfd;
1496 pv.events = POLLWANT;
1497 if ((what = poll (&pv, 1, 1)) < 0) {
1498 if (interrupted)
1499 return 0;
1500 continue;
1501 }
1502 else if (what == 1 && pv.revents & POLLWANT) {
1503 goto FOUND;
1504 }
1505 }
1506#endif
1507
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001508 if (poll(pollv, nprocs, INFTIM) < 0) {
1509 if (interrupted)
1510 return 0;
1511 continue;
1512 }
1513#else /* !HAVE_POLLABLE_PROCFS */
1514 if (proc_poll(pollv, nprocs, INFTIM) < 0) {
1515 if (interrupted)
1516 return 0;
1517 continue;
1518 }
1519#endif /* !HAVE_POLLABLE_PROCFS */
1520 pfd = choose_pfd();
1521 if (pfd == -1)
1522 continue;
1523 break;
1524 }
1525
1526 /* Look up `pfd' in our table. */
1527 if ((tcp = pfd2tcb(pfd)) == NULL) {
1528 fprintf(stderr, "unknown pfd: %u\n", pfd);
1529 exit(1);
1530 }
John Hughesb6643082002-05-23 11:02:22 +00001531#ifdef POLL_HACK
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001532 FOUND:
John Hughesb6643082002-05-23 11:02:22 +00001533#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001534 /* Get the status of the process. */
1535 if (!interrupted) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001536#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001537 ioctl_result = IOCTL_WSTOP (tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001538#else /* FREEBSD */
1539 /* Thanks to some scheduling mystery, the first poller
1540 sometimes waits for the already processed end of fork
1541 event. Doing a non blocking poll here solves the problem. */
1542 if (proc_poll_pipe[0] != -1)
1543 ioctl_result = IOCTL_STATUS (tcp);
1544 else
1545 ioctl_result = IOCTL_WSTOP (tcp);
Roland McGrath553a6092002-12-16 20:40:39 +00001546#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001547 ioctl_errno = errno;
1548#ifndef HAVE_POLLABLE_PROCFS
1549 if (proc_poll_pipe[0] != -1) {
1550 if (ioctl_result < 0)
1551 kill(poller_pid, SIGKILL);
1552 else
1553 kill(poller_pid, SIGUSR1);
1554 }
1555#endif /* !HAVE_POLLABLE_PROCFS */
1556 }
1557 if (interrupted)
1558 return 0;
1559
1560 if (interactive)
1561 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1562
1563 if (ioctl_result < 0) {
1564 /* Find out what happened if it failed. */
1565 switch (ioctl_errno) {
1566 case EINTR:
1567 case EBADF:
1568 continue;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001569#ifdef FREEBSD
1570 case ENOTTY:
Roland McGrath553a6092002-12-16 20:40:39 +00001571#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001572 case ENOENT:
1573 droptcb(tcp);
1574 continue;
1575 default:
1576 perror("PIOCWSTOP");
1577 exit(1);
1578 }
1579 }
1580
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +00001581#ifdef FREEBSD
1582 if ((tcp->flags & TCB_STARTUP) && (tcp->status.PR_WHY == PR_SYSEXIT)) {
1583 /* discard first event for a syscall we never entered */
1584 IOCTL (tcp->pfd, PIOCRUN, 0);
1585 continue;
1586 }
Roland McGrath553a6092002-12-16 20:40:39 +00001587#endif
1588
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001589 /* clear the just started flag */
1590 tcp->flags &= ~TCB_STARTUP;
1591
1592 /* set current output file */
1593 outf = tcp->outf;
1594
1595 if (cflag) {
1596 struct timeval stime;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001597#ifdef FREEBSD
1598 char buf[1024];
1599 int len;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001600
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001601 if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) {
1602 buf[len] = '\0';
1603 sscanf(buf,
1604 "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld",
1605 &stime.tv_sec, &stime.tv_usec);
1606 } else
1607 stime.tv_sec = stime.tv_usec = 0;
Roland McGrath553a6092002-12-16 20:40:39 +00001608#else /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001609 stime.tv_sec = tcp->status.pr_stime.tv_sec;
1610 stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001611#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001612 tv_sub(&tcp->dtime, &stime, &tcp->stime);
1613 tcp->stime = stime;
1614 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001615 what = tcp->status.PR_WHAT;
1616 switch (tcp->status.PR_WHY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001617#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001618 case PR_REQUESTED:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001619 if (tcp->status.PR_FLAGS & PR_ASLEEP) {
1620 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001621 if (trace_syscall(tcp) < 0) {
1622 fprintf(stderr, "syscall trouble\n");
1623 exit(1);
1624 }
1625 }
1626 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001627#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001628 case PR_SYSENTRY:
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001629#ifdef POLL_HACK
1630 in_syscall = tcp;
1631#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001632 case PR_SYSEXIT:
1633 if (trace_syscall(tcp) < 0) {
1634 fprintf(stderr, "syscall trouble\n");
1635 exit(1);
1636 }
1637 break;
1638 case PR_SIGNALLED:
1639 if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) {
1640 printleader(tcp);
1641 tprintf("--- %s (%s) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001642 signame(what), strsignal(what));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001643 printtrailer(tcp);
John Hughes58265892001-10-18 15:13:53 +00001644#ifdef PR_INFO
1645 if (tcp->status.PR_INFO.si_signo == what) {
1646 printleader(tcp);
1647 tprintf(" siginfo=");
1648 printsiginfo(&tcp->status.PR_INFO, 1);
1649 printtrailer(tcp);
1650 }
1651#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001652 }
1653 break;
1654 case PR_FAULTED:
1655 if (!cflag && (qual_flags[what] & QUAL_FAULT)) {
1656 printleader(tcp);
1657 tprintf("=== FAULT %d ===", what);
1658 printtrailer(tcp);
1659 }
1660 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001661#ifdef FREEBSD
1662 case 0: /* handle case we polled for nothing */
1663 continue;
Roland McGrath553a6092002-12-16 20:40:39 +00001664#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001665 default:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001666 fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001667 exit(1);
1668 break;
1669 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001670 arg = 0;
Roland McGrath553a6092002-12-16 20:40:39 +00001671#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001672 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Roland McGrath553a6092002-12-16 20:40:39 +00001673#else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001674 if (IOCTL (tcp->pfd, PIOCRUN, 0) < 0) {
Roland McGrath553a6092002-12-16 20:40:39 +00001675#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001676 perror("PIOCRUN");
1677 exit(1);
1678 }
1679 }
1680 return 0;
1681}
1682
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001683#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001684
1685static int
1686trace()
1687{
1688 int pid;
1689 int wait_errno;
1690 int status;
1691 struct tcb *tcp;
1692#ifdef LINUX
1693 struct rusage ru;
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001694#ifdef __WALL
1695 static int wait4_options = __WALL;
1696#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001697#endif /* LINUX */
1698
1699 while (nprocs != 0) {
1700 if (interactive)
1701 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1702#ifdef LINUX
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001703#ifdef __WALL
1704 pid = wait4(-1, &status, wait4_options, cflag ? &ru : NULL);
Roland McGrath5bc05552002-12-17 04:50:47 +00001705 if (pid < 0 && (wait4_options & __WALL) && errno == EINVAL) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001706 /* this kernel does not support __WALL */
1707 wait4_options &= ~__WALL;
1708 errno = 0;
1709 pid = wait4(-1, &status, wait4_options,
1710 cflag ? &ru : NULL);
1711 }
Roland McGrath5bc05552002-12-17 04:50:47 +00001712 if (pid < 0 && !(wait4_options & __WALL) && errno == ECHILD) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001713 /* most likely a "cloned" process */
1714 pid = wait4(-1, &status, __WCLONE,
1715 cflag ? &ru : NULL);
1716 if (pid == -1) {
1717 fprintf(stderr, "strace: clone wait4 "
1718 "failed: %s\n", strerror(errno));
1719 }
1720 }
1721#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001722 pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001723#endif /* __WALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001724#endif /* LINUX */
1725#ifdef SUNOS4
1726 pid = wait(&status);
1727#endif /* SUNOS4 */
1728 wait_errno = errno;
1729 if (interactive)
1730 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1731
1732 if (interrupted)
1733 return 0;
1734
1735 if (pid == -1) {
1736 switch (wait_errno) {
1737 case EINTR:
1738 continue;
1739 case ECHILD:
1740 /*
1741 * We would like to verify this case
1742 * but sometimes a race in Solbourne's
1743 * version of SunOS sometimes reports
1744 * ECHILD before sending us SIGCHILD.
1745 */
1746#if 0
1747 if (nprocs == 0)
1748 return 0;
1749 fprintf(stderr, "strace: proc miscount\n");
1750 exit(1);
1751#endif
1752 return 0;
1753 default:
1754 errno = wait_errno;
1755 perror("strace: wait");
1756 return -1;
1757 }
1758 }
1759 if (debug)
1760 fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
1761
1762 /* Look up `pid' in our table. */
1763 if ((tcp = pid2tcb(pid)) == NULL) {
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001764#if 0 /* XXX davidm */ /* WTA: disabled again */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001765 struct tcb *tcpchild;
1766
1767 if ((tcpchild = alloctcb(pid)) == NULL) {
1768 fprintf(stderr, " [tcb table full]\n");
1769 kill(pid, SIGKILL); /* XXX */
1770 return 0;
1771 }
1772 tcpchild->flags |= TCB_ATTACHED;
1773 newoutf(tcpchild);
1774 tcp->nchildren++;
1775 if (!qflag)
1776 fprintf(stderr, "Process %d attached\n", pid);
1777#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001778 fprintf(stderr, "unknown pid: %u\n", pid);
1779 if (WIFSTOPPED(status))
1780 ptrace(PTRACE_CONT, pid, (char *) 1, 0);
1781 exit(1);
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001782#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001783 }
1784 /* set current output file */
1785 outf = tcp->outf;
1786 if (cflag) {
1787#ifdef LINUX
1788 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
1789 tcp->stime = ru.ru_stime;
1790#endif /* !LINUX */
1791 }
1792
1793 if (tcp->flags & TCB_SUSPENDED) {
1794 /*
1795 * Apparently, doing any ptrace() call on a stopped
1796 * process, provokes the kernel to report the process
1797 * status again on a subsequent wait(), even if the
1798 * process has not been actually restarted.
1799 * Since we have inspected the arguments of suspended
1800 * processes we end up here testing for this case.
1801 */
1802 continue;
1803 }
1804 if (WIFSIGNALED(status)) {
1805 if (!cflag
1806 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
1807 printleader(tcp);
1808 tprintf("+++ killed by %s +++",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001809 signame(WTERMSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001810 printtrailer(tcp);
1811 }
1812 droptcb(tcp);
1813 continue;
1814 }
1815 if (WIFEXITED(status)) {
1816 if (debug)
1817 fprintf(stderr, "pid %u exited\n", pid);
1818 if (tcp->flags & TCB_ATTACHED)
1819 fprintf(stderr,
1820 "PANIC: attached pid %u exited\n",
1821 pid);
1822 droptcb(tcp);
1823 continue;
1824 }
1825 if (!WIFSTOPPED(status)) {
1826 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
1827 droptcb(tcp);
1828 continue;
1829 }
1830 if (debug)
1831 fprintf(stderr, "pid %u stopped, [%s]\n",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001832 pid, signame(WSTOPSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001833
1834 if (tcp->flags & TCB_STARTUP) {
1835 /*
1836 * This flag is there to keep us in sync.
1837 * Next time this process stops it should
1838 * really be entering a system call.
1839 */
1840 tcp->flags &= ~TCB_STARTUP;
1841 if (tcp->flags & TCB_ATTACHED) {
1842 /*
1843 * Interestingly, the process may stop
1844 * with STOPSIG equal to some other signal
1845 * than SIGSTOP if we happend to attach
1846 * just before the process takes a signal.
1847 */
1848 if (!WIFSTOPPED(status)) {
1849 fprintf(stderr,
1850 "pid %u not stopped\n", pid);
1851 detach(tcp, WSTOPSIG(status));
1852 continue;
1853 }
1854 }
1855 else {
1856#ifdef SUNOS4
1857 /* A child of us stopped at exec */
1858 if (WSTOPSIG(status) == SIGTRAP && followvfork)
1859 fixvfork(tcp);
1860#endif /* SUNOS4 */
1861 }
1862 if (tcp->flags & TCB_BPTSET) {
1863 if (clearbpt(tcp) < 0) /* Pretty fatal */ {
1864 droptcb(tcp);
1865 cleanup();
1866 return -1;
1867 }
1868 }
1869 goto tracing;
1870 }
1871
1872 if (WSTOPSIG(status) != SIGTRAP) {
1873 if (WSTOPSIG(status) == SIGSTOP &&
1874 (tcp->flags & TCB_SIGTRAPPED)) {
1875 /*
1876 * Trapped attempt to block SIGTRAP
1877 * Hope we are back in control now.
1878 */
1879 tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
1880 if (ptrace(PTRACE_SYSCALL,
1881 pid, (char *) 1, 0) < 0) {
1882 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1883 cleanup();
1884 return -1;
1885 }
1886 continue;
1887 }
1888 if (!cflag
1889 && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001890 unsigned long addr = 0, pc = 0;
1891#ifdef PT_GETSIGINFO
1892# define PSR_RI 41
1893 struct siginfo si;
1894 unsigned long psr;
1895
1896 upeek(pid, PT_CR_IPSR, &psr);
1897 upeek(pid, PT_CR_IIP, &pc);
1898
1899 pc += (psr >> PSR_RI) & 0x3;
1900 ptrace(PT_GETSIGINFO, pid, 0, (long) &si);
1901 addr = (unsigned long) si.si_addr;
1902#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001903 printleader(tcp);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001904 tprintf("--- %s (%s) @ %lx (%lx) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001905 signame(WSTOPSIG(status)),
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001906 strsignal(WSTOPSIG(status)), pc, addr);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001907 printtrailer(tcp);
1908 }
1909 if ((tcp->flags & TCB_ATTACHED) &&
1910 !sigishandled(tcp, WSTOPSIG(status))) {
1911 detach(tcp, WSTOPSIG(status));
1912 continue;
1913 }
1914 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1,
1915 WSTOPSIG(status)) < 0) {
1916 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1917 cleanup();
1918 return -1;
1919 }
1920 tcp->flags &= ~TCB_SUSPENDED;
1921 continue;
1922 }
1923 if (trace_syscall(tcp) < 0) {
1924 if (tcp->flags & TCB_ATTACHED)
1925 detach(tcp, 0);
1926 else {
1927 ptrace(PTRACE_KILL,
1928 tcp->pid, (char *) 1, SIGTERM);
1929 droptcb(tcp);
1930 }
1931 continue;
1932 }
1933 if (tcp->flags & TCB_EXITING) {
1934 if (tcp->flags & TCB_ATTACHED)
1935 detach(tcp, 0);
1936 else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) {
1937 perror("strace: ptrace(PTRACE_CONT, ...)");
1938 cleanup();
1939 return -1;
1940 }
1941 continue;
1942 }
1943 if (tcp->flags & TCB_SUSPENDED) {
1944 if (!qflag)
1945 fprintf(stderr, "Process %u suspended\n", pid);
1946 continue;
1947 }
1948 tracing:
1949 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) {
1950 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1951 cleanup();
1952 return -1;
1953 }
1954 }
1955 return 0;
1956}
1957
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001958#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001959
1960static int curcol;
1961
1962#ifdef __STDC__
1963#include <stdarg.h>
1964#define VA_START(a, b) va_start(a, b)
1965#else
1966#include <varargs.h>
1967#define VA_START(a, b) va_start(a)
1968#endif
1969
1970void
1971#ifdef __STDC__
1972tprintf(const char *fmt, ...)
1973#else
1974tprintf(fmt, va_alist)
1975char *fmt;
1976va_dcl
1977#endif
1978{
1979 va_list args;
1980
1981 VA_START(args, fmt);
1982 if (outf)
1983 curcol += vfprintf(outf, fmt, args);
1984 va_end(args);
1985 return;
1986}
1987
1988void
1989printleader(tcp)
1990struct tcb *tcp;
1991{
1992 if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) {
1993 tcp_last->flags |= TCB_REPRINT;
1994 tprintf(" <unfinished ...>\n");
1995 }
1996 curcol = 0;
1997 if ((followfork == 1 || pflag_seen > 1) && outfname)
1998 tprintf("%-5d ", tcp->pid);
1999 else if (nprocs > 1 && !outfname)
2000 tprintf("[pid %5u] ", tcp->pid);
2001 if (tflag) {
2002 char str[sizeof("HH:MM:SS")];
2003 struct timeval tv, dtv;
2004 static struct timeval otv;
2005
2006 gettimeofday(&tv, NULL);
2007 if (rflag) {
2008 if (otv.tv_sec == 0)
2009 otv = tv;
2010 tv_sub(&dtv, &tv, &otv);
2011 tprintf("%6ld.%06ld ",
2012 (long) dtv.tv_sec, (long) dtv.tv_usec);
2013 otv = tv;
2014 }
2015 else if (tflag > 2) {
2016 tprintf("%ld.%06ld ",
2017 (long) tv.tv_sec, (long) tv.tv_usec);
2018 }
2019 else {
2020 time_t local = tv.tv_sec;
2021 strftime(str, sizeof(str), "%T", localtime(&local));
2022 if (tflag > 1)
2023 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
2024 else
2025 tprintf("%s ", str);
2026 }
2027 }
2028 if (iflag)
2029 printcall(tcp);
2030}
2031
2032void
2033tabto(col)
2034int col;
2035{
2036 if (curcol < col)
2037 tprintf("%*s", col - curcol, "");
2038}
2039
2040void
2041printtrailer(tcp)
2042struct tcb *tcp;
2043{
2044 tprintf("\n");
2045 tcp_last = NULL;
2046}
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002047
Wichert Akkermanea78f0f1999-11-29 15:34:02 +00002048#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002049
2050int mp_ioctl (int fd, int cmd, void *arg, int size) {
2051
2052 struct iovec iov[2];
2053 int n = 1;
Roland McGrath553a6092002-12-16 20:40:39 +00002054
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002055 iov[0].iov_base = &cmd;
2056 iov[0].iov_len = sizeof cmd;
2057 if (arg) {
2058 ++n;
2059 iov[1].iov_base = arg;
2060 iov[1].iov_len = size;
2061 }
Roland McGrath553a6092002-12-16 20:40:39 +00002062
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002063 return writev (fd, iov, n);
2064}
2065
2066#endif