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