blob: 3c178ce2d3194f4fce488b6145c374eed1ba3816 [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>
46
47#ifdef SVR4
48#include <sys/stropts.h>
49#include <poll.h>
Wichert Akkermanea78f0f1999-11-29 15:34:02 +000050#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000051#include <sys/uio.h>
52#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000053#endif
54
55int debug = 0, followfork = 0, followvfork = 0, interactive = 0;
56int rflag = 0, tflag = 0, dtime = 0, cflag = 0;
57int iflag = 0, xflag = 0, qflag = 0;
58int pflag_seen = 0;
59
60char *username = NULL;
61uid_t run_uid;
62gid_t run_gid;
63
64int acolumn = DEFAULT_ACOLUMN;
65int max_strlen = DEFAULT_STRLEN;
66char *outfname = NULL;
67FILE *outf;
68struct tcb tcbtab[MAX_PROCS];
69int nprocs;
70char *progname;
71extern char version[];
72extern char **environ;
73
74static struct tcb *pid2tcb P((int pid));
75static int trace P((void));
76static void cleanup P((void));
77static void interrupt P((int sig));
78static sigset_t empty_set, blocked_set;
79
80#ifdef HAVE_SIG_ATOMIC_T
81static volatile sig_atomic_t interrupted;
82#else /* !HAVE_SIG_ATOMIC_T */
83#ifdef __STDC__
84static volatile int interrupted;
85#else /* !__STDC__ */
86static int interrupted;
87#endif /* !__STDC__ */
88#endif /* !HAVE_SIG_ATOMIC_T */
89
90#ifdef SVR4
91
92static struct tcb *pfd2tcb P((int pfd));
93static void reaper P((int sig));
94static void rebuild_pollv P((void));
Wichert Akkermane68d61c1999-06-28 13:17:16 +000095struct pollfd pollv[MAX_PROCS];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000096
97#ifndef HAVE_POLLABLE_PROCFS
98
99static void proc_poll_open P((void));
100static void proc_poller P((int pfd));
101
102struct proc_pollfd {
103 int fd;
104 int revents;
105 int pid;
106};
107
108static int poller_pid;
109static int proc_poll_pipe[2] = { -1, -1 };
110
111#endif /* !HAVE_POLLABLE_PROCFS */
112
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000113#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000114#define POLLWANT POLLWRNORM
115#else
116#define POLLWANT POLLPRI
117#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000118#endif /* SVR4 */
119
120static void
121usage(ofp, exitval)
122FILE *ofp;
123int exitval;
124{
125 fprintf(ofp, "\
126usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\
127 [-p pid] ... [-s strsize] [-u username] [command [arg ...]]\n\
128 or: strace -c [-e expr] ... [-O overhead] [-S sortby] [command [arg ...]]\n\
129-c -- count time, calls, and errors for each syscall and report summary\n\
130-f -- follow forks, -ff -- with output into separate files\n\
131-F -- attempt to follow vforks, -h -- print help message\n\
132-i -- print instruction pointer at time of syscall\n\
133-q -- suppress messages about attaching, detaching, etc.\n\
134-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
135-T -- print time spent in each syscall, -V -- print version\n\
136-v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\
137-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
138-a column -- alignment COLUMN for printing syscall results (default %d)\n\
139-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
140 options: trace, abbrev, verbose, raw, signal, read, or write\n\
141-o file -- send trace output to FILE instead of stderr\n\
142-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
143-p pid -- trace process with process id PID, may be repeated\n\
144-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
145-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
146-u username -- run command as username handling setuid and/or setgid\n\
147", DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
148 exit(exitval);
149}
150
151#ifdef SVR4
152#ifdef MIPS
153void
154foobar()
155{
156}
157#endif /* MIPS */
158#endif /* SVR4 */
159
160int
161main(argc, argv)
162int argc;
163char *argv[];
164{
165 extern int optind;
166 extern char *optarg;
167 struct tcb *tcp;
168 int c, pid = 0;
169 struct sigaction sa;
170
171 static char buf[BUFSIZ];
172
173 progname = argv[0];
174 outf = stderr;
175 interactive = 1;
176 qualify("trace=all");
177 qualify("abbrev=all");
178 qualify("verbose=all");
179 qualify("signal=all");
180 set_sortby(DEFAULT_SORTBY);
181 set_personality(DEFAULT_PERSONALITY);
182 while ((c = getopt(argc, argv,
183 "+cdfFhiqrtTvVxa:e:o:O:p:s:S:u:")) != EOF) {
184 switch (c) {
185 case 'c':
186 cflag++;
187 dtime++;
188 break;
189 case 'd':
190 debug++;
191 break;
192 case 'f':
193 followfork++;
194 break;
195 case 'F':
196 followvfork++;
197 break;
198 case 'h':
199 usage(stdout, 0);
200 break;
201 case 'i':
202 iflag++;
203 break;
204 case 'q':
205 qflag++;
206 break;
207 case 'r':
208 rflag++;
209 tflag++;
210 break;
211 case 't':
212 tflag++;
213 break;
214 case 'T':
215 dtime++;
216 break;
217 case 'x':
218 xflag++;
219 break;
220 case 'v':
221 qualify("abbrev=none");
222 break;
223 case 'V':
224 printf("%s\n", version);
225 exit(0);
226 break;
227 case 'a':
228 acolumn = atoi(optarg);
229 break;
230 case 'e':
231 qualify(optarg);
232 break;
233 case 'o':
234 outfname = strdup(optarg);
235 break;
236 case 'O':
237 set_overhead(atoi(optarg));
238 break;
239 case 'p':
240 if ((pid = atoi(optarg)) == 0) {
241 fprintf(stderr, "%s: Invalid process id: %s\n",
242 progname, optarg);
243 break;
244 }
245 if (pid == getpid()) {
Wichert Akkerman54a47671999-10-17 00:57:34 +0000246 fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000247 break;
248 }
249 if ((tcp = alloctcb(pid)) == NULL) {
250 fprintf(stderr, "%s: tcb table full, please recompile strace\n",
251 progname);
252 exit(1);
253 }
254 tcp->flags |= TCB_ATTACHED;
255 pflag_seen++;
256 break;
257 case 's':
258 max_strlen = atoi(optarg);
259 break;
260 case 'S':
261 set_sortby(optarg);
262 break;
263 case 'u':
264 username = strdup(optarg);
265 break;
266 default:
267 usage(stderr, 1);
268 break;
269 }
270 }
271
272 /* See if they want to run as another user. */
273 if (username != NULL) {
274 struct passwd *pent;
275
276 if (getuid() != 0 || geteuid() != 0) {
277 fprintf(stderr,
278 "%s: you must be root to use the -u option\n",
279 progname);
280 exit(1);
281 }
282 if ((pent = getpwnam(username)) == NULL) {
283 fprintf(stderr, "%s: cannot find user `%s'\n",
284 progname, optarg);
285 exit(1);
286 }
287 run_uid = pent->pw_uid;
288 run_gid = pent->pw_gid;
289 }
290 else {
291 run_uid = getuid();
292 run_gid = getgid();
293 }
294
295#ifndef SVR4
296 setreuid(geteuid(), getuid());
297#endif
298
299 /* See if they want to pipe the output. */
300 if (outfname && (outfname[0] == '|' || outfname[0] == '!')) {
301 if ((outf = popen(outfname + 1, "w")) == NULL) {
302 fprintf(stderr, "%s: can't popen '%s': %s\n",
303 progname, outfname + 1, strerror(errno));
304 exit(1);
305 }
306 free(outfname);
307 outfname = NULL;
308 }
309
310 /* Check if they want to redirect the output. */
311 if (outfname) {
312 if ((outf = fopen(outfname, "w")) == NULL) {
313 fprintf(stderr, "%s: can't fopen '%s': %s\n",
314 progname, outfname, strerror(errno));
315 exit(1);
316 }
317 }
318
319#ifndef SVR4
320 setreuid(geteuid(), getuid());
321#endif
322
323 if (!outfname) {
324 qflag = 1;
325 setvbuf(outf, buf, _IOLBF, BUFSIZ);
326 }
327 else if (optind < argc)
328 interactive = 0;
329 else
330 qflag = 1;
331
332 for (c = 0, tcp = tcbtab; c < MAX_PROCS; c++, tcp++) {
333 /* Reinitialize the output since it may have changed. */
334 tcp->outf = outf;
335 if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
336 continue;
337#ifdef SVR4
338 if (proc_open(tcp, 1) < 0) {
339 fprintf(stderr, "trouble opening proc file\n");
340 droptcb(tcp);
341 continue;
342 }
343#else /* !SVR4 */
344 if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
345 perror("attach: ptrace(PTRACE_ATTACH, ...)");
346 droptcb(tcp);
347 continue;
348 }
349#endif /* !SVR4 */
350 if (!qflag)
351 fprintf(stderr,
352 "Process %u attached - interrupt to quit\n",
353 pid);
354 }
355
356 if (optind < argc) {
357 struct stat statbuf;
358 char *filename;
359 char pathname[MAXPATHLEN];
360
361 filename = argv[optind];
362 if (strchr(filename, '/'))
363 strcpy(pathname, filename);
364#ifdef USE_DEBUGGING_EXEC
365 /*
366 * Debuggers customarily check the current directory
367 * first regardless of the path but doing that gives
368 * security geeks a panic attack.
369 */
370 else if (stat(filename, &statbuf) == 0)
371 strcpy(pathname, filename);
372#endif /* USE_DEBUGGING_EXEC */
373 else {
374 char *path;
375 int m, n, len;
376
377 for (path = getenv("PATH"); path && *path; path += m) {
378 if (strchr(path, ':')) {
379 n = strchr(path, ':') - path;
380 m = n + 1;
381 }
382 else
383 m = n = strlen(path);
384 if (n == 0) {
385 getcwd(pathname, MAXPATHLEN);
386 len = strlen(pathname);
387 }
388 else {
389 strncpy(pathname, path, n);
390 len = n;
391 }
392 if (len && pathname[len - 1] != '/')
393 pathname[len++] = '/';
394 strcpy(pathname + len, filename);
395 if (stat(pathname, &statbuf) == 0)
396 break;
397 }
398 }
399 if (stat(pathname, &statbuf) < 0) {
400 fprintf(stderr, "%s: %s: command not found\n",
401 progname, filename);
402 exit(1);
403 }
404 switch (pid = fork()) {
405 case -1:
406 perror("strace: fork");
407 cleanup();
408 exit(1);
409 break;
410 case 0: {
411#ifdef SVR4
Wichert Akkerman789ed351999-06-14 10:45:01 +0000412 if (outf != stderr) close (fileno (outf));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000413#ifdef MIPS
414 /* Kludge for SGI, see proc_open for details. */
415 sa.sa_handler = foobar;
416 sa.sa_flags = 0;
417 sigemptyset(&sa.sa_mask);
418 sigaction(SIGINT, &sa, NULL);
419#endif /* MIPS */
420 pause();
421#else /* !SVR4 */
422 if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
423 perror("strace: ptrace(PTRACE_TRACEME, ...)");
424 return -1;
425 }
426 if (debug)
427 kill(getpid(), SIGSTOP);
428
429 if (username != NULL || geteuid() == 0) {
430 uid_t run_euid = run_uid;
431 gid_t run_egid = run_gid;
432
433 if (statbuf.st_mode & S_ISUID)
434 run_euid = statbuf.st_uid;
435 if (statbuf.st_mode & S_ISGID)
436 run_egid = statbuf.st_gid;
437
438 /*
439 * It is important to set groups before we
440 * lose privileges on setuid.
441 */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +0000442 if (username != NULL) {
443 if (initgroups(username, run_gid) < 0) {
444 perror("initgroups");
445 exit(1);
446 }
447 if (setregid(run_gid, run_egid) < 0) {
448 perror("setregid");
449 exit(1);
450 }
451 if (setreuid(run_uid, run_euid) < 0) {
452 perror("setreuid");
453 exit(1);
454 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000455 }
456 }
457 else
458 setreuid(run_uid, run_uid);
459#endif /* !SVR4 */
460
461 execv(pathname, &argv[optind]);
462 perror("strace: exec");
463 _exit(1);
464 break;
465 }
466 default:
467 if ((tcp = alloctcb(pid)) == NULL) {
468 fprintf(stderr, "tcb table full\n");
469 cleanup();
470 exit(1);
471 }
472#ifdef SVR4
473 if (proc_open(tcp, 0) < 0) {
474 fprintf(stderr, "trouble opening proc file\n");
475 cleanup();
476 exit(1);
477 }
478#endif /* SVR4 */
479#ifndef SVR4
480 fake_execve(tcp, pathname, &argv[optind], environ);
481#endif
482 break;
483 }
484 }
485 else if (pflag_seen == 0)
486 usage(stderr, 1);
487
488 sigemptyset(&empty_set);
489 sigemptyset(&blocked_set);
490 sa.sa_handler = SIG_IGN;
491 sigemptyset(&sa.sa_mask);
492 sa.sa_flags = 0;
493 sigaction(SIGTTOU, &sa, NULL);
494 sigaction(SIGTTIN, &sa, NULL);
495 if (interactive) {
496 sigaddset(&blocked_set, SIGHUP);
497 sigaddset(&blocked_set, SIGINT);
498 sigaddset(&blocked_set, SIGQUIT);
499 sigaddset(&blocked_set, SIGPIPE);
500 sigaddset(&blocked_set, SIGTERM);
501 sa.sa_handler = interrupt;
502#ifdef SUNOS4
503 /* POSIX signals on sunos4.1 are a little broken. */
504 sa.sa_flags = SA_INTERRUPT;
505#endif /* SUNOS4 */
506 }
507 sigaction(SIGHUP, &sa, NULL);
508 sigaction(SIGINT, &sa, NULL);
509 sigaction(SIGQUIT, &sa, NULL);
510 sigaction(SIGPIPE, &sa, NULL);
511 sigaction(SIGTERM, &sa, NULL);
512#ifdef SVR4
513 sa.sa_handler = reaper;
514 sigaction(SIGCHLD, &sa, NULL);
515#endif /* SVR4 */
516
517 if (trace() < 0)
518 exit(1);
519 cleanup();
520 exit(0);
521}
522
523void
524newoutf(tcp)
525struct tcb *tcp;
526{
527 char name[MAXPATHLEN];
528 FILE *fp;
529
530 if (outfname && followfork > 1) {
531 sprintf(name, "%s.%u", outfname, tcp->pid);
532#ifndef SVR4
533 setreuid(geteuid(), getuid());
534#endif
535 fp = fopen(name, "w");
536#ifndef SVR4
537 setreuid(geteuid(), getuid());
538#endif
539 if (fp == NULL) {
540 perror("fopen");
541 return;
542 }
543 tcp->outf = fp;
544 }
545 return;
546}
547
548struct tcb *
549alloctcb(pid)
550int pid;
551{
552 int i;
553 struct tcb *tcp;
554
555 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
556 if ((tcp->flags & TCB_INUSE) == 0) {
557 tcp->pid = pid;
558 tcp->parent = NULL;
559 tcp->nchildren = 0;
560 tcp->flags = TCB_INUSE | TCB_STARTUP;
561 tcp->outf = outf; /* Initialise to current out file */
562 tcp->stime.tv_sec = 0;
563 tcp->stime.tv_usec = 0;
564 tcp->pfd = -1;
565 nprocs++;
566 return tcp;
567 }
568 }
569 return NULL;
570}
571
572#ifdef SVR4
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000573int
574proc_open(tcp, attaching)
575struct tcb *tcp;
576int attaching;
577{
578 char proc[32];
579 long arg;
580 sysset_t sc_enter, sc_exit;
581 sigset_t signals;
582 fltset_t faults;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000583#ifndef HAVE_POLLABLE_PROCFS
584 static int last_pfd;
585#endif
586
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000587#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000588 /* Open the process pseudo-files in /proc. */
589 sprintf(proc, "/proc/%d/ctl", tcp->pid);
590 if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000591 perror("strace: open(\"/proc/...\", ...)");
592 return -1;
593 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000594 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
595 perror("F_GETFD");
596 return -1;
597 }
598 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
599 perror("F_SETFD");
600 return -1;
601 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000602 sprintf(proc, "/proc/%d/status", tcp->pid);
603 if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
604 perror("strace: open(\"/proc/...\", ...)");
605 return -1;
606 }
607 if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) {
608 perror("F_GETFD");
609 return -1;
610 }
611 if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) {
612 perror("F_SETFD");
613 return -1;
614 }
615 sprintf(proc, "/proc/%d/as", tcp->pid);
616 if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
617 perror("strace: open(\"/proc/...\", ...)");
618 return -1;
619 }
620 if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) {
621 perror("F_GETFD");
622 return -1;
623 }
624 if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) {
625 perror("F_SETFD");
626 return -1;
627 }
628#else
629 /* Open the process pseudo-file in /proc. */
630 sprintf(proc, "/proc/%d", tcp->pid);
631 if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) {
632 perror("strace: open(\"/proc/...\", ...)");
633 return -1;
634 }
635 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
636 perror("F_GETFD");
637 return -1;
638 }
639 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
640 perror("F_SETFD");
641 return -1;
642 }
643#endif
644 rebuild_pollv();
645 if (!attaching) {
646 /*
647 * Wait for the child to pause. Because of a race
648 * condition we have to poll for the event.
649 */
650 for (;;) {
651 if (IOCTL_STATUS (tcp) < 0) {
652 perror("strace: PIOCSTATUS");
653 return -1;
654 }
655 if (tcp->status.PR_FLAGS & PR_ASLEEP)
656 break;
657 }
658 }
659 /* Stop the process so that we own the stop. */
660 if (IOCTL(tcp->pfd, PIOCSTOP, NULL) < 0) {
661 perror("strace: PIOCSTOP");
662 return -1;
663 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000664#ifdef PIOCSET
665 /* Set Run-on-Last-Close. */
666 arg = PR_RLC;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000667 if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000668 perror("PIOCSET PR_RLC");
669 return -1;
670 }
671 /* Set or Reset Inherit-on-Fork. */
672 arg = PR_FORK;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000673 if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000674 perror("PIOC{SET,RESET} PR_FORK");
675 return -1;
676 }
677#else /* !PIOCSET */
678 if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
679 perror("PIOCSRLC");
680 return -1;
681 }
682 if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
683 perror("PIOC{S,R}FORK");
684 return -1;
685 }
686#endif /* !PIOCSET */
687 /* Enable all syscall entries. */
688 prfillset(&sc_enter);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000689 if (IOCTL(tcp->pfd, PIOCSENTRY, &sc_enter) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000690 perror("PIOCSENTRY");
691 return -1;
692 }
693 /* Enable all syscall exits. */
694 prfillset(&sc_exit);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000695 if (IOCTL(tcp->pfd, PIOCSEXIT, &sc_exit) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000696 perror("PIOSEXIT");
697 return -1;
698 }
699 /* Enable all signals. */
700 prfillset(&signals);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000701 if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000702 perror("PIOCSTRACE");
703 return -1;
704 }
705 /* Enable all faults. */
706 prfillset(&faults);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000707 if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000708 perror("PIOCSFAULT");
709 return -1;
710 }
711 if (!attaching) {
712#ifdef MIPS
713 /*
714 * The SGI PRSABORT doesn't work for pause() so
715 * we send it a caught signal to wake it up.
716 */
717 kill(tcp->pid, SIGINT);
718#else /* !MIPS */
719 /* The child is in a pause(), abort it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000720 arg = PRSABORT;
721 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000722 perror("PIOCRUN");
723 return -1;
724 }
725#endif /* !MIPS */
726 for (;;) {
727 /* Wait for the child to do something. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000728 if (IOCTL_WSTOP (tcp) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000729 perror("PIOCWSTOP");
730 return -1;
731 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000732 if (tcp->status.PR_WHY == PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000733#ifdef HAVE_PR_SYSCALL
734 int scno = tcp->status.pr_syscall;
735#else /* !HAVE_PR_SYSCALL */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000736 int scno = tcp->status.PR_WHAT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000737#endif /* !HAVE_PR_SYSCALL */
738 if (scno == SYS_execve)
739 break;
740 }
741 /* Set it running: maybe execve will be next. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000742 arg = 0;
743 if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000744 perror("PIOCRUN");
745 return -1;
746 }
747 }
748 }
749#ifndef HAVE_POLLABLE_PROCFS
750 if (proc_poll_pipe[0] != -1)
751 proc_poller(tcp->pfd);
752 else if (nprocs > 1) {
753 proc_poll_open();
754 proc_poller(last_pfd);
755 proc_poller(tcp->pfd);
756 }
757 last_pfd = tcp->pfd;
758#endif /* !HAVE_POLLABLE_PROCFS */
759 return 0;
760}
761
762#endif /* SVR4 */
763
764static struct tcb *
765pid2tcb(pid)
766int pid;
767{
768 int i;
769 struct tcb *tcp;
770
771 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
772 if (pid && tcp->pid != pid)
773 continue;
774 if (tcp->flags & TCB_INUSE)
775 return tcp;
776 }
777 return NULL;
778}
779
780#ifdef SVR4
781
782static struct tcb *
783pfd2tcb(pfd)
784int pfd;
785{
786 int i;
787 struct tcb *tcp;
788
789 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
790 if (tcp->pfd != pfd)
791 continue;
792 if (tcp->flags & TCB_INUSE)
793 return tcp;
794 }
795 return NULL;
796}
797
798#endif /* SVR4 */
799
800void
801droptcb(tcp)
802struct tcb *tcp;
803{
804 if (tcp->pid == 0)
805 return;
806 nprocs--;
807 tcp->pid = 0;
808 tcp->flags = 0;
809 if (tcp->pfd != -1) {
810 close(tcp->pfd);
811 tcp->pfd = -1;
812#ifdef SVR4
813 rebuild_pollv();
814#endif
815 }
816 if (tcp->parent != NULL) {
817 tcp->parent->nchildren--;
818 tcp->parent = NULL;
819 }
820#if 0
821 if (tcp->outf != stderr)
822 fclose(tcp->outf);
823#endif
824 tcp->outf = 0;
825}
826
827#ifndef SVR4
828
829static int
830resume(tcp)
831struct tcb *tcp;
832{
833 if (tcp == NULL)
834 return -1;
835
836 if (!(tcp->flags & TCB_SUSPENDED)) {
837 fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid);
838 return -1;
839 }
840 tcp->flags &= ~TCB_SUSPENDED;
841
842 if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) {
843 perror("resume: ptrace(PTRACE_SYSCALL, ...)");
844 return -1;
845 }
846
847 if (!qflag)
848 fprintf(stderr, "Process %u resumed\n", tcp->pid);
849 return 0;
850}
851
852#endif /* !SVR4 */
853
854/* detach traced process; continue with sig */
855
856static int
857detach(tcp, sig)
858struct tcb *tcp;
859int sig;
860{
861 int error = 0;
862#ifdef LINUX
863 int status;
864#endif
865
866 if (tcp->flags & TCB_BPTSET)
867 sig = SIGKILL;
868
869#ifdef LINUX
870 /*
871 * Linux wrongly insists the child be stopped
872 * before detaching. Arghh. We go through hoops
873 * to make a clean break of things.
874 */
Wichert Akkermandacfb6e1999-06-03 14:21:07 +0000875#if defined(SPARC)
876#undef PTRACE_DETACH
877#define PTRACE_DETACH PTRACE_SUNDETACH
878#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000879 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
880 /* On a clear day, you can see forever. */
881 }
882 else if (errno != ESRCH) {
883 /* Shouldn't happen. */
884 perror("detach: ptrace(PTRACE_DETACH, ...)");
885 }
886 else if (kill(tcp->pid, 0) < 0) {
887 if (errno != ESRCH)
888 perror("detach: checking sanity");
889 }
890 else if (kill(tcp->pid, SIGSTOP) < 0) {
891 if (errno != ESRCH)
892 perror("detach: stopping child");
893 }
894 else {
895 for (;;) {
896 if (waitpid(tcp->pid, &status, 0) < 0) {
897 if (errno != ECHILD)
898 perror("detach: waiting");
899 break;
900 }
901 if (!WIFSTOPPED(status)) {
902 /* Au revoir, mon ami. */
903 break;
904 }
905 if (WSTOPSIG(status) == SIGSTOP) {
906 if ((error = ptrace(PTRACE_DETACH,
907 tcp->pid, (char *) 1, sig)) < 0) {
908 if (errno != ESRCH)
909 perror("detach: ptrace(PTRACE_DETACH, ...)");
910 /* I died trying. */
911 }
912 break;
913 }
914 if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1,
915 WSTOPSIG(status) == SIGTRAP ?
916 0 : WSTOPSIG(status))) < 0) {
917 if (errno != ESRCH)
918 perror("detach: ptrace(PTRACE_CONT, ...)");
919 break;
920 }
921 }
922 }
923#endif /* LINUX */
924
925#if defined(SUNOS4)
926 /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
927 if (sig && kill(tcp->pid, sig) < 0)
928 perror("detach: kill");
929 sig = 0;
930 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0)
931 perror("detach: ptrace(PTRACE_DETACH, ...)");
932#endif /* SUNOS4 */
933
934#ifndef SVR4
935 if (waiting_parent(tcp))
936 error = resume(tcp->parent);
937#endif /* !SVR4 */
938
939 if (!qflag)
940 fprintf(stderr, "Process %u detached\n", tcp->pid);
941
942 droptcb(tcp);
943 return error;
944}
945
946#ifdef SVR4
947
948static void
949reaper(sig)
950int sig;
951{
952 int pid;
953 int status;
954
955 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
956#if 0
957 struct tcb *tcp;
958
959 tcp = pid2tcb(pid);
960 if (tcp)
961 droptcb(tcp);
962#endif
963 }
964}
965
966#endif /* SVR4 */
967
968static void
969cleanup()
970{
971 int i;
972 struct tcb *tcp;
973
974 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
975 if (!(tcp->flags & TCB_INUSE))
976 continue;
977 if (debug)
978 fprintf(stderr,
979 "cleanup: looking at pid %u\n", tcp->pid);
980 if (tcp_last &&
981 (!outfname || followfork < 2 || tcp_last == tcp)) {
982 tprintf(" <unfinished ...>\n");
983 tcp_last = NULL;
984 }
985 if (tcp->flags & TCB_ATTACHED)
986 detach(tcp, 0);
987 else {
988 kill(tcp->pid, SIGCONT);
989 kill(tcp->pid, SIGTERM);
990 }
991 }
992 if (cflag)
993 call_summary(outf);
994}
995
996static void
997interrupt(sig)
998int sig;
999{
1000 interrupted = 1;
1001}
1002
1003#ifndef HAVE_STRERROR
1004
1005#ifndef SYS_ERRLIST_DECLARED
1006extern int sys_nerr;
1007extern char *sys_errlist[];
1008#endif /* SYS_ERRLIST_DECLARED */
1009
1010const char *
1011strerror(errno)
1012int errno;
1013{
1014 static char buf[64];
1015
1016 if (errno < 1 || errno >= sys_nerr) {
1017 sprintf(buf, "Unknown error %d", errno);
1018 return buf;
1019 }
1020 return sys_errlist[errno];
1021}
1022
1023#endif /* HAVE_STERRROR */
1024
1025#ifndef HAVE_STRSIGNAL
1026
1027#ifndef SYS_SIGLIST_DECLARED
1028#ifdef HAVE__SYS_SIGLIST
1029 extern char *_sys_siglist[];
1030#else
1031 extern char *sys_siglist[];
1032#endif
1033#endif /* SYS_SIGLIST_DECLARED */
1034
1035const char *
1036strsignal(sig)
1037int sig;
1038{
1039 static char buf[64];
1040
1041 if (sig < 1 || sig >= NSIG) {
1042 sprintf(buf, "Unknown signal %d", sig);
1043 return buf;
1044 }
1045#ifdef HAVE__SYS_SIGLIST
1046 return _sys_siglist[sig];
1047#else
1048 return sys_siglist[sig];
1049#endif
1050}
1051
1052#endif /* HAVE_STRSIGNAL */
1053
1054#ifdef SVR4
1055
1056static void
1057rebuild_pollv()
1058{
1059 int i, j;
1060 struct tcb *tcp;
1061
1062 for (i = j = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1063 if (!(tcp->flags & TCB_INUSE))
1064 continue;
1065 pollv[j].fd = tcp->pfd;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001066 pollv[j].events = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001067 j++;
1068 }
1069 if (j != nprocs) {
1070 fprintf(stderr, "strace: proc miscount\n");
1071 exit(1);
1072 }
1073}
1074
1075#ifndef HAVE_POLLABLE_PROCFS
1076
1077static void
1078proc_poll_open()
1079{
1080 int arg;
1081 int i;
1082
1083 if (pipe(proc_poll_pipe) < 0) {
1084 perror("pipe");
1085 exit(1);
1086 }
1087 for (i = 0; i < 2; i++) {
1088 if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) {
1089 perror("F_GETFD");
1090 exit(1);
1091 }
1092 if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) {
1093 perror("F_SETFD");
1094 exit(1);
1095 }
1096 }
1097}
1098
1099static int
1100proc_poll(pollv, nfds, timeout)
1101struct pollfd *pollv;
1102int nfds;
1103int timeout;
1104{
1105 int i;
1106 int n;
1107 struct proc_pollfd pollinfo;
1108
1109 if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
1110 return n;
1111 if (n != sizeof(struct proc_pollfd)) {
1112 fprintf(stderr, "panic: short read: %d\n", n);
1113 exit(1);
1114 }
1115 for (i = 0; i < nprocs; i++) {
1116 if (pollv[i].fd == pollinfo.fd)
1117 pollv[i].revents = pollinfo.revents;
1118 else
1119 pollv[i].revents = 0;
1120 }
1121 poller_pid = pollinfo.pid;
1122 return 1;
1123}
1124
1125static void
1126wakeup_handler(sig)
1127int sig;
1128{
1129}
1130
1131static void
1132proc_poller(pfd)
1133int pfd;
1134{
1135 struct proc_pollfd pollinfo;
1136 struct sigaction sa;
1137 sigset_t blocked_set, empty_set;
1138 int i;
1139 int n;
1140 struct rlimit rl;
1141
1142 switch (fork()) {
1143 case -1:
1144 perror("fork");
1145 _exit(0);
1146 case 0:
1147 break;
1148 default:
1149 return;
1150 }
1151
1152 sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
1153 sa.sa_flags = 0;
1154 sigemptyset(&sa.sa_mask);
1155 sigaction(SIGHUP, &sa, NULL);
1156 sigaction(SIGINT, &sa, NULL);
1157 sigaction(SIGQUIT, &sa, NULL);
1158 sigaction(SIGPIPE, &sa, NULL);
1159 sigaction(SIGTERM, &sa, NULL);
1160 sa.sa_handler = wakeup_handler;
1161 sigaction(SIGUSR1, &sa, NULL);
1162 sigemptyset(&blocked_set);
1163 sigaddset(&blocked_set, SIGUSR1);
1164 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1165 sigemptyset(&empty_set);
1166
1167 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
1168 perror("getrlimit(RLIMIT_NOFILE, ...)");
1169 _exit(0);
1170 }
1171 n = rl.rlim_cur;
1172 for (i = 0; i < n; i++) {
1173 if (i != pfd && i != proc_poll_pipe[1])
1174 close(i);
1175 }
1176
1177 pollinfo.fd = pfd;
1178 pollinfo.pid = getpid();
1179 for (;;) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001180 if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
1181 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001182 switch (errno) {
1183 case EINTR:
1184 continue;
1185 case EBADF:
1186 pollinfo.revents = POLLERR;
1187 break;
1188 case ENOENT:
1189 pollinfo.revents = POLLHUP;
1190 break;
1191 default:
1192 perror("proc_poller: PIOCWSTOP");
1193 }
1194 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1195 _exit(0);
1196 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001197 pollinfo.revents = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001198 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1199 sigsuspend(&empty_set);
1200 }
1201}
1202
1203#endif /* !HAVE_POLLABLE_PROCFS */
1204
1205static int
1206choose_pfd()
1207{
1208 int i, j;
1209 struct tcb *tcp;
1210
1211 static int last;
1212
1213 if (followfork < 2 &&
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001214 last < nprocs && (pollv[last].revents & POLLWANT)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001215 /*
1216 * The previous process is ready to run again. We'll
1217 * let it do so if it is currently in a syscall. This
1218 * heuristic improves the readability of the trace.
1219 */
1220 tcp = pfd2tcb(pollv[last].fd);
1221 if (tcp && (tcp->flags & TCB_INSYSCALL))
1222 return pollv[last].fd;
1223 }
1224
1225 for (i = 0; i < nprocs; i++) {
1226 /* Let competing children run round robin. */
1227 j = (i + last + 1) % nprocs;
1228 if (pollv[j].revents & (POLLHUP | POLLERR)) {
1229 tcp = pfd2tcb(pollv[j].fd);
1230 if (!tcp) {
1231 fprintf(stderr, "strace: lost proc\n");
1232 exit(1);
1233 }
1234 droptcb(tcp);
1235 return -1;
1236 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001237 if (pollv[j].revents & POLLWANT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001238 last = j;
1239 return pollv[j].fd;
1240 }
1241 }
1242 fprintf(stderr, "strace: nothing ready\n");
1243 exit(1);
1244}
1245
1246static int
1247trace()
1248{
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001249#ifdef POLL_HACK
1250 struct tcb *in_syscall;
1251#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001252 struct tcb *tcp;
1253 int pfd;
1254 int what;
1255 int ioctl_result = 0, ioctl_errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001256 long arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001257
1258 for (;;) {
1259 if (interactive)
1260 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1261
1262 if (nprocs == 0)
1263 break;
1264
1265 switch (nprocs) {
1266 case 1:
1267#ifndef HAVE_POLLABLE_PROCFS
1268 if (proc_poll_pipe[0] == -1) {
1269#endif
1270 tcp = pid2tcb(0);
1271 if (!tcp)
1272 continue;
1273 pfd = tcp->pfd;
1274 if (pfd == -1)
1275 continue;
1276 break;
1277#ifndef HAVE_POLLABLE_PROCFS
1278 }
1279 /* fall through ... */
1280#endif /* !HAVE_POLLABLE_PROCFS */
1281 default:
1282#ifdef HAVE_POLLABLE_PROCFS
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001283#ifdef POLL_HACK
1284 /* On some systems (e.g. UnixWare) we get too much ugly
1285 "unfinished..." stuff when multiple proceses are in
1286 syscalls. Here's a nasty hack */
1287
1288 if (in_syscall) {
1289 struct pollfd pv;
1290 tcp = in_syscall;
1291 in_syscall = NULL;
1292 pv.fd = tcp->pfd;
1293 pv.events = POLLWANT;
1294 if ((what = poll (&pv, 1, 1)) < 0) {
1295 if (interrupted)
1296 return 0;
1297 continue;
1298 }
1299 else if (what == 1 && pv.revents & POLLWANT) {
1300 goto FOUND;
1301 }
1302 }
1303#endif
1304
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001305 if (poll(pollv, nprocs, INFTIM) < 0) {
1306 if (interrupted)
1307 return 0;
1308 continue;
1309 }
1310#else /* !HAVE_POLLABLE_PROCFS */
1311 if (proc_poll(pollv, nprocs, INFTIM) < 0) {
1312 if (interrupted)
1313 return 0;
1314 continue;
1315 }
1316#endif /* !HAVE_POLLABLE_PROCFS */
1317 pfd = choose_pfd();
1318 if (pfd == -1)
1319 continue;
1320 break;
1321 }
1322
1323 /* Look up `pfd' in our table. */
1324 if ((tcp = pfd2tcb(pfd)) == NULL) {
1325 fprintf(stderr, "unknown pfd: %u\n", pfd);
1326 exit(1);
1327 }
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001328 FOUND:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001329 /* Get the status of the process. */
1330 if (!interrupted) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001331 ioctl_result = IOCTL_WSTOP (tcp);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001332 ioctl_errno = errno;
1333#ifndef HAVE_POLLABLE_PROCFS
1334 if (proc_poll_pipe[0] != -1) {
1335 if (ioctl_result < 0)
1336 kill(poller_pid, SIGKILL);
1337 else
1338 kill(poller_pid, SIGUSR1);
1339 }
1340#endif /* !HAVE_POLLABLE_PROCFS */
1341 }
1342 if (interrupted)
1343 return 0;
1344
1345 if (interactive)
1346 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1347
1348 if (ioctl_result < 0) {
1349 /* Find out what happened if it failed. */
1350 switch (ioctl_errno) {
1351 case EINTR:
1352 case EBADF:
1353 continue;
1354 case ENOENT:
1355 droptcb(tcp);
1356 continue;
1357 default:
1358 perror("PIOCWSTOP");
1359 exit(1);
1360 }
1361 }
1362
1363 /* clear the just started flag */
1364 tcp->flags &= ~TCB_STARTUP;
1365
1366 /* set current output file */
1367 outf = tcp->outf;
1368
1369 if (cflag) {
1370 struct timeval stime;
1371
1372 stime.tv_sec = tcp->status.pr_stime.tv_sec;
1373 stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
1374 tv_sub(&tcp->dtime, &stime, &tcp->stime);
1375 tcp->stime = stime;
1376 }
1377
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001378 what = tcp->status.PR_WHAT;
1379 switch (tcp->status.PR_WHY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001380 case PR_REQUESTED:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001381 if (tcp->status.PR_FLAGS & PR_ASLEEP) {
1382 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001383 if (trace_syscall(tcp) < 0) {
1384 fprintf(stderr, "syscall trouble\n");
1385 exit(1);
1386 }
1387 }
1388 break;
1389 case PR_SYSENTRY:
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001390#ifdef POLL_HACK
1391 in_syscall = tcp;
1392#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001393 case PR_SYSEXIT:
1394 if (trace_syscall(tcp) < 0) {
1395 fprintf(stderr, "syscall trouble\n");
1396 exit(1);
1397 }
1398 break;
1399 case PR_SIGNALLED:
1400 if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) {
1401 printleader(tcp);
1402 tprintf("--- %s (%s) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001403 signame(what), strsignal(what));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001404 printtrailer(tcp);
1405 }
1406 break;
1407 case PR_FAULTED:
1408 if (!cflag && (qual_flags[what] & QUAL_FAULT)) {
1409 printleader(tcp);
1410 tprintf("=== FAULT %d ===", what);
1411 printtrailer(tcp);
1412 }
1413 break;
1414 default:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001415 fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001416 exit(1);
1417 break;
1418 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001419 arg = 0;
1420 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001421 perror("PIOCRUN");
1422 exit(1);
1423 }
1424 }
1425 return 0;
1426}
1427
1428#else /* !SVR4 */
1429
1430static int
1431trace()
1432{
1433 int pid;
1434 int wait_errno;
1435 int status;
1436 struct tcb *tcp;
1437#ifdef LINUX
1438 struct rusage ru;
1439#endif /* LINUX */
1440
1441 while (nprocs != 0) {
1442 if (interactive)
1443 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1444#ifdef LINUX
1445 pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
1446#endif /* LINUX */
1447#ifdef SUNOS4
1448 pid = wait(&status);
1449#endif /* SUNOS4 */
1450 wait_errno = errno;
1451 if (interactive)
1452 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1453
1454 if (interrupted)
1455 return 0;
1456
1457 if (pid == -1) {
1458 switch (wait_errno) {
1459 case EINTR:
1460 continue;
1461 case ECHILD:
1462 /*
1463 * We would like to verify this case
1464 * but sometimes a race in Solbourne's
1465 * version of SunOS sometimes reports
1466 * ECHILD before sending us SIGCHILD.
1467 */
1468#if 0
1469 if (nprocs == 0)
1470 return 0;
1471 fprintf(stderr, "strace: proc miscount\n");
1472 exit(1);
1473#endif
1474 return 0;
1475 default:
1476 errno = wait_errno;
1477 perror("strace: wait");
1478 return -1;
1479 }
1480 }
1481 if (debug)
1482 fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
1483
1484 /* Look up `pid' in our table. */
1485 if ((tcp = pid2tcb(pid)) == NULL) {
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001486#if 0 /* XXX davidm */ /* WTA: disabled again */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001487 struct tcb *tcpchild;
1488
1489 if ((tcpchild = alloctcb(pid)) == NULL) {
1490 fprintf(stderr, " [tcb table full]\n");
1491 kill(pid, SIGKILL); /* XXX */
1492 return 0;
1493 }
1494 tcpchild->flags |= TCB_ATTACHED;
1495 newoutf(tcpchild);
1496 tcp->nchildren++;
1497 if (!qflag)
1498 fprintf(stderr, "Process %d attached\n", pid);
1499#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001500 fprintf(stderr, "unknown pid: %u\n", pid);
1501 if (WIFSTOPPED(status))
1502 ptrace(PTRACE_CONT, pid, (char *) 1, 0);
1503 exit(1);
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001504#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001505 }
1506 /* set current output file */
1507 outf = tcp->outf;
1508 if (cflag) {
1509#ifdef LINUX
1510 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
1511 tcp->stime = ru.ru_stime;
1512#endif /* !LINUX */
1513 }
1514
1515 if (tcp->flags & TCB_SUSPENDED) {
1516 /*
1517 * Apparently, doing any ptrace() call on a stopped
1518 * process, provokes the kernel to report the process
1519 * status again on a subsequent wait(), even if the
1520 * process has not been actually restarted.
1521 * Since we have inspected the arguments of suspended
1522 * processes we end up here testing for this case.
1523 */
1524 continue;
1525 }
1526 if (WIFSIGNALED(status)) {
1527 if (!cflag
1528 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
1529 printleader(tcp);
1530 tprintf("+++ killed by %s +++",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001531 signame(WTERMSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001532 printtrailer(tcp);
1533 }
1534 droptcb(tcp);
1535 continue;
1536 }
1537 if (WIFEXITED(status)) {
1538 if (debug)
1539 fprintf(stderr, "pid %u exited\n", pid);
1540 if (tcp->flags & TCB_ATTACHED)
1541 fprintf(stderr,
1542 "PANIC: attached pid %u exited\n",
1543 pid);
1544 droptcb(tcp);
1545 continue;
1546 }
1547 if (!WIFSTOPPED(status)) {
1548 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
1549 droptcb(tcp);
1550 continue;
1551 }
1552 if (debug)
1553 fprintf(stderr, "pid %u stopped, [%s]\n",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001554 pid, signame(WSTOPSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001555
1556 if (tcp->flags & TCB_STARTUP) {
1557 /*
1558 * This flag is there to keep us in sync.
1559 * Next time this process stops it should
1560 * really be entering a system call.
1561 */
1562 tcp->flags &= ~TCB_STARTUP;
1563 if (tcp->flags & TCB_ATTACHED) {
1564 /*
1565 * Interestingly, the process may stop
1566 * with STOPSIG equal to some other signal
1567 * than SIGSTOP if we happend to attach
1568 * just before the process takes a signal.
1569 */
1570 if (!WIFSTOPPED(status)) {
1571 fprintf(stderr,
1572 "pid %u not stopped\n", pid);
1573 detach(tcp, WSTOPSIG(status));
1574 continue;
1575 }
1576 }
1577 else {
1578#ifdef SUNOS4
1579 /* A child of us stopped at exec */
1580 if (WSTOPSIG(status) == SIGTRAP && followvfork)
1581 fixvfork(tcp);
1582#endif /* SUNOS4 */
1583 }
1584 if (tcp->flags & TCB_BPTSET) {
1585 if (clearbpt(tcp) < 0) /* Pretty fatal */ {
1586 droptcb(tcp);
1587 cleanup();
1588 return -1;
1589 }
1590 }
1591 goto tracing;
1592 }
1593
1594 if (WSTOPSIG(status) != SIGTRAP) {
1595 if (WSTOPSIG(status) == SIGSTOP &&
1596 (tcp->flags & TCB_SIGTRAPPED)) {
1597 /*
1598 * Trapped attempt to block SIGTRAP
1599 * Hope we are back in control now.
1600 */
1601 tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
1602 if (ptrace(PTRACE_SYSCALL,
1603 pid, (char *) 1, 0) < 0) {
1604 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1605 cleanup();
1606 return -1;
1607 }
1608 continue;
1609 }
1610 if (!cflag
1611 && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
1612 printleader(tcp);
1613 tprintf("--- %s (%s) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001614 signame(WSTOPSIG(status)),
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001615 strsignal(WSTOPSIG(status)));
1616 printtrailer(tcp);
1617 }
1618 if ((tcp->flags & TCB_ATTACHED) &&
1619 !sigishandled(tcp, WSTOPSIG(status))) {
1620 detach(tcp, WSTOPSIG(status));
1621 continue;
1622 }
1623 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1,
1624 WSTOPSIG(status)) < 0) {
1625 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1626 cleanup();
1627 return -1;
1628 }
1629 tcp->flags &= ~TCB_SUSPENDED;
1630 continue;
1631 }
1632 if (trace_syscall(tcp) < 0) {
1633 if (tcp->flags & TCB_ATTACHED)
1634 detach(tcp, 0);
1635 else {
1636 ptrace(PTRACE_KILL,
1637 tcp->pid, (char *) 1, SIGTERM);
1638 droptcb(tcp);
1639 }
1640 continue;
1641 }
1642 if (tcp->flags & TCB_EXITING) {
1643 if (tcp->flags & TCB_ATTACHED)
1644 detach(tcp, 0);
1645 else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) {
1646 perror("strace: ptrace(PTRACE_CONT, ...)");
1647 cleanup();
1648 return -1;
1649 }
1650 continue;
1651 }
1652 if (tcp->flags & TCB_SUSPENDED) {
1653 if (!qflag)
1654 fprintf(stderr, "Process %u suspended\n", pid);
1655 continue;
1656 }
1657 tracing:
1658 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) {
1659 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1660 cleanup();
1661 return -1;
1662 }
1663 }
1664 return 0;
1665}
1666
1667#endif /* !SVR4 */
1668
1669static int curcol;
1670
1671#ifdef __STDC__
1672#include <stdarg.h>
1673#define VA_START(a, b) va_start(a, b)
1674#else
1675#include <varargs.h>
1676#define VA_START(a, b) va_start(a)
1677#endif
1678
1679void
1680#ifdef __STDC__
1681tprintf(const char *fmt, ...)
1682#else
1683tprintf(fmt, va_alist)
1684char *fmt;
1685va_dcl
1686#endif
1687{
1688 va_list args;
1689
1690 VA_START(args, fmt);
1691 if (outf)
1692 curcol += vfprintf(outf, fmt, args);
1693 va_end(args);
1694 return;
1695}
1696
1697void
1698printleader(tcp)
1699struct tcb *tcp;
1700{
1701 if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) {
1702 tcp_last->flags |= TCB_REPRINT;
1703 tprintf(" <unfinished ...>\n");
1704 }
1705 curcol = 0;
1706 if ((followfork == 1 || pflag_seen > 1) && outfname)
1707 tprintf("%-5d ", tcp->pid);
1708 else if (nprocs > 1 && !outfname)
1709 tprintf("[pid %5u] ", tcp->pid);
1710 if (tflag) {
1711 char str[sizeof("HH:MM:SS")];
1712 struct timeval tv, dtv;
1713 static struct timeval otv;
1714
1715 gettimeofday(&tv, NULL);
1716 if (rflag) {
1717 if (otv.tv_sec == 0)
1718 otv = tv;
1719 tv_sub(&dtv, &tv, &otv);
1720 tprintf("%6ld.%06ld ",
1721 (long) dtv.tv_sec, (long) dtv.tv_usec);
1722 otv = tv;
1723 }
1724 else if (tflag > 2) {
1725 tprintf("%ld.%06ld ",
1726 (long) tv.tv_sec, (long) tv.tv_usec);
1727 }
1728 else {
1729 time_t local = tv.tv_sec;
1730 strftime(str, sizeof(str), "%T", localtime(&local));
1731 if (tflag > 1)
1732 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
1733 else
1734 tprintf("%s ", str);
1735 }
1736 }
1737 if (iflag)
1738 printcall(tcp);
1739}
1740
1741void
1742tabto(col)
1743int col;
1744{
1745 if (curcol < col)
1746 tprintf("%*s", col - curcol, "");
1747}
1748
1749void
1750printtrailer(tcp)
1751struct tcb *tcp;
1752{
1753 tprintf("\n");
1754 tcp_last = NULL;
1755}
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001756
Wichert Akkermanea78f0f1999-11-29 15:34:02 +00001757#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001758
1759int mp_ioctl (int fd, int cmd, void *arg, int size) {
1760
1761 struct iovec iov[2];
1762 int n = 1;
1763
1764 iov[0].iov_base = &cmd;
1765 iov[0].iov_len = sizeof cmd;
1766 if (arg) {
1767 ++n;
1768 iov[1].iov_base = arg;
1769 iov[1].iov_len = size;
1770 }
1771
1772 return writev (fd, iov, n);
1773}
1774
1775#endif