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>
52 #include <sysdecode.h>
60 SET_DECLARE(procabi, struct procabi);
62 static sig_atomic_t detaching;
64 static void enter_syscall(struct trussinfo *, struct threadinfo *,
65 struct ptrace_lwpinfo *);
66 static void new_proc(struct trussinfo *, pid_t, lwpid_t);
69 * setup_and_wait() is called to start a process. All it really does
70 * is fork(), enable tracing in the child, and then exec the given
71 * command. At that point, the child process stops, and the parent
72 * can wake up and deal with it.
75 setup_and_wait(struct trussinfo *info, char *command[])
81 err(1, "fork failed");
82 if (pid == 0) { /* Child */
83 ptrace(PT_TRACE_ME, 0, 0, 0);
84 execvp(command[0], command);
85 err(1, "execvp %s", command[0]);
88 /* Only in the parent here */
89 if (waitpid(pid, NULL, 0) < 0)
90 err(1, "unexpect stop in waitpid");
92 new_proc(info, pid, 0);
96 * start_tracing is called to attach to an existing process.
99 start_tracing(struct trussinfo *info, pid_t pid)
105 ret = ptrace(PT_ATTACH, pid, NULL, 0);
107 } while (ret && retry-- > 0);
109 err(1, "can not attach to target process");
111 if (waitpid(pid, NULL, 0) < 0)
112 err(1, "Unexpect stop in waitpid");
114 new_proc(info, pid, 0);
118 * Restore a process back to it's pre-truss state.
119 * Called for SIGINT, SIGTERM, SIGQUIT. This only
120 * applies if truss was told to monitor an already-existing
124 restore_proc(int signo __unused)
131 detach_proc(pid_t pid)
134 /* stop the child so that we can detach */
136 if (waitpid(pid, NULL, 0) < 0)
137 err(1, "Unexpected stop in waitpid");
139 if (ptrace(PT_DETACH, pid, (caddr_t)1, 0) < 0)
140 err(1, "Can not detach the process");
146 * Determine the ABI. This is called after every exec, and when
147 * a process is first monitored.
149 static struct procabi *
152 struct procabi **pabi;
161 mib[2] = KERN_PROC_SV_NAME;
163 error = sysctl(mib, 4, progt, &len, NULL, 0);
165 err(2, "can not get sysvec name");
167 SET_FOREACH(pabi, procabi) {
168 if (strcmp((*pabi)->type, progt) == 0)
171 warnx("ABI %s for pid %ld is not supported", progt, (long)pid);
175 static struct threadinfo *
176 new_thread(struct procinfo *p, lwpid_t lwpid)
178 struct threadinfo *nt;
181 * If this happens it means there is a bug in truss. Unfortunately
182 * this will kill any processes truss is attached to.
184 LIST_FOREACH(nt, &p->threadlist, entries) {
185 if (nt->tid == lwpid)
186 errx(1, "Duplicate thread for LWP %ld", (long)lwpid);
189 nt = calloc(1, sizeof(struct threadinfo));
191 err(1, "calloc() failed");
194 LIST_INSERT_HEAD(&p->threadlist, nt, entries);
199 free_thread(struct threadinfo *t)
202 LIST_REMOVE(t, entries);
207 add_threads(struct trussinfo *info, struct procinfo *p)
209 struct ptrace_lwpinfo pl;
210 struct threadinfo *t;
214 nlwps = ptrace(PT_GETNUMLWPS, p->pid, NULL, 0);
216 err(1, "Unable to fetch number of LWPs");
218 lwps = calloc(nlwps, sizeof(*lwps));
219 nlwps = ptrace(PT_GETLWPLIST, p->pid, (caddr_t)lwps, nlwps);
221 err(1, "Unable to fetch LWP list");
222 for (i = 0; i < nlwps; i++) {
223 t = new_thread(p, lwps[i]);
224 if (ptrace(PT_LWPINFO, lwps[i], (caddr_t)&pl, sizeof(pl)) == -1)
225 err(1, "ptrace(PT_LWPINFO)");
226 if (pl.pl_flags & PL_FLAG_SCE) {
228 enter_syscall(info, t, &pl);
235 new_proc(struct trussinfo *info, pid_t pid, lwpid_t lwpid)
240 * If this happens it means there is a bug in truss. Unfortunately
241 * this will kill any processes truss is attached to.
243 LIST_FOREACH(np, &info->proclist, entries) {
245 errx(1, "Duplicate process for pid %ld", (long)pid);
248 if (info->flags & FOLLOWFORKS)
249 if (ptrace(PT_FOLLOW_FORK, pid, NULL, 1) == -1)
250 err(1, "Unable to follow forks for pid %ld", (long)pid);
251 if (ptrace(PT_LWP_EVENTS, pid, NULL, 1) == -1)
252 err(1, "Unable to enable LWP events for pid %ld", (long)pid);
253 np = calloc(1, sizeof(struct procinfo));
255 np->abi = find_abi(pid);
256 LIST_INIT(&np->threadlist);
257 LIST_INSERT_HEAD(&info->proclist, np, entries);
260 new_thread(np, lwpid);
262 add_threads(info, np);
266 free_proc(struct procinfo *p)
268 struct threadinfo *t, *t2;
270 LIST_FOREACH_SAFE(t, &p->threadlist, entries, t2) {
273 LIST_REMOVE(p, entries);
278 detach_all_procs(struct trussinfo *info)
280 struct procinfo *p, *p2;
282 LIST_FOREACH_SAFE(p, &info->proclist, entries, p2) {
288 static struct procinfo *
289 find_proc(struct trussinfo *info, pid_t pid)
293 LIST_FOREACH(np, &info->proclist, entries) {
302 * Change curthread member based on (pid, lwpid).
305 find_thread(struct trussinfo *info, pid_t pid, lwpid_t lwpid)
308 struct threadinfo *nt;
310 np = find_proc(info, pid);
313 LIST_FOREACH(nt, &np->threadlist, entries) {
314 if (nt->tid == lwpid) {
315 info->curthread = nt;
319 errx(1, "could not find thread");
323 * When a process exits, it should have exactly one thread left.
324 * All of the other threads should have reported thread exit events.
327 find_exit_thread(struct trussinfo *info, pid_t pid)
331 p = find_proc(info, pid);
334 info->curthread = LIST_FIRST(&p->threadlist);
335 assert(info->curthread != NULL);
336 assert(LIST_NEXT(info->curthread, entries) == NULL);
340 alloc_syscall(struct threadinfo *t, struct ptrace_lwpinfo *pl)
344 assert(t->in_syscall == 0);
345 assert(t->cs.number == 0);
346 assert(t->cs.name == NULL);
347 assert(t->cs.nargs == 0);
348 for (i = 0; i < nitems(t->cs.s_args); i++)
349 assert(t->cs.s_args[i] == NULL);
350 memset(t->cs.args, 0, sizeof(t->cs.args));
351 t->cs.number = pl->pl_syscall_code;
356 free_syscall(struct threadinfo *t)
360 for (i = 0; i < t->cs.nargs; i++)
361 free(t->cs.s_args[i]);
362 memset(&t->cs, 0, sizeof(t->cs));
367 enter_syscall(struct trussinfo *info, struct threadinfo *t,
368 struct ptrace_lwpinfo *pl)
373 alloc_syscall(t, pl);
374 narg = MIN(pl->pl_syscall_narg, nitems(t->cs.args));
375 if (narg != 0 && t->proc->abi->fetch_args(info, narg) != 0) {
380 t->cs.name = sysdecode_syscallname(t->proc->abi->abi, t->cs.number);
381 if (t->cs.name == NULL)
382 fprintf(info->outfile, "-- UNKNOWN %s SYSCALL %d --\n",
383 t->proc->abi->type, t->cs.number);
385 sc = get_syscall(t->cs.name, narg);
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.
398 if (t->cs.name != NULL) {
400 fprintf(stderr, "syscall %s(", t->cs.name);
402 for (i = 0; i < t->cs.nargs; i++) {
404 fprintf(stderr, "0x%lx%s", sc ?
405 t->cs.args[sc->args[i].offset] : t->cs.args[i],
406 i < (t->cs.nargs - 1) ? "," : "");
408 if (!(sc->args[i].type & OUT)) {
409 t->cs.s_args[i] = print_arg(&sc->args[i],
410 t->cs.args, 0, info);
414 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 timespecsubt(&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 timespecsubt(&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 report_signal(struct trussinfo *info, siginfo_t *si)
593 struct threadinfo *t;
597 clock_gettime(CLOCK_REALTIME, &t->after);
598 print_line_prefix(info);
599 signame = strsig(si->si_status);
600 fprintf(info->outfile, "SIGNAL %u (%s)\n", si->si_status,
601 signame == NULL ? "?" : signame);
605 * Wait for events until all the processes have exited or truss has been
609 eventloop(struct trussinfo *info)
611 struct ptrace_lwpinfo pl;
615 while (!LIST_EMPTY(&info->proclist)) {
617 detach_all_procs(info);
621 if (waitid(P_ALL, 0, &si, WTRAPPED | WEXITED) == -1) {
624 err(1, "Unexpected error from waitid");
627 assert(si.si_signo == SIGCHLD);
629 switch (si.si_code) {
633 find_exit_thread(info, si.si_pid);
634 if ((info->flags & COUNTONLY) == 0) {
635 if (si.si_code == CLD_EXITED)
636 thread_exit_syscall(info);
637 report_exit(info, &si);
639 free_proc(info->curthread->proc);
640 info->curthread = NULL;
643 if (ptrace(PT_LWPINFO, si.si_pid, (caddr_t)&pl,
645 err(1, "ptrace(PT_LWPINFO)");
647 if (pl.pl_flags & PL_FLAG_CHILD) {
648 new_proc(info, si.si_pid, pl.pl_lwpid);
649 assert(LIST_FIRST(&info->proclist)->abi !=
651 } else if (pl.pl_flags & PL_FLAG_BORN)
652 new_thread(find_proc(info, si.si_pid),
654 find_thread(info, si.si_pid, pl.pl_lwpid);
656 if (si.si_status == SIGTRAP &&
657 (pl.pl_flags & (PL_FLAG_BORN|PL_FLAG_EXITED|
658 PL_FLAG_SCE|PL_FLAG_SCX)) != 0) {
659 if (pl.pl_flags & PL_FLAG_BORN) {
660 if ((info->flags & COUNTONLY) == 0)
661 report_thread_birth(info);
662 } else if (pl.pl_flags & PL_FLAG_EXITED) {
663 if ((info->flags & COUNTONLY) == 0)
664 report_thread_death(info);
665 free_thread(info->curthread);
666 info->curthread = NULL;
667 } else if (pl.pl_flags & PL_FLAG_SCE)
668 enter_syscall(info, info->curthread, &pl);
669 else if (pl.pl_flags & PL_FLAG_SCX)
670 exit_syscall(info, &pl);
672 } else if (pl.pl_flags & PL_FLAG_CHILD) {
673 if ((info->flags & COUNTONLY) == 0)
674 report_new_child(info);
677 if ((info->flags & NOSIGS) == 0)
678 report_signal(info, &si);
679 pending_signal = si.si_status;
681 ptrace(PT_SYSCALL, si.si_pid, (caddr_t)1,
685 errx(1, "waitid reported CLD_STOPPED");