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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
36 #include <sys/param.h>
38 #include <sys/resource.h>
46 #include <sys/stropts.h>
53 int debug = 0, followfork = 0, followvfork = 0, interactive = 0;
54 int rflag = 0, tflag = 0, dtime = 0, cflag = 0;
55 int iflag = 0, xflag = 0, qflag = 0;
58 char *username = NULL;
62 int acolumn = DEFAULT_ACOLUMN;
63 int max_strlen = DEFAULT_STRLEN;
64 char *outfname = NULL;
66 struct tcb tcbtab[MAX_PROCS];
69 extern char version[];
70 extern char **environ;
72 static struct tcb *pid2tcb P((int pid));
73 static int trace P((void));
74 static void cleanup P((void));
75 static void interrupt P((int sig));
76 static sigset_t empty_set, blocked_set;
78 #ifdef HAVE_SIG_ATOMIC_T
79 static volatile sig_atomic_t interrupted;
80 #else /* !HAVE_SIG_ATOMIC_T */
82 static volatile int interrupted;
84 static int interrupted;
85 #endif /* !__STDC__ */
86 #endif /* !HAVE_SIG_ATOMIC_T */
90 static struct tcb *pfd2tcb P((int pfd));
91 static void reaper P((int sig));
92 static void rebuild_pollv P((void));
93 struct pollfd pollv[MAX_PROCS];
95 #ifndef HAVE_POLLABLE_PROCFS
97 static void proc_poll_open P((void));
98 static void proc_poller P((int pfd));
106 static int poller_pid;
107 static int proc_poll_pipe[2] = { -1, -1 };
109 #endif /* !HAVE_POLLABLE_PROCFS */
112 #define POLLWANT POLLWRNORM
114 #define POLLWANT POLLPRI
124 usage: 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);
169 static char buf[BUFSIZ];
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) {
219 qualify("abbrev=none");
222 printf("%s\n", version);
226 acolumn = atoi(optarg);
232 outfname = strdup(optarg);
235 set_overhead(atoi(optarg));
238 if ((pid = atoi(optarg)) == 0) {
239 fprintf(stderr, "%s: Invalid process id: %s\n",
243 if (pid == getpid()) {
244 fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.", progname);
247 if ((tcp = alloctcb(pid)) == NULL) {
248 fprintf(stderr, "%s: tcb table full, please recompile strace\n",
252 tcp->flags |= TCB_ATTACHED;
256 max_strlen = atoi(optarg);
262 username = strdup(optarg);
270 /* See if they want to run as another user. */
271 if (username != NULL) {
274 if (getuid() != 0 || geteuid() != 0) {
276 "%s: you must be root to use the -u option\n",
280 if ((pent = getpwnam(username)) == NULL) {
281 fprintf(stderr, "%s: cannot find user `%s'\n",
285 run_uid = pent->pw_uid;
286 run_gid = pent->pw_gid;
294 setreuid(geteuid(), getuid());
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));
308 /* Check if they want to redirect the output. */
310 if ((outf = fopen(outfname, "w")) == NULL) {
311 fprintf(stderr, "%s: can't fopen '%s': %s\n",
312 progname, outfname, strerror(errno));
318 setreuid(geteuid(), getuid());
323 setvbuf(outf, buf, _IOLBF, BUFSIZ);
325 else if (optind < argc)
330 for (c = 0, tcp = tcbtab; c < MAX_PROCS; c++, tcp++) {
331 /* Reinitialize the output since it may have changed. */
333 if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
336 if (proc_open(tcp, 1) < 0) {
337 fprintf(stderr, "trouble opening proc file\n");
342 if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
343 perror("attach: ptrace(PTRACE_ATTACH, ...)");
350 "Process %u attached - interrupt to quit\n",
357 char pathname[MAXPATHLEN];
359 filename = argv[optind];
360 if (strchr(filename, '/'))
361 strcpy(pathname, filename);
362 #ifdef USE_DEBUGGING_EXEC
364 * Debuggers customarily check the current directory
365 * first regardless of the path but doing that gives
366 * security geeks a panic attack.
368 else if (stat(filename, &statbuf) == 0)
369 strcpy(pathname, filename);
370 #endif /* USE_DEBUGGING_EXEC */
375 for (path = getenv("PATH"); path && *path; path += m) {
376 if (strchr(path, ':')) {
377 n = strchr(path, ':') - path;
381 m = n = strlen(path);
383 getcwd(pathname, MAXPATHLEN);
384 len = strlen(pathname);
387 strncpy(pathname, path, n);
390 if (len && pathname[len - 1] != '/')
391 pathname[len++] = '/';
392 strcpy(pathname + len, filename);
393 if (stat(pathname, &statbuf) == 0)
397 if (stat(pathname, &statbuf) < 0) {
398 fprintf(stderr, "%s: %s: command not found\n",
402 switch (pid = fork()) {
404 perror("strace: fork");
410 if (outf != stderr) close (fileno (outf));
412 /* Kludge for SGI, see proc_open for details. */
413 sa.sa_handler = foobar;
415 sigemptyset(&sa.sa_mask);
416 sigaction(SIGINT, &sa, NULL);
420 if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
421 perror("strace: ptrace(PTRACE_TRACEME, ...)");
425 kill(getpid(), SIGSTOP);
427 if (username != NULL || geteuid() == 0) {
428 uid_t run_euid = run_uid;
429 gid_t run_egid = run_gid;
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;
437 * It is important to set groups before we
438 * lose privileges on setuid.
441 && initgroups(username, run_gid) < 0) {
442 perror("initgroups");
445 if (setregid(run_gid, run_egid) < 0) {
449 if (setreuid(run_uid, run_euid) < 0) {
455 setreuid(run_uid, run_uid);
458 execv(pathname, &argv[optind]);
459 perror("strace: exec");
464 if ((tcp = alloctcb(pid)) == NULL) {
465 fprintf(stderr, "tcb table full\n");
470 if (proc_open(tcp, 0) < 0) {
471 fprintf(stderr, "trouble opening proc file\n");
477 fake_execve(tcp, pathname, &argv[optind], environ);
482 else if (pflag_seen == 0)
485 sigemptyset(&empty_set);
486 sigemptyset(&blocked_set);
487 sa.sa_handler = SIG_IGN;
488 sigemptyset(&sa.sa_mask);
490 sigaction(SIGTTOU, &sa, NULL);
491 sigaction(SIGTTIN, &sa, NULL);
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;
500 /* POSIX signals on sunos4.1 are a little broken. */
501 sa.sa_flags = SA_INTERRUPT;
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);
510 sa.sa_handler = reaper;
511 sigaction(SIGCHLD, &sa, NULL);
524 char name[MAXPATHLEN];
527 if (outfname && followfork > 1) {
528 sprintf(name, "%s.%u", outfname, tcp->pid);
530 setreuid(geteuid(), getuid());
532 fp = fopen(name, "w");
534 setreuid(geteuid(), getuid());
552 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
553 if ((tcp->flags & TCB_INUSE) == 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;
571 proc_open(tcp, attaching)
577 sysset_t sc_enter, sc_exit;
580 #ifndef HAVE_POLLABLE_PROCFS
585 /* 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) {
588 perror("strace: open(\"/proc/...\", ...)");
591 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
595 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
599 sprintf(proc, "/proc/%d/status", tcp->pid);
600 if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
601 perror("strace: open(\"/proc/...\", ...)");
604 if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) {
608 if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) {
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/...\", ...)");
617 if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) {
621 if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) {
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/...\", ...)");
632 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
636 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
644 * Wait for the child to pause. Because of a race
645 * condition we have to poll for the event.
648 if (IOCTL_STATUS (tcp) < 0) {
649 perror("strace: PIOCSTATUS");
652 if (tcp->status.PR_FLAGS & PR_ASLEEP)
656 /* Stop the process so that we own the stop. */
657 if (IOCTL(tcp->pfd, PIOCSTOP, NULL) < 0) {
658 perror("strace: PIOCSTOP");
662 /* Set Run-on-Last-Close. */
664 if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
665 perror("PIOCSET PR_RLC");
668 /* Set or Reset Inherit-on-Fork. */
670 if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
671 perror("PIOC{SET,RESET} PR_FORK");
675 if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
679 if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
680 perror("PIOC{S,R}FORK");
683 #endif /* !PIOCSET */
684 /* Enable all syscall entries. */
685 prfillset(&sc_enter);
686 if (IOCTL(tcp->pfd, PIOCSENTRY, &sc_enter) < 0) {
687 perror("PIOCSENTRY");
690 /* Enable all syscall exits. */
692 if (IOCTL(tcp->pfd, PIOCSEXIT, &sc_exit) < 0) {
696 /* Enable all signals. */
698 if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
699 perror("PIOCSTRACE");
702 /* Enable all faults. */
704 if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
705 perror("PIOCSFAULT");
711 * The SGI PRSABORT doesn't work for pause() so
712 * we send it a caught signal to wake it up.
714 kill(tcp->pid, SIGINT);
716 /* The child is in a pause(), abort it. */
718 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
724 /* Wait for the child to do something. */
725 if (IOCTL_WSTOP (tcp) < 0) {
729 if (tcp->status.PR_WHY == PR_SYSENTRY) {
730 #ifdef HAVE_PR_SYSCALL
731 int scno = tcp->status.pr_syscall;
732 #else /* !HAVE_PR_SYSCALL */
733 int scno = tcp->status.PR_WHAT;
734 #endif /* !HAVE_PR_SYSCALL */
735 if (scno == SYS_execve)
738 /* Set it running: maybe execve will be next. */
740 if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
746 #ifndef HAVE_POLLABLE_PROCFS
747 if (proc_poll_pipe[0] != -1)
748 proc_poller(tcp->pfd);
749 else if (nprocs > 1) {
751 proc_poller(last_pfd);
752 proc_poller(tcp->pfd);
755 #endif /* !HAVE_POLLABLE_PROCFS */
768 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
769 if (pid && tcp->pid != pid)
771 if (tcp->flags & TCB_INUSE)
786 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
789 if (tcp->flags & TCB_INUSE)
806 if (tcp->pfd != -1) {
813 if (tcp->parent != NULL) {
814 tcp->parent->nchildren--;
818 if (tcp->outf != stderr)
833 if (!(tcp->flags & TCB_SUSPENDED)) {
834 fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid);
837 tcp->flags &= ~TCB_SUSPENDED;
839 if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) {
840 perror("resume: ptrace(PTRACE_SYSCALL, ...)");
845 fprintf(stderr, "Process %u resumed\n", tcp->pid);
851 /* detach traced process; continue with sig */
863 if (tcp->flags & TCB_BPTSET)
868 * Linux wrongly insists the child be stopped
869 * before detaching. Arghh. We go through hoops
870 * to make a clean break of things.
874 #define PTRACE_DETACH PTRACE_SUNDETACH
876 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
877 /* On a clear day, you can see forever. */
879 else if (errno != ESRCH) {
880 /* Shouldn't happen. */
881 perror("detach: ptrace(PTRACE_DETACH, ...)");
883 else if (kill(tcp->pid, 0) < 0) {
885 perror("detach: checking sanity");
887 else if (kill(tcp->pid, SIGSTOP) < 0) {
889 perror("detach: stopping child");
893 if (waitpid(tcp->pid, &status, 0) < 0) {
895 perror("detach: waiting");
898 if (!WIFSTOPPED(status)) {
899 /* Au revoir, mon ami. */
902 if (WSTOPSIG(status) == SIGSTOP) {
903 if ((error = ptrace(PTRACE_DETACH,
904 tcp->pid, (char *) 1, sig)) < 0) {
906 perror("detach: ptrace(PTRACE_DETACH, ...)");
911 if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1,
912 WSTOPSIG(status) == SIGTRAP ?
913 0 : WSTOPSIG(status))) < 0) {
915 perror("detach: ptrace(PTRACE_CONT, ...)");
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");
927 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0)
928 perror("detach: ptrace(PTRACE_DETACH, ...)");
932 if (waiting_parent(tcp))
933 error = resume(tcp->parent);
937 fprintf(stderr, "Process %u detached\n", tcp->pid);
952 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
971 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
972 if (!(tcp->flags & TCB_INUSE))
976 "cleanup: looking at pid %u\n", tcp->pid);
978 (!outfname || followfork < 2 || tcp_last == tcp)) {
979 tprintf(" <unfinished ...>\n");
982 if (tcp->flags & TCB_ATTACHED)
985 kill(tcp->pid, SIGCONT);
986 kill(tcp->pid, SIGTERM);
1000 #ifndef HAVE_STRERROR
1002 #ifndef SYS_ERRLIST_DECLARED
1003 extern int sys_nerr;
1004 extern char *sys_errlist[];
1005 #endif /* SYS_ERRLIST_DECLARED */
1011 static char buf[64];
1013 if (errno < 1 || errno >= sys_nerr) {
1014 sprintf(buf, "Unknown error %d", errno);
1017 return sys_errlist[errno];
1020 #endif /* HAVE_STERRROR */
1022 #ifndef HAVE_STRSIGNAL
1024 #ifndef SYS_SIGLIST_DECLARED
1025 #ifdef HAVE__SYS_SIGLIST
1026 extern char *_sys_siglist[];
1028 extern char *sys_siglist[];
1030 #endif /* SYS_SIGLIST_DECLARED */
1036 static char buf[64];
1038 if (sig < 1 || sig >= NSIG) {
1039 sprintf(buf, "Unknown signal %d", sig);
1042 #ifdef HAVE__SYS_SIGLIST
1043 return _sys_siglist[sig];
1045 return sys_siglist[sig];
1049 #endif /* HAVE_STRSIGNAL */
1059 for (i = j = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1060 if (!(tcp->flags & TCB_INUSE))
1062 pollv[j].fd = tcp->pfd;
1063 pollv[j].events = POLLWANT;
1067 fprintf(stderr, "strace: proc miscount\n");
1072 #ifndef HAVE_POLLABLE_PROCFS
1080 if (pipe(proc_poll_pipe) < 0) {
1084 for (i = 0; i < 2; i++) {
1085 if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) {
1089 if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) {
1097 proc_poll(pollv, nfds, timeout)
1098 struct pollfd *pollv;
1104 struct proc_pollfd pollinfo;
1106 if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
1108 if (n != sizeof(struct proc_pollfd)) {
1109 fprintf(stderr, "panic: short read: %d\n", n);
1112 for (i = 0; i < nprocs; i++) {
1113 if (pollv[i].fd == pollinfo.fd)
1114 pollv[i].revents = pollinfo.revents;
1116 pollv[i].revents = 0;
1118 poller_pid = pollinfo.pid;
1132 struct proc_pollfd pollinfo;
1133 struct sigaction sa;
1134 sigset_t blocked_set, empty_set;
1149 sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
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);
1164 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
1165 perror("getrlimit(RLIMIT_NOFILE, ...)");
1169 for (i = 0; i < n; i++) {
1170 if (i != pfd && i != proc_poll_pipe[1])
1175 pollinfo.pid = getpid();
1177 if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
1183 pollinfo.revents = POLLERR;
1186 pollinfo.revents = POLLHUP;
1189 perror("proc_poller: PIOCWSTOP");
1191 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1194 pollinfo.revents = POLLWANT;
1195 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1196 sigsuspend(&empty_set);
1200 #endif /* !HAVE_POLLABLE_PROCFS */
1210 if (followfork < 2 &&
1211 last < nprocs && (pollv[last].revents & POLLWANT)) {
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.
1217 tcp = pfd2tcb(pollv[last].fd);
1218 if (tcp && (tcp->flags & TCB_INSYSCALL))
1219 return pollv[last].fd;
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);
1228 fprintf(stderr, "strace: lost proc\n");
1234 if (pollv[j].revents & POLLWANT) {
1239 fprintf(stderr, "strace: nothing ready\n");
1249 int ioctl_result = 0, ioctl_errno = 0;
1254 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1261 #ifndef HAVE_POLLABLE_PROCFS
1262 if (proc_poll_pipe[0] == -1) {
1271 #ifndef HAVE_POLLABLE_PROCFS
1273 /* fall through ... */
1274 #endif /* !HAVE_POLLABLE_PROCFS */
1276 #ifdef HAVE_POLLABLE_PROCFS
1277 if (poll(pollv, nprocs, INFTIM) < 0) {
1282 #else /* !HAVE_POLLABLE_PROCFS */
1283 if (proc_poll(pollv, nprocs, INFTIM) < 0) {
1288 #endif /* !HAVE_POLLABLE_PROCFS */
1295 /* Look up `pfd' in our table. */
1296 if ((tcp = pfd2tcb(pfd)) == NULL) {
1297 fprintf(stderr, "unknown pfd: %u\n", pfd);
1300 /* Get the status of the process. */
1302 ioctl_result = IOCTL_WSTOP (tcp);
1303 ioctl_errno = errno;
1304 #ifndef HAVE_POLLABLE_PROCFS
1305 if (proc_poll_pipe[0] != -1) {
1306 if (ioctl_result < 0)
1307 kill(poller_pid, SIGKILL);
1309 kill(poller_pid, SIGUSR1);
1311 #endif /* !HAVE_POLLABLE_PROCFS */
1317 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1319 if (ioctl_result < 0) {
1320 /* Find out what happened if it failed. */
1321 switch (ioctl_errno) {
1329 perror("PIOCWSTOP");
1334 /* clear the just started flag */
1335 tcp->flags &= ~TCB_STARTUP;
1337 /* set current output file */
1341 struct timeval stime;
1343 stime.tv_sec = tcp->status.pr_stime.tv_sec;
1344 stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
1345 tv_sub(&tcp->dtime, &stime, &tcp->stime);
1349 what = tcp->status.PR_WHAT;
1350 switch (tcp->status.PR_WHY) {
1352 if (tcp->status.PR_FLAGS & PR_ASLEEP) {
1353 tcp->status.PR_WHY = PR_SYSENTRY;
1354 if (trace_syscall(tcp) < 0) {
1355 fprintf(stderr, "syscall trouble\n");
1362 if (trace_syscall(tcp) < 0) {
1363 fprintf(stderr, "syscall trouble\n");
1368 if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) {
1370 tprintf("--- %s (%s) ---",
1371 signame(what), strsignal(what));
1376 if (!cflag && (qual_flags[what] & QUAL_FAULT)) {
1378 tprintf("=== FAULT %d ===", what);
1383 fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
1388 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
1409 while (nprocs != 0) {
1411 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1413 pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
1416 pid = wait(&status);
1420 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1426 switch (wait_errno) {
1431 * We would like to verify this case
1432 * but sometimes a race in Solbourne's
1433 * version of SunOS sometimes reports
1434 * ECHILD before sending us SIGCHILD.
1439 fprintf(stderr, "strace: proc miscount\n");
1445 perror("strace: wait");
1450 fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
1452 /* Look up `pid' in our table. */
1453 if ((tcp = pid2tcb(pid)) == NULL) {
1454 fprintf(stderr, "unknown pid: %u\n", pid);
1455 if (WIFSTOPPED(status))
1456 ptrace(PTRACE_CONT, pid, (char *) 1, 0);
1459 /* set current output file */
1463 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
1464 tcp->stime = ru.ru_stime;
1468 if (tcp->flags & TCB_SUSPENDED) {
1470 * Apparently, doing any ptrace() call on a stopped
1471 * process, provokes the kernel to report the process
1472 * status again on a subsequent wait(), even if the
1473 * process has not been actually restarted.
1474 * Since we have inspected the arguments of suspended
1475 * processes we end up here testing for this case.
1479 if (WIFSIGNALED(status)) {
1481 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
1483 tprintf("+++ killed by %s +++",
1484 signame(WTERMSIG(status)));
1490 if (WIFEXITED(status)) {
1492 fprintf(stderr, "pid %u exited\n", pid);
1493 if (tcp->flags & TCB_ATTACHED)
1495 "PANIC: attached pid %u exited\n",
1500 if (!WIFSTOPPED(status)) {
1501 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
1506 fprintf(stderr, "pid %u stopped, [%s]\n",
1507 pid, signame(WSTOPSIG(status)));
1509 if (tcp->flags & TCB_STARTUP) {
1511 * This flag is there to keep us in sync.
1512 * Next time this process stops it should
1513 * really be entering a system call.
1515 tcp->flags &= ~TCB_STARTUP;
1516 if (tcp->flags & TCB_ATTACHED) {
1518 * Interestingly, the process may stop
1519 * with STOPSIG equal to some other signal
1520 * than SIGSTOP if we happend to attach
1521 * just before the process takes a signal.
1523 if (!WIFSTOPPED(status)) {
1525 "pid %u not stopped\n", pid);
1526 detach(tcp, WSTOPSIG(status));
1532 /* A child of us stopped at exec */
1533 if (WSTOPSIG(status) == SIGTRAP && followvfork)
1537 if (tcp->flags & TCB_BPTSET) {
1538 if (clearbpt(tcp) < 0) /* Pretty fatal */ {
1547 if (WSTOPSIG(status) != SIGTRAP) {
1548 if (WSTOPSIG(status) == SIGSTOP &&
1549 (tcp->flags & TCB_SIGTRAPPED)) {
1551 * Trapped attempt to block SIGTRAP
1552 * Hope we are back in control now.
1554 tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
1555 if (ptrace(PTRACE_SYSCALL,
1556 pid, (char *) 1, 0) < 0) {
1557 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1564 && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
1566 tprintf("--- %s (%s) ---",
1567 signame(WSTOPSIG(status)),
1568 strsignal(WSTOPSIG(status)));
1571 if ((tcp->flags & TCB_ATTACHED) &&
1572 !sigishandled(tcp, WSTOPSIG(status))) {
1573 detach(tcp, WSTOPSIG(status));
1576 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1,
1577 WSTOPSIG(status)) < 0) {
1578 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1582 tcp->flags &= ~TCB_SUSPENDED;
1585 if (trace_syscall(tcp) < 0) {
1586 if (tcp->flags & TCB_ATTACHED)
1590 tcp->pid, (char *) 1, SIGTERM);
1595 if (tcp->flags & TCB_EXITING) {
1596 if (tcp->flags & TCB_ATTACHED)
1598 else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) {
1599 perror("strace: ptrace(PTRACE_CONT, ...)");
1605 if (tcp->flags & TCB_SUSPENDED) {
1607 fprintf(stderr, "Process %u suspended\n", pid);
1611 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) {
1612 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1626 #define VA_START(a, b) va_start(a, b)
1628 #include <varargs.h>
1629 #define VA_START(a, b) va_start(a)
1634 tprintf(const char *fmt, ...)
1636 tprintf(fmt, va_alist)
1643 VA_START(args, fmt);
1645 curcol += vfprintf(outf, fmt, args);
1654 if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) {
1655 tcp_last->flags |= TCB_REPRINT;
1656 tprintf(" <unfinished ...>\n");
1659 if ((followfork == 1 || pflag_seen > 1) && outfname)
1660 tprintf("%-5d ", tcp->pid);
1661 else if (nprocs > 1 && !outfname)
1662 tprintf("[pid %5u] ", tcp->pid);
1664 char str[sizeof("HH:MM:SS")];
1665 struct timeval tv, dtv;
1666 static struct timeval otv;
1668 gettimeofday(&tv, NULL);
1670 if (otv.tv_sec == 0)
1672 tv_sub(&dtv, &tv, &otv);
1673 tprintf("%6ld.%06ld ",
1674 (long) dtv.tv_sec, (long) dtv.tv_usec);
1677 else if (tflag > 2) {
1678 tprintf("%ld.%06ld ",
1679 (long) tv.tv_sec, (long) tv.tv_usec);
1682 time_t local = tv.tv_sec;
1683 strftime(str, sizeof(str), "%T", localtime(&local));
1685 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
1687 tprintf("%s ", str);
1699 tprintf("%*s", col - curcol, "");
1712 int mp_ioctl (int fd, int cmd, void *arg, int size) {
1714 struct iovec iov[2];
1717 iov[0].iov_base = &cmd;
1718 iov[0].iov_len = sizeof cmd;
1721 iov[1].iov_base = arg;
1722 iov[1].iov_len = size;
1725 return writev (fd, iov, n);