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 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/types.h>
38 #include <sys/param.h>
40 #include <sys/resource.h>
48 #if defined(IA64) && defined(LINUX)
49 # include <asm/ptrace_offsets.h>
57 #include <sys/stropts.h>
65 int debug = 0, followfork = 0, followvfork = 0, interactive = 0;
66 int rflag = 0, tflag = 0, dtime = 0, cflag = 0;
67 int iflag = 0, xflag = 0, qflag = 0;
70 /* Sometimes we want to print only succeeding syscalls. */
71 int not_failing_only = 0;
73 char *username = NULL;
77 int acolumn = DEFAULT_ACOLUMN;
78 int max_strlen = DEFAULT_STRLEN;
79 char *outfname = NULL;
82 unsigned int nprocs, tcbtabsize;
84 extern char **environ;
86 static int trace P((void));
87 static void cleanup P((void));
88 static void interrupt P((int sig));
89 static sigset_t empty_set, blocked_set;
91 #ifdef HAVE_SIG_ATOMIC_T
92 static volatile sig_atomic_t interrupted;
93 #else /* !HAVE_SIG_ATOMIC_T */
95 static volatile int interrupted;
97 static int interrupted;
98 #endif /* !__STDC__ */
99 #endif /* !HAVE_SIG_ATOMIC_T */
103 static struct tcb *pfd2tcb P((int pfd));
104 static void reaper P((int sig));
105 static void rebuild_pollv P((void));
106 static struct pollfd *pollv;
108 #ifndef HAVE_POLLABLE_PROCFS
110 static void proc_poll_open P((void));
111 static void proc_poller P((int pfd));
119 static int poller_pid;
120 static int proc_poll_pipe[2] = { -1, -1 };
122 #endif /* !HAVE_POLLABLE_PROCFS */
124 #ifdef HAVE_MP_PROCFS
125 #define POLLWANT POLLWRNORM
127 #define POLLWANT POLLPRI
129 #endif /* USE_PROCFS */
137 usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\
138 [-p pid] ... [-s strsize] [-u username] [-E var=val] ...\n\
139 [command [arg ...]]\n\
140 or: strace -c [-e expr] ... [-O overhead] [-S sortby] [-E var=val] ...\n\
141 [command [arg ...]]\n\
142 -c -- count time, calls, and errors for each syscall and report summary\n\
143 -f -- follow forks, -ff -- with output into separate files\n\
144 -F -- attempt to follow vforks, -h -- print help message\n\
145 -i -- print instruction pointer at time of syscall\n\
146 -q -- suppress messages about attaching, detaching, etc.\n\
147 -r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
148 -T -- print time spent in each syscall, -V -- print version\n\
149 -v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\
150 -x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
151 -a column -- alignment COLUMN for printing syscall results (default %d)\n\
152 -e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
153 options: trace, abbrev, verbose, raw, signal, read, or write\n\
154 -o file -- send trace output to FILE instead of stderr\n\
155 -O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
156 -p pid -- trace process with process id PID, may be repeated\n\
157 -s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
158 -S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
159 -u username -- run command as username handling setuid and/or setgid\n\
160 -E var=val -- put var=val in the environment for command\n\
161 -E var -- remove var from the environment for command\n\
162 " /* this is broken, so don't document it
163 -z -- print only succeeding syscalls\n\
165 , DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
189 static char buf[BUFSIZ];
191 /* Allocate the initial tcbtab. */
192 tcbtabsize = argc; /* Surely enough for all -p args. */
193 tcbtab = (struct tcb **) malloc (tcbtabsize * sizeof tcbtab[0]);
194 tcbtab[0] = (struct tcb *) calloc (tcbtabsize, sizeof *tcbtab[0]);
195 for (tcp = tcbtab[0]; tcp < &tcbtab[0][tcbtabsize]; ++tcp)
196 tcbtab[tcp - tcbtab[0]] = &tcbtab[0][tcp - tcbtab[0]];
201 qualify("trace=all");
202 qualify("abbrev=all");
203 qualify("verbose=all");
204 qualify("signal=all");
205 set_sortby(DEFAULT_SORTBY);
206 set_personality(DEFAULT_PERSONALITY);
207 while ((c = getopt(argc, argv,
208 "+cdfFhiqrtTvVxza:e:o:O:p:s:S:u:E:")) != EOF) {
246 qualify("abbrev=none");
249 printf("%s -- version %s\n", PACKAGE_NAME, VERSION);
253 not_failing_only = 1;
256 acolumn = atoi(optarg);
262 outfname = strdup(optarg);
265 set_overhead(atoi(optarg));
268 if ((pid = atoi(optarg)) <= 0) {
269 fprintf(stderr, "%s: Invalid process id: %s\n",
273 if (pid == getpid()) {
274 fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
277 if ((tcp = alloctcb(pid)) == NULL) {
278 fprintf(stderr, "%s: out of memory\n",
282 tcp->flags |= TCB_ATTACHED;
286 max_strlen = atoi(optarg);
292 username = strdup(optarg);
295 if (putenv(optarg) < 0) {
296 fprintf(stderr, "%s: out of memory\n",
307 /* See if they want to run as another user. */
308 if (username != NULL) {
311 if (getuid() != 0 || geteuid() != 0) {
313 "%s: you must be root to use the -u option\n",
317 if ((pent = getpwnam(username)) == NULL) {
318 fprintf(stderr, "%s: cannot find user `%s'\n",
322 run_uid = pent->pw_uid;
323 run_gid = pent->pw_gid;
331 setreuid(geteuid(), getuid());
334 /* See if they want to pipe the output. */
335 if (outfname && (outfname[0] == '|' || outfname[0] == '!')) {
336 if ((outf = popen(outfname + 1, "w")) == NULL) {
337 fprintf(stderr, "%s: can't popen '%s': %s\n",
338 progname, outfname + 1, strerror(errno));
345 /* Check if they want to redirect the output. */
349 if ((outf = fopen(outfname, "w")) == NULL) {
350 fprintf(stderr, "%s: can't fopen '%s': %s\n",
351 progname, outfname, strerror(errno));
355 if ((f=fcntl(fileno(outf), F_GETFD)) < 0 ) {
356 perror("failed to get flags for outputfile");
360 if (fcntl(fileno(outf), F_SETFD, f|FD_CLOEXEC) < 0 ) {
361 perror("failed to set flags for outputfile");
367 setreuid(geteuid(), getuid());
371 setvbuf(outf, buf, _IOLBF, BUFSIZ);
373 else if (optind < argc) {
378 for (c = 0; c < tcbtabsize; c++) {
380 /* Reinitialize the output since it may have changed. */
382 if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
385 if (proc_open(tcp, 1) < 0) {
386 fprintf(stderr, "trouble opening proc file\n");
390 #else /* !USE_PROCFS */
391 if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
392 perror("attach: ptrace(PTRACE_ATTACH, ...)");
396 #endif /* !USE_PROCFS */
399 "Process %u attached - interrupt to quit\n",
406 char pathname[MAXPATHLEN];
408 filename = argv[optind];
409 if (strchr(filename, '/'))
410 strcpy(pathname, filename);
411 #ifdef USE_DEBUGGING_EXEC
413 * Debuggers customarily check the current directory
414 * first regardless of the path but doing that gives
415 * security geeks a panic attack.
417 else if (stat(filename, &statbuf) == 0)
418 strcpy(pathname, filename);
419 #endif /* USE_DEBUGGING_EXEC */
424 for (path = getenv("PATH"); path && *path; path += m) {
425 if (strchr(path, ':')) {
426 n = strchr(path, ':') - path;
430 m = n = strlen(path);
432 getcwd(pathname, MAXPATHLEN);
433 len = strlen(pathname);
436 strncpy(pathname, path, n);
439 if (len && pathname[len - 1] != '/')
440 pathname[len++] = '/';
441 strcpy(pathname + len, filename);
442 if (stat(pathname, &statbuf) == 0)
446 if (stat(pathname, &statbuf) < 0) {
447 fprintf(stderr, "%s: %s: command not found\n",
451 switch (pid = fork()) {
453 perror("strace: fork");
459 if (outf != stderr) close (fileno (outf));
461 /* Kludge for SGI, see proc_open for details. */
462 sa.sa_handler = foobar;
464 sigemptyset(&sa.sa_mask);
465 sigaction(SIGINT, &sa, NULL);
470 kill(getpid(), SIGSTOP); /* stop HERE */
472 #else /* !USE_PROCFS */
474 close(fileno (outf));
476 if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
477 perror("strace: ptrace(PTRACE_TRACEME, ...)");
481 kill(getpid(), SIGSTOP);
483 if (username != NULL || geteuid() == 0) {
484 uid_t run_euid = run_uid;
485 gid_t run_egid = run_gid;
487 if (statbuf.st_mode & S_ISUID)
488 run_euid = statbuf.st_uid;
489 if (statbuf.st_mode & S_ISGID)
490 run_egid = statbuf.st_gid;
493 * It is important to set groups before we
494 * lose privileges on setuid.
496 if (username != NULL) {
497 if (initgroups(username, run_gid) < 0) {
498 perror("initgroups");
501 if (setregid(run_gid, run_egid) < 0) {
505 if (setreuid(run_uid, run_euid) < 0) {
512 setreuid(run_uid, run_uid);
513 #endif /* !USE_PROCFS */
515 execv(pathname, &argv[optind]);
516 perror("strace: exec");
521 if ((tcp = alloctcb(pid)) == NULL) {
522 fprintf(stderr, "tcb table full\n");
527 if (proc_open(tcp, 0) < 0) {
528 fprintf(stderr, "trouble opening proc file\n");
532 #endif /* USE_PROCFS */
534 fake_execve(tcp, pathname, &argv[optind], environ);
535 #endif /* !USE_PROCFS */
539 else if (pflag_seen == 0)
542 sigemptyset(&empty_set);
543 sigemptyset(&blocked_set);
544 sa.sa_handler = SIG_IGN;
545 sigemptyset(&sa.sa_mask);
547 sigaction(SIGTTOU, &sa, NULL);
548 sigaction(SIGTTIN, &sa, NULL);
550 sigaddset(&blocked_set, SIGHUP);
551 sigaddset(&blocked_set, SIGINT);
552 sigaddset(&blocked_set, SIGQUIT);
553 sigaddset(&blocked_set, SIGPIPE);
554 sigaddset(&blocked_set, SIGTERM);
555 sa.sa_handler = interrupt;
557 /* POSIX signals on sunos4.1 are a little broken. */
558 sa.sa_flags = SA_INTERRUPT;
561 sigaction(SIGHUP, &sa, NULL);
562 sigaction(SIGINT, &sa, NULL);
563 sigaction(SIGQUIT, &sa, NULL);
564 sigaction(SIGPIPE, &sa, NULL);
565 sigaction(SIGTERM, &sa, NULL);
567 sa.sa_handler = reaper;
568 sigaction(SIGCHLD, &sa, NULL);
570 /* Make sure SIGCHLD has the default action so that waitpid
571 definitely works without losing track of children. The user
572 should not have given us a bogus state to inherit, but he might
573 have. Arguably we should detect SIG_IGN here and pass it on
574 to children, but probably noone really needs that. */
575 sa.sa_handler = SIG_DFL;
576 sigaction(SIGCHLD, &sa, NULL);
577 #endif /* USE_PROCFS */
589 char name[MAXPATHLEN];
592 if (outfname && followfork > 1) {
593 sprintf(name, "%s.%u", outfname, tcp->pid);
595 setreuid(geteuid(), getuid());
597 fp = fopen(name, "w");
599 setreuid(geteuid(), getuid());
617 for (i = 0; i < tcbtabsize; i++) {
619 if ((tcp->flags & TCB_INUSE) == 0) {
624 #ifdef TCB_CLONE_THREAD
625 tcp->nclone_threads = tcp->nclone_detached = 0;
626 tcp->nclone_waiting = 0;
628 tcp->flags = TCB_INUSE | TCB_STARTUP;
629 tcp->outf = outf; /* Initialise to current out file */
630 tcp->stime.tv_sec = 0;
631 tcp->stime.tv_usec = 0;
642 proc_open(tcp, attaching)
654 #ifndef HAVE_POLLABLE_PROCFS
658 #ifdef HAVE_MP_PROCFS
659 /* Open the process pseudo-files in /proc. */
660 sprintf(proc, "/proc/%d/ctl", tcp->pid);
661 if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
662 perror("strace: open(\"/proc/...\", ...)");
665 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
669 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
673 sprintf(proc, "/proc/%d/status", tcp->pid);
674 if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
675 perror("strace: open(\"/proc/...\", ...)");
678 if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) {
682 if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) {
686 sprintf(proc, "/proc/%d/as", tcp->pid);
687 if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
688 perror("strace: open(\"/proc/...\", ...)");
691 if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) {
695 if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) {
700 /* Open the process pseudo-file in /proc. */
702 sprintf(proc, "/proc/%d", tcp->pid);
703 if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) {
705 sprintf(proc, "/proc/%d/mem", tcp->pid);
706 if ((tcp->pfd = open(proc, O_RDWR)) < 0) {
708 perror("strace: open(\"/proc/...\", ...)");
711 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
715 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
721 sprintf(proc, "/proc/%d/regs", tcp->pid);
722 if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) {
723 perror("strace: open(\"/proc/.../regs\", ...)");
727 sprintf(proc, "/proc/%d/status", tcp->pid);
728 if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) {
729 perror("strace: open(\"/proc/.../status\", ...)");
733 tcp->pfd_status = -1;
738 * Wait for the child to pause. Because of a race
739 * condition we have to poll for the event.
742 if (IOCTL_STATUS (tcp) < 0) {
743 perror("strace: PIOCSTATUS");
746 if (tcp->status.PR_FLAGS & PR_ASLEEP)
751 /* Stop the process so that we own the stop. */
752 if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) {
753 perror("strace: PIOCSTOP");
758 /* Set Run-on-Last-Close. */
760 if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
761 perror("PIOCSET PR_RLC");
764 /* Set or Reset Inherit-on-Fork. */
766 if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
767 perror("PIOC{SET,RESET} PR_FORK");
772 if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
776 if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
777 perror("PIOC{S,R}FORK");
781 /* just unset the PF_LINGER flag for the Run-on-Last-Close. */
782 if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) {
787 if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) {
792 #endif /* !PIOCSET */
794 /* Enable all syscall entries we care about. */
795 premptyset(&syscalls);
796 for (i = 1; i < MAX_QUALS; ++i) {
797 if (i > (sizeof syscalls) * CHAR_BIT) break;
798 if (qual_flags [i] & QUAL_TRACE) praddset (&syscalls, i);
800 praddset (&syscalls, SYS_execve);
802 praddset (&syscalls, SYS_fork);
804 praddset (&syscalls, SYS_forkall);
807 praddset (&syscalls, SYS_fork1);
810 praddset (&syscalls, SYS_rfork1);
813 praddset (&syscalls, SYS_rforkall);
816 if (IOCTL(tcp->pfd, PIOCSENTRY, &syscalls) < 0) {
817 perror("PIOCSENTRY");
820 /* Enable the syscall exits. */
821 if (IOCTL(tcp->pfd, PIOCSEXIT, &syscalls) < 0) {
825 /* Enable signals we care about. */
826 premptyset(&signals);
827 for (i = 1; i < MAX_QUALS; ++i) {
828 if (i > (sizeof signals) * CHAR_BIT) break;
829 if (qual_flags [i] & QUAL_SIGNAL) praddset (&signals, i);
831 if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
832 perror("PIOCSTRACE");
835 /* Enable faults we care about */
837 for (i = 1; i < MAX_QUALS; ++i) {
838 if (i > (sizeof faults) * CHAR_BIT) break;
839 if (qual_flags [i] & QUAL_FAULT) praddset (&faults, i);
841 if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
842 perror("PIOCSFAULT");
846 /* set events flags. */
847 arg = S_SIG | S_SCE | S_SCX ;
848 if(ioctl(tcp->pfd, PIOCBIS, arg) < 0) {
856 * The SGI PRSABORT doesn't work for pause() so
857 * we send it a caught signal to wake it up.
859 kill(tcp->pid, SIGINT);
862 /* The child is in a pause(), abort it. */
864 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
871 /* wake up the child if it received the SIGSTOP */
872 kill(tcp->pid, SIGCONT);
875 /* Wait for the child to do something. */
876 if (IOCTL_WSTOP (tcp) < 0) {
880 if (tcp->status.PR_WHY == PR_SYSENTRY) {
881 tcp->flags &= ~TCB_INSYSCALL;
883 if (tcp->scno == SYS_execve)
886 /* Set it running: maybe execve will be next. */
889 if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
891 if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) {
897 /* handle the case where we "opened" the child before
898 it did the kill -STOP */
899 if (tcp->status.PR_WHY == PR_SIGNALLED &&
900 tcp->status.PR_WHAT == SIGSTOP)
901 kill(tcp->pid, SIGCONT);
909 /* We are attaching to an already running process.
910 * Try to figure out the state of the process in syscalls,
911 * to handle the first event well.
912 * This is done by having a look at the "wchan" property of the
913 * process, which tells where it is stopped (if it is). */
915 char wchan[20]; /* should be enough */
917 sprintf(proc, "/proc/%d/status", tcp->pid);
918 status = fopen(proc, "r");
920 (fscanf(status, "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d"
921 "%*d,%*d %*d,%*d %19s", wchan) == 1) &&
922 strcmp(wchan, "nochan") && strcmp(wchan, "spread") &&
923 strcmp(wchan, "stopevent")) {
924 /* The process is asleep in the middle of a syscall.
925 Fake the syscall entry event */
926 tcp->flags &= ~(TCB_INSYSCALL|TCB_STARTUP);
927 tcp->status.PR_WHY = PR_SYSENTRY;
932 } /* otherwise it's a fork being followed */
935 #ifndef HAVE_POLLABLE_PROCFS
936 if (proc_poll_pipe[0] != -1)
937 proc_poller(tcp->pfd);
938 else if (nprocs > 1) {
940 proc_poller(last_pfd);
941 proc_poller(tcp->pfd);
944 #endif /* !HAVE_POLLABLE_PROCFS */
948 #endif /* USE_PROCFS */
957 for (i = 0; i < tcbtabsize; i++) {
959 if (pid && tcp->pid != pid)
961 if (tcp->flags & TCB_INUSE)
975 for (i = 0; i < tcbtabsize; i++) {
976 struct tcb *tcp = tcbtab[i];
979 if (tcp->flags & TCB_INUSE)
985 #endif /* USE_PROCFS */
993 #ifdef TCB_CLONE_THREAD
994 if (tcp->nclone_threads > 0) {
995 /* There are other threads left in this process, but this
996 is the one whose PID represents the whole process.
997 We need to keep this record around as a zombie until
998 all the threads die. */
999 tcp->flags |= TCB_EXITING;
1006 if (tcp->parent != NULL) {
1007 tcp->parent->nchildren--;
1008 #ifdef TCB_CLONE_THREAD
1009 if (tcp->flags & TCB_CLONE_DETACHED)
1010 tcp->parent->nclone_detached--;
1011 if (tcp->flags & TCB_CLONE_THREAD)
1012 tcp->parent->nclone_threads--;
1014 #ifdef TCB_CLONE_DETACHED
1015 if (!(tcp->flags & TCB_CLONE_DETACHED))
1017 tcp->parent->nzombies++;
1022 if (tcp->pfd != -1) {
1026 if (tcp->pfd_reg != -1) {
1027 close(tcp->pfd_reg);
1030 if (tcp->pfd_status != -1) {
1031 close(tcp->pfd_status);
1032 tcp->pfd_status = -1;
1034 #endif /* !FREEBSD */
1036 rebuild_pollv(); /* Note, flags needs to be cleared by now. */
1040 if (outfname && followfork > 1 && tcp->outf)
1055 if (!(tcp->flags & TCB_SUSPENDED)) {
1056 fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid);
1059 tcp->flags &= ~TCB_SUSPENDED;
1060 #ifdef TCB_CLONE_THREAD
1061 if (tcp->flags & TCB_CLONE_THREAD)
1062 tcp->parent->nclone_waiting--;
1065 if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) {
1066 perror("resume: ptrace(PTRACE_SYSCALL, ...)");
1071 fprintf(stderr, "Process %u resumed\n", tcp->pid);
1075 #endif /* !USE_PROCFS */
1077 /* detach traced process; continue with sig */
1086 int status, resumed;
1089 if (tcp->flags & TCB_BPTSET)
1094 * Linux wrongly insists the child be stopped
1095 * before detaching. Arghh. We go through hoops
1096 * to make a clean break of things.
1099 #undef PTRACE_DETACH
1100 #define PTRACE_DETACH PTRACE_SUNDETACH
1102 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
1103 /* On a clear day, you can see forever. */
1105 else if (errno != ESRCH) {
1106 /* Shouldn't happen. */
1107 perror("detach: ptrace(PTRACE_DETACH, ...)");
1109 else if (kill(tcp->pid, 0) < 0) {
1111 perror("detach: checking sanity");
1113 else if (kill(tcp->pid, SIGSTOP) < 0) {
1115 perror("detach: stopping child");
1120 if (wait4(tcp->pid, &status, __WALL, NULL) < 0) {
1121 if (errno == ECHILD) /* Already gone. */
1123 if (errno != EINVAL) {
1124 perror("detach: waiting");
1128 /* No __WALL here. */
1129 if (waitpid(tcp->pid, &status, 0) < 0) {
1130 if (errno != ECHILD) {
1131 perror("detach: waiting");
1135 /* If no processes, try clones. */
1136 if (wait4(tcp->pid, &status, __WCLONE,
1138 if (errno != ECHILD)
1139 perror("detach: waiting");
1142 #endif /* __WCLONE */
1147 if (!WIFSTOPPED(status)) {
1148 /* Au revoir, mon ami. */
1151 if (WSTOPSIG(status) == SIGSTOP) {
1152 if ((error = ptrace(PTRACE_DETACH,
1153 tcp->pid, (char *) 1, sig)) < 0) {
1155 perror("detach: ptrace(PTRACE_DETACH, ...)");
1156 /* I died trying. */
1160 if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1,
1161 WSTOPSIG(status) == SIGTRAP ?
1162 0 : WSTOPSIG(status))) < 0) {
1164 perror("detach: ptrace(PTRACE_CONT, ...)");
1172 /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
1173 if (sig && kill(tcp->pid, sig) < 0)
1174 perror("detach: kill");
1176 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0)
1177 perror("detach: ptrace(PTRACE_DETACH, ...)");
1183 /* XXX This won't always be quite right (but it never was).
1184 A waiter with argument 0 or < -1 is waiting for any pid in
1185 a particular pgrp, which this child might or might not be
1186 in. The waiter will only wake up if it's argument is -1
1187 or if it's waiting for tcp->pid's pgrp. It makes a
1188 difference to wake up a waiter when there might be more
1189 traced children, because it could get a false ECHILD
1190 error. OTOH, if this was the last child in the pgrp, then
1191 it ought to wake up and get ECHILD. We would have to
1192 search the system for all pid's in the pgrp to be sure.
1194 && (t->waitpid == -1 ||
1195 (t->waitpid == 0 && getpgid (tcp->pid) == getpgid (t->pid))
1196 || (t->waitpid < 0 && t->waitpid == -getpid (t->pid)))
1200 (tcp->parent->flags & TCB_SUSPENDED) &&
1201 (tcp->parent->waitpid <= 0 || tcp->parent->waitpid == tcp->pid)) {
1202 error = resume(tcp->parent);
1205 #ifdef TCB_CLONE_THREAD
1206 if (tcp->parent && tcp->parent->nclone_waiting > 0) {
1207 /* Some other threads of our parent are waiting too. */
1210 /* Resume all the threads that were waiting for this PID. */
1211 for (i = 0; i < tcbtabsize; i++) {
1212 struct tcb *t = tcbtab[i];
1213 if (t->parent == tcp->parent && t != tcp
1214 && ((t->flags & (TCB_CLONE_THREAD|TCB_SUSPENDED))
1215 == (TCB_CLONE_THREAD|TCB_SUSPENDED))
1216 && t->waitpid == tcp->pid) {
1217 error |= resume (t);
1222 /* Noone was waiting for this PID in particular,
1223 so now we might need to resume some wildcarders. */
1224 for (i = 0; i < tcbtabsize; i++) {
1225 struct tcb *t = tcbtab[i];
1226 if (t->parent == tcp->parent && t != tcp
1228 & (TCB_CLONE_THREAD|TCB_SUSPENDED))
1229 == (TCB_CLONE_THREAD|TCB_SUSPENDED))
1232 error |= resume (t);
1239 #endif /* !USE_PROCFS */
1242 fprintf(stderr, "Process %u detached\n", tcp->pid);
1257 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
1268 #endif /* USE_PROCFS */
1276 for (i = 0; i < tcbtabsize; i++) {
1278 if (!(tcp->flags & TCB_INUSE))
1282 "cleanup: looking at pid %u\n", tcp->pid);
1284 (!outfname || followfork < 2 || tcp_last == tcp)) {
1285 tprintf(" <unfinished ...>\n");
1288 if (tcp->flags & TCB_ATTACHED)
1291 kill(tcp->pid, SIGCONT);
1292 kill(tcp->pid, SIGTERM);
1306 #ifndef HAVE_STRERROR
1308 #if !HAVE_DECL_SYS_ERRLIST
1309 extern int sys_nerr;
1310 extern char *sys_errlist[];
1311 #endif /* HAVE_DECL_SYS_ERRLIST */
1317 static char buf[64];
1319 if (errno < 1 || errno >= sys_nerr) {
1320 sprintf(buf, "Unknown error %d", errno);
1323 return sys_errlist[errno];
1326 #endif /* HAVE_STERRROR */
1328 #ifndef HAVE_STRSIGNAL
1330 #if defined HAVE_SYS_SIGLIST && !defined HAVE_DECL_SYS_SIGLIST
1331 extern char *sys_siglist[];
1333 #if defined HAVE_SYS__SIGLIST && !defined HAVE_DECL__SYS_SIGLIST
1334 extern char *_sys_siglist[];
1341 static char buf[64];
1343 if (sig < 1 || sig >= NSIG) {
1344 sprintf(buf, "Unknown signal %d", sig);
1347 #ifdef HAVE__SYS_SIGLIST
1348 return _sys_siglist[sig];
1350 return sys_siglist[sig];
1354 #endif /* HAVE_STRSIGNAL */
1365 pollv = (struct pollfd *) malloc(nprocs * sizeof pollv[0]);
1366 if (pollv == NULL) {
1367 fprintf(stderr, "strace: out of memory for poll vector\n");
1371 for (i = j = 0; i < tcbtabsize; i++) {
1372 struct tcb *tcp = tcbtab[i];
1373 if (!(tcp->flags & TCB_INUSE))
1375 pollv[j].fd = tcp->pfd;
1376 pollv[j].events = POLLWANT;
1380 fprintf(stderr, "strace: proc miscount\n");
1385 #ifndef HAVE_POLLABLE_PROCFS
1393 if (pipe(proc_poll_pipe) < 0) {
1397 for (i = 0; i < 2; i++) {
1398 if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) {
1402 if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) {
1410 proc_poll(pollv, nfds, timeout)
1411 struct pollfd *pollv;
1417 struct proc_pollfd pollinfo;
1419 if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
1421 if (n != sizeof(struct proc_pollfd)) {
1422 fprintf(stderr, "panic: short read: %d\n", n);
1425 for (i = 0; i < nprocs; i++) {
1426 if (pollv[i].fd == pollinfo.fd)
1427 pollv[i].revents = pollinfo.revents;
1429 pollv[i].revents = 0;
1431 poller_pid = pollinfo.pid;
1445 struct proc_pollfd pollinfo;
1446 struct sigaction sa;
1447 sigset_t blocked_set, empty_set;
1452 struct procfs_status pfs;
1453 #endif /* FREEBSD */
1465 sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
1467 sigemptyset(&sa.sa_mask);
1468 sigaction(SIGHUP, &sa, NULL);
1469 sigaction(SIGINT, &sa, NULL);
1470 sigaction(SIGQUIT, &sa, NULL);
1471 sigaction(SIGPIPE, &sa, NULL);
1472 sigaction(SIGTERM, &sa, NULL);
1473 sa.sa_handler = wakeup_handler;
1474 sigaction(SIGUSR1, &sa, NULL);
1475 sigemptyset(&blocked_set);
1476 sigaddset(&blocked_set, SIGUSR1);
1477 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1478 sigemptyset(&empty_set);
1480 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
1481 perror("getrlimit(RLIMIT_NOFILE, ...)");
1485 for (i = 0; i < n; i++) {
1486 if (i != pfd && i != proc_poll_pipe[1])
1491 pollinfo.pid = getpid();
1494 if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
1496 if (ioctl(pfd, PIOCWSTOP, &pfs) < 0)
1497 #endif /* FREEBSD */
1503 pollinfo.revents = POLLERR;
1506 pollinfo.revents = POLLHUP;
1509 perror("proc_poller: PIOCWSTOP");
1511 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1514 pollinfo.revents = POLLWANT;
1515 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1516 sigsuspend(&empty_set);
1520 #endif /* !HAVE_POLLABLE_PROCFS */
1530 if (followfork < 2 &&
1531 last < nprocs && (pollv[last].revents & POLLWANT)) {
1533 * The previous process is ready to run again. We'll
1534 * let it do so if it is currently in a syscall. This
1535 * heuristic improves the readability of the trace.
1537 tcp = pfd2tcb(pollv[last].fd);
1538 if (tcp && (tcp->flags & TCB_INSYSCALL))
1539 return pollv[last].fd;
1542 for (i = 0; i < nprocs; i++) {
1543 /* Let competing children run round robin. */
1544 j = (i + last + 1) % nprocs;
1545 if (pollv[j].revents & (POLLHUP | POLLERR)) {
1546 tcp = pfd2tcb(pollv[j].fd);
1548 fprintf(stderr, "strace: lost proc\n");
1554 if (pollv[j].revents & POLLWANT) {
1559 fprintf(stderr, "strace: nothing ready\n");
1567 struct tcb *in_syscall = NULL;
1572 int ioctl_result = 0, ioctl_errno = 0;
1577 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1584 #ifndef HAVE_POLLABLE_PROCFS
1585 if (proc_poll_pipe[0] == -1) {
1594 #ifndef HAVE_POLLABLE_PROCFS
1596 /* fall through ... */
1597 #endif /* !HAVE_POLLABLE_PROCFS */
1599 #ifdef HAVE_POLLABLE_PROCFS
1601 /* On some systems (e.g. UnixWare) we get too much ugly
1602 "unfinished..." stuff when multiple proceses are in
1603 syscalls. Here's a nasty hack */
1610 pv.events = POLLWANT;
1611 if ((what = poll (&pv, 1, 1)) < 0) {
1616 else if (what == 1 && pv.revents & POLLWANT) {
1622 if (poll(pollv, nprocs, INFTIM) < 0) {
1627 #else /* !HAVE_POLLABLE_PROCFS */
1628 if (proc_poll(pollv, nprocs, INFTIM) < 0) {
1633 #endif /* !HAVE_POLLABLE_PROCFS */
1640 /* Look up `pfd' in our table. */
1641 if ((tcp = pfd2tcb(pfd)) == NULL) {
1642 fprintf(stderr, "unknown pfd: %u\n", pfd);
1648 /* Get the status of the process. */
1651 ioctl_result = IOCTL_WSTOP (tcp);
1653 /* Thanks to some scheduling mystery, the first poller
1654 sometimes waits for the already processed end of fork
1655 event. Doing a non blocking poll here solves the problem. */
1656 if (proc_poll_pipe[0] != -1)
1657 ioctl_result = IOCTL_STATUS (tcp);
1659 ioctl_result = IOCTL_WSTOP (tcp);
1660 #endif /* FREEBSD */
1661 ioctl_errno = errno;
1662 #ifndef HAVE_POLLABLE_PROCFS
1663 if (proc_poll_pipe[0] != -1) {
1664 if (ioctl_result < 0)
1665 kill(poller_pid, SIGKILL);
1667 kill(poller_pid, SIGUSR1);
1669 #endif /* !HAVE_POLLABLE_PROCFS */
1675 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1677 if (ioctl_result < 0) {
1678 /* Find out what happened if it failed. */
1679 switch (ioctl_errno) {
1690 perror("PIOCWSTOP");
1696 if ((tcp->flags & TCB_STARTUP) && (tcp->status.PR_WHY == PR_SYSEXIT)) {
1697 /* discard first event for a syscall we never entered */
1698 IOCTL (tcp->pfd, PIOCRUN, 0);
1703 /* clear the just started flag */
1704 tcp->flags &= ~TCB_STARTUP;
1706 /* set current output file */
1710 struct timeval stime;
1715 if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) {
1718 "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld",
1719 &stime.tv_sec, &stime.tv_usec);
1721 stime.tv_sec = stime.tv_usec = 0;
1722 #else /* !FREEBSD */
1723 stime.tv_sec = tcp->status.pr_stime.tv_sec;
1724 stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
1725 #endif /* !FREEBSD */
1726 tv_sub(&tcp->dtime, &stime, &tcp->stime);
1729 what = tcp->status.PR_WHAT;
1730 switch (tcp->status.PR_WHY) {
1733 if (tcp->status.PR_FLAGS & PR_ASLEEP) {
1734 tcp->status.PR_WHY = PR_SYSENTRY;
1735 if (trace_syscall(tcp) < 0) {
1736 fprintf(stderr, "syscall trouble\n");
1741 #endif /* !FREEBSD */
1747 if (trace_syscall(tcp) < 0) {
1748 fprintf(stderr, "syscall trouble\n");
1753 if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) {
1755 tprintf("--- %s (%s) ---",
1756 signame(what), strsignal(what));
1759 if (tcp->status.PR_INFO.si_signo == what) {
1761 tprintf(" siginfo=");
1762 printsiginfo(&tcp->status.PR_INFO, 1);
1769 if (!cflag && (qual_flags[what] & QUAL_FAULT)) {
1771 tprintf("=== FAULT %d ===", what);
1776 case 0: /* handle case we polled for nothing */
1780 fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
1786 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
1788 if (IOCTL (tcp->pfd, PIOCRUN, 0) < 0) {
1797 #else /* !USE_PROCFS */
1799 #ifdef TCB_GROUP_EXITING
1800 /* Handle an exit detach or death signal that is taking all the
1801 related clone threads with it. This is called in three circumstances:
1802 SIG == -1 TCP has already died (TCB_ATTACHED is clear, strace is parent).
1803 SIG == 0 Continuing TCP will perform an exit_group syscall.
1804 SIG == other Continuing TCP with SIG will kill the process.
1807 handle_group_exit(struct tcb *tcp, int sig)
1809 /* We need to locate our records of all the clone threads
1810 related to TCP, either its children or siblings. */
1811 struct tcb *leader = ((tcp->flags & TCB_CLONE_THREAD)
1813 : tcp->nclone_detached > 0
1817 if (leader != NULL && leader != tcp)
1819 "PANIC: handle_group_exit: %d leader %d\n",
1820 tcp->pid, leader ? leader->pid : -1);
1821 droptcb(tcp); /* Already died. */
1824 if (tcp->flags & TCB_ATTACHED) {
1825 if (leader != NULL && leader != tcp) {
1826 /* We need to detach the leader so that the
1827 process death will be reported to its real
1828 parent. But we kill it first to prevent
1829 it doing anything before we kill the whole
1830 process in a moment. We can use
1831 PTRACE_KILL on a thread that's not already
1832 stopped. Then the value we pass in
1833 PTRACE_DETACH just sets the death
1834 signal reported to the real parent. */
1835 ptrace(PTRACE_KILL, leader->pid, 0, 0);
1838 " [%d exit %d kills %d]\n",
1839 tcp->pid, sig, leader->pid);
1840 detach(leader, sig);
1844 else if (ptrace(PTRACE_CONT, tcp->pid, (char *) 1, sig) < 0) {
1845 perror("strace: ptrace(PTRACE_CONT, ...)");
1850 if (leader != NULL && leader != tcp)
1852 /* The leader will report to us as parent now,
1853 and then we'll get to the SIG==-1 case. */
1858 /* Note that TCP and LEADER are no longer valid,
1859 but we can still compare against them. */
1860 if (leader != NULL) {
1862 for (i = 0; i < tcbtabsize; i++) {
1863 struct tcb *t = tcbtab[i];
1864 if (t != tcp && (t->flags & TCB_CLONE_DETACHED)
1865 && t->parent == leader)
1884 static int wait4_options = __WALL;
1888 while (nprocs != 0) {
1890 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1893 pid = wait4(-1, &status, wait4_options, cflag ? &ru : NULL);
1894 if (pid < 0 && (wait4_options & __WALL) && errno == EINVAL) {
1895 /* this kernel does not support __WALL */
1896 wait4_options &= ~__WALL;
1898 pid = wait4(-1, &status, wait4_options,
1899 cflag ? &ru : NULL);
1901 if (pid < 0 && !(wait4_options & __WALL) && errno == ECHILD) {
1902 /* most likely a "cloned" process */
1903 pid = wait4(-1, &status, __WCLONE,
1904 cflag ? &ru : NULL);
1906 fprintf(stderr, "strace: clone wait4 "
1907 "failed: %s\n", strerror(errno));
1911 pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
1915 pid = wait(&status);
1919 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1925 switch (wait_errno) {
1930 * We would like to verify this case
1931 * but sometimes a race in Solbourne's
1932 * version of SunOS sometimes reports
1933 * ECHILD before sending us SIGCHILD.
1938 fprintf(stderr, "strace: proc miscount\n");
1944 perror("strace: wait");
1949 fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
1951 /* Look up `pid' in our table. */
1952 if ((tcp = pid2tcb(pid)) == NULL) {
1954 if (followfork || followvfork) {
1955 /* This is needed to go with the CLONE_PTRACE
1956 changes in process.c/util.c: we might see
1957 the child's initial trap before we see the
1958 parent return from the clone syscall.
1959 Leave the child suspended until the parent
1960 returns from its system call. Only then
1961 will we have the association of parent and
1962 child so that we know how to do clearbpt
1964 if ((tcp = alloctcb(pid)) == NULL) {
1965 fprintf(stderr, " [tcb table full]\n");
1966 kill(pid, SIGKILL); /* XXX */
1969 tcp->flags |= TCB_ATTACHED | TCB_SUSPENDED;
1973 Process %d attached (waiting for parent)\n",
1977 /* This can happen if a clone call used
1978 CLONE_PTRACE itself. */
1981 fprintf(stderr, "unknown pid: %u\n", pid);
1982 if (WIFSTOPPED(status))
1983 ptrace(PTRACE_CONT, pid, (char *) 1, 0);
1987 /* set current output file */
1991 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
1992 tcp->stime = ru.ru_stime;
1996 if (tcp->flags & TCB_SUSPENDED) {
1998 * Apparently, doing any ptrace() call on a stopped
1999 * process, provokes the kernel to report the process
2000 * status again on a subsequent wait(), even if the
2001 * process has not been actually restarted.
2002 * Since we have inspected the arguments of suspended
2003 * processes we end up here testing for this case.
2007 if (WIFSIGNALED(status)) {
2009 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
2011 tprintf("+++ killed by %s +++",
2012 signame(WTERMSIG(status)));
2015 #ifdef TCB_GROUP_EXITING
2016 handle_group_exit(tcp, -1);
2022 if (WIFEXITED(status)) {
2024 fprintf(stderr, "pid %u exited\n", pid);
2025 if (tcp->flags & TCB_ATTACHED)
2027 "PANIC: attached pid %u exited\n",
2029 #ifdef TCB_GROUP_EXITING
2030 handle_group_exit(tcp, -1);
2036 if (!WIFSTOPPED(status)) {
2037 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
2042 fprintf(stderr, "pid %u stopped, [%s]\n",
2043 pid, signame(WSTOPSIG(status)));
2045 if (tcp->flags & TCB_STARTUP) {
2047 * This flag is there to keep us in sync.
2048 * Next time this process stops it should
2049 * really be entering a system call.
2051 tcp->flags &= ~TCB_STARTUP;
2052 if (tcp->flags & TCB_ATTACHED) {
2054 * Interestingly, the process may stop
2055 * with STOPSIG equal to some other signal
2056 * than SIGSTOP if we happend to attach
2057 * just before the process takes a signal.
2059 if (!WIFSTOPPED(status)) {
2061 "pid %u not stopped\n", pid);
2062 detach(tcp, WSTOPSIG(status));
2068 /* A child of us stopped at exec */
2069 if (WSTOPSIG(status) == SIGTRAP && followvfork)
2073 if (tcp->flags & TCB_BPTSET) {
2074 if (clearbpt(tcp) < 0) /* Pretty fatal */ {
2083 if (WSTOPSIG(status) != SIGTRAP) {
2084 if (WSTOPSIG(status) == SIGSTOP &&
2085 (tcp->flags & TCB_SIGTRAPPED)) {
2087 * Trapped attempt to block SIGTRAP
2088 * Hope we are back in control now.
2090 tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
2091 if (ptrace(PTRACE_SYSCALL,
2092 pid, (char *) 1, 0) < 0) {
2093 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
2100 && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
2101 unsigned long addr = 0, pc = 0;
2102 #ifdef PT_GETSIGINFO
2107 upeek(pid, PT_CR_IPSR, &psr);
2108 upeek(pid, PT_CR_IIP, &pc);
2110 pc += (psr >> PSR_RI) & 0x3;
2111 ptrace(PT_GETSIGINFO, pid, 0, (long) &si);
2112 addr = (unsigned long) si.si_addr;
2115 tprintf("--- %s (%s) @ %lx (%lx) ---",
2116 signame(WSTOPSIG(status)),
2117 strsignal(WSTOPSIG(status)), pc, addr);
2120 if ((tcp->flags & TCB_ATTACHED) &&
2121 !sigishandled(tcp, WSTOPSIG(status))) {
2122 #ifdef TCB_GROUP_EXITING
2123 handle_group_exit(tcp, WSTOPSIG(status));
2125 detach(tcp, WSTOPSIG(status));
2129 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1,
2130 WSTOPSIG(status)) < 0) {
2131 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
2135 tcp->flags &= ~TCB_SUSPENDED;
2138 if (trace_syscall(tcp) < 0) {
2139 if (tcp->flags & TCB_ATTACHED)
2143 tcp->pid, (char *) 1, SIGTERM);
2148 if (tcp->flags & TCB_EXITING) {
2149 #ifdef TCB_GROUP_EXITING
2150 if (tcp->flags & TCB_GROUP_EXITING) {
2151 if (handle_group_exit(tcp, 0) < 0)
2156 if (tcp->flags & TCB_ATTACHED)
2158 else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) {
2159 perror("strace: ptrace(PTRACE_CONT, ...)");
2165 if (tcp->flags & TCB_SUSPENDED) {
2167 fprintf(stderr, "Process %u suspended\n", pid);
2171 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) {
2172 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
2180 #endif /* !USE_PROCFS */
2186 #define VA_START(a, b) va_start(a, b)
2188 #include <varargs.h>
2189 #define VA_START(a, b) va_start(a)
2194 tprintf(const char *fmt, ...)
2196 tprintf(fmt, va_alist)
2203 VA_START(args, fmt);
2205 curcol += vfprintf(outf, fmt, args);
2214 if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) {
2215 tcp_last->flags |= TCB_REPRINT;
2216 tprintf(" <unfinished ...>\n");
2219 if ((followfork == 1 || pflag_seen > 1) && outfname)
2220 tprintf("%-5d ", tcp->pid);
2221 else if (nprocs > 1 && !outfname)
2222 tprintf("[pid %5u] ", tcp->pid);
2224 char str[sizeof("HH:MM:SS")];
2225 struct timeval tv, dtv;
2226 static struct timeval otv;
2228 gettimeofday(&tv, NULL);
2230 if (otv.tv_sec == 0)
2232 tv_sub(&dtv, &tv, &otv);
2233 tprintf("%6ld.%06ld ",
2234 (long) dtv.tv_sec, (long) dtv.tv_usec);
2237 else if (tflag > 2) {
2238 tprintf("%ld.%06ld ",
2239 (long) tv.tv_sec, (long) tv.tv_usec);
2242 time_t local = tv.tv_sec;
2243 strftime(str, sizeof(str), "%T", localtime(&local));
2245 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
2247 tprintf("%s ", str);
2259 tprintf("%*s", col - curcol, "");
2270 #ifdef HAVE_MP_PROCFS
2272 int mp_ioctl (int fd, int cmd, void *arg, int size) {
2274 struct iovec iov[2];
2277 iov[0].iov_base = &cmd;
2278 iov[0].iov_len = sizeof cmd;
2281 iov[1].iov_base = arg;
2282 iov[1].iov_len = size;
2285 return writev (fd, iov, n);