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