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> |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | * |
| 29 | * $Id$ |
| 30 | */ |
| 31 | |
| 32 | #include "defs.h" |
| 33 | |
| 34 | #include <signal.h> |
| 35 | #include <errno.h> |
| 36 | #include <sys/param.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <sys/resource.h> |
| 39 | #include <sys/wait.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <pwd.h> |
| 42 | #include <grp.h> |
| 43 | #include <string.h> |
| 44 | |
| 45 | #ifdef SVR4 |
| 46 | #include <sys/stropts.h> |
| 47 | #include <poll.h> |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 48 | #ifdef SVR4_MP |
| 49 | #include <sys/uio.h> |
| 50 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
| 53 | int debug = 0, followfork = 0, followvfork = 0, interactive = 0; |
| 54 | int rflag = 0, tflag = 0, dtime = 0, cflag = 0; |
| 55 | int iflag = 0, xflag = 0, qflag = 0; |
| 56 | int pflag_seen = 0; |
| 57 | |
| 58 | char *username = NULL; |
| 59 | uid_t run_uid; |
| 60 | gid_t run_gid; |
| 61 | |
| 62 | int acolumn = DEFAULT_ACOLUMN; |
| 63 | int max_strlen = DEFAULT_STRLEN; |
| 64 | char *outfname = NULL; |
| 65 | FILE *outf; |
| 66 | struct tcb tcbtab[MAX_PROCS]; |
| 67 | int nprocs; |
| 68 | char *progname; |
| 69 | extern char version[]; |
| 70 | extern char **environ; |
| 71 | |
| 72 | static struct tcb *pid2tcb P((int pid)); |
| 73 | static int trace P((void)); |
| 74 | static void cleanup P((void)); |
| 75 | static void interrupt P((int sig)); |
| 76 | static sigset_t empty_set, blocked_set; |
| 77 | |
| 78 | #ifdef HAVE_SIG_ATOMIC_T |
| 79 | static volatile sig_atomic_t interrupted; |
| 80 | #else /* !HAVE_SIG_ATOMIC_T */ |
| 81 | #ifdef __STDC__ |
| 82 | static volatile int interrupted; |
| 83 | #else /* !__STDC__ */ |
| 84 | static int interrupted; |
| 85 | #endif /* !__STDC__ */ |
| 86 | #endif /* !HAVE_SIG_ATOMIC_T */ |
| 87 | |
| 88 | #ifdef SVR4 |
| 89 | |
| 90 | static struct tcb *pfd2tcb P((int pfd)); |
| 91 | static void reaper P((int sig)); |
| 92 | static void rebuild_pollv P((void)); |
Wichert Akkerman | e68d61c | 1999-06-28 13:17:16 +0000 | [diff] [blame] | 93 | struct pollfd pollv[MAX_PROCS]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 94 | |
| 95 | #ifndef HAVE_POLLABLE_PROCFS |
| 96 | |
| 97 | static void proc_poll_open P((void)); |
| 98 | static void proc_poller P((int pfd)); |
| 99 | |
| 100 | struct proc_pollfd { |
| 101 | int fd; |
| 102 | int revents; |
| 103 | int pid; |
| 104 | }; |
| 105 | |
| 106 | static int poller_pid; |
| 107 | static int proc_poll_pipe[2] = { -1, -1 }; |
| 108 | |
| 109 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 110 | |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 111 | #ifdef SVR4_MP |
| 112 | #define POLLWANT POLLWRNORM |
| 113 | #else |
| 114 | #define POLLWANT POLLPRI |
| 115 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 116 | #endif /* SVR4 */ |
| 117 | |
| 118 | static void |
| 119 | usage(ofp, exitval) |
| 120 | FILE *ofp; |
| 121 | int exitval; |
| 122 | { |
| 123 | fprintf(ofp, "\ |
| 124 | usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\ |
| 125 | [-p pid] ... [-s strsize] [-u username] [command [arg ...]]\n\ |
| 126 | or: strace -c [-e expr] ... [-O overhead] [-S sortby] [command [arg ...]]\n\ |
| 127 | -c -- count time, calls, and errors for each syscall and report summary\n\ |
| 128 | -f -- follow forks, -ff -- with output into separate files\n\ |
| 129 | -F -- attempt to follow vforks, -h -- print help message\n\ |
| 130 | -i -- print instruction pointer at time of syscall\n\ |
| 131 | -q -- suppress messages about attaching, detaching, etc.\n\ |
| 132 | -r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\ |
| 133 | -T -- print time spent in each syscall, -V -- print version\n\ |
| 134 | -v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\ |
| 135 | -x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\ |
| 136 | -a column -- alignment COLUMN for printing syscall results (default %d)\n\ |
| 137 | -e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\ |
| 138 | options: trace, abbrev, verbose, raw, signal, read, or write\n\ |
| 139 | -o file -- send trace output to FILE instead of stderr\n\ |
| 140 | -O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\ |
| 141 | -p pid -- trace process with process id PID, may be repeated\n\ |
| 142 | -s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\ |
| 143 | -S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\ |
| 144 | -u username -- run command as username handling setuid and/or setgid\n\ |
| 145 | ", DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY); |
| 146 | exit(exitval); |
| 147 | } |
| 148 | |
| 149 | #ifdef SVR4 |
| 150 | #ifdef MIPS |
| 151 | void |
| 152 | foobar() |
| 153 | { |
| 154 | } |
| 155 | #endif /* MIPS */ |
| 156 | #endif /* SVR4 */ |
| 157 | |
| 158 | int |
| 159 | main(argc, argv) |
| 160 | int argc; |
| 161 | char *argv[]; |
| 162 | { |
| 163 | extern int optind; |
| 164 | extern char *optarg; |
| 165 | struct tcb *tcp; |
| 166 | int c, pid = 0; |
| 167 | struct sigaction sa; |
| 168 | |
| 169 | static char buf[BUFSIZ]; |
| 170 | |
| 171 | progname = argv[0]; |
| 172 | outf = stderr; |
| 173 | interactive = 1; |
| 174 | qualify("trace=all"); |
| 175 | qualify("abbrev=all"); |
| 176 | qualify("verbose=all"); |
| 177 | qualify("signal=all"); |
| 178 | set_sortby(DEFAULT_SORTBY); |
| 179 | set_personality(DEFAULT_PERSONALITY); |
| 180 | while ((c = getopt(argc, argv, |
| 181 | "+cdfFhiqrtTvVxa:e:o:O:p:s:S:u:")) != EOF) { |
| 182 | switch (c) { |
| 183 | case 'c': |
| 184 | cflag++; |
| 185 | dtime++; |
| 186 | break; |
| 187 | case 'd': |
| 188 | debug++; |
| 189 | break; |
| 190 | case 'f': |
| 191 | followfork++; |
| 192 | break; |
| 193 | case 'F': |
| 194 | followvfork++; |
| 195 | break; |
| 196 | case 'h': |
| 197 | usage(stdout, 0); |
| 198 | break; |
| 199 | case 'i': |
| 200 | iflag++; |
| 201 | break; |
| 202 | case 'q': |
| 203 | qflag++; |
| 204 | break; |
| 205 | case 'r': |
| 206 | rflag++; |
| 207 | tflag++; |
| 208 | break; |
| 209 | case 't': |
| 210 | tflag++; |
| 211 | break; |
| 212 | case 'T': |
| 213 | dtime++; |
| 214 | break; |
| 215 | case 'x': |
| 216 | xflag++; |
| 217 | break; |
| 218 | case 'v': |
| 219 | qualify("abbrev=none"); |
| 220 | break; |
| 221 | case 'V': |
| 222 | printf("%s\n", version); |
| 223 | exit(0); |
| 224 | break; |
| 225 | case 'a': |
| 226 | acolumn = atoi(optarg); |
| 227 | break; |
| 228 | case 'e': |
| 229 | qualify(optarg); |
| 230 | break; |
| 231 | case 'o': |
| 232 | outfname = strdup(optarg); |
| 233 | break; |
| 234 | case 'O': |
| 235 | set_overhead(atoi(optarg)); |
| 236 | break; |
| 237 | case 'p': |
| 238 | if ((pid = atoi(optarg)) == 0) { |
| 239 | fprintf(stderr, "%s: Invalid process id: %s\n", |
| 240 | progname, optarg); |
| 241 | break; |
| 242 | } |
| 243 | if (pid == getpid()) { |
Wichert Akkerman | 54a4767 | 1999-10-17 00:57:34 +0000 | [diff] [blame] | 244 | 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] | 245 | break; |
| 246 | } |
| 247 | if ((tcp = alloctcb(pid)) == NULL) { |
| 248 | fprintf(stderr, "%s: tcb table full, please recompile strace\n", |
| 249 | progname); |
| 250 | exit(1); |
| 251 | } |
| 252 | tcp->flags |= TCB_ATTACHED; |
| 253 | pflag_seen++; |
| 254 | break; |
| 255 | case 's': |
| 256 | max_strlen = atoi(optarg); |
| 257 | break; |
| 258 | case 'S': |
| 259 | set_sortby(optarg); |
| 260 | break; |
| 261 | case 'u': |
| 262 | username = strdup(optarg); |
| 263 | break; |
| 264 | default: |
| 265 | usage(stderr, 1); |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /* See if they want to run as another user. */ |
| 271 | if (username != NULL) { |
| 272 | struct passwd *pent; |
| 273 | |
| 274 | if (getuid() != 0 || geteuid() != 0) { |
| 275 | fprintf(stderr, |
| 276 | "%s: you must be root to use the -u option\n", |
| 277 | progname); |
| 278 | exit(1); |
| 279 | } |
| 280 | if ((pent = getpwnam(username)) == NULL) { |
| 281 | fprintf(stderr, "%s: cannot find user `%s'\n", |
| 282 | progname, optarg); |
| 283 | exit(1); |
| 284 | } |
| 285 | run_uid = pent->pw_uid; |
| 286 | run_gid = pent->pw_gid; |
| 287 | } |
| 288 | else { |
| 289 | run_uid = getuid(); |
| 290 | run_gid = getgid(); |
| 291 | } |
| 292 | |
| 293 | #ifndef SVR4 |
| 294 | setreuid(geteuid(), getuid()); |
| 295 | #endif |
| 296 | |
| 297 | /* See if they want to pipe the output. */ |
| 298 | if (outfname && (outfname[0] == '|' || outfname[0] == '!')) { |
| 299 | if ((outf = popen(outfname + 1, "w")) == NULL) { |
| 300 | fprintf(stderr, "%s: can't popen '%s': %s\n", |
| 301 | progname, outfname + 1, strerror(errno)); |
| 302 | exit(1); |
| 303 | } |
| 304 | free(outfname); |
| 305 | outfname = NULL; |
| 306 | } |
| 307 | |
| 308 | /* Check if they want to redirect the output. */ |
| 309 | if (outfname) { |
| 310 | if ((outf = fopen(outfname, "w")) == NULL) { |
| 311 | fprintf(stderr, "%s: can't fopen '%s': %s\n", |
| 312 | progname, outfname, strerror(errno)); |
| 313 | exit(1); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | #ifndef SVR4 |
| 318 | setreuid(geteuid(), getuid()); |
| 319 | #endif |
| 320 | |
| 321 | if (!outfname) { |
| 322 | qflag = 1; |
| 323 | setvbuf(outf, buf, _IOLBF, BUFSIZ); |
| 324 | } |
| 325 | else if (optind < argc) |
| 326 | interactive = 0; |
| 327 | else |
| 328 | qflag = 1; |
| 329 | |
| 330 | for (c = 0, tcp = tcbtab; c < MAX_PROCS; c++, tcp++) { |
| 331 | /* Reinitialize the output since it may have changed. */ |
| 332 | tcp->outf = outf; |
| 333 | if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED)) |
| 334 | continue; |
| 335 | #ifdef SVR4 |
| 336 | if (proc_open(tcp, 1) < 0) { |
| 337 | fprintf(stderr, "trouble opening proc file\n"); |
| 338 | droptcb(tcp); |
| 339 | continue; |
| 340 | } |
| 341 | #else /* !SVR4 */ |
| 342 | if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) { |
| 343 | perror("attach: ptrace(PTRACE_ATTACH, ...)"); |
| 344 | droptcb(tcp); |
| 345 | continue; |
| 346 | } |
| 347 | #endif /* !SVR4 */ |
| 348 | if (!qflag) |
| 349 | fprintf(stderr, |
| 350 | "Process %u attached - interrupt to quit\n", |
| 351 | pid); |
| 352 | } |
| 353 | |
| 354 | if (optind < argc) { |
| 355 | struct stat statbuf; |
| 356 | char *filename; |
| 357 | char pathname[MAXPATHLEN]; |
| 358 | |
| 359 | filename = argv[optind]; |
| 360 | if (strchr(filename, '/')) |
| 361 | strcpy(pathname, filename); |
| 362 | #ifdef USE_DEBUGGING_EXEC |
| 363 | /* |
| 364 | * Debuggers customarily check the current directory |
| 365 | * first regardless of the path but doing that gives |
| 366 | * security geeks a panic attack. |
| 367 | */ |
| 368 | else if (stat(filename, &statbuf) == 0) |
| 369 | strcpy(pathname, filename); |
| 370 | #endif /* USE_DEBUGGING_EXEC */ |
| 371 | else { |
| 372 | char *path; |
| 373 | int m, n, len; |
| 374 | |
| 375 | for (path = getenv("PATH"); path && *path; path += m) { |
| 376 | if (strchr(path, ':')) { |
| 377 | n = strchr(path, ':') - path; |
| 378 | m = n + 1; |
| 379 | } |
| 380 | else |
| 381 | m = n = strlen(path); |
| 382 | if (n == 0) { |
| 383 | getcwd(pathname, MAXPATHLEN); |
| 384 | len = strlen(pathname); |
| 385 | } |
| 386 | else { |
| 387 | strncpy(pathname, path, n); |
| 388 | len = n; |
| 389 | } |
| 390 | if (len && pathname[len - 1] != '/') |
| 391 | pathname[len++] = '/'; |
| 392 | strcpy(pathname + len, filename); |
| 393 | if (stat(pathname, &statbuf) == 0) |
| 394 | break; |
| 395 | } |
| 396 | } |
| 397 | if (stat(pathname, &statbuf) < 0) { |
| 398 | fprintf(stderr, "%s: %s: command not found\n", |
| 399 | progname, filename); |
| 400 | exit(1); |
| 401 | } |
| 402 | switch (pid = fork()) { |
| 403 | case -1: |
| 404 | perror("strace: fork"); |
| 405 | cleanup(); |
| 406 | exit(1); |
| 407 | break; |
| 408 | case 0: { |
| 409 | #ifdef SVR4 |
Wichert Akkerman | 789ed35 | 1999-06-14 10:45:01 +0000 | [diff] [blame] | 410 | if (outf != stderr) close (fileno (outf)); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 411 | #ifdef MIPS |
| 412 | /* Kludge for SGI, see proc_open for details. */ |
| 413 | sa.sa_handler = foobar; |
| 414 | sa.sa_flags = 0; |
| 415 | sigemptyset(&sa.sa_mask); |
| 416 | sigaction(SIGINT, &sa, NULL); |
| 417 | #endif /* MIPS */ |
| 418 | pause(); |
| 419 | #else /* !SVR4 */ |
| 420 | if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) { |
| 421 | perror("strace: ptrace(PTRACE_TRACEME, ...)"); |
| 422 | return -1; |
| 423 | } |
| 424 | if (debug) |
| 425 | kill(getpid(), SIGSTOP); |
| 426 | |
| 427 | if (username != NULL || geteuid() == 0) { |
| 428 | uid_t run_euid = run_uid; |
| 429 | gid_t run_egid = run_gid; |
| 430 | |
| 431 | if (statbuf.st_mode & S_ISUID) |
| 432 | run_euid = statbuf.st_uid; |
| 433 | if (statbuf.st_mode & S_ISGID) |
| 434 | run_egid = statbuf.st_gid; |
| 435 | |
| 436 | /* |
| 437 | * It is important to set groups before we |
| 438 | * lose privileges on setuid. |
| 439 | */ |
| 440 | if (username != NULL |
| 441 | && initgroups(username, run_gid) < 0) { |
| 442 | perror("initgroups"); |
| 443 | exit(1); |
| 444 | } |
| 445 | if (setregid(run_gid, run_egid) < 0) { |
| 446 | perror("setregid"); |
| 447 | exit(1); |
| 448 | } |
| 449 | if (setreuid(run_uid, run_euid) < 0) { |
| 450 | perror("setreuid"); |
| 451 | exit(1); |
| 452 | } |
| 453 | } |
| 454 | else |
| 455 | setreuid(run_uid, run_uid); |
| 456 | #endif /* !SVR4 */ |
| 457 | |
| 458 | execv(pathname, &argv[optind]); |
| 459 | perror("strace: exec"); |
| 460 | _exit(1); |
| 461 | break; |
| 462 | } |
| 463 | default: |
| 464 | if ((tcp = alloctcb(pid)) == NULL) { |
| 465 | fprintf(stderr, "tcb table full\n"); |
| 466 | cleanup(); |
| 467 | exit(1); |
| 468 | } |
| 469 | #ifdef SVR4 |
| 470 | if (proc_open(tcp, 0) < 0) { |
| 471 | fprintf(stderr, "trouble opening proc file\n"); |
| 472 | cleanup(); |
| 473 | exit(1); |
| 474 | } |
| 475 | #endif /* SVR4 */ |
| 476 | #ifndef SVR4 |
| 477 | fake_execve(tcp, pathname, &argv[optind], environ); |
| 478 | #endif |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | else if (pflag_seen == 0) |
| 483 | usage(stderr, 1); |
| 484 | |
| 485 | sigemptyset(&empty_set); |
| 486 | sigemptyset(&blocked_set); |
| 487 | sa.sa_handler = SIG_IGN; |
| 488 | sigemptyset(&sa.sa_mask); |
| 489 | sa.sa_flags = 0; |
| 490 | sigaction(SIGTTOU, &sa, NULL); |
| 491 | sigaction(SIGTTIN, &sa, NULL); |
| 492 | if (interactive) { |
| 493 | sigaddset(&blocked_set, SIGHUP); |
| 494 | sigaddset(&blocked_set, SIGINT); |
| 495 | sigaddset(&blocked_set, SIGQUIT); |
| 496 | sigaddset(&blocked_set, SIGPIPE); |
| 497 | sigaddset(&blocked_set, SIGTERM); |
| 498 | sa.sa_handler = interrupt; |
| 499 | #ifdef SUNOS4 |
| 500 | /* POSIX signals on sunos4.1 are a little broken. */ |
| 501 | sa.sa_flags = SA_INTERRUPT; |
| 502 | #endif /* SUNOS4 */ |
| 503 | } |
| 504 | sigaction(SIGHUP, &sa, NULL); |
| 505 | sigaction(SIGINT, &sa, NULL); |
| 506 | sigaction(SIGQUIT, &sa, NULL); |
| 507 | sigaction(SIGPIPE, &sa, NULL); |
| 508 | sigaction(SIGTERM, &sa, NULL); |
| 509 | #ifdef SVR4 |
| 510 | sa.sa_handler = reaper; |
| 511 | sigaction(SIGCHLD, &sa, NULL); |
| 512 | #endif /* SVR4 */ |
| 513 | |
| 514 | if (trace() < 0) |
| 515 | exit(1); |
| 516 | cleanup(); |
| 517 | exit(0); |
| 518 | } |
| 519 | |
| 520 | void |
| 521 | newoutf(tcp) |
| 522 | struct tcb *tcp; |
| 523 | { |
| 524 | char name[MAXPATHLEN]; |
| 525 | FILE *fp; |
| 526 | |
| 527 | if (outfname && followfork > 1) { |
| 528 | sprintf(name, "%s.%u", outfname, tcp->pid); |
| 529 | #ifndef SVR4 |
| 530 | setreuid(geteuid(), getuid()); |
| 531 | #endif |
| 532 | fp = fopen(name, "w"); |
| 533 | #ifndef SVR4 |
| 534 | setreuid(geteuid(), getuid()); |
| 535 | #endif |
| 536 | if (fp == NULL) { |
| 537 | perror("fopen"); |
| 538 | return; |
| 539 | } |
| 540 | tcp->outf = fp; |
| 541 | } |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | struct tcb * |
| 546 | alloctcb(pid) |
| 547 | int pid; |
| 548 | { |
| 549 | int i; |
| 550 | struct tcb *tcp; |
| 551 | |
| 552 | for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) { |
| 553 | if ((tcp->flags & TCB_INUSE) == 0) { |
| 554 | tcp->pid = pid; |
| 555 | tcp->parent = NULL; |
| 556 | tcp->nchildren = 0; |
| 557 | tcp->flags = TCB_INUSE | TCB_STARTUP; |
| 558 | tcp->outf = outf; /* Initialise to current out file */ |
| 559 | tcp->stime.tv_sec = 0; |
| 560 | tcp->stime.tv_usec = 0; |
| 561 | tcp->pfd = -1; |
| 562 | nprocs++; |
| 563 | return tcp; |
| 564 | } |
| 565 | } |
| 566 | return NULL; |
| 567 | } |
| 568 | |
| 569 | #ifdef SVR4 |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 570 | int |
| 571 | proc_open(tcp, attaching) |
| 572 | struct tcb *tcp; |
| 573 | int attaching; |
| 574 | { |
| 575 | char proc[32]; |
| 576 | long arg; |
| 577 | sysset_t sc_enter, sc_exit; |
| 578 | sigset_t signals; |
| 579 | fltset_t faults; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 580 | #ifndef HAVE_POLLABLE_PROCFS |
| 581 | static int last_pfd; |
| 582 | #endif |
| 583 | |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 584 | #ifdef SVR4_MP |
| 585 | /* Open the process pseudo-files in /proc. */ |
| 586 | sprintf(proc, "/proc/%d/ctl", tcp->pid); |
| 587 | if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 588 | perror("strace: open(\"/proc/...\", ...)"); |
| 589 | return -1; |
| 590 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 591 | if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) { |
| 592 | perror("F_GETFD"); |
| 593 | return -1; |
| 594 | } |
| 595 | if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) { |
| 596 | perror("F_SETFD"); |
| 597 | return -1; |
| 598 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 599 | sprintf(proc, "/proc/%d/status", tcp->pid); |
| 600 | if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) { |
| 601 | perror("strace: open(\"/proc/...\", ...)"); |
| 602 | return -1; |
| 603 | } |
| 604 | if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) { |
| 605 | perror("F_GETFD"); |
| 606 | return -1; |
| 607 | } |
| 608 | if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) { |
| 609 | perror("F_SETFD"); |
| 610 | return -1; |
| 611 | } |
| 612 | sprintf(proc, "/proc/%d/as", tcp->pid); |
| 613 | if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) { |
| 614 | perror("strace: open(\"/proc/...\", ...)"); |
| 615 | return -1; |
| 616 | } |
| 617 | if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) { |
| 618 | perror("F_GETFD"); |
| 619 | return -1; |
| 620 | } |
| 621 | if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) { |
| 622 | perror("F_SETFD"); |
| 623 | return -1; |
| 624 | } |
| 625 | #else |
| 626 | /* Open the process pseudo-file in /proc. */ |
| 627 | sprintf(proc, "/proc/%d", tcp->pid); |
| 628 | if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) { |
| 629 | perror("strace: open(\"/proc/...\", ...)"); |
| 630 | return -1; |
| 631 | } |
| 632 | if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) { |
| 633 | perror("F_GETFD"); |
| 634 | return -1; |
| 635 | } |
| 636 | if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) { |
| 637 | perror("F_SETFD"); |
| 638 | return -1; |
| 639 | } |
| 640 | #endif |
| 641 | rebuild_pollv(); |
| 642 | if (!attaching) { |
| 643 | /* |
| 644 | * Wait for the child to pause. Because of a race |
| 645 | * condition we have to poll for the event. |
| 646 | */ |
| 647 | for (;;) { |
| 648 | if (IOCTL_STATUS (tcp) < 0) { |
| 649 | perror("strace: PIOCSTATUS"); |
| 650 | return -1; |
| 651 | } |
| 652 | if (tcp->status.PR_FLAGS & PR_ASLEEP) |
| 653 | break; |
| 654 | } |
| 655 | } |
| 656 | /* Stop the process so that we own the stop. */ |
| 657 | if (IOCTL(tcp->pfd, PIOCSTOP, NULL) < 0) { |
| 658 | perror("strace: PIOCSTOP"); |
| 659 | return -1; |
| 660 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 661 | #ifdef PIOCSET |
| 662 | /* Set Run-on-Last-Close. */ |
| 663 | arg = PR_RLC; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 664 | if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 665 | perror("PIOCSET PR_RLC"); |
| 666 | return -1; |
| 667 | } |
| 668 | /* Set or Reset Inherit-on-Fork. */ |
| 669 | arg = PR_FORK; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 670 | if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 671 | perror("PIOC{SET,RESET} PR_FORK"); |
| 672 | return -1; |
| 673 | } |
| 674 | #else /* !PIOCSET */ |
| 675 | if (ioctl(tcp->pfd, PIOCSRLC) < 0) { |
| 676 | perror("PIOCSRLC"); |
| 677 | return -1; |
| 678 | } |
| 679 | if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) { |
| 680 | perror("PIOC{S,R}FORK"); |
| 681 | return -1; |
| 682 | } |
| 683 | #endif /* !PIOCSET */ |
| 684 | /* Enable all syscall entries. */ |
| 685 | prfillset(&sc_enter); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 686 | if (IOCTL(tcp->pfd, PIOCSENTRY, &sc_enter) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 687 | perror("PIOCSENTRY"); |
| 688 | return -1; |
| 689 | } |
| 690 | /* Enable all syscall exits. */ |
| 691 | prfillset(&sc_exit); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 692 | if (IOCTL(tcp->pfd, PIOCSEXIT, &sc_exit) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 693 | perror("PIOSEXIT"); |
| 694 | return -1; |
| 695 | } |
| 696 | /* Enable all signals. */ |
| 697 | prfillset(&signals); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 698 | if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 699 | perror("PIOCSTRACE"); |
| 700 | return -1; |
| 701 | } |
| 702 | /* Enable all faults. */ |
| 703 | prfillset(&faults); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 704 | if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 705 | perror("PIOCSFAULT"); |
| 706 | return -1; |
| 707 | } |
| 708 | if (!attaching) { |
| 709 | #ifdef MIPS |
| 710 | /* |
| 711 | * The SGI PRSABORT doesn't work for pause() so |
| 712 | * we send it a caught signal to wake it up. |
| 713 | */ |
| 714 | kill(tcp->pid, SIGINT); |
| 715 | #else /* !MIPS */ |
| 716 | /* The child is in a pause(), abort it. */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 717 | arg = PRSABORT; |
| 718 | if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 719 | perror("PIOCRUN"); |
| 720 | return -1; |
| 721 | } |
| 722 | #endif /* !MIPS */ |
| 723 | for (;;) { |
| 724 | /* Wait for the child to do something. */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 725 | if (IOCTL_WSTOP (tcp) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 726 | perror("PIOCWSTOP"); |
| 727 | return -1; |
| 728 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 729 | if (tcp->status.PR_WHY == PR_SYSENTRY) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 730 | #ifdef HAVE_PR_SYSCALL |
| 731 | int scno = tcp->status.pr_syscall; |
| 732 | #else /* !HAVE_PR_SYSCALL */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 733 | int scno = tcp->status.PR_WHAT; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 734 | #endif /* !HAVE_PR_SYSCALL */ |
| 735 | if (scno == SYS_execve) |
| 736 | break; |
| 737 | } |
| 738 | /* Set it running: maybe execve will be next. */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 739 | arg = 0; |
| 740 | if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 741 | perror("PIOCRUN"); |
| 742 | return -1; |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | #ifndef HAVE_POLLABLE_PROCFS |
| 747 | if (proc_poll_pipe[0] != -1) |
| 748 | proc_poller(tcp->pfd); |
| 749 | else if (nprocs > 1) { |
| 750 | proc_poll_open(); |
| 751 | proc_poller(last_pfd); |
| 752 | proc_poller(tcp->pfd); |
| 753 | } |
| 754 | last_pfd = tcp->pfd; |
| 755 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | #endif /* SVR4 */ |
| 760 | |
| 761 | static struct tcb * |
| 762 | pid2tcb(pid) |
| 763 | int pid; |
| 764 | { |
| 765 | int i; |
| 766 | struct tcb *tcp; |
| 767 | |
| 768 | for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) { |
| 769 | if (pid && tcp->pid != pid) |
| 770 | continue; |
| 771 | if (tcp->flags & TCB_INUSE) |
| 772 | return tcp; |
| 773 | } |
| 774 | return NULL; |
| 775 | } |
| 776 | |
| 777 | #ifdef SVR4 |
| 778 | |
| 779 | static struct tcb * |
| 780 | pfd2tcb(pfd) |
| 781 | int pfd; |
| 782 | { |
| 783 | int i; |
| 784 | struct tcb *tcp; |
| 785 | |
| 786 | for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) { |
| 787 | if (tcp->pfd != pfd) |
| 788 | continue; |
| 789 | if (tcp->flags & TCB_INUSE) |
| 790 | return tcp; |
| 791 | } |
| 792 | return NULL; |
| 793 | } |
| 794 | |
| 795 | #endif /* SVR4 */ |
| 796 | |
| 797 | void |
| 798 | droptcb(tcp) |
| 799 | struct tcb *tcp; |
| 800 | { |
| 801 | if (tcp->pid == 0) |
| 802 | return; |
| 803 | nprocs--; |
| 804 | tcp->pid = 0; |
| 805 | tcp->flags = 0; |
| 806 | if (tcp->pfd != -1) { |
| 807 | close(tcp->pfd); |
| 808 | tcp->pfd = -1; |
| 809 | #ifdef SVR4 |
| 810 | rebuild_pollv(); |
| 811 | #endif |
| 812 | } |
| 813 | if (tcp->parent != NULL) { |
| 814 | tcp->parent->nchildren--; |
| 815 | tcp->parent = NULL; |
| 816 | } |
| 817 | #if 0 |
| 818 | if (tcp->outf != stderr) |
| 819 | fclose(tcp->outf); |
| 820 | #endif |
| 821 | tcp->outf = 0; |
| 822 | } |
| 823 | |
| 824 | #ifndef SVR4 |
| 825 | |
| 826 | static int |
| 827 | resume(tcp) |
| 828 | struct tcb *tcp; |
| 829 | { |
| 830 | if (tcp == NULL) |
| 831 | return -1; |
| 832 | |
| 833 | if (!(tcp->flags & TCB_SUSPENDED)) { |
| 834 | fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid); |
| 835 | return -1; |
| 836 | } |
| 837 | tcp->flags &= ~TCB_SUSPENDED; |
| 838 | |
| 839 | if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) { |
| 840 | perror("resume: ptrace(PTRACE_SYSCALL, ...)"); |
| 841 | return -1; |
| 842 | } |
| 843 | |
| 844 | if (!qflag) |
| 845 | fprintf(stderr, "Process %u resumed\n", tcp->pid); |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | #endif /* !SVR4 */ |
| 850 | |
| 851 | /* detach traced process; continue with sig */ |
| 852 | |
| 853 | static int |
| 854 | detach(tcp, sig) |
| 855 | struct tcb *tcp; |
| 856 | int sig; |
| 857 | { |
| 858 | int error = 0; |
| 859 | #ifdef LINUX |
| 860 | int status; |
| 861 | #endif |
| 862 | |
| 863 | if (tcp->flags & TCB_BPTSET) |
| 864 | sig = SIGKILL; |
| 865 | |
| 866 | #ifdef LINUX |
| 867 | /* |
| 868 | * Linux wrongly insists the child be stopped |
| 869 | * before detaching. Arghh. We go through hoops |
| 870 | * to make a clean break of things. |
| 871 | */ |
Wichert Akkerman | dacfb6e | 1999-06-03 14:21:07 +0000 | [diff] [blame] | 872 | #if defined(SPARC) |
| 873 | #undef PTRACE_DETACH |
| 874 | #define PTRACE_DETACH PTRACE_SUNDETACH |
| 875 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 876 | if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) { |
| 877 | /* On a clear day, you can see forever. */ |
| 878 | } |
| 879 | else if (errno != ESRCH) { |
| 880 | /* Shouldn't happen. */ |
| 881 | perror("detach: ptrace(PTRACE_DETACH, ...)"); |
| 882 | } |
| 883 | else if (kill(tcp->pid, 0) < 0) { |
| 884 | if (errno != ESRCH) |
| 885 | perror("detach: checking sanity"); |
| 886 | } |
| 887 | else if (kill(tcp->pid, SIGSTOP) < 0) { |
| 888 | if (errno != ESRCH) |
| 889 | perror("detach: stopping child"); |
| 890 | } |
| 891 | else { |
| 892 | for (;;) { |
| 893 | if (waitpid(tcp->pid, &status, 0) < 0) { |
| 894 | if (errno != ECHILD) |
| 895 | perror("detach: waiting"); |
| 896 | break; |
| 897 | } |
| 898 | if (!WIFSTOPPED(status)) { |
| 899 | /* Au revoir, mon ami. */ |
| 900 | break; |
| 901 | } |
| 902 | if (WSTOPSIG(status) == SIGSTOP) { |
| 903 | if ((error = ptrace(PTRACE_DETACH, |
| 904 | tcp->pid, (char *) 1, sig)) < 0) { |
| 905 | if (errno != ESRCH) |
| 906 | perror("detach: ptrace(PTRACE_DETACH, ...)"); |
| 907 | /* I died trying. */ |
| 908 | } |
| 909 | break; |
| 910 | } |
| 911 | if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1, |
| 912 | WSTOPSIG(status) == SIGTRAP ? |
| 913 | 0 : WSTOPSIG(status))) < 0) { |
| 914 | if (errno != ESRCH) |
| 915 | perror("detach: ptrace(PTRACE_CONT, ...)"); |
| 916 | break; |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | #endif /* LINUX */ |
| 921 | |
| 922 | #if defined(SUNOS4) |
| 923 | /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */ |
| 924 | if (sig && kill(tcp->pid, sig) < 0) |
| 925 | perror("detach: kill"); |
| 926 | sig = 0; |
| 927 | if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0) |
| 928 | perror("detach: ptrace(PTRACE_DETACH, ...)"); |
| 929 | #endif /* SUNOS4 */ |
| 930 | |
| 931 | #ifndef SVR4 |
| 932 | if (waiting_parent(tcp)) |
| 933 | error = resume(tcp->parent); |
| 934 | #endif /* !SVR4 */ |
| 935 | |
| 936 | if (!qflag) |
| 937 | fprintf(stderr, "Process %u detached\n", tcp->pid); |
| 938 | |
| 939 | droptcb(tcp); |
| 940 | return error; |
| 941 | } |
| 942 | |
| 943 | #ifdef SVR4 |
| 944 | |
| 945 | static void |
| 946 | reaper(sig) |
| 947 | int sig; |
| 948 | { |
| 949 | int pid; |
| 950 | int status; |
| 951 | |
| 952 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { |
| 953 | #if 0 |
| 954 | struct tcb *tcp; |
| 955 | |
| 956 | tcp = pid2tcb(pid); |
| 957 | if (tcp) |
| 958 | droptcb(tcp); |
| 959 | #endif |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | #endif /* SVR4 */ |
| 964 | |
| 965 | static void |
| 966 | cleanup() |
| 967 | { |
| 968 | int i; |
| 969 | struct tcb *tcp; |
| 970 | |
| 971 | for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) { |
| 972 | if (!(tcp->flags & TCB_INUSE)) |
| 973 | continue; |
| 974 | if (debug) |
| 975 | fprintf(stderr, |
| 976 | "cleanup: looking at pid %u\n", tcp->pid); |
| 977 | if (tcp_last && |
| 978 | (!outfname || followfork < 2 || tcp_last == tcp)) { |
| 979 | tprintf(" <unfinished ...>\n"); |
| 980 | tcp_last = NULL; |
| 981 | } |
| 982 | if (tcp->flags & TCB_ATTACHED) |
| 983 | detach(tcp, 0); |
| 984 | else { |
| 985 | kill(tcp->pid, SIGCONT); |
| 986 | kill(tcp->pid, SIGTERM); |
| 987 | } |
| 988 | } |
| 989 | if (cflag) |
| 990 | call_summary(outf); |
| 991 | } |
| 992 | |
| 993 | static void |
| 994 | interrupt(sig) |
| 995 | int sig; |
| 996 | { |
| 997 | interrupted = 1; |
| 998 | } |
| 999 | |
| 1000 | #ifndef HAVE_STRERROR |
| 1001 | |
| 1002 | #ifndef SYS_ERRLIST_DECLARED |
| 1003 | extern int sys_nerr; |
| 1004 | extern char *sys_errlist[]; |
| 1005 | #endif /* SYS_ERRLIST_DECLARED */ |
| 1006 | |
| 1007 | const char * |
| 1008 | strerror(errno) |
| 1009 | int errno; |
| 1010 | { |
| 1011 | static char buf[64]; |
| 1012 | |
| 1013 | if (errno < 1 || errno >= sys_nerr) { |
| 1014 | sprintf(buf, "Unknown error %d", errno); |
| 1015 | return buf; |
| 1016 | } |
| 1017 | return sys_errlist[errno]; |
| 1018 | } |
| 1019 | |
| 1020 | #endif /* HAVE_STERRROR */ |
| 1021 | |
| 1022 | #ifndef HAVE_STRSIGNAL |
| 1023 | |
| 1024 | #ifndef SYS_SIGLIST_DECLARED |
| 1025 | #ifdef HAVE__SYS_SIGLIST |
| 1026 | extern char *_sys_siglist[]; |
| 1027 | #else |
| 1028 | extern char *sys_siglist[]; |
| 1029 | #endif |
| 1030 | #endif /* SYS_SIGLIST_DECLARED */ |
| 1031 | |
| 1032 | const char * |
| 1033 | strsignal(sig) |
| 1034 | int sig; |
| 1035 | { |
| 1036 | static char buf[64]; |
| 1037 | |
| 1038 | if (sig < 1 || sig >= NSIG) { |
| 1039 | sprintf(buf, "Unknown signal %d", sig); |
| 1040 | return buf; |
| 1041 | } |
| 1042 | #ifdef HAVE__SYS_SIGLIST |
| 1043 | return _sys_siglist[sig]; |
| 1044 | #else |
| 1045 | return sys_siglist[sig]; |
| 1046 | #endif |
| 1047 | } |
| 1048 | |
| 1049 | #endif /* HAVE_STRSIGNAL */ |
| 1050 | |
| 1051 | #ifdef SVR4 |
| 1052 | |
| 1053 | static void |
| 1054 | rebuild_pollv() |
| 1055 | { |
| 1056 | int i, j; |
| 1057 | struct tcb *tcp; |
| 1058 | |
| 1059 | for (i = j = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) { |
| 1060 | if (!(tcp->flags & TCB_INUSE)) |
| 1061 | continue; |
| 1062 | pollv[j].fd = tcp->pfd; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1063 | pollv[j].events = POLLWANT; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1064 | j++; |
| 1065 | } |
| 1066 | if (j != nprocs) { |
| 1067 | fprintf(stderr, "strace: proc miscount\n"); |
| 1068 | exit(1); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | #ifndef HAVE_POLLABLE_PROCFS |
| 1073 | |
| 1074 | static void |
| 1075 | proc_poll_open() |
| 1076 | { |
| 1077 | int arg; |
| 1078 | int i; |
| 1079 | |
| 1080 | if (pipe(proc_poll_pipe) < 0) { |
| 1081 | perror("pipe"); |
| 1082 | exit(1); |
| 1083 | } |
| 1084 | for (i = 0; i < 2; i++) { |
| 1085 | if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) { |
| 1086 | perror("F_GETFD"); |
| 1087 | exit(1); |
| 1088 | } |
| 1089 | if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) { |
| 1090 | perror("F_SETFD"); |
| 1091 | exit(1); |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | static int |
| 1097 | proc_poll(pollv, nfds, timeout) |
| 1098 | struct pollfd *pollv; |
| 1099 | int nfds; |
| 1100 | int timeout; |
| 1101 | { |
| 1102 | int i; |
| 1103 | int n; |
| 1104 | struct proc_pollfd pollinfo; |
| 1105 | |
| 1106 | if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0) |
| 1107 | return n; |
| 1108 | if (n != sizeof(struct proc_pollfd)) { |
| 1109 | fprintf(stderr, "panic: short read: %d\n", n); |
| 1110 | exit(1); |
| 1111 | } |
| 1112 | for (i = 0; i < nprocs; i++) { |
| 1113 | if (pollv[i].fd == pollinfo.fd) |
| 1114 | pollv[i].revents = pollinfo.revents; |
| 1115 | else |
| 1116 | pollv[i].revents = 0; |
| 1117 | } |
| 1118 | poller_pid = pollinfo.pid; |
| 1119 | return 1; |
| 1120 | } |
| 1121 | |
| 1122 | static void |
| 1123 | wakeup_handler(sig) |
| 1124 | int sig; |
| 1125 | { |
| 1126 | } |
| 1127 | |
| 1128 | static void |
| 1129 | proc_poller(pfd) |
| 1130 | int pfd; |
| 1131 | { |
| 1132 | struct proc_pollfd pollinfo; |
| 1133 | struct sigaction sa; |
| 1134 | sigset_t blocked_set, empty_set; |
| 1135 | int i; |
| 1136 | int n; |
| 1137 | struct rlimit rl; |
| 1138 | |
| 1139 | switch (fork()) { |
| 1140 | case -1: |
| 1141 | perror("fork"); |
| 1142 | _exit(0); |
| 1143 | case 0: |
| 1144 | break; |
| 1145 | default: |
| 1146 | return; |
| 1147 | } |
| 1148 | |
| 1149 | sa.sa_handler = interactive ? SIG_DFL : SIG_IGN; |
| 1150 | sa.sa_flags = 0; |
| 1151 | sigemptyset(&sa.sa_mask); |
| 1152 | sigaction(SIGHUP, &sa, NULL); |
| 1153 | sigaction(SIGINT, &sa, NULL); |
| 1154 | sigaction(SIGQUIT, &sa, NULL); |
| 1155 | sigaction(SIGPIPE, &sa, NULL); |
| 1156 | sigaction(SIGTERM, &sa, NULL); |
| 1157 | sa.sa_handler = wakeup_handler; |
| 1158 | sigaction(SIGUSR1, &sa, NULL); |
| 1159 | sigemptyset(&blocked_set); |
| 1160 | sigaddset(&blocked_set, SIGUSR1); |
| 1161 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
| 1162 | sigemptyset(&empty_set); |
| 1163 | |
| 1164 | if (getrlimit(RLIMIT_NOFILE, &rl) < 0) { |
| 1165 | perror("getrlimit(RLIMIT_NOFILE, ...)"); |
| 1166 | _exit(0); |
| 1167 | } |
| 1168 | n = rl.rlim_cur; |
| 1169 | for (i = 0; i < n; i++) { |
| 1170 | if (i != pfd && i != proc_poll_pipe[1]) |
| 1171 | close(i); |
| 1172 | } |
| 1173 | |
| 1174 | pollinfo.fd = pfd; |
| 1175 | pollinfo.pid = getpid(); |
| 1176 | for (;;) { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1177 | if (ioctl(pfd, PIOCWSTOP, NULL) < 0) |
| 1178 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1179 | switch (errno) { |
| 1180 | case EINTR: |
| 1181 | continue; |
| 1182 | case EBADF: |
| 1183 | pollinfo.revents = POLLERR; |
| 1184 | break; |
| 1185 | case ENOENT: |
| 1186 | pollinfo.revents = POLLHUP; |
| 1187 | break; |
| 1188 | default: |
| 1189 | perror("proc_poller: PIOCWSTOP"); |
| 1190 | } |
| 1191 | write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo)); |
| 1192 | _exit(0); |
| 1193 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1194 | pollinfo.revents = POLLWANT; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1195 | write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo)); |
| 1196 | sigsuspend(&empty_set); |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 1201 | |
| 1202 | static int |
| 1203 | choose_pfd() |
| 1204 | { |
| 1205 | int i, j; |
| 1206 | struct tcb *tcp; |
| 1207 | |
| 1208 | static int last; |
| 1209 | |
| 1210 | if (followfork < 2 && |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1211 | last < nprocs && (pollv[last].revents & POLLWANT)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1212 | /* |
| 1213 | * The previous process is ready to run again. We'll |
| 1214 | * let it do so if it is currently in a syscall. This |
| 1215 | * heuristic improves the readability of the trace. |
| 1216 | */ |
| 1217 | tcp = pfd2tcb(pollv[last].fd); |
| 1218 | if (tcp && (tcp->flags & TCB_INSYSCALL)) |
| 1219 | return pollv[last].fd; |
| 1220 | } |
| 1221 | |
| 1222 | for (i = 0; i < nprocs; i++) { |
| 1223 | /* Let competing children run round robin. */ |
| 1224 | j = (i + last + 1) % nprocs; |
| 1225 | if (pollv[j].revents & (POLLHUP | POLLERR)) { |
| 1226 | tcp = pfd2tcb(pollv[j].fd); |
| 1227 | if (!tcp) { |
| 1228 | fprintf(stderr, "strace: lost proc\n"); |
| 1229 | exit(1); |
| 1230 | } |
| 1231 | droptcb(tcp); |
| 1232 | return -1; |
| 1233 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1234 | if (pollv[j].revents & POLLWANT) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1235 | last = j; |
| 1236 | return pollv[j].fd; |
| 1237 | } |
| 1238 | } |
| 1239 | fprintf(stderr, "strace: nothing ready\n"); |
| 1240 | exit(1); |
| 1241 | } |
| 1242 | |
| 1243 | static int |
| 1244 | trace() |
| 1245 | { |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 1246 | #ifdef POLL_HACK |
| 1247 | struct tcb *in_syscall; |
| 1248 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1249 | struct tcb *tcp; |
| 1250 | int pfd; |
| 1251 | int what; |
| 1252 | int ioctl_result = 0, ioctl_errno = 0; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1253 | long arg; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1254 | |
| 1255 | for (;;) { |
| 1256 | if (interactive) |
| 1257 | sigprocmask(SIG_SETMASK, &empty_set, NULL); |
| 1258 | |
| 1259 | if (nprocs == 0) |
| 1260 | break; |
| 1261 | |
| 1262 | switch (nprocs) { |
| 1263 | case 1: |
| 1264 | #ifndef HAVE_POLLABLE_PROCFS |
| 1265 | if (proc_poll_pipe[0] == -1) { |
| 1266 | #endif |
| 1267 | tcp = pid2tcb(0); |
| 1268 | if (!tcp) |
| 1269 | continue; |
| 1270 | pfd = tcp->pfd; |
| 1271 | if (pfd == -1) |
| 1272 | continue; |
| 1273 | break; |
| 1274 | #ifndef HAVE_POLLABLE_PROCFS |
| 1275 | } |
| 1276 | /* fall through ... */ |
| 1277 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 1278 | default: |
| 1279 | #ifdef HAVE_POLLABLE_PROCFS |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 1280 | #ifdef POLL_HACK |
| 1281 | /* On some systems (e.g. UnixWare) we get too much ugly |
| 1282 | "unfinished..." stuff when multiple proceses are in |
| 1283 | syscalls. Here's a nasty hack */ |
| 1284 | |
| 1285 | if (in_syscall) { |
| 1286 | struct pollfd pv; |
| 1287 | tcp = in_syscall; |
| 1288 | in_syscall = NULL; |
| 1289 | pv.fd = tcp->pfd; |
| 1290 | pv.events = POLLWANT; |
| 1291 | if ((what = poll (&pv, 1, 1)) < 0) { |
| 1292 | if (interrupted) |
| 1293 | return 0; |
| 1294 | continue; |
| 1295 | } |
| 1296 | else if (what == 1 && pv.revents & POLLWANT) { |
| 1297 | goto FOUND; |
| 1298 | } |
| 1299 | } |
| 1300 | #endif |
| 1301 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1302 | if (poll(pollv, nprocs, INFTIM) < 0) { |
| 1303 | if (interrupted) |
| 1304 | return 0; |
| 1305 | continue; |
| 1306 | } |
| 1307 | #else /* !HAVE_POLLABLE_PROCFS */ |
| 1308 | if (proc_poll(pollv, nprocs, INFTIM) < 0) { |
| 1309 | if (interrupted) |
| 1310 | return 0; |
| 1311 | continue; |
| 1312 | } |
| 1313 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 1314 | pfd = choose_pfd(); |
| 1315 | if (pfd == -1) |
| 1316 | continue; |
| 1317 | break; |
| 1318 | } |
| 1319 | |
| 1320 | /* Look up `pfd' in our table. */ |
| 1321 | if ((tcp = pfd2tcb(pfd)) == NULL) { |
| 1322 | fprintf(stderr, "unknown pfd: %u\n", pfd); |
| 1323 | exit(1); |
| 1324 | } |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 1325 | FOUND: |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1326 | /* Get the status of the process. */ |
| 1327 | if (!interrupted) { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1328 | ioctl_result = IOCTL_WSTOP (tcp); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1329 | ioctl_errno = errno; |
| 1330 | #ifndef HAVE_POLLABLE_PROCFS |
| 1331 | if (proc_poll_pipe[0] != -1) { |
| 1332 | if (ioctl_result < 0) |
| 1333 | kill(poller_pid, SIGKILL); |
| 1334 | else |
| 1335 | kill(poller_pid, SIGUSR1); |
| 1336 | } |
| 1337 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 1338 | } |
| 1339 | if (interrupted) |
| 1340 | return 0; |
| 1341 | |
| 1342 | if (interactive) |
| 1343 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
| 1344 | |
| 1345 | if (ioctl_result < 0) { |
| 1346 | /* Find out what happened if it failed. */ |
| 1347 | switch (ioctl_errno) { |
| 1348 | case EINTR: |
| 1349 | case EBADF: |
| 1350 | continue; |
| 1351 | case ENOENT: |
| 1352 | droptcb(tcp); |
| 1353 | continue; |
| 1354 | default: |
| 1355 | perror("PIOCWSTOP"); |
| 1356 | exit(1); |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | /* clear the just started flag */ |
| 1361 | tcp->flags &= ~TCB_STARTUP; |
| 1362 | |
| 1363 | /* set current output file */ |
| 1364 | outf = tcp->outf; |
| 1365 | |
| 1366 | if (cflag) { |
| 1367 | struct timeval stime; |
| 1368 | |
| 1369 | stime.tv_sec = tcp->status.pr_stime.tv_sec; |
| 1370 | stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000; |
| 1371 | tv_sub(&tcp->dtime, &stime, &tcp->stime); |
| 1372 | tcp->stime = stime; |
| 1373 | } |
| 1374 | |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1375 | what = tcp->status.PR_WHAT; |
| 1376 | switch (tcp->status.PR_WHY) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1377 | case PR_REQUESTED: |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1378 | if (tcp->status.PR_FLAGS & PR_ASLEEP) { |
| 1379 | tcp->status.PR_WHY = PR_SYSENTRY; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1380 | if (trace_syscall(tcp) < 0) { |
| 1381 | fprintf(stderr, "syscall trouble\n"); |
| 1382 | exit(1); |
| 1383 | } |
| 1384 | } |
| 1385 | break; |
| 1386 | case PR_SYSENTRY: |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 1387 | #ifdef POLL_HACK |
| 1388 | in_syscall = tcp; |
| 1389 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1390 | case PR_SYSEXIT: |
| 1391 | if (trace_syscall(tcp) < 0) { |
| 1392 | fprintf(stderr, "syscall trouble\n"); |
| 1393 | exit(1); |
| 1394 | } |
| 1395 | break; |
| 1396 | case PR_SIGNALLED: |
| 1397 | if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) { |
| 1398 | printleader(tcp); |
| 1399 | tprintf("--- %s (%s) ---", |
Nate Sammons | ce780fc | 1999-03-29 23:23:13 +0000 | [diff] [blame] | 1400 | signame(what), strsignal(what)); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1401 | printtrailer(tcp); |
| 1402 | } |
| 1403 | break; |
| 1404 | case PR_FAULTED: |
| 1405 | if (!cflag && (qual_flags[what] & QUAL_FAULT)) { |
| 1406 | printleader(tcp); |
| 1407 | tprintf("=== FAULT %d ===", what); |
| 1408 | printtrailer(tcp); |
| 1409 | } |
| 1410 | break; |
| 1411 | default: |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1412 | fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1413 | exit(1); |
| 1414 | break; |
| 1415 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1416 | arg = 0; |
| 1417 | if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1418 | perror("PIOCRUN"); |
| 1419 | exit(1); |
| 1420 | } |
| 1421 | } |
| 1422 | return 0; |
| 1423 | } |
| 1424 | |
| 1425 | #else /* !SVR4 */ |
| 1426 | |
| 1427 | static int |
| 1428 | trace() |
| 1429 | { |
| 1430 | int pid; |
| 1431 | int wait_errno; |
| 1432 | int status; |
| 1433 | struct tcb *tcp; |
| 1434 | #ifdef LINUX |
| 1435 | struct rusage ru; |
| 1436 | #endif /* LINUX */ |
| 1437 | |
| 1438 | while (nprocs != 0) { |
| 1439 | if (interactive) |
| 1440 | sigprocmask(SIG_SETMASK, &empty_set, NULL); |
| 1441 | #ifdef LINUX |
| 1442 | pid = wait4(-1, &status, 0, cflag ? &ru : NULL); |
| 1443 | #endif /* LINUX */ |
| 1444 | #ifdef SUNOS4 |
| 1445 | pid = wait(&status); |
| 1446 | #endif /* SUNOS4 */ |
| 1447 | wait_errno = errno; |
| 1448 | if (interactive) |
| 1449 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
| 1450 | |
| 1451 | if (interrupted) |
| 1452 | return 0; |
| 1453 | |
| 1454 | if (pid == -1) { |
| 1455 | switch (wait_errno) { |
| 1456 | case EINTR: |
| 1457 | continue; |
| 1458 | case ECHILD: |
| 1459 | /* |
| 1460 | * We would like to verify this case |
| 1461 | * but sometimes a race in Solbourne's |
| 1462 | * version of SunOS sometimes reports |
| 1463 | * ECHILD before sending us SIGCHILD. |
| 1464 | */ |
| 1465 | #if 0 |
| 1466 | if (nprocs == 0) |
| 1467 | return 0; |
| 1468 | fprintf(stderr, "strace: proc miscount\n"); |
| 1469 | exit(1); |
| 1470 | #endif |
| 1471 | return 0; |
| 1472 | default: |
| 1473 | errno = wait_errno; |
| 1474 | perror("strace: wait"); |
| 1475 | return -1; |
| 1476 | } |
| 1477 | } |
| 1478 | if (debug) |
| 1479 | fprintf(stderr, " [wait(%#x) = %u]\n", status, pid); |
| 1480 | |
| 1481 | /* Look up `pid' in our table. */ |
| 1482 | if ((tcp = pid2tcb(pid)) == NULL) { |
| 1483 | fprintf(stderr, "unknown pid: %u\n", pid); |
| 1484 | if (WIFSTOPPED(status)) |
| 1485 | ptrace(PTRACE_CONT, pid, (char *) 1, 0); |
| 1486 | exit(1); |
| 1487 | } |
| 1488 | /* set current output file */ |
| 1489 | outf = tcp->outf; |
| 1490 | if (cflag) { |
| 1491 | #ifdef LINUX |
| 1492 | tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime); |
| 1493 | tcp->stime = ru.ru_stime; |
| 1494 | #endif /* !LINUX */ |
| 1495 | } |
| 1496 | |
| 1497 | if (tcp->flags & TCB_SUSPENDED) { |
| 1498 | /* |
| 1499 | * Apparently, doing any ptrace() call on a stopped |
| 1500 | * process, provokes the kernel to report the process |
| 1501 | * status again on a subsequent wait(), even if the |
| 1502 | * process has not been actually restarted. |
| 1503 | * Since we have inspected the arguments of suspended |
| 1504 | * processes we end up here testing for this case. |
| 1505 | */ |
| 1506 | continue; |
| 1507 | } |
| 1508 | if (WIFSIGNALED(status)) { |
| 1509 | if (!cflag |
| 1510 | && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) { |
| 1511 | printleader(tcp); |
| 1512 | tprintf("+++ killed by %s +++", |
Nate Sammons | ce780fc | 1999-03-29 23:23:13 +0000 | [diff] [blame] | 1513 | signame(WTERMSIG(status))); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1514 | printtrailer(tcp); |
| 1515 | } |
| 1516 | droptcb(tcp); |
| 1517 | continue; |
| 1518 | } |
| 1519 | if (WIFEXITED(status)) { |
| 1520 | if (debug) |
| 1521 | fprintf(stderr, "pid %u exited\n", pid); |
| 1522 | if (tcp->flags & TCB_ATTACHED) |
| 1523 | fprintf(stderr, |
| 1524 | "PANIC: attached pid %u exited\n", |
| 1525 | pid); |
| 1526 | droptcb(tcp); |
| 1527 | continue; |
| 1528 | } |
| 1529 | if (!WIFSTOPPED(status)) { |
| 1530 | fprintf(stderr, "PANIC: pid %u not stopped\n", pid); |
| 1531 | droptcb(tcp); |
| 1532 | continue; |
| 1533 | } |
| 1534 | if (debug) |
| 1535 | fprintf(stderr, "pid %u stopped, [%s]\n", |
Nate Sammons | ce780fc | 1999-03-29 23:23:13 +0000 | [diff] [blame] | 1536 | pid, signame(WSTOPSIG(status))); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1537 | |
| 1538 | if (tcp->flags & TCB_STARTUP) { |
| 1539 | /* |
| 1540 | * This flag is there to keep us in sync. |
| 1541 | * Next time this process stops it should |
| 1542 | * really be entering a system call. |
| 1543 | */ |
| 1544 | tcp->flags &= ~TCB_STARTUP; |
| 1545 | if (tcp->flags & TCB_ATTACHED) { |
| 1546 | /* |
| 1547 | * Interestingly, the process may stop |
| 1548 | * with STOPSIG equal to some other signal |
| 1549 | * than SIGSTOP if we happend to attach |
| 1550 | * just before the process takes a signal. |
| 1551 | */ |
| 1552 | if (!WIFSTOPPED(status)) { |
| 1553 | fprintf(stderr, |
| 1554 | "pid %u not stopped\n", pid); |
| 1555 | detach(tcp, WSTOPSIG(status)); |
| 1556 | continue; |
| 1557 | } |
| 1558 | } |
| 1559 | else { |
| 1560 | #ifdef SUNOS4 |
| 1561 | /* A child of us stopped at exec */ |
| 1562 | if (WSTOPSIG(status) == SIGTRAP && followvfork) |
| 1563 | fixvfork(tcp); |
| 1564 | #endif /* SUNOS4 */ |
| 1565 | } |
| 1566 | if (tcp->flags & TCB_BPTSET) { |
| 1567 | if (clearbpt(tcp) < 0) /* Pretty fatal */ { |
| 1568 | droptcb(tcp); |
| 1569 | cleanup(); |
| 1570 | return -1; |
| 1571 | } |
| 1572 | } |
| 1573 | goto tracing; |
| 1574 | } |
| 1575 | |
| 1576 | if (WSTOPSIG(status) != SIGTRAP) { |
| 1577 | if (WSTOPSIG(status) == SIGSTOP && |
| 1578 | (tcp->flags & TCB_SIGTRAPPED)) { |
| 1579 | /* |
| 1580 | * Trapped attempt to block SIGTRAP |
| 1581 | * Hope we are back in control now. |
| 1582 | */ |
| 1583 | tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED); |
| 1584 | if (ptrace(PTRACE_SYSCALL, |
| 1585 | pid, (char *) 1, 0) < 0) { |
| 1586 | perror("trace: ptrace(PTRACE_SYSCALL, ...)"); |
| 1587 | cleanup(); |
| 1588 | return -1; |
| 1589 | } |
| 1590 | continue; |
| 1591 | } |
| 1592 | if (!cflag |
| 1593 | && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) { |
| 1594 | printleader(tcp); |
| 1595 | tprintf("--- %s (%s) ---", |
Nate Sammons | ce780fc | 1999-03-29 23:23:13 +0000 | [diff] [blame] | 1596 | signame(WSTOPSIG(status)), |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1597 | strsignal(WSTOPSIG(status))); |
| 1598 | printtrailer(tcp); |
| 1599 | } |
| 1600 | if ((tcp->flags & TCB_ATTACHED) && |
| 1601 | !sigishandled(tcp, WSTOPSIG(status))) { |
| 1602 | detach(tcp, WSTOPSIG(status)); |
| 1603 | continue; |
| 1604 | } |
| 1605 | if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, |
| 1606 | WSTOPSIG(status)) < 0) { |
| 1607 | perror("trace: ptrace(PTRACE_SYSCALL, ...)"); |
| 1608 | cleanup(); |
| 1609 | return -1; |
| 1610 | } |
| 1611 | tcp->flags &= ~TCB_SUSPENDED; |
| 1612 | continue; |
| 1613 | } |
| 1614 | if (trace_syscall(tcp) < 0) { |
| 1615 | if (tcp->flags & TCB_ATTACHED) |
| 1616 | detach(tcp, 0); |
| 1617 | else { |
| 1618 | ptrace(PTRACE_KILL, |
| 1619 | tcp->pid, (char *) 1, SIGTERM); |
| 1620 | droptcb(tcp); |
| 1621 | } |
| 1622 | continue; |
| 1623 | } |
| 1624 | if (tcp->flags & TCB_EXITING) { |
| 1625 | if (tcp->flags & TCB_ATTACHED) |
| 1626 | detach(tcp, 0); |
| 1627 | else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) { |
| 1628 | perror("strace: ptrace(PTRACE_CONT, ...)"); |
| 1629 | cleanup(); |
| 1630 | return -1; |
| 1631 | } |
| 1632 | continue; |
| 1633 | } |
| 1634 | if (tcp->flags & TCB_SUSPENDED) { |
| 1635 | if (!qflag) |
| 1636 | fprintf(stderr, "Process %u suspended\n", pid); |
| 1637 | continue; |
| 1638 | } |
| 1639 | tracing: |
| 1640 | if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) { |
| 1641 | perror("trace: ptrace(PTRACE_SYSCALL, ...)"); |
| 1642 | cleanup(); |
| 1643 | return -1; |
| 1644 | } |
| 1645 | } |
| 1646 | return 0; |
| 1647 | } |
| 1648 | |
| 1649 | #endif /* !SVR4 */ |
| 1650 | |
| 1651 | static int curcol; |
| 1652 | |
| 1653 | #ifdef __STDC__ |
| 1654 | #include <stdarg.h> |
| 1655 | #define VA_START(a, b) va_start(a, b) |
| 1656 | #else |
| 1657 | #include <varargs.h> |
| 1658 | #define VA_START(a, b) va_start(a) |
| 1659 | #endif |
| 1660 | |
| 1661 | void |
| 1662 | #ifdef __STDC__ |
| 1663 | tprintf(const char *fmt, ...) |
| 1664 | #else |
| 1665 | tprintf(fmt, va_alist) |
| 1666 | char *fmt; |
| 1667 | va_dcl |
| 1668 | #endif |
| 1669 | { |
| 1670 | va_list args; |
| 1671 | |
| 1672 | VA_START(args, fmt); |
| 1673 | if (outf) |
| 1674 | curcol += vfprintf(outf, fmt, args); |
| 1675 | va_end(args); |
| 1676 | return; |
| 1677 | } |
| 1678 | |
| 1679 | void |
| 1680 | printleader(tcp) |
| 1681 | struct tcb *tcp; |
| 1682 | { |
| 1683 | if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) { |
| 1684 | tcp_last->flags |= TCB_REPRINT; |
| 1685 | tprintf(" <unfinished ...>\n"); |
| 1686 | } |
| 1687 | curcol = 0; |
| 1688 | if ((followfork == 1 || pflag_seen > 1) && outfname) |
| 1689 | tprintf("%-5d ", tcp->pid); |
| 1690 | else if (nprocs > 1 && !outfname) |
| 1691 | tprintf("[pid %5u] ", tcp->pid); |
| 1692 | if (tflag) { |
| 1693 | char str[sizeof("HH:MM:SS")]; |
| 1694 | struct timeval tv, dtv; |
| 1695 | static struct timeval otv; |
| 1696 | |
| 1697 | gettimeofday(&tv, NULL); |
| 1698 | if (rflag) { |
| 1699 | if (otv.tv_sec == 0) |
| 1700 | otv = tv; |
| 1701 | tv_sub(&dtv, &tv, &otv); |
| 1702 | tprintf("%6ld.%06ld ", |
| 1703 | (long) dtv.tv_sec, (long) dtv.tv_usec); |
| 1704 | otv = tv; |
| 1705 | } |
| 1706 | else if (tflag > 2) { |
| 1707 | tprintf("%ld.%06ld ", |
| 1708 | (long) tv.tv_sec, (long) tv.tv_usec); |
| 1709 | } |
| 1710 | else { |
| 1711 | time_t local = tv.tv_sec; |
| 1712 | strftime(str, sizeof(str), "%T", localtime(&local)); |
| 1713 | if (tflag > 1) |
| 1714 | tprintf("%s.%06ld ", str, (long) tv.tv_usec); |
| 1715 | else |
| 1716 | tprintf("%s ", str); |
| 1717 | } |
| 1718 | } |
| 1719 | if (iflag) |
| 1720 | printcall(tcp); |
| 1721 | } |
| 1722 | |
| 1723 | void |
| 1724 | tabto(col) |
| 1725 | int col; |
| 1726 | { |
| 1727 | if (curcol < col) |
| 1728 | tprintf("%*s", col - curcol, ""); |
| 1729 | } |
| 1730 | |
| 1731 | void |
| 1732 | printtrailer(tcp) |
| 1733 | struct tcb *tcp; |
| 1734 | { |
| 1735 | tprintf("\n"); |
| 1736 | tcp_last = NULL; |
| 1737 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1738 | |
| 1739 | #ifdef SVR4_MP |
| 1740 | |
| 1741 | int mp_ioctl (int fd, int cmd, void *arg, int size) { |
| 1742 | |
| 1743 | struct iovec iov[2]; |
| 1744 | int n = 1; |
| 1745 | |
| 1746 | iov[0].iov_base = &cmd; |
| 1747 | iov[0].iov_len = sizeof cmd; |
| 1748 | if (arg) { |
| 1749 | ++n; |
| 1750 | iov[1].iov_base = arg; |
| 1751 | iov[1].iov_len = size; |
| 1752 | } |
| 1753 | |
| 1754 | return writev (fd, iov, n); |
| 1755 | } |
| 1756 | |
| 1757 | #endif |