xref: /freebsd/usr.bin/kdump/kdump.c (revision 0d66206fff44f864ea8a4b220c3a53b4caa959a0)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1988, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n";
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)kdump.c	8.1 (Berkeley) 6/6/93";
41 #endif
42 #endif /* not lint */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 #define _WANT_KERNEL_ERRNO
47 #ifdef __LP64__
48 #define	_WANT_KEVENT32
49 #endif
50 #define	_WANT_FREEBSD11_KEVENT
51 #define	_WANT_FREEBSD_BITSET
52 #include <sys/param.h>
53 #include <sys/capsicum.h>
54 #include <sys/_bitset.h>
55 #include <sys/bitset.h>
56 #include <sys/errno.h>
57 #include <sys/time.h>
58 #include <sys/uio.h>
59 #include <sys/event.h>
60 #include <sys/ktrace.h>
61 #include <sys/mman.h>
62 #include <sys/ioctl.h>
63 #include <sys/socket.h>
64 #include <sys/stat.h>
65 #include <sys/sysent.h>
66 #include <sys/umtx.h>
67 #include <sys/un.h>
68 #include <sys/queue.h>
69 #include <sys/wait.h>
70 #ifdef WITH_CASPER
71 #include <sys/nv.h>
72 #endif
73 #include <arpa/inet.h>
74 #include <netinet/in.h>
75 #include <ctype.h>
76 #include <capsicum_helpers.h>
77 #include <err.h>
78 #include <grp.h>
79 #include <inttypes.h>
80 #include <locale.h>
81 #include <netdb.h>
82 #include <nl_types.h>
83 #include <pwd.h>
84 #include <stddef.h>
85 #include <stdio.h>
86 #include <stdlib.h>
87 #include <string.h>
88 #include <sysdecode.h>
89 #include <time.h>
90 #include <unistd.h>
91 #include <vis.h>
92 #include "ktrace.h"
93 #include "kdump.h"
94 
95 #ifdef WITH_CASPER
96 #include <libcasper.h>
97 
98 #include <casper/cap_grp.h>
99 #include <casper/cap_pwd.h>
100 #endif
101 
102 int fetchprocinfo(struct ktr_header *, u_int *);
103 u_int findabi(struct ktr_header *);
104 int fread_tail(void *, int, int);
105 void dumpheader(struct ktr_header *, u_int);
106 void dumptimeval(struct ktr_header_v0 *kth);
107 void dumptimespec(struct ktr_header *kth);
108 void ktrsyscall(struct ktr_syscall *, u_int);
109 void ktrsysret(struct ktr_sysret *, u_int);
110 void ktrnamei(char *, int);
111 void hexdump(char *, int, int);
112 void visdump(char *, int, int);
113 void ktrgenio(struct ktr_genio *, int);
114 void ktrpsig(struct ktr_psig *);
115 void ktrcsw(struct ktr_csw *);
116 void ktrcsw_old(struct ktr_csw_old *);
117 void ktruser(int, void *);
118 void ktrcaprights(cap_rights_t *);
119 void ktritimerval(struct itimerval *it);
120 void ktrsockaddr(struct sockaddr *);
121 void ktrstat(struct stat *);
122 void ktrstruct(char *, size_t);
123 void ktrcapfail(struct ktr_cap_fail *);
124 void ktrfault(struct ktr_fault *);
125 void ktrfaultend(struct ktr_faultend *);
126 void ktrkevent(struct kevent *);
127 void ktrstructarray(struct ktr_struct_array *, size_t);
128 void ktrbitset(char *, struct bitset *, size_t);
129 void ktrsyscall_freebsd(struct ktr_syscall *ktr, register_t **resip,
130     int *resnarg, char *resc, u_int sv_flags);
131 void usage(void);
132 
133 #define	TIMESTAMP_NONE		0x0
134 #define	TIMESTAMP_ABSOLUTE	0x1
135 #define	TIMESTAMP_ELAPSED	0x2
136 #define	TIMESTAMP_RELATIVE	0x4
137 
138 bool decimal, fancy = true, resolv;
139 static bool abiflag, suppressdata, syscallno, tail, threads, cpuflag;
140 static int timestamp, maxdata;
141 static const char *tracefile = DEF_TRACEFILE;
142 static struct ktr_header ktr_header;
143 static short version;
144 
145 #define TIME_FORMAT	"%b %e %T %Y"
146 #define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
147 
148 struct proc_info
149 {
150 	TAILQ_ENTRY(proc_info)	info;
151 	u_int			sv_flags;
152 	pid_t			pid;
153 };
154 
155 static TAILQ_HEAD(trace_procs, proc_info) trace_procs;
156 
157 #ifdef WITH_CASPER
158 static cap_channel_t *cappwd, *capgrp;
159 
160 static int
161 cappwdgrp_setup(cap_channel_t **cappwdp, cap_channel_t **capgrpp)
162 {
163 	cap_channel_t *capcas, *cappwdloc, *capgrploc;
164 	const char *cmds[1], *fields[1];
165 
166 	capcas = cap_init();
167 	if (capcas == NULL) {
168 		err(1, "unable to create casper process");
169 		exit(1);
170 	}
171 	cappwdloc = cap_service_open(capcas, "system.pwd");
172 	capgrploc = cap_service_open(capcas, "system.grp");
173 	/* Casper capability no longer needed. */
174 	cap_close(capcas);
175 	if (cappwdloc == NULL || capgrploc == NULL) {
176 		if (cappwdloc == NULL)
177 			warn("unable to open system.pwd service");
178 		if (capgrploc == NULL)
179 			warn("unable to open system.grp service");
180 		exit(1);
181 	}
182 	/* Limit system.pwd to only getpwuid() function and pw_name field. */
183 	cmds[0] = "getpwuid";
184 	if (cap_pwd_limit_cmds(cappwdloc, cmds, 1) < 0)
185 		err(1, "unable to limit system.pwd service");
186 	fields[0] = "pw_name";
187 	if (cap_pwd_limit_fields(cappwdloc, fields, 1) < 0)
188 		err(1, "unable to limit system.pwd service");
189 	/* Limit system.grp to only getgrgid() function and gr_name field. */
190 	cmds[0] = "getgrgid";
191 	if (cap_grp_limit_cmds(capgrploc, cmds, 1) < 0)
192 		err(1, "unable to limit system.grp service");
193 	fields[0] = "gr_name";
194 	if (cap_grp_limit_fields(capgrploc, fields, 1) < 0)
195 		err(1, "unable to limit system.grp service");
196 
197 	*cappwdp = cappwdloc;
198 	*capgrpp = capgrploc;
199 	return (0);
200 }
201 #endif	/* WITH_CASPER */
202 
203 void
204 print_integer_arg(const char *(*decoder)(int), int value)
205 {
206 	const char *str;
207 
208 	str = decoder(value);
209 	if (str != NULL)
210 		printf("%s", str);
211 	else {
212 		if (decimal)
213 			printf("<invalid=%d>", value);
214 		else
215 			printf("<invalid=%#x>", value);
216 	}
217 }
218 
219 /* Like print_integer_arg but unknown values are treated as valid. */
220 void
221 print_integer_arg_valid(const char *(*decoder)(int), int value)
222 {
223 	const char *str;
224 
225 	str = decoder(value);
226 	if (str != NULL)
227 		printf("%s", str);
228 	else {
229 		if (decimal)
230 			printf("%d", value);
231 		else
232 			printf("%#x", value);
233 	}
234 }
235 
236 bool
237 print_mask_arg_part(bool (*decoder)(FILE *, int, int *), int value, int *rem)
238 {
239 
240 	printf("%#x<", value);
241 	return (decoder(stdout, value, rem));
242 }
243 
244 void
245 print_mask_arg(bool (*decoder)(FILE *, int, int *), int value)
246 {
247 	bool invalid;
248 	int rem;
249 
250 	invalid = !print_mask_arg_part(decoder, value, &rem);
251 	printf(">");
252 	if (invalid)
253 		printf("<invalid>%u", rem);
254 }
255 
256 void
257 print_mask_arg0(bool (*decoder)(FILE *, int, int *), int value)
258 {
259 	bool invalid;
260 	int rem;
261 
262 	if (value == 0) {
263 		printf("0");
264 		return;
265 	}
266 	printf("%#x<", value);
267 	invalid = !decoder(stdout, value, &rem);
268 	printf(">");
269 	if (invalid)
270 		printf("<invalid>%u", rem);
271 }
272 
273 static void
274 decode_fileflags(fflags_t value)
275 {
276 	bool invalid;
277 	fflags_t rem;
278 
279 	if (value == 0) {
280 		printf("0");
281 		return;
282 	}
283 	printf("%#x<", value);
284 	invalid = !sysdecode_fileflags(stdout, value, &rem);
285 	printf(">");
286 	if (invalid)
287 		printf("<invalid>%u", rem);
288 }
289 
290 void
291 decode_filemode(int value)
292 {
293 	bool invalid;
294 	int rem;
295 
296 	if (value == 0) {
297 		printf("0");
298 		return;
299 	}
300 	printf("%#o<", value);
301 	invalid = !sysdecode_filemode(stdout, value, &rem);
302 	printf(">");
303 	if (invalid)
304 		printf("<invalid>%u", rem);
305 }
306 
307 void
308 print_mask_arg32(bool (*decoder)(FILE *, uint32_t, uint32_t *), uint32_t value)
309 {
310 	bool invalid;
311 	uint32_t rem;
312 
313 	printf("%#x<", value);
314 	invalid = !decoder(stdout, value, &rem);
315 	printf(">");
316 	if (invalid)
317 		printf("<invalid>%u", rem);
318 }
319 
320 void
321 print_mask_argul(bool (*decoder)(FILE *, u_long, u_long *), u_long value)
322 {
323 	bool invalid;
324 	u_long rem;
325 
326 	if (value == 0) {
327 		printf("0");
328 		return;
329 	}
330 	printf("%#lx<", value);
331 	invalid = !decoder(stdout, value, &rem);
332 	printf(">");
333 	if (invalid)
334 		printf("<invalid>%lu", rem);
335 }
336 
337 int
338 main(int argc, char *argv[])
339 {
340 	int ch, ktrlen, size;
341 	void *m;
342 	int trpoints = ALL_POINTS;
343 	int drop_logged;
344 	pid_t pid = 0;
345 	u_int sv_flags;
346 
347 	setlocale(LC_CTYPE, "");
348 
349 	timestamp = TIMESTAMP_NONE;
350 
351 	while ((ch = getopt(argc,argv,"f:cdElm:np:AHRrSsTt:")) != -1)
352 		switch (ch) {
353 		case 'A':
354 			abiflag = true;
355 			break;
356 		case 'f':
357 			tracefile = optarg;
358 			break;
359 		case 'c':
360 			cpuflag = true;
361 			break;
362 		case 'd':
363 			decimal = true;
364 			break;
365 		case 'l':
366 			tail = true;
367 			break;
368 		case 'm':
369 			maxdata = atoi(optarg);
370 			break;
371 		case 'n':
372 			fancy = false;
373 			break;
374 		case 'p':
375 			pid = atoi(optarg);
376 			break;
377 		case 'r':
378 			resolv = true;
379 			break;
380 		case 'S':
381 			syscallno = true;
382 			break;
383 		case 's':
384 			suppressdata = true;
385 			break;
386 		case 'E':
387 			timestamp |= TIMESTAMP_ELAPSED;
388 			break;
389 		case 'H':
390 			threads = true;
391 			break;
392 		case 'R':
393 			timestamp |= TIMESTAMP_RELATIVE;
394 			break;
395 		case 'T':
396 			timestamp |= TIMESTAMP_ABSOLUTE;
397 			break;
398 		case 't':
399 			trpoints = getpoints(optarg);
400 			if (trpoints < 0)
401 				errx(1, "unknown trace point in %s", optarg);
402 			break;
403 		default:
404 			usage();
405 		}
406 
407 	if (argc > optind)
408 		usage();
409 
410 	m = malloc(size = 1025);
411 	if (m == NULL)
412 		errx(1, "%s", strerror(ENOMEM));
413 	if (strcmp(tracefile, "-") != 0)
414 		if (!freopen(tracefile, "r", stdin))
415 			err(1, "%s", tracefile);
416 
417 	caph_cache_catpages();
418 	caph_cache_tzdata();
419 
420 #ifdef WITH_CASPER
421 	if (resolv) {
422 		if (cappwdgrp_setup(&cappwd, &capgrp) < 0) {
423 			cappwd = NULL;
424 			capgrp = NULL;
425 		}
426 	}
427 	if (!resolv || (cappwd != NULL && capgrp != NULL)) {
428 		if (caph_enter() < 0)
429 			err(1, "unable to enter capability mode");
430 	}
431 #else
432 	if (!resolv) {
433 		if (caph_enter() < 0)
434 			err(1, "unable to enter capability mode");
435 	}
436 #endif
437 	if (caph_limit_stdio() == -1)
438 		err(1, "unable to limit stdio");
439 
440 	TAILQ_INIT(&trace_procs);
441 	drop_logged = 0;
442 	while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
443 		if (ktr_header.ktr_type & KTR_VERSIONED) {
444 			ktr_header.ktr_type &= ~KTR_VERSIONED;
445 			version = ktr_header.ktr_version;
446 		} else
447 			version = KTR_VERSION0;
448 		if (ktr_header.ktr_type & KTR_DROP) {
449 			ktr_header.ktr_type &= ~KTR_DROP;
450 			if (!drop_logged && threads) {
451 				printf(
452 				    "%6d %6d %-8.*s Events dropped.\n",
453 				    ktr_header.ktr_pid,
454 				    ktr_header.ktr_tid > 0 ?
455 				    (lwpid_t)ktr_header.ktr_tid : 0,
456 				    MAXCOMLEN, ktr_header.ktr_comm);
457 				drop_logged = 1;
458 			} else if (!drop_logged) {
459 				printf("%6d %-8.*s Events dropped.\n",
460 				    ktr_header.ktr_pid, MAXCOMLEN,
461 				    ktr_header.ktr_comm);
462 				drop_logged = 1;
463 			}
464 		}
465 		if ((ktrlen = ktr_header.ktr_len) < 0)
466 			errx(1, "bogus length 0x%x", ktrlen);
467 		if (ktrlen > size) {
468 			m = realloc(m, ktrlen+1);
469 			if (m == NULL)
470 				errx(1, "%s", strerror(ENOMEM));
471 			size = ktrlen;
472 		}
473 		if (version == KTR_VERSION0 &&
474 		    fseek(stdin, KTR_OFFSET_V0, SEEK_CUR) < 0)
475 			errx(1, "%s", strerror(errno));
476 		if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
477 			errx(1, "data too short");
478 		if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
479 			continue;
480 		if (pid && ktr_header.ktr_pid != pid &&
481 		    ktr_header.ktr_tid != pid)
482 			continue;
483 		if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
484 			continue;
485 		sv_flags = findabi(&ktr_header);
486 		dumpheader(&ktr_header, sv_flags);
487 		drop_logged = 0;
488 		switch (ktr_header.ktr_type) {
489 		case KTR_SYSCALL:
490 			ktrsyscall((struct ktr_syscall *)m, sv_flags);
491 			break;
492 		case KTR_SYSRET:
493 			ktrsysret((struct ktr_sysret *)m, sv_flags);
494 			break;
495 		case KTR_NAMEI:
496 		case KTR_SYSCTL:
497 			ktrnamei(m, ktrlen);
498 			break;
499 		case KTR_GENIO:
500 			ktrgenio((struct ktr_genio *)m, ktrlen);
501 			break;
502 		case KTR_PSIG:
503 			ktrpsig((struct ktr_psig *)m);
504 			break;
505 		case KTR_CSW:
506 			if (ktrlen == sizeof(struct ktr_csw_old))
507 				ktrcsw_old((struct ktr_csw_old *)m);
508 			else
509 				ktrcsw((struct ktr_csw *)m);
510 			break;
511 		case KTR_USER:
512 			ktruser(ktrlen, m);
513 			break;
514 		case KTR_STRUCT:
515 			ktrstruct(m, ktrlen);
516 			break;
517 		case KTR_CAPFAIL:
518 			ktrcapfail((struct ktr_cap_fail *)m);
519 			break;
520 		case KTR_FAULT:
521 			ktrfault((struct ktr_fault *)m);
522 			break;
523 		case KTR_FAULTEND:
524 			ktrfaultend((struct ktr_faultend *)m);
525 			break;
526 		case KTR_STRUCT_ARRAY:
527 			ktrstructarray((struct ktr_struct_array *)m, ktrlen);
528 			break;
529 		default:
530 			printf("\n");
531 			break;
532 		}
533 		if (tail)
534 			fflush(stdout);
535 	}
536 	return 0;
537 }
538 
539 int
540 fread_tail(void *buf, int size, int num)
541 {
542 	int i;
543 
544 	while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
545 		sleep(1);
546 		clearerr(stdin);
547 	}
548 	return (i);
549 }
550 
551 int
552 fetchprocinfo(struct ktr_header *kth, u_int *flags)
553 {
554 	struct proc_info *pi;
555 
556 	switch (kth->ktr_type) {
557 	case KTR_PROCCTOR:
558 		TAILQ_FOREACH(pi, &trace_procs, info) {
559 			if (pi->pid == kth->ktr_pid) {
560 				TAILQ_REMOVE(&trace_procs, pi, info);
561 				break;
562 			}
563 		}
564 		pi = malloc(sizeof(struct proc_info));
565 		if (pi == NULL)
566 			errx(1, "%s", strerror(ENOMEM));
567 		pi->sv_flags = *flags;
568 		pi->pid = kth->ktr_pid;
569 		TAILQ_INSERT_TAIL(&trace_procs, pi, info);
570 		return (1);
571 
572 	case KTR_PROCDTOR:
573 		TAILQ_FOREACH(pi, &trace_procs, info) {
574 			if (pi->pid == kth->ktr_pid) {
575 				TAILQ_REMOVE(&trace_procs, pi, info);
576 				free(pi);
577 				break;
578 			}
579 		}
580 		return (1);
581 	}
582 
583 	return (0);
584 }
585 
586 u_int
587 findabi(struct ktr_header *kth)
588 {
589 	struct proc_info *pi;
590 
591 	TAILQ_FOREACH(pi, &trace_procs, info) {
592 		if (pi->pid == kth->ktr_pid) {
593 			return (pi->sv_flags);
594 		}
595 	}
596 	return (0);
597 }
598 
599 void
600 dumptimeval(struct ktr_header_v0 *kth)
601 {
602 	static struct timeval prevtime, prevtime_e;
603 	struct timeval temp;
604 	const char *sign;
605 
606 	if (timestamp & TIMESTAMP_ABSOLUTE) {
607 		printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
608 		    kth->ktr_time.tv_usec);
609 	}
610 	if (timestamp & TIMESTAMP_ELAPSED) {
611 		if (prevtime_e.tv_sec == 0)
612 			prevtime_e = kth->ktr_time;
613 		timersub(&kth->ktr_time, &prevtime_e, &temp);
614 		printf("%jd.%06ld ", (intmax_t)temp.tv_sec,
615 		    temp.tv_usec);
616 	}
617 	if (timestamp & TIMESTAMP_RELATIVE) {
618 		if (prevtime.tv_sec == 0)
619 			prevtime = kth->ktr_time;
620 		if (timercmp(&kth->ktr_time, &prevtime, <)) {
621 			timersub(&prevtime, &kth->ktr_time, &temp);
622 			sign = "-";
623 		} else {
624 			timersub(&kth->ktr_time, &prevtime, &temp);
625 			sign = "";
626 		}
627 		prevtime = kth->ktr_time;
628 		printf("%s%jd.%06ld ", sign, (intmax_t)temp.tv_sec,
629 		    temp.tv_usec);
630 	}
631 }
632 
633 void
634 dumptimespec(struct ktr_header *kth)
635 {
636 	static struct timespec prevtime, prevtime_e;
637 	struct timespec temp;
638 	const char *sign;
639 
640 	if (timestamp & TIMESTAMP_ABSOLUTE) {
641 		printf("%jd.%09ld ", (intmax_t)kth->ktr_time.tv_sec,
642 		    kth->ktr_time.tv_nsec);
643 	}
644 	if (timestamp & TIMESTAMP_ELAPSED) {
645 		if (prevtime_e.tv_sec == 0)
646 			prevtime_e = kth->ktr_time;
647 		timespecsub(&kth->ktr_time, &prevtime_e, &temp);
648 		printf("%jd.%09ld ", (intmax_t)temp.tv_sec,
649 		    temp.tv_nsec);
650 	}
651 	if (timestamp & TIMESTAMP_RELATIVE) {
652 		if (prevtime.tv_sec == 0)
653 			prevtime = kth->ktr_time;
654 		if (timespeccmp(&kth->ktr_time, &prevtime, <)) {
655 			timespecsub(&prevtime, &kth->ktr_time, &temp);
656 			sign = "-";
657 		} else {
658 			timespecsub(&kth->ktr_time, &prevtime, &temp);
659 			sign = "";
660 		}
661 		prevtime = kth->ktr_time;
662 		printf("%s%jd.%09ld ", sign, (intmax_t)temp.tv_sec,
663 		    temp.tv_nsec);
664 	}
665 }
666 
667 void
668 dumpheader(struct ktr_header *kth, u_int sv_flags)
669 {
670 	static char unknown[64];
671 	const char *abi;
672 	const char *arch;
673 	const char *type;
674 
675 	switch (kth->ktr_type) {
676 	case KTR_SYSCALL:
677 		type = "CALL";
678 		break;
679 	case KTR_SYSRET:
680 		type = "RET ";
681 		break;
682 	case KTR_NAMEI:
683 		type = "NAMI";
684 		break;
685 	case KTR_GENIO:
686 		type = "GIO ";
687 		break;
688 	case KTR_PSIG:
689 		type = "PSIG";
690 		break;
691 	case KTR_CSW:
692 		type = "CSW ";
693 		break;
694 	case KTR_USER:
695 		type = "USER";
696 		break;
697 	case KTR_STRUCT:
698 	case KTR_STRUCT_ARRAY:
699 		type = "STRU";
700 		break;
701 	case KTR_SYSCTL:
702 		type = "SCTL";
703 		break;
704 	case KTR_CAPFAIL:
705 		type = "CAP ";
706 		break;
707 	case KTR_FAULT:
708 		type = "PFLT";
709 		break;
710 	case KTR_FAULTEND:
711 		type = "PRET";
712 		break;
713 	default:
714 		sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
715 		type = unknown;
716 	}
717 
718 	/*
719 	 * The ktr_tid field was previously the ktr_buffer field, which held
720 	 * the kernel pointer value for the buffer associated with data
721 	 * following the record header.  It now holds a threadid, but only
722 	 * for trace files after the change.  Older trace files still contain
723 	 * kernel pointers.  Detect this and suppress the results by printing
724 	 * negative tid's as 0.
725 	 */
726 	if (threads)
727 		printf("%6d %6d %-8.*s ", kth->ktr_pid,
728 		    kth->ktr_tid > 0 ? (lwpid_t)kth->ktr_tid : 0,
729 		    MAXCOMLEN, kth->ktr_comm);
730 	else
731 		printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
732         if (timestamp) {
733 		if (version == KTR_VERSION0)
734 			dumptimeval((struct ktr_header_v0 *)kth);
735 		else
736 			dumptimespec(kth);
737 	}
738 	if (cpuflag && version > KTR_VERSION0)
739 		printf("%3d ", kth->ktr_cpu);
740 	printf("%s  ", type);
741 	if (abiflag != 0) {
742 		switch (sv_flags & SV_ABI_MASK) {
743 		case SV_ABI_LINUX:
744 			abi = "L";
745 			break;
746 		case SV_ABI_FREEBSD:
747 			abi = "F";
748 			break;
749 		default:
750 			abi = "U";
751 			break;
752 		}
753 
754 		if ((sv_flags & SV_LP64) != 0)
755 			arch = "64";
756 		else if ((sv_flags & SV_ILP32) != 0)
757 			arch = "32";
758 		else
759 			arch = "00";
760 
761 		printf("%s%s  ", abi, arch);
762 	}
763 }
764 
765 #include <sys/syscall.h>
766 
767 static void
768 ioctlname(unsigned long val)
769 {
770 	const char *str;
771 
772 	str = sysdecode_ioctlname(val);
773 	if (str != NULL)
774 		printf("%s", str);
775 	else if (decimal)
776 		printf("%lu", val);
777 	else
778 		printf("%#lx", val);
779 }
780 
781 static enum sysdecode_abi
782 syscallabi(u_int sv_flags)
783 {
784 
785 	if (sv_flags == 0)
786 		return (SYSDECODE_ABI_FREEBSD);
787 	switch (sv_flags & SV_ABI_MASK) {
788 	case SV_ABI_FREEBSD:
789 		return (SYSDECODE_ABI_FREEBSD);
790 	case SV_ABI_LINUX:
791 #ifdef __LP64__
792 		if (sv_flags & SV_ILP32)
793 			return (SYSDECODE_ABI_LINUX32);
794 #endif
795 		return (SYSDECODE_ABI_LINUX);
796 	default:
797 		return (SYSDECODE_ABI_UNKNOWN);
798 	}
799 }
800 
801 static void
802 syscallname(u_int code, u_int sv_flags)
803 {
804 	const char *name;
805 
806 	name = sysdecode_syscallname(syscallabi(sv_flags), code);
807 	if (name == NULL)
808 		printf("[%d]", code);
809 	else {
810 		printf("%s", name);
811 		if (syscallno)
812 			printf("[%d]", code);
813 	}
814 }
815 
816 static void
817 print_signal(int signo)
818 {
819 	const char *signame;
820 
821 	signame = sysdecode_signal(signo);
822 	if (signame != NULL)
823 		printf("%s", signame);
824 	else
825 		printf("SIG %d", signo);
826 }
827 
828 void
829 ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags)
830 {
831 	int narg = ktr->ktr_narg;
832 	register_t *ip;
833 
834 	syscallname(ktr->ktr_code, sv_flags);
835 	ip = &ktr->ktr_args[0];
836 	if (narg) {
837 		char c = '(';
838 		if (fancy) {
839 			switch (sv_flags & SV_ABI_MASK) {
840 			case SV_ABI_FREEBSD:
841 				ktrsyscall_freebsd(ktr, &ip, &narg, &c,
842 				    sv_flags);
843 				break;
844 #ifdef SYSDECODE_HAVE_LINUX
845 			case SV_ABI_LINUX:
846 #ifdef __amd64__
847 				if (sv_flags & SV_ILP32)
848 					ktrsyscall_linux32(ktr, &ip,
849 					    &narg, &c);
850 				else
851 #endif
852 				ktrsyscall_linux(ktr, &ip, &narg, &c);
853 				break;
854 #endif /* SYSDECODE_HAVE_LINUX */
855 			}
856 		}
857 		while (narg > 0)
858 			print_number(ip, narg, c);
859 		putchar(')');
860 	}
861 	putchar('\n');
862 }
863 
864 void
865 ktrsyscall_freebsd(struct ktr_syscall *ktr, register_t **resip,
866     int *resnarg, char *resc, u_int sv_flags)
867 {
868 	int narg = ktr->ktr_narg;
869 	register_t *ip, *first;
870 	intmax_t arg;
871 	int quad_align, quad_slots;
872 
873 	ip = first = &ktr->ktr_args[0];
874 	char c = *resc;
875 
876 			quad_align = 0;
877 			if (sv_flags & SV_ILP32) {
878 #ifdef __powerpc__
879 				quad_align = 1;
880 #endif
881 				quad_slots = 2;
882 			} else
883 				quad_slots = 1;
884 			switch (ktr->ktr_code) {
885 			case SYS_bindat:
886 			case SYS_chflagsat:
887 			case SYS_connectat:
888 			case SYS_faccessat:
889 			case SYS_fchmodat:
890 			case SYS_fchownat:
891 			case SYS_fstatat:
892 			case SYS_futimesat:
893 			case SYS_linkat:
894 			case SYS_mkdirat:
895 			case SYS_mkfifoat:
896 			case SYS_mknodat:
897 			case SYS_openat:
898 			case SYS_readlinkat:
899 			case SYS_renameat:
900 			case SYS_unlinkat:
901 			case SYS_utimensat:
902 				putchar('(');
903 				print_integer_arg_valid(sysdecode_atfd, *ip);
904 				c = ',';
905 				ip++;
906 				narg--;
907 				break;
908 			}
909 			switch (ktr->ktr_code) {
910 			case SYS_ioctl: {
911 				print_number(ip, narg, c);
912 				putchar(c);
913 				ioctlname(*ip);
914 				c = ',';
915 				ip++;
916 				narg--;
917 				break;
918 			}
919 			case SYS_ptrace:
920 				putchar('(');
921 				print_integer_arg(sysdecode_ptrace_request, *ip);
922 				c = ',';
923 				ip++;
924 				narg--;
925 				break;
926 			case SYS_access:
927 			case SYS_eaccess:
928 			case SYS_faccessat:
929 				print_number(ip, narg, c);
930 				putchar(',');
931 				print_mask_arg(sysdecode_access_mode, *ip);
932 				ip++;
933 				narg--;
934 				break;
935 			case SYS_close_range:
936 				print_number(ip, narg, c);
937 				print_number(ip, narg, c);
938 				putchar(',');
939 				print_mask_arg(sysdecode_close_range_flags, *ip);
940 				ip += 3;
941 				narg -= 3;
942 				break;
943 			case SYS_open:
944 			case SYS_openat:
945 				print_number(ip, narg, c);
946 				putchar(',');
947 				print_mask_arg(sysdecode_open_flags, ip[0]);
948 				if ((ip[0] & O_CREAT) == O_CREAT) {
949 					putchar(',');
950 					decode_filemode(ip[1]);
951 				}
952 				ip += 2;
953 				narg -= 2;
954 				break;
955 			case SYS_wait4:
956 				print_number(ip, narg, c);
957 				print_number(ip, narg, c);
958 				putchar(',');
959 				print_mask_arg0(sysdecode_wait4_options, *ip);
960 				ip++;
961 				narg--;
962 				break;
963 			case SYS_wait6:
964 				putchar('(');
965 				print_integer_arg(sysdecode_idtype, *ip);
966 				c = ',';
967 				ip++;
968 				narg--;
969 				print_number64(first, ip, narg, c);
970 				print_number(ip, narg, c);
971 				putchar(',');
972 				print_mask_arg(sysdecode_wait6_options, *ip);
973 				ip++;
974 				narg--;
975 				break;
976 			case SYS_chmod:
977 			case SYS_fchmod:
978 			case SYS_lchmod:
979 			case SYS_fchmodat:
980 				print_number(ip, narg, c);
981 				putchar(',');
982 				decode_filemode(*ip);
983 				ip++;
984 				narg--;
985 				break;
986 			case SYS_mknodat:
987 				print_number(ip, narg, c);
988 				putchar(',');
989 				decode_filemode(*ip);
990 				ip++;
991 				narg--;
992 				break;
993 			case SYS_getfsstat:
994 				print_number(ip, narg, c);
995 				print_number(ip, narg, c);
996 				putchar(',');
997 				print_integer_arg(sysdecode_getfsstat_mode, *ip);
998 				ip++;
999 				narg--;
1000 				break;
1001 			case SYS_mount:
1002 				print_number(ip, narg, c);
1003 				print_number(ip, narg, c);
1004 				putchar(',');
1005 				print_mask_arg(sysdecode_mount_flags, *ip);
1006 				ip++;
1007 				narg--;
1008 				break;
1009 			case SYS_unmount:
1010 				print_number(ip, narg, c);
1011 				putchar(',');
1012 				print_mask_arg(sysdecode_mount_flags, *ip);
1013 				ip++;
1014 				narg--;
1015 				break;
1016 			case SYS_recvmsg:
1017 			case SYS_sendmsg:
1018 				print_number(ip, narg, c);
1019 				print_number(ip, narg, c);
1020 				putchar(',');
1021 				print_mask_arg0(sysdecode_msg_flags, *ip);
1022 				ip++;
1023 				narg--;
1024 				break;
1025 			case SYS_recvfrom:
1026 			case SYS_sendto:
1027 				print_number(ip, narg, c);
1028 				print_number(ip, narg, c);
1029 				print_number(ip, narg, c);
1030 				putchar(',');
1031 				print_mask_arg0(sysdecode_msg_flags, *ip);
1032 				ip++;
1033 				narg--;
1034 				break;
1035 			case SYS_chflags:
1036 			case SYS_chflagsat:
1037 			case SYS_fchflags:
1038 			case SYS_lchflags:
1039 				print_number(ip, narg, c);
1040 				putchar(',');
1041 				decode_fileflags(*ip);
1042 				ip++;
1043 				narg--;
1044 				break;
1045 			case SYS_kill:
1046 				print_number(ip, narg, c);
1047 				putchar(',');
1048 				print_signal(*ip);
1049 				ip++;
1050 				narg--;
1051 				break;
1052 			case SYS_reboot:
1053 				putchar('(');
1054 				print_mask_arg(sysdecode_reboot_howto, *ip);
1055 				ip++;
1056 				narg--;
1057 				break;
1058 			case SYS_umask:
1059 				putchar('(');
1060 				decode_filemode(*ip);
1061 				ip++;
1062 				narg--;
1063 				break;
1064 			case SYS_msync:
1065 				print_number(ip, narg, c);
1066 				print_number(ip, narg, c);
1067 				putchar(',');
1068 				print_mask_arg(sysdecode_msync_flags, *ip);
1069 				ip++;
1070 				narg--;
1071 				break;
1072 #ifdef SYS_freebsd6_mmap
1073 			case SYS_freebsd6_mmap:
1074 				print_number(ip, narg, c);
1075 				print_number(ip, narg, c);
1076 				putchar(',');
1077 				print_mask_arg(sysdecode_mmap_prot, *ip);
1078 				putchar(',');
1079 				ip++;
1080 				narg--;
1081 				print_mask_arg(sysdecode_mmap_flags, *ip);
1082 				ip++;
1083 				narg--;
1084 				break;
1085 #endif
1086 			case SYS_mmap:
1087 				print_number(ip, narg, c);
1088 				print_number(ip, narg, c);
1089 				putchar(',');
1090 				print_mask_arg(sysdecode_mmap_prot, *ip);
1091 				putchar(',');
1092 				ip++;
1093 				narg--;
1094 				print_mask_arg(sysdecode_mmap_flags, *ip);
1095 				ip++;
1096 				narg--;
1097 				break;
1098 			case SYS_mprotect:
1099 				print_number(ip, narg, c);
1100 				print_number(ip, narg, c);
1101 				putchar(',');
1102 				print_mask_arg(sysdecode_mmap_prot, *ip);
1103 				ip++;
1104 				narg--;
1105 				break;
1106 			case SYS_madvise:
1107 				print_number(ip, narg, c);
1108 				print_number(ip, narg, c);
1109 				putchar(',');
1110 				print_integer_arg(sysdecode_madvice, *ip);
1111 				ip++;
1112 				narg--;
1113 				break;
1114 			case SYS_pathconf:
1115 			case SYS_lpathconf:
1116 			case SYS_fpathconf:
1117 				print_number(ip, narg, c);
1118 				putchar(',');
1119 				print_integer_arg(sysdecode_pathconf_name, *ip);
1120 				ip++;
1121 				narg--;
1122 				break;
1123 			case SYS_getpriority:
1124 			case SYS_setpriority:
1125 				putchar('(');
1126 				print_integer_arg(sysdecode_prio_which, *ip);
1127 				c = ',';
1128 				ip++;
1129 				narg--;
1130 				break;
1131 			case SYS_fcntl:
1132 				print_number(ip, narg, c);
1133 				putchar(',');
1134 				print_integer_arg(sysdecode_fcntl_cmd, ip[0]);
1135 				if (sysdecode_fcntl_arg_p(ip[0])) {
1136 					putchar(',');
1137 					if (ip[0] == F_SETFL)
1138 						print_mask_arg(
1139 						    sysdecode_fcntl_fileflags,
1140 							ip[1]);
1141 					else
1142 						sysdecode_fcntl_arg(stdout,
1143 						    ip[0], ip[1],
1144 						    decimal ? 10 : 16);
1145 				}
1146 				ip += 2;
1147 				narg -= 2;
1148 				break;
1149 			case SYS_socket: {
1150 				int sockdomain;
1151 				putchar('(');
1152 				sockdomain = *ip;
1153 				print_integer_arg(sysdecode_socketdomain,
1154 				    sockdomain);
1155 				ip++;
1156 				narg--;
1157 				putchar(',');
1158 				print_mask_arg(sysdecode_socket_type, *ip);
1159 				ip++;
1160 				narg--;
1161 				if (sockdomain == PF_INET ||
1162 				    sockdomain == PF_INET6) {
1163 					putchar(',');
1164 					print_integer_arg(sysdecode_ipproto,
1165 					    *ip);
1166 					ip++;
1167 					narg--;
1168 				}
1169 				c = ',';
1170 				break;
1171 			}
1172 			case SYS_setsockopt:
1173 			case SYS_getsockopt: {
1174 				const char *str;
1175 
1176 				print_number(ip, narg, c);
1177 				putchar(',');
1178 				print_integer_arg_valid(sysdecode_sockopt_level,
1179 				    *ip);
1180 				str = sysdecode_sockopt_name(ip[0], ip[1]);
1181 				if (str != NULL) {
1182 					printf(",%s", str);
1183 					ip++;
1184 					narg--;
1185 				}
1186 				ip++;
1187 				narg--;
1188 				break;
1189 			}
1190 #ifdef SYS_freebsd6_lseek
1191 			case SYS_freebsd6_lseek:
1192 				print_number(ip, narg, c);
1193 				/* Hidden 'pad' argument, not in lseek(2) */
1194 				print_number(ip, narg, c);
1195 				print_number64(first, ip, narg, c);
1196 				putchar(',');
1197 				print_integer_arg(sysdecode_whence, *ip);
1198 				ip++;
1199 				narg--;
1200 				break;
1201 #endif
1202 			case SYS_lseek:
1203 				print_number(ip, narg, c);
1204 				print_number64(first, ip, narg, c);
1205 				putchar(',');
1206 				print_integer_arg(sysdecode_whence, *ip);
1207 				ip++;
1208 				narg--;
1209 				break;
1210 			case SYS_flock:
1211 				print_number(ip, narg, c);
1212 				putchar(',');
1213 				print_mask_arg(sysdecode_flock_operation, *ip);
1214 				ip++;
1215 				narg--;
1216 				break;
1217 			case SYS_mkfifo:
1218 			case SYS_mkfifoat:
1219 			case SYS_mkdir:
1220 			case SYS_mkdirat:
1221 				print_number(ip, narg, c);
1222 				putchar(',');
1223 				decode_filemode(*ip);
1224 				ip++;
1225 				narg--;
1226 				break;
1227 			case SYS_shutdown:
1228 				print_number(ip, narg, c);
1229 				putchar(',');
1230 				print_integer_arg(sysdecode_shutdown_how, *ip);
1231 				ip++;
1232 				narg--;
1233 				break;
1234 			case SYS_socketpair:
1235 				putchar('(');
1236 				print_integer_arg(sysdecode_socketdomain, *ip);
1237 				ip++;
1238 				narg--;
1239 				putchar(',');
1240 				print_mask_arg(sysdecode_socket_type, *ip);
1241 				ip++;
1242 				narg--;
1243 				c = ',';
1244 				break;
1245 			case SYS_getrlimit:
1246 			case SYS_setrlimit:
1247 				putchar('(');
1248 				print_integer_arg(sysdecode_rlimit, *ip);
1249 				ip++;
1250 				narg--;
1251 				c = ',';
1252 				break;
1253 			case SYS_getrusage:
1254 				putchar('(');
1255 				print_integer_arg(sysdecode_getrusage_who, *ip);
1256 				ip++;
1257 				narg--;
1258 				c = ',';
1259 				break;
1260 			case SYS_quotactl:
1261 				print_number(ip, narg, c);
1262 				putchar(',');
1263 				if (!sysdecode_quotactl_cmd(stdout, *ip)) {
1264 					if (decimal)
1265 						printf("<invalid=%d>", (int)*ip);
1266 					else
1267 						printf("<invalid=%#x>",
1268 						    (int)*ip);
1269 				}
1270 				ip++;
1271 				narg--;
1272 				c = ',';
1273 				break;
1274 			case SYS_nfssvc:
1275 				putchar('(');
1276 				print_integer_arg(sysdecode_nfssvc_flags, *ip);
1277 				ip++;
1278 				narg--;
1279 				c = ',';
1280 				break;
1281 			case SYS_rtprio:
1282 			case SYS_rtprio_thread:
1283 				putchar('(');
1284 				print_integer_arg(sysdecode_rtprio_function,
1285 				    *ip);
1286 				ip++;
1287 				narg--;
1288 				c = ',';
1289 				break;
1290 			case SYS___semctl:
1291 				print_number(ip, narg, c);
1292 				print_number(ip, narg, c);
1293 				putchar(',');
1294 				print_integer_arg(sysdecode_semctl_cmd, *ip);
1295 				ip++;
1296 				narg--;
1297 				break;
1298 			case SYS_semget:
1299 				print_number(ip, narg, c);
1300 				print_number(ip, narg, c);
1301 				putchar(',');
1302 				print_mask_arg(sysdecode_semget_flags, *ip);
1303 				ip++;
1304 				narg--;
1305 				break;
1306 			case SYS_msgctl:
1307 				print_number(ip, narg, c);
1308 				putchar(',');
1309 				print_integer_arg(sysdecode_msgctl_cmd, *ip);
1310 				ip++;
1311 				narg--;
1312 				break;
1313 			case SYS_shmat:
1314 				print_number(ip, narg, c);
1315 				print_number(ip, narg, c);
1316 				putchar(',');
1317 				print_mask_arg(sysdecode_shmat_flags, *ip);
1318 				ip++;
1319 				narg--;
1320 				break;
1321 			case SYS_shmctl:
1322 				print_number(ip, narg, c);
1323 				putchar(',');
1324 				print_integer_arg(sysdecode_shmctl_cmd, *ip);
1325 				ip++;
1326 				narg--;
1327 				break;
1328 #ifdef SYS_freebsd12_shm_open
1329 			case SYS_freebsd12_shm_open:
1330 				if (ip[0] == (uintptr_t)SHM_ANON) {
1331 					printf("(SHM_ANON");
1332 					ip++;
1333 				} else {
1334 					print_number(ip, narg, c);
1335 				}
1336 				putchar(',');
1337 				print_mask_arg(sysdecode_open_flags, ip[0]);
1338 				putchar(',');
1339 				decode_filemode(ip[1]);
1340 				ip += 2;
1341 				narg -= 2;
1342 				break;
1343 #endif
1344 			case SYS_shm_open2:
1345 				if (ip[0] == (uintptr_t)SHM_ANON) {
1346 					printf("(SHM_ANON");
1347 					ip++;
1348 				} else {
1349 					print_number(ip, narg, c);
1350 				}
1351 				putchar(',');
1352 				print_mask_arg(sysdecode_open_flags, ip[0]);
1353 				putchar(',');
1354 				decode_filemode(ip[1]);
1355 				putchar(',');
1356 				print_mask_arg(sysdecode_shmflags, ip[2]);
1357 				ip += 3;
1358 				narg -= 3;
1359 				break;
1360 			case SYS_minherit:
1361 				print_number(ip, narg, c);
1362 				print_number(ip, narg, c);
1363 				putchar(',');
1364 				print_integer_arg(sysdecode_minherit_inherit,
1365 				    *ip);
1366 				ip++;
1367 				narg--;
1368 				break;
1369 			case SYS_rfork:
1370 				putchar('(');
1371 				print_mask_arg(sysdecode_rfork_flags, *ip);
1372 				ip++;
1373 				narg--;
1374 				c = ',';
1375 				break;
1376 			case SYS_lio_listio:
1377 				putchar('(');
1378 				print_integer_arg(sysdecode_lio_listio_mode,
1379 				    *ip);
1380 				ip++;
1381 				narg--;
1382 				c = ',';
1383 				break;
1384 			case SYS_mlockall:
1385 				putchar('(');
1386 				print_mask_arg(sysdecode_mlockall_flags, *ip);
1387 				ip++;
1388 				narg--;
1389 				break;
1390 			case SYS_sched_setscheduler:
1391 				print_number(ip, narg, c);
1392 				putchar(',');
1393 				print_integer_arg(sysdecode_scheduler_policy,
1394 				    *ip);
1395 				ip++;
1396 				narg--;
1397 				break;
1398 			case SYS_sched_get_priority_max:
1399 			case SYS_sched_get_priority_min:
1400 				putchar('(');
1401 				print_integer_arg(sysdecode_scheduler_policy,
1402 				    *ip);
1403 				ip++;
1404 				narg--;
1405 				break;
1406 			case SYS_sendfile:
1407 				print_number(ip, narg, c);
1408 				print_number(ip, narg, c);
1409 				print_number(ip, narg, c);
1410 				print_number(ip, narg, c);
1411 				print_number(ip, narg, c);
1412 				print_number(ip, narg, c);
1413 				putchar(',');
1414 				print_mask_arg(sysdecode_sendfile_flags, *ip);
1415 				ip++;
1416 				narg--;
1417 				break;
1418 			case SYS_kldsym:
1419 				print_number(ip, narg, c);
1420 				putchar(',');
1421 				print_integer_arg(sysdecode_kldsym_cmd, *ip);
1422 				ip++;
1423 				narg--;
1424 				break;
1425 			case SYS_sigprocmask:
1426 				putchar('(');
1427 				print_integer_arg(sysdecode_sigprocmask_how,
1428 				    *ip);
1429 				ip++;
1430 				narg--;
1431 				c = ',';
1432 				break;
1433 			case SYS___acl_get_file:
1434 			case SYS___acl_set_file:
1435 			case SYS___acl_get_fd:
1436 			case SYS___acl_set_fd:
1437 			case SYS___acl_delete_file:
1438 			case SYS___acl_delete_fd:
1439 			case SYS___acl_aclcheck_file:
1440 			case SYS___acl_aclcheck_fd:
1441 			case SYS___acl_get_link:
1442 			case SYS___acl_set_link:
1443 			case SYS___acl_delete_link:
1444 			case SYS___acl_aclcheck_link:
1445 				print_number(ip, narg, c);
1446 				putchar(',');
1447 				print_integer_arg(sysdecode_acltype, *ip);
1448 				ip++;
1449 				narg--;
1450 				break;
1451 			case SYS_sigaction:
1452 				putchar('(');
1453 				print_signal(*ip);
1454 				ip++;
1455 				narg--;
1456 				c = ',';
1457 				break;
1458 			case SYS_extattrctl:
1459 				print_number(ip, narg, c);
1460 				putchar(',');
1461 				print_integer_arg(sysdecode_extattrnamespace,
1462 				    *ip);
1463 				ip++;
1464 				narg--;
1465 				break;
1466 			case SYS_nmount:
1467 				print_number(ip, narg, c);
1468 				print_number(ip, narg, c);
1469 				putchar(',');
1470 				print_mask_arg(sysdecode_mount_flags, *ip);
1471 				ip++;
1472 				narg--;
1473 				break;
1474 			case SYS_thr_create:
1475 				print_number(ip, narg, c);
1476 				print_number(ip, narg, c);
1477 				putchar(',');
1478 				print_mask_arg(sysdecode_thr_create_flags, *ip);
1479 				ip++;
1480 				narg--;
1481 				break;
1482 			case SYS_thr_kill:
1483 				print_number(ip, narg, c);
1484 				putchar(',');
1485 				print_signal(*ip);
1486 				ip++;
1487 				narg--;
1488 				break;
1489 			case SYS_kldunloadf:
1490 				print_number(ip, narg, c);
1491 				putchar(',');
1492 				print_integer_arg(sysdecode_kldunload_flags,
1493 				    *ip);
1494 				ip++;
1495 				narg--;
1496 				break;
1497 			case SYS_linkat:
1498 			case SYS_renameat:
1499 			case SYS_symlinkat:
1500 				print_number(ip, narg, c);
1501 				putchar(',');
1502 				print_integer_arg_valid(sysdecode_atfd, *ip);
1503 				ip++;
1504 				narg--;
1505 				print_number(ip, narg, c);
1506 				break;
1507 			case SYS_cap_fcntls_limit:
1508 				print_number(ip, narg, c);
1509 				putchar(',');
1510 				arg = *ip;
1511 				ip++;
1512 				narg--;
1513 				print_mask_arg32(sysdecode_cap_fcntlrights, arg);
1514 				break;
1515 			case SYS_posix_fadvise:
1516 				print_number(ip, narg, c);
1517 				print_number(ip, narg, c);
1518 				print_number(ip, narg, c);
1519 				(void)putchar(',');
1520 				print_integer_arg(sysdecode_fadvice, *ip);
1521 				ip++;
1522 				narg--;
1523 				break;
1524 			case SYS_procctl:
1525 				putchar('(');
1526 				print_integer_arg(sysdecode_idtype, *ip);
1527 				c = ',';
1528 				ip++;
1529 				narg--;
1530 				print_number64(first, ip, narg, c);
1531 				putchar(',');
1532 				print_integer_arg(sysdecode_procctl_cmd, *ip);
1533 				ip++;
1534 				narg--;
1535 				break;
1536 			case SYS__umtx_op: {
1537 				int op;
1538 
1539 				print_number(ip, narg, c);
1540 				putchar(',');
1541 				if (print_mask_arg_part(sysdecode_umtx_op_flags,
1542 				    *ip, &op))
1543 					putchar('|');
1544 				print_integer_arg(sysdecode_umtx_op, op);
1545 				putchar('>');
1546 				switch (*ip) {
1547 				case UMTX_OP_CV_WAIT:
1548 					ip++;
1549 					narg--;
1550 					putchar(',');
1551 					print_mask_argul(
1552 					    sysdecode_umtx_cvwait_flags, *ip);
1553 					break;
1554 				case UMTX_OP_RW_RDLOCK:
1555 					ip++;
1556 					narg--;
1557 					putchar(',');
1558 					print_mask_argul(
1559 					    sysdecode_umtx_rwlock_flags, *ip);
1560 					break;
1561 				}
1562 				ip++;
1563 				narg--;
1564 				break;
1565 			}
1566 			case SYS_ftruncate:
1567 			case SYS_truncate:
1568 				print_number(ip, narg, c);
1569 				print_number64(first, ip, narg, c);
1570 				break;
1571 			case SYS_fchownat:
1572 				print_number(ip, narg, c);
1573 				print_number(ip, narg, c);
1574 				print_number(ip, narg, c);
1575 				break;
1576 			case SYS_fstatat:
1577 			case SYS_utimensat:
1578 				print_number(ip, narg, c);
1579 				print_number(ip, narg, c);
1580 				break;
1581 			case SYS_unlinkat:
1582 				print_number(ip, narg, c);
1583 				break;
1584 			case SYS_sysarch:
1585 				putchar('(');
1586 				print_integer_arg(sysdecode_sysarch_number, *ip);
1587 				ip++;
1588 				narg--;
1589 				c = ',';
1590 				break;
1591 			case SYS_getitimer:
1592 			case SYS_setitimer:
1593 				putchar('(');
1594 				print_integer_arg(sysdecode_itimer, *ip);
1595 				ip++;
1596 				narg--;
1597 				c = ',';
1598 				break;
1599 			}
1600 			switch (ktr->ktr_code) {
1601 			case SYS_chflagsat:
1602 			case SYS_fchownat:
1603 			case SYS_faccessat:
1604 			case SYS_fchmodat:
1605 			case SYS_fstatat:
1606 			case SYS_linkat:
1607 			case SYS_unlinkat:
1608 			case SYS_utimensat:
1609 				putchar(',');
1610 				print_mask_arg0(sysdecode_atflags, *ip);
1611 				ip++;
1612 				narg--;
1613 				break;
1614 			}
1615 	*resc = c;
1616 	*resip = ip;
1617 	*resnarg = narg;
1618 }
1619 
1620 void
1621 ktrsysret(struct ktr_sysret *ktr, u_int sv_flags)
1622 {
1623 	register_t ret = ktr->ktr_retval;
1624 	int error = ktr->ktr_error;
1625 
1626 	syscallname(ktr->ktr_code, sv_flags);
1627 	printf(" ");
1628 
1629 	if (error == 0) {
1630 		if (fancy) {
1631 			printf("%ld", (long)ret);
1632 			if (ret < 0 || ret > 9)
1633 				printf("/%#lx", (unsigned long)ret);
1634 		} else {
1635 			if (decimal)
1636 				printf("%ld", (long)ret);
1637 			else
1638 				printf("%#lx", (unsigned long)ret);
1639 		}
1640 	} else if (error == ERESTART)
1641 		printf("RESTART");
1642 	else if (error == EJUSTRETURN)
1643 		printf("JUSTRETURN");
1644 	else {
1645 		printf("-1 errno %d", sysdecode_freebsd_to_abi_errno(
1646 		    syscallabi(sv_flags), error));
1647 		if (fancy)
1648 			printf(" %s", strerror(ktr->ktr_error));
1649 	}
1650 	putchar('\n');
1651 }
1652 
1653 void
1654 ktrnamei(char *cp, int len)
1655 {
1656 	printf("\"%.*s\"\n", len, cp);
1657 }
1658 
1659 void
1660 hexdump(char *p, int len, int screenwidth)
1661 {
1662 	int n, i;
1663 	int width;
1664 
1665 	width = 0;
1666 	do {
1667 		width += 2;
1668 		i = 13;			/* base offset */
1669 		i += (width / 2) + 1;	/* spaces every second byte */
1670 		i += (width * 2);	/* width of bytes */
1671 		i += 3;			/* "  |" */
1672 		i += width;		/* each byte */
1673 		i += 1;			/* "|" */
1674 	} while (i < screenwidth);
1675 	width -= 2;
1676 
1677 	for (n = 0; n < len; n += width) {
1678 		for (i = n; i < n + width; i++) {
1679 			if ((i % width) == 0) {	/* beginning of line */
1680 				printf("       0x%04x", i);
1681 			}
1682 			if ((i % 2) == 0) {
1683 				printf(" ");
1684 			}
1685 			if (i < len)
1686 				printf("%02x", p[i] & 0xff);
1687 			else
1688 				printf("  ");
1689 		}
1690 		printf("  |");
1691 		for (i = n; i < n + width; i++) {
1692 			if (i >= len)
1693 				break;
1694 			if (p[i] >= ' ' && p[i] <= '~')
1695 				printf("%c", p[i]);
1696 			else
1697 				printf(".");
1698 		}
1699 		printf("|\n");
1700 	}
1701 	if ((i % width) != 0)
1702 		printf("\n");
1703 }
1704 
1705 void
1706 visdump(char *dp, int datalen, int screenwidth)
1707 {
1708 	int col = 0;
1709 	char *cp;
1710 	int width;
1711 	char visbuf[5];
1712 
1713 	printf("       \"");
1714 	col = 8;
1715 	for (;datalen > 0; datalen--, dp++) {
1716 		vis(visbuf, *dp, VIS_CSTYLE | VIS_NOLOCALE, *(dp+1));
1717 		cp = visbuf;
1718 		/*
1719 		 * Keep track of printables and
1720 		 * space chars (like fold(1)).
1721 		 */
1722 		if (col == 0) {
1723 			putchar('\t');
1724 			col = 8;
1725 		}
1726 		switch(*cp) {
1727 		case '\n':
1728 			col = 0;
1729 			putchar('\n');
1730 			continue;
1731 		case '\t':
1732 			width = 8 - (col&07);
1733 			break;
1734 		default:
1735 			width = strlen(cp);
1736 		}
1737 		if (col + width > (screenwidth-2)) {
1738 			printf("\\\n\t");
1739 			col = 8;
1740 		}
1741 		col += width;
1742 		do {
1743 			putchar(*cp++);
1744 		} while (*cp);
1745 	}
1746 	if (col == 0)
1747 		printf("       ");
1748 	printf("\"\n");
1749 }
1750 
1751 void
1752 ktrgenio(struct ktr_genio *ktr, int len)
1753 {
1754 	int datalen = len - sizeof (struct ktr_genio);
1755 	char *dp = (char *)ktr + sizeof (struct ktr_genio);
1756 	static int screenwidth = 0;
1757 	int i, binary;
1758 
1759 	printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1760 		ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1761 		datalen == 1 ? "" : "s");
1762 	if (suppressdata)
1763 		return;
1764 	if (screenwidth == 0) {
1765 		struct winsize ws;
1766 
1767 		if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1768 		    ws.ws_col > 8)
1769 			screenwidth = ws.ws_col;
1770 		else
1771 			screenwidth = 80;
1772 	}
1773 	if (maxdata && datalen > maxdata)
1774 		datalen = maxdata;
1775 
1776 	for (i = 0, binary = 0; i < datalen && binary == 0; i++)  {
1777 		if (dp[i] >= 32 && dp[i] < 127)
1778 			continue;
1779 		if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1780 			continue;
1781 		binary = 1;
1782 	}
1783 	if (binary)
1784 		hexdump(dp, datalen, screenwidth);
1785 	else
1786 		visdump(dp, datalen, screenwidth);
1787 }
1788 
1789 void
1790 ktrpsig(struct ktr_psig *psig)
1791 {
1792 	const char *str;
1793 
1794 	print_signal(psig->signo);
1795 	if (psig->action == SIG_DFL) {
1796 		printf(" SIG_DFL");
1797 	} else {
1798 		printf(" caught handler=0x%lx mask=0x%x",
1799 		    (u_long)psig->action, psig->mask.__bits[0]);
1800 	}
1801 	printf(" code=");
1802 	str = sysdecode_sigcode(psig->signo, psig->code);
1803 	if (str != NULL)
1804 		printf("%s", str);
1805 	else
1806 		printf("<invalid=%#x>", psig->code);
1807 	putchar('\n');
1808 }
1809 
1810 void
1811 ktrcsw_old(struct ktr_csw_old *cs)
1812 {
1813 	printf("%s %s\n", cs->out ? "stop" : "resume",
1814 		cs->user ? "user" : "kernel");
1815 }
1816 
1817 void
1818 ktrcsw(struct ktr_csw *cs)
1819 {
1820 	printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1821 	    cs->user ? "user" : "kernel", cs->wmesg);
1822 }
1823 
1824 void
1825 ktruser(int len, void *p)
1826 {
1827 	unsigned char *cp;
1828 
1829 	if (sysdecode_utrace(stdout, p, len)) {
1830 		printf("\n");
1831 		return;
1832 	}
1833 
1834 	printf("%d ", len);
1835 	cp = p;
1836 	while (len--)
1837 		if (decimal)
1838 			printf(" %d", *cp++);
1839 		else
1840 			printf(" %02x", *cp++);
1841 	printf("\n");
1842 }
1843 
1844 void
1845 ktrcaprights(cap_rights_t *rightsp)
1846 {
1847 
1848 	printf("cap_rights_t ");
1849 	sysdecode_cap_rights(stdout, rightsp);
1850 	printf("\n");
1851 }
1852 
1853 static void
1854 ktrtimeval(struct timeval *tv)
1855 {
1856 
1857 	printf("{%ld, %ld}", (long)tv->tv_sec, tv->tv_usec);
1858 }
1859 
1860 void
1861 ktritimerval(struct itimerval *it)
1862 {
1863 
1864 	printf("itimerval { .interval = ");
1865 	ktrtimeval(&it->it_interval);
1866 	printf(", .value = ");
1867 	ktrtimeval(&it->it_value);
1868 	printf(" }\n");
1869 }
1870 
1871 void
1872 ktrsockaddr(struct sockaddr *sa)
1873 {
1874 /*
1875  TODO: Support additional address families
1876 	#include <netsmb/netbios.h>
1877 	struct sockaddr_nb	*nb;
1878 */
1879 	const char *str;
1880 	char addr[64];
1881 
1882 	/*
1883 	 * note: ktrstruct() has already verified that sa points to a
1884 	 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1885 	 * sa->sa_len bytes long.
1886 	 */
1887 	printf("struct sockaddr { ");
1888 	str = sysdecode_sockaddr_family(sa->sa_family);
1889 	if (str != NULL)
1890 		printf("%s", str);
1891 	else
1892 		printf("<invalid=%d>", sa->sa_family);
1893 	printf(", ");
1894 
1895 #define check_sockaddr_len(n)					\
1896 	if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) {	\
1897 		printf("invalid");				\
1898 		break;						\
1899 	}
1900 
1901 	switch(sa->sa_family) {
1902 	case AF_INET: {
1903 		struct sockaddr_in sa_in;
1904 
1905 		memset(&sa_in, 0, sizeof(sa_in));
1906 		memcpy(&sa_in, sa, sa->sa_len);
1907 		check_sockaddr_len(in);
1908 		inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1909 		printf("%s:%u", addr, ntohs(sa_in.sin_port));
1910 		break;
1911 	}
1912 	case AF_INET6: {
1913 		struct sockaddr_in6 sa_in6;
1914 
1915 		memset(&sa_in6, 0, sizeof(sa_in6));
1916 		memcpy(&sa_in6, sa, sa->sa_len);
1917 		check_sockaddr_len(in6);
1918 		getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1919 		    addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1920 		printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1921 		break;
1922 	}
1923 	case AF_UNIX: {
1924 		struct sockaddr_un sa_un;
1925 
1926 		memset(&sa_un, 0, sizeof(sa_un));
1927 		memcpy(&sa_un, sa, sa->sa_len);
1928 		printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1929 		break;
1930 	}
1931 	default:
1932 		printf("unknown address family");
1933 	}
1934 	printf(" }\n");
1935 }
1936 
1937 void
1938 ktrstat(struct stat *statp)
1939 {
1940 	char mode[12], timestr[PATH_MAX + 4];
1941 	struct passwd *pwd;
1942 	struct group  *grp;
1943 	struct tm *tm;
1944 
1945 	/*
1946 	 * note: ktrstruct() has already verified that statp points to a
1947 	 * buffer exactly sizeof(struct stat) bytes long.
1948 	 */
1949 	printf("struct stat {");
1950 	printf("dev=%ju, ino=%ju, ",
1951 		(uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1952 	if (!resolv)
1953 		printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1954 	else {
1955 		strmode(statp->st_mode, mode);
1956 		printf("mode=%s, ", mode);
1957 	}
1958 	printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1959 	if (!resolv) {
1960 		pwd = NULL;
1961 	} else {
1962 #ifdef WITH_CASPER
1963 		if (cappwd != NULL)
1964 			pwd = cap_getpwuid(cappwd, statp->st_uid);
1965 		else
1966 #endif
1967 			pwd = getpwuid(statp->st_uid);
1968 	}
1969 	if (pwd == NULL)
1970 		printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1971 	else
1972 		printf("uid=\"%s\", ", pwd->pw_name);
1973 	if (!resolv) {
1974 		grp = NULL;
1975 	} else {
1976 #ifdef WITH_CASPER
1977 		if (capgrp != NULL)
1978 			grp = cap_getgrgid(capgrp, statp->st_gid);
1979 		else
1980 #endif
1981 			grp = getgrgid(statp->st_gid);
1982 	}
1983 	if (grp == NULL)
1984 		printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1985 	else
1986 		printf("gid=\"%s\", ", grp->gr_name);
1987 	printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1988 	printf("atime=");
1989 	if (!resolv)
1990 		printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1991 	else {
1992 		tm = localtime(&statp->st_atim.tv_sec);
1993 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1994 		printf("\"%s\"", timestr);
1995 	}
1996 	if (statp->st_atim.tv_nsec != 0)
1997 		printf(".%09ld, ", statp->st_atim.tv_nsec);
1998 	else
1999 		printf(", ");
2000 	printf("mtime=");
2001 	if (!resolv)
2002 		printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
2003 	else {
2004 		tm = localtime(&statp->st_mtim.tv_sec);
2005 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
2006 		printf("\"%s\"", timestr);
2007 	}
2008 	if (statp->st_mtim.tv_nsec != 0)
2009 		printf(".%09ld, ", statp->st_mtim.tv_nsec);
2010 	else
2011 		printf(", ");
2012 	printf("ctime=");
2013 	if (!resolv)
2014 		printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
2015 	else {
2016 		tm = localtime(&statp->st_ctim.tv_sec);
2017 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
2018 		printf("\"%s\"", timestr);
2019 	}
2020 	if (statp->st_ctim.tv_nsec != 0)
2021 		printf(".%09ld, ", statp->st_ctim.tv_nsec);
2022 	else
2023 		printf(", ");
2024 	printf("birthtime=");
2025 	if (!resolv)
2026 		printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
2027 	else {
2028 		tm = localtime(&statp->st_birthtim.tv_sec);
2029 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
2030 		printf("\"%s\"", timestr);
2031 	}
2032 	if (statp->st_birthtim.tv_nsec != 0)
2033 		printf(".%09ld, ", statp->st_birthtim.tv_nsec);
2034 	else
2035 		printf(", ");
2036 	printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
2037 		(uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
2038 		(intmax_t)statp->st_blocks, statp->st_flags);
2039 	printf(" }\n");
2040 }
2041 
2042 void
2043 ktrbitset(char *name, struct bitset *set, size_t setlen)
2044 {
2045 	int i, maxi, c = 0;
2046 
2047 	if (setlen > INT32_MAX)
2048 		setlen = INT32_MAX;
2049 	maxi = setlen * CHAR_BIT;
2050 	printf("%s [ ", name);
2051 	for (i = 0; i < maxi; i++) {
2052 		if (!BIT_ISSET(setlen, i, set))
2053 			continue;
2054 		if (c == 0)
2055 			printf("%d", i);
2056 		else
2057 			printf(", %d", i);
2058 		c++;
2059 	}
2060 	if (c == 0)
2061 		printf(" empty ]\n");
2062 	else
2063 		printf(" ]\n");
2064 }
2065 
2066 void
2067 ktrstruct(char *buf, size_t buflen)
2068 {
2069 	char *name, *data;
2070 	size_t namelen, datalen;
2071 	int i;
2072 	cap_rights_t rights;
2073 	struct itimerval it;
2074 	struct stat sb;
2075 	struct sockaddr_storage ss;
2076 	struct bitset *set;
2077 
2078 	for (name = buf, namelen = 0;
2079 	     namelen < buflen && name[namelen] != '\0';
2080 	     ++namelen)
2081 		/* nothing */;
2082 	if (namelen == buflen)
2083 		goto invalid;
2084 	if (name[namelen] != '\0')
2085 		goto invalid;
2086 	data = buf + namelen + 1;
2087 	datalen = buflen - namelen - 1;
2088 	if (datalen == 0)
2089 		goto invalid;
2090 	/* sanity check */
2091 	for (i = 0; i < (int)namelen; ++i)
2092 		if (!isalpha(name[i]) && name[i] != '_')
2093 			goto invalid;
2094 	if (strcmp(name, "caprights") == 0) {
2095 		if (datalen != sizeof(cap_rights_t))
2096 			goto invalid;
2097 		memcpy(&rights, data, datalen);
2098 		ktrcaprights(&rights);
2099 	} else if (strcmp(name, "itimerval") == 0) {
2100 		if (datalen != sizeof(struct itimerval))
2101 			goto invalid;
2102 		memcpy(&it, data, datalen);
2103 		ktritimerval(&it);
2104 	} else if (strcmp(name, "stat") == 0) {
2105 		if (datalen != sizeof(struct stat))
2106 			goto invalid;
2107 		memcpy(&sb, data, datalen);
2108 		ktrstat(&sb);
2109 	} else if (strcmp(name, "sockaddr") == 0) {
2110 		if (datalen > sizeof(ss))
2111 			goto invalid;
2112 		memcpy(&ss, data, datalen);
2113 		if (datalen != ss.ss_len)
2114 			goto invalid;
2115 		ktrsockaddr((struct sockaddr *)&ss);
2116 	} else if (strcmp(name, "cpuset_t") == 0) {
2117 		if (datalen < 1)
2118 			goto invalid;
2119 		set = malloc(datalen);
2120 		if (set == NULL)
2121 			errx(1, "%s", strerror(ENOMEM));
2122 		memcpy(set, data, datalen);
2123 		ktrbitset(name, set, datalen);
2124 		free(set);
2125 	} else {
2126 #ifdef SYSDECODE_HAVE_LINUX
2127 		if (ktrstruct_linux(name, data, datalen) == false)
2128 #endif
2129 			printf("unknown structure\n");
2130 	}
2131 	return;
2132 invalid:
2133 	printf("invalid record\n");
2134 }
2135 
2136 void
2137 ktrcapfail(struct ktr_cap_fail *ktr)
2138 {
2139 	switch (ktr->cap_type) {
2140 	case CAPFAIL_NOTCAPABLE:
2141 		/* operation on fd with insufficient capabilities */
2142 		printf("operation requires ");
2143 		sysdecode_cap_rights(stdout, &ktr->cap_needed);
2144 		printf(", descriptor holds ");
2145 		sysdecode_cap_rights(stdout, &ktr->cap_held);
2146 		break;
2147 	case CAPFAIL_INCREASE:
2148 		/* requested more capabilities than fd already has */
2149 		printf("attempt to increase capabilities from ");
2150 		sysdecode_cap_rights(stdout, &ktr->cap_held);
2151 		printf(" to ");
2152 		sysdecode_cap_rights(stdout, &ktr->cap_needed);
2153 		break;
2154 	case CAPFAIL_SYSCALL:
2155 		/* called restricted syscall */
2156 		printf("disallowed system call");
2157 		break;
2158 	case CAPFAIL_LOOKUP:
2159 		/* absolute or AT_FDCWD path, ".." path, etc. */
2160 		printf("restricted VFS lookup");
2161 		break;
2162 	default:
2163 		printf("unknown capability failure: ");
2164 		sysdecode_cap_rights(stdout, &ktr->cap_needed);
2165 		printf(" ");
2166 		sysdecode_cap_rights(stdout, &ktr->cap_held);
2167 		break;
2168 	}
2169 	printf("\n");
2170 }
2171 
2172 void
2173 ktrfault(struct ktr_fault *ktr)
2174 {
2175 
2176 	printf("0x%jx ", (uintmax_t)ktr->vaddr);
2177 	print_mask_arg(sysdecode_vmprot, ktr->type);
2178 	printf("\n");
2179 }
2180 
2181 void
2182 ktrfaultend(struct ktr_faultend *ktr)
2183 {
2184 	const char *str;
2185 
2186 	str = sysdecode_vmresult(ktr->result);
2187 	if (str != NULL)
2188 		printf("%s", str);
2189 	else
2190 		printf("<invalid=%d>", ktr->result);
2191 	printf("\n");
2192 }
2193 
2194 void
2195 ktrkevent(struct kevent *kev)
2196 {
2197 
2198 	printf("{ ident=");
2199 	switch (kev->filter) {
2200 	case EVFILT_READ:
2201 	case EVFILT_WRITE:
2202 	case EVFILT_VNODE:
2203 	case EVFILT_PROC:
2204 	case EVFILT_TIMER:
2205 	case EVFILT_PROCDESC:
2206 	case EVFILT_EMPTY:
2207 		printf("%ju", (uintmax_t)kev->ident);
2208 		break;
2209 	case EVFILT_SIGNAL:
2210 		print_signal(kev->ident);
2211 		break;
2212 	default:
2213 		printf("%p", (void *)kev->ident);
2214 	}
2215 	printf(", filter=");
2216 	print_integer_arg(sysdecode_kevent_filter, kev->filter);
2217 	printf(", flags=");
2218 	print_mask_arg0(sysdecode_kevent_flags, kev->flags);
2219 	printf(", fflags=");
2220 	sysdecode_kevent_fflags(stdout, kev->filter, kev->fflags,
2221 	    decimal ? 10 : 16);
2222 	printf(", data=%#jx, udata=%p }", (uintmax_t)kev->data, kev->udata);
2223 }
2224 
2225 void
2226 ktrstructarray(struct ktr_struct_array *ksa, size_t buflen)
2227 {
2228 	struct kevent kev;
2229 	char *name, *data;
2230 	size_t namelen, datalen;
2231 	int i;
2232 	bool first;
2233 
2234 	buflen -= sizeof(*ksa);
2235 	for (name = (char *)(ksa + 1), namelen = 0;
2236 	     namelen < buflen && name[namelen] != '\0';
2237 	     ++namelen)
2238 		/* nothing */;
2239 	if (namelen == buflen)
2240 		goto invalid;
2241 	if (name[namelen] != '\0')
2242 		goto invalid;
2243 	/* sanity check */
2244 	for (i = 0; i < (int)namelen; ++i)
2245 		if (!isalnum(name[i]) && name[i] != '_')
2246 			goto invalid;
2247 	data = name + namelen + 1;
2248 	datalen = buflen - namelen - 1;
2249 	printf("struct %s[] = { ", name);
2250 	first = true;
2251 	for (; datalen >= ksa->struct_size;
2252 	    data += ksa->struct_size, datalen -= ksa->struct_size) {
2253 		if (!first)
2254 			printf("\n             ");
2255 		else
2256 			first = false;
2257 		if (strcmp(name, "kevent") == 0) {
2258 			if (ksa->struct_size != sizeof(kev))
2259 				goto bad_size;
2260 			memcpy(&kev, data, sizeof(kev));
2261 			ktrkevent(&kev);
2262 		} else if (strcmp(name, "freebsd11_kevent") == 0) {
2263 			struct freebsd11_kevent kev11;
2264 
2265 			if (ksa->struct_size != sizeof(kev11))
2266 				goto bad_size;
2267 			memcpy(&kev11, data, sizeof(kev11));
2268 			memset(&kev, 0, sizeof(kev));
2269 			kev.ident = kev11.ident;
2270 			kev.filter = kev11.filter;
2271 			kev.flags = kev11.flags;
2272 			kev.fflags = kev11.fflags;
2273 			kev.data = kev11.data;
2274 			kev.udata = kev11.udata;
2275 			ktrkevent(&kev);
2276 #ifdef _WANT_KEVENT32
2277 		} else if (strcmp(name, "kevent32") == 0) {
2278 			struct kevent32 kev32;
2279 
2280 			if (ksa->struct_size != sizeof(kev32))
2281 				goto bad_size;
2282 			memcpy(&kev32, data, sizeof(kev32));
2283 			memset(&kev, 0, sizeof(kev));
2284 			kev.ident = kev32.ident;
2285 			kev.filter = kev32.filter;
2286 			kev.flags = kev32.flags;
2287 			kev.fflags = kev32.fflags;
2288 #if BYTE_ORDER == BIG_ENDIAN
2289 			kev.data = kev32.data2 | ((int64_t)kev32.data1 << 32);
2290 #else
2291 			kev.data = kev32.data1 | ((int64_t)kev32.data2 << 32);
2292 #endif
2293 			kev.udata = (void *)(uintptr_t)kev32.udata;
2294 			ktrkevent(&kev);
2295 		} else if (strcmp(name, "freebsd11_kevent32") == 0) {
2296 			struct freebsd11_kevent32 kev32;
2297 
2298 			if (ksa->struct_size != sizeof(kev32))
2299 				goto bad_size;
2300 			memcpy(&kev32, data, sizeof(kev32));
2301 			memset(&kev, 0, sizeof(kev));
2302 			kev.ident = kev32.ident;
2303 			kev.filter = kev32.filter;
2304 			kev.flags = kev32.flags;
2305 			kev.fflags = kev32.fflags;
2306 			kev.data = kev32.data;
2307 			kev.udata = (void *)(uintptr_t)kev32.udata;
2308 			ktrkevent(&kev);
2309 #endif
2310 		} else {
2311 			printf("<unknown structure> }\n");
2312 			return;
2313 		}
2314 	}
2315 	printf(" }\n");
2316 	return;
2317 invalid:
2318 	printf("invalid record\n");
2319 	return;
2320 bad_size:
2321 	printf("<bad size> }\n");
2322 	return;
2323 }
2324 
2325 void
2326 usage(void)
2327 {
2328 	fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
2329 	    "[-m maxdata] [-p pid] [-t trstr]\n");
2330 	exit(1);
2331 }
2332