blob: fd6ac6d69ee3be2c0cb3177b82de15f4501610cc [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00006 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * $Id$
31 */
32
Wichert Akkerman2ee6e452000-02-18 15:36:12 +000033#include <sys/types.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000034#include "defs.h"
35
36#include <signal.h>
37#include <errno.h>
38#include <sys/param.h>
39#include <fcntl.h>
40#include <sys/resource.h>
41#include <sys/wait.h>
42#include <sys/stat.h>
43#include <pwd.h>
44#include <grp.h>
45#include <string.h>
John Hughes19e49982001-10-19 08:59:12 +000046#include <limits.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000047
Wichert Akkerman7b3346b2001-10-09 23:47:38 +000048#if defined(IA64) && defined(LINUX)
49# include <asm/ptrace_offsets.h>
50#endif
51
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +000052#ifdef USE_PROCFS
53#include <poll.h>
54#endif
55
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000056#ifdef SVR4
57#include <sys/stropts.h>
Wichert Akkermanea78f0f1999-11-29 15:34:02 +000058#ifdef HAVE_MP_PROCFS
John Hughes1d08dcf2001-07-10 13:48:44 +000059#ifdef HAVE_SYS_UIO_H
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000060#include <sys/uio.h>
61#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000062#endif
John Hughes1d08dcf2001-07-10 13:48:44 +000063#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000064
65int debug = 0, followfork = 0, followvfork = 0, interactive = 0;
66int rflag = 0, tflag = 0, dtime = 0, cflag = 0;
67int iflag = 0, xflag = 0, qflag = 0;
68int pflag_seen = 0;
69
70char *username = NULL;
71uid_t run_uid;
72gid_t run_gid;
73
74int acolumn = DEFAULT_ACOLUMN;
75int max_strlen = DEFAULT_STRLEN;
76char *outfname = NULL;
77FILE *outf;
78struct tcb tcbtab[MAX_PROCS];
79int nprocs;
80char *progname;
81extern char version[];
82extern char **environ;
83
84static struct tcb *pid2tcb P((int pid));
85static int trace P((void));
86static void cleanup P((void));
87static void interrupt P((int sig));
88static sigset_t empty_set, blocked_set;
89
90#ifdef HAVE_SIG_ATOMIC_T
91static volatile sig_atomic_t interrupted;
92#else /* !HAVE_SIG_ATOMIC_T */
93#ifdef __STDC__
94static volatile int interrupted;
95#else /* !__STDC__ */
96static int interrupted;
97#endif /* !__STDC__ */
98#endif /* !HAVE_SIG_ATOMIC_T */
99
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000100#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000101
102static struct tcb *pfd2tcb P((int pfd));
103static void reaper P((int sig));
104static void rebuild_pollv P((void));
Wichert Akkermane68d61c1999-06-28 13:17:16 +0000105struct pollfd pollv[MAX_PROCS];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000106
107#ifndef HAVE_POLLABLE_PROCFS
108
109static void proc_poll_open P((void));
110static void proc_poller P((int pfd));
111
112struct proc_pollfd {
113 int fd;
114 int revents;
115 int pid;
116};
117
118static int poller_pid;
119static int proc_poll_pipe[2] = { -1, -1 };
120
121#endif /* !HAVE_POLLABLE_PROCFS */
122
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000123#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000124#define POLLWANT POLLWRNORM
125#else
126#define POLLWANT POLLPRI
127#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000128#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000129
130static void
131usage(ofp, exitval)
132FILE *ofp;
133int exitval;
134{
135 fprintf(ofp, "\
136usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\
137 [-p pid] ... [-s strsize] [-u username] [command [arg ...]]\n\
138 or: strace -c [-e expr] ... [-O overhead] [-S sortby] [command [arg ...]]\n\
139-c -- count time, calls, and errors for each syscall and report summary\n\
140-f -- follow forks, -ff -- with output into separate files\n\
141-F -- attempt to follow vforks, -h -- print help message\n\
142-i -- print instruction pointer at time of syscall\n\
143-q -- suppress messages about attaching, detaching, etc.\n\
144-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
145-T -- print time spent in each syscall, -V -- print version\n\
146-v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\
147-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
148-a column -- alignment COLUMN for printing syscall results (default %d)\n\
149-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
150 options: trace, abbrev, verbose, raw, signal, read, or write\n\
151-o file -- send trace output to FILE instead of stderr\n\
152-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
153-p pid -- trace process with process id PID, may be repeated\n\
154-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
155-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
156-u username -- run command as username handling setuid and/or setgid\n\
157", DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
158 exit(exitval);
159}
160
161#ifdef SVR4
162#ifdef MIPS
163void
164foobar()
165{
166}
167#endif /* MIPS */
168#endif /* SVR4 */
169
170int
171main(argc, argv)
172int argc;
173char *argv[];
174{
175 extern int optind;
176 extern char *optarg;
177 struct tcb *tcp;
178 int c, pid = 0;
179 struct sigaction sa;
180
181 static char buf[BUFSIZ];
182
183 progname = argv[0];
184 outf = stderr;
185 interactive = 1;
186 qualify("trace=all");
187 qualify("abbrev=all");
188 qualify("verbose=all");
189 qualify("signal=all");
190 set_sortby(DEFAULT_SORTBY);
191 set_personality(DEFAULT_PERSONALITY);
192 while ((c = getopt(argc, argv,
193 "+cdfFhiqrtTvVxa:e:o:O:p:s:S:u:")) != EOF) {
194 switch (c) {
195 case 'c':
196 cflag++;
197 dtime++;
198 break;
199 case 'd':
200 debug++;
201 break;
202 case 'f':
203 followfork++;
204 break;
205 case 'F':
206 followvfork++;
207 break;
208 case 'h':
209 usage(stdout, 0);
210 break;
211 case 'i':
212 iflag++;
213 break;
214 case 'q':
215 qflag++;
216 break;
217 case 'r':
218 rflag++;
219 tflag++;
220 break;
221 case 't':
222 tflag++;
223 break;
224 case 'T':
225 dtime++;
226 break;
227 case 'x':
228 xflag++;
229 break;
230 case 'v':
231 qualify("abbrev=none");
232 break;
233 case 'V':
234 printf("%s\n", version);
235 exit(0);
236 break;
237 case 'a':
238 acolumn = atoi(optarg);
239 break;
240 case 'e':
241 qualify(optarg);
242 break;
243 case 'o':
244 outfname = strdup(optarg);
245 break;
246 case 'O':
247 set_overhead(atoi(optarg));
248 break;
249 case 'p':
250 if ((pid = atoi(optarg)) == 0) {
251 fprintf(stderr, "%s: Invalid process id: %s\n",
252 progname, optarg);
253 break;
254 }
255 if (pid == getpid()) {
Wichert Akkerman54a47671999-10-17 00:57:34 +0000256 fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000257 break;
258 }
259 if ((tcp = alloctcb(pid)) == NULL) {
260 fprintf(stderr, "%s: tcb table full, please recompile strace\n",
261 progname);
262 exit(1);
263 }
264 tcp->flags |= TCB_ATTACHED;
265 pflag_seen++;
266 break;
267 case 's':
268 max_strlen = atoi(optarg);
269 break;
270 case 'S':
271 set_sortby(optarg);
272 break;
273 case 'u':
274 username = strdup(optarg);
275 break;
276 default:
277 usage(stderr, 1);
278 break;
279 }
280 }
281
282 /* See if they want to run as another user. */
283 if (username != NULL) {
284 struct passwd *pent;
285
286 if (getuid() != 0 || geteuid() != 0) {
287 fprintf(stderr,
288 "%s: you must be root to use the -u option\n",
289 progname);
290 exit(1);
291 }
292 if ((pent = getpwnam(username)) == NULL) {
293 fprintf(stderr, "%s: cannot find user `%s'\n",
294 progname, optarg);
295 exit(1);
296 }
297 run_uid = pent->pw_uid;
298 run_gid = pent->pw_gid;
299 }
300 else {
301 run_uid = getuid();
302 run_gid = getgid();
303 }
304
305#ifndef SVR4
306 setreuid(geteuid(), getuid());
307#endif
308
309 /* See if they want to pipe the output. */
310 if (outfname && (outfname[0] == '|' || outfname[0] == '!')) {
311 if ((outf = popen(outfname + 1, "w")) == NULL) {
312 fprintf(stderr, "%s: can't popen '%s': %s\n",
313 progname, outfname + 1, strerror(errno));
314 exit(1);
315 }
316 free(outfname);
317 outfname = NULL;
318 }
319
320 /* Check if they want to redirect the output. */
321 if (outfname) {
Wichert Akkerman54b4f792001-08-03 11:43:35 +0000322 long f;
323
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000324 if ((outf = fopen(outfname, "w")) == NULL) {
325 fprintf(stderr, "%s: can't fopen '%s': %s\n",
326 progname, outfname, strerror(errno));
327 exit(1);
328 }
Wichert Akkerman54b4f792001-08-03 11:43:35 +0000329
330 if ((f=fcntl(fileno(outf), F_GETFD)) < 0 ) {
331 perror("failed to get flags for outputfile");
332 exit(1);
333 }
334
335 if (fcntl(fileno(outf), F_SETFD, f|FD_CLOEXEC) < 0 ) {
336 perror("failed to set flags for outputfile");
337 exit(1);
338 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000339 }
340
341#ifndef SVR4
342 setreuid(geteuid(), getuid());
343#endif
344
345 if (!outfname) {
346 qflag = 1;
347 setvbuf(outf, buf, _IOLBF, BUFSIZ);
348 }
349 else if (optind < argc)
350 interactive = 0;
351 else
352 qflag = 1;
353
354 for (c = 0, tcp = tcbtab; c < MAX_PROCS; c++, tcp++) {
355 /* Reinitialize the output since it may have changed. */
356 tcp->outf = outf;
357 if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
358 continue;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000359#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000360 if (proc_open(tcp, 1) < 0) {
361 fprintf(stderr, "trouble opening proc file\n");
362 droptcb(tcp);
363 continue;
364 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000365#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000366 if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
367 perror("attach: ptrace(PTRACE_ATTACH, ...)");
368 droptcb(tcp);
369 continue;
370 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000371#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000372 if (!qflag)
373 fprintf(stderr,
374 "Process %u attached - interrupt to quit\n",
375 pid);
376 }
377
378 if (optind < argc) {
379 struct stat statbuf;
380 char *filename;
381 char pathname[MAXPATHLEN];
382
383 filename = argv[optind];
384 if (strchr(filename, '/'))
385 strcpy(pathname, filename);
386#ifdef USE_DEBUGGING_EXEC
387 /*
388 * Debuggers customarily check the current directory
389 * first regardless of the path but doing that gives
390 * security geeks a panic attack.
391 */
392 else if (stat(filename, &statbuf) == 0)
393 strcpy(pathname, filename);
394#endif /* USE_DEBUGGING_EXEC */
395 else {
396 char *path;
397 int m, n, len;
398
399 for (path = getenv("PATH"); path && *path; path += m) {
400 if (strchr(path, ':')) {
401 n = strchr(path, ':') - path;
402 m = n + 1;
403 }
404 else
405 m = n = strlen(path);
406 if (n == 0) {
407 getcwd(pathname, MAXPATHLEN);
408 len = strlen(pathname);
409 }
410 else {
411 strncpy(pathname, path, n);
412 len = n;
413 }
414 if (len && pathname[len - 1] != '/')
415 pathname[len++] = '/';
416 strcpy(pathname + len, filename);
417 if (stat(pathname, &statbuf) == 0)
418 break;
419 }
420 }
421 if (stat(pathname, &statbuf) < 0) {
422 fprintf(stderr, "%s: %s: command not found\n",
423 progname, filename);
424 exit(1);
425 }
426 switch (pid = fork()) {
427 case -1:
428 perror("strace: fork");
429 cleanup();
430 exit(1);
431 break;
432 case 0: {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000433#ifdef USE_PROCFS
434 if (outf != stderr) close (fileno (outf));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000435#ifdef MIPS
436 /* Kludge for SGI, see proc_open for details. */
437 sa.sa_handler = foobar;
438 sa.sa_flags = 0;
439 sigemptyset(&sa.sa_mask);
440 sigaction(SIGINT, &sa, NULL);
441#endif /* MIPS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000442#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000443 pause();
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000444#else /* FREEBSD */
445 kill(getpid(), SIGSTOP); /* stop HERE */
446#endif /* FREEBSD */
447#else /* !USE_PROCFS */
Wichert Akkermanbd4125c2000-06-27 17:28:06 +0000448 if (outf!=stderr)
Wichert Akkerman7987cdf2000-07-05 16:05:39 +0000449 close(fileno (outf));
Wichert Akkermanbd4125c2000-06-27 17:28:06 +0000450
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000451 if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
452 perror("strace: ptrace(PTRACE_TRACEME, ...)");
453 return -1;
454 }
455 if (debug)
456 kill(getpid(), SIGSTOP);
457
458 if (username != NULL || geteuid() == 0) {
459 uid_t run_euid = run_uid;
460 gid_t run_egid = run_gid;
461
462 if (statbuf.st_mode & S_ISUID)
463 run_euid = statbuf.st_uid;
464 if (statbuf.st_mode & S_ISGID)
465 run_egid = statbuf.st_gid;
466
467 /*
468 * It is important to set groups before we
469 * lose privileges on setuid.
470 */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +0000471 if (username != NULL) {
472 if (initgroups(username, run_gid) < 0) {
473 perror("initgroups");
474 exit(1);
475 }
476 if (setregid(run_gid, run_egid) < 0) {
477 perror("setregid");
478 exit(1);
479 }
480 if (setreuid(run_uid, run_euid) < 0) {
481 perror("setreuid");
482 exit(1);
483 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000484 }
485 }
486 else
487 setreuid(run_uid, run_uid);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000488#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000489
490 execv(pathname, &argv[optind]);
491 perror("strace: exec");
492 _exit(1);
493 break;
494 }
495 default:
496 if ((tcp = alloctcb(pid)) == NULL) {
497 fprintf(stderr, "tcb table full\n");
498 cleanup();
499 exit(1);
500 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000501#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000502 if (proc_open(tcp, 0) < 0) {
503 fprintf(stderr, "trouble opening proc file\n");
504 cleanup();
505 exit(1);
506 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000507#endif /* USE_PROCFS */
508#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000509 fake_execve(tcp, pathname, &argv[optind], environ);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000510#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000511 break;
512 }
513 }
514 else if (pflag_seen == 0)
515 usage(stderr, 1);
516
517 sigemptyset(&empty_set);
518 sigemptyset(&blocked_set);
519 sa.sa_handler = SIG_IGN;
520 sigemptyset(&sa.sa_mask);
521 sa.sa_flags = 0;
522 sigaction(SIGTTOU, &sa, NULL);
523 sigaction(SIGTTIN, &sa, NULL);
524 if (interactive) {
525 sigaddset(&blocked_set, SIGHUP);
526 sigaddset(&blocked_set, SIGINT);
527 sigaddset(&blocked_set, SIGQUIT);
528 sigaddset(&blocked_set, SIGPIPE);
529 sigaddset(&blocked_set, SIGTERM);
530 sa.sa_handler = interrupt;
531#ifdef SUNOS4
532 /* POSIX signals on sunos4.1 are a little broken. */
533 sa.sa_flags = SA_INTERRUPT;
534#endif /* SUNOS4 */
535 }
536 sigaction(SIGHUP, &sa, NULL);
537 sigaction(SIGINT, &sa, NULL);
538 sigaction(SIGQUIT, &sa, NULL);
539 sigaction(SIGPIPE, &sa, NULL);
540 sigaction(SIGTERM, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000541#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000542 sa.sa_handler = reaper;
543 sigaction(SIGCHLD, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000544#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000545
546 if (trace() < 0)
547 exit(1);
548 cleanup();
549 exit(0);
550}
551
552void
553newoutf(tcp)
554struct tcb *tcp;
555{
556 char name[MAXPATHLEN];
557 FILE *fp;
558
559 if (outfname && followfork > 1) {
560 sprintf(name, "%s.%u", outfname, tcp->pid);
561#ifndef SVR4
562 setreuid(geteuid(), getuid());
563#endif
564 fp = fopen(name, "w");
565#ifndef SVR4
566 setreuid(geteuid(), getuid());
567#endif
568 if (fp == NULL) {
569 perror("fopen");
570 return;
571 }
572 tcp->outf = fp;
573 }
574 return;
575}
576
577struct tcb *
578alloctcb(pid)
579int pid;
580{
581 int i;
582 struct tcb *tcp;
583
584 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
585 if ((tcp->flags & TCB_INUSE) == 0) {
586 tcp->pid = pid;
587 tcp->parent = NULL;
588 tcp->nchildren = 0;
589 tcp->flags = TCB_INUSE | TCB_STARTUP;
590 tcp->outf = outf; /* Initialise to current out file */
591 tcp->stime.tv_sec = 0;
592 tcp->stime.tv_usec = 0;
593 tcp->pfd = -1;
594 nprocs++;
595 return tcp;
596 }
597 }
598 return NULL;
599}
600
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000601#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000602int
603proc_open(tcp, attaching)
604struct tcb *tcp;
605int attaching;
606{
607 char proc[32];
608 long arg;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000609#ifdef SVR4
John Hughes19e49982001-10-19 08:59:12 +0000610 int i;
611 sysset_t syscalls;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000612 sigset_t signals;
613 fltset_t faults;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000614#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000615#ifndef HAVE_POLLABLE_PROCFS
616 static int last_pfd;
617#endif
618
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000619#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000620 /* Open the process pseudo-files in /proc. */
621 sprintf(proc, "/proc/%d/ctl", tcp->pid);
622 if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000623 perror("strace: open(\"/proc/...\", ...)");
624 return -1;
625 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000626 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
627 perror("F_GETFD");
628 return -1;
629 }
630 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
631 perror("F_SETFD");
632 return -1;
633 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000634 sprintf(proc, "/proc/%d/status", tcp->pid);
635 if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
636 perror("strace: open(\"/proc/...\", ...)");
637 return -1;
638 }
639 if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) {
640 perror("F_GETFD");
641 return -1;
642 }
643 if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) {
644 perror("F_SETFD");
645 return -1;
646 }
647 sprintf(proc, "/proc/%d/as", tcp->pid);
648 if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
649 perror("strace: open(\"/proc/...\", ...)");
650 return -1;
651 }
652 if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) {
653 perror("F_GETFD");
654 return -1;
655 }
656 if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) {
657 perror("F_SETFD");
658 return -1;
659 }
660#else
661 /* Open the process pseudo-file in /proc. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000662#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000663 sprintf(proc, "/proc/%d", tcp->pid);
664 if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000665#else /* FREEBSD */
666 sprintf(proc, "/proc/%d/mem", tcp->pid);
667 if ((tcp->pfd = open(proc, O_RDWR)) < 0) {
668#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000669 perror("strace: open(\"/proc/...\", ...)");
670 return -1;
671 }
672 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
673 perror("F_GETFD");
674 return -1;
675 }
676 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
677 perror("F_SETFD");
678 return -1;
679 }
680#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000681#ifdef FREEBSD
682 sprintf(proc, "/proc/%d/regs", tcp->pid);
683 if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) {
684 perror("strace: open(\"/proc/.../regs\", ...)");
685 return -1;
686 }
687 if (cflag) {
688 sprintf(proc, "/proc/%d/status", tcp->pid);
689 if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) {
690 perror("strace: open(\"/proc/.../status\", ...)");
691 return -1;
692 }
693 } else
694 tcp->pfd_status = -1;
695#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000696 rebuild_pollv();
697 if (!attaching) {
698 /*
699 * Wait for the child to pause. Because of a race
700 * condition we have to poll for the event.
701 */
702 for (;;) {
703 if (IOCTL_STATUS (tcp) < 0) {
704 perror("strace: PIOCSTATUS");
705 return -1;
706 }
707 if (tcp->status.PR_FLAGS & PR_ASLEEP)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000708 break;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000709 }
710 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000711#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000712 /* Stop the process so that we own the stop. */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000713 if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000714 perror("strace: PIOCSTOP");
715 return -1;
716 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000717#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000718#ifdef PIOCSET
719 /* Set Run-on-Last-Close. */
720 arg = PR_RLC;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000721 if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000722 perror("PIOCSET PR_RLC");
723 return -1;
724 }
725 /* Set or Reset Inherit-on-Fork. */
726 arg = PR_FORK;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000727 if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000728 perror("PIOC{SET,RESET} PR_FORK");
729 return -1;
730 }
731#else /* !PIOCSET */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000732#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000733 if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
734 perror("PIOCSRLC");
735 return -1;
736 }
737 if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
738 perror("PIOC{S,R}FORK");
739 return -1;
740 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000741#else /* FREEBSD */
742 /* just unset the PF_LINGER flag for the Run-on-Last-Close. */
743 if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) {
744 perror("PIOCGFL");
745 return -1;
746 }
747 arg &= ~PF_LINGER;
748 if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) {
749 perror("PIOCSFL");
750 return -1;
751 }
752#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000753#endif /* !PIOCSET */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000754#ifndef FREEBSD
John Hughes19e49982001-10-19 08:59:12 +0000755 /* Enable all syscall entries we care about. */
756 premptyset(&syscalls);
757 for (i = 1; i < MAX_QUALS; ++i) {
758 if (i > (sizeof syscalls) * CHAR_BIT) break;
759 if (qual_flags [i] & QUAL_TRACE) praddset (&syscalls, i);
760 }
761 praddset (&syscalls, SYS_execve);
762 if (followfork) {
763 praddset (&syscalls, SYS_fork);
764#ifdef SYS_forkall
765 praddset (&syscalls, SYS_forkall);
766#endif
767#ifdef SYS_fork1
768 praddset (&syscalls, SYS_fork1);
769#endif
770#ifdef SYS_rfork1
771 praddset (&syscalls, SYS_rfork1);
772#endif
773#ifdef SYS_rforkall
774 praddset (&syscalls, SYS_rforkall);
775#endif
776 }
777 if (IOCTL(tcp->pfd, PIOCSENTRY, &syscalls) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000778 perror("PIOCSENTRY");
779 return -1;
780 }
John Hughes19e49982001-10-19 08:59:12 +0000781 /* Enable the syscall exits. */
782 if (IOCTL(tcp->pfd, PIOCSEXIT, &syscalls) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000783 perror("PIOSEXIT");
784 return -1;
785 }
John Hughes19e49982001-10-19 08:59:12 +0000786 /* Enable signals we care about. */
787 premptyset(&signals);
788 for (i = 1; i < MAX_QUALS; ++i) {
789 if (i > (sizeof signals) * CHAR_BIT) break;
790 if (qual_flags [i] & QUAL_SIGNAL) praddset (&signals, i);
791 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000792 if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000793 perror("PIOCSTRACE");
794 return -1;
795 }
John Hughes19e49982001-10-19 08:59:12 +0000796 /* Enable faults we care about */
797 premptyset(&faults);
798 for (i = 1; i < MAX_QUALS; ++i) {
799 if (i > (sizeof faults) * CHAR_BIT) break;
800 if (qual_flags [i] & QUAL_FAULT) praddset (&faults, i);
801 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000802 if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000803 perror("PIOCSFAULT");
804 return -1;
805 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000806#else /* FREEBSD */
807 /* set events flags. */
808 arg = S_SIG | S_SCE | S_SCX ;
809 if(ioctl(tcp->pfd, PIOCBIS, arg) < 0) {
810 perror("PIOCBIS");
811 return -1;
812 }
813#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000814 if (!attaching) {
815#ifdef MIPS
816 /*
817 * The SGI PRSABORT doesn't work for pause() so
818 * we send it a caught signal to wake it up.
819 */
820 kill(tcp->pid, SIGINT);
821#else /* !MIPS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000822#ifdef PRSABORT
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000823 /* The child is in a pause(), abort it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000824 arg = PRSABORT;
825 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000826 perror("PIOCRUN");
827 return -1;
828 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000829#endif
830#endif /* !MIPS*/
831#ifdef FREEBSD
832 /* wake up the child if it received the SIGSTOP */
833 kill(tcp->pid, SIGCONT);
834#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000835 for (;;) {
836 /* Wait for the child to do something. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000837 if (IOCTL_WSTOP (tcp) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000838 perror("PIOCWSTOP");
839 return -1;
840 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000841 if (tcp->status.PR_WHY == PR_SYSENTRY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000842 tcp->flags &= ~TCB_INSYSCALL;
843 get_scno(tcp);
844 if (tcp->scno == SYS_execve)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000845 break;
846 }
847 /* Set it running: maybe execve will be next. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000848#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000849 arg = 0;
850 if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000851#else /* FREEBSD */
852 if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) {
853#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000854 perror("PIOCRUN");
855 return -1;
856 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000857#ifdef FREEBSD
858 /* handle the case where we "opened" the child before
859 it did the kill -STOP */
860 if (tcp->status.PR_WHY == PR_SIGNALLED &&
861 tcp->status.PR_WHAT == SIGSTOP)
862 kill(tcp->pid, SIGCONT);
863#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000864 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000865#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000866 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000867#else /* FREEBSD */
868 } else {
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +0000869 if (attaching < 2) {
870 /* We are attaching to an already running process.
871 * Try to figure out the state of the process in syscalls,
872 * to handle the first event well.
873 * This is done by having a look at the "wchan" property of the
874 * process, which tells where it is stopped (if it is). */
875 FILE * status;
876 char wchan[20]; /* should be enough */
877
878 sprintf(proc, "/proc/%d/status", tcp->pid);
879 status = fopen(proc, "r");
880 if (status &&
881 (fscanf(status, "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d"
882 "%*d,%*d %*d,%*d %19s", wchan) == 1) &&
883 strcmp(wchan, "nochan") && strcmp(wchan, "spread") &&
884 strcmp(wchan, "stopevent")) {
885 /* The process is asleep in the middle of a syscall.
886 Fake the syscall entry event */
887 tcp->flags &= ~(TCB_INSYSCALL|TCB_STARTUP);
888 tcp->status.PR_WHY = PR_SYSENTRY;
889 trace_syscall(tcp);
890 }
891 if (status)
892 fclose(status);
893 } /* otherwise it's a fork being followed */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000894 }
895#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000896#ifndef HAVE_POLLABLE_PROCFS
897 if (proc_poll_pipe[0] != -1)
898 proc_poller(tcp->pfd);
899 else if (nprocs > 1) {
900 proc_poll_open();
901 proc_poller(last_pfd);
902 proc_poller(tcp->pfd);
903 }
904 last_pfd = tcp->pfd;
905#endif /* !HAVE_POLLABLE_PROCFS */
906 return 0;
907}
908
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000909#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000910
911static struct tcb *
912pid2tcb(pid)
913int pid;
914{
915 int i;
916 struct tcb *tcp;
917
918 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
919 if (pid && tcp->pid != pid)
920 continue;
921 if (tcp->flags & TCB_INUSE)
922 return tcp;
923 }
924 return NULL;
925}
926
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000927#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000928
929static struct tcb *
930pfd2tcb(pfd)
931int pfd;
932{
933 int i;
934 struct tcb *tcp;
935
936 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
937 if (tcp->pfd != pfd)
938 continue;
939 if (tcp->flags & TCB_INUSE)
940 return tcp;
941 }
942 return NULL;
943}
944
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000945#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000946
947void
948droptcb(tcp)
949struct tcb *tcp;
950{
951 if (tcp->pid == 0)
952 return;
953 nprocs--;
954 tcp->pid = 0;
955 tcp->flags = 0;
956 if (tcp->pfd != -1) {
957 close(tcp->pfd);
958 tcp->pfd = -1;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000959#ifdef FREEBSD
960 if (tcp->pfd_reg != -1) {
961 close(tcp->pfd_reg);
962 tcp->pfd_reg = -1;
963 }
964 if (tcp->pfd_status != -1) {
965 close(tcp->pfd_status);
966 tcp->pfd_status = -1;
967 }
968#endif /* !FREEBSD */
969#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000970 rebuild_pollv();
971#endif
972 }
973 if (tcp->parent != NULL) {
974 tcp->parent->nchildren--;
975 tcp->parent = NULL;
976 }
977#if 0
978 if (tcp->outf != stderr)
979 fclose(tcp->outf);
980#endif
981 tcp->outf = 0;
982}
983
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000984#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000985
986static int
987resume(tcp)
988struct tcb *tcp;
989{
990 if (tcp == NULL)
991 return -1;
992
993 if (!(tcp->flags & TCB_SUSPENDED)) {
994 fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid);
995 return -1;
996 }
997 tcp->flags &= ~TCB_SUSPENDED;
998
999 if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) {
1000 perror("resume: ptrace(PTRACE_SYSCALL, ...)");
1001 return -1;
1002 }
1003
1004 if (!qflag)
1005 fprintf(stderr, "Process %u resumed\n", tcp->pid);
1006 return 0;
1007}
1008
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001009#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001010
1011/* detach traced process; continue with sig */
1012
1013static int
1014detach(tcp, sig)
1015struct tcb *tcp;
1016int sig;
1017{
1018 int error = 0;
1019#ifdef LINUX
1020 int status;
1021#endif
1022
1023 if (tcp->flags & TCB_BPTSET)
1024 sig = SIGKILL;
1025
1026#ifdef LINUX
1027 /*
1028 * Linux wrongly insists the child be stopped
1029 * before detaching. Arghh. We go through hoops
1030 * to make a clean break of things.
1031 */
Wichert Akkermandacfb6e1999-06-03 14:21:07 +00001032#if defined(SPARC)
1033#undef PTRACE_DETACH
1034#define PTRACE_DETACH PTRACE_SUNDETACH
1035#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001036 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
1037 /* On a clear day, you can see forever. */
1038 }
1039 else if (errno != ESRCH) {
1040 /* Shouldn't happen. */
1041 perror("detach: ptrace(PTRACE_DETACH, ...)");
1042 }
1043 else if (kill(tcp->pid, 0) < 0) {
1044 if (errno != ESRCH)
1045 perror("detach: checking sanity");
1046 }
1047 else if (kill(tcp->pid, SIGSTOP) < 0) {
1048 if (errno != ESRCH)
1049 perror("detach: stopping child");
1050 }
1051 else {
1052 for (;;) {
1053 if (waitpid(tcp->pid, &status, 0) < 0) {
1054 if (errno != ECHILD)
1055 perror("detach: waiting");
1056 break;
1057 }
1058 if (!WIFSTOPPED(status)) {
1059 /* Au revoir, mon ami. */
1060 break;
1061 }
1062 if (WSTOPSIG(status) == SIGSTOP) {
1063 if ((error = ptrace(PTRACE_DETACH,
1064 tcp->pid, (char *) 1, sig)) < 0) {
1065 if (errno != ESRCH)
1066 perror("detach: ptrace(PTRACE_DETACH, ...)");
1067 /* I died trying. */
1068 }
1069 break;
1070 }
1071 if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1,
1072 WSTOPSIG(status) == SIGTRAP ?
1073 0 : WSTOPSIG(status))) < 0) {
1074 if (errno != ESRCH)
1075 perror("detach: ptrace(PTRACE_CONT, ...)");
1076 break;
1077 }
1078 }
1079 }
1080#endif /* LINUX */
1081
1082#if defined(SUNOS4)
1083 /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
1084 if (sig && kill(tcp->pid, sig) < 0)
1085 perror("detach: kill");
1086 sig = 0;
1087 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0)
1088 perror("detach: ptrace(PTRACE_DETACH, ...)");
1089#endif /* SUNOS4 */
1090
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001091#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001092 if (waiting_parent(tcp))
1093 error = resume(tcp->parent);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001094#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001095
1096 if (!qflag)
1097 fprintf(stderr, "Process %u detached\n", tcp->pid);
1098
1099 droptcb(tcp);
1100 return error;
1101}
1102
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001103#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001104
1105static void
1106reaper(sig)
1107int sig;
1108{
1109 int pid;
1110 int status;
1111
1112 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
1113#if 0
1114 struct tcb *tcp;
1115
1116 tcp = pid2tcb(pid);
1117 if (tcp)
1118 droptcb(tcp);
1119#endif
1120 }
1121}
1122
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001123#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001124
1125static void
1126cleanup()
1127{
1128 int i;
1129 struct tcb *tcp;
1130
1131 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1132 if (!(tcp->flags & TCB_INUSE))
1133 continue;
1134 if (debug)
1135 fprintf(stderr,
1136 "cleanup: looking at pid %u\n", tcp->pid);
1137 if (tcp_last &&
1138 (!outfname || followfork < 2 || tcp_last == tcp)) {
1139 tprintf(" <unfinished ...>\n");
1140 tcp_last = NULL;
1141 }
1142 if (tcp->flags & TCB_ATTACHED)
1143 detach(tcp, 0);
1144 else {
1145 kill(tcp->pid, SIGCONT);
1146 kill(tcp->pid, SIGTERM);
1147 }
1148 }
1149 if (cflag)
1150 call_summary(outf);
1151}
1152
1153static void
1154interrupt(sig)
1155int sig;
1156{
1157 interrupted = 1;
1158}
1159
1160#ifndef HAVE_STRERROR
1161
1162#ifndef SYS_ERRLIST_DECLARED
1163extern int sys_nerr;
1164extern char *sys_errlist[];
1165#endif /* SYS_ERRLIST_DECLARED */
1166
1167const char *
1168strerror(errno)
1169int errno;
1170{
1171 static char buf[64];
1172
1173 if (errno < 1 || errno >= sys_nerr) {
1174 sprintf(buf, "Unknown error %d", errno);
1175 return buf;
1176 }
1177 return sys_errlist[errno];
1178}
1179
1180#endif /* HAVE_STERRROR */
1181
1182#ifndef HAVE_STRSIGNAL
1183
1184#ifndef SYS_SIGLIST_DECLARED
1185#ifdef HAVE__SYS_SIGLIST
1186 extern char *_sys_siglist[];
1187#else
1188 extern char *sys_siglist[];
1189#endif
1190#endif /* SYS_SIGLIST_DECLARED */
1191
1192const char *
1193strsignal(sig)
1194int sig;
1195{
1196 static char buf[64];
1197
1198 if (sig < 1 || sig >= NSIG) {
1199 sprintf(buf, "Unknown signal %d", sig);
1200 return buf;
1201 }
1202#ifdef HAVE__SYS_SIGLIST
1203 return _sys_siglist[sig];
1204#else
1205 return sys_siglist[sig];
1206#endif
1207}
1208
1209#endif /* HAVE_STRSIGNAL */
1210
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001211#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001212
1213static void
1214rebuild_pollv()
1215{
1216 int i, j;
1217 struct tcb *tcp;
1218
1219 for (i = j = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1220 if (!(tcp->flags & TCB_INUSE))
1221 continue;
1222 pollv[j].fd = tcp->pfd;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001223 pollv[j].events = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001224 j++;
1225 }
1226 if (j != nprocs) {
1227 fprintf(stderr, "strace: proc miscount\n");
1228 exit(1);
1229 }
1230}
1231
1232#ifndef HAVE_POLLABLE_PROCFS
1233
1234static void
1235proc_poll_open()
1236{
1237 int arg;
1238 int i;
1239
1240 if (pipe(proc_poll_pipe) < 0) {
1241 perror("pipe");
1242 exit(1);
1243 }
1244 for (i = 0; i < 2; i++) {
1245 if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) {
1246 perror("F_GETFD");
1247 exit(1);
1248 }
1249 if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) {
1250 perror("F_SETFD");
1251 exit(1);
1252 }
1253 }
1254}
1255
1256static int
1257proc_poll(pollv, nfds, timeout)
1258struct pollfd *pollv;
1259int nfds;
1260int timeout;
1261{
1262 int i;
1263 int n;
1264 struct proc_pollfd pollinfo;
1265
1266 if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
1267 return n;
1268 if (n != sizeof(struct proc_pollfd)) {
1269 fprintf(stderr, "panic: short read: %d\n", n);
1270 exit(1);
1271 }
1272 for (i = 0; i < nprocs; i++) {
1273 if (pollv[i].fd == pollinfo.fd)
1274 pollv[i].revents = pollinfo.revents;
1275 else
1276 pollv[i].revents = 0;
1277 }
1278 poller_pid = pollinfo.pid;
1279 return 1;
1280}
1281
1282static void
1283wakeup_handler(sig)
1284int sig;
1285{
1286}
1287
1288static void
1289proc_poller(pfd)
1290int pfd;
1291{
1292 struct proc_pollfd pollinfo;
1293 struct sigaction sa;
1294 sigset_t blocked_set, empty_set;
1295 int i;
1296 int n;
1297 struct rlimit rl;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001298#ifdef FREEBSD
1299 struct procfs_status pfs;
1300#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001301
1302 switch (fork()) {
1303 case -1:
1304 perror("fork");
1305 _exit(0);
1306 case 0:
1307 break;
1308 default:
1309 return;
1310 }
1311
1312 sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
1313 sa.sa_flags = 0;
1314 sigemptyset(&sa.sa_mask);
1315 sigaction(SIGHUP, &sa, NULL);
1316 sigaction(SIGINT, &sa, NULL);
1317 sigaction(SIGQUIT, &sa, NULL);
1318 sigaction(SIGPIPE, &sa, NULL);
1319 sigaction(SIGTERM, &sa, NULL);
1320 sa.sa_handler = wakeup_handler;
1321 sigaction(SIGUSR1, &sa, NULL);
1322 sigemptyset(&blocked_set);
1323 sigaddset(&blocked_set, SIGUSR1);
1324 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1325 sigemptyset(&empty_set);
1326
1327 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
1328 perror("getrlimit(RLIMIT_NOFILE, ...)");
1329 _exit(0);
1330 }
1331 n = rl.rlim_cur;
1332 for (i = 0; i < n; i++) {
1333 if (i != pfd && i != proc_poll_pipe[1])
1334 close(i);
1335 }
1336
1337 pollinfo.fd = pfd;
1338 pollinfo.pid = getpid();
1339 for (;;) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001340#ifndef FREEBSD
1341 if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
1342#else /* FREEBSD */
1343 if (ioctl(pfd, PIOCWSTOP, &pfs) < 0)
1344#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001345 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001346 switch (errno) {
1347 case EINTR:
1348 continue;
1349 case EBADF:
1350 pollinfo.revents = POLLERR;
1351 break;
1352 case ENOENT:
1353 pollinfo.revents = POLLHUP;
1354 break;
1355 default:
1356 perror("proc_poller: PIOCWSTOP");
1357 }
1358 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1359 _exit(0);
1360 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001361 pollinfo.revents = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001362 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1363 sigsuspend(&empty_set);
1364 }
1365}
1366
1367#endif /* !HAVE_POLLABLE_PROCFS */
1368
1369static int
1370choose_pfd()
1371{
1372 int i, j;
1373 struct tcb *tcp;
1374
1375 static int last;
1376
1377 if (followfork < 2 &&
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001378 last < nprocs && (pollv[last].revents & POLLWANT)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001379 /*
1380 * The previous process is ready to run again. We'll
1381 * let it do so if it is currently in a syscall. This
1382 * heuristic improves the readability of the trace.
1383 */
1384 tcp = pfd2tcb(pollv[last].fd);
1385 if (tcp && (tcp->flags & TCB_INSYSCALL))
1386 return pollv[last].fd;
1387 }
1388
1389 for (i = 0; i < nprocs; i++) {
1390 /* Let competing children run round robin. */
1391 j = (i + last + 1) % nprocs;
1392 if (pollv[j].revents & (POLLHUP | POLLERR)) {
1393 tcp = pfd2tcb(pollv[j].fd);
1394 if (!tcp) {
1395 fprintf(stderr, "strace: lost proc\n");
1396 exit(1);
1397 }
1398 droptcb(tcp);
1399 return -1;
1400 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001401 if (pollv[j].revents & POLLWANT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001402 last = j;
1403 return pollv[j].fd;
1404 }
1405 }
1406 fprintf(stderr, "strace: nothing ready\n");
1407 exit(1);
1408}
1409
1410static int
1411trace()
1412{
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001413#ifdef POLL_HACK
1414 struct tcb *in_syscall;
1415#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001416 struct tcb *tcp;
1417 int pfd;
1418 int what;
1419 int ioctl_result = 0, ioctl_errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001420 long arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001421
1422 for (;;) {
1423 if (interactive)
1424 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1425
1426 if (nprocs == 0)
1427 break;
1428
1429 switch (nprocs) {
1430 case 1:
1431#ifndef HAVE_POLLABLE_PROCFS
1432 if (proc_poll_pipe[0] == -1) {
1433#endif
1434 tcp = pid2tcb(0);
1435 if (!tcp)
1436 continue;
1437 pfd = tcp->pfd;
1438 if (pfd == -1)
1439 continue;
1440 break;
1441#ifndef HAVE_POLLABLE_PROCFS
1442 }
1443 /* fall through ... */
1444#endif /* !HAVE_POLLABLE_PROCFS */
1445 default:
1446#ifdef HAVE_POLLABLE_PROCFS
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001447#ifdef POLL_HACK
1448 /* On some systems (e.g. UnixWare) we get too much ugly
1449 "unfinished..." stuff when multiple proceses are in
1450 syscalls. Here's a nasty hack */
1451
1452 if (in_syscall) {
1453 struct pollfd pv;
1454 tcp = in_syscall;
1455 in_syscall = NULL;
1456 pv.fd = tcp->pfd;
1457 pv.events = POLLWANT;
1458 if ((what = poll (&pv, 1, 1)) < 0) {
1459 if (interrupted)
1460 return 0;
1461 continue;
1462 }
1463 else if (what == 1 && pv.revents & POLLWANT) {
1464 goto FOUND;
1465 }
1466 }
1467#endif
1468
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001469 if (poll(pollv, nprocs, INFTIM) < 0) {
1470 if (interrupted)
1471 return 0;
1472 continue;
1473 }
1474#else /* !HAVE_POLLABLE_PROCFS */
1475 if (proc_poll(pollv, nprocs, INFTIM) < 0) {
1476 if (interrupted)
1477 return 0;
1478 continue;
1479 }
1480#endif /* !HAVE_POLLABLE_PROCFS */
1481 pfd = choose_pfd();
1482 if (pfd == -1)
1483 continue;
1484 break;
1485 }
1486
1487 /* Look up `pfd' in our table. */
1488 if ((tcp = pfd2tcb(pfd)) == NULL) {
1489 fprintf(stderr, "unknown pfd: %u\n", pfd);
1490 exit(1);
1491 }
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001492 FOUND:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001493 /* Get the status of the process. */
1494 if (!interrupted) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001495#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001496 ioctl_result = IOCTL_WSTOP (tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001497#else /* FREEBSD */
1498 /* Thanks to some scheduling mystery, the first poller
1499 sometimes waits for the already processed end of fork
1500 event. Doing a non blocking poll here solves the problem. */
1501 if (proc_poll_pipe[0] != -1)
1502 ioctl_result = IOCTL_STATUS (tcp);
1503 else
1504 ioctl_result = IOCTL_WSTOP (tcp);
1505#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001506 ioctl_errno = errno;
1507#ifndef HAVE_POLLABLE_PROCFS
1508 if (proc_poll_pipe[0] != -1) {
1509 if (ioctl_result < 0)
1510 kill(poller_pid, SIGKILL);
1511 else
1512 kill(poller_pid, SIGUSR1);
1513 }
1514#endif /* !HAVE_POLLABLE_PROCFS */
1515 }
1516 if (interrupted)
1517 return 0;
1518
1519 if (interactive)
1520 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1521
1522 if (ioctl_result < 0) {
1523 /* Find out what happened if it failed. */
1524 switch (ioctl_errno) {
1525 case EINTR:
1526 case EBADF:
1527 continue;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001528#ifdef FREEBSD
1529 case ENOTTY:
1530#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001531 case ENOENT:
1532 droptcb(tcp);
1533 continue;
1534 default:
1535 perror("PIOCWSTOP");
1536 exit(1);
1537 }
1538 }
1539
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +00001540#ifdef FREEBSD
1541 if ((tcp->flags & TCB_STARTUP) && (tcp->status.PR_WHY == PR_SYSEXIT)) {
1542 /* discard first event for a syscall we never entered */
1543 IOCTL (tcp->pfd, PIOCRUN, 0);
1544 continue;
1545 }
1546#endif
1547
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001548 /* clear the just started flag */
1549 tcp->flags &= ~TCB_STARTUP;
1550
1551 /* set current output file */
1552 outf = tcp->outf;
1553
1554 if (cflag) {
1555 struct timeval stime;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001556#ifdef FREEBSD
1557 char buf[1024];
1558 int len;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001559
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001560 if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) {
1561 buf[len] = '\0';
1562 sscanf(buf,
1563 "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld",
1564 &stime.tv_sec, &stime.tv_usec);
1565 } else
1566 stime.tv_sec = stime.tv_usec = 0;
1567#else /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001568 stime.tv_sec = tcp->status.pr_stime.tv_sec;
1569 stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001570#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001571 tv_sub(&tcp->dtime, &stime, &tcp->stime);
1572 tcp->stime = stime;
1573 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001574 what = tcp->status.PR_WHAT;
1575 switch (tcp->status.PR_WHY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001576#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001577 case PR_REQUESTED:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001578 if (tcp->status.PR_FLAGS & PR_ASLEEP) {
1579 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001580 if (trace_syscall(tcp) < 0) {
1581 fprintf(stderr, "syscall trouble\n");
1582 exit(1);
1583 }
1584 }
1585 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001586#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001587 case PR_SYSENTRY:
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001588#ifdef POLL_HACK
1589 in_syscall = tcp;
1590#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001591 case PR_SYSEXIT:
1592 if (trace_syscall(tcp) < 0) {
1593 fprintf(stderr, "syscall trouble\n");
1594 exit(1);
1595 }
1596 break;
1597 case PR_SIGNALLED:
1598 if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) {
1599 printleader(tcp);
1600 tprintf("--- %s (%s) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001601 signame(what), strsignal(what));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001602 printtrailer(tcp);
John Hughes58265892001-10-18 15:13:53 +00001603#ifdef PR_INFO
1604 if (tcp->status.PR_INFO.si_signo == what) {
1605 printleader(tcp);
1606 tprintf(" siginfo=");
1607 printsiginfo(&tcp->status.PR_INFO, 1);
1608 printtrailer(tcp);
1609 }
1610#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001611 }
1612 break;
1613 case PR_FAULTED:
1614 if (!cflag && (qual_flags[what] & QUAL_FAULT)) {
1615 printleader(tcp);
1616 tprintf("=== FAULT %d ===", what);
1617 printtrailer(tcp);
1618 }
1619 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001620#ifdef FREEBSD
1621 case 0: /* handle case we polled for nothing */
1622 continue;
1623#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001624 default:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001625 fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001626 exit(1);
1627 break;
1628 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001629 arg = 0;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001630#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001631 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001632#else
1633 if (IOCTL (tcp->pfd, PIOCRUN, 0) < 0) {
1634#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001635 perror("PIOCRUN");
1636 exit(1);
1637 }
1638 }
1639 return 0;
1640}
1641
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001642#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001643
1644static int
1645trace()
1646{
1647 int pid;
1648 int wait_errno;
1649 int status;
1650 struct tcb *tcp;
1651#ifdef LINUX
1652 struct rusage ru;
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001653#ifdef __WALL
1654 static int wait4_options = __WALL;
1655#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001656#endif /* LINUX */
1657
1658 while (nprocs != 0) {
1659 if (interactive)
1660 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1661#ifdef LINUX
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001662#ifdef __WALL
1663 pid = wait4(-1, &status, wait4_options, cflag ? &ru : NULL);
1664 if ((wait4_options & __WALL) && errno == EINVAL) {
1665 /* this kernel does not support __WALL */
1666 wait4_options &= ~__WALL;
1667 errno = 0;
1668 pid = wait4(-1, &status, wait4_options,
1669 cflag ? &ru : NULL);
1670 }
1671 if (!(wait4_options & __WALL) && errno == ECHILD) {
1672 /* most likely a "cloned" process */
1673 pid = wait4(-1, &status, __WCLONE,
1674 cflag ? &ru : NULL);
1675 if (pid == -1) {
1676 fprintf(stderr, "strace: clone wait4 "
1677 "failed: %s\n", strerror(errno));
1678 }
1679 }
1680#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001681 pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001682#endif /* __WALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001683#endif /* LINUX */
1684#ifdef SUNOS4
1685 pid = wait(&status);
1686#endif /* SUNOS4 */
1687 wait_errno = errno;
1688 if (interactive)
1689 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1690
1691 if (interrupted)
1692 return 0;
1693
1694 if (pid == -1) {
1695 switch (wait_errno) {
1696 case EINTR:
1697 continue;
1698 case ECHILD:
1699 /*
1700 * We would like to verify this case
1701 * but sometimes a race in Solbourne's
1702 * version of SunOS sometimes reports
1703 * ECHILD before sending us SIGCHILD.
1704 */
1705#if 0
1706 if (nprocs == 0)
1707 return 0;
1708 fprintf(stderr, "strace: proc miscount\n");
1709 exit(1);
1710#endif
1711 return 0;
1712 default:
1713 errno = wait_errno;
1714 perror("strace: wait");
1715 return -1;
1716 }
1717 }
1718 if (debug)
1719 fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
1720
1721 /* Look up `pid' in our table. */
1722 if ((tcp = pid2tcb(pid)) == NULL) {
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001723#if 0 /* XXX davidm */ /* WTA: disabled again */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001724 struct tcb *tcpchild;
1725
1726 if ((tcpchild = alloctcb(pid)) == NULL) {
1727 fprintf(stderr, " [tcb table full]\n");
1728 kill(pid, SIGKILL); /* XXX */
1729 return 0;
1730 }
1731 tcpchild->flags |= TCB_ATTACHED;
1732 newoutf(tcpchild);
1733 tcp->nchildren++;
1734 if (!qflag)
1735 fprintf(stderr, "Process %d attached\n", pid);
1736#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001737 fprintf(stderr, "unknown pid: %u\n", pid);
1738 if (WIFSTOPPED(status))
1739 ptrace(PTRACE_CONT, pid, (char *) 1, 0);
1740 exit(1);
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001741#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001742 }
1743 /* set current output file */
1744 outf = tcp->outf;
1745 if (cflag) {
1746#ifdef LINUX
1747 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
1748 tcp->stime = ru.ru_stime;
1749#endif /* !LINUX */
1750 }
1751
1752 if (tcp->flags & TCB_SUSPENDED) {
1753 /*
1754 * Apparently, doing any ptrace() call on a stopped
1755 * process, provokes the kernel to report the process
1756 * status again on a subsequent wait(), even if the
1757 * process has not been actually restarted.
1758 * Since we have inspected the arguments of suspended
1759 * processes we end up here testing for this case.
1760 */
1761 continue;
1762 }
1763 if (WIFSIGNALED(status)) {
1764 if (!cflag
1765 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
1766 printleader(tcp);
1767 tprintf("+++ killed by %s +++",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001768 signame(WTERMSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001769 printtrailer(tcp);
1770 }
1771 droptcb(tcp);
1772 continue;
1773 }
1774 if (WIFEXITED(status)) {
1775 if (debug)
1776 fprintf(stderr, "pid %u exited\n", pid);
1777 if (tcp->flags & TCB_ATTACHED)
1778 fprintf(stderr,
1779 "PANIC: attached pid %u exited\n",
1780 pid);
1781 droptcb(tcp);
1782 continue;
1783 }
1784 if (!WIFSTOPPED(status)) {
1785 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
1786 droptcb(tcp);
1787 continue;
1788 }
1789 if (debug)
1790 fprintf(stderr, "pid %u stopped, [%s]\n",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001791 pid, signame(WSTOPSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001792
1793 if (tcp->flags & TCB_STARTUP) {
1794 /*
1795 * This flag is there to keep us in sync.
1796 * Next time this process stops it should
1797 * really be entering a system call.
1798 */
1799 tcp->flags &= ~TCB_STARTUP;
1800 if (tcp->flags & TCB_ATTACHED) {
1801 /*
1802 * Interestingly, the process may stop
1803 * with STOPSIG equal to some other signal
1804 * than SIGSTOP if we happend to attach
1805 * just before the process takes a signal.
1806 */
1807 if (!WIFSTOPPED(status)) {
1808 fprintf(stderr,
1809 "pid %u not stopped\n", pid);
1810 detach(tcp, WSTOPSIG(status));
1811 continue;
1812 }
1813 }
1814 else {
1815#ifdef SUNOS4
1816 /* A child of us stopped at exec */
1817 if (WSTOPSIG(status) == SIGTRAP && followvfork)
1818 fixvfork(tcp);
1819#endif /* SUNOS4 */
1820 }
1821 if (tcp->flags & TCB_BPTSET) {
1822 if (clearbpt(tcp) < 0) /* Pretty fatal */ {
1823 droptcb(tcp);
1824 cleanup();
1825 return -1;
1826 }
1827 }
1828 goto tracing;
1829 }
1830
1831 if (WSTOPSIG(status) != SIGTRAP) {
1832 if (WSTOPSIG(status) == SIGSTOP &&
1833 (tcp->flags & TCB_SIGTRAPPED)) {
1834 /*
1835 * Trapped attempt to block SIGTRAP
1836 * Hope we are back in control now.
1837 */
1838 tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
1839 if (ptrace(PTRACE_SYSCALL,
1840 pid, (char *) 1, 0) < 0) {
1841 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1842 cleanup();
1843 return -1;
1844 }
1845 continue;
1846 }
1847 if (!cflag
1848 && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001849 unsigned long addr = 0, pc = 0;
1850#ifdef PT_GETSIGINFO
1851# define PSR_RI 41
1852 struct siginfo si;
1853 unsigned long psr;
1854
1855 upeek(pid, PT_CR_IPSR, &psr);
1856 upeek(pid, PT_CR_IIP, &pc);
1857
1858 pc += (psr >> PSR_RI) & 0x3;
1859 ptrace(PT_GETSIGINFO, pid, 0, (long) &si);
1860 addr = (unsigned long) si.si_addr;
1861#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001862 printleader(tcp);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001863 tprintf("--- %s (%s) @ %lx (%lx) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001864 signame(WSTOPSIG(status)),
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001865 strsignal(WSTOPSIG(status)), pc, addr);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001866 printtrailer(tcp);
1867 }
1868 if ((tcp->flags & TCB_ATTACHED) &&
1869 !sigishandled(tcp, WSTOPSIG(status))) {
1870 detach(tcp, WSTOPSIG(status));
1871 continue;
1872 }
1873 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1,
1874 WSTOPSIG(status)) < 0) {
1875 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1876 cleanup();
1877 return -1;
1878 }
1879 tcp->flags &= ~TCB_SUSPENDED;
1880 continue;
1881 }
1882 if (trace_syscall(tcp) < 0) {
1883 if (tcp->flags & TCB_ATTACHED)
1884 detach(tcp, 0);
1885 else {
1886 ptrace(PTRACE_KILL,
1887 tcp->pid, (char *) 1, SIGTERM);
1888 droptcb(tcp);
1889 }
1890 continue;
1891 }
1892 if (tcp->flags & TCB_EXITING) {
1893 if (tcp->flags & TCB_ATTACHED)
1894 detach(tcp, 0);
1895 else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) {
1896 perror("strace: ptrace(PTRACE_CONT, ...)");
1897 cleanup();
1898 return -1;
1899 }
1900 continue;
1901 }
1902 if (tcp->flags & TCB_SUSPENDED) {
1903 if (!qflag)
1904 fprintf(stderr, "Process %u suspended\n", pid);
1905 continue;
1906 }
1907 tracing:
1908 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) {
1909 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1910 cleanup();
1911 return -1;
1912 }
1913 }
1914 return 0;
1915}
1916
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001917#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001918
1919static int curcol;
1920
1921#ifdef __STDC__
1922#include <stdarg.h>
1923#define VA_START(a, b) va_start(a, b)
1924#else
1925#include <varargs.h>
1926#define VA_START(a, b) va_start(a)
1927#endif
1928
1929void
1930#ifdef __STDC__
1931tprintf(const char *fmt, ...)
1932#else
1933tprintf(fmt, va_alist)
1934char *fmt;
1935va_dcl
1936#endif
1937{
1938 va_list args;
1939
1940 VA_START(args, fmt);
1941 if (outf)
1942 curcol += vfprintf(outf, fmt, args);
1943 va_end(args);
1944 return;
1945}
1946
1947void
1948printleader(tcp)
1949struct tcb *tcp;
1950{
1951 if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) {
1952 tcp_last->flags |= TCB_REPRINT;
1953 tprintf(" <unfinished ...>\n");
1954 }
1955 curcol = 0;
1956 if ((followfork == 1 || pflag_seen > 1) && outfname)
1957 tprintf("%-5d ", tcp->pid);
1958 else if (nprocs > 1 && !outfname)
1959 tprintf("[pid %5u] ", tcp->pid);
1960 if (tflag) {
1961 char str[sizeof("HH:MM:SS")];
1962 struct timeval tv, dtv;
1963 static struct timeval otv;
1964
1965 gettimeofday(&tv, NULL);
1966 if (rflag) {
1967 if (otv.tv_sec == 0)
1968 otv = tv;
1969 tv_sub(&dtv, &tv, &otv);
1970 tprintf("%6ld.%06ld ",
1971 (long) dtv.tv_sec, (long) dtv.tv_usec);
1972 otv = tv;
1973 }
1974 else if (tflag > 2) {
1975 tprintf("%ld.%06ld ",
1976 (long) tv.tv_sec, (long) tv.tv_usec);
1977 }
1978 else {
1979 time_t local = tv.tv_sec;
1980 strftime(str, sizeof(str), "%T", localtime(&local));
1981 if (tflag > 1)
1982 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
1983 else
1984 tprintf("%s ", str);
1985 }
1986 }
1987 if (iflag)
1988 printcall(tcp);
1989}
1990
1991void
1992tabto(col)
1993int col;
1994{
1995 if (curcol < col)
1996 tprintf("%*s", col - curcol, "");
1997}
1998
1999void
2000printtrailer(tcp)
2001struct tcb *tcp;
2002{
2003 tprintf("\n");
2004 tcp_last = NULL;
2005}
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002006
Wichert Akkermanea78f0f1999-11-29 15:34:02 +00002007#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002008
2009int mp_ioctl (int fd, int cmd, void *arg, int size) {
2010
2011 struct iovec iov[2];
2012 int n = 1;
2013
2014 iov[0].iov_base = &cmd;
2015 iov[0].iov_len = sizeof cmd;
2016 if (arg) {
2017 ++n;
2018 iov[1].iov_base = arg;
2019 iov[1].iov_len = size;
2020 }
2021
2022 return writev (fd, iov, n);
2023}
2024
2025#endif