strace.c revision 2f1d87e74ff68004b99901f964a1cbab89da09db
176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman/* 276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl> 376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl> 476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com> 54dc8a2aec63e4fb5ee2688544c4de323ed5de3efWichert Akkerman * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl> 676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * All rights reserved. 776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Redistribution and use in source and binary forms, with or without 976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * modification, are permitted provided that the following conditions 1076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * are met: 1176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 1. Redistributions of source code must retain the above copyright 1276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * notice, this list of conditions and the following disclaimer. 1376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 2. Redistributions in binary form must reproduce the above copyright 1476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * notice, this list of conditions and the following disclaimer in the 1576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * documentation and/or other materials provided with the distribution. 1676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 3. The name of the author may not be used to endorse or promote products 1776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * derived from this software without specific prior written permission. 1876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 1976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * 3076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * $Id$ 3176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 3276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 332ee6e45f36566e8735b35ffad40bfcc626a25a98Wichert Akkerman#include <sys/types.h> 3476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "defs.h" 3576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 3676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <signal.h> 3776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <errno.h> 3876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <sys/param.h> 3976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <fcntl.h> 4076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <sys/resource.h> 4176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <sys/wait.h> 4276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <sys/stat.h> 4376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <pwd.h> 4476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <grp.h> 4576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <string.h> 4676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 47bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS 48bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#include <poll.h> 49bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif 50bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 5176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4 5276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <sys/stropts.h> 53ea78f0f77185f7d6d0b2055805139d96e1be816cWichert Akkerman#ifdef HAVE_MP_PROCFS 549ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#include <sys/uio.h> 559ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#endif 5676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif 5776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 5876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint debug = 0, followfork = 0, followvfork = 0, interactive = 0; 5976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint rflag = 0, tflag = 0, dtime = 0, cflag = 0; 6076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint iflag = 0, xflag = 0, qflag = 0; 6176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pflag_seen = 0; 6276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 6376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *username = NULL; 6476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanuid_t run_uid; 6576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermangid_t run_gid; 6676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 6776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint acolumn = DEFAULT_ACOLUMN; 6876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint max_strlen = DEFAULT_STRLEN; 6976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *outfname = NULL; 7076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert AkkermanFILE *outf; 7176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb tcbtab[MAX_PROCS]; 7276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint nprocs; 7376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *progname; 7476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanextern char version[]; 7576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanextern char **environ; 7676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 7776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic struct tcb *pid2tcb P((int pid)); 7876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int trace P((void)); 7976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void cleanup P((void)); 8076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void interrupt P((int sig)); 8176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic sigset_t empty_set, blocked_set; 8276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 8376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE_SIG_ATOMIC_T 8476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic volatile sig_atomic_t interrupted; 8576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !HAVE_SIG_ATOMIC_T */ 8676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef __STDC__ 8776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic volatile int interrupted; 8876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !__STDC__ */ 8976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int interrupted; 9076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !__STDC__ */ 9176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_SIG_ATOMIC_T */ 9276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 93bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS 9476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 9576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic struct tcb *pfd2tcb P((int pfd)); 9676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void reaper P((int sig)); 9776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void rebuild_pollv P((void)); 98e68d61c7d6dcf986d32c462e533ea46f85183db8Wichert Akkermanstruct pollfd pollv[MAX_PROCS]; 9976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 10076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS 10176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 10276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void proc_poll_open P((void)); 10376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void proc_poller P((int pfd)); 10476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 10576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct proc_pollfd { 10676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int fd; 10776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int revents; 10876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int pid; 10976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman}; 11076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 11176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int poller_pid; 11276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int proc_poll_pipe[2] = { -1, -1 }; 11376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 11476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */ 11576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 116ea78f0f77185f7d6d0b2055805139d96e1be816cWichert Akkerman#ifdef HAVE_MP_PROCFS 1179ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#define POLLWANT POLLWRNORM 1189ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#else 1199ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#define POLLWANT POLLPRI 1209ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#endif 121bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */ 12276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 12376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void 12476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanusage(ofp, exitval) 12576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert AkkermanFILE *ofp; 12676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint exitval; 12776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 12876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(ofp, "\ 12976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanusage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\ 13076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman [-p pid] ... [-s strsize] [-u username] [command [arg ...]]\n\ 13176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman or: strace -c [-e expr] ... [-O overhead] [-S sortby] [command [arg ...]]\n\ 13276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-c -- count time, calls, and errors for each syscall and report summary\n\ 13376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-f -- follow forks, -ff -- with output into separate files\n\ 13476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-F -- attempt to follow vforks, -h -- print help message\n\ 13576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-i -- print instruction pointer at time of syscall\n\ 13676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-q -- suppress messages about attaching, detaching, etc.\n\ 13776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\ 13876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-T -- print time spent in each syscall, -V -- print version\n\ 13976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\ 14076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\ 14176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-a column -- alignment COLUMN for printing syscall results (default %d)\n\ 14276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\ 14376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman options: trace, abbrev, verbose, raw, signal, read, or write\n\ 14476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-o file -- send trace output to FILE instead of stderr\n\ 14576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\ 14676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-p pid -- trace process with process id PID, may be repeated\n\ 14776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\ 14876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\ 14976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman-u username -- run command as username handling setuid and/or setgid\n\ 15076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman", DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY); 15176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(exitval); 15276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 15376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 15476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SVR4 15576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef MIPS 15676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid 15776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanfoobar() 15876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 15976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 16076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* MIPS */ 16176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SVR4 */ 16276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 16376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint 16476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanmain(argc, argv) 16576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint argc; 16676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *argv[]; 16776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 16876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman extern int optind; 16976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman extern char *optarg; 17076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct tcb *tcp; 17176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int c, pid = 0; 17276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct sigaction sa; 17376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 17476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman static char buf[BUFSIZ]; 17576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 17676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman progname = argv[0]; 17776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman outf = stderr; 17876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman interactive = 1; 17976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman qualify("trace=all"); 18076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman qualify("abbrev=all"); 18176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman qualify("verbose=all"); 18276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman qualify("signal=all"); 18376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman set_sortby(DEFAULT_SORTBY); 18476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman set_personality(DEFAULT_PERSONALITY); 18576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman while ((c = getopt(argc, argv, 18676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman "+cdfFhiqrtTvVxa:e:o:O:p:s:S:u:")) != EOF) { 18776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman switch (c) { 18876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'c': 18976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman cflag++; 19076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman dtime++; 19176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 19276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'd': 19376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman debug++; 19476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 19576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'f': 19676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman followfork++; 19776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 19876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'F': 19976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman followvfork++; 20076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 20176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'h': 20276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman usage(stdout, 0); 20376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 20476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'i': 20576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman iflag++; 20676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 20776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'q': 20876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman qflag++; 20976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 21076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'r': 21176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman rflag++; 21276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tflag++; 21376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 21476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 't': 21576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tflag++; 21676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 21776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'T': 21876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman dtime++; 21976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 22076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'x': 22176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman xflag++; 22276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 22376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'v': 22476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman qualify("abbrev=none"); 22576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 22676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'V': 22776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman printf("%s\n", version); 22876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(0); 22976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 23076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'a': 23176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman acolumn = atoi(optarg); 23276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 23376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'e': 23476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman qualify(optarg); 23576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 23676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'o': 23776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman outfname = strdup(optarg); 23876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 23976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'O': 24076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman set_overhead(atoi(optarg)); 24176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 24276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'p': 24376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((pid = atoi(optarg)) == 0) { 24476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "%s: Invalid process id: %s\n", 24576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman progname, optarg); 24676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 24776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 24876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (pid == getpid()) { 24954a4767f8609abfe2d7cb1802bc9e8dca97dd08fWichert Akkerman fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname); 25076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 25176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 25276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((tcp = alloctcb(pid)) == NULL) { 25376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "%s: tcb table full, please recompile strace\n", 25476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman progname); 25576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 25676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 25776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->flags |= TCB_ATTACHED; 25876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pflag_seen++; 25976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 26076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 's': 26176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman max_strlen = atoi(optarg); 26276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 26376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'S': 26476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman set_sortby(optarg); 26576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 26676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 'u': 26776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman username = strdup(optarg); 26876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 26976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman default: 27076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman usage(stderr, 1); 27176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 27276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 27376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 27476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 27576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* See if they want to run as another user. */ 27676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (username != NULL) { 27776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct passwd *pent; 27876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 27976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (getuid() != 0 || geteuid() != 0) { 28076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, 28176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman "%s: you must be root to use the -u option\n", 28276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman progname); 28376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 28476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 28576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((pent = getpwnam(username)) == NULL) { 28676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "%s: cannot find user `%s'\n", 28776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman progname, optarg); 28876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 28976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 29076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman run_uid = pent->pw_uid; 29176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman run_gid = pent->pw_gid; 29276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 29376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else { 29476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman run_uid = getuid(); 29576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman run_gid = getgid(); 29676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 29776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 29876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4 29976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman setreuid(geteuid(), getuid()); 30076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif 30176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 30276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* See if they want to pipe the output. */ 30376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (outfname && (outfname[0] == '|' || outfname[0] == '!')) { 30476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((outf = popen(outfname + 1, "w")) == NULL) { 30576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "%s: can't popen '%s': %s\n", 30676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman progname, outfname + 1, strerror(errno)); 30776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 30876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 30976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman free(outfname); 31076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman outfname = NULL; 31176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 31276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 31376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Check if they want to redirect the output. */ 31476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (outfname) { 31576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((outf = fopen(outfname, "w")) == NULL) { 31676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "%s: can't fopen '%s': %s\n", 31776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman progname, outfname, strerror(errno)); 31876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 31976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 32076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 32176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 32276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4 32376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman setreuid(geteuid(), getuid()); 32476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif 32576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 32676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!outfname) { 32776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman qflag = 1; 32876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman setvbuf(outf, buf, _IOLBF, BUFSIZ); 32976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 33076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else if (optind < argc) 33176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman interactive = 0; 33276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else 33376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman qflag = 1; 33476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 33576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (c = 0, tcp = tcbtab; c < MAX_PROCS; c++, tcp++) { 33676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Reinitialize the output since it may have changed. */ 33776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->outf = outf; 33876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED)) 33976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 340bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS 34176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (proc_open(tcp, 1) < 0) { 34276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "trouble opening proc file\n"); 34376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman droptcb(tcp); 34476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 34576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 346bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* !USE_PROCFS */ 34776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) { 34876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("attach: ptrace(PTRACE_ATTACH, ...)"); 34976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman droptcb(tcp); 35076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 35176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 352bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */ 35376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!qflag) 35476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, 35576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman "Process %u attached - interrupt to quit\n", 35676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pid); 35776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 35876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 35976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (optind < argc) { 36076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct stat statbuf; 36176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman char *filename; 36276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman char pathname[MAXPATHLEN]; 36376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 36476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman filename = argv[optind]; 36576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (strchr(filename, '/')) 36676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman strcpy(pathname, filename); 36776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef USE_DEBUGGING_EXEC 36876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* 36976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Debuggers customarily check the current directory 37076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * first regardless of the path but doing that gives 37176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * security geeks a panic attack. 37276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 37376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else if (stat(filename, &statbuf) == 0) 37476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman strcpy(pathname, filename); 37576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* USE_DEBUGGING_EXEC */ 37676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else { 37776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman char *path; 37876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int m, n, len; 37976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 38076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (path = getenv("PATH"); path && *path; path += m) { 38176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (strchr(path, ':')) { 38276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman n = strchr(path, ':') - path; 38376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman m = n + 1; 38476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 38576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else 38676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman m = n = strlen(path); 38776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (n == 0) { 38876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman getcwd(pathname, MAXPATHLEN); 38976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman len = strlen(pathname); 39076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 39176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else { 39276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman strncpy(pathname, path, n); 39376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman len = n; 39476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 39576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (len && pathname[len - 1] != '/') 39676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pathname[len++] = '/'; 39776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman strcpy(pathname + len, filename); 39876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (stat(pathname, &statbuf) == 0) 39976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 40076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 40176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 40276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (stat(pathname, &statbuf) < 0) { 40376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "%s: %s: command not found\n", 40476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman progname, filename); 40576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 40676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 40776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman switch (pid = fork()) { 40876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case -1: 40976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("strace: fork"); 41076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman cleanup(); 41176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 41276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 41376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 0: { 414bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS 415bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if (outf != stderr) close (fileno (outf)); 41676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef MIPS 41776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Kludge for SGI, see proc_open for details. */ 41876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sa.sa_handler = foobar; 41976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sa.sa_flags = 0; 42076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigemptyset(&sa.sa_mask); 42176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaction(SIGINT, &sa, NULL); 42276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* MIPS */ 423bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD 42476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pause(); 425bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */ 426bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman kill(getpid(), SIGSTOP); /* stop HERE */ 427bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */ 428bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* !USE_PROCFS */ 429bd4125c6bcd20e8f014b682b31d6fc5d0fef3ed0Wichert Akkerman if (outf!=stderr) 4307987cdf192632516d6ba493b0a9943f5a3a7c362Wichert Akkerman close(fileno (outf)); 431bd4125c6bcd20e8f014b682b31d6fc5d0fef3ed0Wichert Akkerman 43276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) { 43376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("strace: ptrace(PTRACE_TRACEME, ...)"); 43476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 43576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 43676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (debug) 43776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman kill(getpid(), SIGSTOP); 43876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 43976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (username != NULL || geteuid() == 0) { 44076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman uid_t run_euid = run_uid; 44176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman gid_t run_egid = run_gid; 44276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 44376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (statbuf.st_mode & S_ISUID) 44476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman run_euid = statbuf.st_uid; 44576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (statbuf.st_mode & S_ISGID) 44676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman run_egid = statbuf.st_gid; 44776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 44876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* 44976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * It is important to set groups before we 45076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * lose privileges on setuid. 45176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 4525ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman if (username != NULL) { 4535ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman if (initgroups(username, run_gid) < 0) { 4545ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman perror("initgroups"); 4555ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman exit(1); 4565ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman } 4575ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman if (setregid(run_gid, run_egid) < 0) { 4585ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman perror("setregid"); 4595ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman exit(1); 4605ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman } 4615ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman if (setreuid(run_uid, run_euid) < 0) { 4625ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman perror("setreuid"); 4635ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman exit(1); 4645ae21ead9f83597452f9a0517e8a51fa4823f921Wichert Akkerman } 46576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 46676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 46776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else 46876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman setreuid(run_uid, run_uid); 469bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */ 47076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 47176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman execv(pathname, &argv[optind]); 47276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("strace: exec"); 47376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman _exit(1); 47476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 47576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 47676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman default: 47776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((tcp = alloctcb(pid)) == NULL) { 47876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "tcb table full\n"); 47976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman cleanup(); 48076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 48176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 482bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS 48376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (proc_open(tcp, 0) < 0) { 48476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "trouble opening proc file\n"); 48576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman cleanup(); 48676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 48776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 488bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */ 489bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef USE_PROCFS 49076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fake_execve(tcp, pathname, &argv[optind], environ); 491bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */ 49276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 49376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 49476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 49576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else if (pflag_seen == 0) 49676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman usage(stderr, 1); 49776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 49876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigemptyset(&empty_set); 49976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigemptyset(&blocked_set); 50076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sa.sa_handler = SIG_IGN; 50176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigemptyset(&sa.sa_mask); 50276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sa.sa_flags = 0; 50376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaction(SIGTTOU, &sa, NULL); 50476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaction(SIGTTIN, &sa, NULL); 50576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (interactive) { 50676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaddset(&blocked_set, SIGHUP); 50776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaddset(&blocked_set, SIGINT); 50876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaddset(&blocked_set, SIGQUIT); 50976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaddset(&blocked_set, SIGPIPE); 51076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaddset(&blocked_set, SIGTERM); 51176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sa.sa_handler = interrupt; 51276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SUNOS4 51376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* POSIX signals on sunos4.1 are a little broken. */ 51476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sa.sa_flags = SA_INTERRUPT; 51576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SUNOS4 */ 51676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 51776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaction(SIGHUP, &sa, NULL); 51876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaction(SIGINT, &sa, NULL); 51976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaction(SIGQUIT, &sa, NULL); 52076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaction(SIGPIPE, &sa, NULL); 52176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaction(SIGTERM, &sa, NULL); 522bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS 52376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sa.sa_handler = reaper; 52476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaction(SIGCHLD, &sa, NULL); 525bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */ 52676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 52776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (trace() < 0) 52876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 52976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman cleanup(); 53076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(0); 53176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 53276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 53376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid 53476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermannewoutf(tcp) 53576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp; 53676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 53776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman char name[MAXPATHLEN]; 53876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman FILE *fp; 53976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 54076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (outfname && followfork > 1) { 54176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sprintf(name, "%s.%u", outfname, tcp->pid); 54276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4 54376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman setreuid(geteuid(), getuid()); 54476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif 54576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fp = fopen(name, "w"); 54676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SVR4 54776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman setreuid(geteuid(), getuid()); 54876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif 54976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (fp == NULL) { 55076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("fopen"); 55176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return; 55276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 55376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->outf = fp; 55476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 55576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return; 55676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 55776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 55876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb * 55976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanalloctcb(pid) 56076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pid; 56176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 56276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int i; 56376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct tcb *tcp; 56476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 56576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) { 56676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((tcp->flags & TCB_INUSE) == 0) { 56776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->pid = pid; 56876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->parent = NULL; 56976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->nchildren = 0; 57076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->flags = TCB_INUSE | TCB_STARTUP; 57176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->outf = outf; /* Initialise to current out file */ 57276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->stime.tv_sec = 0; 57376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->stime.tv_usec = 0; 57476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->pfd = -1; 57576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman nprocs++; 57676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return tcp; 57776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 57876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 57976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return NULL; 58076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 58176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 582bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS 58376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint 58476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanproc_open(tcp, attaching) 58576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp; 58676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint attaching; 58776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 58876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman char proc[32]; 58976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman long arg; 590bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef SVR4 59176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sysset_t sc_enter, sc_exit; 59276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigset_t signals; 59376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fltset_t faults; 594bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif 59576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS 59676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman static int last_pfd; 59776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif 59876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 599ea78f0f77185f7d6d0b2055805139d96e1be816cWichert Akkerman#ifdef HAVE_MP_PROCFS 6009ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman /* Open the process pseudo-files in /proc. */ 6019ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman sprintf(proc, "/proc/%d/ctl", tcp->pid); 6029ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) { 6039ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman perror("strace: open(\"/proc/...\", ...)"); 6049ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman return -1; 6059ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6069ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) { 6079ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman perror("F_GETFD"); 6089ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman return -1; 6099ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6109ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) { 6119ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman perror("F_SETFD"); 6129ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman return -1; 6139ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6149ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman sprintf(proc, "/proc/%d/status", tcp->pid); 6159ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) { 6169ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman perror("strace: open(\"/proc/...\", ...)"); 6179ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman return -1; 6189ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6199ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) { 6209ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman perror("F_GETFD"); 6219ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman return -1; 6229ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6239ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) { 6249ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman perror("F_SETFD"); 6259ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman return -1; 6269ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6279ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman sprintf(proc, "/proc/%d/as", tcp->pid); 6289ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) { 6299ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman perror("strace: open(\"/proc/...\", ...)"); 6309ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman return -1; 6319ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6329ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) { 6339ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman perror("F_GETFD"); 6349ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman return -1; 6359ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6369ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) { 6379ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman perror("F_SETFD"); 6389ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman return -1; 6399ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6409ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#else 64176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Open the process pseudo-file in /proc. */ 642bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD 64376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sprintf(proc, "/proc/%d", tcp->pid); 64476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) { 645bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */ 646bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman sprintf(proc, "/proc/%d/mem", tcp->pid); 647bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if ((tcp->pfd = open(proc, O_RDWR)) < 0) { 648bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */ 64976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("strace: open(\"/proc/...\", ...)"); 65076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 65176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 6529ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) { 6539ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman perror("F_GETFD"); 6549ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman return -1; 6559ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6569ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) { 6579ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman perror("F_SETFD"); 6589ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman return -1; 6599ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 6609ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#endif 661bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD 662bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman sprintf(proc, "/proc/%d/regs", tcp->pid); 663bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) { 664bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman perror("strace: open(\"/proc/.../regs\", ...)"); 665bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman return -1; 666bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 667bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if (cflag) { 668bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman sprintf(proc, "/proc/%d/status", tcp->pid); 669bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) { 670bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman perror("strace: open(\"/proc/.../status\", ...)"); 671bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman return -1; 672bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 673bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } else 674bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman tcp->pfd_status = -1; 675bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */ 67676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman rebuild_pollv(); 67776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!attaching) { 67876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* 67976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Wait for the child to pause. Because of a race 68076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * condition we have to poll for the event. 68176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 68276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (;;) { 6839ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (IOCTL_STATUS (tcp) < 0) { 68476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("strace: PIOCSTATUS"); 68576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 68676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 6879ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (tcp->status.PR_FLAGS & PR_ASLEEP) 688bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman break; 68976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 69076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 691bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD 69276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Stop the process so that we own the stop. */ 69316a03d2e97415afe6cf34172a0aea97a95a0b160Wichert Akkerman if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) { 69476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("strace: PIOCSTOP"); 69576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 69676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 697bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif 69876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef PIOCSET 69976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Set Run-on-Last-Close. */ 70076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman arg = PR_RLC; 7019ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) { 70276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("PIOCSET PR_RLC"); 70376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 70476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 70576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Set or Reset Inherit-on-Fork. */ 70676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman arg = PR_FORK; 7079ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) { 70876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("PIOC{SET,RESET} PR_FORK"); 70976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 71076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 71176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !PIOCSET */ 712bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD 71376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (ioctl(tcp->pfd, PIOCSRLC) < 0) { 71476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("PIOCSRLC"); 71576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 71676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 71776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) { 71876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("PIOC{S,R}FORK"); 71976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 72076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 721bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */ 722bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman /* just unset the PF_LINGER flag for the Run-on-Last-Close. */ 723bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) { 724bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman perror("PIOCGFL"); 725bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman return -1; 726bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 727bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman arg &= ~PF_LINGER; 728bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) { 729bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman perror("PIOCSFL"); 730bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman return -1; 731bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 732bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */ 73376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !PIOCSET */ 734bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD 73576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Enable all syscall entries. */ 73676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman prfillset(&sc_enter); 7379ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (IOCTL(tcp->pfd, PIOCSENTRY, &sc_enter) < 0) { 73876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("PIOCSENTRY"); 73976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 74076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 74176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Enable all syscall exits. */ 74276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman prfillset(&sc_exit); 7439ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (IOCTL(tcp->pfd, PIOCSEXIT, &sc_exit) < 0) { 74476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("PIOSEXIT"); 74576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 74676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 74776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Enable all signals. */ 74876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman prfillset(&signals); 7499ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) { 75076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("PIOCSTRACE"); 75176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 75276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 75376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Enable all faults. */ 75476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman prfillset(&faults); 7559ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) { 75676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("PIOCSFAULT"); 75776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 75876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 759bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */ 760bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman /* set events flags. */ 761bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman arg = S_SIG | S_SCE | S_SCX ; 762bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if(ioctl(tcp->pfd, PIOCBIS, arg) < 0) { 763bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman perror("PIOCBIS"); 764bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman return -1; 765bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 766bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */ 76776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!attaching) { 76876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef MIPS 76976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* 77076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * The SGI PRSABORT doesn't work for pause() so 77176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * we send it a caught signal to wake it up. 77276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 77376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman kill(tcp->pid, SIGINT); 77476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !MIPS */ 775bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef PRSABORT 77676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* The child is in a pause(), abort it. */ 7779ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman arg = PRSABORT; 7789ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) { 77976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("PIOCRUN"); 78076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 78176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 782bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif 783bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !MIPS*/ 784bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD 785bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman /* wake up the child if it received the SIGSTOP */ 786bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman kill(tcp->pid, SIGCONT); 787bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif 78876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (;;) { 78976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Wait for the child to do something. */ 7909ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (IOCTL_WSTOP (tcp) < 0) { 79176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("PIOCWSTOP"); 79276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 79376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 7949ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (tcp->status.PR_WHY == PR_SYSENTRY) { 795bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman tcp->flags &= ~TCB_INSYSCALL; 796bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman get_scno(tcp); 797bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if (tcp->scno == SYS_execve) 79876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 79976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 80076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Set it running: maybe execve will be next. */ 801bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD 8029ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman arg = 0; 8039ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) { 804bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */ 805bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) { 806bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */ 80776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("PIOCRUN"); 80876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 80976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 810bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD 811bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman /* handle the case where we "opened" the child before 812bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman it did the kill -STOP */ 813bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if (tcp->status.PR_WHY == PR_SIGNALLED && 814bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman tcp->status.PR_WHAT == SIGSTOP) 815bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman kill(tcp->pid, SIGCONT); 816bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif 81776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 818bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD 81976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 820bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */ 821bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } else { 8222e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman if (attaching < 2) { 8232e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman /* We are attaching to an already running process. 8242e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman * Try to figure out the state of the process in syscalls, 8252e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman * to handle the first event well. 8262e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman * This is done by having a look at the "wchan" property of the 8272e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman * process, which tells where it is stopped (if it is). */ 8282e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman FILE * status; 8292e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman char wchan[20]; /* should be enough */ 8302e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman 8312e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman sprintf(proc, "/proc/%d/status", tcp->pid); 8322e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman status = fopen(proc, "r"); 8332e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman if (status && 8342e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman (fscanf(status, "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d" 8352e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman "%*d,%*d %*d,%*d %19s", wchan) == 1) && 8362e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman strcmp(wchan, "nochan") && strcmp(wchan, "spread") && 8372e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman strcmp(wchan, "stopevent")) { 8382e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman /* The process is asleep in the middle of a syscall. 8392e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman Fake the syscall entry event */ 8402e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman tcp->flags &= ~(TCB_INSYSCALL|TCB_STARTUP); 8412e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman tcp->status.PR_WHY = PR_SYSENTRY; 8422e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman trace_syscall(tcp); 8432e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman } 8442e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman if (status) 8452e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman fclose(status); 8462e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman } /* otherwise it's a fork being followed */ 847bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 848bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */ 84976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS 85076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (proc_poll_pipe[0] != -1) 85176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman proc_poller(tcp->pfd); 85276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else if (nprocs > 1) { 85376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman proc_poll_open(); 85476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman proc_poller(last_pfd); 85576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman proc_poller(tcp->pfd); 85676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 85776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman last_pfd = tcp->pfd; 85876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */ 85976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return 0; 86076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 86176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 862bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */ 86376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 86476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic struct tcb * 86576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanpid2tcb(pid) 86676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pid; 86776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 86876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int i; 86976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct tcb *tcp; 87076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 87176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) { 87276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (pid && tcp->pid != pid) 87376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 87476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->flags & TCB_INUSE) 87576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return tcp; 87676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 87776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return NULL; 87876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 87976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 880bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS 88176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 88276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic struct tcb * 88376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanpfd2tcb(pfd) 88476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pfd; 88576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 88676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int i; 88776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct tcb *tcp; 88876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 88976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) { 89076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->pfd != pfd) 89176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 89276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->flags & TCB_INUSE) 89376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return tcp; 89476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 89576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return NULL; 89676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 89776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 898bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */ 89976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 90076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid 90176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermandroptcb(tcp) 90276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp; 90376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 90476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->pid == 0) 90576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return; 90676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman nprocs--; 90776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->pid = 0; 90876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->flags = 0; 90976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->pfd != -1) { 91076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman close(tcp->pfd); 91176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->pfd = -1; 912bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD 913bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if (tcp->pfd_reg != -1) { 914bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman close(tcp->pfd_reg); 915bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman tcp->pfd_reg = -1; 916bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 917bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if (tcp->pfd_status != -1) { 918bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman close(tcp->pfd_status); 919bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman tcp->pfd_status = -1; 920bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } 921bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !FREEBSD */ 922bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS 92376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman rebuild_pollv(); 92476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif 92576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 92676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->parent != NULL) { 92776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->parent->nchildren--; 92876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->parent = NULL; 92976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 93076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#if 0 93176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->outf != stderr) 93276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fclose(tcp->outf); 93376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif 93476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->outf = 0; 93576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 93676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 937bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef USE_PROCFS 93876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 93976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int 94076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanresume(tcp) 94176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp; 94276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 94376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp == NULL) 94476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 94576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 94676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!(tcp->flags & TCB_SUSPENDED)) { 94776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid); 94876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 94976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 95076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->flags &= ~TCB_SUSPENDED; 95176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 95276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) { 95376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("resume: ptrace(PTRACE_SYSCALL, ...)"); 95476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 95576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 95676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 95776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!qflag) 95876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "Process %u resumed\n", tcp->pid); 95976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return 0; 96076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 96176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 962bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */ 96376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 96476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman/* detach traced process; continue with sig */ 96576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 96676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int 96776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermandetach(tcp, sig) 96876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp; 96976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig; 97076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 97176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int error = 0; 97276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX 97376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int status; 97476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif 97576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 97676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->flags & TCB_BPTSET) 97776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sig = SIGKILL; 97876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 97976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX 98076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* 98176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Linux wrongly insists the child be stopped 98276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * before detaching. Arghh. We go through hoops 98376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * to make a clean break of things. 98476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 985dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman#if defined(SPARC) 986dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman#undef PTRACE_DETACH 987dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman#define PTRACE_DETACH PTRACE_SUNDETACH 988dacfb6ebd630641d851b6df94c3b4587969a6cfbWichert Akkerman#endif 98976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) { 99076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* On a clear day, you can see forever. */ 99176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 99276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else if (errno != ESRCH) { 99376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Shouldn't happen. */ 99476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("detach: ptrace(PTRACE_DETACH, ...)"); 99576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 99676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else if (kill(tcp->pid, 0) < 0) { 99776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (errno != ESRCH) 99876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("detach: checking sanity"); 99976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 100076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else if (kill(tcp->pid, SIGSTOP) < 0) { 100176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (errno != ESRCH) 100276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("detach: stopping child"); 100376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 100476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else { 100576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (;;) { 100676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (waitpid(tcp->pid, &status, 0) < 0) { 100776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (errno != ECHILD) 100876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("detach: waiting"); 100976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 101076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 101176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!WIFSTOPPED(status)) { 101276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Au revoir, mon ami. */ 101376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 101476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 101576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (WSTOPSIG(status) == SIGSTOP) { 101676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((error = ptrace(PTRACE_DETACH, 101776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->pid, (char *) 1, sig)) < 0) { 101876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (errno != ESRCH) 101976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("detach: ptrace(PTRACE_DETACH, ...)"); 102076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* I died trying. */ 102176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 102276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 102376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 102476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1, 102576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman WSTOPSIG(status) == SIGTRAP ? 102676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 0 : WSTOPSIG(status))) < 0) { 102776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (errno != ESRCH) 102876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("detach: ptrace(PTRACE_CONT, ...)"); 102976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 103076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 103176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 103276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 103376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* LINUX */ 103476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 103576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#if defined(SUNOS4) 103676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */ 103776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (sig && kill(tcp->pid, sig) < 0) 103876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("detach: kill"); 103976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sig = 0; 104076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0) 104176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("detach: ptrace(PTRACE_DETACH, ...)"); 104276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SUNOS4 */ 104376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 1044bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef USE_PROCFS 104576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (waiting_parent(tcp)) 104676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman error = resume(tcp->parent); 1047bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */ 104876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 104976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!qflag) 105076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "Process %u detached\n", tcp->pid); 105176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 105276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman droptcb(tcp); 105376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return error; 105476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 105576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 1056bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS 105776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 105876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void 105976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanreaper(sig) 106076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig; 106176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 106276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int pid; 106376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int status; 106476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 106576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { 106676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#if 0 106776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct tcb *tcp; 106876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 106976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp = pid2tcb(pid); 107076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp) 107176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman droptcb(tcp); 107276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif 107376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 107476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 107576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 1076bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* USE_PROCFS */ 107776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 107876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void 107976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermancleanup() 108076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 108176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int i; 108276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct tcb *tcp; 108376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 108476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) { 108576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!(tcp->flags & TCB_INUSE)) 108676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 108776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (debug) 108876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, 108976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman "cleanup: looking at pid %u\n", tcp->pid); 109076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp_last && 109176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman (!outfname || followfork < 2 || tcp_last == tcp)) { 109276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tprintf(" <unfinished ...>\n"); 109376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp_last = NULL; 109476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 109576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->flags & TCB_ATTACHED) 109676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman detach(tcp, 0); 109776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else { 109876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman kill(tcp->pid, SIGCONT); 109976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman kill(tcp->pid, SIGTERM); 110076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 110176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 110276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (cflag) 110376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman call_summary(outf); 110476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 110576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 110676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void 110776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermaninterrupt(sig) 110876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig; 110976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 111076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman interrupted = 1; 111176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 111276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 111376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_STRERROR 111476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 111576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SYS_ERRLIST_DECLARED 111676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanextern int sys_nerr; 111776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanextern char *sys_errlist[]; 111876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SYS_ERRLIST_DECLARED */ 111976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 112076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanconst char * 112176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstrerror(errno) 112276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint errno; 112376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 112476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman static char buf[64]; 112576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 112676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (errno < 1 || errno >= sys_nerr) { 112776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sprintf(buf, "Unknown error %d", errno); 112876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return buf; 112976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 113076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return sys_errlist[errno]; 113176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 113276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 113376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* HAVE_STERRROR */ 113476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 113576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_STRSIGNAL 113676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 113776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef SYS_SIGLIST_DECLARED 113876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE__SYS_SIGLIST 113976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman extern char *_sys_siglist[]; 114076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else 114176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman extern char *sys_siglist[]; 114276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif 114376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SYS_SIGLIST_DECLARED */ 114476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 114576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanconst char * 114676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstrsignal(sig) 114776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig; 114876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 114976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman static char buf[64]; 115076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 115176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (sig < 1 || sig >= NSIG) { 115276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sprintf(buf, "Unknown signal %d", sig); 115376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return buf; 115476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 115576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE__SYS_SIGLIST 115676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return _sys_siglist[sig]; 115776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else 115876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return sys_siglist[sig]; 115976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif 116076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 116176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 116276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* HAVE_STRSIGNAL */ 116376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 1164bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef USE_PROCFS 116576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 116676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void 116776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanrebuild_pollv() 116876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 116976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int i, j; 117076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct tcb *tcp; 117176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 117276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (i = j = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) { 117376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!(tcp->flags & TCB_INUSE)) 117476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 117576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pollv[j].fd = tcp->pfd; 11769ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman pollv[j].events = POLLWANT; 117776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman j++; 117876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 117976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (j != nprocs) { 118076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "strace: proc miscount\n"); 118176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 118276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 118376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 118476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 118576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS 118676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 118776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void 118876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanproc_poll_open() 118976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 119076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int arg; 119176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int i; 119276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 119376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (pipe(proc_poll_pipe) < 0) { 119476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("pipe"); 119576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 119676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 119776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (i = 0; i < 2; i++) { 119876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) { 119976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("F_GETFD"); 120076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 120176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 120276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) { 120376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("F_SETFD"); 120476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 120576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 120676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 120776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 120876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 120976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int 121076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanproc_poll(pollv, nfds, timeout) 121176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct pollfd *pollv; 121276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint nfds; 121376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint timeout; 121476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 121576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int i; 121676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int n; 121776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct proc_pollfd pollinfo; 121876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 121976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0) 122076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return n; 122176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (n != sizeof(struct proc_pollfd)) { 122276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "panic: short read: %d\n", n); 122376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 122476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 122576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (i = 0; i < nprocs; i++) { 122676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (pollv[i].fd == pollinfo.fd) 122776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pollv[i].revents = pollinfo.revents; 122876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else 122976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pollv[i].revents = 0; 123076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 123176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman poller_pid = pollinfo.pid; 123276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return 1; 123376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 123476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 123576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void 123676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanwakeup_handler(sig) 123776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint sig; 123876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 123976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 124076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 124176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic void 124276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanproc_poller(pfd) 124376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint pfd; 124476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 124576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct proc_pollfd pollinfo; 124676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct sigaction sa; 124776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigset_t blocked_set, empty_set; 124876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int i; 124976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int n; 125076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct rlimit rl; 1251bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD 1252bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman struct procfs_status pfs; 1253bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */ 125476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 125576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman switch (fork()) { 125676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case -1: 125776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("fork"); 125876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman _exit(0); 125976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 0: 126076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 126176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman default: 126276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return; 126376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 126476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 126576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sa.sa_handler = interactive ? SIG_DFL : SIG_IGN; 126676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sa.sa_flags = 0; 126776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigemptyset(&sa.sa_mask); 126876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaction(SIGHUP, &sa, NULL); 126976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaction(SIGINT, &sa, NULL); 127076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaction(SIGQUIT, &sa, NULL); 127176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaction(SIGPIPE, &sa, NULL); 127276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaction(SIGTERM, &sa, NULL); 127376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sa.sa_handler = wakeup_handler; 127476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaction(SIGUSR1, &sa, NULL); 127576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigemptyset(&blocked_set); 127676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigaddset(&blocked_set, SIGUSR1); 127776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigprocmask(SIG_BLOCK, &blocked_set, NULL); 127876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigemptyset(&empty_set); 127976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 128076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (getrlimit(RLIMIT_NOFILE, &rl) < 0) { 128176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("getrlimit(RLIMIT_NOFILE, ...)"); 128276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman _exit(0); 128376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 128476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman n = rl.rlim_cur; 128576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (i = 0; i < n; i++) { 128676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (i != pfd && i != proc_poll_pipe[1]) 128776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman close(i); 128876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 128976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 129076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pollinfo.fd = pfd; 129176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pollinfo.pid = getpid(); 129276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (;;) { 1293bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD 1294bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if (ioctl(pfd, PIOCWSTOP, NULL) < 0) 1295bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */ 1296bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if (ioctl(pfd, PIOCWSTOP, &pfs) < 0) 1297bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */ 12989ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman { 129976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman switch (errno) { 130076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case EINTR: 130176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 130276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case EBADF: 130376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pollinfo.revents = POLLERR; 130476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 130576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case ENOENT: 130676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pollinfo.revents = POLLHUP; 130776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 130876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman default: 130976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("proc_poller: PIOCWSTOP"); 131076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 131176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo)); 131276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman _exit(0); 131376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 13149ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman pollinfo.revents = POLLWANT; 131576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo)); 131676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigsuspend(&empty_set); 131776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 131876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 131976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 132076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */ 132176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 132276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int 132376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchoose_pfd() 132476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 132576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int i, j; 132676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct tcb *tcp; 132776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 132876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman static int last; 132976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 133076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (followfork < 2 && 13319ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman last < nprocs && (pollv[last].revents & POLLWANT)) { 133276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* 133376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * The previous process is ready to run again. We'll 133476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * let it do so if it is currently in a syscall. This 133576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * heuristic improves the readability of the trace. 133676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 133776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp = pfd2tcb(pollv[last].fd); 133876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp && (tcp->flags & TCB_INSYSCALL)) 133976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return pollv[last].fd; 134076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 134176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 134276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (i = 0; i < nprocs; i++) { 134376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Let competing children run round robin. */ 134476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman j = (i + last + 1) % nprocs; 134576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (pollv[j].revents & (POLLHUP | POLLERR)) { 134676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp = pfd2tcb(pollv[j].fd); 134776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!tcp) { 134876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "strace: lost proc\n"); 134976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 135076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 135176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman droptcb(tcp); 135276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 135376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 13549ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (pollv[j].revents & POLLWANT) { 135576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman last = j; 135676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return pollv[j].fd; 135776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 135876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 135976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "strace: nothing ready\n"); 136076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 136176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 136276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 136376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int 136476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantrace() 136576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 13669dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#ifdef POLL_HACK 13679dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman struct tcb *in_syscall; 13689dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#endif 136976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct tcb *tcp; 137076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int pfd; 137176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int what; 137276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int ioctl_result = 0, ioctl_errno = 0; 13739ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman long arg; 137476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 137576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman for (;;) { 137676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (interactive) 137776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigprocmask(SIG_SETMASK, &empty_set, NULL); 137876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 137976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (nprocs == 0) 138076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 138176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 138276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman switch (nprocs) { 138376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case 1: 138476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS 138576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (proc_poll_pipe[0] == -1) { 138676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif 138776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp = pid2tcb(0); 138876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!tcp) 138976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 139076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pfd = tcp->pfd; 139176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (pfd == -1) 139276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 139376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 139476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS 139576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 139676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* fall through ... */ 139776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */ 139876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman default: 139976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef HAVE_POLLABLE_PROCFS 14009dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#ifdef POLL_HACK 14019dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman /* On some systems (e.g. UnixWare) we get too much ugly 14029dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman "unfinished..." stuff when multiple proceses are in 14039dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman syscalls. Here's a nasty hack */ 14049dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman 14059dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman if (in_syscall) { 14069dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman struct pollfd pv; 14079dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman tcp = in_syscall; 14089dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman in_syscall = NULL; 14099dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman pv.fd = tcp->pfd; 14109dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman pv.events = POLLWANT; 14119dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman if ((what = poll (&pv, 1, 1)) < 0) { 14129dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman if (interrupted) 14139dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman return 0; 14149dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman continue; 14159dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman } 14169dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman else if (what == 1 && pv.revents & POLLWANT) { 14179dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman goto FOUND; 14189dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman } 14199dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman } 14209dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#endif 14219dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman 142276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (poll(pollv, nprocs, INFTIM) < 0) { 142376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (interrupted) 142476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return 0; 142576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 142676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 142776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else /* !HAVE_POLLABLE_PROCFS */ 142876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (proc_poll(pollv, nprocs, INFTIM) < 0) { 142976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (interrupted) 143076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return 0; 143176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 143276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 143376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */ 143476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pfd = choose_pfd(); 143576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (pfd == -1) 143676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 143776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 143876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 143976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 144076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Look up `pfd' in our table. */ 144176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((tcp = pfd2tcb(pfd)) == NULL) { 144276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "unknown pfd: %u\n", pfd); 144376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 144476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 14459dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman FOUND: 144676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Get the status of the process. */ 144776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!interrupted) { 1448bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD 14499ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman ioctl_result = IOCTL_WSTOP (tcp); 1450bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* FREEBSD */ 1451bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman /* Thanks to some scheduling mystery, the first poller 1452bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman sometimes waits for the already processed end of fork 1453bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman event. Doing a non blocking poll here solves the problem. */ 1454bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if (proc_poll_pipe[0] != -1) 1455bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman ioctl_result = IOCTL_STATUS (tcp); 1456bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman else 1457bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman ioctl_result = IOCTL_WSTOP (tcp); 1458bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* FREEBSD */ 145976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman ioctl_errno = errno; 146076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifndef HAVE_POLLABLE_PROCFS 146176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (proc_poll_pipe[0] != -1) { 146276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (ioctl_result < 0) 146376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman kill(poller_pid, SIGKILL); 146476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else 146576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman kill(poller_pid, SIGUSR1); 146676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 146776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !HAVE_POLLABLE_PROCFS */ 146876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 146976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (interrupted) 147076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return 0; 147176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 147276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (interactive) 147376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigprocmask(SIG_BLOCK, &blocked_set, NULL); 147476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 147576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (ioctl_result < 0) { 147676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Find out what happened if it failed. */ 147776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman switch (ioctl_errno) { 147876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case EINTR: 147976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case EBADF: 148076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 1481bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD 1482bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman case ENOTTY: 1483bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif 148476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case ENOENT: 148576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman droptcb(tcp); 148676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 148776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman default: 148876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("PIOCWSTOP"); 148976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 149076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 149176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 149276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 14932e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman#ifdef FREEBSD 14942e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman if ((tcp->flags & TCB_STARTUP) && (tcp->status.PR_WHY == PR_SYSEXIT)) { 14952e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman /* discard first event for a syscall we never entered */ 14962e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman IOCTL (tcp->pfd, PIOCRUN, 0); 14972e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman continue; 14982e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman } 14992e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman#endif 15002e4ffe59b588159eb80e236f068278ba47735932Wichert Akkerman 150176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* clear the just started flag */ 150276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->flags &= ~TCB_STARTUP; 150376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 150476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* set current output file */ 150576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman outf = tcp->outf; 150676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 150776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (cflag) { 150876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct timeval stime; 1509bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD 1510bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman char buf[1024]; 1511bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman int len; 1512bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman 1513bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) { 1514bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman buf[len] = '\0'; 1515bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman sscanf(buf, 1516bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld", 1517bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman &stime.tv_sec, &stime.tv_usec); 1518bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman } else 1519bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman stime.tv_sec = stime.tv_usec = 0; 1520bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* !FREEBSD */ 152176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman stime.tv_sec = tcp->status.pr_stime.tv_sec; 152276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000; 1523bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !FREEBSD */ 152476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tv_sub(&tcp->dtime, &stime, &tcp->stime); 152576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->stime = stime; 152676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 15279ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman what = tcp->status.PR_WHAT; 15289ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman switch (tcp->status.PR_WHY) { 1529bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD 153076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case PR_REQUESTED: 15319ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (tcp->status.PR_FLAGS & PR_ASLEEP) { 15329ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman tcp->status.PR_WHY = PR_SYSENTRY; 153376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (trace_syscall(tcp) < 0) { 153476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "syscall trouble\n"); 153576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 153676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 153776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 153876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 1539bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !FREEBSD */ 154076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case PR_SYSENTRY: 15419dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#ifdef POLL_HACK 15429dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman in_syscall = tcp; 15439dbf15466e9c178ac4090eba2c1232e2fe0706f9Wichert Akkerman#endif 154476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case PR_SYSEXIT: 154576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (trace_syscall(tcp) < 0) { 154676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "syscall trouble\n"); 154776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 154876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 154976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 155076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case PR_SIGNALLED: 155176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) { 155276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman printleader(tcp); 155376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tprintf("--- %s (%s) ---", 1554ce780fc9e6067b15b65ca2904c698c77503bf635Nate Sammons signame(what), strsignal(what)); 155576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman printtrailer(tcp); 155676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 155776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 155876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case PR_FAULTED: 155976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!cflag && (qual_flags[what] & QUAL_FAULT)) { 156076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman printleader(tcp); 156176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tprintf("=== FAULT %d ===", what); 156276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman printtrailer(tcp); 156376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 156476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 1565bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifdef FREEBSD 1566bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman case 0: /* handle case we polled for nothing */ 1567bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman continue; 1568bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif 156976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman default: 15709ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY); 157176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 157276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 157376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 15749ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman arg = 0; 1575bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#ifndef FREEBSD 15769ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) { 1577bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else 1578bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman if (IOCTL (tcp->pfd, PIOCRUN, 0) < 0) { 1579bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif 158076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("PIOCRUN"); 158176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 158276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 158376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 158476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return 0; 158576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 158676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 1587bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#else /* !USE_PROCFS */ 158876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 158976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int 159076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantrace() 159176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 159276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int pid; 159376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int wait_errno; 159476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int status; 159576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct tcb *tcp; 159676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX 159776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct rusage ru; 15982f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman#ifdef __WALL 15992f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman static int wait4_options = __WALL; 16002f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman#endif 160176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* LINUX */ 160276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 160376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman while (nprocs != 0) { 160476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (interactive) 160576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigprocmask(SIG_SETMASK, &empty_set, NULL); 160676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX 16072f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman#ifdef __WALL 16082f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman pid = wait4(-1, &status, wait4_options, cflag ? &ru : NULL); 16092f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman if ((wait4_options & __WALL) && errno == EINVAL) { 16102f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman /* this kernel does not support __WALL */ 16112f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman wait4_options &= ~__WALL; 16122f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman errno = 0; 16132f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman pid = wait4(-1, &status, wait4_options, 16142f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman cflag ? &ru : NULL); 16152f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman } 16162f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman if (!(wait4_options & __WALL) && errno == ECHILD) { 16172f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman /* most likely a "cloned" process */ 16182f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman pid = wait4(-1, &status, __WCLONE, 16192f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman cflag ? &ru : NULL); 16202f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman if (pid == -1) { 16212f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman fprintf(stderr, "strace: clone wait4 " 16222f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman "failed: %s\n", strerror(errno)); 16232f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman } 16242f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman } 16252f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman#else 162676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pid = wait4(-1, &status, 0, cflag ? &ru : NULL); 16272f1d87e74ff68004b99901f964a1cbab89da09dbWichert Akkerman#endif /* __WALL */ 162876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* LINUX */ 162976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SUNOS4 163076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pid = wait(&status); 163176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SUNOS4 */ 163276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman wait_errno = errno; 163376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (interactive) 163476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman sigprocmask(SIG_BLOCK, &blocked_set, NULL); 163576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 163676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (interrupted) 163776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return 0; 163876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 163976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (pid == -1) { 164076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman switch (wait_errno) { 164176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case EINTR: 164276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 164376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman case ECHILD: 164476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* 164576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * We would like to verify this case 164676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * but sometimes a race in Solbourne's 164776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * version of SunOS sometimes reports 164876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * ECHILD before sending us SIGCHILD. 164976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 165076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#if 0 165176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (nprocs == 0) 165276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return 0; 165376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "strace: proc miscount\n"); 165476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 165576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif 165676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return 0; 165776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman default: 165876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman errno = wait_errno; 165976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("strace: wait"); 166076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 166176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 166276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 166376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (debug) 166476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, " [wait(%#x) = %u]\n", status, pid); 166576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 166676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* Look up `pid' in our table. */ 166776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((tcp = pid2tcb(pid)) == NULL) { 1668faf722234dc8af97776f94fdda7e100fb60650a2Wichert Akkerman#if 0 /* XXX davidm */ /* WTA: disabled again */ 16698b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman struct tcb *tcpchild; 16708b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman 16718b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman if ((tcpchild = alloctcb(pid)) == NULL) { 16728b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman fprintf(stderr, " [tcb table full]\n"); 16738b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman kill(pid, SIGKILL); /* XXX */ 16748b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman return 0; 16758b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman } 16768b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman tcpchild->flags |= TCB_ATTACHED; 16778b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman newoutf(tcpchild); 16788b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman tcp->nchildren++; 16798b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman if (!qflag) 16808b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman fprintf(stderr, "Process %d attached\n", pid); 16818b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman#else 168276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "unknown pid: %u\n", pid); 168376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (WIFSTOPPED(status)) 168476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman ptrace(PTRACE_CONT, pid, (char *) 1, 0); 168576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman exit(1); 16868b1b40cd8b49151374d0ac2a5b1a4b459f9e0ae5Wichert Akkerman#endif 168776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 168876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* set current output file */ 168976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman outf = tcp->outf; 169076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (cflag) { 169176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef LINUX 169276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime); 169376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->stime = ru.ru_stime; 169476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* !LINUX */ 169576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 169676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 169776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->flags & TCB_SUSPENDED) { 169876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* 169976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Apparently, doing any ptrace() call on a stopped 170076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * process, provokes the kernel to report the process 170176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * status again on a subsequent wait(), even if the 170276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * process has not been actually restarted. 170376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Since we have inspected the arguments of suspended 170476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * processes we end up here testing for this case. 170576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 170676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 170776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 170876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (WIFSIGNALED(status)) { 170976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!cflag 171076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) { 171176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman printleader(tcp); 171276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tprintf("+++ killed by %s +++", 1713ce780fc9e6067b15b65ca2904c698c77503bf635Nate Sammons signame(WTERMSIG(status))); 171476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman printtrailer(tcp); 171576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 171676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman droptcb(tcp); 171776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 171876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 171976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (WIFEXITED(status)) { 172076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (debug) 172176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "pid %u exited\n", pid); 172276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->flags & TCB_ATTACHED) 172376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, 172476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman "PANIC: attached pid %u exited\n", 172576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pid); 172676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman droptcb(tcp); 172776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 172876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 172976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!WIFSTOPPED(status)) { 173076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "PANIC: pid %u not stopped\n", pid); 173176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman droptcb(tcp); 173276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 173376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 173476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (debug) 173576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "pid %u stopped, [%s]\n", 1736ce780fc9e6067b15b65ca2904c698c77503bf635Nate Sammons pid, signame(WSTOPSIG(status))); 173776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 173876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->flags & TCB_STARTUP) { 173976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* 174076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * This flag is there to keep us in sync. 174176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Next time this process stops it should 174276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * really be entering a system call. 174376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 174476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->flags &= ~TCB_STARTUP; 174576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->flags & TCB_ATTACHED) { 174676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* 174776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Interestingly, the process may stop 174876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * with STOPSIG equal to some other signal 174976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * than SIGSTOP if we happend to attach 175076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * just before the process takes a signal. 175176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 175276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!WIFSTOPPED(status)) { 175376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, 175476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman "pid %u not stopped\n", pid); 175576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman detach(tcp, WSTOPSIG(status)); 175676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 175776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 175876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 175976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else { 176076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef SUNOS4 176176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* A child of us stopped at exec */ 176276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (WSTOPSIG(status) == SIGTRAP && followvfork) 176376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fixvfork(tcp); 176476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif /* SUNOS4 */ 176576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 176676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->flags & TCB_BPTSET) { 176776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (clearbpt(tcp) < 0) /* Pretty fatal */ { 176876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman droptcb(tcp); 176976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman cleanup(); 177076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 177176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 177276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 177376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman goto tracing; 177476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 177576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 177676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (WSTOPSIG(status) != SIGTRAP) { 177776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (WSTOPSIG(status) == SIGSTOP && 177876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman (tcp->flags & TCB_SIGTRAPPED)) { 177976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman /* 178076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Trapped attempt to block SIGTRAP 178176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman * Hope we are back in control now. 178276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman */ 178376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED); 178476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (ptrace(PTRACE_SYSCALL, 178576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman pid, (char *) 1, 0) < 0) { 178676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("trace: ptrace(PTRACE_SYSCALL, ...)"); 178776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman cleanup(); 178876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 178976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 179076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 179176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 179276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!cflag 179376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) { 179476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman printleader(tcp); 179576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tprintf("--- %s (%s) ---", 1796ce780fc9e6067b15b65ca2904c698c77503bf635Nate Sammons signame(WSTOPSIG(status)), 179776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman strsignal(WSTOPSIG(status))); 179876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman printtrailer(tcp); 179976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 180076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((tcp->flags & TCB_ATTACHED) && 180176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman !sigishandled(tcp, WSTOPSIG(status))) { 180276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman detach(tcp, WSTOPSIG(status)); 180376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 180476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 180576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 180676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman WSTOPSIG(status)) < 0) { 180776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("trace: ptrace(PTRACE_SYSCALL, ...)"); 180876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman cleanup(); 180976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 181076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 181176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->flags &= ~TCB_SUSPENDED; 181276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 181376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 181476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (trace_syscall(tcp) < 0) { 181576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->flags & TCB_ATTACHED) 181676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman detach(tcp, 0); 181776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else { 181876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman ptrace(PTRACE_KILL, 181976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp->pid, (char *) 1, SIGTERM); 182076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman droptcb(tcp); 182176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 182276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 182376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 182476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->flags & TCB_EXITING) { 182576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->flags & TCB_ATTACHED) 182676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman detach(tcp, 0); 182776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) { 182876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("strace: ptrace(PTRACE_CONT, ...)"); 182976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman cleanup(); 183076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 183176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 183276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 183376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 183476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp->flags & TCB_SUSPENDED) { 183576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!qflag) 183676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman fprintf(stderr, "Process %u suspended\n", pid); 183776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman continue; 183876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 183976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tracing: 184076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) { 184176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman perror("trace: ptrace(PTRACE_SYSCALL, ...)"); 184276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman cleanup(); 184376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return -1; 184476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 184576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 184676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return 0; 184776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 184876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 1849bf79f2e16b090ffe59cd1e1820935680a2da7b78Wichert Akkerman#endif /* !USE_PROCFS */ 185076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 185176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstatic int curcol; 185276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 185376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef __STDC__ 185476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <stdarg.h> 185576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#define VA_START(a, b) va_start(a, b) 185676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else 185776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include <varargs.h> 185876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#define VA_START(a, b) va_start(a) 185976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif 186076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 186176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid 186276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#ifdef __STDC__ 186376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantprintf(const char *fmt, ...) 186476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#else 186576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantprintf(fmt, va_alist) 186676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanchar *fmt; 186776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanva_dcl 186876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#endif 186976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 187076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman va_list args; 187176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 187276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman VA_START(args, fmt); 187376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (outf) 187476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman curcol += vfprintf(outf, fmt, args); 187576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman va_end(args); 187676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return; 187776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 187876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 187976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid 188076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanprintleader(tcp) 188176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp; 188276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 188376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) { 188476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp_last->flags |= TCB_REPRINT; 188576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tprintf(" <unfinished ...>\n"); 188676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 188776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman curcol = 0; 188876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((followfork == 1 || pflag_seen > 1) && outfname) 188976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tprintf("%-5d ", tcp->pid); 189076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else if (nprocs > 1 && !outfname) 189176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tprintf("[pid %5u] ", tcp->pid); 189276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tflag) { 189376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman char str[sizeof("HH:MM:SS")]; 189476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman struct timeval tv, dtv; 189576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman static struct timeval otv; 189676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 189776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman gettimeofday(&tv, NULL); 189876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (rflag) { 189976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (otv.tv_sec == 0) 190076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman otv = tv; 190176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tv_sub(&dtv, &tv, &otv); 190276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tprintf("%6ld.%06ld ", 190376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman (long) dtv.tv_sec, (long) dtv.tv_usec); 190476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman otv = tv; 190576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 190676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else if (tflag > 2) { 190776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tprintf("%ld.%06ld ", 190876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman (long) tv.tv_sec, (long) tv.tv_usec); 190976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 191076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else { 191176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman time_t local = tv.tv_sec; 191276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman strftime(str, sizeof(str), "%T", localtime(&local)); 191376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (tflag > 1) 191476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tprintf("%s.%06ld ", str, (long) tv.tv_usec); 191576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman else 191676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tprintf("%s ", str); 191776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 191876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 191976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (iflag) 192076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman printcall(tcp); 192176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 192276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 192376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid 192476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermantabto(col) 192576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanint col; 192676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 192776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (curcol < col) 192876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tprintf("%*s", col - curcol, ""); 192976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 193076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 193176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanvoid 193276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanprinttrailer(tcp) 193376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct tcb *tcp; 193476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman{ 193576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tprintf("\n"); 193676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman tcp_last = NULL; 193776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 19389ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman 1939ea78f0f77185f7d6d0b2055805139d96e1be816cWichert Akkerman#ifdef HAVE_MP_PROCFS 19409ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman 19419ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkermanint mp_ioctl (int fd, int cmd, void *arg, int size) { 19429ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman 19439ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman struct iovec iov[2]; 19449ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman int n = 1; 19459ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman 19469ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman iov[0].iov_base = &cmd; 19479ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman iov[0].iov_len = sizeof cmd; 19489ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman if (arg) { 19499ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman ++n; 19509ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman iov[1].iov_base = arg; 19519ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman iov[1].iov_len = size; 19529ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman } 19539ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman 19549ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman return writev (fd, iov, n); 19559ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman} 19569ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman 19579ce1a63eb20b069607c06f9645ac5a17b418a5f3Wichert Akkerman#endif 1958