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