2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright 1997 Sean Eric Fagan
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * Various setup functions for truss. Not the cleanest-written code,
42 #include <sys/ptrace.h>
43 #include <sys/sysctl.h>
56 #include <sysdecode.h>
64 SET_DECLARE(procabi, struct procabi);
66 static sig_atomic_t detaching;
68 static void enter_syscall(struct trussinfo *, struct threadinfo *,
69 struct ptrace_lwpinfo *);
70 static void new_proc(struct trussinfo *, pid_t, lwpid_t);
73 * setup_and_wait() is called to start a process. All it really does
74 * is fork(), enable tracing in the child, and then exec the given
75 * command. At that point, the child process stops, and the parent
76 * can wake up and deal with it.
79 setup_and_wait(struct trussinfo *info, char *command[])
85 err(1, "fork failed");
86 if (pid == 0) { /* Child */
87 ptrace(PT_TRACE_ME, 0, 0, 0);
88 execvp(command[0], command);
89 err(1, "execvp %s", command[0]);
92 /* Only in the parent here */
93 if (waitpid(pid, NULL, 0) < 0)
94 err(1, "unexpect stop in waitpid");
96 new_proc(info, pid, 0);
100 * start_tracing is called to attach to an existing process.
103 start_tracing(struct trussinfo *info, pid_t pid)
109 ret = ptrace(PT_ATTACH, pid, NULL, 0);
111 } while (ret && retry-- > 0);
113 err(1, "can not attach to target process");
115 if (waitpid(pid, NULL, 0) < 0)
116 err(1, "Unexpect stop in waitpid");
118 new_proc(info, pid, 0);
122 * Restore a process back to it's pre-truss state.
123 * Called for SIGINT, SIGTERM, SIGQUIT. This only
124 * applies if truss was told to monitor an already-existing
128 restore_proc(int signo __unused)
135 detach_proc(pid_t pid)
138 /* stop the child so that we can detach */
140 if (waitpid(pid, NULL, 0) < 0)
141 err(1, "Unexpected stop in waitpid");
143 if (ptrace(PT_DETACH, pid, (caddr_t)1, 0) < 0)
144 err(1, "Can not detach the process");
150 * Determine the ABI. This is called after every exec, and when
151 * a process is first monitored.
153 static struct procabi *
156 struct procabi **pabi;
165 mib[2] = KERN_PROC_SV_NAME;
167 error = sysctl(mib, 4, progt, &len, NULL, 0);
169 err(2, "can not get sysvec name");
171 SET_FOREACH(pabi, procabi) {
172 if (strcmp((*pabi)->type, progt) == 0)
175 warnx("ABI %s for pid %ld is not supported", progt, (long)pid);
179 static struct threadinfo *
180 new_thread(struct procinfo *p, lwpid_t lwpid)
182 struct threadinfo *nt;
185 * If this happens it means there is a bug in truss. Unfortunately
186 * this will kill any processes truss is attached to.
188 LIST_FOREACH(nt, &p->threadlist, entries) {
189 if (nt->tid == lwpid)
190 errx(1, "Duplicate thread for LWP %ld", (long)lwpid);
193 nt = calloc(1, sizeof(struct threadinfo));
195 err(1, "calloc() failed");
198 LIST_INSERT_HEAD(&p->threadlist, nt, entries);
203 free_thread(struct threadinfo *t)
206 LIST_REMOVE(t, entries);
211 add_threads(struct trussinfo *info, struct procinfo *p)
213 struct ptrace_lwpinfo pl;
214 struct threadinfo *t;
218 nlwps = ptrace(PT_GETNUMLWPS, p->pid, NULL, 0);
220 err(1, "Unable to fetch number of LWPs");
222 lwps = calloc(nlwps, sizeof(*lwps));
223 nlwps = ptrace(PT_GETLWPLIST, p->pid, (caddr_t)lwps, nlwps);
225 err(1, "Unable to fetch LWP list");
226 for (i = 0; i < nlwps; i++) {
227 t = new_thread(p, lwps[i]);
228 if (ptrace(PT_LWPINFO, lwps[i], (caddr_t)&pl, sizeof(pl)) == -1)
229 err(1, "ptrace(PT_LWPINFO)");
230 if (pl.pl_flags & PL_FLAG_SCE) {
232 enter_syscall(info, t, &pl);
239 new_proc(struct trussinfo *info, pid_t pid, lwpid_t lwpid)
244 * If this happens it means there is a bug in truss. Unfortunately
245 * this will kill any processes truss is attached to.
247 LIST_FOREACH(np, &info->proclist, entries) {
249 errx(1, "Duplicate process for pid %ld", (long)pid);
252 if (info->flags & FOLLOWFORKS)
253 if (ptrace(PT_FOLLOW_FORK, pid, NULL, 1) == -1)
254 err(1, "Unable to follow forks for pid %ld", (long)pid);
255 if (ptrace(PT_LWP_EVENTS, pid, NULL, 1) == -1)
256 err(1, "Unable to enable LWP events for pid %ld", (long)pid);
257 np = calloc(1, sizeof(struct procinfo));
259 np->abi = find_abi(pid);
260 LIST_INIT(&np->threadlist);
261 LIST_INSERT_HEAD(&info->proclist, np, entries);
264 new_thread(np, lwpid);
266 add_threads(info, np);
270 free_proc(struct procinfo *p)
272 struct threadinfo *t, *t2;
274 LIST_FOREACH_SAFE(t, &p->threadlist, entries, t2) {
277 LIST_REMOVE(p, entries);
282 detach_all_procs(struct trussinfo *info)
284 struct procinfo *p, *p2;
286 LIST_FOREACH_SAFE(p, &info->proclist, entries, p2) {
292 static struct procinfo *
293 find_proc(struct trussinfo *info, pid_t pid)
297 LIST_FOREACH(np, &info->proclist, entries) {
306 * Change curthread member based on (pid, lwpid).
309 find_thread(struct trussinfo *info, pid_t pid, lwpid_t lwpid)
312 struct threadinfo *nt;
314 np = find_proc(info, pid);
317 LIST_FOREACH(nt, &np->threadlist, entries) {
318 if (nt->tid == lwpid) {
319 info->curthread = nt;
323 errx(1, "could not find thread");
327 * When a process exits, it should have exactly one thread left.
328 * All of the other threads should have reported thread exit events.
331 find_exit_thread(struct trussinfo *info, pid_t pid)
335 p = find_proc(info, pid);
338 info->curthread = LIST_FIRST(&p->threadlist);
339 assert(info->curthread != NULL);
340 assert(LIST_NEXT(info->curthread, entries) == NULL);
344 alloc_syscall(struct threadinfo *t, struct ptrace_lwpinfo *pl)
348 assert(t->in_syscall == 0);
349 assert(t->cs.number == 0);
350 assert(t->cs.sc == NULL);
351 assert(t->cs.nargs == 0);
352 for (i = 0; i < nitems(t->cs.s_args); i++)
353 assert(t->cs.s_args[i] == NULL);
354 memset(t->cs.args, 0, sizeof(t->cs.args));
355 t->cs.number = pl->pl_syscall_code;
360 free_syscall(struct threadinfo *t)
364 for (i = 0; i < t->cs.nargs; i++)
365 free(t->cs.s_args[i]);
366 memset(&t->cs, 0, sizeof(t->cs));
371 enter_syscall(struct trussinfo *info, struct threadinfo *t,
372 struct ptrace_lwpinfo *pl)
377 alloc_syscall(t, pl);
378 narg = MIN(pl->pl_syscall_narg, nitems(t->cs.args));
379 if (narg != 0 && t->proc->abi->fetch_args(info, narg) != 0) {
384 sc = get_syscall(t, t->cs.number, narg);
386 fprintf(info->outfile, "-- UNKNOWN %s SYSCALL %d --\n",
387 t->proc->abi->type, t->cs.number);
389 t->cs.nargs = sc->nargs;
390 assert(sc->nargs <= nitems(t->cs.s_args));
395 * At this point, we set up the system call arguments.
396 * We ignore any OUT ones, however -- those are arguments that
397 * are set by the system call, and so are probably meaningless
398 * now. This doesn't currently support arguments that are
399 * passed in *and* out, however.
402 fprintf(stderr, "syscall %s(", sc->name);
404 for (i = 0; i < t->cs.nargs; i++) {
406 fprintf(stderr, "0x%lx%s", t->cs.args[sc->args[i].offset],
407 i < (t->cs.nargs - 1) ? "," : "");
409 if (!(sc->args[i].type & OUT)) {
410 t->cs.s_args[i] = print_arg(&sc->args[i],
411 t->cs.args, 0, info);
415 fprintf(stderr, ")\n");
418 clock_gettime(CLOCK_REALTIME, &t->before);
422 * When a thread exits voluntarily (including when a thread calls
423 * exit() to trigger a process exit), the thread's internal state
424 * holds the arguments passed to the exit system call. When the
425 * thread's exit is reported, log that system call without a return
429 thread_exit_syscall(struct trussinfo *info)
431 struct threadinfo *t;
437 clock_gettime(CLOCK_REALTIME, &t->after);
439 print_syscall_ret(info, 0, NULL);
444 exit_syscall(struct trussinfo *info, struct ptrace_lwpinfo *pl)
446 struct threadinfo *t;
457 clock_gettime(CLOCK_REALTIME, &t->after);
459 if (p->abi->fetch_retval(info, retval, &errorp) < 0) {
466 * Here, we only look for arguments that have OUT masked in --
467 * otherwise, they were handled in enter_syscall().
469 for (i = 0; i < sc->nargs; i++) {
472 if (sc->args[i].type & OUT) {
474 * If an error occurred, then don't bother
475 * getting the data; it may not be valid.
478 asprintf(&temp, "0x%lx",
479 t->cs.args[sc->args[i].offset]);
481 temp = print_arg(&sc->args[i],
482 t->cs.args, retval, info);
484 t->cs.s_args[i] = temp;
488 print_syscall_ret(info, errorp, retval);
492 * If the process executed a new image, check the ABI. If the
493 * new ABI isn't supported, stop tracing this process.
495 if (pl->pl_flags & PL_FLAG_EXEC) {
496 assert(LIST_NEXT(LIST_FIRST(&p->threadlist), entries) == NULL);
497 p->abi = find_abi(p->pid);
498 if (p->abi == NULL) {
499 if (ptrace(PT_DETACH, p->pid, (caddr_t)1, 0) < 0)
500 err(1, "Can not detach the process");
507 print_line_prefix(struct trussinfo *info)
509 struct timespec timediff;
510 struct threadinfo *t;
515 if (info->flags & (FOLLOWFORKS | DISPLAYTIDS)) {
516 if (info->flags & FOLLOWFORKS)
517 len += fprintf(info->outfile, "%5d", t->proc->pid);
518 if ((info->flags & (FOLLOWFORKS | DISPLAYTIDS)) ==
519 (FOLLOWFORKS | DISPLAYTIDS))
520 len += fprintf(info->outfile, " ");
521 if (info->flags & DISPLAYTIDS)
522 len += fprintf(info->outfile, "%6d", t->tid);
523 len += fprintf(info->outfile, ": ");
525 if (info->flags & ABSOLUTETIMESTAMPS) {
526 timespecsub(&t->after, &info->start_time, &timediff);
527 len += fprintf(info->outfile, "%jd.%09ld ",
528 (intmax_t)timediff.tv_sec, timediff.tv_nsec);
530 if (info->flags & RELATIVETIMESTAMPS) {
531 timespecsub(&t->after, &t->before, &timediff);
532 len += fprintf(info->outfile, "%jd.%09ld ",
533 (intmax_t)timediff.tv_sec, timediff.tv_nsec);
539 report_thread_death(struct trussinfo *info)
541 struct threadinfo *t;
544 clock_gettime(CLOCK_REALTIME, &t->after);
545 print_line_prefix(info);
546 fprintf(info->outfile, "<thread %ld exited>\n", (long)t->tid);
550 report_thread_birth(struct trussinfo *info)
552 struct threadinfo *t;
555 clock_gettime(CLOCK_REALTIME, &t->after);
556 t->before = t->after;
557 print_line_prefix(info);
558 fprintf(info->outfile, "<new thread %ld>\n", (long)t->tid);
562 report_exit(struct trussinfo *info, siginfo_t *si)
564 struct threadinfo *t;
567 clock_gettime(CLOCK_REALTIME, &t->after);
568 print_line_prefix(info);
569 if (si->si_code == CLD_EXITED)
570 fprintf(info->outfile, "process exit, rval = %u\n",
573 fprintf(info->outfile, "process killed, signal = %u%s\n",
574 si->si_status, si->si_code == CLD_DUMPED ?
575 " (core dumped)" : "");
579 report_new_child(struct trussinfo *info)
581 struct threadinfo *t;
584 clock_gettime(CLOCK_REALTIME, &t->after);
585 t->before = t->after;
586 print_line_prefix(info);
587 fprintf(info->outfile, "<new process>\n");
591 decode_siginfo(FILE *fp, siginfo_t *si)
595 fprintf(fp, " code=");
596 str = sysdecode_sigcode(si->si_signo, si->si_code);
598 fprintf(fp, "%d", si->si_code);
600 fprintf(fp, "%s", str);
601 switch (si->si_code) {
605 fprintf(fp, " value=%p", si->si_value.sival_ptr);
609 fprintf(fp, " pid=%jd uid=%jd", (intmax_t)si->si_pid,
610 (intmax_t)si->si_uid);
613 fprintf(fp, " value=%p", si->si_value.sival_ptr);
614 fprintf(fp, " timerid=%d", si->si_timerid);
615 fprintf(fp, " overrun=%d", si->si_overrun);
616 if (si->si_errno != 0)
617 fprintf(fp, " errno=%d", si->si_errno);
620 fprintf(fp, " value=%p", si->si_value.sival_ptr);
623 fprintf(fp, " value=%p", si->si_value.sival_ptr);
624 fprintf(fp, " mqd=%d", si->si_mqd);
627 switch (si->si_signo) {
632 fprintf(fp, " trapno=%d", si->si_trapno);
633 fprintf(fp, " addr=%p", si->si_addr);
636 fprintf(fp, " pid=%jd uid=%jd", (intmax_t)si->si_pid,
637 (intmax_t)si->si_uid);
638 fprintf(fp, " status=%d", si->si_status);
645 report_signal(struct trussinfo *info, siginfo_t *si, struct ptrace_lwpinfo *pl)
647 struct threadinfo *t;
651 clock_gettime(CLOCK_REALTIME, &t->after);
652 print_line_prefix(info);
653 signame = sysdecode_signal(si->si_status);
656 fprintf(info->outfile, "SIGNAL %u (%s)", si->si_status, signame);
657 if (pl->pl_event == PL_EVENT_SIGNAL && pl->pl_flags & PL_FLAG_SI)
658 decode_siginfo(info->outfile, &pl->pl_siginfo);
659 fprintf(info->outfile, "\n");
664 * Wait for events until all the processes have exited or truss has been
668 eventloop(struct trussinfo *info)
670 struct ptrace_lwpinfo pl;
674 while (!LIST_EMPTY(&info->proclist)) {
676 detach_all_procs(info);
680 if (waitid(P_ALL, 0, &si, WTRAPPED | WEXITED) == -1) {
683 err(1, "Unexpected error from waitid");
686 assert(si.si_signo == SIGCHLD);
688 switch (si.si_code) {
692 find_exit_thread(info, si.si_pid);
693 if ((info->flags & COUNTONLY) == 0) {
694 if (si.si_code == CLD_EXITED)
695 thread_exit_syscall(info);
696 report_exit(info, &si);
698 free_proc(info->curthread->proc);
699 info->curthread = NULL;
702 if (ptrace(PT_LWPINFO, si.si_pid, (caddr_t)&pl,
704 err(1, "ptrace(PT_LWPINFO)");
706 if (pl.pl_flags & PL_FLAG_CHILD) {
707 new_proc(info, si.si_pid, pl.pl_lwpid);
708 assert(LIST_FIRST(&info->proclist)->abi !=
710 } else if (pl.pl_flags & PL_FLAG_BORN)
711 new_thread(find_proc(info, si.si_pid),
713 find_thread(info, si.si_pid, pl.pl_lwpid);
715 if (si.si_status == SIGTRAP &&
716 (pl.pl_flags & (PL_FLAG_BORN|PL_FLAG_EXITED|
717 PL_FLAG_SCE|PL_FLAG_SCX)) != 0) {
718 if (pl.pl_flags & PL_FLAG_BORN) {
719 if ((info->flags & COUNTONLY) == 0)
720 report_thread_birth(info);
721 } else if (pl.pl_flags & PL_FLAG_EXITED) {
722 if ((info->flags & COUNTONLY) == 0)
723 report_thread_death(info);
724 free_thread(info->curthread);
725 info->curthread = NULL;
726 } else if (pl.pl_flags & PL_FLAG_SCE)
727 enter_syscall(info, info->curthread, &pl);
728 else if (pl.pl_flags & PL_FLAG_SCX)
729 exit_syscall(info, &pl);
731 } else if (pl.pl_flags & PL_FLAG_CHILD) {
732 if ((info->flags & COUNTONLY) == 0)
733 report_new_child(info);
736 if ((info->flags & NOSIGS) == 0)
737 report_signal(info, &si, &pl);
738 pending_signal = si.si_status;
740 ptrace(PT_SYSCALL, si.si_pid, (caddr_t)1,
744 errx(1, "waitid reported CLD_STOPPED");