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