blob: 15aea5aae762df3cf5a5b34900d8491d59228a62 [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
Michal Ludvig17f8fb32002-11-06 13:17:21 +000070/* Sometimes we want to print only succeeding syscalls. */
71int not_failing_only = 0;
72
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000073char *username = NULL;
74uid_t run_uid;
75gid_t run_gid;
76
77int acolumn = DEFAULT_ACOLUMN;
78int max_strlen = DEFAULT_STRLEN;
79char *outfname = NULL;
80FILE *outf;
81struct tcb tcbtab[MAX_PROCS];
82int nprocs;
83char *progname;
84extern char version[];
85extern char **environ;
86
87static struct tcb *pid2tcb P((int pid));
88static int trace P((void));
89static void cleanup P((void));
90static void interrupt P((int sig));
91static sigset_t empty_set, blocked_set;
92
93#ifdef HAVE_SIG_ATOMIC_T
94static volatile sig_atomic_t interrupted;
95#else /* !HAVE_SIG_ATOMIC_T */
96#ifdef __STDC__
97static volatile int interrupted;
98#else /* !__STDC__ */
99static int interrupted;
100#endif /* !__STDC__ */
101#endif /* !HAVE_SIG_ATOMIC_T */
102
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000103#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000104
105static struct tcb *pfd2tcb P((int pfd));
106static void reaper P((int sig));
107static void rebuild_pollv P((void));
Wichert Akkermane68d61c1999-06-28 13:17:16 +0000108struct pollfd pollv[MAX_PROCS];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000109
110#ifndef HAVE_POLLABLE_PROCFS
111
112static void proc_poll_open P((void));
113static void proc_poller P((int pfd));
114
115struct proc_pollfd {
116 int fd;
117 int revents;
118 int pid;
119};
120
121static int poller_pid;
122static int proc_poll_pipe[2] = { -1, -1 };
123
124#endif /* !HAVE_POLLABLE_PROCFS */
125
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000126#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000127#define POLLWANT POLLWRNORM
128#else
129#define POLLWANT POLLPRI
130#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000131#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000132
133static void
134usage(ofp, exitval)
135FILE *ofp;
136int exitval;
137{
138 fprintf(ofp, "\
139usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\
140 [-p pid] ... [-s strsize] [-u username] [command [arg ...]]\n\
141 or: strace -c [-e expr] ... [-O overhead] [-S sortby] [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\
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000160-z -- print only succeeding syscalls\n\
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000161", DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
162 exit(exitval);
163}
164
165#ifdef SVR4
166#ifdef MIPS
167void
168foobar()
169{
170}
171#endif /* MIPS */
172#endif /* SVR4 */
173
174int
175main(argc, argv)
176int argc;
177char *argv[];
178{
179 extern int optind;
180 extern char *optarg;
181 struct tcb *tcp;
182 int c, pid = 0;
183 struct sigaction sa;
184
185 static char buf[BUFSIZ];
186
187 progname = argv[0];
188 outf = stderr;
189 interactive = 1;
190 qualify("trace=all");
191 qualify("abbrev=all");
192 qualify("verbose=all");
193 qualify("signal=all");
194 set_sortby(DEFAULT_SORTBY);
195 set_personality(DEFAULT_PERSONALITY);
196 while ((c = getopt(argc, argv,
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000197 "+cdfFhiqrtTvVxza:e:o:O:p:s:S:u:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000198 switch (c) {
199 case 'c':
200 cflag++;
201 dtime++;
202 break;
203 case 'd':
204 debug++;
205 break;
206 case 'f':
207 followfork++;
208 break;
209 case 'F':
210 followvfork++;
211 break;
212 case 'h':
213 usage(stdout, 0);
214 break;
215 case 'i':
216 iflag++;
217 break;
218 case 'q':
219 qflag++;
220 break;
221 case 'r':
222 rflag++;
223 tflag++;
224 break;
225 case 't':
226 tflag++;
227 break;
228 case 'T':
229 dtime++;
230 break;
231 case 'x':
232 xflag++;
233 break;
234 case 'v':
235 qualify("abbrev=none");
236 break;
237 case 'V':
238 printf("%s\n", version);
239 exit(0);
240 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000241 case 'z':
242 not_failing_only = 1;
243 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000244 case 'a':
245 acolumn = atoi(optarg);
246 break;
247 case 'e':
248 qualify(optarg);
249 break;
250 case 'o':
251 outfname = strdup(optarg);
252 break;
253 case 'O':
254 set_overhead(atoi(optarg));
255 break;
256 case 'p':
257 if ((pid = atoi(optarg)) == 0) {
258 fprintf(stderr, "%s: Invalid process id: %s\n",
259 progname, optarg);
260 break;
261 }
262 if (pid == getpid()) {
Wichert Akkerman54a47671999-10-17 00:57:34 +0000263 fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000264 break;
265 }
266 if ((tcp = alloctcb(pid)) == NULL) {
267 fprintf(stderr, "%s: tcb table full, please recompile strace\n",
268 progname);
269 exit(1);
270 }
271 tcp->flags |= TCB_ATTACHED;
272 pflag_seen++;
273 break;
274 case 's':
275 max_strlen = atoi(optarg);
276 break;
277 case 'S':
278 set_sortby(optarg);
279 break;
280 case 'u':
281 username = strdup(optarg);
282 break;
283 default:
284 usage(stderr, 1);
285 break;
286 }
287 }
288
289 /* See if they want to run as another user. */
290 if (username != NULL) {
291 struct passwd *pent;
292
293 if (getuid() != 0 || geteuid() != 0) {
294 fprintf(stderr,
295 "%s: you must be root to use the -u option\n",
296 progname);
297 exit(1);
298 }
299 if ((pent = getpwnam(username)) == NULL) {
300 fprintf(stderr, "%s: cannot find user `%s'\n",
301 progname, optarg);
302 exit(1);
303 }
304 run_uid = pent->pw_uid;
305 run_gid = pent->pw_gid;
306 }
307 else {
308 run_uid = getuid();
309 run_gid = getgid();
310 }
311
312#ifndef SVR4
313 setreuid(geteuid(), getuid());
314#endif
315
316 /* See if they want to pipe the output. */
317 if (outfname && (outfname[0] == '|' || outfname[0] == '!')) {
318 if ((outf = popen(outfname + 1, "w")) == NULL) {
319 fprintf(stderr, "%s: can't popen '%s': %s\n",
320 progname, outfname + 1, strerror(errno));
321 exit(1);
322 }
323 free(outfname);
324 outfname = NULL;
325 }
326
327 /* Check if they want to redirect the output. */
328 if (outfname) {
Wichert Akkerman54b4f792001-08-03 11:43:35 +0000329 long f;
330
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000331 if ((outf = fopen(outfname, "w")) == NULL) {
332 fprintf(stderr, "%s: can't fopen '%s': %s\n",
333 progname, outfname, strerror(errno));
334 exit(1);
335 }
Wichert Akkerman54b4f792001-08-03 11:43:35 +0000336
337 if ((f=fcntl(fileno(outf), F_GETFD)) < 0 ) {
338 perror("failed to get flags for outputfile");
339 exit(1);
340 }
341
342 if (fcntl(fileno(outf), F_SETFD, f|FD_CLOEXEC) < 0 ) {
343 perror("failed to set flags for outputfile");
344 exit(1);
345 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000346 }
347
348#ifndef SVR4
349 setreuid(geteuid(), getuid());
350#endif
351
352 if (!outfname) {
353 qflag = 1;
354 setvbuf(outf, buf, _IOLBF, BUFSIZ);
355 }
356 else if (optind < argc)
357 interactive = 0;
358 else
359 qflag = 1;
360
361 for (c = 0, tcp = tcbtab; c < MAX_PROCS; c++, tcp++) {
362 /* Reinitialize the output since it may have changed. */
363 tcp->outf = outf;
364 if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
365 continue;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000366#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000367 if (proc_open(tcp, 1) < 0) {
368 fprintf(stderr, "trouble opening proc file\n");
369 droptcb(tcp);
370 continue;
371 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000372#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000373 if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
374 perror("attach: ptrace(PTRACE_ATTACH, ...)");
375 droptcb(tcp);
376 continue;
377 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000378#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000379 if (!qflag)
380 fprintf(stderr,
381 "Process %u attached - interrupt to quit\n",
382 pid);
383 }
384
385 if (optind < argc) {
386 struct stat statbuf;
387 char *filename;
388 char pathname[MAXPATHLEN];
389
390 filename = argv[optind];
391 if (strchr(filename, '/'))
392 strcpy(pathname, filename);
393#ifdef USE_DEBUGGING_EXEC
394 /*
395 * Debuggers customarily check the current directory
396 * first regardless of the path but doing that gives
397 * security geeks a panic attack.
398 */
399 else if (stat(filename, &statbuf) == 0)
400 strcpy(pathname, filename);
401#endif /* USE_DEBUGGING_EXEC */
402 else {
403 char *path;
404 int m, n, len;
405
406 for (path = getenv("PATH"); path && *path; path += m) {
407 if (strchr(path, ':')) {
408 n = strchr(path, ':') - path;
409 m = n + 1;
410 }
411 else
412 m = n = strlen(path);
413 if (n == 0) {
414 getcwd(pathname, MAXPATHLEN);
415 len = strlen(pathname);
416 }
417 else {
418 strncpy(pathname, path, n);
419 len = n;
420 }
421 if (len && pathname[len - 1] != '/')
422 pathname[len++] = '/';
423 strcpy(pathname + len, filename);
424 if (stat(pathname, &statbuf) == 0)
425 break;
426 }
427 }
428 if (stat(pathname, &statbuf) < 0) {
429 fprintf(stderr, "%s: %s: command not found\n",
430 progname, filename);
431 exit(1);
432 }
433 switch (pid = fork()) {
434 case -1:
435 perror("strace: fork");
436 cleanup();
437 exit(1);
438 break;
439 case 0: {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000440#ifdef USE_PROCFS
441 if (outf != stderr) close (fileno (outf));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000442#ifdef MIPS
443 /* Kludge for SGI, see proc_open for details. */
444 sa.sa_handler = foobar;
445 sa.sa_flags = 0;
446 sigemptyset(&sa.sa_mask);
447 sigaction(SIGINT, &sa, NULL);
448#endif /* MIPS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000449#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000450 pause();
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000451#else /* FREEBSD */
452 kill(getpid(), SIGSTOP); /* stop HERE */
453#endif /* FREEBSD */
454#else /* !USE_PROCFS */
Wichert Akkermanbd4125c2000-06-27 17:28:06 +0000455 if (outf!=stderr)
Wichert Akkerman7987cdf2000-07-05 16:05:39 +0000456 close(fileno (outf));
Wichert Akkermanbd4125c2000-06-27 17:28:06 +0000457
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000458 if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
459 perror("strace: ptrace(PTRACE_TRACEME, ...)");
460 return -1;
461 }
462 if (debug)
463 kill(getpid(), SIGSTOP);
464
465 if (username != NULL || geteuid() == 0) {
466 uid_t run_euid = run_uid;
467 gid_t run_egid = run_gid;
468
469 if (statbuf.st_mode & S_ISUID)
470 run_euid = statbuf.st_uid;
471 if (statbuf.st_mode & S_ISGID)
472 run_egid = statbuf.st_gid;
473
474 /*
475 * It is important to set groups before we
476 * lose privileges on setuid.
477 */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +0000478 if (username != NULL) {
479 if (initgroups(username, run_gid) < 0) {
480 perror("initgroups");
481 exit(1);
482 }
483 if (setregid(run_gid, run_egid) < 0) {
484 perror("setregid");
485 exit(1);
486 }
487 if (setreuid(run_uid, run_euid) < 0) {
488 perror("setreuid");
489 exit(1);
490 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000491 }
492 }
493 else
494 setreuid(run_uid, run_uid);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000495#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000496
497 execv(pathname, &argv[optind]);
498 perror("strace: exec");
499 _exit(1);
500 break;
501 }
502 default:
503 if ((tcp = alloctcb(pid)) == NULL) {
504 fprintf(stderr, "tcb table full\n");
505 cleanup();
506 exit(1);
507 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000508#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000509 if (proc_open(tcp, 0) < 0) {
510 fprintf(stderr, "trouble opening proc file\n");
511 cleanup();
512 exit(1);
513 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000514#endif /* USE_PROCFS */
515#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000516 fake_execve(tcp, pathname, &argv[optind], environ);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000517#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000518 break;
519 }
520 }
521 else if (pflag_seen == 0)
522 usage(stderr, 1);
523
524 sigemptyset(&empty_set);
525 sigemptyset(&blocked_set);
526 sa.sa_handler = SIG_IGN;
527 sigemptyset(&sa.sa_mask);
528 sa.sa_flags = 0;
529 sigaction(SIGTTOU, &sa, NULL);
530 sigaction(SIGTTIN, &sa, NULL);
531 if (interactive) {
532 sigaddset(&blocked_set, SIGHUP);
533 sigaddset(&blocked_set, SIGINT);
534 sigaddset(&blocked_set, SIGQUIT);
535 sigaddset(&blocked_set, SIGPIPE);
536 sigaddset(&blocked_set, SIGTERM);
537 sa.sa_handler = interrupt;
538#ifdef SUNOS4
539 /* POSIX signals on sunos4.1 are a little broken. */
540 sa.sa_flags = SA_INTERRUPT;
541#endif /* SUNOS4 */
542 }
543 sigaction(SIGHUP, &sa, NULL);
544 sigaction(SIGINT, &sa, NULL);
545 sigaction(SIGQUIT, &sa, NULL);
546 sigaction(SIGPIPE, &sa, NULL);
547 sigaction(SIGTERM, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000548#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000549 sa.sa_handler = reaper;
550 sigaction(SIGCHLD, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000551#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000552
553 if (trace() < 0)
554 exit(1);
555 cleanup();
556 exit(0);
557}
558
559void
560newoutf(tcp)
561struct tcb *tcp;
562{
563 char name[MAXPATHLEN];
564 FILE *fp;
565
566 if (outfname && followfork > 1) {
567 sprintf(name, "%s.%u", outfname, tcp->pid);
568#ifndef SVR4
569 setreuid(geteuid(), getuid());
570#endif
571 fp = fopen(name, "w");
572#ifndef SVR4
573 setreuid(geteuid(), getuid());
574#endif
575 if (fp == NULL) {
576 perror("fopen");
577 return;
578 }
579 tcp->outf = fp;
580 }
581 return;
582}
583
584struct tcb *
585alloctcb(pid)
586int pid;
587{
588 int i;
589 struct tcb *tcp;
590
591 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
592 if ((tcp->flags & TCB_INUSE) == 0) {
593 tcp->pid = pid;
594 tcp->parent = NULL;
595 tcp->nchildren = 0;
596 tcp->flags = TCB_INUSE | TCB_STARTUP;
597 tcp->outf = outf; /* Initialise to current out file */
598 tcp->stime.tv_sec = 0;
599 tcp->stime.tv_usec = 0;
600 tcp->pfd = -1;
601 nprocs++;
602 return tcp;
603 }
604 }
605 return NULL;
606}
607
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000608#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000609int
610proc_open(tcp, attaching)
611struct tcb *tcp;
612int attaching;
613{
614 char proc[32];
615 long arg;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000616#ifdef SVR4
John Hughes19e49982001-10-19 08:59:12 +0000617 int i;
618 sysset_t syscalls;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000619 sigset_t signals;
620 fltset_t faults;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000621#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000622#ifndef HAVE_POLLABLE_PROCFS
623 static int last_pfd;
624#endif
625
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000626#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000627 /* Open the process pseudo-files in /proc. */
628 sprintf(proc, "/proc/%d/ctl", tcp->pid);
629 if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000630 perror("strace: open(\"/proc/...\", ...)");
631 return -1;
632 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000633 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
634 perror("F_GETFD");
635 return -1;
636 }
637 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
638 perror("F_SETFD");
639 return -1;
640 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000641 sprintf(proc, "/proc/%d/status", tcp->pid);
642 if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
643 perror("strace: open(\"/proc/...\", ...)");
644 return -1;
645 }
646 if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) {
647 perror("F_GETFD");
648 return -1;
649 }
650 if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) {
651 perror("F_SETFD");
652 return -1;
653 }
654 sprintf(proc, "/proc/%d/as", tcp->pid);
655 if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
656 perror("strace: open(\"/proc/...\", ...)");
657 return -1;
658 }
659 if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) {
660 perror("F_GETFD");
661 return -1;
662 }
663 if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) {
664 perror("F_SETFD");
665 return -1;
666 }
667#else
668 /* Open the process pseudo-file in /proc. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000669#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000670 sprintf(proc, "/proc/%d", tcp->pid);
671 if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000672#else /* FREEBSD */
673 sprintf(proc, "/proc/%d/mem", tcp->pid);
674 if ((tcp->pfd = open(proc, O_RDWR)) < 0) {
675#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000676 perror("strace: open(\"/proc/...\", ...)");
677 return -1;
678 }
679 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
680 perror("F_GETFD");
681 return -1;
682 }
683 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
684 perror("F_SETFD");
685 return -1;
686 }
687#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000688#ifdef FREEBSD
689 sprintf(proc, "/proc/%d/regs", tcp->pid);
690 if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) {
691 perror("strace: open(\"/proc/.../regs\", ...)");
692 return -1;
693 }
694 if (cflag) {
695 sprintf(proc, "/proc/%d/status", tcp->pid);
696 if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) {
697 perror("strace: open(\"/proc/.../status\", ...)");
698 return -1;
699 }
700 } else
701 tcp->pfd_status = -1;
702#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000703 rebuild_pollv();
704 if (!attaching) {
705 /*
706 * Wait for the child to pause. Because of a race
707 * condition we have to poll for the event.
708 */
709 for (;;) {
710 if (IOCTL_STATUS (tcp) < 0) {
711 perror("strace: PIOCSTATUS");
712 return -1;
713 }
714 if (tcp->status.PR_FLAGS & PR_ASLEEP)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000715 break;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000716 }
717 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000718#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000719 /* Stop the process so that we own the stop. */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000720 if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000721 perror("strace: PIOCSTOP");
722 return -1;
723 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000724#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000725#ifdef PIOCSET
726 /* Set Run-on-Last-Close. */
727 arg = PR_RLC;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000728 if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000729 perror("PIOCSET PR_RLC");
730 return -1;
731 }
732 /* Set or Reset Inherit-on-Fork. */
733 arg = PR_FORK;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000734 if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000735 perror("PIOC{SET,RESET} PR_FORK");
736 return -1;
737 }
738#else /* !PIOCSET */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000739#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000740 if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
741 perror("PIOCSRLC");
742 return -1;
743 }
744 if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
745 perror("PIOC{S,R}FORK");
746 return -1;
747 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000748#else /* FREEBSD */
749 /* just unset the PF_LINGER flag for the Run-on-Last-Close. */
750 if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) {
751 perror("PIOCGFL");
752 return -1;
753 }
754 arg &= ~PF_LINGER;
755 if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) {
756 perror("PIOCSFL");
757 return -1;
758 }
759#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000760#endif /* !PIOCSET */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000761#ifndef FREEBSD
John Hughes19e49982001-10-19 08:59:12 +0000762 /* Enable all syscall entries we care about. */
763 premptyset(&syscalls);
764 for (i = 1; i < MAX_QUALS; ++i) {
765 if (i > (sizeof syscalls) * CHAR_BIT) break;
766 if (qual_flags [i] & QUAL_TRACE) praddset (&syscalls, i);
767 }
768 praddset (&syscalls, SYS_execve);
769 if (followfork) {
770 praddset (&syscalls, SYS_fork);
771#ifdef SYS_forkall
772 praddset (&syscalls, SYS_forkall);
773#endif
774#ifdef SYS_fork1
775 praddset (&syscalls, SYS_fork1);
776#endif
777#ifdef SYS_rfork1
778 praddset (&syscalls, SYS_rfork1);
779#endif
780#ifdef SYS_rforkall
781 praddset (&syscalls, SYS_rforkall);
782#endif
783 }
784 if (IOCTL(tcp->pfd, PIOCSENTRY, &syscalls) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000785 perror("PIOCSENTRY");
786 return -1;
787 }
John Hughes19e49982001-10-19 08:59:12 +0000788 /* Enable the syscall exits. */
789 if (IOCTL(tcp->pfd, PIOCSEXIT, &syscalls) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000790 perror("PIOSEXIT");
791 return -1;
792 }
John Hughes19e49982001-10-19 08:59:12 +0000793 /* Enable signals we care about. */
794 premptyset(&signals);
795 for (i = 1; i < MAX_QUALS; ++i) {
796 if (i > (sizeof signals) * CHAR_BIT) break;
797 if (qual_flags [i] & QUAL_SIGNAL) praddset (&signals, i);
798 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000799 if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000800 perror("PIOCSTRACE");
801 return -1;
802 }
John Hughes19e49982001-10-19 08:59:12 +0000803 /* Enable faults we care about */
804 premptyset(&faults);
805 for (i = 1; i < MAX_QUALS; ++i) {
806 if (i > (sizeof faults) * CHAR_BIT) break;
807 if (qual_flags [i] & QUAL_FAULT) praddset (&faults, i);
808 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000809 if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000810 perror("PIOCSFAULT");
811 return -1;
812 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000813#else /* FREEBSD */
814 /* set events flags. */
815 arg = S_SIG | S_SCE | S_SCX ;
816 if(ioctl(tcp->pfd, PIOCBIS, arg) < 0) {
817 perror("PIOCBIS");
818 return -1;
819 }
820#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000821 if (!attaching) {
822#ifdef MIPS
823 /*
824 * The SGI PRSABORT doesn't work for pause() so
825 * we send it a caught signal to wake it up.
826 */
827 kill(tcp->pid, SIGINT);
828#else /* !MIPS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000829#ifdef PRSABORT
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000830 /* The child is in a pause(), abort it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000831 arg = PRSABORT;
832 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000833 perror("PIOCRUN");
834 return -1;
835 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000836#endif
837#endif /* !MIPS*/
838#ifdef FREEBSD
839 /* wake up the child if it received the SIGSTOP */
840 kill(tcp->pid, SIGCONT);
841#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000842 for (;;) {
843 /* Wait for the child to do something. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000844 if (IOCTL_WSTOP (tcp) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000845 perror("PIOCWSTOP");
846 return -1;
847 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000848 if (tcp->status.PR_WHY == PR_SYSENTRY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000849 tcp->flags &= ~TCB_INSYSCALL;
850 get_scno(tcp);
851 if (tcp->scno == SYS_execve)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000852 break;
853 }
854 /* Set it running: maybe execve will be next. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000855#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000856 arg = 0;
857 if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000858#else /* FREEBSD */
859 if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) {
860#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000861 perror("PIOCRUN");
862 return -1;
863 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000864#ifdef FREEBSD
865 /* handle the case where we "opened" the child before
866 it did the kill -STOP */
867 if (tcp->status.PR_WHY == PR_SIGNALLED &&
868 tcp->status.PR_WHAT == SIGSTOP)
869 kill(tcp->pid, SIGCONT);
870#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000871 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000872#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000873 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000874#else /* FREEBSD */
875 } else {
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +0000876 if (attaching < 2) {
877 /* We are attaching to an already running process.
878 * Try to figure out the state of the process in syscalls,
879 * to handle the first event well.
880 * This is done by having a look at the "wchan" property of the
881 * process, which tells where it is stopped (if it is). */
882 FILE * status;
883 char wchan[20]; /* should be enough */
884
885 sprintf(proc, "/proc/%d/status", tcp->pid);
886 status = fopen(proc, "r");
887 if (status &&
888 (fscanf(status, "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d"
889 "%*d,%*d %*d,%*d %19s", wchan) == 1) &&
890 strcmp(wchan, "nochan") && strcmp(wchan, "spread") &&
891 strcmp(wchan, "stopevent")) {
892 /* The process is asleep in the middle of a syscall.
893 Fake the syscall entry event */
894 tcp->flags &= ~(TCB_INSYSCALL|TCB_STARTUP);
895 tcp->status.PR_WHY = PR_SYSENTRY;
896 trace_syscall(tcp);
897 }
898 if (status)
899 fclose(status);
900 } /* otherwise it's a fork being followed */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000901 }
902#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000903#ifndef HAVE_POLLABLE_PROCFS
904 if (proc_poll_pipe[0] != -1)
905 proc_poller(tcp->pfd);
906 else if (nprocs > 1) {
907 proc_poll_open();
908 proc_poller(last_pfd);
909 proc_poller(tcp->pfd);
910 }
911 last_pfd = tcp->pfd;
912#endif /* !HAVE_POLLABLE_PROCFS */
913 return 0;
914}
915
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000916#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000917
918static struct tcb *
919pid2tcb(pid)
920int pid;
921{
922 int i;
923 struct tcb *tcp;
924
925 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
926 if (pid && tcp->pid != pid)
927 continue;
928 if (tcp->flags & TCB_INUSE)
929 return tcp;
930 }
931 return NULL;
932}
933
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000934#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000935
936static struct tcb *
937pfd2tcb(pfd)
938int pfd;
939{
940 int i;
941 struct tcb *tcp;
942
943 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
944 if (tcp->pfd != pfd)
945 continue;
946 if (tcp->flags & TCB_INUSE)
947 return tcp;
948 }
949 return NULL;
950}
951
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000952#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000953
954void
955droptcb(tcp)
956struct tcb *tcp;
957{
958 if (tcp->pid == 0)
959 return;
960 nprocs--;
961 tcp->pid = 0;
962 tcp->flags = 0;
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +0000963
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000964 if (tcp->pfd != -1) {
965 close(tcp->pfd);
966 tcp->pfd = -1;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000967#ifdef FREEBSD
968 if (tcp->pfd_reg != -1) {
969 close(tcp->pfd_reg);
970 tcp->pfd_reg = -1;
971 }
972 if (tcp->pfd_status != -1) {
973 close(tcp->pfd_status);
974 tcp->pfd_status = -1;
975 }
976#endif /* !FREEBSD */
977#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000978 rebuild_pollv();
979#endif
980 }
981 if (tcp->parent != NULL) {
982 tcp->parent->nchildren--;
983 tcp->parent = NULL;
984 }
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +0000985
Wichert Akkerman822f0c92002-04-03 10:55:14 +0000986 if (outfname && followfork > 1 && tcp->outf)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000987 fclose(tcp->outf);
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +0000988
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000989 tcp->outf = 0;
990}
991
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000992#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000993
994static int
995resume(tcp)
996struct tcb *tcp;
997{
998 if (tcp == NULL)
999 return -1;
1000
1001 if (!(tcp->flags & TCB_SUSPENDED)) {
1002 fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid);
1003 return -1;
1004 }
1005 tcp->flags &= ~TCB_SUSPENDED;
1006
1007 if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) {
1008 perror("resume: ptrace(PTRACE_SYSCALL, ...)");
1009 return -1;
1010 }
1011
1012 if (!qflag)
1013 fprintf(stderr, "Process %u resumed\n", tcp->pid);
1014 return 0;
1015}
1016
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001017#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001018
1019/* detach traced process; continue with sig */
1020
1021static int
1022detach(tcp, sig)
1023struct tcb *tcp;
1024int sig;
1025{
1026 int error = 0;
1027#ifdef LINUX
1028 int status;
1029#endif
1030
1031 if (tcp->flags & TCB_BPTSET)
1032 sig = SIGKILL;
1033
1034#ifdef LINUX
1035 /*
1036 * Linux wrongly insists the child be stopped
1037 * before detaching. Arghh. We go through hoops
1038 * to make a clean break of things.
1039 */
Wichert Akkermandacfb6e1999-06-03 14:21:07 +00001040#if defined(SPARC)
1041#undef PTRACE_DETACH
1042#define PTRACE_DETACH PTRACE_SUNDETACH
1043#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001044 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
1045 /* On a clear day, you can see forever. */
1046 }
1047 else if (errno != ESRCH) {
1048 /* Shouldn't happen. */
1049 perror("detach: ptrace(PTRACE_DETACH, ...)");
1050 }
1051 else if (kill(tcp->pid, 0) < 0) {
1052 if (errno != ESRCH)
1053 perror("detach: checking sanity");
1054 }
1055 else if (kill(tcp->pid, SIGSTOP) < 0) {
1056 if (errno != ESRCH)
1057 perror("detach: stopping child");
1058 }
1059 else {
1060 for (;;) {
1061 if (waitpid(tcp->pid, &status, 0) < 0) {
1062 if (errno != ECHILD)
1063 perror("detach: waiting");
1064 break;
1065 }
1066 if (!WIFSTOPPED(status)) {
1067 /* Au revoir, mon ami. */
1068 break;
1069 }
1070 if (WSTOPSIG(status) == SIGSTOP) {
1071 if ((error = ptrace(PTRACE_DETACH,
1072 tcp->pid, (char *) 1, sig)) < 0) {
1073 if (errno != ESRCH)
1074 perror("detach: ptrace(PTRACE_DETACH, ...)");
1075 /* I died trying. */
1076 }
1077 break;
1078 }
1079 if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1,
1080 WSTOPSIG(status) == SIGTRAP ?
1081 0 : WSTOPSIG(status))) < 0) {
1082 if (errno != ESRCH)
1083 perror("detach: ptrace(PTRACE_CONT, ...)");
1084 break;
1085 }
1086 }
1087 }
1088#endif /* LINUX */
1089
1090#if defined(SUNOS4)
1091 /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
1092 if (sig && kill(tcp->pid, sig) < 0)
1093 perror("detach: kill");
1094 sig = 0;
1095 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0)
1096 perror("detach: ptrace(PTRACE_DETACH, ...)");
1097#endif /* SUNOS4 */
1098
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001099#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001100 if (waiting_parent(tcp))
1101 error = resume(tcp->parent);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001102#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001103
1104 if (!qflag)
1105 fprintf(stderr, "Process %u detached\n", tcp->pid);
1106
1107 droptcb(tcp);
1108 return error;
1109}
1110
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001111#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001112
1113static void
1114reaper(sig)
1115int sig;
1116{
1117 int pid;
1118 int status;
1119
1120 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
1121#if 0
1122 struct tcb *tcp;
1123
1124 tcp = pid2tcb(pid);
1125 if (tcp)
1126 droptcb(tcp);
1127#endif
1128 }
1129}
1130
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001131#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001132
1133static void
1134cleanup()
1135{
1136 int i;
1137 struct tcb *tcp;
1138
1139 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1140 if (!(tcp->flags & TCB_INUSE))
1141 continue;
1142 if (debug)
1143 fprintf(stderr,
1144 "cleanup: looking at pid %u\n", tcp->pid);
1145 if (tcp_last &&
1146 (!outfname || followfork < 2 || tcp_last == tcp)) {
1147 tprintf(" <unfinished ...>\n");
1148 tcp_last = NULL;
1149 }
1150 if (tcp->flags & TCB_ATTACHED)
1151 detach(tcp, 0);
1152 else {
1153 kill(tcp->pid, SIGCONT);
1154 kill(tcp->pid, SIGTERM);
1155 }
1156 }
1157 if (cflag)
1158 call_summary(outf);
1159}
1160
1161static void
1162interrupt(sig)
1163int sig;
1164{
1165 interrupted = 1;
1166}
1167
1168#ifndef HAVE_STRERROR
1169
1170#ifndef SYS_ERRLIST_DECLARED
1171extern int sys_nerr;
1172extern char *sys_errlist[];
1173#endif /* SYS_ERRLIST_DECLARED */
1174
1175const char *
1176strerror(errno)
1177int errno;
1178{
1179 static char buf[64];
1180
1181 if (errno < 1 || errno >= sys_nerr) {
1182 sprintf(buf, "Unknown error %d", errno);
1183 return buf;
1184 }
1185 return sys_errlist[errno];
1186}
1187
1188#endif /* HAVE_STERRROR */
1189
1190#ifndef HAVE_STRSIGNAL
1191
1192#ifndef SYS_SIGLIST_DECLARED
1193#ifdef HAVE__SYS_SIGLIST
1194 extern char *_sys_siglist[];
1195#else
1196 extern char *sys_siglist[];
1197#endif
1198#endif /* SYS_SIGLIST_DECLARED */
1199
1200const char *
1201strsignal(sig)
1202int sig;
1203{
1204 static char buf[64];
1205
1206 if (sig < 1 || sig >= NSIG) {
1207 sprintf(buf, "Unknown signal %d", sig);
1208 return buf;
1209 }
1210#ifdef HAVE__SYS_SIGLIST
1211 return _sys_siglist[sig];
1212#else
1213 return sys_siglist[sig];
1214#endif
1215}
1216
1217#endif /* HAVE_STRSIGNAL */
1218
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001219#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001220
1221static void
1222rebuild_pollv()
1223{
1224 int i, j;
1225 struct tcb *tcp;
1226
1227 for (i = j = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1228 if (!(tcp->flags & TCB_INUSE))
1229 continue;
1230 pollv[j].fd = tcp->pfd;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001231 pollv[j].events = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001232 j++;
1233 }
1234 if (j != nprocs) {
1235 fprintf(stderr, "strace: proc miscount\n");
1236 exit(1);
1237 }
1238}
1239
1240#ifndef HAVE_POLLABLE_PROCFS
1241
1242static void
1243proc_poll_open()
1244{
1245 int arg;
1246 int i;
1247
1248 if (pipe(proc_poll_pipe) < 0) {
1249 perror("pipe");
1250 exit(1);
1251 }
1252 for (i = 0; i < 2; i++) {
1253 if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) {
1254 perror("F_GETFD");
1255 exit(1);
1256 }
1257 if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) {
1258 perror("F_SETFD");
1259 exit(1);
1260 }
1261 }
1262}
1263
1264static int
1265proc_poll(pollv, nfds, timeout)
1266struct pollfd *pollv;
1267int nfds;
1268int timeout;
1269{
1270 int i;
1271 int n;
1272 struct proc_pollfd pollinfo;
1273
1274 if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
1275 return n;
1276 if (n != sizeof(struct proc_pollfd)) {
1277 fprintf(stderr, "panic: short read: %d\n", n);
1278 exit(1);
1279 }
1280 for (i = 0; i < nprocs; i++) {
1281 if (pollv[i].fd == pollinfo.fd)
1282 pollv[i].revents = pollinfo.revents;
1283 else
1284 pollv[i].revents = 0;
1285 }
1286 poller_pid = pollinfo.pid;
1287 return 1;
1288}
1289
1290static void
1291wakeup_handler(sig)
1292int sig;
1293{
1294}
1295
1296static void
1297proc_poller(pfd)
1298int pfd;
1299{
1300 struct proc_pollfd pollinfo;
1301 struct sigaction sa;
1302 sigset_t blocked_set, empty_set;
1303 int i;
1304 int n;
1305 struct rlimit rl;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001306#ifdef FREEBSD
1307 struct procfs_status pfs;
1308#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001309
1310 switch (fork()) {
1311 case -1:
1312 perror("fork");
1313 _exit(0);
1314 case 0:
1315 break;
1316 default:
1317 return;
1318 }
1319
1320 sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
1321 sa.sa_flags = 0;
1322 sigemptyset(&sa.sa_mask);
1323 sigaction(SIGHUP, &sa, NULL);
1324 sigaction(SIGINT, &sa, NULL);
1325 sigaction(SIGQUIT, &sa, NULL);
1326 sigaction(SIGPIPE, &sa, NULL);
1327 sigaction(SIGTERM, &sa, NULL);
1328 sa.sa_handler = wakeup_handler;
1329 sigaction(SIGUSR1, &sa, NULL);
1330 sigemptyset(&blocked_set);
1331 sigaddset(&blocked_set, SIGUSR1);
1332 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1333 sigemptyset(&empty_set);
1334
1335 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
1336 perror("getrlimit(RLIMIT_NOFILE, ...)");
1337 _exit(0);
1338 }
1339 n = rl.rlim_cur;
1340 for (i = 0; i < n; i++) {
1341 if (i != pfd && i != proc_poll_pipe[1])
1342 close(i);
1343 }
1344
1345 pollinfo.fd = pfd;
1346 pollinfo.pid = getpid();
1347 for (;;) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001348#ifndef FREEBSD
1349 if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
1350#else /* FREEBSD */
1351 if (ioctl(pfd, PIOCWSTOP, &pfs) < 0)
1352#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001353 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001354 switch (errno) {
1355 case EINTR:
1356 continue;
1357 case EBADF:
1358 pollinfo.revents = POLLERR;
1359 break;
1360 case ENOENT:
1361 pollinfo.revents = POLLHUP;
1362 break;
1363 default:
1364 perror("proc_poller: PIOCWSTOP");
1365 }
1366 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1367 _exit(0);
1368 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001369 pollinfo.revents = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001370 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1371 sigsuspend(&empty_set);
1372 }
1373}
1374
1375#endif /* !HAVE_POLLABLE_PROCFS */
1376
1377static int
1378choose_pfd()
1379{
1380 int i, j;
1381 struct tcb *tcp;
1382
1383 static int last;
1384
1385 if (followfork < 2 &&
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001386 last < nprocs && (pollv[last].revents & POLLWANT)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001387 /*
1388 * The previous process is ready to run again. We'll
1389 * let it do so if it is currently in a syscall. This
1390 * heuristic improves the readability of the trace.
1391 */
1392 tcp = pfd2tcb(pollv[last].fd);
1393 if (tcp && (tcp->flags & TCB_INSYSCALL))
1394 return pollv[last].fd;
1395 }
1396
1397 for (i = 0; i < nprocs; i++) {
1398 /* Let competing children run round robin. */
1399 j = (i + last + 1) % nprocs;
1400 if (pollv[j].revents & (POLLHUP | POLLERR)) {
1401 tcp = pfd2tcb(pollv[j].fd);
1402 if (!tcp) {
1403 fprintf(stderr, "strace: lost proc\n");
1404 exit(1);
1405 }
1406 droptcb(tcp);
1407 return -1;
1408 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001409 if (pollv[j].revents & POLLWANT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001410 last = j;
1411 return pollv[j].fd;
1412 }
1413 }
1414 fprintf(stderr, "strace: nothing ready\n");
1415 exit(1);
1416}
1417
1418static int
1419trace()
1420{
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001421#ifdef POLL_HACK
John Hughesd870b3c2002-05-21 11:24:18 +00001422 struct tcb *in_syscall = NULL;
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001423#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001424 struct tcb *tcp;
1425 int pfd;
1426 int what;
1427 int ioctl_result = 0, ioctl_errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001428 long arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001429
1430 for (;;) {
1431 if (interactive)
1432 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1433
1434 if (nprocs == 0)
1435 break;
1436
1437 switch (nprocs) {
1438 case 1:
1439#ifndef HAVE_POLLABLE_PROCFS
1440 if (proc_poll_pipe[0] == -1) {
1441#endif
1442 tcp = pid2tcb(0);
1443 if (!tcp)
1444 continue;
1445 pfd = tcp->pfd;
1446 if (pfd == -1)
1447 continue;
1448 break;
1449#ifndef HAVE_POLLABLE_PROCFS
1450 }
1451 /* fall through ... */
1452#endif /* !HAVE_POLLABLE_PROCFS */
1453 default:
1454#ifdef HAVE_POLLABLE_PROCFS
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001455#ifdef POLL_HACK
1456 /* On some systems (e.g. UnixWare) we get too much ugly
1457 "unfinished..." stuff when multiple proceses are in
1458 syscalls. Here's a nasty hack */
1459
1460 if (in_syscall) {
1461 struct pollfd pv;
1462 tcp = in_syscall;
1463 in_syscall = NULL;
1464 pv.fd = tcp->pfd;
1465 pv.events = POLLWANT;
1466 if ((what = poll (&pv, 1, 1)) < 0) {
1467 if (interrupted)
1468 return 0;
1469 continue;
1470 }
1471 else if (what == 1 && pv.revents & POLLWANT) {
1472 goto FOUND;
1473 }
1474 }
1475#endif
1476
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001477 if (poll(pollv, nprocs, INFTIM) < 0) {
1478 if (interrupted)
1479 return 0;
1480 continue;
1481 }
1482#else /* !HAVE_POLLABLE_PROCFS */
1483 if (proc_poll(pollv, nprocs, INFTIM) < 0) {
1484 if (interrupted)
1485 return 0;
1486 continue;
1487 }
1488#endif /* !HAVE_POLLABLE_PROCFS */
1489 pfd = choose_pfd();
1490 if (pfd == -1)
1491 continue;
1492 break;
1493 }
1494
1495 /* Look up `pfd' in our table. */
1496 if ((tcp = pfd2tcb(pfd)) == NULL) {
1497 fprintf(stderr, "unknown pfd: %u\n", pfd);
1498 exit(1);
1499 }
John Hughesb6643082002-05-23 11:02:22 +00001500#ifdef POLL_HACK
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001501 FOUND:
John Hughesb6643082002-05-23 11:02:22 +00001502#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001503 /* Get the status of the process. */
1504 if (!interrupted) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001505#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001506 ioctl_result = IOCTL_WSTOP (tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001507#else /* FREEBSD */
1508 /* Thanks to some scheduling mystery, the first poller
1509 sometimes waits for the already processed end of fork
1510 event. Doing a non blocking poll here solves the problem. */
1511 if (proc_poll_pipe[0] != -1)
1512 ioctl_result = IOCTL_STATUS (tcp);
1513 else
1514 ioctl_result = IOCTL_WSTOP (tcp);
1515#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001516 ioctl_errno = errno;
1517#ifndef HAVE_POLLABLE_PROCFS
1518 if (proc_poll_pipe[0] != -1) {
1519 if (ioctl_result < 0)
1520 kill(poller_pid, SIGKILL);
1521 else
1522 kill(poller_pid, SIGUSR1);
1523 }
1524#endif /* !HAVE_POLLABLE_PROCFS */
1525 }
1526 if (interrupted)
1527 return 0;
1528
1529 if (interactive)
1530 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1531
1532 if (ioctl_result < 0) {
1533 /* Find out what happened if it failed. */
1534 switch (ioctl_errno) {
1535 case EINTR:
1536 case EBADF:
1537 continue;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001538#ifdef FREEBSD
1539 case ENOTTY:
1540#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001541 case ENOENT:
1542 droptcb(tcp);
1543 continue;
1544 default:
1545 perror("PIOCWSTOP");
1546 exit(1);
1547 }
1548 }
1549
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +00001550#ifdef FREEBSD
1551 if ((tcp->flags & TCB_STARTUP) && (tcp->status.PR_WHY == PR_SYSEXIT)) {
1552 /* discard first event for a syscall we never entered */
1553 IOCTL (tcp->pfd, PIOCRUN, 0);
1554 continue;
1555 }
1556#endif
1557
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001558 /* clear the just started flag */
1559 tcp->flags &= ~TCB_STARTUP;
1560
1561 /* set current output file */
1562 outf = tcp->outf;
1563
1564 if (cflag) {
1565 struct timeval stime;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001566#ifdef FREEBSD
1567 char buf[1024];
1568 int len;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001569
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001570 if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) {
1571 buf[len] = '\0';
1572 sscanf(buf,
1573 "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld",
1574 &stime.tv_sec, &stime.tv_usec);
1575 } else
1576 stime.tv_sec = stime.tv_usec = 0;
1577#else /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001578 stime.tv_sec = tcp->status.pr_stime.tv_sec;
1579 stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001580#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001581 tv_sub(&tcp->dtime, &stime, &tcp->stime);
1582 tcp->stime = stime;
1583 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001584 what = tcp->status.PR_WHAT;
1585 switch (tcp->status.PR_WHY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001586#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001587 case PR_REQUESTED:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001588 if (tcp->status.PR_FLAGS & PR_ASLEEP) {
1589 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001590 if (trace_syscall(tcp) < 0) {
1591 fprintf(stderr, "syscall trouble\n");
1592 exit(1);
1593 }
1594 }
1595 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001596#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001597 case PR_SYSENTRY:
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001598#ifdef POLL_HACK
1599 in_syscall = tcp;
1600#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001601 case PR_SYSEXIT:
1602 if (trace_syscall(tcp) < 0) {
1603 fprintf(stderr, "syscall trouble\n");
1604 exit(1);
1605 }
1606 break;
1607 case PR_SIGNALLED:
1608 if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) {
1609 printleader(tcp);
1610 tprintf("--- %s (%s) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001611 signame(what), strsignal(what));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001612 printtrailer(tcp);
John Hughes58265892001-10-18 15:13:53 +00001613#ifdef PR_INFO
1614 if (tcp->status.PR_INFO.si_signo == what) {
1615 printleader(tcp);
1616 tprintf(" siginfo=");
1617 printsiginfo(&tcp->status.PR_INFO, 1);
1618 printtrailer(tcp);
1619 }
1620#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001621 }
1622 break;
1623 case PR_FAULTED:
1624 if (!cflag && (qual_flags[what] & QUAL_FAULT)) {
1625 printleader(tcp);
1626 tprintf("=== FAULT %d ===", what);
1627 printtrailer(tcp);
1628 }
1629 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001630#ifdef FREEBSD
1631 case 0: /* handle case we polled for nothing */
1632 continue;
1633#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001634 default:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001635 fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001636 exit(1);
1637 break;
1638 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001639 arg = 0;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001640#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001641 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001642#else
1643 if (IOCTL (tcp->pfd, PIOCRUN, 0) < 0) {
1644#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001645 perror("PIOCRUN");
1646 exit(1);
1647 }
1648 }
1649 return 0;
1650}
1651
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001652#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001653
1654static int
1655trace()
1656{
1657 int pid;
1658 int wait_errno;
1659 int status;
1660 struct tcb *tcp;
1661#ifdef LINUX
1662 struct rusage ru;
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001663#ifdef __WALL
1664 static int wait4_options = __WALL;
1665#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001666#endif /* LINUX */
1667
1668 while (nprocs != 0) {
1669 if (interactive)
1670 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1671#ifdef LINUX
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001672#ifdef __WALL
1673 pid = wait4(-1, &status, wait4_options, cflag ? &ru : NULL);
1674 if ((wait4_options & __WALL) && errno == EINVAL) {
1675 /* this kernel does not support __WALL */
1676 wait4_options &= ~__WALL;
1677 errno = 0;
1678 pid = wait4(-1, &status, wait4_options,
1679 cflag ? &ru : NULL);
1680 }
1681 if (!(wait4_options & __WALL) && errno == ECHILD) {
1682 /* most likely a "cloned" process */
1683 pid = wait4(-1, &status, __WCLONE,
1684 cflag ? &ru : NULL);
1685 if (pid == -1) {
1686 fprintf(stderr, "strace: clone wait4 "
1687 "failed: %s\n", strerror(errno));
1688 }
1689 }
1690#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001691 pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001692#endif /* __WALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001693#endif /* LINUX */
1694#ifdef SUNOS4
1695 pid = wait(&status);
1696#endif /* SUNOS4 */
1697 wait_errno = errno;
1698 if (interactive)
1699 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1700
1701 if (interrupted)
1702 return 0;
1703
1704 if (pid == -1) {
1705 switch (wait_errno) {
1706 case EINTR:
1707 continue;
1708 case ECHILD:
1709 /*
1710 * We would like to verify this case
1711 * but sometimes a race in Solbourne's
1712 * version of SunOS sometimes reports
1713 * ECHILD before sending us SIGCHILD.
1714 */
1715#if 0
1716 if (nprocs == 0)
1717 return 0;
1718 fprintf(stderr, "strace: proc miscount\n");
1719 exit(1);
1720#endif
1721 return 0;
1722 default:
1723 errno = wait_errno;
1724 perror("strace: wait");
1725 return -1;
1726 }
1727 }
1728 if (debug)
1729 fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
1730
1731 /* Look up `pid' in our table. */
1732 if ((tcp = pid2tcb(pid)) == NULL) {
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001733#if 0 /* XXX davidm */ /* WTA: disabled again */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001734 struct tcb *tcpchild;
1735
1736 if ((tcpchild = alloctcb(pid)) == NULL) {
1737 fprintf(stderr, " [tcb table full]\n");
1738 kill(pid, SIGKILL); /* XXX */
1739 return 0;
1740 }
1741 tcpchild->flags |= TCB_ATTACHED;
1742 newoutf(tcpchild);
1743 tcp->nchildren++;
1744 if (!qflag)
1745 fprintf(stderr, "Process %d attached\n", pid);
1746#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001747 fprintf(stderr, "unknown pid: %u\n", pid);
1748 if (WIFSTOPPED(status))
1749 ptrace(PTRACE_CONT, pid, (char *) 1, 0);
1750 exit(1);
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001751#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001752 }
1753 /* set current output file */
1754 outf = tcp->outf;
1755 if (cflag) {
1756#ifdef LINUX
1757 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
1758 tcp->stime = ru.ru_stime;
1759#endif /* !LINUX */
1760 }
1761
1762 if (tcp->flags & TCB_SUSPENDED) {
1763 /*
1764 * Apparently, doing any ptrace() call on a stopped
1765 * process, provokes the kernel to report the process
1766 * status again on a subsequent wait(), even if the
1767 * process has not been actually restarted.
1768 * Since we have inspected the arguments of suspended
1769 * processes we end up here testing for this case.
1770 */
1771 continue;
1772 }
1773 if (WIFSIGNALED(status)) {
1774 if (!cflag
1775 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
1776 printleader(tcp);
1777 tprintf("+++ killed by %s +++",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001778 signame(WTERMSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001779 printtrailer(tcp);
1780 }
1781 droptcb(tcp);
1782 continue;
1783 }
1784 if (WIFEXITED(status)) {
1785 if (debug)
1786 fprintf(stderr, "pid %u exited\n", pid);
1787 if (tcp->flags & TCB_ATTACHED)
1788 fprintf(stderr,
1789 "PANIC: attached pid %u exited\n",
1790 pid);
1791 droptcb(tcp);
1792 continue;
1793 }
1794 if (!WIFSTOPPED(status)) {
1795 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
1796 droptcb(tcp);
1797 continue;
1798 }
1799 if (debug)
1800 fprintf(stderr, "pid %u stopped, [%s]\n",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001801 pid, signame(WSTOPSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001802
1803 if (tcp->flags & TCB_STARTUP) {
1804 /*
1805 * This flag is there to keep us in sync.
1806 * Next time this process stops it should
1807 * really be entering a system call.
1808 */
1809 tcp->flags &= ~TCB_STARTUP;
1810 if (tcp->flags & TCB_ATTACHED) {
1811 /*
1812 * Interestingly, the process may stop
1813 * with STOPSIG equal to some other signal
1814 * than SIGSTOP if we happend to attach
1815 * just before the process takes a signal.
1816 */
1817 if (!WIFSTOPPED(status)) {
1818 fprintf(stderr,
1819 "pid %u not stopped\n", pid);
1820 detach(tcp, WSTOPSIG(status));
1821 continue;
1822 }
1823 }
1824 else {
1825#ifdef SUNOS4
1826 /* A child of us stopped at exec */
1827 if (WSTOPSIG(status) == SIGTRAP && followvfork)
1828 fixvfork(tcp);
1829#endif /* SUNOS4 */
1830 }
1831 if (tcp->flags & TCB_BPTSET) {
1832 if (clearbpt(tcp) < 0) /* Pretty fatal */ {
1833 droptcb(tcp);
1834 cleanup();
1835 return -1;
1836 }
1837 }
1838 goto tracing;
1839 }
1840
1841 if (WSTOPSIG(status) != SIGTRAP) {
1842 if (WSTOPSIG(status) == SIGSTOP &&
1843 (tcp->flags & TCB_SIGTRAPPED)) {
1844 /*
1845 * Trapped attempt to block SIGTRAP
1846 * Hope we are back in control now.
1847 */
1848 tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
1849 if (ptrace(PTRACE_SYSCALL,
1850 pid, (char *) 1, 0) < 0) {
1851 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1852 cleanup();
1853 return -1;
1854 }
1855 continue;
1856 }
1857 if (!cflag
1858 && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001859 unsigned long addr = 0, pc = 0;
1860#ifdef PT_GETSIGINFO
1861# define PSR_RI 41
1862 struct siginfo si;
1863 unsigned long psr;
1864
1865 upeek(pid, PT_CR_IPSR, &psr);
1866 upeek(pid, PT_CR_IIP, &pc);
1867
1868 pc += (psr >> PSR_RI) & 0x3;
1869 ptrace(PT_GETSIGINFO, pid, 0, (long) &si);
1870 addr = (unsigned long) si.si_addr;
1871#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001872 printleader(tcp);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001873 tprintf("--- %s (%s) @ %lx (%lx) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001874 signame(WSTOPSIG(status)),
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001875 strsignal(WSTOPSIG(status)), pc, addr);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001876 printtrailer(tcp);
1877 }
1878 if ((tcp->flags & TCB_ATTACHED) &&
1879 !sigishandled(tcp, WSTOPSIG(status))) {
1880 detach(tcp, WSTOPSIG(status));
1881 continue;
1882 }
1883 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1,
1884 WSTOPSIG(status)) < 0) {
1885 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1886 cleanup();
1887 return -1;
1888 }
1889 tcp->flags &= ~TCB_SUSPENDED;
1890 continue;
1891 }
1892 if (trace_syscall(tcp) < 0) {
1893 if (tcp->flags & TCB_ATTACHED)
1894 detach(tcp, 0);
1895 else {
1896 ptrace(PTRACE_KILL,
1897 tcp->pid, (char *) 1, SIGTERM);
1898 droptcb(tcp);
1899 }
1900 continue;
1901 }
1902 if (tcp->flags & TCB_EXITING) {
1903 if (tcp->flags & TCB_ATTACHED)
1904 detach(tcp, 0);
1905 else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) {
1906 perror("strace: ptrace(PTRACE_CONT, ...)");
1907 cleanup();
1908 return -1;
1909 }
1910 continue;
1911 }
1912 if (tcp->flags & TCB_SUSPENDED) {
1913 if (!qflag)
1914 fprintf(stderr, "Process %u suspended\n", pid);
1915 continue;
1916 }
1917 tracing:
1918 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) {
1919 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1920 cleanup();
1921 return -1;
1922 }
1923 }
1924 return 0;
1925}
1926
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001927#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001928
1929static int curcol;
1930
1931#ifdef __STDC__
1932#include <stdarg.h>
1933#define VA_START(a, b) va_start(a, b)
1934#else
1935#include <varargs.h>
1936#define VA_START(a, b) va_start(a)
1937#endif
1938
1939void
1940#ifdef __STDC__
1941tprintf(const char *fmt, ...)
1942#else
1943tprintf(fmt, va_alist)
1944char *fmt;
1945va_dcl
1946#endif
1947{
1948 va_list args;
1949
1950 VA_START(args, fmt);
1951 if (outf)
1952 curcol += vfprintf(outf, fmt, args);
1953 va_end(args);
1954 return;
1955}
1956
1957void
1958printleader(tcp)
1959struct tcb *tcp;
1960{
1961 if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) {
1962 tcp_last->flags |= TCB_REPRINT;
1963 tprintf(" <unfinished ...>\n");
1964 }
1965 curcol = 0;
1966 if ((followfork == 1 || pflag_seen > 1) && outfname)
1967 tprintf("%-5d ", tcp->pid);
1968 else if (nprocs > 1 && !outfname)
1969 tprintf("[pid %5u] ", tcp->pid);
1970 if (tflag) {
1971 char str[sizeof("HH:MM:SS")];
1972 struct timeval tv, dtv;
1973 static struct timeval otv;
1974
1975 gettimeofday(&tv, NULL);
1976 if (rflag) {
1977 if (otv.tv_sec == 0)
1978 otv = tv;
1979 tv_sub(&dtv, &tv, &otv);
1980 tprintf("%6ld.%06ld ",
1981 (long) dtv.tv_sec, (long) dtv.tv_usec);
1982 otv = tv;
1983 }
1984 else if (tflag > 2) {
1985 tprintf("%ld.%06ld ",
1986 (long) tv.tv_sec, (long) tv.tv_usec);
1987 }
1988 else {
1989 time_t local = tv.tv_sec;
1990 strftime(str, sizeof(str), "%T", localtime(&local));
1991 if (tflag > 1)
1992 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
1993 else
1994 tprintf("%s ", str);
1995 }
1996 }
1997 if (iflag)
1998 printcall(tcp);
1999}
2000
2001void
2002tabto(col)
2003int col;
2004{
2005 if (curcol < col)
2006 tprintf("%*s", col - curcol, "");
2007}
2008
2009void
2010printtrailer(tcp)
2011struct tcb *tcp;
2012{
2013 tprintf("\n");
2014 tcp_last = NULL;
2015}
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002016
Wichert Akkermanea78f0f1999-11-29 15:34:02 +00002017#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002018
2019int mp_ioctl (int fd, int cmd, void *arg, int size) {
2020
2021 struct iovec iov[2];
2022 int n = 1;
2023
2024 iov[0].iov_base = &cmd;
2025 iov[0].iov_len = sizeof cmd;
2026 if (arg) {
2027 ++n;
2028 iov[1].iov_base = arg;
2029 iov[1].iov_len = size;
2030 }
2031
2032 return writev (fd, iov, n);
2033}
2034
2035#endif