blob: 48b4feb772ecd5996ee12f34fa009a31491138fd [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;
Roland McGrathee9d4352002-12-18 04:16:10 +000081struct tcb **tcbtab;
82unsigned int nprocs, tcbtabsize;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000083char *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));
Roland McGrathee9d4352002-12-18 04:16:10 +0000108static struct pollfd *pollv;
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
Roland McGrathee9d4352002-12-18 04:16:10 +0000187 /* Allocate the initial tcbtab. */
188 tcbtabsize = argc; /* Surely enough for all -p args. */
189 tcbtab = (struct tcb **) malloc (tcbtabsize * sizeof tcbtab[0]);
190 tcbtab[0] = (struct tcb *) calloc (tcbtabsize, sizeof *tcbtab[0]);
191 for (tcp = tcbtab[0]; tcp < &tcbtab[0][tcbtabsize]; ++tcp)
192 tcbtab[tcp - tcbtab[0]] = &tcbtab[0][tcp - tcbtab[0]];
193
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000194 progname = argv[0];
195 outf = stderr;
196 interactive = 1;
197 qualify("trace=all");
198 qualify("abbrev=all");
199 qualify("verbose=all");
200 qualify("signal=all");
201 set_sortby(DEFAULT_SORTBY);
202 set_personality(DEFAULT_PERSONALITY);
203 while ((c = getopt(argc, argv,
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000204 "+cdfFhiqrtTvVxza:e:o:O:p:s:S:u:")) != EOF) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000205 switch (c) {
206 case 'c':
207 cflag++;
208 dtime++;
209 break;
210 case 'd':
211 debug++;
212 break;
213 case 'f':
214 followfork++;
215 break;
216 case 'F':
217 followvfork++;
218 break;
219 case 'h':
220 usage(stdout, 0);
221 break;
222 case 'i':
223 iflag++;
224 break;
225 case 'q':
226 qflag++;
227 break;
228 case 'r':
229 rflag++;
230 tflag++;
231 break;
232 case 't':
233 tflag++;
234 break;
235 case 'T':
236 dtime++;
237 break;
238 case 'x':
239 xflag++;
240 break;
241 case 'v':
242 qualify("abbrev=none");
243 break;
244 case 'V':
245 printf("%s\n", version);
246 exit(0);
247 break;
Michal Ludvig17f8fb32002-11-06 13:17:21 +0000248 case 'z':
249 not_failing_only = 1;
250 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000251 case 'a':
252 acolumn = atoi(optarg);
253 break;
254 case 'e':
255 qualify(optarg);
256 break;
257 case 'o':
258 outfname = strdup(optarg);
259 break;
260 case 'O':
261 set_overhead(atoi(optarg));
262 break;
263 case 'p':
264 if ((pid = atoi(optarg)) == 0) {
265 fprintf(stderr, "%s: Invalid process id: %s\n",
266 progname, optarg);
267 break;
268 }
269 if (pid == getpid()) {
Wichert Akkerman54a47671999-10-17 00:57:34 +0000270 fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000271 break;
272 }
273 if ((tcp = alloctcb(pid)) == NULL) {
274 fprintf(stderr, "%s: tcb table full, please recompile strace\n",
275 progname);
276 exit(1);
277 }
278 tcp->flags |= TCB_ATTACHED;
279 pflag_seen++;
280 break;
281 case 's':
282 max_strlen = atoi(optarg);
283 break;
284 case 'S':
285 set_sortby(optarg);
286 break;
287 case 'u':
288 username = strdup(optarg);
289 break;
290 default:
291 usage(stderr, 1);
292 break;
293 }
294 }
295
296 /* See if they want to run as another user. */
297 if (username != NULL) {
298 struct passwd *pent;
299
300 if (getuid() != 0 || geteuid() != 0) {
301 fprintf(stderr,
302 "%s: you must be root to use the -u option\n",
303 progname);
304 exit(1);
305 }
306 if ((pent = getpwnam(username)) == NULL) {
307 fprintf(stderr, "%s: cannot find user `%s'\n",
308 progname, optarg);
309 exit(1);
310 }
311 run_uid = pent->pw_uid;
312 run_gid = pent->pw_gid;
313 }
314 else {
315 run_uid = getuid();
316 run_gid = getgid();
317 }
318
319#ifndef SVR4
320 setreuid(geteuid(), getuid());
321#endif
322
323 /* See if they want to pipe the output. */
324 if (outfname && (outfname[0] == '|' || outfname[0] == '!')) {
325 if ((outf = popen(outfname + 1, "w")) == NULL) {
326 fprintf(stderr, "%s: can't popen '%s': %s\n",
327 progname, outfname + 1, strerror(errno));
328 exit(1);
329 }
330 free(outfname);
331 outfname = NULL;
332 }
333
334 /* Check if they want to redirect the output. */
335 if (outfname) {
Wichert Akkerman54b4f792001-08-03 11:43:35 +0000336 long f;
337
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000338 if ((outf = fopen(outfname, "w")) == NULL) {
339 fprintf(stderr, "%s: can't fopen '%s': %s\n",
340 progname, outfname, strerror(errno));
341 exit(1);
342 }
Wichert Akkerman54b4f792001-08-03 11:43:35 +0000343
344 if ((f=fcntl(fileno(outf), F_GETFD)) < 0 ) {
345 perror("failed to get flags for outputfile");
346 exit(1);
347 }
348
349 if (fcntl(fileno(outf), F_SETFD, f|FD_CLOEXEC) < 0 ) {
350 perror("failed to set flags for outputfile");
351 exit(1);
352 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000353 }
354
355#ifndef SVR4
356 setreuid(geteuid(), getuid());
357#endif
358
359 if (!outfname) {
360 qflag = 1;
361 setvbuf(outf, buf, _IOLBF, BUFSIZ);
362 }
363 else if (optind < argc)
364 interactive = 0;
365 else
366 qflag = 1;
367
Roland McGrathee9d4352002-12-18 04:16:10 +0000368 for (c = 0; c < tcbtabsize; c++) {
369 tcp = tcbtab[c];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000370 /* Reinitialize the output since it may have changed. */
371 tcp->outf = outf;
372 if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
373 continue;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000374#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000375 if (proc_open(tcp, 1) < 0) {
376 fprintf(stderr, "trouble opening proc file\n");
377 droptcb(tcp);
378 continue;
379 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000380#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000381 if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
382 perror("attach: ptrace(PTRACE_ATTACH, ...)");
383 droptcb(tcp);
384 continue;
385 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000386#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000387 if (!qflag)
388 fprintf(stderr,
389 "Process %u attached - interrupt to quit\n",
390 pid);
391 }
392
393 if (optind < argc) {
394 struct stat statbuf;
395 char *filename;
396 char pathname[MAXPATHLEN];
397
398 filename = argv[optind];
399 if (strchr(filename, '/'))
400 strcpy(pathname, filename);
401#ifdef USE_DEBUGGING_EXEC
402 /*
403 * Debuggers customarily check the current directory
404 * first regardless of the path but doing that gives
405 * security geeks a panic attack.
406 */
407 else if (stat(filename, &statbuf) == 0)
408 strcpy(pathname, filename);
409#endif /* USE_DEBUGGING_EXEC */
410 else {
411 char *path;
412 int m, n, len;
413
414 for (path = getenv("PATH"); path && *path; path += m) {
415 if (strchr(path, ':')) {
416 n = strchr(path, ':') - path;
417 m = n + 1;
418 }
419 else
420 m = n = strlen(path);
421 if (n == 0) {
422 getcwd(pathname, MAXPATHLEN);
423 len = strlen(pathname);
424 }
425 else {
426 strncpy(pathname, path, n);
427 len = n;
428 }
429 if (len && pathname[len - 1] != '/')
430 pathname[len++] = '/';
431 strcpy(pathname + len, filename);
432 if (stat(pathname, &statbuf) == 0)
433 break;
434 }
435 }
436 if (stat(pathname, &statbuf) < 0) {
437 fprintf(stderr, "%s: %s: command not found\n",
438 progname, filename);
439 exit(1);
440 }
441 switch (pid = fork()) {
442 case -1:
443 perror("strace: fork");
444 cleanup();
445 exit(1);
446 break;
447 case 0: {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000448#ifdef USE_PROCFS
449 if (outf != stderr) close (fileno (outf));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000450#ifdef MIPS
451 /* Kludge for SGI, see proc_open for details. */
452 sa.sa_handler = foobar;
453 sa.sa_flags = 0;
454 sigemptyset(&sa.sa_mask);
455 sigaction(SIGINT, &sa, NULL);
456#endif /* MIPS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000457#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000458 pause();
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000459#else /* FREEBSD */
460 kill(getpid(), SIGSTOP); /* stop HERE */
Roland McGrath553a6092002-12-16 20:40:39 +0000461#endif /* FREEBSD */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000462#else /* !USE_PROCFS */
Roland McGrath553a6092002-12-16 20:40:39 +0000463 if (outf!=stderr)
Wichert Akkerman7987cdf2000-07-05 16:05:39 +0000464 close(fileno (outf));
Wichert Akkermanbd4125c2000-06-27 17:28:06 +0000465
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000466 if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
467 perror("strace: ptrace(PTRACE_TRACEME, ...)");
468 return -1;
469 }
470 if (debug)
471 kill(getpid(), SIGSTOP);
472
473 if (username != NULL || geteuid() == 0) {
474 uid_t run_euid = run_uid;
475 gid_t run_egid = run_gid;
476
477 if (statbuf.st_mode & S_ISUID)
478 run_euid = statbuf.st_uid;
479 if (statbuf.st_mode & S_ISGID)
480 run_egid = statbuf.st_gid;
481
482 /*
483 * It is important to set groups before we
484 * lose privileges on setuid.
485 */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +0000486 if (username != NULL) {
487 if (initgroups(username, run_gid) < 0) {
488 perror("initgroups");
489 exit(1);
490 }
491 if (setregid(run_gid, run_egid) < 0) {
492 perror("setregid");
493 exit(1);
494 }
495 if (setreuid(run_uid, run_euid) < 0) {
496 perror("setreuid");
497 exit(1);
498 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000499 }
500 }
501 else
502 setreuid(run_uid, run_uid);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000503#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000504
505 execv(pathname, &argv[optind]);
506 perror("strace: exec");
507 _exit(1);
508 break;
509 }
510 default:
511 if ((tcp = alloctcb(pid)) == NULL) {
512 fprintf(stderr, "tcb table full\n");
513 cleanup();
514 exit(1);
515 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000516#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000517 if (proc_open(tcp, 0) < 0) {
518 fprintf(stderr, "trouble opening proc file\n");
519 cleanup();
520 exit(1);
521 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000522#endif /* USE_PROCFS */
523#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000524 fake_execve(tcp, pathname, &argv[optind], environ);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000525#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000526 break;
527 }
528 }
529 else if (pflag_seen == 0)
530 usage(stderr, 1);
531
532 sigemptyset(&empty_set);
533 sigemptyset(&blocked_set);
534 sa.sa_handler = SIG_IGN;
535 sigemptyset(&sa.sa_mask);
536 sa.sa_flags = 0;
537 sigaction(SIGTTOU, &sa, NULL);
538 sigaction(SIGTTIN, &sa, NULL);
539 if (interactive) {
540 sigaddset(&blocked_set, SIGHUP);
541 sigaddset(&blocked_set, SIGINT);
542 sigaddset(&blocked_set, SIGQUIT);
543 sigaddset(&blocked_set, SIGPIPE);
544 sigaddset(&blocked_set, SIGTERM);
545 sa.sa_handler = interrupt;
546#ifdef SUNOS4
547 /* POSIX signals on sunos4.1 are a little broken. */
548 sa.sa_flags = SA_INTERRUPT;
549#endif /* SUNOS4 */
550 }
551 sigaction(SIGHUP, &sa, NULL);
552 sigaction(SIGINT, &sa, NULL);
553 sigaction(SIGQUIT, &sa, NULL);
554 sigaction(SIGPIPE, &sa, NULL);
555 sigaction(SIGTERM, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000556#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000557 sa.sa_handler = reaper;
558 sigaction(SIGCHLD, &sa, NULL);
Roland McGrath553a6092002-12-16 20:40:39 +0000559#else
560 /* Make sure SIGCHLD has the default action so that waitpid
561 definitely works without losing track of children. The user
562 should not have given us a bogus state to inherit, but he might
563 have. Arguably we should detect SIG_IGN here and pass it on
564 to children, but probably noone really needs that. */
565 sa.sa_handler = SIG_DFL;
566 sigaction(SIGCHLD, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000567#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000568
569 if (trace() < 0)
570 exit(1);
571 cleanup();
572 exit(0);
573}
574
575void
576newoutf(tcp)
577struct tcb *tcp;
578{
579 char name[MAXPATHLEN];
580 FILE *fp;
581
582 if (outfname && followfork > 1) {
583 sprintf(name, "%s.%u", outfname, tcp->pid);
584#ifndef SVR4
585 setreuid(geteuid(), getuid());
586#endif
587 fp = fopen(name, "w");
588#ifndef SVR4
589 setreuid(geteuid(), getuid());
590#endif
591 if (fp == NULL) {
592 perror("fopen");
593 return;
594 }
595 tcp->outf = fp;
596 }
597 return;
598}
599
600struct tcb *
601alloctcb(pid)
602int pid;
603{
604 int i;
605 struct tcb *tcp;
606
Roland McGrathee9d4352002-12-18 04:16:10 +0000607 for (i = 0; i < tcbtabsize; i++) {
608 tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000609 if ((tcp->flags & TCB_INUSE) == 0) {
610 tcp->pid = pid;
611 tcp->parent = NULL;
612 tcp->nchildren = 0;
613 tcp->flags = TCB_INUSE | TCB_STARTUP;
614 tcp->outf = outf; /* Initialise to current out file */
615 tcp->stime.tv_sec = 0;
616 tcp->stime.tv_usec = 0;
617 tcp->pfd = -1;
618 nprocs++;
619 return tcp;
620 }
621 }
622 return NULL;
623}
624
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000625#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000626int
627proc_open(tcp, attaching)
628struct tcb *tcp;
629int attaching;
630{
631 char proc[32];
632 long arg;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000633#ifdef SVR4
John Hughes19e49982001-10-19 08:59:12 +0000634 int i;
635 sysset_t syscalls;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000636 sigset_t signals;
637 fltset_t faults;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000638#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000639#ifndef HAVE_POLLABLE_PROCFS
640 static int last_pfd;
641#endif
642
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000643#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000644 /* Open the process pseudo-files in /proc. */
645 sprintf(proc, "/proc/%d/ctl", tcp->pid);
646 if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000647 perror("strace: open(\"/proc/...\", ...)");
648 return -1;
649 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000650 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
651 perror("F_GETFD");
652 return -1;
653 }
654 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
655 perror("F_SETFD");
656 return -1;
657 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000658 sprintf(proc, "/proc/%d/status", tcp->pid);
659 if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
660 perror("strace: open(\"/proc/...\", ...)");
661 return -1;
662 }
663 if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) {
664 perror("F_GETFD");
665 return -1;
666 }
667 if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) {
668 perror("F_SETFD");
669 return -1;
670 }
671 sprintf(proc, "/proc/%d/as", tcp->pid);
672 if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
673 perror("strace: open(\"/proc/...\", ...)");
674 return -1;
675 }
676 if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) {
677 perror("F_GETFD");
678 return -1;
679 }
680 if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) {
681 perror("F_SETFD");
682 return -1;
683 }
684#else
685 /* Open the process pseudo-file in /proc. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000686#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000687 sprintf(proc, "/proc/%d", tcp->pid);
688 if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000689#else /* FREEBSD */
690 sprintf(proc, "/proc/%d/mem", tcp->pid);
691 if ((tcp->pfd = open(proc, O_RDWR)) < 0) {
692#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000693 perror("strace: open(\"/proc/...\", ...)");
694 return -1;
695 }
696 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
697 perror("F_GETFD");
698 return -1;
699 }
700 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
701 perror("F_SETFD");
702 return -1;
703 }
704#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000705#ifdef FREEBSD
706 sprintf(proc, "/proc/%d/regs", tcp->pid);
707 if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) {
708 perror("strace: open(\"/proc/.../regs\", ...)");
709 return -1;
710 }
711 if (cflag) {
712 sprintf(proc, "/proc/%d/status", tcp->pid);
713 if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) {
714 perror("strace: open(\"/proc/.../status\", ...)");
715 return -1;
716 }
717 } else
718 tcp->pfd_status = -1;
719#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000720 rebuild_pollv();
721 if (!attaching) {
722 /*
723 * Wait for the child to pause. Because of a race
724 * condition we have to poll for the event.
725 */
726 for (;;) {
727 if (IOCTL_STATUS (tcp) < 0) {
728 perror("strace: PIOCSTATUS");
729 return -1;
730 }
731 if (tcp->status.PR_FLAGS & PR_ASLEEP)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000732 break;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000733 }
734 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000735#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000736 /* Stop the process so that we own the stop. */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000737 if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000738 perror("strace: PIOCSTOP");
739 return -1;
740 }
Roland McGrath553a6092002-12-16 20:40:39 +0000741#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000742#ifdef PIOCSET
743 /* Set Run-on-Last-Close. */
744 arg = PR_RLC;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000745 if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000746 perror("PIOCSET PR_RLC");
747 return -1;
748 }
749 /* Set or Reset Inherit-on-Fork. */
750 arg = PR_FORK;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000751 if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000752 perror("PIOC{SET,RESET} PR_FORK");
753 return -1;
754 }
755#else /* !PIOCSET */
Roland McGrath553a6092002-12-16 20:40:39 +0000756#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000757 if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
758 perror("PIOCSRLC");
759 return -1;
760 }
761 if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
762 perror("PIOC{S,R}FORK");
763 return -1;
764 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000765#else /* FREEBSD */
766 /* just unset the PF_LINGER flag for the Run-on-Last-Close. */
767 if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) {
768 perror("PIOCGFL");
769 return -1;
770 }
771 arg &= ~PF_LINGER;
772 if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) {
773 perror("PIOCSFL");
774 return -1;
775 }
776#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000777#endif /* !PIOCSET */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000778#ifndef FREEBSD
John Hughes19e49982001-10-19 08:59:12 +0000779 /* Enable all syscall entries we care about. */
780 premptyset(&syscalls);
781 for (i = 1; i < MAX_QUALS; ++i) {
782 if (i > (sizeof syscalls) * CHAR_BIT) break;
783 if (qual_flags [i] & QUAL_TRACE) praddset (&syscalls, i);
784 }
785 praddset (&syscalls, SYS_execve);
786 if (followfork) {
787 praddset (&syscalls, SYS_fork);
788#ifdef SYS_forkall
789 praddset (&syscalls, SYS_forkall);
790#endif
Roland McGrath553a6092002-12-16 20:40:39 +0000791#ifdef SYS_fork1
John Hughes19e49982001-10-19 08:59:12 +0000792 praddset (&syscalls, SYS_fork1);
793#endif
794#ifdef SYS_rfork1
795 praddset (&syscalls, SYS_rfork1);
796#endif
797#ifdef SYS_rforkall
798 praddset (&syscalls, SYS_rforkall);
799#endif
800 }
801 if (IOCTL(tcp->pfd, PIOCSENTRY, &syscalls) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000802 perror("PIOCSENTRY");
803 return -1;
804 }
John Hughes19e49982001-10-19 08:59:12 +0000805 /* Enable the syscall exits. */
806 if (IOCTL(tcp->pfd, PIOCSEXIT, &syscalls) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000807 perror("PIOSEXIT");
808 return -1;
809 }
John Hughes19e49982001-10-19 08:59:12 +0000810 /* Enable signals we care about. */
811 premptyset(&signals);
812 for (i = 1; i < MAX_QUALS; ++i) {
813 if (i > (sizeof signals) * CHAR_BIT) break;
814 if (qual_flags [i] & QUAL_SIGNAL) praddset (&signals, i);
815 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000816 if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000817 perror("PIOCSTRACE");
818 return -1;
819 }
John Hughes19e49982001-10-19 08:59:12 +0000820 /* Enable faults we care about */
821 premptyset(&faults);
822 for (i = 1; i < MAX_QUALS; ++i) {
823 if (i > (sizeof faults) * CHAR_BIT) break;
824 if (qual_flags [i] & QUAL_FAULT) praddset (&faults, i);
825 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000826 if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000827 perror("PIOCSFAULT");
828 return -1;
829 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000830#else /* FREEBSD */
831 /* set events flags. */
832 arg = S_SIG | S_SCE | S_SCX ;
833 if(ioctl(tcp->pfd, PIOCBIS, arg) < 0) {
834 perror("PIOCBIS");
835 return -1;
836 }
837#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000838 if (!attaching) {
839#ifdef MIPS
840 /*
841 * The SGI PRSABORT doesn't work for pause() so
842 * we send it a caught signal to wake it up.
843 */
844 kill(tcp->pid, SIGINT);
845#else /* !MIPS */
Roland McGrath553a6092002-12-16 20:40:39 +0000846#ifdef PRSABORT
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000847 /* The child is in a pause(), abort it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000848 arg = PRSABORT;
849 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000850 perror("PIOCRUN");
851 return -1;
852 }
Roland McGrath553a6092002-12-16 20:40:39 +0000853#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000854#endif /* !MIPS*/
855#ifdef FREEBSD
856 /* wake up the child if it received the SIGSTOP */
857 kill(tcp->pid, SIGCONT);
Roland McGrath553a6092002-12-16 20:40:39 +0000858#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000859 for (;;) {
860 /* Wait for the child to do something. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000861 if (IOCTL_WSTOP (tcp) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000862 perror("PIOCWSTOP");
863 return -1;
864 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000865 if (tcp->status.PR_WHY == PR_SYSENTRY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000866 tcp->flags &= ~TCB_INSYSCALL;
867 get_scno(tcp);
868 if (tcp->scno == SYS_execve)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000869 break;
870 }
871 /* Set it running: maybe execve will be next. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000872#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000873 arg = 0;
874 if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000875#else /* FREEBSD */
876 if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) {
Roland McGrath553a6092002-12-16 20:40:39 +0000877#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000878 perror("PIOCRUN");
879 return -1;
880 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000881#ifdef FREEBSD
882 /* handle the case where we "opened" the child before
883 it did the kill -STOP */
884 if (tcp->status.PR_WHY == PR_SIGNALLED &&
885 tcp->status.PR_WHAT == SIGSTOP)
886 kill(tcp->pid, SIGCONT);
Roland McGrath553a6092002-12-16 20:40:39 +0000887#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000888 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000889#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000890 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000891#else /* FREEBSD */
892 } else {
Roland McGrath553a6092002-12-16 20:40:39 +0000893 if (attaching < 2) {
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +0000894 /* We are attaching to an already running process.
895 * Try to figure out the state of the process in syscalls,
896 * to handle the first event well.
897 * This is done by having a look at the "wchan" property of the
898 * process, which tells where it is stopped (if it is). */
899 FILE * status;
900 char wchan[20]; /* should be enough */
Roland McGrath553a6092002-12-16 20:40:39 +0000901
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +0000902 sprintf(proc, "/proc/%d/status", tcp->pid);
903 status = fopen(proc, "r");
904 if (status &&
905 (fscanf(status, "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d"
906 "%*d,%*d %*d,%*d %19s", wchan) == 1) &&
907 strcmp(wchan, "nochan") && strcmp(wchan, "spread") &&
908 strcmp(wchan, "stopevent")) {
909 /* The process is asleep in the middle of a syscall.
910 Fake the syscall entry event */
911 tcp->flags &= ~(TCB_INSYSCALL|TCB_STARTUP);
912 tcp->status.PR_WHY = PR_SYSENTRY;
913 trace_syscall(tcp);
914 }
915 if (status)
916 fclose(status);
917 } /* otherwise it's a fork being followed */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000918 }
919#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000920#ifndef HAVE_POLLABLE_PROCFS
921 if (proc_poll_pipe[0] != -1)
922 proc_poller(tcp->pfd);
923 else if (nprocs > 1) {
924 proc_poll_open();
925 proc_poller(last_pfd);
926 proc_poller(tcp->pfd);
927 }
928 last_pfd = tcp->pfd;
929#endif /* !HAVE_POLLABLE_PROCFS */
930 return 0;
931}
932
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000933#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000934
935static struct tcb *
936pid2tcb(pid)
937int pid;
938{
939 int i;
940 struct tcb *tcp;
941
Roland McGrathee9d4352002-12-18 04:16:10 +0000942 for (i = 0; i < tcbtabsize; i++) {
943 tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000944 if (pid && tcp->pid != pid)
945 continue;
946 if (tcp->flags & TCB_INUSE)
947 return tcp;
948 }
949 return NULL;
950}
951
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000952#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000953
954static struct tcb *
955pfd2tcb(pfd)
956int pfd;
957{
958 int i;
959 struct tcb *tcp;
960
Roland McGrathee9d4352002-12-18 04:16:10 +0000961 for (i = 0, tcp = tcbtab; i < tcbtabsize; i++, tcp++) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000962 if (tcp->pfd != pfd)
963 continue;
964 if (tcp->flags & TCB_INUSE)
965 return tcp;
966 }
967 return NULL;
968}
969
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000970#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000971
972void
973droptcb(tcp)
974struct tcb *tcp;
975{
976 if (tcp->pid == 0)
977 return;
978 nprocs--;
979 tcp->pid = 0;
980 tcp->flags = 0;
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +0000981
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000982 if (tcp->pfd != -1) {
983 close(tcp->pfd);
984 tcp->pfd = -1;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000985#ifdef FREEBSD
986 if (tcp->pfd_reg != -1) {
987 close(tcp->pfd_reg);
988 tcp->pfd_reg = -1;
989 }
990 if (tcp->pfd_status != -1) {
991 close(tcp->pfd_status);
992 tcp->pfd_status = -1;
993 }
Roland McGrath553a6092002-12-16 20:40:39 +0000994#endif /* !FREEBSD */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000995#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000996 rebuild_pollv();
997#endif
998 }
999 if (tcp->parent != NULL) {
1000 tcp->parent->nchildren--;
1001 tcp->parent = NULL;
1002 }
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +00001003
Wichert Akkerman822f0c92002-04-03 10:55:14 +00001004 if (outfname && followfork > 1 && tcp->outf)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001005 fclose(tcp->outf);
Wichert Akkermaneb8ebda2002-04-01 17:48:02 +00001006
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001007 tcp->outf = 0;
1008}
1009
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001010#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001011
1012static int
1013resume(tcp)
1014struct tcb *tcp;
1015{
1016 if (tcp == NULL)
1017 return -1;
1018
1019 if (!(tcp->flags & TCB_SUSPENDED)) {
1020 fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid);
1021 return -1;
1022 }
1023 tcp->flags &= ~TCB_SUSPENDED;
1024
1025 if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) {
1026 perror("resume: ptrace(PTRACE_SYSCALL, ...)");
1027 return -1;
1028 }
1029
1030 if (!qflag)
1031 fprintf(stderr, "Process %u resumed\n", tcp->pid);
1032 return 0;
1033}
1034
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001035#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001036
1037/* detach traced process; continue with sig */
1038
1039static int
1040detach(tcp, sig)
1041struct tcb *tcp;
1042int sig;
1043{
1044 int error = 0;
1045#ifdef LINUX
1046 int status;
1047#endif
1048
1049 if (tcp->flags & TCB_BPTSET)
1050 sig = SIGKILL;
1051
1052#ifdef LINUX
1053 /*
1054 * Linux wrongly insists the child be stopped
Roland McGrath7bf10472002-12-16 20:42:50 +00001055 * before detaching. Arghh. We go through hoops
1056 * to make a clean break of things.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001057 */
Roland McGrath7bf10472002-12-16 20:42:50 +00001058#if defined(SPARC)
1059#undef PTRACE_DETACH
1060#define PTRACE_DETACH PTRACE_SUNDETACH
1061#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001062 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
1063 /* On a clear day, you can see forever. */
Roland McGrath7bf10472002-12-16 20:42:50 +00001064 }
1065 else if (errno != ESRCH) {
1066 /* Shouldn't happen. */
1067 perror("detach: ptrace(PTRACE_DETACH, ...)");
1068 }
1069 else if (kill(tcp->pid, 0) < 0) {
1070 if (errno != ESRCH)
1071 perror("detach: checking sanity");
1072 }
1073 else if (kill(tcp->pid, SIGSTOP) < 0) {
1074 if (errno != ESRCH)
1075 perror("detach: stopping child");
1076 }
1077 else {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001078 for (;;) {
Roland McGrath7508cb42002-12-17 10:48:05 +00001079#ifdef __WALL
1080 if (wait4(tcp->pid, &status, __WALL, NULL) < 0) {
1081 if (errno == ECHILD) /* Already gone. */
1082 break;
1083 if (errno != EINVAL) {
Roland McGrath553a6092002-12-16 20:40:39 +00001084 perror("detach: waiting");
Roland McGrath7508cb42002-12-17 10:48:05 +00001085 break;
1086 }
1087#endif /* __WALL */
1088 /* No __WALL here. */
1089 if (waitpid(tcp->pid, &status, 0) < 0) {
1090 if (errno != ECHILD) {
1091 perror("detach: waiting");
1092 break;
1093 }
1094#ifdef __WCLONE
1095 /* If no processes, try clones. */
1096 if (wait4(tcp->pid, &status, __WCLONE,
1097 NULL) < 0) {
1098 if (errno != ECHILD)
1099 perror("detach: waiting");
1100 break;
1101 }
1102#endif /* __WCLONE */
1103 }
1104#ifdef __WALL
Roland McGrath553a6092002-12-16 20:40:39 +00001105 }
Roland McGrath7508cb42002-12-17 10:48:05 +00001106#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001107 if (!WIFSTOPPED(status)) {
1108 /* Au revoir, mon ami. */
1109 break;
1110 }
1111 if (WSTOPSIG(status) == SIGSTOP) {
1112 if ((error = ptrace(PTRACE_DETACH,
Roland McGrath7bf10472002-12-16 20:42:50 +00001113 tcp->pid, (char *) 1, sig)) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001114 if (errno != ESRCH)
1115 perror("detach: ptrace(PTRACE_DETACH, ...)");
1116 /* I died trying. */
1117 }
1118 break;
1119 }
1120 if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1,
Roland McGrath7bf10472002-12-16 20:42:50 +00001121 WSTOPSIG(status) == SIGTRAP ?
1122 0 : WSTOPSIG(status))) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001123 if (errno != ESRCH)
1124 perror("detach: ptrace(PTRACE_CONT, ...)");
1125 break;
1126 }
1127 }
1128 }
Roland McGrath7bf10472002-12-16 20:42:50 +00001129#endif /* LINUX */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001130
1131#if defined(SUNOS4)
1132 /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
1133 if (sig && kill(tcp->pid, sig) < 0)
1134 perror("detach: kill");
1135 sig = 0;
1136 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0)
1137 perror("detach: ptrace(PTRACE_DETACH, ...)");
1138#endif /* SUNOS4 */
1139
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001140#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001141 if (waiting_parent(tcp))
1142 error = resume(tcp->parent);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001143#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001144
1145 if (!qflag)
1146 fprintf(stderr, "Process %u detached\n", tcp->pid);
1147
1148 droptcb(tcp);
1149 return error;
1150}
1151
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001152#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001153
1154static void
1155reaper(sig)
1156int sig;
1157{
1158 int pid;
1159 int status;
1160
1161 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
1162#if 0
1163 struct tcb *tcp;
1164
1165 tcp = pid2tcb(pid);
1166 if (tcp)
1167 droptcb(tcp);
1168#endif
1169 }
1170}
1171
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001172#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001173
1174static void
1175cleanup()
1176{
1177 int i;
1178 struct tcb *tcp;
1179
Roland McGrathee9d4352002-12-18 04:16:10 +00001180 for (i = 0; i < tcbtabsize; i++) {
1181 tcp = tcbtab[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001182 if (!(tcp->flags & TCB_INUSE))
1183 continue;
1184 if (debug)
1185 fprintf(stderr,
1186 "cleanup: looking at pid %u\n", tcp->pid);
1187 if (tcp_last &&
1188 (!outfname || followfork < 2 || tcp_last == tcp)) {
1189 tprintf(" <unfinished ...>\n");
1190 tcp_last = NULL;
1191 }
1192 if (tcp->flags & TCB_ATTACHED)
1193 detach(tcp, 0);
1194 else {
1195 kill(tcp->pid, SIGCONT);
1196 kill(tcp->pid, SIGTERM);
1197 }
1198 }
1199 if (cflag)
1200 call_summary(outf);
1201}
1202
1203static void
1204interrupt(sig)
1205int sig;
1206{
1207 interrupted = 1;
1208}
1209
1210#ifndef HAVE_STRERROR
1211
Roland McGrath6d2b3492002-12-30 00:51:30 +00001212#if !HAVE_DECL_SYS_ERRLIST
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001213extern int sys_nerr;
1214extern char *sys_errlist[];
Roland McGrath6d2b3492002-12-30 00:51:30 +00001215#endif /* HAVE_DECL_SYS_ERRLIST */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001216
1217const char *
1218strerror(errno)
1219int errno;
1220{
1221 static char buf[64];
1222
1223 if (errno < 1 || errno >= sys_nerr) {
1224 sprintf(buf, "Unknown error %d", errno);
1225 return buf;
1226 }
1227 return sys_errlist[errno];
1228}
1229
1230#endif /* HAVE_STERRROR */
1231
1232#ifndef HAVE_STRSIGNAL
1233
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001234#ifdef HAVE__SYS_SIGLIST
Roland McGrath6d2b3492002-12-30 00:51:30 +00001235#if !HAVE_DECL_SYS_SIGLIST
1236extern char *sys_siglist[];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001237 extern char *_sys_siglist[];
1238#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001239#endif
1240#endif /* SYS_SIGLIST_DECLARED */
1241
1242const char *
1243strsignal(sig)
1244int sig;
1245{
1246 static char buf[64];
1247
1248 if (sig < 1 || sig >= NSIG) {
1249 sprintf(buf, "Unknown signal %d", sig);
1250 return buf;
1251 }
1252#ifdef HAVE__SYS_SIGLIST
1253 return _sys_siglist[sig];
1254#else
1255 return sys_siglist[sig];
1256#endif
1257}
1258
1259#endif /* HAVE_STRSIGNAL */
1260
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001261#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001262
1263static void
1264rebuild_pollv()
1265{
1266 int i, j;
1267 struct tcb *tcp;
1268
Roland McGrathee9d4352002-12-18 04:16:10 +00001269 if (pollv != NULL)
1270 free (pollv);
1271 pollv = (struct poll *) malloc(nprocs * sizeof pollv[0]);
1272 if (pollv == NULL) {
1273 fprintf(stderr, "strace: out of memory for poll vector\n");
1274 exit(1);
1275 }
1276
1277 for (i = j = 0, tcp = tcbtab; i < tcbtabsize; i++, tcp++) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001278 if (!(tcp->flags & TCB_INUSE))
1279 continue;
1280 pollv[j].fd = tcp->pfd;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001281 pollv[j].events = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001282 j++;
1283 }
1284 if (j != nprocs) {
1285 fprintf(stderr, "strace: proc miscount\n");
1286 exit(1);
1287 }
1288}
1289
1290#ifndef HAVE_POLLABLE_PROCFS
1291
1292static void
1293proc_poll_open()
1294{
1295 int arg;
1296 int i;
1297
1298 if (pipe(proc_poll_pipe) < 0) {
1299 perror("pipe");
1300 exit(1);
1301 }
1302 for (i = 0; i < 2; i++) {
1303 if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) {
1304 perror("F_GETFD");
1305 exit(1);
1306 }
1307 if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) {
1308 perror("F_SETFD");
1309 exit(1);
1310 }
1311 }
1312}
1313
1314static int
1315proc_poll(pollv, nfds, timeout)
1316struct pollfd *pollv;
1317int nfds;
1318int timeout;
1319{
1320 int i;
1321 int n;
1322 struct proc_pollfd pollinfo;
1323
1324 if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
1325 return n;
1326 if (n != sizeof(struct proc_pollfd)) {
1327 fprintf(stderr, "panic: short read: %d\n", n);
1328 exit(1);
1329 }
1330 for (i = 0; i < nprocs; i++) {
1331 if (pollv[i].fd == pollinfo.fd)
1332 pollv[i].revents = pollinfo.revents;
1333 else
1334 pollv[i].revents = 0;
1335 }
1336 poller_pid = pollinfo.pid;
1337 return 1;
1338}
1339
1340static void
1341wakeup_handler(sig)
1342int sig;
1343{
1344}
1345
1346static void
1347proc_poller(pfd)
1348int pfd;
1349{
1350 struct proc_pollfd pollinfo;
1351 struct sigaction sa;
1352 sigset_t blocked_set, empty_set;
1353 int i;
1354 int n;
1355 struct rlimit rl;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001356#ifdef FREEBSD
1357 struct procfs_status pfs;
1358#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001359
1360 switch (fork()) {
1361 case -1:
1362 perror("fork");
1363 _exit(0);
1364 case 0:
1365 break;
1366 default:
1367 return;
1368 }
1369
1370 sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
1371 sa.sa_flags = 0;
1372 sigemptyset(&sa.sa_mask);
1373 sigaction(SIGHUP, &sa, NULL);
1374 sigaction(SIGINT, &sa, NULL);
1375 sigaction(SIGQUIT, &sa, NULL);
1376 sigaction(SIGPIPE, &sa, NULL);
1377 sigaction(SIGTERM, &sa, NULL);
1378 sa.sa_handler = wakeup_handler;
1379 sigaction(SIGUSR1, &sa, NULL);
1380 sigemptyset(&blocked_set);
1381 sigaddset(&blocked_set, SIGUSR1);
1382 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1383 sigemptyset(&empty_set);
1384
1385 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
1386 perror("getrlimit(RLIMIT_NOFILE, ...)");
1387 _exit(0);
1388 }
1389 n = rl.rlim_cur;
1390 for (i = 0; i < n; i++) {
1391 if (i != pfd && i != proc_poll_pipe[1])
1392 close(i);
1393 }
1394
1395 pollinfo.fd = pfd;
1396 pollinfo.pid = getpid();
1397 for (;;) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001398#ifndef FREEBSD
1399 if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
1400#else /* FREEBSD */
1401 if (ioctl(pfd, PIOCWSTOP, &pfs) < 0)
1402#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001403 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001404 switch (errno) {
1405 case EINTR:
1406 continue;
1407 case EBADF:
1408 pollinfo.revents = POLLERR;
1409 break;
1410 case ENOENT:
1411 pollinfo.revents = POLLHUP;
1412 break;
1413 default:
1414 perror("proc_poller: PIOCWSTOP");
1415 }
1416 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1417 _exit(0);
1418 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001419 pollinfo.revents = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001420 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1421 sigsuspend(&empty_set);
1422 }
1423}
1424
1425#endif /* !HAVE_POLLABLE_PROCFS */
1426
1427static int
1428choose_pfd()
1429{
1430 int i, j;
1431 struct tcb *tcp;
1432
1433 static int last;
1434
1435 if (followfork < 2 &&
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001436 last < nprocs && (pollv[last].revents & POLLWANT)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001437 /*
1438 * The previous process is ready to run again. We'll
1439 * let it do so if it is currently in a syscall. This
1440 * heuristic improves the readability of the trace.
1441 */
1442 tcp = pfd2tcb(pollv[last].fd);
1443 if (tcp && (tcp->flags & TCB_INSYSCALL))
1444 return pollv[last].fd;
1445 }
1446
1447 for (i = 0; i < nprocs; i++) {
1448 /* Let competing children run round robin. */
1449 j = (i + last + 1) % nprocs;
1450 if (pollv[j].revents & (POLLHUP | POLLERR)) {
1451 tcp = pfd2tcb(pollv[j].fd);
1452 if (!tcp) {
1453 fprintf(stderr, "strace: lost proc\n");
1454 exit(1);
1455 }
1456 droptcb(tcp);
1457 return -1;
1458 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001459 if (pollv[j].revents & POLLWANT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001460 last = j;
1461 return pollv[j].fd;
1462 }
1463 }
1464 fprintf(stderr, "strace: nothing ready\n");
1465 exit(1);
1466}
1467
1468static int
1469trace()
1470{
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001471#ifdef POLL_HACK
John Hughesd870b3c2002-05-21 11:24:18 +00001472 struct tcb *in_syscall = NULL;
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001473#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001474 struct tcb *tcp;
1475 int pfd;
1476 int what;
1477 int ioctl_result = 0, ioctl_errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001478 long arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001479
1480 for (;;) {
1481 if (interactive)
1482 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1483
1484 if (nprocs == 0)
1485 break;
1486
1487 switch (nprocs) {
1488 case 1:
1489#ifndef HAVE_POLLABLE_PROCFS
1490 if (proc_poll_pipe[0] == -1) {
1491#endif
1492 tcp = pid2tcb(0);
1493 if (!tcp)
1494 continue;
1495 pfd = tcp->pfd;
1496 if (pfd == -1)
1497 continue;
1498 break;
1499#ifndef HAVE_POLLABLE_PROCFS
1500 }
1501 /* fall through ... */
1502#endif /* !HAVE_POLLABLE_PROCFS */
1503 default:
1504#ifdef HAVE_POLLABLE_PROCFS
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001505#ifdef POLL_HACK
1506 /* On some systems (e.g. UnixWare) we get too much ugly
1507 "unfinished..." stuff when multiple proceses are in
1508 syscalls. Here's a nasty hack */
Roland McGrath553a6092002-12-16 20:40:39 +00001509
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001510 if (in_syscall) {
1511 struct pollfd pv;
1512 tcp = in_syscall;
1513 in_syscall = NULL;
1514 pv.fd = tcp->pfd;
1515 pv.events = POLLWANT;
1516 if ((what = poll (&pv, 1, 1)) < 0) {
1517 if (interrupted)
1518 return 0;
1519 continue;
1520 }
1521 else if (what == 1 && pv.revents & POLLWANT) {
1522 goto FOUND;
1523 }
1524 }
1525#endif
1526
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001527 if (poll(pollv, nprocs, INFTIM) < 0) {
1528 if (interrupted)
1529 return 0;
1530 continue;
1531 }
1532#else /* !HAVE_POLLABLE_PROCFS */
1533 if (proc_poll(pollv, nprocs, INFTIM) < 0) {
1534 if (interrupted)
1535 return 0;
1536 continue;
1537 }
1538#endif /* !HAVE_POLLABLE_PROCFS */
1539 pfd = choose_pfd();
1540 if (pfd == -1)
1541 continue;
1542 break;
1543 }
1544
1545 /* Look up `pfd' in our table. */
1546 if ((tcp = pfd2tcb(pfd)) == NULL) {
1547 fprintf(stderr, "unknown pfd: %u\n", pfd);
1548 exit(1);
1549 }
John Hughesb6643082002-05-23 11:02:22 +00001550#ifdef POLL_HACK
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001551 FOUND:
John Hughesb6643082002-05-23 11:02:22 +00001552#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001553 /* Get the status of the process. */
1554 if (!interrupted) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001555#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001556 ioctl_result = IOCTL_WSTOP (tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001557#else /* FREEBSD */
1558 /* Thanks to some scheduling mystery, the first poller
1559 sometimes waits for the already processed end of fork
1560 event. Doing a non blocking poll here solves the problem. */
1561 if (proc_poll_pipe[0] != -1)
1562 ioctl_result = IOCTL_STATUS (tcp);
1563 else
1564 ioctl_result = IOCTL_WSTOP (tcp);
Roland McGrath553a6092002-12-16 20:40:39 +00001565#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001566 ioctl_errno = errno;
1567#ifndef HAVE_POLLABLE_PROCFS
1568 if (proc_poll_pipe[0] != -1) {
1569 if (ioctl_result < 0)
1570 kill(poller_pid, SIGKILL);
1571 else
1572 kill(poller_pid, SIGUSR1);
1573 }
1574#endif /* !HAVE_POLLABLE_PROCFS */
1575 }
1576 if (interrupted)
1577 return 0;
1578
1579 if (interactive)
1580 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1581
1582 if (ioctl_result < 0) {
1583 /* Find out what happened if it failed. */
1584 switch (ioctl_errno) {
1585 case EINTR:
1586 case EBADF:
1587 continue;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001588#ifdef FREEBSD
1589 case ENOTTY:
Roland McGrath553a6092002-12-16 20:40:39 +00001590#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001591 case ENOENT:
1592 droptcb(tcp);
1593 continue;
1594 default:
1595 perror("PIOCWSTOP");
1596 exit(1);
1597 }
1598 }
1599
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +00001600#ifdef FREEBSD
1601 if ((tcp->flags & TCB_STARTUP) && (tcp->status.PR_WHY == PR_SYSEXIT)) {
1602 /* discard first event for a syscall we never entered */
1603 IOCTL (tcp->pfd, PIOCRUN, 0);
1604 continue;
1605 }
Roland McGrath553a6092002-12-16 20:40:39 +00001606#endif
1607
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001608 /* clear the just started flag */
1609 tcp->flags &= ~TCB_STARTUP;
1610
1611 /* set current output file */
1612 outf = tcp->outf;
1613
1614 if (cflag) {
1615 struct timeval stime;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001616#ifdef FREEBSD
1617 char buf[1024];
1618 int len;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001619
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001620 if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) {
1621 buf[len] = '\0';
1622 sscanf(buf,
1623 "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld",
1624 &stime.tv_sec, &stime.tv_usec);
1625 } else
1626 stime.tv_sec = stime.tv_usec = 0;
Roland McGrath553a6092002-12-16 20:40:39 +00001627#else /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001628 stime.tv_sec = tcp->status.pr_stime.tv_sec;
1629 stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001630#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001631 tv_sub(&tcp->dtime, &stime, &tcp->stime);
1632 tcp->stime = stime;
1633 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001634 what = tcp->status.PR_WHAT;
1635 switch (tcp->status.PR_WHY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001636#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001637 case PR_REQUESTED:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001638 if (tcp->status.PR_FLAGS & PR_ASLEEP) {
1639 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001640 if (trace_syscall(tcp) < 0) {
1641 fprintf(stderr, "syscall trouble\n");
1642 exit(1);
1643 }
1644 }
1645 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001646#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001647 case PR_SYSENTRY:
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001648#ifdef POLL_HACK
1649 in_syscall = tcp;
1650#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001651 case PR_SYSEXIT:
1652 if (trace_syscall(tcp) < 0) {
1653 fprintf(stderr, "syscall trouble\n");
1654 exit(1);
1655 }
1656 break;
1657 case PR_SIGNALLED:
1658 if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) {
1659 printleader(tcp);
1660 tprintf("--- %s (%s) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001661 signame(what), strsignal(what));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001662 printtrailer(tcp);
John Hughes58265892001-10-18 15:13:53 +00001663#ifdef PR_INFO
1664 if (tcp->status.PR_INFO.si_signo == what) {
1665 printleader(tcp);
1666 tprintf(" siginfo=");
1667 printsiginfo(&tcp->status.PR_INFO, 1);
1668 printtrailer(tcp);
1669 }
1670#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001671 }
1672 break;
1673 case PR_FAULTED:
1674 if (!cflag && (qual_flags[what] & QUAL_FAULT)) {
1675 printleader(tcp);
1676 tprintf("=== FAULT %d ===", what);
1677 printtrailer(tcp);
1678 }
1679 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001680#ifdef FREEBSD
1681 case 0: /* handle case we polled for nothing */
1682 continue;
Roland McGrath553a6092002-12-16 20:40:39 +00001683#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001684 default:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001685 fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001686 exit(1);
1687 break;
1688 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001689 arg = 0;
Roland McGrath553a6092002-12-16 20:40:39 +00001690#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001691 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Roland McGrath553a6092002-12-16 20:40:39 +00001692#else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001693 if (IOCTL (tcp->pfd, PIOCRUN, 0) < 0) {
Roland McGrath553a6092002-12-16 20:40:39 +00001694#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001695 perror("PIOCRUN");
1696 exit(1);
1697 }
1698 }
1699 return 0;
1700}
1701
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001702#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001703
1704static int
1705trace()
1706{
1707 int pid;
1708 int wait_errno;
1709 int status;
1710 struct tcb *tcp;
1711#ifdef LINUX
1712 struct rusage ru;
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001713#ifdef __WALL
1714 static int wait4_options = __WALL;
1715#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001716#endif /* LINUX */
1717
1718 while (nprocs != 0) {
1719 if (interactive)
1720 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1721#ifdef LINUX
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001722#ifdef __WALL
1723 pid = wait4(-1, &status, wait4_options, cflag ? &ru : NULL);
Roland McGrath5bc05552002-12-17 04:50:47 +00001724 if (pid < 0 && (wait4_options & __WALL) && errno == EINVAL) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001725 /* this kernel does not support __WALL */
1726 wait4_options &= ~__WALL;
1727 errno = 0;
1728 pid = wait4(-1, &status, wait4_options,
1729 cflag ? &ru : NULL);
1730 }
Roland McGrath5bc05552002-12-17 04:50:47 +00001731 if (pid < 0 && !(wait4_options & __WALL) && errno == ECHILD) {
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001732 /* most likely a "cloned" process */
1733 pid = wait4(-1, &status, __WCLONE,
1734 cflag ? &ru : NULL);
1735 if (pid == -1) {
1736 fprintf(stderr, "strace: clone wait4 "
1737 "failed: %s\n", strerror(errno));
1738 }
1739 }
1740#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001741 pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001742#endif /* __WALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001743#endif /* LINUX */
1744#ifdef SUNOS4
1745 pid = wait(&status);
1746#endif /* SUNOS4 */
1747 wait_errno = errno;
1748 if (interactive)
1749 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1750
1751 if (interrupted)
1752 return 0;
1753
1754 if (pid == -1) {
1755 switch (wait_errno) {
1756 case EINTR:
1757 continue;
1758 case ECHILD:
1759 /*
1760 * We would like to verify this case
1761 * but sometimes a race in Solbourne's
1762 * version of SunOS sometimes reports
1763 * ECHILD before sending us SIGCHILD.
1764 */
1765#if 0
1766 if (nprocs == 0)
1767 return 0;
1768 fprintf(stderr, "strace: proc miscount\n");
1769 exit(1);
1770#endif
1771 return 0;
1772 default:
1773 errno = wait_errno;
1774 perror("strace: wait");
1775 return -1;
1776 }
1777 }
1778 if (debug)
1779 fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
1780
1781 /* Look up `pid' in our table. */
1782 if ((tcp = pid2tcb(pid)) == NULL) {
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001783#if 0 /* XXX davidm */ /* WTA: disabled again */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001784 struct tcb *tcpchild;
1785
1786 if ((tcpchild = alloctcb(pid)) == NULL) {
1787 fprintf(stderr, " [tcb table full]\n");
1788 kill(pid, SIGKILL); /* XXX */
1789 return 0;
1790 }
1791 tcpchild->flags |= TCB_ATTACHED;
1792 newoutf(tcpchild);
1793 tcp->nchildren++;
1794 if (!qflag)
1795 fprintf(stderr, "Process %d attached\n", pid);
1796#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001797 fprintf(stderr, "unknown pid: %u\n", pid);
1798 if (WIFSTOPPED(status))
1799 ptrace(PTRACE_CONT, pid, (char *) 1, 0);
1800 exit(1);
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001801#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001802 }
1803 /* set current output file */
1804 outf = tcp->outf;
1805 if (cflag) {
1806#ifdef LINUX
1807 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
1808 tcp->stime = ru.ru_stime;
1809#endif /* !LINUX */
1810 }
1811
1812 if (tcp->flags & TCB_SUSPENDED) {
1813 /*
1814 * Apparently, doing any ptrace() call on a stopped
1815 * process, provokes the kernel to report the process
1816 * status again on a subsequent wait(), even if the
1817 * process has not been actually restarted.
1818 * Since we have inspected the arguments of suspended
1819 * processes we end up here testing for this case.
1820 */
1821 continue;
1822 }
1823 if (WIFSIGNALED(status)) {
1824 if (!cflag
1825 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
1826 printleader(tcp);
1827 tprintf("+++ killed by %s +++",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001828 signame(WTERMSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001829 printtrailer(tcp);
1830 }
1831 droptcb(tcp);
1832 continue;
1833 }
1834 if (WIFEXITED(status)) {
1835 if (debug)
1836 fprintf(stderr, "pid %u exited\n", pid);
1837 if (tcp->flags & TCB_ATTACHED)
1838 fprintf(stderr,
1839 "PANIC: attached pid %u exited\n",
1840 pid);
1841 droptcb(tcp);
1842 continue;
1843 }
1844 if (!WIFSTOPPED(status)) {
1845 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
1846 droptcb(tcp);
1847 continue;
1848 }
1849 if (debug)
1850 fprintf(stderr, "pid %u stopped, [%s]\n",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001851 pid, signame(WSTOPSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001852
1853 if (tcp->flags & TCB_STARTUP) {
1854 /*
1855 * This flag is there to keep us in sync.
1856 * Next time this process stops it should
1857 * really be entering a system call.
1858 */
1859 tcp->flags &= ~TCB_STARTUP;
1860 if (tcp->flags & TCB_ATTACHED) {
1861 /*
1862 * Interestingly, the process may stop
1863 * with STOPSIG equal to some other signal
1864 * than SIGSTOP if we happend to attach
1865 * just before the process takes a signal.
1866 */
1867 if (!WIFSTOPPED(status)) {
1868 fprintf(stderr,
1869 "pid %u not stopped\n", pid);
1870 detach(tcp, WSTOPSIG(status));
1871 continue;
1872 }
1873 }
1874 else {
1875#ifdef SUNOS4
1876 /* A child of us stopped at exec */
1877 if (WSTOPSIG(status) == SIGTRAP && followvfork)
1878 fixvfork(tcp);
1879#endif /* SUNOS4 */
1880 }
1881 if (tcp->flags & TCB_BPTSET) {
1882 if (clearbpt(tcp) < 0) /* Pretty fatal */ {
1883 droptcb(tcp);
1884 cleanup();
1885 return -1;
1886 }
1887 }
1888 goto tracing;
1889 }
1890
1891 if (WSTOPSIG(status) != SIGTRAP) {
1892 if (WSTOPSIG(status) == SIGSTOP &&
1893 (tcp->flags & TCB_SIGTRAPPED)) {
1894 /*
1895 * Trapped attempt to block SIGTRAP
1896 * Hope we are back in control now.
1897 */
1898 tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
1899 if (ptrace(PTRACE_SYSCALL,
1900 pid, (char *) 1, 0) < 0) {
1901 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1902 cleanup();
1903 return -1;
1904 }
1905 continue;
1906 }
1907 if (!cflag
1908 && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001909 unsigned long addr = 0, pc = 0;
1910#ifdef PT_GETSIGINFO
1911# define PSR_RI 41
1912 struct siginfo si;
1913 unsigned long psr;
1914
1915 upeek(pid, PT_CR_IPSR, &psr);
1916 upeek(pid, PT_CR_IIP, &pc);
1917
1918 pc += (psr >> PSR_RI) & 0x3;
1919 ptrace(PT_GETSIGINFO, pid, 0, (long) &si);
1920 addr = (unsigned long) si.si_addr;
1921#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001922 printleader(tcp);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001923 tprintf("--- %s (%s) @ %lx (%lx) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001924 signame(WSTOPSIG(status)),
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001925 strsignal(WSTOPSIG(status)), pc, addr);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001926 printtrailer(tcp);
1927 }
1928 if ((tcp->flags & TCB_ATTACHED) &&
1929 !sigishandled(tcp, WSTOPSIG(status))) {
1930 detach(tcp, WSTOPSIG(status));
1931 continue;
1932 }
1933 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1,
1934 WSTOPSIG(status)) < 0) {
1935 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1936 cleanup();
1937 return -1;
1938 }
1939 tcp->flags &= ~TCB_SUSPENDED;
1940 continue;
1941 }
1942 if (trace_syscall(tcp) < 0) {
1943 if (tcp->flags & TCB_ATTACHED)
1944 detach(tcp, 0);
1945 else {
1946 ptrace(PTRACE_KILL,
1947 tcp->pid, (char *) 1, SIGTERM);
1948 droptcb(tcp);
1949 }
1950 continue;
1951 }
1952 if (tcp->flags & TCB_EXITING) {
1953 if (tcp->flags & TCB_ATTACHED)
1954 detach(tcp, 0);
1955 else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) {
1956 perror("strace: ptrace(PTRACE_CONT, ...)");
1957 cleanup();
1958 return -1;
1959 }
1960 continue;
1961 }
1962 if (tcp->flags & TCB_SUSPENDED) {
1963 if (!qflag)
1964 fprintf(stderr, "Process %u suspended\n", pid);
1965 continue;
1966 }
1967 tracing:
1968 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) {
1969 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1970 cleanup();
1971 return -1;
1972 }
1973 }
1974 return 0;
1975}
1976
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001977#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001978
1979static int curcol;
1980
1981#ifdef __STDC__
1982#include <stdarg.h>
1983#define VA_START(a, b) va_start(a, b)
1984#else
1985#include <varargs.h>
1986#define VA_START(a, b) va_start(a)
1987#endif
1988
1989void
1990#ifdef __STDC__
1991tprintf(const char *fmt, ...)
1992#else
1993tprintf(fmt, va_alist)
1994char *fmt;
1995va_dcl
1996#endif
1997{
1998 va_list args;
1999
2000 VA_START(args, fmt);
2001 if (outf)
2002 curcol += vfprintf(outf, fmt, args);
2003 va_end(args);
2004 return;
2005}
2006
2007void
2008printleader(tcp)
2009struct tcb *tcp;
2010{
2011 if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) {
2012 tcp_last->flags |= TCB_REPRINT;
2013 tprintf(" <unfinished ...>\n");
2014 }
2015 curcol = 0;
2016 if ((followfork == 1 || pflag_seen > 1) && outfname)
2017 tprintf("%-5d ", tcp->pid);
2018 else if (nprocs > 1 && !outfname)
2019 tprintf("[pid %5u] ", tcp->pid);
2020 if (tflag) {
2021 char str[sizeof("HH:MM:SS")];
2022 struct timeval tv, dtv;
2023 static struct timeval otv;
2024
2025 gettimeofday(&tv, NULL);
2026 if (rflag) {
2027 if (otv.tv_sec == 0)
2028 otv = tv;
2029 tv_sub(&dtv, &tv, &otv);
2030 tprintf("%6ld.%06ld ",
2031 (long) dtv.tv_sec, (long) dtv.tv_usec);
2032 otv = tv;
2033 }
2034 else if (tflag > 2) {
2035 tprintf("%ld.%06ld ",
2036 (long) tv.tv_sec, (long) tv.tv_usec);
2037 }
2038 else {
2039 time_t local = tv.tv_sec;
2040 strftime(str, sizeof(str), "%T", localtime(&local));
2041 if (tflag > 1)
2042 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
2043 else
2044 tprintf("%s ", str);
2045 }
2046 }
2047 if (iflag)
2048 printcall(tcp);
2049}
2050
2051void
2052tabto(col)
2053int col;
2054{
2055 if (curcol < col)
2056 tprintf("%*s", col - curcol, "");
2057}
2058
2059void
2060printtrailer(tcp)
2061struct tcb *tcp;
2062{
2063 tprintf("\n");
2064 tcp_last = NULL;
2065}
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002066
Wichert Akkermanea78f0f1999-11-29 15:34:02 +00002067#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002068
2069int mp_ioctl (int fd, int cmd, void *arg, int size) {
2070
2071 struct iovec iov[2];
2072 int n = 1;
Roland McGrath553a6092002-12-16 20:40:39 +00002073
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002074 iov[0].iov_base = &cmd;
2075 iov[0].iov_len = sizeof cmd;
2076 if (arg) {
2077 ++n;
2078 iov[1].iov_base = arg;
2079 iov[1].iov_len = size;
2080 }
Roland McGrath553a6092002-12-16 20:40:39 +00002081
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002082 return writev (fd, iov, n);
2083}
2084
2085#endif