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