strace.c revision bf79f2e16b090ffe59cd1e1820935680a2da7b78
176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman/*
276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
54dc8a2aec63e4fb5ee2688544c4de323ed5de3efWichert Akkerman * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * All rights reserved.
776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman *
876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Redistribution and use in source and binary forms, with or without
976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * modification, are permitted provided that the following conditions
1076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * are met:
1176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 1. Redistributions of source code must retain the above copyright
1276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman *    notice, this list of conditions and the following disclaimer.
1376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 2. Redistributions in binary form must reproduce the above copyright
1476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman *    notice, this list of conditions and the following disclaimer in the
1576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman *    documentation and/or other materials provided with the distribution.
1676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 3. The name of the author may not be used to endorse or promote products
1776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman *    derived from this software without specific prior written permission.
1876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman *
1976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman *
3076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman *	$Id$
3176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */
3276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
332ee6e45f36566e8735b35ffad40bfcc626a25a98Wichert Akkerman#include <sys/types.h>
3476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "defs.h"
3576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
3676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <signal.h>
3776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <errno.h>
3876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <sys/param.h>
3976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <fcntl.h>
4076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <sys/resource.h>
4176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <sys/wait.h>
4276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <sys/stat.h>
4376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <pwd.h>
4476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <grp.h>
4576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <string.h>
4676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
47bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
48bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#include <poll.h>
49bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
50bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman
5176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4
5276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <sys/stropts.h>
53ea78f0f77185f7d6d0b2055805139d96e1be816cWichert Akkerman#ifdef HAVE_MP_PROCFS
549ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#include <sys/uio.h>
559ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#endif
5676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
5776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
5876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint debug = 0, followfork = 0, followvfork = 0, interactive = 0;
5976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint rflag = 0, tflag = 0, dtime = 0, cflag = 0;
6076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint iflag = 0, xflag = 0, qflag = 0;
6176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pflag_seen = 0;
6276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
6376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *username = NULL;
6476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanuid_t run_uid;
6576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermangid_t run_gid;
6676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
6776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint acolumn = DEFAULT_ACOLUMN;
6876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint max_strlen = DEFAULT_STRLEN;
6976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *outfname = NULL;
7076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert AkkermanFILE *outf;
7176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb tcbtab[MAX_PROCS];
7276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint nprocs;
7376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *progname;
7476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanextern char version[];
7576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanextern char **environ;
7676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
7776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic struct tcb *pid2tcb P((int pid));
7876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int trace P((void));
7976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void cleanup P((void));
8076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void interrupt P((int sig));
8176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic sigset_t empty_set, blocked_set;
8276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
8376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE_SIG_ATOMIC_T
8476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic volatile sig_atomic_t interrupted;
8576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !HAVE_SIG_ATOMIC_T */
8676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef __STDC__
8776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic volatile int interrupted;
8876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !__STDC__ */
8976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int interrupted;
9076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !__STDC__ */
9176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_SIG_ATOMIC_T */
9276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
93bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
9476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
9576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic struct tcb *pfd2tcb P((int pfd));
9676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void reaper P((int sig));
9776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void rebuild_pollv P((void));
98e68d61c7d6dcf986d32c462e533ea46f85183db8Wichert Akkermanstruct pollfd pollv[MAX_PROCS];
9976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
10076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
10176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
10276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void proc_poll_open P((void));
10376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void proc_poller P((int pfd));
10476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
10576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct proc_pollfd {
10676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int fd;
10776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int revents;
10876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int pid;
10976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman};
11076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
11176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int poller_pid;
11276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int proc_poll_pipe[2] = { -1, -1 };
11376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
11476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
11576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
116ea78f0f77185f7d6d0b2055805139d96e1be816cWichert Akkerman#ifdef HAVE_MP_PROCFS
1179ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#define POLLWANT	POLLWRNORM
1189ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#else
1199ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#define POLLWANT	POLLPRI
1209ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#endif
121bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */
12276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
12376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
12476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanusage(ofp, exitval)
12576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert AkkermanFILE *ofp;
12676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint exitval;
12776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
12876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	fprintf(ofp, "\
12976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanusage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\
13076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman              [-p pid] ... [-s strsize] [-u username] [command [arg ...]]\n\
13176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman   or: strace -c [-e expr] ... [-O overhead] [-S sortby] [command [arg ...]]\n\
13276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-c -- count time, calls, and errors for each syscall and report summary\n\
13376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-f -- follow forks, -ff -- with output into separate files\n\
13476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-F -- attempt to follow vforks, -h -- print help message\n\
13576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-i -- print instruction pointer at time of syscall\n\
13676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-q -- suppress messages about attaching, detaching, etc.\n\
13776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
13876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-T -- print time spent in each syscall, -V -- print version\n\
13976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\
14076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
14176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-a column -- alignment COLUMN for printing syscall results (default %d)\n\
14276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
14376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman   options: trace, abbrev, verbose, raw, signal, read, or write\n\
14476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-o file -- send trace output to FILE instead of stderr\n\
14576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
14676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-p pid -- trace process with process id PID, may be repeated\n\
14776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
14876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
14976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-u username -- run command as username handling setuid and/or setgid\n\
15076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman", DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
15176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	exit(exitval);
15276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
15376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
15476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4
15576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef MIPS
15676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
15776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanfoobar()
15876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
15976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
16076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* MIPS */
16176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SVR4 */
16276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
16376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint
16476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanmain(argc, argv)
16576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint argc;
16676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *argv[];
16776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
16876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	extern int optind;
16976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	extern char *optarg;
17076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
17176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int c, pid = 0;
17276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct sigaction sa;
17376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
17476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	static char buf[BUFSIZ];
17576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
17676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	progname = argv[0];
17776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	outf = stderr;
17876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	interactive = 1;
17976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	qualify("trace=all");
18076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	qualify("abbrev=all");
18176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	qualify("verbose=all");
18276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	qualify("signal=all");
18376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	set_sortby(DEFAULT_SORTBY);
18476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	set_personality(DEFAULT_PERSONALITY);
18576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	while ((c = getopt(argc, argv,
18676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		"+cdfFhiqrtTvVxa:e:o:O:p:s:S:u:")) != EOF) {
18776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		switch (c) {
18876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'c':
18976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			cflag++;
19076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			dtime++;
19176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
19276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'd':
19376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			debug++;
19476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
19576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'f':
19676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			followfork++;
19776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
19876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'F':
19976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			followvfork++;
20076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
20176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'h':
20276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			usage(stdout, 0);
20376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
20476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'i':
20576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			iflag++;
20676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
20776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'q':
20876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			qflag++;
20976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
21076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'r':
21176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			rflag++;
21276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tflag++;
21376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
21476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 't':
21576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tflag++;
21676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
21776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'T':
21876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			dtime++;
21976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
22076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'x':
22176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			xflag++;
22276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
22376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'v':
22476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			qualify("abbrev=none");
22576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
22676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'V':
22776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			printf("%s\n", version);
22876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(0);
22976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
23076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'a':
23176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			acolumn = atoi(optarg);
23276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
23376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'e':
23476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			qualify(optarg);
23576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
23676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'o':
23776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			outfname = strdup(optarg);
23876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
23976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'O':
24076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			set_overhead(atoi(optarg));
24176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
24276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'p':
24376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if ((pid = atoi(optarg)) == 0) {
24476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "%s: Invalid process id: %s\n",
24576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					progname, optarg);
24676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
24776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
24876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (pid == getpid()) {
24954a4767f8609abfe2d7cb1802bc9e8dca97dd08fWichert Akkerman				fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
25076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
25176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
25276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if ((tcp = alloctcb(pid)) == NULL) {
25376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "%s: tcb table full, please recompile strace\n",
25476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					progname);
25576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
25676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
25776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->flags |= TCB_ATTACHED;
25876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			pflag_seen++;
25976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
26076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 's':
26176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			max_strlen = atoi(optarg);
26276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
26376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'S':
26476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			set_sortby(optarg);
26576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
26676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'u':
26776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			username = strdup(optarg);
26876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
26976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		default:
27076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			usage(stderr, 1);
27176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
27276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
27376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
27476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
27576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* See if they want to run as another user. */
27676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (username != NULL) {
27776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		struct passwd *pent;
27876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
27976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (getuid() != 0 || geteuid() != 0) {
28076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr,
28176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				"%s: you must be root to use the -u option\n",
28276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				progname);
28376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
28476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
28576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((pent = getpwnam(username)) == NULL) {
28676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "%s: cannot find user `%s'\n",
28776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				progname, optarg);
28876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
28976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
29076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		run_uid = pent->pw_uid;
29176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		run_gid = pent->pw_gid;
29276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
29376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else {
29476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		run_uid = getuid();
29576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		run_gid = getgid();
29676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
29776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
29876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4
29976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	setreuid(geteuid(), getuid());
30076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
30176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
30276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* See if they want to pipe the output. */
30376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (outfname && (outfname[0] == '|' || outfname[0] == '!')) {
30476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((outf = popen(outfname + 1, "w")) == NULL) {
30576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "%s: can't popen '%s': %s\n",
30676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				progname, outfname + 1, strerror(errno));
30776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
30876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
30976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		free(outfname);
31076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		outfname = NULL;
31176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
31276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
31376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Check if they want to redirect the output. */
31476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (outfname) {
31576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((outf = fopen(outfname, "w")) == NULL) {
31676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "%s: can't fopen '%s': %s\n",
31776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				progname, outfname, strerror(errno));
31876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
31976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
32076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
32176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
32276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4
32376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	setreuid(geteuid(), getuid());
32476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
32576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
32676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!outfname) {
32776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		qflag = 1;
32876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		setvbuf(outf, buf, _IOLBF, BUFSIZ);
32976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
33076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (optind < argc)
33176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		interactive = 0;
33276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else
33376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		qflag = 1;
33476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
33576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (c = 0, tcp = tcbtab; c < MAX_PROCS; c++, tcp++) {
33676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Reinitialize the output since it may have changed. */
33776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->outf = outf;
33876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
33976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
340bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
34176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (proc_open(tcp, 1) < 0) {
34276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "trouble opening proc file\n");
34376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
34476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
34576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
346bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* !USE_PROCFS */
34776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
34876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("attach: ptrace(PTRACE_ATTACH, ...)");
34976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
35076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
35176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
352bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */
35376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!qflag)
35476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr,
35576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				"Process %u attached - interrupt to quit\n",
35676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				pid);
35776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
35876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
35976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (optind < argc) {
36076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		struct stat statbuf;
36176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		char *filename;
36276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		char pathname[MAXPATHLEN];
36376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
36476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		filename = argv[optind];
36576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (strchr(filename, '/'))
36676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			strcpy(pathname, filename);
36776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef USE_DEBUGGING_EXEC
36876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/*
36976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * Debuggers customarily check the current directory
37076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * first regardless of the path but doing that gives
37176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * security geeks a panic attack.
37276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 */
37376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else if (stat(filename, &statbuf) == 0)
37476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			strcpy(pathname, filename);
37576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* USE_DEBUGGING_EXEC */
37676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else {
37776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			char *path;
37876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			int m, n, len;
37976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
38076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			for (path = getenv("PATH"); path && *path; path += m) {
38176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (strchr(path, ':')) {
38276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					n = strchr(path, ':') - path;
38376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					m = n + 1;
38476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
38576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				else
38676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					m = n = strlen(path);
38776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (n == 0) {
38876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					getcwd(pathname, MAXPATHLEN);
38976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					len = strlen(pathname);
39076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
39176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				else {
39276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					strncpy(pathname, path, n);
39376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					len = n;
39476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
39576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (len && pathname[len - 1] != '/')
39676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					pathname[len++] = '/';
39776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				strcpy(pathname + len, filename);
39876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (stat(pathname, &statbuf) == 0)
39976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					break;
40076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
40176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
40276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (stat(pathname, &statbuf) < 0) {
40376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "%s: %s: command not found\n",
40476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				progname, filename);
40576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
40676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
40776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		switch (pid = fork()) {
40876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case -1:
40976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("strace: fork");
41076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			cleanup();
41176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
41276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
41376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 0: {
414bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
415bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		        if (outf != stderr) close (fileno (outf));
41676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef MIPS
41776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/* Kludge for SGI, see proc_open for details. */
41876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sa.sa_handler = foobar;
41976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sa.sa_flags = 0;
42076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigemptyset(&sa.sa_mask);
42176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigaction(SIGINT, &sa, NULL);
42276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* MIPS */
423bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
42476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			pause();
425bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */
426bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			kill(getpid(), SIGSTOP); /* stop HERE */
427bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
428bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* !USE_PROCFS */
429bd4125c6bcd20e8f014b682b31d6fc5d0fef3ed0Wichert Akkerman			if (outf!=stderr)
4307987cdf192632516d6ba493b0a9943f5a3a7c362Wichert Akkerman				close(fileno (outf));
431bd4125c6bcd20e8f014b682b31d6fc5d0fef3ed0Wichert Akkerman
43276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
43376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("strace: ptrace(PTRACE_TRACEME, ...)");
43476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
43576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
43676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (debug)
43776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				kill(getpid(), SIGSTOP);
43876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
43976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (username != NULL || geteuid() == 0) {
44076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				uid_t run_euid = run_uid;
44176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				gid_t run_egid = run_gid;
44276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
44376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (statbuf.st_mode & S_ISUID)
44476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					run_euid = statbuf.st_uid;
44576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (statbuf.st_mode & S_ISGID)
44676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					run_egid = statbuf.st_gid;
44776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
44876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/*
44976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * It is important to set groups before we
45076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * lose privileges on setuid.
45176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 */
4525ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman				if (username != NULL) {
4535ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman					if (initgroups(username, run_gid) < 0) {
4545ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman						perror("initgroups");
4555ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman						exit(1);
4565ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman					}
4575ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman					if (setregid(run_gid, run_egid) < 0) {
4585ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman						perror("setregid");
4595ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman						exit(1);
4605ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman					}
4615ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman					if (setreuid(run_uid, run_euid) < 0) {
4625ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman						perror("setreuid");
4635ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman						exit(1);
4645ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman					}
46576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
46676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
46776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			else
46876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				setreuid(run_uid, run_uid);
469bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */
47076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
47176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			execv(pathname, &argv[optind]);
47276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("strace: exec");
47376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			_exit(1);
47476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
47576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
47676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		default:
47776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if ((tcp = alloctcb(pid)) == NULL) {
47876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "tcb table full\n");
47976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				cleanup();
48076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
48176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
482bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
48376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (proc_open(tcp, 0) < 0) {
48476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "trouble opening proc file\n");
48576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				cleanup();
48676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
48776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
488bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */
489bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef USE_PROCFS
49076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fake_execve(tcp, pathname, &argv[optind], environ);
491bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */
49276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
49376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
49476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
49576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (pflag_seen == 0)
49676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		usage(stderr, 1);
49776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
49876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&empty_set);
49976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&blocked_set);
50076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_handler = SIG_IGN;
50176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&sa.sa_mask);
50276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_flags = 0;
50376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGTTOU, &sa, NULL);
50476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGTTIN, &sa, NULL);
50576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (interactive) {
50676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigaddset(&blocked_set, SIGHUP);
50776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigaddset(&blocked_set, SIGINT);
50876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigaddset(&blocked_set, SIGQUIT);
50976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigaddset(&blocked_set, SIGPIPE);
51076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigaddset(&blocked_set, SIGTERM);
51176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sa.sa_handler = interrupt;
51276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SUNOS4
51376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* POSIX signals on sunos4.1 are a little broken. */
51476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sa.sa_flags = SA_INTERRUPT;
51576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SUNOS4 */
51676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
51776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGHUP, &sa, NULL);
51876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGINT, &sa, NULL);
51976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGQUIT, &sa, NULL);
52076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGPIPE, &sa, NULL);
52176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGTERM, &sa, NULL);
522bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
52376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_handler = reaper;
52476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGCHLD, &sa, NULL);
525bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */
52676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
52776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (trace() < 0)
52876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		exit(1);
52976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	cleanup();
53076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	exit(0);
53176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
53276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
53376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
53476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermannewoutf(tcp)
53576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
53676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
53776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	char name[MAXPATHLEN];
53876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	FILE *fp;
53976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
54076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (outfname && followfork > 1) {
54176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sprintf(name, "%s.%u", outfname, tcp->pid);
54276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4
54376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		setreuid(geteuid(), getuid());
54476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
54576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fp = fopen(name, "w");
54676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4
54776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		setreuid(geteuid(), getuid());
54876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
54976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (fp == NULL) {
55076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("fopen");
55176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return;
55276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
55376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->outf = fp;
55476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
55576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return;
55676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
55776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
55876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *
55976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanalloctcb(pid)
56076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pid;
56176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
56276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
56376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
56476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
56576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
56676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((tcp->flags & TCB_INUSE) == 0) {
56776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->pid = pid;
56876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->parent = NULL;
56976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->nchildren = 0;
57076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->flags = TCB_INUSE | TCB_STARTUP;
57176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->outf = outf; /* Initialise to current out file */
57276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->stime.tv_sec = 0;
57376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->stime.tv_usec = 0;
57476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->pfd = -1;
57576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			nprocs++;
57676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return tcp;
57776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
57876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
57976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return NULL;
58076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
58176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
582bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
58376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint
58476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanproc_open(tcp, attaching)
58576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
58676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint attaching;
58776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
58876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	char proc[32];
58976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	long arg;
590bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef SVR4
59176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sysset_t sc_enter, sc_exit;
59276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigset_t signals;
59376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	fltset_t faults;
594bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
59576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
59676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	static int last_pfd;
59776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
59876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
599ea78f0f77185f7d6d0b2055805139d96e1be816cWichert Akkerman#ifdef HAVE_MP_PROCFS
6009ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	/* Open the process pseudo-files in /proc. */
6019ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	sprintf(proc, "/proc/%d/ctl", tcp->pid);
6029ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
6039ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("strace: open(\"/proc/...\", ...)");
6049ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6059ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6069ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
6079ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_GETFD");
6089ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6099ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6109ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
6119ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_SETFD");
6129ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6139ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6149ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	sprintf(proc, "/proc/%d/status", tcp->pid);
6159ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
6169ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("strace: open(\"/proc/...\", ...)");
6179ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6189ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6199ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) {
6209ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_GETFD");
6219ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6229ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6239ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) {
6249ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_SETFD");
6259ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6269ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6279ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	sprintf(proc, "/proc/%d/as", tcp->pid);
6289ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
6299ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("strace: open(\"/proc/...\", ...)");
6309ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6319ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6329ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) {
6339ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_GETFD");
6349ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6359ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6369ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) {
6379ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_SETFD");
6389ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6399ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6409ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#else
64176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Open the process pseudo-file in /proc. */
642bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
64376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sprintf(proc, "/proc/%d", tcp->pid);
64476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) {
645bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */
646bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	sprintf(proc, "/proc/%d/mem", tcp->pid);
647bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	if ((tcp->pfd = open(proc, O_RDWR)) < 0) {
648bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
64976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("strace: open(\"/proc/...\", ...)");
65076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
65176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
6529ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
6539ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_GETFD");
6549ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6559ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6569ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
6579ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_SETFD");
6589ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6599ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6609ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#endif
661bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD
662bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	sprintf(proc, "/proc/%d/regs", tcp->pid);
663bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) {
664bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		perror("strace: open(\"/proc/.../regs\", ...)");
665bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		return -1;
666bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	}
667bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	if (cflag) {
668bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		sprintf(proc, "/proc/%d/status", tcp->pid);
669bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) {
670bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			perror("strace: open(\"/proc/.../status\", ...)");
671bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			return -1;
672bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		}
673bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	} else
674bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		tcp->pfd_status = -1;
675bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
67676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	rebuild_pollv();
67776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!attaching) {
67876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/*
67976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * Wait for the child to pause.  Because of a race
68076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * condition we have to poll for the event.
68176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 */
68276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		for (;;) {
6839ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (IOCTL_STATUS (tcp) < 0) {
68476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("strace: PIOCSTATUS");
68576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
68676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
687bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
6889ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (tcp->status.PR_FLAGS & PR_ASLEEP)
689bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else
690bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			if (tcp->status.state == 1)
691bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
692bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			    break;
69376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
69476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
695bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
69676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Stop the process so that we own the stop. */
69716a03d2e97415afe6cf34172a0aea97a95a0b160Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) {
69876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("strace: PIOCSTOP");
69976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
70076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
701bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
70276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef PIOCSET
70376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Set Run-on-Last-Close. */
70476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	arg = PR_RLC;
7059ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
70676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOCSET PR_RLC");
70776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
70876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
70976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Set or Reset Inherit-on-Fork. */
71076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	arg = PR_FORK;
7119ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
71276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOC{SET,RESET} PR_FORK");
71376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
71476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
71576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else  /* !PIOCSET */
716bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
71776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
71876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOCSRLC");
71976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
72076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
72176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
72276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOC{S,R}FORK");
72376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
72476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
725bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */
726bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	/* just unset the PF_LINGER flag for the Run-on-Last-Close. */
727bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) {
728bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	        perror("PIOCGFL");
729bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	        return -1;
730bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	}
731bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	arg &= ~PF_LINGER;
732bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) {
733bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	        perror("PIOCSFL");
734bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	        return -1;
735bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	}
736bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
73776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !PIOCSET */
738bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
73976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Enable all syscall entries. */
74076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	prfillset(&sc_enter);
7419ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSENTRY, &sc_enter) < 0) {
74276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOCSENTRY");
74376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
74476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
74576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Enable all syscall exits. */
74676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	prfillset(&sc_exit);
7479ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSEXIT, &sc_exit) < 0) {
74876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOSEXIT");
74976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
75076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
75176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Enable all signals. */
75276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	prfillset(&signals);
7539ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
75476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOCSTRACE");
75576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
75676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
75776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Enable all faults. */
75876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	prfillset(&faults);
7599ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
76076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOCSFAULT");
76176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
76276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
763bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */
764bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	/* set events flags. */
765bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	arg = S_SIG | S_SCE | S_SCX ;
766bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	if(ioctl(tcp->pfd, PIOCBIS, arg) < 0) {
767bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		perror("PIOCBIS");
768bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		return -1;
769bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	}
770bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
77176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!attaching) {
77276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef MIPS
77376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/*
77476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * The SGI PRSABORT doesn't work for pause() so
77576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * we send it a caught signal to wake it up.
77676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 */
77776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		kill(tcp->pid, SIGINT);
77876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !MIPS */
779bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef PRSABORT
78076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* The child is in a pause(), abort it. */
7819ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		arg = PRSABORT;
7829ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
78376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("PIOCRUN");
78476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return -1;
78576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
786bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
787bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !MIPS*/
788bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD
789bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		/* wake up the child if it received the SIGSTOP */
790bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		kill(tcp->pid, SIGCONT);
791bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
79276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		for (;;) {
79376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/* Wait for the child to do something. */
7949ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (IOCTL_WSTOP (tcp) < 0) {
79576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("PIOCWSTOP");
79676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
79776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
7989ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (tcp->status.PR_WHY == PR_SYSENTRY) {
799bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				tcp->flags &= ~TCB_INSYSCALL;
800bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				get_scno(tcp);
801bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				if (tcp->scno == SYS_execve)
80276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					break;
80376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
80476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/* Set it running: maybe execve will be next. */
805bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
8069ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			arg = 0;
8079ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
808bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */
809bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) {
810bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
81176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("PIOCRUN");
81276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
81376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
814bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD
815bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			/* handle the case where we "opened" the child before
816bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			   it did the kill -STOP */
817bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			if (tcp->status.PR_WHY == PR_SIGNALLED &&
818bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			    tcp->status.PR_WHAT == SIGSTOP)
819bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			        kill(tcp->pid, SIGCONT);
820bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
82176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
822bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
82376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
824bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */
825bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	} else {
826bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	       /* little hack to show the current syscall */
827bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	       IOCTL_STATUS(tcp);
828bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	       tcp->flags &= ~TCB_INSYSCALL;
829bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	       tcp->status.why = PR_SYSENTRY;
830bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	       trace_syscall(tcp);
831bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	}
832bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
83376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
83476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (proc_poll_pipe[0] != -1)
83576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		proc_poller(tcp->pfd);
83676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (nprocs > 1) {
83776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		proc_poll_open();
83876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		proc_poller(last_pfd);
83976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		proc_poller(tcp->pfd);
84076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
84176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	last_pfd = tcp->pfd;
84276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
84376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return 0;
84476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
84576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
846bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */
84776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
84876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic struct tcb *
84976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanpid2tcb(pid)
85076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pid;
85176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
85276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
85376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
85476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
85576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
85676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (pid && tcp->pid != pid)
85776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
85876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_INUSE)
85976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return tcp;
86076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
86176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return NULL;
86276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
86376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
864bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
86576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
86676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic struct tcb *
86776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanpfd2tcb(pfd)
86876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pfd;
86976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
87076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
87176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
87276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
87376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
87476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->pfd != pfd)
87576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
87676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_INUSE)
87776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return tcp;
87876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
87976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return NULL;
88076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
88176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
882bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */
88376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
88476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
88576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermandroptcb(tcp)
88676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
88776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
88876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp->pid == 0)
88976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return;
89076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	nprocs--;
89176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tcp->pid = 0;
89276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tcp->flags = 0;
89376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp->pfd != -1) {
89476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		close(tcp->pfd);
89576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->pfd = -1;
896bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD
897bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		if (tcp->pfd_reg != -1) {
898bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		        close(tcp->pfd_reg);
899bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		        tcp->pfd_reg = -1;
900bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		}
901bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		if (tcp->pfd_status != -1) {
902bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			close(tcp->pfd_status);
903bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			tcp->pfd_status = -1;
904bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		}
905bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !FREEBSD */
906bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
90776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		rebuild_pollv();
90876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
90976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
91076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp->parent != NULL) {
91176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->parent->nchildren--;
91276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->parent = NULL;
91376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
91476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#if 0
91576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp->outf != stderr)
91676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fclose(tcp->outf);
91776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
91876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tcp->outf = 0;
91976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
92076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
921bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef USE_PROCFS
92276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
92376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
92476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanresume(tcp)
92576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
92676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
92776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp == NULL)
92876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
92976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
93076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!(tcp->flags & TCB_SUSPENDED)) {
93176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid);
93276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
93376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
93476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tcp->flags &= ~TCB_SUSPENDED;
93576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
93676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) {
93776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("resume: ptrace(PTRACE_SYSCALL, ...)");
93876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
93976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
94076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
94176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!qflag)
94276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fprintf(stderr, "Process %u resumed\n", tcp->pid);
94376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return 0;
94476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
94576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
946bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */
94776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
94876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman/* detach traced process; continue with sig */
94976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
95076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
95176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermandetach(tcp, sig)
95276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
95376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig;
95476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
95576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int error = 0;
95676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX
95776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int status;
95876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
95976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
96076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp->flags & TCB_BPTSET)
96176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sig = SIGKILL;
96276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
96376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX
96476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/*
96576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	 * Linux wrongly insists the child be stopped
96676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	 * before detaching.  Arghh.  We go through hoops
96776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	 * to make a clean break of things.
96876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	 */
969dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman#if defined(SPARC)
970dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman#undef PTRACE_DETACH
971dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman#define PTRACE_DETACH PTRACE_SUNDETACH
972dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman#endif
97376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
97476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* On a clear day, you can see forever. */
97576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
97676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (errno != ESRCH) {
97776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Shouldn't happen. */
97876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("detach: ptrace(PTRACE_DETACH, ...)");
97976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
98076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (kill(tcp->pid, 0) < 0) {
98176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (errno != ESRCH)
98276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("detach: checking sanity");
98376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
98476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (kill(tcp->pid, SIGSTOP) < 0) {
98576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (errno != ESRCH)
98676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("detach: stopping child");
98776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
98876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else {
98976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		for (;;) {
99076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (waitpid(tcp->pid, &status, 0) < 0) {
99176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (errno != ECHILD)
99276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					perror("detach: waiting");
99376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
99476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
99576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!WIFSTOPPED(status)) {
99676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/* Au revoir, mon ami. */
99776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
99876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
99976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (WSTOPSIG(status) == SIGSTOP) {
100076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if ((error = ptrace(PTRACE_DETACH,
100176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				    tcp->pid, (char *) 1, sig)) < 0) {
100276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					if (errno != ESRCH)
100376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman						perror("detach: ptrace(PTRACE_DETACH, ...)");
100476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					/* I died trying. */
100576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
100676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
100776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
100876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1,
100976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			    WSTOPSIG(status) == SIGTRAP ?
101076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			    0 : WSTOPSIG(status))) < 0) {
101176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (errno != ESRCH)
101276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					perror("detach: ptrace(PTRACE_CONT, ...)");
101376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
101476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
101576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
101676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
101776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* LINUX */
101876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
101976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#if defined(SUNOS4)
102076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
102176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (sig && kill(tcp->pid, sig) < 0)
102276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("detach: kill");
102376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sig = 0;
102476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0)
102576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("detach: ptrace(PTRACE_DETACH, ...)");
102676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SUNOS4 */
102776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
1028bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef USE_PROCFS
102976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (waiting_parent(tcp))
103076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		error = resume(tcp->parent);
1031bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */
103276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
103376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!qflag)
103476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fprintf(stderr, "Process %u detached\n", tcp->pid);
103576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
103676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	droptcb(tcp);
103776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return error;
103876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
103976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
1040bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
104176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
104276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
104376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanreaper(sig)
104476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig;
104576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
104676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int pid;
104776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int status;
104876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
104976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
105076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#if 0
105176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		struct tcb *tcp;
105276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
105376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp = pid2tcb(pid);
105476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp)
105576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
105676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
105776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
105876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
105976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
1060bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */
106176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
106276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
106376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermancleanup()
106476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
106576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
106676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
106776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
106876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
106976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!(tcp->flags & TCB_INUSE))
107076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
107176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (debug)
107276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr,
107376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				"cleanup: looking at pid %u\n", tcp->pid);
107476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp_last &&
107576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		    (!outfname || followfork < 2 || tcp_last == tcp)) {
107676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tprintf(" <unfinished ...>\n");
107776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp_last = NULL;
107876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
107976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_ATTACHED)
108076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			detach(tcp, 0);
108176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else {
108276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			kill(tcp->pid, SIGCONT);
108376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			kill(tcp->pid, SIGTERM);
108476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
108576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
108676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (cflag)
108776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		call_summary(outf);
108876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
108976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
109076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
109176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermaninterrupt(sig)
109276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig;
109376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
109476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	interrupted = 1;
109576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
109676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
109776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_STRERROR
109876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
109976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SYS_ERRLIST_DECLARED
110076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanextern int sys_nerr;
110176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanextern char *sys_errlist[];
110276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SYS_ERRLIST_DECLARED */
110376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
110476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanconst char *
110576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstrerror(errno)
110676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint errno;
110776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
110876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	static char buf[64];
110976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
111076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (errno < 1 || errno >= sys_nerr) {
111176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sprintf(buf, "Unknown error %d", errno);
111276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return buf;
111376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
111476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return sys_errlist[errno];
111576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
111676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
111776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* HAVE_STERRROR */
111876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
111976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_STRSIGNAL
112076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
112176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SYS_SIGLIST_DECLARED
112276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE__SYS_SIGLIST
112376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	extern char *_sys_siglist[];
112476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else
112576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	extern char *sys_siglist[];
112676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
112776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SYS_SIGLIST_DECLARED */
112876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
112976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanconst char *
113076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstrsignal(sig)
113176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig;
113276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
113376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	static char buf[64];
113476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
113576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (sig < 1 || sig >= NSIG) {
113676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sprintf(buf, "Unknown signal %d", sig);
113776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return buf;
113876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
113976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE__SYS_SIGLIST
114076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return _sys_siglist[sig];
114176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else
114276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return sys_siglist[sig];
114376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
114476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
114576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
114676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* HAVE_STRSIGNAL */
114776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
1148bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
114976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
115076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
115176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanrebuild_pollv()
115276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
115376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i, j;
115476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
115576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
115676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = j = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
115776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!(tcp->flags & TCB_INUSE))
115876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
115976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		pollv[j].fd = tcp->pfd;
11609ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		pollv[j].events = POLLWANT;
116176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		j++;
116276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
116376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (j != nprocs) {
116476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fprintf(stderr, "strace: proc miscount\n");
116576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		exit(1);
116676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
116776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
116876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
116976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
117076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
117176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
117276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanproc_poll_open()
117376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
117476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int arg;
117576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
117676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
117776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (pipe(proc_poll_pipe) < 0) {
117876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("pipe");
117976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		exit(1);
118076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
118176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0; i < 2; i++) {
118276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) {
118376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("F_GETFD");
118476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
118576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
118676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) {
118776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("F_SETFD");
118876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
118976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
119076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
119176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
119276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
119376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
119476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanproc_poll(pollv, nfds, timeout)
119576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct pollfd *pollv;
119676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint nfds;
119776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint timeout;
119876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
119976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
120076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int n;
120176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct proc_pollfd pollinfo;
120276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
120376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
120476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return n;
120576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (n != sizeof(struct proc_pollfd)) {
120676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fprintf(stderr, "panic: short read: %d\n", n);
120776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		exit(1);
120876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
120976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0; i < nprocs; i++) {
121076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (pollv[i].fd == pollinfo.fd)
121176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			pollv[i].revents = pollinfo.revents;
121276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else
121376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			pollv[i].revents = 0;
121476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
121576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	poller_pid = pollinfo.pid;
121676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return 1;
121776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
121876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
121976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
122076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanwakeup_handler(sig)
122176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig;
122276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
122376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
122476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
122576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
122676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanproc_poller(pfd)
122776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pfd;
122876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
122976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct proc_pollfd pollinfo;
123076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct sigaction sa;
123176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigset_t blocked_set, empty_set;
123276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
123376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int n;
123476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct rlimit rl;
1235bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD
1236bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	struct procfs_status pfs;
1237bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
123876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
123976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	switch (fork()) {
124076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	case -1:
124176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("fork");
124276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		_exit(0);
124376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	case 0:
124476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		break;
124576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	default:
124676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return;
124776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
124876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
124976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
125076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_flags = 0;
125176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&sa.sa_mask);
125276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGHUP, &sa, NULL);
125376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGINT, &sa, NULL);
125476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGQUIT, &sa, NULL);
125576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGPIPE, &sa, NULL);
125676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGTERM, &sa, NULL);
125776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_handler = wakeup_handler;
125876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGUSR1, &sa, NULL);
125976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&blocked_set);
126076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaddset(&blocked_set, SIGUSR1);
126176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigprocmask(SIG_BLOCK, &blocked_set, NULL);
126276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&empty_set);
126376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
126476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
126576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("getrlimit(RLIMIT_NOFILE, ...)");
126676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		_exit(0);
126776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
126876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	n = rl.rlim_cur;
126976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0; i < n; i++) {
127076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (i != pfd && i != proc_poll_pipe[1])
127176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			close(i);
127276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
127376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
127476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	pollinfo.fd = pfd;
127576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	pollinfo.pid = getpid();
127676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (;;) {
1277bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
1278bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	        if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
1279bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */
1280bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	        if (ioctl(pfd, PIOCWSTOP, &pfs) < 0)
1281bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
12829ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		{
128376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			switch (errno) {
128476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case EINTR:
128576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
128676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case EBADF:
128776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				pollinfo.revents = POLLERR;
128876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
128976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case ENOENT:
129076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				pollinfo.revents = POLLHUP;
129176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
129276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			default:
129376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("proc_poller: PIOCWSTOP");
129476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
129576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
129676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			_exit(0);
129776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
12989ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		pollinfo.revents = POLLWANT;
129976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
130076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigsuspend(&empty_set);
130176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
130276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
130376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
130476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
130576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
130676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
130776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchoose_pfd()
130876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
130976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i, j;
131076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
131176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
131276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	static int last;
131376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
131476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (followfork < 2 &&
13159ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	    last < nprocs && (pollv[last].revents & POLLWANT)) {
131676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/*
131776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * The previous process is ready to run again.  We'll
131876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * let it do so if it is currently in a syscall.  This
131976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * heuristic improves the readability of the trace.
132076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 */
132176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp = pfd2tcb(pollv[last].fd);
132276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp && (tcp->flags & TCB_INSYSCALL))
132376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return pollv[last].fd;
132476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
132576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
132676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0; i < nprocs; i++) {
132776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Let competing children run round robin. */
132876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		j = (i + last + 1) % nprocs;
132976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (pollv[j].revents & (POLLHUP | POLLERR)) {
133076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp = pfd2tcb(pollv[j].fd);
133176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!tcp) {
133276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "strace: lost proc\n");
133376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
133476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
133576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
133676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return -1;
133776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
13389ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		if (pollv[j].revents & POLLWANT) {
133976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			last = j;
134076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return pollv[j].fd;
134176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
134276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
134376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	fprintf(stderr, "strace: nothing ready\n");
134476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	exit(1);
134576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
134676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
134776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
134876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantrace()
134976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
13509dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#ifdef POLL_HACK
13519dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman	struct tcb *in_syscall;
13529dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#endif
135376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
135476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int pfd;
135576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int what;
135676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int ioctl_result = 0, ioctl_errno = 0;
13579ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	long arg;
135876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
135976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (;;) {
136076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interactive)
136176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigprocmask(SIG_SETMASK, &empty_set, NULL);
136276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
136376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (nprocs == 0)
136476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
136576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
136676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		switch (nprocs) {
136776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 1:
136876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
136976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (proc_poll_pipe[0] == -1) {
137076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
137176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tcp = pid2tcb(0);
137276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (!tcp)
137376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					continue;
137476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				pfd = tcp->pfd;
137576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (pfd == -1)
137676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					continue;
137776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
137876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
137976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
138076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/* fall through ... */
138176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
138276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		default:
138376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE_POLLABLE_PROCFS
13849dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#ifdef POLL_HACK
13859dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman		        /* On some systems (e.g. UnixWare) we get too much ugly
13869dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman			   "unfinished..." stuff when multiple proceses are in
13879dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman			   syscalls.  Here's a nasty hack */
13889dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman
13899dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman			if (in_syscall) {
13909dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				struct pollfd pv;
13919dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				tcp = in_syscall;
13929dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				in_syscall = NULL;
13939dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				pv.fd = tcp->pfd;
13949dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				pv.events = POLLWANT;
13959dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				if ((what = poll (&pv, 1, 1)) < 0) {
13969dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman					if (interrupted)
13979dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman						return 0;
13989dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman					continue;
13999dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				}
14009dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				else if (what == 1 && pv.revents & POLLWANT) {
14019dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman					goto FOUND;
14029dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				}
14039dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman			}
14049dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#endif
14059dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman
140676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (poll(pollv, nprocs, INFTIM) < 0) {
140776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (interrupted)
140876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					return 0;
140976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
141076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
141176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !HAVE_POLLABLE_PROCFS */
141276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (proc_poll(pollv, nprocs, INFTIM) < 0) {
141376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (interrupted)
141476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					return 0;
141576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
141676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
141776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
141876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			pfd = choose_pfd();
141976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (pfd == -1)
142076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
142176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
142276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
142376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
142476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Look up `pfd' in our table. */
142576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((tcp = pfd2tcb(pfd)) == NULL) {
142676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "unknown pfd: %u\n", pfd);
142776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
142876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
14299dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman	FOUND:
143076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Get the status of the process. */
143176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!interrupted) {
1432bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
14339ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			ioctl_result = IOCTL_WSTOP (tcp);
1434bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */
1435bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			/* Thanks to some scheduling mystery, the first poller
1436bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			   sometimes waits for the already processed end of fork
1437bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			   event. Doing a non blocking poll here solves the problem. */
1438bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			if (proc_poll_pipe[0] != -1)
1439bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				ioctl_result = IOCTL_STATUS (tcp);
1440bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			else
1441bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			  	ioctl_result = IOCTL_WSTOP (tcp);
1442bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
144376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			ioctl_errno = errno;
144476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
144576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (proc_poll_pipe[0] != -1) {
144676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (ioctl_result < 0)
144776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					kill(poller_pid, SIGKILL);
144876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				else
144976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					kill(poller_pid, SIGUSR1);
145076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
145176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
145276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
145376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interrupted)
145476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return 0;
145576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
145676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interactive)
145776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigprocmask(SIG_BLOCK, &blocked_set, NULL);
145876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
145976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (ioctl_result < 0) {
146076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/* Find out what happened if it failed. */
146176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			switch (ioctl_errno) {
146276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case EINTR:
146376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case EBADF:
146476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
1465bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD
1466bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			case ENOTTY:
1467bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
146876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case ENOENT:
146976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				droptcb(tcp);
147076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
147176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			default:
147276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("PIOCWSTOP");
147376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
147476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
147576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
147676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
147776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* clear the just started flag */
147876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->flags &= ~TCB_STARTUP;
147976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
148076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* set current output file */
148176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		outf = tcp->outf;
148276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
148376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (cflag) {
148476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			struct timeval stime;
1485bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD
1486bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			char buf[1024];
1487bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			int len;
1488bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman
1489bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) {
1490bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				buf[len] = '\0';
1491bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				sscanf(buf,
1492bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				       "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld",
1493bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				       &stime.tv_sec, &stime.tv_usec);
1494bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			} else
1495bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				stime.tv_sec = stime.tv_usec = 0;
1496bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* !FREEBSD */
149776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			stime.tv_sec = tcp->status.pr_stime.tv_sec;
149876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
1499bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !FREEBSD */
150076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tv_sub(&tcp->dtime, &stime, &tcp->stime);
150176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->stime = stime;
150276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
15039ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		what = tcp->status.PR_WHAT;
15049ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		switch (tcp->status.PR_WHY) {
1505bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
150676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case PR_REQUESTED:
15079ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (tcp->status.PR_FLAGS & PR_ASLEEP) {
15089ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman				tcp->status.PR_WHY = PR_SYSENTRY;
150976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (trace_syscall(tcp) < 0) {
151076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					fprintf(stderr, "syscall trouble\n");
151176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					exit(1);
151276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
151376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
151476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
1515bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !FREEBSD */
151676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case PR_SYSENTRY:
15179dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#ifdef POLL_HACK
15189dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman		        in_syscall = tcp;
15199dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#endif
152076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case PR_SYSEXIT:
152176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (trace_syscall(tcp) < 0) {
152276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "syscall trouble\n");
152376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
152476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
152576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
152676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case PR_SIGNALLED:
152776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) {
152876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printleader(tcp);
152976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("--- %s (%s) ---",
1530ce780fc9e6067b15b65ca2904c698c77503bf635Nate Sammons					signame(what), strsignal(what));
153176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printtrailer(tcp);
153276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
153376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
153476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case PR_FAULTED:
153576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!cflag && (qual_flags[what] & QUAL_FAULT)) {
153676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printleader(tcp);
153776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("=== FAULT %d ===", what);
153876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printtrailer(tcp);
153976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
154076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
1541bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD
1542bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		case 0: /* handle case we polled for nothing */
1543bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		  	continue;
1544bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
154576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		default:
15469ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
154776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
154876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
154976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
15509ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		arg = 0;
1551bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
15529ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
1553bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else
1554bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		if (IOCTL (tcp->pfd, PIOCRUN, 0) < 0) {
1555bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
155676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("PIOCRUN");
155776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
155876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
155976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
156076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return 0;
156176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
156276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
1563bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* !USE_PROCFS */
156476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
156576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
156676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantrace()
156776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
156876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int pid;
156976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int wait_errno;
157076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int status;
157176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
157276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX
157376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct rusage ru;
157476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* LINUX */
157576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
157676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	while (nprocs != 0) {
157776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interactive)
157876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigprocmask(SIG_SETMASK, &empty_set, NULL);
157976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX
158076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
158176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* LINUX */
158276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SUNOS4
158376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		pid = wait(&status);
158476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SUNOS4 */
158576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		wait_errno = errno;
158676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interactive)
158776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigprocmask(SIG_BLOCK, &blocked_set, NULL);
158876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
158976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interrupted)
159076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return 0;
159176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
159276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (pid == -1) {
159376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			switch (wait_errno) {
159476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case EINTR:
159576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
159676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case ECHILD:
159776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/*
159876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * We would like to verify this case
159976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * but sometimes a race in Solbourne's
160076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * version of SunOS sometimes reports
160176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * ECHILD before sending us SIGCHILD.
160276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 */
160376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#if 0
160476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (nprocs == 0)
160576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					return 0;
160676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "strace: proc miscount\n");
160776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
160876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
160976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return 0;
161076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			default:
161176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				errno = wait_errno;
161276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("strace: wait");
161376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
161476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
161576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
161676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (debug)
161776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
161876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
161976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Look up `pid' in our table. */
162076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((tcp = pid2tcb(pid)) == NULL) {
1621faf722234dc8af97776f94fdda7e100fb60650a2Wichert Akkerman#if 0 /* XXX davidm */ /* WTA: disabled again */
16228b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman			struct tcb *tcpchild;
16238b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman
16248b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman			if ((tcpchild = alloctcb(pid)) == NULL) {
16258b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman				fprintf(stderr, " [tcb table full]\n");
16268b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman				kill(pid, SIGKILL); /* XXX */
16278b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman				return 0;
16288b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman			}
16298b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman			tcpchild->flags |= TCB_ATTACHED;
16308b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman			newoutf(tcpchild);
16318b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman			tcp->nchildren++;
16328b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman			if (!qflag)
16338b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman				fprintf(stderr, "Process %d attached\n", pid);
16348b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman#else
163576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "unknown pid: %u\n", pid);
163676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (WIFSTOPPED(status))
163776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				ptrace(PTRACE_CONT, pid, (char *) 1, 0);
163876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
16398b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman#endif
164076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
164176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* set current output file */
164276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		outf = tcp->outf;
164376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (cflag) {
164476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX
164576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
164676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->stime = ru.ru_stime;
164776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !LINUX */
164876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
164976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
165076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_SUSPENDED) {
165176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/*
165276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * Apparently, doing any ptrace() call on a stopped
165376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * process, provokes the kernel to report the process
165476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * status again on a subsequent wait(), even if the
165576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * process has not been actually restarted.
165676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * Since we have inspected the arguments of suspended
165776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * processes we end up here testing for this case.
165876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 */
165976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
166076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
166176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (WIFSIGNALED(status)) {
166276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!cflag
166376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			    && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
166476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printleader(tcp);
166576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("+++ killed by %s +++",
1666ce780fc9e6067b15b65ca2904c698c77503bf635Nate Sammons					signame(WTERMSIG(status)));
166776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printtrailer(tcp);
166876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
166976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
167076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
167176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
167276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (WIFEXITED(status)) {
167376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (debug)
167476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "pid %u exited\n", pid);
167576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tcp->flags & TCB_ATTACHED)
167676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr,
167776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					"PANIC: attached pid %u exited\n",
167876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					pid);
167976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
168076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
168176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
168276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!WIFSTOPPED(status)) {
168376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
168476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
168576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
168676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
168776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (debug)
168876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "pid %u stopped, [%s]\n",
1689ce780fc9e6067b15b65ca2904c698c77503bf635Nate Sammons				pid, signame(WSTOPSIG(status)));
169076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
169176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_STARTUP) {
169276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/*
169376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * This flag is there to keep us in sync.
169476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * Next time this process stops it should
169576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * really be entering a system call.
169676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 */
169776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->flags &= ~TCB_STARTUP;
169876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tcp->flags & TCB_ATTACHED) {
169976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/*
170076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * Interestingly, the process may stop
170176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * with STOPSIG equal to some other signal
170276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * than SIGSTOP if we happend to attach
170376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * just before the process takes a signal.
170476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 */
170576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (!WIFSTOPPED(status)) {
170676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					fprintf(stderr,
170776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman						"pid %u not stopped\n", pid);
170876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					detach(tcp, WSTOPSIG(status));
170976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					continue;
171076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
171176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
171276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			else {
171376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SUNOS4
171476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/* A child of us stopped at exec */
171576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (WSTOPSIG(status) == SIGTRAP && followvfork)
171676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					fixvfork(tcp);
171776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SUNOS4 */
171876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
171976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tcp->flags & TCB_BPTSET) {
172076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (clearbpt(tcp) < 0) /* Pretty fatal */ {
172176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					droptcb(tcp);
172276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					cleanup();
172376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					return -1;
172476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
172576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
172676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			goto tracing;
172776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
172876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
172976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (WSTOPSIG(status) != SIGTRAP) {
173076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (WSTOPSIG(status) == SIGSTOP &&
173176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					(tcp->flags & TCB_SIGTRAPPED)) {
173276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/*
173376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * Trapped attempt to block SIGTRAP
173476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * Hope we are back in control now.
173576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 */
173676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
173776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (ptrace(PTRACE_SYSCALL,
173876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman						pid, (char *) 1, 0) < 0) {
173976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					perror("trace: ptrace(PTRACE_SYSCALL, ...)");
174076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					cleanup();
174176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					return -1;
174276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
174376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
174476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
174576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!cflag
174676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			    && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
174776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printleader(tcp);
174876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("--- %s (%s) ---",
1749ce780fc9e6067b15b65ca2904c698c77503bf635Nate Sammons					signame(WSTOPSIG(status)),
175076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					strsignal(WSTOPSIG(status)));
175176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printtrailer(tcp);
175276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
175376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if ((tcp->flags & TCB_ATTACHED) &&
175476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				!sigishandled(tcp, WSTOPSIG(status))) {
175576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				detach(tcp, WSTOPSIG(status));
175676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
175776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
175876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (ptrace(PTRACE_SYSCALL, pid, (char *) 1,
175976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				   WSTOPSIG(status)) < 0) {
176076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("trace: ptrace(PTRACE_SYSCALL, ...)");
176176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				cleanup();
176276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
176376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
176476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->flags &= ~TCB_SUSPENDED;
176576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
176676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
176776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (trace_syscall(tcp) < 0) {
176876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tcp->flags & TCB_ATTACHED)
176976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				detach(tcp, 0);
177076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			else {
177176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				ptrace(PTRACE_KILL,
177276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					tcp->pid, (char *) 1, SIGTERM);
177376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				droptcb(tcp);
177476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
177576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
177676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
177776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_EXITING) {
177876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tcp->flags & TCB_ATTACHED)
177976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				detach(tcp, 0);
178076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) {
178176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("strace: ptrace(PTRACE_CONT, ...)");
178276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				cleanup();
178376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
178476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
178576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
178676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
178776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_SUSPENDED) {
178876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!qflag)
178976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "Process %u suspended\n", pid);
179076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
179176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
179276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tracing:
179376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) {
179476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("trace: ptrace(PTRACE_SYSCALL, ...)");
179576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			cleanup();
179676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return -1;
179776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
179876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
179976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return 0;
180076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
180176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
1802bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */
180376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
180476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int curcol;
180576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
180676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef __STDC__
180776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <stdarg.h>
180876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#define VA_START(a, b) va_start(a, b)
180976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else
181076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <varargs.h>
181176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#define VA_START(a, b) va_start(a)
181276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
181376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
181476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
181576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef __STDC__
181676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantprintf(const char *fmt, ...)
181776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else
181876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantprintf(fmt, va_alist)
181976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *fmt;
182076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanva_dcl
182176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
182276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
182376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	va_list args;
182476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
182576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	VA_START(args, fmt);
182676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (outf)
182776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		curcol += vfprintf(outf, fmt, args);
182876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	va_end(args);
182976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return;
183076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
183176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
183276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
183376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanprintleader(tcp)
183476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
183576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
183676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) {
183776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp_last->flags |= TCB_REPRINT;
183876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tprintf(" <unfinished ...>\n");
183976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
184076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	curcol = 0;
184176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if ((followfork == 1 || pflag_seen > 1) && outfname)
184276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tprintf("%-5d ", tcp->pid);
184376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (nprocs > 1 && !outfname)
184476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tprintf("[pid %5u] ", tcp->pid);
184576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tflag) {
184676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		char str[sizeof("HH:MM:SS")];
184776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		struct timeval tv, dtv;
184876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		static struct timeval otv;
184976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
185076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		gettimeofday(&tv, NULL);
185176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (rflag) {
185276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (otv.tv_sec == 0)
185376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				otv = tv;
185476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tv_sub(&dtv, &tv, &otv);
185576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tprintf("%6ld.%06ld ",
185676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				(long) dtv.tv_sec, (long) dtv.tv_usec);
185776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			otv = tv;
185876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
185976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else if (tflag > 2) {
186076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tprintf("%ld.%06ld ",
186176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				(long) tv.tv_sec, (long) tv.tv_usec);
186276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
186376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else {
186476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			time_t local = tv.tv_sec;
186576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			strftime(str, sizeof(str), "%T", localtime(&local));
186676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tflag > 1)
186776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("%s.%06ld ", str, (long) tv.tv_usec);
186876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			else
186976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("%s ", str);
187076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
187176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
187276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (iflag)
187376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		printcall(tcp);
187476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
187576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
187676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
187776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantabto(col)
187876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint col;
187976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
188076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (curcol < col)
188176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tprintf("%*s", col - curcol, "");
188276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
188376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
188476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
188576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanprinttrailer(tcp)
188676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
188776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
188876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tprintf("\n");
188976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tcp_last = NULL;
189076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
18919ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
1892ea78f0f77185f7d6d0b2055805139d96e1be816cWichert Akkerman#ifdef HAVE_MP_PROCFS
18939ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
18949ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkermanint mp_ioctl (int fd, int cmd, void *arg, int size) {
18959ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
18969ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	struct iovec iov[2];
18979ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	int n = 1;
18989ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
18999ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	iov[0].iov_base = &cmd;
19009ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	iov[0].iov_len = sizeof cmd;
19019ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (arg) {
19029ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		++n;
19039ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		iov[1].iov_base = arg;
19049ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		iov[1].iov_len = size;
19059ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
19069ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
19079ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	return writev (fd, iov, n);
19089ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman}
19099ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
19109ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#endif
1911