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