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 | # include <asm/unistd.h> |
Denys Vlasenko | 44f87ef | 2011-08-17 15:18:21 +0200 | [diff] [blame] | 51 | # if defined __NR_tkill |
| 52 | # define my_tkill(tid, sig) syscall(__NR_tkill, (tid), (sig)) |
Roland McGrath | 134813a | 2007-06-02 00:07:33 +0000 | [diff] [blame] | 53 | # else |
| 54 | /* kill() may choose arbitrarily the target task of the process group |
| 55 | while we later wait on a that specific TID. PID process waits become |
| 56 | TID task specific waits for a process under ptrace(2). */ |
| 57 | # warning "Neither tkill(2) nor tgkill(2) available, risk of strace hangs!" |
Denys Vlasenko | 44f87ef | 2011-08-17 15:18:21 +0200 | [diff] [blame] | 58 | # define my_tkill(tid, sig) kill((tid), (sig)) |
Roland McGrath | 134813a | 2007-06-02 00:07:33 +0000 | [diff] [blame] | 59 | # endif |
Roland McGrath | 134813a | 2007-06-02 00:07:33 +0000 | [diff] [blame] | 60 | |
Denys Vlasenko | 8470374 | 2012-02-25 02:38:52 +0100 | [diff] [blame] | 61 | #if defined(IA64) |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 62 | # include <asm/ptrace_offsets.h> |
| 63 | #endif |
| 64 | |
Denys Vlasenko | 96d5a76 | 2008-12-29 19:13:27 +0000 | [diff] [blame] | 65 | extern char **environ; |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 66 | extern int optind; |
| 67 | extern char *optarg; |
Denys Vlasenko | 96d5a76 | 2008-12-29 19:13:27 +0000 | [diff] [blame] | 68 | |
Roland McGrath | 41c4822 | 2008-07-18 00:25:10 +0000 | [diff] [blame] | 69 | int debug = 0, followfork = 0; |
Denys Vlasenko | f44cce4 | 2011-06-21 14:34:10 +0200 | [diff] [blame] | 70 | unsigned int ptrace_setoptions = 0; |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 71 | /* Which WSTOPSIG(status) value marks syscall traps? */ |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 72 | static unsigned int syscall_trap_sig = SIGTRAP; |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 73 | int dtime = 0, xflag = 0, qflag = 0; |
| 74 | cflag_t cflag = CFLAG_NONE; |
Denys Vlasenko | fd88338 | 2012-03-09 13:03:41 +0100 | [diff] [blame] | 75 | static int iflag = 0, rflag = 0, tflag = 0; |
| 76 | static int print_pid_pfx = 0; |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 77 | |
| 78 | /* -I n */ |
| 79 | enum { |
| 80 | INTR_NOT_SET = 0, |
| 81 | INTR_ANYWHERE = 1, /* don't block/ignore any signals */ |
| 82 | INTR_WHILE_WAIT = 2, /* block fatal signals while decoding syscall. default */ |
| 83 | INTR_NEVER = 3, /* block fatal signals. default if '-o FILE PROG' */ |
| 84 | INTR_BLOCK_TSTP_TOO = 4, /* block fatal signals and SIGTSTP (^Z) */ |
| 85 | NUM_INTR_OPTS |
| 86 | }; |
| 87 | static int opt_intr; |
| 88 | /* We play with signal mask only if this mode is active: */ |
| 89 | #define interactive (opt_intr == INTR_WHILE_WAIT) |
| 90 | |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 91 | /* |
| 92 | * daemonized_tracer supports -D option. |
| 93 | * With this option, strace forks twice. |
| 94 | * Unlike normal case, with -D *grandparent* process exec's, |
| 95 | * becoming a traced process. Child exits (this prevents traced process |
| 96 | * from having children it doesn't expect to have), and grandchild |
| 97 | * attaches to grandparent similarly to strace -p PID. |
| 98 | * This allows for more transparent interaction in cases |
| 99 | * when process and its parent are communicating via signals, |
| 100 | * wait() etc. Without -D, strace process gets lodged in between, |
| 101 | * disrupting parent<->child link. |
| 102 | */ |
| 103 | static bool daemonized_tracer = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 104 | |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 105 | #ifdef USE_SEIZE |
| 106 | static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP; |
| 107 | # define use_seize (post_attach_sigstop == 0) |
| 108 | #else |
| 109 | # define post_attach_sigstop TCB_IGNORE_ONE_SIGSTOP |
| 110 | # define use_seize 0 |
| 111 | #endif |
| 112 | |
Michal Ludvig | 17f8fb3 | 2002-11-06 13:17:21 +0000 | [diff] [blame] | 113 | /* Sometimes we want to print only succeeding syscalls. */ |
| 114 | int not_failing_only = 0; |
| 115 | |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 116 | /* Show path associated with fd arguments */ |
| 117 | int show_fd_path = 0; |
| 118 | |
| 119 | /* are we filtering traces based on paths? */ |
| 120 | int tracing_paths = 0; |
| 121 | |
Dmitry V. Levin | a680965 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 122 | static int exit_code = 0; |
| 123 | static int strace_child = 0; |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 124 | static int strace_tracer_pid = 0; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 125 | |
Dmitry V. Levin | b9fe011 | 2006-12-13 16:59:44 +0000 | [diff] [blame] | 126 | static char *username = NULL; |
Denys Vlasenko | ead73bd | 2011-06-24 22:49:58 +0200 | [diff] [blame] | 127 | static uid_t run_uid; |
| 128 | static gid_t run_gid; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 129 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 130 | int max_strlen = DEFAULT_STRLEN; |
Denys Vlasenko | 102ec49 | 2011-08-25 01:27:59 +0200 | [diff] [blame] | 131 | static int acolumn = DEFAULT_ACOLUMN; |
| 132 | static char *acolumn_spaces; |
Dmitry V. Levin | b9fe011 | 2006-12-13 16:59:44 +0000 | [diff] [blame] | 133 | static char *outfname = NULL; |
Denys Vlasenko | ead73bd | 2011-06-24 22:49:58 +0200 | [diff] [blame] | 134 | static FILE *outf; |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 135 | struct tcb *printing_tcp = NULL; |
Andreas Schwab | ccdff48 | 2009-10-27 16:27:13 +0100 | [diff] [blame] | 136 | static int curcol; |
Denys Vlasenko | ead73bd | 2011-06-24 22:49:58 +0200 | [diff] [blame] | 137 | static struct tcb **tcbtab; |
Denys Vlasenko | 2b60c35 | 2011-06-22 12:45:25 +0200 | [diff] [blame] | 138 | static unsigned int nprocs, tcbtabsize; |
Denys Vlasenko | ead73bd | 2011-06-24 22:49:58 +0200 | [diff] [blame] | 139 | static const char *progname; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 140 | |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 141 | static char *os_release; /* from uname() */ |
| 142 | |
Denys Vlasenko | 4c19638 | 2012-01-04 15:11:09 +0100 | [diff] [blame] | 143 | static int detach(struct tcb *tcp); |
Andreas Schwab | e5355de | 2009-10-27 16:56:43 +0100 | [diff] [blame] | 144 | static int trace(void); |
| 145 | static void cleanup(void); |
| 146 | static void interrupt(int sig); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 147 | static sigset_t empty_set, blocked_set; |
| 148 | |
| 149 | #ifdef HAVE_SIG_ATOMIC_T |
| 150 | static volatile sig_atomic_t interrupted; |
Denys Vlasenko | a355925 | 2012-01-29 16:43:51 +0100 | [diff] [blame] | 151 | #else |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 152 | static volatile int interrupted; |
Denys Vlasenko | a355925 | 2012-01-29 16:43:51 +0100 | [diff] [blame] | 153 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 154 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 155 | static void |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 156 | usage(FILE *ofp, int exitval) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 157 | { |
| 158 | fprintf(ofp, "\ |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 159 | usage: strace [-CdDffhiqrtttTvVxxy] [-I n] [-a column] [-e expr]... [-o file]\n\ |
| 160 | [-p pid]... [-s strsize] [-u username] [-E var=val]...\n\ |
Denys Vlasenko | cdab1be | 2012-02-03 12:17:57 +0100 | [diff] [blame] | 161 | [-P path] [PROG [ARGS]]\n\ |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 162 | or: strace -c [-D] [-I n] [-e expr]... [-O overhead] [-S sortby] [-E var=val]...\n\ |
Denys Vlasenko | cdab1be | 2012-02-03 12:17:57 +0100 | [diff] [blame] | 163 | [PROG [ARGS]]\n\ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 164 | -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] | 165 | -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] | 166 | -D -- run tracer process as a detached grandchild, not as parent\n\ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 167 | -f -- follow forks, -ff -- with output into separate files\n\ |
Denys Vlasenko | cdab1be | 2012-02-03 12:17:57 +0100 | [diff] [blame] | 168 | -F -- attempt to follow vforks\n\ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 169 | -i -- print instruction pointer at time of syscall\n\ |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 170 | -I interruptible\n\ |
| 171 | 1: no signals are blocked\n\ |
| 172 | 2: fatal signals are blocked while decoding syscall (default)\n\ |
| 173 | 3: fatal signals are always blocked (default if '-o FILE PROG')\n\ |
| 174 | 4: fatal signals and SIGTSTP (^Z) are always blocked\n\ |
| 175 | (useful to make 'strace -o FILE PROG' not stop on ^Z)\n\ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 176 | -q -- suppress messages about attaching, detaching, etc.\n\ |
| 177 | -r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\ |
Denys Vlasenko | cdab1be | 2012-02-03 12:17:57 +0100 | [diff] [blame] | 178 | -T -- print time spent in each syscall\n\ |
| 179 | -v -- verbose mode: print unabbreviated argv, stat, termios, etc. args\n\ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 180 | -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] | 181 | -y -- print paths associated with file descriptor arguments\n\ |
Denys Vlasenko | cdab1be | 2012-02-03 12:17:57 +0100 | [diff] [blame] | 182 | -h -- print help message\n\ |
| 183 | -V -- print version\n\ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 184 | -a column -- alignment COLUMN for printing syscall results (default %d)\n\ |
| 185 | -e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\ |
| 186 | options: trace, abbrev, verbose, raw, signal, read, or write\n\ |
| 187 | -o file -- send trace output to FILE instead of stderr\n\ |
| 188 | -O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\ |
| 189 | -p pid -- trace process with process id PID, may be repeated\n\ |
| 190 | -s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\ |
| 191 | -S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\ |
| 192 | -u username -- run command as username handling setuid and/or setgid\n\ |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 193 | -E var=val -- put var=val in the environment for command\n\ |
| 194 | -E var -- remove var from the environment for command\n\ |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 195 | -P path -- trace accesses to path\n\ |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 196 | " /* this is broken, so don't document it |
Michal Ludvig | 17f8fb3 | 2002-11-06 13:17:21 +0000 | [diff] [blame] | 197 | -z -- print only succeeding syscalls\n\ |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 198 | */ |
| 199 | , DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 200 | exit(exitval); |
| 201 | } |
| 202 | |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 203 | static void die(void) __attribute__ ((noreturn)); |
| 204 | static void die(void) |
| 205 | { |
| 206 | if (strace_tracer_pid == getpid()) { |
| 207 | cflag = 0; |
| 208 | cleanup(); |
| 209 | } |
| 210 | exit(1); |
| 211 | } |
| 212 | |
| 213 | 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] | 214 | { |
Denys Vlasenko | 82bb78c | 2012-01-24 10:17:18 +0100 | [diff] [blame] | 215 | char *msg; |
| 216 | |
Dmitry V. Levin | 44d0532 | 2011-06-09 15:50:41 +0000 | [diff] [blame] | 217 | fflush(NULL); |
Denys Vlasenko | 82bb78c | 2012-01-24 10:17:18 +0100 | [diff] [blame] | 218 | |
| 219 | /* We want to print entire message with single fprintf to ensure |
| 220 | * message integrity if stderr is shared with other programs. |
| 221 | * Thus we use vasprintf + single fprintf. |
| 222 | */ |
| 223 | msg = NULL; |
Denys Vlasenko | cfad543 | 2012-01-24 12:48:02 +0100 | [diff] [blame] | 224 | if (vasprintf(&msg, fmt, p) >= 0) { |
Denys Vlasenko | 82bb78c | 2012-01-24 10:17:18 +0100 | [diff] [blame] | 225 | if (err_no) |
| 226 | fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(err_no)); |
| 227 | else |
| 228 | fprintf(stderr, "%s: %s\n", progname, msg); |
| 229 | free(msg); |
| 230 | } else { |
| 231 | /* malloc in vasprintf failed, try it without malloc */ |
| 232 | fprintf(stderr, "%s: ", progname); |
| 233 | vfprintf(stderr, fmt, p); |
| 234 | if (err_no) |
| 235 | fprintf(stderr, ": %s\n", strerror(err_no)); |
| 236 | else |
| 237 | putc('\n', stderr); |
| 238 | } |
| 239 | /* We don't switch stderr to buffered, thus fprintf(stderr) |
| 240 | * always flushes its output and this is not necessary: */ |
| 241 | /* fflush(stderr); */ |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 242 | } |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 243 | |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 244 | void error_msg(const char *fmt, ...) |
| 245 | { |
| 246 | va_list p; |
| 247 | va_start(p, fmt); |
| 248 | verror_msg(0, fmt, p); |
| 249 | va_end(p); |
| 250 | } |
| 251 | |
| 252 | void error_msg_and_die(const char *fmt, ...) |
| 253 | { |
| 254 | va_list p; |
| 255 | va_start(p, fmt); |
| 256 | verror_msg(0, fmt, p); |
| 257 | die(); |
| 258 | } |
| 259 | |
| 260 | void perror_msg(const char *fmt, ...) |
| 261 | { |
| 262 | va_list p; |
| 263 | va_start(p, fmt); |
| 264 | verror_msg(errno, fmt, p); |
| 265 | va_end(p); |
| 266 | } |
| 267 | |
| 268 | void perror_msg_and_die(const char *fmt, ...) |
| 269 | { |
| 270 | va_list p; |
| 271 | va_start(p, fmt); |
| 272 | verror_msg(errno, fmt, p); |
| 273 | die(); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 276 | void die_out_of_memory(void) |
| 277 | { |
| 278 | static bool recursed = 0; |
| 279 | if (recursed) |
| 280 | exit(1); |
| 281 | recursed = 1; |
| 282 | error_msg_and_die("Out of memory"); |
| 283 | } |
| 284 | |
Mike Frysinger | c1a5b7e | 2009-10-07 20:41:29 -0400 | [diff] [blame] | 285 | /* Glue for systems without a MMU that cannot provide fork() */ |
| 286 | #ifdef HAVE_FORK |
| 287 | # define strace_vforked 0 |
| 288 | #else |
| 289 | # define strace_vforked 1 |
| 290 | # define fork() vfork() |
| 291 | #endif |
| 292 | |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 293 | #ifdef USE_SEIZE |
| 294 | static int |
| 295 | ptrace_attach_or_seize(int pid) |
| 296 | { |
| 297 | int r; |
| 298 | if (!use_seize) |
| 299 | return ptrace(PTRACE_ATTACH, pid, 0, 0); |
| 300 | r = ptrace(PTRACE_SEIZE, pid, 0, PTRACE_SEIZE_DEVEL); |
| 301 | if (r) |
| 302 | return r; |
| 303 | r = ptrace(PTRACE_INTERRUPT, pid, 0, 0); |
| 304 | return r; |
| 305 | } |
| 306 | #else |
| 307 | # define ptrace_attach_or_seize(pid) ptrace(PTRACE_ATTACH, (pid), 0, 0) |
| 308 | #endif |
| 309 | |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 310 | static void |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 311 | set_cloexec_flag(int fd) |
| 312 | { |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 313 | int flags, newflags; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 314 | |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 315 | flags = fcntl(fd, F_GETFD); |
| 316 | if (flags < 0) { |
| 317 | /* Can happen only if fd is bad. |
| 318 | * Should never happen: if it does, we have a bug |
| 319 | * in the caller. Therefore we just abort |
| 320 | * instead of propagating the error. |
| 321 | */ |
| 322 | perror_msg_and_die("fcntl(%d, F_GETFD)", fd); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | newflags = flags | FD_CLOEXEC; |
| 326 | if (flags == newflags) |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 327 | return; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 328 | |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 329 | fcntl(fd, F_SETFD, newflags); /* never fails */ |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 332 | static void kill_save_errno(pid_t pid, int sig) |
| 333 | { |
| 334 | int saved_errno = errno; |
| 335 | |
| 336 | (void) kill(pid, sig); |
| 337 | errno = saved_errno; |
| 338 | } |
| 339 | |
Denys Vlasenko | 2e856a1 | 2012-03-12 23:02:26 +0100 | [diff] [blame] | 340 | void |
| 341 | tprintf(const char *fmt, ...) |
| 342 | { |
| 343 | va_list args; |
| 344 | |
| 345 | va_start(args, fmt); |
| 346 | if (outf) { |
| 347 | int n = vfprintf(outf, fmt, args); |
| 348 | if (n < 0) { |
| 349 | if (outf != stderr) |
| 350 | perror(outfname == NULL |
| 351 | ? "<writing to pipe>" : outfname); |
| 352 | } else |
| 353 | curcol += n; |
| 354 | } |
| 355 | va_end(args); |
| 356 | } |
| 357 | |
| 358 | void |
| 359 | tprints(const char *str) |
| 360 | { |
| 361 | if (outf) { |
| 362 | int n = fputs(str, outf); |
| 363 | if (n >= 0) { |
| 364 | curcol += strlen(str); |
| 365 | return; |
| 366 | } |
| 367 | if (outf != stderr) |
| 368 | perror(outfname == NULL |
| 369 | ? "<writing to pipe>" : outfname); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | void |
| 374 | printleader(struct tcb *tcp) |
| 375 | { |
| 376 | if (printing_tcp) { |
| 377 | if (printing_tcp->ptrace_errno) { |
| 378 | if (printing_tcp->flags & TCB_INSYSCALL) { |
| 379 | tprints(" <unavailable>) "); |
| 380 | tabto(); |
| 381 | } |
| 382 | tprints("= ? <unavailable>\n"); |
| 383 | printing_tcp->ptrace_errno = 0; |
| 384 | } else if (!outfname || followfork < 2 || printing_tcp == tcp) { |
| 385 | printing_tcp->flags |= TCB_REPRINT; |
| 386 | tprints(" <unfinished ...>\n"); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | printing_tcp = tcp; |
| 391 | curcol = 0; |
| 392 | |
| 393 | if (print_pid_pfx) |
| 394 | tprintf("%-5d ", tcp->pid); |
| 395 | else if (nprocs > 1 && !outfname) |
| 396 | tprintf("[pid %5u] ", tcp->pid); |
| 397 | |
| 398 | if (tflag) { |
| 399 | char str[sizeof("HH:MM:SS")]; |
| 400 | struct timeval tv, dtv; |
| 401 | static struct timeval otv; |
| 402 | |
| 403 | gettimeofday(&tv, NULL); |
| 404 | if (rflag) { |
| 405 | if (otv.tv_sec == 0) |
| 406 | otv = tv; |
| 407 | tv_sub(&dtv, &tv, &otv); |
| 408 | tprintf("%6ld.%06ld ", |
| 409 | (long) dtv.tv_sec, (long) dtv.tv_usec); |
| 410 | otv = tv; |
| 411 | } |
| 412 | else if (tflag > 2) { |
| 413 | tprintf("%ld.%06ld ", |
| 414 | (long) tv.tv_sec, (long) tv.tv_usec); |
| 415 | } |
| 416 | else { |
| 417 | time_t local = tv.tv_sec; |
| 418 | strftime(str, sizeof(str), "%T", localtime(&local)); |
| 419 | if (tflag > 1) |
| 420 | tprintf("%s.%06ld ", str, (long) tv.tv_usec); |
| 421 | else |
| 422 | tprintf("%s ", str); |
| 423 | } |
| 424 | } |
| 425 | if (iflag) |
| 426 | printcall(tcp); |
| 427 | } |
| 428 | |
| 429 | void |
| 430 | tabto(void) |
| 431 | { |
| 432 | if (curcol < acolumn) |
| 433 | tprints(acolumn_spaces + curcol); |
| 434 | } |
| 435 | |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 436 | /* |
| 437 | * When strace is setuid executable, we have to swap uids |
| 438 | * before and after filesystem and process management operations. |
| 439 | */ |
| 440 | static void |
| 441 | swap_uid(void) |
| 442 | { |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 443 | int euid = geteuid(), uid = getuid(); |
| 444 | |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 445 | if (euid != uid && setreuid(euid, uid) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 446 | perror_msg_and_die("setreuid"); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 447 | } |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Roland McGrath | 4bfa626 | 2007-07-05 20:03:16 +0000 | [diff] [blame] | 450 | #if _LFS64_LARGEFILE |
| 451 | # define fopen_for_output fopen64 |
| 452 | #else |
| 453 | # define fopen_for_output fopen |
| 454 | #endif |
| 455 | |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 456 | static FILE * |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 457 | strace_fopen(const char *path) |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 458 | { |
| 459 | FILE *fp; |
| 460 | |
| 461 | swap_uid(); |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 462 | fp = fopen_for_output(path, "w"); |
| 463 | if (!fp) |
| 464 | perror_msg_and_die("Can't fopen '%s'", path); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 465 | swap_uid(); |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 466 | set_cloexec_flag(fileno(fp)); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 467 | return fp; |
| 468 | } |
| 469 | |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 470 | static int popen_pid = 0; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 471 | |
| 472 | #ifndef _PATH_BSHELL |
| 473 | # define _PATH_BSHELL "/bin/sh" |
| 474 | #endif |
| 475 | |
| 476 | /* |
| 477 | * We cannot use standard popen(3) here because we have to distinguish |
| 478 | * popen child process from other processes we trace, and standard popen(3) |
| 479 | * does not export its child's pid. |
| 480 | */ |
| 481 | static FILE * |
| 482 | strace_popen(const char *command) |
| 483 | { |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 484 | FILE *fp; |
| 485 | int fds[2]; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 486 | |
| 487 | swap_uid(); |
| 488 | if (pipe(fds) < 0) |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 489 | perror_msg_and_die("pipe"); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 490 | |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 491 | set_cloexec_flag(fds[1]); /* never fails */ |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 492 | |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 493 | popen_pid = vfork(); |
| 494 | if (popen_pid == -1) |
| 495 | perror_msg_and_die("vfork"); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 496 | |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 497 | if (popen_pid == 0) { |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 498 | /* child */ |
| 499 | close(fds[1]); |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 500 | if (fds[0] != 0) { |
| 501 | if (dup2(fds[0], 0)) |
| 502 | perror_msg_and_die("dup2"); |
| 503 | close(fds[0]); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 504 | } |
| 505 | execl(_PATH_BSHELL, "sh", "-c", command, NULL); |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 506 | perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 507 | } |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 508 | |
| 509 | /* parent */ |
| 510 | close(fds[0]); |
| 511 | swap_uid(); |
| 512 | fp = fdopen(fds[1], "w"); |
| 513 | if (!fp) |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 514 | die_out_of_memory(); |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 515 | return fp; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 518 | static void |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 519 | newoutf(struct tcb *tcp) |
| 520 | { |
| 521 | if (outfname && followfork > 1) { |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 522 | char name[520 + sizeof(int) * 3]; |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 523 | sprintf(name, "%.512s.%u", outfname, tcp->pid); |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 524 | tcp->outf = strace_fopen(name); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 525 | } |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Denys Vlasenko | 558e512 | 2012-03-12 23:32:16 +0100 | [diff] [blame^] | 528 | static void |
| 529 | process_opt_p_list(char *opt) |
Denys Vlasenko | e8172b7 | 2012-03-09 13:01:04 +0100 | [diff] [blame] | 530 | { |
| 531 | while (*opt) { |
| 532 | /* |
| 533 | * We accept -p PID,PID; -p "`pidof PROG`"; -p "`pgrep PROG`". |
| 534 | * pidof uses space as delim, pgrep uses newline. :( |
| 535 | */ |
| 536 | int pid; |
Denys Vlasenko | e8172b7 | 2012-03-09 13:01:04 +0100 | [diff] [blame] | 537 | char *delim = opt + strcspn(opt, ", \n\t"); |
| 538 | char c = *delim; |
| 539 | |
| 540 | *delim = '\0'; |
| 541 | pid = atoi(opt); /* TODO: stricter parsing of the number? */ |
| 542 | if (pid <= 0) { |
| 543 | error_msg("Invalid process id: '%s'", opt); |
| 544 | *delim = c; |
| 545 | return; |
| 546 | } |
| 547 | if (pid == strace_tracer_pid) { |
| 548 | error_msg("I'm sorry, I can't let you do that, Dave."); |
| 549 | *delim = c; |
| 550 | return; |
| 551 | } |
| 552 | *delim = c; |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 553 | alloc_tcb(pid, 0); |
Denys Vlasenko | e8172b7 | 2012-03-09 13:01:04 +0100 | [diff] [blame] | 554 | if (c == '\0') |
| 555 | break; |
| 556 | opt = delim + 1; |
| 557 | } |
| 558 | } |
| 559 | |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 560 | static void |
| 561 | startup_attach(void) |
| 562 | { |
| 563 | int tcbi; |
| 564 | struct tcb *tcp; |
| 565 | |
| 566 | /* |
| 567 | * Block user interruptions as we would leave the traced |
| 568 | * process stopped (process state T) if we would terminate in |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 569 | * between PTRACE_ATTACH and wait4() on SIGSTOP. |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 570 | * We rely on cleanup() from this point on. |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 571 | */ |
| 572 | if (interactive) |
| 573 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
| 574 | |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 575 | if (daemonized_tracer) { |
| 576 | pid_t pid = fork(); |
| 577 | if (pid < 0) { |
Denys Vlasenko | 014ca3a | 2011-09-02 16:19:30 +0200 | [diff] [blame] | 578 | perror_msg_and_die("fork"); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 579 | } |
| 580 | if (pid) { /* parent */ |
| 581 | /* |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 582 | * Wait for grandchild to attach to straced process |
| 583 | * (grandparent). Grandchild SIGKILLs us after it attached. |
| 584 | * Grandparent's wait() is unblocked by our death, |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 585 | * it proceeds to exec the straced program. |
| 586 | */ |
| 587 | pause(); |
| 588 | _exit(0); /* paranoia */ |
| 589 | } |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 590 | /* grandchild */ |
| 591 | /* We will be the tracer process. Remember our new pid: */ |
| 592 | strace_tracer_pid = getpid(); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 595 | for (tcbi = 0; tcbi < tcbtabsize; tcbi++) { |
| 596 | tcp = tcbtab[tcbi]; |
Denys Vlasenko | 44f87ef | 2011-08-17 15:18:21 +0200 | [diff] [blame] | 597 | |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 598 | if (!(tcp->flags & TCB_INUSE)) |
| 599 | continue; |
| 600 | |
Denys Vlasenko | d116a73 | 2011-09-05 14:01:33 +0200 | [diff] [blame] | 601 | /* Is this a process we should attach to, but not yet attached? */ |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 602 | if (tcp->flags & TCB_ATTACHED) |
| 603 | continue; /* no, we already attached it */ |
Denys Vlasenko | d116a73 | 2011-09-05 14:01:33 +0200 | [diff] [blame] | 604 | |
| 605 | /* Reinitialize the output since it may have changed */ |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 606 | tcp->outf = outf; |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 607 | newoutf(tcp); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 608 | |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 609 | if (followfork && !daemonized_tracer) { |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 610 | char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3]; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 611 | DIR *dir; |
| 612 | |
| 613 | sprintf(procdir, "/proc/%d/task", tcp->pid); |
| 614 | dir = opendir(procdir); |
| 615 | if (dir != NULL) { |
| 616 | unsigned int ntid = 0, nerr = 0; |
| 617 | struct dirent *de; |
Denys Vlasenko | 381dbc2 | 2011-09-05 13:59:39 +0200 | [diff] [blame] | 618 | |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 619 | while ((de = readdir(dir)) != NULL) { |
Denys Vlasenko | 381dbc2 | 2011-09-05 13:59:39 +0200 | [diff] [blame] | 620 | struct tcb *cur_tcp; |
| 621 | int tid; |
| 622 | |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 623 | if (de->d_fileno == 0) |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 624 | continue; |
| 625 | tid = atoi(de->d_name); |
| 626 | if (tid <= 0) |
| 627 | continue; |
| 628 | ++ntid; |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 629 | if (ptrace_attach_or_seize(tid) < 0) { |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 630 | ++nerr; |
Denys Vlasenko | f95397a | 2011-06-24 16:51:16 +0200 | [diff] [blame] | 631 | if (debug) |
| 632 | fprintf(stderr, "attach to pid %d failed\n", tid); |
Denys Vlasenko | 381dbc2 | 2011-09-05 13:59:39 +0200 | [diff] [blame] | 633 | continue; |
Denys Vlasenko | f95397a | 2011-06-24 16:51:16 +0200 | [diff] [blame] | 634 | } |
Denys Vlasenko | 381dbc2 | 2011-09-05 13:59:39 +0200 | [diff] [blame] | 635 | if (debug) |
| 636 | fprintf(stderr, "attach to pid %d succeeded\n", tid); |
| 637 | cur_tcp = tcp; |
| 638 | if (tid != tcp->pid) |
| 639 | cur_tcp = alloctcb(tid); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 640 | cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 641 | } |
| 642 | closedir(dir); |
Denys Vlasenko | 381dbc2 | 2011-09-05 13:59:39 +0200 | [diff] [blame] | 643 | if (interactive) { |
| 644 | sigprocmask(SIG_SETMASK, &empty_set, NULL); |
| 645 | if (interrupted) |
| 646 | goto ret; |
| 647 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
| 648 | } |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 649 | ntid -= nerr; |
| 650 | if (ntid == 0) { |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 651 | perror("attach: ptrace(PTRACE_ATTACH, ...)"); |
| 652 | droptcb(tcp); |
| 653 | continue; |
| 654 | } |
| 655 | if (!qflag) { |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 656 | fprintf(stderr, ntid > 1 |
| 657 | ? "Process %u attached with %u threads - interrupt to quit\n" |
| 658 | : "Process %u attached - interrupt to quit\n", |
Denys Vlasenko | 44f87ef | 2011-08-17 15:18:21 +0200 | [diff] [blame] | 659 | tcp->pid, ntid); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 660 | } |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 661 | if (!(tcp->flags & TCB_ATTACHED)) { |
Denys Vlasenko | f88837a | 2011-09-05 14:05:46 +0200 | [diff] [blame] | 662 | /* -p PID, we failed to attach to PID itself |
| 663 | * but did attach to some of its sibling threads. |
| 664 | * Drop PID's tcp. |
| 665 | */ |
| 666 | droptcb(tcp); |
| 667 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 668 | continue; |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 669 | } /* if (opendir worked) */ |
| 670 | } /* if (-f) */ |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 671 | if (ptrace_attach_or_seize(tcp->pid) < 0) { |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 672 | perror("attach: ptrace(PTRACE_ATTACH, ...)"); |
| 673 | droptcb(tcp); |
| 674 | continue; |
| 675 | } |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 676 | tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop; |
Denys Vlasenko | f95397a | 2011-06-24 16:51:16 +0200 | [diff] [blame] | 677 | if (debug) |
| 678 | fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 679 | |
| 680 | if (daemonized_tracer) { |
| 681 | /* |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 682 | * Make parent go away. |
| 683 | * Also makes grandparent's wait() unblock. |
| 684 | */ |
| 685 | kill(getppid(), SIGKILL); |
| 686 | } |
| 687 | |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 688 | if (!qflag) |
| 689 | fprintf(stderr, |
| 690 | "Process %u attached - interrupt to quit\n", |
| 691 | tcp->pid); |
Denys Vlasenko | f95397a | 2011-06-24 16:51:16 +0200 | [diff] [blame] | 692 | } /* for each tcbtab[] */ |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 693 | |
Denys Vlasenko | 44f87ef | 2011-08-17 15:18:21 +0200 | [diff] [blame] | 694 | ret: |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 695 | if (interactive) |
| 696 | sigprocmask(SIG_SETMASK, &empty_set, NULL); |
| 697 | } |
| 698 | |
| 699 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 700 | startup_child(char **argv) |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 701 | { |
| 702 | struct stat statbuf; |
| 703 | const char *filename; |
| 704 | char pathname[MAXPATHLEN]; |
| 705 | int pid = 0; |
| 706 | struct tcb *tcp; |
| 707 | |
| 708 | filename = argv[0]; |
| 709 | if (strchr(filename, '/')) { |
| 710 | if (strlen(filename) > sizeof pathname - 1) { |
| 711 | errno = ENAMETOOLONG; |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 712 | perror_msg_and_die("exec"); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 713 | } |
| 714 | strcpy(pathname, filename); |
| 715 | } |
| 716 | #ifdef USE_DEBUGGING_EXEC |
| 717 | /* |
| 718 | * Debuggers customarily check the current directory |
| 719 | * first regardless of the path but doing that gives |
| 720 | * security geeks a panic attack. |
| 721 | */ |
| 722 | else if (stat(filename, &statbuf) == 0) |
| 723 | strcpy(pathname, filename); |
| 724 | #endif /* USE_DEBUGGING_EXEC */ |
| 725 | else { |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 726 | const char *path; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 727 | int m, n, len; |
| 728 | |
| 729 | for (path = getenv("PATH"); path && *path; path += m) { |
Denys Vlasenko | 4f3df07 | 2012-01-29 22:38:35 +0100 | [diff] [blame] | 730 | const char *colon = strchr(path, ':'); |
| 731 | if (colon) { |
| 732 | n = colon - path; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 733 | m = n + 1; |
| 734 | } |
| 735 | else |
| 736 | m = n = strlen(path); |
| 737 | if (n == 0) { |
| 738 | if (!getcwd(pathname, MAXPATHLEN)) |
| 739 | continue; |
| 740 | len = strlen(pathname); |
| 741 | } |
| 742 | else if (n > sizeof pathname - 1) |
| 743 | continue; |
| 744 | else { |
| 745 | strncpy(pathname, path, n); |
| 746 | len = n; |
| 747 | } |
| 748 | if (len && pathname[len - 1] != '/') |
| 749 | pathname[len++] = '/'; |
| 750 | strcpy(pathname + len, filename); |
| 751 | if (stat(pathname, &statbuf) == 0 && |
| 752 | /* Accept only regular files |
| 753 | with some execute bits set. |
| 754 | XXX not perfect, might still fail */ |
| 755 | S_ISREG(statbuf.st_mode) && |
| 756 | (statbuf.st_mode & 0111)) |
| 757 | break; |
| 758 | } |
| 759 | } |
| 760 | if (stat(pathname, &statbuf) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 761 | perror_msg_and_die("Can't stat '%s'", filename); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 762 | } |
Dmitry V. Levin | a680965 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 763 | strace_child = pid = fork(); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 764 | if (pid < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 765 | perror_msg_and_die("fork"); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 766 | } |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 767 | if ((pid != 0 && daemonized_tracer) /* -D: parent to become a traced process */ |
| 768 | || (pid == 0 && !daemonized_tracer) /* not -D: child to become a traced process */ |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 769 | ) { |
| 770 | pid = getpid(); |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 771 | if (outf != stderr) |
| 772 | close(fileno(outf)); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 773 | if (!daemonized_tracer && !use_seize) { |
| 774 | if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 775 | perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)"); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 776 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 777 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 778 | |
Denys Vlasenko | ab034fb | 2011-09-01 10:27:42 +0200 | [diff] [blame] | 779 | if (username != NULL) { |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 780 | uid_t run_euid = run_uid; |
| 781 | gid_t run_egid = run_gid; |
| 782 | |
| 783 | if (statbuf.st_mode & S_ISUID) |
| 784 | run_euid = statbuf.st_uid; |
| 785 | if (statbuf.st_mode & S_ISGID) |
| 786 | run_egid = statbuf.st_gid; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 787 | /* |
| 788 | * It is important to set groups before we |
| 789 | * lose privileges on setuid. |
| 790 | */ |
Denys Vlasenko | ab034fb | 2011-09-01 10:27:42 +0200 | [diff] [blame] | 791 | if (initgroups(username, run_gid) < 0) { |
| 792 | perror_msg_and_die("initgroups"); |
| 793 | } |
| 794 | if (setregid(run_gid, run_egid) < 0) { |
| 795 | perror_msg_and_die("setregid"); |
| 796 | } |
| 797 | if (setreuid(run_uid, run_euid) < 0) { |
| 798 | perror_msg_and_die("setreuid"); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 799 | } |
| 800 | } |
Denys Vlasenko | ab034fb | 2011-09-01 10:27:42 +0200 | [diff] [blame] | 801 | else if (geteuid() != 0) |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 802 | setreuid(run_uid, run_uid); |
| 803 | |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 804 | if (!daemonized_tracer) { |
| 805 | /* |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 806 | * Induce a ptrace stop. Tracer (our parent) |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 807 | * will resume us with PTRACE_SYSCALL and display |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 808 | * the immediately following execve syscall. |
| 809 | * Can't do this on NOMMU systems, we are after |
| 810 | * vfork: parent is blocked, stopping would deadlock. |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 811 | */ |
Mike Frysinger | c1a5b7e | 2009-10-07 20:41:29 -0400 | [diff] [blame] | 812 | if (!strace_vforked) |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 813 | kill(pid, SIGSTOP); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 814 | } else { |
| 815 | struct sigaction sv_sigchld; |
| 816 | sigaction(SIGCHLD, NULL, &sv_sigchld); |
| 817 | /* |
| 818 | * Make sure it is not SIG_IGN, otherwise wait |
| 819 | * will not block. |
| 820 | */ |
| 821 | signal(SIGCHLD, SIG_DFL); |
| 822 | /* |
| 823 | * Wait for grandchild to attach to us. |
| 824 | * It kills child after that, and wait() unblocks. |
| 825 | */ |
| 826 | alarm(3); |
| 827 | wait(NULL); |
| 828 | alarm(0); |
| 829 | sigaction(SIGCHLD, &sv_sigchld, NULL); |
| 830 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 831 | |
| 832 | execv(pathname, argv); |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 833 | perror_msg_and_die("exec"); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 834 | } |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 835 | |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 836 | /* We are the tracer */ |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 837 | |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 838 | if (!daemonized_tracer) { |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 839 | if (!use_seize) { |
| 840 | /* child did PTRACE_TRACEME, nothing to do in parent */ |
| 841 | } else { |
| 842 | if (!strace_vforked) { |
| 843 | /* Wait until child stopped itself */ |
| 844 | int status; |
| 845 | while (waitpid(pid, &status, WSTOPPED) < 0) { |
| 846 | if (errno == EINTR) |
| 847 | continue; |
| 848 | perror_msg_and_die("waitpid"); |
| 849 | } |
| 850 | if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) { |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 851 | kill_save_errno(pid, SIGKILL); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 852 | perror_msg_and_die("Unexpected wait status %x", status); |
| 853 | } |
| 854 | } |
| 855 | /* Else: vforked case, we have no way to sync. |
| 856 | * Just attach to it as soon as possible. |
| 857 | * This means that we may miss a few first syscalls... |
| 858 | */ |
| 859 | |
| 860 | if (ptrace_attach_or_seize(pid)) { |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 861 | kill_save_errno(pid, SIGKILL); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 862 | perror_msg_and_die("Can't attach to %d", pid); |
| 863 | } |
| 864 | if (!strace_vforked) |
| 865 | kill(pid, SIGCONT); |
| 866 | } |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 867 | tcp = alloctcb(pid); |
Denys Vlasenko | f88837a | 2011-09-05 14:05:46 +0200 | [diff] [blame] | 868 | if (!strace_vforked) |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 869 | tcp->flags |= TCB_ATTACHED | TCB_STRACE_CHILD | TCB_STARTUP | post_attach_sigstop; |
Denys Vlasenko | f88837a | 2011-09-05 14:05:46 +0200 | [diff] [blame] | 870 | else |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 871 | tcp->flags |= TCB_ATTACHED | TCB_STRACE_CHILD | TCB_STARTUP; |
Denys Vlasenko | 2e968c0 | 2011-09-01 10:23:09 +0200 | [diff] [blame] | 872 | } |
| 873 | else { |
| 874 | /* With -D, *we* are child here, IOW: different pid. Fetch it: */ |
| 875 | strace_tracer_pid = getpid(); |
| 876 | /* The tracee is our parent: */ |
| 877 | pid = getppid(); |
Denys Vlasenko | f202502 | 2012-03-09 15:29:45 +0100 | [diff] [blame] | 878 | alloctcb(pid); |
| 879 | /* attaching will be done later, by startup_attach */ |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 880 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 883 | /* |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 884 | * Test whether the kernel support PTRACE_O_TRACECLONE et al options. |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 885 | * 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] | 886 | * and then see which options are supported by the kernel. |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 887 | */ |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 888 | static void |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 889 | test_ptrace_setoptions_followfork(void) |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 890 | { |
Dmitry V. Levin | 2fabd0e | 2011-02-19 21:33:50 +0000 | [diff] [blame] | 891 | int pid, expected_grandchild = 0, found_grandchild = 0; |
| 892 | const unsigned int test_options = PTRACE_O_TRACECLONE | |
| 893 | PTRACE_O_TRACEFORK | |
| 894 | PTRACE_O_TRACEVFORK; |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 895 | |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 896 | pid = fork(); |
| 897 | if (pid < 0) |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 898 | perror_msg_and_die("fork"); |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 899 | if (pid == 0) { |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 900 | pid = getpid(); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 901 | if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 902 | perror_msg_and_die("%s: PTRACE_TRACEME doesn't work", |
| 903 | __func__); |
Denys Vlasenko | 4c65c44 | 2012-03-08 11:54:10 +0100 | [diff] [blame] | 904 | kill_save_errno(pid, SIGSTOP); |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 905 | if (fork() < 0) |
| 906 | perror_msg_and_die("fork"); |
| 907 | _exit(0); |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 908 | } |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 909 | |
| 910 | while (1) { |
| 911 | int status, tracee_pid; |
| 912 | |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 913 | errno = 0; |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 914 | tracee_pid = wait(&status); |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 915 | if (tracee_pid <= 0) { |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 916 | if (errno == EINTR) |
| 917 | continue; |
Denys Vlasenko | 4c65c44 | 2012-03-08 11:54:10 +0100 | [diff] [blame] | 918 | if (errno == ECHILD) |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 919 | break; |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 920 | kill_save_errno(pid, SIGKILL); |
| 921 | perror_msg_and_die("%s: unexpected wait result %d", |
| 922 | __func__, tracee_pid); |
| 923 | } |
| 924 | if (WIFEXITED(status)) { |
| 925 | if (WEXITSTATUS(status)) { |
| 926 | if (tracee_pid != pid) |
| 927 | kill_save_errno(pid, SIGKILL); |
| 928 | error_msg_and_die("%s: unexpected exit status %u", |
| 929 | __func__, WEXITSTATUS(status)); |
| 930 | } |
| 931 | continue; |
| 932 | } |
| 933 | if (WIFSIGNALED(status)) { |
| 934 | if (tracee_pid != pid) |
| 935 | kill_save_errno(pid, SIGKILL); |
| 936 | error_msg_and_die("%s: unexpected signal %u", |
| 937 | __func__, WTERMSIG(status)); |
| 938 | } |
| 939 | if (!WIFSTOPPED(status)) { |
| 940 | if (tracee_pid != pid) |
| 941 | kill_save_errno(tracee_pid, SIGKILL); |
Denys Vlasenko | 4c65c44 | 2012-03-08 11:54:10 +0100 | [diff] [blame] | 942 | kill_save_errno(pid, SIGKILL); |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 943 | error_msg_and_die("%s: unexpected wait status %x", |
| 944 | __func__, status); |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 945 | } |
| 946 | if (tracee_pid != pid) { |
Dmitry V. Levin | 2fabd0e | 2011-02-19 21:33:50 +0000 | [diff] [blame] | 947 | found_grandchild = tracee_pid; |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 948 | if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0) { |
| 949 | kill_save_errno(tracee_pid, SIGKILL); |
| 950 | kill_save_errno(pid, SIGKILL); |
| 951 | perror_msg_and_die("PTRACE_CONT doesn't work"); |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 952 | } |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 953 | continue; |
| 954 | } |
| 955 | switch (WSTOPSIG(status)) { |
| 956 | case SIGSTOP: |
| 957 | if (ptrace(PTRACE_SETOPTIONS, pid, 0, test_options) < 0 |
| 958 | && errno != EINVAL && errno != EIO) |
| 959 | perror_msg("PTRACE_SETOPTIONS"); |
| 960 | break; |
| 961 | case SIGTRAP: |
| 962 | if (status >> 16 == PTRACE_EVENT_FORK) { |
| 963 | long msg = 0; |
| 964 | |
| 965 | if (ptrace(PTRACE_GETEVENTMSG, pid, |
| 966 | NULL, (long) &msg) == 0) |
| 967 | expected_grandchild = msg; |
| 968 | } |
| 969 | break; |
| 970 | } |
| 971 | if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) { |
| 972 | kill_save_errno(pid, SIGKILL); |
| 973 | perror_msg_and_die("PTRACE_SYSCALL doesn't work"); |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 974 | } |
| 975 | } |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 976 | if (expected_grandchild && expected_grandchild == found_grandchild) { |
Denys Vlasenko | f44cce4 | 2011-06-21 14:34:10 +0200 | [diff] [blame] | 977 | ptrace_setoptions |= test_options; |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 978 | if (debug) |
| 979 | fprintf(stderr, "ptrace_setoptions = %#x\n", |
| 980 | ptrace_setoptions); |
| 981 | return; |
| 982 | } |
| 983 | error_msg("Test for PTRACE_O_TRACECLONE failed, " |
| 984 | "giving up using this feature."); |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 985 | } |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 986 | |
| 987 | /* |
| 988 | * Test whether the kernel support PTRACE_O_TRACESYSGOOD. |
| 989 | * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it, |
| 990 | * and then see whether it will stop with (SIGTRAP | 0x80). |
| 991 | * |
| 992 | * Use of this option enables correct handling of user-generated SIGTRAPs, |
| 993 | * and SIGTRAPs generated by special instructions such as int3 on x86: |
| 994 | * _start: .globl _start |
| 995 | * int3 |
| 996 | * movl $42, %ebx |
| 997 | * movl $1, %eax |
| 998 | * int $0x80 |
| 999 | * (compile with: "gcc -nostartfiles -nostdlib -o int3 int3.S") |
| 1000 | */ |
| 1001 | static void |
| 1002 | test_ptrace_setoptions_for_all(void) |
| 1003 | { |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 1004 | const unsigned int test_options = PTRACE_O_TRACESYSGOOD | |
| 1005 | PTRACE_O_TRACEEXEC; |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1006 | int pid; |
| 1007 | int it_worked = 0; |
| 1008 | |
| 1009 | pid = fork(); |
| 1010 | if (pid < 0) |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1011 | perror_msg_and_die("fork"); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1012 | |
| 1013 | if (pid == 0) { |
| 1014 | pid = getpid(); |
| 1015 | if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1016 | /* Note: exits with exitcode 1 */ |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 1017 | perror_msg_and_die("%s: PTRACE_TRACEME doesn't work", |
| 1018 | __func__); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1019 | kill(pid, SIGSTOP); |
| 1020 | _exit(0); /* parent should see entry into this syscall */ |
| 1021 | } |
| 1022 | |
| 1023 | while (1) { |
| 1024 | int status, tracee_pid; |
| 1025 | |
| 1026 | errno = 0; |
| 1027 | tracee_pid = wait(&status); |
| 1028 | if (tracee_pid <= 0) { |
| 1029 | if (errno == EINTR) |
| 1030 | continue; |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 1031 | kill_save_errno(pid, SIGKILL); |
| 1032 | perror_msg_and_die("%s: unexpected wait result %d", |
| 1033 | __func__, tracee_pid); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1034 | } |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1035 | if (WIFEXITED(status)) { |
| 1036 | if (WEXITSTATUS(status) == 0) |
| 1037 | break; |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 1038 | error_msg_and_die("%s: unexpected exit status %u", |
| 1039 | __func__, WEXITSTATUS(status)); |
| 1040 | } |
| 1041 | if (WIFSIGNALED(status)) { |
| 1042 | error_msg_and_die("%s: unexpected signal %u", |
| 1043 | __func__, WTERMSIG(status)); |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1044 | } |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1045 | if (!WIFSTOPPED(status)) { |
| 1046 | kill(pid, SIGKILL); |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 1047 | error_msg_and_die("%s: unexpected wait status %x", |
| 1048 | __func__, status); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1049 | } |
| 1050 | if (WSTOPSIG(status) == SIGSTOP) { |
| 1051 | /* |
| 1052 | * We don't check "options aren't accepted" error. |
| 1053 | * If it happens, we'll never get (SIGTRAP | 0x80), |
| 1054 | * and thus will decide to not use the option. |
| 1055 | * IOW: the outcome of the test will be correct. |
| 1056 | */ |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 1057 | if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0 |
| 1058 | && errno != EINVAL && errno != EIO) |
| 1059 | perror_msg("PTRACE_SETOPTIONS"); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1060 | } |
| 1061 | if (WSTOPSIG(status) == (SIGTRAP | 0x80)) { |
| 1062 | it_worked = 1; |
| 1063 | } |
| 1064 | if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) { |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 1065 | kill_save_errno(pid, SIGKILL); |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1066 | perror_msg_and_die("PTRACE_SYSCALL doesn't work"); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | if (it_worked) { |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1071 | syscall_trap_sig = (SIGTRAP | 0x80); |
Denys Vlasenko | f44cce4 | 2011-06-21 14:34:10 +0200 | [diff] [blame] | 1072 | ptrace_setoptions |= test_options; |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1073 | if (debug) |
Denys Vlasenko | f44cce4 | 2011-06-21 14:34:10 +0200 | [diff] [blame] | 1074 | fprintf(stderr, "ptrace_setoptions = %#x\n", |
| 1075 | ptrace_setoptions); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1076 | return; |
| 1077 | } |
| 1078 | |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 1079 | error_msg("Test for PTRACE_O_TRACESYSGOOD failed, " |
| 1080 | "giving up using this feature."); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1081 | } |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1082 | |
| 1083 | # ifdef USE_SEIZE |
| 1084 | static void |
| 1085 | test_ptrace_seize(void) |
| 1086 | { |
| 1087 | int pid; |
| 1088 | |
| 1089 | pid = fork(); |
| 1090 | if (pid < 0) |
| 1091 | perror_msg_and_die("fork"); |
| 1092 | |
| 1093 | if (pid == 0) { |
| 1094 | pause(); |
| 1095 | _exit(0); |
| 1096 | } |
| 1097 | |
| 1098 | /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After |
| 1099 | * attaching tracee continues to run unless a trap condition occurs. |
| 1100 | * PTRACE_SEIZE doesn't affect signal or group stop state. |
| 1101 | */ |
| 1102 | if (ptrace(PTRACE_SEIZE, pid, 0, PTRACE_SEIZE_DEVEL) == 0) { |
| 1103 | post_attach_sigstop = 0; /* this sets use_seize to 1 */ |
| 1104 | } else if (debug) { |
| 1105 | fprintf(stderr, "PTRACE_SEIZE doesn't work\n"); |
| 1106 | } |
| 1107 | |
| 1108 | kill(pid, SIGKILL); |
| 1109 | |
| 1110 | while (1) { |
| 1111 | int status, tracee_pid; |
| 1112 | |
| 1113 | errno = 0; |
| 1114 | tracee_pid = waitpid(pid, &status, 0); |
| 1115 | if (tracee_pid <= 0) { |
| 1116 | if (errno == EINTR) |
| 1117 | continue; |
| 1118 | perror_msg_and_die("%s: unexpected wait result %d", |
| 1119 | __func__, tracee_pid); |
| 1120 | } |
| 1121 | if (WIFSIGNALED(status)) { |
| 1122 | return; |
| 1123 | } |
| 1124 | error_msg_and_die("%s: unexpected wait status %x", |
| 1125 | __func__, status); |
| 1126 | } |
| 1127 | } |
| 1128 | # else /* !USE_SEIZE */ |
| 1129 | # define test_ptrace_seize() ((void)0) |
| 1130 | # endif |
| 1131 | |
Denys Vlasenko | ecc8b97 | 2012-03-12 23:05:25 +0100 | [diff] [blame] | 1132 | static void |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1133 | get_os_release(void) |
| 1134 | { |
| 1135 | struct utsname u; |
| 1136 | if (uname(&u) < 0) |
| 1137 | perror_msg_and_die("uname"); |
| 1138 | os_release = strdup(u.release); |
| 1139 | if (!os_release) |
| 1140 | die_out_of_memory(); |
| 1141 | } |
| 1142 | |
Denys Vlasenko | ecc8b97 | 2012-03-12 23:05:25 +0100 | [diff] [blame] | 1143 | /* |
| 1144 | * Initialization part of main() was eating much stack (~0.5k), |
| 1145 | * which was unused after init. |
| 1146 | * We can reuse it if we move init code into a separate function. |
| 1147 | * |
| 1148 | * Don't want main() to inline us and defeat the reason |
| 1149 | * we have a separate function. |
| 1150 | */ |
| 1151 | static void __attribute__ ((noinline)) |
| 1152 | init(int argc, char *argv[]) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1153 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1154 | struct tcb *tcp; |
Denys Vlasenko | e8172b7 | 2012-03-09 13:01:04 +0100 | [diff] [blame] | 1155 | int c; |
Dmitry V. Levin | 06350db | 2008-07-25 15:42:34 +0000 | [diff] [blame] | 1156 | int optF = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1157 | struct sigaction sa; |
| 1158 | |
Dmitry V. Levin | 08b623e | 2007-10-08 21:04:41 +0000 | [diff] [blame] | 1159 | progname = argv[0] ? argv[0] : "strace"; |
| 1160 | |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1161 | strace_tracer_pid = getpid(); |
| 1162 | |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1163 | get_os_release(); |
| 1164 | |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 1165 | /* Allocate the initial tcbtab. */ |
| 1166 | tcbtabsize = argc; /* Surely enough for all -p args. */ |
Denys Vlasenko | 4f12af2 | 2011-06-23 13:16:23 +0200 | [diff] [blame] | 1167 | tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0])); |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 1168 | if (!tcbtab) |
| 1169 | die_out_of_memory(); |
Denys Vlasenko | 4f12af2 | 2011-06-23 13:16:23 +0200 | [diff] [blame] | 1170 | tcp = calloc(tcbtabsize, sizeof(*tcp)); |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 1171 | if (!tcp) |
| 1172 | die_out_of_memory(); |
Denys Vlasenko | 4f12af2 | 2011-06-23 13:16:23 +0200 | [diff] [blame] | 1173 | for (c = 0; c < tcbtabsize; c++) |
| 1174 | tcbtab[c] = tcp++; |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 1175 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1176 | outf = stderr; |
Roland McGrath | 138c6a3 | 2006-01-12 09:50:49 +0000 | [diff] [blame] | 1177 | set_sortby(DEFAULT_SORTBY); |
| 1178 | set_personality(DEFAULT_PERSONALITY); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1179 | qualify("trace=all"); |
| 1180 | qualify("abbrev=all"); |
| 1181 | qualify("verbose=all"); |
| 1182 | qualify("signal=all"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1183 | while ((c = getopt(argc, argv, |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 1184 | "+cCdfFhiqrtTvVxyz" |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 1185 | "D" |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 1186 | "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1187 | switch (c) { |
| 1188 | case 'c': |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 1189 | if (cflag == CFLAG_BOTH) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1190 | error_msg_and_die("-c and -C are mutually exclusive options"); |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 1191 | } |
| 1192 | cflag = CFLAG_ONLY_STATS; |
| 1193 | break; |
| 1194 | case 'C': |
| 1195 | if (cflag == CFLAG_ONLY_STATS) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1196 | error_msg_and_die("-c and -C are mutually exclusive options"); |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 1197 | } |
| 1198 | cflag = CFLAG_BOTH; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1199 | break; |
| 1200 | case 'd': |
| 1201 | debug++; |
| 1202 | break; |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 1203 | case 'D': |
| 1204 | daemonized_tracer = 1; |
| 1205 | break; |
Roland McGrath | 41c4822 | 2008-07-18 00:25:10 +0000 | [diff] [blame] | 1206 | case 'F': |
Dmitry V. Levin | 06350db | 2008-07-25 15:42:34 +0000 | [diff] [blame] | 1207 | optF = 1; |
| 1208 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1209 | case 'f': |
| 1210 | followfork++; |
| 1211 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1212 | case 'h': |
| 1213 | usage(stdout, 0); |
| 1214 | break; |
| 1215 | case 'i': |
| 1216 | iflag++; |
| 1217 | break; |
| 1218 | case 'q': |
| 1219 | qflag++; |
| 1220 | break; |
| 1221 | case 'r': |
| 1222 | rflag++; |
| 1223 | tflag++; |
| 1224 | break; |
| 1225 | case 't': |
| 1226 | tflag++; |
| 1227 | break; |
| 1228 | case 'T': |
| 1229 | dtime++; |
| 1230 | break; |
| 1231 | case 'x': |
| 1232 | xflag++; |
| 1233 | break; |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 1234 | case 'y': |
| 1235 | show_fd_path = 1; |
| 1236 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1237 | case 'v': |
| 1238 | qualify("abbrev=none"); |
| 1239 | break; |
| 1240 | case 'V': |
Roland McGrath | 9c9a253 | 2003-02-20 02:56:29 +0000 | [diff] [blame] | 1241 | printf("%s -- version %s\n", PACKAGE_NAME, VERSION); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1242 | exit(0); |
| 1243 | break; |
Michal Ludvig | 17f8fb3 | 2002-11-06 13:17:21 +0000 | [diff] [blame] | 1244 | case 'z': |
| 1245 | not_failing_only = 1; |
| 1246 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1247 | case 'a': |
| 1248 | acolumn = atoi(optarg); |
Denys Vlasenko | 102ec49 | 2011-08-25 01:27:59 +0200 | [diff] [blame] | 1249 | if (acolumn < 0) |
| 1250 | error_msg_and_die("Bad column width '%s'", optarg); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1251 | break; |
| 1252 | case 'e': |
| 1253 | qualify(optarg); |
| 1254 | break; |
| 1255 | case 'o': |
| 1256 | outfname = strdup(optarg); |
| 1257 | break; |
| 1258 | case 'O': |
| 1259 | set_overhead(atoi(optarg)); |
| 1260 | break; |
| 1261 | case 'p': |
Denys Vlasenko | e8172b7 | 2012-03-09 13:01:04 +0100 | [diff] [blame] | 1262 | process_opt_p_list(optarg); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1263 | break; |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 1264 | case 'P': |
| 1265 | tracing_paths = 1; |
| 1266 | if (pathtrace_select(optarg)) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1267 | error_msg_and_die("Failed to select path '%s'", optarg); |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 1268 | } |
| 1269 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1270 | case 's': |
| 1271 | max_strlen = atoi(optarg); |
Roland McGrath | dccec72 | 2005-05-09 07:45:47 +0000 | [diff] [blame] | 1272 | if (max_strlen < 0) { |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 1273 | error_msg_and_die("Invalid -%c argument: '%s'", c, optarg); |
Roland McGrath | dccec72 | 2005-05-09 07:45:47 +0000 | [diff] [blame] | 1274 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1275 | break; |
| 1276 | case 'S': |
| 1277 | set_sortby(optarg); |
| 1278 | break; |
| 1279 | case 'u': |
| 1280 | username = strdup(optarg); |
| 1281 | break; |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 1282 | case 'E': |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 1283 | if (putenv(optarg) < 0) |
| 1284 | die_out_of_memory(); |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 1285 | break; |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 1286 | case 'I': |
| 1287 | opt_intr = atoi(optarg); |
| 1288 | if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS) { |
| 1289 | error_msg_and_die("Invalid -%c argument: '%s'", c, optarg); |
| 1290 | } |
| 1291 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1292 | default: |
| 1293 | usage(stderr, 1); |
| 1294 | break; |
| 1295 | } |
| 1296 | } |
Denys Vlasenko | 837399a | 2012-01-24 11:37:03 +0100 | [diff] [blame] | 1297 | argv += optind; |
| 1298 | /* argc -= optind; - no need, argc is not used below */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1299 | |
Denys Vlasenko | 102ec49 | 2011-08-25 01:27:59 +0200 | [diff] [blame] | 1300 | acolumn_spaces = malloc(acolumn + 1); |
| 1301 | if (!acolumn_spaces) |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 1302 | die_out_of_memory(); |
Denys Vlasenko | 102ec49 | 2011-08-25 01:27:59 +0200 | [diff] [blame] | 1303 | memset(acolumn_spaces, ' ', acolumn); |
| 1304 | acolumn_spaces[acolumn] = '\0'; |
| 1305 | |
Denys Vlasenko | 837399a | 2012-01-24 11:37:03 +0100 | [diff] [blame] | 1306 | /* Must have PROG [ARGS], or -p PID. Not both. */ |
Denys Vlasenko | fd88338 | 2012-03-09 13:03:41 +0100 | [diff] [blame] | 1307 | if (!argv[0] == !nprocs) |
Roland McGrath | ce0d154 | 2003-11-11 21:24:23 +0000 | [diff] [blame] | 1308 | usage(stderr, 1); |
| 1309 | |
Denys Vlasenko | fd88338 | 2012-03-09 13:03:41 +0100 | [diff] [blame] | 1310 | if (nprocs != 0 && daemonized_tracer) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1311 | error_msg_and_die("-D and -p are mutually exclusive options"); |
Wang Chao | d322a4b | 2010-08-05 14:30:11 +0800 | [diff] [blame] | 1312 | } |
| 1313 | |
Dmitry V. Levin | 06350db | 2008-07-25 15:42:34 +0000 | [diff] [blame] | 1314 | if (!followfork) |
| 1315 | followfork = optF; |
| 1316 | |
Roland McGrath | cb9def6 | 2006-04-25 07:48:03 +0000 | [diff] [blame] | 1317 | if (followfork > 1 && cflag) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1318 | 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] | 1319 | } |
| 1320 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1321 | /* See if they want to run as another user. */ |
| 1322 | if (username != NULL) { |
| 1323 | struct passwd *pent; |
| 1324 | |
| 1325 | if (getuid() != 0 || geteuid() != 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1326 | 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] | 1327 | } |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 1328 | pent = getpwnam(username); |
| 1329 | if (pent == NULL) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1330 | error_msg_and_die("Cannot find user '%s'", username); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1331 | } |
| 1332 | run_uid = pent->pw_uid; |
| 1333 | run_gid = pent->pw_gid; |
| 1334 | } |
| 1335 | else { |
| 1336 | run_uid = getuid(); |
| 1337 | run_gid = getgid(); |
| 1338 | } |
| 1339 | |
Dmitry V. Levin | 04f8b48 | 2011-08-16 18:57:29 +0000 | [diff] [blame] | 1340 | if (followfork) |
| 1341 | test_ptrace_setoptions_followfork(); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1342 | test_ptrace_setoptions_for_all(); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1343 | test_ptrace_seize(); |
Dmitry V. Levin | 8044bc1 | 2010-12-07 12:50:49 +0000 | [diff] [blame] | 1344 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1345 | /* Check if they want to redirect the output. */ |
| 1346 | if (outfname) { |
Roland McGrath | 37b9a66 | 2003-11-07 02:26:54 +0000 | [diff] [blame] | 1347 | /* See if they want to pipe the output. */ |
| 1348 | if (outfname[0] == '|' || outfname[0] == '!') { |
| 1349 | /* |
| 1350 | * We can't do the <outfname>.PID funny business |
| 1351 | * when using popen, so prohibit it. |
| 1352 | */ |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 1353 | if (followfork > 1) |
| 1354 | error_msg_and_die("Piping the output and -ff are mutually exclusive"); |
| 1355 | outf = strace_popen(outfname + 1); |
Roland McGrath | 37b9a66 | 2003-11-07 02:26:54 +0000 | [diff] [blame] | 1356 | } |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 1357 | else if (followfork <= 1) |
| 1358 | outf = strace_fopen(outfname); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1361 | if (!outfname || outfname[0] == '|' || outfname[0] == '!') { |
Denys Vlasenko | a677da5 | 2012-01-24 11:31:51 +0100 | [diff] [blame] | 1362 | char *buf = malloc(BUFSIZ); |
| 1363 | if (!buf) |
| 1364 | die_out_of_memory(); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1365 | setvbuf(outf, buf, _IOLBF, BUFSIZ); |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1366 | } |
Denys Vlasenko | 837399a | 2012-01-24 11:37:03 +0100 | [diff] [blame] | 1367 | if (outfname && argv[0]) { |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 1368 | if (!opt_intr) |
| 1369 | opt_intr = INTR_NEVER; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1370 | qflag = 1; |
Roland McGrath | 3693105 | 2003-06-03 01:35:20 +0000 | [diff] [blame] | 1371 | } |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 1372 | if (!opt_intr) |
| 1373 | opt_intr = INTR_WHILE_WAIT; |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 1374 | |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 1375 | /* argv[0] -pPID -oFILE Default interactive setting |
| 1376 | * yes 0 0 INTR_WHILE_WAIT |
| 1377 | * no 1 0 INTR_WHILE_WAIT |
| 1378 | * yes 0 1 INTR_NEVER |
| 1379 | * no 1 1 INTR_WHILE_WAIT |
Roland McGrath | 54cc1c8 | 2007-11-03 23:34:11 +0000 | [diff] [blame] | 1380 | */ |
| 1381 | |
| 1382 | /* STARTUP_CHILD must be called before the signal handlers get |
| 1383 | installed below as they are inherited into the spawned process. |
| 1384 | Also we do not need to be protected by them as during interruption |
| 1385 | in the STARTUP_CHILD mode we kill the spawned process anyway. */ |
Denys Vlasenko | 837399a | 2012-01-24 11:37:03 +0100 | [diff] [blame] | 1386 | if (argv[0]) |
| 1387 | startup_child(argv); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1388 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1389 | sigemptyset(&empty_set); |
| 1390 | sigemptyset(&blocked_set); |
| 1391 | sa.sa_handler = SIG_IGN; |
| 1392 | sigemptyset(&sa.sa_mask); |
| 1393 | sa.sa_flags = 0; |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 1394 | sigaction(SIGTTOU, &sa, NULL); /* SIG_IGN */ |
| 1395 | sigaction(SIGTTIN, &sa, NULL); /* SIG_IGN */ |
| 1396 | if (opt_intr != INTR_ANYWHERE) { |
| 1397 | if (opt_intr == INTR_BLOCK_TSTP_TOO) |
| 1398 | sigaction(SIGTSTP, &sa, NULL); /* SIG_IGN */ |
| 1399 | /* |
| 1400 | * In interactive mode (if no -o OUTFILE, or -p PID is used), |
| 1401 | * fatal signals are blocked while syscall stop is processed, |
| 1402 | * and acted on in between, when waiting for new syscall stops. |
| 1403 | * In non-interactive mode, signals are ignored. |
| 1404 | */ |
| 1405 | if (opt_intr == INTR_WHILE_WAIT) { |
| 1406 | sigaddset(&blocked_set, SIGHUP); |
| 1407 | sigaddset(&blocked_set, SIGINT); |
| 1408 | sigaddset(&blocked_set, SIGQUIT); |
| 1409 | sigaddset(&blocked_set, SIGPIPE); |
| 1410 | sigaddset(&blocked_set, SIGTERM); |
| 1411 | sa.sa_handler = interrupt; |
Denys Vlasenko | b51581e | 2012-01-29 16:53:03 +0100 | [diff] [blame] | 1412 | } |
| 1413 | /* SIG_IGN, or set handler for these */ |
| 1414 | sigaction(SIGHUP, &sa, NULL); |
| 1415 | sigaction(SIGINT, &sa, NULL); |
| 1416 | sigaction(SIGQUIT, &sa, NULL); |
| 1417 | sigaction(SIGPIPE, &sa, NULL); |
| 1418 | sigaction(SIGTERM, &sa, NULL); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1419 | } |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1420 | /* Make sure SIGCHLD has the default action so that waitpid |
| 1421 | definitely works without losing track of children. The user |
| 1422 | should not have given us a bogus state to inherit, but he might |
| 1423 | have. Arguably we should detect SIG_IGN here and pass it on |
| 1424 | to children, but probably noone really needs that. */ |
| 1425 | sa.sa_handler = SIG_DFL; |
| 1426 | sigaction(SIGCHLD, &sa, NULL); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1427 | |
Denys Vlasenko | fd88338 | 2012-03-09 13:03:41 +0100 | [diff] [blame] | 1428 | if (nprocs != 0 || daemonized_tracer) |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1429 | startup_attach(); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1430 | |
Denys Vlasenko | fd88338 | 2012-03-09 13:03:41 +0100 | [diff] [blame] | 1431 | /* Do we want pids printed in our -o OUTFILE? |
| 1432 | * -ff: no (every pid has its own file); or |
| 1433 | * -f: yes (there can be more pids in the future); or |
| 1434 | * -p PID1,PID2: yes (there are already more than one pid) |
| 1435 | */ |
| 1436 | print_pid_pfx = (outfname && followfork < 2 && (followfork == 1 || nprocs > 1)); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Denys Vlasenko | 2b60c35 | 2011-06-22 12:45:25 +0200 | [diff] [blame] | 1439 | static void |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 1440 | expand_tcbtab(void) |
Roland McGrath | 7b54a7a | 2004-06-04 01:50:45 +0000 | [diff] [blame] | 1441 | { |
| 1442 | /* Allocate some more TCBs and expand the table. |
| 1443 | We don't want to relocate the TCBs because our |
| 1444 | callers have pointers and it would be a pain. |
| 1445 | So tcbtab is a table of pointers. Since we never |
| 1446 | free the TCBs, we allocate a single chunk of many. */ |
Denys Vlasenko | 18da273 | 2011-06-22 12:41:57 +0200 | [diff] [blame] | 1447 | int i = tcbtabsize; |
| 1448 | struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0])); |
| 1449 | struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0])); |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 1450 | if (!newtab || !newtcbs) |
| 1451 | die_out_of_memory(); |
Roland McGrath | 7b54a7a | 2004-06-04 01:50:45 +0000 | [diff] [blame] | 1452 | tcbtabsize *= 2; |
| 1453 | tcbtab = newtab; |
Denys Vlasenko | 18da273 | 2011-06-22 12:41:57 +0200 | [diff] [blame] | 1454 | while (i < tcbtabsize) |
| 1455 | tcbtab[i++] = newtcbs++; |
Roland McGrath | 7b54a7a | 2004-06-04 01:50:45 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1458 | struct tcb * |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 1459 | alloc_tcb(int pid, int command_options_parsed) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1460 | { |
| 1461 | int i; |
| 1462 | struct tcb *tcp; |
| 1463 | |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 1464 | if (nprocs == tcbtabsize) |
| 1465 | expand_tcbtab(); |
| 1466 | |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 1467 | for (i = 0; i < tcbtabsize; i++) { |
| 1468 | tcp = tcbtab[i]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1469 | if ((tcp->flags & TCB_INUSE) == 0) { |
Denys Vlasenko | 18da273 | 2011-06-22 12:41:57 +0200 | [diff] [blame] | 1470 | memset(tcp, 0, sizeof(*tcp)); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1471 | tcp->pid = pid; |
Denys Vlasenko | 381dbc2 | 2011-09-05 13:59:39 +0200 | [diff] [blame] | 1472 | tcp->flags = TCB_INUSE; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1473 | tcp->outf = outf; /* Initialise to current out file */ |
Dmitry V. Levin | a5a839a | 2011-12-23 00:50:49 +0000 | [diff] [blame] | 1474 | #if SUPPORTED_PERSONALITIES > 1 |
| 1475 | tcp->currpers = current_personality; |
| 1476 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1477 | nprocs++; |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 1478 | if (debug) |
| 1479 | 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] | 1480 | if (command_options_parsed) |
| 1481 | newoutf(tcp); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1482 | return tcp; |
| 1483 | } |
| 1484 | } |
Denys Vlasenko | 18da273 | 2011-06-22 12:41:57 +0200 | [diff] [blame] | 1485 | error_msg_and_die("bug in alloc_tcb"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Denys Vlasenko | eebb04d | 2012-01-27 15:24:48 +0100 | [diff] [blame] | 1488 | static struct tcb * |
Roland McGrath | 54e931f | 2010-09-14 18:59:20 -0700 | [diff] [blame] | 1489 | pid2tcb(int pid) |
| 1490 | { |
| 1491 | int i; |
| 1492 | |
| 1493 | if (pid <= 0) |
| 1494 | return NULL; |
| 1495 | |
| 1496 | for (i = 0; i < tcbtabsize; i++) { |
| 1497 | struct tcb *tcp = tcbtab[i]; |
| 1498 | if (tcp->pid == pid && (tcp->flags & TCB_INUSE)) |
| 1499 | return tcp; |
| 1500 | } |
| 1501 | |
| 1502 | return NULL; |
| 1503 | } |
| 1504 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1505 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1506 | droptcb(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1507 | { |
| 1508 | if (tcp->pid == 0) |
| 1509 | return; |
Denys Vlasenko | 19cdada | 2011-08-17 10:45:32 +0200 | [diff] [blame] | 1510 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1511 | nprocs--; |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 1512 | if (debug) |
| 1513 | 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] | 1514 | |
Wichert Akkerman | 822f0c9 | 2002-04-03 10:55:14 +0000 | [diff] [blame] | 1515 | if (outfname && followfork > 1 && tcp->outf) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1516 | fclose(tcp->outf); |
Wichert Akkerman | eb8ebda | 2002-04-01 17:48:02 +0000 | [diff] [blame] | 1517 | |
Denys Vlasenko | 19cdada | 2011-08-17 10:45:32 +0200 | [diff] [blame] | 1518 | memset(tcp, 0, sizeof(*tcp)); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Roland McGrath | 0a46388 | 2007-07-05 18:43:16 +0000 | [diff] [blame] | 1521 | /* detach traced process; continue with sig |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 1522 | * Never call DETACH twice on the same process as both unattached and |
| 1523 | * attached-unstopped processes give the same ESRCH. For unattached process we |
| 1524 | * would SIGSTOP it and wait for its SIGSTOP notification forever. |
| 1525 | */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1526 | static int |
Denys Vlasenko | 4c19638 | 2012-01-04 15:11:09 +0100 | [diff] [blame] | 1527 | detach(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1528 | { |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 1529 | int error; |
| 1530 | int status, sigstop_expected; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1531 | |
| 1532 | if (tcp->flags & TCB_BPTSET) |
Andreas Schwab | 840d85b | 2010-01-12 11:16:32 +0100 | [diff] [blame] | 1533 | clearbpt(tcp); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1534 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1535 | /* |
| 1536 | * Linux wrongly insists the child be stopped |
Roland McGrath | 7bf1047 | 2002-12-16 20:42:50 +0000 | [diff] [blame] | 1537 | * before detaching. Arghh. We go through hoops |
| 1538 | * to make a clean break of things. |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1539 | */ |
Roland McGrath | 7bf1047 | 2002-12-16 20:42:50 +0000 | [diff] [blame] | 1540 | #if defined(SPARC) |
| 1541 | #undef PTRACE_DETACH |
| 1542 | #define PTRACE_DETACH PTRACE_SUNDETACH |
| 1543 | #endif |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 1544 | |
| 1545 | sigstop_expected = 0; |
| 1546 | if (tcp->flags & TCB_ATTACHED) { |
| 1547 | /* |
| 1548 | * We attached but possibly didn't see the expected SIGSTOP. |
| 1549 | * We must catch exactly one as otherwise the detached process |
| 1550 | * would be left stopped (process state T). |
| 1551 | */ |
| 1552 | sigstop_expected = (tcp->flags & TCB_IGNORE_ONE_SIGSTOP); |
| 1553 | error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, 0); |
| 1554 | if (error == 0) { |
| 1555 | /* On a clear day, you can see forever. */ |
| 1556 | } |
| 1557 | else if (errno != ESRCH) { |
| 1558 | /* Shouldn't happen. */ |
| 1559 | perror("detach: ptrace(PTRACE_DETACH, ...)"); |
| 1560 | } |
| 1561 | else if (my_tkill(tcp->pid, 0) < 0) { |
| 1562 | if (errno != ESRCH) |
| 1563 | perror("detach: checking sanity"); |
| 1564 | } |
| 1565 | else if (!sigstop_expected && my_tkill(tcp->pid, SIGSTOP) < 0) { |
| 1566 | if (errno != ESRCH) |
| 1567 | perror("detach: stopping child"); |
| 1568 | } |
| 1569 | else |
| 1570 | sigstop_expected = 1; |
Roland McGrath | 7bf1047 | 2002-12-16 20:42:50 +0000 | [diff] [blame] | 1571 | } |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 1572 | |
| 1573 | if (sigstop_expected) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1574 | for (;;) { |
Roland McGrath | 7508cb4 | 2002-12-17 10:48:05 +0000 | [diff] [blame] | 1575 | #ifdef __WALL |
Denys Vlasenko | 37ab4b7 | 2012-03-09 15:34:16 +0100 | [diff] [blame] | 1576 | if (waitpid(tcp->pid, &status, __WALL) < 0) { |
Roland McGrath | 7508cb4 | 2002-12-17 10:48:05 +0000 | [diff] [blame] | 1577 | if (errno == ECHILD) /* Already gone. */ |
| 1578 | break; |
| 1579 | if (errno != EINVAL) { |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1580 | perror("detach: waiting"); |
Roland McGrath | 7508cb4 | 2002-12-17 10:48:05 +0000 | [diff] [blame] | 1581 | break; |
| 1582 | } |
| 1583 | #endif /* __WALL */ |
| 1584 | /* No __WALL here. */ |
| 1585 | if (waitpid(tcp->pid, &status, 0) < 0) { |
| 1586 | if (errno != ECHILD) { |
| 1587 | perror("detach: waiting"); |
| 1588 | break; |
| 1589 | } |
| 1590 | #ifdef __WCLONE |
| 1591 | /* If no processes, try clones. */ |
Denys Vlasenko | 37ab4b7 | 2012-03-09 15:34:16 +0100 | [diff] [blame] | 1592 | if (waitpid(tcp->pid, &status, __WCLONE) < 0) { |
Roland McGrath | 7508cb4 | 2002-12-17 10:48:05 +0000 | [diff] [blame] | 1593 | if (errno != ECHILD) |
| 1594 | perror("detach: waiting"); |
| 1595 | break; |
| 1596 | } |
| 1597 | #endif /* __WCLONE */ |
| 1598 | } |
| 1599 | #ifdef __WALL |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1600 | } |
Roland McGrath | 7508cb4 | 2002-12-17 10:48:05 +0000 | [diff] [blame] | 1601 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1602 | if (!WIFSTOPPED(status)) { |
| 1603 | /* Au revoir, mon ami. */ |
| 1604 | break; |
| 1605 | } |
| 1606 | if (WSTOPSIG(status) == SIGSTOP) { |
Denys Vlasenko | 4c19638 | 2012-01-04 15:11:09 +0100 | [diff] [blame] | 1607 | ptrace_restart(PTRACE_DETACH, tcp, 0); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1608 | break; |
| 1609 | } |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 1610 | error = ptrace_restart(PTRACE_CONT, tcp, |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1611 | WSTOPSIG(status) == syscall_trap_sig ? 0 |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 1612 | : WSTOPSIG(status)); |
| 1613 | if (error < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1614 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1615 | } |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 1616 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1617 | |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 1618 | if (!qflag && (tcp->flags & TCB_ATTACHED)) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1619 | fprintf(stderr, "Process %u detached\n", tcp->pid); |
| 1620 | |
| 1621 | droptcb(tcp); |
Roland McGrath | a08a97e | 2005-08-03 11:23:46 +0000 | [diff] [blame] | 1622 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1623 | return error; |
| 1624 | } |
| 1625 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1626 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1627 | cleanup(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1628 | { |
| 1629 | int i; |
| 1630 | struct tcb *tcp; |
Denys Vlasenko | 3521884 | 2012-01-29 21:17:56 +0100 | [diff] [blame] | 1631 | int fatal_sig; |
| 1632 | |
| 1633 | /* 'interrupted' is a volatile object, fetch it only once */ |
| 1634 | fatal_sig = interrupted; |
| 1635 | if (!fatal_sig) |
| 1636 | fatal_sig = SIGTERM; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1637 | |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 1638 | for (i = 0; i < tcbtabsize; i++) { |
| 1639 | tcp = tcbtab[i]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1640 | if (!(tcp->flags & TCB_INUSE)) |
| 1641 | continue; |
| 1642 | if (debug) |
| 1643 | fprintf(stderr, |
| 1644 | "cleanup: looking at pid %u\n", tcp->pid); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 1645 | if (printing_tcp && |
| 1646 | (!outfname || followfork < 2 || printing_tcp == tcp)) { |
| 1647 | tprints(" <unfinished ...>\n"); |
| 1648 | printing_tcp = NULL; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1649 | } |
Denys Vlasenko | 75fe85c | 2012-03-09 15:15:24 +0100 | [diff] [blame] | 1650 | if (!(tcp->flags & TCB_STRACE_CHILD)) |
Denys Vlasenko | 4c19638 | 2012-01-04 15:11:09 +0100 | [diff] [blame] | 1651 | detach(tcp); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1652 | else { |
| 1653 | kill(tcp->pid, SIGCONT); |
Denys Vlasenko | a355925 | 2012-01-29 16:43:51 +0100 | [diff] [blame] | 1654 | kill(tcp->pid, fatal_sig); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1655 | } |
| 1656 | } |
| 1657 | if (cflag) |
| 1658 | call_summary(outf); |
| 1659 | } |
| 1660 | |
| 1661 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1662 | interrupt(int sig) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1663 | { |
Denys Vlasenko | a355925 | 2012-01-29 16:43:51 +0100 | [diff] [blame] | 1664 | interrupted = sig; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | #ifndef HAVE_STRERROR |
| 1668 | |
Roland McGrath | 6d2b349 | 2002-12-30 00:51:30 +0000 | [diff] [blame] | 1669 | #if !HAVE_DECL_SYS_ERRLIST |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1670 | extern int sys_nerr; |
| 1671 | extern char *sys_errlist[]; |
Roland McGrath | 6d2b349 | 2002-12-30 00:51:30 +0000 | [diff] [blame] | 1672 | #endif /* HAVE_DECL_SYS_ERRLIST */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1673 | |
| 1674 | const char * |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1675 | strerror(int err_no) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1676 | { |
Denys Vlasenko | 1945ccc | 2012-02-27 14:37:48 +0100 | [diff] [blame] | 1677 | static char buf[sizeof("Unknown error %d") + sizeof(int)*3]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1678 | |
Denys Vlasenko | 35aba6a | 2011-05-25 15:33:26 +0200 | [diff] [blame] | 1679 | if (err_no < 1 || err_no >= sys_nerr) { |
| 1680 | sprintf(buf, "Unknown error %d", err_no); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1681 | return buf; |
| 1682 | } |
Denys Vlasenko | 35aba6a | 2011-05-25 15:33:26 +0200 | [diff] [blame] | 1683 | return sys_errlist[err_no]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | #endif /* HAVE_STERRROR */ |
| 1687 | |
| 1688 | #ifndef HAVE_STRSIGNAL |
| 1689 | |
Roland McGrath | 8f474e0 | 2003-01-14 07:53:33 +0000 | [diff] [blame] | 1690 | #if defined HAVE_SYS_SIGLIST && !defined HAVE_DECL_SYS_SIGLIST |
Roland McGrath | 6d2b349 | 2002-12-30 00:51:30 +0000 | [diff] [blame] | 1691 | extern char *sys_siglist[]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1692 | #endif |
Roland McGrath | 8f474e0 | 2003-01-14 07:53:33 +0000 | [diff] [blame] | 1693 | #if defined HAVE_SYS__SIGLIST && !defined HAVE_DECL__SYS_SIGLIST |
| 1694 | extern char *_sys_siglist[]; |
| 1695 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1696 | |
| 1697 | const char * |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1698 | strsignal(int sig) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1699 | { |
Denys Vlasenko | 1945ccc | 2012-02-27 14:37:48 +0100 | [diff] [blame] | 1700 | static char buf[sizeof("Unknown signal %d") + sizeof(int)*3]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1701 | |
| 1702 | if (sig < 1 || sig >= NSIG) { |
| 1703 | sprintf(buf, "Unknown signal %d", sig); |
| 1704 | return buf; |
| 1705 | } |
| 1706 | #ifdef HAVE__SYS_SIGLIST |
| 1707 | return _sys_siglist[sig]; |
| 1708 | #else |
| 1709 | return sys_siglist[sig]; |
| 1710 | #endif |
| 1711 | } |
| 1712 | |
| 1713 | #endif /* HAVE_STRSIGNAL */ |
| 1714 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1715 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1716 | trace(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1717 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1718 | struct rusage ru; |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame] | 1719 | struct rusage *rup = cflag ? &ru : NULL; |
| 1720 | # ifdef __WALL |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1721 | static int wait4_options = __WALL; |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame] | 1722 | # endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1723 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1724 | while (nprocs != 0) { |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1725 | int pid; |
| 1726 | int wait_errno; |
| 1727 | int status, sig; |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1728 | int stopped; |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1729 | struct tcb *tcp; |
| 1730 | unsigned event; |
| 1731 | |
Denys Vlasenko | 222713a | 2009-03-17 14:29:59 +0000 | [diff] [blame] | 1732 | if (interrupted) |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1733 | return 0; |
| 1734 | if (interactive) |
| 1735 | sigprocmask(SIG_SETMASK, &empty_set, NULL); |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame] | 1736 | # ifdef __WALL |
| 1737 | pid = wait4(-1, &status, wait4_options, rup); |
Roland McGrath | 5bc0555 | 2002-12-17 04:50:47 +0000 | [diff] [blame] | 1738 | if (pid < 0 && (wait4_options & __WALL) && errno == EINVAL) { |
Wichert Akkerman | 2f1d87e | 2001-03-28 14:40:14 +0000 | [diff] [blame] | 1739 | /* this kernel does not support __WALL */ |
| 1740 | wait4_options &= ~__WALL; |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame] | 1741 | pid = wait4(-1, &status, wait4_options, rup); |
Wichert Akkerman | 2f1d87e | 2001-03-28 14:40:14 +0000 | [diff] [blame] | 1742 | } |
Roland McGrath | 5bc0555 | 2002-12-17 04:50:47 +0000 | [diff] [blame] | 1743 | if (pid < 0 && !(wait4_options & __WALL) && errno == ECHILD) { |
Wichert Akkerman | 2f1d87e | 2001-03-28 14:40:14 +0000 | [diff] [blame] | 1744 | /* most likely a "cloned" process */ |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame] | 1745 | pid = wait4(-1, &status, __WCLONE, rup); |
| 1746 | if (pid < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1747 | perror_msg("wait4(__WCLONE) failed"); |
Wichert Akkerman | 2f1d87e | 2001-03-28 14:40:14 +0000 | [diff] [blame] | 1748 | } |
| 1749 | } |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame] | 1750 | # else |
| 1751 | pid = wait4(-1, &status, 0, rup); |
| 1752 | # endif /* __WALL */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1753 | wait_errno = errno; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1754 | if (interactive) |
| 1755 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1756 | |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame] | 1757 | if (pid < 0) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1758 | switch (wait_errno) { |
| 1759 | case EINTR: |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1760 | continue; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1761 | case ECHILD: |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1762 | /* |
| 1763 | * We would like to verify this case |
| 1764 | * but sometimes a race in Solbourne's |
| 1765 | * version of SunOS sometimes reports |
| 1766 | * ECHILD before sending us SIGCHILD. |
| 1767 | */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1768 | return 0; |
| 1769 | default: |
| 1770 | errno = wait_errno; |
Denys Vlasenko | 4c65c44 | 2012-03-08 11:54:10 +0100 | [diff] [blame] | 1771 | perror_msg("wait"); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1772 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1773 | } |
| 1774 | } |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 1775 | if (pid == popen_pid) { |
| 1776 | if (WIFEXITED(status) || WIFSIGNALED(status)) |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 1777 | popen_pid = 0; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 1778 | continue; |
| 1779 | } |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1780 | |
| 1781 | event = ((unsigned)status >> 16); |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 1782 | if (debug) { |
| 1783 | char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16]; |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1784 | if (event != 0) { |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 1785 | static const char *const event_names[] = { |
| 1786 | [PTRACE_EVENT_CLONE] = "CLONE", |
| 1787 | [PTRACE_EVENT_FORK] = "FORK", |
| 1788 | [PTRACE_EVENT_VFORK] = "VFORK", |
| 1789 | [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE", |
| 1790 | [PTRACE_EVENT_EXEC] = "EXEC", |
| 1791 | [PTRACE_EVENT_EXIT] = "EXIT", |
| 1792 | }; |
| 1793 | const char *e; |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1794 | if (event < ARRAY_SIZE(event_names)) |
| 1795 | e = event_names[event]; |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 1796 | else { |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1797 | sprintf(buf, "?? (%u)", event); |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 1798 | e = buf; |
| 1799 | } |
| 1800 | fprintf(stderr, " PTRACE_EVENT_%s", e); |
| 1801 | } |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 1802 | strcpy(buf, "???"); |
| 1803 | if (WIFSIGNALED(status)) |
| 1804 | #ifdef WCOREDUMP |
| 1805 | sprintf(buf, "WIFSIGNALED,%ssig=%s", |
| 1806 | WCOREDUMP(status) ? "core," : "", |
| 1807 | signame(WTERMSIG(status))); |
| 1808 | #else |
| 1809 | sprintf(buf, "WIFSIGNALED,sig=%s", |
| 1810 | signame(WTERMSIG(status))); |
| 1811 | #endif |
| 1812 | if (WIFEXITED(status)) |
| 1813 | sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status)); |
| 1814 | if (WIFSTOPPED(status)) |
| 1815 | sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status))); |
Denys Vlasenko | 5bd67c8 | 2011-08-15 11:36:09 +0200 | [diff] [blame] | 1816 | #ifdef WIFCONTINUED |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 1817 | if (WIFCONTINUED(status)) |
| 1818 | strcpy(buf, "WIFCONTINUED"); |
Denys Vlasenko | 5bd67c8 | 2011-08-15 11:36:09 +0200 | [diff] [blame] | 1819 | #endif |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 1820 | fprintf(stderr, " [wait(0x%04x) = %u] %s\n", status, pid, buf); |
| 1821 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1822 | |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1823 | /* Look up 'pid' in our table. */ |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 1824 | tcp = pid2tcb(pid); |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1825 | |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1826 | /* Under Linux, execve changes pid to thread leader's pid, |
| 1827 | * and we see this changed pid on EVENT_EXEC and later, |
| 1828 | * execve sysexit. Leader "disappears" without exit |
| 1829 | * notification. Let user know that, drop leader's tcb, |
| 1830 | * and fix up pid in execve thread's tcb. |
| 1831 | * Effectively, execve thread's tcb replaces leader's tcb. |
| 1832 | * |
| 1833 | * BTW, leader is 'stuck undead' (doesn't report WIFEXITED |
| 1834 | * on exit syscall) in multithreaded programs exactly |
| 1835 | * in order to handle this case. |
| 1836 | * |
| 1837 | * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0. |
| 1838 | * On 2.6 and earlier, it can return garbage. |
| 1839 | */ |
| 1840 | if (event == PTRACE_EVENT_EXEC && os_release[0] >= '3') { |
| 1841 | long old_pid = 0; |
| 1842 | if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) >= 0 |
| 1843 | && old_pid > 0 |
| 1844 | && old_pid != pid |
| 1845 | ) { |
| 1846 | struct tcb *execve_thread = pid2tcb(old_pid); |
| 1847 | if (tcp) { |
| 1848 | outf = tcp->outf; |
| 1849 | curcol = tcp->curcol; |
| 1850 | if (!cflag) { |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 1851 | if (printing_tcp) |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1852 | tprints(" <unfinished ...>\n"); |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1853 | printleader(tcp); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 1854 | tprintf("+++ superseded by execve in pid %lu +++\n", old_pid); |
| 1855 | printing_tcp = NULL; |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1856 | fflush(outf); |
| 1857 | } |
| 1858 | if (execve_thread) { |
| 1859 | /* swap output FILEs (needed for -ff) */ |
| 1860 | tcp->outf = execve_thread->outf; |
| 1861 | execve_thread->outf = outf; |
| 1862 | } |
| 1863 | droptcb(tcp); |
| 1864 | } |
| 1865 | tcp = execve_thread; |
| 1866 | if (tcp) { |
| 1867 | tcp->pid = pid; |
| 1868 | tcp->flags |= TCB_REPRINT; |
| 1869 | } |
| 1870 | } |
| 1871 | } |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1872 | |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 1873 | if (tcp == NULL) { |
Roland McGrath | 41c4822 | 2008-07-18 00:25:10 +0000 | [diff] [blame] | 1874 | if (followfork) { |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 1875 | /* This is needed to go with the CLONE_PTRACE |
| 1876 | changes in process.c/util.c: we might see |
| 1877 | the child's initial trap before we see the |
| 1878 | parent return from the clone syscall. |
| 1879 | Leave the child suspended until the parent |
| 1880 | returns from its system call. Only then |
| 1881 | will we have the association of parent and |
| 1882 | child so that we know how to do clearbpt |
| 1883 | in the child. */ |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 1884 | tcp = alloctcb(pid); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1885 | tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop; |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 1886 | if (!qflag) |
Denys Vlasenko | 833fb13 | 2011-08-17 11:30:56 +0200 | [diff] [blame] | 1887 | fprintf(stderr, "Process %d attached\n", |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 1888 | pid); |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1889 | } |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 1890 | else |
| 1891 | /* This can happen if a clone call used |
| 1892 | CLONE_PTRACE itself. */ |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 1893 | { |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 1894 | if (WIFSTOPPED(status)) |
Denys Vlasenko | 97c503f | 2012-03-09 15:11:21 +0100 | [diff] [blame] | 1895 | ptrace(PTRACE_CONT, pid, (char *) 0, 0); |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1896 | error_msg_and_die("Unknown pid: %u", pid); |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 1897 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1898 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1899 | /* set current output file */ |
| 1900 | outf = tcp->outf; |
Andreas Schwab | ccdff48 | 2009-10-27 16:27:13 +0100 | [diff] [blame] | 1901 | curcol = tcp->curcol; |
Denys Vlasenko | 13d22f1 | 2011-06-24 23:01:57 +0200 | [diff] [blame] | 1902 | if (cflag) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1903 | tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime); |
| 1904 | tcp->stime = ru.ru_stime; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1905 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1906 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1907 | if (WIFSIGNALED(status)) { |
Dmitry V. Levin | a680965 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 1908 | if (pid == strace_child) |
| 1909 | exit_code = 0x100 | WTERMSIG(status); |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 1910 | if (cflag != CFLAG_ONLY_STATS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1911 | && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) { |
| 1912 | printleader(tcp); |
Denys Vlasenko | 13d22f1 | 2011-06-24 23:01:57 +0200 | [diff] [blame] | 1913 | #ifdef WCOREDUMP |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 1914 | tprintf("+++ killed by %s %s+++\n", |
Roland McGrath | 2efe879 | 2004-01-13 09:59:45 +0000 | [diff] [blame] | 1915 | signame(WTERMSIG(status)), |
Denys Vlasenko | 13d22f1 | 2011-06-24 23:01:57 +0200 | [diff] [blame] | 1916 | WCOREDUMP(status) ? "(core dumped) " : ""); |
| 1917 | #else |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 1918 | tprintf("+++ killed by %s +++\n", |
Denys Vlasenko | 13d22f1 | 2011-06-24 23:01:57 +0200 | [diff] [blame] | 1919 | signame(WTERMSIG(status))); |
Roland McGrath | 2efe879 | 2004-01-13 09:59:45 +0000 | [diff] [blame] | 1920 | #endif |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 1921 | printing_tcp = NULL; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1922 | } |
Denys Vlasenko | 5e09d77 | 2012-01-18 16:20:56 +0100 | [diff] [blame] | 1923 | fflush(tcp->outf); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1924 | droptcb(tcp); |
| 1925 | continue; |
| 1926 | } |
| 1927 | if (WIFEXITED(status)) { |
Dmitry V. Levin | a680965 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 1928 | if (pid == strace_child) |
| 1929 | exit_code = WEXITSTATUS(status); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 1930 | if (tcp == printing_tcp) { |
| 1931 | tprints(" <unfinished ...>\n"); |
| 1932 | printing_tcp = NULL; |
Roland McGrath | 0a39690 | 2003-06-10 03:05:53 +0000 | [diff] [blame] | 1933 | } |
Denys Vlasenko | 19cdada | 2011-08-17 10:45:32 +0200 | [diff] [blame] | 1934 | if (!cflag /* && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL) */ ) { |
| 1935 | printleader(tcp); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 1936 | tprintf("+++ exited with %d +++\n", WEXITSTATUS(status)); |
| 1937 | printing_tcp = NULL; |
Denys Vlasenko | 19cdada | 2011-08-17 10:45:32 +0200 | [diff] [blame] | 1938 | } |
Denys Vlasenko | 5e09d77 | 2012-01-18 16:20:56 +0100 | [diff] [blame] | 1939 | fflush(tcp->outf); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1940 | droptcb(tcp); |
| 1941 | continue; |
| 1942 | } |
| 1943 | if (!WIFSTOPPED(status)) { |
| 1944 | fprintf(stderr, "PANIC: pid %u not stopped\n", pid); |
| 1945 | droptcb(tcp); |
| 1946 | continue; |
| 1947 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1948 | |
Denys Vlasenko | f88837a | 2011-09-05 14:05:46 +0200 | [diff] [blame] | 1949 | /* Is this the very first time we see this tracee stopped? */ |
| 1950 | if (tcp->flags & TCB_STARTUP) { |
| 1951 | if (debug) |
| 1952 | fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n", tcp->pid); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1953 | tcp->flags &= ~TCB_STARTUP; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1954 | if (tcp->flags & TCB_BPTSET) { |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1955 | /* |
| 1956 | * One example is a breakpoint inherited from |
Denys Vlasenko | 2ecba32 | 2011-08-21 17:35:39 +0200 | [diff] [blame] | 1957 | * parent through fork(). |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1958 | */ |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 1959 | if (clearbpt(tcp) < 0) { |
| 1960 | /* Pretty fatal */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1961 | droptcb(tcp); |
| 1962 | cleanup(); |
| 1963 | return -1; |
| 1964 | } |
| 1965 | } |
Denys Vlasenko | 44f87ef | 2011-08-17 15:18:21 +0200 | [diff] [blame] | 1966 | if (ptrace_setoptions) { |
| 1967 | if (debug) |
| 1968 | fprintf(stderr, "setting opts %x on pid %d\n", ptrace_setoptions, tcp->pid); |
| 1969 | if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) { |
| 1970 | if (errno != ESRCH) { |
| 1971 | /* Should never happen, really */ |
| 1972 | perror_msg_and_die("PTRACE_SETOPTIONS"); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1973 | } |
| 1974 | } |
| 1975 | } |
Denys Vlasenko | f88837a | 2011-09-05 14:05:46 +0200 | [diff] [blame] | 1976 | } |
| 1977 | |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1978 | sig = WSTOPSIG(status); |
| 1979 | |
Denys Vlasenko | f7db5dd | 2012-01-28 01:16:02 +0100 | [diff] [blame] | 1980 | if (event != 0) { |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1981 | /* Ptrace event */ |
| 1982 | #ifdef USE_SEIZE |
| 1983 | if (event == PTRACE_EVENT_STOP || event == PTRACE_EVENT_STOP1) { |
Denys Vlasenko | 6703816 | 2012-01-29 16:46:46 +0100 | [diff] [blame] | 1984 | /* |
| 1985 | * PTRACE_INTERRUPT-stop or group-stop. |
| 1986 | * PTRACE_INTERRUPT-stop has sig == SIGTRAP here. |
| 1987 | */ |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 1988 | if (sig == SIGSTOP |
| 1989 | || sig == SIGTSTP |
| 1990 | || sig == SIGTTIN |
| 1991 | || sig == SIGTTOU |
| 1992 | ) { |
| 1993 | stopped = 1; |
| 1994 | goto show_stopsig; |
| 1995 | } |
| 1996 | } |
| 1997 | #endif |
Denys Vlasenko | f88837a | 2011-09-05 14:05:46 +0200 | [diff] [blame] | 1998 | goto restart_tracee_with_sig_0; |
| 1999 | } |
| 2000 | |
Denys Vlasenko | f88837a | 2011-09-05 14:05:46 +0200 | [diff] [blame] | 2001 | /* Is this post-attach SIGSTOP? |
| 2002 | * Interestingly, the process may stop |
| 2003 | * with STOPSIG equal to some other signal |
| 2004 | * than SIGSTOP if we happend to attach |
| 2005 | * just before the process takes a signal. |
| 2006 | */ |
| 2007 | if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) { |
| 2008 | if (debug) |
| 2009 | fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid); |
| 2010 | tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP; |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2011 | goto restart_tracee_with_sig_0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2014 | if (sig != syscall_trap_sig) { |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 2015 | siginfo_t si; |
| 2016 | |
| 2017 | /* Nonzero (true) if tracee is stopped by signal |
| 2018 | * (as opposed to "tracee received signal"). |
| 2019 | */ |
| 2020 | stopped = (ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0); |
Denys Vlasenko | 6703816 | 2012-01-29 16:46:46 +0100 | [diff] [blame] | 2021 | #ifdef USE_SEIZE |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 2022 | show_stopsig: |
Denys Vlasenko | 6703816 | 2012-01-29 16:46:46 +0100 | [diff] [blame] | 2023 | #endif |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 2024 | if (cflag != CFLAG_ONLY_STATS |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2025 | && (qual_flags[sig] & QUAL_SIGNAL)) { |
Dmitry V. Levin | c15dfc7 | 2011-03-10 14:44:45 +0000 | [diff] [blame] | 2026 | #if defined(PT_CR_IPSR) && defined(PT_CR_IIP) |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2027 | long pc = 0; |
| 2028 | long psr = 0; |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 2029 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2030 | upeek(tcp, PT_CR_IPSR, &psr); |
| 2031 | upeek(tcp, PT_CR_IIP, &pc); |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 2032 | |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2033 | # define PSR_RI 41 |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 2034 | pc += (psr >> PSR_RI) & 0x3; |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2035 | # define PC_FORMAT_STR " @ %lx" |
Denys Vlasenko | 2ecba32 | 2011-08-21 17:35:39 +0200 | [diff] [blame] | 2036 | # define PC_FORMAT_ARG , pc |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2037 | #else |
Denys Vlasenko | 2ecba32 | 2011-08-21 17:35:39 +0200 | [diff] [blame] | 2038 | # define PC_FORMAT_STR "" |
| 2039 | # define PC_FORMAT_ARG /* nothing */ |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 2040 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2041 | printleader(tcp); |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 2042 | if (!stopped) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 2043 | tprints("--- "); |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2044 | printsiginfo(&si, verbose(tcp)); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2045 | tprintf(" (%s)" PC_FORMAT_STR " ---\n", |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2046 | strsignal(sig) |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2047 | PC_FORMAT_ARG); |
| 2048 | } else |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2049 | tprintf("--- %s by %s" PC_FORMAT_STR " ---\n", |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2050 | strsignal(sig), |
| 2051 | signame(sig) |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2052 | PC_FORMAT_ARG); |
Denys Vlasenko | 000b601 | 2012-01-28 01:25:03 +0100 | [diff] [blame] | 2053 | printing_tcp = NULL; |
Denys Vlasenko | 5e09d77 | 2012-01-18 16:20:56 +0100 | [diff] [blame] | 2054 | fflush(tcp->outf); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2055 | } |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame] | 2056 | |
| 2057 | if (!stopped) |
| 2058 | /* It's signal-delivery-stop. Inject the signal */ |
| 2059 | goto restart_tracee; |
| 2060 | |
| 2061 | /* It's group-stop */ |
| 2062 | #ifdef USE_SEIZE |
| 2063 | if (use_seize) { |
| 2064 | /* |
| 2065 | * This ends ptrace-stop, but does *not* end group-stop. |
| 2066 | * This makes stopping signals work properly on straced process |
| 2067 | * (that is, process really stops. It used to continue to run). |
| 2068 | */ |
| 2069 | if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) { |
| 2070 | cleanup(); |
| 2071 | return -1; |
| 2072 | } |
| 2073 | continue; |
| 2074 | } |
| 2075 | /* We don't have PTRACE_LISTEN support... */ |
| 2076 | #endif |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2077 | goto restart_tracee; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2078 | } |
Denys Vlasenko | 2ecba32 | 2011-08-21 17:35:39 +0200 | [diff] [blame] | 2079 | |
| 2080 | /* We handled quick cases, we are permitted to interrupt now. */ |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 2081 | if (interrupted) |
| 2082 | return 0; |
Denys Vlasenko | 2ecba32 | 2011-08-21 17:35:39 +0200 | [diff] [blame] | 2083 | |
| 2084 | /* This should be syscall entry or exit. |
| 2085 | * (Or it still can be that pesky post-execve SIGTRAP!) |
| 2086 | * Handle it. |
| 2087 | */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2088 | if (trace_syscall(tcp) < 0 && !tcp->ptrace_errno) { |
| 2089 | /* ptrace() failed in trace_syscall() with ESRCH. |
| 2090 | * Likely a result of process disappearing mid-flight. |
| 2091 | * Observed case: exit_group() terminating |
Denys Vlasenko | f1e6903 | 2012-01-04 15:15:26 +0100 | [diff] [blame] | 2092 | * all processes in thread group. |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 2093 | */ |
Denys Vlasenko | f202502 | 2012-03-09 15:29:45 +0100 | [diff] [blame] | 2094 | if (printing_tcp) { |
| 2095 | /* Do we have dangling line "syscall(param, param"? |
| 2096 | * Finish the line then. |
Denys Vlasenko | f1e6903 | 2012-01-04 15:15:26 +0100 | [diff] [blame] | 2097 | */ |
Denys Vlasenko | f202502 | 2012-03-09 15:29:45 +0100 | [diff] [blame] | 2098 | printing_tcp->flags |= TCB_REPRINT; |
| 2099 | tprints(" <unfinished ...>\n"); |
| 2100 | printing_tcp = NULL; |
| 2101 | fflush(tcp->outf); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2102 | } |
Denys Vlasenko | f202502 | 2012-03-09 15:29:45 +0100 | [diff] [blame] | 2103 | /* We assume that ptrace error was caused by process death. |
| 2104 | * We used to detach(tcp) here, but since we no longer |
| 2105 | * implement "detach before death" policy/hack, |
| 2106 | * we can let this process to report its death to us |
| 2107 | * normally, via WIFEXITED or WIFSIGNALED wait status. |
| 2108 | */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2109 | continue; |
| 2110 | } |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2111 | restart_tracee_with_sig_0: |
| 2112 | sig = 0; |
| 2113 | restart_tracee: |
Andreas Schwab | ccdff48 | 2009-10-27 16:27:13 +0100 | [diff] [blame] | 2114 | /* Remember current print column before continuing. */ |
| 2115 | tcp->curcol = curcol; |
Denys Vlasenko | 6cda73f | 2011-09-02 16:23:53 +0200 | [diff] [blame] | 2116 | if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2117 | cleanup(); |
| 2118 | return -1; |
| 2119 | } |
| 2120 | } |
| 2121 | return 0; |
| 2122 | } |
Denys Vlasenko | ecc8b97 | 2012-03-12 23:05:25 +0100 | [diff] [blame] | 2123 | |
| 2124 | int |
| 2125 | main(int argc, char *argv[]) |
| 2126 | { |
| 2127 | init(argc, argv); |
| 2128 | |
| 2129 | /* Run main tracing loop */ |
| 2130 | if (trace() < 0) |
| 2131 | return 1; |
| 2132 | |
| 2133 | cleanup(); |
| 2134 | fflush(NULL); |
| 2135 | if (exit_code > 0xff) { |
| 2136 | /* Avoid potential core file clobbering. */ |
| 2137 | struct rlimit rlim = {0, 0}; |
| 2138 | setrlimit(RLIMIT_CORE, &rlim); |
| 2139 | |
| 2140 | /* Child was killed by a signal, mimic that. */ |
| 2141 | exit_code &= 0xff; |
| 2142 | signal(exit_code, SIG_DFL); |
| 2143 | raise(exit_code); |
| 2144 | /* Paranoia - what if this signal is not fatal? |
| 2145 | Exit with 128 + signo then. */ |
| 2146 | exit_code += 128; |
| 2147 | } |
| 2148 | |
| 2149 | return exit_code; |
| 2150 | } |