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