xref: /freebsd/usr.bin/kdump/kdump.c (revision 78ee8d1c4cdad7a56dbf50f1c8ade75531ce620c)
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 				    "%6jd %6jd %-8.*s Events dropped.\n",
453 				    (intmax_t)ktr_header.ktr_pid,
454 				    ktr_header.ktr_tid > 0 ?
455 				    (intmax_t)ktr_header.ktr_tid : 0,
456 				    MAXCOMLEN, ktr_header.ktr_comm);
457 				drop_logged = 1;
458 			} else if (!drop_logged) {
459 				printf("%6jd %-8.*s Events dropped.\n",
460 				    (intmax_t)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("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
728 		    kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
729 		    MAXCOMLEN, kth->ktr_comm);
730 	else
731 		printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
732 		    kth->ktr_comm);
733         if (timestamp) {
734 		if (version == KTR_VERSION0)
735 			dumptimeval((struct ktr_header_v0 *)kth);
736 		else
737 			dumptimespec(kth);
738 	}
739 	if (cpuflag && version > KTR_VERSION0)
740 		printf("%3d ", kth->ktr_cpu);
741 	printf("%s  ", type);
742 	if (abiflag != 0) {
743 		switch (sv_flags & SV_ABI_MASK) {
744 		case SV_ABI_LINUX:
745 			abi = "L";
746 			break;
747 		case SV_ABI_FREEBSD:
748 			abi = "F";
749 			break;
750 		default:
751 			abi = "U";
752 			break;
753 		}
754 
755 		if ((sv_flags & SV_LP64) != 0)
756 			arch = "64";
757 		else if ((sv_flags & SV_ILP32) != 0)
758 			arch = "32";
759 		else
760 			arch = "00";
761 
762 		printf("%s%s  ", abi, arch);
763 	}
764 }
765 
766 #include <sys/syscall.h>
767 
768 static void
769 ioctlname(unsigned long val)
770 {
771 	const char *str;
772 
773 	str = sysdecode_ioctlname(val);
774 	if (str != NULL)
775 		printf("%s", str);
776 	else if (decimal)
777 		printf("%lu", val);
778 	else
779 		printf("%#lx", val);
780 }
781 
782 static enum sysdecode_abi
783 syscallabi(u_int sv_flags)
784 {
785 
786 	if (sv_flags == 0)
787 		return (SYSDECODE_ABI_FREEBSD);
788 	switch (sv_flags & SV_ABI_MASK) {
789 	case SV_ABI_FREEBSD:
790 		return (SYSDECODE_ABI_FREEBSD);
791 	case SV_ABI_LINUX:
792 #ifdef __LP64__
793 		if (sv_flags & SV_ILP32)
794 			return (SYSDECODE_ABI_LINUX32);
795 #endif
796 		return (SYSDECODE_ABI_LINUX);
797 	default:
798 		return (SYSDECODE_ABI_UNKNOWN);
799 	}
800 }
801 
802 static void
803 syscallname(u_int code, u_int sv_flags)
804 {
805 	const char *name;
806 
807 	name = sysdecode_syscallname(syscallabi(sv_flags), code);
808 	if (name == NULL)
809 		printf("[%d]", code);
810 	else {
811 		printf("%s", name);
812 		if (syscallno)
813 			printf("[%d]", code);
814 	}
815 }
816 
817 static void
818 print_signal(int signo)
819 {
820 	const char *signame;
821 
822 	signame = sysdecode_signal(signo);
823 	if (signame != NULL)
824 		printf("%s", signame);
825 	else
826 		printf("SIG %d", signo);
827 }
828 
829 void
830 ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags)
831 {
832 	int narg = ktr->ktr_narg;
833 	register_t *ip;
834 
835 	syscallname(ktr->ktr_code, sv_flags);
836 	ip = &ktr->ktr_args[0];
837 	if (narg) {
838 		char c = '(';
839 		if (fancy) {
840 			switch (sv_flags & SV_ABI_MASK) {
841 			case SV_ABI_FREEBSD:
842 				ktrsyscall_freebsd(ktr, &ip, &narg, &c,
843 				    sv_flags);
844 				break;
845 #ifdef SYSDECODE_HAVE_LINUX
846 			case SV_ABI_LINUX:
847 #ifdef __amd64__
848 				if (sv_flags & SV_ILP32)
849 					ktrsyscall_linux32(ktr, &ip,
850 					    &narg, &c);
851 				else
852 #endif
853 				ktrsyscall_linux(ktr, &ip, &narg, &c);
854 				break;
855 #endif /* SYSDECODE_HAVE_LINUX */
856 			}
857 		}
858 		while (narg > 0)
859 			print_number(ip, narg, c);
860 		putchar(')');
861 	}
862 	putchar('\n');
863 }
864 
865 void
866 ktrsyscall_freebsd(struct ktr_syscall *ktr, register_t **resip,
867     int *resnarg, char *resc, u_int sv_flags)
868 {
869 	int narg = ktr->ktr_narg;
870 	register_t *ip, *first;
871 	intmax_t arg;
872 	int quad_align, quad_slots;
873 
874 	ip = first = &ktr->ktr_args[0];
875 	char c = *resc;
876 
877 			quad_align = 0;
878 			if (sv_flags & SV_ILP32) {
879 #ifdef __powerpc__
880 				quad_align = 1;
881 #endif
882 				quad_slots = 2;
883 			} else
884 				quad_slots = 1;
885 			switch (ktr->ktr_code) {
886 			case SYS_bindat:
887 			case SYS_chflagsat:
888 			case SYS_connectat:
889 			case SYS_faccessat:
890 			case SYS_fchmodat:
891 			case SYS_fchownat:
892 			case SYS_fstatat:
893 			case SYS_futimesat:
894 			case SYS_linkat:
895 			case SYS_mkdirat:
896 			case SYS_mkfifoat:
897 			case SYS_mknodat:
898 			case SYS_openat:
899 			case SYS_readlinkat:
900 			case SYS_renameat:
901 			case SYS_unlinkat:
902 			case SYS_utimensat:
903 				putchar('(');
904 				print_integer_arg_valid(sysdecode_atfd, *ip);
905 				c = ',';
906 				ip++;
907 				narg--;
908 				break;
909 			}
910 			switch (ktr->ktr_code) {
911 			case SYS_ioctl: {
912 				print_number(ip, narg, c);
913 				putchar(c);
914 				ioctlname(*ip);
915 				c = ',';
916 				ip++;
917 				narg--;
918 				break;
919 			}
920 			case SYS_ptrace:
921 				putchar('(');
922 				print_integer_arg(sysdecode_ptrace_request, *ip);
923 				c = ',';
924 				ip++;
925 				narg--;
926 				break;
927 			case SYS_access:
928 			case SYS_eaccess:
929 			case SYS_faccessat:
930 				print_number(ip, narg, c);
931 				putchar(',');
932 				print_mask_arg(sysdecode_access_mode, *ip);
933 				ip++;
934 				narg--;
935 				break;
936 			case SYS_close_range:
937 				print_number(ip, narg, c);
938 				print_number(ip, narg, c);
939 				putchar(',');
940 				print_mask_arg(sysdecode_close_range_flags, *ip);
941 				ip += 3;
942 				narg -= 3;
943 				break;
944 			case SYS_open:
945 			case SYS_openat:
946 				print_number(ip, narg, c);
947 				putchar(',');
948 				print_mask_arg(sysdecode_open_flags, ip[0]);
949 				if ((ip[0] & O_CREAT) == O_CREAT) {
950 					putchar(',');
951 					decode_filemode(ip[1]);
952 				}
953 				ip += 2;
954 				narg -= 2;
955 				break;
956 			case SYS_wait4:
957 				print_number(ip, narg, c);
958 				print_number(ip, narg, c);
959 				putchar(',');
960 				print_mask_arg0(sysdecode_wait4_options, *ip);
961 				ip++;
962 				narg--;
963 				break;
964 			case SYS_wait6:
965 				putchar('(');
966 				print_integer_arg(sysdecode_idtype, *ip);
967 				c = ',';
968 				ip++;
969 				narg--;
970 				print_number64(first, ip, narg, c);
971 				print_number(ip, narg, c);
972 				putchar(',');
973 				print_mask_arg(sysdecode_wait6_options, *ip);
974 				ip++;
975 				narg--;
976 				break;
977 			case SYS_chmod:
978 			case SYS_fchmod:
979 			case SYS_lchmod:
980 			case SYS_fchmodat:
981 				print_number(ip, narg, c);
982 				putchar(',');
983 				decode_filemode(*ip);
984 				ip++;
985 				narg--;
986 				break;
987 			case SYS_mknodat:
988 				print_number(ip, narg, c);
989 				putchar(',');
990 				decode_filemode(*ip);
991 				ip++;
992 				narg--;
993 				break;
994 			case SYS_getfsstat:
995 				print_number(ip, narg, c);
996 				print_number(ip, narg, c);
997 				putchar(',');
998 				print_integer_arg(sysdecode_getfsstat_mode, *ip);
999 				ip++;
1000 				narg--;
1001 				break;
1002 			case SYS_mount:
1003 				print_number(ip, narg, c);
1004 				print_number(ip, narg, c);
1005 				putchar(',');
1006 				print_mask_arg(sysdecode_mount_flags, *ip);
1007 				ip++;
1008 				narg--;
1009 				break;
1010 			case SYS_unmount:
1011 				print_number(ip, narg, c);
1012 				putchar(',');
1013 				print_mask_arg(sysdecode_mount_flags, *ip);
1014 				ip++;
1015 				narg--;
1016 				break;
1017 			case SYS_recvmsg:
1018 			case SYS_sendmsg:
1019 				print_number(ip, narg, c);
1020 				print_number(ip, narg, c);
1021 				putchar(',');
1022 				print_mask_arg0(sysdecode_msg_flags, *ip);
1023 				ip++;
1024 				narg--;
1025 				break;
1026 			case SYS_recvfrom:
1027 			case SYS_sendto:
1028 				print_number(ip, narg, c);
1029 				print_number(ip, narg, c);
1030 				print_number(ip, narg, c);
1031 				putchar(',');
1032 				print_mask_arg0(sysdecode_msg_flags, *ip);
1033 				ip++;
1034 				narg--;
1035 				break;
1036 			case SYS_chflags:
1037 			case SYS_chflagsat:
1038 			case SYS_fchflags:
1039 			case SYS_lchflags:
1040 				print_number(ip, narg, c);
1041 				putchar(',');
1042 				decode_fileflags(*ip);
1043 				ip++;
1044 				narg--;
1045 				break;
1046 			case SYS_kill:
1047 				print_number(ip, narg, c);
1048 				putchar(',');
1049 				print_signal(*ip);
1050 				ip++;
1051 				narg--;
1052 				break;
1053 			case SYS_reboot:
1054 				putchar('(');
1055 				print_mask_arg(sysdecode_reboot_howto, *ip);
1056 				ip++;
1057 				narg--;
1058 				break;
1059 			case SYS_umask:
1060 				putchar('(');
1061 				decode_filemode(*ip);
1062 				ip++;
1063 				narg--;
1064 				break;
1065 			case SYS_msync:
1066 				print_number(ip, narg, c);
1067 				print_number(ip, narg, c);
1068 				putchar(',');
1069 				print_mask_arg(sysdecode_msync_flags, *ip);
1070 				ip++;
1071 				narg--;
1072 				break;
1073 #ifdef SYS_freebsd6_mmap
1074 			case SYS_freebsd6_mmap:
1075 				print_number(ip, narg, c);
1076 				print_number(ip, narg, c);
1077 				putchar(',');
1078 				print_mask_arg(sysdecode_mmap_prot, *ip);
1079 				putchar(',');
1080 				ip++;
1081 				narg--;
1082 				print_mask_arg(sysdecode_mmap_flags, *ip);
1083 				ip++;
1084 				narg--;
1085 				break;
1086 #endif
1087 			case SYS_mmap:
1088 				print_number(ip, narg, c);
1089 				print_number(ip, narg, c);
1090 				putchar(',');
1091 				print_mask_arg(sysdecode_mmap_prot, *ip);
1092 				putchar(',');
1093 				ip++;
1094 				narg--;
1095 				print_mask_arg(sysdecode_mmap_flags, *ip);
1096 				ip++;
1097 				narg--;
1098 				break;
1099 			case SYS_mprotect:
1100 				print_number(ip, narg, c);
1101 				print_number(ip, narg, c);
1102 				putchar(',');
1103 				print_mask_arg(sysdecode_mmap_prot, *ip);
1104 				ip++;
1105 				narg--;
1106 				break;
1107 			case SYS_madvise:
1108 				print_number(ip, narg, c);
1109 				print_number(ip, narg, c);
1110 				putchar(',');
1111 				print_integer_arg(sysdecode_madvice, *ip);
1112 				ip++;
1113 				narg--;
1114 				break;
1115 			case SYS_pathconf:
1116 			case SYS_lpathconf:
1117 			case SYS_fpathconf:
1118 				print_number(ip, narg, c);
1119 				putchar(',');
1120 				print_integer_arg(sysdecode_pathconf_name, *ip);
1121 				ip++;
1122 				narg--;
1123 				break;
1124 			case SYS_getpriority:
1125 			case SYS_setpriority:
1126 				putchar('(');
1127 				print_integer_arg(sysdecode_prio_which, *ip);
1128 				c = ',';
1129 				ip++;
1130 				narg--;
1131 				break;
1132 			case SYS_fcntl:
1133 				print_number(ip, narg, c);
1134 				putchar(',');
1135 				print_integer_arg(sysdecode_fcntl_cmd, ip[0]);
1136 				if (sysdecode_fcntl_arg_p(ip[0])) {
1137 					putchar(',');
1138 					if (ip[0] == F_SETFL)
1139 						print_mask_arg(
1140 						    sysdecode_fcntl_fileflags,
1141 							ip[1]);
1142 					else
1143 						sysdecode_fcntl_arg(stdout,
1144 						    ip[0], ip[1],
1145 						    decimal ? 10 : 16);
1146 				}
1147 				ip += 2;
1148 				narg -= 2;
1149 				break;
1150 			case SYS_socket: {
1151 				int sockdomain;
1152 				putchar('(');
1153 				sockdomain = *ip;
1154 				print_integer_arg(sysdecode_socketdomain,
1155 				    sockdomain);
1156 				ip++;
1157 				narg--;
1158 				putchar(',');
1159 				print_mask_arg(sysdecode_socket_type, *ip);
1160 				ip++;
1161 				narg--;
1162 				if (sockdomain == PF_INET ||
1163 				    sockdomain == PF_INET6) {
1164 					putchar(',');
1165 					print_integer_arg(sysdecode_ipproto,
1166 					    *ip);
1167 					ip++;
1168 					narg--;
1169 				}
1170 				c = ',';
1171 				break;
1172 			}
1173 			case SYS_setsockopt:
1174 			case SYS_getsockopt: {
1175 				const char *str;
1176 
1177 				print_number(ip, narg, c);
1178 				putchar(',');
1179 				print_integer_arg_valid(sysdecode_sockopt_level,
1180 				    *ip);
1181 				str = sysdecode_sockopt_name(ip[0], ip[1]);
1182 				if (str != NULL) {
1183 					printf(",%s", str);
1184 					ip++;
1185 					narg--;
1186 				}
1187 				ip++;
1188 				narg--;
1189 				break;
1190 			}
1191 #ifdef SYS_freebsd6_lseek
1192 			case SYS_freebsd6_lseek:
1193 				print_number(ip, narg, c);
1194 				/* Hidden 'pad' argument, not in lseek(2) */
1195 				print_number(ip, narg, c);
1196 				print_number64(first, ip, narg, c);
1197 				putchar(',');
1198 				print_integer_arg(sysdecode_whence, *ip);
1199 				ip++;
1200 				narg--;
1201 				break;
1202 #endif
1203 			case SYS_lseek:
1204 				print_number(ip, narg, c);
1205 				print_number64(first, ip, narg, c);
1206 				putchar(',');
1207 				print_integer_arg(sysdecode_whence, *ip);
1208 				ip++;
1209 				narg--;
1210 				break;
1211 			case SYS_flock:
1212 				print_number(ip, narg, c);
1213 				putchar(',');
1214 				print_mask_arg(sysdecode_flock_operation, *ip);
1215 				ip++;
1216 				narg--;
1217 				break;
1218 			case SYS_mkfifo:
1219 			case SYS_mkfifoat:
1220 			case SYS_mkdir:
1221 			case SYS_mkdirat:
1222 				print_number(ip, narg, c);
1223 				putchar(',');
1224 				decode_filemode(*ip);
1225 				ip++;
1226 				narg--;
1227 				break;
1228 			case SYS_shutdown:
1229 				print_number(ip, narg, c);
1230 				putchar(',');
1231 				print_integer_arg(sysdecode_shutdown_how, *ip);
1232 				ip++;
1233 				narg--;
1234 				break;
1235 			case SYS_socketpair:
1236 				putchar('(');
1237 				print_integer_arg(sysdecode_socketdomain, *ip);
1238 				ip++;
1239 				narg--;
1240 				putchar(',');
1241 				print_mask_arg(sysdecode_socket_type, *ip);
1242 				ip++;
1243 				narg--;
1244 				c = ',';
1245 				break;
1246 			case SYS_getrlimit:
1247 			case SYS_setrlimit:
1248 				putchar('(');
1249 				print_integer_arg(sysdecode_rlimit, *ip);
1250 				ip++;
1251 				narg--;
1252 				c = ',';
1253 				break;
1254 			case SYS_getrusage:
1255 				putchar('(');
1256 				print_integer_arg(sysdecode_getrusage_who, *ip);
1257 				ip++;
1258 				narg--;
1259 				c = ',';
1260 				break;
1261 			case SYS_quotactl:
1262 				print_number(ip, narg, c);
1263 				putchar(',');
1264 				if (!sysdecode_quotactl_cmd(stdout, *ip)) {
1265 					if (decimal)
1266 						printf("<invalid=%d>", (int)*ip);
1267 					else
1268 						printf("<invalid=%#x>",
1269 						    (int)*ip);
1270 				}
1271 				ip++;
1272 				narg--;
1273 				c = ',';
1274 				break;
1275 			case SYS_nfssvc:
1276 				putchar('(');
1277 				print_integer_arg(sysdecode_nfssvc_flags, *ip);
1278 				ip++;
1279 				narg--;
1280 				c = ',';
1281 				break;
1282 			case SYS_rtprio:
1283 			case SYS_rtprio_thread:
1284 				putchar('(');
1285 				print_integer_arg(sysdecode_rtprio_function,
1286 				    *ip);
1287 				ip++;
1288 				narg--;
1289 				c = ',';
1290 				break;
1291 			case SYS___semctl:
1292 				print_number(ip, narg, c);
1293 				print_number(ip, narg, c);
1294 				putchar(',');
1295 				print_integer_arg(sysdecode_semctl_cmd, *ip);
1296 				ip++;
1297 				narg--;
1298 				break;
1299 			case SYS_semget:
1300 				print_number(ip, narg, c);
1301 				print_number(ip, narg, c);
1302 				putchar(',');
1303 				print_mask_arg(sysdecode_semget_flags, *ip);
1304 				ip++;
1305 				narg--;
1306 				break;
1307 			case SYS_msgctl:
1308 				print_number(ip, narg, c);
1309 				putchar(',');
1310 				print_integer_arg(sysdecode_msgctl_cmd, *ip);
1311 				ip++;
1312 				narg--;
1313 				break;
1314 			case SYS_shmat:
1315 				print_number(ip, narg, c);
1316 				print_number(ip, narg, c);
1317 				putchar(',');
1318 				print_mask_arg(sysdecode_shmat_flags, *ip);
1319 				ip++;
1320 				narg--;
1321 				break;
1322 			case SYS_shmctl:
1323 				print_number(ip, narg, c);
1324 				putchar(',');
1325 				print_integer_arg(sysdecode_shmctl_cmd, *ip);
1326 				ip++;
1327 				narg--;
1328 				break;
1329 #ifdef SYS_freebsd12_shm_open
1330 			case SYS_freebsd12_shm_open:
1331 				if (ip[0] == (uintptr_t)SHM_ANON) {
1332 					printf("(SHM_ANON");
1333 					ip++;
1334 				} else {
1335 					print_number(ip, narg, c);
1336 				}
1337 				putchar(',');
1338 				print_mask_arg(sysdecode_open_flags, ip[0]);
1339 				putchar(',');
1340 				decode_filemode(ip[1]);
1341 				ip += 2;
1342 				narg -= 2;
1343 				break;
1344 #endif
1345 			case SYS_shm_open2:
1346 				if (ip[0] == (uintptr_t)SHM_ANON) {
1347 					printf("(SHM_ANON");
1348 					ip++;
1349 				} else {
1350 					print_number(ip, narg, c);
1351 				}
1352 				putchar(',');
1353 				print_mask_arg(sysdecode_open_flags, ip[0]);
1354 				putchar(',');
1355 				decode_filemode(ip[1]);
1356 				putchar(',');
1357 				print_mask_arg(sysdecode_shmflags, ip[2]);
1358 				ip += 3;
1359 				narg -= 3;
1360 				break;
1361 			case SYS_minherit:
1362 				print_number(ip, narg, c);
1363 				print_number(ip, narg, c);
1364 				putchar(',');
1365 				print_integer_arg(sysdecode_minherit_inherit,
1366 				    *ip);
1367 				ip++;
1368 				narg--;
1369 				break;
1370 			case SYS_rfork:
1371 				putchar('(');
1372 				print_mask_arg(sysdecode_rfork_flags, *ip);
1373 				ip++;
1374 				narg--;
1375 				c = ',';
1376 				break;
1377 			case SYS_lio_listio:
1378 				putchar('(');
1379 				print_integer_arg(sysdecode_lio_listio_mode,
1380 				    *ip);
1381 				ip++;
1382 				narg--;
1383 				c = ',';
1384 				break;
1385 			case SYS_mlockall:
1386 				putchar('(');
1387 				print_mask_arg(sysdecode_mlockall_flags, *ip);
1388 				ip++;
1389 				narg--;
1390 				break;
1391 			case SYS_sched_setscheduler:
1392 				print_number(ip, narg, c);
1393 				putchar(',');
1394 				print_integer_arg(sysdecode_scheduler_policy,
1395 				    *ip);
1396 				ip++;
1397 				narg--;
1398 				break;
1399 			case SYS_sched_get_priority_max:
1400 			case SYS_sched_get_priority_min:
1401 				putchar('(');
1402 				print_integer_arg(sysdecode_scheduler_policy,
1403 				    *ip);
1404 				ip++;
1405 				narg--;
1406 				break;
1407 			case SYS_sendfile:
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 				print_number(ip, narg, c);
1414 				putchar(',');
1415 				print_mask_arg(sysdecode_sendfile_flags, *ip);
1416 				ip++;
1417 				narg--;
1418 				break;
1419 			case SYS_kldsym:
1420 				print_number(ip, narg, c);
1421 				putchar(',');
1422 				print_integer_arg(sysdecode_kldsym_cmd, *ip);
1423 				ip++;
1424 				narg--;
1425 				break;
1426 			case SYS_sigprocmask:
1427 				putchar('(');
1428 				print_integer_arg(sysdecode_sigprocmask_how,
1429 				    *ip);
1430 				ip++;
1431 				narg--;
1432 				c = ',';
1433 				break;
1434 			case SYS___acl_get_file:
1435 			case SYS___acl_set_file:
1436 			case SYS___acl_get_fd:
1437 			case SYS___acl_set_fd:
1438 			case SYS___acl_delete_file:
1439 			case SYS___acl_delete_fd:
1440 			case SYS___acl_aclcheck_file:
1441 			case SYS___acl_aclcheck_fd:
1442 			case SYS___acl_get_link:
1443 			case SYS___acl_set_link:
1444 			case SYS___acl_delete_link:
1445 			case SYS___acl_aclcheck_link:
1446 				print_number(ip, narg, c);
1447 				putchar(',');
1448 				print_integer_arg(sysdecode_acltype, *ip);
1449 				ip++;
1450 				narg--;
1451 				break;
1452 			case SYS_sigaction:
1453 				putchar('(');
1454 				print_signal(*ip);
1455 				ip++;
1456 				narg--;
1457 				c = ',';
1458 				break;
1459 			case SYS_extattrctl:
1460 				print_number(ip, narg, c);
1461 				putchar(',');
1462 				print_integer_arg(sysdecode_extattrnamespace,
1463 				    *ip);
1464 				ip++;
1465 				narg--;
1466 				break;
1467 			case SYS_nmount:
1468 				print_number(ip, narg, c);
1469 				print_number(ip, narg, c);
1470 				putchar(',');
1471 				print_mask_arg(sysdecode_mount_flags, *ip);
1472 				ip++;
1473 				narg--;
1474 				break;
1475 			case SYS_thr_create:
1476 				print_number(ip, narg, c);
1477 				print_number(ip, narg, c);
1478 				putchar(',');
1479 				print_mask_arg(sysdecode_thr_create_flags, *ip);
1480 				ip++;
1481 				narg--;
1482 				break;
1483 			case SYS_thr_kill:
1484 				print_number(ip, narg, c);
1485 				putchar(',');
1486 				print_signal(*ip);
1487 				ip++;
1488 				narg--;
1489 				break;
1490 			case SYS_kldunloadf:
1491 				print_number(ip, narg, c);
1492 				putchar(',');
1493 				print_integer_arg(sysdecode_kldunload_flags,
1494 				    *ip);
1495 				ip++;
1496 				narg--;
1497 				break;
1498 			case SYS_linkat:
1499 			case SYS_renameat:
1500 			case SYS_symlinkat:
1501 				print_number(ip, narg, c);
1502 				putchar(',');
1503 				print_integer_arg_valid(sysdecode_atfd, *ip);
1504 				ip++;
1505 				narg--;
1506 				print_number(ip, narg, c);
1507 				break;
1508 			case SYS_cap_fcntls_limit:
1509 				print_number(ip, narg, c);
1510 				putchar(',');
1511 				arg = *ip;
1512 				ip++;
1513 				narg--;
1514 				print_mask_arg32(sysdecode_cap_fcntlrights, arg);
1515 				break;
1516 			case SYS_posix_fadvise:
1517 				print_number(ip, narg, c);
1518 				print_number(ip, narg, c);
1519 				print_number(ip, narg, c);
1520 				(void)putchar(',');
1521 				print_integer_arg(sysdecode_fadvice, *ip);
1522 				ip++;
1523 				narg--;
1524 				break;
1525 			case SYS_procctl:
1526 				putchar('(');
1527 				print_integer_arg(sysdecode_idtype, *ip);
1528 				c = ',';
1529 				ip++;
1530 				narg--;
1531 				print_number64(first, ip, narg, c);
1532 				putchar(',');
1533 				print_integer_arg(sysdecode_procctl_cmd, *ip);
1534 				ip++;
1535 				narg--;
1536 				break;
1537 			case SYS__umtx_op: {
1538 				int op;
1539 
1540 				print_number(ip, narg, c);
1541 				putchar(',');
1542 				if (print_mask_arg_part(sysdecode_umtx_op_flags,
1543 				    *ip, &op))
1544 					putchar('|');
1545 				print_integer_arg(sysdecode_umtx_op, op);
1546 				putchar('>');
1547 				switch (*ip) {
1548 				case UMTX_OP_CV_WAIT:
1549 					ip++;
1550 					narg--;
1551 					putchar(',');
1552 					print_mask_argul(
1553 					    sysdecode_umtx_cvwait_flags, *ip);
1554 					break;
1555 				case UMTX_OP_RW_RDLOCK:
1556 					ip++;
1557 					narg--;
1558 					putchar(',');
1559 					print_mask_argul(
1560 					    sysdecode_umtx_rwlock_flags, *ip);
1561 					break;
1562 				}
1563 				ip++;
1564 				narg--;
1565 				break;
1566 			}
1567 			case SYS_ftruncate:
1568 			case SYS_truncate:
1569 				print_number(ip, narg, c);
1570 				print_number64(first, ip, narg, c);
1571 				break;
1572 			case SYS_fchownat:
1573 				print_number(ip, narg, c);
1574 				print_number(ip, narg, c);
1575 				print_number(ip, narg, c);
1576 				break;
1577 			case SYS_fstatat:
1578 			case SYS_utimensat:
1579 				print_number(ip, narg, c);
1580 				print_number(ip, narg, c);
1581 				break;
1582 			case SYS_unlinkat:
1583 				print_number(ip, narg, c);
1584 				break;
1585 			case SYS_sysarch:
1586 				putchar('(');
1587 				print_integer_arg(sysdecode_sysarch_number, *ip);
1588 				ip++;
1589 				narg--;
1590 				c = ',';
1591 				break;
1592 			case SYS_getitimer:
1593 			case SYS_setitimer:
1594 				putchar('(');
1595 				print_integer_arg(sysdecode_itimer, *ip);
1596 				ip++;
1597 				narg--;
1598 				c = ',';
1599 				break;
1600 			}
1601 			switch (ktr->ktr_code) {
1602 			case SYS_chflagsat:
1603 			case SYS_fchownat:
1604 			case SYS_faccessat:
1605 			case SYS_fchmodat:
1606 			case SYS_fstatat:
1607 			case SYS_linkat:
1608 			case SYS_unlinkat:
1609 			case SYS_utimensat:
1610 				putchar(',');
1611 				print_mask_arg0(sysdecode_atflags, *ip);
1612 				ip++;
1613 				narg--;
1614 				break;
1615 			}
1616 	*resc = c;
1617 	*resip = ip;
1618 	*resnarg = narg;
1619 }
1620 
1621 void
1622 ktrsysret(struct ktr_sysret *ktr, u_int sv_flags)
1623 {
1624 	register_t ret = ktr->ktr_retval;
1625 	int error = ktr->ktr_error;
1626 
1627 	syscallname(ktr->ktr_code, sv_flags);
1628 	printf(" ");
1629 
1630 	if (error == 0) {
1631 		if (fancy) {
1632 			printf("%ld", (long)ret);
1633 			if (ret < 0 || ret > 9)
1634 				printf("/%#lx", (unsigned long)ret);
1635 		} else {
1636 			if (decimal)
1637 				printf("%ld", (long)ret);
1638 			else
1639 				printf("%#lx", (unsigned long)ret);
1640 		}
1641 	} else if (error == ERESTART)
1642 		printf("RESTART");
1643 	else if (error == EJUSTRETURN)
1644 		printf("JUSTRETURN");
1645 	else {
1646 		printf("-1 errno %d", sysdecode_freebsd_to_abi_errno(
1647 		    syscallabi(sv_flags), error));
1648 		if (fancy)
1649 			printf(" %s", strerror(ktr->ktr_error));
1650 	}
1651 	putchar('\n');
1652 }
1653 
1654 void
1655 ktrnamei(char *cp, int len)
1656 {
1657 	printf("\"%.*s\"\n", len, cp);
1658 }
1659 
1660 void
1661 hexdump(char *p, int len, int screenwidth)
1662 {
1663 	int n, i;
1664 	int width;
1665 
1666 	width = 0;
1667 	do {
1668 		width += 2;
1669 		i = 13;			/* base offset */
1670 		i += (width / 2) + 1;	/* spaces every second byte */
1671 		i += (width * 2);	/* width of bytes */
1672 		i += 3;			/* "  |" */
1673 		i += width;		/* each byte */
1674 		i += 1;			/* "|" */
1675 	} while (i < screenwidth);
1676 	width -= 2;
1677 
1678 	for (n = 0; n < len; n += width) {
1679 		for (i = n; i < n + width; i++) {
1680 			if ((i % width) == 0) {	/* beginning of line */
1681 				printf("       0x%04x", i);
1682 			}
1683 			if ((i % 2) == 0) {
1684 				printf(" ");
1685 			}
1686 			if (i < len)
1687 				printf("%02x", p[i] & 0xff);
1688 			else
1689 				printf("  ");
1690 		}
1691 		printf("  |");
1692 		for (i = n; i < n + width; i++) {
1693 			if (i >= len)
1694 				break;
1695 			if (p[i] >= ' ' && p[i] <= '~')
1696 				printf("%c", p[i]);
1697 			else
1698 				printf(".");
1699 		}
1700 		printf("|\n");
1701 	}
1702 	if ((i % width) != 0)
1703 		printf("\n");
1704 }
1705 
1706 void
1707 visdump(char *dp, int datalen, int screenwidth)
1708 {
1709 	int col = 0;
1710 	char *cp;
1711 	int width;
1712 	char visbuf[5];
1713 
1714 	printf("       \"");
1715 	col = 8;
1716 	for (;datalen > 0; datalen--, dp++) {
1717 		vis(visbuf, *dp, VIS_CSTYLE | VIS_NOLOCALE, *(dp+1));
1718 		cp = visbuf;
1719 		/*
1720 		 * Keep track of printables and
1721 		 * space chars (like fold(1)).
1722 		 */
1723 		if (col == 0) {
1724 			putchar('\t');
1725 			col = 8;
1726 		}
1727 		switch(*cp) {
1728 		case '\n':
1729 			col = 0;
1730 			putchar('\n');
1731 			continue;
1732 		case '\t':
1733 			width = 8 - (col&07);
1734 			break;
1735 		default:
1736 			width = strlen(cp);
1737 		}
1738 		if (col + width > (screenwidth-2)) {
1739 			printf("\\\n\t");
1740 			col = 8;
1741 		}
1742 		col += width;
1743 		do {
1744 			putchar(*cp++);
1745 		} while (*cp);
1746 	}
1747 	if (col == 0)
1748 		printf("       ");
1749 	printf("\"\n");
1750 }
1751 
1752 void
1753 ktrgenio(struct ktr_genio *ktr, int len)
1754 {
1755 	int datalen = len - sizeof (struct ktr_genio);
1756 	char *dp = (char *)ktr + sizeof (struct ktr_genio);
1757 	static int screenwidth = 0;
1758 	int i, binary;
1759 
1760 	printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1761 		ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1762 		datalen == 1 ? "" : "s");
1763 	if (suppressdata)
1764 		return;
1765 	if (screenwidth == 0) {
1766 		struct winsize ws;
1767 
1768 		if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1769 		    ws.ws_col > 8)
1770 			screenwidth = ws.ws_col;
1771 		else
1772 			screenwidth = 80;
1773 	}
1774 	if (maxdata && datalen > maxdata)
1775 		datalen = maxdata;
1776 
1777 	for (i = 0, binary = 0; i < datalen && binary == 0; i++)  {
1778 		if (dp[i] >= 32 && dp[i] < 127)
1779 			continue;
1780 		if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1781 			continue;
1782 		binary = 1;
1783 	}
1784 	if (binary)
1785 		hexdump(dp, datalen, screenwidth);
1786 	else
1787 		visdump(dp, datalen, screenwidth);
1788 }
1789 
1790 void
1791 ktrpsig(struct ktr_psig *psig)
1792 {
1793 	const char *str;
1794 
1795 	print_signal(psig->signo);
1796 	if (psig->action == SIG_DFL) {
1797 		printf(" SIG_DFL");
1798 	} else {
1799 		printf(" caught handler=0x%lx mask=0x%x",
1800 		    (u_long)psig->action, psig->mask.__bits[0]);
1801 	}
1802 	printf(" code=");
1803 	str = sysdecode_sigcode(psig->signo, psig->code);
1804 	if (str != NULL)
1805 		printf("%s", str);
1806 	else
1807 		printf("<invalid=%#x>", psig->code);
1808 	putchar('\n');
1809 }
1810 
1811 void
1812 ktrcsw_old(struct ktr_csw_old *cs)
1813 {
1814 	printf("%s %s\n", cs->out ? "stop" : "resume",
1815 		cs->user ? "user" : "kernel");
1816 }
1817 
1818 void
1819 ktrcsw(struct ktr_csw *cs)
1820 {
1821 	printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1822 	    cs->user ? "user" : "kernel", cs->wmesg);
1823 }
1824 
1825 void
1826 ktruser(int len, void *p)
1827 {
1828 	unsigned char *cp;
1829 
1830 	if (sysdecode_utrace(stdout, p, len)) {
1831 		printf("\n");
1832 		return;
1833 	}
1834 
1835 	printf("%d ", len);
1836 	cp = p;
1837 	while (len--)
1838 		if (decimal)
1839 			printf(" %d", *cp++);
1840 		else
1841 			printf(" %02x", *cp++);
1842 	printf("\n");
1843 }
1844 
1845 void
1846 ktrcaprights(cap_rights_t *rightsp)
1847 {
1848 
1849 	printf("cap_rights_t ");
1850 	sysdecode_cap_rights(stdout, rightsp);
1851 	printf("\n");
1852 }
1853 
1854 static void
1855 ktrtimeval(struct timeval *tv)
1856 {
1857 
1858 	printf("{%ld, %ld}", (long)tv->tv_sec, tv->tv_usec);
1859 }
1860 
1861 void
1862 ktritimerval(struct itimerval *it)
1863 {
1864 
1865 	printf("itimerval { .interval = ");
1866 	ktrtimeval(&it->it_interval);
1867 	printf(", .value = ");
1868 	ktrtimeval(&it->it_value);
1869 	printf(" }\n");
1870 }
1871 
1872 void
1873 ktrsockaddr(struct sockaddr *sa)
1874 {
1875 /*
1876  TODO: Support additional address families
1877 	#include <netsmb/netbios.h>
1878 	struct sockaddr_nb	*nb;
1879 */
1880 	const char *str;
1881 	char addr[64];
1882 
1883 	/*
1884 	 * note: ktrstruct() has already verified that sa points to a
1885 	 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1886 	 * sa->sa_len bytes long.
1887 	 */
1888 	printf("struct sockaddr { ");
1889 	str = sysdecode_sockaddr_family(sa->sa_family);
1890 	if (str != NULL)
1891 		printf("%s", str);
1892 	else
1893 		printf("<invalid=%d>", sa->sa_family);
1894 	printf(", ");
1895 
1896 #define check_sockaddr_len(n)					\
1897 	if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) {	\
1898 		printf("invalid");				\
1899 		break;						\
1900 	}
1901 
1902 	switch(sa->sa_family) {
1903 	case AF_INET: {
1904 		struct sockaddr_in sa_in;
1905 
1906 		memset(&sa_in, 0, sizeof(sa_in));
1907 		memcpy(&sa_in, sa, sa->sa_len);
1908 		check_sockaddr_len(in);
1909 		inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1910 		printf("%s:%u", addr, ntohs(sa_in.sin_port));
1911 		break;
1912 	}
1913 	case AF_INET6: {
1914 		struct sockaddr_in6 sa_in6;
1915 
1916 		memset(&sa_in6, 0, sizeof(sa_in6));
1917 		memcpy(&sa_in6, sa, sa->sa_len);
1918 		check_sockaddr_len(in6);
1919 		getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1920 		    addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1921 		printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1922 		break;
1923 	}
1924 	case AF_UNIX: {
1925 		struct sockaddr_un sa_un;
1926 
1927 		memset(&sa_un, 0, sizeof(sa_un));
1928 		memcpy(&sa_un, sa, sa->sa_len);
1929 		printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1930 		break;
1931 	}
1932 	default:
1933 		printf("unknown address family");
1934 	}
1935 	printf(" }\n");
1936 }
1937 
1938 void
1939 ktrstat(struct stat *statp)
1940 {
1941 	char mode[12], timestr[PATH_MAX + 4];
1942 	struct passwd *pwd;
1943 	struct group  *grp;
1944 	struct tm *tm;
1945 
1946 	/*
1947 	 * note: ktrstruct() has already verified that statp points to a
1948 	 * buffer exactly sizeof(struct stat) bytes long.
1949 	 */
1950 	printf("struct stat {");
1951 	printf("dev=%ju, ino=%ju, ",
1952 		(uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1953 	if (!resolv)
1954 		printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1955 	else {
1956 		strmode(statp->st_mode, mode);
1957 		printf("mode=%s, ", mode);
1958 	}
1959 	printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1960 	if (!resolv) {
1961 		pwd = NULL;
1962 	} else {
1963 #ifdef WITH_CASPER
1964 		if (cappwd != NULL)
1965 			pwd = cap_getpwuid(cappwd, statp->st_uid);
1966 		else
1967 #endif
1968 			pwd = getpwuid(statp->st_uid);
1969 	}
1970 	if (pwd == NULL)
1971 		printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1972 	else
1973 		printf("uid=\"%s\", ", pwd->pw_name);
1974 	if (!resolv) {
1975 		grp = NULL;
1976 	} else {
1977 #ifdef WITH_CASPER
1978 		if (capgrp != NULL)
1979 			grp = cap_getgrgid(capgrp, statp->st_gid);
1980 		else
1981 #endif
1982 			grp = getgrgid(statp->st_gid);
1983 	}
1984 	if (grp == NULL)
1985 		printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1986 	else
1987 		printf("gid=\"%s\", ", grp->gr_name);
1988 	printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1989 	printf("atime=");
1990 	if (!resolv)
1991 		printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1992 	else {
1993 		tm = localtime(&statp->st_atim.tv_sec);
1994 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1995 		printf("\"%s\"", timestr);
1996 	}
1997 	if (statp->st_atim.tv_nsec != 0)
1998 		printf(".%09ld, ", statp->st_atim.tv_nsec);
1999 	else
2000 		printf(", ");
2001 	printf("mtime=");
2002 	if (!resolv)
2003 		printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
2004 	else {
2005 		tm = localtime(&statp->st_mtim.tv_sec);
2006 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
2007 		printf("\"%s\"", timestr);
2008 	}
2009 	if (statp->st_mtim.tv_nsec != 0)
2010 		printf(".%09ld, ", statp->st_mtim.tv_nsec);
2011 	else
2012 		printf(", ");
2013 	printf("ctime=");
2014 	if (!resolv)
2015 		printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
2016 	else {
2017 		tm = localtime(&statp->st_ctim.tv_sec);
2018 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
2019 		printf("\"%s\"", timestr);
2020 	}
2021 	if (statp->st_ctim.tv_nsec != 0)
2022 		printf(".%09ld, ", statp->st_ctim.tv_nsec);
2023 	else
2024 		printf(", ");
2025 	printf("birthtime=");
2026 	if (!resolv)
2027 		printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
2028 	else {
2029 		tm = localtime(&statp->st_birthtim.tv_sec);
2030 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
2031 		printf("\"%s\"", timestr);
2032 	}
2033 	if (statp->st_birthtim.tv_nsec != 0)
2034 		printf(".%09ld, ", statp->st_birthtim.tv_nsec);
2035 	else
2036 		printf(", ");
2037 	printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
2038 		(uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
2039 		(intmax_t)statp->st_blocks, statp->st_flags);
2040 	printf(" }\n");
2041 }
2042 
2043 void
2044 ktrbitset(char *name, struct bitset *set, size_t setlen)
2045 {
2046 	int i, maxi, c = 0;
2047 
2048 	if (setlen > INT32_MAX)
2049 		setlen = INT32_MAX;
2050 	maxi = setlen * CHAR_BIT;
2051 	printf("%s [ ", name);
2052 	for (i = 0; i < maxi; i++) {
2053 		if (!BIT_ISSET(setlen, i, set))
2054 			continue;
2055 		if (c == 0)
2056 			printf("%d", i);
2057 		else
2058 			printf(", %d", i);
2059 		c++;
2060 	}
2061 	if (c == 0)
2062 		printf(" empty ]\n");
2063 	else
2064 		printf(" ]\n");
2065 }
2066 
2067 void
2068 ktrstruct(char *buf, size_t buflen)
2069 {
2070 	char *name, *data;
2071 	size_t namelen, datalen;
2072 	int i;
2073 	cap_rights_t rights;
2074 	struct itimerval it;
2075 	struct stat sb;
2076 	struct sockaddr_storage ss;
2077 	struct bitset *set;
2078 
2079 	for (name = buf, namelen = 0;
2080 	     namelen < buflen && name[namelen] != '\0';
2081 	     ++namelen)
2082 		/* nothing */;
2083 	if (namelen == buflen)
2084 		goto invalid;
2085 	if (name[namelen] != '\0')
2086 		goto invalid;
2087 	data = buf + namelen + 1;
2088 	datalen = buflen - namelen - 1;
2089 	if (datalen == 0)
2090 		goto invalid;
2091 	/* sanity check */
2092 	for (i = 0; i < (int)namelen; ++i)
2093 		if (!isalpha(name[i]) && name[i] != '_')
2094 			goto invalid;
2095 	if (strcmp(name, "caprights") == 0) {
2096 		if (datalen != sizeof(cap_rights_t))
2097 			goto invalid;
2098 		memcpy(&rights, data, datalen);
2099 		ktrcaprights(&rights);
2100 	} else if (strcmp(name, "itimerval") == 0) {
2101 		if (datalen != sizeof(struct itimerval))
2102 			goto invalid;
2103 		memcpy(&it, data, datalen);
2104 		ktritimerval(&it);
2105 	} else if (strcmp(name, "stat") == 0) {
2106 		if (datalen != sizeof(struct stat))
2107 			goto invalid;
2108 		memcpy(&sb, data, datalen);
2109 		ktrstat(&sb);
2110 	} else if (strcmp(name, "sockaddr") == 0) {
2111 		if (datalen > sizeof(ss))
2112 			goto invalid;
2113 		memcpy(&ss, data, datalen);
2114 		if (datalen != ss.ss_len)
2115 			goto invalid;
2116 		ktrsockaddr((struct sockaddr *)&ss);
2117 	} else if (strcmp(name, "cpuset_t") == 0) {
2118 		if (datalen < 1)
2119 			goto invalid;
2120 		set = malloc(datalen);
2121 		if (set == NULL)
2122 			errx(1, "%s", strerror(ENOMEM));
2123 		memcpy(set, data, datalen);
2124 		ktrbitset(name, set, datalen);
2125 		free(set);
2126 	} else {
2127 #ifdef SYSDECODE_HAVE_LINUX
2128 		if (ktrstruct_linux(name, data, datalen) == false)
2129 #endif
2130 			printf("unknown structure\n");
2131 	}
2132 	return;
2133 invalid:
2134 	printf("invalid record\n");
2135 }
2136 
2137 void
2138 ktrcapfail(struct ktr_cap_fail *ktr)
2139 {
2140 	switch (ktr->cap_type) {
2141 	case CAPFAIL_NOTCAPABLE:
2142 		/* operation on fd with insufficient capabilities */
2143 		printf("operation requires ");
2144 		sysdecode_cap_rights(stdout, &ktr->cap_needed);
2145 		printf(", descriptor holds ");
2146 		sysdecode_cap_rights(stdout, &ktr->cap_held);
2147 		break;
2148 	case CAPFAIL_INCREASE:
2149 		/* requested more capabilities than fd already has */
2150 		printf("attempt to increase capabilities from ");
2151 		sysdecode_cap_rights(stdout, &ktr->cap_held);
2152 		printf(" to ");
2153 		sysdecode_cap_rights(stdout, &ktr->cap_needed);
2154 		break;
2155 	case CAPFAIL_SYSCALL:
2156 		/* called restricted syscall */
2157 		printf("disallowed system call");
2158 		break;
2159 	case CAPFAIL_LOOKUP:
2160 		/* absolute or AT_FDCWD path, ".." path, etc. */
2161 		printf("restricted VFS lookup");
2162 		break;
2163 	default:
2164 		printf("unknown capability failure: ");
2165 		sysdecode_cap_rights(stdout, &ktr->cap_needed);
2166 		printf(" ");
2167 		sysdecode_cap_rights(stdout, &ktr->cap_held);
2168 		break;
2169 	}
2170 	printf("\n");
2171 }
2172 
2173 void
2174 ktrfault(struct ktr_fault *ktr)
2175 {
2176 
2177 	printf("0x%jx ", (uintmax_t)ktr->vaddr);
2178 	print_mask_arg(sysdecode_vmprot, ktr->type);
2179 	printf("\n");
2180 }
2181 
2182 void
2183 ktrfaultend(struct ktr_faultend *ktr)
2184 {
2185 	const char *str;
2186 
2187 	str = sysdecode_vmresult(ktr->result);
2188 	if (str != NULL)
2189 		printf("%s", str);
2190 	else
2191 		printf("<invalid=%d>", ktr->result);
2192 	printf("\n");
2193 }
2194 
2195 void
2196 ktrkevent(struct kevent *kev)
2197 {
2198 
2199 	printf("{ ident=");
2200 	switch (kev->filter) {
2201 	case EVFILT_READ:
2202 	case EVFILT_WRITE:
2203 	case EVFILT_VNODE:
2204 	case EVFILT_PROC:
2205 	case EVFILT_TIMER:
2206 	case EVFILT_PROCDESC:
2207 	case EVFILT_EMPTY:
2208 		printf("%ju", (uintmax_t)kev->ident);
2209 		break;
2210 	case EVFILT_SIGNAL:
2211 		print_signal(kev->ident);
2212 		break;
2213 	default:
2214 		printf("%p", (void *)kev->ident);
2215 	}
2216 	printf(", filter=");
2217 	print_integer_arg(sysdecode_kevent_filter, kev->filter);
2218 	printf(", flags=");
2219 	print_mask_arg0(sysdecode_kevent_flags, kev->flags);
2220 	printf(", fflags=");
2221 	sysdecode_kevent_fflags(stdout, kev->filter, kev->fflags,
2222 	    decimal ? 10 : 16);
2223 	printf(", data=%#jx, udata=%p }", (uintmax_t)kev->data, kev->udata);
2224 }
2225 
2226 void
2227 ktrstructarray(struct ktr_struct_array *ksa, size_t buflen)
2228 {
2229 	struct kevent kev;
2230 	char *name, *data;
2231 	size_t namelen, datalen;
2232 	int i;
2233 	bool first;
2234 
2235 	buflen -= sizeof(*ksa);
2236 	for (name = (char *)(ksa + 1), namelen = 0;
2237 	     namelen < buflen && name[namelen] != '\0';
2238 	     ++namelen)
2239 		/* nothing */;
2240 	if (namelen == buflen)
2241 		goto invalid;
2242 	if (name[namelen] != '\0')
2243 		goto invalid;
2244 	/* sanity check */
2245 	for (i = 0; i < (int)namelen; ++i)
2246 		if (!isalnum(name[i]) && name[i] != '_')
2247 			goto invalid;
2248 	data = name + namelen + 1;
2249 	datalen = buflen - namelen - 1;
2250 	printf("struct %s[] = { ", name);
2251 	first = true;
2252 	for (; datalen >= ksa->struct_size;
2253 	    data += ksa->struct_size, datalen -= ksa->struct_size) {
2254 		if (!first)
2255 			printf("\n             ");
2256 		else
2257 			first = false;
2258 		if (strcmp(name, "kevent") == 0) {
2259 			if (ksa->struct_size != sizeof(kev))
2260 				goto bad_size;
2261 			memcpy(&kev, data, sizeof(kev));
2262 			ktrkevent(&kev);
2263 		} else if (strcmp(name, "freebsd11_kevent") == 0) {
2264 			struct freebsd11_kevent kev11;
2265 
2266 			if (ksa->struct_size != sizeof(kev11))
2267 				goto bad_size;
2268 			memcpy(&kev11, data, sizeof(kev11));
2269 			memset(&kev, 0, sizeof(kev));
2270 			kev.ident = kev11.ident;
2271 			kev.filter = kev11.filter;
2272 			kev.flags = kev11.flags;
2273 			kev.fflags = kev11.fflags;
2274 			kev.data = kev11.data;
2275 			kev.udata = kev11.udata;
2276 			ktrkevent(&kev);
2277 #ifdef _WANT_KEVENT32
2278 		} else if (strcmp(name, "kevent32") == 0) {
2279 			struct kevent32 kev32;
2280 
2281 			if (ksa->struct_size != sizeof(kev32))
2282 				goto bad_size;
2283 			memcpy(&kev32, data, sizeof(kev32));
2284 			memset(&kev, 0, sizeof(kev));
2285 			kev.ident = kev32.ident;
2286 			kev.filter = kev32.filter;
2287 			kev.flags = kev32.flags;
2288 			kev.fflags = kev32.fflags;
2289 #if BYTE_ORDER == BIG_ENDIAN
2290 			kev.data = kev32.data2 | ((int64_t)kev32.data1 << 32);
2291 #else
2292 			kev.data = kev32.data1 | ((int64_t)kev32.data2 << 32);
2293 #endif
2294 			kev.udata = (void *)(uintptr_t)kev32.udata;
2295 			ktrkevent(&kev);
2296 		} else if (strcmp(name, "freebsd11_kevent32") == 0) {
2297 			struct freebsd11_kevent32 kev32;
2298 
2299 			if (ksa->struct_size != sizeof(kev32))
2300 				goto bad_size;
2301 			memcpy(&kev32, data, sizeof(kev32));
2302 			memset(&kev, 0, sizeof(kev));
2303 			kev.ident = kev32.ident;
2304 			kev.filter = kev32.filter;
2305 			kev.flags = kev32.flags;
2306 			kev.fflags = kev32.fflags;
2307 			kev.data = kev32.data;
2308 			kev.udata = (void *)(uintptr_t)kev32.udata;
2309 			ktrkevent(&kev);
2310 #endif
2311 		} else {
2312 			printf("<unknown structure> }\n");
2313 			return;
2314 		}
2315 	}
2316 	printf(" }\n");
2317 	return;
2318 invalid:
2319 	printf("invalid record\n");
2320 	return;
2321 bad_size:
2322 	printf("<bad size> }\n");
2323 	return;
2324 }
2325 
2326 void
2327 usage(void)
2328 {
2329 	fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
2330 	    "[-m maxdata] [-p pid] [-t trstr]\n");
2331 	exit(1);
2332 }
2333