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