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