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