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 struct procabi_table {
69 static sig_atomic_t detaching;
71 static void enter_syscall(struct trussinfo *, struct threadinfo *,
72 struct ptrace_lwpinfo *);
73 static void new_proc(struct trussinfo *, pid_t, lwpid_t);
76 static struct procabi cloudabi32 = {
78 .abi = SYSDECODE_ABI_CLOUDABI32,
79 .pointer_size = sizeof(uint32_t),
80 .extra_syscalls = STAILQ_HEAD_INITIALIZER(cloudabi32.extra_syscalls),
84 static struct procabi cloudabi64 = {
86 .abi = SYSDECODE_ABI_CLOUDABI64,
87 .pointer_size = sizeof(uint64_t),
88 .extra_syscalls = STAILQ_HEAD_INITIALIZER(cloudabi64.extra_syscalls),
92 static struct procabi freebsd = {
94 .abi = SYSDECODE_ABI_FREEBSD,
95 .pointer_size = sizeof(void *),
96 .extra_syscalls = STAILQ_HEAD_INITIALIZER(freebsd.extra_syscalls),
100 #if !defined(__SIZEOF_POINTER__)
101 #error "Use a modern compiler."
104 #if __SIZEOF_POINTER__ > 4
105 static struct procabi freebsd32 = {
107 .abi = SYSDECODE_ABI_FREEBSD32,
108 .pointer_size = sizeof(uint32_t),
109 .compat_prefix = "freebsd32",
110 .extra_syscalls = STAILQ_HEAD_INITIALIZER(freebsd32.extra_syscalls),
115 static struct procabi linux = {
117 .abi = SYSDECODE_ABI_LINUX,
118 .pointer_size = sizeof(void *),
119 .extra_syscalls = STAILQ_HEAD_INITIALIZER(linux.extra_syscalls),
123 #if __SIZEOF_POINTER__ > 4
124 static struct procabi linux32 = {
126 .abi = SYSDECODE_ABI_LINUX32,
127 .pointer_size = sizeof(uint32_t),
128 .extra_syscalls = STAILQ_HEAD_INITIALIZER(linux32.extra_syscalls),
133 static struct procabi_table abis[] = {
134 { "CloudABI ELF32", &cloudabi32 },
135 { "CloudABI ELF64", &cloudabi64 },
136 #if __SIZEOF_POINTER__ == 4
137 { "FreeBSD ELF32", &freebsd },
138 #elif __SIZEOF_POINTER__ == 8
139 { "FreeBSD ELF64", &freebsd },
140 { "FreeBSD ELF32", &freebsd32 },
142 #error "Unsupported pointer size"
144 #if defined(__powerpc64__)
145 { "FreeBSD ELF64 V2", &freebsd },
147 #if defined(__amd64__)
148 { "FreeBSD a.out", &freebsd32 },
150 #if defined(__i386__)
151 { "FreeBSD a.out", &freebsd },
153 #if __SIZEOF_POINTER__ >= 8
154 { "Linux ELF64", &linux },
155 { "Linux ELF32", &linux32 },
157 { "Linux ELF32", &linux },
162 * setup_and_wait() is called to start a process. All it really does
163 * is fork(), enable tracing in the child, and then exec the given
164 * command. At that point, the child process stops, and the parent
165 * can wake up and deal with it.
168 setup_and_wait(struct trussinfo *info, char *command[])
174 err(1, "fork failed");
175 if (pid == 0) { /* Child */
176 ptrace(PT_TRACE_ME, 0, 0, 0);
177 execvp(command[0], command);
178 err(1, "execvp %s", command[0]);
181 /* Only in the parent here */
182 if (waitpid(pid, NULL, 0) < 0)
183 err(1, "unexpect stop in waitpid");
185 new_proc(info, pid, 0);
189 * start_tracing is called to attach to an existing process.
192 start_tracing(struct trussinfo *info, pid_t pid)
198 ret = ptrace(PT_ATTACH, pid, NULL, 0);
200 } while (ret && retry-- > 0);
202 err(1, "can not attach to target process");
204 if (waitpid(pid, NULL, 0) < 0)
205 err(1, "Unexpect stop in waitpid");
207 new_proc(info, pid, 0);
211 * Restore a process back to it's pre-truss state.
212 * Called for SIGINT, SIGTERM, SIGQUIT. This only
213 * applies if truss was told to monitor an already-existing
217 restore_proc(int signo __unused)
224 detach_proc(pid_t pid)
227 /* stop the child so that we can detach */
229 if (waitpid(pid, NULL, 0) < 0)
230 err(1, "Unexpected stop in waitpid");
232 if (ptrace(PT_DETACH, pid, (caddr_t)1, 0) < 0)
233 err(1, "Can not detach the process");
239 * Determine the ABI. This is called after every exec, and when
240 * a process is first monitored.
242 static struct procabi *
254 mib[2] = KERN_PROC_SV_NAME;
256 error = sysctl(mib, 4, progt, &len, NULL, 0);
258 err(2, "can not get sysvec name");
260 for (i = 0; i < nitems(abis); i++) {
261 if (strcmp(abis[i].name, progt) == 0)
262 return (abis[i].abi);
264 warnx("ABI %s for pid %ld is not supported", progt, (long)pid);
268 static struct threadinfo *
269 new_thread(struct procinfo *p, lwpid_t lwpid)
271 struct threadinfo *nt;
274 * If this happens it means there is a bug in truss. Unfortunately
275 * this will kill any processes truss is attached to.
277 LIST_FOREACH(nt, &p->threadlist, entries) {
278 if (nt->tid == lwpid)
279 errx(1, "Duplicate thread for LWP %ld", (long)lwpid);
282 nt = calloc(1, sizeof(struct threadinfo));
284 err(1, "calloc() failed");
287 LIST_INSERT_HEAD(&p->threadlist, nt, entries);
292 free_thread(struct threadinfo *t)
295 LIST_REMOVE(t, entries);
300 add_threads(struct trussinfo *info, struct procinfo *p)
302 struct ptrace_lwpinfo pl;
303 struct threadinfo *t;
307 nlwps = ptrace(PT_GETNUMLWPS, p->pid, NULL, 0);
309 err(1, "Unable to fetch number of LWPs");
311 lwps = calloc(nlwps, sizeof(*lwps));
312 nlwps = ptrace(PT_GETLWPLIST, p->pid, (caddr_t)lwps, nlwps);
314 err(1, "Unable to fetch LWP list");
315 for (i = 0; i < nlwps; i++) {
316 t = new_thread(p, lwps[i]);
317 if (ptrace(PT_LWPINFO, lwps[i], (caddr_t)&pl, sizeof(pl)) == -1)
318 err(1, "ptrace(PT_LWPINFO)");
319 if (pl.pl_flags & PL_FLAG_SCE) {
321 enter_syscall(info, t, &pl);
328 new_proc(struct trussinfo *info, pid_t pid, lwpid_t lwpid)
333 * If this happens it means there is a bug in truss. Unfortunately
334 * this will kill any processes truss is attached to.
336 LIST_FOREACH(np, &info->proclist, entries) {
338 errx(1, "Duplicate process for pid %ld", (long)pid);
341 if (info->flags & FOLLOWFORKS)
342 if (ptrace(PT_FOLLOW_FORK, pid, NULL, 1) == -1)
343 err(1, "Unable to follow forks for pid %ld", (long)pid);
344 if (ptrace(PT_LWP_EVENTS, pid, NULL, 1) == -1)
345 err(1, "Unable to enable LWP events for pid %ld", (long)pid);
346 np = calloc(1, sizeof(struct procinfo));
348 np->abi = find_abi(pid);
349 LIST_INIT(&np->threadlist);
350 LIST_INSERT_HEAD(&info->proclist, np, entries);
353 new_thread(np, lwpid);
355 add_threads(info, np);
359 free_proc(struct procinfo *p)
361 struct threadinfo *t, *t2;
363 LIST_FOREACH_SAFE(t, &p->threadlist, entries, t2) {
366 LIST_REMOVE(p, entries);
371 detach_all_procs(struct trussinfo *info)
373 struct procinfo *p, *p2;
375 LIST_FOREACH_SAFE(p, &info->proclist, entries, p2) {
381 static struct procinfo *
382 find_proc(struct trussinfo *info, pid_t pid)
386 LIST_FOREACH(np, &info->proclist, entries) {
395 * Change curthread member based on (pid, lwpid).
398 find_thread(struct trussinfo *info, pid_t pid, lwpid_t lwpid)
401 struct threadinfo *nt;
403 np = find_proc(info, pid);
406 LIST_FOREACH(nt, &np->threadlist, entries) {
407 if (nt->tid == lwpid) {
408 info->curthread = nt;
412 errx(1, "could not find thread");
416 * When a process exits, it should have exactly one thread left.
417 * All of the other threads should have reported thread exit events.
420 find_exit_thread(struct trussinfo *info, pid_t pid)
424 p = find_proc(info, pid);
427 info->curthread = LIST_FIRST(&p->threadlist);
428 assert(info->curthread != NULL);
429 assert(LIST_NEXT(info->curthread, entries) == NULL);
433 alloc_syscall(struct threadinfo *t, struct ptrace_lwpinfo *pl)
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;
449 free_syscall(struct threadinfo *t)
453 for (i = 0; i < t->cs.nargs; i++)
454 free(t->cs.s_args[i]);
455 memset(&t->cs, 0, sizeof(t->cs));
460 enter_syscall(struct trussinfo *info, struct threadinfo *t,
461 struct ptrace_lwpinfo *pl)
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) {
474 sc = get_syscall(t, t->cs.number, narg);
476 fprintf(info->outfile, "-- UNKNOWN %s SYSCALL %d --\n",
477 t->proc->abi->type, t->cs.number);
479 t->cs.nargs = sc->decode.nargs;
480 assert(sc->decode.nargs <= nitems(t->cs.s_args));
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.
492 fprintf(stderr, "syscall %s(", sc->name);
494 for (i = 0; i < t->cs.nargs; i++) {
496 fprintf(stderr, "0x%lx%s",
497 t->cs.args[sc->decode.args[i].offset],
498 i < (t->cs.nargs - 1) ? "," : "");
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);
506 fprintf(stderr, ")\n");
509 clock_gettime(CLOCK_REALTIME, &t->before);
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
520 thread_exit_syscall(struct trussinfo *info)
522 struct threadinfo *t;
528 clock_gettime(CLOCK_REALTIME, &t->after);
530 print_syscall_ret(info, 0, NULL);
535 exit_syscall(struct trussinfo *info, struct ptrace_lwpinfo *pl)
537 struct threadinfo *t;
540 struct ptrace_sc_ret psr;
547 clock_gettime(CLOCK_REALTIME, &t->after);
549 if (ptrace(PT_GET_SC_RET, t->tid, (caddr_t)&psr, sizeof(psr)) != 0) {
556 * Here, we only look for arguments that have OUT masked in --
557 * otherwise, they were handled in enter_syscall().
559 for (i = 0; i < sc->decode.nargs; i++) {
562 if (sc->decode.args[i].type & OUT) {
564 * If an error occurred, then don't bother
565 * getting the data; it may not be valid.
567 if (psr.sr_error != 0) {
568 asprintf(&temp, "0x%lx",
569 t->cs.args[sc->decode.args[i].offset]);
571 temp = print_arg(&sc->decode.args[i],
572 t->cs.args, psr.sr_retval, info);
574 t->cs.s_args[i] = temp;
578 print_syscall_ret(info, psr.sr_error, psr.sr_retval);
582 * If the process executed a new image, check the ABI. If the
583 * new ABI isn't supported, stop tracing this process.
585 if (pl->pl_flags & PL_FLAG_EXEC) {
586 assert(LIST_NEXT(LIST_FIRST(&p->threadlist), entries) == NULL);
587 p->abi = find_abi(p->pid);
588 if (p->abi == NULL) {
589 if (ptrace(PT_DETACH, p->pid, (caddr_t)1, 0) < 0)
590 err(1, "Can not detach the process");
597 print_line_prefix(struct trussinfo *info)
599 struct timespec timediff;
600 struct threadinfo *t;
605 if (info->flags & (FOLLOWFORKS | DISPLAYTIDS)) {
606 if (info->flags & FOLLOWFORKS)
607 len += fprintf(info->outfile, "%5d", t->proc->pid);
608 if ((info->flags & (FOLLOWFORKS | DISPLAYTIDS)) ==
609 (FOLLOWFORKS | DISPLAYTIDS))
610 len += fprintf(info->outfile, " ");
611 if (info->flags & DISPLAYTIDS)
612 len += fprintf(info->outfile, "%6d", t->tid);
613 len += fprintf(info->outfile, ": ");
615 if (info->flags & ABSOLUTETIMESTAMPS) {
616 timespecsub(&t->after, &info->start_time, &timediff);
617 len += fprintf(info->outfile, "%jd.%09ld ",
618 (intmax_t)timediff.tv_sec, timediff.tv_nsec);
620 if (info->flags & RELATIVETIMESTAMPS) {
621 timespecsub(&t->after, &t->before, &timediff);
622 len += fprintf(info->outfile, "%jd.%09ld ",
623 (intmax_t)timediff.tv_sec, timediff.tv_nsec);
629 report_thread_death(struct trussinfo *info)
631 struct threadinfo *t;
634 clock_gettime(CLOCK_REALTIME, &t->after);
635 print_line_prefix(info);
636 fprintf(info->outfile, "<thread %ld exited>\n", (long)t->tid);
640 report_thread_birth(struct trussinfo *info)
642 struct threadinfo *t;
645 clock_gettime(CLOCK_REALTIME, &t->after);
646 t->before = t->after;
647 print_line_prefix(info);
648 fprintf(info->outfile, "<new thread %ld>\n", (long)t->tid);
652 report_exit(struct trussinfo *info, siginfo_t *si)
654 struct threadinfo *t;
657 clock_gettime(CLOCK_REALTIME, &t->after);
658 print_line_prefix(info);
659 if (si->si_code == CLD_EXITED)
660 fprintf(info->outfile, "process exit, rval = %u\n",
663 fprintf(info->outfile, "process killed, signal = %u%s\n",
664 si->si_status, si->si_code == CLD_DUMPED ?
665 " (core dumped)" : "");
669 report_new_child(struct trussinfo *info)
671 struct threadinfo *t;
674 clock_gettime(CLOCK_REALTIME, &t->after);
675 t->before = t->after;
676 print_line_prefix(info);
677 fprintf(info->outfile, "<new process>\n");
681 decode_siginfo(FILE *fp, siginfo_t *si)
685 fprintf(fp, " code=");
686 str = sysdecode_sigcode(si->si_signo, si->si_code);
688 fprintf(fp, "%d", si->si_code);
690 fprintf(fp, "%s", str);
691 switch (si->si_code) {
695 fprintf(fp, " value=%p", si->si_value.sival_ptr);
699 fprintf(fp, " pid=%jd uid=%jd", (intmax_t)si->si_pid,
700 (intmax_t)si->si_uid);
703 fprintf(fp, " value=%p", si->si_value.sival_ptr);
704 fprintf(fp, " timerid=%d", si->si_timerid);
705 fprintf(fp, " overrun=%d", si->si_overrun);
706 if (si->si_errno != 0)
707 fprintf(fp, " errno=%d", si->si_errno);
710 fprintf(fp, " value=%p", si->si_value.sival_ptr);
713 fprintf(fp, " value=%p", si->si_value.sival_ptr);
714 fprintf(fp, " mqd=%d", si->si_mqd);
717 switch (si->si_signo) {
722 fprintf(fp, " trapno=%d", si->si_trapno);
723 fprintf(fp, " addr=%p", si->si_addr);
726 fprintf(fp, " pid=%jd uid=%jd", (intmax_t)si->si_pid,
727 (intmax_t)si->si_uid);
728 fprintf(fp, " status=%d", si->si_status);
735 report_signal(struct trussinfo *info, siginfo_t *si, struct ptrace_lwpinfo *pl)
737 struct threadinfo *t;
741 clock_gettime(CLOCK_REALTIME, &t->after);
742 print_line_prefix(info);
743 signame = sysdecode_signal(si->si_status);
746 fprintf(info->outfile, "SIGNAL %u (%s)", si->si_status, signame);
747 if (pl->pl_event == PL_EVENT_SIGNAL && pl->pl_flags & PL_FLAG_SI)
748 decode_siginfo(info->outfile, &pl->pl_siginfo);
749 fprintf(info->outfile, "\n");
754 * Wait for events until all the processes have exited or truss has been
758 eventloop(struct trussinfo *info)
760 struct ptrace_lwpinfo pl;
764 while (!LIST_EMPTY(&info->proclist)) {
766 detach_all_procs(info);
770 if (waitid(P_ALL, 0, &si, WTRAPPED | WEXITED) == -1) {
773 err(1, "Unexpected error from waitid");
776 assert(si.si_signo == SIGCHLD);
778 switch (si.si_code) {
782 find_exit_thread(info, si.si_pid);
783 if ((info->flags & COUNTONLY) == 0) {
784 if (si.si_code == CLD_EXITED)
785 thread_exit_syscall(info);
786 report_exit(info, &si);
788 free_proc(info->curthread->proc);
789 info->curthread = NULL;
792 if (ptrace(PT_LWPINFO, si.si_pid, (caddr_t)&pl,
794 err(1, "ptrace(PT_LWPINFO)");
796 if (pl.pl_flags & PL_FLAG_CHILD) {
797 new_proc(info, si.si_pid, pl.pl_lwpid);
798 assert(LIST_FIRST(&info->proclist)->abi !=
800 } else if (pl.pl_flags & PL_FLAG_BORN)
801 new_thread(find_proc(info, si.si_pid),
803 find_thread(info, si.si_pid, pl.pl_lwpid);
805 if (si.si_status == SIGTRAP &&
806 (pl.pl_flags & (PL_FLAG_BORN|PL_FLAG_EXITED|
807 PL_FLAG_SCE|PL_FLAG_SCX)) != 0) {
808 if (pl.pl_flags & PL_FLAG_BORN) {
809 if ((info->flags & COUNTONLY) == 0)
810 report_thread_birth(info);
811 } else if (pl.pl_flags & PL_FLAG_EXITED) {
812 if ((info->flags & COUNTONLY) == 0)
813 report_thread_death(info);
814 free_thread(info->curthread);
815 info->curthread = NULL;
816 } else if (pl.pl_flags & PL_FLAG_SCE)
817 enter_syscall(info, info->curthread, &pl);
818 else if (pl.pl_flags & PL_FLAG_SCX)
819 exit_syscall(info, &pl);
821 } else if (pl.pl_flags & PL_FLAG_CHILD) {
822 if ((info->flags & COUNTONLY) == 0)
823 report_new_child(info);
826 if ((info->flags & NOSIGS) == 0)
827 report_signal(info, &si, &pl);
828 pending_signal = si.si_status;
830 ptrace(PT_SYSCALL, si.si_pid, (caddr_t)1,
834 errx(1, "waitid reported CLD_STOPPED");