xref: /freebsd/usr.bin/kdump/kdump.c (revision 3fc36ee018bb836bd1796067cf4ef8683f166ebc)
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(int, void *);
109 void ktrcaprights(cap_rights_t *);
110 void ktritimerval(struct itimerval *it);
111 void ktrsockaddr(struct sockaddr *);
112 void ktrstat(struct stat *);
113 void ktrstruct(char *, size_t);
114 void ktrcapfail(struct ktr_cap_fail *);
115 void ktrfault(struct ktr_fault *);
116 void ktrfaultend(struct ktr_faultend *);
117 void limitfd(int fd);
118 void usage(void);
119 
120 #define	TIMESTAMP_NONE		0x0
121 #define	TIMESTAMP_ABSOLUTE	0x1
122 #define	TIMESTAMP_ELAPSED	0x2
123 #define	TIMESTAMP_RELATIVE	0x4
124 
125 extern const char *signames[];
126 
127 static int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
128     resolv = 0, abiflag = 0, syscallno = 0;
129 static const char *tracefile = DEF_TRACEFILE;
130 static struct ktr_header ktr_header;
131 
132 #define TIME_FORMAT	"%b %e %T %Y"
133 #define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
134 
135 #define print_number(i,n,c) do {					\
136 	if (decimal)							\
137 		printf("%c%jd", c, (intmax_t)*i);			\
138 	else								\
139 		printf("%c%#jx", c, (uintmax_t)(u_register_t)*i);	\
140 	i++;								\
141 	n--;								\
142 	c = ',';							\
143 } while (0)
144 
145 struct proc_info
146 {
147 	TAILQ_ENTRY(proc_info)	info;
148 	u_int			sv_flags;
149 	pid_t			pid;
150 };
151 
152 static TAILQ_HEAD(trace_procs, proc_info) trace_procs;
153 
154 #ifdef HAVE_LIBCASPER
155 static cap_channel_t *cappwd, *capgrp;
156 #endif
157 
158 static void
159 strerror_init(void)
160 {
161 
162 	/*
163 	 * Cache NLS data before entering capability mode.
164 	 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
165 	 */
166 	(void)catopen("libc", NL_CAT_LOCALE);
167 }
168 
169 static void
170 localtime_init(void)
171 {
172 	time_t ltime;
173 
174 	/*
175 	 * Allow localtime(3) to cache /etc/localtime content before entering
176 	 * capability mode.
177 	 * XXXPJD: There should be localtime_init() in libc.
178 	 */
179 	(void)time(&ltime);
180 	(void)localtime(&ltime);
181 }
182 
183 #ifdef HAVE_LIBCASPER
184 static int
185 cappwdgrp_setup(cap_channel_t **cappwdp, cap_channel_t **capgrpp)
186 {
187 	cap_channel_t *capcas, *cappwdloc, *capgrploc;
188 	const char *cmds[1], *fields[1];
189 
190 	capcas = cap_init();
191 	if (capcas == NULL) {
192 		err(1, "unable to create casper process");
193 		exit(1);
194 	}
195 	cappwdloc = cap_service_open(capcas, "system.pwd");
196 	capgrploc = cap_service_open(capcas, "system.grp");
197 	/* Casper capability no longer needed. */
198 	cap_close(capcas);
199 	if (cappwdloc == NULL || capgrploc == NULL) {
200 		if (cappwdloc == NULL)
201 			warn("unable to open system.pwd service");
202 		if (capgrploc == NULL)
203 			warn("unable to open system.grp service");
204 		exit(1);
205 	}
206 	/* Limit system.pwd to only getpwuid() function and pw_name field. */
207 	cmds[0] = "getpwuid";
208 	if (cap_pwd_limit_cmds(cappwdloc, cmds, 1) < 0)
209 		err(1, "unable to limit system.pwd service");
210 	fields[0] = "pw_name";
211 	if (cap_pwd_limit_fields(cappwdloc, fields, 1) < 0)
212 		err(1, "unable to limit system.pwd service");
213 	/* Limit system.grp to only getgrgid() function and gr_name field. */
214 	cmds[0] = "getgrgid";
215 	if (cap_grp_limit_cmds(capgrploc, cmds, 1) < 0)
216 		err(1, "unable to limit system.grp service");
217 	fields[0] = "gr_name";
218 	if (cap_grp_limit_fields(capgrploc, fields, 1) < 0)
219 		err(1, "unable to limit system.grp service");
220 
221 	*cappwdp = cappwdloc;
222 	*capgrpp = capgrploc;
223 	return (0);
224 }
225 #endif	/* HAVE_LIBCASPER */
226 
227 int
228 main(int argc, char *argv[])
229 {
230 	int ch, ktrlen, size;
231 	void *m;
232 	int trpoints = ALL_POINTS;
233 	int drop_logged;
234 	pid_t pid = 0;
235 	u_int sv_flags;
236 
237 	setlocale(LC_CTYPE, "");
238 
239 	timestamp = TIMESTAMP_NONE;
240 
241 	while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1)
242 		switch (ch) {
243 		case 'A':
244 			abiflag = 1;
245 			break;
246 		case 'f':
247 			tracefile = optarg;
248 			break;
249 		case 'd':
250 			decimal = 1;
251 			break;
252 		case 'l':
253 			tail = 1;
254 			break;
255 		case 'm':
256 			maxdata = atoi(optarg);
257 			break;
258 		case 'n':
259 			fancy = 0;
260 			break;
261 		case 'p':
262 			pid = atoi(optarg);
263 			break;
264 		case 'r':
265 			resolv = 1;
266 			break;
267 		case 'S':
268 			syscallno = 1;
269 			break;
270 		case 's':
271 			suppressdata = 1;
272 			break;
273 		case 'E':
274 			timestamp |= TIMESTAMP_ELAPSED;
275 			break;
276 		case 'H':
277 			threads = 1;
278 			break;
279 		case 'R':
280 			timestamp |= TIMESTAMP_RELATIVE;
281 			break;
282 		case 'T':
283 			timestamp |= TIMESTAMP_ABSOLUTE;
284 			break;
285 		case 't':
286 			trpoints = getpoints(optarg);
287 			if (trpoints < 0)
288 				errx(1, "unknown trace point in %s", optarg);
289 			break;
290 		default:
291 			usage();
292 		}
293 
294 	if (argc > optind)
295 		usage();
296 
297 	m = malloc(size = 1025);
298 	if (m == NULL)
299 		errx(1, "%s", strerror(ENOMEM));
300 	if (strcmp(tracefile, "-") != 0)
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 			case SYS_fchmodat:
810 				print_number(ip, narg, c);
811 				putchar(',');
812 				modename(*ip);
813 				ip++;
814 				narg--;
815 				break;
816 			case SYS_mknod:
817 			case SYS_mknodat:
818 				print_number(ip, narg, c);
819 				putchar(',');
820 				modename(*ip);
821 				ip++;
822 				narg--;
823 				break;
824 			case SYS_getfsstat:
825 				print_number(ip, narg, c);
826 				print_number(ip, narg, c);
827 				putchar(',');
828 				getfsstatflagsname(*ip);
829 				ip++;
830 				narg--;
831 				break;
832 			case SYS_mount:
833 				print_number(ip, narg, c);
834 				print_number(ip, narg, c);
835 				putchar(',');
836 				mountflagsname(*ip);
837 				ip++;
838 				narg--;
839 				break;
840 			case SYS_unmount:
841 				print_number(ip, narg, c);
842 				putchar(',');
843 				mountflagsname(*ip);
844 				ip++;
845 				narg--;
846 				break;
847 			case SYS_recvmsg:
848 			case SYS_sendmsg:
849 				print_number(ip, narg, c);
850 				print_number(ip, narg, c);
851 				putchar(',');
852 				sendrecvflagsname(*ip);
853 				ip++;
854 				narg--;
855 				break;
856 			case SYS_recvfrom:
857 			case SYS_sendto:
858 				print_number(ip, narg, c);
859 				print_number(ip, narg, c);
860 				print_number(ip, narg, c);
861 				putchar(',');
862 				sendrecvflagsname(*ip);
863 				ip++;
864 				narg--;
865 				break;
866 			case SYS_chflags:
867 			case SYS_fchflags:
868 			case SYS_lchflags:
869 				print_number(ip, narg, c);
870 				putchar(',');
871 				modename(*ip);
872 				ip++;
873 				narg--;
874 				break;
875 			case SYS_kill:
876 				print_number(ip, narg, c);
877 				putchar(',');
878 				signame(*ip);
879 				ip++;
880 				narg--;
881 				break;
882 			case SYS_reboot:
883 				putchar('(');
884 				rebootoptname(*ip);
885 				ip++;
886 				narg--;
887 				break;
888 			case SYS_umask:
889 				putchar('(');
890 				modename(*ip);
891 				ip++;
892 				narg--;
893 				break;
894 			case SYS_msync:
895 				print_number(ip, narg, c);
896 				print_number(ip, narg, c);
897 				putchar(',');
898 				msyncflagsname(*ip);
899 				ip++;
900 				narg--;
901 				break;
902 #ifdef SYS_freebsd6_mmap
903 			case SYS_freebsd6_mmap:
904 				print_number(ip, narg, c);
905 				print_number(ip, narg, c);
906 				putchar(',');
907 				mmapprotname(*ip);
908 				putchar(',');
909 				ip++;
910 				narg--;
911 				mmapflagsname(*ip);
912 				ip++;
913 				narg--;
914 				break;
915 #endif
916 			case SYS_mmap:
917 				print_number(ip, narg, c);
918 				print_number(ip, narg, c);
919 				putchar(',');
920 				mmapprotname(*ip);
921 				putchar(',');
922 				ip++;
923 				narg--;
924 				mmapflagsname(*ip);
925 				ip++;
926 				narg--;
927 				break;
928 			case SYS_mprotect:
929 				print_number(ip, narg, c);
930 				print_number(ip, narg, c);
931 				putchar(',');
932 				mmapprotname(*ip);
933 				ip++;
934 				narg--;
935 				break;
936 			case SYS_madvise:
937 				print_number(ip, narg, c);
938 				print_number(ip, narg, c);
939 				putchar(',');
940 				madvisebehavname(*ip);
941 				ip++;
942 				narg--;
943 				break;
944 			case SYS_setpriority:
945 				print_number(ip, narg, c);
946 				print_number(ip, narg, c);
947 				putchar(',');
948 				prioname(*ip);
949 				ip++;
950 				narg--;
951 				break;
952 			case SYS_fcntl:
953 				print_number(ip, narg, c);
954 				putchar(',');
955 				fcntlcmdname(ip[0], ip[1], decimal);
956 				ip += 2;
957 				narg -= 2;
958 				break;
959 			case SYS_socket: {
960 				int sockdomain;
961 				putchar('(');
962 				sockdomain = *ip;
963 				sockdomainname(sockdomain);
964 				ip++;
965 				narg--;
966 				putchar(',');
967 				socktypenamewithflags(*ip);
968 				ip++;
969 				narg--;
970 				if (sockdomain == PF_INET ||
971 				    sockdomain == PF_INET6) {
972 					putchar(',');
973 					sockipprotoname(*ip);
974 					ip++;
975 					narg--;
976 				}
977 				c = ',';
978 				break;
979 			}
980 			case SYS_setsockopt:
981 			case SYS_getsockopt:
982 				print_number(ip, narg, c);
983 				putchar(',');
984 				sockoptlevelname(*ip, decimal);
985 				if (*ip == SOL_SOCKET) {
986 					ip++;
987 					narg--;
988 					putchar(',');
989 					sockoptname(*ip);
990 				}
991 				ip++;
992 				narg--;
993 				break;
994 #ifdef SYS_freebsd6_lseek
995 			case SYS_freebsd6_lseek:
996 				print_number(ip, narg, c);
997 				/* Hidden 'pad' argument, not in lseek(2) */
998 				print_number(ip, narg, c);
999 				print_number(ip, narg, c);
1000 				putchar(',');
1001 				whencename(*ip);
1002 				ip++;
1003 				narg--;
1004 				break;
1005 #endif
1006 			case SYS_lseek:
1007 				print_number(ip, narg, c);
1008 				/* Hidden 'pad' argument, not in lseek(2) */
1009 				print_number(ip, narg, c);
1010 				putchar(',');
1011 				whencename(*ip);
1012 				ip++;
1013 				narg--;
1014 				break;
1015 			case SYS_flock:
1016 				print_number(ip, narg, c);
1017 				putchar(',');
1018 				flockname(*ip);
1019 				ip++;
1020 				narg--;
1021 				break;
1022 			case SYS_mkfifo:
1023 			case SYS_mkfifoat:
1024 			case SYS_mkdir:
1025 			case SYS_mkdirat:
1026 				print_number(ip, narg, c);
1027 				putchar(',');
1028 				modename(*ip);
1029 				ip++;
1030 				narg--;
1031 				break;
1032 			case SYS_shutdown:
1033 				print_number(ip, narg, c);
1034 				putchar(',');
1035 				shutdownhowname(*ip);
1036 				ip++;
1037 				narg--;
1038 				break;
1039 			case SYS_socketpair:
1040 				putchar('(');
1041 				sockdomainname(*ip);
1042 				ip++;
1043 				narg--;
1044 				putchar(',');
1045 				socktypenamewithflags(*ip);
1046 				ip++;
1047 				narg--;
1048 				c = ',';
1049 				break;
1050 			case SYS_getrlimit:
1051 			case SYS_setrlimit:
1052 				putchar('(');
1053 				rlimitname(*ip);
1054 				ip++;
1055 				narg--;
1056 				c = ',';
1057 				break;
1058 			case SYS_quotactl:
1059 				print_number(ip, narg, c);
1060 				putchar(',');
1061 				quotactlname(*ip);
1062 				ip++;
1063 				narg--;
1064 				c = ',';
1065 				break;
1066 			case SYS_nfssvc:
1067 				putchar('(');
1068 				nfssvcname(*ip);
1069 				ip++;
1070 				narg--;
1071 				c = ',';
1072 				break;
1073 			case SYS_rtprio:
1074 				putchar('(');
1075 				rtprioname(*ip);
1076 				ip++;
1077 				narg--;
1078 				c = ',';
1079 				break;
1080 			case SYS___semctl:
1081 				print_number(ip, narg, c);
1082 				print_number(ip, narg, c);
1083 				putchar(',');
1084 				semctlname(*ip);
1085 				ip++;
1086 				narg--;
1087 				break;
1088 			case SYS_semget:
1089 				print_number(ip, narg, c);
1090 				print_number(ip, narg, c);
1091 				putchar(',');
1092 				semgetname(*ip);
1093 				ip++;
1094 				narg--;
1095 				break;
1096 			case SYS_msgctl:
1097 				print_number(ip, narg, c);
1098 				putchar(',');
1099 				shmctlname(*ip);
1100 				ip++;
1101 				narg--;
1102 				break;
1103 			case SYS_shmat:
1104 				print_number(ip, narg, c);
1105 				print_number(ip, narg, c);
1106 				putchar(',');
1107 				shmatname(*ip);
1108 				ip++;
1109 				narg--;
1110 				break;
1111 			case SYS_shmctl:
1112 				print_number(ip, narg, c);
1113 				putchar(',');
1114 				shmctlname(*ip);
1115 				ip++;
1116 				narg--;
1117 				break;
1118 			case SYS_shm_open:
1119 				print_number(ip, narg, c);
1120 				putchar(',');
1121 				flagsname(ip[0]);
1122 				printf(",0%o", (unsigned int)ip[1]);
1123 				ip += 3;
1124 				narg -= 3;
1125 				break;
1126 			case SYS_minherit:
1127 				print_number(ip, narg, c);
1128 				print_number(ip, narg, c);
1129 				putchar(',');
1130 				minheritname(*ip);
1131 				ip++;
1132 				narg--;
1133 				break;
1134 			case SYS_rfork:
1135 				putchar('(');
1136 				rforkname(*ip);
1137 				ip++;
1138 				narg--;
1139 				c = ',';
1140 				break;
1141 			case SYS_lio_listio:
1142 				putchar('(');
1143 				lio_listioname(*ip);
1144 				ip++;
1145 				narg--;
1146 				c = ',';
1147 				break;
1148 			case SYS_mlockall:
1149 				putchar('(');
1150 				mlockallname(*ip);
1151 				ip++;
1152 				narg--;
1153 				break;
1154 			case SYS_sched_setscheduler:
1155 				print_number(ip, narg, c);
1156 				putchar(',');
1157 				schedpolicyname(*ip);
1158 				ip++;
1159 				narg--;
1160 				break;
1161 			case SYS_sched_get_priority_max:
1162 			case SYS_sched_get_priority_min:
1163 				putchar('(');
1164 				schedpolicyname(*ip);
1165 				ip++;
1166 				narg--;
1167 				break;
1168 			case SYS_sendfile:
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 				print_number(ip, narg, c);
1175 				putchar(',');
1176 				sendfileflagsname(*(int *)ip);
1177 				ip++;
1178 				narg--;
1179 				break;
1180 			case SYS_kldsym:
1181 				print_number(ip, narg, c);
1182 				putchar(',');
1183 				kldsymcmdname(*ip);
1184 				ip++;
1185 				narg--;
1186 				break;
1187 			case SYS_sigprocmask:
1188 				putchar('(');
1189 				sigprocmaskhowname(*ip);
1190 				ip++;
1191 				narg--;
1192 				c = ',';
1193 				break;
1194 			case SYS___acl_get_file:
1195 			case SYS___acl_set_file:
1196 			case SYS___acl_get_fd:
1197 			case SYS___acl_set_fd:
1198 			case SYS___acl_delete_file:
1199 			case SYS___acl_delete_fd:
1200 			case SYS___acl_aclcheck_file:
1201 			case SYS___acl_aclcheck_fd:
1202 			case SYS___acl_get_link:
1203 			case SYS___acl_set_link:
1204 			case SYS___acl_delete_link:
1205 			case SYS___acl_aclcheck_link:
1206 				print_number(ip, narg, c);
1207 				putchar(',');
1208 				acltypename(*ip);
1209 				ip++;
1210 				narg--;
1211 				break;
1212 			case SYS_sigaction:
1213 				putchar('(');
1214 				signame(*ip);
1215 				ip++;
1216 				narg--;
1217 				c = ',';
1218 				break;
1219 			case SYS_extattrctl:
1220 				print_number(ip, narg, c);
1221 				putchar(',');
1222 				extattrctlname(*ip);
1223 				ip++;
1224 				narg--;
1225 				break;
1226 			case SYS_nmount:
1227 				print_number(ip, narg, c);
1228 				print_number(ip, narg, c);
1229 				putchar(',');
1230 				mountflagsname(*ip);
1231 				ip++;
1232 				narg--;
1233 				break;
1234 			case SYS_thr_create:
1235 				print_number(ip, narg, c);
1236 				print_number(ip, narg, c);
1237 				putchar(',');
1238 				thrcreateflagsname(*ip);
1239 				ip++;
1240 				narg--;
1241 				break;
1242 			case SYS_thr_kill:
1243 				print_number(ip, narg, c);
1244 				putchar(',');
1245 				signame(*ip);
1246 				ip++;
1247 				narg--;
1248 				break;
1249 			case SYS_kldunloadf:
1250 				print_number(ip, narg, c);
1251 				putchar(',');
1252 				kldunloadfflagsname(*ip);
1253 				ip++;
1254 				narg--;
1255 				break;
1256 			case SYS_linkat:
1257 			case SYS_renameat:
1258 			case SYS_symlinkat:
1259 				print_number(ip, narg, c);
1260 				putchar(',');
1261 				atfdname(*ip, decimal);
1262 				ip++;
1263 				narg--;
1264 				break;
1265 			case SYS_cap_fcntls_limit:
1266 				print_number(ip, narg, c);
1267 				putchar(',');
1268 				arg = *ip;
1269 				ip++;
1270 				narg--;
1271 				capfcntlname(arg);
1272 				break;
1273 			case SYS_posix_fadvise:
1274 				print_number(ip, narg, c);
1275 				print_number(ip, narg, c);
1276 				print_number(ip, narg, c);
1277 				(void)putchar(',');
1278 				fadvisebehavname((int)*ip);
1279 				ip++;
1280 				narg--;
1281 				break;
1282 			case SYS_procctl:
1283 				putchar('(');
1284 				idtypename(*ip, decimal);
1285 				c = ',';
1286 				ip++;
1287 				narg--;
1288 				print_number(ip, narg, c);
1289 				putchar(',');
1290 				procctlcmdname(*ip);
1291 				ip++;
1292 				narg--;
1293 				break;
1294 			case SYS__umtx_op:
1295 				print_number(ip, narg, c);
1296 				putchar(',');
1297 				umtxopname(*ip);
1298 				switch (*ip) {
1299 				case UMTX_OP_CV_WAIT:
1300 					ip++;
1301 					narg--;
1302 					putchar(',');
1303 					umtxcvwaitflags(*ip);
1304 					break;
1305 				case UMTX_OP_RW_RDLOCK:
1306 					ip++;
1307 					narg--;
1308 					putchar(',');
1309 					umtxrwlockflags(*ip);
1310 					break;
1311 				}
1312 				ip++;
1313 				narg--;
1314 			}
1315 		}
1316 		while (narg > 0) {
1317 			print_number(ip, narg, c);
1318 		}
1319 		putchar(')');
1320 	}
1321 	putchar('\n');
1322 }
1323 
1324 void
1325 ktrsysret(struct ktr_sysret *ktr, u_int sv_flags)
1326 {
1327 	register_t ret = ktr->ktr_retval;
1328 	int error = ktr->ktr_error;
1329 
1330 	syscallname(ktr->ktr_code, sv_flags);
1331 	printf(" ");
1332 
1333 	if (error == 0) {
1334 		if (fancy) {
1335 			printf("%ld", (long)ret);
1336 			if (ret < 0 || ret > 9)
1337 				printf("/%#lx", (unsigned long)ret);
1338 		} else {
1339 			if (decimal)
1340 				printf("%ld", (long)ret);
1341 			else
1342 				printf("%#lx", (unsigned long)ret);
1343 		}
1344 	} else if (error == ERESTART)
1345 		printf("RESTART");
1346 	else if (error == EJUSTRETURN)
1347 		printf("JUSTRETURN");
1348 	else {
1349 		printf("-1 errno %d", sysdecode_freebsd_to_abi_errno(
1350 		    syscallabi(sv_flags), error));
1351 		if (fancy)
1352 			printf(" %s", strerror(ktr->ktr_error));
1353 	}
1354 	putchar('\n');
1355 }
1356 
1357 void
1358 ktrnamei(char *cp, int len)
1359 {
1360 	printf("\"%.*s\"\n", len, cp);
1361 }
1362 
1363 void
1364 hexdump(char *p, int len, int screenwidth)
1365 {
1366 	int n, i;
1367 	int width;
1368 
1369 	width = 0;
1370 	do {
1371 		width += 2;
1372 		i = 13;			/* base offset */
1373 		i += (width / 2) + 1;	/* spaces every second byte */
1374 		i += (width * 2);	/* width of bytes */
1375 		i += 3;			/* "  |" */
1376 		i += width;		/* each byte */
1377 		i += 1;			/* "|" */
1378 	} while (i < screenwidth);
1379 	width -= 2;
1380 
1381 	for (n = 0; n < len; n += width) {
1382 		for (i = n; i < n + width; i++) {
1383 			if ((i % width) == 0) {	/* beginning of line */
1384 				printf("       0x%04x", i);
1385 			}
1386 			if ((i % 2) == 0) {
1387 				printf(" ");
1388 			}
1389 			if (i < len)
1390 				printf("%02x", p[i] & 0xff);
1391 			else
1392 				printf("  ");
1393 		}
1394 		printf("  |");
1395 		for (i = n; i < n + width; i++) {
1396 			if (i >= len)
1397 				break;
1398 			if (p[i] >= ' ' && p[i] <= '~')
1399 				printf("%c", p[i]);
1400 			else
1401 				printf(".");
1402 		}
1403 		printf("|\n");
1404 	}
1405 	if ((i % width) != 0)
1406 		printf("\n");
1407 }
1408 
1409 void
1410 visdump(char *dp, int datalen, int screenwidth)
1411 {
1412 	int col = 0;
1413 	char *cp;
1414 	int width;
1415 	char visbuf[5];
1416 
1417 	printf("       \"");
1418 	col = 8;
1419 	for (;datalen > 0; datalen--, dp++) {
1420 		 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1421 		cp = visbuf;
1422 		/*
1423 		 * Keep track of printables and
1424 		 * space chars (like fold(1)).
1425 		 */
1426 		if (col == 0) {
1427 			putchar('\t');
1428 			col = 8;
1429 		}
1430 		switch(*cp) {
1431 		case '\n':
1432 			col = 0;
1433 			putchar('\n');
1434 			continue;
1435 		case '\t':
1436 			width = 8 - (col&07);
1437 			break;
1438 		default:
1439 			width = strlen(cp);
1440 		}
1441 		if (col + width > (screenwidth-2)) {
1442 			printf("\\\n\t");
1443 			col = 8;
1444 		}
1445 		col += width;
1446 		do {
1447 			putchar(*cp++);
1448 		} while (*cp);
1449 	}
1450 	if (col == 0)
1451 		printf("       ");
1452 	printf("\"\n");
1453 }
1454 
1455 void
1456 ktrgenio(struct ktr_genio *ktr, int len)
1457 {
1458 	int datalen = len - sizeof (struct ktr_genio);
1459 	char *dp = (char *)ktr + sizeof (struct ktr_genio);
1460 	static int screenwidth = 0;
1461 	int i, binary;
1462 
1463 	printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1464 		ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1465 		datalen == 1 ? "" : "s");
1466 	if (suppressdata)
1467 		return;
1468 	if (screenwidth == 0) {
1469 		struct winsize ws;
1470 
1471 		if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1472 		    ws.ws_col > 8)
1473 			screenwidth = ws.ws_col;
1474 		else
1475 			screenwidth = 80;
1476 	}
1477 	if (maxdata && datalen > maxdata)
1478 		datalen = maxdata;
1479 
1480 	for (i = 0, binary = 0; i < datalen && binary == 0; i++)  {
1481 		if (dp[i] >= 32 && dp[i] < 127)
1482 			continue;
1483 		if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1484 			continue;
1485 		binary = 1;
1486 	}
1487 	if (binary)
1488 		hexdump(dp, datalen, screenwidth);
1489 	else
1490 		visdump(dp, datalen, screenwidth);
1491 }
1492 
1493 const char *signames[] = {
1494 	"NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT",	/*  1 - 6  */
1495 	"EMT", "FPE", "KILL", "BUS", "SEGV", "SYS",		/*  7 - 12 */
1496 	"PIPE", "ALRM",  "TERM", "URG", "STOP", "TSTP",		/* 13 - 18 */
1497 	"CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU",		/* 19 - 24 */
1498 	"XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1",	/* 25 - 30 */
1499 	"USR2", NULL,						/* 31 - 32 */
1500 };
1501 
1502 void
1503 ktrpsig(struct ktr_psig *psig)
1504 {
1505 	if (psig->signo > 0 && psig->signo < NSIG)
1506 		printf("SIG%s ", signames[psig->signo]);
1507 	else
1508 		printf("SIG %d ", psig->signo);
1509 	if (psig->action == SIG_DFL) {
1510 		printf("SIG_DFL code=");
1511 		sigcodename(psig->signo, psig->code);
1512 		putchar('\n');
1513 	} else {
1514 		printf("caught handler=0x%lx mask=0x%x code=",
1515 		    (u_long)psig->action, psig->mask.__bits[0]);
1516 		sigcodename(psig->signo, psig->code);
1517 		putchar('\n');
1518 	}
1519 }
1520 
1521 void
1522 ktrcsw_old(struct ktr_csw_old *cs)
1523 {
1524 	printf("%s %s\n", cs->out ? "stop" : "resume",
1525 		cs->user ? "user" : "kernel");
1526 }
1527 
1528 void
1529 ktrcsw(struct ktr_csw *cs)
1530 {
1531 	printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1532 	    cs->user ? "user" : "kernel", cs->wmesg);
1533 }
1534 
1535 void
1536 ktruser(int len, void *p)
1537 {
1538 	unsigned char *cp;
1539 
1540 	if (sysdecode_utrace(stdout, p, len)) {
1541 		printf("\n");
1542 		return;
1543 	}
1544 
1545 	printf("%d ", len);
1546 	cp = p;
1547 	while (len--)
1548 		if (decimal)
1549 			printf(" %d", *cp++);
1550 		else
1551 			printf(" %02x", *cp++);
1552 	printf("\n");
1553 }
1554 
1555 void
1556 ktrcaprights(cap_rights_t *rightsp)
1557 {
1558 
1559 	printf("cap_rights_t ");
1560 	capname(rightsp);
1561 	printf("\n");
1562 }
1563 
1564 static void
1565 ktrtimeval(struct timeval *tv)
1566 {
1567 
1568 	printf("{%ld, %ld}", (long)tv->tv_sec, tv->tv_usec);
1569 }
1570 
1571 void
1572 ktritimerval(struct itimerval *it)
1573 {
1574 
1575 	printf("itimerval { .interval = ");
1576 	ktrtimeval(&it->it_interval);
1577 	printf(", .value = ");
1578 	ktrtimeval(&it->it_value);
1579 	printf(" }\n");
1580 }
1581 
1582 void
1583 ktrsockaddr(struct sockaddr *sa)
1584 {
1585 /*
1586  TODO: Support additional address families
1587 	#include <netnatm/natm.h>
1588 	struct sockaddr_natm	*natm;
1589 	#include <netsmb/netbios.h>
1590 	struct sockaddr_nb	*nb;
1591 */
1592 	char addr[64];
1593 
1594 	/*
1595 	 * note: ktrstruct() has already verified that sa points to a
1596 	 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1597 	 * sa->sa_len bytes long.
1598 	 */
1599 	printf("struct sockaddr { ");
1600 	sockfamilyname(sa->sa_family);
1601 	printf(", ");
1602 
1603 #define check_sockaddr_len(n)					\
1604 	if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) {	\
1605 		printf("invalid");				\
1606 		break;						\
1607 	}
1608 
1609 	switch(sa->sa_family) {
1610 	case AF_INET: {
1611 		struct sockaddr_in sa_in;
1612 
1613 		memset(&sa_in, 0, sizeof(sa_in));
1614 		memcpy(&sa_in, sa, sa->sa_len);
1615 		check_sockaddr_len(in);
1616 		inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1617 		printf("%s:%u", addr, ntohs(sa_in.sin_port));
1618 		break;
1619 	}
1620 	case AF_INET6: {
1621 		struct sockaddr_in6 sa_in6;
1622 
1623 		memset(&sa_in6, 0, sizeof(sa_in6));
1624 		memcpy(&sa_in6, sa, sa->sa_len);
1625 		check_sockaddr_len(in6);
1626 		getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1627 		    addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1628 		printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1629 		break;
1630 	}
1631 	case AF_UNIX: {
1632 		struct sockaddr_un sa_un;
1633 
1634 		memset(&sa_un, 0, sizeof(sa_un));
1635 		memcpy(&sa_un, sa, sa->sa_len);
1636 		printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1637 		break;
1638 	}
1639 	default:
1640 		printf("unknown address family");
1641 	}
1642 	printf(" }\n");
1643 }
1644 
1645 void
1646 ktrstat(struct stat *statp)
1647 {
1648 	char mode[12], timestr[PATH_MAX + 4];
1649 	struct passwd *pwd;
1650 	struct group  *grp;
1651 	struct tm *tm;
1652 
1653 	/*
1654 	 * note: ktrstruct() has already verified that statp points to a
1655 	 * buffer exactly sizeof(struct stat) bytes long.
1656 	 */
1657 	printf("struct stat {");
1658 	printf("dev=%ju, ino=%ju, ",
1659 		(uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1660 	if (resolv == 0)
1661 		printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1662 	else {
1663 		strmode(statp->st_mode, mode);
1664 		printf("mode=%s, ", mode);
1665 	}
1666 	printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1667 	if (resolv == 0) {
1668 		pwd = NULL;
1669 	} else {
1670 #ifdef HAVE_LIBCASPER
1671 		if (cappwd != NULL)
1672 			pwd = cap_getpwuid(cappwd, statp->st_uid);
1673 		else
1674 #endif
1675 			pwd = getpwuid(statp->st_uid);
1676 	}
1677 	if (pwd == NULL)
1678 		printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1679 	else
1680 		printf("uid=\"%s\", ", pwd->pw_name);
1681 	if (resolv == 0) {
1682 		grp = NULL;
1683 	} else {
1684 #ifdef HAVE_LIBCASPER
1685 		if (capgrp != NULL)
1686 			grp = cap_getgrgid(capgrp, statp->st_gid);
1687 		else
1688 #endif
1689 			grp = getgrgid(statp->st_gid);
1690 	}
1691 	if (grp == NULL)
1692 		printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1693 	else
1694 		printf("gid=\"%s\", ", grp->gr_name);
1695 	printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1696 	printf("atime=");
1697 	if (resolv == 0)
1698 		printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1699 	else {
1700 		tm = localtime(&statp->st_atim.tv_sec);
1701 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1702 		printf("\"%s\"", timestr);
1703 	}
1704 	if (statp->st_atim.tv_nsec != 0)
1705 		printf(".%09ld, ", statp->st_atim.tv_nsec);
1706 	else
1707 		printf(", ");
1708 	printf("mtime=");
1709 	if (resolv == 0)
1710 		printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1711 	else {
1712 		tm = localtime(&statp->st_mtim.tv_sec);
1713 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1714 		printf("\"%s\"", timestr);
1715 	}
1716 	if (statp->st_mtim.tv_nsec != 0)
1717 		printf(".%09ld, ", statp->st_mtim.tv_nsec);
1718 	else
1719 		printf(", ");
1720 	printf("ctime=");
1721 	if (resolv == 0)
1722 		printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1723 	else {
1724 		tm = localtime(&statp->st_ctim.tv_sec);
1725 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1726 		printf("\"%s\"", timestr);
1727 	}
1728 	if (statp->st_ctim.tv_nsec != 0)
1729 		printf(".%09ld, ", statp->st_ctim.tv_nsec);
1730 	else
1731 		printf(", ");
1732 	printf("birthtime=");
1733 	if (resolv == 0)
1734 		printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1735 	else {
1736 		tm = localtime(&statp->st_birthtim.tv_sec);
1737 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1738 		printf("\"%s\"", timestr);
1739 	}
1740 	if (statp->st_birthtim.tv_nsec != 0)
1741 		printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1742 	else
1743 		printf(", ");
1744 	printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1745 		(uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1746 		(intmax_t)statp->st_blocks, statp->st_flags);
1747 	printf(" }\n");
1748 }
1749 
1750 void
1751 ktrstruct(char *buf, size_t buflen)
1752 {
1753 	char *name, *data;
1754 	size_t namelen, datalen;
1755 	int i;
1756 	cap_rights_t rights;
1757 	struct itimerval it;
1758 	struct stat sb;
1759 	struct sockaddr_storage ss;
1760 
1761 	for (name = buf, namelen = 0;
1762 	     namelen < buflen && name[namelen] != '\0';
1763 	     ++namelen)
1764 		/* nothing */;
1765 	if (namelen == buflen)
1766 		goto invalid;
1767 	if (name[namelen] != '\0')
1768 		goto invalid;
1769 	data = buf + namelen + 1;
1770 	datalen = buflen - namelen - 1;
1771 	if (datalen == 0)
1772 		goto invalid;
1773 	/* sanity check */
1774 	for (i = 0; i < (int)namelen; ++i)
1775 		if (!isalpha(name[i]))
1776 			goto invalid;
1777 	if (strcmp(name, "caprights") == 0) {
1778 		if (datalen != sizeof(cap_rights_t))
1779 			goto invalid;
1780 		memcpy(&rights, data, datalen);
1781 		ktrcaprights(&rights);
1782 	} else if (strcmp(name, "itimerval") == 0) {
1783 		if (datalen != sizeof(struct itimerval))
1784 			goto invalid;
1785 		memcpy(&it, data, datalen);
1786 		ktritimerval(&it);
1787 	} else if (strcmp(name, "stat") == 0) {
1788 		if (datalen != sizeof(struct stat))
1789 			goto invalid;
1790 		memcpy(&sb, data, datalen);
1791 		ktrstat(&sb);
1792 	} else if (strcmp(name, "sockaddr") == 0) {
1793 		if (datalen > sizeof(ss))
1794 			goto invalid;
1795 		memcpy(&ss, data, datalen);
1796 		if (datalen != ss.ss_len)
1797 			goto invalid;
1798 		ktrsockaddr((struct sockaddr *)&ss);
1799 	} else {
1800 		printf("unknown structure\n");
1801 	}
1802 	return;
1803 invalid:
1804 	printf("invalid record\n");
1805 }
1806 
1807 void
1808 ktrcapfail(struct ktr_cap_fail *ktr)
1809 {
1810 	switch (ktr->cap_type) {
1811 	case CAPFAIL_NOTCAPABLE:
1812 		/* operation on fd with insufficient capabilities */
1813 		printf("operation requires ");
1814 		capname(&ktr->cap_needed);
1815 		printf(", descriptor holds ");
1816 		capname(&ktr->cap_held);
1817 		break;
1818 	case CAPFAIL_INCREASE:
1819 		/* requested more capabilities than fd already has */
1820 		printf("attempt to increase capabilities from ");
1821 		capname(&ktr->cap_held);
1822 		printf(" to ");
1823 		capname(&ktr->cap_needed);
1824 		break;
1825 	case CAPFAIL_SYSCALL:
1826 		/* called restricted syscall */
1827 		printf("disallowed system call");
1828 		break;
1829 	case CAPFAIL_LOOKUP:
1830 		/* used ".." in strict-relative mode */
1831 		printf("restricted VFS lookup");
1832 		break;
1833 	default:
1834 		printf("unknown capability failure: ");
1835 		capname(&ktr->cap_needed);
1836 		printf(" ");
1837 		capname(&ktr->cap_held);
1838 		break;
1839 	}
1840 	printf("\n");
1841 }
1842 
1843 void
1844 ktrfault(struct ktr_fault *ktr)
1845 {
1846 
1847 	printf("0x%jx ", (uintmax_t)ktr->vaddr);
1848 	vmprotname(ktr->type);
1849 	printf("\n");
1850 }
1851 
1852 void
1853 ktrfaultend(struct ktr_faultend *ktr)
1854 {
1855 
1856 	vmresultname(ktr->result);
1857 	printf("\n");
1858 }
1859 
1860 void
1861 usage(void)
1862 {
1863 	fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
1864 	    "[-m maxdata] [-p pid] [-t trstr]\n");
1865 	exit(1);
1866 }
1867