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