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