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> |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 47 | #include <limits.h> |
Roland McGrath | 70b0853 | 2004-04-09 00:25:21 +0000 | [diff] [blame] | 48 | #include <dirent.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 | #ifdef LINUX |
| 51 | # include <asm/unistd.h> |
| 52 | # if defined __NR_tgkill |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 53 | # define my_tgkill(pid, tid, sig) syscall(__NR_tgkill, (pid), (tid), (sig)) |
Roland McGrath | 134813a | 2007-06-02 00:07:33 +0000 | [diff] [blame] | 54 | # elif defined __NR_tkill |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 55 | # define my_tgkill(pid, tid, sig) syscall(__NR_tkill, (tid), (sig)) |
Roland McGrath | 134813a | 2007-06-02 00:07:33 +0000 | [diff] [blame] | 56 | # else |
| 57 | /* kill() may choose arbitrarily the target task of the process group |
| 58 | while we later wait on a that specific TID. PID process waits become |
| 59 | TID task specific waits for a process under ptrace(2). */ |
| 60 | # warning "Neither tkill(2) nor tgkill(2) available, risk of strace hangs!" |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 61 | # define my_tgkill(pid, tid, sig) kill((tid), (sig)) |
Roland McGrath | 134813a | 2007-06-02 00:07:33 +0000 | [diff] [blame] | 62 | # endif |
| 63 | #endif |
| 64 | |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 65 | #if defined(IA64) && defined(LINUX) |
| 66 | # include <asm/ptrace_offsets.h> |
| 67 | #endif |
| 68 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 69 | #ifdef USE_PROCFS |
| 70 | #include <poll.h> |
| 71 | #endif |
| 72 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 73 | #ifdef SVR4 |
| 74 | #include <sys/stropts.h> |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 75 | #ifdef HAVE_MP_PROCFS |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 76 | #ifdef HAVE_SYS_UIO_H |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 77 | #include <sys/uio.h> |
| 78 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 79 | #endif |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 80 | #endif |
Denys Vlasenko | 96d5a76 | 2008-12-29 19:13:27 +0000 | [diff] [blame] | 81 | extern char **environ; |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 82 | extern int optind; |
| 83 | extern char *optarg; |
Denys Vlasenko | 96d5a76 | 2008-12-29 19:13:27 +0000 | [diff] [blame] | 84 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 85 | |
Roland McGrath | 41c4822 | 2008-07-18 00:25:10 +0000 | [diff] [blame] | 86 | int debug = 0, followfork = 0; |
Denys Vlasenko | f44cce4 | 2011-06-21 14:34:10 +0200 | [diff] [blame] | 87 | unsigned int ptrace_setoptions = 0; |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 88 | /* Which WSTOPSIG(status) value marks syscall traps? */ |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 89 | static unsigned int syscall_trap_sig = SIGTRAP; |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 90 | int dtime = 0, xflag = 0, qflag = 0; |
| 91 | cflag_t cflag = CFLAG_NONE; |
Dmitry V. Levin | b9fe011 | 2006-12-13 16:59:44 +0000 | [diff] [blame] | 92 | static int iflag = 0, interactive = 0, pflag_seen = 0, rflag = 0, tflag = 0; |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 93 | /* |
| 94 | * daemonized_tracer supports -D option. |
| 95 | * With this option, strace forks twice. |
| 96 | * Unlike normal case, with -D *grandparent* process exec's, |
| 97 | * becoming a traced process. Child exits (this prevents traced process |
| 98 | * from having children it doesn't expect to have), and grandchild |
| 99 | * attaches to grandparent similarly to strace -p PID. |
| 100 | * This allows for more transparent interaction in cases |
| 101 | * when process and its parent are communicating via signals, |
| 102 | * wait() etc. Without -D, strace process gets lodged in between, |
| 103 | * disrupting parent<->child link. |
| 104 | */ |
| 105 | static bool daemonized_tracer = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 106 | |
Michal Ludvig | 17f8fb3 | 2002-11-06 13:17:21 +0000 | [diff] [blame] | 107 | /* Sometimes we want to print only succeeding syscalls. */ |
| 108 | int not_failing_only = 0; |
| 109 | |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 110 | /* Show path associated with fd arguments */ |
| 111 | int show_fd_path = 0; |
| 112 | |
| 113 | /* are we filtering traces based on paths? */ |
| 114 | int tracing_paths = 0; |
| 115 | |
Dmitry V. Levin | a680965 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 116 | static int exit_code = 0; |
| 117 | static int strace_child = 0; |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 118 | static int strace_tracer_pid = 0; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 119 | |
Dmitry V. Levin | b9fe011 | 2006-12-13 16:59:44 +0000 | [diff] [blame] | 120 | static char *username = NULL; |
Denys Vlasenko | ead73bd | 2011-06-24 22:49:58 +0200 | [diff] [blame] | 121 | static uid_t run_uid; |
| 122 | static gid_t run_gid; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 123 | |
| 124 | int acolumn = DEFAULT_ACOLUMN; |
| 125 | int max_strlen = DEFAULT_STRLEN; |
Dmitry V. Levin | b9fe011 | 2006-12-13 16:59:44 +0000 | [diff] [blame] | 126 | static char *outfname = NULL; |
Denys Vlasenko | ead73bd | 2011-06-24 22:49:58 +0200 | [diff] [blame] | 127 | static FILE *outf; |
Andreas Schwab | ccdff48 | 2009-10-27 16:27:13 +0100 | [diff] [blame] | 128 | static int curcol; |
Denys Vlasenko | ead73bd | 2011-06-24 22:49:58 +0200 | [diff] [blame] | 129 | static struct tcb **tcbtab; |
Denys Vlasenko | 2b60c35 | 2011-06-22 12:45:25 +0200 | [diff] [blame] | 130 | static unsigned int nprocs, tcbtabsize; |
Denys Vlasenko | ead73bd | 2011-06-24 22:49:58 +0200 | [diff] [blame] | 131 | static const char *progname; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 132 | |
Andreas Schwab | e5355de | 2009-10-27 16:56:43 +0100 | [diff] [blame] | 133 | static int detach(struct tcb *tcp, int sig); |
| 134 | static int trace(void); |
| 135 | static void cleanup(void); |
| 136 | static void interrupt(int sig); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 137 | static sigset_t empty_set, blocked_set; |
| 138 | |
| 139 | #ifdef HAVE_SIG_ATOMIC_T |
| 140 | static volatile sig_atomic_t interrupted; |
| 141 | #else /* !HAVE_SIG_ATOMIC_T */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 142 | static volatile int interrupted; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 143 | #endif /* !HAVE_SIG_ATOMIC_T */ |
| 144 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 145 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 146 | |
Andreas Schwab | e5355de | 2009-10-27 16:56:43 +0100 | [diff] [blame] | 147 | static struct tcb *pfd2tcb(int pfd); |
| 148 | static void reaper(int sig); |
| 149 | static void rebuild_pollv(void); |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 150 | static struct pollfd *pollv; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 151 | |
| 152 | #ifndef HAVE_POLLABLE_PROCFS |
| 153 | |
Andreas Schwab | e5355de | 2009-10-27 16:56:43 +0100 | [diff] [blame] | 154 | static void proc_poll_open(void); |
| 155 | static void proc_poller(int pfd); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 156 | |
| 157 | struct proc_pollfd { |
| 158 | int fd; |
| 159 | int revents; |
| 160 | int pid; |
| 161 | }; |
| 162 | |
| 163 | static int poller_pid; |
| 164 | static int proc_poll_pipe[2] = { -1, -1 }; |
| 165 | |
| 166 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 167 | |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 168 | #ifdef HAVE_MP_PROCFS |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 169 | #define POLLWANT POLLWRNORM |
| 170 | #else |
| 171 | #define POLLWANT POLLPRI |
| 172 | #endif |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 173 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 174 | |
| 175 | static void |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 176 | usage(FILE *ofp, int exitval) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 177 | { |
| 178 | fprintf(ofp, "\ |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 179 | usage: strace [-CdDffhiqrtttTvVxxy] [-a column] [-e expr] ... [-o file]\n\ |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 180 | [-p pid] ... [-s strsize] [-u username] [-E var=val] ...\n\ |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 181 | [-P path] [command [arg ...]]\n\ |
Andreas Schwab | b87d30c | 2010-06-11 15:49:36 +0200 | [diff] [blame] | 182 | or: strace -c [-D] [-e expr] ... [-O overhead] [-S sortby] [-E var=val] ...\n\ |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 183 | [command [arg ...]]\n\ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 184 | -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] | 185 | -C -- like -c but also print regular output while processes are running\n\ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 186 | -f -- follow forks, -ff -- with output into separate files\n\ |
| 187 | -F -- attempt to follow vforks, -h -- print help message\n\ |
| 188 | -i -- print instruction pointer at time of syscall\n\ |
| 189 | -q -- suppress messages about attaching, detaching, etc.\n\ |
| 190 | -r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\ |
| 191 | -T -- print time spent in each syscall, -V -- print version\n\ |
| 192 | -v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\ |
| 193 | -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] | 194 | -y -- print paths associated with file descriptor arguments\n\ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 195 | -a column -- alignment COLUMN for printing syscall results (default %d)\n\ |
| 196 | -e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\ |
| 197 | options: trace, abbrev, verbose, raw, signal, read, or write\n\ |
| 198 | -o file -- send trace output to FILE instead of stderr\n\ |
| 199 | -O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\ |
| 200 | -p pid -- trace process with process id PID, may be repeated\n\ |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 201 | -D -- run tracer process as a detached grandchild, not as parent\n\ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 202 | -s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\ |
| 203 | -S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\ |
| 204 | -u username -- run command as username handling setuid and/or setgid\n\ |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 205 | -E var=val -- put var=val in the environment for command\n\ |
| 206 | -E var -- remove var from the environment for command\n\ |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 207 | -P path -- trace accesses to path\n\ |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 208 | " /* this is broken, so don't document it |
Michal Ludvig | 17f8fb3 | 2002-11-06 13:17:21 +0000 | [diff] [blame] | 209 | -z -- print only succeeding syscalls\n\ |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 210 | */ |
| 211 | , DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 212 | exit(exitval); |
| 213 | } |
| 214 | |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 215 | static void die(void) __attribute__ ((noreturn)); |
| 216 | static void die(void) |
| 217 | { |
| 218 | if (strace_tracer_pid == getpid()) { |
| 219 | cflag = 0; |
| 220 | cleanup(); |
| 221 | } |
| 222 | exit(1); |
| 223 | } |
| 224 | |
| 225 | 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] | 226 | { |
Dmitry V. Levin | 44d0532 | 2011-06-09 15:50:41 +0000 | [diff] [blame] | 227 | fflush(NULL); |
| 228 | fprintf(stderr, "%s: ", progname); |
| 229 | vfprintf(stderr, fmt, p); |
| 230 | if (err_no) |
| 231 | fprintf(stderr, ": %s\n", strerror(err_no)); |
| 232 | else |
| 233 | putc('\n', stderr); |
| 234 | fflush(stderr); |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 235 | } |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 236 | |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 237 | void error_msg(const char *fmt, ...) |
| 238 | { |
| 239 | va_list p; |
| 240 | va_start(p, fmt); |
| 241 | verror_msg(0, fmt, p); |
| 242 | va_end(p); |
| 243 | } |
| 244 | |
| 245 | void error_msg_and_die(const char *fmt, ...) |
| 246 | { |
| 247 | va_list p; |
| 248 | va_start(p, fmt); |
| 249 | verror_msg(0, fmt, p); |
| 250 | die(); |
| 251 | } |
| 252 | |
| 253 | void perror_msg(const char *fmt, ...) |
| 254 | { |
| 255 | va_list p; |
| 256 | va_start(p, fmt); |
| 257 | verror_msg(errno, fmt, p); |
| 258 | va_end(p); |
| 259 | } |
| 260 | |
| 261 | void perror_msg_and_die(const char *fmt, ...) |
| 262 | { |
| 263 | va_list p; |
| 264 | va_start(p, fmt); |
| 265 | verror_msg(errno, fmt, p); |
| 266 | die(); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 269 | #ifdef SVR4 |
| 270 | #ifdef MIPS |
| 271 | void |
| 272 | foobar() |
| 273 | { |
| 274 | } |
| 275 | #endif /* MIPS */ |
| 276 | #endif /* SVR4 */ |
| 277 | |
Mike Frysinger | c1a5b7e | 2009-10-07 20:41:29 -0400 | [diff] [blame] | 278 | /* Glue for systems without a MMU that cannot provide fork() */ |
| 279 | #ifdef HAVE_FORK |
| 280 | # define strace_vforked 0 |
| 281 | #else |
| 282 | # define strace_vforked 1 |
| 283 | # define fork() vfork() |
| 284 | #endif |
| 285 | |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 286 | static void |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 287 | set_cloexec_flag(int fd) |
| 288 | { |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 289 | int flags, newflags; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 290 | |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 291 | flags = fcntl(fd, F_GETFD); |
| 292 | if (flags < 0) { |
| 293 | /* Can happen only if fd is bad. |
| 294 | * Should never happen: if it does, we have a bug |
| 295 | * in the caller. Therefore we just abort |
| 296 | * instead of propagating the error. |
| 297 | */ |
| 298 | perror_msg_and_die("fcntl(%d, F_GETFD)", fd); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | newflags = flags | FD_CLOEXEC; |
| 302 | if (flags == newflags) |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 303 | return; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 304 | |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 305 | fcntl(fd, F_SETFD, newflags); /* never fails */ |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /* |
| 309 | * When strace is setuid executable, we have to swap uids |
| 310 | * before and after filesystem and process management operations. |
| 311 | */ |
| 312 | static void |
| 313 | swap_uid(void) |
| 314 | { |
| 315 | #ifndef SVR4 |
| 316 | int euid = geteuid(), uid = getuid(); |
| 317 | |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 318 | if (euid != uid && setreuid(euid, uid) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 319 | perror_msg_and_die("setreuid"); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 320 | } |
| 321 | #endif |
| 322 | } |
| 323 | |
Roland McGrath | 4bfa626 | 2007-07-05 20:03:16 +0000 | [diff] [blame] | 324 | #if _LFS64_LARGEFILE |
| 325 | # define fopen_for_output fopen64 |
| 326 | #else |
| 327 | # define fopen_for_output fopen |
| 328 | #endif |
| 329 | |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 330 | static FILE * |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 331 | strace_fopen(const char *path) |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 332 | { |
| 333 | FILE *fp; |
| 334 | |
| 335 | swap_uid(); |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 336 | fp = fopen_for_output(path, "w"); |
| 337 | if (!fp) |
| 338 | perror_msg_and_die("Can't fopen '%s'", path); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 339 | swap_uid(); |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 340 | set_cloexec_flag(fileno(fp)); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 341 | return fp; |
| 342 | } |
| 343 | |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 344 | static int popen_pid = 0; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 345 | |
| 346 | #ifndef _PATH_BSHELL |
| 347 | # define _PATH_BSHELL "/bin/sh" |
| 348 | #endif |
| 349 | |
| 350 | /* |
| 351 | * We cannot use standard popen(3) here because we have to distinguish |
| 352 | * popen child process from other processes we trace, and standard popen(3) |
| 353 | * does not export its child's pid. |
| 354 | */ |
| 355 | static FILE * |
| 356 | strace_popen(const char *command) |
| 357 | { |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 358 | FILE *fp; |
| 359 | int fds[2]; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 360 | |
| 361 | swap_uid(); |
| 362 | if (pipe(fds) < 0) |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 363 | perror_msg_and_die("pipe"); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 364 | |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 365 | set_cloexec_flag(fds[1]); /* never fails */ |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 366 | |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 367 | popen_pid = vfork(); |
| 368 | if (popen_pid == -1) |
| 369 | perror_msg_and_die("vfork"); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 370 | |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 371 | if (popen_pid == 0) { |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 372 | /* child */ |
| 373 | close(fds[1]); |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 374 | if (fds[0] != 0) { |
| 375 | if (dup2(fds[0], 0)) |
| 376 | perror_msg_and_die("dup2"); |
| 377 | close(fds[0]); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 378 | } |
| 379 | execl(_PATH_BSHELL, "sh", "-c", command, NULL); |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 380 | perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 381 | } |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 382 | |
| 383 | /* parent */ |
| 384 | close(fds[0]); |
| 385 | swap_uid(); |
| 386 | fp = fdopen(fds[1], "w"); |
| 387 | if (!fp) |
| 388 | error_msg_and_die("Out of memory"); |
| 389 | return fp; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 392 | static void |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 393 | newoutf(struct tcb *tcp) |
| 394 | { |
| 395 | if (outfname && followfork > 1) { |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 396 | char name[520 + sizeof(int) * 3]; |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 397 | sprintf(name, "%.512s.%u", outfname, tcp->pid); |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 398 | tcp->outf = strace_fopen(name); |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 399 | } |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 402 | static void |
| 403 | startup_attach(void) |
| 404 | { |
| 405 | int tcbi; |
| 406 | struct tcb *tcp; |
| 407 | |
| 408 | /* |
| 409 | * Block user interruptions as we would leave the traced |
| 410 | * process stopped (process state T) if we would terminate in |
| 411 | * between PTRACE_ATTACH and wait4 () on SIGSTOP. |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 412 | * We rely on cleanup() from this point on. |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 413 | */ |
| 414 | if (interactive) |
| 415 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
| 416 | |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 417 | if (daemonized_tracer) { |
| 418 | pid_t pid = fork(); |
| 419 | if (pid < 0) { |
| 420 | _exit(1); |
| 421 | } |
| 422 | if (pid) { /* parent */ |
| 423 | /* |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 424 | * Wait for grandchild to attach to straced process |
| 425 | * (grandparent). Grandchild SIGKILLs us after it attached. |
| 426 | * Grandparent's wait() is unblocked by our death, |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 427 | * it proceeds to exec the straced program. |
| 428 | */ |
| 429 | pause(); |
| 430 | _exit(0); /* paranoia */ |
| 431 | } |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 432 | /* grandchild */ |
| 433 | /* We will be the tracer process. Remember our new pid: */ |
| 434 | strace_tracer_pid = getpid(); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 437 | for (tcbi = 0; tcbi < tcbtabsize; tcbi++) { |
| 438 | tcp = tcbtab[tcbi]; |
| 439 | if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED)) |
| 440 | continue; |
| 441 | #ifdef LINUX |
| 442 | if (tcp->flags & TCB_CLONE_THREAD) |
| 443 | continue; |
| 444 | #endif |
| 445 | /* Reinitialize the output since it may have changed. */ |
| 446 | tcp->outf = outf; |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 447 | newoutf(tcp); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 448 | |
| 449 | #ifdef USE_PROCFS |
| 450 | if (proc_open(tcp, 1) < 0) { |
| 451 | fprintf(stderr, "trouble opening proc file\n"); |
| 452 | droptcb(tcp); |
| 453 | continue; |
| 454 | } |
| 455 | #else /* !USE_PROCFS */ |
| 456 | # ifdef LINUX |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 457 | if (followfork && !daemonized_tracer) { |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 458 | char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3]; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 459 | DIR *dir; |
| 460 | |
| 461 | sprintf(procdir, "/proc/%d/task", tcp->pid); |
| 462 | dir = opendir(procdir); |
| 463 | if (dir != NULL) { |
| 464 | unsigned int ntid = 0, nerr = 0; |
| 465 | struct dirent *de; |
| 466 | int tid; |
| 467 | while ((de = readdir(dir)) != NULL) { |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 468 | if (de->d_fileno == 0) |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 469 | continue; |
| 470 | tid = atoi(de->d_name); |
| 471 | if (tid <= 0) |
| 472 | continue; |
| 473 | ++ntid; |
Denys Vlasenko | f95397a | 2011-06-24 16:51:16 +0200 | [diff] [blame] | 474 | if (ptrace(PTRACE_ATTACH, tid, (char *) 1, 0) < 0) { |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 475 | ++nerr; |
Denys Vlasenko | f95397a | 2011-06-24 16:51:16 +0200 | [diff] [blame] | 476 | if (debug) |
| 477 | fprintf(stderr, "attach to pid %d failed\n", tid); |
| 478 | } |
| 479 | else { |
| 480 | if (debug) |
| 481 | fprintf(stderr, "attach to pid %d succeeded\n", tid); |
| 482 | if (tid != tcbtab[tcbi]->pid) { |
| 483 | tcp = alloctcb(tid); |
| 484 | tcp->flags |= TCB_ATTACHED|TCB_CLONE_THREAD; |
| 485 | tcbtab[tcbi]->nclone_threads++; |
| 486 | tcp->parent = tcbtab[tcbi]; |
| 487 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 488 | } |
Denys Vlasenko | aab52ca | 2009-03-17 14:46:54 +0000 | [diff] [blame] | 489 | if (interactive) { |
| 490 | sigprocmask(SIG_SETMASK, &empty_set, NULL); |
| 491 | if (interrupted) |
| 492 | return; |
| 493 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
| 494 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 495 | } |
| 496 | closedir(dir); |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 497 | ntid -= nerr; |
| 498 | if (ntid == 0) { |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 499 | perror("attach: ptrace(PTRACE_ATTACH, ...)"); |
| 500 | droptcb(tcp); |
| 501 | continue; |
| 502 | } |
| 503 | if (!qflag) { |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 504 | fprintf(stderr, ntid > 1 |
| 505 | ? "Process %u attached with %u threads - interrupt to quit\n" |
| 506 | : "Process %u attached - interrupt to quit\n", |
| 507 | tcbtab[tcbi]->pid, ntid); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 508 | } |
| 509 | continue; |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 510 | } /* if (opendir worked) */ |
| 511 | } /* if (-f) */ |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 512 | # endif |
| 513 | if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) { |
| 514 | perror("attach: ptrace(PTRACE_ATTACH, ...)"); |
| 515 | droptcb(tcp); |
| 516 | continue; |
| 517 | } |
Denys Vlasenko | f95397a | 2011-06-24 16:51:16 +0200 | [diff] [blame] | 518 | if (debug) |
| 519 | fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 520 | |
| 521 | if (daemonized_tracer) { |
| 522 | /* |
| 523 | * It is our grandparent we trace, not a -p PID. |
| 524 | * Don't want to just detach on exit, so... |
| 525 | */ |
| 526 | tcp->flags &= ~TCB_ATTACHED; |
| 527 | /* |
| 528 | * Make parent go away. |
| 529 | * Also makes grandparent's wait() unblock. |
| 530 | */ |
| 531 | kill(getppid(), SIGKILL); |
| 532 | } |
| 533 | |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 534 | #endif /* !USE_PROCFS */ |
| 535 | if (!qflag) |
| 536 | fprintf(stderr, |
| 537 | "Process %u attached - interrupt to quit\n", |
| 538 | tcp->pid); |
Denys Vlasenko | f95397a | 2011-06-24 16:51:16 +0200 | [diff] [blame] | 539 | } /* for each tcbtab[] */ |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 540 | |
| 541 | if (interactive) |
| 542 | sigprocmask(SIG_SETMASK, &empty_set, NULL); |
| 543 | } |
| 544 | |
| 545 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 546 | startup_child(char **argv) |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 547 | { |
| 548 | struct stat statbuf; |
| 549 | const char *filename; |
| 550 | char pathname[MAXPATHLEN]; |
| 551 | int pid = 0; |
| 552 | struct tcb *tcp; |
| 553 | |
| 554 | filename = argv[0]; |
| 555 | if (strchr(filename, '/')) { |
| 556 | if (strlen(filename) > sizeof pathname - 1) { |
| 557 | errno = ENAMETOOLONG; |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 558 | perror_msg_and_die("exec"); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 559 | } |
| 560 | strcpy(pathname, filename); |
| 561 | } |
| 562 | #ifdef USE_DEBUGGING_EXEC |
| 563 | /* |
| 564 | * Debuggers customarily check the current directory |
| 565 | * first regardless of the path but doing that gives |
| 566 | * security geeks a panic attack. |
| 567 | */ |
| 568 | else if (stat(filename, &statbuf) == 0) |
| 569 | strcpy(pathname, filename); |
| 570 | #endif /* USE_DEBUGGING_EXEC */ |
| 571 | else { |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 572 | const char *path; |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 573 | int m, n, len; |
| 574 | |
| 575 | for (path = getenv("PATH"); path && *path; path += m) { |
| 576 | if (strchr(path, ':')) { |
| 577 | n = strchr(path, ':') - path; |
| 578 | m = n + 1; |
| 579 | } |
| 580 | else |
| 581 | m = n = strlen(path); |
| 582 | if (n == 0) { |
| 583 | if (!getcwd(pathname, MAXPATHLEN)) |
| 584 | continue; |
| 585 | len = strlen(pathname); |
| 586 | } |
| 587 | else if (n > sizeof pathname - 1) |
| 588 | continue; |
| 589 | else { |
| 590 | strncpy(pathname, path, n); |
| 591 | len = n; |
| 592 | } |
| 593 | if (len && pathname[len - 1] != '/') |
| 594 | pathname[len++] = '/'; |
| 595 | strcpy(pathname + len, filename); |
| 596 | if (stat(pathname, &statbuf) == 0 && |
| 597 | /* Accept only regular files |
| 598 | with some execute bits set. |
| 599 | XXX not perfect, might still fail */ |
| 600 | S_ISREG(statbuf.st_mode) && |
| 601 | (statbuf.st_mode & 0111)) |
| 602 | break; |
| 603 | } |
| 604 | } |
| 605 | if (stat(pathname, &statbuf) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 606 | perror_msg_and_die("Can't stat '%s'", filename); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 607 | } |
Dmitry V. Levin | a680965 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 608 | strace_child = pid = fork(); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 609 | if (pid < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 610 | perror_msg_and_die("fork"); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 611 | } |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 612 | if ((pid != 0 && daemonized_tracer) /* -D: parent to become a traced process */ |
| 613 | || (pid == 0 && !daemonized_tracer) /* not -D: child to become a traced process */ |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 614 | ) { |
| 615 | pid = getpid(); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 616 | #ifdef USE_PROCFS |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 617 | if (outf != stderr) close(fileno(outf)); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 618 | #ifdef MIPS |
| 619 | /* Kludge for SGI, see proc_open for details. */ |
| 620 | sa.sa_handler = foobar; |
| 621 | sa.sa_flags = 0; |
| 622 | sigemptyset(&sa.sa_mask); |
| 623 | sigaction(SIGINT, &sa, NULL); |
| 624 | #endif /* MIPS */ |
| 625 | #ifndef FREEBSD |
| 626 | pause(); |
| 627 | #else /* FREEBSD */ |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 628 | kill(pid, SIGSTOP); /* stop HERE */ |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 629 | #endif /* FREEBSD */ |
| 630 | #else /* !USE_PROCFS */ |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 631 | if (outf != stderr) |
| 632 | close(fileno(outf)); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 633 | |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 634 | if (!daemonized_tracer) { |
| 635 | if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 636 | perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)"); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 637 | } |
| 638 | if (debug) |
| 639 | kill(pid, SIGSTOP); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 640 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 641 | |
| 642 | if (username != NULL || geteuid() == 0) { |
| 643 | uid_t run_euid = run_uid; |
| 644 | gid_t run_egid = run_gid; |
| 645 | |
| 646 | if (statbuf.st_mode & S_ISUID) |
| 647 | run_euid = statbuf.st_uid; |
| 648 | if (statbuf.st_mode & S_ISGID) |
| 649 | run_egid = statbuf.st_gid; |
| 650 | |
| 651 | /* |
| 652 | * It is important to set groups before we |
| 653 | * lose privileges on setuid. |
| 654 | */ |
| 655 | if (username != NULL) { |
| 656 | if (initgroups(username, run_gid) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 657 | perror_msg_and_die("initgroups"); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 658 | } |
| 659 | if (setregid(run_gid, run_egid) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 660 | perror_msg_and_die("setregid"); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 661 | } |
| 662 | if (setreuid(run_uid, run_euid) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 663 | perror_msg_and_die("setreuid"); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | } |
| 667 | else |
| 668 | setreuid(run_uid, run_uid); |
| 669 | |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 670 | if (!daemonized_tracer) { |
| 671 | /* |
| 672 | * Induce an immediate stop so that the parent |
| 673 | * will resume us with PTRACE_SYSCALL and display |
| 674 | * this execve call normally. |
Mike Frysinger | c1a5b7e | 2009-10-07 20:41:29 -0400 | [diff] [blame] | 675 | * Unless of course we're on a no-MMU system where |
| 676 | * we vfork()-ed, so we cannot stop the child. |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 677 | */ |
Mike Frysinger | c1a5b7e | 2009-10-07 20:41:29 -0400 | [diff] [blame] | 678 | if (!strace_vforked) |
| 679 | kill(getpid(), SIGSTOP); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 680 | } else { |
| 681 | struct sigaction sv_sigchld; |
| 682 | sigaction(SIGCHLD, NULL, &sv_sigchld); |
| 683 | /* |
| 684 | * Make sure it is not SIG_IGN, otherwise wait |
| 685 | * will not block. |
| 686 | */ |
| 687 | signal(SIGCHLD, SIG_DFL); |
| 688 | /* |
| 689 | * Wait for grandchild to attach to us. |
| 690 | * It kills child after that, and wait() unblocks. |
| 691 | */ |
| 692 | alarm(3); |
| 693 | wait(NULL); |
| 694 | alarm(0); |
| 695 | sigaction(SIGCHLD, &sv_sigchld, NULL); |
| 696 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 697 | #endif /* !USE_PROCFS */ |
| 698 | |
| 699 | execv(pathname, argv); |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 700 | perror_msg_and_die("exec"); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 701 | } |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 702 | |
| 703 | /* We are the tracer. */ |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 704 | /* With -D, we are *child* here, IOW: different pid. Fetch it. */ |
| 705 | strace_tracer_pid = getpid(); |
| 706 | |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 707 | tcp = alloctcb(daemonized_tracer ? getppid() : pid); |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 708 | if (daemonized_tracer) { |
| 709 | /* We want subsequent startup_attach() to attach to it. */ |
| 710 | tcp->flags |= TCB_ATTACHED; |
| 711 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 712 | #ifdef USE_PROCFS |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 713 | if (proc_open(tcp, 0) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 714 | perror_msg_and_die("trouble opening proc file"); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 715 | } |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 716 | #endif /* USE_PROCFS */ |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 719 | #ifdef LINUX |
| 720 | /* |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 721 | * Test whether the kernel support PTRACE_O_TRACECLONE et al options. |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 722 | * 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] | 723 | * and then see which options are supported by the kernel. |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 724 | */ |
| 725 | static int |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 726 | test_ptrace_setoptions_followfork(void) |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 727 | { |
Dmitry V. Levin | 2fabd0e | 2011-02-19 21:33:50 +0000 | [diff] [blame] | 728 | int pid, expected_grandchild = 0, found_grandchild = 0; |
| 729 | const unsigned int test_options = PTRACE_O_TRACECLONE | |
| 730 | PTRACE_O_TRACEFORK | |
| 731 | PTRACE_O_TRACEVFORK; |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 732 | |
| 733 | if ((pid = fork()) < 0) |
| 734 | return -1; |
| 735 | else if (pid == 0) { |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 736 | if (ptrace(PTRACE_TRACEME, 0, (char *)1, 0) < 0) |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 737 | _exit(1); |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 738 | kill(getpid(), SIGSTOP); |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 739 | _exit(fork() < 0); |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 740 | } |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 741 | |
| 742 | while (1) { |
| 743 | int status, tracee_pid; |
| 744 | |
| 745 | tracee_pid = wait(&status); |
| 746 | if (tracee_pid == -1) { |
| 747 | if (errno == EINTR) |
| 748 | continue; |
| 749 | else if (errno == ECHILD) |
| 750 | break; |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 751 | perror("test_ptrace_setoptions_followfork"); |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 752 | return -1; |
| 753 | } |
| 754 | if (tracee_pid != pid) { |
Dmitry V. Levin | 2fabd0e | 2011-02-19 21:33:50 +0000 | [diff] [blame] | 755 | found_grandchild = tracee_pid; |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 756 | if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0 && |
| 757 | errno != ESRCH) |
| 758 | kill(tracee_pid, SIGKILL); |
| 759 | } |
| 760 | else if (WIFSTOPPED(status)) { |
Dmitry V. Levin | 2fabd0e | 2011-02-19 21:33:50 +0000 | [diff] [blame] | 761 | switch (WSTOPSIG(status)) { |
| 762 | case SIGSTOP: |
| 763 | if (ptrace(PTRACE_SETOPTIONS, pid, |
| 764 | NULL, test_options) < 0) { |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 765 | kill(pid, SIGKILL); |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 766 | return -1; |
| 767 | } |
Dmitry V. Levin | 2fabd0e | 2011-02-19 21:33:50 +0000 | [diff] [blame] | 768 | break; |
| 769 | case SIGTRAP: |
| 770 | if (status >> 16 == PTRACE_EVENT_FORK) { |
| 771 | long msg = 0; |
| 772 | |
| 773 | if (ptrace(PTRACE_GETEVENTMSG, pid, |
| 774 | NULL, (long) &msg) == 0) |
| 775 | expected_grandchild = msg; |
| 776 | } |
| 777 | break; |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 778 | } |
Dmitry V. Levin | b146744 | 2010-12-02 20:56:43 +0000 | [diff] [blame] | 779 | if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0 && |
| 780 | errno != ESRCH) |
| 781 | kill(pid, SIGKILL); |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 782 | } |
| 783 | } |
Dmitry V. Levin | 2fabd0e | 2011-02-19 21:33:50 +0000 | [diff] [blame] | 784 | if (expected_grandchild && expected_grandchild == found_grandchild) |
Denys Vlasenko | f44cce4 | 2011-06-21 14:34:10 +0200 | [diff] [blame] | 785 | ptrace_setoptions |= test_options; |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 786 | return 0; |
| 787 | } |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 788 | |
| 789 | /* |
| 790 | * Test whether the kernel support PTRACE_O_TRACESYSGOOD. |
| 791 | * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it, |
| 792 | * and then see whether it will stop with (SIGTRAP | 0x80). |
| 793 | * |
| 794 | * Use of this option enables correct handling of user-generated SIGTRAPs, |
| 795 | * and SIGTRAPs generated by special instructions such as int3 on x86: |
| 796 | * _start: .globl _start |
| 797 | * int3 |
| 798 | * movl $42, %ebx |
| 799 | * movl $1, %eax |
| 800 | * int $0x80 |
| 801 | * (compile with: "gcc -nostartfiles -nostdlib -o int3 int3.S") |
| 802 | */ |
| 803 | static void |
| 804 | test_ptrace_setoptions_for_all(void) |
| 805 | { |
| 806 | const unsigned int test_options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEEXEC; |
| 807 | int pid; |
| 808 | int it_worked = 0; |
| 809 | |
| 810 | pid = fork(); |
| 811 | if (pid < 0) |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 812 | perror_msg_and_die("fork"); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 813 | |
| 814 | if (pid == 0) { |
| 815 | pid = getpid(); |
| 816 | if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 817 | /* Note: exits with exitcode 1 */ |
| 818 | perror_msg_and_die("%s: PTRACE_TRACEME doesn't work", __func__); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 819 | kill(pid, SIGSTOP); |
| 820 | _exit(0); /* parent should see entry into this syscall */ |
| 821 | } |
| 822 | |
| 823 | while (1) { |
| 824 | int status, tracee_pid; |
| 825 | |
| 826 | errno = 0; |
| 827 | tracee_pid = wait(&status); |
| 828 | if (tracee_pid <= 0) { |
| 829 | if (errno == EINTR) |
| 830 | continue; |
| 831 | kill(pid, SIGKILL); |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 832 | perror_msg_and_die("%s: unexpected wait result %d", __func__, tracee_pid); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 833 | } |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 834 | if (WIFEXITED(status)) { |
| 835 | if (WEXITSTATUS(status) == 0) |
| 836 | break; |
| 837 | /* PTRACE_TRACEME failed in child. This is fatal. */ |
| 838 | exit(1); |
| 839 | } |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 840 | if (!WIFSTOPPED(status)) { |
| 841 | kill(pid, SIGKILL); |
| 842 | error_msg_and_die("%s: unexpected wait status %x", __func__, status); |
| 843 | } |
| 844 | if (WSTOPSIG(status) == SIGSTOP) { |
| 845 | /* |
| 846 | * We don't check "options aren't accepted" error. |
| 847 | * If it happens, we'll never get (SIGTRAP | 0x80), |
| 848 | * and thus will decide to not use the option. |
| 849 | * IOW: the outcome of the test will be correct. |
| 850 | */ |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 851 | if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0) |
| 852 | if (errno != EINVAL) |
| 853 | perror_msg("PTRACE_SETOPTIONS"); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 854 | } |
| 855 | if (WSTOPSIG(status) == (SIGTRAP | 0x80)) { |
| 856 | it_worked = 1; |
| 857 | } |
| 858 | if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) { |
| 859 | kill(pid, SIGKILL); |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 860 | perror_msg_and_die("PTRACE_SYSCALL doesn't work"); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
| 864 | if (it_worked) { |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 865 | syscall_trap_sig = (SIGTRAP | 0x80); |
Denys Vlasenko | f44cce4 | 2011-06-21 14:34:10 +0200 | [diff] [blame] | 866 | ptrace_setoptions |= test_options; |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 867 | if (debug) |
Denys Vlasenko | f44cce4 | 2011-06-21 14:34:10 +0200 | [diff] [blame] | 868 | fprintf(stderr, "ptrace_setoptions = %#x\n", |
| 869 | ptrace_setoptions); |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 870 | return; |
| 871 | } |
| 872 | |
| 873 | fprintf(stderr, |
| 874 | "Test for PTRACE_O_TRACESYSGOOD failed, giving up using this feature.\n"); |
| 875 | } |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 876 | #endif |
| 877 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 878 | int |
Dmitry V. Levin | 08b623e | 2007-10-08 21:04:41 +0000 | [diff] [blame] | 879 | main(int argc, char *argv[]) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 880 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 881 | struct tcb *tcp; |
| 882 | int c, pid = 0; |
Dmitry V. Levin | 06350db | 2008-07-25 15:42:34 +0000 | [diff] [blame] | 883 | int optF = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 884 | struct sigaction sa; |
| 885 | |
Dmitry V. Levin | 08b623e | 2007-10-08 21:04:41 +0000 | [diff] [blame] | 886 | progname = argv[0] ? argv[0] : "strace"; |
| 887 | |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 888 | strace_tracer_pid = getpid(); |
| 889 | |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 890 | /* Allocate the initial tcbtab. */ |
| 891 | tcbtabsize = argc; /* Surely enough for all -p args. */ |
Denys Vlasenko | 4f12af2 | 2011-06-23 13:16:23 +0200 | [diff] [blame] | 892 | tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0])); |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 893 | if (tcbtab == NULL) |
| 894 | error_msg_and_die("Out of memory"); |
Denys Vlasenko | 4f12af2 | 2011-06-23 13:16:23 +0200 | [diff] [blame] | 895 | tcp = calloc(tcbtabsize, sizeof(*tcp)); |
| 896 | if (tcp == NULL) |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 897 | error_msg_and_die("Out of memory"); |
Denys Vlasenko | 4f12af2 | 2011-06-23 13:16:23 +0200 | [diff] [blame] | 898 | for (c = 0; c < tcbtabsize; c++) |
| 899 | tcbtab[c] = tcp++; |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 900 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 901 | outf = stderr; |
| 902 | interactive = 1; |
Roland McGrath | 138c6a3 | 2006-01-12 09:50:49 +0000 | [diff] [blame] | 903 | set_sortby(DEFAULT_SORTBY); |
| 904 | set_personality(DEFAULT_PERSONALITY); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 905 | qualify("trace=all"); |
| 906 | qualify("abbrev=all"); |
| 907 | qualify("verbose=all"); |
| 908 | qualify("signal=all"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 909 | while ((c = getopt(argc, argv, |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 910 | "+cCdfFhiqrtTvVxyz" |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 911 | #ifndef USE_PROCFS |
| 912 | "D" |
| 913 | #endif |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 914 | "a:e:o:O:p:s:S:u:E:P:")) != EOF) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 915 | switch (c) { |
| 916 | case 'c': |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 917 | if (cflag == CFLAG_BOTH) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 918 | error_msg_and_die("-c and -C are mutually exclusive options"); |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 919 | } |
| 920 | cflag = CFLAG_ONLY_STATS; |
| 921 | break; |
| 922 | case 'C': |
| 923 | if (cflag == CFLAG_ONLY_STATS) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 924 | error_msg_and_die("-c and -C are mutually exclusive options"); |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 925 | } |
| 926 | cflag = CFLAG_BOTH; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 927 | break; |
| 928 | case 'd': |
| 929 | debug++; |
| 930 | break; |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 931 | #ifndef USE_PROCFS |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 932 | case 'D': |
| 933 | daemonized_tracer = 1; |
| 934 | break; |
| 935 | #endif |
Roland McGrath | 41c4822 | 2008-07-18 00:25:10 +0000 | [diff] [blame] | 936 | case 'F': |
Dmitry V. Levin | 06350db | 2008-07-25 15:42:34 +0000 | [diff] [blame] | 937 | optF = 1; |
| 938 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 939 | case 'f': |
| 940 | followfork++; |
| 941 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 942 | case 'h': |
| 943 | usage(stdout, 0); |
| 944 | break; |
| 945 | case 'i': |
| 946 | iflag++; |
| 947 | break; |
| 948 | case 'q': |
| 949 | qflag++; |
| 950 | break; |
| 951 | case 'r': |
| 952 | rflag++; |
| 953 | tflag++; |
| 954 | break; |
| 955 | case 't': |
| 956 | tflag++; |
| 957 | break; |
| 958 | case 'T': |
| 959 | dtime++; |
| 960 | break; |
| 961 | case 'x': |
| 962 | xflag++; |
| 963 | break; |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 964 | case 'y': |
| 965 | show_fd_path = 1; |
| 966 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 967 | case 'v': |
| 968 | qualify("abbrev=none"); |
| 969 | break; |
| 970 | case 'V': |
Roland McGrath | 9c9a253 | 2003-02-20 02:56:29 +0000 | [diff] [blame] | 971 | printf("%s -- version %s\n", PACKAGE_NAME, VERSION); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 972 | exit(0); |
| 973 | break; |
Michal Ludvig | 17f8fb3 | 2002-11-06 13:17:21 +0000 | [diff] [blame] | 974 | case 'z': |
| 975 | not_failing_only = 1; |
| 976 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 977 | case 'a': |
| 978 | acolumn = atoi(optarg); |
| 979 | break; |
| 980 | case 'e': |
| 981 | qualify(optarg); |
| 982 | break; |
| 983 | case 'o': |
| 984 | outfname = strdup(optarg); |
| 985 | break; |
| 986 | case 'O': |
| 987 | set_overhead(atoi(optarg)); |
| 988 | break; |
| 989 | case 'p': |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 990 | if ((pid = atoi(optarg)) <= 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 991 | error_msg("Invalid process id: '%s'", optarg); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 992 | break; |
| 993 | } |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 994 | if (pid == strace_tracer_pid) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 995 | 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] | 996 | break; |
| 997 | } |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 998 | tcp = alloc_tcb(pid, 0); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 999 | tcp->flags |= TCB_ATTACHED; |
| 1000 | pflag_seen++; |
| 1001 | break; |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 1002 | case 'P': |
| 1003 | tracing_paths = 1; |
| 1004 | if (pathtrace_select(optarg)) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1005 | error_msg_and_die("Failed to select path '%s'", optarg); |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 1006 | } |
| 1007 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1008 | case 's': |
| 1009 | max_strlen = atoi(optarg); |
Roland McGrath | dccec72 | 2005-05-09 07:45:47 +0000 | [diff] [blame] | 1010 | if (max_strlen < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1011 | error_msg_and_die("Invalid -s argument: '%s'", optarg); |
Roland McGrath | dccec72 | 2005-05-09 07:45:47 +0000 | [diff] [blame] | 1012 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1013 | break; |
| 1014 | case 'S': |
| 1015 | set_sortby(optarg); |
| 1016 | break; |
| 1017 | case 'u': |
| 1018 | username = strdup(optarg); |
| 1019 | break; |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 1020 | case 'E': |
| 1021 | if (putenv(optarg) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1022 | error_msg_and_die("Out of memory"); |
Roland McGrath | de6e533 | 2003-01-24 04:31:23 +0000 | [diff] [blame] | 1023 | } |
| 1024 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1025 | default: |
| 1026 | usage(stderr, 1); |
| 1027 | break; |
| 1028 | } |
| 1029 | } |
| 1030 | |
Roland McGrath | d0c4c0c | 2006-04-25 07:39:40 +0000 | [diff] [blame] | 1031 | if ((optind == argc) == !pflag_seen) |
Roland McGrath | ce0d154 | 2003-11-11 21:24:23 +0000 | [diff] [blame] | 1032 | usage(stderr, 1); |
| 1033 | |
Wang Chao | d322a4b | 2010-08-05 14:30:11 +0800 | [diff] [blame] | 1034 | if (pflag_seen && daemonized_tracer) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1035 | error_msg_and_die("-D and -p are mutually exclusive options"); |
Wang Chao | d322a4b | 2010-08-05 14:30:11 +0800 | [diff] [blame] | 1036 | } |
| 1037 | |
Dmitry V. Levin | 06350db | 2008-07-25 15:42:34 +0000 | [diff] [blame] | 1038 | if (!followfork) |
| 1039 | followfork = optF; |
| 1040 | |
Roland McGrath | cb9def6 | 2006-04-25 07:48:03 +0000 | [diff] [blame] | 1041 | if (followfork > 1 && cflag) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1042 | 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] | 1043 | } |
| 1044 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1045 | /* See if they want to run as another user. */ |
| 1046 | if (username != NULL) { |
| 1047 | struct passwd *pent; |
| 1048 | |
| 1049 | if (getuid() != 0 || geteuid() != 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1050 | 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] | 1051 | } |
| 1052 | if ((pent = getpwnam(username)) == NULL) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1053 | error_msg_and_die("Cannot find user '%s'", username); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1054 | } |
| 1055 | run_uid = pent->pw_uid; |
| 1056 | run_gid = pent->pw_gid; |
| 1057 | } |
| 1058 | else { |
| 1059 | run_uid = getuid(); |
| 1060 | run_gid = getgid(); |
| 1061 | } |
| 1062 | |
Dmitry V. Levin | 8044bc1 | 2010-12-07 12:50:49 +0000 | [diff] [blame] | 1063 | #ifdef LINUX |
| 1064 | if (followfork) { |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1065 | if (test_ptrace_setoptions_followfork() < 0) { |
Dmitry V. Levin | 8044bc1 | 2010-12-07 12:50:49 +0000 | [diff] [blame] | 1066 | fprintf(stderr, |
| 1067 | "Test for options supported by PTRACE_SETOPTIONS " |
| 1068 | "failed, giving up using this feature.\n"); |
Denys Vlasenko | f44cce4 | 2011-06-21 14:34:10 +0200 | [diff] [blame] | 1069 | ptrace_setoptions = 0; |
Dmitry V. Levin | 8044bc1 | 2010-12-07 12:50:49 +0000 | [diff] [blame] | 1070 | } |
| 1071 | if (debug) |
Denys Vlasenko | f44cce4 | 2011-06-21 14:34:10 +0200 | [diff] [blame] | 1072 | fprintf(stderr, "ptrace_setoptions = %#x\n", |
| 1073 | ptrace_setoptions); |
Dmitry V. Levin | 8044bc1 | 2010-12-07 12:50:49 +0000 | [diff] [blame] | 1074 | } |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 1075 | test_ptrace_setoptions_for_all(); |
Dmitry V. Levin | 8044bc1 | 2010-12-07 12:50:49 +0000 | [diff] [blame] | 1076 | #endif |
| 1077 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1078 | /* Check if they want to redirect the output. */ |
| 1079 | if (outfname) { |
Roland McGrath | 37b9a66 | 2003-11-07 02:26:54 +0000 | [diff] [blame] | 1080 | /* See if they want to pipe the output. */ |
| 1081 | if (outfname[0] == '|' || outfname[0] == '!') { |
| 1082 | /* |
| 1083 | * We can't do the <outfname>.PID funny business |
| 1084 | * when using popen, so prohibit it. |
| 1085 | */ |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 1086 | if (followfork > 1) |
| 1087 | error_msg_and_die("Piping the output and -ff are mutually exclusive"); |
| 1088 | outf = strace_popen(outfname + 1); |
Roland McGrath | 37b9a66 | 2003-11-07 02:26:54 +0000 | [diff] [blame] | 1089 | } |
Denys Vlasenko | 3d5ed41 | 2011-06-22 13:17:16 +0200 | [diff] [blame] | 1090 | else if (followfork <= 1) |
| 1091 | outf = strace_fopen(outfname); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1094 | if (!outfname || outfname[0] == '|' || outfname[0] == '!') { |
| 1095 | static char buf[BUFSIZ]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1096 | setvbuf(outf, buf, _IOLBF, BUFSIZ); |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1097 | } |
Roland McGrath | 37b9a66 | 2003-11-07 02:26:54 +0000 | [diff] [blame] | 1098 | if (outfname && optind < argc) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1099 | interactive = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1100 | qflag = 1; |
Roland McGrath | 3693105 | 2003-06-03 01:35:20 +0000 | [diff] [blame] | 1101 | } |
Wang Chao | b13c0de | 2010-11-12 17:25:19 +0800 | [diff] [blame] | 1102 | |
Roland McGrath | 54cc1c8 | 2007-11-03 23:34:11 +0000 | [diff] [blame] | 1103 | /* Valid states here: |
| 1104 | optind < argc pflag_seen outfname interactive |
| 1105 | 1 0 0 1 |
| 1106 | 0 1 0 1 |
| 1107 | 1 0 1 0 |
| 1108 | 0 1 1 1 |
| 1109 | */ |
| 1110 | |
| 1111 | /* STARTUP_CHILD must be called before the signal handlers get |
| 1112 | installed below as they are inherited into the spawned process. |
| 1113 | Also we do not need to be protected by them as during interruption |
| 1114 | in the STARTUP_CHILD mode we kill the spawned process anyway. */ |
| 1115 | if (!pflag_seen) |
| 1116 | startup_child(&argv[optind]); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1117 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1118 | sigemptyset(&empty_set); |
| 1119 | sigemptyset(&blocked_set); |
| 1120 | sa.sa_handler = SIG_IGN; |
| 1121 | sigemptyset(&sa.sa_mask); |
| 1122 | sa.sa_flags = 0; |
| 1123 | sigaction(SIGTTOU, &sa, NULL); |
| 1124 | sigaction(SIGTTIN, &sa, NULL); |
| 1125 | if (interactive) { |
| 1126 | sigaddset(&blocked_set, SIGHUP); |
| 1127 | sigaddset(&blocked_set, SIGINT); |
| 1128 | sigaddset(&blocked_set, SIGQUIT); |
| 1129 | sigaddset(&blocked_set, SIGPIPE); |
| 1130 | sigaddset(&blocked_set, SIGTERM); |
| 1131 | sa.sa_handler = interrupt; |
| 1132 | #ifdef SUNOS4 |
| 1133 | /* POSIX signals on sunos4.1 are a little broken. */ |
| 1134 | sa.sa_flags = SA_INTERRUPT; |
| 1135 | #endif /* SUNOS4 */ |
| 1136 | } |
| 1137 | sigaction(SIGHUP, &sa, NULL); |
| 1138 | sigaction(SIGINT, &sa, NULL); |
| 1139 | sigaction(SIGQUIT, &sa, NULL); |
| 1140 | sigaction(SIGPIPE, &sa, NULL); |
| 1141 | sigaction(SIGTERM, &sa, NULL); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1142 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1143 | sa.sa_handler = reaper; |
| 1144 | sigaction(SIGCHLD, &sa, NULL); |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1145 | #else |
| 1146 | /* Make sure SIGCHLD has the default action so that waitpid |
| 1147 | definitely works without losing track of children. The user |
| 1148 | should not have given us a bogus state to inherit, but he might |
| 1149 | have. Arguably we should detect SIG_IGN here and pass it on |
| 1150 | to children, but probably noone really needs that. */ |
| 1151 | sa.sa_handler = SIG_DFL; |
| 1152 | sigaction(SIGCHLD, &sa, NULL); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1153 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1154 | |
Denys Vlasenko | ecfe2f1 | 2008-12-30 20:51:30 +0000 | [diff] [blame] | 1155 | if (pflag_seen || daemonized_tracer) |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1156 | startup_attach(); |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1157 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1158 | if (trace() < 0) |
| 1159 | exit(1); |
| 1160 | cleanup(); |
Dmitry V. Levin | a680965 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 1161 | fflush(NULL); |
| 1162 | if (exit_code > 0xff) { |
| 1163 | /* Child was killed by a signal, mimic that. */ |
| 1164 | exit_code &= 0xff; |
| 1165 | signal(exit_code, SIG_DFL); |
| 1166 | raise(exit_code); |
| 1167 | /* Paranoia - what if this signal is not fatal? |
| 1168 | Exit with 128 + signo then. */ |
| 1169 | exit_code += 128; |
| 1170 | } |
| 1171 | exit(exit_code); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Denys Vlasenko | 2b60c35 | 2011-06-22 12:45:25 +0200 | [diff] [blame] | 1174 | static void |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 1175 | expand_tcbtab(void) |
Roland McGrath | 7b54a7a | 2004-06-04 01:50:45 +0000 | [diff] [blame] | 1176 | { |
| 1177 | /* Allocate some more TCBs and expand the table. |
| 1178 | We don't want to relocate the TCBs because our |
| 1179 | callers have pointers and it would be a pain. |
| 1180 | So tcbtab is a table of pointers. Since we never |
| 1181 | free the TCBs, we allocate a single chunk of many. */ |
Denys Vlasenko | 18da273 | 2011-06-22 12:41:57 +0200 | [diff] [blame] | 1182 | int i = tcbtabsize; |
| 1183 | struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0])); |
| 1184 | struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0])); |
| 1185 | if (newtab == NULL || newtcbs == NULL) |
| 1186 | error_msg_and_die("expand_tcbtab: out of memory"); |
Roland McGrath | 7b54a7a | 2004-06-04 01:50:45 +0000 | [diff] [blame] | 1187 | tcbtabsize *= 2; |
| 1188 | tcbtab = newtab; |
Denys Vlasenko | 18da273 | 2011-06-22 12:41:57 +0200 | [diff] [blame] | 1189 | while (i < tcbtabsize) |
| 1190 | tcbtab[i++] = newtcbs++; |
Roland McGrath | 7b54a7a | 2004-06-04 01:50:45 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1193 | struct tcb * |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 1194 | alloc_tcb(int pid, int command_options_parsed) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1195 | { |
| 1196 | int i; |
| 1197 | struct tcb *tcp; |
| 1198 | |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 1199 | if (nprocs == tcbtabsize) |
| 1200 | expand_tcbtab(); |
| 1201 | |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 1202 | for (i = 0; i < tcbtabsize; i++) { |
| 1203 | tcp = tcbtab[i]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1204 | if ((tcp->flags & TCB_INUSE) == 0) { |
Denys Vlasenko | 18da273 | 2011-06-22 12:41:57 +0200 | [diff] [blame] | 1205 | memset(tcp, 0, sizeof(*tcp)); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1206 | tcp->pid = pid; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1207 | tcp->flags = TCB_INUSE | TCB_STARTUP; |
| 1208 | tcp->outf = outf; /* Initialise to current out file */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1209 | tcp->pfd = -1; |
| 1210 | nprocs++; |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 1211 | if (debug) |
| 1212 | 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] | 1213 | if (command_options_parsed) |
| 1214 | newoutf(tcp); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1215 | return tcp; |
| 1216 | } |
| 1217 | } |
Denys Vlasenko | 18da273 | 2011-06-22 12:41:57 +0200 | [diff] [blame] | 1218 | error_msg_and_die("bug in alloc_tcb"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1221 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1222 | int |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 1223 | proc_open(struct tcb *tcp, int attaching) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1224 | { |
| 1225 | char proc[32]; |
| 1226 | long arg; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1227 | #ifdef SVR4 |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1228 | int i; |
| 1229 | sysset_t syscalls; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1230 | sigset_t signals; |
| 1231 | fltset_t faults; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1232 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1233 | #ifndef HAVE_POLLABLE_PROCFS |
| 1234 | static int last_pfd; |
| 1235 | #endif |
| 1236 | |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 1237 | #ifdef HAVE_MP_PROCFS |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1238 | /* Open the process pseudo-files in /proc. */ |
| 1239 | sprintf(proc, "/proc/%d/ctl", tcp->pid); |
| 1240 | if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1241 | perror("strace: open(\"/proc/...\", ...)"); |
| 1242 | return -1; |
| 1243 | } |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 1244 | set_cloexec_flag(tcp->pfd); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1245 | sprintf(proc, "/proc/%d/status", tcp->pid); |
| 1246 | if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) { |
| 1247 | perror("strace: open(\"/proc/...\", ...)"); |
| 1248 | return -1; |
| 1249 | } |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 1250 | set_cloexec_flag(tcp->pfd_stat); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1251 | sprintf(proc, "/proc/%d/as", tcp->pid); |
| 1252 | if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) { |
| 1253 | perror("strace: open(\"/proc/...\", ...)"); |
| 1254 | return -1; |
| 1255 | } |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 1256 | set_cloexec_flag(tcp->pfd_as); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1257 | #else |
| 1258 | /* Open the process pseudo-file in /proc. */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1259 | #ifndef FREEBSD |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1260 | sprintf(proc, "/proc/%d", tcp->pid); |
Andreas Schwab | 372cc84 | 2010-07-09 11:49:27 +0200 | [diff] [blame] | 1261 | tcp->pfd = open(proc, O_RDWR|O_EXCL); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1262 | #else /* FREEBSD */ |
| 1263 | sprintf(proc, "/proc/%d/mem", tcp->pid); |
Andreas Schwab | 372cc84 | 2010-07-09 11:49:27 +0200 | [diff] [blame] | 1264 | tcp->pfd = open(proc, O_RDWR); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1265 | #endif /* FREEBSD */ |
Andreas Schwab | 372cc84 | 2010-07-09 11:49:27 +0200 | [diff] [blame] | 1266 | if (tcp->pfd < 0) { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1267 | perror("strace: open(\"/proc/...\", ...)"); |
| 1268 | return -1; |
| 1269 | } |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 1270 | set_cloexec_flag(tcp->pfd); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1271 | #endif |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1272 | #ifdef FREEBSD |
| 1273 | sprintf(proc, "/proc/%d/regs", tcp->pid); |
| 1274 | if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) { |
| 1275 | perror("strace: open(\"/proc/.../regs\", ...)"); |
| 1276 | return -1; |
| 1277 | } |
| 1278 | if (cflag) { |
| 1279 | sprintf(proc, "/proc/%d/status", tcp->pid); |
| 1280 | if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) { |
| 1281 | perror("strace: open(\"/proc/.../status\", ...)"); |
| 1282 | return -1; |
| 1283 | } |
| 1284 | } else |
| 1285 | tcp->pfd_status = -1; |
| 1286 | #endif /* FREEBSD */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1287 | rebuild_pollv(); |
| 1288 | if (!attaching) { |
| 1289 | /* |
| 1290 | * Wait for the child to pause. Because of a race |
| 1291 | * condition we have to poll for the event. |
| 1292 | */ |
| 1293 | for (;;) { |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1294 | if (IOCTL_STATUS(tcp) < 0) { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1295 | perror("strace: PIOCSTATUS"); |
| 1296 | return -1; |
| 1297 | } |
| 1298 | if (tcp->status.PR_FLAGS & PR_ASLEEP) |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1299 | break; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1300 | } |
| 1301 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1302 | #ifndef FREEBSD |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1303 | /* Stop the process so that we own the stop. */ |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 1304 | if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1305 | perror("strace: PIOCSTOP"); |
| 1306 | return -1; |
| 1307 | } |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1308 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1309 | #ifdef PIOCSET |
| 1310 | /* Set Run-on-Last-Close. */ |
| 1311 | arg = PR_RLC; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1312 | if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1313 | perror("PIOCSET PR_RLC"); |
| 1314 | return -1; |
| 1315 | } |
| 1316 | /* Set or Reset Inherit-on-Fork. */ |
| 1317 | arg = PR_FORK; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1318 | if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1319 | perror("PIOC{SET,RESET} PR_FORK"); |
| 1320 | return -1; |
| 1321 | } |
| 1322 | #else /* !PIOCSET */ |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1323 | #ifndef FREEBSD |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1324 | if (ioctl(tcp->pfd, PIOCSRLC) < 0) { |
| 1325 | perror("PIOCSRLC"); |
| 1326 | return -1; |
| 1327 | } |
| 1328 | if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) { |
| 1329 | perror("PIOC{S,R}FORK"); |
| 1330 | return -1; |
| 1331 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1332 | #else /* FREEBSD */ |
| 1333 | /* just unset the PF_LINGER flag for the Run-on-Last-Close. */ |
| 1334 | if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) { |
| 1335 | perror("PIOCGFL"); |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1336 | return -1; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1337 | } |
| 1338 | arg &= ~PF_LINGER; |
| 1339 | if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) { |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1340 | perror("PIOCSFL"); |
| 1341 | return -1; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1342 | } |
| 1343 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1344 | #endif /* !PIOCSET */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1345 | #ifndef FREEBSD |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1346 | /* Enable all syscall entries we care about. */ |
| 1347 | premptyset(&syscalls); |
| 1348 | for (i = 1; i < MAX_QUALS; ++i) { |
| 1349 | if (i > (sizeof syscalls) * CHAR_BIT) break; |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1350 | if (qual_flags[i] & QUAL_TRACE) praddset(&syscalls, i); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1351 | } |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1352 | praddset(&syscalls, SYS_execve); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1353 | if (followfork) { |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1354 | praddset(&syscalls, SYS_fork); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1355 | #ifdef SYS_forkall |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1356 | praddset(&syscalls, SYS_forkall); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1357 | #endif |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1358 | #ifdef SYS_fork1 |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1359 | praddset(&syscalls, SYS_fork1); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1360 | #endif |
| 1361 | #ifdef SYS_rfork1 |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1362 | praddset(&syscalls, SYS_rfork1); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1363 | #endif |
| 1364 | #ifdef SYS_rforkall |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1365 | praddset(&syscalls, SYS_rforkall); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1366 | #endif |
| 1367 | } |
| 1368 | if (IOCTL(tcp->pfd, PIOCSENTRY, &syscalls) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1369 | perror("PIOCSENTRY"); |
| 1370 | return -1; |
| 1371 | } |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1372 | /* Enable the syscall exits. */ |
| 1373 | if (IOCTL(tcp->pfd, PIOCSEXIT, &syscalls) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1374 | perror("PIOSEXIT"); |
| 1375 | return -1; |
| 1376 | } |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1377 | /* Enable signals we care about. */ |
| 1378 | premptyset(&signals); |
| 1379 | for (i = 1; i < MAX_QUALS; ++i) { |
| 1380 | if (i > (sizeof signals) * CHAR_BIT) break; |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1381 | if (qual_flags[i] & QUAL_SIGNAL) praddset(&signals, i); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1382 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1383 | if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1384 | perror("PIOCSTRACE"); |
| 1385 | return -1; |
| 1386 | } |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1387 | /* Enable faults we care about */ |
| 1388 | premptyset(&faults); |
| 1389 | for (i = 1; i < MAX_QUALS; ++i) { |
| 1390 | if (i > (sizeof faults) * CHAR_BIT) break; |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1391 | if (qual_flags[i] & QUAL_FAULT) praddset(&faults, i); |
John Hughes | 19e4998 | 2001-10-19 08:59:12 +0000 | [diff] [blame] | 1392 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1393 | if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1394 | perror("PIOCSFAULT"); |
| 1395 | return -1; |
| 1396 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1397 | #else /* FREEBSD */ |
| 1398 | /* set events flags. */ |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1399 | arg = S_SIG | S_SCE | S_SCX; |
| 1400 | if (ioctl(tcp->pfd, PIOCBIS, arg) < 0) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1401 | perror("PIOCBIS"); |
| 1402 | return -1; |
| 1403 | } |
| 1404 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1405 | if (!attaching) { |
| 1406 | #ifdef MIPS |
| 1407 | /* |
| 1408 | * The SGI PRSABORT doesn't work for pause() so |
| 1409 | * we send it a caught signal to wake it up. |
| 1410 | */ |
| 1411 | kill(tcp->pid, SIGINT); |
| 1412 | #else /* !MIPS */ |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1413 | #ifdef PRSABORT |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1414 | /* The child is in a pause(), abort it. */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1415 | arg = PRSABORT; |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1416 | if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1417 | perror("PIOCRUN"); |
| 1418 | return -1; |
| 1419 | } |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1420 | #endif |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1421 | #endif /* !MIPS*/ |
| 1422 | #ifdef FREEBSD |
| 1423 | /* wake up the child if it received the SIGSTOP */ |
| 1424 | kill(tcp->pid, SIGCONT); |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1425 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1426 | for (;;) { |
| 1427 | /* Wait for the child to do something. */ |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1428 | if (IOCTL_WSTOP(tcp) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1429 | perror("PIOCWSTOP"); |
| 1430 | return -1; |
| 1431 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1432 | if (tcp->status.PR_WHY == PR_SYSENTRY) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1433 | tcp->flags &= ~TCB_INSYSCALL; |
| 1434 | get_scno(tcp); |
Roland McGrath | 76989d7 | 2005-06-07 23:21:31 +0000 | [diff] [blame] | 1435 | if (known_scno(tcp) == SYS_execve) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1436 | break; |
| 1437 | } |
| 1438 | /* Set it running: maybe execve will be next. */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1439 | #ifndef FREEBSD |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1440 | arg = 0; |
| 1441 | if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1442 | #else /* FREEBSD */ |
| 1443 | if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) { |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1444 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1445 | perror("PIOCRUN"); |
| 1446 | return -1; |
| 1447 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1448 | #ifdef FREEBSD |
| 1449 | /* handle the case where we "opened" the child before |
| 1450 | it did the kill -STOP */ |
| 1451 | if (tcp->status.PR_WHY == PR_SIGNALLED && |
| 1452 | tcp->status.PR_WHAT == SIGSTOP) |
| 1453 | kill(tcp->pid, SIGCONT); |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1454 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1455 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1456 | #ifndef FREEBSD |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1457 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1458 | #else /* FREEBSD */ |
| 1459 | } else { |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1460 | if (attaching < 2) { |
Wichert Akkerman | 2e4ffe5 | 2000-09-03 23:57:48 +0000 | [diff] [blame] | 1461 | /* We are attaching to an already running process. |
| 1462 | * Try to figure out the state of the process in syscalls, |
| 1463 | * to handle the first event well. |
| 1464 | * This is done by having a look at the "wchan" property of the |
| 1465 | * process, which tells where it is stopped (if it is). */ |
| 1466 | FILE * status; |
| 1467 | char wchan[20]; /* should be enough */ |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1468 | |
Wichert Akkerman | 2e4ffe5 | 2000-09-03 23:57:48 +0000 | [diff] [blame] | 1469 | sprintf(proc, "/proc/%d/status", tcp->pid); |
| 1470 | status = fopen(proc, "r"); |
| 1471 | if (status && |
| 1472 | (fscanf(status, "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d" |
| 1473 | "%*d,%*d %*d,%*d %19s", wchan) == 1) && |
| 1474 | strcmp(wchan, "nochan") && strcmp(wchan, "spread") && |
| 1475 | strcmp(wchan, "stopevent")) { |
| 1476 | /* The process is asleep in the middle of a syscall. |
| 1477 | Fake the syscall entry event */ |
| 1478 | tcp->flags &= ~(TCB_INSYSCALL|TCB_STARTUP); |
| 1479 | tcp->status.PR_WHY = PR_SYSENTRY; |
| 1480 | trace_syscall(tcp); |
| 1481 | } |
| 1482 | if (status) |
| 1483 | fclose(status); |
| 1484 | } /* otherwise it's a fork being followed */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1485 | } |
| 1486 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1487 | #ifndef HAVE_POLLABLE_PROCFS |
| 1488 | if (proc_poll_pipe[0] != -1) |
| 1489 | proc_poller(tcp->pfd); |
| 1490 | else if (nprocs > 1) { |
| 1491 | proc_poll_open(); |
| 1492 | proc_poller(last_pfd); |
| 1493 | proc_poller(tcp->pfd); |
| 1494 | } |
| 1495 | last_pfd = tcp->pfd; |
| 1496 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1500 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1501 | |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 1502 | struct tcb * |
Roland McGrath | 54e931f | 2010-09-14 18:59:20 -0700 | [diff] [blame] | 1503 | pid2tcb(int pid) |
| 1504 | { |
| 1505 | int i; |
| 1506 | |
| 1507 | if (pid <= 0) |
| 1508 | return NULL; |
| 1509 | |
| 1510 | for (i = 0; i < tcbtabsize; i++) { |
| 1511 | struct tcb *tcp = tcbtab[i]; |
| 1512 | if (tcp->pid == pid && (tcp->flags & TCB_INUSE)) |
| 1513 | return tcp; |
| 1514 | } |
| 1515 | |
| 1516 | return NULL; |
| 1517 | } |
| 1518 | |
| 1519 | #ifdef USE_PROCFS |
| 1520 | |
| 1521 | static struct tcb * |
| 1522 | first_used_tcb(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1523 | { |
| 1524 | int i; |
| 1525 | struct tcb *tcp; |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 1526 | for (i = 0; i < tcbtabsize; i++) { |
| 1527 | tcp = tcbtab[i]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1528 | if (tcp->flags & TCB_INUSE) |
| 1529 | return tcp; |
| 1530 | } |
| 1531 | return NULL; |
| 1532 | } |
| 1533 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1534 | static struct tcb * |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1535 | pfd2tcb(int pfd) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1536 | { |
| 1537 | int i; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1538 | |
Roland McGrath | ca16be8 | 2003-01-10 19:55:28 +0000 | [diff] [blame] | 1539 | for (i = 0; i < tcbtabsize; i++) { |
| 1540 | struct tcb *tcp = tcbtab[i]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1541 | if (tcp->pfd != pfd) |
| 1542 | continue; |
| 1543 | if (tcp->flags & TCB_INUSE) |
| 1544 | return tcp; |
| 1545 | } |
| 1546 | return NULL; |
| 1547 | } |
| 1548 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1549 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1550 | |
| 1551 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1552 | droptcb(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1553 | { |
| 1554 | if (tcp->pid == 0) |
| 1555 | return; |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 1556 | #ifdef TCB_CLONE_THREAD |
| 1557 | if (tcp->nclone_threads > 0) { |
| 1558 | /* There are other threads left in this process, but this |
| 1559 | is the one whose PID represents the whole process. |
| 1560 | We need to keep this record around as a zombie until |
| 1561 | all the threads die. */ |
| 1562 | tcp->flags |= TCB_EXITING; |
| 1563 | return; |
| 1564 | } |
| 1565 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1566 | nprocs--; |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 1567 | if (debug) |
| 1568 | fprintf(stderr, "dropped tcb for pid %d, %d remain\n", tcp->pid, nprocs); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1569 | tcp->pid = 0; |
Wichert Akkerman | eb8ebda | 2002-04-01 17:48:02 +0000 | [diff] [blame] | 1570 | |
Roland McGrath | e29341c | 2003-01-10 20:14:20 +0000 | [diff] [blame] | 1571 | if (tcp->parent != NULL) { |
Roland McGrath | e29341c | 2003-01-10 20:14:20 +0000 | [diff] [blame] | 1572 | #ifdef TCB_CLONE_THREAD |
Roland McGrath | e29341c | 2003-01-10 20:14:20 +0000 | [diff] [blame] | 1573 | if (tcp->flags & TCB_CLONE_THREAD) |
| 1574 | tcp->parent->nclone_threads--; |
| 1575 | #endif |
Roland McGrath | 276ceb3 | 2007-11-13 08:12:12 +0000 | [diff] [blame] | 1576 | #ifdef LINUX |
Denys Vlasenko | b56d6d3 | 2011-06-21 16:06:28 +0200 | [diff] [blame] | 1577 | /* Update fields like NCLONE_DETACHED, only |
| 1578 | for zombie group leader that has already reported |
| 1579 | and been short-circuited at the top of this |
Roland McGrath | 276ceb3 | 2007-11-13 08:12:12 +0000 | [diff] [blame] | 1580 | function. The same condition as at the top of DETACH. */ |
| 1581 | if ((tcp->flags & TCB_CLONE_THREAD) && |
| 1582 | tcp->parent->nclone_threads == 0 && |
| 1583 | (tcp->parent->flags & TCB_EXITING)) |
| 1584 | droptcb(tcp->parent); |
| 1585 | #endif |
Roland McGrath | e29341c | 2003-01-10 20:14:20 +0000 | [diff] [blame] | 1586 | tcp->parent = NULL; |
| 1587 | } |
| 1588 | |
| 1589 | tcp->flags = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1590 | if (tcp->pfd != -1) { |
| 1591 | close(tcp->pfd); |
| 1592 | tcp->pfd = -1; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1593 | #ifdef FREEBSD |
| 1594 | if (tcp->pfd_reg != -1) { |
| 1595 | close(tcp->pfd_reg); |
| 1596 | tcp->pfd_reg = -1; |
| 1597 | } |
| 1598 | if (tcp->pfd_status != -1) { |
| 1599 | close(tcp->pfd_status); |
| 1600 | tcp->pfd_status = -1; |
| 1601 | } |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1602 | #endif /* !FREEBSD */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1603 | #ifdef USE_PROCFS |
Roland McGrath | e29341c | 2003-01-10 20:14:20 +0000 | [diff] [blame] | 1604 | rebuild_pollv(); /* Note, flags needs to be cleared by now. */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1605 | #endif |
| 1606 | } |
Wichert Akkerman | eb8ebda | 2002-04-01 17:48:02 +0000 | [diff] [blame] | 1607 | |
Wichert Akkerman | 822f0c9 | 2002-04-03 10:55:14 +0000 | [diff] [blame] | 1608 | if (outfname && followfork > 1 && tcp->outf) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1609 | fclose(tcp->outf); |
Wichert Akkerman | eb8ebda | 2002-04-01 17:48:02 +0000 | [diff] [blame] | 1610 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1611 | tcp->outf = 0; |
| 1612 | } |
| 1613 | |
Roland McGrath | 0a46388 | 2007-07-05 18:43:16 +0000 | [diff] [blame] | 1614 | /* detach traced process; continue with sig |
| 1615 | Never call DETACH twice on the same process as both unattached and |
| 1616 | attached-unstopped processes give the same ESRCH. For unattached process we |
| 1617 | would SIGSTOP it and wait for its SIGSTOP notification forever. */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1618 | |
| 1619 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1620 | detach(struct tcb *tcp, int sig) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1621 | { |
| 1622 | int error = 0; |
Roland McGrath | ca16be8 | 2003-01-10 19:55:28 +0000 | [diff] [blame] | 1623 | #ifdef LINUX |
Roland McGrath | 1bfd310 | 2007-08-03 10:02:00 +0000 | [diff] [blame] | 1624 | int status, catch_sigstop; |
Roland McGrath | a08a97e | 2005-08-03 11:23:46 +0000 | [diff] [blame] | 1625 | struct tcb *zombie = NULL; |
| 1626 | |
| 1627 | /* If the group leader is lingering only because of this other |
| 1628 | thread now dying, then detach the leader as well. */ |
| 1629 | if ((tcp->flags & TCB_CLONE_THREAD) && |
| 1630 | tcp->parent->nclone_threads == 1 && |
| 1631 | (tcp->parent->flags & TCB_EXITING)) |
| 1632 | zombie = tcp->parent; |
Roland McGrath | ca16be8 | 2003-01-10 19:55:28 +0000 | [diff] [blame] | 1633 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1634 | |
| 1635 | if (tcp->flags & TCB_BPTSET) |
Andreas Schwab | 840d85b | 2010-01-12 11:16:32 +0100 | [diff] [blame] | 1636 | clearbpt(tcp); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1637 | |
| 1638 | #ifdef LINUX |
| 1639 | /* |
| 1640 | * Linux wrongly insists the child be stopped |
Roland McGrath | 7bf1047 | 2002-12-16 20:42:50 +0000 | [diff] [blame] | 1641 | * before detaching. Arghh. We go through hoops |
| 1642 | * to make a clean break of things. |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1643 | */ |
Roland McGrath | 7bf1047 | 2002-12-16 20:42:50 +0000 | [diff] [blame] | 1644 | #if defined(SPARC) |
| 1645 | #undef PTRACE_DETACH |
| 1646 | #define PTRACE_DETACH PTRACE_SUNDETACH |
| 1647 | #endif |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1648 | /* |
| 1649 | * On TCB_STARTUP we did PTRACE_ATTACH but still did not get the |
| 1650 | * expected SIGSTOP. We must catch exactly one as otherwise the |
| 1651 | * detached process would be left stopped (process state T). |
| 1652 | */ |
| 1653 | catch_sigstop = (tcp->flags & TCB_STARTUP); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1654 | if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) { |
| 1655 | /* On a clear day, you can see forever. */ |
Roland McGrath | 7bf1047 | 2002-12-16 20:42:50 +0000 | [diff] [blame] | 1656 | } |
| 1657 | else if (errno != ESRCH) { |
| 1658 | /* Shouldn't happen. */ |
| 1659 | perror("detach: ptrace(PTRACE_DETACH, ...)"); |
| 1660 | } |
Roland McGrath | 134813a | 2007-06-02 00:07:33 +0000 | [diff] [blame] | 1661 | else if (my_tgkill((tcp->flags & TCB_CLONE_THREAD ? tcp->parent->pid |
| 1662 | : tcp->pid), |
| 1663 | tcp->pid, 0) < 0) { |
Roland McGrath | 7bf1047 | 2002-12-16 20:42:50 +0000 | [diff] [blame] | 1664 | if (errno != ESRCH) |
| 1665 | perror("detach: checking sanity"); |
| 1666 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1667 | else if (!catch_sigstop && my_tgkill((tcp->flags & TCB_CLONE_THREAD |
| 1668 | ? tcp->parent->pid : tcp->pid), |
| 1669 | tcp->pid, SIGSTOP) < 0) { |
Roland McGrath | 7bf1047 | 2002-12-16 20:42:50 +0000 | [diff] [blame] | 1670 | if (errno != ESRCH) |
| 1671 | perror("detach: stopping child"); |
| 1672 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 1673 | else |
| 1674 | catch_sigstop = 1; |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 1675 | if (catch_sigstop) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1676 | for (;;) { |
Roland McGrath | 7508cb4 | 2002-12-17 10:48:05 +0000 | [diff] [blame] | 1677 | #ifdef __WALL |
| 1678 | if (wait4(tcp->pid, &status, __WALL, NULL) < 0) { |
| 1679 | if (errno == ECHILD) /* Already gone. */ |
| 1680 | break; |
| 1681 | if (errno != EINVAL) { |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1682 | perror("detach: waiting"); |
Roland McGrath | 7508cb4 | 2002-12-17 10:48:05 +0000 | [diff] [blame] | 1683 | break; |
| 1684 | } |
| 1685 | #endif /* __WALL */ |
| 1686 | /* No __WALL here. */ |
| 1687 | if (waitpid(tcp->pid, &status, 0) < 0) { |
| 1688 | if (errno != ECHILD) { |
| 1689 | perror("detach: waiting"); |
| 1690 | break; |
| 1691 | } |
| 1692 | #ifdef __WCLONE |
| 1693 | /* If no processes, try clones. */ |
| 1694 | if (wait4(tcp->pid, &status, __WCLONE, |
| 1695 | NULL) < 0) { |
| 1696 | if (errno != ECHILD) |
| 1697 | perror("detach: waiting"); |
| 1698 | break; |
| 1699 | } |
| 1700 | #endif /* __WCLONE */ |
| 1701 | } |
| 1702 | #ifdef __WALL |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 1703 | } |
Roland McGrath | 7508cb4 | 2002-12-17 10:48:05 +0000 | [diff] [blame] | 1704 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1705 | if (!WIFSTOPPED(status)) { |
| 1706 | /* Au revoir, mon ami. */ |
| 1707 | break; |
| 1708 | } |
| 1709 | if (WSTOPSIG(status) == SIGSTOP) { |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 1710 | ptrace_restart(PTRACE_DETACH, tcp, sig); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1711 | break; |
| 1712 | } |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 1713 | error = ptrace_restart(PTRACE_CONT, tcp, |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 1714 | WSTOPSIG(status) == syscall_trap_sig ? 0 |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 1715 | : WSTOPSIG(status)); |
| 1716 | if (error < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1717 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1718 | } |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 1719 | } |
Roland McGrath | 7bf1047 | 2002-12-16 20:42:50 +0000 | [diff] [blame] | 1720 | #endif /* LINUX */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1721 | |
| 1722 | #if defined(SUNOS4) |
| 1723 | /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */ |
| 1724 | if (sig && kill(tcp->pid, sig) < 0) |
| 1725 | perror("detach: kill"); |
| 1726 | sig = 0; |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 1727 | error = ptrace_restart(PTRACE_DETACH, tcp, sig); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1728 | #endif /* SUNOS4 */ |
| 1729 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1730 | if (!qflag) |
| 1731 | fprintf(stderr, "Process %u detached\n", tcp->pid); |
| 1732 | |
| 1733 | droptcb(tcp); |
Roland McGrath | a08a97e | 2005-08-03 11:23:46 +0000 | [diff] [blame] | 1734 | |
| 1735 | #ifdef LINUX |
Roland McGrath | 0a46388 | 2007-07-05 18:43:16 +0000 | [diff] [blame] | 1736 | if (zombie != NULL) { |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1737 | /* TCP no longer exists therefore you must not detach() it. */ |
Roland McGrath | 0a46388 | 2007-07-05 18:43:16 +0000 | [diff] [blame] | 1738 | droptcb(zombie); |
| 1739 | } |
Roland McGrath | a08a97e | 2005-08-03 11:23:46 +0000 | [diff] [blame] | 1740 | #endif |
| 1741 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1742 | return error; |
| 1743 | } |
| 1744 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1745 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1746 | |
Dmitry V. Levin | e5e6085 | 2009-12-31 22:50:49 +0000 | [diff] [blame] | 1747 | static void reaper(int sig) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1748 | { |
| 1749 | int pid; |
| 1750 | int status; |
| 1751 | |
| 1752 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1753 | } |
| 1754 | } |
| 1755 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1756 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1757 | |
| 1758 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1759 | cleanup(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1760 | { |
| 1761 | int i; |
| 1762 | struct tcb *tcp; |
| 1763 | |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 1764 | for (i = 0; i < tcbtabsize; i++) { |
| 1765 | tcp = tcbtab[i]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1766 | if (!(tcp->flags & TCB_INUSE)) |
| 1767 | continue; |
| 1768 | if (debug) |
| 1769 | fprintf(stderr, |
| 1770 | "cleanup: looking at pid %u\n", tcp->pid); |
| 1771 | if (tcp_last && |
| 1772 | (!outfname || followfork < 2 || tcp_last == tcp)) { |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 1773 | tprintf(" <unfinished ...>"); |
| 1774 | printtrailer(); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1775 | } |
| 1776 | if (tcp->flags & TCB_ATTACHED) |
| 1777 | detach(tcp, 0); |
| 1778 | else { |
| 1779 | kill(tcp->pid, SIGCONT); |
| 1780 | kill(tcp->pid, SIGTERM); |
| 1781 | } |
| 1782 | } |
| 1783 | if (cflag) |
| 1784 | call_summary(outf); |
| 1785 | } |
| 1786 | |
| 1787 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1788 | interrupt(int sig) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1789 | { |
| 1790 | interrupted = 1; |
| 1791 | } |
| 1792 | |
| 1793 | #ifndef HAVE_STRERROR |
| 1794 | |
Roland McGrath | 6d2b349 | 2002-12-30 00:51:30 +0000 | [diff] [blame] | 1795 | #if !HAVE_DECL_SYS_ERRLIST |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1796 | extern int sys_nerr; |
| 1797 | extern char *sys_errlist[]; |
Roland McGrath | 6d2b349 | 2002-12-30 00:51:30 +0000 | [diff] [blame] | 1798 | #endif /* HAVE_DECL_SYS_ERRLIST */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1799 | |
| 1800 | const char * |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1801 | strerror(int err_no) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1802 | { |
| 1803 | static char buf[64]; |
| 1804 | |
Denys Vlasenko | 35aba6a | 2011-05-25 15:33:26 +0200 | [diff] [blame] | 1805 | if (err_no < 1 || err_no >= sys_nerr) { |
| 1806 | sprintf(buf, "Unknown error %d", err_no); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1807 | return buf; |
| 1808 | } |
Denys Vlasenko | 35aba6a | 2011-05-25 15:33:26 +0200 | [diff] [blame] | 1809 | return sys_errlist[err_no]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | #endif /* HAVE_STERRROR */ |
| 1813 | |
| 1814 | #ifndef HAVE_STRSIGNAL |
| 1815 | |
Roland McGrath | 8f474e0 | 2003-01-14 07:53:33 +0000 | [diff] [blame] | 1816 | #if defined HAVE_SYS_SIGLIST && !defined HAVE_DECL_SYS_SIGLIST |
Roland McGrath | 6d2b349 | 2002-12-30 00:51:30 +0000 | [diff] [blame] | 1817 | extern char *sys_siglist[]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1818 | #endif |
Roland McGrath | 8f474e0 | 2003-01-14 07:53:33 +0000 | [diff] [blame] | 1819 | #if defined HAVE_SYS__SIGLIST && !defined HAVE_DECL__SYS_SIGLIST |
| 1820 | extern char *_sys_siglist[]; |
| 1821 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1822 | |
| 1823 | const char * |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1824 | strsignal(int sig) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1825 | { |
| 1826 | static char buf[64]; |
| 1827 | |
| 1828 | if (sig < 1 || sig >= NSIG) { |
| 1829 | sprintf(buf, "Unknown signal %d", sig); |
| 1830 | return buf; |
| 1831 | } |
| 1832 | #ifdef HAVE__SYS_SIGLIST |
| 1833 | return _sys_siglist[sig]; |
| 1834 | #else |
| 1835 | return sys_siglist[sig]; |
| 1836 | #endif |
| 1837 | } |
| 1838 | |
| 1839 | #endif /* HAVE_STRSIGNAL */ |
| 1840 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1841 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1842 | |
| 1843 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1844 | rebuild_pollv(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1845 | { |
| 1846 | int i, j; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1847 | |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 1848 | if (pollv != NULL) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1849 | free(pollv); |
Roland McGrath | c012d22 | 2003-01-10 20:05:56 +0000 | [diff] [blame] | 1850 | pollv = (struct pollfd *) malloc(nprocs * sizeof pollv[0]); |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 1851 | if (pollv == NULL) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1852 | error_msg_and_die("Out of memory"); |
Roland McGrath | ee9d435 | 2002-12-18 04:16:10 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
Roland McGrath | ca16be8 | 2003-01-10 19:55:28 +0000 | [diff] [blame] | 1855 | for (i = j = 0; i < tcbtabsize; i++) { |
| 1856 | struct tcb *tcp = tcbtab[i]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1857 | if (!(tcp->flags & TCB_INUSE)) |
| 1858 | continue; |
| 1859 | pollv[j].fd = tcp->pfd; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1860 | pollv[j].events = POLLWANT; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1861 | j++; |
| 1862 | } |
| 1863 | if (j != nprocs) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1864 | error_msg_and_die("proc miscount"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | #ifndef HAVE_POLLABLE_PROCFS |
| 1869 | |
| 1870 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1871 | proc_poll_open(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1872 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1873 | int i; |
| 1874 | |
| 1875 | if (pipe(proc_poll_pipe) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1876 | perror_msg_and_die("pipe"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1877 | } |
| 1878 | for (i = 0; i < 2; i++) { |
Denys Vlasenko | 1f532ab | 2011-06-22 13:11:23 +0200 | [diff] [blame] | 1879 | set_cloexec_flag(proc_poll_pipe[i]); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1884 | proc_poll(struct pollfd *pollv, int nfds, int timeout) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1885 | { |
| 1886 | int i; |
| 1887 | int n; |
| 1888 | struct proc_pollfd pollinfo; |
| 1889 | |
| 1890 | if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0) |
| 1891 | return n; |
| 1892 | if (n != sizeof(struct proc_pollfd)) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1893 | error_msg_and_die("panic: short read: %d", n); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1894 | } |
| 1895 | for (i = 0; i < nprocs; i++) { |
| 1896 | if (pollv[i].fd == pollinfo.fd) |
| 1897 | pollv[i].revents = pollinfo.revents; |
| 1898 | else |
| 1899 | pollv[i].revents = 0; |
| 1900 | } |
| 1901 | poller_pid = pollinfo.pid; |
| 1902 | return 1; |
| 1903 | } |
| 1904 | |
| 1905 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1906 | wakeup_handler(int sig) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1907 | { |
| 1908 | } |
| 1909 | |
| 1910 | static void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1911 | proc_poller(int pfd) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1912 | { |
| 1913 | struct proc_pollfd pollinfo; |
| 1914 | struct sigaction sa; |
| 1915 | sigset_t blocked_set, empty_set; |
| 1916 | int i; |
| 1917 | int n; |
| 1918 | struct rlimit rl; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1919 | #ifdef FREEBSD |
| 1920 | struct procfs_status pfs; |
| 1921 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1922 | |
| 1923 | switch (fork()) { |
| 1924 | case -1: |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1925 | perror_msg_and_die("fork"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1926 | case 0: |
| 1927 | break; |
| 1928 | default: |
| 1929 | return; |
| 1930 | } |
| 1931 | |
| 1932 | sa.sa_handler = interactive ? SIG_DFL : SIG_IGN; |
| 1933 | sa.sa_flags = 0; |
| 1934 | sigemptyset(&sa.sa_mask); |
| 1935 | sigaction(SIGHUP, &sa, NULL); |
| 1936 | sigaction(SIGINT, &sa, NULL); |
| 1937 | sigaction(SIGQUIT, &sa, NULL); |
| 1938 | sigaction(SIGPIPE, &sa, NULL); |
| 1939 | sigaction(SIGTERM, &sa, NULL); |
| 1940 | sa.sa_handler = wakeup_handler; |
| 1941 | sigaction(SIGUSR1, &sa, NULL); |
| 1942 | sigemptyset(&blocked_set); |
| 1943 | sigaddset(&blocked_set, SIGUSR1); |
| 1944 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
| 1945 | sigemptyset(&empty_set); |
| 1946 | |
| 1947 | if (getrlimit(RLIMIT_NOFILE, &rl) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 1948 | perror_msg_and_die("getrlimit(RLIMIT_NOFILE, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1949 | } |
| 1950 | n = rl.rlim_cur; |
| 1951 | for (i = 0; i < n; i++) { |
| 1952 | if (i != pfd && i != proc_poll_pipe[1]) |
| 1953 | close(i); |
| 1954 | } |
| 1955 | |
| 1956 | pollinfo.fd = pfd; |
| 1957 | pollinfo.pid = getpid(); |
| 1958 | for (;;) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1959 | #ifndef FREEBSD |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1960 | if (ioctl(pfd, PIOCWSTOP, NULL) < 0) |
| 1961 | #else |
| 1962 | if (ioctl(pfd, PIOCWSTOP, &pfs) < 0) |
| 1963 | #endif |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1964 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1965 | switch (errno) { |
| 1966 | case EINTR: |
| 1967 | continue; |
| 1968 | case EBADF: |
| 1969 | pollinfo.revents = POLLERR; |
| 1970 | break; |
| 1971 | case ENOENT: |
| 1972 | pollinfo.revents = POLLHUP; |
| 1973 | break; |
| 1974 | default: |
| 1975 | perror("proc_poller: PIOCWSTOP"); |
| 1976 | } |
| 1977 | write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo)); |
| 1978 | _exit(0); |
| 1979 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1980 | pollinfo.revents = POLLWANT; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1981 | write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo)); |
| 1982 | sigsuspend(&empty_set); |
| 1983 | } |
| 1984 | } |
| 1985 | |
| 1986 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 1987 | |
| 1988 | static int |
| 1989 | choose_pfd() |
| 1990 | { |
| 1991 | int i, j; |
| 1992 | struct tcb *tcp; |
| 1993 | |
| 1994 | static int last; |
| 1995 | |
| 1996 | if (followfork < 2 && |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1997 | last < nprocs && (pollv[last].revents & POLLWANT)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1998 | /* |
| 1999 | * The previous process is ready to run again. We'll |
| 2000 | * let it do so if it is currently in a syscall. This |
| 2001 | * heuristic improves the readability of the trace. |
| 2002 | */ |
| 2003 | tcp = pfd2tcb(pollv[last].fd); |
| 2004 | if (tcp && (tcp->flags & TCB_INSYSCALL)) |
| 2005 | return pollv[last].fd; |
| 2006 | } |
| 2007 | |
| 2008 | for (i = 0; i < nprocs; i++) { |
| 2009 | /* Let competing children run round robin. */ |
| 2010 | j = (i + last + 1) % nprocs; |
| 2011 | if (pollv[j].revents & (POLLHUP | POLLERR)) { |
| 2012 | tcp = pfd2tcb(pollv[j].fd); |
| 2013 | if (!tcp) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2014 | error_msg_and_die("lost proc"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2015 | } |
| 2016 | droptcb(tcp); |
| 2017 | return -1; |
| 2018 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2019 | if (pollv[j].revents & POLLWANT) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2020 | last = j; |
| 2021 | return pollv[j].fd; |
| 2022 | } |
| 2023 | } |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2024 | error_msg_and_die("nothing ready"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 2028 | trace(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2029 | { |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 2030 | #ifdef POLL_HACK |
John Hughes | d870b3c | 2002-05-21 11:24:18 +0000 | [diff] [blame] | 2031 | struct tcb *in_syscall = NULL; |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 2032 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2033 | struct tcb *tcp; |
| 2034 | int pfd; |
| 2035 | int what; |
| 2036 | int ioctl_result = 0, ioctl_errno = 0; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2037 | long arg; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2038 | |
| 2039 | for (;;) { |
| 2040 | if (interactive) |
| 2041 | sigprocmask(SIG_SETMASK, &empty_set, NULL); |
| 2042 | |
| 2043 | if (nprocs == 0) |
| 2044 | break; |
| 2045 | |
| 2046 | switch (nprocs) { |
| 2047 | case 1: |
| 2048 | #ifndef HAVE_POLLABLE_PROCFS |
| 2049 | if (proc_poll_pipe[0] == -1) { |
| 2050 | #endif |
Roland McGrath | 54e931f | 2010-09-14 18:59:20 -0700 | [diff] [blame] | 2051 | tcp = first_used_tcb(); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2052 | if (!tcp) |
| 2053 | continue; |
| 2054 | pfd = tcp->pfd; |
| 2055 | if (pfd == -1) |
| 2056 | continue; |
| 2057 | break; |
| 2058 | #ifndef HAVE_POLLABLE_PROCFS |
| 2059 | } |
| 2060 | /* fall through ... */ |
| 2061 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 2062 | default: |
| 2063 | #ifdef HAVE_POLLABLE_PROCFS |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 2064 | #ifdef POLL_HACK |
| 2065 | /* On some systems (e.g. UnixWare) we get too much ugly |
| 2066 | "unfinished..." stuff when multiple proceses are in |
| 2067 | syscalls. Here's a nasty hack */ |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2068 | |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 2069 | if (in_syscall) { |
| 2070 | struct pollfd pv; |
| 2071 | tcp = in_syscall; |
| 2072 | in_syscall = NULL; |
| 2073 | pv.fd = tcp->pfd; |
| 2074 | pv.events = POLLWANT; |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 2075 | if ((what = poll(&pv, 1, 1)) < 0) { |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 2076 | if (interrupted) |
| 2077 | return 0; |
| 2078 | continue; |
| 2079 | } |
| 2080 | else if (what == 1 && pv.revents & POLLWANT) { |
| 2081 | goto FOUND; |
| 2082 | } |
| 2083 | } |
| 2084 | #endif |
| 2085 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2086 | if (poll(pollv, nprocs, INFTIM) < 0) { |
| 2087 | if (interrupted) |
| 2088 | return 0; |
| 2089 | continue; |
| 2090 | } |
| 2091 | #else /* !HAVE_POLLABLE_PROCFS */ |
| 2092 | if (proc_poll(pollv, nprocs, INFTIM) < 0) { |
| 2093 | if (interrupted) |
| 2094 | return 0; |
| 2095 | continue; |
| 2096 | } |
| 2097 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 2098 | pfd = choose_pfd(); |
| 2099 | if (pfd == -1) |
| 2100 | continue; |
| 2101 | break; |
| 2102 | } |
| 2103 | |
| 2104 | /* Look up `pfd' in our table. */ |
| 2105 | if ((tcp = pfd2tcb(pfd)) == NULL) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2106 | error_msg_and_die("unknown pfd: %u", pfd); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2107 | } |
John Hughes | b664308 | 2002-05-23 11:02:22 +0000 | [diff] [blame] | 2108 | #ifdef POLL_HACK |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 2109 | FOUND: |
John Hughes | b664308 | 2002-05-23 11:02:22 +0000 | [diff] [blame] | 2110 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2111 | /* Get the status of the process. */ |
| 2112 | if (!interrupted) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2113 | #ifndef FREEBSD |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 2114 | ioctl_result = IOCTL_WSTOP(tcp); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2115 | #else /* FREEBSD */ |
| 2116 | /* Thanks to some scheduling mystery, the first poller |
| 2117 | sometimes waits for the already processed end of fork |
| 2118 | event. Doing a non blocking poll here solves the problem. */ |
| 2119 | if (proc_poll_pipe[0] != -1) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 2120 | ioctl_result = IOCTL_STATUS(tcp); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2121 | else |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 2122 | ioctl_result = IOCTL_WSTOP(tcp); |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2123 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2124 | ioctl_errno = errno; |
| 2125 | #ifndef HAVE_POLLABLE_PROCFS |
| 2126 | if (proc_poll_pipe[0] != -1) { |
| 2127 | if (ioctl_result < 0) |
| 2128 | kill(poller_pid, SIGKILL); |
| 2129 | else |
| 2130 | kill(poller_pid, SIGUSR1); |
| 2131 | } |
| 2132 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 2133 | } |
| 2134 | if (interrupted) |
| 2135 | return 0; |
| 2136 | |
| 2137 | if (interactive) |
| 2138 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
| 2139 | |
| 2140 | if (ioctl_result < 0) { |
| 2141 | /* Find out what happened if it failed. */ |
| 2142 | switch (ioctl_errno) { |
| 2143 | case EINTR: |
| 2144 | case EBADF: |
| 2145 | continue; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2146 | #ifdef FREEBSD |
| 2147 | case ENOTTY: |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2148 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2149 | case ENOENT: |
| 2150 | droptcb(tcp); |
| 2151 | continue; |
| 2152 | default: |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2153 | perror_msg_and_die("PIOCWSTOP"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2154 | } |
| 2155 | } |
| 2156 | |
Wichert Akkerman | 2e4ffe5 | 2000-09-03 23:57:48 +0000 | [diff] [blame] | 2157 | #ifdef FREEBSD |
| 2158 | if ((tcp->flags & TCB_STARTUP) && (tcp->status.PR_WHY == PR_SYSEXIT)) { |
| 2159 | /* discard first event for a syscall we never entered */ |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 2160 | IOCTL(tcp->pfd, PIOCRUN, 0); |
Wichert Akkerman | 2e4ffe5 | 2000-09-03 23:57:48 +0000 | [diff] [blame] | 2161 | continue; |
| 2162 | } |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2163 | #endif |
| 2164 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2165 | /* clear the just started flag */ |
| 2166 | tcp->flags &= ~TCB_STARTUP; |
| 2167 | |
| 2168 | /* set current output file */ |
| 2169 | outf = tcp->outf; |
Andreas Schwab | ccdff48 | 2009-10-27 16:27:13 +0100 | [diff] [blame] | 2170 | curcol = tcp->curcol; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2171 | |
| 2172 | if (cflag) { |
| 2173 | struct timeval stime; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2174 | #ifdef FREEBSD |
| 2175 | char buf[1024]; |
| 2176 | int len; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2177 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2178 | if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) { |
| 2179 | buf[len] = '\0'; |
| 2180 | sscanf(buf, |
| 2181 | "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld", |
| 2182 | &stime.tv_sec, &stime.tv_usec); |
| 2183 | } else |
| 2184 | stime.tv_sec = stime.tv_usec = 0; |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2185 | #else /* !FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2186 | stime.tv_sec = tcp->status.pr_stime.tv_sec; |
| 2187 | stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2188 | #endif /* !FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2189 | tv_sub(&tcp->dtime, &stime, &tcp->stime); |
| 2190 | tcp->stime = stime; |
| 2191 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2192 | what = tcp->status.PR_WHAT; |
| 2193 | switch (tcp->status.PR_WHY) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2194 | #ifndef FREEBSD |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2195 | case PR_REQUESTED: |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2196 | if (tcp->status.PR_FLAGS & PR_ASLEEP) { |
| 2197 | tcp->status.PR_WHY = PR_SYSENTRY; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2198 | if (trace_syscall(tcp) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2199 | error_msg_and_die("syscall trouble"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2200 | } |
| 2201 | } |
| 2202 | break; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2203 | #endif /* !FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2204 | case PR_SYSENTRY: |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 2205 | #ifdef POLL_HACK |
| 2206 | in_syscall = tcp; |
| 2207 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2208 | case PR_SYSEXIT: |
| 2209 | if (trace_syscall(tcp) < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2210 | error_msg_and_die("syscall trouble"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2211 | } |
| 2212 | break; |
| 2213 | case PR_SIGNALLED: |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 2214 | if (cflag != CFLAG_ONLY_STATS |
| 2215 | && (qual_flags[what] & QUAL_SIGNAL)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2216 | printleader(tcp); |
| 2217 | tprintf("--- %s (%s) ---", |
Nate Sammons | ce780fc | 1999-03-29 23:23:13 +0000 | [diff] [blame] | 2218 | signame(what), strsignal(what)); |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 2219 | printtrailer(); |
John Hughes | 5826589 | 2001-10-18 15:13:53 +0000 | [diff] [blame] | 2220 | #ifdef PR_INFO |
| 2221 | if (tcp->status.PR_INFO.si_signo == what) { |
| 2222 | printleader(tcp); |
| 2223 | tprintf(" siginfo="); |
| 2224 | printsiginfo(&tcp->status.PR_INFO, 1); |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 2225 | printtrailer(); |
John Hughes | 5826589 | 2001-10-18 15:13:53 +0000 | [diff] [blame] | 2226 | } |
| 2227 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2228 | } |
| 2229 | break; |
| 2230 | case PR_FAULTED: |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 2231 | if (cflag != CFLAGS_ONLY_STATS |
| 2232 | && (qual_flags[what] & QUAL_FAULT)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2233 | printleader(tcp); |
| 2234 | tprintf("=== FAULT %d ===", what); |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 2235 | printtrailer(); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2236 | } |
| 2237 | break; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2238 | #ifdef FREEBSD |
| 2239 | case 0: /* handle case we polled for nothing */ |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 2240 | continue; |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2241 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2242 | default: |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2243 | error_msg_and_die("odd stop %d", tcp->status.PR_WHY); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2244 | break; |
| 2245 | } |
Andreas Schwab | ccdff48 | 2009-10-27 16:27:13 +0100 | [diff] [blame] | 2246 | /* Remember current print column before continuing. */ |
| 2247 | tcp->curcol = curcol; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2248 | arg = 0; |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2249 | #ifndef FREEBSD |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 2250 | if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2251 | #else |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 2252 | if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2253 | #endif |
Andreas Schwab | 372cc84 | 2010-07-09 11:49:27 +0200 | [diff] [blame] | 2254 | { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2255 | perror_msg_and_die("PIOCRUN"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2256 | } |
| 2257 | } |
| 2258 | return 0; |
| 2259 | } |
| 2260 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2261 | #else /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2262 | |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2263 | #ifdef TCB_GROUP_EXITING |
| 2264 | /* Handle an exit detach or death signal that is taking all the |
| 2265 | related clone threads with it. This is called in three circumstances: |
| 2266 | SIG == -1 TCP has already died (TCB_ATTACHED is clear, strace is parent). |
| 2267 | SIG == 0 Continuing TCP will perform an exit_group syscall. |
| 2268 | SIG == other Continuing TCP with SIG will kill the process. |
| 2269 | */ |
| 2270 | static int |
| 2271 | handle_group_exit(struct tcb *tcp, int sig) |
| 2272 | { |
| 2273 | /* We need to locate our records of all the clone threads |
| 2274 | related to TCP, either its children or siblings. */ |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 2275 | struct tcb *leader = NULL; |
| 2276 | |
| 2277 | if (tcp->flags & TCB_CLONE_THREAD) |
| 2278 | leader = tcp->parent; |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2279 | |
| 2280 | if (sig < 0) { |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 2281 | if (leader != NULL && leader != tcp |
| 2282 | && !(leader->flags & TCB_GROUP_EXITING) |
| 2283 | && !(tcp->flags & TCB_STARTUP) |
| 2284 | ) { |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2285 | fprintf(stderr, |
| 2286 | "PANIC: handle_group_exit: %d leader %d\n", |
| 2287 | tcp->pid, leader ? leader->pid : -1); |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 2288 | } |
| 2289 | /* TCP no longer exists therefore you must not detach() it. */ |
Roland McGrath | 0a46388 | 2007-07-05 18:43:16 +0000 | [diff] [blame] | 2290 | droptcb(tcp); /* Already died. */ |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2291 | } |
| 2292 | else { |
Roland McGrath | a08a97e | 2005-08-03 11:23:46 +0000 | [diff] [blame] | 2293 | /* Mark that we are taking the process down. */ |
| 2294 | tcp->flags |= TCB_EXITING | TCB_GROUP_EXITING; |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2295 | if (tcp->flags & TCB_ATTACHED) { |
Roland McGrath | d6a32f1 | 2007-07-11 08:35:11 +0000 | [diff] [blame] | 2296 | detach(tcp, sig); |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 2297 | if (leader != NULL && leader != tcp) |
Roland McGrath | 1bfd310 | 2007-08-03 10:02:00 +0000 | [diff] [blame] | 2298 | leader->flags |= TCB_GROUP_EXITING; |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 2299 | } else { |
| 2300 | if (ptrace_restart(PTRACE_CONT, tcp, sig) < 0) { |
| 2301 | cleanup(); |
| 2302 | return -1; |
| 2303 | } |
| 2304 | if (leader != NULL) { |
Roland McGrath | 0569095 | 2004-10-20 01:00:27 +0000 | [diff] [blame] | 2305 | leader->flags |= TCB_GROUP_EXITING; |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 2306 | if (leader != tcp) |
| 2307 | droptcb(tcp); |
| 2308 | } |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2309 | /* The leader will report to us as parent now, |
| 2310 | and then we'll get to the SIG==-1 case. */ |
| 2311 | return 0; |
| 2312 | } |
| 2313 | } |
| 2314 | |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2315 | return 0; |
| 2316 | } |
| 2317 | #endif |
| 2318 | |
Wang Chao | ca8ab8d | 2010-11-12 17:26:08 +0800 | [diff] [blame] | 2319 | #ifdef LINUX |
| 2320 | static int |
| 2321 | handle_ptrace_event(int status, struct tcb *tcp) |
| 2322 | { |
| 2323 | if (status >> 16 == PTRACE_EVENT_VFORK || |
| 2324 | status >> 16 == PTRACE_EVENT_CLONE || |
| 2325 | status >> 16 == PTRACE_EVENT_FORK) { |
Dmitry V. Levin | 2fabd0e | 2011-02-19 21:33:50 +0000 | [diff] [blame] | 2326 | long childpid; |
Wang Chao | ca8ab8d | 2010-11-12 17:26:08 +0800 | [diff] [blame] | 2327 | |
| 2328 | if (do_ptrace(PTRACE_GETEVENTMSG, tcp, NULL, &childpid) < 0) { |
| 2329 | if (errno != ESRCH) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2330 | error_msg_and_die("Cannot get new child's pid"); |
Wang Chao | ca8ab8d | 2010-11-12 17:26:08 +0800 | [diff] [blame] | 2331 | } |
| 2332 | return -1; |
| 2333 | } |
| 2334 | return handle_new_child(tcp, childpid, 0); |
| 2335 | } |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 2336 | if (status >> 16 == PTRACE_EVENT_EXEC) { |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 2337 | return 0; |
| 2338 | } |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 2339 | /* Some PTRACE_EVENT_foo we didn't ask for?! */ |
| 2340 | error_msg("Unexpected status %x on pid %d", status, tcp->pid); |
Wang Chao | ca8ab8d | 2010-11-12 17:26:08 +0800 | [diff] [blame] | 2341 | return 1; |
| 2342 | } |
| 2343 | #endif |
| 2344 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2345 | static int |
| 2346 | trace() |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2347 | { |
| 2348 | int pid; |
| 2349 | int wait_errno; |
| 2350 | int status; |
| 2351 | struct tcb *tcp; |
| 2352 | #ifdef LINUX |
| 2353 | struct rusage ru; |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame^] | 2354 | struct rusage *rup = cflag ? &ru : NULL; |
| 2355 | # ifdef __WALL |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2356 | static int wait4_options = __WALL; |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame^] | 2357 | # endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2358 | #endif /* LINUX */ |
| 2359 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2360 | while (nprocs != 0) { |
Denys Vlasenko | 222713a | 2009-03-17 14:29:59 +0000 | [diff] [blame] | 2361 | if (interrupted) |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2362 | return 0; |
| 2363 | if (interactive) |
| 2364 | sigprocmask(SIG_SETMASK, &empty_set, NULL); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2365 | #ifdef LINUX |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame^] | 2366 | # ifdef __WALL |
| 2367 | pid = wait4(-1, &status, wait4_options, rup); |
Roland McGrath | 5bc0555 | 2002-12-17 04:50:47 +0000 | [diff] [blame] | 2368 | if (pid < 0 && (wait4_options & __WALL) && errno == EINVAL) { |
Wichert Akkerman | 2f1d87e | 2001-03-28 14:40:14 +0000 | [diff] [blame] | 2369 | /* this kernel does not support __WALL */ |
| 2370 | wait4_options &= ~__WALL; |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame^] | 2371 | pid = wait4(-1, &status, wait4_options, rup); |
Wichert Akkerman | 2f1d87e | 2001-03-28 14:40:14 +0000 | [diff] [blame] | 2372 | } |
Roland McGrath | 5bc0555 | 2002-12-17 04:50:47 +0000 | [diff] [blame] | 2373 | if (pid < 0 && !(wait4_options & __WALL) && errno == ECHILD) { |
Wichert Akkerman | 2f1d87e | 2001-03-28 14:40:14 +0000 | [diff] [blame] | 2374 | /* most likely a "cloned" process */ |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame^] | 2375 | pid = wait4(-1, &status, __WCLONE, rup); |
| 2376 | if (pid < 0) { |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2377 | perror_msg("wait4(__WCLONE) failed"); |
Wichert Akkerman | 2f1d87e | 2001-03-28 14:40:14 +0000 | [diff] [blame] | 2378 | } |
| 2379 | } |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame^] | 2380 | # else |
| 2381 | pid = wait4(-1, &status, 0, rup); |
| 2382 | # endif /* __WALL */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2383 | #endif /* LINUX */ |
| 2384 | #ifdef SUNOS4 |
| 2385 | pid = wait(&status); |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame^] | 2386 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2387 | wait_errno = errno; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2388 | if (interactive) |
| 2389 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2390 | |
Denys Vlasenko | 26d1b1e | 2011-08-15 12:24:14 +0200 | [diff] [blame^] | 2391 | if (pid < 0) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2392 | switch (wait_errno) { |
| 2393 | case EINTR: |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2394 | continue; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2395 | case ECHILD: |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2396 | /* |
| 2397 | * We would like to verify this case |
| 2398 | * but sometimes a race in Solbourne's |
| 2399 | * version of SunOS sometimes reports |
| 2400 | * ECHILD before sending us SIGCHILD. |
| 2401 | */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2402 | return 0; |
| 2403 | default: |
| 2404 | errno = wait_errno; |
| 2405 | perror("strace: wait"); |
| 2406 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2407 | } |
| 2408 | } |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 2409 | if (pid == popen_pid) { |
| 2410 | if (WIFEXITED(status) || WIFSIGNALED(status)) |
Denys Vlasenko | 7dd2338 | 2011-06-22 13:03:56 +0200 | [diff] [blame] | 2411 | popen_pid = 0; |
Dmitry V. Levin | 10de62b | 2006-12-13 21:45:31 +0000 | [diff] [blame] | 2412 | continue; |
| 2413 | } |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 2414 | if (debug) { |
| 2415 | char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16]; |
| 2416 | #ifdef LINUX |
| 2417 | unsigned ev = (unsigned)status >> 16; |
| 2418 | if (ev) { |
| 2419 | static const char *const event_names[] = { |
| 2420 | [PTRACE_EVENT_CLONE] = "CLONE", |
| 2421 | [PTRACE_EVENT_FORK] = "FORK", |
| 2422 | [PTRACE_EVENT_VFORK] = "VFORK", |
| 2423 | [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE", |
| 2424 | [PTRACE_EVENT_EXEC] = "EXEC", |
| 2425 | [PTRACE_EVENT_EXIT] = "EXIT", |
| 2426 | }; |
| 2427 | const char *e; |
| 2428 | if (ev < ARRAY_SIZE(event_names)) |
| 2429 | e = event_names[ev]; |
| 2430 | else { |
| 2431 | sprintf(buf, "?? (%u)", ev); |
| 2432 | e = buf; |
| 2433 | } |
| 2434 | fprintf(stderr, " PTRACE_EVENT_%s", e); |
| 2435 | } |
| 2436 | #endif |
| 2437 | strcpy(buf, "???"); |
| 2438 | if (WIFSIGNALED(status)) |
| 2439 | #ifdef WCOREDUMP |
| 2440 | sprintf(buf, "WIFSIGNALED,%ssig=%s", |
| 2441 | WCOREDUMP(status) ? "core," : "", |
| 2442 | signame(WTERMSIG(status))); |
| 2443 | #else |
| 2444 | sprintf(buf, "WIFSIGNALED,sig=%s", |
| 2445 | signame(WTERMSIG(status))); |
| 2446 | #endif |
| 2447 | if (WIFEXITED(status)) |
| 2448 | sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status)); |
| 2449 | if (WIFSTOPPED(status)) |
| 2450 | sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status))); |
Denys Vlasenko | 5bd67c8 | 2011-08-15 11:36:09 +0200 | [diff] [blame] | 2451 | #ifdef WIFCONTINUED |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 2452 | if (WIFCONTINUED(status)) |
| 2453 | strcpy(buf, "WIFCONTINUED"); |
Denys Vlasenko | 5bd67c8 | 2011-08-15 11:36:09 +0200 | [diff] [blame] | 2454 | #endif |
Denys Vlasenko | 1d5f12e | 2011-06-24 16:41:35 +0200 | [diff] [blame] | 2455 | fprintf(stderr, " [wait(0x%04x) = %u] %s\n", status, pid, buf); |
| 2456 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2457 | |
| 2458 | /* Look up `pid' in our table. */ |
| 2459 | if ((tcp = pid2tcb(pid)) == NULL) { |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2460 | #ifdef LINUX |
Roland McGrath | 41c4822 | 2008-07-18 00:25:10 +0000 | [diff] [blame] | 2461 | if (followfork) { |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2462 | /* This is needed to go with the CLONE_PTRACE |
| 2463 | changes in process.c/util.c: we might see |
| 2464 | the child's initial trap before we see the |
| 2465 | parent return from the clone syscall. |
| 2466 | Leave the child suspended until the parent |
| 2467 | returns from its system call. Only then |
| 2468 | will we have the association of parent and |
| 2469 | child so that we know how to do clearbpt |
| 2470 | in the child. */ |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 2471 | tcp = alloctcb(pid); |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2472 | tcp->flags |= TCB_ATTACHED | TCB_SUSPENDED; |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2473 | if (!qflag) |
| 2474 | fprintf(stderr, "\ |
| 2475 | Process %d attached (waiting for parent)\n", |
| 2476 | pid); |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 2477 | } |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2478 | else |
| 2479 | /* This can happen if a clone call used |
| 2480 | CLONE_PTRACE itself. */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2481 | #endif |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2482 | { |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2483 | if (WIFSTOPPED(status)) |
| 2484 | ptrace(PTRACE_CONT, pid, (char *) 1, 0); |
Denys Vlasenko | cb2ad00 | 2011-06-23 13:05:29 +0200 | [diff] [blame] | 2485 | error_msg_and_die("Unknown pid: %u", pid); |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2486 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2487 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2488 | /* set current output file */ |
| 2489 | outf = tcp->outf; |
Andreas Schwab | ccdff48 | 2009-10-27 16:27:13 +0100 | [diff] [blame] | 2490 | curcol = tcp->curcol; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2491 | #ifdef LINUX |
Denys Vlasenko | 13d22f1 | 2011-06-24 23:01:57 +0200 | [diff] [blame] | 2492 | if (cflag) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2493 | tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime); |
| 2494 | tcp->stime = ru.ru_stime; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2495 | } |
Denys Vlasenko | 13d22f1 | 2011-06-24 23:01:57 +0200 | [diff] [blame] | 2496 | #endif |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2497 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2498 | if (tcp->flags & TCB_SUSPENDED) { |
| 2499 | /* |
| 2500 | * Apparently, doing any ptrace() call on a stopped |
| 2501 | * process, provokes the kernel to report the process |
| 2502 | * status again on a subsequent wait(), even if the |
| 2503 | * process has not been actually restarted. |
| 2504 | * Since we have inspected the arguments of suspended |
| 2505 | * processes we end up here testing for this case. |
| 2506 | */ |
| 2507 | continue; |
| 2508 | } |
| 2509 | if (WIFSIGNALED(status)) { |
Dmitry V. Levin | a680965 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 2510 | if (pid == strace_child) |
| 2511 | exit_code = 0x100 | WTERMSIG(status); |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 2512 | if (cflag != CFLAG_ONLY_STATS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2513 | && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) { |
| 2514 | printleader(tcp); |
Denys Vlasenko | 13d22f1 | 2011-06-24 23:01:57 +0200 | [diff] [blame] | 2515 | #ifdef WCOREDUMP |
Roland McGrath | 2efe879 | 2004-01-13 09:59:45 +0000 | [diff] [blame] | 2516 | tprintf("+++ killed by %s %s+++", |
| 2517 | signame(WTERMSIG(status)), |
Denys Vlasenko | 13d22f1 | 2011-06-24 23:01:57 +0200 | [diff] [blame] | 2518 | WCOREDUMP(status) ? "(core dumped) " : ""); |
| 2519 | #else |
| 2520 | tprintf("+++ killed by %s +++", |
| 2521 | signame(WTERMSIG(status))); |
Roland McGrath | 2efe879 | 2004-01-13 09:59:45 +0000 | [diff] [blame] | 2522 | #endif |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 2523 | printtrailer(); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2524 | } |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2525 | #ifdef TCB_GROUP_EXITING |
| 2526 | handle_group_exit(tcp, -1); |
| 2527 | #else |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2528 | droptcb(tcp); |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2529 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2530 | continue; |
| 2531 | } |
| 2532 | if (WIFEXITED(status)) { |
Dmitry V. Levin | a680965 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 2533 | if (pid == strace_child) |
| 2534 | exit_code = WEXITSTATUS(status); |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 2535 | if ((tcp->flags & (TCB_ATTACHED|TCB_STARTUP)) == TCB_ATTACHED |
Roland McGrath | 0569095 | 2004-10-20 01:00:27 +0000 | [diff] [blame] | 2536 | #ifdef TCB_GROUP_EXITING |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 2537 | && !(tcp->parent && (tcp->parent->flags & TCB_GROUP_EXITING)) |
Roland McGrath | 1bfd310 | 2007-08-03 10:02:00 +0000 | [diff] [blame] | 2538 | && !(tcp->flags & TCB_GROUP_EXITING) |
Roland McGrath | 0569095 | 2004-10-20 01:00:27 +0000 | [diff] [blame] | 2539 | #endif |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 2540 | ) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2541 | fprintf(stderr, |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 2542 | "PANIC: attached pid %u exited with %d\n", |
| 2543 | pid, WEXITSTATUS(status)); |
| 2544 | } |
Roland McGrath | 0a39690 | 2003-06-10 03:05:53 +0000 | [diff] [blame] | 2545 | if (tcp == tcp_last) { |
Denys Vlasenko | 7a8bf06 | 2009-01-29 20:38:20 +0000 | [diff] [blame] | 2546 | if ((tcp->flags & (TCB_INSYSCALL|TCB_REPRINT)) == TCB_INSYSCALL) |
Roland McGrath | 0a39690 | 2003-06-10 03:05:53 +0000 | [diff] [blame] | 2547 | tprintf(" <unfinished ... exit status %d>\n", |
| 2548 | WEXITSTATUS(status)); |
| 2549 | tcp_last = NULL; |
| 2550 | } |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2551 | #ifdef TCB_GROUP_EXITING |
| 2552 | handle_group_exit(tcp, -1); |
| 2553 | #else |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2554 | droptcb(tcp); |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2555 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2556 | continue; |
| 2557 | } |
| 2558 | if (!WIFSTOPPED(status)) { |
| 2559 | fprintf(stderr, "PANIC: pid %u not stopped\n", pid); |
| 2560 | droptcb(tcp); |
| 2561 | continue; |
| 2562 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2563 | |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 2564 | if (status >> 16) { |
Wang Chao | ca8ab8d | 2010-11-12 17:26:08 +0800 | [diff] [blame] | 2565 | if (handle_ptrace_event(status, tcp) != 1) |
| 2566 | goto tracing; |
| 2567 | } |
| 2568 | |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 2569 | /* |
| 2570 | * Interestingly, the process may stop |
| 2571 | * with STOPSIG equal to some other signal |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2572 | * than SIGSTOP if we happend to attach |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 2573 | * just before the process takes a signal. |
Mike Frysinger | c1a5b7e | 2009-10-07 20:41:29 -0400 | [diff] [blame] | 2574 | * A no-MMU vforked child won't send up a signal, |
| 2575 | * so skip the first (lost) execve notification. |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 2576 | */ |
Mike Frysinger | c1a5b7e | 2009-10-07 20:41:29 -0400 | [diff] [blame] | 2577 | if ((tcp->flags & TCB_STARTUP) && |
| 2578 | (WSTOPSIG(status) == SIGSTOP || strace_vforked)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2579 | /* |
| 2580 | * This flag is there to keep us in sync. |
| 2581 | * Next time this process stops it should |
| 2582 | * really be entering a system call. |
| 2583 | */ |
| 2584 | tcp->flags &= ~TCB_STARTUP; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2585 | if (tcp->flags & TCB_BPTSET) { |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 2586 | /* |
| 2587 | * One example is a breakpoint inherited from |
| 2588 | * parent through fork (). |
| 2589 | */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2590 | if (clearbpt(tcp) < 0) /* Pretty fatal */ { |
| 2591 | droptcb(tcp); |
| 2592 | cleanup(); |
| 2593 | return -1; |
| 2594 | } |
| 2595 | } |
Wang Chao | ca8ab8d | 2010-11-12 17:26:08 +0800 | [diff] [blame] | 2596 | #ifdef LINUX |
Denys Vlasenko | f44cce4 | 2011-06-21 14:34:10 +0200 | [diff] [blame] | 2597 | /* If options were not set for this tracee yet */ |
| 2598 | if (tcp->parent == NULL) { |
| 2599 | if (ptrace_setoptions) { |
| 2600 | if (debug) |
| 2601 | fprintf(stderr, "setting opts %x on pid %d\n", ptrace_setoptions, tcp->pid); |
| 2602 | if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) { |
| 2603 | if (errno != ESRCH) { |
| 2604 | /* Should never happen, really */ |
| 2605 | perror_msg_and_die("PTRACE_SETOPTIONS"); |
| 2606 | } |
Denys Vlasenko | 3454e4b | 2011-05-23 21:29:03 +0200 | [diff] [blame] | 2607 | } |
| 2608 | } |
| 2609 | } |
Wang Chao | ca8ab8d | 2010-11-12 17:26:08 +0800 | [diff] [blame] | 2610 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2611 | goto tracing; |
| 2612 | } |
| 2613 | |
Denys Vlasenko | 7542276 | 2011-05-27 14:36:01 +0200 | [diff] [blame] | 2614 | if (WSTOPSIG(status) != syscall_trap_sig) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2615 | if (WSTOPSIG(status) == SIGSTOP && |
| 2616 | (tcp->flags & TCB_SIGTRAPPED)) { |
| 2617 | /* |
| 2618 | * Trapped attempt to block SIGTRAP |
| 2619 | * Hope we are back in control now. |
| 2620 | */ |
| 2621 | tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED); |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 2622 | if (ptrace_restart(PTRACE_SYSCALL, tcp, 0) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2623 | cleanup(); |
| 2624 | return -1; |
| 2625 | } |
| 2626 | continue; |
| 2627 | } |
Dmitry V. Levin | e3a7ef5 | 2010-03-28 19:24:54 +0000 | [diff] [blame] | 2628 | if (cflag != CFLAG_ONLY_STATS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2629 | && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) { |
Dmitry V. Levin | c15dfc7 | 2011-03-10 14:44:45 +0000 | [diff] [blame] | 2630 | siginfo_t si; |
| 2631 | #if defined(PT_CR_IPSR) && defined(PT_CR_IIP) |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2632 | long pc = 0; |
| 2633 | long psr = 0; |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 2634 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2635 | upeek(tcp, PT_CR_IPSR, &psr); |
| 2636 | upeek(tcp, PT_CR_IIP, &pc); |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 2637 | |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2638 | # define PSR_RI 41 |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 2639 | pc += (psr >> PSR_RI) & 0x3; |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2640 | # define PC_FORMAT_STR " @ %lx" |
| 2641 | # define PC_FORMAT_ARG pc |
| 2642 | #else |
| 2643 | # define PC_FORMAT_STR "%s" |
| 2644 | # define PC_FORMAT_ARG "" |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 2645 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2646 | printleader(tcp); |
Dmitry V. Levin | 6b7a261 | 2011-03-10 21:20:35 +0000 | [diff] [blame] | 2647 | if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si) == 0) { |
| 2648 | tprintf("--- "); |
| 2649 | printsiginfo(&si, verbose(tcp)); |
| 2650 | tprintf(" (%s)" PC_FORMAT_STR " ---", |
| 2651 | strsignal(WSTOPSIG(status)), |
| 2652 | PC_FORMAT_ARG); |
| 2653 | } else |
| 2654 | tprintf("--- %s by %s" PC_FORMAT_STR " ---", |
| 2655 | strsignal(WSTOPSIG(status)), |
| 2656 | signame(WSTOPSIG(status)), |
| 2657 | PC_FORMAT_ARG); |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 2658 | printtrailer(); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2659 | } |
Roland McGrath | 0569095 | 2004-10-20 01:00:27 +0000 | [diff] [blame] | 2660 | if (((tcp->flags & TCB_ATTACHED) || |
| 2661 | tcp->nclone_threads > 0) && |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2662 | !sigishandled(tcp, WSTOPSIG(status))) { |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2663 | #ifdef TCB_GROUP_EXITING |
| 2664 | handle_group_exit(tcp, WSTOPSIG(status)); |
| 2665 | #else |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2666 | detach(tcp, WSTOPSIG(status)); |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2667 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2668 | continue; |
| 2669 | } |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 2670 | if (ptrace_restart(PTRACE_SYSCALL, tcp, WSTOPSIG(status)) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2671 | cleanup(); |
| 2672 | return -1; |
| 2673 | } |
| 2674 | tcp->flags &= ~TCB_SUSPENDED; |
| 2675 | continue; |
| 2676 | } |
Roland McGrath | 0220331 | 2007-06-11 22:06:31 +0000 | [diff] [blame] | 2677 | /* we handled the STATUS, we are permitted to interrupt now. */ |
| 2678 | if (interrupted) |
| 2679 | return 0; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2680 | if (trace_syscall(tcp) < 0 && !tcp->ptrace_errno) { |
| 2681 | /* ptrace() failed in trace_syscall() with ESRCH. |
| 2682 | * Likely a result of process disappearing mid-flight. |
| 2683 | * Observed case: exit_group() terminating |
| 2684 | * all processes in thread group. In this case, threads |
| 2685 | * "disappear" in an unpredictable moment without any |
| 2686 | * notification to strace via wait(). |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 2687 | */ |
| 2688 | if (tcp->flags & TCB_ATTACHED) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2689 | if (tcp_last) { |
| 2690 | /* Do we have dangling line "syscall(param, param"? |
Denys Vlasenko | 178de00 | 2011-06-24 22:54:25 +0200 | [diff] [blame] | 2691 | * Finish the line then. |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2692 | */ |
| 2693 | tcp_last->flags |= TCB_REPRINT; |
| 2694 | tprintf(" <unfinished ...>"); |
| 2695 | printtrailer(); |
| 2696 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2697 | detach(tcp, 0); |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 2698 | } else { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2699 | ptrace(PTRACE_KILL, |
| 2700 | tcp->pid, (char *) 1, SIGTERM); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2701 | droptcb(tcp); |
| 2702 | } |
| 2703 | continue; |
| 2704 | } |
| 2705 | if (tcp->flags & TCB_EXITING) { |
Roland McGrath | e85bbfe | 2003-01-09 06:53:31 +0000 | [diff] [blame] | 2706 | #ifdef TCB_GROUP_EXITING |
| 2707 | if (tcp->flags & TCB_GROUP_EXITING) { |
| 2708 | if (handle_group_exit(tcp, 0) < 0) |
| 2709 | return -1; |
| 2710 | continue; |
| 2711 | } |
| 2712 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2713 | if (tcp->flags & TCB_ATTACHED) |
| 2714 | detach(tcp, 0); |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 2715 | else if (ptrace_restart(PTRACE_CONT, tcp, 0) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2716 | cleanup(); |
| 2717 | return -1; |
| 2718 | } |
| 2719 | continue; |
| 2720 | } |
| 2721 | if (tcp->flags & TCB_SUSPENDED) { |
| 2722 | if (!qflag) |
| 2723 | fprintf(stderr, "Process %u suspended\n", pid); |
| 2724 | continue; |
| 2725 | } |
| 2726 | tracing: |
Andreas Schwab | ccdff48 | 2009-10-27 16:27:13 +0100 | [diff] [blame] | 2727 | /* Remember current print column before continuing. */ |
| 2728 | tcp->curcol = curcol; |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 2729 | if (ptrace_restart(PTRACE_SYSCALL, tcp, 0) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2730 | cleanup(); |
| 2731 | return -1; |
| 2732 | } |
| 2733 | } |
| 2734 | return 0; |
| 2735 | } |
| 2736 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2737 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2738 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2739 | void |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2740 | tprintf(const char *fmt, ...) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2741 | { |
| 2742 | va_list args; |
| 2743 | |
Andreas Schwab | e5355de | 2009-10-27 16:56:43 +0100 | [diff] [blame] | 2744 | va_start(args, fmt); |
Roland McGrath | b310a0c | 2003-11-06 23:41:22 +0000 | [diff] [blame] | 2745 | if (outf) { |
| 2746 | int n = vfprintf(outf, fmt, args); |
Andreas Schwab | ccdff48 | 2009-10-27 16:27:13 +0100 | [diff] [blame] | 2747 | if (n < 0) { |
| 2748 | if (outf != stderr) |
| 2749 | perror(outfname == NULL |
| 2750 | ? "<writing to pipe>" : outfname); |
| 2751 | } else |
Roland McGrath | b310a0c | 2003-11-06 23:41:22 +0000 | [diff] [blame] | 2752 | curcol += n; |
| 2753 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2754 | va_end(args); |
| 2755 | return; |
| 2756 | } |
| 2757 | |
| 2758 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 2759 | printleader(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2760 | { |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 2761 | if (tcp_last) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2762 | if (tcp_last->ptrace_errno) { |
Denys Vlasenko | 7e0615f | 2009-01-28 19:00:54 +0000 | [diff] [blame] | 2763 | if (tcp_last->flags & TCB_INSYSCALL) { |
Denys Vlasenko | e62df00 | 2011-06-08 16:15:04 +0200 | [diff] [blame] | 2764 | tprintf(" <unavailable>) "); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2765 | tabto(acolumn); |
Denys Vlasenko | 7e0615f | 2009-01-28 19:00:54 +0000 | [diff] [blame] | 2766 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2767 | tprintf("= ? <unavailable>\n"); |
| 2768 | tcp_last->ptrace_errno = 0; |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 2769 | } else if (!outfname || followfork < 2 || tcp_last == tcp) { |
Denys Vlasenko | 7e0615f | 2009-01-28 19:00:54 +0000 | [diff] [blame] | 2770 | tcp_last->flags |= TCB_REPRINT; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2771 | tprintf(" <unfinished ...>\n"); |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 2772 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2773 | } |
| 2774 | curcol = 0; |
| 2775 | if ((followfork == 1 || pflag_seen > 1) && outfname) |
| 2776 | tprintf("%-5d ", tcp->pid); |
| 2777 | else if (nprocs > 1 && !outfname) |
| 2778 | tprintf("[pid %5u] ", tcp->pid); |
| 2779 | if (tflag) { |
| 2780 | char str[sizeof("HH:MM:SS")]; |
| 2781 | struct timeval tv, dtv; |
| 2782 | static struct timeval otv; |
| 2783 | |
| 2784 | gettimeofday(&tv, NULL); |
| 2785 | if (rflag) { |
| 2786 | if (otv.tv_sec == 0) |
| 2787 | otv = tv; |
| 2788 | tv_sub(&dtv, &tv, &otv); |
| 2789 | tprintf("%6ld.%06ld ", |
| 2790 | (long) dtv.tv_sec, (long) dtv.tv_usec); |
| 2791 | otv = tv; |
| 2792 | } |
| 2793 | else if (tflag > 2) { |
| 2794 | tprintf("%ld.%06ld ", |
| 2795 | (long) tv.tv_sec, (long) tv.tv_usec); |
| 2796 | } |
| 2797 | else { |
| 2798 | time_t local = tv.tv_sec; |
| 2799 | strftime(str, sizeof(str), "%T", localtime(&local)); |
| 2800 | if (tflag > 1) |
| 2801 | tprintf("%s.%06ld ", str, (long) tv.tv_usec); |
| 2802 | else |
| 2803 | tprintf("%s ", str); |
| 2804 | } |
| 2805 | } |
| 2806 | if (iflag) |
| 2807 | printcall(tcp); |
| 2808 | } |
| 2809 | |
| 2810 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 2811 | tabto(int col) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2812 | { |
| 2813 | if (curcol < col) |
| 2814 | tprintf("%*s", col - curcol, ""); |
| 2815 | } |
| 2816 | |
| 2817 | void |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 2818 | printtrailer(void) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2819 | { |
| 2820 | tprintf("\n"); |
| 2821 | tcp_last = NULL; |
| 2822 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2823 | |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 2824 | #ifdef HAVE_MP_PROCFS |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2825 | |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 2826 | int |
| 2827 | mp_ioctl(int fd, int cmd, void *arg, int size) |
| 2828 | { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2829 | struct iovec iov[2]; |
| 2830 | int n = 1; |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2831 | |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2832 | iov[0].iov_base = &cmd; |
| 2833 | iov[0].iov_len = sizeof cmd; |
| 2834 | if (arg) { |
| 2835 | ++n; |
| 2836 | iov[1].iov_base = arg; |
| 2837 | iov[1].iov_len = size; |
| 2838 | } |
Roland McGrath | 553a609 | 2002-12-16 20:40:39 +0000 | [diff] [blame] | 2839 | |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 2840 | return writev(fd, iov, n); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | #endif |