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