strace.c revision 5826589d8e210c2d34c8ce496d5b7a0f54188ef0
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
477b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman#if defined(IA64) && defined(LINUX)
487b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman# include <asm/ptrace_offsets.h>
497b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman#endif
507b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman
51bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
52bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#include <poll.h>
53bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
54bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman
5576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4
5676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <sys/stropts.h>
57ea78f0f77185f7d6d0b2055805139d96e1be816cWichert Akkerman#ifdef HAVE_MP_PROCFS
581d08dcf46dd4b2cfe3e27723c05b0aec9955c591John Hughes#ifdef HAVE_SYS_UIO_H
599ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#include <sys/uio.h>
609ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#endif
6176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
621d08dcf46dd4b2cfe3e27723c05b0aec9955c591John Hughes#endif
6376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
6476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint debug = 0, followfork = 0, followvfork = 0, interactive = 0;
6576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint rflag = 0, tflag = 0, dtime = 0, cflag = 0;
6676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint iflag = 0, xflag = 0, qflag = 0;
6776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pflag_seen = 0;
6876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
6976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *username = NULL;
7076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanuid_t run_uid;
7176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermangid_t run_gid;
7276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
7376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint acolumn = DEFAULT_ACOLUMN;
7476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint max_strlen = DEFAULT_STRLEN;
7576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *outfname = NULL;
7676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert AkkermanFILE *outf;
7776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb tcbtab[MAX_PROCS];
7876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint nprocs;
7976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *progname;
8076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanextern char version[];
8176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanextern char **environ;
8276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
8376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic struct tcb *pid2tcb P((int pid));
8476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int trace P((void));
8576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void cleanup P((void));
8676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void interrupt P((int sig));
8776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic sigset_t empty_set, blocked_set;
8876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
8976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE_SIG_ATOMIC_T
9076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic volatile sig_atomic_t interrupted;
9176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !HAVE_SIG_ATOMIC_T */
9276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef __STDC__
9376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic volatile int interrupted;
9476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !__STDC__ */
9576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int interrupted;
9676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !__STDC__ */
9776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_SIG_ATOMIC_T */
9876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
99bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
10076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
10176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic struct tcb *pfd2tcb P((int pfd));
10276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void reaper P((int sig));
10376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void rebuild_pollv P((void));
104e68d61c7d6dcf986d32c462e533ea46f85183db8Wichert Akkermanstruct pollfd pollv[MAX_PROCS];
10576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
10676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
10776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
10876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void proc_poll_open P((void));
10976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void proc_poller P((int pfd));
11076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
11176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct proc_pollfd {
11276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int fd;
11376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int revents;
11476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int pid;
11576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman};
11676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
11776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int poller_pid;
11876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int proc_poll_pipe[2] = { -1, -1 };
11976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
12076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
12176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
122ea78f0f77185f7d6d0b2055805139d96e1be816cWichert Akkerman#ifdef HAVE_MP_PROCFS
1239ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#define POLLWANT	POLLWRNORM
1249ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#else
1259ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#define POLLWANT	POLLPRI
1269ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#endif
127bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */
12876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
12976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
13076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanusage(ofp, exitval)
13176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert AkkermanFILE *ofp;
13276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint exitval;
13376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
13476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	fprintf(ofp, "\
13576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanusage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\
13676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman              [-p pid] ... [-s strsize] [-u username] [command [arg ...]]\n\
13776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman   or: strace -c [-e expr] ... [-O overhead] [-S sortby] [command [arg ...]]\n\
13876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-c -- count time, calls, and errors for each syscall and report summary\n\
13976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-f -- follow forks, -ff -- with output into separate files\n\
14076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-F -- attempt to follow vforks, -h -- print help message\n\
14176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-i -- print instruction pointer at time of syscall\n\
14276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-q -- suppress messages about attaching, detaching, etc.\n\
14376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
14476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-T -- print time spent in each syscall, -V -- print version\n\
14576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\
14676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
14776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-a column -- alignment COLUMN for printing syscall results (default %d)\n\
14876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
14976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman   options: trace, abbrev, verbose, raw, signal, read, or write\n\
15076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-o file -- send trace output to FILE instead of stderr\n\
15176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
15276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-p pid -- trace process with process id PID, may be repeated\n\
15376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
15476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
15576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-u username -- run command as username handling setuid and/or setgid\n\
15676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman", DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
15776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	exit(exitval);
15876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
15976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
16076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4
16176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef MIPS
16276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
16376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanfoobar()
16476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
16576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
16676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* MIPS */
16776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SVR4 */
16876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
16976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint
17076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanmain(argc, argv)
17176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint argc;
17276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *argv[];
17376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
17476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	extern int optind;
17576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	extern char *optarg;
17676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
17776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int c, pid = 0;
17876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct sigaction sa;
17976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
18076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	static char buf[BUFSIZ];
18176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
18276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	progname = argv[0];
18376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	outf = stderr;
18476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	interactive = 1;
18576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	qualify("trace=all");
18676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	qualify("abbrev=all");
18776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	qualify("verbose=all");
18876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	qualify("signal=all");
18976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	set_sortby(DEFAULT_SORTBY);
19076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	set_personality(DEFAULT_PERSONALITY);
19176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	while ((c = getopt(argc, argv,
19276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		"+cdfFhiqrtTvVxa:e:o:O:p:s:S:u:")) != EOF) {
19376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		switch (c) {
19476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'c':
19576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			cflag++;
19676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			dtime++;
19776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
19876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'd':
19976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			debug++;
20076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
20176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'f':
20276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			followfork++;
20376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
20476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'F':
20576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			followvfork++;
20676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
20776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'h':
20876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			usage(stdout, 0);
20976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
21076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'i':
21176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			iflag++;
21276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
21376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'q':
21476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			qflag++;
21576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
21676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'r':
21776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			rflag++;
21876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tflag++;
21976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
22076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 't':
22176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tflag++;
22276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
22376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'T':
22476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			dtime++;
22576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
22676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'x':
22776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			xflag++;
22876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
22976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'v':
23076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			qualify("abbrev=none");
23176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
23276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'V':
23376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			printf("%s\n", version);
23476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(0);
23576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
23676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'a':
23776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			acolumn = atoi(optarg);
23876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
23976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'e':
24076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			qualify(optarg);
24176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
24276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'o':
24376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			outfname = strdup(optarg);
24476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
24576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'O':
24676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			set_overhead(atoi(optarg));
24776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
24876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'p':
24976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if ((pid = atoi(optarg)) == 0) {
25076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "%s: Invalid process id: %s\n",
25176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					progname, optarg);
25276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
25376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
25476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (pid == getpid()) {
25554a4767f8609abfe2d7cb1802bc9e8dca97dd08fWichert Akkerman				fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
25676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
25776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
25876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if ((tcp = alloctcb(pid)) == NULL) {
25976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "%s: tcb table full, please recompile strace\n",
26076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					progname);
26176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
26276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
26376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->flags |= TCB_ATTACHED;
26476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			pflag_seen++;
26576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
26676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 's':
26776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			max_strlen = atoi(optarg);
26876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
26976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'S':
27076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			set_sortby(optarg);
27176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
27276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'u':
27376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			username = strdup(optarg);
27476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
27576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		default:
27676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			usage(stderr, 1);
27776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
27876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
27976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
28076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
28176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* See if they want to run as another user. */
28276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (username != NULL) {
28376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		struct passwd *pent;
28476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
28576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (getuid() != 0 || geteuid() != 0) {
28676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr,
28776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				"%s: you must be root to use the -u option\n",
28876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				progname);
28976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
29076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
29176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((pent = getpwnam(username)) == NULL) {
29276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "%s: cannot find user `%s'\n",
29376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				progname, optarg);
29476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
29576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
29676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		run_uid = pent->pw_uid;
29776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		run_gid = pent->pw_gid;
29876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
29976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else {
30076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		run_uid = getuid();
30176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		run_gid = getgid();
30276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
30376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
30476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4
30576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	setreuid(geteuid(), getuid());
30676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
30776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
30876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* See if they want to pipe the output. */
30976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (outfname && (outfname[0] == '|' || outfname[0] == '!')) {
31076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((outf = popen(outfname + 1, "w")) == NULL) {
31176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "%s: can't popen '%s': %s\n",
31276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				progname, outfname + 1, strerror(errno));
31376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
31476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
31576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		free(outfname);
31676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		outfname = NULL;
31776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
31876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
31976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Check if they want to redirect the output. */
32076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (outfname) {
32154b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman		long f;
32254b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman
32376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((outf = fopen(outfname, "w")) == NULL) {
32476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "%s: can't fopen '%s': %s\n",
32576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				progname, outfname, strerror(errno));
32676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
32776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
32854b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman
32954b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman		if ((f=fcntl(fileno(outf), F_GETFD)) < 0 ) {
33054b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman			perror("failed to get flags for outputfile");
33154b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman			exit(1);
33254b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman		}
33354b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman
33454b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman		if (fcntl(fileno(outf), F_SETFD, f|FD_CLOEXEC) < 0 ) {
33554b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman			perror("failed to set flags for outputfile");
33654b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman			exit(1);
33754b4f79216c819ec93aaf5031acb1772fccef7a9Wichert Akkerman		}
33876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
33976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
34076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4
34176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	setreuid(geteuid(), getuid());
34276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
34376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
34476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!outfname) {
34576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		qflag = 1;
34676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		setvbuf(outf, buf, _IOLBF, BUFSIZ);
34776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
34876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (optind < argc)
34976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		interactive = 0;
35076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else
35176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		qflag = 1;
35276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
35376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (c = 0, tcp = tcbtab; c < MAX_PROCS; c++, tcp++) {
35476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Reinitialize the output since it may have changed. */
35576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->outf = outf;
35676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
35776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
358bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
35976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (proc_open(tcp, 1) < 0) {
36076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "trouble opening proc file\n");
36176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
36276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
36376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
364bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* !USE_PROCFS */
36576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
36676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("attach: ptrace(PTRACE_ATTACH, ...)");
36776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
36876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
36976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
370bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */
37176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!qflag)
37276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr,
37376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				"Process %u attached - interrupt to quit\n",
37476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				pid);
37576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
37676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
37776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (optind < argc) {
37876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		struct stat statbuf;
37976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		char *filename;
38076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		char pathname[MAXPATHLEN];
38176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
38276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		filename = argv[optind];
38376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (strchr(filename, '/'))
38476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			strcpy(pathname, filename);
38576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef USE_DEBUGGING_EXEC
38676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/*
38776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * Debuggers customarily check the current directory
38876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * first regardless of the path but doing that gives
38976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * security geeks a panic attack.
39076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 */
39176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else if (stat(filename, &statbuf) == 0)
39276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			strcpy(pathname, filename);
39376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* USE_DEBUGGING_EXEC */
39476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else {
39576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			char *path;
39676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			int m, n, len;
39776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
39876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			for (path = getenv("PATH"); path && *path; path += m) {
39976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (strchr(path, ':')) {
40076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					n = strchr(path, ':') - path;
40176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					m = n + 1;
40276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
40376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				else
40476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					m = n = strlen(path);
40576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (n == 0) {
40676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					getcwd(pathname, MAXPATHLEN);
40776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					len = strlen(pathname);
40876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
40976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				else {
41076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					strncpy(pathname, path, n);
41176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					len = n;
41276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
41376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (len && pathname[len - 1] != '/')
41476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					pathname[len++] = '/';
41576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				strcpy(pathname + len, filename);
41676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (stat(pathname, &statbuf) == 0)
41776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					break;
41876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
41976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
42076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (stat(pathname, &statbuf) < 0) {
42176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "%s: %s: command not found\n",
42276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				progname, filename);
42376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
42476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
42576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		switch (pid = fork()) {
42676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case -1:
42776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("strace: fork");
42876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			cleanup();
42976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
43076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
43176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 0: {
432bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
433bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		        if (outf != stderr) close (fileno (outf));
43476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef MIPS
43576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/* Kludge for SGI, see proc_open for details. */
43676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sa.sa_handler = foobar;
43776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sa.sa_flags = 0;
43876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigemptyset(&sa.sa_mask);
43976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigaction(SIGINT, &sa, NULL);
44076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* MIPS */
441bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
44276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			pause();
443bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */
444bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			kill(getpid(), SIGSTOP); /* stop HERE */
445bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
446bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* !USE_PROCFS */
447bd4125c6bcd20e8f014b682b31d6fc5d0fef3ed0Wichert Akkerman			if (outf!=stderr)
4487987cdf192632516d6ba493b0a9943f5a3a7c362Wichert Akkerman				close(fileno (outf));
449bd4125c6bcd20e8f014b682b31d6fc5d0fef3ed0Wichert Akkerman
45076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
45176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("strace: ptrace(PTRACE_TRACEME, ...)");
45276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
45376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
45476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (debug)
45576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				kill(getpid(), SIGSTOP);
45676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
45776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (username != NULL || geteuid() == 0) {
45876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				uid_t run_euid = run_uid;
45976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				gid_t run_egid = run_gid;
46076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
46176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (statbuf.st_mode & S_ISUID)
46276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					run_euid = statbuf.st_uid;
46376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (statbuf.st_mode & S_ISGID)
46476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					run_egid = statbuf.st_gid;
46576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
46676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/*
46776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * It is important to set groups before we
46876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * lose privileges on setuid.
46976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 */
4705ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman				if (username != NULL) {
4715ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman					if (initgroups(username, run_gid) < 0) {
4725ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman						perror("initgroups");
4735ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman						exit(1);
4745ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman					}
4755ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman					if (setregid(run_gid, run_egid) < 0) {
4765ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman						perror("setregid");
4775ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman						exit(1);
4785ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman					}
4795ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman					if (setreuid(run_uid, run_euid) < 0) {
4805ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman						perror("setreuid");
4815ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman						exit(1);
4825ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman					}
48376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
48476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
48576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			else
48676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				setreuid(run_uid, run_uid);
487bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */
48876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
48976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			execv(pathname, &argv[optind]);
49076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("strace: exec");
49176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			_exit(1);
49276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
49376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
49476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		default:
49576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if ((tcp = alloctcb(pid)) == NULL) {
49676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "tcb table full\n");
49776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				cleanup();
49876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
49976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
500bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
50176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (proc_open(tcp, 0) < 0) {
50276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "trouble opening proc file\n");
50376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				cleanup();
50476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
50576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
506bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */
507bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef USE_PROCFS
50876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fake_execve(tcp, pathname, &argv[optind], environ);
509bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */
51076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
51176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
51276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
51376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (pflag_seen == 0)
51476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		usage(stderr, 1);
51576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
51676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&empty_set);
51776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&blocked_set);
51876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_handler = SIG_IGN;
51976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&sa.sa_mask);
52076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_flags = 0;
52176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGTTOU, &sa, NULL);
52276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGTTIN, &sa, NULL);
52376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (interactive) {
52476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigaddset(&blocked_set, SIGHUP);
52576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigaddset(&blocked_set, SIGINT);
52676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigaddset(&blocked_set, SIGQUIT);
52776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigaddset(&blocked_set, SIGPIPE);
52876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigaddset(&blocked_set, SIGTERM);
52976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sa.sa_handler = interrupt;
53076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SUNOS4
53176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* POSIX signals on sunos4.1 are a little broken. */
53276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sa.sa_flags = SA_INTERRUPT;
53376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SUNOS4 */
53476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
53576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGHUP, &sa, NULL);
53676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGINT, &sa, NULL);
53776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGQUIT, &sa, NULL);
53876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGPIPE, &sa, NULL);
53976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGTERM, &sa, NULL);
540bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
54176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_handler = reaper;
54276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGCHLD, &sa, NULL);
543bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */
54476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
54576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (trace() < 0)
54676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		exit(1);
54776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	cleanup();
54876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	exit(0);
54976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
55076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
55176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
55276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermannewoutf(tcp)
55376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
55476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
55576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	char name[MAXPATHLEN];
55676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	FILE *fp;
55776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
55876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (outfname && followfork > 1) {
55976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sprintf(name, "%s.%u", outfname, tcp->pid);
56076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4
56176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		setreuid(geteuid(), getuid());
56276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
56376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fp = fopen(name, "w");
56476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4
56576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		setreuid(geteuid(), getuid());
56676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
56776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (fp == NULL) {
56876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("fopen");
56976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return;
57076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
57176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->outf = fp;
57276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
57376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return;
57476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
57576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
57676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *
57776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanalloctcb(pid)
57876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pid;
57976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
58076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
58176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
58276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
58376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
58476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((tcp->flags & TCB_INUSE) == 0) {
58576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->pid = pid;
58676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->parent = NULL;
58776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->nchildren = 0;
58876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->flags = TCB_INUSE | TCB_STARTUP;
58976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->outf = outf; /* Initialise to current out file */
59076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->stime.tv_sec = 0;
59176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->stime.tv_usec = 0;
59276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->pfd = -1;
59376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			nprocs++;
59476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return tcp;
59576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
59676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
59776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return NULL;
59876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
59976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
600bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
60176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint
60276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanproc_open(tcp, attaching)
60376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
60476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint attaching;
60576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
60676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	char proc[32];
60776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	long arg;
608bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef SVR4
60976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sysset_t sc_enter, sc_exit;
61076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigset_t signals;
61176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	fltset_t faults;
612bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
61376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
61476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	static int last_pfd;
61576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
61676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
617ea78f0f77185f7d6d0b2055805139d96e1be816cWichert Akkerman#ifdef HAVE_MP_PROCFS
6189ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	/* Open the process pseudo-files in /proc. */
6199ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	sprintf(proc, "/proc/%d/ctl", tcp->pid);
6209ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
6219ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("strace: open(\"/proc/...\", ...)");
6229ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6239ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6249ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
6259ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_GETFD");
6269ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6279ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6289ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
6299ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_SETFD");
6309ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6319ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6329ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	sprintf(proc, "/proc/%d/status", tcp->pid);
6339ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
6349ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("strace: open(\"/proc/...\", ...)");
6359ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6369ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6379ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) {
6389ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_GETFD");
6399ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6409ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6419ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) {
6429ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_SETFD");
6439ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6449ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6459ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	sprintf(proc, "/proc/%d/as", tcp->pid);
6469ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
6479ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("strace: open(\"/proc/...\", ...)");
6489ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6499ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6509ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) {
6519ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_GETFD");
6529ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6539ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6549ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) {
6559ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_SETFD");
6569ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6579ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6589ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#else
65976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Open the process pseudo-file in /proc. */
660bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
66176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sprintf(proc, "/proc/%d", tcp->pid);
66276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) {
663bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */
664bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	sprintf(proc, "/proc/%d/mem", tcp->pid);
665bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	if ((tcp->pfd = open(proc, O_RDWR)) < 0) {
666bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
66776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("strace: open(\"/proc/...\", ...)");
66876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
66976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
6709ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
6719ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_GETFD");
6729ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6739ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6749ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
6759ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_SETFD");
6769ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6779ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6789ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#endif
679bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD
680bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	sprintf(proc, "/proc/%d/regs", tcp->pid);
681bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) {
682bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		perror("strace: open(\"/proc/.../regs\", ...)");
683bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		return -1;
684bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	}
685bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	if (cflag) {
686bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		sprintf(proc, "/proc/%d/status", tcp->pid);
687bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) {
688bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			perror("strace: open(\"/proc/.../status\", ...)");
689bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			return -1;
690bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		}
691bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	} else
692bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		tcp->pfd_status = -1;
693bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
69476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	rebuild_pollv();
69576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!attaching) {
69676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/*
69776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * Wait for the child to pause.  Because of a race
69876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * condition we have to poll for the event.
69976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 */
70076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		for (;;) {
7019ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (IOCTL_STATUS (tcp) < 0) {
70276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("strace: PIOCSTATUS");
70376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
70476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
7059ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (tcp->status.PR_FLAGS & PR_ASLEEP)
706bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			    break;
70776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
70876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
709bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
71076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Stop the process so that we own the stop. */
71116a03d2e97415afe6cf34172a0aea97a95a0b160Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) {
71276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("strace: PIOCSTOP");
71376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
71476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
715bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
71676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef PIOCSET
71776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Set Run-on-Last-Close. */
71876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	arg = PR_RLC;
7199ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
72076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOCSET PR_RLC");
72176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
72276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
72376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Set or Reset Inherit-on-Fork. */
72476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	arg = PR_FORK;
7259ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
72676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOC{SET,RESET} PR_FORK");
72776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
72876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
72976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else  /* !PIOCSET */
730bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
73176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
73276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOCSRLC");
73376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
73476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
73576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
73676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOC{S,R}FORK");
73776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
73876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
739bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */
740bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	/* just unset the PF_LINGER flag for the Run-on-Last-Close. */
741bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) {
742bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	        perror("PIOCGFL");
743bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	        return -1;
744bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	}
745bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	arg &= ~PF_LINGER;
746bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) {
747bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	        perror("PIOCSFL");
748bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	        return -1;
749bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	}
750bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
75176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !PIOCSET */
752bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
75376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Enable all syscall entries. */
75476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	prfillset(&sc_enter);
7559ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSENTRY, &sc_enter) < 0) {
75676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOCSENTRY");
75776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
75876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
75976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Enable all syscall exits. */
76076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	prfillset(&sc_exit);
7619ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSEXIT, &sc_exit) < 0) {
76276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOSEXIT");
76376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
76476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
76576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Enable all signals. */
76676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	prfillset(&signals);
7679ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
76876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOCSTRACE");
76976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
77076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
77176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Enable all faults. */
77276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	prfillset(&faults);
7739ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
77476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOCSFAULT");
77576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
77676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
777bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */
778bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	/* set events flags. */
779bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	arg = S_SIG | S_SCE | S_SCX ;
780bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	if(ioctl(tcp->pfd, PIOCBIS, arg) < 0) {
781bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		perror("PIOCBIS");
782bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		return -1;
783bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	}
784bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
78576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!attaching) {
78676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef MIPS
78776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/*
78876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * The SGI PRSABORT doesn't work for pause() so
78976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * we send it a caught signal to wake it up.
79076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 */
79176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		kill(tcp->pid, SIGINT);
79276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !MIPS */
793bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef PRSABORT
79476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* The child is in a pause(), abort it. */
7959ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		arg = PRSABORT;
7969ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
79776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("PIOCRUN");
79876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return -1;
79976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
800bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
801bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !MIPS*/
802bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD
803bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		/* wake up the child if it received the SIGSTOP */
804bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		kill(tcp->pid, SIGCONT);
805bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
80676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		for (;;) {
80776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/* Wait for the child to do something. */
8089ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (IOCTL_WSTOP (tcp) < 0) {
80976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("PIOCWSTOP");
81076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
81176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
8129ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (tcp->status.PR_WHY == PR_SYSENTRY) {
813bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				tcp->flags &= ~TCB_INSYSCALL;
814bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				get_scno(tcp);
815bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				if (tcp->scno == SYS_execve)
81676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					break;
81776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
81876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/* Set it running: maybe execve will be next. */
819bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
8209ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			arg = 0;
8219ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
822bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */
823bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) {
824bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
82576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("PIOCRUN");
82676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
82776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
828bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD
829bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			/* handle the case where we "opened" the child before
830bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			   it did the kill -STOP */
831bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			if (tcp->status.PR_WHY == PR_SIGNALLED &&
832bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			    tcp->status.PR_WHAT == SIGSTOP)
833bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			        kill(tcp->pid, SIGCONT);
834bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
83576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
836bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
83776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
838bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */
839bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	} else {
8402e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman		if (attaching < 2) {
8412e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			/* We are attaching to an already running process.
8422e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			 * Try to figure out the state of the process in syscalls,
8432e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			 * to handle the first event well.
8442e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			 * This is done by having a look at the "wchan" property of the
8452e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			 * process, which tells where it is stopped (if it is). */
8462e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			FILE * status;
8472e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			char wchan[20]; /* should be enough */
8482e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman
8492e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			sprintf(proc, "/proc/%d/status", tcp->pid);
8502e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			status = fopen(proc, "r");
8512e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			if (status &&
8522e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			    (fscanf(status, "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d"
8532e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman				    "%*d,%*d %*d,%*d %19s", wchan) == 1) &&
8542e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			    strcmp(wchan, "nochan") && strcmp(wchan, "spread") &&
8552e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			    strcmp(wchan, "stopevent")) {
8562e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman				/* The process is asleep in the middle of a syscall.
8572e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman				   Fake the syscall entry event */
8582e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman				tcp->flags &= ~(TCB_INSYSCALL|TCB_STARTUP);
8592e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman				tcp->status.PR_WHY = PR_SYSENTRY;
8602e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman				trace_syscall(tcp);
8612e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			}
8622e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			if (status)
8632e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman				fclose(status);
8642e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman		} /* otherwise it's a fork being followed */
865bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	}
866bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
86776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
86876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (proc_poll_pipe[0] != -1)
86976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		proc_poller(tcp->pfd);
87076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (nprocs > 1) {
87176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		proc_poll_open();
87276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		proc_poller(last_pfd);
87376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		proc_poller(tcp->pfd);
87476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
87576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	last_pfd = tcp->pfd;
87676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
87776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return 0;
87876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
87976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
880bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */
88176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
88276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic struct tcb *
88376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanpid2tcb(pid)
88476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pid;
88576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
88676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
88776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
88876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
88976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
89076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (pid && tcp->pid != pid)
89176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
89276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_INUSE)
89376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return tcp;
89476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
89576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return NULL;
89676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
89776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
898bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
89976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
90076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic struct tcb *
90176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanpfd2tcb(pfd)
90276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pfd;
90376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
90476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
90576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
90676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
90776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
90876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->pfd != pfd)
90976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
91076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_INUSE)
91176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return tcp;
91276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
91376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return NULL;
91476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
91576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
916bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */
91776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
91876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
91976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermandroptcb(tcp)
92076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
92176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
92276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp->pid == 0)
92376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return;
92476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	nprocs--;
92576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tcp->pid = 0;
92676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tcp->flags = 0;
92776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp->pfd != -1) {
92876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		close(tcp->pfd);
92976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->pfd = -1;
930bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD
931bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		if (tcp->pfd_reg != -1) {
932bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		        close(tcp->pfd_reg);
933bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		        tcp->pfd_reg = -1;
934bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		}
935bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		if (tcp->pfd_status != -1) {
936bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			close(tcp->pfd_status);
937bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			tcp->pfd_status = -1;
938bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		}
939bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !FREEBSD */
940bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
94176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		rebuild_pollv();
94276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
94376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
94476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp->parent != NULL) {
94576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->parent->nchildren--;
94676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->parent = NULL;
94776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
94876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#if 0
94976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp->outf != stderr)
95076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fclose(tcp->outf);
95176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
95276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tcp->outf = 0;
95376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
95476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
955bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef USE_PROCFS
95676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
95776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
95876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanresume(tcp)
95976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
96076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
96176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp == NULL)
96276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
96376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
96476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!(tcp->flags & TCB_SUSPENDED)) {
96576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid);
96676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
96776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
96876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tcp->flags &= ~TCB_SUSPENDED;
96976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
97076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) {
97176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("resume: ptrace(PTRACE_SYSCALL, ...)");
97276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
97376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
97476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
97576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!qflag)
97676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fprintf(stderr, "Process %u resumed\n", tcp->pid);
97776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return 0;
97876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
97976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
980bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */
98176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
98276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman/* detach traced process; continue with sig */
98376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
98476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
98576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermandetach(tcp, sig)
98676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
98776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig;
98876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
98976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int error = 0;
99076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX
99176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int status;
99276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
99376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
99476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp->flags & TCB_BPTSET)
99576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sig = SIGKILL;
99676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
99776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX
99876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/*
99976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	 * Linux wrongly insists the child be stopped
100076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	 * before detaching.  Arghh.  We go through hoops
100176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	 * to make a clean break of things.
100276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	 */
1003dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman#if defined(SPARC)
1004dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman#undef PTRACE_DETACH
1005dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman#define PTRACE_DETACH PTRACE_SUNDETACH
1006dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman#endif
100776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
100876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* On a clear day, you can see forever. */
100976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
101076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (errno != ESRCH) {
101176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Shouldn't happen. */
101276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("detach: ptrace(PTRACE_DETACH, ...)");
101376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
101476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (kill(tcp->pid, 0) < 0) {
101576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (errno != ESRCH)
101676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("detach: checking sanity");
101776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
101876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (kill(tcp->pid, SIGSTOP) < 0) {
101976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (errno != ESRCH)
102076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("detach: stopping child");
102176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
102276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else {
102376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		for (;;) {
102476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (waitpid(tcp->pid, &status, 0) < 0) {
102576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (errno != ECHILD)
102676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					perror("detach: waiting");
102776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
102876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
102976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!WIFSTOPPED(status)) {
103076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/* Au revoir, mon ami. */
103176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
103276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
103376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (WSTOPSIG(status) == SIGSTOP) {
103476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if ((error = ptrace(PTRACE_DETACH,
103576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				    tcp->pid, (char *) 1, sig)) < 0) {
103676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					if (errno != ESRCH)
103776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman						perror("detach: ptrace(PTRACE_DETACH, ...)");
103876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					/* I died trying. */
103976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
104076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
104176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
104276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1,
104376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			    WSTOPSIG(status) == SIGTRAP ?
104476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			    0 : WSTOPSIG(status))) < 0) {
104576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (errno != ESRCH)
104676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					perror("detach: ptrace(PTRACE_CONT, ...)");
104776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
104876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
104976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
105076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
105176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* LINUX */
105276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
105376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#if defined(SUNOS4)
105476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
105576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (sig && kill(tcp->pid, sig) < 0)
105676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("detach: kill");
105776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sig = 0;
105876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0)
105976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("detach: ptrace(PTRACE_DETACH, ...)");
106076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SUNOS4 */
106176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
1062bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef USE_PROCFS
106376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (waiting_parent(tcp))
106476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		error = resume(tcp->parent);
1065bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */
106676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
106776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!qflag)
106876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fprintf(stderr, "Process %u detached\n", tcp->pid);
106976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
107076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	droptcb(tcp);
107176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return error;
107276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
107376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
1074bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
107576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
107676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
107776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanreaper(sig)
107876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig;
107976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
108076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int pid;
108176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int status;
108276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
108376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
108476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#if 0
108576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		struct tcb *tcp;
108676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
108776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp = pid2tcb(pid);
108876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp)
108976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
109076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
109176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
109276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
109376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
1094bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */
109576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
109676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
109776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermancleanup()
109876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
109976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
110076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
110176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
110276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
110376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!(tcp->flags & TCB_INUSE))
110476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
110576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (debug)
110676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr,
110776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				"cleanup: looking at pid %u\n", tcp->pid);
110876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp_last &&
110976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		    (!outfname || followfork < 2 || tcp_last == tcp)) {
111076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tprintf(" <unfinished ...>\n");
111176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp_last = NULL;
111276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
111376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_ATTACHED)
111476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			detach(tcp, 0);
111576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else {
111676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			kill(tcp->pid, SIGCONT);
111776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			kill(tcp->pid, SIGTERM);
111876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
111976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
112076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (cflag)
112176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		call_summary(outf);
112276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
112376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
112476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
112576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermaninterrupt(sig)
112676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig;
112776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
112876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	interrupted = 1;
112976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
113076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
113176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_STRERROR
113276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
113376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SYS_ERRLIST_DECLARED
113476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanextern int sys_nerr;
113576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanextern char *sys_errlist[];
113676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SYS_ERRLIST_DECLARED */
113776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
113876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanconst char *
113976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstrerror(errno)
114076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint errno;
114176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
114276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	static char buf[64];
114376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
114476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (errno < 1 || errno >= sys_nerr) {
114576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sprintf(buf, "Unknown error %d", errno);
114676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return buf;
114776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
114876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return sys_errlist[errno];
114976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
115076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
115176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* HAVE_STERRROR */
115276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
115376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_STRSIGNAL
115476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
115576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SYS_SIGLIST_DECLARED
115676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE__SYS_SIGLIST
115776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	extern char *_sys_siglist[];
115876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else
115976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	extern char *sys_siglist[];
116076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
116176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SYS_SIGLIST_DECLARED */
116276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
116376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanconst char *
116476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstrsignal(sig)
116576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig;
116676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
116776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	static char buf[64];
116876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
116976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (sig < 1 || sig >= NSIG) {
117076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sprintf(buf, "Unknown signal %d", sig);
117176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return buf;
117276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
117376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE__SYS_SIGLIST
117476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return _sys_siglist[sig];
117576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else
117676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return sys_siglist[sig];
117776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
117876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
117976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
118076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* HAVE_STRSIGNAL */
118176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
1182bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS
118376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
118476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
118576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanrebuild_pollv()
118676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
118776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i, j;
118876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
118976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
119076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = j = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
119176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!(tcp->flags & TCB_INUSE))
119276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
119376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		pollv[j].fd = tcp->pfd;
11949ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		pollv[j].events = POLLWANT;
119576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		j++;
119676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
119776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (j != nprocs) {
119876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fprintf(stderr, "strace: proc miscount\n");
119976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		exit(1);
120076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
120176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
120276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
120376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
120476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
120576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
120676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanproc_poll_open()
120776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
120876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int arg;
120976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
121076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
121176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (pipe(proc_poll_pipe) < 0) {
121276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("pipe");
121376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		exit(1);
121476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
121576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0; i < 2; i++) {
121676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) {
121776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("F_GETFD");
121876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
121976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
122076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) {
122176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("F_SETFD");
122276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
122376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
122476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
122576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
122676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
122776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
122876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanproc_poll(pollv, nfds, timeout)
122976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct pollfd *pollv;
123076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint nfds;
123176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint timeout;
123276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
123376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
123476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int n;
123576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct proc_pollfd pollinfo;
123676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
123776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
123876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return n;
123976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (n != sizeof(struct proc_pollfd)) {
124076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fprintf(stderr, "panic: short read: %d\n", n);
124176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		exit(1);
124276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
124376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0; i < nprocs; i++) {
124476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (pollv[i].fd == pollinfo.fd)
124576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			pollv[i].revents = pollinfo.revents;
124676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else
124776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			pollv[i].revents = 0;
124876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
124976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	poller_pid = pollinfo.pid;
125076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return 1;
125176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
125276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
125376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
125476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanwakeup_handler(sig)
125576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig;
125676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
125776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
125876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
125976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
126076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanproc_poller(pfd)
126176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pfd;
126276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
126376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct proc_pollfd pollinfo;
126476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct sigaction sa;
126576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigset_t blocked_set, empty_set;
126676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
126776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int n;
126876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct rlimit rl;
1269bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD
1270bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	struct procfs_status pfs;
1271bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
127276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
127376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	switch (fork()) {
127476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	case -1:
127576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("fork");
127676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		_exit(0);
127776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	case 0:
127876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		break;
127976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	default:
128076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return;
128176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
128276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
128376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
128476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_flags = 0;
128576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&sa.sa_mask);
128676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGHUP, &sa, NULL);
128776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGINT, &sa, NULL);
128876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGQUIT, &sa, NULL);
128976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGPIPE, &sa, NULL);
129076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGTERM, &sa, NULL);
129176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_handler = wakeup_handler;
129276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGUSR1, &sa, NULL);
129376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&blocked_set);
129476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaddset(&blocked_set, SIGUSR1);
129576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigprocmask(SIG_BLOCK, &blocked_set, NULL);
129676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&empty_set);
129776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
129876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
129976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("getrlimit(RLIMIT_NOFILE, ...)");
130076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		_exit(0);
130176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
130276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	n = rl.rlim_cur;
130376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0; i < n; i++) {
130476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (i != pfd && i != proc_poll_pipe[1])
130576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			close(i);
130676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
130776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
130876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	pollinfo.fd = pfd;
130976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	pollinfo.pid = getpid();
131076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (;;) {
1311bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
1312bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	        if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
1313bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */
1314bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman	        if (ioctl(pfd, PIOCWSTOP, &pfs) < 0)
1315bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
13169ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		{
131776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			switch (errno) {
131876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case EINTR:
131976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
132076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case EBADF:
132176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				pollinfo.revents = POLLERR;
132276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
132376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case ENOENT:
132476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				pollinfo.revents = POLLHUP;
132576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
132676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			default:
132776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("proc_poller: PIOCWSTOP");
132876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
132976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
133076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			_exit(0);
133176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
13329ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		pollinfo.revents = POLLWANT;
133376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
133476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigsuspend(&empty_set);
133576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
133676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
133776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
133876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
133976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
134076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
134176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchoose_pfd()
134276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
134376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i, j;
134476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
134576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
134676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	static int last;
134776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
134876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (followfork < 2 &&
13499ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	    last < nprocs && (pollv[last].revents & POLLWANT)) {
135076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/*
135176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * The previous process is ready to run again.  We'll
135276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * let it do so if it is currently in a syscall.  This
135376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * heuristic improves the readability of the trace.
135476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 */
135576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp = pfd2tcb(pollv[last].fd);
135676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp && (tcp->flags & TCB_INSYSCALL))
135776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return pollv[last].fd;
135876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
135976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
136076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0; i < nprocs; i++) {
136176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Let competing children run round robin. */
136276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		j = (i + last + 1) % nprocs;
136376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (pollv[j].revents & (POLLHUP | POLLERR)) {
136476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp = pfd2tcb(pollv[j].fd);
136576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!tcp) {
136676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "strace: lost proc\n");
136776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
136876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
136976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
137076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return -1;
137176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
13729ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		if (pollv[j].revents & POLLWANT) {
137376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			last = j;
137476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return pollv[j].fd;
137576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
137676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
137776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	fprintf(stderr, "strace: nothing ready\n");
137876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	exit(1);
137976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
138076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
138176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
138276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantrace()
138376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
13849dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#ifdef POLL_HACK
13859dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman	struct tcb *in_syscall;
13869dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#endif
138776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
138876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int pfd;
138976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int what;
139076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int ioctl_result = 0, ioctl_errno = 0;
13919ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	long arg;
139276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
139376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (;;) {
139476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interactive)
139576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigprocmask(SIG_SETMASK, &empty_set, NULL);
139676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
139776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (nprocs == 0)
139876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
139976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
140076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		switch (nprocs) {
140176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 1:
140276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
140376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (proc_poll_pipe[0] == -1) {
140476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
140576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tcp = pid2tcb(0);
140676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (!tcp)
140776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					continue;
140876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				pfd = tcp->pfd;
140976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (pfd == -1)
141076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					continue;
141176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
141276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
141376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
141476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/* fall through ... */
141576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
141676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		default:
141776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE_POLLABLE_PROCFS
14189dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#ifdef POLL_HACK
14199dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman		        /* On some systems (e.g. UnixWare) we get too much ugly
14209dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman			   "unfinished..." stuff when multiple proceses are in
14219dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman			   syscalls.  Here's a nasty hack */
14229dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman
14239dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman			if (in_syscall) {
14249dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				struct pollfd pv;
14259dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				tcp = in_syscall;
14269dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				in_syscall = NULL;
14279dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				pv.fd = tcp->pfd;
14289dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				pv.events = POLLWANT;
14299dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				if ((what = poll (&pv, 1, 1)) < 0) {
14309dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman					if (interrupted)
14319dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman						return 0;
14329dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman					continue;
14339dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				}
14349dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				else if (what == 1 && pv.revents & POLLWANT) {
14359dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman					goto FOUND;
14369dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman				}
14379dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman			}
14389dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#endif
14399dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman
144076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (poll(pollv, nprocs, INFTIM) < 0) {
144176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (interrupted)
144276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					return 0;
144376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
144476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
144576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !HAVE_POLLABLE_PROCFS */
144676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (proc_poll(pollv, nprocs, INFTIM) < 0) {
144776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (interrupted)
144876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					return 0;
144976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
145076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
145176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
145276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			pfd = choose_pfd();
145376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (pfd == -1)
145476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
145576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
145676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
145776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
145876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Look up `pfd' in our table. */
145976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((tcp = pfd2tcb(pfd)) == NULL) {
146076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "unknown pfd: %u\n", pfd);
146176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
146276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
14639dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman	FOUND:
146476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Get the status of the process. */
146576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!interrupted) {
1466bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
14679ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			ioctl_result = IOCTL_WSTOP (tcp);
1468bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */
1469bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			/* Thanks to some scheduling mystery, the first poller
1470bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			   sometimes waits for the already processed end of fork
1471bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			   event. Doing a non blocking poll here solves the problem. */
1472bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			if (proc_poll_pipe[0] != -1)
1473bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				ioctl_result = IOCTL_STATUS (tcp);
1474bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			else
1475bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			  	ioctl_result = IOCTL_WSTOP (tcp);
1476bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */
147776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			ioctl_errno = errno;
147876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
147976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (proc_poll_pipe[0] != -1) {
148076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (ioctl_result < 0)
148176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					kill(poller_pid, SIGKILL);
148276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				else
148376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					kill(poller_pid, SIGUSR1);
148476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
148576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
148676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
148776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interrupted)
148876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return 0;
148976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
149076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interactive)
149176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigprocmask(SIG_BLOCK, &blocked_set, NULL);
149276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
149376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (ioctl_result < 0) {
149476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/* Find out what happened if it failed. */
149576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			switch (ioctl_errno) {
149676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case EINTR:
149776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case EBADF:
149876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
1499bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD
1500bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			case ENOTTY:
1501bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
150276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case ENOENT:
150376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				droptcb(tcp);
150476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
150576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			default:
150676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("PIOCWSTOP");
150776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
150876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
150976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
151076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
15112e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman#ifdef FREEBSD
15122e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman		if ((tcp->flags & TCB_STARTUP) && (tcp->status.PR_WHY == PR_SYSEXIT)) {
15132e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			/* discard first event for a syscall we never entered */
15142e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			IOCTL (tcp->pfd, PIOCRUN, 0);
15152e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman			continue;
15162e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman		}
15172e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman#endif
15182e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman
151976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* clear the just started flag */
152076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->flags &= ~TCB_STARTUP;
152176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
152276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* set current output file */
152376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		outf = tcp->outf;
152476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
152576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (cflag) {
152676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			struct timeval stime;
1527bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD
1528bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			char buf[1024];
1529bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			int len;
1530bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman
1531bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) {
1532bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				buf[len] = '\0';
1533bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				sscanf(buf,
1534bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				       "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld",
1535bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				       &stime.tv_sec, &stime.tv_usec);
1536bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman			} else
1537bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman				stime.tv_sec = stime.tv_usec = 0;
1538bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* !FREEBSD */
153976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			stime.tv_sec = tcp->status.pr_stime.tv_sec;
154076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
1541bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !FREEBSD */
154276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tv_sub(&tcp->dtime, &stime, &tcp->stime);
154376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->stime = stime;
154476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
15459ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		what = tcp->status.PR_WHAT;
15469ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		switch (tcp->status.PR_WHY) {
1547bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
154876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case PR_REQUESTED:
15499ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (tcp->status.PR_FLAGS & PR_ASLEEP) {
15509ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman				tcp->status.PR_WHY = PR_SYSENTRY;
155176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (trace_syscall(tcp) < 0) {
155276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					fprintf(stderr, "syscall trouble\n");
155376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					exit(1);
155476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
155576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
155676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
1557bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !FREEBSD */
155876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case PR_SYSENTRY:
15599dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#ifdef POLL_HACK
15609dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman		        in_syscall = tcp;
15619dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#endif
156276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case PR_SYSEXIT:
156376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (trace_syscall(tcp) < 0) {
156476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "syscall trouble\n");
156576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
156676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
156776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
156876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case PR_SIGNALLED:
156976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) {
157076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printleader(tcp);
157176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("--- %s (%s) ---",
1572ce780fc9e6067b15b65ca2904c698c77503bf635Nate Sammons					signame(what), strsignal(what));
157376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printtrailer(tcp);
15745826589d8e210c2d34c8ce496d5b7a0f54188ef0John Hughes#ifdef PR_INFO
15755826589d8e210c2d34c8ce496d5b7a0f54188ef0John Hughes				if (tcp->status.PR_INFO.si_signo == what) {
15765826589d8e210c2d34c8ce496d5b7a0f54188ef0John Hughes					printleader(tcp);
15775826589d8e210c2d34c8ce496d5b7a0f54188ef0John Hughes					tprintf("    siginfo=");
15785826589d8e210c2d34c8ce496d5b7a0f54188ef0John Hughes					printsiginfo(&tcp->status.PR_INFO, 1);
15795826589d8e210c2d34c8ce496d5b7a0f54188ef0John Hughes					printtrailer(tcp);
15805826589d8e210c2d34c8ce496d5b7a0f54188ef0John Hughes				}
15815826589d8e210c2d34c8ce496d5b7a0f54188ef0John Hughes#endif
158276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
158376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
158476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case PR_FAULTED:
158576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!cflag && (qual_flags[what] & QUAL_FAULT)) {
158676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printleader(tcp);
158776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("=== FAULT %d ===", what);
158876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printtrailer(tcp);
158976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
159076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
1591bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD
1592bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		case 0: /* handle case we polled for nothing */
1593bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		  	continue;
1594bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
159576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		default:
15969ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
159776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
159876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
159976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
16009ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		arg = 0;
1601bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD
16029ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
1603bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else
1604bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman		if (IOCTL (tcp->pfd, PIOCRUN, 0) < 0) {
1605bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif
160676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("PIOCRUN");
160776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
160876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
160976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
161076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return 0;
161176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
161276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
1613bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* !USE_PROCFS */
161476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
161576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
161676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantrace()
161776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
161876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int pid;
161976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int wait_errno;
162076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int status;
162176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
162276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX
162376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct rusage ru;
16242f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman#ifdef __WALL
16252f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman	static int wait4_options = __WALL;
16262f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman#endif
162776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* LINUX */
162876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
162976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	while (nprocs != 0) {
163076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interactive)
163176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigprocmask(SIG_SETMASK, &empty_set, NULL);
163276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX
16332f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman#ifdef __WALL
16342f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman		pid = wait4(-1, &status, wait4_options, cflag ? &ru : NULL);
16352f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman		if ((wait4_options & __WALL) && errno == EINVAL) {
16362f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman			/* this kernel does not support __WALL */
16372f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman			wait4_options &= ~__WALL;
16382f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman			errno = 0;
16392f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman			pid = wait4(-1, &status, wait4_options,
16402f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman					cflag ? &ru : NULL);
16412f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman		}
16422f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman		if (!(wait4_options & __WALL) && errno == ECHILD) {
16432f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman			/* most likely a "cloned" process */
16442f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman			pid = wait4(-1, &status, __WCLONE,
16452f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman					cflag ? &ru : NULL);
16462f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman			if (pid == -1) {
16472f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman				fprintf(stderr, "strace: clone wait4 "
16482f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman						"failed: %s\n", strerror(errno));
16492f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman			}
16502f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman		}
16512f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman#else
165276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
16532f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman#endif /* __WALL */
165476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* LINUX */
165576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SUNOS4
165676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		pid = wait(&status);
165776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SUNOS4 */
165876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		wait_errno = errno;
165976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interactive)
166076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigprocmask(SIG_BLOCK, &blocked_set, NULL);
166176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
166276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interrupted)
166376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return 0;
166476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
166576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (pid == -1) {
166676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			switch (wait_errno) {
166776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case EINTR:
166876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
166976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case ECHILD:
167076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/*
167176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * We would like to verify this case
167276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * but sometimes a race in Solbourne's
167376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * version of SunOS sometimes reports
167476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * ECHILD before sending us SIGCHILD.
167576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 */
167676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#if 0
167776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (nprocs == 0)
167876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					return 0;
167976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "strace: proc miscount\n");
168076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
168176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
168276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return 0;
168376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			default:
168476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				errno = wait_errno;
168576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("strace: wait");
168676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
168776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
168876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
168976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (debug)
169076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
169176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
169276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Look up `pid' in our table. */
169376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((tcp = pid2tcb(pid)) == NULL) {
1694faf722234dc8af97776f94fdda7e100fb60650a2Wichert Akkerman#if 0 /* XXX davidm */ /* WTA: disabled again */
16958b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman			struct tcb *tcpchild;
16968b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman
16978b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman			if ((tcpchild = alloctcb(pid)) == NULL) {
16988b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman				fprintf(stderr, " [tcb table full]\n");
16998b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman				kill(pid, SIGKILL); /* XXX */
17008b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman				return 0;
17018b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman			}
17028b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman			tcpchild->flags |= TCB_ATTACHED;
17038b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman			newoutf(tcpchild);
17048b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman			tcp->nchildren++;
17058b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman			if (!qflag)
17068b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman				fprintf(stderr, "Process %d attached\n", pid);
17078b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman#else
170876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "unknown pid: %u\n", pid);
170976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (WIFSTOPPED(status))
171076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				ptrace(PTRACE_CONT, pid, (char *) 1, 0);
171176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
17128b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman#endif
171376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
171476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* set current output file */
171576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		outf = tcp->outf;
171676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (cflag) {
171776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX
171876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
171976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->stime = ru.ru_stime;
172076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !LINUX */
172176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
172276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
172376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_SUSPENDED) {
172476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/*
172576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * Apparently, doing any ptrace() call on a stopped
172676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * process, provokes the kernel to report the process
172776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * status again on a subsequent wait(), even if the
172876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * process has not been actually restarted.
172976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * Since we have inspected the arguments of suspended
173076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * processes we end up here testing for this case.
173176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 */
173276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
173376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
173476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (WIFSIGNALED(status)) {
173576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!cflag
173676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			    && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
173776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printleader(tcp);
173876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("+++ killed by %s +++",
1739ce780fc9e6067b15b65ca2904c698c77503bf635Nate Sammons					signame(WTERMSIG(status)));
174076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printtrailer(tcp);
174176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
174276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
174376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
174476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
174576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (WIFEXITED(status)) {
174676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (debug)
174776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "pid %u exited\n", pid);
174876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tcp->flags & TCB_ATTACHED)
174976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr,
175076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					"PANIC: attached pid %u exited\n",
175176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					pid);
175276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
175376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
175476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
175576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!WIFSTOPPED(status)) {
175676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
175776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
175876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
175976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
176076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (debug)
176176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "pid %u stopped, [%s]\n",
1762ce780fc9e6067b15b65ca2904c698c77503bf635Nate Sammons				pid, signame(WSTOPSIG(status)));
176376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
176476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_STARTUP) {
176576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/*
176676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * This flag is there to keep us in sync.
176776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * Next time this process stops it should
176876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * really be entering a system call.
176976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 */
177076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->flags &= ~TCB_STARTUP;
177176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tcp->flags & TCB_ATTACHED) {
177276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/*
177376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * Interestingly, the process may stop
177476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * with STOPSIG equal to some other signal
177576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * than SIGSTOP if we happend to attach
177676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * just before the process takes a signal.
177776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 */
177876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (!WIFSTOPPED(status)) {
177976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					fprintf(stderr,
178076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman						"pid %u not stopped\n", pid);
178176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					detach(tcp, WSTOPSIG(status));
178276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					continue;
178376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
178476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
178576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			else {
178676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SUNOS4
178776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/* A child of us stopped at exec */
178876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (WSTOPSIG(status) == SIGTRAP && followvfork)
178976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					fixvfork(tcp);
179076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SUNOS4 */
179176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
179276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tcp->flags & TCB_BPTSET) {
179376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (clearbpt(tcp) < 0) /* Pretty fatal */ {
179476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					droptcb(tcp);
179576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					cleanup();
179676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					return -1;
179776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
179876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
179976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			goto tracing;
180076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
180176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
180276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (WSTOPSIG(status) != SIGTRAP) {
180376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (WSTOPSIG(status) == SIGSTOP &&
180476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					(tcp->flags & TCB_SIGTRAPPED)) {
180576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/*
180676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * Trapped attempt to block SIGTRAP
180776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * Hope we are back in control now.
180876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 */
180976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
181076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (ptrace(PTRACE_SYSCALL,
181176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman						pid, (char *) 1, 0) < 0) {
181276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					perror("trace: ptrace(PTRACE_SYSCALL, ...)");
181376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					cleanup();
181476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					return -1;
181576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
181676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
181776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
181876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!cflag
181976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			    && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
18207b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman				unsigned long addr = 0, pc = 0;
18217b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman#ifdef PT_GETSIGINFO
18227b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman#				define PSR_RI	41
18237b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman				struct siginfo si;
18247b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman				unsigned long psr;
18257b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman
18267b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman				upeek(pid, PT_CR_IPSR, &psr);
18277b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman				upeek(pid, PT_CR_IIP, &pc);
18287b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman
18297b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman				pc += (psr >> PSR_RI) & 0x3;
18307b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman				ptrace(PT_GETSIGINFO, pid, 0, (long) &si);
18317b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman				addr = (unsigned long) si.si_addr;
18327b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman#endif
183376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printleader(tcp);
18347b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman				tprintf("--- %s (%s) @ %lx (%lx) ---",
1835ce780fc9e6067b15b65ca2904c698c77503bf635Nate Sammons					signame(WSTOPSIG(status)),
18367b3346be42d6a1f539e95d385ee498f8b3c529f8Wichert Akkerman					strsignal(WSTOPSIG(status)), pc, addr);
183776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printtrailer(tcp);
183876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
183976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if ((tcp->flags & TCB_ATTACHED) &&
184076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				!sigishandled(tcp, WSTOPSIG(status))) {
184176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				detach(tcp, WSTOPSIG(status));
184276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
184376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
184476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (ptrace(PTRACE_SYSCALL, pid, (char *) 1,
184576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				   WSTOPSIG(status)) < 0) {
184676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("trace: ptrace(PTRACE_SYSCALL, ...)");
184776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				cleanup();
184876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
184976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
185076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->flags &= ~TCB_SUSPENDED;
185176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
185276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
185376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (trace_syscall(tcp) < 0) {
185476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tcp->flags & TCB_ATTACHED)
185576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				detach(tcp, 0);
185676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			else {
185776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				ptrace(PTRACE_KILL,
185876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					tcp->pid, (char *) 1, SIGTERM);
185976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				droptcb(tcp);
186076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
186176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
186276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
186376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_EXITING) {
186476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tcp->flags & TCB_ATTACHED)
186576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				detach(tcp, 0);
186676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) {
186776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("strace: ptrace(PTRACE_CONT, ...)");
186876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				cleanup();
186976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
187076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
187176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
187276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
187376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_SUSPENDED) {
187476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!qflag)
187576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "Process %u suspended\n", pid);
187676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
187776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
187876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tracing:
187976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) {
188076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("trace: ptrace(PTRACE_SYSCALL, ...)");
188176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			cleanup();
188276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return -1;
188376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
188476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
188576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return 0;
188676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
188776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
1888bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */
188976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
189076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int curcol;
189176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
189276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef __STDC__
189376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <stdarg.h>
189476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#define VA_START(a, b) va_start(a, b)
189576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else
189676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <varargs.h>
189776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#define VA_START(a, b) va_start(a)
189876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
189976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
190076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
190176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef __STDC__
190276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantprintf(const char *fmt, ...)
190376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else
190476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantprintf(fmt, va_alist)
190576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *fmt;
190676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanva_dcl
190776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
190876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
190976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	va_list args;
191076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
191176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	VA_START(args, fmt);
191276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (outf)
191376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		curcol += vfprintf(outf, fmt, args);
191476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	va_end(args);
191576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return;
191676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
191776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
191876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
191976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanprintleader(tcp)
192076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
192176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
192276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) {
192376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp_last->flags |= TCB_REPRINT;
192476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tprintf(" <unfinished ...>\n");
192576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
192676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	curcol = 0;
192776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if ((followfork == 1 || pflag_seen > 1) && outfname)
192876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tprintf("%-5d ", tcp->pid);
192976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (nprocs > 1 && !outfname)
193076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tprintf("[pid %5u] ", tcp->pid);
193176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tflag) {
193276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		char str[sizeof("HH:MM:SS")];
193376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		struct timeval tv, dtv;
193476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		static struct timeval otv;
193576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
193676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		gettimeofday(&tv, NULL);
193776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (rflag) {
193876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (otv.tv_sec == 0)
193976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				otv = tv;
194076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tv_sub(&dtv, &tv, &otv);
194176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tprintf("%6ld.%06ld ",
194276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				(long) dtv.tv_sec, (long) dtv.tv_usec);
194376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			otv = tv;
194476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
194576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else if (tflag > 2) {
194676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tprintf("%ld.%06ld ",
194776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				(long) tv.tv_sec, (long) tv.tv_usec);
194876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
194976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else {
195076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			time_t local = tv.tv_sec;
195176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			strftime(str, sizeof(str), "%T", localtime(&local));
195276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tflag > 1)
195376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("%s.%06ld ", str, (long) tv.tv_usec);
195476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			else
195576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("%s ", str);
195676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
195776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
195876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (iflag)
195976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		printcall(tcp);
196076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
196176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
196276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
196376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantabto(col)
196476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint col;
196576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
196676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (curcol < col)
196776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tprintf("%*s", col - curcol, "");
196876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
196976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
197076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
197176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanprinttrailer(tcp)
197276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
197376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
197476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tprintf("\n");
197576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tcp_last = NULL;
197676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
19779ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
1978ea78f0f77185f7d6d0b2055805139d96e1be816cWichert Akkerman#ifdef HAVE_MP_PROCFS
19799ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
19809ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkermanint mp_ioctl (int fd, int cmd, void *arg, int size) {
19819ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
19829ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	struct iovec iov[2];
19839ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	int n = 1;
19849ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
19859ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	iov[0].iov_base = &cmd;
19869ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	iov[0].iov_len = sizeof cmd;
19879ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (arg) {
19889ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		++n;
19899ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		iov[1].iov_base = arg;
19909ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		iov[1].iov_len = size;
19919ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
19929ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
19939ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	return writev (fd, iov, n);
19949ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman}
19959ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
19969ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#endif
1997