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