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