xref: /freebsd/usr.bin/kdump/kdump.c (revision 1e248b8346cbf585990a4da0199c6cc3b9cc2340)
1 /*-
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1988, 1993\n\
33 	The Regents of the University of California.  All rights reserved.\n";
34 #endif /* not lint */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)kdump.c	8.1 (Berkeley) 6/6/93";
39 #endif
40 #endif /* not lint */
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #define _KERNEL
45 extern int errno;
46 #include <sys/errno.h>
47 #undef _KERNEL
48 #include <sys/param.h>
49 #include <sys/capsicum.h>
50 #include <sys/errno.h>
51 #define _KERNEL
52 #include <sys/time.h>
53 #undef _KERNEL
54 #include <sys/uio.h>
55 #include <sys/ktrace.h>
56 #include <sys/ioctl.h>
57 #include <sys/socket.h>
58 #include <sys/stat.h>
59 #include <sys/sysent.h>
60 #include <sys/umtx.h>
61 #include <sys/un.h>
62 #include <sys/queue.h>
63 #include <sys/wait.h>
64 #ifdef HAVE_LIBCASPER
65 #include <sys/nv.h>
66 #endif
67 #include <arpa/inet.h>
68 #include <netinet/in.h>
69 #include <ctype.h>
70 #include <err.h>
71 #include <grp.h>
72 #include <inttypes.h>
73 #include <locale.h>
74 #include <netdb.h>
75 #include <nl_types.h>
76 #include <pwd.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <sysdecode.h>
81 #include <termios.h>
82 #include <time.h>
83 #include <unistd.h>
84 #include <vis.h>
85 #include "ktrace.h"
86 #include "kdump_subr.h"
87 
88 #ifdef HAVE_LIBCASPER
89 #include <libcasper.h>
90 
91 #include <casper/cap_grp.h>
92 #include <casper/cap_pwd.h>
93 #endif
94 
95 u_int abidump(struct ktr_header *);
96 int fetchprocinfo(struct ktr_header *, u_int *);
97 int fread_tail(void *, int, int);
98 void dumpheader(struct ktr_header *);
99 void ktrsyscall(struct ktr_syscall *, u_int);
100 void ktrsysret(struct ktr_sysret *, u_int);
101 void ktrnamei(char *, int);
102 void hexdump(char *, int, int);
103 void visdump(char *, int, int);
104 void ktrgenio(struct ktr_genio *, int);
105 void ktrpsig(struct ktr_psig *);
106 void ktrcsw(struct ktr_csw *);
107 void ktrcsw_old(struct ktr_csw_old *);
108 void ktruser_malloc(void *);
109 void ktruser_rtld(int, void *);
110 void ktruser(int, void *);
111 void ktrcaprights(cap_rights_t *);
112 void ktrsockaddr(struct sockaddr *);
113 void ktrstat(struct stat *);
114 void ktrstruct(char *, size_t);
115 void ktrcapfail(struct ktr_cap_fail *);
116 void ktrfault(struct ktr_fault *);
117 void ktrfaultend(struct ktr_faultend *);
118 void limitfd(int fd);
119 void usage(void);
120 
121 #define	TIMESTAMP_NONE		0x0
122 #define	TIMESTAMP_ABSOLUTE	0x1
123 #define	TIMESTAMP_ELAPSED	0x2
124 #define	TIMESTAMP_RELATIVE	0x4
125 
126 extern const char *signames[];
127 
128 static int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
129     resolv = 0, abiflag = 0, syscallno = 0;
130 static const char *tracefile = DEF_TRACEFILE;
131 static struct ktr_header ktr_header;
132 
133 #define TIME_FORMAT	"%b %e %T %Y"
134 #define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
135 
136 #define print_number(i,n,c) do {					\
137 	if (decimal)							\
138 		printf("%c%jd", c, (intmax_t)*i);			\
139 	else								\
140 		printf("%c%#jx", c, (uintmax_t)(u_register_t)*i);	\
141 	i++;								\
142 	n--;								\
143 	c = ',';							\
144 } while (0)
145 
146 struct proc_info
147 {
148 	TAILQ_ENTRY(proc_info)	info;
149 	u_int			sv_flags;
150 	pid_t			pid;
151 };
152 
153 static TAILQ_HEAD(trace_procs, proc_info) trace_procs;
154 
155 #ifdef HAVE_LIBCASPER
156 static cap_channel_t *cappwd, *capgrp;
157 #endif
158 
159 static void
160 strerror_init(void)
161 {
162 
163 	/*
164 	 * Cache NLS data before entering capability mode.
165 	 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
166 	 */
167 	(void)catopen("libc", NL_CAT_LOCALE);
168 }
169 
170 static void
171 localtime_init(void)
172 {
173 	time_t ltime;
174 
175 	/*
176 	 * Allow localtime(3) to cache /etc/localtime content before entering
177 	 * capability mode.
178 	 * XXXPJD: There should be localtime_init() in libc.
179 	 */
180 	(void)time(&ltime);
181 	(void)localtime(&ltime);
182 }
183 
184 #ifdef HAVE_LIBCASPER
185 static int
186 cappwdgrp_setup(cap_channel_t **cappwdp, cap_channel_t **capgrpp)
187 {
188 	cap_channel_t *capcas, *cappwdloc, *capgrploc;
189 	const char *cmds[1], *fields[1];
190 
191 	capcas = cap_init();
192 	if (capcas == NULL) {
193 		err(1, "unable to create casper process");
194 		exit(1);
195 	}
196 	cappwdloc = cap_service_open(capcas, "system.pwd");
197 	capgrploc = cap_service_open(capcas, "system.grp");
198 	/* Casper capability no longer needed. */
199 	cap_close(capcas);
200 	if (cappwdloc == NULL || capgrploc == NULL) {
201 		if (cappwdloc == NULL)
202 			warn("unable to open system.pwd service");
203 		if (capgrploc == NULL)
204 			warn("unable to open system.grp service");
205 		exit(1);
206 	}
207 	/* Limit system.pwd to only getpwuid() function and pw_name field. */
208 	cmds[0] = "getpwuid";
209 	if (cap_pwd_limit_cmds(cappwdloc, cmds, 1) < 0)
210 		err(1, "unable to limit system.pwd service");
211 	fields[0] = "pw_name";
212 	if (cap_pwd_limit_fields(cappwdloc, fields, 1) < 0)
213 		err(1, "unable to limit system.pwd service");
214 	/* Limit system.grp to only getgrgid() function and gr_name field. */
215 	cmds[0] = "getgrgid";
216 	if (cap_grp_limit_cmds(capgrploc, cmds, 1) < 0)
217 		err(1, "unable to limit system.grp service");
218 	fields[0] = "gr_name";
219 	if (cap_grp_limit_fields(capgrploc, fields, 1) < 0)
220 		err(1, "unable to limit system.grp service");
221 
222 	*cappwdp = cappwdloc;
223 	*capgrpp = capgrploc;
224 	return (0);
225 }
226 #endif	/* HAVE_LIBCASPER */
227 
228 int
229 main(int argc, char *argv[])
230 {
231 	int ch, ktrlen, size;
232 	void *m;
233 	int trpoints = ALL_POINTS;
234 	int drop_logged;
235 	pid_t pid = 0;
236 	u_int sv_flags;
237 
238 	setlocale(LC_CTYPE, "");
239 
240 	timestamp = TIMESTAMP_NONE;
241 
242 	while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1)
243 		switch (ch) {
244 		case 'A':
245 			abiflag = 1;
246 			break;
247 		case 'f':
248 			tracefile = optarg;
249 			break;
250 		case 'd':
251 			decimal = 1;
252 			break;
253 		case 'l':
254 			tail = 1;
255 			break;
256 		case 'm':
257 			maxdata = atoi(optarg);
258 			break;
259 		case 'n':
260 			fancy = 0;
261 			break;
262 		case 'p':
263 			pid = atoi(optarg);
264 			break;
265 		case 'r':
266 			resolv = 1;
267 			break;
268 		case 'S':
269 			syscallno = 1;
270 			break;
271 		case 's':
272 			suppressdata = 1;
273 			break;
274 		case 'E':
275 			timestamp |= TIMESTAMP_ELAPSED;
276 			break;
277 		case 'H':
278 			threads = 1;
279 			break;
280 		case 'R':
281 			timestamp |= TIMESTAMP_RELATIVE;
282 			break;
283 		case 'T':
284 			timestamp |= TIMESTAMP_ABSOLUTE;
285 			break;
286 		case 't':
287 			trpoints = getpoints(optarg);
288 			if (trpoints < 0)
289 				errx(1, "unknown trace point in %s", optarg);
290 			break;
291 		default:
292 			usage();
293 		}
294 
295 	if (argc > optind)
296 		usage();
297 
298 	m = malloc(size = 1025);
299 	if (m == NULL)
300 		errx(1, "%s", strerror(ENOMEM));
301 	if (!freopen(tracefile, "r", stdin))
302 		err(1, "%s", tracefile);
303 
304 	strerror_init();
305 	localtime_init();
306 #ifdef HAVE_LIBCASPER
307 	if (resolv != 0) {
308 		if (cappwdgrp_setup(&cappwd, &capgrp) < 0) {
309 			cappwd = NULL;
310 			capgrp = NULL;
311 		}
312 	}
313 	if (resolv == 0 || (cappwd != NULL && capgrp != NULL)) {
314 		if (cap_enter() < 0 && errno != ENOSYS)
315 			err(1, "unable to enter capability mode");
316 	}
317 #else
318 	if (resolv == 0) {
319 		if (cap_enter() < 0 && errno != ENOSYS)
320 			err(1, "unable to enter capability mode");
321 	}
322 #endif
323 	limitfd(STDIN_FILENO);
324 	limitfd(STDOUT_FILENO);
325 	limitfd(STDERR_FILENO);
326 
327 	TAILQ_INIT(&trace_procs);
328 	drop_logged = 0;
329 	while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
330 		if (ktr_header.ktr_type & KTR_DROP) {
331 			ktr_header.ktr_type &= ~KTR_DROP;
332 			if (!drop_logged && threads) {
333 				printf(
334 				    "%6jd %6jd %-8.*s Events dropped.\n",
335 				    (intmax_t)ktr_header.ktr_pid,
336 				    ktr_header.ktr_tid > 0 ?
337 				    (intmax_t)ktr_header.ktr_tid : 0,
338 				    MAXCOMLEN, ktr_header.ktr_comm);
339 				drop_logged = 1;
340 			} else if (!drop_logged) {
341 				printf("%6jd %-8.*s Events dropped.\n",
342 				    (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
343 				    ktr_header.ktr_comm);
344 				drop_logged = 1;
345 			}
346 		}
347 		if (trpoints & (1<<ktr_header.ktr_type))
348 			if (pid == 0 || ktr_header.ktr_pid == pid ||
349 			    ktr_header.ktr_tid == pid)
350 				dumpheader(&ktr_header);
351 		if ((ktrlen = ktr_header.ktr_len) < 0)
352 			errx(1, "bogus length 0x%x", ktrlen);
353 		if (ktrlen > size) {
354 			m = realloc(m, ktrlen+1);
355 			if (m == NULL)
356 				errx(1, "%s", strerror(ENOMEM));
357 			size = ktrlen;
358 		}
359 		if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
360 			errx(1, "data too short");
361 		if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
362 			continue;
363 		sv_flags = abidump(&ktr_header);
364 		if (pid && ktr_header.ktr_pid != pid &&
365 		    ktr_header.ktr_tid != pid)
366 			continue;
367 		if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
368 			continue;
369 		drop_logged = 0;
370 		switch (ktr_header.ktr_type) {
371 		case KTR_SYSCALL:
372 			ktrsyscall((struct ktr_syscall *)m, sv_flags);
373 			break;
374 		case KTR_SYSRET:
375 			ktrsysret((struct ktr_sysret *)m, sv_flags);
376 			break;
377 		case KTR_NAMEI:
378 		case KTR_SYSCTL:
379 			ktrnamei(m, ktrlen);
380 			break;
381 		case KTR_GENIO:
382 			ktrgenio((struct ktr_genio *)m, ktrlen);
383 			break;
384 		case KTR_PSIG:
385 			ktrpsig((struct ktr_psig *)m);
386 			break;
387 		case KTR_CSW:
388 			if (ktrlen == sizeof(struct ktr_csw_old))
389 				ktrcsw_old((struct ktr_csw_old *)m);
390 			else
391 				ktrcsw((struct ktr_csw *)m);
392 			break;
393 		case KTR_USER:
394 			ktruser(ktrlen, m);
395 			break;
396 		case KTR_STRUCT:
397 			ktrstruct(m, ktrlen);
398 			break;
399 		case KTR_CAPFAIL:
400 			ktrcapfail((struct ktr_cap_fail *)m);
401 			break;
402 		case KTR_FAULT:
403 			ktrfault((struct ktr_fault *)m);
404 			break;
405 		case KTR_FAULTEND:
406 			ktrfaultend((struct ktr_faultend *)m);
407 			break;
408 		default:
409 			printf("\n");
410 			break;
411 		}
412 		if (tail)
413 			fflush(stdout);
414 	}
415 	return 0;
416 }
417 
418 void
419 limitfd(int fd)
420 {
421 	cap_rights_t rights;
422 	unsigned long cmd;
423 
424 	cap_rights_init(&rights, CAP_FSTAT);
425 	cmd = 0;
426 
427 	switch (fd) {
428 	case STDIN_FILENO:
429 		cap_rights_set(&rights, CAP_READ);
430 		break;
431 	case STDOUT_FILENO:
432 		cap_rights_set(&rights, CAP_IOCTL, CAP_WRITE);
433 		cmd = TIOCGETA;	/* required by isatty(3) in printf(3) */
434 		break;
435 	case STDERR_FILENO:
436 		cap_rights_set(&rights, CAP_WRITE);
437 		if (!suppressdata) {
438 			cap_rights_set(&rights, CAP_IOCTL);
439 			cmd = TIOCGWINSZ;
440 		}
441 		break;
442 	default:
443 		abort();
444 	}
445 
446 	if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
447 		err(1, "unable to limit rights for descriptor %d", fd);
448 	if (cmd != 0 && cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
449 		err(1, "unable to limit ioctls for descriptor %d", fd);
450 }
451 
452 int
453 fread_tail(void *buf, int size, int num)
454 {
455 	int i;
456 
457 	while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
458 		sleep(1);
459 		clearerr(stdin);
460 	}
461 	return (i);
462 }
463 
464 int
465 fetchprocinfo(struct ktr_header *kth, u_int *flags)
466 {
467 	struct proc_info *pi;
468 
469 	switch (kth->ktr_type) {
470 	case KTR_PROCCTOR:
471 		TAILQ_FOREACH(pi, &trace_procs, info) {
472 			if (pi->pid == kth->ktr_pid) {
473 				TAILQ_REMOVE(&trace_procs, pi, info);
474 				break;
475 			}
476 		}
477 		pi = malloc(sizeof(struct proc_info));
478 		if (pi == NULL)
479 			errx(1, "%s", strerror(ENOMEM));
480 		pi->sv_flags = *flags;
481 		pi->pid = kth->ktr_pid;
482 		TAILQ_INSERT_TAIL(&trace_procs, pi, info);
483 		return (1);
484 
485 	case KTR_PROCDTOR:
486 		TAILQ_FOREACH(pi, &trace_procs, info) {
487 			if (pi->pid == kth->ktr_pid) {
488 				TAILQ_REMOVE(&trace_procs, pi, info);
489 				free(pi);
490 				break;
491 			}
492 		}
493 		return (1);
494 	}
495 
496 	return (0);
497 }
498 
499 u_int
500 abidump(struct ktr_header *kth)
501 {
502 	struct proc_info *pi;
503 	const char *abi;
504 	const char *arch;
505 	u_int flags = 0;
506 
507 	TAILQ_FOREACH(pi, &trace_procs, info) {
508 		if (pi->pid == kth->ktr_pid) {
509 			flags = pi->sv_flags;
510 			break;
511 		}
512 	}
513 
514 	if (abiflag == 0)
515 		return (flags);
516 
517 	switch (flags & SV_ABI_MASK) {
518 	case SV_ABI_LINUX:
519 		abi = "L";
520 		break;
521 	case SV_ABI_FREEBSD:
522 		abi = "F";
523 		break;
524 	case SV_ABI_CLOUDABI:
525 		abi = "C";
526 		break;
527 	default:
528 		abi = "U";
529 		break;
530 	}
531 
532 	if (flags & SV_LP64)
533 		arch = "64";
534 	else if (flags & SV_ILP32)
535 		arch = "32";
536 	else
537 		arch = "00";
538 
539 	printf("%s%s  ", abi, arch);
540 
541 	return (flags);
542 }
543 
544 void
545 dumpheader(struct ktr_header *kth)
546 {
547 	static char unknown[64];
548 	static struct timeval prevtime, prevtime_e, temp;
549 	const char *type;
550 	const char *sign;
551 
552 	switch (kth->ktr_type) {
553 	case KTR_SYSCALL:
554 		type = "CALL";
555 		break;
556 	case KTR_SYSRET:
557 		type = "RET ";
558 		break;
559 	case KTR_NAMEI:
560 		type = "NAMI";
561 		break;
562 	case KTR_GENIO:
563 		type = "GIO ";
564 		break;
565 	case KTR_PSIG:
566 		type = "PSIG";
567 		break;
568 	case KTR_CSW:
569 		type = "CSW ";
570 		break;
571 	case KTR_USER:
572 		type = "USER";
573 		break;
574 	case KTR_STRUCT:
575 		type = "STRU";
576 		break;
577 	case KTR_SYSCTL:
578 		type = "SCTL";
579 		break;
580 	case KTR_PROCCTOR:
581 		/* FALLTHROUGH */
582 	case KTR_PROCDTOR:
583 		return;
584 	case KTR_CAPFAIL:
585 		type = "CAP ";
586 		break;
587 	case KTR_FAULT:
588 		type = "PFLT";
589 		break;
590 	case KTR_FAULTEND:
591 		type = "PRET";
592 		break;
593 	default:
594 		sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
595 		type = unknown;
596 	}
597 
598 	/*
599 	 * The ktr_tid field was previously the ktr_buffer field, which held
600 	 * the kernel pointer value for the buffer associated with data
601 	 * following the record header.  It now holds a threadid, but only
602 	 * for trace files after the change.  Older trace files still contain
603 	 * kernel pointers.  Detect this and suppress the results by printing
604 	 * negative tid's as 0.
605 	 */
606 	if (threads)
607 		printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
608 		    kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
609 		    MAXCOMLEN, kth->ktr_comm);
610 	else
611 		printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
612 		    kth->ktr_comm);
613         if (timestamp) {
614 		if (timestamp & TIMESTAMP_ABSOLUTE) {
615 			printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
616 			    kth->ktr_time.tv_usec);
617 		}
618 		if (timestamp & TIMESTAMP_ELAPSED) {
619 			if (prevtime_e.tv_sec == 0)
620 				prevtime_e = kth->ktr_time;
621 			timevalsub(&kth->ktr_time, &prevtime_e);
622 			printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
623 			    kth->ktr_time.tv_usec);
624 			timevaladd(&kth->ktr_time, &prevtime_e);
625 		}
626 		if (timestamp & TIMESTAMP_RELATIVE) {
627 			if (prevtime.tv_sec == 0)
628 				prevtime = kth->ktr_time;
629 			temp = kth->ktr_time;
630 			timevalsub(&kth->ktr_time, &prevtime);
631 			if ((intmax_t)kth->ktr_time.tv_sec < 0) {
632                         	kth->ktr_time = prevtime;
633 				prevtime = temp;
634 				timevalsub(&kth->ktr_time, &prevtime);
635 				sign = "-";
636 			} else {
637 				prevtime = temp;
638 				sign = "";
639 			}
640 			printf("%s%jd.%06ld ", sign, (intmax_t)kth->ktr_time.tv_sec,
641 			    kth->ktr_time.tv_usec);
642 		}
643 	}
644 	printf("%s  ", type);
645 }
646 
647 #include <sys/syscall.h>
648 
649 static void
650 ioctlname(unsigned long val)
651 {
652 	const char *str;
653 
654 	str = sysdecode_ioctlname(val);
655 	if (str != NULL)
656 		printf("%s", str);
657 	else if (decimal)
658 		printf("%lu", val);
659 	else
660 		printf("%#lx", val);
661 }
662 
663 static enum sysdecode_abi
664 syscallabi(u_int sv_flags)
665 {
666 
667 	if (sv_flags == 0)
668 		return (SYSDECODE_ABI_FREEBSD);
669 	switch (sv_flags & SV_ABI_MASK) {
670 	case SV_ABI_FREEBSD:
671 		return (SYSDECODE_ABI_FREEBSD);
672 #if defined(__amd64__) || defined(__i386__)
673 	case SV_ABI_LINUX:
674 #ifdef __amd64__
675 		if (sv_flags & SV_ILP32)
676 			return (SYSDECODE_ABI_LINUX32);
677 #endif
678 		return (SYSDECODE_ABI_LINUX);
679 #endif
680 #if defined(__aarch64__) || defined(__amd64__)
681 	case SV_ABI_CLOUDABI:
682 		return (SYSDECODE_ABI_CLOUDABI64);
683 #endif
684 	default:
685 		return (SYSDECODE_ABI_UNKNOWN);
686 	}
687 }
688 
689 static void
690 syscallname(u_int code, u_int sv_flags)
691 {
692 	const char *name;
693 
694 	name = sysdecode_syscallname(syscallabi(sv_flags), code);
695 	if (name == NULL)
696 		printf("[%d]", code);
697 	else {
698 		printf("%s", name);
699 		if (syscallno)
700 			printf("[%d]", code);
701 	}
702 }
703 
704 void
705 ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags)
706 {
707 	int narg = ktr->ktr_narg;
708 	register_t *ip;
709 	intmax_t arg;
710 
711 	syscallname(ktr->ktr_code, sv_flags);
712 	ip = &ktr->ktr_args[0];
713 	if (narg) {
714 		char c = '(';
715 		if (fancy &&
716 		    (sv_flags == 0 ||
717 		    (sv_flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
718 			switch (ktr->ktr_code) {
719 			case SYS_bindat:
720 			case SYS_connectat:
721 			case SYS_faccessat:
722 			case SYS_fchmodat:
723 			case SYS_fchownat:
724 			case SYS_fstatat:
725 			case SYS_futimesat:
726 			case SYS_linkat:
727 			case SYS_mkdirat:
728 			case SYS_mkfifoat:
729 			case SYS_mknodat:
730 			case SYS_openat:
731 			case SYS_readlinkat:
732 			case SYS_renameat:
733 			case SYS_unlinkat:
734 			case SYS_utimensat:
735 				putchar('(');
736 				atfdname(*ip, decimal);
737 				c = ',';
738 				ip++;
739 				narg--;
740 				break;
741 			}
742 			switch (ktr->ktr_code) {
743 			case SYS_ioctl: {
744 				print_number(ip, narg, c);
745 				putchar(c);
746 				ioctlname(*ip);
747 				c = ',';
748 				ip++;
749 				narg--;
750 				break;
751 			}
752 			case SYS_ptrace:
753 				putchar('(');
754 				ptraceopname(*ip);
755 				c = ',';
756 				ip++;
757 				narg--;
758 				break;
759 			case SYS_access:
760 			case SYS_eaccess:
761 			case SYS_faccessat:
762 				print_number(ip, narg, c);
763 				putchar(',');
764 				accessmodename(*ip);
765 				ip++;
766 				narg--;
767 				break;
768 			case SYS_open:
769 			case SYS_openat:
770 				print_number(ip, narg, c);
771 				putchar(',');
772 				flagsandmodename(ip[0], ip[1], decimal);
773 				ip += 2;
774 				narg -= 2;
775 				break;
776 			case SYS_wait4:
777 				print_number(ip, narg, c);
778 				print_number(ip, narg, c);
779 				/*
780 				 * A flags value of zero is valid for
781 				 * wait4() but not for wait6(), so
782 				 * handle zero special here.
783 				 */
784 				if (*ip == 0) {
785 					print_number(ip, narg, c);
786 				} else {
787 					putchar(',');
788 					wait6optname(*ip);
789 					ip++;
790 					narg--;
791 				}
792 				break;
793 			case SYS_wait6:
794 				putchar('(');
795 				idtypename(*ip, decimal);
796 				c = ',';
797 				ip++;
798 				narg--;
799 				print_number(ip, narg, c);
800 				print_number(ip, narg, c);
801 				putchar(',');
802 				wait6optname(*ip);
803 				ip++;
804 				narg--;
805 				break;
806 			case SYS_chmod:
807 			case SYS_fchmod:
808 			case SYS_lchmod:
809 				print_number(ip, narg, c);
810 				putchar(',');
811 				modename(*ip);
812 				ip++;
813 				narg--;
814 				break;
815 			case SYS_mknod:
816 			case SYS_mknodat:
817 				print_number(ip, narg, c);
818 				putchar(',');
819 				modename(*ip);
820 				ip++;
821 				narg--;
822 				break;
823 			case SYS_getfsstat:
824 				print_number(ip, narg, c);
825 				print_number(ip, narg, c);
826 				putchar(',');
827 				getfsstatflagsname(*ip);
828 				ip++;
829 				narg--;
830 				break;
831 			case SYS_mount:
832 				print_number(ip, narg, c);
833 				print_number(ip, narg, c);
834 				putchar(',');
835 				mountflagsname(*ip);
836 				ip++;
837 				narg--;
838 				break;
839 			case SYS_unmount:
840 				print_number(ip, narg, c);
841 				putchar(',');
842 				mountflagsname(*ip);
843 				ip++;
844 				narg--;
845 				break;
846 			case SYS_recvmsg:
847 			case SYS_sendmsg:
848 				print_number(ip, narg, c);
849 				print_number(ip, narg, c);
850 				putchar(',');
851 				sendrecvflagsname(*ip);
852 				ip++;
853 				narg--;
854 				break;
855 			case SYS_recvfrom:
856 			case SYS_sendto:
857 				print_number(ip, narg, c);
858 				print_number(ip, narg, c);
859 				print_number(ip, narg, c);
860 				putchar(',');
861 				sendrecvflagsname(*ip);
862 				ip++;
863 				narg--;
864 				break;
865 			case SYS_chflags:
866 			case SYS_fchflags:
867 			case SYS_lchflags:
868 				print_number(ip, narg, c);
869 				putchar(',');
870 				modename(*ip);
871 				ip++;
872 				narg--;
873 				break;
874 			case SYS_kill:
875 				print_number(ip, narg, c);
876 				putchar(',');
877 				signame(*ip);
878 				ip++;
879 				narg--;
880 				break;
881 			case SYS_reboot:
882 				putchar('(');
883 				rebootoptname(*ip);
884 				ip++;
885 				narg--;
886 				break;
887 			case SYS_umask:
888 				putchar('(');
889 				modename(*ip);
890 				ip++;
891 				narg--;
892 				break;
893 			case SYS_msync:
894 				print_number(ip, narg, c);
895 				print_number(ip, narg, c);
896 				putchar(',');
897 				msyncflagsname(*ip);
898 				ip++;
899 				narg--;
900 				break;
901 #ifdef SYS_freebsd6_mmap
902 			case SYS_freebsd6_mmap:
903 				print_number(ip, narg, c);
904 				print_number(ip, narg, c);
905 				putchar(',');
906 				mmapprotname(*ip);
907 				putchar(',');
908 				ip++;
909 				narg--;
910 				mmapflagsname(*ip);
911 				ip++;
912 				narg--;
913 				break;
914 #endif
915 			case SYS_mmap:
916 				print_number(ip, narg, c);
917 				print_number(ip, narg, c);
918 				putchar(',');
919 				mmapprotname(*ip);
920 				putchar(',');
921 				ip++;
922 				narg--;
923 				mmapflagsname(*ip);
924 				ip++;
925 				narg--;
926 				break;
927 			case SYS_mprotect:
928 				print_number(ip, narg, c);
929 				print_number(ip, narg, c);
930 				putchar(',');
931 				mmapprotname(*ip);
932 				ip++;
933 				narg--;
934 				break;
935 			case SYS_madvise:
936 				print_number(ip, narg, c);
937 				print_number(ip, narg, c);
938 				putchar(',');
939 				madvisebehavname(*ip);
940 				ip++;
941 				narg--;
942 				break;
943 			case SYS_setpriority:
944 				print_number(ip, narg, c);
945 				print_number(ip, narg, c);
946 				putchar(',');
947 				prioname(*ip);
948 				ip++;
949 				narg--;
950 				break;
951 			case SYS_fcntl:
952 				print_number(ip, narg, c);
953 				putchar(',');
954 				fcntlcmdname(ip[0], ip[1], decimal);
955 				ip += 2;
956 				narg -= 2;
957 				break;
958 			case SYS_socket: {
959 				int sockdomain;
960 				putchar('(');
961 				sockdomain = *ip;
962 				sockdomainname(sockdomain);
963 				ip++;
964 				narg--;
965 				putchar(',');
966 				socktypenamewithflags(*ip);
967 				ip++;
968 				narg--;
969 				if (sockdomain == PF_INET ||
970 				    sockdomain == PF_INET6) {
971 					putchar(',');
972 					sockipprotoname(*ip);
973 					ip++;
974 					narg--;
975 				}
976 				c = ',';
977 				break;
978 			}
979 			case SYS_setsockopt:
980 			case SYS_getsockopt:
981 				print_number(ip, narg, c);
982 				putchar(',');
983 				sockoptlevelname(*ip, decimal);
984 				if (*ip == SOL_SOCKET) {
985 					ip++;
986 					narg--;
987 					putchar(',');
988 					sockoptname(*ip);
989 				}
990 				ip++;
991 				narg--;
992 				break;
993 #ifdef SYS_freebsd6_lseek
994 			case SYS_freebsd6_lseek:
995 				print_number(ip, narg, c);
996 				/* Hidden 'pad' argument, not in lseek(2) */
997 				print_number(ip, narg, c);
998 				print_number(ip, narg, c);
999 				putchar(',');
1000 				whencename(*ip);
1001 				ip++;
1002 				narg--;
1003 				break;
1004 #endif
1005 			case SYS_lseek:
1006 				print_number(ip, narg, c);
1007 				/* Hidden 'pad' argument, not in lseek(2) */
1008 				print_number(ip, narg, c);
1009 				putchar(',');
1010 				whencename(*ip);
1011 				ip++;
1012 				narg--;
1013 				break;
1014 			case SYS_flock:
1015 				print_number(ip, narg, c);
1016 				putchar(',');
1017 				flockname(*ip);
1018 				ip++;
1019 				narg--;
1020 				break;
1021 			case SYS_mkfifo:
1022 			case SYS_mkfifoat:
1023 			case SYS_mkdir:
1024 			case SYS_mkdirat:
1025 				print_number(ip, narg, c);
1026 				putchar(',');
1027 				modename(*ip);
1028 				ip++;
1029 				narg--;
1030 				break;
1031 			case SYS_shutdown:
1032 				print_number(ip, narg, c);
1033 				putchar(',');
1034 				shutdownhowname(*ip);
1035 				ip++;
1036 				narg--;
1037 				break;
1038 			case SYS_socketpair:
1039 				putchar('(');
1040 				sockdomainname(*ip);
1041 				ip++;
1042 				narg--;
1043 				putchar(',');
1044 				socktypenamewithflags(*ip);
1045 				ip++;
1046 				narg--;
1047 				c = ',';
1048 				break;
1049 			case SYS_getrlimit:
1050 			case SYS_setrlimit:
1051 				putchar('(');
1052 				rlimitname(*ip);
1053 				ip++;
1054 				narg--;
1055 				c = ',';
1056 				break;
1057 			case SYS_quotactl:
1058 				print_number(ip, narg, c);
1059 				putchar(',');
1060 				quotactlname(*ip);
1061 				ip++;
1062 				narg--;
1063 				c = ',';
1064 				break;
1065 			case SYS_nfssvc:
1066 				putchar('(');
1067 				nfssvcname(*ip);
1068 				ip++;
1069 				narg--;
1070 				c = ',';
1071 				break;
1072 			case SYS_rtprio:
1073 				putchar('(');
1074 				rtprioname(*ip);
1075 				ip++;
1076 				narg--;
1077 				c = ',';
1078 				break;
1079 			case SYS___semctl:
1080 				print_number(ip, narg, c);
1081 				print_number(ip, narg, c);
1082 				putchar(',');
1083 				semctlname(*ip);
1084 				ip++;
1085 				narg--;
1086 				break;
1087 			case SYS_semget:
1088 				print_number(ip, narg, c);
1089 				print_number(ip, narg, c);
1090 				putchar(',');
1091 				semgetname(*ip);
1092 				ip++;
1093 				narg--;
1094 				break;
1095 			case SYS_msgctl:
1096 				print_number(ip, narg, c);
1097 				putchar(',');
1098 				shmctlname(*ip);
1099 				ip++;
1100 				narg--;
1101 				break;
1102 			case SYS_shmat:
1103 				print_number(ip, narg, c);
1104 				print_number(ip, narg, c);
1105 				putchar(',');
1106 				shmatname(*ip);
1107 				ip++;
1108 				narg--;
1109 				break;
1110 			case SYS_shmctl:
1111 				print_number(ip, narg, c);
1112 				putchar(',');
1113 				shmctlname(*ip);
1114 				ip++;
1115 				narg--;
1116 				break;
1117 			case SYS_shm_open:
1118 				print_number(ip, narg, c);
1119 				putchar(',');
1120 				flagsname(ip[0]);
1121 				printf(",0%o", (unsigned int)ip[1]);
1122 				ip += 3;
1123 				narg -= 3;
1124 				break;
1125 			case SYS_minherit:
1126 				print_number(ip, narg, c);
1127 				print_number(ip, narg, c);
1128 				putchar(',');
1129 				minheritname(*ip);
1130 				ip++;
1131 				narg--;
1132 				break;
1133 			case SYS_rfork:
1134 				putchar('(');
1135 				rforkname(*ip);
1136 				ip++;
1137 				narg--;
1138 				c = ',';
1139 				break;
1140 			case SYS_lio_listio:
1141 				putchar('(');
1142 				lio_listioname(*ip);
1143 				ip++;
1144 				narg--;
1145 				c = ',';
1146 				break;
1147 			case SYS_mlockall:
1148 				putchar('(');
1149 				mlockallname(*ip);
1150 				ip++;
1151 				narg--;
1152 				break;
1153 			case SYS_sched_setscheduler:
1154 				print_number(ip, narg, c);
1155 				putchar(',');
1156 				schedpolicyname(*ip);
1157 				ip++;
1158 				narg--;
1159 				break;
1160 			case SYS_sched_get_priority_max:
1161 			case SYS_sched_get_priority_min:
1162 				putchar('(');
1163 				schedpolicyname(*ip);
1164 				ip++;
1165 				narg--;
1166 				break;
1167 			case SYS_sendfile:
1168 				print_number(ip, narg, c);
1169 				print_number(ip, narg, c);
1170 				print_number(ip, narg, c);
1171 				print_number(ip, narg, c);
1172 				print_number(ip, narg, c);
1173 				print_number(ip, narg, c);
1174 				putchar(',');
1175 				sendfileflagsname(*(int *)ip);
1176 				ip++;
1177 				narg--;
1178 				break;
1179 			case SYS_kldsym:
1180 				print_number(ip, narg, c);
1181 				putchar(',');
1182 				kldsymcmdname(*ip);
1183 				ip++;
1184 				narg--;
1185 				break;
1186 			case SYS_sigprocmask:
1187 				putchar('(');
1188 				sigprocmaskhowname(*ip);
1189 				ip++;
1190 				narg--;
1191 				c = ',';
1192 				break;
1193 			case SYS___acl_get_file:
1194 			case SYS___acl_set_file:
1195 			case SYS___acl_get_fd:
1196 			case SYS___acl_set_fd:
1197 			case SYS___acl_delete_file:
1198 			case SYS___acl_delete_fd:
1199 			case SYS___acl_aclcheck_file:
1200 			case SYS___acl_aclcheck_fd:
1201 			case SYS___acl_get_link:
1202 			case SYS___acl_set_link:
1203 			case SYS___acl_delete_link:
1204 			case SYS___acl_aclcheck_link:
1205 				print_number(ip, narg, c);
1206 				putchar(',');
1207 				acltypename(*ip);
1208 				ip++;
1209 				narg--;
1210 				break;
1211 			case SYS_sigaction:
1212 				putchar('(');
1213 				signame(*ip);
1214 				ip++;
1215 				narg--;
1216 				c = ',';
1217 				break;
1218 			case SYS_extattrctl:
1219 				print_number(ip, narg, c);
1220 				putchar(',');
1221 				extattrctlname(*ip);
1222 				ip++;
1223 				narg--;
1224 				break;
1225 			case SYS_nmount:
1226 				print_number(ip, narg, c);
1227 				print_number(ip, narg, c);
1228 				putchar(',');
1229 				mountflagsname(*ip);
1230 				ip++;
1231 				narg--;
1232 				break;
1233 			case SYS_thr_create:
1234 				print_number(ip, narg, c);
1235 				print_number(ip, narg, c);
1236 				putchar(',');
1237 				thrcreateflagsname(*ip);
1238 				ip++;
1239 				narg--;
1240 				break;
1241 			case SYS_thr_kill:
1242 				print_number(ip, narg, c);
1243 				putchar(',');
1244 				signame(*ip);
1245 				ip++;
1246 				narg--;
1247 				break;
1248 			case SYS_kldunloadf:
1249 				print_number(ip, narg, c);
1250 				putchar(',');
1251 				kldunloadfflagsname(*ip);
1252 				ip++;
1253 				narg--;
1254 				break;
1255 			case SYS_linkat:
1256 			case SYS_renameat:
1257 			case SYS_symlinkat:
1258 				print_number(ip, narg, c);
1259 				putchar(',');
1260 				atfdname(*ip, decimal);
1261 				ip++;
1262 				narg--;
1263 				break;
1264 			case SYS_cap_fcntls_limit:
1265 				print_number(ip, narg, c);
1266 				putchar(',');
1267 				arg = *ip;
1268 				ip++;
1269 				narg--;
1270 				capfcntlname(arg);
1271 				break;
1272 			case SYS_posix_fadvise:
1273 				print_number(ip, narg, c);
1274 				print_number(ip, narg, c);
1275 				print_number(ip, narg, c);
1276 				(void)putchar(',');
1277 				fadvisebehavname((int)*ip);
1278 				ip++;
1279 				narg--;
1280 				break;
1281 			case SYS_procctl:
1282 				putchar('(');
1283 				idtypename(*ip, decimal);
1284 				c = ',';
1285 				ip++;
1286 				narg--;
1287 				print_number(ip, narg, c);
1288 				putchar(',');
1289 				procctlcmdname(*ip);
1290 				ip++;
1291 				narg--;
1292 				break;
1293 			case SYS__umtx_op:
1294 				print_number(ip, narg, c);
1295 				putchar(',');
1296 				umtxopname(*ip);
1297 				switch (*ip) {
1298 				case UMTX_OP_CV_WAIT:
1299 					ip++;
1300 					narg--;
1301 					putchar(',');
1302 					umtxcvwaitflags(*ip);
1303 					break;
1304 				case UMTX_OP_RW_RDLOCK:
1305 					ip++;
1306 					narg--;
1307 					putchar(',');
1308 					umtxrwlockflags(*ip);
1309 					break;
1310 				}
1311 				ip++;
1312 				narg--;
1313 			}
1314 		}
1315 		while (narg > 0) {
1316 			print_number(ip, narg, c);
1317 		}
1318 		putchar(')');
1319 	}
1320 	putchar('\n');
1321 }
1322 
1323 void
1324 ktrsysret(struct ktr_sysret *ktr, u_int sv_flags)
1325 {
1326 	register_t ret = ktr->ktr_retval;
1327 	int error = ktr->ktr_error;
1328 
1329 	syscallname(ktr->ktr_code, sv_flags);
1330 	printf(" ");
1331 
1332 	if (error == 0) {
1333 		if (fancy) {
1334 			printf("%ld", (long)ret);
1335 			if (ret < 0 || ret > 9)
1336 				printf("/%#lx", (unsigned long)ret);
1337 		} else {
1338 			if (decimal)
1339 				printf("%ld", (long)ret);
1340 			else
1341 				printf("%#lx", (unsigned long)ret);
1342 		}
1343 	} else if (error == ERESTART)
1344 		printf("RESTART");
1345 	else if (error == EJUSTRETURN)
1346 		printf("JUSTRETURN");
1347 	else {
1348 		printf("-1 errno %d", sysdecode_freebsd_to_abi_errno(
1349 		    syscallabi(sv_flags), error));
1350 		if (fancy)
1351 			printf(" %s", strerror(ktr->ktr_error));
1352 	}
1353 	putchar('\n');
1354 }
1355 
1356 void
1357 ktrnamei(char *cp, int len)
1358 {
1359 	printf("\"%.*s\"\n", len, cp);
1360 }
1361 
1362 void
1363 hexdump(char *p, int len, int screenwidth)
1364 {
1365 	int n, i;
1366 	int width;
1367 
1368 	width = 0;
1369 	do {
1370 		width += 2;
1371 		i = 13;			/* base offset */
1372 		i += (width / 2) + 1;	/* spaces every second byte */
1373 		i += (width * 2);	/* width of bytes */
1374 		i += 3;			/* "  |" */
1375 		i += width;		/* each byte */
1376 		i += 1;			/* "|" */
1377 	} while (i < screenwidth);
1378 	width -= 2;
1379 
1380 	for (n = 0; n < len; n += width) {
1381 		for (i = n; i < n + width; i++) {
1382 			if ((i % width) == 0) {	/* beginning of line */
1383 				printf("       0x%04x", i);
1384 			}
1385 			if ((i % 2) == 0) {
1386 				printf(" ");
1387 			}
1388 			if (i < len)
1389 				printf("%02x", p[i] & 0xff);
1390 			else
1391 				printf("  ");
1392 		}
1393 		printf("  |");
1394 		for (i = n; i < n + width; i++) {
1395 			if (i >= len)
1396 				break;
1397 			if (p[i] >= ' ' && p[i] <= '~')
1398 				printf("%c", p[i]);
1399 			else
1400 				printf(".");
1401 		}
1402 		printf("|\n");
1403 	}
1404 	if ((i % width) != 0)
1405 		printf("\n");
1406 }
1407 
1408 void
1409 visdump(char *dp, int datalen, int screenwidth)
1410 {
1411 	int col = 0;
1412 	char *cp;
1413 	int width;
1414 	char visbuf[5];
1415 
1416 	printf("       \"");
1417 	col = 8;
1418 	for (;datalen > 0; datalen--, dp++) {
1419 		 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1420 		cp = visbuf;
1421 		/*
1422 		 * Keep track of printables and
1423 		 * space chars (like fold(1)).
1424 		 */
1425 		if (col == 0) {
1426 			putchar('\t');
1427 			col = 8;
1428 		}
1429 		switch(*cp) {
1430 		case '\n':
1431 			col = 0;
1432 			putchar('\n');
1433 			continue;
1434 		case '\t':
1435 			width = 8 - (col&07);
1436 			break;
1437 		default:
1438 			width = strlen(cp);
1439 		}
1440 		if (col + width > (screenwidth-2)) {
1441 			printf("\\\n\t");
1442 			col = 8;
1443 		}
1444 		col += width;
1445 		do {
1446 			putchar(*cp++);
1447 		} while (*cp);
1448 	}
1449 	if (col == 0)
1450 		printf("       ");
1451 	printf("\"\n");
1452 }
1453 
1454 void
1455 ktrgenio(struct ktr_genio *ktr, int len)
1456 {
1457 	int datalen = len - sizeof (struct ktr_genio);
1458 	char *dp = (char *)ktr + sizeof (struct ktr_genio);
1459 	static int screenwidth = 0;
1460 	int i, binary;
1461 
1462 	printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1463 		ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1464 		datalen == 1 ? "" : "s");
1465 	if (suppressdata)
1466 		return;
1467 	if (screenwidth == 0) {
1468 		struct winsize ws;
1469 
1470 		if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1471 		    ws.ws_col > 8)
1472 			screenwidth = ws.ws_col;
1473 		else
1474 			screenwidth = 80;
1475 	}
1476 	if (maxdata && datalen > maxdata)
1477 		datalen = maxdata;
1478 
1479 	for (i = 0, binary = 0; i < datalen && binary == 0; i++)  {
1480 		if (dp[i] >= 32 && dp[i] < 127)
1481 			continue;
1482 		if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1483 			continue;
1484 		binary = 1;
1485 	}
1486 	if (binary)
1487 		hexdump(dp, datalen, screenwidth);
1488 	else
1489 		visdump(dp, datalen, screenwidth);
1490 }
1491 
1492 const char *signames[] = {
1493 	"NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT",	/*  1 - 6  */
1494 	"EMT", "FPE", "KILL", "BUS", "SEGV", "SYS",		/*  7 - 12 */
1495 	"PIPE", "ALRM",  "TERM", "URG", "STOP", "TSTP",		/* 13 - 18 */
1496 	"CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU",		/* 19 - 24 */
1497 	"XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1",	/* 25 - 30 */
1498 	"USR2", NULL,						/* 31 - 32 */
1499 };
1500 
1501 void
1502 ktrpsig(struct ktr_psig *psig)
1503 {
1504 	if (psig->signo > 0 && psig->signo < NSIG)
1505 		printf("SIG%s ", signames[psig->signo]);
1506 	else
1507 		printf("SIG %d ", psig->signo);
1508 	if (psig->action == SIG_DFL) {
1509 		printf("SIG_DFL code=");
1510 		sigcodename(psig->signo, psig->code);
1511 		putchar('\n');
1512 	} else {
1513 		printf("caught handler=0x%lx mask=0x%x code=",
1514 		    (u_long)psig->action, psig->mask.__bits[0]);
1515 		sigcodename(psig->signo, psig->code);
1516 		putchar('\n');
1517 	}
1518 }
1519 
1520 void
1521 ktrcsw_old(struct ktr_csw_old *cs)
1522 {
1523 	printf("%s %s\n", cs->out ? "stop" : "resume",
1524 		cs->user ? "user" : "kernel");
1525 }
1526 
1527 void
1528 ktrcsw(struct ktr_csw *cs)
1529 {
1530 	printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1531 	    cs->user ? "user" : "kernel", cs->wmesg);
1532 }
1533 
1534 void
1535 ktruser(int len, void *p)
1536 {
1537 	unsigned char *cp;
1538 
1539 	if (sysdecode_utrace(stdout, p, len)) {
1540 		printf("\n");
1541 		return;
1542 	}
1543 
1544 	printf("%d ", len);
1545 	cp = p;
1546 	while (len--)
1547 		if (decimal)
1548 			printf(" %d", *cp++);
1549 		else
1550 			printf(" %02x", *cp++);
1551 	printf("\n");
1552 }
1553 
1554 void
1555 ktrcaprights(cap_rights_t *rightsp)
1556 {
1557 
1558 	printf("cap_rights_t ");
1559 	capname(rightsp);
1560 	printf("\n");
1561 }
1562 
1563 void
1564 ktrsockaddr(struct sockaddr *sa)
1565 {
1566 /*
1567  TODO: Support additional address families
1568 	#include <netnatm/natm.h>
1569 	struct sockaddr_natm	*natm;
1570 	#include <netsmb/netbios.h>
1571 	struct sockaddr_nb	*nb;
1572 */
1573 	char addr[64];
1574 
1575 	/*
1576 	 * note: ktrstruct() has already verified that sa points to a
1577 	 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1578 	 * sa->sa_len bytes long.
1579 	 */
1580 	printf("struct sockaddr { ");
1581 	sockfamilyname(sa->sa_family);
1582 	printf(", ");
1583 
1584 #define check_sockaddr_len(n)					\
1585 	if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) {	\
1586 		printf("invalid");				\
1587 		break;						\
1588 	}
1589 
1590 	switch(sa->sa_family) {
1591 	case AF_INET: {
1592 		struct sockaddr_in sa_in;
1593 
1594 		memset(&sa_in, 0, sizeof(sa_in));
1595 		memcpy(&sa_in, sa, sa->sa_len);
1596 		check_sockaddr_len(in);
1597 		inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1598 		printf("%s:%u", addr, ntohs(sa_in.sin_port));
1599 		break;
1600 	}
1601 	case AF_INET6: {
1602 		struct sockaddr_in6 sa_in6;
1603 
1604 		memset(&sa_in6, 0, sizeof(sa_in6));
1605 		memcpy(&sa_in6, sa, sa->sa_len);
1606 		check_sockaddr_len(in6);
1607 		getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1608 		    addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1609 		printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1610 		break;
1611 	}
1612 	case AF_UNIX: {
1613 		struct sockaddr_un sa_un;
1614 
1615 		memset(&sa_un, 0, sizeof(sa_un));
1616 		memcpy(&sa_un, sa, sa->sa_len);
1617 		printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1618 		break;
1619 	}
1620 	default:
1621 		printf("unknown address family");
1622 	}
1623 	printf(" }\n");
1624 }
1625 
1626 void
1627 ktrstat(struct stat *statp)
1628 {
1629 	char mode[12], timestr[PATH_MAX + 4];
1630 	struct passwd *pwd;
1631 	struct group  *grp;
1632 	struct tm *tm;
1633 
1634 	/*
1635 	 * note: ktrstruct() has already verified that statp points to a
1636 	 * buffer exactly sizeof(struct stat) bytes long.
1637 	 */
1638 	printf("struct stat {");
1639 	printf("dev=%ju, ino=%ju, ",
1640 		(uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1641 	if (resolv == 0)
1642 		printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1643 	else {
1644 		strmode(statp->st_mode, mode);
1645 		printf("mode=%s, ", mode);
1646 	}
1647 	printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1648 	if (resolv == 0) {
1649 		pwd = NULL;
1650 	} else {
1651 #ifdef HAVE_LIBCASPER
1652 		if (cappwd != NULL)
1653 			pwd = cap_getpwuid(cappwd, statp->st_uid);
1654 		else
1655 #endif
1656 			pwd = getpwuid(statp->st_uid);
1657 	}
1658 	if (pwd == NULL)
1659 		printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1660 	else
1661 		printf("uid=\"%s\", ", pwd->pw_name);
1662 	if (resolv == 0) {
1663 		grp = NULL;
1664 	} else {
1665 #ifdef HAVE_LIBCASPER
1666 		if (capgrp != NULL)
1667 			grp = cap_getgrgid(capgrp, statp->st_gid);
1668 		else
1669 #endif
1670 			grp = getgrgid(statp->st_gid);
1671 	}
1672 	if (grp == NULL)
1673 		printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1674 	else
1675 		printf("gid=\"%s\", ", grp->gr_name);
1676 	printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1677 	printf("atime=");
1678 	if (resolv == 0)
1679 		printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1680 	else {
1681 		tm = localtime(&statp->st_atim.tv_sec);
1682 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1683 		printf("\"%s\"", timestr);
1684 	}
1685 	if (statp->st_atim.tv_nsec != 0)
1686 		printf(".%09ld, ", statp->st_atim.tv_nsec);
1687 	else
1688 		printf(", ");
1689 	printf("stime=");
1690 	if (resolv == 0)
1691 		printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1692 	else {
1693 		tm = localtime(&statp->st_mtim.tv_sec);
1694 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1695 		printf("\"%s\"", timestr);
1696 	}
1697 	if (statp->st_mtim.tv_nsec != 0)
1698 		printf(".%09ld, ", statp->st_mtim.tv_nsec);
1699 	else
1700 		printf(", ");
1701 	printf("ctime=");
1702 	if (resolv == 0)
1703 		printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1704 	else {
1705 		tm = localtime(&statp->st_ctim.tv_sec);
1706 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1707 		printf("\"%s\"", timestr);
1708 	}
1709 	if (statp->st_ctim.tv_nsec != 0)
1710 		printf(".%09ld, ", statp->st_ctim.tv_nsec);
1711 	else
1712 		printf(", ");
1713 	printf("birthtime=");
1714 	if (resolv == 0)
1715 		printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1716 	else {
1717 		tm = localtime(&statp->st_birthtim.tv_sec);
1718 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1719 		printf("\"%s\"", timestr);
1720 	}
1721 	if (statp->st_birthtim.tv_nsec != 0)
1722 		printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1723 	else
1724 		printf(", ");
1725 	printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1726 		(uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1727 		(intmax_t)statp->st_blocks, statp->st_flags);
1728 	printf(" }\n");
1729 }
1730 
1731 void
1732 ktrstruct(char *buf, size_t buflen)
1733 {
1734 	char *name, *data;
1735 	size_t namelen, datalen;
1736 	int i;
1737 	cap_rights_t rights;
1738 	struct stat sb;
1739 	struct sockaddr_storage ss;
1740 
1741 	for (name = buf, namelen = 0;
1742 	     namelen < buflen && name[namelen] != '\0';
1743 	     ++namelen)
1744 		/* nothing */;
1745 	if (namelen == buflen)
1746 		goto invalid;
1747 	if (name[namelen] != '\0')
1748 		goto invalid;
1749 	data = buf + namelen + 1;
1750 	datalen = buflen - namelen - 1;
1751 	if (datalen == 0)
1752 		goto invalid;
1753 	/* sanity check */
1754 	for (i = 0; i < (int)namelen; ++i)
1755 		if (!isalpha(name[i]))
1756 			goto invalid;
1757 	if (strcmp(name, "caprights") == 0) {
1758 		if (datalen != sizeof(cap_rights_t))
1759 			goto invalid;
1760 		memcpy(&rights, data, datalen);
1761 		ktrcaprights(&rights);
1762 	} else if (strcmp(name, "stat") == 0) {
1763 		if (datalen != sizeof(struct stat))
1764 			goto invalid;
1765 		memcpy(&sb, data, datalen);
1766 		ktrstat(&sb);
1767 	} else if (strcmp(name, "sockaddr") == 0) {
1768 		if (datalen > sizeof(ss))
1769 			goto invalid;
1770 		memcpy(&ss, data, datalen);
1771 		if (datalen != ss.ss_len)
1772 			goto invalid;
1773 		ktrsockaddr((struct sockaddr *)&ss);
1774 	} else {
1775 		printf("unknown structure\n");
1776 	}
1777 	return;
1778 invalid:
1779 	printf("invalid record\n");
1780 }
1781 
1782 void
1783 ktrcapfail(struct ktr_cap_fail *ktr)
1784 {
1785 	switch (ktr->cap_type) {
1786 	case CAPFAIL_NOTCAPABLE:
1787 		/* operation on fd with insufficient capabilities */
1788 		printf("operation requires ");
1789 		capname(&ktr->cap_needed);
1790 		printf(", descriptor holds ");
1791 		capname(&ktr->cap_held);
1792 		break;
1793 	case CAPFAIL_INCREASE:
1794 		/* requested more capabilities than fd already has */
1795 		printf("attempt to increase capabilities from ");
1796 		capname(&ktr->cap_held);
1797 		printf(" to ");
1798 		capname(&ktr->cap_needed);
1799 		break;
1800 	case CAPFAIL_SYSCALL:
1801 		/* called restricted syscall */
1802 		printf("disallowed system call");
1803 		break;
1804 	case CAPFAIL_LOOKUP:
1805 		/* used ".." in strict-relative mode */
1806 		printf("restricted VFS lookup");
1807 		break;
1808 	default:
1809 		printf("unknown capability failure: ");
1810 		capname(&ktr->cap_needed);
1811 		printf(" ");
1812 		capname(&ktr->cap_held);
1813 		break;
1814 	}
1815 	printf("\n");
1816 }
1817 
1818 void
1819 ktrfault(struct ktr_fault *ktr)
1820 {
1821 
1822 	printf("0x%jx ", (uintmax_t)ktr->vaddr);
1823 	vmprotname(ktr->type);
1824 	printf("\n");
1825 }
1826 
1827 void
1828 ktrfaultend(struct ktr_faultend *ktr)
1829 {
1830 
1831 	vmresultname(ktr->result);
1832 	printf("\n");
1833 }
1834 
1835 void
1836 usage(void)
1837 {
1838 	fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
1839 	    "[-m maxdata] [-p pid] [-t trstr]\n");
1840 	exit(1);
1841 }
1842