1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright 1997 Sean Eric Fagan 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Sean Eric Fagan 17 * 4. Neither the name of the author may be used to endorse or promote 18 * products derived from this software without specific prior written 19 * permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 /* 36 * Various setup functions for truss. Not the cleanest-written code, 37 * I'm afraid. 38 */ 39 40 #include <sys/ptrace.h> 41 #include <sys/sysctl.h> 42 #include <sys/time.h> 43 #include <sys/wait.h> 44 45 #include <assert.h> 46 #include <err.h> 47 #include <errno.h> 48 #include <signal.h> 49 #include <stdbool.h> 50 #include <stdint.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <sysdecode.h> 55 #include <time.h> 56 #include <unistd.h> 57 58 #include "truss.h" 59 #include "syscall.h" 60 #include "extern.h" 61 62 struct procabi_table { 63 const char *name; 64 struct procabi *abi; 65 }; 66 67 static sig_atomic_t detaching; 68 69 static void enter_syscall(struct trussinfo *, struct threadinfo *, 70 struct ptrace_lwpinfo *); 71 static void new_proc(struct trussinfo *, pid_t, lwpid_t); 72 73 74 static struct procabi freebsd = { 75 .type = "FreeBSD", 76 .abi = SYSDECODE_ABI_FREEBSD, 77 .pointer_size = sizeof(void *), 78 .extra_syscalls = STAILQ_HEAD_INITIALIZER(freebsd.extra_syscalls), 79 .syscalls = { NULL } 80 }; 81 82 #if !defined(__SIZEOF_POINTER__) 83 #error "Use a modern compiler." 84 #endif 85 86 #if __SIZEOF_POINTER__ > 4 87 static struct procabi freebsd32 = { 88 .type = "FreeBSD32", 89 .abi = SYSDECODE_ABI_FREEBSD32, 90 .pointer_size = sizeof(uint32_t), 91 .compat_prefix = "freebsd32_", 92 .extra_syscalls = STAILQ_HEAD_INITIALIZER(freebsd32.extra_syscalls), 93 .syscalls = { NULL } 94 }; 95 #endif 96 97 static struct procabi linux = { 98 .type = "Linux", 99 .abi = SYSDECODE_ABI_LINUX, 100 .pointer_size = sizeof(void *), 101 .extra_syscalls = STAILQ_HEAD_INITIALIZER(linux.extra_syscalls), 102 .syscalls = { NULL } 103 }; 104 105 #if __SIZEOF_POINTER__ > 4 106 static struct procabi linux32 = { 107 .type = "Linux32", 108 .abi = SYSDECODE_ABI_LINUX32, 109 .pointer_size = sizeof(uint32_t), 110 .extra_syscalls = STAILQ_HEAD_INITIALIZER(linux32.extra_syscalls), 111 .syscalls = { NULL } 112 }; 113 #endif 114 115 static struct procabi_table abis[] = { 116 #if __SIZEOF_POINTER__ == 4 117 { "FreeBSD ELF32", &freebsd }, 118 #elif __SIZEOF_POINTER__ == 8 119 { "FreeBSD ELF64", &freebsd }, 120 { "FreeBSD ELF32", &freebsd32 }, 121 #else 122 #error "Unsupported pointer size" 123 #endif 124 #if defined(__powerpc64__) 125 { "FreeBSD ELF64 V2", &freebsd }, 126 #endif 127 #if defined(__amd64__) 128 { "FreeBSD a.out", &freebsd32 }, 129 #endif 130 #if defined(__i386__) 131 { "FreeBSD a.out", &freebsd }, 132 #endif 133 #if __SIZEOF_POINTER__ >= 8 134 { "Linux ELF64", &linux }, 135 { "Linux ELF32", &linux32 }, 136 #else 137 { "Linux ELF32", &linux }, 138 #endif 139 }; 140 141 /* 142 * setup_and_wait() is called to start a process. All it really does 143 * is fork(), enable tracing in the child, and then exec the given 144 * command. At that point, the child process stops, and the parent 145 * can wake up and deal with it. 146 */ 147 void 148 setup_and_wait(struct trussinfo *info, char *command[]) 149 { 150 pid_t pid; 151 152 pid = vfork(); 153 if (pid == -1) 154 err(1, "fork failed"); 155 if (pid == 0) { /* Child */ 156 ptrace(PT_TRACE_ME, 0, 0, 0); 157 execvp(command[0], command); 158 err(1, "execvp %s", command[0]); 159 } 160 161 /* Only in the parent here */ 162 if (waitpid(pid, NULL, 0) < 0) 163 err(1, "unexpected stop in waitpid"); 164 165 new_proc(info, pid, 0); 166 } 167 168 /* 169 * start_tracing is called to attach to an existing process. 170 */ 171 void 172 start_tracing(struct trussinfo *info, pid_t pid) 173 { 174 int ret, retry; 175 176 retry = 10; 177 do { 178 ret = ptrace(PT_ATTACH, pid, NULL, 0); 179 usleep(200); 180 } while (ret && retry-- > 0); 181 if (ret) 182 err(1, "Cannot attach to target process"); 183 184 if (waitpid(pid, NULL, 0) < 0) 185 err(1, "Unexpected stop in waitpid"); 186 187 new_proc(info, pid, 0); 188 } 189 190 /* 191 * Restore a process back to it's pre-truss state. 192 * Called for SIGINT, SIGTERM, SIGQUIT. This only 193 * applies if truss was told to monitor an already-existing 194 * process. 195 */ 196 void 197 restore_proc(int signo __unused) 198 { 199 200 detaching = 1; 201 } 202 203 static void 204 detach_proc(pid_t pid) 205 { 206 int sig, status; 207 208 /* 209 * Stop the child so that we can detach. Filter out possible 210 * lingering SIGTRAP events buffered in the threads. 211 */ 212 kill(pid, SIGSTOP); 213 for (;;) { 214 if (waitpid(pid, &status, 0) < 0) 215 err(1, "Unexpected error in waitpid"); 216 sig = WIFSTOPPED(status) ? WSTOPSIG(status) : 0; 217 if (sig == SIGSTOP) 218 break; 219 if (sig == SIGTRAP) 220 sig = 0; 221 if (ptrace(PT_CONTINUE, pid, (caddr_t)1, sig) < 0) 222 err(1, "Can not continue for detach"); 223 } 224 225 if (ptrace(PT_DETACH, pid, (caddr_t)1, 0) < 0) 226 err(1, "Can not detach the process"); 227 228 kill(pid, SIGCONT); 229 } 230 231 /* 232 * Determine the ABI. This is called after every exec, and when 233 * a process is first monitored. 234 */ 235 static struct procabi * 236 find_abi(pid_t pid) 237 { 238 size_t len; 239 unsigned int i; 240 int error; 241 int mib[4]; 242 char progt[32]; 243 244 len = sizeof(progt); 245 mib[0] = CTL_KERN; 246 mib[1] = KERN_PROC; 247 mib[2] = KERN_PROC_SV_NAME; 248 mib[3] = pid; 249 error = sysctl(mib, 4, progt, &len, NULL, 0); 250 if (error != 0) 251 err(2, "can not get sysvec name"); 252 253 for (i = 0; i < nitems(abis); i++) { 254 if (strcmp(abis[i].name, progt) == 0) 255 return (abis[i].abi); 256 } 257 warnx("ABI %s for pid %ld is not supported", progt, (long)pid); 258 return (NULL); 259 } 260 261 static struct threadinfo * 262 new_thread(struct procinfo *p, lwpid_t lwpid) 263 { 264 struct threadinfo *nt; 265 266 /* 267 * If this happens it means there is a bug in truss. Unfortunately 268 * this will kill any processes truss is attached to. 269 */ 270 LIST_FOREACH(nt, &p->threadlist, entries) { 271 if (nt->tid == lwpid) 272 errx(1, "Duplicate thread for LWP %ld", (long)lwpid); 273 } 274 275 nt = calloc(1, sizeof(struct threadinfo)); 276 if (nt == NULL) 277 err(1, "calloc() failed"); 278 nt->proc = p; 279 nt->tid = lwpid; 280 LIST_INSERT_HEAD(&p->threadlist, nt, entries); 281 return (nt); 282 } 283 284 static void 285 free_thread(struct threadinfo *t) 286 { 287 288 LIST_REMOVE(t, entries); 289 free(t); 290 } 291 292 static void 293 add_threads(struct trussinfo *info, struct procinfo *p) 294 { 295 struct ptrace_lwpinfo pl; 296 struct threadinfo *t; 297 lwpid_t *lwps; 298 int i, nlwps; 299 300 nlwps = ptrace(PT_GETNUMLWPS, p->pid, NULL, 0); 301 if (nlwps == -1) 302 err(1, "Unable to fetch number of LWPs"); 303 assert(nlwps > 0); 304 lwps = calloc(nlwps, sizeof(*lwps)); 305 nlwps = ptrace(PT_GETLWPLIST, p->pid, (caddr_t)lwps, nlwps); 306 if (nlwps == -1) 307 err(1, "Unable to fetch LWP list"); 308 for (i = 0; i < nlwps; i++) { 309 t = new_thread(p, lwps[i]); 310 if (ptrace(PT_LWPINFO, lwps[i], (caddr_t)&pl, sizeof(pl)) == -1) 311 err(1, "ptrace(PT_LWPINFO)"); 312 if (pl.pl_flags & PL_FLAG_SCE) { 313 info->curthread = t; 314 enter_syscall(info, t, &pl); 315 } 316 } 317 free(lwps); 318 } 319 320 static void 321 new_proc(struct trussinfo *info, pid_t pid, lwpid_t lwpid) 322 { 323 struct procinfo *np; 324 325 /* 326 * If this happens it means there is a bug in truss. Unfortunately 327 * this will kill any processes truss is attached to. 328 */ 329 LIST_FOREACH(np, &info->proclist, entries) { 330 if (np->pid == pid) 331 errx(1, "Duplicate process for pid %ld", (long)pid); 332 } 333 334 if (info->flags & FOLLOWFORKS) 335 if (ptrace(PT_FOLLOW_FORK, pid, NULL, 1) == -1) 336 err(1, "Unable to follow forks for pid %ld", (long)pid); 337 if (ptrace(PT_LWP_EVENTS, pid, NULL, 1) == -1) 338 err(1, "Unable to enable LWP events for pid %ld", (long)pid); 339 np = calloc(1, sizeof(struct procinfo)); 340 np->pid = pid; 341 np->abi = find_abi(pid); 342 LIST_INIT(&np->threadlist); 343 LIST_INIT(&np->fdlist); 344 LIST_INSERT_HEAD(&info->proclist, np, entries); 345 346 if (lwpid != 0) 347 new_thread(np, lwpid); 348 else 349 add_threads(info, np); 350 } 351 352 static void 353 free_proc(struct procinfo *p) 354 { 355 struct threadinfo *t, *t2; 356 struct fd_domain *f, *f2; 357 358 LIST_FOREACH_SAFE(t, &p->threadlist, entries, t2) { 359 free(t); 360 } 361 362 LIST_FOREACH_SAFE(f, &p->fdlist, entries, f2) { 363 free(f); 364 } 365 366 LIST_REMOVE(p, entries); 367 free(p); 368 } 369 370 static void 371 detach_all_procs(struct trussinfo *info) 372 { 373 struct procinfo *p, *p2; 374 375 LIST_FOREACH_SAFE(p, &info->proclist, entries, p2) { 376 detach_proc(p->pid); 377 free_proc(p); 378 } 379 } 380 381 static struct procinfo * 382 find_proc(struct trussinfo *info, pid_t pid) 383 { 384 struct procinfo *np; 385 386 LIST_FOREACH(np, &info->proclist, entries) { 387 if (np->pid == pid) 388 return (np); 389 } 390 391 return (NULL); 392 } 393 394 /* 395 * Change curthread member based on (pid, lwpid). 396 */ 397 static void 398 find_thread(struct trussinfo *info, pid_t pid, lwpid_t lwpid) 399 { 400 struct procinfo *np; 401 struct threadinfo *nt; 402 403 np = find_proc(info, pid); 404 assert(np != NULL); 405 406 LIST_FOREACH(nt, &np->threadlist, entries) { 407 if (nt->tid == lwpid) { 408 info->curthread = nt; 409 return; 410 } 411 } 412 errx(1, "could not find thread"); 413 } 414 415 /* 416 * When a process exits, it should have exactly one thread left. 417 * All of the other threads should have reported thread exit events. 418 */ 419 static void 420 find_exit_thread(struct trussinfo *info, pid_t pid) 421 { 422 struct procinfo *p; 423 424 p = find_proc(info, pid); 425 assert(p != NULL); 426 427 info->curthread = LIST_FIRST(&p->threadlist); 428 assert(info->curthread != NULL); 429 assert(LIST_NEXT(info->curthread, entries) == NULL); 430 } 431 432 static void 433 alloc_syscall(struct threadinfo *t, struct ptrace_lwpinfo *pl) 434 { 435 u_int i; 436 437 assert(t->in_syscall == 0); 438 assert(t->cs.number == 0); 439 assert(t->cs.sc == NULL); 440 assert(t->cs.nargs == 0); 441 for (i = 0; i < nitems(t->cs.s_args); i++) 442 assert(t->cs.s_args[i] == NULL); 443 memset(t->cs.args, 0, sizeof(t->cs.args)); 444 t->cs.number = pl->pl_syscall_code; 445 t->in_syscall = 1; 446 } 447 448 static void 449 free_syscall(struct threadinfo *t) 450 { 451 u_int i; 452 453 for (i = 0; i < t->cs.nargs; i++) 454 free(t->cs.s_args[i]); 455 memset(&t->cs, 0, sizeof(t->cs)); 456 t->in_syscall = 0; 457 } 458 459 static void 460 enter_syscall(struct trussinfo *info, struct threadinfo *t, 461 struct ptrace_lwpinfo *pl) 462 { 463 struct syscall *sc; 464 u_int i, narg; 465 466 alloc_syscall(t, pl); 467 narg = MIN(pl->pl_syscall_narg, nitems(t->cs.args)); 468 if (narg != 0 && ptrace(PT_GET_SC_ARGS, t->tid, (caddr_t)t->cs.args, 469 sizeof(t->cs.args)) != 0) { 470 free_syscall(t); 471 return; 472 } 473 474 sc = get_syscall(t, t->cs.number, narg); 475 if (sc->unknown) 476 fprintf(info->outfile, "-- UNKNOWN %s SYSCALL %d --\n", 477 t->proc->abi->type, t->cs.number); 478 479 t->cs.nargs = sc->decode.nargs; 480 assert(sc->decode.nargs <= nitems(t->cs.s_args)); 481 482 t->cs.sc = sc; 483 484 /* 485 * At this point, we set up the system call arguments. 486 * We ignore any OUT ones, however -- those are arguments that 487 * are set by the system call, and so are probably meaningless 488 * now. This doesn't currently support arguments that are 489 * passed in *and* out, however. 490 */ 491 #if DEBUG 492 fprintf(stderr, "syscall %s(", sc->name); 493 #endif 494 for (i = 0; i < t->cs.nargs; i++) { 495 #if DEBUG 496 fprintf(stderr, "0x%lx%s", 497 t->cs.args[sc->decode.args[i].offset], 498 i < (t->cs.nargs - 1) ? "," : ""); 499 #endif 500 if (!(sc->decode.args[i].type & OUT)) { 501 t->cs.s_args[i] = print_arg(&sc->decode.args[i], 502 t->cs.args, NULL, info, &sc->decode); 503 } 504 } 505 #if DEBUG 506 fprintf(stderr, ")\n"); 507 #endif 508 509 clock_gettime(CLOCK_REALTIME, &t->before); 510 } 511 512 /* 513 * When a thread exits voluntarily (including when a thread calls 514 * exit() to trigger a process exit), the thread's internal state 515 * holds the arguments passed to the exit system call. When the 516 * thread's exit is reported, log that system call without a return 517 * value. 518 */ 519 static void 520 thread_exit_syscall(struct trussinfo *info) 521 { 522 struct threadinfo *t; 523 524 t = info->curthread; 525 if (!t->in_syscall) 526 return; 527 528 clock_gettime(CLOCK_REALTIME, &t->after); 529 530 print_syscall_ret(info, 0, NULL); 531 free_syscall(t); 532 } 533 534 static void 535 exit_syscall(struct trussinfo *info, struct ptrace_lwpinfo *pl) 536 { 537 struct threadinfo *t; 538 struct procinfo *p; 539 struct syscall *sc; 540 struct ptrace_sc_ret psr; 541 u_int i; 542 543 t = info->curthread; 544 if (!t->in_syscall) 545 return; 546 547 clock_gettime(CLOCK_REALTIME, &t->after); 548 p = t->proc; 549 if (ptrace(PT_GET_SC_RET, t->tid, (caddr_t)&psr, sizeof(psr)) != 0) { 550 free_syscall(t); 551 return; 552 } 553 554 sc = t->cs.sc; 555 /* 556 * Here, we only look for arguments that have OUT masked in -- 557 * otherwise, they were handled in enter_syscall(). 558 */ 559 for (i = 0; i < sc->decode.nargs; i++) { 560 char *temp; 561 562 if (sc->decode.args[i].type & OUT) { 563 /* 564 * If an error occurred, then don't bother 565 * getting the data; it may not be valid. 566 */ 567 if (psr.sr_error != 0) { 568 asprintf(&temp, "0x%lx", 569 (long)t->cs.args[sc->decode.args[i].offset]); 570 } else { 571 temp = print_arg(&sc->decode.args[i], 572 t->cs.args, psr.sr_retval, info, 573 &sc->decode); 574 } 575 t->cs.s_args[i] = temp; 576 } 577 } 578 579 /* 580 * Track successfully created sockets so later syscalls using the 581 * returned file descriptor can be identified by socket domain. 582 */ 583 if (strcmp(sc->name, "socket") == 0 && 584 psr.sr_error == 0) { 585 586 struct fd_domain *f = calloc(1, sizeof(*f)); 587 588 f->fd = (int)psr.sr_retval[0]; 589 f->domain = (int)t->cs.args[0]; 590 591 LIST_INSERT_HEAD(&p->fdlist, f, entries); 592 } 593 594 if (strcmp(sc->name, "close") == 0 && 595 psr.sr_error == 0) { 596 597 struct fd_domain *f, *f2; 598 599 LIST_FOREACH_SAFE(f, &p->fdlist, entries, f2) { 600 if (f->fd == (int)t->cs.args[0]) { 601 LIST_REMOVE(f, entries); 602 free(f); 603 } 604 } 605 } 606 607 print_syscall_ret(info, psr.sr_error, psr.sr_retval); 608 free_syscall(t); 609 610 /* 611 * If the process executed a new image, check the ABI. If the 612 * new ABI isn't supported, stop tracing this process. 613 */ 614 if (pl->pl_flags & PL_FLAG_EXEC) { 615 assert(LIST_NEXT(LIST_FIRST(&p->threadlist), entries) == NULL); 616 p->abi = find_abi(p->pid); 617 if (p->abi == NULL) { 618 if (ptrace(PT_DETACH, p->pid, (caddr_t)1, 0) < 0) 619 err(1, "Can not detach the process"); 620 free_proc(p); 621 } 622 } 623 } 624 625 int 626 print_line_prefix(struct trussinfo *info) 627 { 628 struct timespec timediff; 629 struct threadinfo *t; 630 int len; 631 632 len = 0; 633 t = info->curthread; 634 if (info->flags & (FOLLOWFORKS | DISPLAYTIDS)) { 635 if (info->flags & FOLLOWFORKS) 636 len += fprintf(info->outfile, "%5d", t->proc->pid); 637 if ((info->flags & (FOLLOWFORKS | DISPLAYTIDS)) == 638 (FOLLOWFORKS | DISPLAYTIDS)) 639 len += fprintf(info->outfile, " "); 640 if (info->flags & DISPLAYTIDS) 641 len += fprintf(info->outfile, "%6d", t->tid); 642 len += fprintf(info->outfile, ": "); 643 } 644 if (info->flags & ABSOLUTETIMESTAMPS) { 645 timespecsub(&t->after, &info->start_time, &timediff); 646 len += fprintf(info->outfile, "%jd.%09ld ", 647 (intmax_t)timediff.tv_sec, timediff.tv_nsec); 648 } 649 if (info->flags & RELATIVETIMESTAMPS) { 650 timespecsub(&t->after, &t->before, &timediff); 651 len += fprintf(info->outfile, "%jd.%09ld ", 652 (intmax_t)timediff.tv_sec, timediff.tv_nsec); 653 } 654 return (len); 655 } 656 657 static void 658 report_thread_death(struct trussinfo *info) 659 { 660 struct threadinfo *t; 661 662 t = info->curthread; 663 clock_gettime(CLOCK_REALTIME, &t->after); 664 print_line_prefix(info); 665 fprintf(info->outfile, "<thread %ld exited>\n", (long)t->tid); 666 } 667 668 static void 669 report_thread_birth(struct trussinfo *info) 670 { 671 struct threadinfo *t; 672 673 t = info->curthread; 674 clock_gettime(CLOCK_REALTIME, &t->after); 675 t->before = t->after; 676 print_line_prefix(info); 677 fprintf(info->outfile, "<new thread %ld>\n", (long)t->tid); 678 } 679 680 static void 681 report_exit(struct trussinfo *info, siginfo_t *si) 682 { 683 struct threadinfo *t; 684 685 t = info->curthread; 686 clock_gettime(CLOCK_REALTIME, &t->after); 687 print_line_prefix(info); 688 if (si->si_code == CLD_EXITED) 689 fprintf(info->outfile, "process exit, rval = %u\n", 690 si->si_status); 691 else 692 fprintf(info->outfile, "process killed, signal = %u%s\n", 693 si->si_status, si->si_code == CLD_DUMPED ? 694 " (core dumped)" : ""); 695 } 696 697 static void 698 report_new_child(struct trussinfo *info) 699 { 700 struct threadinfo *t; 701 702 t = info->curthread; 703 clock_gettime(CLOCK_REALTIME, &t->after); 704 t->before = t->after; 705 print_line_prefix(info); 706 fprintf(info->outfile, "<new process>\n"); 707 } 708 709 void 710 decode_siginfo(FILE *fp, siginfo_t *si) 711 { 712 const char *str; 713 714 fprintf(fp, " code="); 715 str = sysdecode_sigcode(si->si_signo, si->si_code); 716 if (str == NULL) 717 fprintf(fp, "%d", si->si_code); 718 else 719 fprintf(fp, "%s", str); 720 switch (si->si_code) { 721 case SI_NOINFO: 722 break; 723 case SI_QUEUE: 724 fprintf(fp, " value=%p", si->si_value.sival_ptr); 725 /* FALLTHROUGH */ 726 case SI_USER: 727 case SI_LWP: 728 fprintf(fp, " pid=%jd uid=%jd", (intmax_t)si->si_pid, 729 (intmax_t)si->si_uid); 730 break; 731 case SI_TIMER: 732 fprintf(fp, " value=%p", si->si_value.sival_ptr); 733 fprintf(fp, " timerid=%d", si->si_timerid); 734 fprintf(fp, " overrun=%d", si->si_overrun); 735 if (si->si_errno != 0) 736 fprintf(fp, " errno=%d", si->si_errno); 737 break; 738 case SI_ASYNCIO: 739 fprintf(fp, " value=%p", si->si_value.sival_ptr); 740 break; 741 case SI_MESGQ: 742 fprintf(fp, " value=%p", si->si_value.sival_ptr); 743 fprintf(fp, " mqd=%d", si->si_mqd); 744 break; 745 default: 746 switch (si->si_signo) { 747 case SIGILL: 748 case SIGFPE: 749 case SIGSEGV: 750 case SIGBUS: 751 fprintf(fp, " trapno=%d", si->si_trapno); 752 fprintf(fp, " addr=%p", si->si_addr); 753 break; 754 case SIGCHLD: 755 fprintf(fp, " pid=%jd uid=%jd", (intmax_t)si->si_pid, 756 (intmax_t)si->si_uid); 757 fprintf(fp, " status=%d", si->si_status); 758 break; 759 } 760 } 761 } 762 763 static void 764 report_signal(struct trussinfo *info, siginfo_t *si, struct ptrace_lwpinfo *pl) 765 { 766 struct threadinfo *t; 767 const char *signame; 768 769 t = info->curthread; 770 clock_gettime(CLOCK_REALTIME, &t->after); 771 print_line_prefix(info); 772 signame = sysdecode_signal(si->si_status); 773 if (signame == NULL) 774 signame = "?"; 775 fprintf(info->outfile, "SIGNAL %u (%s)", si->si_status, signame); 776 if (pl->pl_event == PL_EVENT_SIGNAL && pl->pl_flags & PL_FLAG_SI) 777 decode_siginfo(info->outfile, &pl->pl_siginfo); 778 fprintf(info->outfile, "\n"); 779 780 } 781 782 /* 783 * Wait for events until all the processes have exited or truss has been 784 * asked to stop. 785 */ 786 void 787 eventloop(struct trussinfo *info) 788 { 789 struct ptrace_lwpinfo pl; 790 siginfo_t si; 791 int pending_signal; 792 793 while (!LIST_EMPTY(&info->proclist)) { 794 if (detaching) { 795 detach_all_procs(info); 796 return; 797 } 798 799 if (waitid(P_ALL, 0, &si, WTRAPPED | WEXITED) == -1) { 800 if (errno == EINTR) 801 continue; 802 err(1, "Unexpected error from waitid"); 803 } 804 805 assert(si.si_signo == SIGCHLD); 806 807 switch (si.si_code) { 808 case CLD_EXITED: 809 case CLD_KILLED: 810 case CLD_DUMPED: 811 find_exit_thread(info, si.si_pid); 812 if ((info->flags & COUNTONLY) == 0) { 813 if (si.si_code == CLD_EXITED) 814 thread_exit_syscall(info); 815 report_exit(info, &si); 816 } 817 free_proc(info->curthread->proc); 818 info->curthread = NULL; 819 break; 820 case CLD_TRAPPED: 821 if (ptrace(PT_LWPINFO, si.si_pid, (caddr_t)&pl, 822 sizeof(pl)) == -1) 823 err(1, "ptrace(PT_LWPINFO)"); 824 825 if (pl.pl_flags & PL_FLAG_CHILD) { 826 new_proc(info, si.si_pid, pl.pl_lwpid); 827 assert(LIST_FIRST(&info->proclist)->abi != 828 NULL); 829 } else if (pl.pl_flags & PL_FLAG_BORN) 830 new_thread(find_proc(info, si.si_pid), 831 pl.pl_lwpid); 832 find_thread(info, si.si_pid, pl.pl_lwpid); 833 834 if (si.si_status == SIGTRAP && 835 (pl.pl_flags & (PL_FLAG_BORN|PL_FLAG_EXITED| 836 PL_FLAG_SCE|PL_FLAG_SCX)) != 0) { 837 if (pl.pl_flags & PL_FLAG_BORN) { 838 if ((info->flags & COUNTONLY) == 0) 839 report_thread_birth(info); 840 } else if (pl.pl_flags & PL_FLAG_EXITED) { 841 if ((info->flags & COUNTONLY) == 0) 842 report_thread_death(info); 843 free_thread(info->curthread); 844 info->curthread = NULL; 845 } else if (pl.pl_flags & PL_FLAG_SCE) 846 enter_syscall(info, info->curthread, &pl); 847 else if (pl.pl_flags & PL_FLAG_SCX) 848 exit_syscall(info, &pl); 849 pending_signal = 0; 850 } else if (pl.pl_flags & PL_FLAG_CHILD) { 851 if ((info->flags & COUNTONLY) == 0) 852 report_new_child(info); 853 pending_signal = 0; 854 } else { 855 if ((info->flags & NOSIGS) == 0) 856 report_signal(info, &si, &pl); 857 pending_signal = si.si_status; 858 } 859 ptrace(PT_SYSCALL, si.si_pid, (caddr_t)1, 860 pending_signal); 861 break; 862 case CLD_STOPPED: 863 errx(1, "waitid reported CLD_STOPPED"); 864 case CLD_CONTINUED: 865 break; 866 } 867 } 868 } 869