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>
59 SET_DECLARE(procabi, struct procabi);
61 static sig_atomic_t detaching;
63 static void new_proc(struct trussinfo *, pid_t);
66 * setup_and_wait() is called to start a process. All it really does
67 * is fork(), enable tracing in the child, and then exec the given
68 * command. At that point, the child process stops, and the parent
69 * can wake up and deal with it.
72 setup_and_wait(struct trussinfo *info, char *command[])
78 err(1, "fork failed");
79 if (pid == 0) { /* Child */
80 ptrace(PT_TRACE_ME, 0, 0, 0);
81 execvp(command[0], command);
82 err(1, "execvp %s", command[0]);
85 /* Only in the parent here */
86 if (waitpid(pid, NULL, 0) < 0)
87 err(1, "unexpect stop in waitpid");
93 * start_tracing is called to attach to an existing process.
96 start_tracing(struct trussinfo *info, pid_t pid)
102 ret = ptrace(PT_ATTACH, pid, NULL, 0);
104 } while (ret && retry-- > 0);
106 err(1, "can not attach to target process");
108 if (waitpid(pid, NULL, 0) < 0)
109 err(1, "Unexpect stop in waitpid");
115 * Restore a process back to it's pre-truss state.
116 * Called for SIGINT, SIGTERM, SIGQUIT. This only
117 * applies if truss was told to monitor an already-existing
121 restore_proc(int signo __unused)
128 detach_proc(pid_t pid)
131 /* stop the child so that we can detach */
133 if (waitpid(pid, NULL, 0) < 0)
134 err(1, "Unexpected stop in waitpid");
136 if (ptrace(PT_DETACH, pid, (caddr_t)1, 0) < 0)
137 err(1, "Can not detach the process");
143 * Determine the ABI. This is called after every exec, and when
144 * a process is first monitored.
146 static struct procabi *
149 struct procabi **pabi;
158 mib[2] = KERN_PROC_SV_NAME;
160 error = sysctl(mib, 4, progt, &len, NULL, 0);
162 err(2, "can not get sysvec name");
164 SET_FOREACH(pabi, procabi) {
165 if (strcmp((*pabi)->type, progt) == 0)
168 warnx("ABI %s for pid %ld is not supported", progt, (long)pid);
173 new_proc(struct trussinfo *info, pid_t pid)
178 * If this happens it means there is a bug in truss. Unfortunately
179 * this will kill any processes are attached to.
181 LIST_FOREACH(np, &info->proclist, entries) {
183 errx(1, "Duplicate process for pid %ld", (long)pid);
186 if (info->flags & FOLLOWFORKS)
187 if (ptrace(PT_FOLLOW_FORK, pid, NULL, 1) == -1)
188 err(1, "Unable to follow forks for pid %ld", (long)pid);
189 np = calloc(1, sizeof(struct procinfo));
191 np->abi = find_abi(pid);
192 SLIST_INIT(&np->threadlist);
193 LIST_INSERT_HEAD(&info->proclist, np, entries);
197 free_proc(struct procinfo *p)
199 struct threadinfo *t, *t2;
201 SLIST_FOREACH_SAFE(t, &p->threadlist, entries, t2) {
204 LIST_REMOVE(p, entries);
209 detach_all_procs(struct trussinfo *info)
211 struct procinfo *p, *p2;
213 LIST_FOREACH_SAFE(p, &info->proclist, entries, p2) {
219 static struct procinfo *
220 find_proc(struct trussinfo *info, pid_t pid)
224 LIST_FOREACH(np, &info->proclist, entries) {
233 * Change curthread member based on (pid, lwpid).
234 * If it is a new thread, create a threadinfo structure.
237 find_thread(struct trussinfo *info, pid_t pid, lwpid_t lwpid)
240 struct threadinfo *nt;
242 np = find_proc(info, pid);
245 SLIST_FOREACH(nt, &np->threadlist, entries) {
246 if (nt->tid == lwpid) {
247 info->curthread = nt;
252 nt = calloc(1, sizeof(struct threadinfo));
254 err(1, "calloc() failed");
257 SLIST_INSERT_HEAD(&np->threadlist, nt, entries);
258 info->curthread = nt;
262 * When a process exits, it no longer has any threads left. However,
263 * the main loop expects a valid curthread. In cases when a thread
264 * triggers the termination (e.g. calling exit or triggering a fault)
265 * we would ideally use that thread. However, if a process is killed
266 * by a signal sent from another process then there is no "correct"
267 * thread. We just punt and use the first thread.
270 find_exit_thread(struct trussinfo *info, pid_t pid)
273 struct threadinfo *nt;
275 np = find_proc(info, pid);
278 if (SLIST_EMPTY(&np->threadlist)) {
280 * If an existing process exits right after we attach
281 * to it but before it posts any events, there won't
282 * be any threads. Create a dummy thread and set its
283 * "before" time to the global start time.
285 nt = calloc(1, sizeof(struct threadinfo));
287 err(1, "calloc() failed");
290 SLIST_INSERT_HEAD(&np->threadlist, nt, entries);
291 nt->before = info->start_time;
293 info->curthread = SLIST_FIRST(&np->threadlist);
297 alloc_syscall(struct threadinfo *t, struct ptrace_lwpinfo *pl)
301 assert(t->in_syscall == 0);
302 assert(t->cs.number == 0);
303 assert(t->cs.name == NULL);
304 assert(t->cs.nargs == 0);
305 for (i = 0; i < nitems(t->cs.s_args); i++)
306 assert(t->cs.s_args[i] == NULL);
307 memset(t->cs.args, 0, sizeof(t->cs.args));
308 t->cs.number = pl->pl_syscall_code;
313 free_syscall(struct threadinfo *t)
317 for (i = 0; i < t->cs.nargs; i++)
318 free(t->cs.s_args[i]);
319 memset(&t->cs, 0, sizeof(t->cs));
324 enter_syscall(struct trussinfo *info, struct ptrace_lwpinfo *pl)
326 struct threadinfo *t;
331 alloc_syscall(t, pl);
332 narg = MIN(pl->pl_syscall_narg, nitems(t->cs.args));
333 if (narg != 0 && t->proc->abi->fetch_args(info, narg) != 0) {
338 if (t->cs.number >= 0 && t->cs.number < t->proc->abi->nsyscalls)
339 t->cs.name = t->proc->abi->syscallnames[t->cs.number];
340 if (t->cs.name == NULL)
341 fprintf(info->outfile, "-- UNKNOWN %s SYSCALL %d --\n",
342 t->proc->abi->type, t->cs.number);
344 sc = get_syscall(t->cs.name, narg);
345 t->cs.nargs = sc->nargs;
346 assert(sc->nargs <= nitems(t->cs.s_args));
351 * At this point, we set up the system call arguments.
352 * We ignore any OUT ones, however -- those are arguments that
353 * are set by the system call, and so are probably meaningless
354 * now. This doesn't currently support arguments that are
355 * passed in *and* out, however.
357 if (t->cs.name != NULL) {
359 fprintf(stderr, "syscall %s(", t->cs.name);
361 for (i = 0; i < t->cs.nargs; i++) {
363 fprintf(stderr, "0x%lx%s", sc ?
364 t->cs.args[sc->args[i].offset] : t->cs.args[i],
365 i < (t->cs.nargs - 1) ? "," : "");
367 if (!(sc->args[i].type & OUT)) {
368 t->cs.s_args[i] = print_arg(&sc->args[i],
369 t->cs.args, 0, info);
373 fprintf(stderr, ")\n");
377 clock_gettime(CLOCK_REALTIME, &t->before);
381 exit_syscall(struct trussinfo *info, struct ptrace_lwpinfo *pl)
383 struct threadinfo *t;
394 clock_gettime(CLOCK_REALTIME, &t->after);
396 if (p->abi->fetch_retval(info, retval, &errorp) < 0) {
403 * Here, we only look for arguments that have OUT masked in --
404 * otherwise, they were handled in enter_syscall().
406 for (i = 0; i < sc->nargs; i++) {
409 if (sc->args[i].type & OUT) {
411 * If an error occurred, then don't bother
412 * getting the data; it may not be valid.
415 asprintf(&temp, "0x%lx",
416 t->cs.args[sc->args[i].offset]);
418 temp = print_arg(&sc->args[i],
419 t->cs.args, retval, info);
421 t->cs.s_args[i] = temp;
425 print_syscall_ret(info, errorp, retval);
429 * If the process executed a new image, check the ABI. If the
430 * new ABI isn't supported, stop tracing this process.
432 if (pl->pl_flags & PL_FLAG_EXEC) {
433 p->abi = find_abi(p->pid);
434 if (p->abi == NULL) {
435 if (ptrace(PT_DETACH, p->pid, (caddr_t)1, 0) < 0)
436 err(1, "Can not detach the process");
443 print_line_prefix(struct trussinfo *info)
445 struct timespec timediff;
446 struct threadinfo *t;
451 if (info->flags & (FOLLOWFORKS | DISPLAYTIDS)) {
452 if (info->flags & FOLLOWFORKS)
453 len += fprintf(info->outfile, "%5d", t->proc->pid);
454 if ((info->flags & (FOLLOWFORKS | DISPLAYTIDS)) ==
455 (FOLLOWFORKS | DISPLAYTIDS))
456 len += fprintf(info->outfile, " ");
457 if (info->flags & DISPLAYTIDS)
458 len += fprintf(info->outfile, "%6d", t->tid);
459 len += fprintf(info->outfile, ": ");
461 if (info->flags & ABSOLUTETIMESTAMPS) {
462 timespecsubt(&t->after, &info->start_time, &timediff);
463 len += fprintf(info->outfile, "%jd.%09ld ",
464 (intmax_t)timediff.tv_sec, timediff.tv_nsec);
466 if (info->flags & RELATIVETIMESTAMPS) {
467 timespecsubt(&t->after, &t->before, &timediff);
468 len += fprintf(info->outfile, "%jd.%09ld ",
469 (intmax_t)timediff.tv_sec, timediff.tv_nsec);
475 report_exit(struct trussinfo *info, siginfo_t *si)
477 struct threadinfo *t;
480 clock_gettime(CLOCK_REALTIME, &t->after);
481 print_line_prefix(info);
482 if (si->si_code == CLD_EXITED)
483 fprintf(info->outfile, "process exit, rval = %u\n",
486 fprintf(info->outfile, "process killed, signal = %u%s\n",
487 si->si_status, si->si_code == CLD_DUMPED ?
488 " (core dumped)" : "");
492 report_new_child(struct trussinfo *info)
494 struct threadinfo *t;
497 clock_gettime(CLOCK_REALTIME, &t->after);
498 t->before = t->after;
499 print_line_prefix(info);
500 fprintf(info->outfile, "<new process>\n");
504 report_signal(struct trussinfo *info, siginfo_t *si)
506 struct threadinfo *t;
510 clock_gettime(CLOCK_REALTIME, &t->after);
511 print_line_prefix(info);
512 signame = strsig(si->si_status);
513 fprintf(info->outfile, "SIGNAL %u (%s)\n", si->si_status,
514 signame == NULL ? "?" : signame);
518 * Wait for events until all the processes have exited or truss has been
522 eventloop(struct trussinfo *info)
524 struct ptrace_lwpinfo pl;
528 while (!LIST_EMPTY(&info->proclist)) {
530 detach_all_procs(info);
534 if (waitid(P_ALL, 0, &si, WTRAPPED | WEXITED) == -1) {
537 err(1, "Unexpected error from waitid");
540 assert(si.si_signo == SIGCHLD);
542 switch (si.si_code) {
546 find_exit_thread(info, si.si_pid);
547 if ((info->flags & COUNTONLY) == 0)
548 report_exit(info, &si);
549 free_proc(info->curthread->proc);
550 info->curthread = NULL;
553 if (ptrace(PT_LWPINFO, si.si_pid, (caddr_t)&pl,
555 err(1, "ptrace(PT_LWPINFO)");
557 if (pl.pl_flags & PL_FLAG_CHILD) {
558 new_proc(info, si.si_pid);
559 assert(LIST_FIRST(&info->proclist)->abi !=
562 find_thread(info, si.si_pid, pl.pl_lwpid);
564 if (si.si_status == SIGTRAP &&
565 (pl.pl_flags & (PL_FLAG_SCE|PL_FLAG_SCX)) != 0) {
566 if (pl.pl_flags & PL_FLAG_SCE)
567 enter_syscall(info, &pl);
568 else if (pl.pl_flags & PL_FLAG_SCX)
569 exit_syscall(info, &pl);
571 } else if (pl.pl_flags & PL_FLAG_CHILD) {
572 if ((info->flags & COUNTONLY) == 0)
573 report_new_child(info);
576 if ((info->flags & NOSIGS) == 0)
577 report_signal(info, &si);
578 pending_signal = si.si_status;
580 ptrace(PT_SYSCALL, si.si_pid, (caddr_t)1,
584 errx(1, "waitid reported CLD_STOPPED");