blob: 56f6c1b43e20131c7e526a927415718032e896fc [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>
46
Wichert Akkerman7b3346b2001-10-09 23:47:38 +000047#if defined(IA64) && defined(LINUX)
48# include <asm/ptrace_offsets.h>
49#endif
50
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +000051#ifdef USE_PROCFS
52#include <poll.h>
53#endif
54
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000055#ifdef SVR4
56#include <sys/stropts.h>
Wichert Akkermanea78f0f1999-11-29 15:34:02 +000057#ifdef HAVE_MP_PROCFS
John Hughes1d08dcf2001-07-10 13:48:44 +000058#ifdef HAVE_SYS_UIO_H
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000059#include <sys/uio.h>
60#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000061#endif
John Hughes1d08dcf2001-07-10 13:48:44 +000062#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000063
64int debug = 0, followfork = 0, followvfork = 0, interactive = 0;
65int rflag = 0, tflag = 0, dtime = 0, cflag = 0;
66int iflag = 0, xflag = 0, qflag = 0;
67int pflag_seen = 0;
68
69char *username = NULL;
70uid_t run_uid;
71gid_t run_gid;
72
73int acolumn = DEFAULT_ACOLUMN;
74int max_strlen = DEFAULT_STRLEN;
75char *outfname = NULL;
76FILE *outf;
77struct tcb tcbtab[MAX_PROCS];
78int nprocs;
79char *progname;
80extern char version[];
81extern char **environ;
82
83static struct tcb *pid2tcb P((int pid));
84static int trace P((void));
85static void cleanup P((void));
86static void interrupt P((int sig));
87static sigset_t empty_set, blocked_set;
88
89#ifdef HAVE_SIG_ATOMIC_T
90static volatile sig_atomic_t interrupted;
91#else /* !HAVE_SIG_ATOMIC_T */
92#ifdef __STDC__
93static volatile int interrupted;
94#else /* !__STDC__ */
95static int interrupted;
96#endif /* !__STDC__ */
97#endif /* !HAVE_SIG_ATOMIC_T */
98
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +000099#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000100
101static struct tcb *pfd2tcb P((int pfd));
102static void reaper P((int sig));
103static void rebuild_pollv P((void));
Wichert Akkermane68d61c1999-06-28 13:17:16 +0000104struct pollfd pollv[MAX_PROCS];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000105
106#ifndef HAVE_POLLABLE_PROCFS
107
108static void proc_poll_open P((void));
109static void proc_poller P((int pfd));
110
111struct proc_pollfd {
112 int fd;
113 int revents;
114 int pid;
115};
116
117static int poller_pid;
118static int proc_poll_pipe[2] = { -1, -1 };
119
120#endif /* !HAVE_POLLABLE_PROCFS */
121
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000122#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000123#define POLLWANT POLLWRNORM
124#else
125#define POLLWANT POLLPRI
126#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000127#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000128
129static void
130usage(ofp, exitval)
131FILE *ofp;
132int exitval;
133{
134 fprintf(ofp, "\
135usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\
136 [-p pid] ... [-s strsize] [-u username] [command [arg ...]]\n\
137 or: strace -c [-e expr] ... [-O overhead] [-S sortby] [command [arg ...]]\n\
138-c -- count time, calls, and errors for each syscall and report summary\n\
139-f -- follow forks, -ff -- with output into separate files\n\
140-F -- attempt to follow vforks, -h -- print help message\n\
141-i -- print instruction pointer at time of syscall\n\
142-q -- suppress messages about attaching, detaching, etc.\n\
143-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
144-T -- print time spent in each syscall, -V -- print version\n\
145-v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\
146-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
147-a column -- alignment COLUMN for printing syscall results (default %d)\n\
148-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
149 options: trace, abbrev, verbose, raw, signal, read, or write\n\
150-o file -- send trace output to FILE instead of stderr\n\
151-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
152-p pid -- trace process with process id PID, may be repeated\n\
153-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
154-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
155-u username -- run command as username handling setuid and/or setgid\n\
156", DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
157 exit(exitval);
158}
159
160#ifdef SVR4
161#ifdef MIPS
162void
163foobar()
164{
165}
166#endif /* MIPS */
167#endif /* SVR4 */
168
169int
170main(argc, argv)
171int argc;
172char *argv[];
173{
174 extern int optind;
175 extern char *optarg;
176 struct tcb *tcp;
177 int c, pid = 0;
178 struct sigaction sa;
179
180 static char buf[BUFSIZ];
181
182 progname = argv[0];
183 outf = stderr;
184 interactive = 1;
185 qualify("trace=all");
186 qualify("abbrev=all");
187 qualify("verbose=all");
188 qualify("signal=all");
189 set_sortby(DEFAULT_SORTBY);
190 set_personality(DEFAULT_PERSONALITY);
191 while ((c = getopt(argc, argv,
192 "+cdfFhiqrtTvVxa:e:o:O:p:s:S:u:")) != EOF) {
193 switch (c) {
194 case 'c':
195 cflag++;
196 dtime++;
197 break;
198 case 'd':
199 debug++;
200 break;
201 case 'f':
202 followfork++;
203 break;
204 case 'F':
205 followvfork++;
206 break;
207 case 'h':
208 usage(stdout, 0);
209 break;
210 case 'i':
211 iflag++;
212 break;
213 case 'q':
214 qflag++;
215 break;
216 case 'r':
217 rflag++;
218 tflag++;
219 break;
220 case 't':
221 tflag++;
222 break;
223 case 'T':
224 dtime++;
225 break;
226 case 'x':
227 xflag++;
228 break;
229 case 'v':
230 qualify("abbrev=none");
231 break;
232 case 'V':
233 printf("%s\n", version);
234 exit(0);
235 break;
236 case 'a':
237 acolumn = atoi(optarg);
238 break;
239 case 'e':
240 qualify(optarg);
241 break;
242 case 'o':
243 outfname = strdup(optarg);
244 break;
245 case 'O':
246 set_overhead(atoi(optarg));
247 break;
248 case 'p':
249 if ((pid = atoi(optarg)) == 0) {
250 fprintf(stderr, "%s: Invalid process id: %s\n",
251 progname, optarg);
252 break;
253 }
254 if (pid == getpid()) {
Wichert Akkerman54a47671999-10-17 00:57:34 +0000255 fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000256 break;
257 }
258 if ((tcp = alloctcb(pid)) == NULL) {
259 fprintf(stderr, "%s: tcb table full, please recompile strace\n",
260 progname);
261 exit(1);
262 }
263 tcp->flags |= TCB_ATTACHED;
264 pflag_seen++;
265 break;
266 case 's':
267 max_strlen = atoi(optarg);
268 break;
269 case 'S':
270 set_sortby(optarg);
271 break;
272 case 'u':
273 username = strdup(optarg);
274 break;
275 default:
276 usage(stderr, 1);
277 break;
278 }
279 }
280
281 /* See if they want to run as another user. */
282 if (username != NULL) {
283 struct passwd *pent;
284
285 if (getuid() != 0 || geteuid() != 0) {
286 fprintf(stderr,
287 "%s: you must be root to use the -u option\n",
288 progname);
289 exit(1);
290 }
291 if ((pent = getpwnam(username)) == NULL) {
292 fprintf(stderr, "%s: cannot find user `%s'\n",
293 progname, optarg);
294 exit(1);
295 }
296 run_uid = pent->pw_uid;
297 run_gid = pent->pw_gid;
298 }
299 else {
300 run_uid = getuid();
301 run_gid = getgid();
302 }
303
304#ifndef SVR4
305 setreuid(geteuid(), getuid());
306#endif
307
308 /* See if they want to pipe the output. */
309 if (outfname && (outfname[0] == '|' || outfname[0] == '!')) {
310 if ((outf = popen(outfname + 1, "w")) == NULL) {
311 fprintf(stderr, "%s: can't popen '%s': %s\n",
312 progname, outfname + 1, strerror(errno));
313 exit(1);
314 }
315 free(outfname);
316 outfname = NULL;
317 }
318
319 /* Check if they want to redirect the output. */
320 if (outfname) {
Wichert Akkerman54b4f792001-08-03 11:43:35 +0000321 long f;
322
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000323 if ((outf = fopen(outfname, "w")) == NULL) {
324 fprintf(stderr, "%s: can't fopen '%s': %s\n",
325 progname, outfname, strerror(errno));
326 exit(1);
327 }
Wichert Akkerman54b4f792001-08-03 11:43:35 +0000328
329 if ((f=fcntl(fileno(outf), F_GETFD)) < 0 ) {
330 perror("failed to get flags for outputfile");
331 exit(1);
332 }
333
334 if (fcntl(fileno(outf), F_SETFD, f|FD_CLOEXEC) < 0 ) {
335 perror("failed to set flags for outputfile");
336 exit(1);
337 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000338 }
339
340#ifndef SVR4
341 setreuid(geteuid(), getuid());
342#endif
343
344 if (!outfname) {
345 qflag = 1;
346 setvbuf(outf, buf, _IOLBF, BUFSIZ);
347 }
348 else if (optind < argc)
349 interactive = 0;
350 else
351 qflag = 1;
352
353 for (c = 0, tcp = tcbtab; c < MAX_PROCS; c++, tcp++) {
354 /* Reinitialize the output since it may have changed. */
355 tcp->outf = outf;
356 if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
357 continue;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000358#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000359 if (proc_open(tcp, 1) < 0) {
360 fprintf(stderr, "trouble opening proc file\n");
361 droptcb(tcp);
362 continue;
363 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000364#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000365 if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
366 perror("attach: ptrace(PTRACE_ATTACH, ...)");
367 droptcb(tcp);
368 continue;
369 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000370#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000371 if (!qflag)
372 fprintf(stderr,
373 "Process %u attached - interrupt to quit\n",
374 pid);
375 }
376
377 if (optind < argc) {
378 struct stat statbuf;
379 char *filename;
380 char pathname[MAXPATHLEN];
381
382 filename = argv[optind];
383 if (strchr(filename, '/'))
384 strcpy(pathname, filename);
385#ifdef USE_DEBUGGING_EXEC
386 /*
387 * Debuggers customarily check the current directory
388 * first regardless of the path but doing that gives
389 * security geeks a panic attack.
390 */
391 else if (stat(filename, &statbuf) == 0)
392 strcpy(pathname, filename);
393#endif /* USE_DEBUGGING_EXEC */
394 else {
395 char *path;
396 int m, n, len;
397
398 for (path = getenv("PATH"); path && *path; path += m) {
399 if (strchr(path, ':')) {
400 n = strchr(path, ':') - path;
401 m = n + 1;
402 }
403 else
404 m = n = strlen(path);
405 if (n == 0) {
406 getcwd(pathname, MAXPATHLEN);
407 len = strlen(pathname);
408 }
409 else {
410 strncpy(pathname, path, n);
411 len = n;
412 }
413 if (len && pathname[len - 1] != '/')
414 pathname[len++] = '/';
415 strcpy(pathname + len, filename);
416 if (stat(pathname, &statbuf) == 0)
417 break;
418 }
419 }
420 if (stat(pathname, &statbuf) < 0) {
421 fprintf(stderr, "%s: %s: command not found\n",
422 progname, filename);
423 exit(1);
424 }
425 switch (pid = fork()) {
426 case -1:
427 perror("strace: fork");
428 cleanup();
429 exit(1);
430 break;
431 case 0: {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000432#ifdef USE_PROCFS
433 if (outf != stderr) close (fileno (outf));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000434#ifdef MIPS
435 /* Kludge for SGI, see proc_open for details. */
436 sa.sa_handler = foobar;
437 sa.sa_flags = 0;
438 sigemptyset(&sa.sa_mask);
439 sigaction(SIGINT, &sa, NULL);
440#endif /* MIPS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000441#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000442 pause();
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000443#else /* FREEBSD */
444 kill(getpid(), SIGSTOP); /* stop HERE */
445#endif /* FREEBSD */
446#else /* !USE_PROCFS */
Wichert Akkermanbd4125c2000-06-27 17:28:06 +0000447 if (outf!=stderr)
Wichert Akkerman7987cdf2000-07-05 16:05:39 +0000448 close(fileno (outf));
Wichert Akkermanbd4125c2000-06-27 17:28:06 +0000449
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000450 if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
451 perror("strace: ptrace(PTRACE_TRACEME, ...)");
452 return -1;
453 }
454 if (debug)
455 kill(getpid(), SIGSTOP);
456
457 if (username != NULL || geteuid() == 0) {
458 uid_t run_euid = run_uid;
459 gid_t run_egid = run_gid;
460
461 if (statbuf.st_mode & S_ISUID)
462 run_euid = statbuf.st_uid;
463 if (statbuf.st_mode & S_ISGID)
464 run_egid = statbuf.st_gid;
465
466 /*
467 * It is important to set groups before we
468 * lose privileges on setuid.
469 */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +0000470 if (username != NULL) {
471 if (initgroups(username, run_gid) < 0) {
472 perror("initgroups");
473 exit(1);
474 }
475 if (setregid(run_gid, run_egid) < 0) {
476 perror("setregid");
477 exit(1);
478 }
479 if (setreuid(run_uid, run_euid) < 0) {
480 perror("setreuid");
481 exit(1);
482 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000483 }
484 }
485 else
486 setreuid(run_uid, run_uid);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000487#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000488
489 execv(pathname, &argv[optind]);
490 perror("strace: exec");
491 _exit(1);
492 break;
493 }
494 default:
495 if ((tcp = alloctcb(pid)) == NULL) {
496 fprintf(stderr, "tcb table full\n");
497 cleanup();
498 exit(1);
499 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000500#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000501 if (proc_open(tcp, 0) < 0) {
502 fprintf(stderr, "trouble opening proc file\n");
503 cleanup();
504 exit(1);
505 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000506#endif /* USE_PROCFS */
507#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000508 fake_execve(tcp, pathname, &argv[optind], environ);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000509#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000510 break;
511 }
512 }
513 else if (pflag_seen == 0)
514 usage(stderr, 1);
515
516 sigemptyset(&empty_set);
517 sigemptyset(&blocked_set);
518 sa.sa_handler = SIG_IGN;
519 sigemptyset(&sa.sa_mask);
520 sa.sa_flags = 0;
521 sigaction(SIGTTOU, &sa, NULL);
522 sigaction(SIGTTIN, &sa, NULL);
523 if (interactive) {
524 sigaddset(&blocked_set, SIGHUP);
525 sigaddset(&blocked_set, SIGINT);
526 sigaddset(&blocked_set, SIGQUIT);
527 sigaddset(&blocked_set, SIGPIPE);
528 sigaddset(&blocked_set, SIGTERM);
529 sa.sa_handler = interrupt;
530#ifdef SUNOS4
531 /* POSIX signals on sunos4.1 are a little broken. */
532 sa.sa_flags = SA_INTERRUPT;
533#endif /* SUNOS4 */
534 }
535 sigaction(SIGHUP, &sa, NULL);
536 sigaction(SIGINT, &sa, NULL);
537 sigaction(SIGQUIT, &sa, NULL);
538 sigaction(SIGPIPE, &sa, NULL);
539 sigaction(SIGTERM, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000540#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000541 sa.sa_handler = reaper;
542 sigaction(SIGCHLD, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000543#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000544
545 if (trace() < 0)
546 exit(1);
547 cleanup();
548 exit(0);
549}
550
551void
552newoutf(tcp)
553struct tcb *tcp;
554{
555 char name[MAXPATHLEN];
556 FILE *fp;
557
558 if (outfname && followfork > 1) {
559 sprintf(name, "%s.%u", outfname, tcp->pid);
560#ifndef SVR4
561 setreuid(geteuid(), getuid());
562#endif
563 fp = fopen(name, "w");
564#ifndef SVR4
565 setreuid(geteuid(), getuid());
566#endif
567 if (fp == NULL) {
568 perror("fopen");
569 return;
570 }
571 tcp->outf = fp;
572 }
573 return;
574}
575
576struct tcb *
577alloctcb(pid)
578int pid;
579{
580 int i;
581 struct tcb *tcp;
582
583 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
584 if ((tcp->flags & TCB_INUSE) == 0) {
585 tcp->pid = pid;
586 tcp->parent = NULL;
587 tcp->nchildren = 0;
588 tcp->flags = TCB_INUSE | TCB_STARTUP;
589 tcp->outf = outf; /* Initialise to current out file */
590 tcp->stime.tv_sec = 0;
591 tcp->stime.tv_usec = 0;
592 tcp->pfd = -1;
593 nprocs++;
594 return tcp;
595 }
596 }
597 return NULL;
598}
599
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000600#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000601int
602proc_open(tcp, attaching)
603struct tcb *tcp;
604int attaching;
605{
606 char proc[32];
607 long arg;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000608#ifdef SVR4
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000609 sysset_t sc_enter, sc_exit;
610 sigset_t signals;
611 fltset_t faults;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000612#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000613#ifndef HAVE_POLLABLE_PROCFS
614 static int last_pfd;
615#endif
616
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000617#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000618 /* Open the process pseudo-files in /proc. */
619 sprintf(proc, "/proc/%d/ctl", tcp->pid);
620 if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000621 perror("strace: open(\"/proc/...\", ...)");
622 return -1;
623 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000624 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
625 perror("F_GETFD");
626 return -1;
627 }
628 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
629 perror("F_SETFD");
630 return -1;
631 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000632 sprintf(proc, "/proc/%d/status", tcp->pid);
633 if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
634 perror("strace: open(\"/proc/...\", ...)");
635 return -1;
636 }
637 if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) {
638 perror("F_GETFD");
639 return -1;
640 }
641 if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) {
642 perror("F_SETFD");
643 return -1;
644 }
645 sprintf(proc, "/proc/%d/as", tcp->pid);
646 if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
647 perror("strace: open(\"/proc/...\", ...)");
648 return -1;
649 }
650 if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) {
651 perror("F_GETFD");
652 return -1;
653 }
654 if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) {
655 perror("F_SETFD");
656 return -1;
657 }
658#else
659 /* Open the process pseudo-file in /proc. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000660#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000661 sprintf(proc, "/proc/%d", tcp->pid);
662 if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000663#else /* FREEBSD */
664 sprintf(proc, "/proc/%d/mem", tcp->pid);
665 if ((tcp->pfd = open(proc, O_RDWR)) < 0) {
666#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000667 perror("strace: open(\"/proc/...\", ...)");
668 return -1;
669 }
670 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
671 perror("F_GETFD");
672 return -1;
673 }
674 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
675 perror("F_SETFD");
676 return -1;
677 }
678#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000679#ifdef FREEBSD
680 sprintf(proc, "/proc/%d/regs", tcp->pid);
681 if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) {
682 perror("strace: open(\"/proc/.../regs\", ...)");
683 return -1;
684 }
685 if (cflag) {
686 sprintf(proc, "/proc/%d/status", tcp->pid);
687 if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) {
688 perror("strace: open(\"/proc/.../status\", ...)");
689 return -1;
690 }
691 } else
692 tcp->pfd_status = -1;
693#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000694 rebuild_pollv();
695 if (!attaching) {
696 /*
697 * Wait for the child to pause. Because of a race
698 * condition we have to poll for the event.
699 */
700 for (;;) {
701 if (IOCTL_STATUS (tcp) < 0) {
702 perror("strace: PIOCSTATUS");
703 return -1;
704 }
705 if (tcp->status.PR_FLAGS & PR_ASLEEP)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000706 break;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000707 }
708 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000709#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000710 /* Stop the process so that we own the stop. */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000711 if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000712 perror("strace: PIOCSTOP");
713 return -1;
714 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000715#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000716#ifdef PIOCSET
717 /* Set Run-on-Last-Close. */
718 arg = PR_RLC;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000719 if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000720 perror("PIOCSET PR_RLC");
721 return -1;
722 }
723 /* Set or Reset Inherit-on-Fork. */
724 arg = PR_FORK;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000725 if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000726 perror("PIOC{SET,RESET} PR_FORK");
727 return -1;
728 }
729#else /* !PIOCSET */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000730#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000731 if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
732 perror("PIOCSRLC");
733 return -1;
734 }
735 if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
736 perror("PIOC{S,R}FORK");
737 return -1;
738 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000739#else /* FREEBSD */
740 /* just unset the PF_LINGER flag for the Run-on-Last-Close. */
741 if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) {
742 perror("PIOCGFL");
743 return -1;
744 }
745 arg &= ~PF_LINGER;
746 if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) {
747 perror("PIOCSFL");
748 return -1;
749 }
750#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000751#endif /* !PIOCSET */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000752#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000753 /* Enable all syscall entries. */
754 prfillset(&sc_enter);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000755 if (IOCTL(tcp->pfd, PIOCSENTRY, &sc_enter) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000756 perror("PIOCSENTRY");
757 return -1;
758 }
759 /* Enable all syscall exits. */
760 prfillset(&sc_exit);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000761 if (IOCTL(tcp->pfd, PIOCSEXIT, &sc_exit) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000762 perror("PIOSEXIT");
763 return -1;
764 }
765 /* Enable all signals. */
766 prfillset(&signals);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000767 if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000768 perror("PIOCSTRACE");
769 return -1;
770 }
771 /* Enable all faults. */
772 prfillset(&faults);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000773 if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000774 perror("PIOCSFAULT");
775 return -1;
776 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000777#else /* FREEBSD */
778 /* set events flags. */
779 arg = S_SIG | S_SCE | S_SCX ;
780 if(ioctl(tcp->pfd, PIOCBIS, arg) < 0) {
781 perror("PIOCBIS");
782 return -1;
783 }
784#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000785 if (!attaching) {
786#ifdef MIPS
787 /*
788 * The SGI PRSABORT doesn't work for pause() so
789 * we send it a caught signal to wake it up.
790 */
791 kill(tcp->pid, SIGINT);
792#else /* !MIPS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000793#ifdef PRSABORT
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000794 /* The child is in a pause(), abort it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000795 arg = PRSABORT;
796 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000797 perror("PIOCRUN");
798 return -1;
799 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000800#endif
801#endif /* !MIPS*/
802#ifdef FREEBSD
803 /* wake up the child if it received the SIGSTOP */
804 kill(tcp->pid, SIGCONT);
805#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000806 for (;;) {
807 /* Wait for the child to do something. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000808 if (IOCTL_WSTOP (tcp) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000809 perror("PIOCWSTOP");
810 return -1;
811 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000812 if (tcp->status.PR_WHY == PR_SYSENTRY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000813 tcp->flags &= ~TCB_INSYSCALL;
814 get_scno(tcp);
815 if (tcp->scno == SYS_execve)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000816 break;
817 }
818 /* Set it running: maybe execve will be next. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000819#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000820 arg = 0;
821 if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000822#else /* FREEBSD */
823 if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) {
824#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000825 perror("PIOCRUN");
826 return -1;
827 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000828#ifdef FREEBSD
829 /* handle the case where we "opened" the child before
830 it did the kill -STOP */
831 if (tcp->status.PR_WHY == PR_SIGNALLED &&
832 tcp->status.PR_WHAT == SIGSTOP)
833 kill(tcp->pid, SIGCONT);
834#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000835 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000836#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000837 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000838#else /* FREEBSD */
839 } else {
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +0000840 if (attaching < 2) {
841 /* We are attaching to an already running process.
842 * Try to figure out the state of the process in syscalls,
843 * to handle the first event well.
844 * This is done by having a look at the "wchan" property of the
845 * process, which tells where it is stopped (if it is). */
846 FILE * status;
847 char wchan[20]; /* should be enough */
848
849 sprintf(proc, "/proc/%d/status", tcp->pid);
850 status = fopen(proc, "r");
851 if (status &&
852 (fscanf(status, "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d"
853 "%*d,%*d %*d,%*d %19s", wchan) == 1) &&
854 strcmp(wchan, "nochan") && strcmp(wchan, "spread") &&
855 strcmp(wchan, "stopevent")) {
856 /* The process is asleep in the middle of a syscall.
857 Fake the syscall entry event */
858 tcp->flags &= ~(TCB_INSYSCALL|TCB_STARTUP);
859 tcp->status.PR_WHY = PR_SYSENTRY;
860 trace_syscall(tcp);
861 }
862 if (status)
863 fclose(status);
864 } /* otherwise it's a fork being followed */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000865 }
866#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000867#ifndef HAVE_POLLABLE_PROCFS
868 if (proc_poll_pipe[0] != -1)
869 proc_poller(tcp->pfd);
870 else if (nprocs > 1) {
871 proc_poll_open();
872 proc_poller(last_pfd);
873 proc_poller(tcp->pfd);
874 }
875 last_pfd = tcp->pfd;
876#endif /* !HAVE_POLLABLE_PROCFS */
877 return 0;
878}
879
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000880#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000881
882static struct tcb *
883pid2tcb(pid)
884int pid;
885{
886 int i;
887 struct tcb *tcp;
888
889 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
890 if (pid && tcp->pid != pid)
891 continue;
892 if (tcp->flags & TCB_INUSE)
893 return tcp;
894 }
895 return NULL;
896}
897
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000898#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000899
900static struct tcb *
901pfd2tcb(pfd)
902int pfd;
903{
904 int i;
905 struct tcb *tcp;
906
907 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
908 if (tcp->pfd != pfd)
909 continue;
910 if (tcp->flags & TCB_INUSE)
911 return tcp;
912 }
913 return NULL;
914}
915
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000916#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000917
918void
919droptcb(tcp)
920struct tcb *tcp;
921{
922 if (tcp->pid == 0)
923 return;
924 nprocs--;
925 tcp->pid = 0;
926 tcp->flags = 0;
927 if (tcp->pfd != -1) {
928 close(tcp->pfd);
929 tcp->pfd = -1;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000930#ifdef FREEBSD
931 if (tcp->pfd_reg != -1) {
932 close(tcp->pfd_reg);
933 tcp->pfd_reg = -1;
934 }
935 if (tcp->pfd_status != -1) {
936 close(tcp->pfd_status);
937 tcp->pfd_status = -1;
938 }
939#endif /* !FREEBSD */
940#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000941 rebuild_pollv();
942#endif
943 }
944 if (tcp->parent != NULL) {
945 tcp->parent->nchildren--;
946 tcp->parent = NULL;
947 }
948#if 0
949 if (tcp->outf != stderr)
950 fclose(tcp->outf);
951#endif
952 tcp->outf = 0;
953}
954
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000955#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000956
957static int
958resume(tcp)
959struct tcb *tcp;
960{
961 if (tcp == NULL)
962 return -1;
963
964 if (!(tcp->flags & TCB_SUSPENDED)) {
965 fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid);
966 return -1;
967 }
968 tcp->flags &= ~TCB_SUSPENDED;
969
970 if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) {
971 perror("resume: ptrace(PTRACE_SYSCALL, ...)");
972 return -1;
973 }
974
975 if (!qflag)
976 fprintf(stderr, "Process %u resumed\n", tcp->pid);
977 return 0;
978}
979
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000980#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000981
982/* detach traced process; continue with sig */
983
984static int
985detach(tcp, sig)
986struct tcb *tcp;
987int sig;
988{
989 int error = 0;
990#ifdef LINUX
991 int status;
992#endif
993
994 if (tcp->flags & TCB_BPTSET)
995 sig = SIGKILL;
996
997#ifdef LINUX
998 /*
999 * Linux wrongly insists the child be stopped
1000 * before detaching. Arghh. We go through hoops
1001 * to make a clean break of things.
1002 */
Wichert Akkermandacfb6e1999-06-03 14:21:07 +00001003#if defined(SPARC)
1004#undef PTRACE_DETACH
1005#define PTRACE_DETACH PTRACE_SUNDETACH
1006#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001007 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
1008 /* On a clear day, you can see forever. */
1009 }
1010 else if (errno != ESRCH) {
1011 /* Shouldn't happen. */
1012 perror("detach: ptrace(PTRACE_DETACH, ...)");
1013 }
1014 else if (kill(tcp->pid, 0) < 0) {
1015 if (errno != ESRCH)
1016 perror("detach: checking sanity");
1017 }
1018 else if (kill(tcp->pid, SIGSTOP) < 0) {
1019 if (errno != ESRCH)
1020 perror("detach: stopping child");
1021 }
1022 else {
1023 for (;;) {
1024 if (waitpid(tcp->pid, &status, 0) < 0) {
1025 if (errno != ECHILD)
1026 perror("detach: waiting");
1027 break;
1028 }
1029 if (!WIFSTOPPED(status)) {
1030 /* Au revoir, mon ami. */
1031 break;
1032 }
1033 if (WSTOPSIG(status) == SIGSTOP) {
1034 if ((error = ptrace(PTRACE_DETACH,
1035 tcp->pid, (char *) 1, sig)) < 0) {
1036 if (errno != ESRCH)
1037 perror("detach: ptrace(PTRACE_DETACH, ...)");
1038 /* I died trying. */
1039 }
1040 break;
1041 }
1042 if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1,
1043 WSTOPSIG(status) == SIGTRAP ?
1044 0 : WSTOPSIG(status))) < 0) {
1045 if (errno != ESRCH)
1046 perror("detach: ptrace(PTRACE_CONT, ...)");
1047 break;
1048 }
1049 }
1050 }
1051#endif /* LINUX */
1052
1053#if defined(SUNOS4)
1054 /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
1055 if (sig && kill(tcp->pid, sig) < 0)
1056 perror("detach: kill");
1057 sig = 0;
1058 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0)
1059 perror("detach: ptrace(PTRACE_DETACH, ...)");
1060#endif /* SUNOS4 */
1061
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001062#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001063 if (waiting_parent(tcp))
1064 error = resume(tcp->parent);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001065#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001066
1067 if (!qflag)
1068 fprintf(stderr, "Process %u detached\n", tcp->pid);
1069
1070 droptcb(tcp);
1071 return error;
1072}
1073
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001074#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001075
1076static void
1077reaper(sig)
1078int sig;
1079{
1080 int pid;
1081 int status;
1082
1083 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
1084#if 0
1085 struct tcb *tcp;
1086
1087 tcp = pid2tcb(pid);
1088 if (tcp)
1089 droptcb(tcp);
1090#endif
1091 }
1092}
1093
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001094#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001095
1096static void
1097cleanup()
1098{
1099 int i;
1100 struct tcb *tcp;
1101
1102 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1103 if (!(tcp->flags & TCB_INUSE))
1104 continue;
1105 if (debug)
1106 fprintf(stderr,
1107 "cleanup: looking at pid %u\n", tcp->pid);
1108 if (tcp_last &&
1109 (!outfname || followfork < 2 || tcp_last == tcp)) {
1110 tprintf(" <unfinished ...>\n");
1111 tcp_last = NULL;
1112 }
1113 if (tcp->flags & TCB_ATTACHED)
1114 detach(tcp, 0);
1115 else {
1116 kill(tcp->pid, SIGCONT);
1117 kill(tcp->pid, SIGTERM);
1118 }
1119 }
1120 if (cflag)
1121 call_summary(outf);
1122}
1123
1124static void
1125interrupt(sig)
1126int sig;
1127{
1128 interrupted = 1;
1129}
1130
1131#ifndef HAVE_STRERROR
1132
1133#ifndef SYS_ERRLIST_DECLARED
1134extern int sys_nerr;
1135extern char *sys_errlist[];
1136#endif /* SYS_ERRLIST_DECLARED */
1137
1138const char *
1139strerror(errno)
1140int errno;
1141{
1142 static char buf[64];
1143
1144 if (errno < 1 || errno >= sys_nerr) {
1145 sprintf(buf, "Unknown error %d", errno);
1146 return buf;
1147 }
1148 return sys_errlist[errno];
1149}
1150
1151#endif /* HAVE_STERRROR */
1152
1153#ifndef HAVE_STRSIGNAL
1154
1155#ifndef SYS_SIGLIST_DECLARED
1156#ifdef HAVE__SYS_SIGLIST
1157 extern char *_sys_siglist[];
1158#else
1159 extern char *sys_siglist[];
1160#endif
1161#endif /* SYS_SIGLIST_DECLARED */
1162
1163const char *
1164strsignal(sig)
1165int sig;
1166{
1167 static char buf[64];
1168
1169 if (sig < 1 || sig >= NSIG) {
1170 sprintf(buf, "Unknown signal %d", sig);
1171 return buf;
1172 }
1173#ifdef HAVE__SYS_SIGLIST
1174 return _sys_siglist[sig];
1175#else
1176 return sys_siglist[sig];
1177#endif
1178}
1179
1180#endif /* HAVE_STRSIGNAL */
1181
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001182#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001183
1184static void
1185rebuild_pollv()
1186{
1187 int i, j;
1188 struct tcb *tcp;
1189
1190 for (i = j = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1191 if (!(tcp->flags & TCB_INUSE))
1192 continue;
1193 pollv[j].fd = tcp->pfd;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001194 pollv[j].events = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001195 j++;
1196 }
1197 if (j != nprocs) {
1198 fprintf(stderr, "strace: proc miscount\n");
1199 exit(1);
1200 }
1201}
1202
1203#ifndef HAVE_POLLABLE_PROCFS
1204
1205static void
1206proc_poll_open()
1207{
1208 int arg;
1209 int i;
1210
1211 if (pipe(proc_poll_pipe) < 0) {
1212 perror("pipe");
1213 exit(1);
1214 }
1215 for (i = 0; i < 2; i++) {
1216 if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) {
1217 perror("F_GETFD");
1218 exit(1);
1219 }
1220 if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) {
1221 perror("F_SETFD");
1222 exit(1);
1223 }
1224 }
1225}
1226
1227static int
1228proc_poll(pollv, nfds, timeout)
1229struct pollfd *pollv;
1230int nfds;
1231int timeout;
1232{
1233 int i;
1234 int n;
1235 struct proc_pollfd pollinfo;
1236
1237 if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
1238 return n;
1239 if (n != sizeof(struct proc_pollfd)) {
1240 fprintf(stderr, "panic: short read: %d\n", n);
1241 exit(1);
1242 }
1243 for (i = 0; i < nprocs; i++) {
1244 if (pollv[i].fd == pollinfo.fd)
1245 pollv[i].revents = pollinfo.revents;
1246 else
1247 pollv[i].revents = 0;
1248 }
1249 poller_pid = pollinfo.pid;
1250 return 1;
1251}
1252
1253static void
1254wakeup_handler(sig)
1255int sig;
1256{
1257}
1258
1259static void
1260proc_poller(pfd)
1261int pfd;
1262{
1263 struct proc_pollfd pollinfo;
1264 struct sigaction sa;
1265 sigset_t blocked_set, empty_set;
1266 int i;
1267 int n;
1268 struct rlimit rl;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001269#ifdef FREEBSD
1270 struct procfs_status pfs;
1271#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001272
1273 switch (fork()) {
1274 case -1:
1275 perror("fork");
1276 _exit(0);
1277 case 0:
1278 break;
1279 default:
1280 return;
1281 }
1282
1283 sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
1284 sa.sa_flags = 0;
1285 sigemptyset(&sa.sa_mask);
1286 sigaction(SIGHUP, &sa, NULL);
1287 sigaction(SIGINT, &sa, NULL);
1288 sigaction(SIGQUIT, &sa, NULL);
1289 sigaction(SIGPIPE, &sa, NULL);
1290 sigaction(SIGTERM, &sa, NULL);
1291 sa.sa_handler = wakeup_handler;
1292 sigaction(SIGUSR1, &sa, NULL);
1293 sigemptyset(&blocked_set);
1294 sigaddset(&blocked_set, SIGUSR1);
1295 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1296 sigemptyset(&empty_set);
1297
1298 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
1299 perror("getrlimit(RLIMIT_NOFILE, ...)");
1300 _exit(0);
1301 }
1302 n = rl.rlim_cur;
1303 for (i = 0; i < n; i++) {
1304 if (i != pfd && i != proc_poll_pipe[1])
1305 close(i);
1306 }
1307
1308 pollinfo.fd = pfd;
1309 pollinfo.pid = getpid();
1310 for (;;) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001311#ifndef FREEBSD
1312 if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
1313#else /* FREEBSD */
1314 if (ioctl(pfd, PIOCWSTOP, &pfs) < 0)
1315#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001316 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001317 switch (errno) {
1318 case EINTR:
1319 continue;
1320 case EBADF:
1321 pollinfo.revents = POLLERR;
1322 break;
1323 case ENOENT:
1324 pollinfo.revents = POLLHUP;
1325 break;
1326 default:
1327 perror("proc_poller: PIOCWSTOP");
1328 }
1329 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1330 _exit(0);
1331 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001332 pollinfo.revents = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001333 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1334 sigsuspend(&empty_set);
1335 }
1336}
1337
1338#endif /* !HAVE_POLLABLE_PROCFS */
1339
1340static int
1341choose_pfd()
1342{
1343 int i, j;
1344 struct tcb *tcp;
1345
1346 static int last;
1347
1348 if (followfork < 2 &&
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001349 last < nprocs && (pollv[last].revents & POLLWANT)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001350 /*
1351 * The previous process is ready to run again. We'll
1352 * let it do so if it is currently in a syscall. This
1353 * heuristic improves the readability of the trace.
1354 */
1355 tcp = pfd2tcb(pollv[last].fd);
1356 if (tcp && (tcp->flags & TCB_INSYSCALL))
1357 return pollv[last].fd;
1358 }
1359
1360 for (i = 0; i < nprocs; i++) {
1361 /* Let competing children run round robin. */
1362 j = (i + last + 1) % nprocs;
1363 if (pollv[j].revents & (POLLHUP | POLLERR)) {
1364 tcp = pfd2tcb(pollv[j].fd);
1365 if (!tcp) {
1366 fprintf(stderr, "strace: lost proc\n");
1367 exit(1);
1368 }
1369 droptcb(tcp);
1370 return -1;
1371 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001372 if (pollv[j].revents & POLLWANT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001373 last = j;
1374 return pollv[j].fd;
1375 }
1376 }
1377 fprintf(stderr, "strace: nothing ready\n");
1378 exit(1);
1379}
1380
1381static int
1382trace()
1383{
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001384#ifdef POLL_HACK
1385 struct tcb *in_syscall;
1386#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001387 struct tcb *tcp;
1388 int pfd;
1389 int what;
1390 int ioctl_result = 0, ioctl_errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001391 long arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001392
1393 for (;;) {
1394 if (interactive)
1395 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1396
1397 if (nprocs == 0)
1398 break;
1399
1400 switch (nprocs) {
1401 case 1:
1402#ifndef HAVE_POLLABLE_PROCFS
1403 if (proc_poll_pipe[0] == -1) {
1404#endif
1405 tcp = pid2tcb(0);
1406 if (!tcp)
1407 continue;
1408 pfd = tcp->pfd;
1409 if (pfd == -1)
1410 continue;
1411 break;
1412#ifndef HAVE_POLLABLE_PROCFS
1413 }
1414 /* fall through ... */
1415#endif /* !HAVE_POLLABLE_PROCFS */
1416 default:
1417#ifdef HAVE_POLLABLE_PROCFS
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001418#ifdef POLL_HACK
1419 /* On some systems (e.g. UnixWare) we get too much ugly
1420 "unfinished..." stuff when multiple proceses are in
1421 syscalls. Here's a nasty hack */
1422
1423 if (in_syscall) {
1424 struct pollfd pv;
1425 tcp = in_syscall;
1426 in_syscall = NULL;
1427 pv.fd = tcp->pfd;
1428 pv.events = POLLWANT;
1429 if ((what = poll (&pv, 1, 1)) < 0) {
1430 if (interrupted)
1431 return 0;
1432 continue;
1433 }
1434 else if (what == 1 && pv.revents & POLLWANT) {
1435 goto FOUND;
1436 }
1437 }
1438#endif
1439
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001440 if (poll(pollv, nprocs, INFTIM) < 0) {
1441 if (interrupted)
1442 return 0;
1443 continue;
1444 }
1445#else /* !HAVE_POLLABLE_PROCFS */
1446 if (proc_poll(pollv, nprocs, INFTIM) < 0) {
1447 if (interrupted)
1448 return 0;
1449 continue;
1450 }
1451#endif /* !HAVE_POLLABLE_PROCFS */
1452 pfd = choose_pfd();
1453 if (pfd == -1)
1454 continue;
1455 break;
1456 }
1457
1458 /* Look up `pfd' in our table. */
1459 if ((tcp = pfd2tcb(pfd)) == NULL) {
1460 fprintf(stderr, "unknown pfd: %u\n", pfd);
1461 exit(1);
1462 }
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001463 FOUND:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001464 /* Get the status of the process. */
1465 if (!interrupted) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001466#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001467 ioctl_result = IOCTL_WSTOP (tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001468#else /* FREEBSD */
1469 /* Thanks to some scheduling mystery, the first poller
1470 sometimes waits for the already processed end of fork
1471 event. Doing a non blocking poll here solves the problem. */
1472 if (proc_poll_pipe[0] != -1)
1473 ioctl_result = IOCTL_STATUS (tcp);
1474 else
1475 ioctl_result = IOCTL_WSTOP (tcp);
1476#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001477 ioctl_errno = errno;
1478#ifndef HAVE_POLLABLE_PROCFS
1479 if (proc_poll_pipe[0] != -1) {
1480 if (ioctl_result < 0)
1481 kill(poller_pid, SIGKILL);
1482 else
1483 kill(poller_pid, SIGUSR1);
1484 }
1485#endif /* !HAVE_POLLABLE_PROCFS */
1486 }
1487 if (interrupted)
1488 return 0;
1489
1490 if (interactive)
1491 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1492
1493 if (ioctl_result < 0) {
1494 /* Find out what happened if it failed. */
1495 switch (ioctl_errno) {
1496 case EINTR:
1497 case EBADF:
1498 continue;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001499#ifdef FREEBSD
1500 case ENOTTY:
1501#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001502 case ENOENT:
1503 droptcb(tcp);
1504 continue;
1505 default:
1506 perror("PIOCWSTOP");
1507 exit(1);
1508 }
1509 }
1510
Wichert Akkerman2e4ffe52000-09-03 23:57:48 +00001511#ifdef FREEBSD
1512 if ((tcp->flags & TCB_STARTUP) && (tcp->status.PR_WHY == PR_SYSEXIT)) {
1513 /* discard first event for a syscall we never entered */
1514 IOCTL (tcp->pfd, PIOCRUN, 0);
1515 continue;
1516 }
1517#endif
1518
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001519 /* clear the just started flag */
1520 tcp->flags &= ~TCB_STARTUP;
1521
1522 /* set current output file */
1523 outf = tcp->outf;
1524
1525 if (cflag) {
1526 struct timeval stime;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001527#ifdef FREEBSD
1528 char buf[1024];
1529 int len;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001530
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001531 if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) {
1532 buf[len] = '\0';
1533 sscanf(buf,
1534 "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld",
1535 &stime.tv_sec, &stime.tv_usec);
1536 } else
1537 stime.tv_sec = stime.tv_usec = 0;
1538#else /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001539 stime.tv_sec = tcp->status.pr_stime.tv_sec;
1540 stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001541#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001542 tv_sub(&tcp->dtime, &stime, &tcp->stime);
1543 tcp->stime = stime;
1544 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001545 what = tcp->status.PR_WHAT;
1546 switch (tcp->status.PR_WHY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001547#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001548 case PR_REQUESTED:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001549 if (tcp->status.PR_FLAGS & PR_ASLEEP) {
1550 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001551 if (trace_syscall(tcp) < 0) {
1552 fprintf(stderr, "syscall trouble\n");
1553 exit(1);
1554 }
1555 }
1556 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001557#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001558 case PR_SYSENTRY:
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001559#ifdef POLL_HACK
1560 in_syscall = tcp;
1561#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001562 case PR_SYSEXIT:
1563 if (trace_syscall(tcp) < 0) {
1564 fprintf(stderr, "syscall trouble\n");
1565 exit(1);
1566 }
1567 break;
1568 case PR_SIGNALLED:
1569 if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) {
1570 printleader(tcp);
1571 tprintf("--- %s (%s) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001572 signame(what), strsignal(what));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001573 printtrailer(tcp);
1574 }
1575 break;
1576 case PR_FAULTED:
1577 if (!cflag && (qual_flags[what] & QUAL_FAULT)) {
1578 printleader(tcp);
1579 tprintf("=== FAULT %d ===", what);
1580 printtrailer(tcp);
1581 }
1582 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001583#ifdef FREEBSD
1584 case 0: /* handle case we polled for nothing */
1585 continue;
1586#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001587 default:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001588 fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001589 exit(1);
1590 break;
1591 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001592 arg = 0;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001593#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001594 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001595#else
1596 if (IOCTL (tcp->pfd, PIOCRUN, 0) < 0) {
1597#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001598 perror("PIOCRUN");
1599 exit(1);
1600 }
1601 }
1602 return 0;
1603}
1604
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001605#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001606
1607static int
1608trace()
1609{
1610 int pid;
1611 int wait_errno;
1612 int status;
1613 struct tcb *tcp;
1614#ifdef LINUX
1615 struct rusage ru;
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001616#ifdef __WALL
1617 static int wait4_options = __WALL;
1618#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001619#endif /* LINUX */
1620
1621 while (nprocs != 0) {
1622 if (interactive)
1623 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1624#ifdef LINUX
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001625#ifdef __WALL
1626 pid = wait4(-1, &status, wait4_options, cflag ? &ru : NULL);
1627 if ((wait4_options & __WALL) && errno == EINVAL) {
1628 /* this kernel does not support __WALL */
1629 wait4_options &= ~__WALL;
1630 errno = 0;
1631 pid = wait4(-1, &status, wait4_options,
1632 cflag ? &ru : NULL);
1633 }
1634 if (!(wait4_options & __WALL) && errno == ECHILD) {
1635 /* most likely a "cloned" process */
1636 pid = wait4(-1, &status, __WCLONE,
1637 cflag ? &ru : NULL);
1638 if (pid == -1) {
1639 fprintf(stderr, "strace: clone wait4 "
1640 "failed: %s\n", strerror(errno));
1641 }
1642 }
1643#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001644 pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
Wichert Akkerman2f1d87e2001-03-28 14:40:14 +00001645#endif /* __WALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001646#endif /* LINUX */
1647#ifdef SUNOS4
1648 pid = wait(&status);
1649#endif /* SUNOS4 */
1650 wait_errno = errno;
1651 if (interactive)
1652 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1653
1654 if (interrupted)
1655 return 0;
1656
1657 if (pid == -1) {
1658 switch (wait_errno) {
1659 case EINTR:
1660 continue;
1661 case ECHILD:
1662 /*
1663 * We would like to verify this case
1664 * but sometimes a race in Solbourne's
1665 * version of SunOS sometimes reports
1666 * ECHILD before sending us SIGCHILD.
1667 */
1668#if 0
1669 if (nprocs == 0)
1670 return 0;
1671 fprintf(stderr, "strace: proc miscount\n");
1672 exit(1);
1673#endif
1674 return 0;
1675 default:
1676 errno = wait_errno;
1677 perror("strace: wait");
1678 return -1;
1679 }
1680 }
1681 if (debug)
1682 fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
1683
1684 /* Look up `pid' in our table. */
1685 if ((tcp = pid2tcb(pid)) == NULL) {
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001686#if 0 /* XXX davidm */ /* WTA: disabled again */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001687 struct tcb *tcpchild;
1688
1689 if ((tcpchild = alloctcb(pid)) == NULL) {
1690 fprintf(stderr, " [tcb table full]\n");
1691 kill(pid, SIGKILL); /* XXX */
1692 return 0;
1693 }
1694 tcpchild->flags |= TCB_ATTACHED;
1695 newoutf(tcpchild);
1696 tcp->nchildren++;
1697 if (!qflag)
1698 fprintf(stderr, "Process %d attached\n", pid);
1699#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001700 fprintf(stderr, "unknown pid: %u\n", pid);
1701 if (WIFSTOPPED(status))
1702 ptrace(PTRACE_CONT, pid, (char *) 1, 0);
1703 exit(1);
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001704#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001705 }
1706 /* set current output file */
1707 outf = tcp->outf;
1708 if (cflag) {
1709#ifdef LINUX
1710 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
1711 tcp->stime = ru.ru_stime;
1712#endif /* !LINUX */
1713 }
1714
1715 if (tcp->flags & TCB_SUSPENDED) {
1716 /*
1717 * Apparently, doing any ptrace() call on a stopped
1718 * process, provokes the kernel to report the process
1719 * status again on a subsequent wait(), even if the
1720 * process has not been actually restarted.
1721 * Since we have inspected the arguments of suspended
1722 * processes we end up here testing for this case.
1723 */
1724 continue;
1725 }
1726 if (WIFSIGNALED(status)) {
1727 if (!cflag
1728 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
1729 printleader(tcp);
1730 tprintf("+++ killed by %s +++",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001731 signame(WTERMSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001732 printtrailer(tcp);
1733 }
1734 droptcb(tcp);
1735 continue;
1736 }
1737 if (WIFEXITED(status)) {
1738 if (debug)
1739 fprintf(stderr, "pid %u exited\n", pid);
1740 if (tcp->flags & TCB_ATTACHED)
1741 fprintf(stderr,
1742 "PANIC: attached pid %u exited\n",
1743 pid);
1744 droptcb(tcp);
1745 continue;
1746 }
1747 if (!WIFSTOPPED(status)) {
1748 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
1749 droptcb(tcp);
1750 continue;
1751 }
1752 if (debug)
1753 fprintf(stderr, "pid %u stopped, [%s]\n",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001754 pid, signame(WSTOPSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001755
1756 if (tcp->flags & TCB_STARTUP) {
1757 /*
1758 * This flag is there to keep us in sync.
1759 * Next time this process stops it should
1760 * really be entering a system call.
1761 */
1762 tcp->flags &= ~TCB_STARTUP;
1763 if (tcp->flags & TCB_ATTACHED) {
1764 /*
1765 * Interestingly, the process may stop
1766 * with STOPSIG equal to some other signal
1767 * than SIGSTOP if we happend to attach
1768 * just before the process takes a signal.
1769 */
1770 if (!WIFSTOPPED(status)) {
1771 fprintf(stderr,
1772 "pid %u not stopped\n", pid);
1773 detach(tcp, WSTOPSIG(status));
1774 continue;
1775 }
1776 }
1777 else {
1778#ifdef SUNOS4
1779 /* A child of us stopped at exec */
1780 if (WSTOPSIG(status) == SIGTRAP && followvfork)
1781 fixvfork(tcp);
1782#endif /* SUNOS4 */
1783 }
1784 if (tcp->flags & TCB_BPTSET) {
1785 if (clearbpt(tcp) < 0) /* Pretty fatal */ {
1786 droptcb(tcp);
1787 cleanup();
1788 return -1;
1789 }
1790 }
1791 goto tracing;
1792 }
1793
1794 if (WSTOPSIG(status) != SIGTRAP) {
1795 if (WSTOPSIG(status) == SIGSTOP &&
1796 (tcp->flags & TCB_SIGTRAPPED)) {
1797 /*
1798 * Trapped attempt to block SIGTRAP
1799 * Hope we are back in control now.
1800 */
1801 tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
1802 if (ptrace(PTRACE_SYSCALL,
1803 pid, (char *) 1, 0) < 0) {
1804 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1805 cleanup();
1806 return -1;
1807 }
1808 continue;
1809 }
1810 if (!cflag
1811 && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001812 unsigned long addr = 0, pc = 0;
1813#ifdef PT_GETSIGINFO
1814# define PSR_RI 41
1815 struct siginfo si;
1816 unsigned long psr;
1817
1818 upeek(pid, PT_CR_IPSR, &psr);
1819 upeek(pid, PT_CR_IIP, &pc);
1820
1821 pc += (psr >> PSR_RI) & 0x3;
1822 ptrace(PT_GETSIGINFO, pid, 0, (long) &si);
1823 addr = (unsigned long) si.si_addr;
1824#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001825 printleader(tcp);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001826 tprintf("--- %s (%s) @ %lx (%lx) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001827 signame(WSTOPSIG(status)),
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001828 strsignal(WSTOPSIG(status)), pc, addr);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001829 printtrailer(tcp);
1830 }
1831 if ((tcp->flags & TCB_ATTACHED) &&
1832 !sigishandled(tcp, WSTOPSIG(status))) {
1833 detach(tcp, WSTOPSIG(status));
1834 continue;
1835 }
1836 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1,
1837 WSTOPSIG(status)) < 0) {
1838 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1839 cleanup();
1840 return -1;
1841 }
1842 tcp->flags &= ~TCB_SUSPENDED;
1843 continue;
1844 }
1845 if (trace_syscall(tcp) < 0) {
1846 if (tcp->flags & TCB_ATTACHED)
1847 detach(tcp, 0);
1848 else {
1849 ptrace(PTRACE_KILL,
1850 tcp->pid, (char *) 1, SIGTERM);
1851 droptcb(tcp);
1852 }
1853 continue;
1854 }
1855 if (tcp->flags & TCB_EXITING) {
1856 if (tcp->flags & TCB_ATTACHED)
1857 detach(tcp, 0);
1858 else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) {
1859 perror("strace: ptrace(PTRACE_CONT, ...)");
1860 cleanup();
1861 return -1;
1862 }
1863 continue;
1864 }
1865 if (tcp->flags & TCB_SUSPENDED) {
1866 if (!qflag)
1867 fprintf(stderr, "Process %u suspended\n", pid);
1868 continue;
1869 }
1870 tracing:
1871 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) {
1872 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1873 cleanup();
1874 return -1;
1875 }
1876 }
1877 return 0;
1878}
1879
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001880#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001881
1882static int curcol;
1883
1884#ifdef __STDC__
1885#include <stdarg.h>
1886#define VA_START(a, b) va_start(a, b)
1887#else
1888#include <varargs.h>
1889#define VA_START(a, b) va_start(a)
1890#endif
1891
1892void
1893#ifdef __STDC__
1894tprintf(const char *fmt, ...)
1895#else
1896tprintf(fmt, va_alist)
1897char *fmt;
1898va_dcl
1899#endif
1900{
1901 va_list args;
1902
1903 VA_START(args, fmt);
1904 if (outf)
1905 curcol += vfprintf(outf, fmt, args);
1906 va_end(args);
1907 return;
1908}
1909
1910void
1911printleader(tcp)
1912struct tcb *tcp;
1913{
1914 if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) {
1915 tcp_last->flags |= TCB_REPRINT;
1916 tprintf(" <unfinished ...>\n");
1917 }
1918 curcol = 0;
1919 if ((followfork == 1 || pflag_seen > 1) && outfname)
1920 tprintf("%-5d ", tcp->pid);
1921 else if (nprocs > 1 && !outfname)
1922 tprintf("[pid %5u] ", tcp->pid);
1923 if (tflag) {
1924 char str[sizeof("HH:MM:SS")];
1925 struct timeval tv, dtv;
1926 static struct timeval otv;
1927
1928 gettimeofday(&tv, NULL);
1929 if (rflag) {
1930 if (otv.tv_sec == 0)
1931 otv = tv;
1932 tv_sub(&dtv, &tv, &otv);
1933 tprintf("%6ld.%06ld ",
1934 (long) dtv.tv_sec, (long) dtv.tv_usec);
1935 otv = tv;
1936 }
1937 else if (tflag > 2) {
1938 tprintf("%ld.%06ld ",
1939 (long) tv.tv_sec, (long) tv.tv_usec);
1940 }
1941 else {
1942 time_t local = tv.tv_sec;
1943 strftime(str, sizeof(str), "%T", localtime(&local));
1944 if (tflag > 1)
1945 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
1946 else
1947 tprintf("%s ", str);
1948 }
1949 }
1950 if (iflag)
1951 printcall(tcp);
1952}
1953
1954void
1955tabto(col)
1956int col;
1957{
1958 if (curcol < col)
1959 tprintf("%*s", col - curcol, "");
1960}
1961
1962void
1963printtrailer(tcp)
1964struct tcb *tcp;
1965{
1966 tprintf("\n");
1967 tcp_last = NULL;
1968}
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001969
Wichert Akkermanea78f0f1999-11-29 15:34:02 +00001970#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001971
1972int mp_ioctl (int fd, int cmd, void *arg, int size) {
1973
1974 struct iovec iov[2];
1975 int n = 1;
1976
1977 iov[0].iov_base = &cmd;
1978 iov[0].iov_len = sizeof cmd;
1979 if (arg) {
1980 ++n;
1981 iov[1].iov_base = arg;
1982 iov[1].iov_len = size;
1983 }
1984
1985 return writev (fd, iov, n);
1986}
1987
1988#endif