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