blob: ea9439bbb318e78382ac3bde8ff9a6fea42357ae [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 McGrath553a6092002-12-16 20:40:39 +00001045 * before detaching. This creates numerous headaches
1046 * as the process we are tracing may be running.
1047 *
1048 * First try to simply detach from the process; if it was
1049 * already stopped, this will succeed and we're done.
1050 *
1051 * Otherwise stop the process by sending it a SIGSTOP
1052 * signal.
1053 *
1054 * Once the process is stopped we have to make sure it
1055 * received the SIGSTOP (it may have received a SIGTRAP or
1056 * other signal). If it did not receive the SIGSTOP,
1057 * restart the process and try again.
1058 *
1059 * Once stopped with a SIGSTOP, we can detach from the
1060 * process via PTRACE_DETACH.
1061 *
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001062 */
Roland McGrath553a6092002-12-16 20:40:39 +00001063
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001064 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
1065 /* On a clear day, you can see forever. */
Roland McGrath553a6092002-12-16 20:40:39 +00001066 } else {
1067
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001068 for (;;) {
Roland McGrath553a6092002-12-16 20:40:39 +00001069 if (kill(tcp->pid, 0) < 0) {
1070 if (errno != ESRCH)
1071 perror("detach: checking sanity");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001072 }
Roland McGrath553a6092002-12-16 20:40:39 +00001073 else if (kill(tcp->pid, SIGSTOP) < 0) {
1074 if (errno != ESRCH)
1075 perror("detach: stopping child");
1076 }
1077
1078 /*
1079 * At this point the child should be stopped. Try to
1080 * wait on it so we can get its stop status. Use WNOHANG
1081 * to avoid this wait hanging.
1082 */
1083 if (waitpid (tcp->pid, &status, (WUNTRACED | WNOHANG)) < 0) {
1084 if (errno != ECHILD) {
1085 perror("detach: waiting");
1086 } else {
1087
1088 /*
1089 * Try again, this time with the __WCLONE
1090 * flag. Note we may get notifications
1091 * for other processes/threads!
1092 */
1093 errno = 0;
1094 while (1) {
1095 int x;
1096
1097 x = waitpid (-1, &status, __WCLONE);
1098 if (x == tcp->pid || x < 0 || errno != 0)
1099 break;
1100 }
1101 }
1102
1103 if (errno) {
1104 perror ("Unable to wait on inferior");
1105 return -1;
1106 }
1107 }
1108
1109 /*
1110 * At this point we have wait status for the
1111 * inferior. If it did not stop, then all
1112 * bets are off.
1113 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001114 if (!WIFSTOPPED(status)) {
1115 /* Au revoir, mon ami. */
1116 break;
1117 }
Roland McGrath553a6092002-12-16 20:40:39 +00001118
1119 /*
1120 * If the process/thread has stopped with a
1121 * SIGSTOP, then we can continue and detach
1122 * with PTRACE_DETACH.
1123 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001124 if (WSTOPSIG(status) == SIGSTOP) {
1125 if ((error = ptrace(PTRACE_DETACH,
Roland McGrath553a6092002-12-16 20:40:39 +00001126 tcp->pid, (char *) 1, sig)) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001127 if (errno != ESRCH)
1128 perror("detach: ptrace(PTRACE_DETACH, ...)");
1129 /* I died trying. */
1130 }
1131 break;
1132 }
Roland McGrath553a6092002-12-16 20:40:39 +00001133
1134 /*
1135 * The process/thread did not stop with a SIGSTOP,
1136 * so let it continue and try again to stop it with
1137 * a SIGSTOP.
1138 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001139 if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1,
Roland McGrath553a6092002-12-16 20:40:39 +00001140 WSTOPSIG(status) == SIGTRAP ?
1141 0 : WSTOPSIG(status))) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001142 if (errno != ESRCH)
1143 perror("detach: ptrace(PTRACE_CONT, ...)");
1144 break;
1145 }
1146 }
1147 }
Roland McGrath553a6092002-12-16 20:40:39 +00001148#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001149
1150#if defined(SUNOS4)
1151 /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
1152 if (sig && kill(tcp->pid, sig) < 0)
1153 perror("detach: kill");
1154 sig = 0;
1155 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0)
1156 perror("detach: ptrace(PTRACE_DETACH, ...)");
1157#endif /* SUNOS4 */
1158
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001159#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001160 if (waiting_parent(tcp))
1161 error = resume(tcp->parent);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001162#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001163
1164 if (!qflag)
1165 fprintf(stderr, "Process %u detached\n", tcp->pid);
1166
1167 droptcb(tcp);
1168 return error;
1169}
1170
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001171#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001172
1173static void
1174reaper(sig)
1175int sig;
1176{
1177 int pid;
1178 int status;
1179
1180 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
1181#if 0
1182 struct tcb *tcp;
1183
1184 tcp = pid2tcb(pid);
1185 if (tcp)
1186 droptcb(tcp);
1187#endif
1188 }
1189}
1190
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001191#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001192
1193static void
1194cleanup()
1195{
1196 int i;
1197 struct tcb *tcp;
1198
1199 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1200 if (!(tcp->flags & TCB_INUSE))
1201 continue;
1202 if (debug)
1203 fprintf(stderr,
1204 "cleanup: looking at pid %u\n", tcp->pid);
1205 if (tcp_last &&
1206 (!outfname || followfork < 2 || tcp_last == tcp)) {
1207 tprintf(" <unfinished ...>\n");
1208 tcp_last = NULL;
1209 }
1210 if (tcp->flags & TCB_ATTACHED)
1211 detach(tcp, 0);
1212 else {
1213 kill(tcp->pid, SIGCONT);
1214 kill(tcp->pid, SIGTERM);
1215 }
1216 }
1217 if (cflag)
1218 call_summary(outf);
1219}
1220
1221static void
1222interrupt(sig)
1223int sig;
1224{
1225 interrupted = 1;
1226}
1227
1228#ifndef HAVE_STRERROR
1229
1230#ifndef SYS_ERRLIST_DECLARED
1231extern int sys_nerr;
1232extern char *sys_errlist[];
1233#endif /* SYS_ERRLIST_DECLARED */
1234
1235const char *
1236strerror(errno)
1237int errno;
1238{
1239 static char buf[64];
1240
1241 if (errno < 1 || errno >= sys_nerr) {
1242 sprintf(buf, "Unknown error %d", errno);
1243 return buf;
1244 }
1245 return sys_errlist[errno];
1246}
1247
1248#endif /* HAVE_STERRROR */
1249
1250#ifndef HAVE_STRSIGNAL
1251
1252#ifndef SYS_SIGLIST_DECLARED
1253#ifdef HAVE__SYS_SIGLIST
1254 extern char *_sys_siglist[];
1255#else
1256 extern char *sys_siglist[];
1257#endif
1258#endif /* SYS_SIGLIST_DECLARED */
1259
1260const char *
1261strsignal(sig)
1262int sig;
1263{
1264 static char buf[64];
1265
1266 if (sig < 1 || sig >= NSIG) {
1267 sprintf(buf, "Unknown signal %d", sig);
1268 return buf;
1269 }
1270#ifdef HAVE__SYS_SIGLIST
1271 return _sys_siglist[sig];
1272#else
1273 return sys_siglist[sig];
1274#endif
1275}
1276
1277#endif /* HAVE_STRSIGNAL */
1278
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001279#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001280
1281static void
1282rebuild_pollv()
1283{
1284 int i, j;
1285 struct tcb *tcp;
1286
1287 for (i = j = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1288 if (!(tcp->flags & TCB_INUSE))
1289 continue;
1290 pollv[j].fd = tcp->pfd;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001291 pollv[j].events = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001292 j++;
1293 }
1294 if (j != nprocs) {
1295 fprintf(stderr, "strace: proc miscount\n");
1296 exit(1);
1297 }
1298}
1299
1300#ifndef HAVE_POLLABLE_PROCFS
1301
1302static void
1303proc_poll_open()
1304{
1305 int arg;
1306 int i;
1307
1308 if (pipe(proc_poll_pipe) < 0) {
1309 perror("pipe");
1310 exit(1);
1311 }
1312 for (i = 0; i < 2; i++) {
1313 if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) {
1314 perror("F_GETFD");
1315 exit(1);
1316 }
1317 if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) {
1318 perror("F_SETFD");
1319 exit(1);
1320 }
1321 }
1322}
1323
1324static int
1325proc_poll(pollv, nfds, timeout)
1326struct pollfd *pollv;
1327int nfds;
1328int timeout;
1329{
1330 int i;
1331 int n;
1332 struct proc_pollfd pollinfo;
1333
1334 if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
1335 return n;
1336 if (n != sizeof(struct proc_pollfd)) {
1337 fprintf(stderr, "panic: short read: %d\n", n);
1338 exit(1);
1339 }
1340 for (i = 0; i < nprocs; i++) {
1341 if (pollv[i].fd == pollinfo.fd)
1342 pollv[i].revents = pollinfo.revents;
1343 else
1344 pollv[i].revents = 0;
1345 }
1346 poller_pid = pollinfo.pid;
1347 return 1;
1348}
1349
1350static void
1351wakeup_handler(sig)
1352int sig;
1353{
1354}
1355
1356static void
1357proc_poller(pfd)
1358int pfd;
1359{
1360 struct proc_pollfd pollinfo;
1361 struct sigaction sa;
1362 sigset_t blocked_set, empty_set;
1363 int i;
1364 int n;
1365 struct rlimit rl;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001366#ifdef FREEBSD
1367 struct procfs_status pfs;
1368#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001369
1370 switch (fork()) {
1371 case -1:
1372 perror("fork");
1373 _exit(0);
1374 case 0:
1375 break;
1376 default:
1377 return;
1378 }
1379
1380 sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
1381 sa.sa_flags = 0;
1382 sigemptyset(&sa.sa_mask);
1383 sigaction(SIGHUP, &sa, NULL);
1384 sigaction(SIGINT, &sa, NULL);
1385 sigaction(SIGQUIT, &sa, NULL);
1386 sigaction(SIGPIPE, &sa, NULL);
1387 sigaction(SIGTERM, &sa, NULL);
1388 sa.sa_handler = wakeup_handler;
1389 sigaction(SIGUSR1, &sa, NULL);
1390 sigemptyset(&blocked_set);
1391 sigaddset(&blocked_set, SIGUSR1);
1392 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1393 sigemptyset(&empty_set);
1394
1395 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
1396 perror("getrlimit(RLIMIT_NOFILE, ...)");
1397 _exit(0);
1398 }
1399 n = rl.rlim_cur;
1400 for (i = 0; i < n; i++) {
1401 if (i != pfd && i != proc_poll_pipe[1])
1402 close(i);
1403 }
1404
1405 pollinfo.fd = pfd;
1406 pollinfo.pid = getpid();
1407 for (;;) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001408#ifndef FREEBSD
1409 if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
1410#else /* FREEBSD */
1411 if (ioctl(pfd, PIOCWSTOP, &pfs) < 0)
1412#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001413 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001414 switch (errno) {
1415 case EINTR:
1416 continue;
1417 case EBADF:
1418 pollinfo.revents = POLLERR;
1419 break;
1420 case ENOENT:
1421 pollinfo.revents = POLLHUP;
1422 break;
1423 default:
1424 perror("proc_poller: PIOCWSTOP");
1425 }
1426 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1427 _exit(0);
1428 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001429 pollinfo.revents = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001430 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1431 sigsuspend(&empty_set);
1432 }
1433}
1434
1435#endif /* !HAVE_POLLABLE_PROCFS */
1436
1437static int
1438choose_pfd()
1439{
1440 int i, j;
1441 struct tcb *tcp;
1442
1443 static int last;
1444
1445 if (followfork < 2 &&
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001446 last < nprocs && (pollv[last].revents & POLLWANT)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001447 /*
1448 * The previous process is ready to run again. We'll
1449 * let it do so if it is currently in a syscall. This
1450 * heuristic improves the readability of the trace.
1451 */
1452 tcp = pfd2tcb(pollv[last].fd);
1453 if (tcp && (tcp->flags & TCB_INSYSCALL))
1454 return pollv[last].fd;
1455 }
1456
1457 for (i = 0; i < nprocs; i++) {
1458 /* Let competing children run round robin. */
1459 j = (i + last + 1) % nprocs;
1460 if (pollv[j].revents & (POLLHUP | POLLERR)) {
1461 tcp = pfd2tcb(pollv[j].fd);
1462 if (!tcp) {
1463 fprintf(stderr, "strace: lost proc\n");
1464 exit(1);
1465 }
1466 droptcb(tcp);
1467 return -1;
1468 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001469 if (pollv[j].revents & POLLWANT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001470 last = j;
1471 return pollv[j].fd;
1472 }
1473 }
1474 fprintf(stderr, "strace: nothing ready\n");
1475 exit(1);
1476}
1477
1478static int
1479trace()
1480{
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001481#ifdef POLL_HACK
John Hughesd870b3c2002-05-21 11:24:18 +00001482 struct tcb *in_syscall = NULL;
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001483#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001484 struct tcb *tcp;
1485 int pfd;
1486 int what;
1487 int ioctl_result = 0, ioctl_errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001488 long arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001489
1490 for (;;) {
1491 if (interactive)
1492 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1493
1494 if (nprocs == 0)
1495 break;
1496
1497 switch (nprocs) {
1498 case 1:
1499#ifndef HAVE_POLLABLE_PROCFS
1500 if (proc_poll_pipe[0] == -1) {
1501#endif
1502 tcp = pid2tcb(0);
1503 if (!tcp)
1504 continue;
1505 pfd = tcp->pfd;
1506 if (pfd == -1)
1507 continue;
1508 break;
1509#ifndef HAVE_POLLABLE_PROCFS
1510 }
1511 /* fall through ... */
1512#endif /* !HAVE_POLLABLE_PROCFS */
1513 default:
1514#ifdef HAVE_POLLABLE_PROCFS
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001515#ifdef POLL_HACK
1516 /* On some systems (e.g. UnixWare) we get too much ugly
1517 "unfinished..." stuff when multiple proceses are in
1518 syscalls. Here's a nasty hack */
Roland McGrath553a6092002-12-16 20:40:39 +00001519
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001520 if (in_syscall) {
1521 struct pollfd pv;
1522 tcp = in_syscall;
1523 in_syscall = NULL;
1524 pv.fd = tcp->pfd;
1525 pv.events = POLLWANT;
1526 if ((what = poll (&pv, 1, 1)) < 0) {
1527 if (interrupted)
1528 return 0;
1529 continue;
1530 }
1531 else if (what == 1 && pv.revents & POLLWANT) {
1532 goto FOUND;
1533 }
1534 }
1535#endif
1536
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001537 if (poll(pollv, nprocs, INFTIM) < 0) {
1538 if (interrupted)
1539 return 0;
1540 continue;
1541 }
1542#else /* !HAVE_POLLABLE_PROCFS */
1543 if (proc_poll(pollv, nprocs, INFTIM) < 0) {
1544 if (interrupted)
1545 return 0;
1546 continue;
1547 }
1548#endif /* !HAVE_POLLABLE_PROCFS */
1549 pfd = choose_pfd();
1550 if (pfd == -1)
1551 continue;
1552 break;
1553 }
1554
1555 /* Look up `pfd' in our table. */
1556 if ((tcp = pfd2tcb(pfd)) == NULL) {
1557 fprintf(stderr, "unknown pfd: %u\n", pfd);
1558 exit(1);
1559 }
John Hughesb6643082002-05-23 11:02:22 +00001560#ifdef POLL_HACK
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001561 FOUND:
John Hughesb6643082002-05-23 11:02:22 +00001562#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001563 /* Get the status of the process. */
1564 if (!interrupted) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001565#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001566 ioctl_result = IOCTL_WSTOP (tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001567#else /* FREEBSD */
1568 /* Thanks to some scheduling mystery, the first poller
1569 sometimes waits for the already processed end of fork
1570 event. Doing a non blocking poll here solves the problem. */
1571 if (proc_poll_pipe[0] != -1)
1572 ioctl_result = IOCTL_STATUS (tcp);
1573 else
1574 ioctl_result = IOCTL_WSTOP (tcp);
Roland McGrath553a6092002-12-16 20:40:39 +00001575#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001576 ioctl_errno = errno;
1577#ifndef HAVE_POLLABLE_PROCFS
1578 if (proc_poll_pipe[0] != -1) {
1579 if (ioctl_result < 0)
1580 kill(poller_pid, SIGKILL);
1581 else
1582 kill(poller_pid, SIGUSR1);
1583 }
1584#endif /* !HAVE_POLLABLE_PROCFS */
1585 }
1586 if (interrupted)
1587 return 0;
1588
1589 if (interactive)
1590 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1591
1592 if (ioctl_result < 0) {
1593 /* Find out what happened if it failed. */
1594 switch (ioctl_errno) {
1595 case EINTR:
1596 case EBADF:
1597 continue;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001598#ifdef FREEBSD
1599 case ENOTTY:
Roland McGrath553a6092002-12-16 20:40:39 +00001600#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001601 case ENOENT:
1602 droptcb(tcp);
1603 continue;
1604 default:
1605 perror("PIOCWSTOP");
1606 exit(1);
1607 }
1608 }
1609
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +00001610#ifdef FREEBSD
1611 if ((tcp->flags & TCB_STARTUP) && (tcp->status.PR_WHY == PR_SYSEXIT)) {
1612 /* discard first event for a syscall we never entered */
1613 IOCTL (tcp->pfd, PIOCRUN, 0);
1614 continue;
1615 }
Roland McGrath553a6092002-12-16 20:40:39 +00001616#endif
1617
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001618 /* clear the just started flag */
1619 tcp->flags &= ~TCB_STARTUP;
1620
1621 /* set current output file */
1622 outf = tcp->outf;
1623
1624 if (cflag) {
1625 struct timeval stime;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001626#ifdef FREEBSD
1627 char buf[1024];
1628 int len;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001629
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001630 if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) {
1631 buf[len] = '\0';
1632 sscanf(buf,
1633 "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld",
1634 &stime.tv_sec, &stime.tv_usec);
1635 } else
1636 stime.tv_sec = stime.tv_usec = 0;
Roland McGrath553a6092002-12-16 20:40:39 +00001637#else /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001638 stime.tv_sec = tcp->status.pr_stime.tv_sec;
1639 stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001640#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001641 tv_sub(&tcp->dtime, &stime, &tcp->stime);
1642 tcp->stime = stime;
1643 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001644 what = tcp->status.PR_WHAT;
1645 switch (tcp->status.PR_WHY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001646#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001647 case PR_REQUESTED:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001648 if (tcp->status.PR_FLAGS & PR_ASLEEP) {
1649 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001650 if (trace_syscall(tcp) < 0) {
1651 fprintf(stderr, "syscall trouble\n");
1652 exit(1);
1653 }
1654 }
1655 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001656#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001657 case PR_SYSENTRY:
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001658#ifdef POLL_HACK
1659 in_syscall = tcp;
1660#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001661 case PR_SYSEXIT:
1662 if (trace_syscall(tcp) < 0) {
1663 fprintf(stderr, "syscall trouble\n");
1664 exit(1);
1665 }
1666 break;
1667 case PR_SIGNALLED:
1668 if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) {
1669 printleader(tcp);
1670 tprintf("--- %s (%s) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001671 signame(what), strsignal(what));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001672 printtrailer(tcp);
John Hughes58265892001-10-18 15:13:53 +00001673#ifdef PR_INFO
1674 if (tcp->status.PR_INFO.si_signo == what) {
1675 printleader(tcp);
1676 tprintf(" siginfo=");
1677 printsiginfo(&tcp->status.PR_INFO, 1);
1678 printtrailer(tcp);
1679 }
1680#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001681 }
1682 break;
1683 case PR_FAULTED:
1684 if (!cflag && (qual_flags[what] & QUAL_FAULT)) {
1685 printleader(tcp);
1686 tprintf("=== FAULT %d ===", what);
1687 printtrailer(tcp);
1688 }
1689 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001690#ifdef FREEBSD
1691 case 0: /* handle case we polled for nothing */
1692 continue;
Roland McGrath553a6092002-12-16 20:40:39 +00001693#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001694 default:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001695 fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001696 exit(1);
1697 break;
1698 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001699 arg = 0;
Roland McGrath553a6092002-12-16 20:40:39 +00001700#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001701 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Roland McGrath553a6092002-12-16 20:40:39 +00001702#else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001703 if (IOCTL (tcp->pfd, PIOCRUN, 0) < 0) {
Roland McGrath553a6092002-12-16 20:40:39 +00001704#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001705 perror("PIOCRUN");
1706 exit(1);
1707 }
1708 }
1709 return 0;
1710}
1711
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001712#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001713
1714static int
1715trace()
1716{
1717 int pid;
1718 int wait_errno;
1719 int status;
1720 struct tcb *tcp;
1721#ifdef LINUX
1722 struct rusage ru;
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001723#ifdef __WALL
1724 static int wait4_options = __WALL;
1725#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001726#endif /* LINUX */
1727
1728 while (nprocs != 0) {
1729 if (interactive)
1730 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1731#ifdef LINUX
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001732#ifdef __WALL
1733 pid = wait4(-1, &status, wait4_options, cflag ? &ru : NULL);
1734 if ((wait4_options & __WALL) && errno == EINVAL) {
1735 /* this kernel does not support __WALL */
1736 wait4_options &= ~__WALL;
1737 errno = 0;
1738 pid = wait4(-1, &status, wait4_options,
1739 cflag ? &ru : NULL);
1740 }
1741 if (!(wait4_options & __WALL) && errno == ECHILD) {
1742 /* most likely a "cloned" process */
1743 pid = wait4(-1, &status, __WCLONE,
1744 cflag ? &ru : NULL);
1745 if (pid == -1) {
1746 fprintf(stderr, "strace: clone wait4 "
1747 "failed: %s\n", strerror(errno));
1748 }
1749 }
1750#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001751 pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001752#endif /* __WALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001753#endif /* LINUX */
1754#ifdef SUNOS4
1755 pid = wait(&status);
1756#endif /* SUNOS4 */
1757 wait_errno = errno;
1758 if (interactive)
1759 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1760
1761 if (interrupted)
1762 return 0;
1763
1764 if (pid == -1) {
1765 switch (wait_errno) {
1766 case EINTR:
1767 continue;
1768 case ECHILD:
1769 /*
1770 * We would like to verify this case
1771 * but sometimes a race in Solbourne's
1772 * version of SunOS sometimes reports
1773 * ECHILD before sending us SIGCHILD.
1774 */
1775#if 0
1776 if (nprocs == 0)
1777 return 0;
1778 fprintf(stderr, "strace: proc miscount\n");
1779 exit(1);
1780#endif
1781 return 0;
1782 default:
1783 errno = wait_errno;
1784 perror("strace: wait");
1785 return -1;
1786 }
1787 }
1788 if (debug)
1789 fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
1790
1791 /* Look up `pid' in our table. */
1792 if ((tcp = pid2tcb(pid)) == NULL) {
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001793#if 0 /* XXX davidm */ /* WTA: disabled again */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001794 struct tcb *tcpchild;
1795
1796 if ((tcpchild = alloctcb(pid)) == NULL) {
1797 fprintf(stderr, " [tcb table full]\n");
1798 kill(pid, SIGKILL); /* XXX */
1799 return 0;
1800 }
1801 tcpchild->flags |= TCB_ATTACHED;
1802 newoutf(tcpchild);
1803 tcp->nchildren++;
1804 if (!qflag)
1805 fprintf(stderr, "Process %d attached\n", pid);
1806#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001807 fprintf(stderr, "unknown pid: %u\n", pid);
1808 if (WIFSTOPPED(status))
1809 ptrace(PTRACE_CONT, pid, (char *) 1, 0);
1810 exit(1);
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001811#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001812 }
1813 /* set current output file */
1814 outf = tcp->outf;
1815 if (cflag) {
1816#ifdef LINUX
1817 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
1818 tcp->stime = ru.ru_stime;
1819#endif /* !LINUX */
1820 }
1821
1822 if (tcp->flags & TCB_SUSPENDED) {
1823 /*
1824 * Apparently, doing any ptrace() call on a stopped
1825 * process, provokes the kernel to report the process
1826 * status again on a subsequent wait(), even if the
1827 * process has not been actually restarted.
1828 * Since we have inspected the arguments of suspended
1829 * processes we end up here testing for this case.
1830 */
1831 continue;
1832 }
1833 if (WIFSIGNALED(status)) {
1834 if (!cflag
1835 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
1836 printleader(tcp);
1837 tprintf("+++ killed by %s +++",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001838 signame(WTERMSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001839 printtrailer(tcp);
1840 }
1841 droptcb(tcp);
1842 continue;
1843 }
1844 if (WIFEXITED(status)) {
1845 if (debug)
1846 fprintf(stderr, "pid %u exited\n", pid);
1847 if (tcp->flags & TCB_ATTACHED)
1848 fprintf(stderr,
1849 "PANIC: attached pid %u exited\n",
1850 pid);
1851 droptcb(tcp);
1852 continue;
1853 }
1854 if (!WIFSTOPPED(status)) {
1855 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
1856 droptcb(tcp);
1857 continue;
1858 }
1859 if (debug)
1860 fprintf(stderr, "pid %u stopped, [%s]\n",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001861 pid, signame(WSTOPSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001862
1863 if (tcp->flags & TCB_STARTUP) {
1864 /*
1865 * This flag is there to keep us in sync.
1866 * Next time this process stops it should
1867 * really be entering a system call.
1868 */
1869 tcp->flags &= ~TCB_STARTUP;
1870 if (tcp->flags & TCB_ATTACHED) {
1871 /*
1872 * Interestingly, the process may stop
1873 * with STOPSIG equal to some other signal
1874 * than SIGSTOP if we happend to attach
1875 * just before the process takes a signal.
1876 */
1877 if (!WIFSTOPPED(status)) {
1878 fprintf(stderr,
1879 "pid %u not stopped\n", pid);
1880 detach(tcp, WSTOPSIG(status));
1881 continue;
1882 }
1883 }
1884 else {
1885#ifdef SUNOS4
1886 /* A child of us stopped at exec */
1887 if (WSTOPSIG(status) == SIGTRAP && followvfork)
1888 fixvfork(tcp);
1889#endif /* SUNOS4 */
1890 }
1891 if (tcp->flags & TCB_BPTSET) {
1892 if (clearbpt(tcp) < 0) /* Pretty fatal */ {
1893 droptcb(tcp);
1894 cleanup();
1895 return -1;
1896 }
1897 }
1898 goto tracing;
1899 }
1900
1901 if (WSTOPSIG(status) != SIGTRAP) {
1902 if (WSTOPSIG(status) == SIGSTOP &&
1903 (tcp->flags & TCB_SIGTRAPPED)) {
1904 /*
1905 * Trapped attempt to block SIGTRAP
1906 * Hope we are back in control now.
1907 */
1908 tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
1909 if (ptrace(PTRACE_SYSCALL,
1910 pid, (char *) 1, 0) < 0) {
1911 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1912 cleanup();
1913 return -1;
1914 }
1915 continue;
1916 }
1917 if (!cflag
1918 && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001919 unsigned long addr = 0, pc = 0;
1920#ifdef PT_GETSIGINFO
1921# define PSR_RI 41
1922 struct siginfo si;
1923 unsigned long psr;
1924
1925 upeek(pid, PT_CR_IPSR, &psr);
1926 upeek(pid, PT_CR_IIP, &pc);
1927
1928 pc += (psr >> PSR_RI) & 0x3;
1929 ptrace(PT_GETSIGINFO, pid, 0, (long) &si);
1930 addr = (unsigned long) si.si_addr;
1931#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001932 printleader(tcp);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001933 tprintf("--- %s (%s) @ %lx (%lx) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001934 signame(WSTOPSIG(status)),
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001935 strsignal(WSTOPSIG(status)), pc, addr);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001936 printtrailer(tcp);
1937 }
1938 if ((tcp->flags & TCB_ATTACHED) &&
1939 !sigishandled(tcp, WSTOPSIG(status))) {
1940 detach(tcp, WSTOPSIG(status));
1941 continue;
1942 }
1943 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1,
1944 WSTOPSIG(status)) < 0) {
1945 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1946 cleanup();
1947 return -1;
1948 }
1949 tcp->flags &= ~TCB_SUSPENDED;
1950 continue;
1951 }
1952 if (trace_syscall(tcp) < 0) {
1953 if (tcp->flags & TCB_ATTACHED)
1954 detach(tcp, 0);
1955 else {
1956 ptrace(PTRACE_KILL,
1957 tcp->pid, (char *) 1, SIGTERM);
1958 droptcb(tcp);
1959 }
1960 continue;
1961 }
1962 if (tcp->flags & TCB_EXITING) {
1963 if (tcp->flags & TCB_ATTACHED)
1964 detach(tcp, 0);
1965 else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) {
1966 perror("strace: ptrace(PTRACE_CONT, ...)");
1967 cleanup();
1968 return -1;
1969 }
1970 continue;
1971 }
1972 if (tcp->flags & TCB_SUSPENDED) {
1973 if (!qflag)
1974 fprintf(stderr, "Process %u suspended\n", pid);
1975 continue;
1976 }
1977 tracing:
1978 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) {
1979 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1980 cleanup();
1981 return -1;
1982 }
1983 }
1984 return 0;
1985}
1986
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001987#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001988
1989static int curcol;
1990
1991#ifdef __STDC__
1992#include <stdarg.h>
1993#define VA_START(a, b) va_start(a, b)
1994#else
1995#include <varargs.h>
1996#define VA_START(a, b) va_start(a)
1997#endif
1998
1999void
2000#ifdef __STDC__
2001tprintf(const char *fmt, ...)
2002#else
2003tprintf(fmt, va_alist)
2004char *fmt;
2005va_dcl
2006#endif
2007{
2008 va_list args;
2009
2010 VA_START(args, fmt);
2011 if (outf)
2012 curcol += vfprintf(outf, fmt, args);
2013 va_end(args);
2014 return;
2015}
2016
2017void
2018printleader(tcp)
2019struct tcb *tcp;
2020{
2021 if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) {
2022 tcp_last->flags |= TCB_REPRINT;
2023 tprintf(" <unfinished ...>\n");
2024 }
2025 curcol = 0;
2026 if ((followfork == 1 || pflag_seen > 1) && outfname)
2027 tprintf("%-5d ", tcp->pid);
2028 else if (nprocs > 1 && !outfname)
2029 tprintf("[pid %5u] ", tcp->pid);
2030 if (tflag) {
2031 char str[sizeof("HH:MM:SS")];
2032 struct timeval tv, dtv;
2033 static struct timeval otv;
2034
2035 gettimeofday(&tv, NULL);
2036 if (rflag) {
2037 if (otv.tv_sec == 0)
2038 otv = tv;
2039 tv_sub(&dtv, &tv, &otv);
2040 tprintf("%6ld.%06ld ",
2041 (long) dtv.tv_sec, (long) dtv.tv_usec);
2042 otv = tv;
2043 }
2044 else if (tflag > 2) {
2045 tprintf("%ld.%06ld ",
2046 (long) tv.tv_sec, (long) tv.tv_usec);
2047 }
2048 else {
2049 time_t local = tv.tv_sec;
2050 strftime(str, sizeof(str), "%T", localtime(&local));
2051 if (tflag > 1)
2052 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
2053 else
2054 tprintf("%s ", str);
2055 }
2056 }
2057 if (iflag)
2058 printcall(tcp);
2059}
2060
2061void
2062tabto(col)
2063int col;
2064{
2065 if (curcol < col)
2066 tprintf("%*s", col - curcol, "");
2067}
2068
2069void
2070printtrailer(tcp)
2071struct tcb *tcp;
2072{
2073 tprintf("\n");
2074 tcp_last = NULL;
2075}
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002076
Wichert Akkermanea78f0f1999-11-29 15:34:02 +00002077#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002078
2079int mp_ioctl (int fd, int cmd, void *arg, int size) {
2080
2081 struct iovec iov[2];
2082 int n = 1;
Roland McGrath553a6092002-12-16 20:40:39 +00002083
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002084 iov[0].iov_base = &cmd;
2085 iov[0].iov_len = sizeof cmd;
2086 if (arg) {
2087 ++n;
2088 iov[1].iov_base = arg;
2089 iov[1].iov_len = size;
2090 }
Roland McGrath553a6092002-12-16 20:40:39 +00002091
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002092 return writev (fd, iov, n);
2093}
2094
2095#endif