xref: /freebsd/usr.bin/truss/setup.c (revision 66df505066f51e6d8411b966765d828817f88971)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright 1997 Sean Eric Fagan
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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
19  *    permission.
20  *
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
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 /*
38  * Various setup functions for truss.  Not the cleanest-written code,
39  * I'm afraid.
40  */
41 
42 #include <sys/ptrace.h>
43 #include <sys/sysctl.h>
44 #include <sys/time.h>
45 #include <sys/wait.h>
46 
47 #include <assert.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <signal.h>
51 #include <stdbool.h>
52 #include <stdint.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <sysdecode.h>
57 #include <time.h>
58 #include <unistd.h>
59 
60 #include "truss.h"
61 #include "syscall.h"
62 #include "extern.h"
63 
64 struct procabi_table {
65 	const char *name;
66 	struct procabi *abi;
67 };
68 
69 static sig_atomic_t detaching;
70 
71 static void	enter_syscall(struct trussinfo *, struct threadinfo *,
72 		    struct ptrace_lwpinfo *);
73 static void	new_proc(struct trussinfo *, pid_t, lwpid_t);
74 
75 
76 static struct procabi freebsd = {
77 	.type = "FreeBSD",
78 	.abi = SYSDECODE_ABI_FREEBSD,
79 	.pointer_size = sizeof(void *),
80 	.extra_syscalls = STAILQ_HEAD_INITIALIZER(freebsd.extra_syscalls),
81 	.syscalls = { NULL }
82 };
83 
84 #if !defined(__SIZEOF_POINTER__)
85 #error "Use a modern compiler."
86 #endif
87 
88 #if __SIZEOF_POINTER__ > 4
89 static struct procabi freebsd32 = {
90 	.type = "FreeBSD32",
91 	.abi = SYSDECODE_ABI_FREEBSD32,
92 	.pointer_size = sizeof(uint32_t),
93 	.compat_prefix = "freebsd32_",
94 	.extra_syscalls = STAILQ_HEAD_INITIALIZER(freebsd32.extra_syscalls),
95 	.syscalls = { NULL }
96 };
97 #endif
98 
99 static struct procabi linux = {
100 	.type = "Linux",
101 	.abi = SYSDECODE_ABI_LINUX,
102 	.pointer_size = sizeof(void *),
103 	.extra_syscalls = STAILQ_HEAD_INITIALIZER(linux.extra_syscalls),
104 	.syscalls = { NULL }
105 };
106 
107 #if __SIZEOF_POINTER__ > 4
108 static struct procabi linux32 = {
109 	.type = "Linux32",
110 	.abi = SYSDECODE_ABI_LINUX32,
111 	.pointer_size = sizeof(uint32_t),
112 	.extra_syscalls = STAILQ_HEAD_INITIALIZER(linux32.extra_syscalls),
113 	.syscalls = { NULL }
114 };
115 #endif
116 
117 static struct procabi_table abis[] = {
118 #if __SIZEOF_POINTER__ == 4
119 	{ "FreeBSD ELF32", &freebsd },
120 #elif __SIZEOF_POINTER__ == 8
121 	{ "FreeBSD ELF64", &freebsd },
122 	{ "FreeBSD ELF32", &freebsd32 },
123 #else
124 #error "Unsupported pointer size"
125 #endif
126 #if defined(__powerpc64__)
127 	{ "FreeBSD ELF64 V2", &freebsd },
128 #endif
129 #if defined(__amd64__)
130 	{ "FreeBSD a.out", &freebsd32 },
131 #endif
132 #if defined(__i386__)
133 	{ "FreeBSD a.out", &freebsd },
134 #endif
135 #if __SIZEOF_POINTER__ >= 8
136 	{ "Linux ELF64", &linux },
137 	{ "Linux ELF32", &linux32 },
138 #else
139 	{ "Linux ELF32", &linux },
140 #endif
141 };
142 
143 /*
144  * setup_and_wait() is called to start a process.  All it really does
145  * is fork(), enable tracing in the child, and then exec the given
146  * command.  At that point, the child process stops, and the parent
147  * can wake up and deal with it.
148  */
149 void
150 setup_and_wait(struct trussinfo *info, char *command[])
151 {
152 	pid_t pid;
153 
154 	pid = vfork();
155 	if (pid == -1)
156 		err(1, "fork failed");
157 	if (pid == 0) {	/* Child */
158 		ptrace(PT_TRACE_ME, 0, 0, 0);
159 		execvp(command[0], command);
160 		err(1, "execvp %s", command[0]);
161 	}
162 
163 	/* Only in the parent here */
164 	if (waitpid(pid, NULL, 0) < 0)
165 		err(1, "unexpect stop in waitpid");
166 
167 	new_proc(info, pid, 0);
168 }
169 
170 /*
171  * start_tracing is called to attach to an existing process.
172  */
173 void
174 start_tracing(struct trussinfo *info, pid_t pid)
175 {
176 	int ret, retry;
177 
178 	retry = 10;
179 	do {
180 		ret = ptrace(PT_ATTACH, pid, NULL, 0);
181 		usleep(200);
182 	} while (ret && retry-- > 0);
183 	if (ret)
184 		err(1, "can not attach to target process");
185 
186 	if (waitpid(pid, NULL, 0) < 0)
187 		err(1, "Unexpect stop in waitpid");
188 
189 	new_proc(info, pid, 0);
190 }
191 
192 /*
193  * Restore a process back to it's pre-truss state.
194  * Called for SIGINT, SIGTERM, SIGQUIT.  This only
195  * applies if truss was told to monitor an already-existing
196  * process.
197  */
198 void
199 restore_proc(int signo __unused)
200 {
201 
202 	detaching = 1;
203 }
204 
205 static void
206 detach_proc(pid_t pid)
207 {
208 
209 	/* stop the child so that we can detach */
210 	kill(pid, SIGSTOP);
211 	if (waitpid(pid, NULL, 0) < 0)
212 		err(1, "Unexpected stop in waitpid");
213 
214 	if (ptrace(PT_DETACH, pid, (caddr_t)1, 0) < 0)
215 		err(1, "Can not detach the process");
216 
217 	kill(pid, SIGCONT);
218 }
219 
220 /*
221  * Determine the ABI.  This is called after every exec, and when
222  * a process is first monitored.
223  */
224 static struct procabi *
225 find_abi(pid_t pid)
226 {
227 	size_t len;
228 	unsigned int i;
229 	int error;
230 	int mib[4];
231 	char progt[32];
232 
233 	len = sizeof(progt);
234 	mib[0] = CTL_KERN;
235 	mib[1] = KERN_PROC;
236 	mib[2] = KERN_PROC_SV_NAME;
237 	mib[3] = pid;
238 	error = sysctl(mib, 4, progt, &len, NULL, 0);
239 	if (error != 0)
240 		err(2, "can not get sysvec name");
241 
242 	for (i = 0; i < nitems(abis); i++) {
243 		if (strcmp(abis[i].name, progt) == 0)
244 			return (abis[i].abi);
245 	}
246 	warnx("ABI %s for pid %ld is not supported", progt, (long)pid);
247 	return (NULL);
248 }
249 
250 static struct threadinfo *
251 new_thread(struct procinfo *p, lwpid_t lwpid)
252 {
253 	struct threadinfo *nt;
254 
255 	/*
256 	 * If this happens it means there is a bug in truss.  Unfortunately
257 	 * this will kill any processes truss is attached to.
258 	 */
259 	LIST_FOREACH(nt, &p->threadlist, entries) {
260 		if (nt->tid == lwpid)
261 			errx(1, "Duplicate thread for LWP %ld", (long)lwpid);
262 	}
263 
264 	nt = calloc(1, sizeof(struct threadinfo));
265 	if (nt == NULL)
266 		err(1, "calloc() failed");
267 	nt->proc = p;
268 	nt->tid = lwpid;
269 	LIST_INSERT_HEAD(&p->threadlist, nt, entries);
270 	return (nt);
271 }
272 
273 static void
274 free_thread(struct threadinfo *t)
275 {
276 
277 	LIST_REMOVE(t, entries);
278 	free(t);
279 }
280 
281 static void
282 add_threads(struct trussinfo *info, struct procinfo *p)
283 {
284 	struct ptrace_lwpinfo pl;
285 	struct threadinfo *t;
286 	lwpid_t *lwps;
287 	int i, nlwps;
288 
289 	nlwps = ptrace(PT_GETNUMLWPS, p->pid, NULL, 0);
290 	if (nlwps == -1)
291 		err(1, "Unable to fetch number of LWPs");
292 	assert(nlwps > 0);
293 	lwps = calloc(nlwps, sizeof(*lwps));
294 	nlwps = ptrace(PT_GETLWPLIST, p->pid, (caddr_t)lwps, nlwps);
295 	if (nlwps == -1)
296 		err(1, "Unable to fetch LWP list");
297 	for (i = 0; i < nlwps; i++) {
298 		t = new_thread(p, lwps[i]);
299 		if (ptrace(PT_LWPINFO, lwps[i], (caddr_t)&pl, sizeof(pl)) == -1)
300 			err(1, "ptrace(PT_LWPINFO)");
301 		if (pl.pl_flags & PL_FLAG_SCE) {
302 			info->curthread = t;
303 			enter_syscall(info, t, &pl);
304 		}
305 	}
306 	free(lwps);
307 }
308 
309 static void
310 new_proc(struct trussinfo *info, pid_t pid, lwpid_t lwpid)
311 {
312 	struct procinfo *np;
313 
314 	/*
315 	 * If this happens it means there is a bug in truss.  Unfortunately
316 	 * this will kill any processes truss is attached to.
317 	 */
318 	LIST_FOREACH(np, &info->proclist, entries) {
319 		if (np->pid == pid)
320 			errx(1, "Duplicate process for pid %ld", (long)pid);
321 	}
322 
323 	if (info->flags & FOLLOWFORKS)
324 		if (ptrace(PT_FOLLOW_FORK, pid, NULL, 1) == -1)
325 			err(1, "Unable to follow forks for pid %ld", (long)pid);
326 	if (ptrace(PT_LWP_EVENTS, pid, NULL, 1) == -1)
327 		err(1, "Unable to enable LWP events for pid %ld", (long)pid);
328 	np = calloc(1, sizeof(struct procinfo));
329 	np->pid = pid;
330 	np->abi = find_abi(pid);
331 	LIST_INIT(&np->threadlist);
332 	LIST_INSERT_HEAD(&info->proclist, np, entries);
333 
334 	if (lwpid != 0)
335 		new_thread(np, lwpid);
336 	else
337 		add_threads(info, np);
338 }
339 
340 static void
341 free_proc(struct procinfo *p)
342 {
343 	struct threadinfo *t, *t2;
344 
345 	LIST_FOREACH_SAFE(t, &p->threadlist, entries, t2) {
346 		free(t);
347 	}
348 	LIST_REMOVE(p, entries);
349 	free(p);
350 }
351 
352 static void
353 detach_all_procs(struct trussinfo *info)
354 {
355 	struct procinfo *p, *p2;
356 
357 	LIST_FOREACH_SAFE(p, &info->proclist, entries, p2) {
358 		detach_proc(p->pid);
359 		free_proc(p);
360 	}
361 }
362 
363 static struct procinfo *
364 find_proc(struct trussinfo *info, pid_t pid)
365 {
366 	struct procinfo *np;
367 
368 	LIST_FOREACH(np, &info->proclist, entries) {
369 		if (np->pid == pid)
370 			return (np);
371 	}
372 
373 	return (NULL);
374 }
375 
376 /*
377  * Change curthread member based on (pid, lwpid).
378  */
379 static void
380 find_thread(struct trussinfo *info, pid_t pid, lwpid_t lwpid)
381 {
382 	struct procinfo *np;
383 	struct threadinfo *nt;
384 
385 	np = find_proc(info, pid);
386 	assert(np != NULL);
387 
388 	LIST_FOREACH(nt, &np->threadlist, entries) {
389 		if (nt->tid == lwpid) {
390 			info->curthread = nt;
391 			return;
392 		}
393 	}
394 	errx(1, "could not find thread");
395 }
396 
397 /*
398  * When a process exits, it should have exactly one thread left.
399  * All of the other threads should have reported thread exit events.
400  */
401 static void
402 find_exit_thread(struct trussinfo *info, pid_t pid)
403 {
404 	struct procinfo *p;
405 
406 	p = find_proc(info, pid);
407 	assert(p != NULL);
408 
409 	info->curthread = LIST_FIRST(&p->threadlist);
410 	assert(info->curthread != NULL);
411 	assert(LIST_NEXT(info->curthread, entries) == NULL);
412 }
413 
414 static void
415 alloc_syscall(struct threadinfo *t, struct ptrace_lwpinfo *pl)
416 {
417 	u_int i;
418 
419 	assert(t->in_syscall == 0);
420 	assert(t->cs.number == 0);
421 	assert(t->cs.sc == NULL);
422 	assert(t->cs.nargs == 0);
423 	for (i = 0; i < nitems(t->cs.s_args); i++)
424 		assert(t->cs.s_args[i] == NULL);
425 	memset(t->cs.args, 0, sizeof(t->cs.args));
426 	t->cs.number = pl->pl_syscall_code;
427 	t->in_syscall = 1;
428 }
429 
430 static void
431 free_syscall(struct threadinfo *t)
432 {
433 	u_int i;
434 
435 	for (i = 0; i < t->cs.nargs; i++)
436 		free(t->cs.s_args[i]);
437 	memset(&t->cs, 0, sizeof(t->cs));
438 	t->in_syscall = 0;
439 }
440 
441 static void
442 enter_syscall(struct trussinfo *info, struct threadinfo *t,
443     struct ptrace_lwpinfo *pl)
444 {
445 	struct syscall *sc;
446 	u_int i, narg;
447 
448 	alloc_syscall(t, pl);
449 	narg = MIN(pl->pl_syscall_narg, nitems(t->cs.args));
450 	if (narg != 0 && ptrace(PT_GET_SC_ARGS, t->tid, (caddr_t)t->cs.args,
451 	    sizeof(t->cs.args)) != 0) {
452 		free_syscall(t);
453 		return;
454 	}
455 
456 	sc = get_syscall(t, t->cs.number, narg);
457 	if (sc->unknown)
458 		fprintf(info->outfile, "-- UNKNOWN %s SYSCALL %d --\n",
459 		    t->proc->abi->type, t->cs.number);
460 
461 	t->cs.nargs = sc->decode.nargs;
462 	assert(sc->decode.nargs <= nitems(t->cs.s_args));
463 
464 	t->cs.sc = sc;
465 
466 	/*
467 	 * At this point, we set up the system call arguments.
468 	 * We ignore any OUT ones, however -- those are arguments that
469 	 * are set by the system call, and so are probably meaningless
470 	 * now.	This doesn't currently support arguments that are
471 	 * passed in *and* out, however.
472 	 */
473 #if DEBUG
474 	fprintf(stderr, "syscall %s(", sc->name);
475 #endif
476 	for (i = 0; i < t->cs.nargs; i++) {
477 #if DEBUG
478 		fprintf(stderr, "0x%lx%s",
479 		    t->cs.args[sc->decode.args[i].offset],
480 		    i < (t->cs.nargs - 1) ? "," : "");
481 #endif
482 		if (!(sc->decode.args[i].type & OUT)) {
483 			t->cs.s_args[i] = print_arg(&sc->decode.args[i],
484 			    t->cs.args, NULL, info);
485 		}
486 	}
487 #if DEBUG
488 	fprintf(stderr, ")\n");
489 #endif
490 
491 	clock_gettime(CLOCK_REALTIME, &t->before);
492 }
493 
494 /*
495  * When a thread exits voluntarily (including when a thread calls
496  * exit() to trigger a process exit), the thread's internal state
497  * holds the arguments passed to the exit system call.  When the
498  * thread's exit is reported, log that system call without a return
499  * value.
500  */
501 static void
502 thread_exit_syscall(struct trussinfo *info)
503 {
504 	struct threadinfo *t;
505 
506 	t = info->curthread;
507 	if (!t->in_syscall)
508 		return;
509 
510 	clock_gettime(CLOCK_REALTIME, &t->after);
511 
512 	print_syscall_ret(info, 0, NULL);
513 	free_syscall(t);
514 }
515 
516 static void
517 exit_syscall(struct trussinfo *info, struct ptrace_lwpinfo *pl)
518 {
519 	struct threadinfo *t;
520 	struct procinfo *p;
521 	struct syscall *sc;
522 	struct ptrace_sc_ret psr;
523 	u_int i;
524 
525 	t = info->curthread;
526 	if (!t->in_syscall)
527 		return;
528 
529 	clock_gettime(CLOCK_REALTIME, &t->after);
530 	p = t->proc;
531 	if (ptrace(PT_GET_SC_RET, t->tid, (caddr_t)&psr, sizeof(psr)) != 0) {
532 		free_syscall(t);
533 		return;
534 	}
535 
536 	sc = t->cs.sc;
537 	/*
538 	 * Here, we only look for arguments that have OUT masked in --
539 	 * otherwise, they were handled in enter_syscall().
540 	 */
541 	for (i = 0; i < sc->decode.nargs; i++) {
542 		char *temp;
543 
544 		if (sc->decode.args[i].type & OUT) {
545 			/*
546 			 * If an error occurred, then don't bother
547 			 * getting the data; it may not be valid.
548 			 */
549 			if (psr.sr_error != 0) {
550 				asprintf(&temp, "0x%lx",
551 				    t->cs.args[sc->decode.args[i].offset]);
552 			} else {
553 				temp = print_arg(&sc->decode.args[i],
554 				    t->cs.args, psr.sr_retval, info);
555 			}
556 			t->cs.s_args[i] = temp;
557 		}
558 	}
559 
560 	print_syscall_ret(info, psr.sr_error, psr.sr_retval);
561 	free_syscall(t);
562 
563 	/*
564 	 * If the process executed a new image, check the ABI.  If the
565 	 * new ABI isn't supported, stop tracing this process.
566 	 */
567 	if (pl->pl_flags & PL_FLAG_EXEC) {
568 		assert(LIST_NEXT(LIST_FIRST(&p->threadlist), entries) == NULL);
569 		p->abi = find_abi(p->pid);
570 		if (p->abi == NULL) {
571 			if (ptrace(PT_DETACH, p->pid, (caddr_t)1, 0) < 0)
572 				err(1, "Can not detach the process");
573 			free_proc(p);
574 		}
575 	}
576 }
577 
578 int
579 print_line_prefix(struct trussinfo *info)
580 {
581 	struct timespec timediff;
582 	struct threadinfo *t;
583 	int len;
584 
585 	len = 0;
586 	t = info->curthread;
587 	if (info->flags & (FOLLOWFORKS | DISPLAYTIDS)) {
588 		if (info->flags & FOLLOWFORKS)
589 			len += fprintf(info->outfile, "%5d", t->proc->pid);
590 		if ((info->flags & (FOLLOWFORKS | DISPLAYTIDS)) ==
591 		    (FOLLOWFORKS | DISPLAYTIDS))
592 			len += fprintf(info->outfile, " ");
593 		if (info->flags & DISPLAYTIDS)
594 			len += fprintf(info->outfile, "%6d", t->tid);
595 		len += fprintf(info->outfile, ": ");
596 	}
597 	if (info->flags & ABSOLUTETIMESTAMPS) {
598 		timespecsub(&t->after, &info->start_time, &timediff);
599 		len += fprintf(info->outfile, "%jd.%09ld ",
600 		    (intmax_t)timediff.tv_sec, timediff.tv_nsec);
601 	}
602 	if (info->flags & RELATIVETIMESTAMPS) {
603 		timespecsub(&t->after, &t->before, &timediff);
604 		len += fprintf(info->outfile, "%jd.%09ld ",
605 		    (intmax_t)timediff.tv_sec, timediff.tv_nsec);
606 	}
607 	return (len);
608 }
609 
610 static void
611 report_thread_death(struct trussinfo *info)
612 {
613 	struct threadinfo *t;
614 
615 	t = info->curthread;
616 	clock_gettime(CLOCK_REALTIME, &t->after);
617 	print_line_prefix(info);
618 	fprintf(info->outfile, "<thread %ld exited>\n", (long)t->tid);
619 }
620 
621 static void
622 report_thread_birth(struct trussinfo *info)
623 {
624 	struct threadinfo *t;
625 
626 	t = info->curthread;
627 	clock_gettime(CLOCK_REALTIME, &t->after);
628 	t->before = t->after;
629 	print_line_prefix(info);
630 	fprintf(info->outfile, "<new thread %ld>\n", (long)t->tid);
631 }
632 
633 static void
634 report_exit(struct trussinfo *info, siginfo_t *si)
635 {
636 	struct threadinfo *t;
637 
638 	t = info->curthread;
639 	clock_gettime(CLOCK_REALTIME, &t->after);
640 	print_line_prefix(info);
641 	if (si->si_code == CLD_EXITED)
642 		fprintf(info->outfile, "process exit, rval = %u\n",
643 		    si->si_status);
644 	else
645 		fprintf(info->outfile, "process killed, signal = %u%s\n",
646 		    si->si_status, si->si_code == CLD_DUMPED ?
647 		    " (core dumped)" : "");
648 }
649 
650 static void
651 report_new_child(struct trussinfo *info)
652 {
653 	struct threadinfo *t;
654 
655 	t = info->curthread;
656 	clock_gettime(CLOCK_REALTIME, &t->after);
657 	t->before = t->after;
658 	print_line_prefix(info);
659 	fprintf(info->outfile, "<new process>\n");
660 }
661 
662 void
663 decode_siginfo(FILE *fp, siginfo_t *si)
664 {
665 	const char *str;
666 
667 	fprintf(fp, " code=");
668 	str = sysdecode_sigcode(si->si_signo, si->si_code);
669 	if (str == NULL)
670 		fprintf(fp, "%d", si->si_code);
671 	else
672 		fprintf(fp, "%s", str);
673 	switch (si->si_code) {
674 	case SI_NOINFO:
675 		break;
676 	case SI_QUEUE:
677 		fprintf(fp, " value=%p", si->si_value.sival_ptr);
678 		/* FALLTHROUGH */
679 	case SI_USER:
680 	case SI_LWP:
681 		fprintf(fp, " pid=%jd uid=%jd", (intmax_t)si->si_pid,
682 		    (intmax_t)si->si_uid);
683 		break;
684 	case SI_TIMER:
685 		fprintf(fp, " value=%p", si->si_value.sival_ptr);
686 		fprintf(fp, " timerid=%d", si->si_timerid);
687 		fprintf(fp, " overrun=%d", si->si_overrun);
688 		if (si->si_errno != 0)
689 			fprintf(fp, " errno=%d", si->si_errno);
690 		break;
691 	case SI_ASYNCIO:
692 		fprintf(fp, " value=%p", si->si_value.sival_ptr);
693 		break;
694 	case SI_MESGQ:
695 		fprintf(fp, " value=%p", si->si_value.sival_ptr);
696 		fprintf(fp, " mqd=%d", si->si_mqd);
697 		break;
698 	default:
699 		switch (si->si_signo) {
700 		case SIGILL:
701 		case SIGFPE:
702 		case SIGSEGV:
703 		case SIGBUS:
704 			fprintf(fp, " trapno=%d", si->si_trapno);
705 			fprintf(fp, " addr=%p", si->si_addr);
706 			break;
707 		case SIGCHLD:
708 			fprintf(fp, " pid=%jd uid=%jd", (intmax_t)si->si_pid,
709 			    (intmax_t)si->si_uid);
710 			fprintf(fp, " status=%d", si->si_status);
711 			break;
712 		}
713 	}
714 }
715 
716 static void
717 report_signal(struct trussinfo *info, siginfo_t *si, struct ptrace_lwpinfo *pl)
718 {
719 	struct threadinfo *t;
720 	const char *signame;
721 
722 	t = info->curthread;
723 	clock_gettime(CLOCK_REALTIME, &t->after);
724 	print_line_prefix(info);
725 	signame = sysdecode_signal(si->si_status);
726 	if (signame == NULL)
727 		signame = "?";
728 	fprintf(info->outfile, "SIGNAL %u (%s)", si->si_status, signame);
729 	if (pl->pl_event == PL_EVENT_SIGNAL && pl->pl_flags & PL_FLAG_SI)
730 		decode_siginfo(info->outfile, &pl->pl_siginfo);
731 	fprintf(info->outfile, "\n");
732 
733 }
734 
735 /*
736  * Wait for events until all the processes have exited or truss has been
737  * asked to stop.
738  */
739 void
740 eventloop(struct trussinfo *info)
741 {
742 	struct ptrace_lwpinfo pl;
743 	siginfo_t si;
744 	int pending_signal;
745 
746 	while (!LIST_EMPTY(&info->proclist)) {
747 		if (detaching) {
748 			detach_all_procs(info);
749 			return;
750 		}
751 
752 		if (waitid(P_ALL, 0, &si, WTRAPPED | WEXITED) == -1) {
753 			if (errno == EINTR)
754 				continue;
755 			err(1, "Unexpected error from waitid");
756 		}
757 
758 		assert(si.si_signo == SIGCHLD);
759 
760 		switch (si.si_code) {
761 		case CLD_EXITED:
762 		case CLD_KILLED:
763 		case CLD_DUMPED:
764 			find_exit_thread(info, si.si_pid);
765 			if ((info->flags & COUNTONLY) == 0) {
766 				if (si.si_code == CLD_EXITED)
767 					thread_exit_syscall(info);
768 				report_exit(info, &si);
769 			}
770 			free_proc(info->curthread->proc);
771 			info->curthread = NULL;
772 			break;
773 		case CLD_TRAPPED:
774 			if (ptrace(PT_LWPINFO, si.si_pid, (caddr_t)&pl,
775 			    sizeof(pl)) == -1)
776 				err(1, "ptrace(PT_LWPINFO)");
777 
778 			if (pl.pl_flags & PL_FLAG_CHILD) {
779 				new_proc(info, si.si_pid, pl.pl_lwpid);
780 				assert(LIST_FIRST(&info->proclist)->abi !=
781 				    NULL);
782 			} else if (pl.pl_flags & PL_FLAG_BORN)
783 				new_thread(find_proc(info, si.si_pid),
784 				    pl.pl_lwpid);
785 			find_thread(info, si.si_pid, pl.pl_lwpid);
786 
787 			if (si.si_status == SIGTRAP &&
788 			    (pl.pl_flags & (PL_FLAG_BORN|PL_FLAG_EXITED|
789 			    PL_FLAG_SCE|PL_FLAG_SCX)) != 0) {
790 				if (pl.pl_flags & PL_FLAG_BORN) {
791 					if ((info->flags & COUNTONLY) == 0)
792 						report_thread_birth(info);
793 				} else if (pl.pl_flags & PL_FLAG_EXITED) {
794 					if ((info->flags & COUNTONLY) == 0)
795 						report_thread_death(info);
796 					free_thread(info->curthread);
797 					info->curthread = NULL;
798 				} else if (pl.pl_flags & PL_FLAG_SCE)
799 					enter_syscall(info, info->curthread, &pl);
800 				else if (pl.pl_flags & PL_FLAG_SCX)
801 					exit_syscall(info, &pl);
802 				pending_signal = 0;
803 			} else if (pl.pl_flags & PL_FLAG_CHILD) {
804 				if ((info->flags & COUNTONLY) == 0)
805 					report_new_child(info);
806 				pending_signal = 0;
807 			} else {
808 				if ((info->flags & NOSIGS) == 0)
809 					report_signal(info, &si, &pl);
810 				pending_signal = si.si_status;
811 			}
812 			ptrace(PT_SYSCALL, si.si_pid, (caddr_t)1,
813 			    pending_signal);
814 			break;
815 		case CLD_STOPPED:
816 			errx(1, "waitid reported CLD_STOPPED");
817 		case CLD_CONTINUED:
818 			break;
819 		}
820 	}
821 }
822