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