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 f->protocol = (int)t->cs.args[2]; 591 592 LIST_INSERT_HEAD(&p->fdlist, f, entries); 593 } 594 595 if (strcmp(sc->name, "close") == 0 && 596 psr.sr_error == 0) { 597 598 struct fd_domain *f, *f2; 599 600 LIST_FOREACH_SAFE(f, &p->fdlist, entries, f2) { 601 if (f->fd == (int)t->cs.args[0]) { 602 LIST_REMOVE(f, entries); 603 free(f); 604 } 605 } 606 } 607 608 print_syscall_ret(info, psr.sr_error, psr.sr_retval); 609 free_syscall(t); 610 611 /* 612 * If the process executed a new image, check the ABI. If the 613 * new ABI isn't supported, stop tracing this process. 614 */ 615 if (pl->pl_flags & PL_FLAG_EXEC) { 616 assert(LIST_NEXT(LIST_FIRST(&p->threadlist), entries) == NULL); 617 p->abi = find_abi(p->pid); 618 if (p->abi == NULL) { 619 if (ptrace(PT_DETACH, p->pid, (caddr_t)1, 0) < 0) 620 err(1, "Can not detach the process"); 621 free_proc(p); 622 } 623 } 624 } 625 626 int 627 print_line_prefix(struct trussinfo *info) 628 { 629 struct timespec timediff; 630 struct threadinfo *t; 631 int len; 632 633 len = 0; 634 t = info->curthread; 635 if (info->flags & (FOLLOWFORKS | DISPLAYTIDS)) { 636 if (info->flags & FOLLOWFORKS) 637 len += fprintf(info->outfile, "%5d", t->proc->pid); 638 if ((info->flags & (FOLLOWFORKS | DISPLAYTIDS)) == 639 (FOLLOWFORKS | DISPLAYTIDS)) 640 len += fprintf(info->outfile, " "); 641 if (info->flags & DISPLAYTIDS) 642 len += fprintf(info->outfile, "%6d", t->tid); 643 len += fprintf(info->outfile, ": "); 644 } 645 if (info->flags & ABSOLUTETIMESTAMPS) { 646 timespecsub(&t->after, &info->start_time, &timediff); 647 len += fprintf(info->outfile, "%jd.%09ld ", 648 (intmax_t)timediff.tv_sec, timediff.tv_nsec); 649 } 650 if (info->flags & RELATIVETIMESTAMPS) { 651 timespecsub(&t->after, &t->before, &timediff); 652 len += fprintf(info->outfile, "%jd.%09ld ", 653 (intmax_t)timediff.tv_sec, timediff.tv_nsec); 654 } 655 return (len); 656 } 657 658 static void 659 report_thread_death(struct trussinfo *info) 660 { 661 struct threadinfo *t; 662 663 t = info->curthread; 664 clock_gettime(CLOCK_REALTIME, &t->after); 665 print_line_prefix(info); 666 fprintf(info->outfile, "<thread %ld exited>\n", (long)t->tid); 667 } 668 669 static void 670 report_thread_birth(struct trussinfo *info) 671 { 672 struct threadinfo *t; 673 674 t = info->curthread; 675 clock_gettime(CLOCK_REALTIME, &t->after); 676 t->before = t->after; 677 print_line_prefix(info); 678 fprintf(info->outfile, "<new thread %ld>\n", (long)t->tid); 679 } 680 681 static void 682 report_exit(struct trussinfo *info, siginfo_t *si) 683 { 684 struct threadinfo *t; 685 686 t = info->curthread; 687 clock_gettime(CLOCK_REALTIME, &t->after); 688 print_line_prefix(info); 689 if (si->si_code == CLD_EXITED) 690 fprintf(info->outfile, "process exit, rval = %u\n", 691 si->si_status); 692 else 693 fprintf(info->outfile, "process killed, signal = %u%s\n", 694 si->si_status, si->si_code == CLD_DUMPED ? 695 " (core dumped)" : ""); 696 } 697 698 static void 699 report_new_child(struct trussinfo *info) 700 { 701 struct threadinfo *t; 702 703 t = info->curthread; 704 clock_gettime(CLOCK_REALTIME, &t->after); 705 t->before = t->after; 706 print_line_prefix(info); 707 fprintf(info->outfile, "<new process>\n"); 708 } 709 710 void 711 decode_siginfo(FILE *fp, siginfo_t *si) 712 { 713 const char *str; 714 715 fprintf(fp, " code="); 716 str = sysdecode_sigcode(si->si_signo, si->si_code); 717 if (str == NULL) 718 fprintf(fp, "%d", si->si_code); 719 else 720 fprintf(fp, "%s", str); 721 switch (si->si_code) { 722 case SI_NOINFO: 723 break; 724 case SI_QUEUE: 725 fprintf(fp, " value=%p", si->si_value.sival_ptr); 726 /* FALLTHROUGH */ 727 case SI_USER: 728 case SI_LWP: 729 fprintf(fp, " pid=%jd uid=%jd", (intmax_t)si->si_pid, 730 (intmax_t)si->si_uid); 731 break; 732 case SI_TIMER: 733 fprintf(fp, " value=%p", si->si_value.sival_ptr); 734 fprintf(fp, " timerid=%d", si->si_timerid); 735 fprintf(fp, " overrun=%d", si->si_overrun); 736 if (si->si_errno != 0) 737 fprintf(fp, " errno=%d", si->si_errno); 738 break; 739 case SI_ASYNCIO: 740 fprintf(fp, " value=%p", si->si_value.sival_ptr); 741 break; 742 case SI_MESGQ: 743 fprintf(fp, " value=%p", si->si_value.sival_ptr); 744 fprintf(fp, " mqd=%d", si->si_mqd); 745 break; 746 default: 747 switch (si->si_signo) { 748 case SIGILL: 749 case SIGFPE: 750 case SIGSEGV: 751 case SIGBUS: 752 fprintf(fp, " trapno=%d", si->si_trapno); 753 fprintf(fp, " addr=%p", si->si_addr); 754 break; 755 case SIGCHLD: 756 fprintf(fp, " pid=%jd uid=%jd", (intmax_t)si->si_pid, 757 (intmax_t)si->si_uid); 758 fprintf(fp, " status=%d", si->si_status); 759 break; 760 } 761 } 762 } 763 764 static void 765 report_signal(struct trussinfo *info, siginfo_t *si, struct ptrace_lwpinfo *pl) 766 { 767 struct threadinfo *t; 768 const char *signame; 769 770 t = info->curthread; 771 clock_gettime(CLOCK_REALTIME, &t->after); 772 print_line_prefix(info); 773 signame = sysdecode_signal(si->si_status); 774 if (signame == NULL) 775 signame = "?"; 776 fprintf(info->outfile, "SIGNAL %u (%s)", si->si_status, signame); 777 if (pl->pl_event == PL_EVENT_SIGNAL && pl->pl_flags & PL_FLAG_SI) 778 decode_siginfo(info->outfile, &pl->pl_siginfo); 779 fprintf(info->outfile, "\n"); 780 781 } 782 783 /* 784 * Wait for events until all the processes have exited or truss has been 785 * asked to stop. 786 */ 787 void 788 eventloop(struct trussinfo *info) 789 { 790 struct ptrace_lwpinfo pl; 791 siginfo_t si; 792 int pending_signal; 793 794 while (!LIST_EMPTY(&info->proclist)) { 795 if (detaching) { 796 detach_all_procs(info); 797 return; 798 } 799 800 if (waitid(P_ALL, 0, &si, WTRAPPED | WEXITED) == -1) { 801 if (errno == EINTR) 802 continue; 803 err(1, "Unexpected error from waitid"); 804 } 805 806 assert(si.si_signo == SIGCHLD); 807 808 switch (si.si_code) { 809 case CLD_EXITED: 810 case CLD_KILLED: 811 case CLD_DUMPED: 812 find_exit_thread(info, si.si_pid); 813 if ((info->flags & COUNTONLY) == 0) { 814 if (si.si_code == CLD_EXITED) 815 thread_exit_syscall(info); 816 report_exit(info, &si); 817 } 818 free_proc(info->curthread->proc); 819 info->curthread = NULL; 820 break; 821 case CLD_TRAPPED: 822 if (ptrace(PT_LWPINFO, si.si_pid, (caddr_t)&pl, 823 sizeof(pl)) == -1) 824 err(1, "ptrace(PT_LWPINFO)"); 825 826 if (pl.pl_flags & PL_FLAG_CHILD) { 827 new_proc(info, si.si_pid, pl.pl_lwpid); 828 assert(LIST_FIRST(&info->proclist)->abi != 829 NULL); 830 } else if (pl.pl_flags & PL_FLAG_BORN) 831 new_thread(find_proc(info, si.si_pid), 832 pl.pl_lwpid); 833 find_thread(info, si.si_pid, pl.pl_lwpid); 834 835 if (si.si_status == SIGTRAP && 836 (pl.pl_flags & (PL_FLAG_BORN|PL_FLAG_EXITED| 837 PL_FLAG_SCE|PL_FLAG_SCX)) != 0) { 838 if (pl.pl_flags & PL_FLAG_BORN) { 839 if ((info->flags & COUNTONLY) == 0) 840 report_thread_birth(info); 841 } else if (pl.pl_flags & PL_FLAG_EXITED) { 842 if ((info->flags & COUNTONLY) == 0) 843 report_thread_death(info); 844 free_thread(info->curthread); 845 info->curthread = NULL; 846 } else if (pl.pl_flags & PL_FLAG_SCE) 847 enter_syscall(info, info->curthread, &pl); 848 else if (pl.pl_flags & PL_FLAG_SCX) 849 exit_syscall(info, &pl); 850 pending_signal = 0; 851 } else if (pl.pl_flags & PL_FLAG_CHILD) { 852 if ((info->flags & COUNTONLY) == 0) 853 report_new_child(info); 854 pending_signal = 0; 855 } else { 856 if ((info->flags & NOSIGS) == 0) 857 report_signal(info, &si, &pl); 858 pending_signal = si.si_status; 859 } 860 ptrace(PT_SYSCALL, si.si_pid, (caddr_t)1, 861 pending_signal); 862 break; 863 case CLD_STOPPED: 864 errx(1, "waitid reported CLD_STOPPED"); 865 case CLD_CONTINUED: 866 break; 867 } 868 } 869 } 870