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