strace.c revision 54a4767f8609abfe2d7cb1802bc9e8dca97dd08f
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>
576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * All rights reserved.
676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman *
776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Redistribution and use in source and binary forms, with or without
876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * modification, are permitted provided that the following conditions
976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * are met:
1076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 1. Redistributions of source code must retain the above copyright
1176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman *    notice, this list of conditions and the following disclaimer.
1276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 2. Redistributions in binary form must reproduce the above copyright
1376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman *    notice, this list of conditions and the following disclaimer in the
1476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman *    documentation and/or other materials provided with the distribution.
1576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 3. The name of the author may not be used to endorse or promote products
1676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman *    derived from this software without specific prior written permission.
1776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman *
1876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman *
2976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman *	$Id$
3076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */
3176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
3276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "defs.h"
3376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
3476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <signal.h>
3576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <errno.h>
3676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <sys/param.h>
3776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <fcntl.h>
3876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <sys/resource.h>
3976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <sys/wait.h>
4076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <sys/stat.h>
4176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <pwd.h>
4276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <grp.h>
4376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <string.h>
4476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
4576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4
4676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <sys/stropts.h>
4776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <poll.h>
489ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#ifdef SVR4_MP
499ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#include <sys/uio.h>
509ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#endif
5176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
5276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
5376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint debug = 0, followfork = 0, followvfork = 0, interactive = 0;
5476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint rflag = 0, tflag = 0, dtime = 0, cflag = 0;
5576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint iflag = 0, xflag = 0, qflag = 0;
5676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pflag_seen = 0;
5776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
5876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *username = NULL;
5976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanuid_t run_uid;
6076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermangid_t run_gid;
6176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
6276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint acolumn = DEFAULT_ACOLUMN;
6376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint max_strlen = DEFAULT_STRLEN;
6476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *outfname = NULL;
6576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert AkkermanFILE *outf;
6676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb tcbtab[MAX_PROCS];
6776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint nprocs;
6876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *progname;
6976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanextern char version[];
7076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanextern char **environ;
7176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
7276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic struct tcb *pid2tcb P((int pid));
7376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int trace P((void));
7476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void cleanup P((void));
7576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void interrupt P((int sig));
7676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic sigset_t empty_set, blocked_set;
7776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
7876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE_SIG_ATOMIC_T
7976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic volatile sig_atomic_t interrupted;
8076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !HAVE_SIG_ATOMIC_T */
8176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef __STDC__
8276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic volatile int interrupted;
8376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !__STDC__ */
8476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int interrupted;
8576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !__STDC__ */
8676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_SIG_ATOMIC_T */
8776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
8876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4
8976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
9076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic struct tcb *pfd2tcb P((int pfd));
9176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void reaper P((int sig));
9276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void rebuild_pollv P((void));
93e68d61c7d6dcf986d32c462e533ea46f85183db8Wichert Akkermanstruct pollfd pollv[MAX_PROCS];
9476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
9576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
9676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
9776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void proc_poll_open P((void));
9876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void proc_poller P((int pfd));
9976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
10076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct proc_pollfd {
10176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int fd;
10276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int revents;
10376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int pid;
10476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman};
10576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
10676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int poller_pid;
10776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int proc_poll_pipe[2] = { -1, -1 };
10876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
10976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
11076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
1119ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#ifdef SVR4_MP
1129ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#define POLLWANT	POLLWRNORM
1139ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#else
1149ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#define POLLWANT	POLLPRI
1159ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#endif
11676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SVR4 */
11776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
11876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
11976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanusage(ofp, exitval)
12076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert AkkermanFILE *ofp;
12176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint exitval;
12276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
12376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	fprintf(ofp, "\
12476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanusage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\
12576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman              [-p pid] ... [-s strsize] [-u username] [command [arg ...]]\n\
12676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman   or: strace -c [-e expr] ... [-O overhead] [-S sortby] [command [arg ...]]\n\
12776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-c -- count time, calls, and errors for each syscall and report summary\n\
12876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-f -- follow forks, -ff -- with output into separate files\n\
12976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-F -- attempt to follow vforks, -h -- print help message\n\
13076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-i -- print instruction pointer at time of syscall\n\
13176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-q -- suppress messages about attaching, detaching, etc.\n\
13276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\
13376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-T -- print time spent in each syscall, -V -- print version\n\
13476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\
13576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
13676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-a column -- alignment COLUMN for printing syscall results (default %d)\n\
13776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\
13876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman   options: trace, abbrev, verbose, raw, signal, read, or write\n\
13976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-o file -- send trace output to FILE instead of stderr\n\
14076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\
14176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-p pid -- trace process with process id PID, may be repeated\n\
14276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\
14376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\
14476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-u username -- run command as username handling setuid and/or setgid\n\
14576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman", DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
14676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	exit(exitval);
14776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
14876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
14976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4
15076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef MIPS
15176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
15276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanfoobar()
15376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
15476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
15576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* MIPS */
15676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SVR4 */
15776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
15876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint
15976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanmain(argc, argv)
16076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint argc;
16176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *argv[];
16276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
16376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	extern int optind;
16476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	extern char *optarg;
16576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
16676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int c, pid = 0;
16776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct sigaction sa;
16876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
16976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	static char buf[BUFSIZ];
17076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
17176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	progname = argv[0];
17276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	outf = stderr;
17376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	interactive = 1;
17476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	qualify("trace=all");
17576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	qualify("abbrev=all");
17676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	qualify("verbose=all");
17776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	qualify("signal=all");
17876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	set_sortby(DEFAULT_SORTBY);
17976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	set_personality(DEFAULT_PERSONALITY);
18076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	while ((c = getopt(argc, argv,
18176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		"+cdfFhiqrtTvVxa:e:o:O:p:s:S:u:")) != EOF) {
18276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		switch (c) {
18376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'c':
18476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			cflag++;
18576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			dtime++;
18676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
18776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'd':
18876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			debug++;
18976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
19076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'f':
19176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			followfork++;
19276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
19376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'F':
19476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			followvfork++;
19576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
19676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'h':
19776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			usage(stdout, 0);
19876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
19976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'i':
20076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			iflag++;
20176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
20276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'q':
20376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			qflag++;
20476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
20576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'r':
20676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			rflag++;
20776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tflag++;
20876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
20976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 't':
21076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tflag++;
21176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
21276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'T':
21376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			dtime++;
21476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
21576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'x':
21676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			xflag++;
21776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
21876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'v':
21976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			qualify("abbrev=none");
22076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
22176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'V':
22276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			printf("%s\n", version);
22376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(0);
22476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
22576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'a':
22676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			acolumn = atoi(optarg);
22776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
22876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'e':
22976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			qualify(optarg);
23076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
23176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'o':
23276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			outfname = strdup(optarg);
23376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
23476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'O':
23576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			set_overhead(atoi(optarg));
23676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
23776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'p':
23876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if ((pid = atoi(optarg)) == 0) {
23976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "%s: Invalid process id: %s\n",
24076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					progname, optarg);
24176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
24276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
24376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (pid == getpid()) {
24454a4767f8609abfe2d7cb1802bc9e8dca97dd08fWichert Akkerman				fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname);
24576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
24676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
24776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if ((tcp = alloctcb(pid)) == NULL) {
24876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "%s: tcb table full, please recompile strace\n",
24976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					progname);
25076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
25176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
25276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->flags |= TCB_ATTACHED;
25376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			pflag_seen++;
25476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
25576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 's':
25676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			max_strlen = atoi(optarg);
25776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
25876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'S':
25976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			set_sortby(optarg);
26076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
26176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 'u':
26276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			username = strdup(optarg);
26376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
26476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		default:
26576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			usage(stderr, 1);
26676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
26776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
26876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
26976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
27076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* See if they want to run as another user. */
27176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (username != NULL) {
27276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		struct passwd *pent;
27376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
27476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (getuid() != 0 || geteuid() != 0) {
27576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr,
27676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				"%s: you must be root to use the -u option\n",
27776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				progname);
27876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
27976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
28076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((pent = getpwnam(username)) == NULL) {
28176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "%s: cannot find user `%s'\n",
28276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				progname, optarg);
28376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
28476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
28576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		run_uid = pent->pw_uid;
28676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		run_gid = pent->pw_gid;
28776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
28876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else {
28976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		run_uid = getuid();
29076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		run_gid = getgid();
29176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
29276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
29376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4
29476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	setreuid(geteuid(), getuid());
29576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
29676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
29776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* See if they want to pipe the output. */
29876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (outfname && (outfname[0] == '|' || outfname[0] == '!')) {
29976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((outf = popen(outfname + 1, "w")) == NULL) {
30076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "%s: can't popen '%s': %s\n",
30176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				progname, outfname + 1, strerror(errno));
30276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
30376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
30476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		free(outfname);
30576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		outfname = NULL;
30676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
30776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
30876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Check if they want to redirect the output. */
30976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (outfname) {
31076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((outf = fopen(outfname, "w")) == NULL) {
31176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "%s: can't fopen '%s': %s\n",
31276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				progname, outfname, strerror(errno));
31376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
31476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
31576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
31676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
31776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4
31876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	setreuid(geteuid(), getuid());
31976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
32076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
32176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!outfname) {
32276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		qflag = 1;
32376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		setvbuf(outf, buf, _IOLBF, BUFSIZ);
32476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
32576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (optind < argc)
32676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		interactive = 0;
32776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else
32876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		qflag = 1;
32976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
33076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (c = 0, tcp = tcbtab; c < MAX_PROCS; c++, tcp++) {
33176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Reinitialize the output since it may have changed. */
33276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->outf = outf;
33376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED))
33476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
33576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4
33676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (proc_open(tcp, 1) < 0) {
33776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "trouble opening proc file\n");
33876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
33976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
34076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
34176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !SVR4 */
34276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) {
34376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("attach: ptrace(PTRACE_ATTACH, ...)");
34476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
34576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
34676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
34776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !SVR4 */
34876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!qflag)
34976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr,
35076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				"Process %u attached - interrupt to quit\n",
35176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				pid);
35276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
35376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
35476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (optind < argc) {
35576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		struct stat statbuf;
35676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		char *filename;
35776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		char pathname[MAXPATHLEN];
35876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
35976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		filename = argv[optind];
36076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (strchr(filename, '/'))
36176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			strcpy(pathname, filename);
36276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef USE_DEBUGGING_EXEC
36376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/*
36476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * Debuggers customarily check the current directory
36576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * first regardless of the path but doing that gives
36676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * security geeks a panic attack.
36776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 */
36876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else if (stat(filename, &statbuf) == 0)
36976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			strcpy(pathname, filename);
37076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* USE_DEBUGGING_EXEC */
37176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else {
37276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			char *path;
37376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			int m, n, len;
37476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
37576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			for (path = getenv("PATH"); path && *path; path += m) {
37676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (strchr(path, ':')) {
37776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					n = strchr(path, ':') - path;
37876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					m = n + 1;
37976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
38076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				else
38176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					m = n = strlen(path);
38276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (n == 0) {
38376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					getcwd(pathname, MAXPATHLEN);
38476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					len = strlen(pathname);
38576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
38676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				else {
38776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					strncpy(pathname, path, n);
38876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					len = n;
38976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
39076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (len && pathname[len - 1] != '/')
39176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					pathname[len++] = '/';
39276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				strcpy(pathname + len, filename);
39376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (stat(pathname, &statbuf) == 0)
39476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					break;
39576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
39676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
39776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (stat(pathname, &statbuf) < 0) {
39876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "%s: %s: command not found\n",
39976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				progname, filename);
40076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
40176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
40276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		switch (pid = fork()) {
40376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case -1:
40476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("strace: fork");
40576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			cleanup();
40676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
40776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
40876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 0: {
40976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4
410789ed35db520a552ba5ea0a5cbe5b31efa7adde5Wichert Akkerman			if (outf != stderr) close (fileno (outf));
41176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef MIPS
41276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/* Kludge for SGI, see proc_open for details. */
41376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sa.sa_handler = foobar;
41476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sa.sa_flags = 0;
41576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigemptyset(&sa.sa_mask);
41676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigaction(SIGINT, &sa, NULL);
41776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* MIPS */
41876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			pause();
41976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !SVR4 */
42076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) {
42176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("strace: ptrace(PTRACE_TRACEME, ...)");
42276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
42376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
42476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (debug)
42576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				kill(getpid(), SIGSTOP);
42676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
42776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (username != NULL || geteuid() == 0) {
42876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				uid_t run_euid = run_uid;
42976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				gid_t run_egid = run_gid;
43076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
43176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (statbuf.st_mode & S_ISUID)
43276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					run_euid = statbuf.st_uid;
43376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (statbuf.st_mode & S_ISGID)
43476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					run_egid = statbuf.st_gid;
43576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
43676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/*
43776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * It is important to set groups before we
43876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * lose privileges on setuid.
43976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 */
44076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (username != NULL
44176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				    && initgroups(username, run_gid) < 0) {
44276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					perror("initgroups");
44376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					exit(1);
44476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
44576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (setregid(run_gid, run_egid) < 0) {
44676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					perror("setregid");
44776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					exit(1);
44876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
44976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (setreuid(run_uid, run_euid) < 0) {
45076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					perror("setreuid");
45176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					exit(1);
45276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
45376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
45476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			else
45576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				setreuid(run_uid, run_uid);
45676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !SVR4 */
45776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
45876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			execv(pathname, &argv[optind]);
45976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("strace: exec");
46076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			_exit(1);
46176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
46276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
46376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		default:
46476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if ((tcp = alloctcb(pid)) == NULL) {
46576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "tcb table full\n");
46676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				cleanup();
46776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
46876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
46976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4
47076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (proc_open(tcp, 0) < 0) {
47176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "trouble opening proc file\n");
47276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				cleanup();
47376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
47476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
47576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SVR4 */
47676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4
47776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fake_execve(tcp, pathname, &argv[optind], environ);
47876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
47976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
48076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
48176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
48276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (pflag_seen == 0)
48376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		usage(stderr, 1);
48476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
48576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&empty_set);
48676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&blocked_set);
48776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_handler = SIG_IGN;
48876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&sa.sa_mask);
48976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_flags = 0;
49076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGTTOU, &sa, NULL);
49176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGTTIN, &sa, NULL);
49276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (interactive) {
49376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigaddset(&blocked_set, SIGHUP);
49476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigaddset(&blocked_set, SIGINT);
49576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigaddset(&blocked_set, SIGQUIT);
49676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigaddset(&blocked_set, SIGPIPE);
49776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigaddset(&blocked_set, SIGTERM);
49876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sa.sa_handler = interrupt;
49976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SUNOS4
50076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* POSIX signals on sunos4.1 are a little broken. */
50176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sa.sa_flags = SA_INTERRUPT;
50276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SUNOS4 */
50376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
50476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGHUP, &sa, NULL);
50576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGINT, &sa, NULL);
50676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGQUIT, &sa, NULL);
50776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGPIPE, &sa, NULL);
50876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGTERM, &sa, NULL);
50976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4
51076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_handler = reaper;
51176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGCHLD, &sa, NULL);
51276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SVR4 */
51376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
51476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (trace() < 0)
51576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		exit(1);
51676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	cleanup();
51776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	exit(0);
51876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
51976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
52076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
52176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermannewoutf(tcp)
52276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
52376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
52476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	char name[MAXPATHLEN];
52576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	FILE *fp;
52676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
52776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (outfname && followfork > 1) {
52876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sprintf(name, "%s.%u", outfname, tcp->pid);
52976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4
53076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		setreuid(geteuid(), getuid());
53176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
53276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fp = fopen(name, "w");
53376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4
53476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		setreuid(geteuid(), getuid());
53576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
53676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (fp == NULL) {
53776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("fopen");
53876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return;
53976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
54076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->outf = fp;
54176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
54276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return;
54376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
54476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
54576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *
54676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanalloctcb(pid)
54776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pid;
54876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
54976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
55076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
55176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
55276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
55376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((tcp->flags & TCB_INUSE) == 0) {
55476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->pid = pid;
55576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->parent = NULL;
55676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->nchildren = 0;
55776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->flags = TCB_INUSE | TCB_STARTUP;
55876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->outf = outf; /* Initialise to current out file */
55976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->stime.tv_sec = 0;
56076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->stime.tv_usec = 0;
56176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->pfd = -1;
56276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			nprocs++;
56376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return tcp;
56476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
56576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
56676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return NULL;
56776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
56876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
56976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4
57076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint
57176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanproc_open(tcp, attaching)
57276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
57376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint attaching;
57476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
57576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	char proc[32];
57676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	long arg;
57776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sysset_t sc_enter, sc_exit;
57876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigset_t signals;
57976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	fltset_t faults;
58076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
58176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	static int last_pfd;
58276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
58376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
5849ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#ifdef SVR4_MP
5859ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	/* Open the process pseudo-files in /proc. */
5869ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	sprintf(proc, "/proc/%d/ctl", tcp->pid);
5879ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
5889ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("strace: open(\"/proc/...\", ...)");
5899ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
5909ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
5919ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
5929ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_GETFD");
5939ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
5949ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
5959ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
5969ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_SETFD");
5979ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
5989ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
5999ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	sprintf(proc, "/proc/%d/status", tcp->pid);
6009ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
6019ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("strace: open(\"/proc/...\", ...)");
6029ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6039ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6049ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) {
6059ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_GETFD");
6069ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6079ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6089ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) {
6099ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_SETFD");
6109ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6119ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6129ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	sprintf(proc, "/proc/%d/as", tcp->pid);
6139ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
6149ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("strace: open(\"/proc/...\", ...)");
6159ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6169ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6179ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) {
6189ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_GETFD");
6199ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6209ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6219ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) {
6229ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_SETFD");
6239ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6249ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6259ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#else
62676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Open the process pseudo-file in /proc. */
62776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sprintf(proc, "/proc/%d", tcp->pid);
62876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) {
62976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("strace: open(\"/proc/...\", ...)");
63076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
63176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
6329ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) {
6339ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_GETFD");
6349ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6359ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6369ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) {
6379ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		perror("F_SETFD");
6389ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		return -1;
6399ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
6409ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#endif
64176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	rebuild_pollv();
64276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!attaching) {
64376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/*
64476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * Wait for the child to pause.  Because of a race
64576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * condition we have to poll for the event.
64676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 */
64776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		for (;;) {
6489ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (IOCTL_STATUS (tcp) < 0) {
64976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("strace: PIOCSTATUS");
65076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
65176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
6529ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (tcp->status.PR_FLAGS & PR_ASLEEP)
65376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
65476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
65576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
65676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Stop the process so that we own the stop. */
6579ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSTOP, NULL) < 0) {
65876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("strace: PIOCSTOP");
65976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
66076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
66176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef PIOCSET
66276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Set Run-on-Last-Close. */
66376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	arg = PR_RLC;
6649ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) {
66576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOCSET PR_RLC");
66676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
66776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
66876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Set or Reset Inherit-on-Fork. */
66976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	arg = PR_FORK;
6709ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) {
67176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOC{SET,RESET} PR_FORK");
67276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
67376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
67476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else  /* !PIOCSET */
67576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (ioctl(tcp->pfd, PIOCSRLC) < 0) {
67676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOCSRLC");
67776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
67876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
67976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) {
68076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOC{S,R}FORK");
68176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
68276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
68376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !PIOCSET */
68476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Enable all syscall entries. */
68576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	prfillset(&sc_enter);
6869ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSENTRY, &sc_enter) < 0) {
68776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOCSENTRY");
68876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
68976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
69076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Enable all syscall exits. */
69176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	prfillset(&sc_exit);
6929ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSEXIT, &sc_exit) < 0) {
69376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOSEXIT");
69476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
69576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
69676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Enable all signals. */
69776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	prfillset(&signals);
6989ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) {
69976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOCSTRACE");
70076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
70176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
70276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* Enable all faults. */
70376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	prfillset(&faults);
7049ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) {
70576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("PIOCSFAULT");
70676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
70776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
70876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!attaching) {
70976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef MIPS
71076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/*
71176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * The SGI PRSABORT doesn't work for pause() so
71276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * we send it a caught signal to wake it up.
71376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 */
71476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		kill(tcp->pid, SIGINT);
71576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !MIPS */
71676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* The child is in a pause(), abort it. */
7179ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		arg = PRSABORT;
7189ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
71976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("PIOCRUN");
72076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return -1;
72176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
72276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !MIPS */
72376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		for (;;) {
72476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/* Wait for the child to do something. */
7259ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (IOCTL_WSTOP (tcp) < 0) {
72676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("PIOCWSTOP");
72776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
72876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
7299ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (tcp->status.PR_WHY == PR_SYSENTRY) {
73076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE_PR_SYSCALL
73176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				int scno = tcp->status.pr_syscall;
73276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !HAVE_PR_SYSCALL */
7339ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman				int scno = tcp->status.PR_WHAT;
73476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_PR_SYSCALL */
73576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (scno == SYS_execve)
73676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					break;
73776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
73876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/* Set it running: maybe execve will be next. */
7399ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			arg = 0;
7409ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) {
74176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("PIOCRUN");
74276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
74376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
74476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
74576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
74676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
74776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (proc_poll_pipe[0] != -1)
74876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		proc_poller(tcp->pfd);
74976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (nprocs > 1) {
75076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		proc_poll_open();
75176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		proc_poller(last_pfd);
75276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		proc_poller(tcp->pfd);
75376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
75476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	last_pfd = tcp->pfd;
75576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
75676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return 0;
75776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
75876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
75976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SVR4 */
76076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
76176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic struct tcb *
76276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanpid2tcb(pid)
76376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pid;
76476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
76576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
76676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
76776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
76876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
76976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (pid && tcp->pid != pid)
77076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
77176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_INUSE)
77276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return tcp;
77376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
77476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return NULL;
77576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
77676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
77776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4
77876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
77976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic struct tcb *
78076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanpfd2tcb(pfd)
78176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pfd;
78276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
78376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
78476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
78576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
78676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
78776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->pfd != pfd)
78876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
78976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_INUSE)
79076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return tcp;
79176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
79276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return NULL;
79376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
79476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
79576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SVR4 */
79676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
79776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
79876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermandroptcb(tcp)
79976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
80076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
80176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp->pid == 0)
80276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return;
80376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	nprocs--;
80476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tcp->pid = 0;
80576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tcp->flags = 0;
80676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp->pfd != -1) {
80776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		close(tcp->pfd);
80876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->pfd = -1;
80976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4
81076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		rebuild_pollv();
81176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
81276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
81376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp->parent != NULL) {
81476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->parent->nchildren--;
81576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->parent = NULL;
81676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
81776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#if 0
81876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp->outf != stderr)
81976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fclose(tcp->outf);
82076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
82176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tcp->outf = 0;
82276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
82376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
82476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4
82576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
82676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
82776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanresume(tcp)
82876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
82976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
83076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp == NULL)
83176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
83276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
83376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!(tcp->flags & TCB_SUSPENDED)) {
83476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid);
83576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
83676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
83776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tcp->flags &= ~TCB_SUSPENDED;
83876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
83976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) {
84076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("resume: ptrace(PTRACE_SYSCALL, ...)");
84176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return -1;
84276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
84376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
84476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!qflag)
84576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fprintf(stderr, "Process %u resumed\n", tcp->pid);
84676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return 0;
84776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
84876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
84976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !SVR4 */
85076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
85176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman/* detach traced process; continue with sig */
85276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
85376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
85476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermandetach(tcp, sig)
85576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
85676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig;
85776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
85876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int error = 0;
85976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX
86076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int status;
86176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
86276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
86376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp->flags & TCB_BPTSET)
86476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sig = SIGKILL;
86576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
86676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX
86776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/*
86876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	 * Linux wrongly insists the child be stopped
86976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	 * before detaching.  Arghh.  We go through hoops
87076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	 * to make a clean break of things.
87176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	 */
872dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman#if defined(SPARC)
873dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman#undef PTRACE_DETACH
874dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman#define PTRACE_DETACH PTRACE_SUNDETACH
875dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman#endif
87676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
87776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* On a clear day, you can see forever. */
87876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
87976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (errno != ESRCH) {
88076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Shouldn't happen. */
88176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("detach: ptrace(PTRACE_DETACH, ...)");
88276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
88376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (kill(tcp->pid, 0) < 0) {
88476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (errno != ESRCH)
88576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("detach: checking sanity");
88676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
88776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (kill(tcp->pid, SIGSTOP) < 0) {
88876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (errno != ESRCH)
88976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("detach: stopping child");
89076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
89176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else {
89276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		for (;;) {
89376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (waitpid(tcp->pid, &status, 0) < 0) {
89476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (errno != ECHILD)
89576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					perror("detach: waiting");
89676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
89776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
89876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!WIFSTOPPED(status)) {
89976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/* Au revoir, mon ami. */
90076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
90176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
90276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (WSTOPSIG(status) == SIGSTOP) {
90376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if ((error = ptrace(PTRACE_DETACH,
90476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				    tcp->pid, (char *) 1, sig)) < 0) {
90576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					if (errno != ESRCH)
90676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman						perror("detach: ptrace(PTRACE_DETACH, ...)");
90776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					/* I died trying. */
90876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
90976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
91076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
91176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1,
91276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			    WSTOPSIG(status) == SIGTRAP ?
91376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			    0 : WSTOPSIG(status))) < 0) {
91476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (errno != ESRCH)
91576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					perror("detach: ptrace(PTRACE_CONT, ...)");
91676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
91776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
91876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
91976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
92076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* LINUX */
92176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
92276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#if defined(SUNOS4)
92376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	/* PTRACE_DETACH won't respect `sig' argument, so we post it here. */
92476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (sig && kill(tcp->pid, sig) < 0)
92576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("detach: kill");
92676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sig = 0;
92776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0)
92876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("detach: ptrace(PTRACE_DETACH, ...)");
92976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SUNOS4 */
93076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
93176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4
93276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (waiting_parent(tcp))
93376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		error = resume(tcp->parent);
93476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !SVR4 */
93576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
93676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (!qflag)
93776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fprintf(stderr, "Process %u detached\n", tcp->pid);
93876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
93976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	droptcb(tcp);
94076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return error;
94176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
94276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
94376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4
94476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
94576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
94676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanreaper(sig)
94776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig;
94876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
94976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int pid;
95076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int status;
95176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
95276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
95376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#if 0
95476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		struct tcb *tcp;
95576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
95676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp = pid2tcb(pid);
95776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp)
95876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
95976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
96076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
96176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
96276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
96376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SVR4 */
96476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
96576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
96676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermancleanup()
96776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
96876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
96976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
97076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
97176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
97276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!(tcp->flags & TCB_INUSE))
97376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
97476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (debug)
97576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr,
97676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				"cleanup: looking at pid %u\n", tcp->pid);
97776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp_last &&
97876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		    (!outfname || followfork < 2 || tcp_last == tcp)) {
97976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tprintf(" <unfinished ...>\n");
98076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp_last = NULL;
98176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
98276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_ATTACHED)
98376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			detach(tcp, 0);
98476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else {
98576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			kill(tcp->pid, SIGCONT);
98676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			kill(tcp->pid, SIGTERM);
98776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
98876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
98976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (cflag)
99076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		call_summary(outf);
99176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
99276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
99376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
99476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermaninterrupt(sig)
99576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig;
99676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
99776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	interrupted = 1;
99876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
99976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
100076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_STRERROR
100176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
100276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SYS_ERRLIST_DECLARED
100376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanextern int sys_nerr;
100476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanextern char *sys_errlist[];
100576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SYS_ERRLIST_DECLARED */
100676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
100776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanconst char *
100876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstrerror(errno)
100976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint errno;
101076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
101176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	static char buf[64];
101276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
101376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (errno < 1 || errno >= sys_nerr) {
101476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sprintf(buf, "Unknown error %d", errno);
101576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return buf;
101676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
101776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return sys_errlist[errno];
101876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
101976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
102076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* HAVE_STERRROR */
102176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
102276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_STRSIGNAL
102376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
102476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SYS_SIGLIST_DECLARED
102576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE__SYS_SIGLIST
102676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	extern char *_sys_siglist[];
102776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else
102876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	extern char *sys_siglist[];
102976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
103076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SYS_SIGLIST_DECLARED */
103176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
103276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanconst char *
103376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstrsignal(sig)
103476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig;
103576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
103676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	static char buf[64];
103776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
103876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (sig < 1 || sig >= NSIG) {
103976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sprintf(buf, "Unknown signal %d", sig);
104076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return buf;
104176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
104276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE__SYS_SIGLIST
104376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return _sys_siglist[sig];
104476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else
104576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return sys_siglist[sig];
104676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
104776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
104876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
104976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* HAVE_STRSIGNAL */
105076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
105176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4
105276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
105376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
105476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanrebuild_pollv()
105576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
105676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i, j;
105776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
105876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
105976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = j = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) {
106076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!(tcp->flags & TCB_INUSE))
106176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
106276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		pollv[j].fd = tcp->pfd;
10639ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		pollv[j].events = POLLWANT;
106476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		j++;
106576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
106676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (j != nprocs) {
106776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fprintf(stderr, "strace: proc miscount\n");
106876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		exit(1);
106976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
107076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
107176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
107276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
107376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
107476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
107576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanproc_poll_open()
107676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
107776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int arg;
107876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
107976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
108076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (pipe(proc_poll_pipe) < 0) {
108176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("pipe");
108276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		exit(1);
108376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
108476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0; i < 2; i++) {
108576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) {
108676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("F_GETFD");
108776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
108876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
108976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) {
109076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("F_SETFD");
109176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
109276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
109376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
109476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
109576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
109676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
109776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanproc_poll(pollv, nfds, timeout)
109876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct pollfd *pollv;
109976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint nfds;
110076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint timeout;
110176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
110276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
110376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int n;
110476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct proc_pollfd pollinfo;
110576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
110676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
110776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return n;
110876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (n != sizeof(struct proc_pollfd)) {
110976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		fprintf(stderr, "panic: short read: %d\n", n);
111076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		exit(1);
111176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
111276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0; i < nprocs; i++) {
111376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (pollv[i].fd == pollinfo.fd)
111476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			pollv[i].revents = pollinfo.revents;
111576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else
111676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			pollv[i].revents = 0;
111776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
111876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	poller_pid = pollinfo.pid;
111976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return 1;
112076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
112176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
112276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
112376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanwakeup_handler(sig)
112476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig;
112576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
112676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
112776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
112876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void
112976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanproc_poller(pfd)
113076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pfd;
113176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
113276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct proc_pollfd pollinfo;
113376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct sigaction sa;
113476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigset_t blocked_set, empty_set;
113576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i;
113676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int n;
113776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct rlimit rl;
113876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
113976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	switch (fork()) {
114076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	case -1:
114176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("fork");
114276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		_exit(0);
114376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	case 0:
114476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		break;
114576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	default:
114676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		return;
114776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
114876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
114976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_handler = interactive ? SIG_DFL : SIG_IGN;
115076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_flags = 0;
115176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&sa.sa_mask);
115276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGHUP, &sa, NULL);
115376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGINT, &sa, NULL);
115476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGQUIT, &sa, NULL);
115576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGPIPE, &sa, NULL);
115676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGTERM, &sa, NULL);
115776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sa.sa_handler = wakeup_handler;
115876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaction(SIGUSR1, &sa, NULL);
115976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&blocked_set);
116076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigaddset(&blocked_set, SIGUSR1);
116176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigprocmask(SIG_BLOCK, &blocked_set, NULL);
116276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	sigemptyset(&empty_set);
116376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
116476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
116576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		perror("getrlimit(RLIMIT_NOFILE, ...)");
116676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		_exit(0);
116776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
116876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	n = rl.rlim_cur;
116976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0; i < n; i++) {
117076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (i != pfd && i != proc_poll_pipe[1])
117176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			close(i);
117276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
117376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
117476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	pollinfo.fd = pfd;
117576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	pollinfo.pid = getpid();
117676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (;;) {
11779ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		if (ioctl(pfd, PIOCWSTOP, NULL) < 0)
11789ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		{
117976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			switch (errno) {
118076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case EINTR:
118176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
118276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case EBADF:
118376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				pollinfo.revents = POLLERR;
118476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
118576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case ENOENT:
118676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				pollinfo.revents = POLLHUP;
118776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
118876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			default:
118976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("proc_poller: PIOCWSTOP");
119076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
119176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
119276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			_exit(0);
119376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
11949ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		pollinfo.revents = POLLWANT;
119576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo));
119676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		sigsuspend(&empty_set);
119776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
119876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
119976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
120076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
120176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
120276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
120376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchoose_pfd()
120476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
120576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int i, j;
120676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
120776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
120876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	static int last;
120976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
121076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (followfork < 2 &&
12119ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	    last < nprocs && (pollv[last].revents & POLLWANT)) {
121276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/*
121376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * The previous process is ready to run again.  We'll
121476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * let it do so if it is currently in a syscall.  This
121576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 * heuristic improves the readability of the trace.
121676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		 */
121776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp = pfd2tcb(pollv[last].fd);
121876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp && (tcp->flags & TCB_INSYSCALL))
121976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return pollv[last].fd;
122076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
122176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
122276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (i = 0; i < nprocs; i++) {
122376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Let competing children run round robin. */
122476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		j = (i + last + 1) % nprocs;
122576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (pollv[j].revents & (POLLHUP | POLLERR)) {
122676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp = pfd2tcb(pollv[j].fd);
122776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!tcp) {
122876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "strace: lost proc\n");
122976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
123076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
123176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
123276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return -1;
123376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
12349ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		if (pollv[j].revents & POLLWANT) {
123576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			last = j;
123676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return pollv[j].fd;
123776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
123876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
123976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	fprintf(stderr, "strace: nothing ready\n");
124076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	exit(1);
124176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
124276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
124376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
124476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantrace()
124576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
124676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
124776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int pfd;
124876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int what;
124976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int ioctl_result = 0, ioctl_errno = 0;
12509ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	long arg;
125176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
125276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	for (;;) {
125376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interactive)
125476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigprocmask(SIG_SETMASK, &empty_set, NULL);
125576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
125676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (nprocs == 0)
125776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
125876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
125976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		switch (nprocs) {
126076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case 1:
126176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
126276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (proc_poll_pipe[0] == -1) {
126376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
126476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tcp = pid2tcb(0);
126576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (!tcp)
126676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					continue;
126776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				pfd = tcp->pfd;
126876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (pfd == -1)
126976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					continue;
127076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				break;
127176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
127276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
127376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/* fall through ... */
127476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
127576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		default:
127676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE_POLLABLE_PROCFS
127776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (poll(pollv, nprocs, INFTIM) < 0) {
127876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (interrupted)
127976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					return 0;
128076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
128176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
128276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !HAVE_POLLABLE_PROCFS */
128376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (proc_poll(pollv, nprocs, INFTIM) < 0) {
128476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (interrupted)
128576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					return 0;
128676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
128776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
128876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
128976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			pfd = choose_pfd();
129076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (pfd == -1)
129176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
129276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
129376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
129476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
129576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Look up `pfd' in our table. */
129676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((tcp = pfd2tcb(pfd)) == NULL) {
129776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "unknown pfd: %u\n", pfd);
129876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
129976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
130076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Get the status of the process. */
130176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!interrupted) {
13029ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			ioctl_result = IOCTL_WSTOP (tcp);
130376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			ioctl_errno = errno;
130476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS
130576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (proc_poll_pipe[0] != -1) {
130676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (ioctl_result < 0)
130776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					kill(poller_pid, SIGKILL);
130876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				else
130976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					kill(poller_pid, SIGUSR1);
131076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
131176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */
131276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
131376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interrupted)
131476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return 0;
131576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
131676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interactive)
131776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigprocmask(SIG_BLOCK, &blocked_set, NULL);
131876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
131976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (ioctl_result < 0) {
132076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/* Find out what happened if it failed. */
132176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			switch (ioctl_errno) {
132276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case EINTR:
132376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case EBADF:
132476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
132576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case ENOENT:
132676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				droptcb(tcp);
132776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
132876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			default:
132976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("PIOCWSTOP");
133076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
133176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
133276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
133376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
133476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* clear the just started flag */
133576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp->flags &= ~TCB_STARTUP;
133676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
133776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* set current output file */
133876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		outf = tcp->outf;
133976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
134076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (cflag) {
134176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			struct timeval stime;
134276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
134376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			stime.tv_sec = tcp->status.pr_stime.tv_sec;
134476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000;
134576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tv_sub(&tcp->dtime, &stime, &tcp->stime);
134676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->stime = stime;
134776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
134876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
13499ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		what = tcp->status.PR_WHAT;
13509ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		switch (tcp->status.PR_WHY) {
135176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case PR_REQUESTED:
13529ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			if (tcp->status.PR_FLAGS & PR_ASLEEP) {
13539ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman				tcp->status.PR_WHY = PR_SYSENTRY;
135476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (trace_syscall(tcp) < 0) {
135576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					fprintf(stderr, "syscall trouble\n");
135676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					exit(1);
135776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
135876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
135976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
136076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case PR_SYSENTRY:
136176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case PR_SYSEXIT:
136276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (trace_syscall(tcp) < 0) {
136376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "syscall trouble\n");
136476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
136576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
136676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
136776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case PR_SIGNALLED:
136876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) {
136976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printleader(tcp);
137076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("--- %s (%s) ---",
1371ce780fc9e6067b15b65ca2904c698c77503bf635Nate Sammons					signame(what), strsignal(what));
137276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printtrailer(tcp);
137376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
137476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
137576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		case PR_FAULTED:
137676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!cflag && (qual_flags[what] & QUAL_FAULT)) {
137776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printleader(tcp);
137876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("=== FAULT %d ===", what);
137976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printtrailer(tcp);
138076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
138176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
138276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		default:
13839ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman			fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY);
138476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
138576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			break;
138676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
13879ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		arg = 0;
13889ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) {
138976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("PIOCRUN");
139076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
139176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
139276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
139376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return 0;
139476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
139576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
139676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !SVR4 */
139776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
139876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int
139976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantrace()
140076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
140176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int pid;
140276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int wait_errno;
140376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	int status;
140476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct tcb *tcp;
140576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX
140676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	struct rusage ru;
140776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* LINUX */
140876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
140976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	while (nprocs != 0) {
141076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interactive)
141176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigprocmask(SIG_SETMASK, &empty_set, NULL);
141276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX
141376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		pid = wait4(-1, &status, 0, cflag ? &ru : NULL);
141476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* LINUX */
141576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SUNOS4
141676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		pid = wait(&status);
141776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SUNOS4 */
141876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		wait_errno = errno;
141976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interactive)
142076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			sigprocmask(SIG_BLOCK, &blocked_set, NULL);
142176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
142276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (interrupted)
142376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return 0;
142476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
142576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (pid == -1) {
142676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			switch (wait_errno) {
142776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case EINTR:
142876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
142976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			case ECHILD:
143076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/*
143176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * We would like to verify this case
143276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * but sometimes a race in Solbourne's
143376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * version of SunOS sometimes reports
143476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * ECHILD before sending us SIGCHILD.
143576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 */
143676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#if 0
143776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (nprocs == 0)
143876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					return 0;
143976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "strace: proc miscount\n");
144076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				exit(1);
144176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
144276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return 0;
144376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			default:
144476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				errno = wait_errno;
144576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("strace: wait");
144676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
144776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
144876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
144976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (debug)
145076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, " [wait(%#x) = %u]\n", status, pid);
145176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
145276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* Look up `pid' in our table. */
145376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if ((tcp = pid2tcb(pid)) == NULL) {
145476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "unknown pid: %u\n", pid);
145576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (WIFSTOPPED(status))
145676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				ptrace(PTRACE_CONT, pid, (char *) 1, 0);
145776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			exit(1);
145876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
145976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		/* set current output file */
146076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		outf = tcp->outf;
146176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (cflag) {
146276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX
146376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime);
146476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->stime = ru.ru_stime;
146576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !LINUX */
146676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
146776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
146876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_SUSPENDED) {
146976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/*
147076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * Apparently, doing any ptrace() call on a stopped
147176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * process, provokes the kernel to report the process
147276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * status again on a subsequent wait(), even if the
147376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * process has not been actually restarted.
147476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * Since we have inspected the arguments of suspended
147576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * processes we end up here testing for this case.
147676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 */
147776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
147876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
147976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (WIFSIGNALED(status)) {
148076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!cflag
148176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			    && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) {
148276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printleader(tcp);
148376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("+++ killed by %s +++",
1484ce780fc9e6067b15b65ca2904c698c77503bf635Nate Sammons					signame(WTERMSIG(status)));
148576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printtrailer(tcp);
148676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
148776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
148876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
148976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
149076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (WIFEXITED(status)) {
149176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (debug)
149276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "pid %u exited\n", pid);
149376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tcp->flags & TCB_ATTACHED)
149476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr,
149576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					"PANIC: attached pid %u exited\n",
149676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					pid);
149776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
149876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
149976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
150076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (!WIFSTOPPED(status)) {
150176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "PANIC: pid %u not stopped\n", pid);
150276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			droptcb(tcp);
150376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
150476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
150576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (debug)
150676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			fprintf(stderr, "pid %u stopped, [%s]\n",
1507ce780fc9e6067b15b65ca2904c698c77503bf635Nate Sammons				pid, signame(WSTOPSIG(status)));
150876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
150976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_STARTUP) {
151076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			/*
151176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * This flag is there to keep us in sync.
151276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * Next time this process stops it should
151376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 * really be entering a system call.
151476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			 */
151576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->flags &= ~TCB_STARTUP;
151676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tcp->flags & TCB_ATTACHED) {
151776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/*
151876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * Interestingly, the process may stop
151976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * with STOPSIG equal to some other signal
152076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * than SIGSTOP if we happend to attach
152176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * just before the process takes a signal.
152276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 */
152376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (!WIFSTOPPED(status)) {
152476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					fprintf(stderr,
152576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman						"pid %u not stopped\n", pid);
152676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					detach(tcp, WSTOPSIG(status));
152776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					continue;
152876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
152976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
153076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			else {
153176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SUNOS4
153276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/* A child of us stopped at exec */
153376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (WSTOPSIG(status) == SIGTRAP && followvfork)
153476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					fixvfork(tcp);
153576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SUNOS4 */
153676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
153776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tcp->flags & TCB_BPTSET) {
153876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (clearbpt(tcp) < 0) /* Pretty fatal */ {
153976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					droptcb(tcp);
154076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					cleanup();
154176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					return -1;
154276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
154376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
154476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			goto tracing;
154576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
154676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
154776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (WSTOPSIG(status) != SIGTRAP) {
154876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (WSTOPSIG(status) == SIGSTOP &&
154976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					(tcp->flags & TCB_SIGTRAPPED)) {
155076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				/*
155176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * Trapped attempt to block SIGTRAP
155276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 * Hope we are back in control now.
155376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				 */
155476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED);
155576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				if (ptrace(PTRACE_SYSCALL,
155676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman						pid, (char *) 1, 0) < 0) {
155776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					perror("trace: ptrace(PTRACE_SYSCALL, ...)");
155876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					cleanup();
155976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					return -1;
156076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				}
156176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
156276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
156376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!cflag
156476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			    && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) {
156576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printleader(tcp);
156676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("--- %s (%s) ---",
1567ce780fc9e6067b15b65ca2904c698c77503bf635Nate Sammons					signame(WSTOPSIG(status)),
156876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					strsignal(WSTOPSIG(status)));
156976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				printtrailer(tcp);
157076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
157176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if ((tcp->flags & TCB_ATTACHED) &&
157276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				!sigishandled(tcp, WSTOPSIG(status))) {
157376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				detach(tcp, WSTOPSIG(status));
157476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				continue;
157576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
157676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (ptrace(PTRACE_SYSCALL, pid, (char *) 1,
157776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				   WSTOPSIG(status)) < 0) {
157876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("trace: ptrace(PTRACE_SYSCALL, ...)");
157976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				cleanup();
158076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
158176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
158276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tcp->flags &= ~TCB_SUSPENDED;
158376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
158476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
158576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (trace_syscall(tcp) < 0) {
158676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tcp->flags & TCB_ATTACHED)
158776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				detach(tcp, 0);
158876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			else {
158976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				ptrace(PTRACE_KILL,
159076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman					tcp->pid, (char *) 1, SIGTERM);
159176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				droptcb(tcp);
159276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
159376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
159476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
159576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_EXITING) {
159676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tcp->flags & TCB_ATTACHED)
159776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				detach(tcp, 0);
159876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) {
159976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				perror("strace: ptrace(PTRACE_CONT, ...)");
160076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				cleanup();
160176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				return -1;
160276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			}
160376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
160476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
160576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (tcp->flags & TCB_SUSPENDED) {
160676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (!qflag)
160776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				fprintf(stderr, "Process %u suspended\n", pid);
160876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			continue;
160976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
161076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tracing:
161176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) {
161276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			perror("trace: ptrace(PTRACE_SYSCALL, ...)");
161376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			cleanup();
161476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			return -1;
161576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
161676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
161776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return 0;
161876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
161976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
162076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !SVR4 */
162176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
162276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int curcol;
162376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
162476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef __STDC__
162576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <stdarg.h>
162676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#define VA_START(a, b) va_start(a, b)
162776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else
162876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <varargs.h>
162976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#define VA_START(a, b) va_start(a)
163076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
163176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
163276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
163376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef __STDC__
163476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantprintf(const char *fmt, ...)
163576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else
163676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantprintf(fmt, va_alist)
163776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *fmt;
163876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanva_dcl
163976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif
164076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
164176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	va_list args;
164276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
164376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	VA_START(args, fmt);
164476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (outf)
164576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		curcol += vfprintf(outf, fmt, args);
164676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	va_end(args);
164776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	return;
164876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
164976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
165076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
165176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanprintleader(tcp)
165276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
165376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
165476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) {
165576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tcp_last->flags |= TCB_REPRINT;
165676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tprintf(" <unfinished ...>\n");
165776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
165876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	curcol = 0;
165976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if ((followfork == 1 || pflag_seen > 1) && outfname)
166076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tprintf("%-5d ", tcp->pid);
166176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	else if (nprocs > 1 && !outfname)
166276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tprintf("[pid %5u] ", tcp->pid);
166376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (tflag) {
166476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		char str[sizeof("HH:MM:SS")];
166576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		struct timeval tv, dtv;
166676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		static struct timeval otv;
166776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
166876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		gettimeofday(&tv, NULL);
166976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		if (rflag) {
167076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (otv.tv_sec == 0)
167176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				otv = tv;
167276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tv_sub(&dtv, &tv, &otv);
167376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tprintf("%6ld.%06ld ",
167476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				(long) dtv.tv_sec, (long) dtv.tv_usec);
167576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			otv = tv;
167676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
167776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else if (tflag > 2) {
167876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			tprintf("%ld.%06ld ",
167976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				(long) tv.tv_sec, (long) tv.tv_usec);
168076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
168176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		else {
168276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			time_t local = tv.tv_sec;
168376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			strftime(str, sizeof(str), "%T", localtime(&local));
168476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			if (tflag > 1)
168576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("%s.%06ld ", str, (long) tv.tv_usec);
168676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman			else
168776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman				tprintf("%s ", str);
168876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		}
168976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	}
169076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (iflag)
169176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		printcall(tcp);
169276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
169376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
169476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
169576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantabto(col)
169676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint col;
169776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
169876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	if (curcol < col)
169976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman		tprintf("%*s", col - curcol, "");
170076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
170176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman
170276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid
170376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanprinttrailer(tcp)
170476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp;
170576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{
170676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tprintf("\n");
170776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman	tcp_last = NULL;
170876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}
17099ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
17109ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#ifdef SVR4_MP
17119ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
17129ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkermanint mp_ioctl (int fd, int cmd, void *arg, int size) {
17139ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
17149ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	struct iovec iov[2];
17159ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	int n = 1;
17169ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
17179ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	iov[0].iov_base = &cmd;
17189ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	iov[0].iov_len = sizeof cmd;
17199ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	if (arg) {
17209ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		++n;
17219ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		iov[1].iov_base = arg;
17229ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman		iov[1].iov_len = size;
17239ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	}
17249ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
17259ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman	return writev (fd, iov, n);
17269ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman}
17279ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman
17289ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#endif
1729