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