blob: 3e4b4a610c3b31851ba37a1151a639d8afbaa84b [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 Akkermanbf79f2e2000-09-01 21:03:06 +000047#ifdef USE_PROCFS
48#include <poll.h>
49#endif
50
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000051#ifdef SVR4
52#include <sys/stropts.h>
Wichert Akkermanea78f0f1999-11-29 15:34:02 +000053#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000054#include <sys/uio.h>
55#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000056#endif
57
58int debug = 0, followfork = 0, followvfork = 0, interactive = 0;
59int rflag = 0, tflag = 0, dtime = 0, cflag = 0;
60int iflag = 0, xflag = 0, qflag = 0;
61int pflag_seen = 0;
62
63char *username = NULL;
64uid_t run_uid;
65gid_t run_gid;
66
67int acolumn = DEFAULT_ACOLUMN;
68int max_strlen = DEFAULT_STRLEN;
69char *outfname = NULL;
70FILE *outf;
71struct tcb tcbtab[MAX_PROCS];
72int nprocs;
73char *progname;
74extern char version[];
75extern char **environ;
76
77static struct tcb *pid2tcb P((int pid));
78static int trace P((void));
79static void cleanup P((void));
80static void interrupt P((int sig));
81static sigset_t empty_set, blocked_set;
82
83#ifdef HAVE_SIG_ATOMIC_T
84static volatile sig_atomic_t interrupted;
85#else /* !HAVE_SIG_ATOMIC_T */
86#ifdef __STDC__
87static volatile int interrupted;
88#else /* !__STDC__ */
89static int interrupted;
90#endif /* !__STDC__ */
91#endif /* !HAVE_SIG_ATOMIC_T */
92
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +000093#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000094
95static struct tcb *pfd2tcb P((int pfd));
96static void reaper P((int sig));
97static void rebuild_pollv P((void));
Wichert Akkermane68d61c1999-06-28 13:17:16 +000098struct pollfd pollv[MAX_PROCS];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000099
100#ifndef HAVE_POLLABLE_PROCFS
101
102static void proc_poll_open P((void));
103static void proc_poller P((int pfd));
104
105struct proc_pollfd {
106 int fd;
107 int revents;
108 int pid;
109};
110
111static int poller_pid;
112static int proc_poll_pipe[2] = { -1, -1 };
113
114#endif /* !HAVE_POLLABLE_PROCFS */
115
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000116#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000117#define POLLWANT POLLWRNORM
118#else
119#define POLLWANT POLLPRI
120#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000121#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000122
123static void
124usage(ofp, exitval)
125FILE *ofp;
126int exitval;
127{
128 fprintf(ofp, "\
129usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\
130 [-p pid] ... [-s strsize] [-u username] [command [arg ...]]\n\
131 or: strace -c [-e expr] ... [-O overhead] [-S sortby] [command [arg ...]]\n\
132-c -- count time, calls, and errors for each syscall and report summary\n\
133-f -- follow forks, -ff -- with output into separate files\n\
134-F -- attempt to follow vforks, -h -- print help message\n\
135-i -- print instruction pointer at time of syscall\n\
136-q -- suppress messages about attaching, detaching, etc.\n\
137-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
138-T -- print time spent in each syscall, -V -- print version\n\
139-v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\
140-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
141-a column -- alignment COLUMN for printing syscall results (default %d)\n\
142-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
143 options: trace, abbrev, verbose, raw, signal, read, or write\n\
144-o file -- send trace output to FILE instead of stderr\n\
145-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
146-p pid -- trace process with process id PID, may be repeated\n\
147-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
148-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
149-u username -- run command as username handling setuid and/or setgid\n\
150", DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
151 exit(exitval);
152}
153
154#ifdef SVR4
155#ifdef MIPS
156void
157foobar()
158{
159}
160#endif /* MIPS */
161#endif /* SVR4 */
162
163int
164main(argc, argv)
165int argc;
166char *argv[];
167{
168 extern int optind;
169 extern char *optarg;
170 struct tcb *tcp;
171 int c, pid = 0;
172 struct sigaction sa;
173
174 static char buf[BUFSIZ];
175
176 progname = argv[0];
177 outf = stderr;
178 interactive = 1;
179 qualify("trace=all");
180 qualify("abbrev=all");
181 qualify("verbose=all");
182 qualify("signal=all");
183 set_sortby(DEFAULT_SORTBY);
184 set_personality(DEFAULT_PERSONALITY);
185 while ((c = getopt(argc, argv,
186 "+cdfFhiqrtTvVxa:e:o:O:p:s:S:u:")) != EOF) {
187 switch (c) {
188 case 'c':
189 cflag++;
190 dtime++;
191 break;
192 case 'd':
193 debug++;
194 break;
195 case 'f':
196 followfork++;
197 break;
198 case 'F':
199 followvfork++;
200 break;
201 case 'h':
202 usage(stdout, 0);
203 break;
204 case 'i':
205 iflag++;
206 break;
207 case 'q':
208 qflag++;
209 break;
210 case 'r':
211 rflag++;
212 tflag++;
213 break;
214 case 't':
215 tflag++;
216 break;
217 case 'T':
218 dtime++;
219 break;
220 case 'x':
221 xflag++;
222 break;
223 case 'v':
224 qualify("abbrev=none");
225 break;
226 case 'V':
227 printf("%s\n", version);
228 exit(0);
229 break;
230 case 'a':
231 acolumn = atoi(optarg);
232 break;
233 case 'e':
234 qualify(optarg);
235 break;
236 case 'o':
237 outfname = strdup(optarg);
238 break;
239 case 'O':
240 set_overhead(atoi(optarg));
241 break;
242 case 'p':
243 if ((pid = atoi(optarg)) == 0) {
244 fprintf(stderr, "%s: Invalid process id: %s\n",
245 progname, optarg);
246 break;
247 }
248 if (pid == getpid()) {
Wichert Akkerman54a47671999-10-17 00:57:34 +0000249 fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000250 break;
251 }
252 if ((tcp = alloctcb(pid)) == NULL) {
253 fprintf(stderr, "%s: tcb table full, please recompile strace\n",
254 progname);
255 exit(1);
256 }
257 tcp->flags |= TCB_ATTACHED;
258 pflag_seen++;
259 break;
260 case 's':
261 max_strlen = atoi(optarg);
262 break;
263 case 'S':
264 set_sortby(optarg);
265 break;
266 case 'u':
267 username = strdup(optarg);
268 break;
269 default:
270 usage(stderr, 1);
271 break;
272 }
273 }
274
275 /* See if they want to run as another user. */
276 if (username != NULL) {
277 struct passwd *pent;
278
279 if (getuid() != 0 || geteuid() != 0) {
280 fprintf(stderr,
281 "%s: you must be root to use the -u option\n",
282 progname);
283 exit(1);
284 }
285 if ((pent = getpwnam(username)) == NULL) {
286 fprintf(stderr, "%s: cannot find user `%s'\n",
287 progname, optarg);
288 exit(1);
289 }
290 run_uid = pent->pw_uid;
291 run_gid = pent->pw_gid;
292 }
293 else {
294 run_uid = getuid();
295 run_gid = getgid();
296 }
297
298#ifndef SVR4
299 setreuid(geteuid(), getuid());
300#endif
301
302 /* See if they want to pipe the output. */
303 if (outfname && (outfname[0] == '|' || outfname[0] == '!')) {
304 if ((outf = popen(outfname + 1, "w")) == NULL) {
305 fprintf(stderr, "%s: can't popen '%s': %s\n",
306 progname, outfname + 1, strerror(errno));
307 exit(1);
308 }
309 free(outfname);
310 outfname = NULL;
311 }
312
313 /* Check if they want to redirect the output. */
314 if (outfname) {
315 if ((outf = fopen(outfname, "w")) == NULL) {
316 fprintf(stderr, "%s: can't fopen '%s': %s\n",
317 progname, outfname, strerror(errno));
318 exit(1);
319 }
320 }
321
322#ifndef SVR4
323 setreuid(geteuid(), getuid());
324#endif
325
326 if (!outfname) {
327 qflag = 1;
328 setvbuf(outf, buf, _IOLBF, BUFSIZ);
329 }
330 else if (optind < argc)
331 interactive = 0;
332 else
333 qflag = 1;
334
335 for (c = 0, tcp = tcbtab; c < MAX_PROCS; c++, tcp++) {
336 /* Reinitialize the output since it may have changed. */
337 tcp->outf = outf;
338 if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
339 continue;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000340#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000341 if (proc_open(tcp, 1) < 0) {
342 fprintf(stderr, "trouble opening proc file\n");
343 droptcb(tcp);
344 continue;
345 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000346#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000347 if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
348 perror("attach: ptrace(PTRACE_ATTACH, ...)");
349 droptcb(tcp);
350 continue;
351 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000352#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000353 if (!qflag)
354 fprintf(stderr,
355 "Process %u attached - interrupt to quit\n",
356 pid);
357 }
358
359 if (optind < argc) {
360 struct stat statbuf;
361 char *filename;
362 char pathname[MAXPATHLEN];
363
364 filename = argv[optind];
365 if (strchr(filename, '/'))
366 strcpy(pathname, filename);
367#ifdef USE_DEBUGGING_EXEC
368 /*
369 * Debuggers customarily check the current directory
370 * first regardless of the path but doing that gives
371 * security geeks a panic attack.
372 */
373 else if (stat(filename, &statbuf) == 0)
374 strcpy(pathname, filename);
375#endif /* USE_DEBUGGING_EXEC */
376 else {
377 char *path;
378 int m, n, len;
379
380 for (path = getenv("PATH"); path && *path; path += m) {
381 if (strchr(path, ':')) {
382 n = strchr(path, ':') - path;
383 m = n + 1;
384 }
385 else
386 m = n = strlen(path);
387 if (n == 0) {
388 getcwd(pathname, MAXPATHLEN);
389 len = strlen(pathname);
390 }
391 else {
392 strncpy(pathname, path, n);
393 len = n;
394 }
395 if (len && pathname[len - 1] != '/')
396 pathname[len++] = '/';
397 strcpy(pathname + len, filename);
398 if (stat(pathname, &statbuf) == 0)
399 break;
400 }
401 }
402 if (stat(pathname, &statbuf) < 0) {
403 fprintf(stderr, "%s: %s: command not found\n",
404 progname, filename);
405 exit(1);
406 }
407 switch (pid = fork()) {
408 case -1:
409 perror("strace: fork");
410 cleanup();
411 exit(1);
412 break;
413 case 0: {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000414#ifdef USE_PROCFS
415 if (outf != stderr) close (fileno (outf));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000416#ifdef MIPS
417 /* Kludge for SGI, see proc_open for details. */
418 sa.sa_handler = foobar;
419 sa.sa_flags = 0;
420 sigemptyset(&sa.sa_mask);
421 sigaction(SIGINT, &sa, NULL);
422#endif /* MIPS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000423#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000424 pause();
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000425#else /* FREEBSD */
426 kill(getpid(), SIGSTOP); /* stop HERE */
427#endif /* FREEBSD */
428#else /* !USE_PROCFS */
Wichert Akkermanbd4125c2000-06-27 17:28:06 +0000429 if (outf!=stderr)
Wichert Akkerman7987cdf2000-07-05 16:05:39 +0000430 close(fileno (outf));
Wichert Akkermanbd4125c2000-06-27 17:28:06 +0000431
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000432 if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
433 perror("strace: ptrace(PTRACE_TRACEME, ...)");
434 return -1;
435 }
436 if (debug)
437 kill(getpid(), SIGSTOP);
438
439 if (username != NULL || geteuid() == 0) {
440 uid_t run_euid = run_uid;
441 gid_t run_egid = run_gid;
442
443 if (statbuf.st_mode & S_ISUID)
444 run_euid = statbuf.st_uid;
445 if (statbuf.st_mode & S_ISGID)
446 run_egid = statbuf.st_gid;
447
448 /*
449 * It is important to set groups before we
450 * lose privileges on setuid.
451 */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +0000452 if (username != NULL) {
453 if (initgroups(username, run_gid) < 0) {
454 perror("initgroups");
455 exit(1);
456 }
457 if (setregid(run_gid, run_egid) < 0) {
458 perror("setregid");
459 exit(1);
460 }
461 if (setreuid(run_uid, run_euid) < 0) {
462 perror("setreuid");
463 exit(1);
464 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000465 }
466 }
467 else
468 setreuid(run_uid, run_uid);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000469#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000470
471 execv(pathname, &argv[optind]);
472 perror("strace: exec");
473 _exit(1);
474 break;
475 }
476 default:
477 if ((tcp = alloctcb(pid)) == NULL) {
478 fprintf(stderr, "tcb table full\n");
479 cleanup();
480 exit(1);
481 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000482#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000483 if (proc_open(tcp, 0) < 0) {
484 fprintf(stderr, "trouble opening proc file\n");
485 cleanup();
486 exit(1);
487 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000488#endif /* USE_PROCFS */
489#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000490 fake_execve(tcp, pathname, &argv[optind], environ);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000491#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000492 break;
493 }
494 }
495 else if (pflag_seen == 0)
496 usage(stderr, 1);
497
498 sigemptyset(&empty_set);
499 sigemptyset(&blocked_set);
500 sa.sa_handler = SIG_IGN;
501 sigemptyset(&sa.sa_mask);
502 sa.sa_flags = 0;
503 sigaction(SIGTTOU, &sa, NULL);
504 sigaction(SIGTTIN, &sa, NULL);
505 if (interactive) {
506 sigaddset(&blocked_set, SIGHUP);
507 sigaddset(&blocked_set, SIGINT);
508 sigaddset(&blocked_set, SIGQUIT);
509 sigaddset(&blocked_set, SIGPIPE);
510 sigaddset(&blocked_set, SIGTERM);
511 sa.sa_handler = interrupt;
512#ifdef SUNOS4
513 /* POSIX signals on sunos4.1 are a little broken. */
514 sa.sa_flags = SA_INTERRUPT;
515#endif /* SUNOS4 */
516 }
517 sigaction(SIGHUP, &sa, NULL);
518 sigaction(SIGINT, &sa, NULL);
519 sigaction(SIGQUIT, &sa, NULL);
520 sigaction(SIGPIPE, &sa, NULL);
521 sigaction(SIGTERM, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000522#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000523 sa.sa_handler = reaper;
524 sigaction(SIGCHLD, &sa, NULL);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000525#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000526
527 if (trace() < 0)
528 exit(1);
529 cleanup();
530 exit(0);
531}
532
533void
534newoutf(tcp)
535struct tcb *tcp;
536{
537 char name[MAXPATHLEN];
538 FILE *fp;
539
540 if (outfname && followfork > 1) {
541 sprintf(name, "%s.%u", outfname, tcp->pid);
542#ifndef SVR4
543 setreuid(geteuid(), getuid());
544#endif
545 fp = fopen(name, "w");
546#ifndef SVR4
547 setreuid(geteuid(), getuid());
548#endif
549 if (fp == NULL) {
550 perror("fopen");
551 return;
552 }
553 tcp->outf = fp;
554 }
555 return;
556}
557
558struct tcb *
559alloctcb(pid)
560int pid;
561{
562 int i;
563 struct tcb *tcp;
564
565 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
566 if ((tcp->flags & TCB_INUSE) == 0) {
567 tcp->pid = pid;
568 tcp->parent = NULL;
569 tcp->nchildren = 0;
570 tcp->flags = TCB_INUSE | TCB_STARTUP;
571 tcp->outf = outf; /* Initialise to current out file */
572 tcp->stime.tv_sec = 0;
573 tcp->stime.tv_usec = 0;
574 tcp->pfd = -1;
575 nprocs++;
576 return tcp;
577 }
578 }
579 return NULL;
580}
581
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000582#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000583int
584proc_open(tcp, attaching)
585struct tcb *tcp;
586int attaching;
587{
588 char proc[32];
589 long arg;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000590#ifdef SVR4
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000591 sysset_t sc_enter, sc_exit;
592 sigset_t signals;
593 fltset_t faults;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000594#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000595#ifndef HAVE_POLLABLE_PROCFS
596 static int last_pfd;
597#endif
598
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000599#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000600 /* Open the process pseudo-files in /proc. */
601 sprintf(proc, "/proc/%d/ctl", tcp->pid);
602 if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000603 perror("strace: open(\"/proc/...\", ...)");
604 return -1;
605 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000606 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
607 perror("F_GETFD");
608 return -1;
609 }
610 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
611 perror("F_SETFD");
612 return -1;
613 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000614 sprintf(proc, "/proc/%d/status", tcp->pid);
615 if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
616 perror("strace: open(\"/proc/...\", ...)");
617 return -1;
618 }
619 if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) {
620 perror("F_GETFD");
621 return -1;
622 }
623 if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) {
624 perror("F_SETFD");
625 return -1;
626 }
627 sprintf(proc, "/proc/%d/as", tcp->pid);
628 if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
629 perror("strace: open(\"/proc/...\", ...)");
630 return -1;
631 }
632 if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) {
633 perror("F_GETFD");
634 return -1;
635 }
636 if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) {
637 perror("F_SETFD");
638 return -1;
639 }
640#else
641 /* Open the process pseudo-file in /proc. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000642#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000643 sprintf(proc, "/proc/%d", tcp->pid);
644 if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000645#else /* FREEBSD */
646 sprintf(proc, "/proc/%d/mem", tcp->pid);
647 if ((tcp->pfd = open(proc, O_RDWR)) < 0) {
648#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000649 perror("strace: open(\"/proc/...\", ...)");
650 return -1;
651 }
652 if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
653 perror("F_GETFD");
654 return -1;
655 }
656 if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
657 perror("F_SETFD");
658 return -1;
659 }
660#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000661#ifdef FREEBSD
662 sprintf(proc, "/proc/%d/regs", tcp->pid);
663 if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) {
664 perror("strace: open(\"/proc/.../regs\", ...)");
665 return -1;
666 }
667 if (cflag) {
668 sprintf(proc, "/proc/%d/status", tcp->pid);
669 if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) {
670 perror("strace: open(\"/proc/.../status\", ...)");
671 return -1;
672 }
673 } else
674 tcp->pfd_status = -1;
675#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000676 rebuild_pollv();
677 if (!attaching) {
678 /*
679 * Wait for the child to pause. Because of a race
680 * condition we have to poll for the event.
681 */
682 for (;;) {
683 if (IOCTL_STATUS (tcp) < 0) {
684 perror("strace: PIOCSTATUS");
685 return -1;
686 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000687#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000688 if (tcp->status.PR_FLAGS & PR_ASLEEP)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000689#else
690 if (tcp->status.state == 1)
691#endif
692 break;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000693 }
694 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000695#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000696 /* Stop the process so that we own the stop. */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000697 if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000698 perror("strace: PIOCSTOP");
699 return -1;
700 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000701#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000702#ifdef PIOCSET
703 /* Set Run-on-Last-Close. */
704 arg = PR_RLC;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000705 if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000706 perror("PIOCSET PR_RLC");
707 return -1;
708 }
709 /* Set or Reset Inherit-on-Fork. */
710 arg = PR_FORK;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000711 if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000712 perror("PIOC{SET,RESET} PR_FORK");
713 return -1;
714 }
715#else /* !PIOCSET */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000716#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000717 if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
718 perror("PIOCSRLC");
719 return -1;
720 }
721 if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
722 perror("PIOC{S,R}FORK");
723 return -1;
724 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000725#else /* FREEBSD */
726 /* just unset the PF_LINGER flag for the Run-on-Last-Close. */
727 if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) {
728 perror("PIOCGFL");
729 return -1;
730 }
731 arg &= ~PF_LINGER;
732 if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) {
733 perror("PIOCSFL");
734 return -1;
735 }
736#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000737#endif /* !PIOCSET */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000738#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000739 /* Enable all syscall entries. */
740 prfillset(&sc_enter);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000741 if (IOCTL(tcp->pfd, PIOCSENTRY, &sc_enter) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000742 perror("PIOCSENTRY");
743 return -1;
744 }
745 /* Enable all syscall exits. */
746 prfillset(&sc_exit);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000747 if (IOCTL(tcp->pfd, PIOCSEXIT, &sc_exit) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000748 perror("PIOSEXIT");
749 return -1;
750 }
751 /* Enable all signals. */
752 prfillset(&signals);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000753 if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000754 perror("PIOCSTRACE");
755 return -1;
756 }
757 /* Enable all faults. */
758 prfillset(&faults);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000759 if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000760 perror("PIOCSFAULT");
761 return -1;
762 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000763#else /* FREEBSD */
764 /* set events flags. */
765 arg = S_SIG | S_SCE | S_SCX ;
766 if(ioctl(tcp->pfd, PIOCBIS, arg) < 0) {
767 perror("PIOCBIS");
768 return -1;
769 }
770#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000771 if (!attaching) {
772#ifdef MIPS
773 /*
774 * The SGI PRSABORT doesn't work for pause() so
775 * we send it a caught signal to wake it up.
776 */
777 kill(tcp->pid, SIGINT);
778#else /* !MIPS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000779#ifdef PRSABORT
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000780 /* The child is in a pause(), abort it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000781 arg = PRSABORT;
782 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000783 perror("PIOCRUN");
784 return -1;
785 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000786#endif
787#endif /* !MIPS*/
788#ifdef FREEBSD
789 /* wake up the child if it received the SIGSTOP */
790 kill(tcp->pid, SIGCONT);
791#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000792 for (;;) {
793 /* Wait for the child to do something. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000794 if (IOCTL_WSTOP (tcp) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000795 perror("PIOCWSTOP");
796 return -1;
797 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000798 if (tcp->status.PR_WHY == PR_SYSENTRY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000799 tcp->flags &= ~TCB_INSYSCALL;
800 get_scno(tcp);
801 if (tcp->scno == SYS_execve)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000802 break;
803 }
804 /* Set it running: maybe execve will be next. */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000805#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000806 arg = 0;
807 if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000808#else /* FREEBSD */
809 if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) {
810#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000811 perror("PIOCRUN");
812 return -1;
813 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000814#ifdef FREEBSD
815 /* handle the case where we "opened" the child before
816 it did the kill -STOP */
817 if (tcp->status.PR_WHY == PR_SIGNALLED &&
818 tcp->status.PR_WHAT == SIGSTOP)
819 kill(tcp->pid, SIGCONT);
820#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000821 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000822#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000823 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000824#else /* FREEBSD */
825 } else {
826 /* little hack to show the current syscall */
827 IOCTL_STATUS(tcp);
828 tcp->flags &= ~TCB_INSYSCALL;
829 tcp->status.why = PR_SYSENTRY;
830 trace_syscall(tcp);
831 }
832#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000833#ifndef HAVE_POLLABLE_PROCFS
834 if (proc_poll_pipe[0] != -1)
835 proc_poller(tcp->pfd);
836 else if (nprocs > 1) {
837 proc_poll_open();
838 proc_poller(last_pfd);
839 proc_poller(tcp->pfd);
840 }
841 last_pfd = tcp->pfd;
842#endif /* !HAVE_POLLABLE_PROCFS */
843 return 0;
844}
845
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000846#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000847
848static struct tcb *
849pid2tcb(pid)
850int pid;
851{
852 int i;
853 struct tcb *tcp;
854
855 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
856 if (pid && tcp->pid != pid)
857 continue;
858 if (tcp->flags & TCB_INUSE)
859 return tcp;
860 }
861 return NULL;
862}
863
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000864#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000865
866static struct tcb *
867pfd2tcb(pfd)
868int pfd;
869{
870 int i;
871 struct tcb *tcp;
872
873 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
874 if (tcp->pfd != pfd)
875 continue;
876 if (tcp->flags & TCB_INUSE)
877 return tcp;
878 }
879 return NULL;
880}
881
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000882#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000883
884void
885droptcb(tcp)
886struct tcb *tcp;
887{
888 if (tcp->pid == 0)
889 return;
890 nprocs--;
891 tcp->pid = 0;
892 tcp->flags = 0;
893 if (tcp->pfd != -1) {
894 close(tcp->pfd);
895 tcp->pfd = -1;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000896#ifdef FREEBSD
897 if (tcp->pfd_reg != -1) {
898 close(tcp->pfd_reg);
899 tcp->pfd_reg = -1;
900 }
901 if (tcp->pfd_status != -1) {
902 close(tcp->pfd_status);
903 tcp->pfd_status = -1;
904 }
905#endif /* !FREEBSD */
906#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000907 rebuild_pollv();
908#endif
909 }
910 if (tcp->parent != NULL) {
911 tcp->parent->nchildren--;
912 tcp->parent = NULL;
913 }
914#if 0
915 if (tcp->outf != stderr)
916 fclose(tcp->outf);
917#endif
918 tcp->outf = 0;
919}
920
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000921#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000922
923static int
924resume(tcp)
925struct tcb *tcp;
926{
927 if (tcp == NULL)
928 return -1;
929
930 if (!(tcp->flags & TCB_SUSPENDED)) {
931 fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid);
932 return -1;
933 }
934 tcp->flags &= ~TCB_SUSPENDED;
935
936 if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) {
937 perror("resume: ptrace(PTRACE_SYSCALL, ...)");
938 return -1;
939 }
940
941 if (!qflag)
942 fprintf(stderr, "Process %u resumed\n", tcp->pid);
943 return 0;
944}
945
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000946#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000947
948/* detach traced process; continue with sig */
949
950static int
951detach(tcp, sig)
952struct tcb *tcp;
953int sig;
954{
955 int error = 0;
956#ifdef LINUX
957 int status;
958#endif
959
960 if (tcp->flags & TCB_BPTSET)
961 sig = SIGKILL;
962
963#ifdef LINUX
964 /*
965 * Linux wrongly insists the child be stopped
966 * before detaching. Arghh. We go through hoops
967 * to make a clean break of things.
968 */
Wichert Akkermandacfb6e1999-06-03 14:21:07 +0000969#if defined(SPARC)
970#undef PTRACE_DETACH
971#define PTRACE_DETACH PTRACE_SUNDETACH
972#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000973 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
974 /* On a clear day, you can see forever. */
975 }
976 else if (errno != ESRCH) {
977 /* Shouldn't happen. */
978 perror("detach: ptrace(PTRACE_DETACH, ...)");
979 }
980 else if (kill(tcp->pid, 0) < 0) {
981 if (errno != ESRCH)
982 perror("detach: checking sanity");
983 }
984 else if (kill(tcp->pid, SIGSTOP) < 0) {
985 if (errno != ESRCH)
986 perror("detach: stopping child");
987 }
988 else {
989 for (;;) {
990 if (waitpid(tcp->pid, &status, 0) < 0) {
991 if (errno != ECHILD)
992 perror("detach: waiting");
993 break;
994 }
995 if (!WIFSTOPPED(status)) {
996 /* Au revoir, mon ami. */
997 break;
998 }
999 if (WSTOPSIG(status) == SIGSTOP) {
1000 if ((error = ptrace(PTRACE_DETACH,
1001 tcp->pid, (char *) 1, sig)) < 0) {
1002 if (errno != ESRCH)
1003 perror("detach: ptrace(PTRACE_DETACH, ...)");
1004 /* I died trying. */
1005 }
1006 break;
1007 }
1008 if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1,
1009 WSTOPSIG(status) == SIGTRAP ?
1010 0 : WSTOPSIG(status))) < 0) {
1011 if (errno != ESRCH)
1012 perror("detach: ptrace(PTRACE_CONT, ...)");
1013 break;
1014 }
1015 }
1016 }
1017#endif /* LINUX */
1018
1019#if defined(SUNOS4)
1020 /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
1021 if (sig && kill(tcp->pid, sig) < 0)
1022 perror("detach: kill");
1023 sig = 0;
1024 if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0)
1025 perror("detach: ptrace(PTRACE_DETACH, ...)");
1026#endif /* SUNOS4 */
1027
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001028#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001029 if (waiting_parent(tcp))
1030 error = resume(tcp->parent);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001031#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001032
1033 if (!qflag)
1034 fprintf(stderr, "Process %u detached\n", tcp->pid);
1035
1036 droptcb(tcp);
1037 return error;
1038}
1039
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001040#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001041
1042static void
1043reaper(sig)
1044int sig;
1045{
1046 int pid;
1047 int status;
1048
1049 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
1050#if 0
1051 struct tcb *tcp;
1052
1053 tcp = pid2tcb(pid);
1054 if (tcp)
1055 droptcb(tcp);
1056#endif
1057 }
1058}
1059
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001060#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001061
1062static void
1063cleanup()
1064{
1065 int i;
1066 struct tcb *tcp;
1067
1068 for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1069 if (!(tcp->flags & TCB_INUSE))
1070 continue;
1071 if (debug)
1072 fprintf(stderr,
1073 "cleanup: looking at pid %u\n", tcp->pid);
1074 if (tcp_last &&
1075 (!outfname || followfork < 2 || tcp_last == tcp)) {
1076 tprintf(" <unfinished ...>\n");
1077 tcp_last = NULL;
1078 }
1079 if (tcp->flags & TCB_ATTACHED)
1080 detach(tcp, 0);
1081 else {
1082 kill(tcp->pid, SIGCONT);
1083 kill(tcp->pid, SIGTERM);
1084 }
1085 }
1086 if (cflag)
1087 call_summary(outf);
1088}
1089
1090static void
1091interrupt(sig)
1092int sig;
1093{
1094 interrupted = 1;
1095}
1096
1097#ifndef HAVE_STRERROR
1098
1099#ifndef SYS_ERRLIST_DECLARED
1100extern int sys_nerr;
1101extern char *sys_errlist[];
1102#endif /* SYS_ERRLIST_DECLARED */
1103
1104const char *
1105strerror(errno)
1106int errno;
1107{
1108 static char buf[64];
1109
1110 if (errno < 1 || errno >= sys_nerr) {
1111 sprintf(buf, "Unknown error %d", errno);
1112 return buf;
1113 }
1114 return sys_errlist[errno];
1115}
1116
1117#endif /* HAVE_STERRROR */
1118
1119#ifndef HAVE_STRSIGNAL
1120
1121#ifndef SYS_SIGLIST_DECLARED
1122#ifdef HAVE__SYS_SIGLIST
1123 extern char *_sys_siglist[];
1124#else
1125 extern char *sys_siglist[];
1126#endif
1127#endif /* SYS_SIGLIST_DECLARED */
1128
1129const char *
1130strsignal(sig)
1131int sig;
1132{
1133 static char buf[64];
1134
1135 if (sig < 1 || sig >= NSIG) {
1136 sprintf(buf, "Unknown signal %d", sig);
1137 return buf;
1138 }
1139#ifdef HAVE__SYS_SIGLIST
1140 return _sys_siglist[sig];
1141#else
1142 return sys_siglist[sig];
1143#endif
1144}
1145
1146#endif /* HAVE_STRSIGNAL */
1147
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001148#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001149
1150static void
1151rebuild_pollv()
1152{
1153 int i, j;
1154 struct tcb *tcp;
1155
1156 for (i = j = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
1157 if (!(tcp->flags & TCB_INUSE))
1158 continue;
1159 pollv[j].fd = tcp->pfd;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001160 pollv[j].events = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001161 j++;
1162 }
1163 if (j != nprocs) {
1164 fprintf(stderr, "strace: proc miscount\n");
1165 exit(1);
1166 }
1167}
1168
1169#ifndef HAVE_POLLABLE_PROCFS
1170
1171static void
1172proc_poll_open()
1173{
1174 int arg;
1175 int i;
1176
1177 if (pipe(proc_poll_pipe) < 0) {
1178 perror("pipe");
1179 exit(1);
1180 }
1181 for (i = 0; i < 2; i++) {
1182 if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) {
1183 perror("F_GETFD");
1184 exit(1);
1185 }
1186 if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) {
1187 perror("F_SETFD");
1188 exit(1);
1189 }
1190 }
1191}
1192
1193static int
1194proc_poll(pollv, nfds, timeout)
1195struct pollfd *pollv;
1196int nfds;
1197int timeout;
1198{
1199 int i;
1200 int n;
1201 struct proc_pollfd pollinfo;
1202
1203 if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
1204 return n;
1205 if (n != sizeof(struct proc_pollfd)) {
1206 fprintf(stderr, "panic: short read: %d\n", n);
1207 exit(1);
1208 }
1209 for (i = 0; i < nprocs; i++) {
1210 if (pollv[i].fd == pollinfo.fd)
1211 pollv[i].revents = pollinfo.revents;
1212 else
1213 pollv[i].revents = 0;
1214 }
1215 poller_pid = pollinfo.pid;
1216 return 1;
1217}
1218
1219static void
1220wakeup_handler(sig)
1221int sig;
1222{
1223}
1224
1225static void
1226proc_poller(pfd)
1227int pfd;
1228{
1229 struct proc_pollfd pollinfo;
1230 struct sigaction sa;
1231 sigset_t blocked_set, empty_set;
1232 int i;
1233 int n;
1234 struct rlimit rl;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001235#ifdef FREEBSD
1236 struct procfs_status pfs;
1237#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001238
1239 switch (fork()) {
1240 case -1:
1241 perror("fork");
1242 _exit(0);
1243 case 0:
1244 break;
1245 default:
1246 return;
1247 }
1248
1249 sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
1250 sa.sa_flags = 0;
1251 sigemptyset(&sa.sa_mask);
1252 sigaction(SIGHUP, &sa, NULL);
1253 sigaction(SIGINT, &sa, NULL);
1254 sigaction(SIGQUIT, &sa, NULL);
1255 sigaction(SIGPIPE, &sa, NULL);
1256 sigaction(SIGTERM, &sa, NULL);
1257 sa.sa_handler = wakeup_handler;
1258 sigaction(SIGUSR1, &sa, NULL);
1259 sigemptyset(&blocked_set);
1260 sigaddset(&blocked_set, SIGUSR1);
1261 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1262 sigemptyset(&empty_set);
1263
1264 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
1265 perror("getrlimit(RLIMIT_NOFILE, ...)");
1266 _exit(0);
1267 }
1268 n = rl.rlim_cur;
1269 for (i = 0; i < n; i++) {
1270 if (i != pfd && i != proc_poll_pipe[1])
1271 close(i);
1272 }
1273
1274 pollinfo.fd = pfd;
1275 pollinfo.pid = getpid();
1276 for (;;) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001277#ifndef FREEBSD
1278 if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
1279#else /* FREEBSD */
1280 if (ioctl(pfd, PIOCWSTOP, &pfs) < 0)
1281#endif /* FREEBSD */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001282 {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001283 switch (errno) {
1284 case EINTR:
1285 continue;
1286 case EBADF:
1287 pollinfo.revents = POLLERR;
1288 break;
1289 case ENOENT:
1290 pollinfo.revents = POLLHUP;
1291 break;
1292 default:
1293 perror("proc_poller: PIOCWSTOP");
1294 }
1295 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1296 _exit(0);
1297 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001298 pollinfo.revents = POLLWANT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001299 write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
1300 sigsuspend(&empty_set);
1301 }
1302}
1303
1304#endif /* !HAVE_POLLABLE_PROCFS */
1305
1306static int
1307choose_pfd()
1308{
1309 int i, j;
1310 struct tcb *tcp;
1311
1312 static int last;
1313
1314 if (followfork < 2 &&
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001315 last < nprocs && (pollv[last].revents & POLLWANT)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001316 /*
1317 * The previous process is ready to run again. We'll
1318 * let it do so if it is currently in a syscall. This
1319 * heuristic improves the readability of the trace.
1320 */
1321 tcp = pfd2tcb(pollv[last].fd);
1322 if (tcp && (tcp->flags & TCB_INSYSCALL))
1323 return pollv[last].fd;
1324 }
1325
1326 for (i = 0; i < nprocs; i++) {
1327 /* Let competing children run round robin. */
1328 j = (i + last + 1) % nprocs;
1329 if (pollv[j].revents & (POLLHUP | POLLERR)) {
1330 tcp = pfd2tcb(pollv[j].fd);
1331 if (!tcp) {
1332 fprintf(stderr, "strace: lost proc\n");
1333 exit(1);
1334 }
1335 droptcb(tcp);
1336 return -1;
1337 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001338 if (pollv[j].revents & POLLWANT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001339 last = j;
1340 return pollv[j].fd;
1341 }
1342 }
1343 fprintf(stderr, "strace: nothing ready\n");
1344 exit(1);
1345}
1346
1347static int
1348trace()
1349{
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001350#ifdef POLL_HACK
1351 struct tcb *in_syscall;
1352#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001353 struct tcb *tcp;
1354 int pfd;
1355 int what;
1356 int ioctl_result = 0, ioctl_errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001357 long arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001358
1359 for (;;) {
1360 if (interactive)
1361 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1362
1363 if (nprocs == 0)
1364 break;
1365
1366 switch (nprocs) {
1367 case 1:
1368#ifndef HAVE_POLLABLE_PROCFS
1369 if (proc_poll_pipe[0] == -1) {
1370#endif
1371 tcp = pid2tcb(0);
1372 if (!tcp)
1373 continue;
1374 pfd = tcp->pfd;
1375 if (pfd == -1)
1376 continue;
1377 break;
1378#ifndef HAVE_POLLABLE_PROCFS
1379 }
1380 /* fall through ... */
1381#endif /* !HAVE_POLLABLE_PROCFS */
1382 default:
1383#ifdef HAVE_POLLABLE_PROCFS
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001384#ifdef POLL_HACK
1385 /* On some systems (e.g. UnixWare) we get too much ugly
1386 "unfinished..." stuff when multiple proceses are in
1387 syscalls. Here's a nasty hack */
1388
1389 if (in_syscall) {
1390 struct pollfd pv;
1391 tcp = in_syscall;
1392 in_syscall = NULL;
1393 pv.fd = tcp->pfd;
1394 pv.events = POLLWANT;
1395 if ((what = poll (&pv, 1, 1)) < 0) {
1396 if (interrupted)
1397 return 0;
1398 continue;
1399 }
1400 else if (what == 1 && pv.revents & POLLWANT) {
1401 goto FOUND;
1402 }
1403 }
1404#endif
1405
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001406 if (poll(pollv, nprocs, INFTIM) < 0) {
1407 if (interrupted)
1408 return 0;
1409 continue;
1410 }
1411#else /* !HAVE_POLLABLE_PROCFS */
1412 if (proc_poll(pollv, nprocs, INFTIM) < 0) {
1413 if (interrupted)
1414 return 0;
1415 continue;
1416 }
1417#endif /* !HAVE_POLLABLE_PROCFS */
1418 pfd = choose_pfd();
1419 if (pfd == -1)
1420 continue;
1421 break;
1422 }
1423
1424 /* Look up `pfd' in our table. */
1425 if ((tcp = pfd2tcb(pfd)) == NULL) {
1426 fprintf(stderr, "unknown pfd: %u\n", pfd);
1427 exit(1);
1428 }
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001429 FOUND:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001430 /* Get the status of the process. */
1431 if (!interrupted) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001432#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001433 ioctl_result = IOCTL_WSTOP (tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001434#else /* FREEBSD */
1435 /* Thanks to some scheduling mystery, the first poller
1436 sometimes waits for the already processed end of fork
1437 event. Doing a non blocking poll here solves the problem. */
1438 if (proc_poll_pipe[0] != -1)
1439 ioctl_result = IOCTL_STATUS (tcp);
1440 else
1441 ioctl_result = IOCTL_WSTOP (tcp);
1442#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001443 ioctl_errno = errno;
1444#ifndef HAVE_POLLABLE_PROCFS
1445 if (proc_poll_pipe[0] != -1) {
1446 if (ioctl_result < 0)
1447 kill(poller_pid, SIGKILL);
1448 else
1449 kill(poller_pid, SIGUSR1);
1450 }
1451#endif /* !HAVE_POLLABLE_PROCFS */
1452 }
1453 if (interrupted)
1454 return 0;
1455
1456 if (interactive)
1457 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1458
1459 if (ioctl_result < 0) {
1460 /* Find out what happened if it failed. */
1461 switch (ioctl_errno) {
1462 case EINTR:
1463 case EBADF:
1464 continue;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001465#ifdef FREEBSD
1466 case ENOTTY:
1467#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001468 case ENOENT:
1469 droptcb(tcp);
1470 continue;
1471 default:
1472 perror("PIOCWSTOP");
1473 exit(1);
1474 }
1475 }
1476
1477 /* clear the just started flag */
1478 tcp->flags &= ~TCB_STARTUP;
1479
1480 /* set current output file */
1481 outf = tcp->outf;
1482
1483 if (cflag) {
1484 struct timeval stime;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001485#ifdef FREEBSD
1486 char buf[1024];
1487 int len;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001488
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001489 if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) {
1490 buf[len] = '\0';
1491 sscanf(buf,
1492 "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld",
1493 &stime.tv_sec, &stime.tv_usec);
1494 } else
1495 stime.tv_sec = stime.tv_usec = 0;
1496#else /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001497 stime.tv_sec = tcp->status.pr_stime.tv_sec;
1498 stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001499#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001500 tv_sub(&tcp->dtime, &stime, &tcp->stime);
1501 tcp->stime = stime;
1502 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001503 what = tcp->status.PR_WHAT;
1504 switch (tcp->status.PR_WHY) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001505#ifndef FREEBSD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001506 case PR_REQUESTED:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001507 if (tcp->status.PR_FLAGS & PR_ASLEEP) {
1508 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001509 if (trace_syscall(tcp) < 0) {
1510 fprintf(stderr, "syscall trouble\n");
1511 exit(1);
1512 }
1513 }
1514 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001515#endif /* !FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001516 case PR_SYSENTRY:
Wichert Akkerman9dbf1541999-11-26 13:11:29 +00001517#ifdef POLL_HACK
1518 in_syscall = tcp;
1519#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001520 case PR_SYSEXIT:
1521 if (trace_syscall(tcp) < 0) {
1522 fprintf(stderr, "syscall trouble\n");
1523 exit(1);
1524 }
1525 break;
1526 case PR_SIGNALLED:
1527 if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) {
1528 printleader(tcp);
1529 tprintf("--- %s (%s) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001530 signame(what), strsignal(what));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001531 printtrailer(tcp);
1532 }
1533 break;
1534 case PR_FAULTED:
1535 if (!cflag && (qual_flags[what] & QUAL_FAULT)) {
1536 printleader(tcp);
1537 tprintf("=== FAULT %d ===", what);
1538 printtrailer(tcp);
1539 }
1540 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001541#ifdef FREEBSD
1542 case 0: /* handle case we polled for nothing */
1543 continue;
1544#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001545 default:
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001546 fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001547 exit(1);
1548 break;
1549 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001550 arg = 0;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001551#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001552 if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001553#else
1554 if (IOCTL (tcp->pfd, PIOCRUN, 0) < 0) {
1555#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001556 perror("PIOCRUN");
1557 exit(1);
1558 }
1559 }
1560 return 0;
1561}
1562
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001563#else /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001564
1565static int
1566trace()
1567{
1568 int pid;
1569 int wait_errno;
1570 int status;
1571 struct tcb *tcp;
1572#ifdef LINUX
1573 struct rusage ru;
1574#endif /* LINUX */
1575
1576 while (nprocs != 0) {
1577 if (interactive)
1578 sigprocmask(SIG_SETMASK, &empty_set, NULL);
1579#ifdef LINUX
1580 pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
1581#endif /* LINUX */
1582#ifdef SUNOS4
1583 pid = wait(&status);
1584#endif /* SUNOS4 */
1585 wait_errno = errno;
1586 if (interactive)
1587 sigprocmask(SIG_BLOCK, &blocked_set, NULL);
1588
1589 if (interrupted)
1590 return 0;
1591
1592 if (pid == -1) {
1593 switch (wait_errno) {
1594 case EINTR:
1595 continue;
1596 case ECHILD:
1597 /*
1598 * We would like to verify this case
1599 * but sometimes a race in Solbourne's
1600 * version of SunOS sometimes reports
1601 * ECHILD before sending us SIGCHILD.
1602 */
1603#if 0
1604 if (nprocs == 0)
1605 return 0;
1606 fprintf(stderr, "strace: proc miscount\n");
1607 exit(1);
1608#endif
1609 return 0;
1610 default:
1611 errno = wait_errno;
1612 perror("strace: wait");
1613 return -1;
1614 }
1615 }
1616 if (debug)
1617 fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
1618
1619 /* Look up `pid' in our table. */
1620 if ((tcp = pid2tcb(pid)) == NULL) {
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001621#if 0 /* XXX davidm */ /* WTA: disabled again */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001622 struct tcb *tcpchild;
1623
1624 if ((tcpchild = alloctcb(pid)) == NULL) {
1625 fprintf(stderr, " [tcb table full]\n");
1626 kill(pid, SIGKILL); /* XXX */
1627 return 0;
1628 }
1629 tcpchild->flags |= TCB_ATTACHED;
1630 newoutf(tcpchild);
1631 tcp->nchildren++;
1632 if (!qflag)
1633 fprintf(stderr, "Process %d attached\n", pid);
1634#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001635 fprintf(stderr, "unknown pid: %u\n", pid);
1636 if (WIFSTOPPED(status))
1637 ptrace(PTRACE_CONT, pid, (char *) 1, 0);
1638 exit(1);
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001639#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001640 }
1641 /* set current output file */
1642 outf = tcp->outf;
1643 if (cflag) {
1644#ifdef LINUX
1645 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
1646 tcp->stime = ru.ru_stime;
1647#endif /* !LINUX */
1648 }
1649
1650 if (tcp->flags & TCB_SUSPENDED) {
1651 /*
1652 * Apparently, doing any ptrace() call on a stopped
1653 * process, provokes the kernel to report the process
1654 * status again on a subsequent wait(), even if the
1655 * process has not been actually restarted.
1656 * Since we have inspected the arguments of suspended
1657 * processes we end up here testing for this case.
1658 */
1659 continue;
1660 }
1661 if (WIFSIGNALED(status)) {
1662 if (!cflag
1663 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
1664 printleader(tcp);
1665 tprintf("+++ killed by %s +++",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001666 signame(WTERMSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001667 printtrailer(tcp);
1668 }
1669 droptcb(tcp);
1670 continue;
1671 }
1672 if (WIFEXITED(status)) {
1673 if (debug)
1674 fprintf(stderr, "pid %u exited\n", pid);
1675 if (tcp->flags & TCB_ATTACHED)
1676 fprintf(stderr,
1677 "PANIC: attached pid %u exited\n",
1678 pid);
1679 droptcb(tcp);
1680 continue;
1681 }
1682 if (!WIFSTOPPED(status)) {
1683 fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
1684 droptcb(tcp);
1685 continue;
1686 }
1687 if (debug)
1688 fprintf(stderr, "pid %u stopped, [%s]\n",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001689 pid, signame(WSTOPSIG(status)));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001690
1691 if (tcp->flags & TCB_STARTUP) {
1692 /*
1693 * This flag is there to keep us in sync.
1694 * Next time this process stops it should
1695 * really be entering a system call.
1696 */
1697 tcp->flags &= ~TCB_STARTUP;
1698 if (tcp->flags & TCB_ATTACHED) {
1699 /*
1700 * Interestingly, the process may stop
1701 * with STOPSIG equal to some other signal
1702 * than SIGSTOP if we happend to attach
1703 * just before the process takes a signal.
1704 */
1705 if (!WIFSTOPPED(status)) {
1706 fprintf(stderr,
1707 "pid %u not stopped\n", pid);
1708 detach(tcp, WSTOPSIG(status));
1709 continue;
1710 }
1711 }
1712 else {
1713#ifdef SUNOS4
1714 /* A child of us stopped at exec */
1715 if (WSTOPSIG(status) == SIGTRAP && followvfork)
1716 fixvfork(tcp);
1717#endif /* SUNOS4 */
1718 }
1719 if (tcp->flags & TCB_BPTSET) {
1720 if (clearbpt(tcp) < 0) /* Pretty fatal */ {
1721 droptcb(tcp);
1722 cleanup();
1723 return -1;
1724 }
1725 }
1726 goto tracing;
1727 }
1728
1729 if (WSTOPSIG(status) != SIGTRAP) {
1730 if (WSTOPSIG(status) == SIGSTOP &&
1731 (tcp->flags & TCB_SIGTRAPPED)) {
1732 /*
1733 * Trapped attempt to block SIGTRAP
1734 * Hope we are back in control now.
1735 */
1736 tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
1737 if (ptrace(PTRACE_SYSCALL,
1738 pid, (char *) 1, 0) < 0) {
1739 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1740 cleanup();
1741 return -1;
1742 }
1743 continue;
1744 }
1745 if (!cflag
1746 && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
1747 printleader(tcp);
1748 tprintf("--- %s (%s) ---",
Nate Sammonsce780fc1999-03-29 23:23:13 +00001749 signame(WSTOPSIG(status)),
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001750 strsignal(WSTOPSIG(status)));
1751 printtrailer(tcp);
1752 }
1753 if ((tcp->flags & TCB_ATTACHED) &&
1754 !sigishandled(tcp, WSTOPSIG(status))) {
1755 detach(tcp, WSTOPSIG(status));
1756 continue;
1757 }
1758 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1,
1759 WSTOPSIG(status)) < 0) {
1760 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1761 cleanup();
1762 return -1;
1763 }
1764 tcp->flags &= ~TCB_SUSPENDED;
1765 continue;
1766 }
1767 if (trace_syscall(tcp) < 0) {
1768 if (tcp->flags & TCB_ATTACHED)
1769 detach(tcp, 0);
1770 else {
1771 ptrace(PTRACE_KILL,
1772 tcp->pid, (char *) 1, SIGTERM);
1773 droptcb(tcp);
1774 }
1775 continue;
1776 }
1777 if (tcp->flags & TCB_EXITING) {
1778 if (tcp->flags & TCB_ATTACHED)
1779 detach(tcp, 0);
1780 else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) {
1781 perror("strace: ptrace(PTRACE_CONT, ...)");
1782 cleanup();
1783 return -1;
1784 }
1785 continue;
1786 }
1787 if (tcp->flags & TCB_SUSPENDED) {
1788 if (!qflag)
1789 fprintf(stderr, "Process %u suspended\n", pid);
1790 continue;
1791 }
1792 tracing:
1793 if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) {
1794 perror("trace: ptrace(PTRACE_SYSCALL, ...)");
1795 cleanup();
1796 return -1;
1797 }
1798 }
1799 return 0;
1800}
1801
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001802#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001803
1804static int curcol;
1805
1806#ifdef __STDC__
1807#include <stdarg.h>
1808#define VA_START(a, b) va_start(a, b)
1809#else
1810#include <varargs.h>
1811#define VA_START(a, b) va_start(a)
1812#endif
1813
1814void
1815#ifdef __STDC__
1816tprintf(const char *fmt, ...)
1817#else
1818tprintf(fmt, va_alist)
1819char *fmt;
1820va_dcl
1821#endif
1822{
1823 va_list args;
1824
1825 VA_START(args, fmt);
1826 if (outf)
1827 curcol += vfprintf(outf, fmt, args);
1828 va_end(args);
1829 return;
1830}
1831
1832void
1833printleader(tcp)
1834struct tcb *tcp;
1835{
1836 if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) {
1837 tcp_last->flags |= TCB_REPRINT;
1838 tprintf(" <unfinished ...>\n");
1839 }
1840 curcol = 0;
1841 if ((followfork == 1 || pflag_seen > 1) && outfname)
1842 tprintf("%-5d ", tcp->pid);
1843 else if (nprocs > 1 && !outfname)
1844 tprintf("[pid %5u] ", tcp->pid);
1845 if (tflag) {
1846 char str[sizeof("HH:MM:SS")];
1847 struct timeval tv, dtv;
1848 static struct timeval otv;
1849
1850 gettimeofday(&tv, NULL);
1851 if (rflag) {
1852 if (otv.tv_sec == 0)
1853 otv = tv;
1854 tv_sub(&dtv, &tv, &otv);
1855 tprintf("%6ld.%06ld ",
1856 (long) dtv.tv_sec, (long) dtv.tv_usec);
1857 otv = tv;
1858 }
1859 else if (tflag > 2) {
1860 tprintf("%ld.%06ld ",
1861 (long) tv.tv_sec, (long) tv.tv_usec);
1862 }
1863 else {
1864 time_t local = tv.tv_sec;
1865 strftime(str, sizeof(str), "%T", localtime(&local));
1866 if (tflag > 1)
1867 tprintf("%s.%06ld ", str, (long) tv.tv_usec);
1868 else
1869 tprintf("%s ", str);
1870 }
1871 }
1872 if (iflag)
1873 printcall(tcp);
1874}
1875
1876void
1877tabto(col)
1878int col;
1879{
1880 if (curcol < col)
1881 tprintf("%*s", col - curcol, "");
1882}
1883
1884void
1885printtrailer(tcp)
1886struct tcb *tcp;
1887{
1888 tprintf("\n");
1889 tcp_last = NULL;
1890}
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001891
Wichert Akkermanea78f0f1999-11-29 15:34:02 +00001892#ifdef HAVE_MP_PROCFS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001893
1894int mp_ioctl (int fd, int cmd, void *arg, int size) {
1895
1896 struct iovec iov[2];
1897 int n = 1;
1898
1899 iov[0].iov_base = &cmd;
1900 iov[0].iov_len = sizeof cmd;
1901 if (arg) {
1902 ++n;
1903 iov[1].iov_base = arg;
1904 iov[1].iov_len = size;
1905 }
1906
1907 return writev (fd, iov, n);
1908}
1909
1910#endif