blob: ec8d752717b16c7d493fad70115656d6ca4b9e43 [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 */
Roland McGrath553a6092002-12-16 20:40:39 +0000453#endif /* FREEBSD */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000454#else /* !USE_PROCFS */
Roland McGrath553a6092002-12-16 20:40:39 +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);
Roland McGrath553a6092002-12-16 20:40:39 +0000551#else
552 /* Make sure SIGCHLD has the default action so that waitpid
553 definitely works without losing track of children. The user
554 should not have given us a bogus state to inherit, but he might
555 have. Arguably we should detect SIG_IGN here and pass it on
556 to children, but probably noone really needs that. */
557 sa.sa_handler = SIG_DFL;
558 sigaction(SIGCHLD, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000559#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000560
561 if (trace() < 0)
562 exit(1);
563 cleanup();
564 exit(0);
565}
566
567void
568newoutf(tcp)
569struct tcb *tcp;
570{
571 char name[MAXPATHLEN];
572 FILE *fp;
573
574 if (outfname && followfork > 1) {
575 sprintf(name, "%s.%u", outfname, tcp->pid);
576#ifndef SVR4
577 setreuid(geteuid(), getuid());
578#endif
579 fp = fopen(name, "w");
580#ifndef SVR4
581 setreuid(geteuid(), getuid());
582#endif
583 if (fp == NULL) {
584 perror("fopen");
585 return;
586 }
587 tcp->outf = fp;
588 }
589 return;
590}
591
592struct tcb *
593alloctcb(pid)
594int pid;
595{
596 int i;
597 struct tcb *tcp;
598
599 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
600 if ((tcp->flags & TCB_INUSE) == 0) {
601 tcp->pid = pid;
602 tcp->parent = NULL;
603 tcp->nchildren = 0;
604 tcp->flags = TCB_INUSE | TCB_STARTUP;
605 tcp->outf = outf; /* Initialise to current out file */
606 tcp->stime.tv_sec = 0;
607 tcp->stime.tv_usec = 0;
608 tcp->pfd = -1;
609 nprocs++;
610 return tcp;
611 }
612 }
613 return NULL;
614}
615
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000616#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000617int
618proc_open(tcp, attaching)
619struct tcb *tcp;
620int attaching;
621{
622 char proc[32];
623 long arg;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000624#ifdef SVR4
John Hughes19e49982001-10-19 08:59:12 +0000625 int i;
626 sysset_t syscalls;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000627 sigset_t signals;
628 fltset_t faults;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000629#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000630#ifndef HAVE_POLLABLE_PROCFS
631 static int last_pfd;
632#endif
633
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000634#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000635 /* Open the process pseudo-files in /proc. */
636 sprintf(proc, "/proc/%d/ctl", tcp->pid);
637 if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000638 perror("strace: open(\"/proc/...\", ...)");
639 return -1;
640 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000641 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
642 perror("F_GETFD");
643 return -1;
644 }
645 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
646 perror("F_SETFD");
647 return -1;
648 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000649 sprintf(proc, "/proc/%d/status", tcp->pid);
650 if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
651 perror("strace: open(\"/proc/...\", ...)");
652 return -1;
653 }
654 if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) {
655 perror("F_GETFD");
656 return -1;
657 }
658 if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) {
659 perror("F_SETFD");
660 return -1;
661 }
662 sprintf(proc, "/proc/%d/as", tcp->pid);
663 if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
664 perror("strace: open(\"/proc/...\", ...)");
665 return -1;
666 }
667 if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) {
668 perror("F_GETFD");
669 return -1;
670 }
671 if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) {
672 perror("F_SETFD");
673 return -1;
674 }
675#else
676 /* Open the process pseudo-file in /proc. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000677#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000678 sprintf(proc, "/proc/%d", tcp->pid);
679 if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000680#else /* FREEBSD */
681 sprintf(proc, "/proc/%d/mem", tcp->pid);
682 if ((tcp->pfd = open(proc, O_RDWR)) < 0) {
683#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000684 perror("strace: open(\"/proc/...\", ...)");
685 return -1;
686 }
687 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
688 perror("F_GETFD");
689 return -1;
690 }
691 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
692 perror("F_SETFD");
693 return -1;
694 }
695#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000696#ifdef FREEBSD
697 sprintf(proc, "/proc/%d/regs", tcp->pid);
698 if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) {
699 perror("strace: open(\"/proc/.../regs\", ...)");
700 return -1;
701 }
702 if (cflag) {
703 sprintf(proc, "/proc/%d/status", tcp->pid);
704 if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) {
705 perror("strace: open(\"/proc/.../status\", ...)");
706 return -1;
707 }
708 } else
709 tcp->pfd_status = -1;
710#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000711 rebuild_pollv();
712 if (!attaching) {
713 /*
714 * Wait for the child to pause. Because of a race
715 * condition we have to poll for the event.
716 */
717 for (;;) {
718 if (IOCTL_STATUS (tcp) < 0) {
719 perror("strace: PIOCSTATUS");
720 return -1;
721 }
722 if (tcp->status.PR_FLAGS & PR_ASLEEP)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000723 break;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000724 }
725 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000726#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000727 /* Stop the process so that we own the stop. */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000728 if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000729 perror("strace: PIOCSTOP");
730 return -1;
731 }
Roland McGrath553a6092002-12-16 20:40:39 +0000732#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000733#ifdef PIOCSET
734 /* Set Run-on-Last-Close. */
735 arg = PR_RLC;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000736 if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000737 perror("PIOCSET PR_RLC");
738 return -1;
739 }
740 /* Set or Reset Inherit-on-Fork. */
741 arg = PR_FORK;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000742 if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000743 perror("PIOC{SET,RESET} PR_FORK");
744 return -1;
745 }
746#else /* !PIOCSET */
Roland McGrath553a6092002-12-16 20:40:39 +0000747#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000748 if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
749 perror("PIOCSRLC");
750 return -1;
751 }
752 if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
753 perror("PIOC{S,R}FORK");
754 return -1;
755 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000756#else /* FREEBSD */
757 /* just unset the PF_LINGER flag for the Run-on-Last-Close. */
758 if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) {
759 perror("PIOCGFL");
760 return -1;
761 }
762 arg &= ~PF_LINGER;
763 if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) {
764 perror("PIOCSFL");
765 return -1;
766 }
767#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000768#endif /* !PIOCSET */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000769#ifndef FREEBSD
John Hughes19e49982001-10-19 08:59:12 +0000770 /* Enable all syscall entries we care about. */
771 premptyset(&syscalls);
772 for (i = 1; i < MAX_QUALS; ++i) {
773 if (i > (sizeof syscalls) * CHAR_BIT) break;
774 if (qual_flags [i] & QUAL_TRACE) praddset (&syscalls, i);
775 }
776 praddset (&syscalls, SYS_execve);
777 if (followfork) {
778 praddset (&syscalls, SYS_fork);
779#ifdef SYS_forkall
780 praddset (&syscalls, SYS_forkall);
781#endif
Roland McGrath553a6092002-12-16 20:40:39 +0000782#ifdef SYS_fork1
John Hughes19e49982001-10-19 08:59:12 +0000783 praddset (&syscalls, SYS_fork1);
784#endif
785#ifdef SYS_rfork1
786 praddset (&syscalls, SYS_rfork1);
787#endif
788#ifdef SYS_rforkall
789 praddset (&syscalls, SYS_rforkall);
790#endif
791 }
792 if (IOCTL(tcp->pfd, PIOCSENTRY, &syscalls) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000793 perror("PIOCSENTRY");
794 return -1;
795 }
John Hughes19e49982001-10-19 08:59:12 +0000796 /* Enable the syscall exits. */
797 if (IOCTL(tcp->pfd, PIOCSEXIT, &syscalls) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000798 perror("PIOSEXIT");
799 return -1;
800 }
John Hughes19e49982001-10-19 08:59:12 +0000801 /* Enable signals we care about. */
802 premptyset(&signals);
803 for (i = 1; i < MAX_QUALS; ++i) {
804 if (i > (sizeof signals) * CHAR_BIT) break;
805 if (qual_flags [i] & QUAL_SIGNAL) praddset (&signals, i);
806 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000807 if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000808 perror("PIOCSTRACE");
809 return -1;
810 }
John Hughes19e49982001-10-19 08:59:12 +0000811 /* Enable faults we care about */
812 premptyset(&faults);
813 for (i = 1; i < MAX_QUALS; ++i) {
814 if (i > (sizeof faults) * CHAR_BIT) break;
815 if (qual_flags [i] & QUAL_FAULT) praddset (&faults, i);
816 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000817 if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000818 perror("PIOCSFAULT");
819 return -1;
820 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000821#else /* FREEBSD */
822 /* set events flags. */
823 arg = S_SIG | S_SCE | S_SCX ;
824 if(ioctl(tcp->pfd, PIOCBIS, arg) < 0) {
825 perror("PIOCBIS");
826 return -1;
827 }
828#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000829 if (!attaching) {
830#ifdef MIPS
831 /*
832 * The SGI PRSABORT doesn't work for pause() so
833 * we send it a caught signal to wake it up.
834 */
835 kill(tcp->pid, SIGINT);
836#else /* !MIPS */
Roland McGrath553a6092002-12-16 20:40:39 +0000837#ifdef PRSABORT
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000838 /* The child is in a pause(), abort it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000839 arg = PRSABORT;
840 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000841 perror("PIOCRUN");
842 return -1;
843 }
Roland McGrath553a6092002-12-16 20:40:39 +0000844#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000845#endif /* !MIPS*/
846#ifdef FREEBSD
847 /* wake up the child if it received the SIGSTOP */
848 kill(tcp->pid, SIGCONT);
Roland McGrath553a6092002-12-16 20:40:39 +0000849#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000850 for (;;) {
851 /* Wait for the child to do something. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000852 if (IOCTL_WSTOP (tcp) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000853 perror("PIOCWSTOP");
854 return -1;
855 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000856 if (tcp->status.PR_WHY == PR_SYSENTRY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000857 tcp->flags &= ~TCB_INSYSCALL;
858 get_scno(tcp);
859 if (tcp->scno == SYS_execve)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000860 break;
861 }
862 /* Set it running: maybe execve will be next. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000863#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000864 arg = 0;
865 if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000866#else /* FREEBSD */
867 if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) {
Roland McGrath553a6092002-12-16 20:40:39 +0000868#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000869 perror("PIOCRUN");
870 return -1;
871 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000872#ifdef FREEBSD
873 /* handle the case where we "opened" the child before
874 it did the kill -STOP */
875 if (tcp->status.PR_WHY == PR_SIGNALLED &&
876 tcp->status.PR_WHAT == SIGSTOP)
877 kill(tcp->pid, SIGCONT);
Roland McGrath553a6092002-12-16 20:40:39 +0000878#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000879 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000880#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000881 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000882#else /* FREEBSD */
883 } else {
Roland McGrath553a6092002-12-16 20:40:39 +0000884 if (attaching < 2) {
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +0000885 /* We are attaching to an already running process.
886 * Try to figure out the state of the process in syscalls,
887 * to handle the first event well.
888 * This is done by having a look at the "wchan" property of the
889 * process, which tells where it is stopped (if it is). */
890 FILE * status;
891 char wchan[20]; /* should be enough */
Roland McGrath553a6092002-12-16 20:40:39 +0000892
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +0000893 sprintf(proc, "/proc/%d/status", tcp->pid);
894 status = fopen(proc, "r");
895 if (status &&
896 (fscanf(status, "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d"
897 "%*d,%*d %*d,%*d %19s", wchan) == 1) &&
898 strcmp(wchan, "nochan") && strcmp(wchan, "spread") &&
899 strcmp(wchan, "stopevent")) {
900 /* The process is asleep in the middle of a syscall.
901 Fake the syscall entry event */
902 tcp->flags &= ~(TCB_INSYSCALL|TCB_STARTUP);
903 tcp->status.PR_WHY = PR_SYSENTRY;
904 trace_syscall(tcp);
905 }
906 if (status)
907 fclose(status);
908 } /* otherwise it's a fork being followed */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000909 }
910#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000911#ifndef HAVE_POLLABLE_PROCFS
912 if (proc_poll_pipe[0] != -1)
913 proc_poller(tcp->pfd);
914 else if (nprocs > 1) {
915 proc_poll_open();
916 proc_poller(last_pfd);
917 proc_poller(tcp->pfd);
918 }
919 last_pfd = tcp->pfd;
920#endif /* !HAVE_POLLABLE_PROCFS */
921 return 0;
922}
923
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000924#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000925
926static struct tcb *
927pid2tcb(pid)
928int pid;
929{
930 int i;
931 struct tcb *tcp;
932
933 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
934 if (pid && tcp->pid != pid)
935 continue;
936 if (tcp->flags & TCB_INUSE)
937 return tcp;
938 }
939 return NULL;
940}
941
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000942#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000943
944static struct tcb *
945pfd2tcb(pfd)
946int pfd;
947{
948 int i;
949 struct tcb *tcp;
950
951 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
952 if (tcp->pfd != pfd)
953 continue;
954 if (tcp->flags & TCB_INUSE)
955 return tcp;
956 }
957 return NULL;
958}
959
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000960#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000961
962void
963droptcb(tcp)
964struct tcb *tcp;
965{
966 if (tcp->pid == 0)
967 return;
968 nprocs--;
969 tcp->pid = 0;
970 tcp->flags = 0;
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +0000971
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000972 if (tcp->pfd != -1) {
973 close(tcp->pfd);
974 tcp->pfd = -1;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000975#ifdef FREEBSD
976 if (tcp->pfd_reg != -1) {
977 close(tcp->pfd_reg);
978 tcp->pfd_reg = -1;
979 }
980 if (tcp->pfd_status != -1) {
981 close(tcp->pfd_status);
982 tcp->pfd_status = -1;
983 }
Roland McGrath553a6092002-12-16 20:40:39 +0000984#endif /* !FREEBSD */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000985#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000986 rebuild_pollv();
987#endif
988 }
989 if (tcp->parent != NULL) {
990 tcp->parent->nchildren--;
991 tcp->parent = NULL;
992 }
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +0000993
Wichert Akkerman822f0c92002-04-03 10:55:14 +0000994 if (outfname && followfork > 1 && tcp->outf)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000995 fclose(tcp->outf);
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +0000996
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000997 tcp->outf = 0;
998}
999
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001000#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001001
1002static int
1003resume(tcp)
1004struct tcb *tcp;
1005{
1006 if (tcp == NULL)
1007 return -1;
1008
1009 if (!(tcp->flags & TCB_SUSPENDED)) {
1010 fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid);
1011 return -1;
1012 }
1013 tcp->flags &= ~TCB_SUSPENDED;
1014
1015 if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) {
1016 perror("resume: ptrace(PTRACE_SYSCALL, ...)");
1017 return -1;
1018 }
1019
1020 if (!qflag)
1021 fprintf(stderr, "Process %u resumed\n", tcp->pid);
1022 return 0;
1023}
1024
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001025#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001026
1027/* detach traced process; continue with sig */
1028
1029static int
1030detach(tcp, sig)
1031struct tcb *tcp;
1032int sig;
1033{
1034 int error = 0;
1035#ifdef LINUX
1036 int status;
1037#endif
1038
1039 if (tcp->flags & TCB_BPTSET)
1040 sig = SIGKILL;
1041
1042#ifdef LINUX
1043 /*
1044 * Linux wrongly insists the child be stopped
Roland McGrath7bf10472002-12-16 20:42:50 +00001045 * before detaching. Arghh. We go through hoops
1046 * to make a clean break of things.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001047 */
Roland McGrath7bf10472002-12-16 20:42:50 +00001048#if defined(SPARC)
1049#undef PTRACE_DETACH
1050#define PTRACE_DETACH PTRACE_SUNDETACH
1051#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001052 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
1053 /* On a clear day, you can see forever. */
Roland McGrath7bf10472002-12-16 20:42:50 +00001054 }
1055 else if (errno != ESRCH) {
1056 /* Shouldn't happen. */
1057 perror("detach: ptrace(PTRACE_DETACH, ...)");
1058 }
1059 else if (kill(tcp->pid, 0) < 0) {
1060 if (errno != ESRCH)
1061 perror("detach: checking sanity");
1062 }
1063 else if (kill(tcp->pid, SIGSTOP) < 0) {
1064 if (errno != ESRCH)
1065 perror("detach: stopping child");
1066 }
1067 else {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001068 for (;;) {
Roland McGrath7bf10472002-12-16 20:42:50 +00001069 if (waitpid(tcp->pid, &status, 0) < 0) {
1070 if (errno != ECHILD)
Roland McGrath553a6092002-12-16 20:40:39 +00001071 perror("detach: waiting");
Roland McGrath7bf10472002-12-16 20:42:50 +00001072 break;
Roland McGrath553a6092002-12-16 20:40:39 +00001073 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001074 if (!WIFSTOPPED(status)) {
1075 /* Au revoir, mon ami. */
1076 break;
1077 }
1078 if (WSTOPSIG(status) == SIGSTOP) {
1079 if ((error = ptrace(PTRACE_DETACH,
Roland McGrath7bf10472002-12-16 20:42:50 +00001080 tcp->pid, (char *) 1, sig)) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001081 if (errno != ESRCH)
1082 perror("detach: ptrace(PTRACE_DETACH, ...)");
1083 /* I died trying. */
1084 }
1085 break;
1086 }
1087 if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1,
Roland McGrath7bf10472002-12-16 20:42:50 +00001088 WSTOPSIG(status) == SIGTRAP ?
1089 0 : WSTOPSIG(status))) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001090 if (errno != ESRCH)
1091 perror("detach: ptrace(PTRACE_CONT, ...)");
1092 break;
1093 }
1094 }
1095 }
Roland McGrath7bf10472002-12-16 20:42:50 +00001096#endif /* LINUX */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001097
1098#if defined(SUNOS4)
1099 /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
1100 if (sig && kill(tcp->pid, sig) < 0)
1101 perror("detach: kill");
1102 sig = 0;
1103 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0)
1104 perror("detach: ptrace(PTRACE_DETACH, ...)");
1105#endif /* SUNOS4 */
1106
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001107#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001108 if (waiting_parent(tcp))
1109 error = resume(tcp->parent);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001110#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001111
1112 if (!qflag)
1113 fprintf(stderr, "Process %u detached\n", tcp->pid);
1114
1115 droptcb(tcp);
1116 return error;
1117}
1118
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001119#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001120
1121static void
1122reaper(sig)
1123int sig;
1124{
1125 int pid;
1126 int status;
1127
1128 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
1129#if 0
1130 struct tcb *tcp;
1131
1132 tcp = pid2tcb(pid);
1133 if (tcp)
1134 droptcb(tcp);
1135#endif
1136 }
1137}
1138
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001139#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001140
1141static void
1142cleanup()
1143{
1144 int i;
1145 struct tcb *tcp;
1146
1147 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1148 if (!(tcp->flags & TCB_INUSE))
1149 continue;
1150 if (debug)
1151 fprintf(stderr,
1152 "cleanup: looking at pid %u\n", tcp->pid);
1153 if (tcp_last &&
1154 (!outfname || followfork < 2 || tcp_last == tcp)) {
1155 tprintf(" <unfinished ...>\n");
1156 tcp_last = NULL;
1157 }
1158 if (tcp->flags & TCB_ATTACHED)
1159 detach(tcp, 0);
1160 else {
1161 kill(tcp->pid, SIGCONT);
1162 kill(tcp->pid, SIGTERM);
1163 }
1164 }
1165 if (cflag)
1166 call_summary(outf);
1167}
1168
1169static void
1170interrupt(sig)
1171int sig;
1172{
1173 interrupted = 1;
1174}
1175
1176#ifndef HAVE_STRERROR
1177
1178#ifndef SYS_ERRLIST_DECLARED
1179extern int sys_nerr;
1180extern char *sys_errlist[];
1181#endif /* SYS_ERRLIST_DECLARED */
1182
1183const char *
1184strerror(errno)
1185int errno;
1186{
1187 static char buf[64];
1188
1189 if (errno < 1 || errno >= sys_nerr) {
1190 sprintf(buf, "Unknown error %d", errno);
1191 return buf;
1192 }
1193 return sys_errlist[errno];
1194}
1195
1196#endif /* HAVE_STERRROR */
1197
1198#ifndef HAVE_STRSIGNAL
1199
1200#ifndef SYS_SIGLIST_DECLARED
1201#ifdef HAVE__SYS_SIGLIST
1202 extern char *_sys_siglist[];
1203#else
1204 extern char *sys_siglist[];
1205#endif
1206#endif /* SYS_SIGLIST_DECLARED */
1207
1208const char *
1209strsignal(sig)
1210int sig;
1211{
1212 static char buf[64];
1213
1214 if (sig < 1 || sig >= NSIG) {
1215 sprintf(buf, "Unknown signal %d", sig);
1216 return buf;
1217 }
1218#ifdef HAVE__SYS_SIGLIST
1219 return _sys_siglist[sig];
1220#else
1221 return sys_siglist[sig];
1222#endif
1223}
1224
1225#endif /* HAVE_STRSIGNAL */
1226
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001227#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001228
1229static void
1230rebuild_pollv()
1231{
1232 int i, j;
1233 struct tcb *tcp;
1234
1235 for (i = j = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1236 if (!(tcp->flags & TCB_INUSE))
1237 continue;
1238 pollv[j].fd = tcp->pfd;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001239 pollv[j].events = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001240 j++;
1241 }
1242 if (j != nprocs) {
1243 fprintf(stderr, "strace: proc miscount\n");
1244 exit(1);
1245 }
1246}
1247
1248#ifndef HAVE_POLLABLE_PROCFS
1249
1250static void
1251proc_poll_open()
1252{
1253 int arg;
1254 int i;
1255
1256 if (pipe(proc_poll_pipe) < 0) {
1257 perror("pipe");
1258 exit(1);
1259 }
1260 for (i = 0; i < 2; i++) {
1261 if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) {
1262 perror("F_GETFD");
1263 exit(1);
1264 }
1265 if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) {
1266 perror("F_SETFD");
1267 exit(1);
1268 }
1269 }
1270}
1271
1272static int
1273proc_poll(pollv, nfds, timeout)
1274struct pollfd *pollv;
1275int nfds;
1276int timeout;
1277{
1278 int i;
1279 int n;
1280 struct proc_pollfd pollinfo;
1281
1282 if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
1283 return n;
1284 if (n != sizeof(struct proc_pollfd)) {
1285 fprintf(stderr, "panic: short read: %d\n", n);
1286 exit(1);
1287 }
1288 for (i = 0; i < nprocs; i++) {
1289 if (pollv[i].fd == pollinfo.fd)
1290 pollv[i].revents = pollinfo.revents;
1291 else
1292 pollv[i].revents = 0;
1293 }
1294 poller_pid = pollinfo.pid;
1295 return 1;
1296}
1297
1298static void
1299wakeup_handler(sig)
1300int sig;
1301{
1302}
1303
1304static void
1305proc_poller(pfd)
1306int pfd;
1307{
1308 struct proc_pollfd pollinfo;
1309 struct sigaction sa;
1310 sigset_t blocked_set, empty_set;
1311 int i;
1312 int n;
1313 struct rlimit rl;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001314#ifdef FREEBSD
1315 struct procfs_status pfs;
1316#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001317
1318 switch (fork()) {
1319 case -1:
1320 perror("fork");
1321 _exit(0);
1322 case 0:
1323 break;
1324 default:
1325 return;
1326 }
1327
1328 sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
1329 sa.sa_flags = 0;
1330 sigemptyset(&sa.sa_mask);
1331 sigaction(SIGHUP, &sa, NULL);
1332 sigaction(SIGINT, &sa, NULL);
1333 sigaction(SIGQUIT, &sa, NULL);
1334 sigaction(SIGPIPE, &sa, NULL);
1335 sigaction(SIGTERM, &sa, NULL);
1336 sa.sa_handler = wakeup_handler;
1337 sigaction(SIGUSR1, &sa, NULL);
1338 sigemptyset(&blocked_set);
1339 sigaddset(&blocked_set, SIGUSR1);
1340 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1341 sigemptyset(&empty_set);
1342
1343 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
1344 perror("getrlimit(RLIMIT_NOFILE, ...)");
1345 _exit(0);
1346 }
1347 n = rl.rlim_cur;
1348 for (i = 0; i < n; i++) {
1349 if (i != pfd && i != proc_poll_pipe[1])
1350 close(i);
1351 }
1352
1353 pollinfo.fd = pfd;
1354 pollinfo.pid = getpid();
1355 for (;;) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001356#ifndef FREEBSD
1357 if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
1358#else /* FREEBSD */
1359 if (ioctl(pfd, PIOCWSTOP, &pfs) < 0)
1360#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001361 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001362 switch (errno) {
1363 case EINTR:
1364 continue;
1365 case EBADF:
1366 pollinfo.revents = POLLERR;
1367 break;
1368 case ENOENT:
1369 pollinfo.revents = POLLHUP;
1370 break;
1371 default:
1372 perror("proc_poller: PIOCWSTOP");
1373 }
1374 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1375 _exit(0);
1376 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001377 pollinfo.revents = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001378 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1379 sigsuspend(&empty_set);
1380 }
1381}
1382
1383#endif /* !HAVE_POLLABLE_PROCFS */
1384
1385static int
1386choose_pfd()
1387{
1388 int i, j;
1389 struct tcb *tcp;
1390
1391 static int last;
1392
1393 if (followfork < 2 &&
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001394 last < nprocs && (pollv[last].revents & POLLWANT)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001395 /*
1396 * The previous process is ready to run again. We'll
1397 * let it do so if it is currently in a syscall. This
1398 * heuristic improves the readability of the trace.
1399 */
1400 tcp = pfd2tcb(pollv[last].fd);
1401 if (tcp && (tcp->flags & TCB_INSYSCALL))
1402 return pollv[last].fd;
1403 }
1404
1405 for (i = 0; i < nprocs; i++) {
1406 /* Let competing children run round robin. */
1407 j = (i + last + 1) % nprocs;
1408 if (pollv[j].revents & (POLLHUP | POLLERR)) {
1409 tcp = pfd2tcb(pollv[j].fd);
1410 if (!tcp) {
1411 fprintf(stderr, "strace: lost proc\n");
1412 exit(1);
1413 }
1414 droptcb(tcp);
1415 return -1;
1416 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001417 if (pollv[j].revents & POLLWANT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001418 last = j;
1419 return pollv[j].fd;
1420 }
1421 }
1422 fprintf(stderr, "strace: nothing ready\n");
1423 exit(1);
1424}
1425
1426static int
1427trace()
1428{
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001429#ifdef POLL_HACK
John Hughesd870b3c2002-05-21 11:24:18 +00001430 struct tcb *in_syscall = NULL;
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001431#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001432 struct tcb *tcp;
1433 int pfd;
1434 int what;
1435 int ioctl_result = 0, ioctl_errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001436 long arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001437
1438 for (;;) {
1439 if (interactive)
1440 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1441
1442 if (nprocs == 0)
1443 break;
1444
1445 switch (nprocs) {
1446 case 1:
1447#ifndef HAVE_POLLABLE_PROCFS
1448 if (proc_poll_pipe[0] == -1) {
1449#endif
1450 tcp = pid2tcb(0);
1451 if (!tcp)
1452 continue;
1453 pfd = tcp->pfd;
1454 if (pfd == -1)
1455 continue;
1456 break;
1457#ifndef HAVE_POLLABLE_PROCFS
1458 }
1459 /* fall through ... */
1460#endif /* !HAVE_POLLABLE_PROCFS */
1461 default:
1462#ifdef HAVE_POLLABLE_PROCFS
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001463#ifdef POLL_HACK
1464 /* On some systems (e.g. UnixWare) we get too much ugly
1465 "unfinished..." stuff when multiple proceses are in
1466 syscalls. Here's a nasty hack */
Roland McGrath553a6092002-12-16 20:40:39 +00001467
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001468 if (in_syscall) {
1469 struct pollfd pv;
1470 tcp = in_syscall;
1471 in_syscall = NULL;
1472 pv.fd = tcp->pfd;
1473 pv.events = POLLWANT;
1474 if ((what = poll (&pv, 1, 1)) < 0) {
1475 if (interrupted)
1476 return 0;
1477 continue;
1478 }
1479 else if (what == 1 && pv.revents & POLLWANT) {
1480 goto FOUND;
1481 }
1482 }
1483#endif
1484
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001485 if (poll(pollv, nprocs, INFTIM) < 0) {
1486 if (interrupted)
1487 return 0;
1488 continue;
1489 }
1490#else /* !HAVE_POLLABLE_PROCFS */
1491 if (proc_poll(pollv, nprocs, INFTIM) < 0) {
1492 if (interrupted)
1493 return 0;
1494 continue;
1495 }
1496#endif /* !HAVE_POLLABLE_PROCFS */
1497 pfd = choose_pfd();
1498 if (pfd == -1)
1499 continue;
1500 break;
1501 }
1502
1503 /* Look up `pfd' in our table. */
1504 if ((tcp = pfd2tcb(pfd)) == NULL) {
1505 fprintf(stderr, "unknown pfd: %u\n", pfd);
1506 exit(1);
1507 }
John Hughesb6643082002-05-23 11:02:22 +00001508#ifdef POLL_HACK
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001509 FOUND:
John Hughesb6643082002-05-23 11:02:22 +00001510#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001511 /* Get the status of the process. */
1512 if (!interrupted) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001513#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001514 ioctl_result = IOCTL_WSTOP (tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001515#else /* FREEBSD */
1516 /* Thanks to some scheduling mystery, the first poller
1517 sometimes waits for the already processed end of fork
1518 event. Doing a non blocking poll here solves the problem. */
1519 if (proc_poll_pipe[0] != -1)
1520 ioctl_result = IOCTL_STATUS (tcp);
1521 else
1522 ioctl_result = IOCTL_WSTOP (tcp);
Roland McGrath553a6092002-12-16 20:40:39 +00001523#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001524 ioctl_errno = errno;
1525#ifndef HAVE_POLLABLE_PROCFS
1526 if (proc_poll_pipe[0] != -1) {
1527 if (ioctl_result < 0)
1528 kill(poller_pid, SIGKILL);
1529 else
1530 kill(poller_pid, SIGUSR1);
1531 }
1532#endif /* !HAVE_POLLABLE_PROCFS */
1533 }
1534 if (interrupted)
1535 return 0;
1536
1537 if (interactive)
1538 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1539
1540 if (ioctl_result < 0) {
1541 /* Find out what happened if it failed. */
1542 switch (ioctl_errno) {
1543 case EINTR:
1544 case EBADF:
1545 continue;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001546#ifdef FREEBSD
1547 case ENOTTY:
Roland McGrath553a6092002-12-16 20:40:39 +00001548#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001549 case ENOENT:
1550 droptcb(tcp);
1551 continue;
1552 default:
1553 perror("PIOCWSTOP");
1554 exit(1);
1555 }
1556 }
1557
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +00001558#ifdef FREEBSD
1559 if ((tcp->flags & TCB_STARTUP) && (tcp->status.PR_WHY == PR_SYSEXIT)) {
1560 /* discard first event for a syscall we never entered */
1561 IOCTL (tcp->pfd, PIOCRUN, 0);
1562 continue;
1563 }
Roland McGrath553a6092002-12-16 20:40:39 +00001564#endif
1565
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001566 /* clear the just started flag */
1567 tcp->flags &= ~TCB_STARTUP;
1568
1569 /* set current output file */
1570 outf = tcp->outf;
1571
1572 if (cflag) {
1573 struct timeval stime;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001574#ifdef FREEBSD
1575 char buf[1024];
1576 int len;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001577
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001578 if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) {
1579 buf[len] = '\0';
1580 sscanf(buf,
1581 "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld",
1582 &stime.tv_sec, &stime.tv_usec);
1583 } else
1584 stime.tv_sec = stime.tv_usec = 0;
Roland McGrath553a6092002-12-16 20:40:39 +00001585#else /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001586 stime.tv_sec = tcp->status.pr_stime.tv_sec;
1587 stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001588#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001589 tv_sub(&tcp->dtime, &stime, &tcp->stime);
1590 tcp->stime = stime;
1591 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001592 what = tcp->status.PR_WHAT;
1593 switch (tcp->status.PR_WHY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001594#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001595 case PR_REQUESTED:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001596 if (tcp->status.PR_FLAGS & PR_ASLEEP) {
1597 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001598 if (trace_syscall(tcp) < 0) {
1599 fprintf(stderr, "syscall trouble\n");
1600 exit(1);
1601 }
1602 }
1603 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001604#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001605 case PR_SYSENTRY:
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001606#ifdef POLL_HACK
1607 in_syscall = tcp;
1608#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001609 case PR_SYSEXIT:
1610 if (trace_syscall(tcp) < 0) {
1611 fprintf(stderr, "syscall trouble\n");
1612 exit(1);
1613 }
1614 break;
1615 case PR_SIGNALLED:
1616 if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) {
1617 printleader(tcp);
1618 tprintf("--- %s (%s) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001619 signame(what), strsignal(what));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001620 printtrailer(tcp);
John Hughes58265892001-10-18 15:13:53 +00001621#ifdef PR_INFO
1622 if (tcp->status.PR_INFO.si_signo == what) {
1623 printleader(tcp);
1624 tprintf(" siginfo=");
1625 printsiginfo(&tcp->status.PR_INFO, 1);
1626 printtrailer(tcp);
1627 }
1628#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001629 }
1630 break;
1631 case PR_FAULTED:
1632 if (!cflag && (qual_flags[what] & QUAL_FAULT)) {
1633 printleader(tcp);
1634 tprintf("=== FAULT %d ===", what);
1635 printtrailer(tcp);
1636 }
1637 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001638#ifdef FREEBSD
1639 case 0: /* handle case we polled for nothing */
1640 continue;
Roland McGrath553a6092002-12-16 20:40:39 +00001641#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001642 default:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001643 fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001644 exit(1);
1645 break;
1646 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001647 arg = 0;
Roland McGrath553a6092002-12-16 20:40:39 +00001648#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001649 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Roland McGrath553a6092002-12-16 20:40:39 +00001650#else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001651 if (IOCTL (tcp->pfd, PIOCRUN, 0) < 0) {
Roland McGrath553a6092002-12-16 20:40:39 +00001652#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001653 perror("PIOCRUN");
1654 exit(1);
1655 }
1656 }
1657 return 0;
1658}
1659
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001660#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001661
1662static int
1663trace()
1664{
1665 int pid;
1666 int wait_errno;
1667 int status;
1668 struct tcb *tcp;
1669#ifdef LINUX
1670 struct rusage ru;
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001671#ifdef __WALL
1672 static int wait4_options = __WALL;
1673#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001674#endif /* LINUX */
1675
1676 while (nprocs != 0) {
1677 if (interactive)
1678 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1679#ifdef LINUX
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001680#ifdef __WALL
1681 pid = wait4(-1, &status, wait4_options, cflag ? &ru : NULL);
1682 if ((wait4_options & __WALL) && errno == EINVAL) {
1683 /* this kernel does not support __WALL */
1684 wait4_options &= ~__WALL;
1685 errno = 0;
1686 pid = wait4(-1, &status, wait4_options,
1687 cflag ? &ru : NULL);
1688 }
1689 if (!(wait4_options & __WALL) && errno == ECHILD) {
1690 /* most likely a "cloned" process */
1691 pid = wait4(-1, &status, __WCLONE,
1692 cflag ? &ru : NULL);
1693 if (pid == -1) {
1694 fprintf(stderr, "strace: clone wait4 "
1695 "failed: %s\n", strerror(errno));
1696 }
1697 }
1698#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001699 pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001700#endif /* __WALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001701#endif /* LINUX */
1702#ifdef SUNOS4
1703 pid = wait(&status);
1704#endif /* SUNOS4 */
1705 wait_errno = errno;
1706 if (interactive)
1707 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1708
1709 if (interrupted)
1710 return 0;
1711
1712 if (pid == -1) {
1713 switch (wait_errno) {
1714 case EINTR:
1715 continue;
1716 case ECHILD:
1717 /*
1718 * We would like to verify this case
1719 * but sometimes a race in Solbourne's
1720 * version of SunOS sometimes reports
1721 * ECHILD before sending us SIGCHILD.
1722 */
1723#if 0
1724 if (nprocs == 0)
1725 return 0;
1726 fprintf(stderr, "strace: proc miscount\n");
1727 exit(1);
1728#endif
1729 return 0;
1730 default:
1731 errno = wait_errno;
1732 perror("strace: wait");
1733 return -1;
1734 }
1735 }
1736 if (debug)
1737 fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
1738
1739 /* Look up `pid' in our table. */
1740 if ((tcp = pid2tcb(pid)) == NULL) {
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001741#if 0 /* XXX davidm */ /* WTA: disabled again */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001742 struct tcb *tcpchild;
1743
1744 if ((tcpchild = alloctcb(pid)) == NULL) {
1745 fprintf(stderr, " [tcb table full]\n");
1746 kill(pid, SIGKILL); /* XXX */
1747 return 0;
1748 }
1749 tcpchild->flags |= TCB_ATTACHED;
1750 newoutf(tcpchild);
1751 tcp->nchildren++;
1752 if (!qflag)
1753 fprintf(stderr, "Process %d attached\n", pid);
1754#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001755 fprintf(stderr, "unknown pid: %u\n", pid);
1756 if (WIFSTOPPED(status))
1757 ptrace(PTRACE_CONT, pid, (char *) 1, 0);
1758 exit(1);
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001759#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001760 }
1761 /* set current output file */
1762 outf = tcp->outf;
1763 if (cflag) {
1764#ifdef LINUX
1765 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
1766 tcp->stime = ru.ru_stime;
1767#endif /* !LINUX */
1768 }
1769
1770 if (tcp->flags & TCB_SUSPENDED) {
1771 /*
1772 * Apparently, doing any ptrace() call on a stopped
1773 * process, provokes the kernel to report the process
1774 * status again on a subsequent wait(), even if the
1775 * process has not been actually restarted.
1776 * Since we have inspected the arguments of suspended
1777 * processes we end up here testing for this case.
1778 */
1779 continue;
1780 }
1781 if (WIFSIGNALED(status)) {
1782 if (!cflag
1783 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
1784 printleader(tcp);
1785 tprintf("+++ killed by %s +++",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001786 signame(WTERMSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001787 printtrailer(tcp);
1788 }
1789 droptcb(tcp);
1790 continue;
1791 }
1792 if (WIFEXITED(status)) {
1793 if (debug)
1794 fprintf(stderr, "pid %u exited\n", pid);
1795 if (tcp->flags & TCB_ATTACHED)
1796 fprintf(stderr,
1797 "PANIC: attached pid %u exited\n",
1798 pid);
1799 droptcb(tcp);
1800 continue;
1801 }
1802 if (!WIFSTOPPED(status)) {
1803 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
1804 droptcb(tcp);
1805 continue;
1806 }
1807 if (debug)
1808 fprintf(stderr, "pid %u stopped, [%s]\n",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001809 pid, signame(WSTOPSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001810
1811 if (tcp->flags & TCB_STARTUP) {
1812 /*
1813 * This flag is there to keep us in sync.
1814 * Next time this process stops it should
1815 * really be entering a system call.
1816 */
1817 tcp->flags &= ~TCB_STARTUP;
1818 if (tcp->flags & TCB_ATTACHED) {
1819 /*
1820 * Interestingly, the process may stop
1821 * with STOPSIG equal to some other signal
1822 * than SIGSTOP if we happend to attach
1823 * just before the process takes a signal.
1824 */
1825 if (!WIFSTOPPED(status)) {
1826 fprintf(stderr,
1827 "pid %u not stopped\n", pid);
1828 detach(tcp, WSTOPSIG(status));
1829 continue;
1830 }
1831 }
1832 else {
1833#ifdef SUNOS4
1834 /* A child of us stopped at exec */
1835 if (WSTOPSIG(status) == SIGTRAP && followvfork)
1836 fixvfork(tcp);
1837#endif /* SUNOS4 */
1838 }
1839 if (tcp->flags & TCB_BPTSET) {
1840 if (clearbpt(tcp) < 0) /* Pretty fatal */ {
1841 droptcb(tcp);
1842 cleanup();
1843 return -1;
1844 }
1845 }
1846 goto tracing;
1847 }
1848
1849 if (WSTOPSIG(status) != SIGTRAP) {
1850 if (WSTOPSIG(status) == SIGSTOP &&
1851 (tcp->flags & TCB_SIGTRAPPED)) {
1852 /*
1853 * Trapped attempt to block SIGTRAP
1854 * Hope we are back in control now.
1855 */
1856 tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
1857 if (ptrace(PTRACE_SYSCALL,
1858 pid, (char *) 1, 0) < 0) {
1859 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1860 cleanup();
1861 return -1;
1862 }
1863 continue;
1864 }
1865 if (!cflag
1866 && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001867 unsigned long addr = 0, pc = 0;
1868#ifdef PT_GETSIGINFO
1869# define PSR_RI 41
1870 struct siginfo si;
1871 unsigned long psr;
1872
1873 upeek(pid, PT_CR_IPSR, &psr);
1874 upeek(pid, PT_CR_IIP, &pc);
1875
1876 pc += (psr >> PSR_RI) & 0x3;
1877 ptrace(PT_GETSIGINFO, pid, 0, (long) &si);
1878 addr = (unsigned long) si.si_addr;
1879#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001880 printleader(tcp);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001881 tprintf("--- %s (%s) @ %lx (%lx) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001882 signame(WSTOPSIG(status)),
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001883 strsignal(WSTOPSIG(status)), pc, addr);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001884 printtrailer(tcp);
1885 }
1886 if ((tcp->flags & TCB_ATTACHED) &&
1887 !sigishandled(tcp, WSTOPSIG(status))) {
1888 detach(tcp, WSTOPSIG(status));
1889 continue;
1890 }
1891 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1,
1892 WSTOPSIG(status)) < 0) {
1893 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1894 cleanup();
1895 return -1;
1896 }
1897 tcp->flags &= ~TCB_SUSPENDED;
1898 continue;
1899 }
1900 if (trace_syscall(tcp) < 0) {
1901 if (tcp->flags & TCB_ATTACHED)
1902 detach(tcp, 0);
1903 else {
1904 ptrace(PTRACE_KILL,
1905 tcp->pid, (char *) 1, SIGTERM);
1906 droptcb(tcp);
1907 }
1908 continue;
1909 }
1910 if (tcp->flags & TCB_EXITING) {
1911 if (tcp->flags & TCB_ATTACHED)
1912 detach(tcp, 0);
1913 else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) {
1914 perror("strace: ptrace(PTRACE_CONT, ...)");
1915 cleanup();
1916 return -1;
1917 }
1918 continue;
1919 }
1920 if (tcp->flags & TCB_SUSPENDED) {
1921 if (!qflag)
1922 fprintf(stderr, "Process %u suspended\n", pid);
1923 continue;
1924 }
1925 tracing:
1926 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) {
1927 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1928 cleanup();
1929 return -1;
1930 }
1931 }
1932 return 0;
1933}
1934
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001935#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001936
1937static int curcol;
1938
1939#ifdef __STDC__
1940#include <stdarg.h>
1941#define VA_START(a, b) va_start(a, b)
1942#else
1943#include <varargs.h>
1944#define VA_START(a, b) va_start(a)
1945#endif
1946
1947void
1948#ifdef __STDC__
1949tprintf(const char *fmt, ...)
1950#else
1951tprintf(fmt, va_alist)
1952char *fmt;
1953va_dcl
1954#endif
1955{
1956 va_list args;
1957
1958 VA_START(args, fmt);
1959 if (outf)
1960 curcol += vfprintf(outf, fmt, args);
1961 va_end(args);
1962 return;
1963}
1964
1965void
1966printleader(tcp)
1967struct tcb *tcp;
1968{
1969 if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) {
1970 tcp_last->flags |= TCB_REPRINT;
1971 tprintf(" <unfinished ...>\n");
1972 }
1973 curcol = 0;
1974 if ((followfork == 1 || pflag_seen > 1) && outfname)
1975 tprintf("%-5d ", tcp->pid);
1976 else if (nprocs > 1 && !outfname)
1977 tprintf("[pid %5u] ", tcp->pid);
1978 if (tflag) {
1979 char str[sizeof("HH:MM:SS")];
1980 struct timeval tv, dtv;
1981 static struct timeval otv;
1982
1983 gettimeofday(&tv, NULL);
1984 if (rflag) {
1985 if (otv.tv_sec == 0)
1986 otv = tv;
1987 tv_sub(&dtv, &tv, &otv);
1988 tprintf("%6ld.%06ld ",
1989 (long) dtv.tv_sec, (long) dtv.tv_usec);
1990 otv = tv;
1991 }
1992 else if (tflag > 2) {
1993 tprintf("%ld.%06ld ",
1994 (long) tv.tv_sec, (long) tv.tv_usec);
1995 }
1996 else {
1997 time_t local = tv.tv_sec;
1998 strftime(str, sizeof(str), "%T", localtime(&local));
1999 if (tflag > 1)
2000 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
2001 else
2002 tprintf("%s ", str);
2003 }
2004 }
2005 if (iflag)
2006 printcall(tcp);
2007}
2008
2009void
2010tabto(col)
2011int col;
2012{
2013 if (curcol < col)
2014 tprintf("%*s", col - curcol, "");
2015}
2016
2017void
2018printtrailer(tcp)
2019struct tcb *tcp;
2020{
2021 tprintf("\n");
2022 tcp_last = NULL;
2023}
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002024
Wichert Akkermanea78f0f1999-11-29 15:34:02 +00002025#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002026
2027int mp_ioctl (int fd, int cmd, void *arg, int size) {
2028
2029 struct iovec iov[2];
2030 int n = 1;
Roland McGrath553a6092002-12-16 20:40:39 +00002031
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002032 iov[0].iov_base = &cmd;
2033 iov[0].iov_len = sizeof cmd;
2034 if (arg) {
2035 ++n;
2036 iov[1].iov_base = arg;
2037 iov[1].iov_len = size;
2038 }
Roland McGrath553a6092002-12-16 20:40:39 +00002039
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002040 return writev (fd, iov, n);
2041}
2042
2043#endif