strace.c revision d0ffdf494ac722f3d121c6e807f105000409e6a6
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 31#include "defs.h" 32#include <stdarg.h> 33#include <sys/param.h> 34#include <fcntl.h> 35#include <sys/resource.h> 36#include <sys/wait.h> 37#include <sys/stat.h> 38#include <pwd.h> 39#include <grp.h> 40#include <dirent.h> 41#include <sys/utsname.h> 42#ifdef HAVE_PRCTL 43# include <sys/prctl.h> 44#endif 45#if defined(IA64) 46# include <asm/ptrace_offsets.h> 47#endif 48/* In some libc, these aren't declared. Do it ourself: */ 49extern char **environ; 50extern int optind; 51extern char *optarg; 52 53 54#if defined __NR_tkill 55# define my_tkill(tid, sig) syscall(__NR_tkill, (tid), (sig)) 56#else 57 /* kill() may choose arbitrarily the target task of the process group 58 while we later wait on a that specific TID. PID process waits become 59 TID task specific waits for a process under ptrace(2). */ 60# warning "tkill(2) not available, risk of strace hangs!" 61# define my_tkill(tid, sig) kill((tid), (sig)) 62#endif 63 64/* Glue for systems without a MMU that cannot provide fork() */ 65#if !defined(HAVE_FORK) 66# undef NOMMU_SYSTEM 67# define NOMMU_SYSTEM 1 68#endif 69#if NOMMU_SYSTEM 70# define fork() vfork() 71#endif 72 73cflag_t cflag = CFLAG_NONE; 74unsigned int followfork = 0; 75unsigned int ptrace_setoptions = 0; 76unsigned int xflag = 0; 77bool need_fork_exec_workarounds = 0; 78bool debug_flag = 0; 79bool Tflag = 0; 80unsigned int qflag = 0; 81/* Which WSTOPSIG(status) value marks syscall traps? */ 82static unsigned int syscall_trap_sig = SIGTRAP; 83static unsigned int tflag = 0; 84static bool iflag = 0; 85static bool rflag = 0; 86static bool print_pid_pfx = 0; 87 88/* -I n */ 89enum { 90 INTR_NOT_SET = 0, 91 INTR_ANYWHERE = 1, /* don't block/ignore any signals */ 92 INTR_WHILE_WAIT = 2, /* block fatal signals while decoding syscall. default */ 93 INTR_NEVER = 3, /* block fatal signals. default if '-o FILE PROG' */ 94 INTR_BLOCK_TSTP_TOO = 4, /* block fatal signals and SIGTSTP (^Z) */ 95 NUM_INTR_OPTS 96}; 97static int opt_intr; 98/* We play with signal mask only if this mode is active: */ 99#define interactive (opt_intr == INTR_WHILE_WAIT) 100 101/* 102 * daemonized_tracer supports -D option. 103 * With this option, strace forks twice. 104 * Unlike normal case, with -D *grandparent* process exec's, 105 * becoming a traced process. Child exits (this prevents traced process 106 * from having children it doesn't expect to have), and grandchild 107 * attaches to grandparent similarly to strace -p PID. 108 * This allows for more transparent interaction in cases 109 * when process and its parent are communicating via signals, 110 * wait() etc. Without -D, strace process gets lodged in between, 111 * disrupting parent<->child link. 112 */ 113static bool daemonized_tracer = 0; 114 115#if USE_SEIZE 116static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP; 117# define use_seize (post_attach_sigstop == 0) 118#else 119# define post_attach_sigstop TCB_IGNORE_ONE_SIGSTOP 120# define use_seize 0 121#endif 122 123/* Sometimes we want to print only succeeding syscalls. */ 124bool not_failing_only = 0; 125 126/* Show path associated with fd arguments */ 127bool show_fd_path = 0; 128 129static bool detach_on_execve = 0; 130/* Are we "strace PROG" and need to skip detach on first execve? */ 131static bool skip_one_b_execve = 0; 132/* Are we "strace PROG" and need to hide everything until execve? */ 133bool hide_log_until_execve = 0; 134 135static int exit_code = 0; 136static int strace_child = 0; 137static int strace_tracer_pid = 0; 138 139static char *username = NULL; 140static uid_t run_uid; 141static gid_t run_gid; 142 143unsigned int max_strlen = DEFAULT_STRLEN; 144static int acolumn = DEFAULT_ACOLUMN; 145static char *acolumn_spaces; 146 147static char *outfname = NULL; 148/* If -ff, points to stderr. Else, it's our common output log */ 149static FILE *shared_log; 150 151struct tcb *printing_tcp = NULL; 152static struct tcb *current_tcp; 153 154static struct tcb **tcbtab; 155static unsigned int nprocs, tcbtabsize; 156static const char *progname; 157 158unsigned os_release; /* generated from uname()'s u.release */ 159 160static void detach(struct tcb *tcp); 161static void cleanup(void); 162static void interrupt(int sig); 163static sigset_t empty_set, blocked_set; 164 165#ifdef HAVE_SIG_ATOMIC_T 166static volatile sig_atomic_t interrupted; 167#else 168static volatile int interrupted; 169#endif 170 171#ifndef HAVE_STRERROR 172 173#if !HAVE_DECL_SYS_ERRLIST 174extern int sys_nerr; 175extern char *sys_errlist[]; 176#endif 177 178const char * 179strerror(int err_no) 180{ 181 static char buf[sizeof("Unknown error %d") + sizeof(int)*3]; 182 183 if (err_no < 1 || err_no >= sys_nerr) { 184 sprintf(buf, "Unknown error %d", err_no); 185 return buf; 186 } 187 return sys_errlist[err_no]; 188} 189 190#endif /* HAVE_STERRROR */ 191 192static void 193usage(FILE *ofp, int exitval) 194{ 195 fprintf(ofp, "\ 196usage: strace [-CdffhiqrtttTvVxxy] [-I n] [-e expr]...\n\ 197 [-a column] [-o file] [-s strsize] [-P path]...\n\ 198 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\ 199 or: strace -c[df] [-I n] [-e expr]... [-O overhead] [-S sortby]\n\ 200 -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\ 201-c -- count time, calls, and errors for each syscall and report summary\n\ 202-C -- like -c but also print regular output\n\ 203-d -- enable debug output to stderr\n\ 204-D -- run tracer process as a detached grandchild, not as parent\n\ 205-f -- follow forks, -ff -- with output into separate files\n\ 206-i -- print instruction pointer at time of syscall\n\ 207-q -- suppress messages about attaching, detaching, etc.\n\ 208-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\ 209-T -- print time spent in each syscall\n\ 210-v -- verbose mode: print unabbreviated argv, stat, termios, etc. args\n\ 211-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\ 212-y -- print paths associated with file descriptor arguments\n\ 213-h -- print help message, -V -- print version\n\ 214-a column -- alignment COLUMN for printing syscall results (default %d)\n\ 215-b execve -- detach on this syscall\n\ 216-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\ 217 options: trace, abbrev, verbose, raw, signal, read, write\n\ 218-I interruptible --\n\ 219 1: no signals are blocked\n\ 220 2: fatal signals are blocked while decoding syscall (default)\n\ 221 3: fatal signals are always blocked (default if '-o FILE PROG')\n\ 222 4: fatal signals and SIGTSTP (^Z) are always blocked\n\ 223 (useful to make 'strace -o FILE PROG' not stop on ^Z)\n\ 224-o file -- send trace output to FILE instead of stderr\n\ 225-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\ 226-p pid -- trace process with process id PID, may be repeated\n\ 227-s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\ 228-S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\ 229-u username -- run command as username handling setuid and/or setgid\n\ 230-E var=val -- put var=val in the environment for command\n\ 231-E var -- remove var from the environment for command\n\ 232-P path -- trace accesses to path\n\ 233" 234/* ancient, no one should use it 235-F -- attempt to follow vforks (deprecated, use -f)\n\ 236 */ 237/* this is broken, so don't document it 238-z -- print only succeeding syscalls\n\ 239 */ 240, DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY); 241 exit(exitval); 242} 243 244static void die(void) __attribute__ ((noreturn)); 245static void die(void) 246{ 247 if (strace_tracer_pid == getpid()) { 248 cflag = 0; 249 cleanup(); 250 } 251 exit(1); 252} 253 254static void verror_msg(int err_no, const char *fmt, va_list p) 255{ 256 char *msg; 257 258 fflush(NULL); 259 260 /* We want to print entire message with single fprintf to ensure 261 * message integrity if stderr is shared with other programs. 262 * Thus we use vasprintf + single fprintf. 263 */ 264 msg = NULL; 265 if (vasprintf(&msg, fmt, p) >= 0) { 266 if (err_no) 267 fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(err_no)); 268 else 269 fprintf(stderr, "%s: %s\n", progname, msg); 270 free(msg); 271 } else { 272 /* malloc in vasprintf failed, try it without malloc */ 273 fprintf(stderr, "%s: ", progname); 274 vfprintf(stderr, fmt, p); 275 if (err_no) 276 fprintf(stderr, ": %s\n", strerror(err_no)); 277 else 278 putc('\n', stderr); 279 } 280 /* We don't switch stderr to buffered, thus fprintf(stderr) 281 * always flushes its output and this is not necessary: */ 282 /* fflush(stderr); */ 283} 284 285void error_msg(const char *fmt, ...) 286{ 287 va_list p; 288 va_start(p, fmt); 289 verror_msg(0, fmt, p); 290 va_end(p); 291} 292 293void error_msg_and_die(const char *fmt, ...) 294{ 295 va_list p; 296 va_start(p, fmt); 297 verror_msg(0, fmt, p); 298 die(); 299} 300 301void perror_msg(const char *fmt, ...) 302{ 303 va_list p; 304 va_start(p, fmt); 305 verror_msg(errno, fmt, p); 306 va_end(p); 307} 308 309void perror_msg_and_die(const char *fmt, ...) 310{ 311 va_list p; 312 va_start(p, fmt); 313 verror_msg(errno, fmt, p); 314 die(); 315} 316 317void die_out_of_memory(void) 318{ 319 static bool recursed = 0; 320 if (recursed) 321 exit(1); 322 recursed = 1; 323 error_msg_and_die("Out of memory"); 324} 325 326static void 327error_opt_arg(int opt, const char *arg) 328{ 329 error_msg_and_die("Invalid -%c argument: '%s'", opt, arg); 330} 331 332#if USE_SEIZE 333static int 334ptrace_attach_or_seize(int pid) 335{ 336 int r; 337 if (!use_seize) 338 return ptrace(PTRACE_ATTACH, pid, 0, 0); 339 r = ptrace(PTRACE_SEIZE, pid, 0, 0); 340 if (r) 341 return r; 342 r = ptrace(PTRACE_INTERRUPT, pid, 0, 0); 343 return r; 344} 345#else 346# define ptrace_attach_or_seize(pid) ptrace(PTRACE_ATTACH, (pid), 0, 0) 347#endif 348 349/* 350 * Used when we want to unblock stopped traced process. 351 * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL. 352 * Returns 0 on success or if error was ESRCH 353 * (presumably process was killed while we talk to it). 354 * Otherwise prints error message and returns -1. 355 */ 356static int 357ptrace_restart(int op, struct tcb *tcp, int sig) 358{ 359 int err; 360 const char *msg; 361 362 errno = 0; 363 ptrace(op, tcp->pid, (void *) 0, (long) sig); 364 err = errno; 365 if (!err) 366 return 0; 367 368 msg = "SYSCALL"; 369 if (op == PTRACE_CONT) 370 msg = "CONT"; 371 if (op == PTRACE_DETACH) 372 msg = "DETACH"; 373#ifdef PTRACE_LISTEN 374 if (op == PTRACE_LISTEN) 375 msg = "LISTEN"; 376#endif 377 /* 378 * Why curcol != 0? Otherwise sometimes we get this: 379 * 380 * 10252 kill(10253, SIGKILL) = 0 381 * <ptrace(SYSCALL,10252):No such process>10253 ...next decode... 382 * 383 * 10252 died after we retrieved syscall exit data, 384 * but before we tried to restart it. Log looks ugly. 385 */ 386 if (current_tcp && current_tcp->curcol != 0) { 387 tprintf(" <ptrace(%s):%s>\n", msg, strerror(err)); 388 line_ended(); 389 } 390 if (err == ESRCH) 391 return 0; 392 errno = err; 393 perror_msg("ptrace(PTRACE_%s,pid:%d,sig:%d)", msg, tcp->pid, sig); 394 return -1; 395} 396 397static void 398set_cloexec_flag(int fd) 399{ 400 int flags, newflags; 401 402 flags = fcntl(fd, F_GETFD); 403 if (flags < 0) { 404 /* Can happen only if fd is bad. 405 * Should never happen: if it does, we have a bug 406 * in the caller. Therefore we just abort 407 * instead of propagating the error. 408 */ 409 perror_msg_and_die("fcntl(%d, F_GETFD)", fd); 410 } 411 412 newflags = flags | FD_CLOEXEC; 413 if (flags == newflags) 414 return; 415 416 fcntl(fd, F_SETFD, newflags); /* never fails */ 417} 418 419static void kill_save_errno(pid_t pid, int sig) 420{ 421 int saved_errno = errno; 422 423 (void) kill(pid, sig); 424 errno = saved_errno; 425} 426 427/* 428 * When strace is setuid executable, we have to swap uids 429 * before and after filesystem and process management operations. 430 */ 431static void 432swap_uid(void) 433{ 434 int euid = geteuid(), uid = getuid(); 435 436 if (euid != uid && setreuid(euid, uid) < 0) { 437 perror_msg_and_die("setreuid"); 438 } 439} 440 441#if _LFS64_LARGEFILE 442# define fopen_for_output fopen64 443# define struct_stat struct stat64 444# define stat_file stat64 445# define struct_dirent struct dirent64 446# define read_dir readdir64 447# define struct_rlimit struct rlimit64 448# define set_rlimit setrlimit64 449#else 450# define fopen_for_output fopen 451# define struct_stat struct stat 452# define stat_file stat 453# define struct_dirent struct dirent 454# define read_dir readdir 455# define struct_rlimit struct rlimit 456# define set_rlimit setrlimit 457#endif 458 459static FILE * 460strace_fopen(const char *path) 461{ 462 FILE *fp; 463 464 swap_uid(); 465 fp = fopen_for_output(path, "w"); 466 if (!fp) 467 perror_msg_and_die("Can't fopen '%s'", path); 468 swap_uid(); 469 set_cloexec_flag(fileno(fp)); 470 return fp; 471} 472 473static int popen_pid = 0; 474 475#ifndef _PATH_BSHELL 476# define _PATH_BSHELL "/bin/sh" 477#endif 478 479/* 480 * We cannot use standard popen(3) here because we have to distinguish 481 * popen child process from other processes we trace, and standard popen(3) 482 * does not export its child's pid. 483 */ 484static FILE * 485strace_popen(const char *command) 486{ 487 FILE *fp; 488 int fds[2]; 489 490 swap_uid(); 491 if (pipe(fds) < 0) 492 perror_msg_and_die("pipe"); 493 494 set_cloexec_flag(fds[1]); /* never fails */ 495 496 popen_pid = vfork(); 497 if (popen_pid == -1) 498 perror_msg_and_die("vfork"); 499 500 if (popen_pid == 0) { 501 /* child */ 502 close(fds[1]); 503 if (fds[0] != 0) { 504 if (dup2(fds[0], 0)) 505 perror_msg_and_die("dup2"); 506 close(fds[0]); 507 } 508 execl(_PATH_BSHELL, "sh", "-c", command, NULL); 509 perror_msg_and_die("Can't execute '%s'", _PATH_BSHELL); 510 } 511 512 /* parent */ 513 close(fds[0]); 514 swap_uid(); 515 fp = fdopen(fds[1], "w"); 516 if (!fp) 517 die_out_of_memory(); 518 return fp; 519} 520 521void 522tprintf(const char *fmt, ...) 523{ 524 va_list args; 525 526 va_start(args, fmt); 527 if (current_tcp) { 528 int n = strace_vfprintf(current_tcp->outf, fmt, args); 529 if (n < 0) { 530 if (current_tcp->outf != stderr) 531 perror_msg("%s", outfname); 532 } else 533 current_tcp->curcol += n; 534 } 535 va_end(args); 536} 537 538void 539tprints(const char *str) 540{ 541 if (current_tcp) { 542 int n = fputs_unlocked(str, current_tcp->outf); 543 if (n >= 0) { 544 current_tcp->curcol += strlen(str); 545 return; 546 } 547 if (current_tcp->outf != stderr) 548 perror_msg("%s", outfname); 549 } 550} 551 552void 553line_ended(void) 554{ 555 if (current_tcp) { 556 current_tcp->curcol = 0; 557 fflush(current_tcp->outf); 558 } 559 if (printing_tcp) { 560 printing_tcp->curcol = 0; 561 printing_tcp = NULL; 562 } 563} 564 565void 566printleader(struct tcb *tcp) 567{ 568 /* If -ff, "previous tcb we printed" is always the same as current, 569 * because we have per-tcb output files. 570 */ 571 if (followfork >= 2) 572 printing_tcp = tcp; 573 574 if (printing_tcp) { 575 current_tcp = printing_tcp; 576 if (printing_tcp->curcol != 0 && (followfork < 2 || printing_tcp == tcp)) { 577 /* 578 * case 1: we have a shared log (i.e. not -ff), and last line 579 * wasn't finished (same or different tcb, doesn't matter). 580 * case 2: split log, we are the same tcb, but our last line 581 * didn't finish ("SIGKILL nuked us after syscall entry" etc). 582 */ 583 tprints(" <unfinished ...>\n"); 584 printing_tcp->curcol = 0; 585 } 586 } 587 588 printing_tcp = tcp; 589 current_tcp = tcp; 590 current_tcp->curcol = 0; 591 592 if (print_pid_pfx) 593 tprintf("%-5d ", tcp->pid); 594 else if (nprocs > 1 && !outfname) 595 tprintf("[pid %5u] ", tcp->pid); 596 597 if (tflag) { 598 char str[sizeof("HH:MM:SS")]; 599 struct timeval tv, dtv; 600 static struct timeval otv; 601 602 gettimeofday(&tv, NULL); 603 if (rflag) { 604 if (otv.tv_sec == 0) 605 otv = tv; 606 tv_sub(&dtv, &tv, &otv); 607 tprintf("%6ld.%06ld ", 608 (long) dtv.tv_sec, (long) dtv.tv_usec); 609 otv = tv; 610 } 611 else if (tflag > 2) { 612 tprintf("%ld.%06ld ", 613 (long) tv.tv_sec, (long) tv.tv_usec); 614 } 615 else { 616 time_t local = tv.tv_sec; 617 strftime(str, sizeof(str), "%T", localtime(&local)); 618 if (tflag > 1) 619 tprintf("%s.%06ld ", str, (long) tv.tv_usec); 620 else 621 tprintf("%s ", str); 622 } 623 } 624 if (iflag) 625 print_pc(tcp); 626} 627 628void 629tabto(void) 630{ 631 if (current_tcp->curcol < acolumn) 632 tprints(acolumn_spaces + current_tcp->curcol); 633} 634 635/* Should be only called directly *after successful attach* to a tracee. 636 * Otherwise, "strace -oFILE -ff -p<nonexistant_pid>" 637 * may create bogus empty FILE.<nonexistant_pid>, and then die. 638 */ 639static void 640newoutf(struct tcb *tcp) 641{ 642 tcp->outf = shared_log; /* if not -ff mode, the same file is for all */ 643 if (followfork >= 2) { 644 char name[520 + sizeof(int) * 3]; 645 sprintf(name, "%.512s.%u", outfname, tcp->pid); 646 tcp->outf = strace_fopen(name); 647 } 648} 649 650static void 651expand_tcbtab(void) 652{ 653 /* Allocate some more TCBs and expand the table. 654 We don't want to relocate the TCBs because our 655 callers have pointers and it would be a pain. 656 So tcbtab is a table of pointers. Since we never 657 free the TCBs, we allocate a single chunk of many. */ 658 int i = tcbtabsize; 659 struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0])); 660 struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0])); 661 if (!newtab || !newtcbs) 662 die_out_of_memory(); 663 tcbtabsize *= 2; 664 tcbtab = newtab; 665 while (i < tcbtabsize) 666 tcbtab[i++] = newtcbs++; 667} 668 669static struct tcb * 670alloctcb(int pid) 671{ 672 int i; 673 struct tcb *tcp; 674 675 if (nprocs == tcbtabsize) 676 expand_tcbtab(); 677 678 for (i = 0; i < tcbtabsize; i++) { 679 tcp = tcbtab[i]; 680 if (!tcp->pid) { 681 memset(tcp, 0, sizeof(*tcp)); 682 tcp->pid = pid; 683#if SUPPORTED_PERSONALITIES > 1 684 tcp->currpers = current_personality; 685#endif 686 nprocs++; 687 if (debug_flag) 688 fprintf(stderr, "new tcb for pid %d, active tcbs:%d\n", tcp->pid, nprocs); 689 return tcp; 690 } 691 } 692 error_msg_and_die("bug in alloctcb"); 693} 694 695static void 696droptcb(struct tcb *tcp) 697{ 698 if (tcp->pid == 0) 699 return; 700 701 nprocs--; 702 if (debug_flag) 703 fprintf(stderr, "dropped tcb for pid %d, %d remain\n", tcp->pid, nprocs); 704 705 if (tcp->outf) { 706 if (followfork >= 2) { 707 if (tcp->curcol != 0) 708 fprintf(tcp->outf, " <detached ...>\n"); 709 fclose(tcp->outf); 710 } else { 711 if (printing_tcp == tcp && tcp->curcol != 0) 712 fprintf(tcp->outf, " <detached ...>\n"); 713 fflush(tcp->outf); 714 } 715 } 716 717 if (current_tcp == tcp) 718 current_tcp = NULL; 719 if (printing_tcp == tcp) 720 printing_tcp = NULL; 721 722 memset(tcp, 0, sizeof(*tcp)); 723} 724 725/* Detach traced process. 726 * Never call DETACH twice on the same process as both unattached and 727 * attached-unstopped processes give the same ESRCH. For unattached process we 728 * would SIGSTOP it and wait for its SIGSTOP notification forever. 729 */ 730static void 731detach(struct tcb *tcp) 732{ 733 int error; 734 int status; 735 736 if (tcp->flags & TCB_BPTSET) 737 clearbpt(tcp); 738 739 /* 740 * Linux wrongly insists the child be stopped 741 * before detaching. Arghh. We go through hoops 742 * to make a clean break of things. 743 */ 744#if defined(SPARC) 745# undef PTRACE_DETACH 746# define PTRACE_DETACH PTRACE_SUNDETACH 747#endif 748 749 if (!(tcp->flags & TCB_ATTACHED)) 750 goto drop; 751 752 /* We attached but possibly didn't see the expected SIGSTOP. 753 * We must catch exactly one as otherwise the detached process 754 * would be left stopped (process state T). 755 */ 756 if (tcp->flags & TCB_IGNORE_ONE_SIGSTOP) 757 goto wait_loop; 758 759 error = ptrace(PTRACE_DETACH, tcp->pid, 0, 0); 760 if (!error) { 761 /* On a clear day, you can see forever. */ 762 goto drop; 763 } 764 if (errno != ESRCH) { 765 /* Shouldn't happen. */ 766 perror_msg("detach: ptrace(PTRACE_DETACH,%u)", tcp->pid); 767 goto drop; 768 } 769 /* ESRCH: process is either not stopped or doesn't exist. */ 770 if (my_tkill(tcp->pid, 0) < 0) { 771 if (errno != ESRCH) 772 /* Shouldn't happen. */ 773 perror_msg("detach: tkill(%u,0)", tcp->pid); 774 /* else: process doesn't exist. */ 775 goto drop; 776 } 777 /* Process is not stopped, need to stop it. */ 778 if (use_seize) { 779 /* 780 * With SEIZE, tracee can be in group-stop already. 781 * In this state sending it another SIGSTOP does nothing. 782 * Need to use INTERRUPT. 783 * Testcase: trying to ^C a "strace -p <stopped_process>". 784 */ 785 error = ptrace(PTRACE_INTERRUPT, tcp->pid, 0, 0); 786 if (!error) 787 goto wait_loop; 788 if (errno != ESRCH) 789 perror_msg("detach: ptrace(PTRACE_INTERRUPT,%u)", tcp->pid); 790 } 791 else { 792 error = my_tkill(tcp->pid, SIGSTOP); 793 if (!error) 794 goto wait_loop; 795 if (errno != ESRCH) 796 perror_msg("detach: tkill(%u,SIGSTOP)", tcp->pid); 797 } 798 /* Either process doesn't exist, or some weird error. */ 799 goto drop; 800 801 wait_loop: 802 /* We end up here in three cases: 803 * 1. We sent PTRACE_INTERRUPT (use_seize case) 804 * 2. We sent SIGSTOP (!use_seize) 805 * 3. Attach SIGSTOP was already pending (TCB_IGNORE_ONE_SIGSTOP set) 806 */ 807 for (;;) { 808 int sig; 809 if (waitpid(tcp->pid, &status, __WALL) < 0) { 810 if (errno == EINTR) 811 continue; 812 /* 813 * if (errno == ECHILD) break; 814 * ^^^ WRONG! We expect this PID to exist, 815 * and want to emit a message otherwise: 816 */ 817 perror_msg("detach: waitpid(%u)", tcp->pid); 818 break; 819 } 820 if (!WIFSTOPPED(status)) { 821 /* 822 * Tracee exited or was killed by signal. 823 * We shouldn't normally reach this place: 824 * we don't want to consume exit status. 825 * Consider "strace -p PID" being ^C-ed: 826 * we want merely to detach from PID. 827 * 828 * However, we _can_ end up here if tracee 829 * was SIGKILLed. 830 */ 831 break; 832 } 833 sig = WSTOPSIG(status); 834 if (debug_flag) 835 fprintf(stderr, "detach wait: event:%d sig:%d\n", 836 (unsigned)status >> 16, sig); 837 if (use_seize) { 838 unsigned event = (unsigned)status >> 16; 839 if (event == PTRACE_EVENT_STOP /*&& sig == SIGTRAP*/) { 840 /* 841 * sig == SIGTRAP: PTRACE_INTERRUPT stop. 842 * sig == other: process was already stopped 843 * with this stopping sig (see tests/detach-stopped). 844 * Looks like re-injecting this sig is not necessary 845 * in DETACH for the tracee to remain stopped. 846 */ 847 sig = 0; 848 } 849 /* 850 * PTRACE_INTERRUPT is not guaranteed to produce 851 * the above event if other ptrace-stop is pending. 852 * See tests/detach-sleeping testcase: 853 * strace got SIGINT while tracee is sleeping. 854 * We sent PTRACE_INTERRUPT. 855 * We see syscall exit, not PTRACE_INTERRUPT stop. 856 * We won't get PTRACE_INTERRUPT stop 857 * if we would CONT now. Need to DETACH. 858 */ 859 if (sig == syscall_trap_sig) 860 sig = 0; 861 /* else: not sure in which case we can be here. 862 * Signal stop? Inject it while detaching. 863 */ 864 ptrace_restart(PTRACE_DETACH, tcp, sig); 865 break; 866 } 867 /* Note: this check has to be after use_seize check */ 868 /* (else, in use_seize case SIGSTOP will be mistreated) */ 869 if (sig == SIGSTOP) { 870 /* Detach, suppressing SIGSTOP */ 871 ptrace_restart(PTRACE_DETACH, tcp, 0); 872 break; 873 } 874 if (sig == syscall_trap_sig) 875 sig = 0; 876 /* Can't detach just yet, may need to wait for SIGSTOP */ 877 error = ptrace_restart(PTRACE_CONT, tcp, sig); 878 if (error < 0) { 879 /* Should not happen. 880 * Note: ptrace_restart returns 0 on ESRCH, so it's not it. 881 * ptrace_restart already emitted error message. 882 */ 883 break; 884 } 885 } 886 887 drop: 888 if (!qflag && (tcp->flags & TCB_ATTACHED)) 889 fprintf(stderr, "Process %u detached\n", tcp->pid); 890 891 droptcb(tcp); 892} 893 894static void 895process_opt_p_list(char *opt) 896{ 897 while (*opt) { 898 /* 899 * We accept -p PID,PID; -p "`pidof PROG`"; -p "`pgrep PROG`". 900 * pidof uses space as delim, pgrep uses newline. :( 901 */ 902 int pid; 903 char *delim = opt + strcspn(opt, ", \n\t"); 904 char c = *delim; 905 906 *delim = '\0'; 907 pid = string_to_uint(opt); 908 if (pid <= 0) { 909 error_msg_and_die("Invalid process id: '%s'", opt); 910 } 911 if (pid == strace_tracer_pid) { 912 error_msg_and_die("I'm sorry, I can't let you do that, Dave."); 913 } 914 *delim = c; 915 alloctcb(pid); 916 if (c == '\0') 917 break; 918 opt = delim + 1; 919 } 920} 921 922static void 923startup_attach(void) 924{ 925 int tcbi; 926 struct tcb *tcp; 927 928 /* 929 * Block user interruptions as we would leave the traced 930 * process stopped (process state T) if we would terminate in 931 * between PTRACE_ATTACH and wait4() on SIGSTOP. 932 * We rely on cleanup() from this point on. 933 */ 934 if (interactive) 935 sigprocmask(SIG_BLOCK, &blocked_set, NULL); 936 937 if (daemonized_tracer) { 938 pid_t pid = fork(); 939 if (pid < 0) { 940 perror_msg_and_die("fork"); 941 } 942 if (pid) { /* parent */ 943 /* 944 * Wait for grandchild to attach to straced process 945 * (grandparent). Grandchild SIGKILLs us after it attached. 946 * Grandparent's wait() is unblocked by our death, 947 * it proceeds to exec the straced program. 948 */ 949 pause(); 950 _exit(0); /* paranoia */ 951 } 952 /* grandchild */ 953 /* We will be the tracer process. Remember our new pid: */ 954 strace_tracer_pid = getpid(); 955 } 956 957 for (tcbi = 0; tcbi < tcbtabsize; tcbi++) { 958 tcp = tcbtab[tcbi]; 959 960 if (!tcp->pid) 961 continue; 962 963 /* Is this a process we should attach to, but not yet attached? */ 964 if (tcp->flags & TCB_ATTACHED) 965 continue; /* no, we already attached it */ 966 967 if (followfork && !daemonized_tracer) { 968 char procdir[sizeof("/proc/%d/task") + sizeof(int) * 3]; 969 DIR *dir; 970 971 sprintf(procdir, "/proc/%d/task", tcp->pid); 972 dir = opendir(procdir); 973 if (dir != NULL) { 974 unsigned int ntid = 0, nerr = 0; 975 struct_dirent *de; 976 977 while ((de = read_dir(dir)) != NULL) { 978 struct tcb *cur_tcp; 979 int tid; 980 981 if (de->d_fileno == 0) 982 continue; 983 /* we trust /proc filesystem */ 984 tid = atoi(de->d_name); 985 if (tid <= 0) 986 continue; 987 ++ntid; 988 if (ptrace_attach_or_seize(tid) < 0) { 989 ++nerr; 990 if (debug_flag) 991 fprintf(stderr, "attach to pid %d failed\n", tid); 992 continue; 993 } 994 if (debug_flag) 995 fprintf(stderr, "attach to pid %d succeeded\n", tid); 996 cur_tcp = tcp; 997 if (tid != tcp->pid) 998 cur_tcp = alloctcb(tid); 999 cur_tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop; 1000 newoutf(cur_tcp); 1001 } 1002 closedir(dir); 1003 if (interactive) { 1004 sigprocmask(SIG_SETMASK, &empty_set, NULL); 1005 if (interrupted) 1006 goto ret; 1007 sigprocmask(SIG_BLOCK, &blocked_set, NULL); 1008 } 1009 ntid -= nerr; 1010 if (ntid == 0) { 1011 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)"); 1012 droptcb(tcp); 1013 continue; 1014 } 1015 if (!qflag) { 1016 fprintf(stderr, ntid > 1 1017? "Process %u attached with %u threads\n" 1018: "Process %u attached\n", 1019 tcp->pid, ntid); 1020 } 1021 if (!(tcp->flags & TCB_ATTACHED)) { 1022 /* -p PID, we failed to attach to PID itself 1023 * but did attach to some of its sibling threads. 1024 * Drop PID's tcp. 1025 */ 1026 droptcb(tcp); 1027 } 1028 continue; 1029 } /* if (opendir worked) */ 1030 } /* if (-f) */ 1031 if (ptrace_attach_or_seize(tcp->pid) < 0) { 1032 perror_msg("attach: ptrace(PTRACE_ATTACH, ...)"); 1033 droptcb(tcp); 1034 continue; 1035 } 1036 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop; 1037 newoutf(tcp); 1038 if (debug_flag) 1039 fprintf(stderr, "attach to pid %d (main) succeeded\n", tcp->pid); 1040 1041 if (daemonized_tracer) { 1042 /* 1043 * Make parent go away. 1044 * Also makes grandparent's wait() unblock. 1045 */ 1046 kill(getppid(), SIGKILL); 1047 } 1048 1049 if (!qflag) 1050 fprintf(stderr, 1051 "Process %u attached\n", 1052 tcp->pid); 1053 } /* for each tcbtab[] */ 1054 1055 ret: 1056 if (interactive) 1057 sigprocmask(SIG_SETMASK, &empty_set, NULL); 1058} 1059 1060/* Stack-o-phobic exec helper, in the hope to work around 1061 * NOMMU + "daemonized tracer" difficulty. 1062 */ 1063struct exec_params { 1064 int fd_to_close; 1065 uid_t run_euid; 1066 gid_t run_egid; 1067 char **argv; 1068 char *pathname; 1069}; 1070static struct exec_params params_for_tracee; 1071static void __attribute__ ((noinline, noreturn)) 1072exec_or_die(void) 1073{ 1074 struct exec_params *params = ¶ms_for_tracee; 1075 1076 if (params->fd_to_close >= 0) 1077 close(params->fd_to_close); 1078 if (!daemonized_tracer && !use_seize) { 1079 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) { 1080 perror_msg_and_die("ptrace(PTRACE_TRACEME, ...)"); 1081 } 1082 } 1083 1084 if (username != NULL) { 1085 /* 1086 * It is important to set groups before we 1087 * lose privileges on setuid. 1088 */ 1089 if (initgroups(username, run_gid) < 0) { 1090 perror_msg_and_die("initgroups"); 1091 } 1092 if (setregid(run_gid, params->run_egid) < 0) { 1093 perror_msg_and_die("setregid"); 1094 } 1095 if (setreuid(run_uid, params->run_euid) < 0) { 1096 perror_msg_and_die("setreuid"); 1097 } 1098 } 1099 else if (geteuid() != 0) 1100 if (setreuid(run_uid, run_uid) < 0) { 1101 perror_msg_and_die("setreuid"); 1102 } 1103 1104 if (!daemonized_tracer) { 1105 /* 1106 * Induce a ptrace stop. Tracer (our parent) 1107 * will resume us with PTRACE_SYSCALL and display 1108 * the immediately following execve syscall. 1109 * Can't do this on NOMMU systems, we are after 1110 * vfork: parent is blocked, stopping would deadlock. 1111 */ 1112 if (!NOMMU_SYSTEM) 1113 kill(getpid(), SIGSTOP); 1114 } else { 1115 alarm(3); 1116 /* we depend on SIGCHLD set to SIG_DFL by init code */ 1117 /* if it happens to be SIG_IGN'ed, wait won't block */ 1118 wait(NULL); 1119 alarm(0); 1120 } 1121 1122 execv(params->pathname, params->argv); 1123 perror_msg_and_die("exec"); 1124} 1125 1126static void 1127startup_child(char **argv) 1128{ 1129 struct_stat statbuf; 1130 const char *filename; 1131 char pathname[MAXPATHLEN]; 1132 int pid; 1133 struct tcb *tcp; 1134 1135 filename = argv[0]; 1136 if (strchr(filename, '/')) { 1137 if (strlen(filename) > sizeof pathname - 1) { 1138 errno = ENAMETOOLONG; 1139 perror_msg_and_die("exec"); 1140 } 1141 strcpy(pathname, filename); 1142 } 1143#ifdef USE_DEBUGGING_EXEC 1144 /* 1145 * Debuggers customarily check the current directory 1146 * first regardless of the path but doing that gives 1147 * security geeks a panic attack. 1148 */ 1149 else if (stat_file(filename, &statbuf) == 0) 1150 strcpy(pathname, filename); 1151#endif /* USE_DEBUGGING_EXEC */ 1152 else { 1153 const char *path; 1154 int m, n, len; 1155 1156 for (path = getenv("PATH"); path && *path; path += m) { 1157 const char *colon = strchr(path, ':'); 1158 if (colon) { 1159 n = colon - path; 1160 m = n + 1; 1161 } 1162 else 1163 m = n = strlen(path); 1164 if (n == 0) { 1165 if (!getcwd(pathname, MAXPATHLEN)) 1166 continue; 1167 len = strlen(pathname); 1168 } 1169 else if (n > sizeof pathname - 1) 1170 continue; 1171 else { 1172 strncpy(pathname, path, n); 1173 len = n; 1174 } 1175 if (len && pathname[len - 1] != '/') 1176 pathname[len++] = '/'; 1177 strcpy(pathname + len, filename); 1178 if (stat_file(pathname, &statbuf) == 0 && 1179 /* Accept only regular files 1180 with some execute bits set. 1181 XXX not perfect, might still fail */ 1182 S_ISREG(statbuf.st_mode) && 1183 (statbuf.st_mode & 0111)) 1184 break; 1185 } 1186 } 1187 if (stat_file(pathname, &statbuf) < 0) { 1188 perror_msg_and_die("Can't stat '%s'", filename); 1189 } 1190 1191 params_for_tracee.fd_to_close = (shared_log != stderr) ? fileno(shared_log) : -1; 1192 params_for_tracee.run_euid = (statbuf.st_mode & S_ISUID) ? statbuf.st_uid : run_uid; 1193 params_for_tracee.run_egid = (statbuf.st_mode & S_ISGID) ? statbuf.st_gid : run_gid; 1194 params_for_tracee.argv = argv; 1195 /* 1196 * On NOMMU, can be safely freed only after execve in tracee. 1197 * It's hard to know when that happens, so we just leak it. 1198 */ 1199 params_for_tracee.pathname = NOMMU_SYSTEM ? strdup(pathname) : pathname; 1200 1201#if defined HAVE_PRCTL && defined PR_SET_PTRACER && defined PR_SET_PTRACER_ANY 1202 if (daemonized_tracer) 1203 prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY); 1204#endif 1205 1206 pid = fork(); 1207 if (pid < 0) { 1208 perror_msg_and_die("fork"); 1209 } 1210 if ((pid != 0 && daemonized_tracer) 1211 || (pid == 0 && !daemonized_tracer) 1212 ) { 1213 /* We are to become the tracee. Two cases: 1214 * -D: we are parent 1215 * not -D: we are child 1216 */ 1217 exec_or_die(); 1218 } 1219 1220 /* We are the tracer */ 1221 1222 if (!daemonized_tracer) { 1223 strace_child = pid; 1224 if (!use_seize) { 1225 /* child did PTRACE_TRACEME, nothing to do in parent */ 1226 } else { 1227 if (!NOMMU_SYSTEM) { 1228 /* Wait until child stopped itself */ 1229 int status; 1230 while (waitpid(pid, &status, WSTOPPED) < 0) { 1231 if (errno == EINTR) 1232 continue; 1233 perror_msg_and_die("waitpid"); 1234 } 1235 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP) { 1236 kill_save_errno(pid, SIGKILL); 1237 perror_msg_and_die("Unexpected wait status %x", status); 1238 } 1239 } 1240 /* Else: NOMMU case, we have no way to sync. 1241 * Just attach to it as soon as possible. 1242 * This means that we may miss a few first syscalls... 1243 */ 1244 1245 if (ptrace_attach_or_seize(pid)) { 1246 kill_save_errno(pid, SIGKILL); 1247 perror_msg_and_die("Can't attach to %d", pid); 1248 } 1249 if (!NOMMU_SYSTEM) 1250 kill(pid, SIGCONT); 1251 } 1252 tcp = alloctcb(pid); 1253 if (!NOMMU_SYSTEM) 1254 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop; 1255 else 1256 tcp->flags |= TCB_ATTACHED | TCB_STARTUP; 1257 newoutf(tcp); 1258 } 1259 else { 1260 /* With -D, we are *child* here, IOW: different pid. Fetch it: */ 1261 strace_tracer_pid = getpid(); 1262 /* The tracee is our parent: */ 1263 pid = getppid(); 1264 alloctcb(pid); 1265 /* attaching will be done later, by startup_attach */ 1266 /* note: we don't do newoutf(tcp) here either! */ 1267 1268 /* NOMMU BUG! -D mode is active, we (child) return, 1269 * and we will scribble over parent's stack! 1270 * When parent later unpauses, it segfaults. 1271 * 1272 * We work around it 1273 * (1) by declaring exec_or_die() NORETURN, 1274 * hopefully compiler will just jump to it 1275 * instead of call (won't push anything to stack), 1276 * (2) by trying very hard in exec_or_die() 1277 * to not use any stack, 1278 * (3) having a really big (MAXPATHLEN) stack object 1279 * in this function, which creates a "buffer" between 1280 * child's and parent's stack pointers. 1281 * This may save us if (1) and (2) failed 1282 * and compiler decided to use stack in exec_or_die() anyway 1283 * (happens on i386 because of stack parameter passing). 1284 * 1285 * A cleaner solution is to use makecontext + setcontext 1286 * to create a genuine separate stack and execute on it. 1287 */ 1288 } 1289} 1290 1291/* 1292 * Test whether the kernel support PTRACE_O_TRACECLONE et al options. 1293 * First fork a new child, call ptrace with PTRACE_SETOPTIONS on it, 1294 * and then see which options are supported by the kernel. 1295 */ 1296static int 1297test_ptrace_setoptions_followfork(void) 1298{ 1299 int pid, expected_grandchild = 0, found_grandchild = 0; 1300 const unsigned int test_options = PTRACE_O_TRACECLONE | 1301 PTRACE_O_TRACEFORK | 1302 PTRACE_O_TRACEVFORK; 1303 1304 /* Need fork for test. NOMMU has no forks */ 1305 if (NOMMU_SYSTEM) 1306 goto worked; /* be bold, and pretend that test succeeded */ 1307 1308 pid = fork(); 1309 if (pid < 0) 1310 perror_msg_and_die("fork"); 1311 if (pid == 0) { 1312 pid = getpid(); 1313 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) 1314 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work", 1315 __func__); 1316 kill_save_errno(pid, SIGSTOP); 1317 if (fork() < 0) 1318 perror_msg_and_die("fork"); 1319 _exit(0); 1320 } 1321 1322 while (1) { 1323 int status, tracee_pid; 1324 1325 errno = 0; 1326 tracee_pid = wait(&status); 1327 if (tracee_pid <= 0) { 1328 if (errno == EINTR) 1329 continue; 1330 if (errno == ECHILD) 1331 break; 1332 kill_save_errno(pid, SIGKILL); 1333 perror_msg_and_die("%s: unexpected wait result %d", 1334 __func__, tracee_pid); 1335 } 1336 if (WIFEXITED(status)) { 1337 if (WEXITSTATUS(status)) { 1338 if (tracee_pid != pid) 1339 kill_save_errno(pid, SIGKILL); 1340 error_msg_and_die("%s: unexpected exit status %u", 1341 __func__, WEXITSTATUS(status)); 1342 } 1343 continue; 1344 } 1345 if (WIFSIGNALED(status)) { 1346 if (tracee_pid != pid) 1347 kill_save_errno(pid, SIGKILL); 1348 error_msg_and_die("%s: unexpected signal %u", 1349 __func__, WTERMSIG(status)); 1350 } 1351 if (!WIFSTOPPED(status)) { 1352 if (tracee_pid != pid) 1353 kill_save_errno(tracee_pid, SIGKILL); 1354 kill_save_errno(pid, SIGKILL); 1355 error_msg_and_die("%s: unexpected wait status %x", 1356 __func__, status); 1357 } 1358 if (tracee_pid != pid) { 1359 found_grandchild = tracee_pid; 1360 if (ptrace(PTRACE_CONT, tracee_pid, 0, 0) < 0) { 1361 kill_save_errno(tracee_pid, SIGKILL); 1362 kill_save_errno(pid, SIGKILL); 1363 perror_msg_and_die("PTRACE_CONT doesn't work"); 1364 } 1365 continue; 1366 } 1367 switch (WSTOPSIG(status)) { 1368 case SIGSTOP: 1369 if (ptrace(PTRACE_SETOPTIONS, pid, 0, test_options) < 0 1370 && errno != EINVAL && errno != EIO) 1371 perror_msg("PTRACE_SETOPTIONS"); 1372 break; 1373 case SIGTRAP: 1374 if (status >> 16 == PTRACE_EVENT_FORK) { 1375 long msg = 0; 1376 1377 if (ptrace(PTRACE_GETEVENTMSG, pid, 1378 NULL, (long) &msg) == 0) 1379 expected_grandchild = msg; 1380 } 1381 break; 1382 } 1383 if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) { 1384 kill_save_errno(pid, SIGKILL); 1385 perror_msg_and_die("PTRACE_SYSCALL doesn't work"); 1386 } 1387 } 1388 if (expected_grandchild && expected_grandchild == found_grandchild) { 1389 worked: 1390 ptrace_setoptions |= test_options; 1391 if (debug_flag) 1392 fprintf(stderr, "ptrace_setoptions = %#x\n", 1393 ptrace_setoptions); 1394 return 0; 1395 } 1396 error_msg("Test for PTRACE_O_TRACECLONE failed, " 1397 "giving up using this feature."); 1398 return 1; 1399} 1400 1401/* 1402 * Test whether the kernel support PTRACE_O_TRACESYSGOOD. 1403 * First fork a new child, call ptrace(PTRACE_SETOPTIONS) on it, 1404 * and then see whether it will stop with (SIGTRAP | 0x80). 1405 * 1406 * Use of this option enables correct handling of user-generated SIGTRAPs, 1407 * and SIGTRAPs generated by special instructions such as int3 on x86: 1408 * _start: .globl _start 1409 * int3 1410 * movl $42, %ebx 1411 * movl $1, %eax 1412 * int $0x80 1413 * (compile with: "gcc -nostartfiles -nostdlib -o int3 int3.S") 1414 */ 1415static int 1416test_ptrace_setoptions_for_all(void) 1417{ 1418 const unsigned int test_options = PTRACE_O_TRACESYSGOOD | 1419 PTRACE_O_TRACEEXEC; 1420 int pid; 1421 int it_worked = 0; 1422 1423 /* Need fork for test. NOMMU has no forks */ 1424 if (NOMMU_SYSTEM) 1425 goto worked; /* be bold, and pretend that test succeeded */ 1426 1427 pid = fork(); 1428 if (pid < 0) 1429 perror_msg_and_die("fork"); 1430 1431 if (pid == 0) { 1432 pid = getpid(); 1433 if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) 1434 /* Note: exits with exitcode 1 */ 1435 perror_msg_and_die("%s: PTRACE_TRACEME doesn't work", 1436 __func__); 1437 kill(pid, SIGSTOP); 1438 _exit(0); /* parent should see entry into this syscall */ 1439 } 1440 1441 while (1) { 1442 int status, tracee_pid; 1443 1444 errno = 0; 1445 tracee_pid = wait(&status); 1446 if (tracee_pid <= 0) { 1447 if (errno == EINTR) 1448 continue; 1449 kill_save_errno(pid, SIGKILL); 1450 perror_msg_and_die("%s: unexpected wait result %d", 1451 __func__, tracee_pid); 1452 } 1453 if (WIFEXITED(status)) { 1454 if (WEXITSTATUS(status) == 0) 1455 break; 1456 error_msg_and_die("%s: unexpected exit status %u", 1457 __func__, WEXITSTATUS(status)); 1458 } 1459 if (WIFSIGNALED(status)) { 1460 error_msg_and_die("%s: unexpected signal %u", 1461 __func__, WTERMSIG(status)); 1462 } 1463 if (!WIFSTOPPED(status)) { 1464 kill(pid, SIGKILL); 1465 error_msg_and_die("%s: unexpected wait status %x", 1466 __func__, status); 1467 } 1468 if (WSTOPSIG(status) == SIGSTOP) { 1469 /* 1470 * We don't check "options aren't accepted" error. 1471 * If it happens, we'll never get (SIGTRAP | 0x80), 1472 * and thus will decide to not use the option. 1473 * IOW: the outcome of the test will be correct. 1474 */ 1475 if (ptrace(PTRACE_SETOPTIONS, pid, 0L, test_options) < 0 1476 && errno != EINVAL && errno != EIO) 1477 perror_msg("PTRACE_SETOPTIONS"); 1478 } 1479 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) { 1480 it_worked = 1; 1481 } 1482 if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) { 1483 kill_save_errno(pid, SIGKILL); 1484 perror_msg_and_die("PTRACE_SYSCALL doesn't work"); 1485 } 1486 } 1487 1488 if (it_worked) { 1489 worked: 1490 syscall_trap_sig = (SIGTRAP | 0x80); 1491 ptrace_setoptions |= test_options; 1492 if (debug_flag) 1493 fprintf(stderr, "ptrace_setoptions = %#x\n", 1494 ptrace_setoptions); 1495 return 0; 1496 } 1497 1498 error_msg("Test for PTRACE_O_TRACESYSGOOD failed, " 1499 "giving up using this feature."); 1500 return 1; 1501} 1502 1503#if USE_SEIZE 1504static void 1505test_ptrace_seize(void) 1506{ 1507 int pid; 1508 1509 /* Need fork for test. NOMMU has no forks */ 1510 if (NOMMU_SYSTEM) { 1511 post_attach_sigstop = 0; /* this sets use_seize to 1 */ 1512 return; 1513 } 1514 1515 pid = fork(); 1516 if (pid < 0) 1517 perror_msg_and_die("fork"); 1518 1519 if (pid == 0) { 1520 pause(); 1521 _exit(0); 1522 } 1523 1524 /* PTRACE_SEIZE, unlike ATTACH, doesn't force tracee to trap. After 1525 * attaching tracee continues to run unless a trap condition occurs. 1526 * PTRACE_SEIZE doesn't affect signal or group stop state. 1527 */ 1528 if (ptrace(PTRACE_SEIZE, pid, 0, 0) == 0) { 1529 post_attach_sigstop = 0; /* this sets use_seize to 1 */ 1530 } else if (debug_flag) { 1531 fprintf(stderr, "PTRACE_SEIZE doesn't work\n"); 1532 } 1533 1534 kill(pid, SIGKILL); 1535 1536 while (1) { 1537 int status, tracee_pid; 1538 1539 errno = 0; 1540 tracee_pid = waitpid(pid, &status, 0); 1541 if (tracee_pid <= 0) { 1542 if (errno == EINTR) 1543 continue; 1544 perror_msg_and_die("%s: unexpected wait result %d", 1545 __func__, tracee_pid); 1546 } 1547 if (WIFSIGNALED(status)) { 1548 return; 1549 } 1550 error_msg_and_die("%s: unexpected wait status %x", 1551 __func__, status); 1552 } 1553} 1554#else /* !USE_SEIZE */ 1555# define test_ptrace_seize() ((void)0) 1556#endif 1557 1558static unsigned 1559get_os_release(void) 1560{ 1561 unsigned rel; 1562 const char *p; 1563 struct utsname u; 1564 if (uname(&u) < 0) 1565 perror_msg_and_die("uname"); 1566 /* u.release has this form: "3.2.9[-some-garbage]" */ 1567 rel = 0; 1568 p = u.release; 1569 for (;;) { 1570 if (!(*p >= '0' && *p <= '9')) 1571 error_msg_and_die("Bad OS release string: '%s'", u.release); 1572 /* Note: this open-codes KERNEL_VERSION(): */ 1573 rel = (rel << 8) | atoi(p); 1574 if (rel >= KERNEL_VERSION(1,0,0)) 1575 break; 1576 while (*p >= '0' && *p <= '9') 1577 p++; 1578 if (*p != '.') { 1579 if (rel >= KERNEL_VERSION(0,1,0)) { 1580 /* "X.Y-something" means "X.Y.0" */ 1581 rel <<= 8; 1582 break; 1583 } 1584 error_msg_and_die("Bad OS release string: '%s'", u.release); 1585 } 1586 p++; 1587 } 1588 return rel; 1589} 1590 1591/* 1592 * Initialization part of main() was eating much stack (~0.5k), 1593 * which was unused after init. 1594 * We can reuse it if we move init code into a separate function. 1595 * 1596 * Don't want main() to inline us and defeat the reason 1597 * we have a separate function. 1598 */ 1599static void __attribute__ ((noinline)) 1600init(int argc, char *argv[]) 1601{ 1602 struct tcb *tcp; 1603 int c, i; 1604 int optF = 0; 1605 struct sigaction sa; 1606 1607 progname = argv[0] ? argv[0] : "strace"; 1608 1609 /* Make sure SIGCHLD has the default action so that waitpid 1610 definitely works without losing track of children. The user 1611 should not have given us a bogus state to inherit, but he might 1612 have. Arguably we should detect SIG_IGN here and pass it on 1613 to children, but probably noone really needs that. */ 1614 signal(SIGCHLD, SIG_DFL); 1615 1616 strace_tracer_pid = getpid(); 1617 1618 os_release = get_os_release(); 1619 1620 /* Allocate the initial tcbtab. */ 1621 tcbtabsize = argc; /* Surely enough for all -p args. */ 1622 tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0])); 1623 if (!tcbtab) 1624 die_out_of_memory(); 1625 tcp = calloc(tcbtabsize, sizeof(*tcp)); 1626 if (!tcp) 1627 die_out_of_memory(); 1628 for (c = 0; c < tcbtabsize; c++) 1629 tcbtab[c] = tcp++; 1630 1631 shared_log = stderr; 1632 set_sortby(DEFAULT_SORTBY); 1633 set_personality(DEFAULT_PERSONALITY); 1634 qualify("trace=all"); 1635 qualify("abbrev=all"); 1636 qualify("verbose=all"); 1637#if DEFAULT_QUAL_FLAGS != (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE) 1638# error Bug in DEFAULT_QUAL_FLAGS 1639#endif 1640 qualify("signal=all"); 1641 while ((c = getopt(argc, argv, 1642 "+b:cCdfFhiqrtTvVxyz" 1643 "D" 1644 "a:e:o:O:p:s:S:u:E:P:I:")) != EOF) { 1645 switch (c) { 1646 case 'b': 1647 if (strcmp(optarg, "execve") != 0) 1648 error_msg_and_die("Syscall '%s' for -b isn't supported", 1649 optarg); 1650 detach_on_execve = 1; 1651 break; 1652 case 'c': 1653 if (cflag == CFLAG_BOTH) { 1654 error_msg_and_die("-c and -C are mutually exclusive"); 1655 } 1656 cflag = CFLAG_ONLY_STATS; 1657 break; 1658 case 'C': 1659 if (cflag == CFLAG_ONLY_STATS) { 1660 error_msg_and_die("-c and -C are mutually exclusive"); 1661 } 1662 cflag = CFLAG_BOTH; 1663 break; 1664 case 'd': 1665 debug_flag = 1; 1666 break; 1667 case 'D': 1668 daemonized_tracer = 1; 1669 break; 1670 case 'F': 1671 optF = 1; 1672 break; 1673 case 'f': 1674 followfork++; 1675 break; 1676 case 'h': 1677 usage(stdout, 0); 1678 break; 1679 case 'i': 1680 iflag = 1; 1681 break; 1682 case 'q': 1683 qflag++; 1684 break; 1685 case 'r': 1686 rflag = 1; 1687 /* fall through to tflag++ */ 1688 case 't': 1689 tflag++; 1690 break; 1691 case 'T': 1692 Tflag = 1; 1693 break; 1694 case 'x': 1695 xflag++; 1696 break; 1697 case 'y': 1698 show_fd_path = 1; 1699 break; 1700 case 'v': 1701 qualify("abbrev=none"); 1702 break; 1703 case 'V': 1704 printf("%s -- version %s\n", PACKAGE_NAME, VERSION); 1705 exit(0); 1706 break; 1707 case 'z': 1708 not_failing_only = 1; 1709 break; 1710 case 'a': 1711 acolumn = string_to_uint(optarg); 1712 if (acolumn < 0) 1713 error_opt_arg(c, optarg); 1714 break; 1715 case 'e': 1716 qualify(optarg); 1717 break; 1718 case 'o': 1719 outfname = strdup(optarg); 1720 break; 1721 case 'O': 1722 i = string_to_uint(optarg); 1723 if (i < 0) 1724 error_opt_arg(c, optarg); 1725 set_overhead(i); 1726 break; 1727 case 'p': 1728 process_opt_p_list(optarg); 1729 break; 1730 case 'P': 1731 pathtrace_select(optarg); 1732 break; 1733 case 's': 1734 i = string_to_uint(optarg); 1735 if (i < 0) 1736 error_opt_arg(c, optarg); 1737 max_strlen = i; 1738 break; 1739 case 'S': 1740 set_sortby(optarg); 1741 break; 1742 case 'u': 1743 username = strdup(optarg); 1744 break; 1745 case 'E': 1746 if (putenv(optarg) < 0) 1747 die_out_of_memory(); 1748 break; 1749 case 'I': 1750 opt_intr = string_to_uint(optarg); 1751 if (opt_intr <= 0 || opt_intr >= NUM_INTR_OPTS) 1752 error_opt_arg(c, optarg); 1753 break; 1754 default: 1755 usage(stderr, 1); 1756 break; 1757 } 1758 } 1759 argv += optind; 1760 /* argc -= optind; - no need, argc is not used below */ 1761 1762 acolumn_spaces = malloc(acolumn + 1); 1763 if (!acolumn_spaces) 1764 die_out_of_memory(); 1765 memset(acolumn_spaces, ' ', acolumn); 1766 acolumn_spaces[acolumn] = '\0'; 1767 1768 /* Must have PROG [ARGS], or -p PID. Not both. */ 1769 if (!argv[0] == !nprocs) 1770 usage(stderr, 1); 1771 1772 if (nprocs != 0 && daemonized_tracer) { 1773 error_msg_and_die("-D and -p are mutually exclusive"); 1774 } 1775 1776 if (!followfork) 1777 followfork = optF; 1778 1779 if (followfork >= 2 && cflag) { 1780 error_msg_and_die("(-c or -C) and -ff are mutually exclusive"); 1781 } 1782 1783 /* See if they want to run as another user. */ 1784 if (username != NULL) { 1785 struct passwd *pent; 1786 1787 if (getuid() != 0 || geteuid() != 0) { 1788 error_msg_and_die("You must be root to use the -u option"); 1789 } 1790 pent = getpwnam(username); 1791 if (pent == NULL) { 1792 error_msg_and_die("Cannot find user '%s'", username); 1793 } 1794 run_uid = pent->pw_uid; 1795 run_gid = pent->pw_gid; 1796 } 1797 else { 1798 run_uid = getuid(); 1799 run_gid = getgid(); 1800 } 1801 1802 /* 1803 * On any reasonably recent Linux kernel (circa about 2.5.46) 1804 * need_fork_exec_workarounds should stay 0 after these tests: 1805 */ 1806 /*need_fork_exec_workarounds = 0; - already is */ 1807 if (followfork) 1808 need_fork_exec_workarounds = test_ptrace_setoptions_followfork(); 1809 need_fork_exec_workarounds |= test_ptrace_setoptions_for_all(); 1810 test_ptrace_seize(); 1811 1812 /* Check if they want to redirect the output. */ 1813 if (outfname) { 1814 /* See if they want to pipe the output. */ 1815 if (outfname[0] == '|' || outfname[0] == '!') { 1816 /* 1817 * We can't do the <outfname>.PID funny business 1818 * when using popen, so prohibit it. 1819 */ 1820 if (followfork >= 2) 1821 error_msg_and_die("Piping the output and -ff are mutually exclusive"); 1822 shared_log = strace_popen(outfname + 1); 1823 } 1824 else if (followfork < 2) 1825 shared_log = strace_fopen(outfname); 1826 } else { 1827 /* -ff without -o FILE is the same as single -f */ 1828 if (followfork >= 2) 1829 followfork = 1; 1830 } 1831 1832 if (!outfname || outfname[0] == '|' || outfname[0] == '!') { 1833 char *buf = malloc(BUFSIZ); 1834 if (!buf) 1835 die_out_of_memory(); 1836 setvbuf(shared_log, buf, _IOLBF, BUFSIZ); 1837 } 1838 if (outfname && argv[0]) { 1839 if (!opt_intr) 1840 opt_intr = INTR_NEVER; 1841 qflag = 1; 1842 } 1843 if (!opt_intr) 1844 opt_intr = INTR_WHILE_WAIT; 1845 1846 /* argv[0] -pPID -oFILE Default interactive setting 1847 * yes 0 0 INTR_WHILE_WAIT 1848 * no 1 0 INTR_WHILE_WAIT 1849 * yes 0 1 INTR_NEVER 1850 * no 1 1 INTR_WHILE_WAIT 1851 */ 1852 1853 sigemptyset(&empty_set); 1854 sigemptyset(&blocked_set); 1855 1856 /* startup_child() must be called before the signal handlers get 1857 * installed below as they are inherited into the spawned process. 1858 * Also we do not need to be protected by them as during interruption 1859 * in the startup_child() mode we kill the spawned process anyway. 1860 */ 1861 if (argv[0]) { 1862 if (!NOMMU_SYSTEM || daemonized_tracer) 1863 hide_log_until_execve = 1; 1864 skip_one_b_execve = 1; 1865 startup_child(argv); 1866 } 1867 1868 sa.sa_handler = SIG_IGN; 1869 sigemptyset(&sa.sa_mask); 1870 sa.sa_flags = 0; 1871 sigaction(SIGTTOU, &sa, NULL); /* SIG_IGN */ 1872 sigaction(SIGTTIN, &sa, NULL); /* SIG_IGN */ 1873 if (opt_intr != INTR_ANYWHERE) { 1874 if (opt_intr == INTR_BLOCK_TSTP_TOO) 1875 sigaction(SIGTSTP, &sa, NULL); /* SIG_IGN */ 1876 /* 1877 * In interactive mode (if no -o OUTFILE, or -p PID is used), 1878 * fatal signals are blocked while syscall stop is processed, 1879 * and acted on in between, when waiting for new syscall stops. 1880 * In non-interactive mode, signals are ignored. 1881 */ 1882 if (opt_intr == INTR_WHILE_WAIT) { 1883 sigaddset(&blocked_set, SIGHUP); 1884 sigaddset(&blocked_set, SIGINT); 1885 sigaddset(&blocked_set, SIGQUIT); 1886 sigaddset(&blocked_set, SIGPIPE); 1887 sigaddset(&blocked_set, SIGTERM); 1888 sa.sa_handler = interrupt; 1889 } 1890 /* SIG_IGN, or set handler for these */ 1891 sigaction(SIGHUP, &sa, NULL); 1892 sigaction(SIGINT, &sa, NULL); 1893 sigaction(SIGQUIT, &sa, NULL); 1894 sigaction(SIGPIPE, &sa, NULL); 1895 sigaction(SIGTERM, &sa, NULL); 1896 } 1897 if (nprocs != 0 || daemonized_tracer) 1898 startup_attach(); 1899 1900 /* Do we want pids printed in our -o OUTFILE? 1901 * -ff: no (every pid has its own file); or 1902 * -f: yes (there can be more pids in the future); or 1903 * -p PID1,PID2: yes (there are already more than one pid) 1904 */ 1905 print_pid_pfx = (outfname && followfork < 2 && (followfork == 1 || nprocs > 1)); 1906} 1907 1908static struct tcb * 1909pid2tcb(int pid) 1910{ 1911 int i; 1912 1913 if (pid <= 0) 1914 return NULL; 1915 1916 for (i = 0; i < tcbtabsize; i++) { 1917 struct tcb *tcp = tcbtab[i]; 1918 if (tcp->pid == pid) 1919 return tcp; 1920 } 1921 1922 return NULL; 1923} 1924 1925static void 1926cleanup(void) 1927{ 1928 int i; 1929 struct tcb *tcp; 1930 int fatal_sig; 1931 1932 /* 'interrupted' is a volatile object, fetch it only once */ 1933 fatal_sig = interrupted; 1934 if (!fatal_sig) 1935 fatal_sig = SIGTERM; 1936 1937 for (i = 0; i < tcbtabsize; i++) { 1938 tcp = tcbtab[i]; 1939 if (!tcp->pid) 1940 continue; 1941 if (debug_flag) 1942 fprintf(stderr, 1943 "cleanup: looking at pid %u\n", tcp->pid); 1944 if (tcp->pid == strace_child) { 1945 kill(tcp->pid, SIGCONT); 1946 kill(tcp->pid, fatal_sig); 1947 } 1948 detach(tcp); 1949 } 1950 if (cflag) 1951 call_summary(shared_log); 1952} 1953 1954static void 1955interrupt(int sig) 1956{ 1957 interrupted = sig; 1958} 1959 1960static void 1961trace(void) 1962{ 1963 struct rusage ru; 1964 1965 while (nprocs != 0) { 1966 int pid; 1967 int wait_errno; 1968 int status, sig; 1969 int stopped; 1970 struct tcb *tcp; 1971 unsigned event; 1972 1973 if (interrupted) 1974 return; 1975 1976 if (interactive) 1977 sigprocmask(SIG_SETMASK, &empty_set, NULL); 1978 pid = wait4(-1, &status, __WALL, (cflag ? &ru : NULL)); 1979 wait_errno = errno; 1980 if (interactive) 1981 sigprocmask(SIG_BLOCK, &blocked_set, NULL); 1982 1983 if (pid < 0) { 1984 if (wait_errno == EINTR) 1985 continue; 1986 if (wait_errno == ECHILD) 1987 /* Should not happen since nprocs > 0 */ 1988 return; 1989 errno = wait_errno; 1990 perror_msg_and_die("wait4(__WALL)"); 1991 } 1992 1993 if (pid == popen_pid) { 1994 if (!WIFSTOPPED(status)) 1995 popen_pid = 0; 1996 continue; 1997 } 1998 1999 event = ((unsigned)status >> 16); 2000 if (debug_flag) { 2001 char buf[sizeof("WIFEXITED,exitcode=%u") + sizeof(int)*3 /*paranoia:*/ + 16]; 2002 char evbuf[sizeof(",EVENT_VFORK_DONE (%u)") + sizeof(int)*3 /*paranoia:*/ + 16]; 2003 strcpy(buf, "???"); 2004 if (WIFSIGNALED(status)) 2005#ifdef WCOREDUMP 2006 sprintf(buf, "WIFSIGNALED,%ssig=%s", 2007 WCOREDUMP(status) ? "core," : "", 2008 signame(WTERMSIG(status))); 2009#else 2010 sprintf(buf, "WIFSIGNALED,sig=%s", 2011 signame(WTERMSIG(status))); 2012#endif 2013 if (WIFEXITED(status)) 2014 sprintf(buf, "WIFEXITED,exitcode=%u", WEXITSTATUS(status)); 2015 if (WIFSTOPPED(status)) 2016 sprintf(buf, "WIFSTOPPED,sig=%s", signame(WSTOPSIG(status))); 2017#ifdef WIFCONTINUED 2018 /* Should never be seen */ 2019 if (WIFCONTINUED(status)) 2020 strcpy(buf, "WIFCONTINUED"); 2021#endif 2022 evbuf[0] = '\0'; 2023 if (event != 0) { 2024 static const char *const event_names[] = { 2025 [PTRACE_EVENT_CLONE] = "CLONE", 2026 [PTRACE_EVENT_FORK] = "FORK", 2027 [PTRACE_EVENT_VFORK] = "VFORK", 2028 [PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE", 2029 [PTRACE_EVENT_EXEC] = "EXEC", 2030 [PTRACE_EVENT_EXIT] = "EXIT", 2031 /* [PTRACE_EVENT_STOP (=128)] would make biggish array */ 2032 }; 2033 const char *e = "??"; 2034 if (event < ARRAY_SIZE(event_names)) 2035 e = event_names[event]; 2036 else if (event == PTRACE_EVENT_STOP) 2037 e = "STOP"; 2038 sprintf(evbuf, ",EVENT_%s (%u)", e, event); 2039 } 2040 fprintf(stderr, " [wait(0x%04x) = %u] %s%s\n", status, pid, buf, evbuf); 2041 } 2042 2043 /* Look up 'pid' in our table. */ 2044 tcp = pid2tcb(pid); 2045 2046 if (!tcp) { 2047 if (!WIFSTOPPED(status)) { 2048 /* This can happen if we inherited 2049 * an unknown child. Example: 2050 * (sleep 1 & exec strace sleep 2) 2051 */ 2052 error_msg("Exit of unknown pid %u seen", pid); 2053 continue; 2054 } 2055 if (followfork) { 2056 /* We assume it's a fork/vfork/clone child */ 2057 tcp = alloctcb(pid); 2058 tcp->flags |= TCB_ATTACHED | TCB_STARTUP | post_attach_sigstop; 2059 newoutf(tcp); 2060 if (!qflag) 2061 fprintf(stderr, "Process %d attached\n", 2062 pid); 2063 } else { 2064 /* This can happen if a clone call used 2065 * CLONE_PTRACE itself. 2066 */ 2067 ptrace(PTRACE_CONT, pid, (char *) 0, 0); 2068 error_msg("Stop of unknown pid %u seen, PTRACE_CONTed it", pid); 2069 continue; 2070 } 2071 } 2072 2073 clear_regs(); 2074 if (WIFSTOPPED(status)) 2075 get_regs(pid); 2076 2077 /* Under Linux, execve changes pid to thread leader's pid, 2078 * and we see this changed pid on EVENT_EXEC and later, 2079 * execve sysexit. Leader "disappears" without exit 2080 * notification. Let user know that, drop leader's tcb, 2081 * and fix up pid in execve thread's tcb. 2082 * Effectively, execve thread's tcb replaces leader's tcb. 2083 * 2084 * BTW, leader is 'stuck undead' (doesn't report WIFEXITED 2085 * on exit syscall) in multithreaded programs exactly 2086 * in order to handle this case. 2087 * 2088 * PTRACE_GETEVENTMSG returns old pid starting from Linux 3.0. 2089 * On 2.6 and earlier, it can return garbage. 2090 */ 2091 if (event == PTRACE_EVENT_EXEC && os_release >= KERNEL_VERSION(3,0,0)) { 2092 FILE *fp; 2093 struct tcb *execve_thread; 2094 long old_pid = 0; 2095 2096 if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, (long) &old_pid) < 0) 2097 goto dont_switch_tcbs; 2098 if (old_pid <= 0 || old_pid == pid) 2099 goto dont_switch_tcbs; 2100 execve_thread = pid2tcb(old_pid); 2101 /* It should be !NULL, but I feel paranoid */ 2102 if (!execve_thread) 2103 goto dont_switch_tcbs; 2104 2105 if (execve_thread->curcol != 0) { 2106 /* 2107 * One case we are here is -ff: 2108 * try "strace -oLOG -ff test/threaded_execve" 2109 */ 2110 fprintf(execve_thread->outf, " <pid changed to %d ...>\n", pid); 2111 /*execve_thread->curcol = 0; - no need, see code below */ 2112 } 2113 /* Swap output FILEs (needed for -ff) */ 2114 fp = execve_thread->outf; 2115 execve_thread->outf = tcp->outf; 2116 tcp->outf = fp; 2117 /* And their column positions */ 2118 execve_thread->curcol = tcp->curcol; 2119 tcp->curcol = 0; 2120 /* Drop leader, but close execve'd thread outfile (if -ff) */ 2121 droptcb(tcp); 2122 /* Switch to the thread, reusing leader's outfile and pid */ 2123 tcp = execve_thread; 2124 tcp->pid = pid; 2125 if (cflag != CFLAG_ONLY_STATS) { 2126 printleader(tcp); 2127 tprintf("+++ superseded by execve in pid %lu +++\n", old_pid); 2128 line_ended(); 2129 tcp->flags |= TCB_REPRINT; 2130 } 2131 } 2132 dont_switch_tcbs: 2133 2134 if (event == PTRACE_EVENT_EXEC) { 2135 if (detach_on_execve && !skip_one_b_execve) 2136 detach(tcp); /* do "-b execve" thingy */ 2137 skip_one_b_execve = 0; 2138 } 2139 2140 /* Set current output file */ 2141 current_tcp = tcp; 2142 2143 if (cflag) { 2144 tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime); 2145 tcp->stime = ru.ru_stime; 2146 } 2147 2148 if (WIFSIGNALED(status)) { 2149 if (pid == strace_child) 2150 exit_code = 0x100 | WTERMSIG(status); 2151 if (cflag != CFLAG_ONLY_STATS 2152 && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL) 2153 ) { 2154 printleader(tcp); 2155#ifdef WCOREDUMP 2156 tprintf("+++ killed by %s %s+++\n", 2157 signame(WTERMSIG(status)), 2158 WCOREDUMP(status) ? "(core dumped) " : ""); 2159#else 2160 tprintf("+++ killed by %s +++\n", 2161 signame(WTERMSIG(status))); 2162#endif 2163 line_ended(); 2164 } 2165 droptcb(tcp); 2166 continue; 2167 } 2168 if (WIFEXITED(status)) { 2169 if (pid == strace_child) 2170 exit_code = WEXITSTATUS(status); 2171 if (cflag != CFLAG_ONLY_STATS && 2172 qflag < 2) { 2173 printleader(tcp); 2174 tprintf("+++ exited with %d +++\n", WEXITSTATUS(status)); 2175 line_ended(); 2176 } 2177 droptcb(tcp); 2178 continue; 2179 } 2180 if (!WIFSTOPPED(status)) { 2181 fprintf(stderr, "PANIC: pid %u not stopped\n", pid); 2182 droptcb(tcp); 2183 continue; 2184 } 2185 2186 /* Is this the very first time we see this tracee stopped? */ 2187 if (tcp->flags & TCB_STARTUP) { 2188 if (debug_flag) 2189 fprintf(stderr, "pid %d has TCB_STARTUP, initializing it\n", tcp->pid); 2190 tcp->flags &= ~TCB_STARTUP; 2191 if (tcp->flags & TCB_BPTSET) { 2192 /* 2193 * One example is a breakpoint inherited from 2194 * parent through fork(). 2195 */ 2196 if (clearbpt(tcp) < 0) { 2197 /* Pretty fatal */ 2198 droptcb(tcp); 2199 exit_code = 1; 2200 return; 2201 } 2202 } 2203 if (ptrace_setoptions) { 2204 if (debug_flag) 2205 fprintf(stderr, "setting opts %x on pid %d\n", ptrace_setoptions, tcp->pid); 2206 if (ptrace(PTRACE_SETOPTIONS, tcp->pid, NULL, ptrace_setoptions) < 0) { 2207 if (errno != ESRCH) { 2208 /* Should never happen, really */ 2209 perror_msg_and_die("PTRACE_SETOPTIONS"); 2210 } 2211 } 2212 } 2213 } 2214 2215 sig = WSTOPSIG(status); 2216 2217 if (event != 0) { 2218 /* Ptrace event */ 2219#if USE_SEIZE 2220 if (event == PTRACE_EVENT_STOP) { 2221 /* 2222 * PTRACE_INTERRUPT-stop or group-stop. 2223 * PTRACE_INTERRUPT-stop has sig == SIGTRAP here. 2224 */ 2225 if (sig == SIGSTOP 2226 || sig == SIGTSTP 2227 || sig == SIGTTIN 2228 || sig == SIGTTOU 2229 ) { 2230 stopped = 1; 2231 goto show_stopsig; 2232 } 2233 } 2234#endif 2235 goto restart_tracee_with_sig_0; 2236 } 2237 2238 /* Is this post-attach SIGSTOP? 2239 * Interestingly, the process may stop 2240 * with STOPSIG equal to some other signal 2241 * than SIGSTOP if we happend to attach 2242 * just before the process takes a signal. 2243 */ 2244 if (sig == SIGSTOP && (tcp->flags & TCB_IGNORE_ONE_SIGSTOP)) { 2245 if (debug_flag) 2246 fprintf(stderr, "ignored SIGSTOP on pid %d\n", tcp->pid); 2247 tcp->flags &= ~TCB_IGNORE_ONE_SIGSTOP; 2248 goto restart_tracee_with_sig_0; 2249 } 2250 2251 if (sig != syscall_trap_sig) { 2252 siginfo_t si; 2253 2254 /* Nonzero (true) if tracee is stopped by signal 2255 * (as opposed to "tracee received signal"). 2256 * TODO: shouldn't we check for errno == EINVAL too? 2257 * We can get ESRCH instead, you know... 2258 */ 2259 stopped = (ptrace(PTRACE_GETSIGINFO, pid, 0, (long) &si) < 0); 2260#if USE_SEIZE 2261 show_stopsig: 2262#endif 2263 if (cflag != CFLAG_ONLY_STATS 2264 && !hide_log_until_execve 2265 && (qual_flags[sig] & QUAL_SIGNAL) 2266 ) { 2267 printleader(tcp); 2268 if (!stopped) { 2269 tprintf("--- %s ", signame(sig)); 2270 printsiginfo(&si, verbose(tcp)); 2271 tprints(" ---\n"); 2272 } else 2273 tprintf("--- stopped by %s ---\n", 2274 signame(sig)); 2275 line_ended(); 2276 } 2277 2278 if (!stopped) 2279 /* It's signal-delivery-stop. Inject the signal */ 2280 goto restart_tracee; 2281 2282 /* It's group-stop */ 2283 if (use_seize) { 2284 /* 2285 * This ends ptrace-stop, but does *not* end group-stop. 2286 * This makes stopping signals work properly on straced process 2287 * (that is, process really stops. It used to continue to run). 2288 */ 2289 if (ptrace_restart(PTRACE_LISTEN, tcp, 0) < 0) { 2290 /* Note: ptrace_restart emitted error message */ 2291 exit_code = 1; 2292 return; 2293 } 2294 continue; 2295 } 2296 /* We don't have PTRACE_LISTEN support... */ 2297 goto restart_tracee; 2298 } 2299 2300 /* We handled quick cases, we are permitted to interrupt now. */ 2301 if (interrupted) 2302 return; 2303 2304 /* This should be syscall entry or exit. 2305 * (Or it still can be that pesky post-execve SIGTRAP!) 2306 * Handle it. 2307 */ 2308 if (trace_syscall(tcp) < 0) { 2309 /* ptrace() failed in trace_syscall(). 2310 * Likely a result of process disappearing mid-flight. 2311 * Observed case: exit_group() or SIGKILL terminating 2312 * all processes in thread group. 2313 * We assume that ptrace error was caused by process death. 2314 * We used to detach(tcp) here, but since we no longer 2315 * implement "detach before death" policy/hack, 2316 * we can let this process to report its death to us 2317 * normally, via WIFEXITED or WIFSIGNALED wait status. 2318 */ 2319 continue; 2320 } 2321 restart_tracee_with_sig_0: 2322 sig = 0; 2323 restart_tracee: 2324 if (ptrace_restart(PTRACE_SYSCALL, tcp, sig) < 0) { 2325 /* Note: ptrace_restart emitted error message */ 2326 exit_code = 1; 2327 return; 2328 } 2329 } /* while (nprocs != 0) */ 2330} 2331 2332int 2333main(int argc, char *argv[]) 2334{ 2335 init(argc, argv); 2336 2337 /* Run main tracing loop */ 2338 trace(); 2339 2340 cleanup(); 2341 fflush(NULL); 2342 if (shared_log != stderr) 2343 fclose(shared_log); 2344 if (popen_pid) { 2345 while (waitpid(popen_pid, NULL, 0) < 0 && errno == EINTR) 2346 ; 2347 } 2348 if (exit_code > 0xff) { 2349 /* Avoid potential core file clobbering. */ 2350 struct_rlimit rlim = {0, 0}; 2351 set_rlimit(RLIMIT_CORE, &rlim); 2352 2353 /* Child was killed by a signal, mimic that. */ 2354 exit_code &= 0xff; 2355 signal(exit_code, SIG_DFL); 2356 raise(exit_code); 2357 /* Paranoia - what if this signal is not fatal? 2358 Exit with 128 + signo then. */ 2359 exit_code += 128; 2360 } 2361 2362 return exit_code; 2363} 2364