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