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