2 * Copyright 1997 Sean Eric Fagan
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Sean Eric Fagan
15 * 4. Neither the name of the author may be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 * Various setup functions for truss. Not the cleanest-written code,
40 #include <sys/ptrace.h>
41 #include <sys/sysctl.h>
53 #include <sysdecode.h>
61 SET_DECLARE(procabi, struct procabi);
63 static sig_atomic_t detaching;
65 static void enter_syscall(struct trussinfo *, struct threadinfo *,
66 struct ptrace_lwpinfo *);
67 static void new_proc(struct trussinfo *, pid_t, lwpid_t);
70 * setup_and_wait() is called to start a process. All it really does
71 * is fork(), enable tracing in the child, and then exec the given
72 * command. At that point, the child process stops, and the parent
73 * can wake up and deal with it.
76 setup_and_wait(struct trussinfo *info, char *command[])
82 err(1, "fork failed");
83 if (pid == 0) { /* Child */
84 ptrace(PT_TRACE_ME, 0, 0, 0);
85 execvp(command[0], command);
86 err(1, "execvp %s", command[0]);
89 /* Only in the parent here */
90 if (waitpid(pid, NULL, 0) < 0)
91 err(1, "unexpect stop in waitpid");
93 new_proc(info, pid, 0);
97 * start_tracing is called to attach to an existing process.
100 start_tracing(struct trussinfo *info, pid_t pid)
106 ret = ptrace(PT_ATTACH, pid, NULL, 0);
108 } while (ret && retry-- > 0);
110 err(1, "can not attach to target process");
112 if (waitpid(pid, NULL, 0) < 0)
113 err(1, "Unexpect stop in waitpid");
115 new_proc(info, pid, 0);
119 * Restore a process back to it's pre-truss state.
120 * Called for SIGINT, SIGTERM, SIGQUIT. This only
121 * applies if truss was told to monitor an already-existing
125 restore_proc(int signo __unused)
132 detach_proc(pid_t pid)
135 /* stop the child so that we can detach */
137 if (waitpid(pid, NULL, 0) < 0)
138 err(1, "Unexpected stop in waitpid");
140 if (ptrace(PT_DETACH, pid, (caddr_t)1, 0) < 0)
141 err(1, "Can not detach the process");
147 * Determine the ABI. This is called after every exec, and when
148 * a process is first monitored.
150 static struct procabi *
153 struct procabi **pabi;
162 mib[2] = KERN_PROC_SV_NAME;
164 error = sysctl(mib, 4, progt, &len, NULL, 0);
166 err(2, "can not get sysvec name");
168 SET_FOREACH(pabi, procabi) {
169 if (strcmp((*pabi)->type, progt) == 0)
172 warnx("ABI %s for pid %ld is not supported", progt, (long)pid);
176 static struct threadinfo *
177 new_thread(struct procinfo *p, lwpid_t lwpid)
179 struct threadinfo *nt;
182 * If this happens it means there is a bug in truss. Unfortunately
183 * this will kill any processes truss is attached to.
185 LIST_FOREACH(nt, &p->threadlist, entries) {
186 if (nt->tid == lwpid)
187 errx(1, "Duplicate thread for LWP %ld", (long)lwpid);
190 nt = calloc(1, sizeof(struct threadinfo));
192 err(1, "calloc() failed");
195 LIST_INSERT_HEAD(&p->threadlist, nt, entries);
200 free_thread(struct threadinfo *t)
203 LIST_REMOVE(t, entries);
208 add_threads(struct trussinfo *info, struct procinfo *p)
210 struct ptrace_lwpinfo pl;
211 struct threadinfo *t;
215 nlwps = ptrace(PT_GETNUMLWPS, p->pid, NULL, 0);
217 err(1, "Unable to fetch number of LWPs");
219 lwps = calloc(nlwps, sizeof(*lwps));
220 nlwps = ptrace(PT_GETLWPLIST, p->pid, (caddr_t)lwps, nlwps);
222 err(1, "Unable to fetch LWP list");
223 for (i = 0; i < nlwps; i++) {
224 t = new_thread(p, lwps[i]);
225 if (ptrace(PT_LWPINFO, lwps[i], (caddr_t)&pl, sizeof(pl)) == -1)
226 err(1, "ptrace(PT_LWPINFO)");
227 if (pl.pl_flags & PL_FLAG_SCE) {
229 enter_syscall(info, t, &pl);
236 new_proc(struct trussinfo *info, pid_t pid, lwpid_t lwpid)
241 * If this happens it means there is a bug in truss. Unfortunately
242 * this will kill any processes truss is attached to.
244 LIST_FOREACH(np, &info->proclist, entries) {
246 errx(1, "Duplicate process for pid %ld", (long)pid);
249 if (info->flags & FOLLOWFORKS)
250 if (ptrace(PT_FOLLOW_FORK, pid, NULL, 1) == -1)
251 err(1, "Unable to follow forks for pid %ld", (long)pid);
252 if (ptrace(PT_LWP_EVENTS, pid, NULL, 1) == -1)
253 err(1, "Unable to enable LWP events for pid %ld", (long)pid);
254 np = calloc(1, sizeof(struct procinfo));
256 np->abi = find_abi(pid);
257 LIST_INIT(&np->threadlist);
258 LIST_INSERT_HEAD(&info->proclist, np, entries);
261 new_thread(np, lwpid);
263 add_threads(info, np);
267 free_proc(struct procinfo *p)
269 struct threadinfo *t, *t2;
271 LIST_FOREACH_SAFE(t, &p->threadlist, entries, t2) {
274 LIST_REMOVE(p, entries);
279 detach_all_procs(struct trussinfo *info)
281 struct procinfo *p, *p2;
283 LIST_FOREACH_SAFE(p, &info->proclist, entries, p2) {
289 static struct procinfo *
290 find_proc(struct trussinfo *info, pid_t pid)
294 LIST_FOREACH(np, &info->proclist, entries) {
303 * Change curthread member based on (pid, lwpid).
306 find_thread(struct trussinfo *info, pid_t pid, lwpid_t lwpid)
309 struct threadinfo *nt;
311 np = find_proc(info, pid);
314 LIST_FOREACH(nt, &np->threadlist, entries) {
315 if (nt->tid == lwpid) {
316 info->curthread = nt;
320 errx(1, "could not find thread");
324 * When a process exits, it should have exactly one thread left.
325 * All of the other threads should have reported thread exit events.
328 find_exit_thread(struct trussinfo *info, pid_t pid)
332 p = find_proc(info, pid);
335 info->curthread = LIST_FIRST(&p->threadlist);
336 assert(info->curthread != NULL);
337 assert(LIST_NEXT(info->curthread, entries) == NULL);
341 alloc_syscall(struct threadinfo *t, struct ptrace_lwpinfo *pl)
345 assert(t->in_syscall == 0);
346 assert(t->cs.number == 0);
347 assert(t->cs.sc == NULL);
348 assert(t->cs.nargs == 0);
349 for (i = 0; i < nitems(t->cs.s_args); i++)
350 assert(t->cs.s_args[i] == NULL);
351 memset(t->cs.args, 0, sizeof(t->cs.args));
352 t->cs.number = pl->pl_syscall_code;
357 free_syscall(struct threadinfo *t)
361 for (i = 0; i < t->cs.nargs; i++)
362 free(t->cs.s_args[i]);
363 memset(&t->cs, 0, sizeof(t->cs));
368 enter_syscall(struct trussinfo *info, struct threadinfo *t,
369 struct ptrace_lwpinfo *pl)
374 alloc_syscall(t, pl);
375 narg = MIN(pl->pl_syscall_narg, nitems(t->cs.args));
376 if (narg != 0 && t->proc->abi->fetch_args(info, narg) != 0) {
381 sc = get_syscall(t, t->cs.number, narg);
383 fprintf(info->outfile, "-- UNKNOWN %s SYSCALL %d --\n",
384 t->proc->abi->type, t->cs.number);
386 t->cs.nargs = sc->nargs;
387 assert(sc->nargs <= nitems(t->cs.s_args));
392 * At this point, we set up the system call arguments.
393 * We ignore any OUT ones, however -- those are arguments that
394 * are set by the system call, and so are probably meaningless
395 * now. This doesn't currently support arguments that are
396 * passed in *and* out, however.
399 fprintf(stderr, "syscall %s(", sc->name);
401 for (i = 0; i < t->cs.nargs; i++) {
403 fprintf(stderr, "0x%lx%s", t->cs.args[sc->args[i].offset],
404 i < (t->cs.nargs - 1) ? "," : "");
406 if (!(sc->args[i].type & OUT)) {
407 t->cs.s_args[i] = print_arg(&sc->args[i],
408 t->cs.args, 0, info);
412 fprintf(stderr, ")\n");
415 clock_gettime(CLOCK_REALTIME, &t->before);
419 * When a thread exits voluntarily (including when a thread calls
420 * exit() to trigger a process exit), the thread's internal state
421 * holds the arguments passed to the exit system call. When the
422 * thread's exit is reported, log that system call without a return
426 thread_exit_syscall(struct trussinfo *info)
428 struct threadinfo *t;
434 clock_gettime(CLOCK_REALTIME, &t->after);
436 print_syscall_ret(info, 0, NULL);
441 exit_syscall(struct trussinfo *info, struct ptrace_lwpinfo *pl)
443 struct threadinfo *t;
454 clock_gettime(CLOCK_REALTIME, &t->after);
456 if (p->abi->fetch_retval(info, retval, &errorp) < 0) {
463 * Here, we only look for arguments that have OUT masked in --
464 * otherwise, they were handled in enter_syscall().
466 for (i = 0; i < sc->nargs; i++) {
469 if (sc->args[i].type & OUT) {
471 * If an error occurred, then don't bother
472 * getting the data; it may not be valid.
475 asprintf(&temp, "0x%lx",
476 t->cs.args[sc->args[i].offset]);
478 temp = print_arg(&sc->args[i],
479 t->cs.args, retval, info);
481 t->cs.s_args[i] = temp;
485 print_syscall_ret(info, errorp, retval);
489 * If the process executed a new image, check the ABI. If the
490 * new ABI isn't supported, stop tracing this process.
492 if (pl->pl_flags & PL_FLAG_EXEC) {
493 assert(LIST_NEXT(LIST_FIRST(&p->threadlist), entries) == NULL);
494 p->abi = find_abi(p->pid);
495 if (p->abi == NULL) {
496 if (ptrace(PT_DETACH, p->pid, (caddr_t)1, 0) < 0)
497 err(1, "Can not detach the process");
504 print_line_prefix(struct trussinfo *info)
506 struct timespec timediff;
507 struct threadinfo *t;
512 if (info->flags & (FOLLOWFORKS | DISPLAYTIDS)) {
513 if (info->flags & FOLLOWFORKS)
514 len += fprintf(info->outfile, "%5d", t->proc->pid);
515 if ((info->flags & (FOLLOWFORKS | DISPLAYTIDS)) ==
516 (FOLLOWFORKS | DISPLAYTIDS))
517 len += fprintf(info->outfile, " ");
518 if (info->flags & DISPLAYTIDS)
519 len += fprintf(info->outfile, "%6d", t->tid);
520 len += fprintf(info->outfile, ": ");
522 if (info->flags & ABSOLUTETIMESTAMPS) {
523 timespecsubt(&t->after, &info->start_time, &timediff);
524 len += fprintf(info->outfile, "%jd.%09ld ",
525 (intmax_t)timediff.tv_sec, timediff.tv_nsec);
527 if (info->flags & RELATIVETIMESTAMPS) {
528 timespecsubt(&t->after, &t->before, &timediff);
529 len += fprintf(info->outfile, "%jd.%09ld ",
530 (intmax_t)timediff.tv_sec, timediff.tv_nsec);
536 report_thread_death(struct trussinfo *info)
538 struct threadinfo *t;
541 clock_gettime(CLOCK_REALTIME, &t->after);
542 print_line_prefix(info);
543 fprintf(info->outfile, "<thread %ld exited>\n", (long)t->tid);
547 report_thread_birth(struct trussinfo *info)
549 struct threadinfo *t;
552 clock_gettime(CLOCK_REALTIME, &t->after);
553 t->before = t->after;
554 print_line_prefix(info);
555 fprintf(info->outfile, "<new thread %ld>\n", (long)t->tid);
559 report_exit(struct trussinfo *info, siginfo_t *si)
561 struct threadinfo *t;
564 clock_gettime(CLOCK_REALTIME, &t->after);
565 print_line_prefix(info);
566 if (si->si_code == CLD_EXITED)
567 fprintf(info->outfile, "process exit, rval = %u\n",
570 fprintf(info->outfile, "process killed, signal = %u%s\n",
571 si->si_status, si->si_code == CLD_DUMPED ?
572 " (core dumped)" : "");
576 report_new_child(struct trussinfo *info)
578 struct threadinfo *t;
581 clock_gettime(CLOCK_REALTIME, &t->after);
582 t->before = t->after;
583 print_line_prefix(info);
584 fprintf(info->outfile, "<new process>\n");
588 decode_siginfo(FILE *fp, siginfo_t *si)
592 fprintf(fp, " code=");
593 str = sysdecode_sigcode(si->si_signo, si->si_code);
595 fprintf(fp, "%d", si->si_code);
597 fprintf(fp, "%s", str);
598 switch (si->si_code) {
602 fprintf(fp, " value=%p", si->si_value.sival_ptr);
606 fprintf(fp, " pid=%jd uid=%jd", (intmax_t)si->si_pid,
607 (intmax_t)si->si_uid);
610 fprintf(fp, " value=%p", si->si_value.sival_ptr);
611 fprintf(fp, " timerid=%d", si->si_timerid);
612 fprintf(fp, " overrun=%d", si->si_overrun);
613 if (si->si_errno != 0)
614 fprintf(fp, " errno=%d", si->si_errno);
617 fprintf(fp, " value=%p", si->si_value.sival_ptr);
620 fprintf(fp, " value=%p", si->si_value.sival_ptr);
621 fprintf(fp, " mqd=%d", si->si_mqd);
624 switch (si->si_signo) {
629 fprintf(fp, " trapno=%d", si->si_trapno);
630 fprintf(fp, " addr=%p", si->si_addr);
633 fprintf(fp, " pid=%jd uid=%jd", (intmax_t)si->si_pid,
634 (intmax_t)si->si_uid);
635 fprintf(fp, " status=%d", si->si_status);
642 report_signal(struct trussinfo *info, siginfo_t *si, struct ptrace_lwpinfo *pl)
644 struct threadinfo *t;
648 clock_gettime(CLOCK_REALTIME, &t->after);
649 print_line_prefix(info);
650 signame = sysdecode_signal(si->si_status);
653 fprintf(info->outfile, "SIGNAL %u (%s)", si->si_status, signame);
654 if (pl->pl_event == PL_EVENT_SIGNAL && pl->pl_flags & PL_FLAG_SI)
655 decode_siginfo(info->outfile, &pl->pl_siginfo);
656 fprintf(info->outfile, "\n");
661 * Wait for events until all the processes have exited or truss has been
665 eventloop(struct trussinfo *info)
667 struct ptrace_lwpinfo pl;
671 while (!LIST_EMPTY(&info->proclist)) {
673 detach_all_procs(info);
677 if (waitid(P_ALL, 0, &si, WTRAPPED | WEXITED) == -1) {
680 err(1, "Unexpected error from waitid");
683 assert(si.si_signo == SIGCHLD);
685 switch (si.si_code) {
689 find_exit_thread(info, si.si_pid);
690 if ((info->flags & COUNTONLY) == 0) {
691 if (si.si_code == CLD_EXITED)
692 thread_exit_syscall(info);
693 report_exit(info, &si);
695 free_proc(info->curthread->proc);
696 info->curthread = NULL;
699 if (ptrace(PT_LWPINFO, si.si_pid, (caddr_t)&pl,
701 err(1, "ptrace(PT_LWPINFO)");
703 if (pl.pl_flags & PL_FLAG_CHILD) {
704 new_proc(info, si.si_pid, pl.pl_lwpid);
705 assert(LIST_FIRST(&info->proclist)->abi !=
707 } else if (pl.pl_flags & PL_FLAG_BORN)
708 new_thread(find_proc(info, si.si_pid),
710 find_thread(info, si.si_pid, pl.pl_lwpid);
712 if (si.si_status == SIGTRAP &&
713 (pl.pl_flags & (PL_FLAG_BORN|PL_FLAG_EXITED|
714 PL_FLAG_SCE|PL_FLAG_SCX)) != 0) {
715 if (pl.pl_flags & PL_FLAG_BORN) {
716 if ((info->flags & COUNTONLY) == 0)
717 report_thread_birth(info);
718 } else if (pl.pl_flags & PL_FLAG_EXITED) {
719 if ((info->flags & COUNTONLY) == 0)
720 report_thread_death(info);
721 free_thread(info->curthread);
722 info->curthread = NULL;
723 } else if (pl.pl_flags & PL_FLAG_SCE)
724 enter_syscall(info, info->curthread, &pl);
725 else if (pl.pl_flags & PL_FLAG_SCX)
726 exit_syscall(info, &pl);
728 } else if (pl.pl_flags & PL_FLAG_CHILD) {
729 if ((info->flags & COUNTONLY) == 0)
730 report_new_child(info);
733 if ((info->flags & NOSIGS) == 0)
734 report_signal(info, &si, &pl);
735 pending_signal = si.si_status;
737 ptrace(PT_SYSCALL, si.si_pid, (caddr_t)1,
741 errx(1, "waitid reported CLD_STOPPED");