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