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