Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl> |
| 3 | * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl> |
| 4 | * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com> |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 5 | * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. The name of the author may not be used to endorse or promote products |
| 17 | * derived from this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | * |
| 30 | * $Id$ |
| 31 | */ |
| 32 | |
| 33 | #include "defs.h" |
| 34 | |
Roland McGrath | 795edb1 | 2005-02-02 04:44:57 +0000 | [diff] [blame] | 35 | #include <sys/types.h> |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 36 | #include <stdarg.h> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 37 | #include <signal.h> |
| 38 | #include <errno.h> |
| 39 | #include <sys/param.h> |
| 40 | #include <fcntl.h> |
| 41 | #include <sys/resource.h> |
| 42 | #include <sys/wait.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <pwd.h> |
| 45 | #include <grp.h> |
| 46 | #include <string.h> |
Roland McGrath | 70b0853 | 2004-04-09 00:25:21 +0000 | [diff] [blame] | 47 | #include <dirent.h> |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 48 | #include <sys/utsname.h> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 49 | |
Roland McGrath | 134813a | 2007-06-02 00:07:33 +0000 | [diff] [blame] | 50 | #ifdef LINUX |
| 51 | # include <asm/unistd.h> |
Denys Vlasenko | 44f87ef | 2011-08-17 15:18:21 +0200 | [diff] [blame] | 52 | # if defined __NR_tkill |
| 53 | # define my_tkill(tid, sig) syscall(__NR_tkill, (tid), (sig)) |
Roland McGrath | 134813a | 2007-06-02 00:07:33 +0000 | [diff] [blame] | 54 | # else |
| 55 | /* kill() may choose arbitrarily the target task of the process group |
| 56 | while we later wait on a that specific TID. PID process waits become |
| 57 | TID task specific waits for a process under ptrace(2). */ |
| 58 | # warning "Neither tkill(2) nor tgkill(2) available, risk of strace hangs!" |
Denys Vlasenko | 44f87ef | 2011-08-17 15:18:21 +0200 | [diff] [blame] | 59 | # define my_tkill(tid, sig) kill((tid), (sig)) |
Roland McGrath | 134813a | 2007-06-02 00:07:33 +0000 | [diff] [blame] | 60 | # endif |
| 61 | #endif |
| 62 | |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 63 | #if defined(IA64) && defined(LINUX) |
| 64 | # include <asm/ptrace_offsets.h> |
| 65 | #endif |
| 66 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 67 | #ifdef USE_PROCFS |
| 68 | #include <poll.h> |
| 69 | #endif |
| 70 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 71 | #ifdef SVR4 |
| 72 | #include <sys/stropts.h> |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 73 | #ifdef HAVE_MP_PROCFS |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 74 | #ifdef HAVE_SYS_UIO_H |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 75 | #include <sys/uio.h> |
| 76 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 77 | #endif |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 78 | #endif |
Denys Vlasenko | 96d5a76 | 2008-12-29 19:13:27 +0000 | [diff] [blame] | 79 | extern char **environ; |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 80 | extern int optind; |
| 81 | extern char *optarg; |
Denys Vlasenko | 96d5a76 | 2008-12-29 19:13:27 +0000 | [diff] [blame] | 82 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 83 | |
Roland McGrath | 41c4822 | 2008-07-18 00:25:10 +0000 | [diff] [blame] | 84 | int debug = 0, followfork = 0; |
Denys Vlasenko | f44cce4 | 2011-06-21 14:34:10 +0200 | [diff] [blame] | 85 | unsigned int ptrace_setoptions = 0; |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 86 | /* Which WSTOPSIG(status) value marks syscall traps? */ |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 87 | static unsigned int syscall_trap_sig = SIGTRAP; |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 88 | int dtime = 0, xflag = 0, qflag = 0; |
| 89 | cflag_t cflag = CFLAG_NONE; |
Denys Vlasenko | aa6ec41 | 2012-01-24 11:35:38 +0100 | [diff] [blame] | 90 | static int iflag = 0, pflag_seen = 0, rflag = 0, tflag = 0; |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame^] | 91 | |
| 92 | /* -I n */ |
| 93 | enum { |
| 94 | INTR_NOT_SET = 0, |
| 95 | INTR_ANYWHERE = 1, /* don't block/ignore any signals */ |
| 96 | INTR_WHILE_WAIT = 2, /* block fatal signals while decoding syscall. default */ |
| 97 | INTR_NEVER = 3, /* block fatal signals. default if '-o FILE PROG' */ |
| 98 | INTR_BLOCK_TSTP_TOO = 4, /* block fatal signals and SIGTSTP (^Z) */ |
| 99 | NUM_INTR_OPTS |
| 100 | }; |
| 101 | static int opt_intr; |
| 102 | /* We play with signal mask only if this mode is active: */ |
| 103 | #define interactive (opt_intr == INTR_WHILE_WAIT) |
| 104 | |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 105 | /* |
| 106 | * daemonized_tracer supports -D option. |
| 107 | * With this option, strace forks twice. |
| 108 | * Unlike normal case, with -D *grandparent* process exec's, |
| 109 | * becoming a traced process. Child exits (this prevents traced process |
| 110 | * from having children it doesn't expect to have), and grandchild |
| 111 | * attaches to grandparent similarly to strace -p PID. |
| 112 | * This allows for more transparent interaction in cases |
| 113 | * when process and its parent are communicating via signals, |
| 114 | * wait() etc. Without -D, strace process gets lodged in between, |
| 115 | * disrupting parent<->child link. |
| 116 | */ |
| 117 | static bool daemonized_tracer = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 118 | |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 119 | #ifdef USE_SEIZE |
| 120 | static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP; |
| 121 | # define use_seize (post_attach_sigstop == 0) |
| 122 | #else |
| 123 | # define post_attach_sigstop TCB_IGNORE_ONE_SIGSTOP |
| 124 | # define use_seize 0 |
| 125 | #endif |
| 126 | |
Michal Ludvig | 17f8fb3 | 2002-11-06 13:17:21 +0000 | [diff] [blame] | 127 | /* Sometimes we want to print only succeeding syscalls. */ |
| 128 | int not_failing_only = 0; |
| 129 | |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 130 | /* Show path associated with fd arguments */ |
| 131 | int show_fd_path = 0; |
| 132 | |
| 133 | /* are we filtering traces based on paths? */ |
| 134 | int tracing_paths = 0; |
| 135 | |
Dmitry V. Levin | a680965 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 136 | static int exit_code = 0; |
| 137 | static int strace_child = 0; |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 138 | static int strace_tracer_pid = 0; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 139 | |
Dmitry V. Levin | b9fe011 | 2006-12-13 16:59:44 +0000 | [diff] [blame] | 140 | static char *username = NULL; |
Denys Vlasenko | ead73bd | 2011-06-24 22:49:58 +0200 | [diff] [blame] | 141 | static uid_t run_uid; |
| 142 | static gid_t run_gid; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 143 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 144 | int max_strlen = DEFAULT_STRLEN; |
Denys Vlasenko | 102ec49 | 2011-08-25 01:27:59 +0200 | [diff] [blame] | 145 | static int acolumn = DEFAULT_ACOLUMN; |
| 146 | static char *acolumn_spaces; |
Dmitry V. Levin | b9fe011 | 2006-12-13 16:59:44 +0000 | [diff] [blame] | 147 | static char *outfname = NULL; |
Denys Vlasenko | ead73bd | 2011-06-24 22:49:58 +0200 | [diff] [blame] | 148 | static FILE *outf; |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 149 | struct tcb *printing_tcp = NULL; |
Andreas Schwab | ccdff48 | 2009-10-27 16:27:13 +0100 | [diff] [blame] | 150 | static int curcol; |
Denys Vlasenko | ead73bd | 2011-06-24 22:49:58 +0200 | [diff] [blame] | 151 | static struct tcb **tcbtab; |
Denys Vlasenko | 2b60c35 | 2011-06-22 12:45:25 +0200 | [diff] [blame] | 152 | static unsigned int nprocs, tcbtabsize; |
Denys Vlasenko | ead73bd | 2011-06-24 22:49:58 +0200 | [diff] [blame] | 153 | static const char *progname; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 154 | |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 155 | static char *os_release; /* from uname() */ |
| 156 | |
Denys Vlasenko | 4c19638 | 2012-01-04 15:11:09 +0100 | [diff] [blame] | 157 | static int detach(struct tcb *tcp); |
Andreas Schwab | e5355de | 2009-10-27 16:56:43 +0100 | [diff] [blame] | 158 | static int trace(void); |
| 159 | static void cleanup(void); |
| 160 | static void interrupt(int sig); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 161 | static sigset_t empty_set, blocked_set; |
| 162 | |
| 163 | #ifdef HAVE_SIG_ATOMIC_T |
| 164 | static volatile sig_atomic_t interrupted; |
Denys Vlasenko | a355925 | 2012-01-29 16:43:51 +0100 | [diff] [blame] | 165 | #else |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 166 | static volatile int interrupted; |
Denys Vlasenko | a355925 | 2012-01-29 16:43:51 +0100 | [diff] [blame] | 167 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 168 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 169 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 170 | |
Andreas Schwab | e5355de | 2009-10-27 16:56:43 +0100 | [diff] [blame] | 171 | static struct tcb *pfd2tcb(int pfd); |
| 172 | static void reaper(int sig); |
| 173 | static void rebuild_pollv(void); |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 174 | static struct pollfd *pollv; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 175 | |
| 176 | #ifndef HAVE_POLLABLE_PROCFS |
| 177 | |
Andreas Schwab | e5355de | 2009-10-27 16:56:43 +0100 | [diff] [blame] | 178 | static void proc_poll_open(void); |
| 179 | static void proc_poller(int pfd); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 180 | |
| 181 | struct proc_pollfd { |
| 182 | int fd; |
| 183 | int revents; |
| 184 | int pid; |
| 185 | }; |
| 186 | |
| 187 | static int poller_pid; |
| 188 | static int proc_poll_pipe[2] = { -1, -1 }; |
| 189 | |
| 190 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 191 | |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 192 | #ifdef HAVE_MP_PROCFS |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 193 | #define POLLWANT POLLWRNORM |
| 194 | #else |
| 195 | #define POLLWANT POLLPRI |
| 196 | #endif |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 197 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 198 | |
| 199 | static void |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 200 | usage(FILE *ofp, int exitval) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 201 | { |
| 202 | fprintf(ofp, "\ |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame^] | 203 | usage: strace [-CdDffhiqrtttTvVxxy] [-I n] [-a column] [-e expr]... [-o file]\n\ |
| 204 | [-p pid]... [-s strsize] [-u username] [-E var=val]...\n\ |
| 205 | [-P path] [PROG [ARGS]]]\n\ |
| 206 | or: strace -c [-D] [-I n] [-e expr]... [-O overhead] [-S sortby] [-E var=val]...\n\ |
| 207 | [PROG [ARGS]]]\n\ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 208 | -c -- count time, calls, and errors for each syscall and report summary\n\ |
Andreas Schwab | b87d30c | 2010-06-11 15:49:36 +0200 | [diff] [blame] | 209 | -C -- like -c but also print regular output while processes are running\n\ |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame^] | 210 | -D -- run tracer process as a detached grandchild, not as parent\n\ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 211 | -f -- follow forks, -ff -- with output into separate files\n\ |
| 212 | -F -- attempt to follow vforks, -h -- print help message\n\ |
| 213 | -i -- print instruction pointer at time of syscall\n\ |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame^] | 214 | -I interruptible\n\ |
| 215 | 1: no signals are blocked\n\ |
| 216 | 2: fatal signals are blocked while decoding syscall (default)\n\ |
| 217 | 3: fatal signals are always blocked (default if '-o FILE PROG')\n\ |
| 218 | 4: fatal signals and SIGTSTP (^Z) are always blocked\n\ |
| 219 | (useful to make 'strace -o FILE PROG' not stop on ^Z)\n\ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 220 | -q -- suppress messages about attaching, detaching, etc.\n\ |
| 221 | -r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\ |
| 222 | -T -- print time spent in each syscall, -V -- print version\n\ |
| 223 | -v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\ |
| 224 | -x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\ |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 225 | -y -- print paths associated with file descriptor arguments\n\ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 226 | -a column -- alignment COLUMN for printing syscall results (default %d)\n\ |
| 227 | -e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\ |
| 228 | options: trace, abbrev, verbose, raw, signal, read, or write\n\ |
| 229 | -o file -- send trace output to FILE instead of stderr\n\ |
| 230 | -O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\ |
| 231 | -p pid -- trace process with process id PID, may be repeated\n\ |
| 232 | -s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\ |
| 233 | -S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\ |
| 234 | -u username -- run command as username handling setuid and/or setgid\n\ |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 235 | -E var=val -- put var=val in the environment for command\n\ |
| 236 | -E var -- remove var from the environment for command\n\ |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 237 | -P path -- trace accesses to path\n\ |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 238 | " /* this is broken, so don't document it |
Michal Ludvig | 17f8fb3 | 2002-11-06 13:17:21 +0000 | [diff] [blame] | 239 | -z -- print only succeeding syscalls\n\ |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 240 | */ |
| 241 | , DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 242 | exit(exitval); |
| 243 | } |
| 244 | |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 245 | static void die(void) __attribute__ ((noreturn)); |
| 246 | static void die(void) |
| 247 | { |
| 248 | if (strace_tracer_pid == getpid()) { |
| 249 | cflag = 0; |
| 250 | cleanup(); |
| 251 | } |
| 252 | exit(1); |
| 253 | } |
| 254 | |
| 255 | static void verror_msg(int err_no, const char *fmt, va_list p) |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 256 | { |
Denys Vlasenko | 82bb78c | 2012-01-24 10:17:18 +0100 | [diff] [blame] | 257 | char *msg; |
| 258 | |
Dmitry V. Levin | 44d0532 | 2011-06-09 15:50:41 +0000 | [diff] [blame] | 259 | fflush(NULL); |
Denys Vlasenko | 82bb78c | 2012-01-24 10:17:18 +0100 | [diff] [blame] | 260 | |
| 261 | /* We want to print entire message with single fprintf to ensure |
| 262 | * message integrity if stderr is shared with other programs. |
| 263 | * Thus we use vasprintf + single fprintf. |
| 264 | */ |
| 265 | msg = NULL; |
Denys Vlasenko | cfad543 | 2012-01-24 12:48:02 +0100 | [diff] [blame] | 266 | if (vasprintf(&msg, fmt, p) >= 0) { |
Denys Vlasenko | 82bb78c | 2012-01-24 10:17:18 +0100 | [diff] [blame] | 267 | if (err_no) |
| 268 | fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(err_no)); |
| 269 | else |
| 270 | fprintf(stderr, "%s: %s\n", progname, msg); |
| 271 | free(msg); |
| 272 | } else { |
| 273 | /* malloc in vasprintf failed, try it without malloc */ |
| 274 | fprintf(stderr, "%s: ", progname); |
| 275 | vfprintf(stderr, fmt, p); |
| 276 | if (err_no) |
| 277 | fprintf(stderr, ": %s\n", strerror(err_no)); |
| 278 | else |
| 279 | putc('\n', stderr); |
| 280 | } |
| 281 | /* We don't switch stderr to buffered, thus fprintf(stderr) |
| 282 | * always flushes its output and this is not necessary: */ |
| 283 | /* fflush(stderr); */ |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 284 | } |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 285 | |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 286 | void error_msg(const char *fmt, ...) |
| 287 | { |
| 288 | va_list p; |
| 289 | va_start(p, fmt); |
| 290 | verror_msg(0, fmt, p); |
| 291 | va_end(p); |
| 292 | } |
| 293 | |
| 294 | void error_msg_and_die(const char *fmt, ...) |
| 295 | { |
| 296 | va_list p; |
| 297 | va_start(p, fmt); |
| 298 | verror_msg(0, fmt, p); |
| 299 | die(); |
| 300 | } |
| 301 | |
| 302 | void perror_msg(const char *fmt, ...) |
| 303 | { |
| 304 | va_list p; |
| 305 | va_start(p, fmt); |
| 306 | verror_msg(errno, fmt, p); |
| 307 | va_end(p); |
| 308 | } |
| 309 | |
| 310 | void perror_msg_and_die(const char *fmt, ...) |
| 311 | { |
| 312 | va_list p; |
| 313 | va_start(p, fmt); |
| 314 | verror_msg(errno, fmt, p); |
| 315 | die(); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 316 | } |
| 317 | |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 318 | void die_out_of_memory(void) |
| 319 | { |
| 320 | static bool recursed = 0; |
| 321 | if (recursed) |
| 322 | exit(1); |
| 323 | recursed = 1; |
| 324 | error_msg_and_die("Out of memory"); |
| 325 | } |
| 326 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 327 | #ifdef SVR4 |
| 328 | #ifdef MIPS |
| 329 | void |
| 330 | foobar() |
| 331 | { |
| 332 | } |
| 333 | #endif /* MIPS */ |
| 334 | #endif /* SVR4 */ |
| 335 | |
Mike Frysinger | c1a5b7e | 2009-10-07 20:41:29 -0400 | [diff] [blame] | 336 | /* Glue for systems without a MMU that cannot provide fork() */ |
| 337 | #ifdef HAVE_FORK |
| 338 | # define strace_vforked 0 |
| 339 | #else |
| 340 | # define strace_vforked 1 |
| 341 | # define fork() vfork() |
| 342 | #endif |
| 343 | |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 344 | #ifdef USE_SEIZE |
| 345 | static int |
| 346 | ptrace_attach_or_seize(int pid) |
| 347 | { |
| 348 | int r; |
| 349 | if (!use_seize) |
| 350 | return ptrace(PTRACE_ATTACH, pid, 0, 0); |
| 351 | r = ptrace(PTRACE_SEIZE, pid, 0, PTRACE_SEIZE_DEVEL); |
| 352 | if (r) |
| 353 | return r; |
| 354 | r = ptrace(PTRACE_INTERRUPT, pid, 0, 0); |
| 355 | return r; |
| 356 | } |
| 357 | #else |
| 358 | # define ptrace_attach_or_seize(pid) ptrace(PTRACE_ATTACH, (pid), 0, 0) |
| 359 | #endif |
| 360 | |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 361 | static void |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 362 | set_cloexec_flag(int fd) |
| 363 | { |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 364 | int flags, newflags; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 365 | |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 366 | flags = fcntl(fd, F_GETFD); |
| 367 | if (flags < 0) { |
| 368 | /* Can happen only if fd is bad. |
| 369 | * Should never happen: if it does, we have a bug |
| 370 | * in the caller. Therefore we just abort |
| 371 | * instead of propagating the error. |
| 372 | */ |
| 373 | perror_msg_and_die("fcntl(%d, F_GETFD)", fd); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | newflags = flags | FD_CLOEXEC; |
| 377 | if (flags == newflags) |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 378 | return; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 379 | |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 380 | fcntl(fd, F_SETFD, newflags); /* never fails */ |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /* |
| 384 | * When strace is setuid executable, we have to swap uids |
| 385 | * before and after filesystem and process management operations. |
| 386 | */ |
| 387 | static void |
| 388 | swap_uid(void) |
| 389 | { |
| 390 | #ifndef SVR4 |
| 391 | int euid = geteuid(), uid = getuid(); |
| 392 | |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 393 | if (euid != uid && setreuid(euid, uid) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 394 | perror_msg_and_die("setreuid"); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 395 | } |
| 396 | #endif |
| 397 | } |
| 398 | |
Roland McGrath | 4bfa626 | 2007-07-05 20:03:16 +0000 | [diff] [blame] | 399 | #if _LFS64_LARGEFILE |
| 400 | # define fopen_for_output fopen64 |
| 401 | #else |
| 402 | # define fopen_for_output fopen |
| 403 | #endif |
| 404 | |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 405 | static FILE * |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 406 | strace_fopen(const char *path) |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 407 | { |
| 408 | FILE *fp; |
| 409 | |
| 410 | swap_uid(); |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 411 | fp = fopen_for_output(path, "w"); |
| 412 | if (!fp) |
| 413 | perror_msg_and_die("Can't fopen '%s'", path); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 414 | swap_uid(); |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 415 | set_cloexec_flag(fileno(fp)); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 416 | return fp; |
| 417 | } |
| 418 | |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 419 | static int popen_pid = 0; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 420 | |
| 421 | #ifndef _PATH_BSHELL |
| 422 | # define _PATH_BSHELL "/bin/sh" |
| 423 | #endif |
| 424 | |
| 425 | /* |
| 426 | * We cannot use standard popen(3) here because we have to distinguish |
| 427 | * popen child process from other processes we trace, and standard popen(3) |
| 428 | * does not export its child's pid. |
| 429 | */ |
| 430 | static FILE * |
| 431 | strace_popen(const char *command) |
| 432 | { |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 433 | FILE *fp; |
| 434 | int fds[2]; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 435 | |
| 436 | swap_uid(); |
| 437 | if (pipe(fds) < 0) |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 438 | perror_msg_and_die("pipe"); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 439 | |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 440 | set_cloexec_flag(fds[1]); /* never fails */ |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 441 | |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 442 | popen_pid = vfork(); |
| 443 | if (popen_pid == -1) |
| 444 | perror_msg_and_die("vfork"); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 445 | |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 446 | if (popen_pid == 0) { |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 447 | /* child */ |
| 448 | close(fds[1]); |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 449 | if (fds[0] != 0) { |
| 450 | if (dup2(fds[0], 0)) |
| 451 | perror_msg_and_die("dup2"); |
| 452 | close(fds[0]); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 453 | } |
| 454 | execl(_PATH_BSHELL, "sh", "-c", command, NULL); |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 455 | perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 456 | } |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 457 | |
| 458 | /* parent */ |
| 459 | close(fds[0]); |
| 460 | swap_uid(); |
| 461 | fp = fdopen(fds[1], "w"); |
| 462 | if (!fp) |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 463 | die_out_of_memory(); |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 464 | return fp; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 467 | static void |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 468 | newoutf(struct tcb *tcp) |
| 469 | { |
| 470 | if (outfname && followfork > 1) { |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 471 | char name[520 + sizeof(int) * 3]; |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 472 | sprintf(name, "%.512s.%u", outfname, tcp->pid); |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 473 | tcp->outf = strace_fopen(name); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 474 | } |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 477 | static void |
| 478 | startup_attach(void) |
| 479 | { |
| 480 | int tcbi; |
| 481 | struct tcb *tcp; |
| 482 | |
| 483 | /* |
| 484 | * Block user interruptions as we would leave the traced |
| 485 | * process stopped (process state T) if we would terminate in |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 486 | * between PTRACE_ATTACH and wait4() on SIGSTOP. |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 487 | * We rely on cleanup() from this point on. |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 488 | */ |
| 489 | if (interactive) |
| 490 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
| 491 | |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 492 | if (daemonized_tracer) { |
| 493 | pid_t pid = fork(); |
| 494 | if (pid < 0) { |
Denys Vlasenko | 014ca3a | 2011-09-02 16:19:30 +0200 | [diff] [blame] | 495 | perror_msg_and_die("fork"); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 496 | } |
| 497 | if (pid) { /* parent */ |
| 498 | /* |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 499 | * Wait for grandchild to attach to straced process |
| 500 | * (grandparent). Grandchild SIGKILLs us after it attached. |
| 501 | * Grandparent's wait() is unblocked by our death, |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 502 | * it proceeds to exec the straced program. |
| 503 | */ |
| 504 | pause(); |
| 505 | _exit(0); /* paranoia */ |
| 506 | } |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 507 | /* grandchild */ |
| 508 | /* We will be the tracer process. Remember our new pid: */ |
| 509 | strace_tracer_pid = getpid(); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 512 | for (tcbi = 0; tcbi < tcbtabsize; tcbi++) { |
| 513 | tcp = tcbtab[tcbi]; |
Denys Vlasenko | 44f87ef | 2011-08-17 15:18:21 +0200 | [diff] [blame] | 514 | |
Denys Vlasenko | d116a73 | 2011-09-05 14:01:33 +0200 | [diff] [blame] | 515 | /* Is this a process we should attach to, but not yet attached? */ |
| 516 | if ((tcp->flags & (TCB_ATTACHED | TCB_STARTUP)) != TCB_ATTACHED) |
| 517 | continue; /* no */ |
| 518 | |
| 519 | /* Reinitialize the output since it may have changed */ |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 520 | tcp->outf = outf; |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 521 | newoutf(tcp); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 522 | |
| 523 | #ifdef USE_PROCFS |
| 524 | if (proc_open(tcp, 1) < 0) { |
| 525 | fprintf(stderr, "trouble opening proc file\n"); |
| 526 | droptcb(tcp); |
| 527 | continue; |
| 528 | } |
| 529 | #else /* !USE_PROCFS */ |
| 530 | # ifdef LINUX |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 531 | if (followfork && !daemonized_tracer) { |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 532 | char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3]; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 533 | DIR *dir; |
| 534 | |
| 535 | sprintf(procdir, "/proc/%d/task", tcp->pid); |
| 536 | dir = opendir(procdir); |
| 537 | if (dir != NULL) { |
| 538 | unsigned int ntid = 0, nerr = 0; |
| 539 | struct dirent *de; |
Denys Vlasenko | 381dbc2 | 2011-09-05 13:59:39 +0200 | [diff] [blame] | 540 | |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 541 | while ((de = readdir(dir)) != NULL) { |
Denys Vlasenko | 381dbc2 | 2011-09-05 13:59:39 +0200 | [diff] [blame] | 542 | struct tcb *cur_tcp; |
| 543 | int tid; |
| 544 | |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 545 | if (de->d_fileno == 0) |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 546 | continue; |
| 547 | tid = atoi(de->d_name); |
| 548 | if (tid <= 0) |
| 549 | continue; |
| 550 | ++ntid; |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 551 | if (ptrace_attach_or_seize(tid) < 0) { |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 552 | ++nerr; |
Denys Vlasenko | f95397a | 2011-06-24 16:51:16 +0200 | [diff] [blame] | 553 | if (debug) |
| 554 | fprintf(stderr, "attach to pid %d failed\n", tid); |
Denys Vlasenko | 381dbc2 | 2011-09-05 13:59:39 +0200 | [diff] [blame] | 555 | continue; |
Denys Vlasenko | f95397a | 2011-06-24 16:51:16 +0200 | [diff] [blame] | 556 | } |
Denys Vlasenko | 381dbc2 | 2011-09-05 13:59:39 +0200 | [diff] [blame] | 557 | if (debug) |
| 558 | fprintf(stderr, "attach to pid %d succeeded\n", tid); |
| 559 | cur_tcp = tcp; |
| 560 | if (tid != tcp->pid) |
| 561 | cur_tcp = alloctcb(tid); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 562 | cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 563 | } |
| 564 | closedir(dir); |
Denys Vlasenko | 381dbc2 | 2011-09-05 13:59:39 +0200 | [diff] [blame] | 565 | if (interactive) { |
| 566 | sigprocmask(SIG_SETMASK, &empty_set, NULL); |
| 567 | if (interrupted) |
| 568 | goto ret; |
| 569 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
| 570 | } |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 571 | ntid -= nerr; |
| 572 | if (ntid == 0) { |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 573 | perror("attach: ptrace(PTRACE_ATTACH, ...)"); |
| 574 | droptcb(tcp); |
| 575 | continue; |
| 576 | } |
| 577 | if (!qflag) { |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 578 | fprintf(stderr, ntid > 1 |
| 579 | ? "Process %u attached with %u threads - interrupt to quit\n" |
| 580 | : "Process %u attached - interrupt to quit\n", |
Denys Vlasenko | 44f87ef | 2011-08-17 15:18:21 +0200 | [diff] [blame] | 581 | tcp->pid, ntid); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 582 | } |
Denys Vlasenko | f88837a | 2011-09-05 14:05:46 +0200 | [diff] [blame] | 583 | if (!(tcp->flags & TCB_STARTUP)) { |
| 584 | /* -p PID, we failed to attach to PID itself |
| 585 | * but did attach to some of its sibling threads. |
| 586 | * Drop PID's tcp. |
| 587 | */ |
| 588 | droptcb(tcp); |
| 589 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 590 | continue; |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 591 | } /* if (opendir worked) */ |
| 592 | } /* if (-f) */ |
Denys Vlasenko | 44f87ef | 2011-08-17 15:18:21 +0200 | [diff] [blame] | 593 | # endif /* LINUX */ |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 594 | if (ptrace_attach_or_seize(tcp->pid) < 0) { |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 595 | perror("attach: ptrace(PTRACE_ATTACH, ...)"); |
| 596 | droptcb(tcp); |
| 597 | continue; |
| 598 | } |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 599 | tcp->flags |= TCB_STARTUP | post_attach_sigstop; |
Denys Vlasenko | f95397a | 2011-06-24 16:51:16 +0200 | [diff] [blame] | 600 | if (debug) |
| 601 | fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 602 | |
| 603 | if (daemonized_tracer) { |
| 604 | /* |
| 605 | * It is our grandparent we trace, not a -p PID. |
| 606 | * Don't want to just detach on exit, so... |
| 607 | */ |
| 608 | tcp->flags &= ~TCB_ATTACHED; |
| 609 | /* |
| 610 | * Make parent go away. |
| 611 | * Also makes grandparent's wait() unblock. |
| 612 | */ |
| 613 | kill(getppid(), SIGKILL); |
| 614 | } |
| 615 | |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 616 | #endif /* !USE_PROCFS */ |
| 617 | if (!qflag) |
| 618 | fprintf(stderr, |
| 619 | "Process %u attached - interrupt to quit\n", |
| 620 | tcp->pid); |
Denys Vlasenko | f95397a | 2011-06-24 16:51:16 +0200 | [diff] [blame] | 621 | } /* for each tcbtab[] */ |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 622 | |
Denys Vlasenko | 44f87ef | 2011-08-17 15:18:21 +0200 | [diff] [blame] | 623 | ret: |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 624 | if (interactive) |
| 625 | sigprocmask(SIG_SETMASK, &empty_set, NULL); |
| 626 | } |
| 627 | |
| 628 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 629 | startup_child(char **argv) |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 630 | { |
| 631 | struct stat statbuf; |
| 632 | const char *filename; |
| 633 | char pathname[MAXPATHLEN]; |
| 634 | int pid = 0; |
| 635 | struct tcb *tcp; |
| 636 | |
| 637 | filename = argv[0]; |
| 638 | if (strchr(filename, '/')) { |
| 639 | if (strlen(filename) > sizeof pathname - 1) { |
| 640 | errno = ENAMETOOLONG; |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 641 | perror_msg_and_die("exec"); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 642 | } |
| 643 | strcpy(pathname, filename); |
| 644 | } |
| 645 | #ifdef USE_DEBUGGING_EXEC |
| 646 | /* |
| 647 | * Debuggers customarily check the current directory |
| 648 | * first regardless of the path but doing that gives |
| 649 | * security geeks a panic attack. |
| 650 | */ |
| 651 | else if (stat(filename, &statbuf) == 0) |
| 652 | strcpy(pathname, filename); |
| 653 | #endif /* USE_DEBUGGING_EXEC */ |
| 654 | else { |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 655 | const char *path; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 656 | int m, n, len; |
| 657 | |
| 658 | for (path = getenv("PATH"); path && *path; path += m) { |
| 659 | if (strchr(path, ':')) { |
| 660 | n = strchr(path, ':') - path; |
| 661 | m = n + 1; |
| 662 | } |
| 663 | else |
| 664 | m = n = strlen(path); |
| 665 | if (n == 0) { |
| 666 | if (!getcwd(pathname, MAXPATHLEN)) |
| 667 | continue; |
| 668 | len = strlen(pathname); |
| 669 | } |
| 670 | else if (n > sizeof pathname - 1) |
| 671 | continue; |
| 672 | else { |
| 673 | strncpy(pathname, path, n); |
| 674 | len = n; |
| 675 | } |
| 676 | if (len && pathname[len - 1] != '/') |
| 677 | pathname[len++] = '/'; |
| 678 | strcpy(pathname + len, filename); |
| 679 | if (stat(pathname, &statbuf) == 0 && |
| 680 | /* Accept only regular files |
| 681 | with some execute bits set. |
| 682 | XXX not perfect, might still fail */ |
| 683 | S_ISREG(statbuf.st_mode) && |
| 684 | (statbuf.st_mode & 0111)) |
| 685 | break; |
| 686 | } |
| 687 | } |
| 688 | if (stat(pathname, &statbuf) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 689 | perror_msg_and_die("Can't stat '%s'", filename); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 690 | } |
Dmitry V. Levin | a680965 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 691 | strace_child = pid = fork(); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 692 | if (pid < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 693 | perror_msg_and_die("fork"); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 694 | } |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 695 | if ((pid != 0 && daemonized_tracer) /* -D: parent to become a traced process */ |
| 696 | || (pid == 0 && !daemonized_tracer) /* not -D: child to become a traced process */ |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 697 | ) { |
| 698 | pid = getpid(); |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 699 | if (outf != stderr) |
| 700 | close(fileno(outf)); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 701 | #ifdef USE_PROCFS |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 702 | # ifdef MIPS |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 703 | /* Kludge for SGI, see proc_open for details. */ |
| 704 | sa.sa_handler = foobar; |
| 705 | sa.sa_flags = 0; |
| 706 | sigemptyset(&sa.sa_mask); |
| 707 | sigaction(SIGINT, &sa, NULL); |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 708 | # endif |
| 709 | # ifndef FREEBSD |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 710 | pause(); |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 711 | # else |
Denys Vlasenko | 014ca3a | 2011-09-02 16:19:30 +0200 | [diff] [blame] | 712 | kill(pid, SIGSTOP); |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 713 | # endif |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 714 | #else /* !USE_PROCFS */ |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 715 | if (!daemonized_tracer && !use_seize) { |
| 716 | if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 717 | perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)"); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 718 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 719 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 720 | |
Denys Vlasenko | ab034fb | 2011-09-01 10:27:42 +0200 | [diff] [blame] | 721 | if (username != NULL) { |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 722 | uid_t run_euid = run_uid; |
| 723 | gid_t run_egid = run_gid; |
| 724 | |
| 725 | if (statbuf.st_mode & S_ISUID) |
| 726 | run_euid = statbuf.st_uid; |
| 727 | if (statbuf.st_mode & S_ISGID) |
| 728 | run_egid = statbuf.st_gid; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 729 | /* |
| 730 | * It is important to set groups before we |
| 731 | * lose privileges on setuid. |
| 732 | */ |
Denys Vlasenko | ab034fb | 2011-09-01 10:27:42 +0200 | [diff] [blame] | 733 | if (initgroups(username, run_gid) < 0) { |
| 734 | perror_msg_and_die("initgroups"); |
| 735 | } |
| 736 | if (setregid(run_gid, run_egid) < 0) { |
| 737 | perror_msg_and_die("setregid"); |
| 738 | } |
| 739 | if (setreuid(run_uid, run_euid) < 0) { |
| 740 | perror_msg_and_die("setreuid"); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 741 | } |
| 742 | } |
Denys Vlasenko | ab034fb | 2011-09-01 10:27:42 +0200 | [diff] [blame] | 743 | else if (geteuid() != 0) |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 744 | setreuid(run_uid, run_uid); |
| 745 | |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 746 | if (!daemonized_tracer) { |
| 747 | /* |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 748 | * Induce a ptrace stop. Tracer (our parent) |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 749 | * will resume us with PTRACE_SYSCALL and display |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 750 | * the immediately following execve syscall. |
| 751 | * Can't do this on NOMMU systems, we are after |
| 752 | * vfork: parent is blocked, stopping would deadlock. |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 753 | */ |
Mike Frysinger | c1a5b7e | 2009-10-07 20:41:29 -0400 | [diff] [blame] | 754 | if (!strace_vforked) |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 755 | kill(pid, SIGSTOP); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 756 | } else { |
| 757 | struct sigaction sv_sigchld; |
| 758 | sigaction(SIGCHLD, NULL, &sv_sigchld); |
| 759 | /* |
| 760 | * Make sure it is not SIG_IGN, otherwise wait |
| 761 | * will not block. |
| 762 | */ |
| 763 | signal(SIGCHLD, SIG_DFL); |
| 764 | /* |
| 765 | * Wait for grandchild to attach to us. |
| 766 | * It kills child after that, and wait() unblocks. |
| 767 | */ |
| 768 | alarm(3); |
| 769 | wait(NULL); |
| 770 | alarm(0); |
| 771 | sigaction(SIGCHLD, &sv_sigchld, NULL); |
| 772 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 773 | #endif /* !USE_PROCFS */ |
| 774 | |
| 775 | execv(pathname, argv); |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 776 | perror_msg_and_die("exec"); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 777 | } |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 778 | |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 779 | /* We are the tracer */ |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 780 | |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 781 | if (!daemonized_tracer) { |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 782 | if (!use_seize) { |
| 783 | /* child did PTRACE_TRACEME, nothing to do in parent */ |
| 784 | } else { |
| 785 | if (!strace_vforked) { |
| 786 | /* Wait until child stopped itself */ |
| 787 | int status; |
| 788 | while (waitpid(pid, &status, WSTOPPED) < 0) { |
| 789 | if (errno == EINTR) |
| 790 | continue; |
| 791 | perror_msg_and_die("waitpid"); |
| 792 | } |
| 793 | if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) { |
| 794 | kill(pid, SIGKILL); |
| 795 | perror_msg_and_die("Unexpected wait status %x", status); |
| 796 | } |
| 797 | } |
| 798 | /* Else: vforked case, we have no way to sync. |
| 799 | * Just attach to it as soon as possible. |
| 800 | * This means that we may miss a few first syscalls... |
| 801 | */ |
| 802 | |
| 803 | if (ptrace_attach_or_seize(pid)) { |
| 804 | kill(pid, SIGKILL); |
| 805 | perror_msg_and_die("Can't attach to %d", pid); |
| 806 | } |
| 807 | if (!strace_vforked) |
| 808 | kill(pid, SIGCONT); |
| 809 | } |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 810 | tcp = alloctcb(pid); |
Denys Vlasenko | f88837a | 2011-09-05 14:05:46 +0200 | [diff] [blame] | 811 | if (!strace_vforked) |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 812 | tcp->flags |= TCB_STARTUP | post_attach_sigstop; |
Denys Vlasenko | f88837a | 2011-09-05 14:05:46 +0200 | [diff] [blame] | 813 | else |
| 814 | tcp->flags |= TCB_STARTUP; |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 815 | } |
| 816 | else { |
| 817 | /* With -D, *we* are child here, IOW: different pid. Fetch it: */ |
| 818 | strace_tracer_pid = getpid(); |
| 819 | /* The tracee is our parent: */ |
| 820 | pid = getppid(); |
| 821 | tcp = alloctcb(pid); |
| 822 | /* We want subsequent startup_attach() to attach to it: */ |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 823 | tcp->flags |= TCB_ATTACHED; |
| 824 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 825 | #ifdef USE_PROCFS |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 826 | if (proc_open(tcp, 0) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 827 | perror_msg_and_die("trouble opening proc file"); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 828 | } |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 829 | #endif |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 832 | #ifdef LINUX |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 833 | static void kill_save_errno(pid_t pid, int sig) |
| 834 | { |
| 835 | int saved_errno = errno; |
| 836 | |
| 837 | (void) kill(pid, sig); |
| 838 | errno = saved_errno; |
| 839 | } |
| 840 | |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 841 | /* |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 842 | * Test whether the kernel support PTRACE_O_TRACECLONE et al options. |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 843 | * First fork a new child, call ptrace with PTRACE_SETOPTIONS on it, |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 844 | * and then see which options are supported by the kernel. |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 845 | */ |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 846 | static void |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 847 | test_ptrace_setoptions_followfork(void) |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 848 | { |
Dmitry V. Levin | 2fabd0e | 2011-02-19 21:33:50 +0000 | [diff] [blame] | 849 | int pid, expected_grandchild = 0, found_grandchild = 0; |
| 850 | const unsigned int test_options = PTRACE_O_TRACECLONE | |
| 851 | PTRACE_O_TRACEFORK | |
| 852 | PTRACE_O_TRACEVFORK; |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 853 | |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 854 | pid = fork(); |
| 855 | if (pid < 0) |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 856 | perror_msg_and_die("fork"); |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 857 | if (pid == 0) { |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 858 | pid = getpid(); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 859 | if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 860 | perror_msg_and_die("%s: PTRACE_TRACEME doesn't work", |
| 861 | __func__); |
| 862 | kill(pid, SIGSTOP); |
| 863 | if (fork() < 0) |
| 864 | perror_msg_and_die("fork"); |
| 865 | _exit(0); |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 866 | } |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 867 | |
| 868 | while (1) { |
| 869 | int status, tracee_pid; |
| 870 | |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 871 | errno = 0; |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 872 | tracee_pid = wait(&status); |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 873 | if (tracee_pid <= 0) { |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 874 | if (errno == EINTR) |
| 875 | continue; |
| 876 | else if (errno == ECHILD) |
| 877 | break; |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 878 | kill_save_errno(pid, SIGKILL); |
| 879 | perror_msg_and_die("%s: unexpected wait result %d", |
| 880 | __func__, tracee_pid); |
| 881 | } |
| 882 | if (WIFEXITED(status)) { |
| 883 | if (WEXITSTATUS(status)) { |
| 884 | if (tracee_pid != pid) |
| 885 | kill_save_errno(pid, SIGKILL); |
| 886 | error_msg_and_die("%s: unexpected exit status %u", |
| 887 | __func__, WEXITSTATUS(status)); |
| 888 | } |
| 889 | continue; |
| 890 | } |
| 891 | if (WIFSIGNALED(status)) { |
| 892 | if (tracee_pid != pid) |
| 893 | kill_save_errno(pid, SIGKILL); |
| 894 | error_msg_and_die("%s: unexpected signal %u", |
| 895 | __func__, WTERMSIG(status)); |
| 896 | } |
| 897 | if (!WIFSTOPPED(status)) { |
| 898 | if (tracee_pid != pid) |
| 899 | kill_save_errno(tracee_pid, SIGKILL); |
| 900 | kill(pid, SIGKILL); |
| 901 | error_msg_and_die("%s: unexpected wait status %x", |
| 902 | __func__, status); |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 903 | } |
| 904 | if (tracee_pid != pid) { |
Dmitry V. Levin | 2fabd0e | 2011-02-19 21:33:50 +0000 | [diff] [blame] | 905 | found_grandchild = tracee_pid; |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 906 | if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0) { |
| 907 | kill_save_errno(tracee_pid, SIGKILL); |
| 908 | kill_save_errno(pid, SIGKILL); |
| 909 | perror_msg_and_die("PTRACE_CONT doesn't work"); |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 910 | } |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 911 | continue; |
| 912 | } |
| 913 | switch (WSTOPSIG(status)) { |
| 914 | case SIGSTOP: |
| 915 | if (ptrace(PTRACE_SETOPTIONS, pid, 0, test_options) < 0 |
| 916 | && errno != EINVAL && errno != EIO) |
| 917 | perror_msg("PTRACE_SETOPTIONS"); |
| 918 | break; |
| 919 | case SIGTRAP: |
| 920 | if (status >> 16 == PTRACE_EVENT_FORK) { |
| 921 | long msg = 0; |
| 922 | |
| 923 | if (ptrace(PTRACE_GETEVENTMSG, pid, |
| 924 | NULL, (long) &msg) == 0) |
| 925 | expected_grandchild = msg; |
| 926 | } |
| 927 | break; |
| 928 | } |
| 929 | if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) { |
| 930 | kill_save_errno(pid, SIGKILL); |
| 931 | perror_msg_and_die("PTRACE_SYSCALL doesn't work"); |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 932 | } |
| 933 | } |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 934 | if (expected_grandchild && expected_grandchild == found_grandchild) { |
Denys Vlasenko | f44cce4 | 2011-06-21 14:34:10 +0200 | [diff] [blame] | 935 | ptrace_setoptions |= test_options; |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 936 | if (debug) |
| 937 | fprintf(stderr, "ptrace_setoptions = %#x\n", |
| 938 | ptrace_setoptions); |
| 939 | return; |
| 940 | } |
| 941 | error_msg("Test for PTRACE_O_TRACECLONE failed, " |
| 942 | "giving up using this feature."); |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 943 | } |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 944 | |
| 945 | /* |
| 946 | * Test whether the kernel support PTRACE_O_TRACESYSGOOD. |
| 947 | * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it, |
| 948 | * and then see whether it will stop with (SIGTRAP | 0x80). |
| 949 | * |
| 950 | * Use of this option enables correct handling of user-generated SIGTRAPs, |
| 951 | * and SIGTRAPs generated by special instructions such as int3 on x86: |
| 952 | * _start: .globl _start |
| 953 | * int3 |
| 954 | * movl $42, %ebx |
| 955 | * movl $1, %eax |
| 956 | * int $0x80 |
| 957 | * (compile with: "gcc -nostartfiles -nostdlib -o int3 int3.S") |
| 958 | */ |
| 959 | static void |
| 960 | test_ptrace_setoptions_for_all(void) |
| 961 | { |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 962 | const unsigned int test_options = PTRACE_O_TRACESYSGOOD | |
| 963 | PTRACE_O_TRACEEXEC; |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 964 | int pid; |
| 965 | int it_worked = 0; |
| 966 | |
| 967 | pid = fork(); |
| 968 | if (pid < 0) |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 969 | perror_msg_and_die("fork"); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 970 | |
| 971 | if (pid == 0) { |
| 972 | pid = getpid(); |
| 973 | if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 974 | /* Note: exits with exitcode 1 */ |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 975 | perror_msg_and_die("%s: PTRACE_TRACEME doesn't work", |
| 976 | __func__); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 977 | kill(pid, SIGSTOP); |
| 978 | _exit(0); /* parent should see entry into this syscall */ |
| 979 | } |
| 980 | |
| 981 | while (1) { |
| 982 | int status, tracee_pid; |
| 983 | |
| 984 | errno = 0; |
| 985 | tracee_pid = wait(&status); |
| 986 | if (tracee_pid <= 0) { |
| 987 | if (errno == EINTR) |
| 988 | continue; |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 989 | kill_save_errno(pid, SIGKILL); |
| 990 | perror_msg_and_die("%s: unexpected wait result %d", |
| 991 | __func__, tracee_pid); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 992 | } |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 993 | if (WIFEXITED(status)) { |
| 994 | if (WEXITSTATUS(status) == 0) |
| 995 | break; |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 996 | error_msg_and_die("%s: unexpected exit status %u", |
| 997 | __func__, WEXITSTATUS(status)); |
| 998 | } |
| 999 | if (WIFSIGNALED(status)) { |
| 1000 | error_msg_and_die("%s: unexpected signal %u", |
| 1001 | __func__, WTERMSIG(status)); |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1002 | } |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1003 | if (!WIFSTOPPED(status)) { |
| 1004 | kill(pid, SIGKILL); |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 1005 | error_msg_and_die("%s: unexpected wait status %x", |
| 1006 | __func__, status); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1007 | } |
| 1008 | if (WSTOPSIG(status) == SIGSTOP) { |
| 1009 | /* |
| 1010 | * We don't check "options aren't accepted" error. |
| 1011 | * If it happens, we'll never get (SIGTRAP | 0x80), |
| 1012 | * and thus will decide to not use the option. |
| 1013 | * IOW: the outcome of the test will be correct. |
| 1014 | */ |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 1015 | if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0 |
| 1016 | && errno != EINVAL && errno != EIO) |
| 1017 | perror_msg("PTRACE_SETOPTIONS"); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1018 | } |
| 1019 | if (WSTOPSIG(status) == (SIGTRAP | 0x80)) { |
| 1020 | it_worked = 1; |
| 1021 | } |
| 1022 | if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) { |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 1023 | kill_save_errno(pid, SIGKILL); |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1024 | perror_msg_and_die("PTRACE_SYSCALL doesn't work"); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | if (it_worked) { |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1029 | syscall_trap_sig = (SIGTRAP | 0x80); |
Denys Vlasenko | f44cce4 | 2011-06-21 14:34:10 +0200 | [diff] [blame] | 1030 | ptrace_setoptions |= test_options; |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1031 | if (debug) |
Denys Vlasenko | f44cce4 | 2011-06-21 14:34:10 +0200 | [diff] [blame] | 1032 | fprintf(stderr, "ptrace_setoptions = %#x\n", |
| 1033 | ptrace_setoptions); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1034 | return; |
| 1035 | } |
| 1036 | |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 1037 | error_msg("Test for PTRACE_O_TRACESYSGOOD failed, " |
| 1038 | "giving up using this feature."); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1039 | } |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1040 | |
| 1041 | # ifdef USE_SEIZE |
| 1042 | static void |
| 1043 | test_ptrace_seize(void) |
| 1044 | { |
| 1045 | int pid; |
| 1046 | |
| 1047 | pid = fork(); |
| 1048 | if (pid < 0) |
| 1049 | perror_msg_and_die("fork"); |
| 1050 | |
| 1051 | if (pid == 0) { |
| 1052 | pause(); |
| 1053 | _exit(0); |
| 1054 | } |
| 1055 | |
| 1056 | /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After |
| 1057 | * attaching tracee continues to run unless a trap condition occurs. |
| 1058 | * PTRACE_SEIZE doesn't affect signal or group stop state. |
| 1059 | */ |
| 1060 | if (ptrace(PTRACE_SEIZE, pid, 0, PTRACE_SEIZE_DEVEL) == 0) { |
| 1061 | post_attach_sigstop = 0; /* this sets use_seize to 1 */ |
| 1062 | } else if (debug) { |
| 1063 | fprintf(stderr, "PTRACE_SEIZE doesn't work\n"); |
| 1064 | } |
| 1065 | |
| 1066 | kill(pid, SIGKILL); |
| 1067 | |
| 1068 | while (1) { |
| 1069 | int status, tracee_pid; |
| 1070 | |
| 1071 | errno = 0; |
| 1072 | tracee_pid = waitpid(pid, &status, 0); |
| 1073 | if (tracee_pid <= 0) { |
| 1074 | if (errno == EINTR) |
| 1075 | continue; |
| 1076 | perror_msg_and_die("%s: unexpected wait result %d", |
| 1077 | __func__, tracee_pid); |
| 1078 | } |
| 1079 | if (WIFSIGNALED(status)) { |
| 1080 | return; |
| 1081 | } |
| 1082 | error_msg_and_die("%s: unexpected wait status %x", |
| 1083 | __func__, status); |
| 1084 | } |
| 1085 | } |
| 1086 | # else /* !USE_SEIZE */ |
| 1087 | # define test_ptrace_seize() ((void)0) |
| 1088 | # endif |
| 1089 | |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 1090 | #endif |
| 1091 | |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1092 | /* Noinline: don't want main to have struct utsname permanently on stack */ |
| 1093 | static void __attribute__ ((noinline)) |
| 1094 | get_os_release(void) |
| 1095 | { |
| 1096 | struct utsname u; |
| 1097 | if (uname(&u) < 0) |
| 1098 | perror_msg_and_die("uname"); |
| 1099 | os_release = strdup(u.release); |
| 1100 | if (!os_release) |
| 1101 | die_out_of_memory(); |
| 1102 | } |
| 1103 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1104 | int |
Dmitry V. Levin | 08b623e | 2007-10-08 21:04:41 +0000 | [diff] [blame] | 1105 | main(int argc, char *argv[]) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1106 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1107 | struct tcb *tcp; |
| 1108 | int c, pid = 0; |
Dmitry V. Levin | 06350db | 2008-07-25 15:42:34 +0000 | [diff] [blame] | 1109 | int optF = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1110 | struct sigaction sa; |
| 1111 | |
Dmitry V. Levin | 08b623e | 2007-10-08 21:04:41 +0000 | [diff] [blame] | 1112 | progname = argv[0] ? argv[0] : "strace"; |
| 1113 | |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1114 | strace_tracer_pid = getpid(); |
| 1115 | |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1116 | get_os_release(); |
| 1117 | |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 1118 | /* Allocate the initial tcbtab. */ |
| 1119 | tcbtabsize = argc; /* Surely enough for all -p args. */ |
Denys Vlasenko | 4f12af2 | 2011-06-23 13:16:23 +0200 | [diff] [blame] | 1120 | tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0])); |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 1121 | if (!tcbtab) |
| 1122 | die_out_of_memory(); |
Denys Vlasenko | 4f12af2 | 2011-06-23 13:16:23 +0200 | [diff] [blame] | 1123 | tcp = calloc(tcbtabsize, sizeof(*tcp)); |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 1124 | if (!tcp) |
| 1125 | die_out_of_memory(); |
Denys Vlasenko | 4f12af2 | 2011-06-23 13:16:23 +0200 | [diff] [blame] | 1126 | for (c = 0; c < tcbtabsize; c++) |
| 1127 | tcbtab[c] = tcp++; |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 1128 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1129 | outf = stderr; |
Roland McGrath | 138c6a3 | 2006-01-12 09:50:49 +0000 | [diff] [blame] | 1130 | set_sortby(DEFAULT_SORTBY); |
| 1131 | set_personality(DEFAULT_PERSONALITY); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1132 | qualify("trace=all"); |
| 1133 | qualify("abbrev=all"); |
| 1134 | qualify("verbose=all"); |
| 1135 | qualify("signal=all"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1136 | while ((c = getopt(argc, argv, |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 1137 | "+cCdfFhiqrtTvVxyz" |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 1138 | #ifndef USE_PROCFS |
| 1139 | "D" |
| 1140 | #endif |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame^] | 1141 | "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1142 | switch (c) { |
| 1143 | case 'c': |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 1144 | if (cflag == CFLAG_BOTH) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1145 | error_msg_and_die("-c and -C are mutually exclusive options"); |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 1146 | } |
| 1147 | cflag = CFLAG_ONLY_STATS; |
| 1148 | break; |
| 1149 | case 'C': |
| 1150 | if (cflag == CFLAG_ONLY_STATS) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1151 | error_msg_and_die("-c and -C are mutually exclusive options"); |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 1152 | } |
| 1153 | cflag = CFLAG_BOTH; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1154 | break; |
| 1155 | case 'd': |
| 1156 | debug++; |
| 1157 | break; |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 1158 | #ifndef USE_PROCFS |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 1159 | case 'D': |
| 1160 | daemonized_tracer = 1; |
| 1161 | break; |
| 1162 | #endif |
Roland McGrath | 41c4822 | 2008-07-18 00:25:10 +0000 | [diff] [blame] | 1163 | case 'F': |
Dmitry V. Levin | 06350db | 2008-07-25 15:42:34 +0000 | [diff] [blame] | 1164 | optF = 1; |
| 1165 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1166 | case 'f': |
| 1167 | followfork++; |
| 1168 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1169 | case 'h': |
| 1170 | usage(stdout, 0); |
| 1171 | break; |
| 1172 | case 'i': |
| 1173 | iflag++; |
| 1174 | break; |
| 1175 | case 'q': |
| 1176 | qflag++; |
| 1177 | break; |
| 1178 | case 'r': |
| 1179 | rflag++; |
| 1180 | tflag++; |
| 1181 | break; |
| 1182 | case 't': |
| 1183 | tflag++; |
| 1184 | break; |
| 1185 | case 'T': |
| 1186 | dtime++; |
| 1187 | break; |
| 1188 | case 'x': |
| 1189 | xflag++; |
| 1190 | break; |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 1191 | case 'y': |
| 1192 | show_fd_path = 1; |
| 1193 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1194 | case 'v': |
| 1195 | qualify("abbrev=none"); |
| 1196 | break; |
| 1197 | case 'V': |
Roland McGrath | 9c9a253 | 2003-02-20 02:56:29 +0000 | [diff] [blame] | 1198 | printf("%s -- version %s\n", PACKAGE_NAME, VERSION); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1199 | exit(0); |
| 1200 | break; |
Michal Ludvig | 17f8fb3 | 2002-11-06 13:17:21 +0000 | [diff] [blame] | 1201 | case 'z': |
| 1202 | not_failing_only = 1; |
| 1203 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1204 | case 'a': |
| 1205 | acolumn = atoi(optarg); |
Denys Vlasenko | 102ec49 | 2011-08-25 01:27:59 +0200 | [diff] [blame] | 1206 | if (acolumn < 0) |
| 1207 | error_msg_and_die("Bad column width '%s'", optarg); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1208 | break; |
| 1209 | case 'e': |
| 1210 | qualify(optarg); |
| 1211 | break; |
| 1212 | case 'o': |
| 1213 | outfname = strdup(optarg); |
| 1214 | break; |
| 1215 | case 'O': |
| 1216 | set_overhead(atoi(optarg)); |
| 1217 | break; |
| 1218 | case 'p': |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 1219 | pid = atoi(optarg); |
| 1220 | if (pid <= 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1221 | error_msg("Invalid process id: '%s'", optarg); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1222 | break; |
| 1223 | } |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1224 | if (pid == strace_tracer_pid) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1225 | error_msg("I'm sorry, I can't let you do that, Dave."); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1226 | break; |
| 1227 | } |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 1228 | tcp = alloc_tcb(pid, 0); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1229 | tcp->flags |= TCB_ATTACHED; |
| 1230 | pflag_seen++; |
| 1231 | break; |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 1232 | case 'P': |
| 1233 | tracing_paths = 1; |
| 1234 | if (pathtrace_select(optarg)) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1235 | error_msg_and_die("Failed to select path '%s'", optarg); |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 1236 | } |
| 1237 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1238 | case 's': |
| 1239 | max_strlen = atoi(optarg); |
Roland McGrath | dccec72 | 2005-05-09 07:45:47 +0000 | [diff] [blame] | 1240 | if (max_strlen < 0) { |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame^] | 1241 | error_msg_and_die("Invalid -%c argument: '%s'", c, optarg); |
Roland McGrath | dccec72 | 2005-05-09 07:45:47 +0000 | [diff] [blame] | 1242 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1243 | break; |
| 1244 | case 'S': |
| 1245 | set_sortby(optarg); |
| 1246 | break; |
| 1247 | case 'u': |
| 1248 | username = strdup(optarg); |
| 1249 | break; |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 1250 | case 'E': |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 1251 | if (putenv(optarg) < 0) |
| 1252 | die_out_of_memory(); |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 1253 | break; |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame^] | 1254 | case 'I': |
| 1255 | opt_intr = atoi(optarg); |
| 1256 | if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS) { |
| 1257 | error_msg_and_die("Invalid -%c argument: '%s'", c, optarg); |
| 1258 | } |
| 1259 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1260 | default: |
| 1261 | usage(stderr, 1); |
| 1262 | break; |
| 1263 | } |
| 1264 | } |
Denys Vlasenko | 837399a | 2012-01-24 11:37:03 +0100 | [diff] [blame] | 1265 | argv += optind; |
| 1266 | /* argc -= optind; - no need, argc is not used below */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1267 | |
Denys Vlasenko | 102ec49 | 2011-08-25 01:27:59 +0200 | [diff] [blame] | 1268 | acolumn_spaces = malloc(acolumn + 1); |
| 1269 | if (!acolumn_spaces) |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 1270 | die_out_of_memory(); |
Denys Vlasenko | 102ec49 | 2011-08-25 01:27:59 +0200 | [diff] [blame] | 1271 | memset(acolumn_spaces, ' ', acolumn); |
| 1272 | acolumn_spaces[acolumn] = '\0'; |
| 1273 | |
Denys Vlasenko | 837399a | 2012-01-24 11:37:03 +0100 | [diff] [blame] | 1274 | /* Must have PROG [ARGS], or -p PID. Not both. */ |
| 1275 | if (!argv[0] == !pflag_seen) |
Roland McGrath | ce0d154 | 2003-11-11 21:24:23 +0000 | [diff] [blame] | 1276 | usage(stderr, 1); |
| 1277 | |
Wang Chao | d322a4b | 2010-08-05 14:30:11 +0800 | [diff] [blame] | 1278 | if (pflag_seen && daemonized_tracer) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1279 | error_msg_and_die("-D and -p are mutually exclusive options"); |
Wang Chao | d322a4b | 2010-08-05 14:30:11 +0800 | [diff] [blame] | 1280 | } |
| 1281 | |
Dmitry V. Levin | 06350db | 2008-07-25 15:42:34 +0000 | [diff] [blame] | 1282 | if (!followfork) |
| 1283 | followfork = optF; |
| 1284 | |
Roland McGrath | cb9def6 | 2006-04-25 07:48:03 +0000 | [diff] [blame] | 1285 | if (followfork > 1 && cflag) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1286 | error_msg_and_die("(-c or -C) and -ff are mutually exclusive options"); |
Roland McGrath | cb9def6 | 2006-04-25 07:48:03 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1289 | /* See if they want to run as another user. */ |
| 1290 | if (username != NULL) { |
| 1291 | struct passwd *pent; |
| 1292 | |
| 1293 | if (getuid() != 0 || geteuid() != 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1294 | error_msg_and_die("You must be root to use the -u option"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1295 | } |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 1296 | pent = getpwnam(username); |
| 1297 | if (pent == NULL) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1298 | error_msg_and_die("Cannot find user '%s'", username); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1299 | } |
| 1300 | run_uid = pent->pw_uid; |
| 1301 | run_gid = pent->pw_gid; |
| 1302 | } |
| 1303 | else { |
| 1304 | run_uid = getuid(); |
| 1305 | run_gid = getgid(); |
| 1306 | } |
| 1307 | |
Dmitry V. Levin | 8044bc1 | 2010-12-07 12:50:49 +0000 | [diff] [blame] | 1308 | #ifdef LINUX |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 1309 | if (followfork) |
| 1310 | test_ptrace_setoptions_followfork(); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1311 | test_ptrace_setoptions_for_all(); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1312 | test_ptrace_seize(); |
Dmitry V. Levin | 8044bc1 | 2010-12-07 12:50:49 +0000 | [diff] [blame] | 1313 | #endif |
| 1314 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1315 | /* Check if they want to redirect the output. */ |
| 1316 | if (outfname) { |
Roland McGrath | 37b9a66 | 2003-11-07 02:26:54 +0000 | [diff] [blame] | 1317 | /* See if they want to pipe the output. */ |
| 1318 | if (outfname[0] == '|' || outfname[0] == '!') { |
| 1319 | /* |
| 1320 | * We can't do the <outfname>.PID funny business |
| 1321 | * when using popen, so prohibit it. |
| 1322 | */ |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 1323 | if (followfork > 1) |
| 1324 | error_msg_and_die("Piping the output and -ff are mutually exclusive"); |
| 1325 | outf = strace_popen(outfname + 1); |
Roland McGrath | 37b9a66 | 2003-11-07 02:26:54 +0000 | [diff] [blame] | 1326 | } |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 1327 | else if (followfork <= 1) |
| 1328 | outf = strace_fopen(outfname); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1331 | if (!outfname || outfname[0] == '|' || outfname[0] == '!') { |
Denys Vlasenko | a677da5 | 2012-01-24 11:31:51 +0100 | [diff] [blame] | 1332 | char *buf = malloc(BUFSIZ); |
| 1333 | if (!buf) |
| 1334 | die_out_of_memory(); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1335 | setvbuf(outf, buf, _IOLBF, BUFSIZ); |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1336 | } |
Denys Vlasenko | 837399a | 2012-01-24 11:37:03 +0100 | [diff] [blame] | 1337 | if (outfname && argv[0]) { |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame^] | 1338 | if (!opt_intr) |
| 1339 | opt_intr = INTR_NEVER; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1340 | qflag = 1; |
Roland McGrath | 3693105 | 2003-06-03 01:35:20 +0000 | [diff] [blame] | 1341 | } |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame^] | 1342 | if (!opt_intr) |
| 1343 | opt_intr = INTR_WHILE_WAIT; |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 1344 | |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame^] | 1345 | /* argv[0] -pPID -oFILE Default interactive setting |
| 1346 | * yes 0 0 INTR_WHILE_WAIT |
| 1347 | * no 1 0 INTR_WHILE_WAIT |
| 1348 | * yes 0 1 INTR_NEVER |
| 1349 | * no 1 1 INTR_WHILE_WAIT |
Roland McGrath | 54cc1c8 | 2007-11-03 23:34:11 +0000 | [diff] [blame] | 1350 | */ |
| 1351 | |
| 1352 | /* STARTUP_CHILD must be called before the signal handlers get |
| 1353 | installed below as they are inherited into the spawned process. |
| 1354 | Also we do not need to be protected by them as during interruption |
| 1355 | in the STARTUP_CHILD mode we kill the spawned process anyway. */ |
Denys Vlasenko | 837399a | 2012-01-24 11:37:03 +0100 | [diff] [blame] | 1356 | if (argv[0]) |
| 1357 | startup_child(argv); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1358 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1359 | sigemptyset(&empty_set); |
| 1360 | sigemptyset(&blocked_set); |
| 1361 | sa.sa_handler = SIG_IGN; |
| 1362 | sigemptyset(&sa.sa_mask); |
| 1363 | sa.sa_flags = 0; |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame^] | 1364 | sigaction(SIGTTOU, &sa, NULL); /* SIG_IGN */ |
| 1365 | sigaction(SIGTTIN, &sa, NULL); /* SIG_IGN */ |
| 1366 | if (opt_intr != INTR_ANYWHERE) { |
| 1367 | if (opt_intr == INTR_BLOCK_TSTP_TOO) |
| 1368 | sigaction(SIGTSTP, &sa, NULL); /* SIG_IGN */ |
| 1369 | /* |
| 1370 | * In interactive mode (if no -o OUTFILE, or -p PID is used), |
| 1371 | * fatal signals are blocked while syscall stop is processed, |
| 1372 | * and acted on in between, when waiting for new syscall stops. |
| 1373 | * In non-interactive mode, signals are ignored. |
| 1374 | */ |
| 1375 | if (opt_intr == INTR_WHILE_WAIT) { |
| 1376 | sigaddset(&blocked_set, SIGHUP); |
| 1377 | sigaddset(&blocked_set, SIGINT); |
| 1378 | sigaddset(&blocked_set, SIGQUIT); |
| 1379 | sigaddset(&blocked_set, SIGPIPE); |
| 1380 | sigaddset(&blocked_set, SIGTERM); |
| 1381 | sa.sa_handler = interrupt; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1382 | #ifdef SUNOS4 |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame^] | 1383 | /* POSIX signals on sunos4.1 are a little broken. */ |
| 1384 | sa.sa_flags = SA_INTERRUPT; |
| 1385 | #endif |
| 1386 | } |
| 1387 | /* SIG_IGN, or set handler for these */ |
| 1388 | sigaction(SIGHUP, &sa, NULL); |
| 1389 | sigaction(SIGINT, &sa, NULL); |
| 1390 | sigaction(SIGQUIT, &sa, NULL); |
| 1391 | sigaction(SIGPIPE, &sa, NULL); |
| 1392 | sigaction(SIGTERM, &sa, NULL); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1393 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1394 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1395 | sa.sa_handler = reaper; |
| 1396 | sigaction(SIGCHLD, &sa, NULL); |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1397 | #else |
| 1398 | /* Make sure SIGCHLD has the default action so that waitpid |
| 1399 | definitely works without losing track of children. The user |
| 1400 | should not have given us a bogus state to inherit, but he might |
| 1401 | have. Arguably we should detect SIG_IGN here and pass it on |
| 1402 | to children, but probably noone really needs that. */ |
| 1403 | sa.sa_handler = SIG_DFL; |
| 1404 | sigaction(SIGCHLD, &sa, NULL); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1405 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1406 | |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 1407 | if (pflag_seen || daemonized_tracer) |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1408 | startup_attach(); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1409 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1410 | if (trace() < 0) |
| 1411 | exit(1); |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame^] | 1412 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1413 | cleanup(); |
Dmitry V. Levin | a680965 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 1414 | fflush(NULL); |
| 1415 | if (exit_code > 0xff) { |
| 1416 | /* Child was killed by a signal, mimic that. */ |
| 1417 | exit_code &= 0xff; |
| 1418 | signal(exit_code, SIG_DFL); |
| 1419 | raise(exit_code); |
| 1420 | /* Paranoia - what if this signal is not fatal? |
| 1421 | Exit with 128 + signo then. */ |
| 1422 | exit_code += 128; |
| 1423 | } |
| 1424 | exit(exit_code); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Denys Vlasenko | 2b60c35 | 2011-06-22 12:45:25 +0200 | [diff] [blame] | 1427 | static void |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 1428 | expand_tcbtab(void) |
Roland McGrath | 7b54a7a | 2004-06-04 01:50:45 +0000 | [diff] [blame] | 1429 | { |
| 1430 | /* Allocate some more TCBs and expand the table. |
| 1431 | We don't want to relocate the TCBs because our |
| 1432 | callers have pointers and it would be a pain. |
| 1433 | So tcbtab is a table of pointers. Since we never |
| 1434 | free the TCBs, we allocate a single chunk of many. */ |
Denys Vlasenko | 18da273 | 2011-06-22 12:41:57 +0200 | [diff] [blame] | 1435 | int i = tcbtabsize; |
| 1436 | struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0])); |
| 1437 | struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0])); |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 1438 | if (!newtab || !newtcbs) |
| 1439 | die_out_of_memory(); |
Roland McGrath | 7b54a7a | 2004-06-04 01:50:45 +0000 | [diff] [blame] | 1440 | tcbtabsize *= 2; |
| 1441 | tcbtab = newtab; |
Denys Vlasenko | 18da273 | 2011-06-22 12:41:57 +0200 | [diff] [blame] | 1442 | while (i < tcbtabsize) |
| 1443 | tcbtab[i++] = newtcbs++; |
Roland McGrath | 7b54a7a | 2004-06-04 01:50:45 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1446 | struct tcb * |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 1447 | alloc_tcb(int pid, int command_options_parsed) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1448 | { |
| 1449 | int i; |
| 1450 | struct tcb *tcp; |
| 1451 | |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 1452 | if (nprocs == tcbtabsize) |
| 1453 | expand_tcbtab(); |
| 1454 | |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 1455 | for (i = 0; i < tcbtabsize; i++) { |
| 1456 | tcp = tcbtab[i]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1457 | if ((tcp->flags & TCB_INUSE) == 0) { |
Denys Vlasenko | 18da273 | 2011-06-22 12:41:57 +0200 | [diff] [blame] | 1458 | memset(tcp, 0, sizeof(*tcp)); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1459 | tcp->pid = pid; |
Denys Vlasenko | 381dbc2 | 2011-09-05 13:59:39 +0200 | [diff] [blame] | 1460 | tcp->flags = TCB_INUSE; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1461 | tcp->outf = outf; /* Initialise to current out file */ |
Dmitry V. Levin | a5a839a | 2011-12-23 00:50:49 +0000 | [diff] [blame] | 1462 | #if SUPPORTED_PERSONALITIES > 1 |
| 1463 | tcp->currpers = current_personality; |
| 1464 | #endif |
Denys Vlasenko | 8dc0c8c | 2011-08-20 13:44:56 +0200 | [diff] [blame] | 1465 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1466 | tcp->pfd = -1; |
Denys Vlasenko | 8dc0c8c | 2011-08-20 13:44:56 +0200 | [diff] [blame] | 1467 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1468 | nprocs++; |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 1469 | if (debug) |
| 1470 | fprintf(stderr, "new tcb for pid %d, active tcbs:%d\n", tcp->pid, nprocs); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 1471 | if (command_options_parsed) |
| 1472 | newoutf(tcp); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1473 | return tcp; |
| 1474 | } |
| 1475 | } |
Denys Vlasenko | 18da273 | 2011-06-22 12:41:57 +0200 | [diff] [blame] | 1476 | error_msg_and_die("bug in alloc_tcb"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1479 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1480 | int |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 1481 | proc_open(struct tcb *tcp, int attaching) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1482 | { |
| 1483 | char proc[32]; |
| 1484 | long arg; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1485 | #ifdef SVR4 |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1486 | int i; |
| 1487 | sysset_t syscalls; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1488 | sigset_t signals; |
| 1489 | fltset_t faults; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1490 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1491 | #ifndef HAVE_POLLABLE_PROCFS |
| 1492 | static int last_pfd; |
| 1493 | #endif |
| 1494 | |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 1495 | #ifdef HAVE_MP_PROCFS |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1496 | /* Open the process pseudo-files in /proc. */ |
| 1497 | sprintf(proc, "/proc/%d/ctl", tcp->pid); |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 1498 | tcp->pfd = open(proc, O_WRONLY|O_EXCL); |
| 1499 | if (tcp->pfd < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1500 | perror("strace: open(\"/proc/...\", ...)"); |
| 1501 | return -1; |
| 1502 | } |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 1503 | set_cloexec_flag(tcp->pfd); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1504 | sprintf(proc, "/proc/%d/status", tcp->pid); |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 1505 | tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL); |
| 1506 | if (tcp->pfd_stat < 0) { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1507 | perror("strace: open(\"/proc/...\", ...)"); |
| 1508 | return -1; |
| 1509 | } |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 1510 | set_cloexec_flag(tcp->pfd_stat); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1511 | sprintf(proc, "/proc/%d/as", tcp->pid); |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 1512 | tcp->pfd_as = open(proc, O_RDONLY|O_EXCL); |
| 1513 | if (tcp->pfd_as < 0) { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1514 | perror("strace: open(\"/proc/...\", ...)"); |
| 1515 | return -1; |
| 1516 | } |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 1517 | set_cloexec_flag(tcp->pfd_as); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1518 | #else |
| 1519 | /* Open the process pseudo-file in /proc. */ |
Denys Vlasenko | 0890c8a | 2011-08-21 00:10:45 +0200 | [diff] [blame] | 1520 | # ifndef FREEBSD |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1521 | sprintf(proc, "/proc/%d", tcp->pid); |
Andreas Schwab | 372cc84 | 2010-07-09 11:49:27 +0200 | [diff] [blame] | 1522 | tcp->pfd = open(proc, O_RDWR|O_EXCL); |
Denys Vlasenko | 0890c8a | 2011-08-21 00:10:45 +0200 | [diff] [blame] | 1523 | # else |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1524 | sprintf(proc, "/proc/%d/mem", tcp->pid); |
Andreas Schwab | 372cc84 | 2010-07-09 11:49:27 +0200 | [diff] [blame] | 1525 | tcp->pfd = open(proc, O_RDWR); |
Denys Vlasenko | 0890c8a | 2011-08-21 00:10:45 +0200 | [diff] [blame] | 1526 | # endif |
Andreas Schwab | 372cc84 | 2010-07-09 11:49:27 +0200 | [diff] [blame] | 1527 | if (tcp->pfd < 0) { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1528 | perror("strace: open(\"/proc/...\", ...)"); |
| 1529 | return -1; |
| 1530 | } |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 1531 | set_cloexec_flag(tcp->pfd); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1532 | #endif |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1533 | #ifdef FREEBSD |
| 1534 | sprintf(proc, "/proc/%d/regs", tcp->pid); |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 1535 | tcp->pfd_reg = open(proc, O_RDONLY); |
| 1536 | if (tcp->pfd_reg < 0) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1537 | perror("strace: open(\"/proc/.../regs\", ...)"); |
| 1538 | return -1; |
| 1539 | } |
| 1540 | if (cflag) { |
| 1541 | sprintf(proc, "/proc/%d/status", tcp->pid); |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 1542 | tcp->pfd_status = open(proc, O_RDONLY); |
| 1543 | if (tcp->pfd_status < 0) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1544 | perror("strace: open(\"/proc/.../status\", ...)"); |
| 1545 | return -1; |
| 1546 | } |
| 1547 | } else |
| 1548 | tcp->pfd_status = -1; |
| 1549 | #endif /* FREEBSD */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1550 | rebuild_pollv(); |
| 1551 | if (!attaching) { |
| 1552 | /* |
| 1553 | * Wait for the child to pause. Because of a race |
| 1554 | * condition we have to poll for the event. |
| 1555 | */ |
| 1556 | for (;;) { |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1557 | if (IOCTL_STATUS(tcp) < 0) { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1558 | perror("strace: PIOCSTATUS"); |
| 1559 | return -1; |
| 1560 | } |
| 1561 | if (tcp->status.PR_FLAGS & PR_ASLEEP) |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1562 | break; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1563 | } |
| 1564 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1565 | #ifndef FREEBSD |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1566 | /* Stop the process so that we own the stop. */ |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 1567 | if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1568 | perror("strace: PIOCSTOP"); |
| 1569 | return -1; |
| 1570 | } |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1571 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1572 | #ifdef PIOCSET |
| 1573 | /* Set Run-on-Last-Close. */ |
| 1574 | arg = PR_RLC; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1575 | if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1576 | perror("PIOCSET PR_RLC"); |
| 1577 | return -1; |
| 1578 | } |
| 1579 | /* Set or Reset Inherit-on-Fork. */ |
| 1580 | arg = PR_FORK; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1581 | if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1582 | perror("PIOC{SET,RESET} PR_FORK"); |
| 1583 | return -1; |
| 1584 | } |
| 1585 | #else /* !PIOCSET */ |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1586 | #ifndef FREEBSD |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1587 | if (ioctl(tcp->pfd, PIOCSRLC) < 0) { |
| 1588 | perror("PIOCSRLC"); |
| 1589 | return -1; |
| 1590 | } |
| 1591 | if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) { |
| 1592 | perror("PIOC{S,R}FORK"); |
| 1593 | return -1; |
| 1594 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1595 | #else /* FREEBSD */ |
| 1596 | /* just unset the PF_LINGER flag for the Run-on-Last-Close. */ |
| 1597 | if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) { |
| 1598 | perror("PIOCGFL"); |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1599 | return -1; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1600 | } |
| 1601 | arg &= ~PF_LINGER; |
| 1602 | if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) { |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1603 | perror("PIOCSFL"); |
| 1604 | return -1; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1605 | } |
| 1606 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1607 | #endif /* !PIOCSET */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1608 | #ifndef FREEBSD |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1609 | /* Enable all syscall entries we care about. */ |
| 1610 | premptyset(&syscalls); |
| 1611 | for (i = 1; i < MAX_QUALS; ++i) { |
| 1612 | if (i > (sizeof syscalls) * CHAR_BIT) break; |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1613 | if (qual_flags[i] & QUAL_TRACE) praddset(&syscalls, i); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1614 | } |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1615 | praddset(&syscalls, SYS_execve); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1616 | if (followfork) { |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1617 | praddset(&syscalls, SYS_fork); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1618 | #ifdef SYS_forkall |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1619 | praddset(&syscalls, SYS_forkall); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1620 | #endif |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1621 | #ifdef SYS_fork1 |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1622 | praddset(&syscalls, SYS_fork1); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1623 | #endif |
| 1624 | #ifdef SYS_rfork1 |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1625 | praddset(&syscalls, SYS_rfork1); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1626 | #endif |
| 1627 | #ifdef SYS_rforkall |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1628 | praddset(&syscalls, SYS_rforkall); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1629 | #endif |
| 1630 | } |
| 1631 | if (IOCTL(tcp->pfd, PIOCSENTRY, &syscalls) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1632 | perror("PIOCSENTRY"); |
| 1633 | return -1; |
| 1634 | } |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1635 | /* Enable the syscall exits. */ |
| 1636 | if (IOCTL(tcp->pfd, PIOCSEXIT, &syscalls) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1637 | perror("PIOSEXIT"); |
| 1638 | return -1; |
| 1639 | } |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1640 | /* Enable signals we care about. */ |
| 1641 | premptyset(&signals); |
| 1642 | for (i = 1; i < MAX_QUALS; ++i) { |
| 1643 | if (i > (sizeof signals) * CHAR_BIT) break; |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1644 | if (qual_flags[i] & QUAL_SIGNAL) praddset(&signals, i); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1645 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1646 | if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1647 | perror("PIOCSTRACE"); |
| 1648 | return -1; |
| 1649 | } |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1650 | /* Enable faults we care about */ |
| 1651 | premptyset(&faults); |
| 1652 | for (i = 1; i < MAX_QUALS; ++i) { |
| 1653 | if (i > (sizeof faults) * CHAR_BIT) break; |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1654 | if (qual_flags[i] & QUAL_FAULT) praddset(&faults, i); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1655 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1656 | if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1657 | perror("PIOCSFAULT"); |
| 1658 | return -1; |
| 1659 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1660 | #else /* FREEBSD */ |
| 1661 | /* set events flags. */ |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1662 | arg = S_SIG | S_SCE | S_SCX; |
| 1663 | if (ioctl(tcp->pfd, PIOCBIS, arg) < 0) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1664 | perror("PIOCBIS"); |
| 1665 | return -1; |
| 1666 | } |
| 1667 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1668 | if (!attaching) { |
| 1669 | #ifdef MIPS |
| 1670 | /* |
| 1671 | * The SGI PRSABORT doesn't work for pause() so |
| 1672 | * we send it a caught signal to wake it up. |
| 1673 | */ |
| 1674 | kill(tcp->pid, SIGINT); |
| 1675 | #else /* !MIPS */ |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1676 | #ifdef PRSABORT |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1677 | /* The child is in a pause(), abort it. */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1678 | arg = PRSABORT; |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1679 | if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1680 | perror("PIOCRUN"); |
| 1681 | return -1; |
| 1682 | } |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1683 | #endif |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1684 | #endif /* !MIPS*/ |
| 1685 | #ifdef FREEBSD |
| 1686 | /* wake up the child if it received the SIGSTOP */ |
| 1687 | kill(tcp->pid, SIGCONT); |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1688 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1689 | for (;;) { |
| 1690 | /* Wait for the child to do something. */ |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1691 | if (IOCTL_WSTOP(tcp) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1692 | perror("PIOCWSTOP"); |
| 1693 | return -1; |
| 1694 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1695 | if (tcp->status.PR_WHY == PR_SYSENTRY) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1696 | tcp->flags &= ~TCB_INSYSCALL; |
Denys Vlasenko | 06602d9 | 2011-08-24 17:53:52 +0200 | [diff] [blame] | 1697 | get_scno(tcp); |
Roland McGrath | 76989d7 | 2005-06-07 23:21:31 +0000 | [diff] [blame] | 1698 | if (known_scno(tcp) == SYS_execve) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1699 | break; |
| 1700 | } |
| 1701 | /* Set it running: maybe execve will be next. */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1702 | #ifndef FREEBSD |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1703 | arg = 0; |
Denys Vlasenko | 0890c8a | 2011-08-21 00:10:45 +0200 | [diff] [blame] | 1704 | if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) |
| 1705 | #else |
| 1706 | if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) |
| 1707 | #endif |
| 1708 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1709 | perror("PIOCRUN"); |
| 1710 | return -1; |
| 1711 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1712 | #ifdef FREEBSD |
| 1713 | /* handle the case where we "opened" the child before |
| 1714 | it did the kill -STOP */ |
| 1715 | if (tcp->status.PR_WHY == PR_SIGNALLED && |
| 1716 | tcp->status.PR_WHAT == SIGSTOP) |
| 1717 | kill(tcp->pid, SIGCONT); |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1718 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1719 | } |
| 1720 | } |
Denys Vlasenko | 0890c8a | 2011-08-21 00:10:45 +0200 | [diff] [blame] | 1721 | #ifdef FREEBSD |
| 1722 | else { |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1723 | if (attaching < 2) { |
Wichert Akkerman | 2e4ffe5 | 2000-09-03 23:57:48 +0000 | [diff] [blame] | 1724 | /* We are attaching to an already running process. |
| 1725 | * Try to figure out the state of the process in syscalls, |
| 1726 | * to handle the first event well. |
| 1727 | * This is done by having a look at the "wchan" property of the |
| 1728 | * process, which tells where it is stopped (if it is). */ |
| 1729 | FILE * status; |
| 1730 | char wchan[20]; /* should be enough */ |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1731 | |
Wichert Akkerman | 2e4ffe5 | 2000-09-03 23:57:48 +0000 | [diff] [blame] | 1732 | sprintf(proc, "/proc/%d/status", tcp->pid); |
| 1733 | status = fopen(proc, "r"); |
| 1734 | if (status && |
| 1735 | (fscanf(status, "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d" |
| 1736 | "%*d,%*d %*d,%*d %19s", wchan) == 1) && |
| 1737 | strcmp(wchan, "nochan") && strcmp(wchan, "spread") && |
| 1738 | strcmp(wchan, "stopevent")) { |
| 1739 | /* The process is asleep in the middle of a syscall. |
| 1740 | Fake the syscall entry event */ |
| 1741 | tcp->flags &= ~(TCB_INSYSCALL|TCB_STARTUP); |
| 1742 | tcp->status.PR_WHY = PR_SYSENTRY; |
| 1743 | trace_syscall(tcp); |
| 1744 | } |
| 1745 | if (status) |
| 1746 | fclose(status); |
| 1747 | } /* otherwise it's a fork being followed */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1748 | } |
| 1749 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1750 | #ifndef HAVE_POLLABLE_PROCFS |
| 1751 | if (proc_poll_pipe[0] != -1) |
| 1752 | proc_poller(tcp->pfd); |
| 1753 | else if (nprocs > 1) { |
| 1754 | proc_poll_open(); |
| 1755 | proc_poller(last_pfd); |
| 1756 | proc_poller(tcp->pfd); |
| 1757 | } |
| 1758 | last_pfd = tcp->pfd; |
| 1759 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 1760 | return 0; |
| 1761 | } |
| 1762 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1763 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1764 | |
Denys Vlasenko | eebb04d | 2012-01-27 15:24:48 +0100 | [diff] [blame] | 1765 | static struct tcb * |
Roland McGrath | 54e931f | 2010-09-14 18:59:20 -0700 | [diff] [blame] | 1766 | pid2tcb(int pid) |
| 1767 | { |
| 1768 | int i; |
| 1769 | |
| 1770 | if (pid <= 0) |
| 1771 | return NULL; |
| 1772 | |
| 1773 | for (i = 0; i < tcbtabsize; i++) { |
| 1774 | struct tcb *tcp = tcbtab[i]; |
| 1775 | if (tcp->pid == pid && (tcp->flags & TCB_INUSE)) |
| 1776 | return tcp; |
| 1777 | } |
| 1778 | |
| 1779 | return NULL; |
| 1780 | } |
| 1781 | |
| 1782 | #ifdef USE_PROCFS |
| 1783 | |
| 1784 | static struct tcb * |
| 1785 | first_used_tcb(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1786 | { |
| 1787 | int i; |
| 1788 | struct tcb *tcp; |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 1789 | for (i = 0; i < tcbtabsize; i++) { |
| 1790 | tcp = tcbtab[i]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1791 | if (tcp->flags & TCB_INUSE) |
| 1792 | return tcp; |
| 1793 | } |
| 1794 | return NULL; |
| 1795 | } |
| 1796 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1797 | static struct tcb * |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1798 | pfd2tcb(int pfd) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1799 | { |
| 1800 | int i; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1801 | |
Roland McGrath | ca16be8 | 2003-01-10 19:55:28 +0000 | [diff] [blame] | 1802 | for (i = 0; i < tcbtabsize; i++) { |
| 1803 | struct tcb *tcp = tcbtab[i]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1804 | if (tcp->pfd != pfd) |
| 1805 | continue; |
| 1806 | if (tcp->flags & TCB_INUSE) |
| 1807 | return tcp; |
| 1808 | } |
| 1809 | return NULL; |
| 1810 | } |
| 1811 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1812 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1813 | |
| 1814 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1815 | droptcb(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1816 | { |
| 1817 | if (tcp->pid == 0) |
| 1818 | return; |
Denys Vlasenko | 19cdada | 2011-08-17 10:45:32 +0200 | [diff] [blame] | 1819 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1820 | nprocs--; |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 1821 | if (debug) |
| 1822 | fprintf(stderr, "dropped tcb for pid %d, %d remain\n", tcp->pid, nprocs); |
Wichert Akkerman | eb8ebda | 2002-04-01 17:48:02 +0000 | [diff] [blame] | 1823 | |
Denys Vlasenko | 8dc0c8c | 2011-08-20 13:44:56 +0200 | [diff] [blame] | 1824 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1825 | if (tcp->pfd != -1) { |
| 1826 | close(tcp->pfd); |
| 1827 | tcp->pfd = -1; |
Denys Vlasenko | 8dc0c8c | 2011-08-20 13:44:56 +0200 | [diff] [blame] | 1828 | # ifdef FREEBSD |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1829 | if (tcp->pfd_reg != -1) { |
| 1830 | close(tcp->pfd_reg); |
| 1831 | tcp->pfd_reg = -1; |
| 1832 | } |
| 1833 | if (tcp->pfd_status != -1) { |
| 1834 | close(tcp->pfd_status); |
| 1835 | tcp->pfd_status = -1; |
| 1836 | } |
Denys Vlasenko | 8dc0c8c | 2011-08-20 13:44:56 +0200 | [diff] [blame] | 1837 | # endif |
Denys Vlasenko | 19cdada | 2011-08-17 10:45:32 +0200 | [diff] [blame] | 1838 | tcp->flags = 0; /* rebuild_pollv needs it */ |
| 1839 | rebuild_pollv(); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1840 | } |
Denys Vlasenko | 8dc0c8c | 2011-08-20 13:44:56 +0200 | [diff] [blame] | 1841 | #endif |
Wichert Akkerman | eb8ebda | 2002-04-01 17:48:02 +0000 | [diff] [blame] | 1842 | |
Wichert Akkerman | 822f0c9 | 2002-04-03 10:55:14 +0000 | [diff] [blame] | 1843 | if (outfname && followfork > 1 && tcp->outf) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1844 | fclose(tcp->outf); |
Wichert Akkerman | eb8ebda | 2002-04-01 17:48:02 +0000 | [diff] [blame] | 1845 | |
Denys Vlasenko | 19cdada | 2011-08-17 10:45:32 +0200 | [diff] [blame] | 1846 | memset(tcp, 0, sizeof(*tcp)); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
Roland McGrath | 0a46388 | 2007-07-05 18:43:16 +0000 | [diff] [blame] | 1849 | /* detach traced process; continue with sig |
| 1850 | Never call DETACH twice on the same process as both unattached and |
| 1851 | attached-unstopped processes give the same ESRCH. For unattached process we |
| 1852 | would SIGSTOP it and wait for its SIGSTOP notification forever. */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1853 | |
| 1854 | static int |
Denys Vlasenko | 4c19638 | 2012-01-04 15:11:09 +0100 | [diff] [blame] | 1855 | detach(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1856 | { |
| 1857 | int error = 0; |
Roland McGrath | ca16be8 | 2003-01-10 19:55:28 +0000 | [diff] [blame] | 1858 | #ifdef LINUX |
Roland McGrath | 1bfd310 | 2007-08-03 10:02:00 +0000 | [diff] [blame] | 1859 | int status, catch_sigstop; |
Roland McGrath | ca16be8 | 2003-01-10 19:55:28 +0000 | [diff] [blame] | 1860 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1861 | |
| 1862 | if (tcp->flags & TCB_BPTSET) |
Andreas Schwab | 840d85b | 2010-01-12 11:16:32 +0100 | [diff] [blame] | 1863 | clearbpt(tcp); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1864 | |
| 1865 | #ifdef LINUX |
| 1866 | /* |
| 1867 | * Linux wrongly insists the child be stopped |
Roland McGrath | 7bf1047 | 2002-12-16 20:42:50 +0000 | [diff] [blame] | 1868 | * before detaching. Arghh. We go through hoops |
| 1869 | * to make a clean break of things. |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1870 | */ |
Roland McGrath | 7bf1047 | 2002-12-16 20:42:50 +0000 | [diff] [blame] | 1871 | #if defined(SPARC) |
| 1872 | #undef PTRACE_DETACH |
| 1873 | #define PTRACE_DETACH PTRACE_SUNDETACH |
| 1874 | #endif |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1875 | /* |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1876 | * We attached but possibly didn't see the expected SIGSTOP. |
Denys Vlasenko | f88837a | 2011-09-05 14:05:46 +0200 | [diff] [blame] | 1877 | * We must catch exactly one as otherwise the detached process |
| 1878 | * would be left stopped (process state T). |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1879 | */ |
Denys Vlasenko | f88837a | 2011-09-05 14:05:46 +0200 | [diff] [blame] | 1880 | catch_sigstop = (tcp->flags & TCB_IGNORE_ONE_SIGSTOP); |
Denys Vlasenko | 4c19638 | 2012-01-04 15:11:09 +0100 | [diff] [blame] | 1881 | error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, 0); |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 1882 | if (error == 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1883 | /* On a clear day, you can see forever. */ |
Roland McGrath | 7bf1047 | 2002-12-16 20:42:50 +0000 | [diff] [blame] | 1884 | } |
| 1885 | else if (errno != ESRCH) { |
| 1886 | /* Shouldn't happen. */ |
| 1887 | perror("detach: ptrace(PTRACE_DETACH, ...)"); |
| 1888 | } |
Denys Vlasenko | 44f87ef | 2011-08-17 15:18:21 +0200 | [diff] [blame] | 1889 | else if (my_tkill(tcp->pid, 0) < 0) { |
Roland McGrath | 7bf1047 | 2002-12-16 20:42:50 +0000 | [diff] [blame] | 1890 | if (errno != ESRCH) |
| 1891 | perror("detach: checking sanity"); |
| 1892 | } |
Denys Vlasenko | 44f87ef | 2011-08-17 15:18:21 +0200 | [diff] [blame] | 1893 | else if (!catch_sigstop && my_tkill(tcp->pid, SIGSTOP) < 0) { |
Roland McGrath | 7bf1047 | 2002-12-16 20:42:50 +0000 | [diff] [blame] | 1894 | if (errno != ESRCH) |
| 1895 | perror("detach: stopping child"); |
| 1896 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1897 | else |
| 1898 | catch_sigstop = 1; |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 1899 | if (catch_sigstop) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1900 | for (;;) { |
Roland McGrath | 7508cb4 | 2002-12-17 10:48:05 +0000 | [diff] [blame] | 1901 | #ifdef __WALL |
| 1902 | if (wait4(tcp->pid, &status, __WALL, NULL) < 0) { |
| 1903 | if (errno == ECHILD) /* Already gone. */ |
| 1904 | break; |
| 1905 | if (errno != EINVAL) { |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1906 | perror("detach: waiting"); |
Roland McGrath | 7508cb4 | 2002-12-17 10:48:05 +0000 | [diff] [blame] | 1907 | break; |
| 1908 | } |
| 1909 | #endif /* __WALL */ |
| 1910 | /* No __WALL here. */ |
| 1911 | if (waitpid(tcp->pid, &status, 0) < 0) { |
| 1912 | if (errno != ECHILD) { |
| 1913 | perror("detach: waiting"); |
| 1914 | break; |
| 1915 | } |
| 1916 | #ifdef __WCLONE |
| 1917 | /* If no processes, try clones. */ |
| 1918 | if (wait4(tcp->pid, &status, __WCLONE, |
| 1919 | NULL) < 0) { |
| 1920 | if (errno != ECHILD) |
| 1921 | perror("detach: waiting"); |
| 1922 | break; |
| 1923 | } |
| 1924 | #endif /* __WCLONE */ |
| 1925 | } |
| 1926 | #ifdef __WALL |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1927 | } |
Roland McGrath | 7508cb4 | 2002-12-17 10:48:05 +0000 | [diff] [blame] | 1928 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1929 | if (!WIFSTOPPED(status)) { |
| 1930 | /* Au revoir, mon ami. */ |
| 1931 | break; |
| 1932 | } |
| 1933 | if (WSTOPSIG(status) == SIGSTOP) { |
Denys Vlasenko | 4c19638 | 2012-01-04 15:11:09 +0100 | [diff] [blame] | 1934 | ptrace_restart(PTRACE_DETACH, tcp, 0); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1935 | break; |
| 1936 | } |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 1937 | error = ptrace_restart(PTRACE_CONT, tcp, |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1938 | WSTOPSIG(status) == syscall_trap_sig ? 0 |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 1939 | : WSTOPSIG(status)); |
| 1940 | if (error < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1941 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1942 | } |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 1943 | } |
Roland McGrath | 7bf1047 | 2002-12-16 20:42:50 +0000 | [diff] [blame] | 1944 | #endif /* LINUX */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1945 | |
| 1946 | #if defined(SUNOS4) |
| 1947 | /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */ |
Denys Vlasenko | 4c19638 | 2012-01-04 15:11:09 +0100 | [diff] [blame] | 1948 | error = ptrace_restart(PTRACE_DETACH, tcp, 0); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1949 | #endif /* SUNOS4 */ |
| 1950 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1951 | if (!qflag) |
| 1952 | fprintf(stderr, "Process %u detached\n", tcp->pid); |
| 1953 | |
| 1954 | droptcb(tcp); |
Roland McGrath | a08a97e | 2005-08-03 11:23:46 +0000 | [diff] [blame] | 1955 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1956 | return error; |
| 1957 | } |
| 1958 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1959 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1960 | |
Dmitry V. Levin | e5e6085 | 2009-12-31 22:50:49 +0000 | [diff] [blame] | 1961 | static void reaper(int sig) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1962 | { |
| 1963 | int pid; |
| 1964 | int status; |
| 1965 | |
| 1966 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1967 | } |
| 1968 | } |
| 1969 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1970 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1971 | |
| 1972 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1973 | cleanup(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1974 | { |
| 1975 | int i; |
| 1976 | struct tcb *tcp; |
Denys Vlasenko | a355925 | 2012-01-29 16:43:51 +0100 | [diff] [blame] | 1977 | int fatal_sig = interrupted ? interrupted : SIGTERM; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1978 | |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 1979 | for (i = 0; i < tcbtabsize; i++) { |
| 1980 | tcp = tcbtab[i]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1981 | if (!(tcp->flags & TCB_INUSE)) |
| 1982 | continue; |
| 1983 | if (debug) |
| 1984 | fprintf(stderr, |
| 1985 | "cleanup: looking at pid %u\n", tcp->pid); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 1986 | if (printing_tcp && |
| 1987 | (!outfname || followfork < 2 || printing_tcp == tcp)) { |
| 1988 | tprints(" <unfinished ...>\n"); |
| 1989 | printing_tcp = NULL; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1990 | } |
| 1991 | if (tcp->flags & TCB_ATTACHED) |
Denys Vlasenko | 4c19638 | 2012-01-04 15:11:09 +0100 | [diff] [blame] | 1992 | detach(tcp); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1993 | else { |
| 1994 | kill(tcp->pid, SIGCONT); |
Denys Vlasenko | a355925 | 2012-01-29 16:43:51 +0100 | [diff] [blame] | 1995 | kill(tcp->pid, fatal_sig); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1996 | } |
| 1997 | } |
| 1998 | if (cflag) |
| 1999 | call_summary(outf); |
| 2000 | } |
| 2001 | |
| 2002 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 2003 | interrupt(int sig) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2004 | { |
Denys Vlasenko | a355925 | 2012-01-29 16:43:51 +0100 | [diff] [blame] | 2005 | interrupted = sig; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | #ifndef HAVE_STRERROR |
| 2009 | |
Roland McGrath | 6d2b349 | 2002-12-30 00:51:30 +0000 | [diff] [blame] | 2010 | #if !HAVE_DECL_SYS_ERRLIST |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2011 | extern int sys_nerr; |
| 2012 | extern char *sys_errlist[]; |
Roland McGrath | 6d2b349 | 2002-12-30 00:51:30 +0000 | [diff] [blame] | 2013 | #endif /* HAVE_DECL_SYS_ERRLIST */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2014 | |
| 2015 | const char * |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 2016 | strerror(int err_no) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2017 | { |
| 2018 | static char buf[64]; |
| 2019 | |
Denys Vlasenko | 35aba6a | 2011-05-25 15:33:26 +0200 | [diff] [blame] | 2020 | if (err_no < 1 || err_no >= sys_nerr) { |
| 2021 | sprintf(buf, "Unknown error %d", err_no); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2022 | return buf; |
| 2023 | } |
Denys Vlasenko | 35aba6a | 2011-05-25 15:33:26 +0200 | [diff] [blame] | 2024 | return sys_errlist[err_no]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | #endif /* HAVE_STERRROR */ |
| 2028 | |
| 2029 | #ifndef HAVE_STRSIGNAL |
| 2030 | |
Roland McGrath | 8f474e0 | 2003-01-14 07:53:33 +0000 | [diff] [blame] | 2031 | #if defined HAVE_SYS_SIGLIST && !defined HAVE_DECL_SYS_SIGLIST |
Roland McGrath | 6d2b349 | 2002-12-30 00:51:30 +0000 | [diff] [blame] | 2032 | extern char *sys_siglist[]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2033 | #endif |
Roland McGrath | 8f474e0 | 2003-01-14 07:53:33 +0000 | [diff] [blame] | 2034 | #if defined HAVE_SYS__SIGLIST && !defined HAVE_DECL__SYS_SIGLIST |
| 2035 | extern char *_sys_siglist[]; |
| 2036 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2037 | |
| 2038 | const char * |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 2039 | strsignal(int sig) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2040 | { |
| 2041 | static char buf[64]; |
| 2042 | |
| 2043 | if (sig < 1 || sig >= NSIG) { |
| 2044 | sprintf(buf, "Unknown signal %d", sig); |
| 2045 | return buf; |
| 2046 | } |
| 2047 | #ifdef HAVE__SYS_SIGLIST |
| 2048 | return _sys_siglist[sig]; |
| 2049 | #else |
| 2050 | return sys_siglist[sig]; |
| 2051 | #endif |
| 2052 | } |
| 2053 | |
| 2054 | #endif /* HAVE_STRSIGNAL */ |
| 2055 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2056 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2057 | |
| 2058 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 2059 | rebuild_pollv(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2060 | { |
| 2061 | int i, j; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2062 | |
Denys Vlasenko | cfd364b | 2011-08-20 13:41:13 +0200 | [diff] [blame] | 2063 | free(pollv); |
| 2064 | pollv = malloc(nprocs * sizeof(pollv[0])); |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 2065 | if (!pollv) |
| 2066 | die_out_of_memory(); |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 2067 | |
Roland McGrath | ca16be8 | 2003-01-10 19:55:28 +0000 | [diff] [blame] | 2068 | for (i = j = 0; i < tcbtabsize; i++) { |
| 2069 | struct tcb *tcp = tcbtab[i]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2070 | if (!(tcp->flags & TCB_INUSE)) |
| 2071 | continue; |
| 2072 | pollv[j].fd = tcp->pfd; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2073 | pollv[j].events = POLLWANT; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2074 | j++; |
| 2075 | } |
| 2076 | if (j != nprocs) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2077 | error_msg_and_die("proc miscount"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2078 | } |
| 2079 | } |
| 2080 | |
| 2081 | #ifndef HAVE_POLLABLE_PROCFS |
| 2082 | |
| 2083 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 2084 | proc_poll_open(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2085 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2086 | int i; |
| 2087 | |
| 2088 | if (pipe(proc_poll_pipe) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2089 | perror_msg_and_die("pipe"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2090 | } |
| 2091 | for (i = 0; i < 2; i++) { |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 2092 | set_cloexec_flag(proc_poll_pipe[i]); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 2097 | proc_poll(struct pollfd *pollv, int nfds, int timeout) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2098 | { |
| 2099 | int i; |
| 2100 | int n; |
| 2101 | struct proc_pollfd pollinfo; |
| 2102 | |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 2103 | n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo)); |
| 2104 | if (n < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2105 | return n; |
| 2106 | if (n != sizeof(struct proc_pollfd)) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2107 | error_msg_and_die("panic: short read: %d", n); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2108 | } |
| 2109 | for (i = 0; i < nprocs; i++) { |
| 2110 | if (pollv[i].fd == pollinfo.fd) |
| 2111 | pollv[i].revents = pollinfo.revents; |
| 2112 | else |
| 2113 | pollv[i].revents = 0; |
| 2114 | } |
| 2115 | poller_pid = pollinfo.pid; |
| 2116 | return 1; |
| 2117 | } |
| 2118 | |
| 2119 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 2120 | wakeup_handler(int sig) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2121 | { |
| 2122 | } |
| 2123 | |
| 2124 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 2125 | proc_poller(int pfd) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2126 | { |
| 2127 | struct proc_pollfd pollinfo; |
| 2128 | struct sigaction sa; |
| 2129 | sigset_t blocked_set, empty_set; |
| 2130 | int i; |
| 2131 | int n; |
| 2132 | struct rlimit rl; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2133 | #ifdef FREEBSD |
| 2134 | struct procfs_status pfs; |
| 2135 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2136 | |
| 2137 | switch (fork()) { |
| 2138 | case -1: |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2139 | perror_msg_and_die("fork"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2140 | case 0: |
| 2141 | break; |
| 2142 | default: |
| 2143 | return; |
| 2144 | } |
| 2145 | |
| 2146 | sa.sa_handler = interactive ? SIG_DFL : SIG_IGN; |
| 2147 | sa.sa_flags = 0; |
| 2148 | sigemptyset(&sa.sa_mask); |
| 2149 | sigaction(SIGHUP, &sa, NULL); |
| 2150 | sigaction(SIGINT, &sa, NULL); |
| 2151 | sigaction(SIGQUIT, &sa, NULL); |
| 2152 | sigaction(SIGPIPE, &sa, NULL); |
| 2153 | sigaction(SIGTERM, &sa, NULL); |
| 2154 | sa.sa_handler = wakeup_handler; |
| 2155 | sigaction(SIGUSR1, &sa, NULL); |
| 2156 | sigemptyset(&blocked_set); |
| 2157 | sigaddset(&blocked_set, SIGUSR1); |
| 2158 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
| 2159 | sigemptyset(&empty_set); |
| 2160 | |
| 2161 | if (getrlimit(RLIMIT_NOFILE, &rl) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2162 | perror_msg_and_die("getrlimit(RLIMIT_NOFILE, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2163 | } |
| 2164 | n = rl.rlim_cur; |
| 2165 | for (i = 0; i < n; i++) { |
| 2166 | if (i != pfd && i != proc_poll_pipe[1]) |
| 2167 | close(i); |
| 2168 | } |
| 2169 | |
| 2170 | pollinfo.fd = pfd; |
| 2171 | pollinfo.pid = getpid(); |
| 2172 | for (;;) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2173 | #ifndef FREEBSD |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 2174 | if (ioctl(pfd, PIOCWSTOP, NULL) < 0) |
| 2175 | #else |
| 2176 | if (ioctl(pfd, PIOCWSTOP, &pfs) < 0) |
| 2177 | #endif |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2178 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2179 | switch (errno) { |
| 2180 | case EINTR: |
| 2181 | continue; |
| 2182 | case EBADF: |
| 2183 | pollinfo.revents = POLLERR; |
| 2184 | break; |
| 2185 | case ENOENT: |
| 2186 | pollinfo.revents = POLLHUP; |
| 2187 | break; |
| 2188 | default: |
| 2189 | perror("proc_poller: PIOCWSTOP"); |
| 2190 | } |
| 2191 | write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo)); |
| 2192 | _exit(0); |
| 2193 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2194 | pollinfo.revents = POLLWANT; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2195 | write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo)); |
| 2196 | sigsuspend(&empty_set); |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 2201 | |
| 2202 | static int |
| 2203 | choose_pfd() |
| 2204 | { |
| 2205 | int i, j; |
| 2206 | struct tcb *tcp; |
| 2207 | |
| 2208 | static int last; |
| 2209 | |
| 2210 | if (followfork < 2 && |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2211 | last < nprocs && (pollv[last].revents & POLLWANT)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2212 | /* |
| 2213 | * The previous process is ready to run again. We'll |
| 2214 | * let it do so if it is currently in a syscall. This |
| 2215 | * heuristic improves the readability of the trace. |
| 2216 | */ |
| 2217 | tcp = pfd2tcb(pollv[last].fd); |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 2218 | if (tcp && exiting(tcp)) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2219 | return pollv[last].fd; |
| 2220 | } |
| 2221 | |
| 2222 | for (i = 0; i < nprocs; i++) { |
| 2223 | /* Let competing children run round robin. */ |
| 2224 | j = (i + last + 1) % nprocs; |
| 2225 | if (pollv[j].revents & (POLLHUP | POLLERR)) { |
| 2226 | tcp = pfd2tcb(pollv[j].fd); |
| 2227 | if (!tcp) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2228 | error_msg_and_die("lost proc"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2229 | } |
| 2230 | droptcb(tcp); |
| 2231 | return -1; |
| 2232 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2233 | if (pollv[j].revents & POLLWANT) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2234 | last = j; |
| 2235 | return pollv[j].fd; |
| 2236 | } |
| 2237 | } |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2238 | error_msg_and_die("nothing ready"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 2242 | trace(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2243 | { |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 2244 | #ifdef POLL_HACK |
John Hughes | d870b3c | 2002-05-21 11:24:18 +0000 | [diff] [blame] | 2245 | struct tcb *in_syscall = NULL; |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 2246 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2247 | struct tcb *tcp; |
| 2248 | int pfd; |
| 2249 | int what; |
| 2250 | int ioctl_result = 0, ioctl_errno = 0; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2251 | long arg; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2252 | |
| 2253 | for (;;) { |
| 2254 | if (interactive) |
| 2255 | sigprocmask(SIG_SETMASK, &empty_set, NULL); |
| 2256 | |
| 2257 | if (nprocs == 0) |
| 2258 | break; |
| 2259 | |
| 2260 | switch (nprocs) { |
| 2261 | case 1: |
| 2262 | #ifndef HAVE_POLLABLE_PROCFS |
| 2263 | if (proc_poll_pipe[0] == -1) { |
| 2264 | #endif |
Roland McGrath | 54e931f | 2010-09-14 18:59:20 -0700 | [diff] [blame] | 2265 | tcp = first_used_tcb(); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2266 | if (!tcp) |
| 2267 | continue; |
| 2268 | pfd = tcp->pfd; |
| 2269 | if (pfd == -1) |
| 2270 | continue; |
| 2271 | break; |
| 2272 | #ifndef HAVE_POLLABLE_PROCFS |
| 2273 | } |
| 2274 | /* fall through ... */ |
| 2275 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 2276 | default: |
| 2277 | #ifdef HAVE_POLLABLE_PROCFS |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 2278 | #ifdef POLL_HACK |
| 2279 | /* On some systems (e.g. UnixWare) we get too much ugly |
| 2280 | "unfinished..." stuff when multiple proceses are in |
| 2281 | syscalls. Here's a nasty hack */ |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2282 | |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 2283 | if (in_syscall) { |
| 2284 | struct pollfd pv; |
| 2285 | tcp = in_syscall; |
| 2286 | in_syscall = NULL; |
| 2287 | pv.fd = tcp->pfd; |
| 2288 | pv.events = POLLWANT; |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 2289 | what = poll(&pv, 1, 1); |
| 2290 | if (what < 0) { |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 2291 | if (interrupted) |
| 2292 | return 0; |
| 2293 | continue; |
| 2294 | } |
| 2295 | else if (what == 1 && pv.revents & POLLWANT) { |
| 2296 | goto FOUND; |
| 2297 | } |
| 2298 | } |
| 2299 | #endif |
| 2300 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2301 | if (poll(pollv, nprocs, INFTIM) < 0) { |
| 2302 | if (interrupted) |
| 2303 | return 0; |
| 2304 | continue; |
| 2305 | } |
| 2306 | #else /* !HAVE_POLLABLE_PROCFS */ |
| 2307 | if (proc_poll(pollv, nprocs, INFTIM) < 0) { |
| 2308 | if (interrupted) |
| 2309 | return 0; |
| 2310 | continue; |
| 2311 | } |
| 2312 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 2313 | pfd = choose_pfd(); |
| 2314 | if (pfd == -1) |
| 2315 | continue; |
| 2316 | break; |
| 2317 | } |
| 2318 | |
| 2319 | /* Look up `pfd' in our table. */ |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 2320 | tcp = pfd2tcb(pfd); |
| 2321 | if (tcp == NULL) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2322 | error_msg_and_die("unknown pfd: %u", pfd); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2323 | } |
John Hughes | b664308 | 2002-05-23 11:02:22 +0000 | [diff] [blame] | 2324 | #ifdef POLL_HACK |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 2325 | FOUND: |
John Hughes | b664308 | 2002-05-23 11:02:22 +0000 | [diff] [blame] | 2326 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2327 | /* Get the status of the process. */ |
| 2328 | if (!interrupted) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2329 | #ifndef FREEBSD |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 2330 | ioctl_result = IOCTL_WSTOP(tcp); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2331 | #else /* FREEBSD */ |
| 2332 | /* Thanks to some scheduling mystery, the first poller |
| 2333 | sometimes waits for the already processed end of fork |
| 2334 | event. Doing a non blocking poll here solves the problem. */ |
| 2335 | if (proc_poll_pipe[0] != -1) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 2336 | ioctl_result = IOCTL_STATUS(tcp); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2337 | else |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 2338 | ioctl_result = IOCTL_WSTOP(tcp); |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2339 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2340 | ioctl_errno = errno; |
| 2341 | #ifndef HAVE_POLLABLE_PROCFS |
| 2342 | if (proc_poll_pipe[0] != -1) { |
| 2343 | if (ioctl_result < 0) |
| 2344 | kill(poller_pid, SIGKILL); |
| 2345 | else |
| 2346 | kill(poller_pid, SIGUSR1); |
| 2347 | } |
| 2348 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 2349 | } |
| 2350 | if (interrupted) |
| 2351 | return 0; |
| 2352 | |
| 2353 | if (interactive) |
| 2354 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
| 2355 | |
| 2356 | if (ioctl_result < 0) { |
| 2357 | /* Find out what happened if it failed. */ |
| 2358 | switch (ioctl_errno) { |
| 2359 | case EINTR: |
| 2360 | case EBADF: |
| 2361 | continue; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2362 | #ifdef FREEBSD |
| 2363 | case ENOTTY: |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2364 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2365 | case ENOENT: |
| 2366 | droptcb(tcp); |
| 2367 | continue; |
| 2368 | default: |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2369 | perror_msg_and_die("PIOCWSTOP"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2370 | } |
| 2371 | } |
| 2372 | |
Wichert Akkerman | 2e4ffe5 | 2000-09-03 23:57:48 +0000 | [diff] [blame] | 2373 | #ifdef FREEBSD |
| 2374 | if ((tcp->flags & TCB_STARTUP) && (tcp->status.PR_WHY == PR_SYSEXIT)) { |
| 2375 | /* discard first event for a syscall we never entered */ |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 2376 | IOCTL(tcp->pfd, PIOCRUN, 0); |
Wichert Akkerman | 2e4ffe5 | 2000-09-03 23:57:48 +0000 | [diff] [blame] | 2377 | continue; |
| 2378 | } |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2379 | #endif |
| 2380 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2381 | /* clear the just started flag */ |
| 2382 | tcp->flags &= ~TCB_STARTUP; |
| 2383 | |
| 2384 | /* set current output file */ |
| 2385 | outf = tcp->outf; |
Andreas Schwab | ccdff48 | 2009-10-27 16:27:13 +0100 | [diff] [blame] | 2386 | curcol = tcp->curcol; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2387 | |
| 2388 | if (cflag) { |
| 2389 | struct timeval stime; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2390 | #ifdef FREEBSD |
| 2391 | char buf[1024]; |
| 2392 | int len; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2393 | |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 2394 | len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0); |
| 2395 | if (len > 0) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2396 | buf[len] = '\0'; |
| 2397 | sscanf(buf, |
| 2398 | "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld", |
| 2399 | &stime.tv_sec, &stime.tv_usec); |
| 2400 | } else |
| 2401 | stime.tv_sec = stime.tv_usec = 0; |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2402 | #else /* !FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2403 | stime.tv_sec = tcp->status.pr_stime.tv_sec; |
| 2404 | stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2405 | #endif /* !FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2406 | tv_sub(&tcp->dtime, &stime, &tcp->stime); |
| 2407 | tcp->stime = stime; |
| 2408 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2409 | what = tcp->status.PR_WHAT; |
| 2410 | switch (tcp->status.PR_WHY) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2411 | #ifndef FREEBSD |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2412 | case PR_REQUESTED: |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2413 | if (tcp->status.PR_FLAGS & PR_ASLEEP) { |
| 2414 | tcp->status.PR_WHY = PR_SYSENTRY; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2415 | if (trace_syscall(tcp) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2416 | error_msg_and_die("syscall trouble"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2417 | } |
| 2418 | } |
| 2419 | break; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2420 | #endif /* !FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2421 | case PR_SYSENTRY: |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 2422 | #ifdef POLL_HACK |
| 2423 | in_syscall = tcp; |
| 2424 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2425 | case PR_SYSEXIT: |
| 2426 | if (trace_syscall(tcp) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2427 | error_msg_and_die("syscall trouble"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2428 | } |
| 2429 | break; |
| 2430 | case PR_SIGNALLED: |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 2431 | if (cflag != CFLAG_ONLY_STATS |
| 2432 | && (qual_flags[what] & QUAL_SIGNAL)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2433 | printleader(tcp); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2434 | tprintf("--- %s (%s) ---\n", |
Nate Sammons | ce780fc | 1999-03-29 23:23:13 +0000 | [diff] [blame] | 2435 | signame(what), strsignal(what)); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2436 | printing_tcp = NULL; |
John Hughes | 5826589 | 2001-10-18 15:13:53 +0000 | [diff] [blame] | 2437 | #ifdef PR_INFO |
| 2438 | if (tcp->status.PR_INFO.si_signo == what) { |
| 2439 | printleader(tcp); |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 2440 | tprints(" siginfo="); |
John Hughes | 5826589 | 2001-10-18 15:13:53 +0000 | [diff] [blame] | 2441 | printsiginfo(&tcp->status.PR_INFO, 1); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2442 | tprints("\n"); |
| 2443 | printing_tcp = NULL; |
John Hughes | 5826589 | 2001-10-18 15:13:53 +0000 | [diff] [blame] | 2444 | } |
| 2445 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2446 | } |
| 2447 | break; |
| 2448 | case PR_FAULTED: |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 2449 | if (cflag != CFLAGS_ONLY_STATS |
| 2450 | && (qual_flags[what] & QUAL_FAULT)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2451 | printleader(tcp); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2452 | tprintf("=== FAULT %d ===\n", what); |
| 2453 | printing_tcp = NULL; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2454 | } |
| 2455 | break; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2456 | #ifdef FREEBSD |
| 2457 | case 0: /* handle case we polled for nothing */ |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 2458 | continue; |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2459 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2460 | default: |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2461 | error_msg_and_die("odd stop %d", tcp->status.PR_WHY); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2462 | break; |
| 2463 | } |
Andreas Schwab | ccdff48 | 2009-10-27 16:27:13 +0100 | [diff] [blame] | 2464 | /* Remember current print column before continuing. */ |
| 2465 | tcp->curcol = curcol; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2466 | arg = 0; |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2467 | #ifndef FREEBSD |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 2468 | if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2469 | #else |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 2470 | if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2471 | #endif |
Andreas Schwab | 372cc84 | 2010-07-09 11:49:27 +0200 | [diff] [blame] | 2472 | { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2473 | perror_msg_and_die("PIOCRUN"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2474 | } |
| 2475 | } |
| 2476 | return 0; |
| 2477 | } |
| 2478 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2479 | #else /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2480 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2481 | static int |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 2482 | trace(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2483 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2484 | #ifdef LINUX |
| 2485 | struct rusage ru; |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame] | 2486 | struct rusage *rup = cflag ? &ru : NULL; |
| 2487 | # ifdef __WALL |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2488 | static int wait4_options = __WALL; |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame] | 2489 | # endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2490 | #endif /* LINUX */ |
| 2491 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2492 | while (nprocs != 0) { |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 2493 | int pid; |
| 2494 | int wait_errno; |
| 2495 | int status, sig; |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 2496 | int stopped; |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 2497 | struct tcb *tcp; |
| 2498 | unsigned event; |
| 2499 | |
Denys Vlasenko | 222713a | 2009-03-17 14:29:59 +0000 | [diff] [blame] | 2500 | if (interrupted) |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2501 | return 0; |
| 2502 | if (interactive) |
| 2503 | sigprocmask(SIG_SETMASK, &empty_set, NULL); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2504 | #ifdef LINUX |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame] | 2505 | # ifdef __WALL |
| 2506 | pid = wait4(-1, &status, wait4_options, rup); |
Roland McGrath | 5bc0555 | 2002-12-17 04:50:47 +0000 | [diff] [blame] | 2507 | if (pid < 0 && (wait4_options & __WALL) && errno == EINVAL) { |
Wichert Akkerman | 2f1d87e | 2001-03-28 14:40:14 +0000 | [diff] [blame] | 2508 | /* this kernel does not support __WALL */ |
| 2509 | wait4_options &= ~__WALL; |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame] | 2510 | pid = wait4(-1, &status, wait4_options, rup); |
Wichert Akkerman | 2f1d87e | 2001-03-28 14:40:14 +0000 | [diff] [blame] | 2511 | } |
Roland McGrath | 5bc0555 | 2002-12-17 04:50:47 +0000 | [diff] [blame] | 2512 | if (pid < 0 && !(wait4_options & __WALL) && errno == ECHILD) { |
Wichert Akkerman | 2f1d87e | 2001-03-28 14:40:14 +0000 | [diff] [blame] | 2513 | /* most likely a "cloned" process */ |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame] | 2514 | pid = wait4(-1, &status, __WCLONE, rup); |
| 2515 | if (pid < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2516 | perror_msg("wait4(__WCLONE) failed"); |
Wichert Akkerman | 2f1d87e | 2001-03-28 14:40:14 +0000 | [diff] [blame] | 2517 | } |
| 2518 | } |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame] | 2519 | # else |
| 2520 | pid = wait4(-1, &status, 0, rup); |
| 2521 | # endif /* __WALL */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2522 | #endif /* LINUX */ |
| 2523 | #ifdef SUNOS4 |
| 2524 | pid = wait(&status); |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame] | 2525 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2526 | wait_errno = errno; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2527 | if (interactive) |
| 2528 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2529 | |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame] | 2530 | if (pid < 0) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2531 | switch (wait_errno) { |
| 2532 | case EINTR: |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2533 | continue; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2534 | case ECHILD: |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2535 | /* |
| 2536 | * We would like to verify this case |
| 2537 | * but sometimes a race in Solbourne's |
| 2538 | * version of SunOS sometimes reports |
| 2539 | * ECHILD before sending us SIGCHILD. |
| 2540 | */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2541 | return 0; |
| 2542 | default: |
| 2543 | errno = wait_errno; |
| 2544 | perror("strace: wait"); |
| 2545 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2546 | } |
| 2547 | } |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 2548 | if (pid == popen_pid) { |
| 2549 | if (WIFEXITED(status) || WIFSIGNALED(status)) |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 2550 | popen_pid = 0; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 2551 | continue; |
| 2552 | } |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 2553 | |
| 2554 | event = ((unsigned)status >> 16); |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 2555 | if (debug) { |
| 2556 | char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16]; |
| 2557 | #ifdef LINUX |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 2558 | if (event != 0) { |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 2559 | static const char *const event_names[] = { |
| 2560 | [PTRACE_EVENT_CLONE] = "CLONE", |
| 2561 | [PTRACE_EVENT_FORK] = "FORK", |
| 2562 | [PTRACE_EVENT_VFORK] = "VFORK", |
| 2563 | [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE", |
| 2564 | [PTRACE_EVENT_EXEC] = "EXEC", |
| 2565 | [PTRACE_EVENT_EXIT] = "EXIT", |
| 2566 | }; |
| 2567 | const char *e; |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 2568 | if (event < ARRAY_SIZE(event_names)) |
| 2569 | e = event_names[event]; |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 2570 | else { |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 2571 | sprintf(buf, "?? (%u)", event); |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 2572 | e = buf; |
| 2573 | } |
| 2574 | fprintf(stderr, " PTRACE_EVENT_%s", e); |
| 2575 | } |
| 2576 | #endif |
| 2577 | strcpy(buf, "???"); |
| 2578 | if (WIFSIGNALED(status)) |
| 2579 | #ifdef WCOREDUMP |
| 2580 | sprintf(buf, "WIFSIGNALED,%ssig=%s", |
| 2581 | WCOREDUMP(status) ? "core," : "", |
| 2582 | signame(WTERMSIG(status))); |
| 2583 | #else |
| 2584 | sprintf(buf, "WIFSIGNALED,sig=%s", |
| 2585 | signame(WTERMSIG(status))); |
| 2586 | #endif |
| 2587 | if (WIFEXITED(status)) |
| 2588 | sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status)); |
| 2589 | if (WIFSTOPPED(status)) |
| 2590 | sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status))); |
Denys Vlasenko | 5bd67c8 | 2011-08-15 11:36:09 +0200 | [diff] [blame] | 2591 | #ifdef WIFCONTINUED |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 2592 | if (WIFCONTINUED(status)) |
| 2593 | strcpy(buf, "WIFCONTINUED"); |
Denys Vlasenko | 5bd67c8 | 2011-08-15 11:36:09 +0200 | [diff] [blame] | 2594 | #endif |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 2595 | fprintf(stderr, " [wait(0x%04x) = %u] %s\n", status, pid, buf); |
| 2596 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2597 | |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 2598 | /* Look up 'pid' in our table. */ |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 2599 | tcp = pid2tcb(pid); |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 2600 | |
| 2601 | #ifdef LINUX |
| 2602 | /* Under Linux, execve changes pid to thread leader's pid, |
| 2603 | * and we see this changed pid on EVENT_EXEC and later, |
| 2604 | * execve sysexit. Leader "disappears" without exit |
| 2605 | * notification. Let user know that, drop leader's tcb, |
| 2606 | * and fix up pid in execve thread's tcb. |
| 2607 | * Effectively, execve thread's tcb replaces leader's tcb. |
| 2608 | * |
| 2609 | * BTW, leader is 'stuck undead' (doesn't report WIFEXITED |
| 2610 | * on exit syscall) in multithreaded programs exactly |
| 2611 | * in order to handle this case. |
| 2612 | * |
| 2613 | * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0. |
| 2614 | * On 2.6 and earlier, it can return garbage. |
| 2615 | */ |
| 2616 | if (event == PTRACE_EVENT_EXEC && os_release[0] >= '3') { |
| 2617 | long old_pid = 0; |
| 2618 | if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) >= 0 |
| 2619 | && old_pid > 0 |
| 2620 | && old_pid != pid |
| 2621 | ) { |
| 2622 | struct tcb *execve_thread = pid2tcb(old_pid); |
| 2623 | if (tcp) { |
| 2624 | outf = tcp->outf; |
| 2625 | curcol = tcp->curcol; |
| 2626 | if (!cflag) { |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2627 | if (printing_tcp) |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 2628 | tprints(" <unfinished ...>\n"); |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 2629 | printleader(tcp); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2630 | tprintf("+++ superseded by execve in pid %lu +++\n", old_pid); |
| 2631 | printing_tcp = NULL; |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 2632 | fflush(outf); |
| 2633 | } |
| 2634 | if (execve_thread) { |
| 2635 | /* swap output FILEs (needed for -ff) */ |
| 2636 | tcp->outf = execve_thread->outf; |
| 2637 | execve_thread->outf = outf; |
| 2638 | } |
| 2639 | droptcb(tcp); |
| 2640 | } |
| 2641 | tcp = execve_thread; |
| 2642 | if (tcp) { |
| 2643 | tcp->pid = pid; |
| 2644 | tcp->flags |= TCB_REPRINT; |
| 2645 | } |
| 2646 | } |
| 2647 | } |
| 2648 | #endif |
| 2649 | |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 2650 | if (tcp == NULL) { |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2651 | #ifdef LINUX |
Roland McGrath | 41c4822 | 2008-07-18 00:25:10 +0000 | [diff] [blame] | 2652 | if (followfork) { |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2653 | /* This is needed to go with the CLONE_PTRACE |
| 2654 | changes in process.c/util.c: we might see |
| 2655 | the child's initial trap before we see the |
| 2656 | parent return from the clone syscall. |
| 2657 | Leave the child suspended until the parent |
| 2658 | returns from its system call. Only then |
| 2659 | will we have the association of parent and |
| 2660 | child so that we know how to do clearbpt |
| 2661 | in the child. */ |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 2662 | tcp = alloctcb(pid); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 2663 | tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop; |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2664 | if (!qflag) |
Denys Vlasenko | 833fb13 | 2011-08-17 11:30:56 +0200 | [diff] [blame] | 2665 | fprintf(stderr, "Process %d attached\n", |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2666 | pid); |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 2667 | } |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2668 | else |
| 2669 | /* This can happen if a clone call used |
| 2670 | CLONE_PTRACE itself. */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2671 | #endif |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2672 | { |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2673 | if (WIFSTOPPED(status)) |
| 2674 | ptrace(PTRACE_CONT, pid, (char *) 1, 0); |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2675 | error_msg_and_die("Unknown pid: %u", pid); |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2676 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2677 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2678 | /* set current output file */ |
| 2679 | outf = tcp->outf; |
Andreas Schwab | ccdff48 | 2009-10-27 16:27:13 +0100 | [diff] [blame] | 2680 | curcol = tcp->curcol; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2681 | #ifdef LINUX |
Denys Vlasenko | 13d22f1 | 2011-06-24 23:01:57 +0200 | [diff] [blame] | 2682 | if (cflag) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2683 | tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime); |
| 2684 | tcp->stime = ru.ru_stime; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2685 | } |
Denys Vlasenko | 13d22f1 | 2011-06-24 23:01:57 +0200 | [diff] [blame] | 2686 | #endif |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2687 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2688 | if (WIFSIGNALED(status)) { |
Dmitry V. Levin | a680965 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 2689 | if (pid == strace_child) |
| 2690 | exit_code = 0x100 | WTERMSIG(status); |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 2691 | if (cflag != CFLAG_ONLY_STATS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2692 | && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) { |
| 2693 | printleader(tcp); |
Denys Vlasenko | 13d22f1 | 2011-06-24 23:01:57 +0200 | [diff] [blame] | 2694 | #ifdef WCOREDUMP |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2695 | tprintf("+++ killed by %s %s+++\n", |
Roland McGrath | 2efe879 | 2004-01-13 09:59:45 +0000 | [diff] [blame] | 2696 | signame(WTERMSIG(status)), |
Denys Vlasenko | 13d22f1 | 2011-06-24 23:01:57 +0200 | [diff] [blame] | 2697 | WCOREDUMP(status) ? "(core dumped) " : ""); |
| 2698 | #else |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2699 | tprintf("+++ killed by %s +++\n", |
Denys Vlasenko | 13d22f1 | 2011-06-24 23:01:57 +0200 | [diff] [blame] | 2700 | signame(WTERMSIG(status))); |
Roland McGrath | 2efe879 | 2004-01-13 09:59:45 +0000 | [diff] [blame] | 2701 | #endif |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2702 | printing_tcp = NULL; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2703 | } |
Denys Vlasenko | 5e09d77 | 2012-01-18 16:20:56 +0100 | [diff] [blame] | 2704 | fflush(tcp->outf); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2705 | droptcb(tcp); |
| 2706 | continue; |
| 2707 | } |
| 2708 | if (WIFEXITED(status)) { |
Dmitry V. Levin | a680965 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 2709 | if (pid == strace_child) |
| 2710 | exit_code = WEXITSTATUS(status); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2711 | if (tcp == printing_tcp) { |
| 2712 | tprints(" <unfinished ...>\n"); |
| 2713 | printing_tcp = NULL; |
Roland McGrath | 0a39690 | 2003-06-10 03:05:53 +0000 | [diff] [blame] | 2714 | } |
Denys Vlasenko | 19cdada | 2011-08-17 10:45:32 +0200 | [diff] [blame] | 2715 | if (!cflag /* && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL) */ ) { |
| 2716 | printleader(tcp); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2717 | tprintf("+++ exited with %d +++\n", WEXITSTATUS(status)); |
| 2718 | printing_tcp = NULL; |
Denys Vlasenko | 19cdada | 2011-08-17 10:45:32 +0200 | [diff] [blame] | 2719 | } |
Denys Vlasenko | 5e09d77 | 2012-01-18 16:20:56 +0100 | [diff] [blame] | 2720 | fflush(tcp->outf); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2721 | droptcb(tcp); |
| 2722 | continue; |
| 2723 | } |
| 2724 | if (!WIFSTOPPED(status)) { |
| 2725 | fprintf(stderr, "PANIC: pid %u not stopped\n", pid); |
| 2726 | droptcb(tcp); |
| 2727 | continue; |
| 2728 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2729 | |
Denys Vlasenko | f88837a | 2011-09-05 14:05:46 +0200 | [diff] [blame] | 2730 | /* Is this the very first time we see this tracee stopped? */ |
| 2731 | if (tcp->flags & TCB_STARTUP) { |
| 2732 | if (debug) |
| 2733 | fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n", tcp->pid); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2734 | tcp->flags &= ~TCB_STARTUP; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2735 | if (tcp->flags & TCB_BPTSET) { |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 2736 | /* |
| 2737 | * One example is a breakpoint inherited from |
Denys Vlasenko | 2ecba32 | 2011-08-21 17:35:39 +0200 | [diff] [blame] | 2738 | * parent through fork(). |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 2739 | */ |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2740 | if (clearbpt(tcp) < 0) { |
| 2741 | /* Pretty fatal */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2742 | droptcb(tcp); |
| 2743 | cleanup(); |
| 2744 | return -1; |
| 2745 | } |
| 2746 | } |
Wang Chao | ca8ab8d | 2010-11-12 17:26:08 +0800 | [diff] [blame] | 2747 | #ifdef LINUX |
Denys Vlasenko | 44f87ef | 2011-08-17 15:18:21 +0200 | [diff] [blame] | 2748 | if (ptrace_setoptions) { |
| 2749 | if (debug) |
| 2750 | fprintf(stderr, "setting opts %x on pid %d\n", ptrace_setoptions, tcp->pid); |
| 2751 | if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) { |
| 2752 | if (errno != ESRCH) { |
| 2753 | /* Should never happen, really */ |
| 2754 | perror_msg_and_die("PTRACE_SETOPTIONS"); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 2755 | } |
| 2756 | } |
| 2757 | } |
Wang Chao | ca8ab8d | 2010-11-12 17:26:08 +0800 | [diff] [blame] | 2758 | #endif |
Denys Vlasenko | f88837a | 2011-09-05 14:05:46 +0200 | [diff] [blame] | 2759 | } |
| 2760 | |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 2761 | sig = WSTOPSIG(status); |
| 2762 | |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 2763 | if (event != 0) { |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 2764 | /* Ptrace event */ |
| 2765 | #ifdef USE_SEIZE |
| 2766 | if (event == PTRACE_EVENT_STOP || event == PTRACE_EVENT_STOP1) { |
Denys Vlasenko | 6703816 | 2012-01-29 16:46:46 +0100 | [diff] [blame] | 2767 | /* |
| 2768 | * PTRACE_INTERRUPT-stop or group-stop. |
| 2769 | * PTRACE_INTERRUPT-stop has sig == SIGTRAP here. |
| 2770 | */ |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 2771 | if (sig == SIGSTOP |
| 2772 | || sig == SIGTSTP |
| 2773 | || sig == SIGTTIN |
| 2774 | || sig == SIGTTOU |
| 2775 | ) { |
| 2776 | stopped = 1; |
| 2777 | goto show_stopsig; |
| 2778 | } |
| 2779 | } |
| 2780 | #endif |
Denys Vlasenko | f88837a | 2011-09-05 14:05:46 +0200 | [diff] [blame] | 2781 | goto restart_tracee_with_sig_0; |
| 2782 | } |
| 2783 | |
Denys Vlasenko | f88837a | 2011-09-05 14:05:46 +0200 | [diff] [blame] | 2784 | /* Is this post-attach SIGSTOP? |
| 2785 | * Interestingly, the process may stop |
| 2786 | * with STOPSIG equal to some other signal |
| 2787 | * than SIGSTOP if we happend to attach |
| 2788 | * just before the process takes a signal. |
| 2789 | */ |
| 2790 | if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) { |
| 2791 | if (debug) |
| 2792 | fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid); |
| 2793 | tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP; |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2794 | goto restart_tracee_with_sig_0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2797 | if (sig != syscall_trap_sig) { |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 2798 | siginfo_t si; |
| 2799 | |
| 2800 | /* Nonzero (true) if tracee is stopped by signal |
| 2801 | * (as opposed to "tracee received signal"). |
| 2802 | */ |
| 2803 | stopped = (ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0); |
Denys Vlasenko | 6703816 | 2012-01-29 16:46:46 +0100 | [diff] [blame] | 2804 | #ifdef USE_SEIZE |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 2805 | show_stopsig: |
Denys Vlasenko | 6703816 | 2012-01-29 16:46:46 +0100 | [diff] [blame] | 2806 | #endif |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 2807 | if (cflag != CFLAG_ONLY_STATS |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2808 | && (qual_flags[sig] & QUAL_SIGNAL)) { |
Dmitry V. Levin | c15dfc7 | 2011-03-10 14:44:45 +0000 | [diff] [blame] | 2809 | #if defined(PT_CR_IPSR) && defined(PT_CR_IIP) |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2810 | long pc = 0; |
| 2811 | long psr = 0; |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 2812 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2813 | upeek(tcp, PT_CR_IPSR, &psr); |
| 2814 | upeek(tcp, PT_CR_IIP, &pc); |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 2815 | |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2816 | # define PSR_RI 41 |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 2817 | pc += (psr >> PSR_RI) & 0x3; |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2818 | # define PC_FORMAT_STR " @ %lx" |
Denys Vlasenko | 2ecba32 | 2011-08-21 17:35:39 +0200 | [diff] [blame] | 2819 | # define PC_FORMAT_ARG , pc |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2820 | #else |
Denys Vlasenko | 2ecba32 | 2011-08-21 17:35:39 +0200 | [diff] [blame] | 2821 | # define PC_FORMAT_STR "" |
| 2822 | # define PC_FORMAT_ARG /* nothing */ |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 2823 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2824 | printleader(tcp); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 2825 | if (!stopped) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 2826 | tprints("--- "); |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2827 | printsiginfo(&si, verbose(tcp)); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2828 | tprintf(" (%s)" PC_FORMAT_STR " ---\n", |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2829 | strsignal(sig) |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2830 | PC_FORMAT_ARG); |
| 2831 | } else |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2832 | tprintf("--- %s by %s" PC_FORMAT_STR " ---\n", |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2833 | strsignal(sig), |
| 2834 | signame(sig) |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2835 | PC_FORMAT_ARG); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2836 | printing_tcp = NULL; |
Denys Vlasenko | 5e09d77 | 2012-01-18 16:20:56 +0100 | [diff] [blame] | 2837 | fflush(tcp->outf); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2838 | } |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 2839 | |
| 2840 | if (!stopped) |
| 2841 | /* It's signal-delivery-stop. Inject the signal */ |
| 2842 | goto restart_tracee; |
| 2843 | |
| 2844 | /* It's group-stop */ |
| 2845 | #ifdef USE_SEIZE |
| 2846 | if (use_seize) { |
| 2847 | /* |
| 2848 | * This ends ptrace-stop, but does *not* end group-stop. |
| 2849 | * This makes stopping signals work properly on straced process |
| 2850 | * (that is, process really stops. It used to continue to run). |
| 2851 | */ |
| 2852 | if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) { |
| 2853 | cleanup(); |
| 2854 | return -1; |
| 2855 | } |
| 2856 | continue; |
| 2857 | } |
| 2858 | /* We don't have PTRACE_LISTEN support... */ |
| 2859 | #endif |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2860 | goto restart_tracee; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2861 | } |
Denys Vlasenko | 2ecba32 | 2011-08-21 17:35:39 +0200 | [diff] [blame] | 2862 | |
| 2863 | /* We handled quick cases, we are permitted to interrupt now. */ |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 2864 | if (interrupted) |
| 2865 | return 0; |
Denys Vlasenko | 2ecba32 | 2011-08-21 17:35:39 +0200 | [diff] [blame] | 2866 | |
| 2867 | /* This should be syscall entry or exit. |
| 2868 | * (Or it still can be that pesky post-execve SIGTRAP!) |
| 2869 | * Handle it. |
| 2870 | */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2871 | if (trace_syscall(tcp) < 0 && !tcp->ptrace_errno) { |
| 2872 | /* ptrace() failed in trace_syscall() with ESRCH. |
| 2873 | * Likely a result of process disappearing mid-flight. |
| 2874 | * Observed case: exit_group() terminating |
Denys Vlasenko | f1e6903 | 2012-01-04 15:15:26 +0100 | [diff] [blame] | 2875 | * all processes in thread group. |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 2876 | */ |
| 2877 | if (tcp->flags & TCB_ATTACHED) { |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2878 | if (printing_tcp) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2879 | /* Do we have dangling line "syscall(param, param"? |
Denys Vlasenko | 178de00 | 2011-06-24 22:54:25 +0200 | [diff] [blame] | 2880 | * Finish the line then. |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2881 | */ |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2882 | printing_tcp->flags |= TCB_REPRINT; |
| 2883 | tprints(" <unfinished ...>\n"); |
| 2884 | printing_tcp = NULL; |
Denys Vlasenko | 5e09d77 | 2012-01-18 16:20:56 +0100 | [diff] [blame] | 2885 | fflush(tcp->outf); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2886 | } |
Denys Vlasenko | f1e6903 | 2012-01-04 15:15:26 +0100 | [diff] [blame] | 2887 | /* We assume that ptrace error was caused by process death. |
| 2888 | * We used to detach(tcp) here, but since we no longer |
| 2889 | * implement "detach before death" policy/hack, |
| 2890 | * we can let this process to report its death to us |
| 2891 | * normally, via WIFEXITED or WIFSIGNALED wait status. |
| 2892 | */ |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 2893 | } else { |
Denys Vlasenko | f1e6903 | 2012-01-04 15:15:26 +0100 | [diff] [blame] | 2894 | /* It's our real child (and we also trace it) */ |
| 2895 | /* my_tkill(pid, SIGKILL); - why? */ |
| 2896 | /* droptcb(tcp); - why? */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2897 | } |
| 2898 | continue; |
| 2899 | } |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2900 | restart_tracee_with_sig_0: |
| 2901 | sig = 0; |
| 2902 | restart_tracee: |
Andreas Schwab | ccdff48 | 2009-10-27 16:27:13 +0100 | [diff] [blame] | 2903 | /* Remember current print column before continuing. */ |
| 2904 | tcp->curcol = curcol; |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2905 | if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2906 | cleanup(); |
| 2907 | return -1; |
| 2908 | } |
| 2909 | } |
| 2910 | return 0; |
| 2911 | } |
| 2912 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2913 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2914 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2915 | void |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2916 | tprintf(const char *fmt, ...) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2917 | { |
| 2918 | va_list args; |
| 2919 | |
Andreas Schwab | e5355de | 2009-10-27 16:56:43 +0100 | [diff] [blame] | 2920 | va_start(args, fmt); |
Roland McGrath | b310a0c | 2003-11-06 23:41:22 +0000 | [diff] [blame] | 2921 | if (outf) { |
| 2922 | int n = vfprintf(outf, fmt, args); |
Andreas Schwab | ccdff48 | 2009-10-27 16:27:13 +0100 | [diff] [blame] | 2923 | if (n < 0) { |
| 2924 | if (outf != stderr) |
| 2925 | perror(outfname == NULL |
| 2926 | ? "<writing to pipe>" : outfname); |
| 2927 | } else |
Roland McGrath | b310a0c | 2003-11-06 23:41:22 +0000 | [diff] [blame] | 2928 | curcol += n; |
| 2929 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2930 | va_end(args); |
Denys Vlasenko | 5940e65 | 2011-09-01 09:55:05 +0200 | [diff] [blame] | 2931 | } |
| 2932 | |
| 2933 | void |
| 2934 | tprints(const char *str) |
| 2935 | { |
| 2936 | if (outf) { |
| 2937 | int n = fputs(str, outf); |
| 2938 | if (n >= 0) { |
| 2939 | curcol += strlen(str); |
| 2940 | return; |
| 2941 | } |
| 2942 | if (outf != stderr) |
| 2943 | perror(outfname == NULL |
| 2944 | ? "<writing to pipe>" : outfname); |
| 2945 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 2949 | printleader(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2950 | { |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2951 | if (printing_tcp) { |
| 2952 | if (printing_tcp->ptrace_errno) { |
| 2953 | if (printing_tcp->flags & TCB_INSYSCALL) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 2954 | tprints(" <unavailable>) "); |
Denys Vlasenko | 102ec49 | 2011-08-25 01:27:59 +0200 | [diff] [blame] | 2955 | tabto(); |
Denys Vlasenko | 7e0615f | 2009-01-28 19:00:54 +0000 | [diff] [blame] | 2956 | } |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 2957 | tprints("= ? <unavailable>\n"); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2958 | printing_tcp->ptrace_errno = 0; |
| 2959 | } else if (!outfname || followfork < 2 || printing_tcp == tcp) { |
| 2960 | printing_tcp->flags |= TCB_REPRINT; |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 2961 | tprints(" <unfinished ...>\n"); |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 2962 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2963 | } |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2964 | |
| 2965 | printing_tcp = tcp; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2966 | curcol = 0; |
| 2967 | if ((followfork == 1 || pflag_seen > 1) && outfname) |
| 2968 | tprintf("%-5d ", tcp->pid); |
| 2969 | else if (nprocs > 1 && !outfname) |
| 2970 | tprintf("[pid %5u] ", tcp->pid); |
| 2971 | if (tflag) { |
| 2972 | char str[sizeof("HH:MM:SS")]; |
| 2973 | struct timeval tv, dtv; |
| 2974 | static struct timeval otv; |
| 2975 | |
| 2976 | gettimeofday(&tv, NULL); |
| 2977 | if (rflag) { |
| 2978 | if (otv.tv_sec == 0) |
| 2979 | otv = tv; |
| 2980 | tv_sub(&dtv, &tv, &otv); |
| 2981 | tprintf("%6ld.%06ld ", |
| 2982 | (long) dtv.tv_sec, (long) dtv.tv_usec); |
| 2983 | otv = tv; |
| 2984 | } |
| 2985 | else if (tflag > 2) { |
| 2986 | tprintf("%ld.%06ld ", |
| 2987 | (long) tv.tv_sec, (long) tv.tv_usec); |
| 2988 | } |
| 2989 | else { |
| 2990 | time_t local = tv.tv_sec; |
| 2991 | strftime(str, sizeof(str), "%T", localtime(&local)); |
| 2992 | if (tflag > 1) |
| 2993 | tprintf("%s.%06ld ", str, (long) tv.tv_usec); |
| 2994 | else |
| 2995 | tprintf("%s ", str); |
| 2996 | } |
| 2997 | } |
| 2998 | if (iflag) |
| 2999 | printcall(tcp); |
| 3000 | } |
| 3001 | |
| 3002 | void |
Denys Vlasenko | 102ec49 | 2011-08-25 01:27:59 +0200 | [diff] [blame] | 3003 | tabto(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 3004 | { |
Denys Vlasenko | 102ec49 | 2011-08-25 01:27:59 +0200 | [diff] [blame] | 3005 | if (curcol < acolumn) |
Denys Vlasenko | 5940e65 | 2011-09-01 09:55:05 +0200 | [diff] [blame] | 3006 | tprints(acolumn_spaces + curcol); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 3009 | #ifdef HAVE_MP_PROCFS |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 3010 | |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 3011 | int |
| 3012 | mp_ioctl(int fd, int cmd, void *arg, int size) |
| 3013 | { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 3014 | struct iovec iov[2]; |
| 3015 | int n = 1; |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 3016 | |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 3017 | iov[0].iov_base = &cmd; |
| 3018 | iov[0].iov_len = sizeof cmd; |
| 3019 | if (arg) { |
| 3020 | ++n; |
| 3021 | iov[1].iov_base = arg; |
| 3022 | iov[1].iov_len = size; |
| 3023 | } |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 3024 | |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 3025 | return writev(fd, iov, n); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
| 3028 | #endif |