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