xref: /freebsd/usr.bin/kdump/kdump.c (revision fe50a38eb029e6e551fef9f6cf6ffa515f1897fa)
1 /*-
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1988, 1993\n\
33 	The Regents of the University of California.  All rights reserved.\n";
34 #endif /* not lint */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)kdump.c	8.1 (Berkeley) 6/6/93";
39 #endif
40 #endif /* not lint */
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #define _KERNEL
45 extern int errno;
46 #include <sys/errno.h>
47 #undef _KERNEL
48 #include <sys/param.h>
49 #include <sys/capsicum.h>
50 #include <sys/errno.h>
51 #define _KERNEL
52 #include <sys/time.h>
53 #undef _KERNEL
54 #include <sys/uio.h>
55 #include <sys/ktrace.h>
56 #include <sys/ioctl.h>
57 #include <sys/socket.h>
58 #include <sys/stat.h>
59 #include <sys/sysent.h>
60 #include <sys/umtx.h>
61 #include <sys/un.h>
62 #include <sys/queue.h>
63 #include <sys/wait.h>
64 #ifdef HAVE_LIBCAPSICUM
65 #include <sys/nv.h>
66 #endif
67 #include <arpa/inet.h>
68 #include <netinet/in.h>
69 #include <ctype.h>
70 #include <err.h>
71 #include <grp.h>
72 #include <inttypes.h>
73 #ifdef HAVE_LIBCAPSICUM
74 #include <libcapsicum.h>
75 #include <libcapsicum_grp.h>
76 #include <libcapsicum_pwd.h>
77 #include <libcapsicum_service.h>
78 #endif
79 #include <locale.h>
80 #include <netdb.h>
81 #include <nl_types.h>
82 #include <pwd.h>
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <termios.h>
87 #include <time.h>
88 #include <unistd.h>
89 #include <vis.h>
90 #include "ktrace.h"
91 #include "kdump_subr.h"
92 
93 u_int abidump(struct ktr_header *);
94 int fetchprocinfo(struct ktr_header *, u_int *);
95 int fread_tail(void *, int, int);
96 void dumpheader(struct ktr_header *);
97 void ktrsyscall(struct ktr_syscall *, u_int);
98 void ktrsysret(struct ktr_sysret *, u_int);
99 void ktrnamei(char *, int);
100 void hexdump(char *, int, int);
101 void visdump(char *, int, int);
102 void ktrgenio(struct ktr_genio *, int);
103 void ktrpsig(struct ktr_psig *);
104 void ktrcsw(struct ktr_csw *);
105 void ktrcsw_old(struct ktr_csw_old *);
106 void ktruser_malloc(void *);
107 void ktruser_rtld(int, void *);
108 void ktruser(int, void *);
109 void ktrcaprights(cap_rights_t *);
110 void ktrsockaddr(struct sockaddr *);
111 void ktrstat(struct stat *);
112 void ktrstruct(char *, size_t);
113 void ktrcapfail(struct ktr_cap_fail *);
114 void ktrfault(struct ktr_fault *);
115 void ktrfaultend(struct ktr_faultend *);
116 void limitfd(int fd);
117 void usage(void);
118 void ioctlname(unsigned long, int);
119 int kdump_print_utrace(FILE *, void *, size_t, int);
120 
121 #define	TIMESTAMP_NONE		0x0
122 #define	TIMESTAMP_ABSOLUTE	0x1
123 #define	TIMESTAMP_ELAPSED	0x2
124 #define	TIMESTAMP_RELATIVE	0x4
125 
126 extern const char *signames[], *syscallnames[];
127 extern int nsyscalls;
128 
129 static int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
130     resolv = 0, abiflag = 0, syscallno = 0;
131 static const char *tracefile = DEF_TRACEFILE;
132 static struct ktr_header ktr_header;
133 
134 #define TIME_FORMAT	"%b %e %T %Y"
135 #define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
136 
137 #define print_number(i,n,c) do {					\
138 	if (decimal)							\
139 		printf("%c%jd", c, (intmax_t)*i);			\
140 	else								\
141 		printf("%c%#jx", c, (uintmax_t)(u_register_t)*i);	\
142 	i++;								\
143 	n--;								\
144 	c = ',';							\
145 } while (0)
146 
147 #if defined(__amd64__) || defined(__i386__)
148 
149 void linux_ktrsyscall(struct ktr_syscall *, u_int);
150 void linux_ktrsysret(struct ktr_sysret *, u_int);
151 extern const char *linux_syscallnames[];
152 
153 #include <linux_syscalls.c>
154 
155 /*
156  * from linux.h
157  * Linux syscalls return negative errno's, we do positive and map them
158  */
159 static int bsd_to_linux_errno[ELAST + 1] = {
160 	-0,  -1,  -2,  -3,  -4,  -5,  -6,  -7,  -8,  -9,
161 	-10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
162 	-20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
163 	-30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
164 	-90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
165 	-100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
166 	-110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
167 	-116, -66,  -6,  -6,  -6,  -6,  -6, -37, -38,  -9,
168 	-6,  -6, -43, -42, -75,-125, -84, -95, -16, -74,
169 	-72, -67, -71
170 };
171 #endif
172 
173 #if defined(__amd64__)
174 extern const char *linux32_syscallnames[];
175 
176 #include <linux32_syscalls.c>
177 #endif
178 
179 struct proc_info
180 {
181 	TAILQ_ENTRY(proc_info)	info;
182 	u_int			sv_flags;
183 	pid_t			pid;
184 };
185 
186 static TAILQ_HEAD(trace_procs, proc_info) trace_procs;
187 
188 #ifdef HAVE_LIBCAPSICUM
189 static cap_channel_t *cappwd, *capgrp;
190 #endif
191 
192 static void
193 strerror_init(void)
194 {
195 
196 	/*
197 	 * Cache NLS data before entering capability mode.
198 	 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
199 	 */
200 	(void)catopen("libc", NL_CAT_LOCALE);
201 }
202 
203 static void
204 localtime_init(void)
205 {
206 	time_t ltime;
207 
208 	/*
209 	 * Allow localtime(3) to cache /etc/localtime content before entering
210 	 * capability mode.
211 	 * XXXPJD: There should be localtime_init() in libc.
212 	 */
213 	(void)time(&ltime);
214 	(void)localtime(&ltime);
215 }
216 
217 #ifdef HAVE_LIBCAPSICUM
218 static int
219 cappwdgrp_setup(cap_channel_t **cappwdp, cap_channel_t **capgrpp)
220 {
221 	cap_channel_t *capcas, *cappwdloc, *capgrploc;
222 	const char *cmds[1], *fields[1];
223 
224 	capcas = cap_init();
225 	if (capcas == NULL) {
226 		warn("unable to contact casperd");
227 		return (-1);
228 	}
229 	cappwdloc = cap_service_open(capcas, "system.pwd");
230 	capgrploc = cap_service_open(capcas, "system.grp");
231 	/* Casper capability no longer needed. */
232 	cap_close(capcas);
233 	if (cappwdloc == NULL || capgrploc == NULL) {
234 		if (cappwdloc == NULL)
235 			warn("unable to open system.pwd service");
236 		if (capgrploc == NULL)
237 			warn("unable to open system.grp service");
238 		exit(1);
239 	}
240 	/* Limit system.pwd to only getpwuid() function and pw_name field. */
241 	cmds[0] = "getpwuid";
242 	if (cap_pwd_limit_cmds(cappwdloc, cmds, 1) < 0)
243 		err(1, "unable to limit system.pwd service");
244 	fields[0] = "pw_name";
245 	if (cap_pwd_limit_fields(cappwdloc, fields, 1) < 0)
246 		err(1, "unable to limit system.pwd service");
247 	/* Limit system.grp to only getgrgid() function and gr_name field. */
248 	cmds[0] = "getgrgid";
249 	if (cap_grp_limit_cmds(capgrploc, cmds, 1) < 0)
250 		err(1, "unable to limit system.grp service");
251 	fields[0] = "gr_name";
252 	if (cap_grp_limit_fields(capgrploc, fields, 1) < 0)
253 		err(1, "unable to limit system.grp service");
254 
255 	*cappwdp = cappwdloc;
256 	*capgrpp = capgrploc;
257 	return (0);
258 }
259 #endif	/* HAVE_LIBCAPSICUM */
260 
261 int
262 main(int argc, char *argv[])
263 {
264 	int ch, ktrlen, size;
265 	void *m;
266 	int trpoints = ALL_POINTS;
267 	int drop_logged;
268 	pid_t pid = 0;
269 	u_int sv_flags;
270 
271 	setlocale(LC_CTYPE, "");
272 
273 	timestamp = TIMESTAMP_NONE;
274 
275 	while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1)
276 		switch (ch) {
277 		case 'A':
278 			abiflag = 1;
279 			break;
280 		case 'f':
281 			tracefile = optarg;
282 			break;
283 		case 'd':
284 			decimal = 1;
285 			break;
286 		case 'l':
287 			tail = 1;
288 			break;
289 		case 'm':
290 			maxdata = atoi(optarg);
291 			break;
292 		case 'n':
293 			fancy = 0;
294 			break;
295 		case 'p':
296 			pid = atoi(optarg);
297 			break;
298 		case 'r':
299 			resolv = 1;
300 			break;
301 		case 'S':
302 			syscallno = 1;
303 			break;
304 		case 's':
305 			suppressdata = 1;
306 			break;
307 		case 'E':
308 			timestamp |= TIMESTAMP_ELAPSED;
309 			break;
310 		case 'H':
311 			threads = 1;
312 			break;
313 		case 'R':
314 			timestamp |= TIMESTAMP_RELATIVE;
315 			break;
316 		case 'T':
317 			timestamp |= TIMESTAMP_ABSOLUTE;
318 			break;
319 		case 't':
320 			trpoints = getpoints(optarg);
321 			if (trpoints < 0)
322 				errx(1, "unknown trace point in %s", optarg);
323 			break;
324 		default:
325 			usage();
326 		}
327 
328 	if (argc > optind)
329 		usage();
330 
331 	m = malloc(size = 1025);
332 	if (m == NULL)
333 		errx(1, "%s", strerror(ENOMEM));
334 	if (!freopen(tracefile, "r", stdin))
335 		err(1, "%s", tracefile);
336 
337 	strerror_init();
338 	localtime_init();
339 #ifdef HAVE_LIBCAPSICUM
340 	if (resolv != 0) {
341 		if (cappwdgrp_setup(&cappwd, &capgrp) < 0) {
342 			cappwd = NULL;
343 			capgrp = NULL;
344 		}
345 	}
346 	if (resolv == 0 || (cappwd != NULL && capgrp != NULL)) {
347 		if (cap_enter() < 0 && errno != ENOSYS)
348 			err(1, "unable to enter capability mode");
349 	}
350 #else
351 	if (resolv == 0) {
352 		if (cap_enter() < 0 && errno != ENOSYS)
353 			err(1, "unable to enter capability mode");
354 	}
355 #endif
356 	limitfd(STDIN_FILENO);
357 	limitfd(STDOUT_FILENO);
358 	limitfd(STDERR_FILENO);
359 
360 	TAILQ_INIT(&trace_procs);
361 	drop_logged = 0;
362 	while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
363 		if (ktr_header.ktr_type & KTR_DROP) {
364 			ktr_header.ktr_type &= ~KTR_DROP;
365 			if (!drop_logged && threads) {
366 				printf(
367 				    "%6jd %6jd %-8.*s Events dropped.\n",
368 				    (intmax_t)ktr_header.ktr_pid,
369 				    ktr_header.ktr_tid > 0 ?
370 				    (intmax_t)ktr_header.ktr_tid : 0,
371 				    MAXCOMLEN, ktr_header.ktr_comm);
372 				drop_logged = 1;
373 			} else if (!drop_logged) {
374 				printf("%6jd %-8.*s Events dropped.\n",
375 				    (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
376 				    ktr_header.ktr_comm);
377 				drop_logged = 1;
378 			}
379 		}
380 		if (trpoints & (1<<ktr_header.ktr_type))
381 			if (pid == 0 || ktr_header.ktr_pid == pid ||
382 			    ktr_header.ktr_tid == pid)
383 				dumpheader(&ktr_header);
384 		if ((ktrlen = ktr_header.ktr_len) < 0)
385 			errx(1, "bogus length 0x%x", ktrlen);
386 		if (ktrlen > size) {
387 			m = realloc(m, ktrlen+1);
388 			if (m == NULL)
389 				errx(1, "%s", strerror(ENOMEM));
390 			size = ktrlen;
391 		}
392 		if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
393 			errx(1, "data too short");
394 		if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
395 			continue;
396 		sv_flags = abidump(&ktr_header);
397 		if (pid && ktr_header.ktr_pid != pid &&
398 		    ktr_header.ktr_tid != pid)
399 			continue;
400 		if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
401 			continue;
402 		drop_logged = 0;
403 		switch (ktr_header.ktr_type) {
404 		case KTR_SYSCALL:
405 #if defined(__amd64__) || defined(__i386__)
406 			if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
407 				linux_ktrsyscall((struct ktr_syscall *)m,
408 				    sv_flags);
409 			else
410 #endif
411 				ktrsyscall((struct ktr_syscall *)m, sv_flags);
412 			break;
413 		case KTR_SYSRET:
414 #if defined(__amd64__) || defined(__i386__)
415 			if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
416 				linux_ktrsysret((struct ktr_sysret *)m,
417 				    sv_flags);
418 			else
419 #endif
420 				ktrsysret((struct ktr_sysret *)m, sv_flags);
421 			break;
422 		case KTR_NAMEI:
423 		case KTR_SYSCTL:
424 			ktrnamei(m, ktrlen);
425 			break;
426 		case KTR_GENIO:
427 			ktrgenio((struct ktr_genio *)m, ktrlen);
428 			break;
429 		case KTR_PSIG:
430 			ktrpsig((struct ktr_psig *)m);
431 			break;
432 		case KTR_CSW:
433 			if (ktrlen == sizeof(struct ktr_csw_old))
434 				ktrcsw_old((struct ktr_csw_old *)m);
435 			else
436 				ktrcsw((struct ktr_csw *)m);
437 			break;
438 		case KTR_USER:
439 			ktruser(ktrlen, m);
440 			break;
441 		case KTR_STRUCT:
442 			ktrstruct(m, ktrlen);
443 			break;
444 		case KTR_CAPFAIL:
445 			ktrcapfail((struct ktr_cap_fail *)m);
446 			break;
447 		case KTR_FAULT:
448 			ktrfault((struct ktr_fault *)m);
449 			break;
450 		case KTR_FAULTEND:
451 			ktrfaultend((struct ktr_faultend *)m);
452 			break;
453 		default:
454 			printf("\n");
455 			break;
456 		}
457 		if (tail)
458 			fflush(stdout);
459 	}
460 	return 0;
461 }
462 
463 void
464 limitfd(int fd)
465 {
466 	cap_rights_t rights;
467 	unsigned long cmd;
468 
469 	cap_rights_init(&rights, CAP_FSTAT);
470 	cmd = 0;
471 
472 	switch (fd) {
473 	case STDIN_FILENO:
474 		cap_rights_set(&rights, CAP_READ);
475 		break;
476 	case STDOUT_FILENO:
477 		cap_rights_set(&rights, CAP_IOCTL, CAP_WRITE);
478 		cmd = TIOCGETA;	/* required by isatty(3) in printf(3) */
479 		break;
480 	case STDERR_FILENO:
481 		cap_rights_set(&rights, CAP_WRITE);
482 		if (!suppressdata) {
483 			cap_rights_set(&rights, CAP_IOCTL);
484 			cmd = TIOCGWINSZ;
485 		}
486 		break;
487 	default:
488 		abort();
489 	}
490 
491 	if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
492 		err(1, "unable to limit rights for descriptor %d", fd);
493 	if (cmd != 0 && cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
494 		err(1, "unable to limit ioctls for descriptor %d", fd);
495 }
496 
497 int
498 fread_tail(void *buf, int size, int num)
499 {
500 	int i;
501 
502 	while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
503 		sleep(1);
504 		clearerr(stdin);
505 	}
506 	return (i);
507 }
508 
509 int
510 fetchprocinfo(struct ktr_header *kth, u_int *flags)
511 {
512 	struct proc_info *pi;
513 
514 	switch (kth->ktr_type) {
515 	case KTR_PROCCTOR:
516 		TAILQ_FOREACH(pi, &trace_procs, info) {
517 			if (pi->pid == kth->ktr_pid) {
518 				TAILQ_REMOVE(&trace_procs, pi, info);
519 				break;
520 			}
521 		}
522 		pi = malloc(sizeof(struct proc_info));
523 		if (pi == NULL)
524 			errx(1, "%s", strerror(ENOMEM));
525 		pi->sv_flags = *flags;
526 		pi->pid = kth->ktr_pid;
527 		TAILQ_INSERT_TAIL(&trace_procs, pi, info);
528 		return (1);
529 
530 	case KTR_PROCDTOR:
531 		TAILQ_FOREACH(pi, &trace_procs, info) {
532 			if (pi->pid == kth->ktr_pid) {
533 				TAILQ_REMOVE(&trace_procs, pi, info);
534 				free(pi);
535 				break;
536 			}
537 		}
538 		return (1);
539 	}
540 
541 	return (0);
542 }
543 
544 u_int
545 abidump(struct ktr_header *kth)
546 {
547 	struct proc_info *pi;
548 	const char *abi;
549 	const char *arch;
550 	u_int flags = 0;
551 
552 	TAILQ_FOREACH(pi, &trace_procs, info) {
553 		if (pi->pid == kth->ktr_pid) {
554 			flags = pi->sv_flags;
555 			break;
556 		}
557 	}
558 
559 	if (abiflag == 0)
560 		return (flags);
561 
562 	switch (flags & SV_ABI_MASK) {
563 	case SV_ABI_LINUX:
564 		abi = "L";
565 		break;
566 	case SV_ABI_FREEBSD:
567 		abi = "F";
568 		break;
569 	default:
570 		abi = "U";
571 		break;
572 	}
573 
574 	if (flags != 0) {
575 		if (flags & SV_LP64)
576 			arch = "64";
577 		else
578 			arch = "32";
579 	} else
580 		arch = "00";
581 
582 	printf("%s%s  ", abi, arch);
583 
584 	return (flags);
585 }
586 
587 void
588 dumpheader(struct ktr_header *kth)
589 {
590 	static char unknown[64];
591 	static struct timeval prevtime, prevtime_e, temp;
592 	const char *type;
593 	const char *sign;
594 
595 	switch (kth->ktr_type) {
596 	case KTR_SYSCALL:
597 		type = "CALL";
598 		break;
599 	case KTR_SYSRET:
600 		type = "RET ";
601 		break;
602 	case KTR_NAMEI:
603 		type = "NAMI";
604 		break;
605 	case KTR_GENIO:
606 		type = "GIO ";
607 		break;
608 	case KTR_PSIG:
609 		type = "PSIG";
610 		break;
611 	case KTR_CSW:
612 		type = "CSW ";
613 		break;
614 	case KTR_USER:
615 		type = "USER";
616 		break;
617 	case KTR_STRUCT:
618 		type = "STRU";
619 		break;
620 	case KTR_SYSCTL:
621 		type = "SCTL";
622 		break;
623 	case KTR_PROCCTOR:
624 		/* FALLTHROUGH */
625 	case KTR_PROCDTOR:
626 		return;
627 	case KTR_CAPFAIL:
628 		type = "CAP ";
629 		break;
630 	case KTR_FAULT:
631 		type = "PFLT";
632 		break;
633 	case KTR_FAULTEND:
634 		type = "PRET";
635 		break;
636 	default:
637 		sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
638 		type = unknown;
639 	}
640 
641 	/*
642 	 * The ktr_tid field was previously the ktr_buffer field, which held
643 	 * the kernel pointer value for the buffer associated with data
644 	 * following the record header.  It now holds a threadid, but only
645 	 * for trace files after the change.  Older trace files still contain
646 	 * kernel pointers.  Detect this and suppress the results by printing
647 	 * negative tid's as 0.
648 	 */
649 	if (threads)
650 		printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
651 		    kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
652 		    MAXCOMLEN, kth->ktr_comm);
653 	else
654 		printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
655 		    kth->ktr_comm);
656         if (timestamp) {
657 		if (timestamp & TIMESTAMP_ABSOLUTE) {
658 			printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
659 			    kth->ktr_time.tv_usec);
660 		}
661 		if (timestamp & TIMESTAMP_ELAPSED) {
662 			if (prevtime_e.tv_sec == 0)
663 				prevtime_e = kth->ktr_time;
664 			timevalsub(&kth->ktr_time, &prevtime_e);
665 			printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
666 			    kth->ktr_time.tv_usec);
667 			timevaladd(&kth->ktr_time, &prevtime_e);
668 		}
669 		if (timestamp & TIMESTAMP_RELATIVE) {
670 			if (prevtime.tv_sec == 0)
671 				prevtime = kth->ktr_time;
672 			temp = kth->ktr_time;
673 			timevalsub(&kth->ktr_time, &prevtime);
674 			if ((intmax_t)kth->ktr_time.tv_sec < 0) {
675                         	kth->ktr_time = prevtime;
676 				prevtime = temp;
677 				timevalsub(&kth->ktr_time, &prevtime);
678 				sign = "-";
679 			} else {
680 				prevtime = temp;
681 				sign = "";
682 			}
683 			printf("%s%jd.%06ld ", sign, (intmax_t)kth->ktr_time.tv_sec,
684 			    kth->ktr_time.tv_usec);
685 		}
686 	}
687 	printf("%s  ", type);
688 }
689 
690 #include <sys/syscall.h>
691 #define KTRACE
692 #include <sys/kern/syscalls.c>
693 #undef KTRACE
694 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
695 
696 void
697 ktrsyscall(struct ktr_syscall *ktr, u_int flags)
698 {
699 	int narg = ktr->ktr_narg;
700 	register_t *ip;
701 	intmax_t arg;
702 
703 	if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
704 	    (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0))
705 		printf("[%d]", ktr->ktr_code);
706 	else {
707 		printf("%s", syscallnames[ktr->ktr_code]);
708 		if (syscallno)
709 			printf("[%d]", ktr->ktr_code);
710 	}
711 	ip = &ktr->ktr_args[0];
712 	if (narg) {
713 		char c = '(';
714 		if (fancy &&
715 		    (flags == 0 || (flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
716 			switch (ktr->ktr_code) {
717 			case SYS_bindat:
718 			case SYS_connectat:
719 			case SYS_faccessat:
720 			case SYS_fchmodat:
721 			case SYS_fchownat:
722 			case SYS_fstatat:
723 			case SYS_futimesat:
724 			case SYS_linkat:
725 			case SYS_mkdirat:
726 			case SYS_mkfifoat:
727 			case SYS_mknodat:
728 			case SYS_openat:
729 			case SYS_readlinkat:
730 			case SYS_renameat:
731 			case SYS_unlinkat:
732 			case SYS_utimensat:
733 				putchar('(');
734 				atfdname(*ip, decimal);
735 				c = ',';
736 				ip++;
737 				narg--;
738 				break;
739 			}
740 			switch (ktr->ktr_code) {
741 			case SYS_ioctl: {
742 				print_number(ip, narg, c);
743 				putchar(c);
744 				ioctlname(*ip, decimal);
745 				c = ',';
746 				ip++;
747 				narg--;
748 				break;
749 			}
750 			case SYS_ptrace:
751 				putchar('(');
752 				ptraceopname(*ip);
753 				c = ',';
754 				ip++;
755 				narg--;
756 				break;
757 			case SYS_access:
758 			case SYS_eaccess:
759 			case SYS_faccessat:
760 				print_number(ip, narg, c);
761 				putchar(',');
762 				accessmodename(*ip);
763 				ip++;
764 				narg--;
765 				break;
766 			case SYS_open:
767 			case SYS_openat:
768 				print_number(ip, narg, c);
769 				putchar(',');
770 				flagsandmodename(ip[0], ip[1], decimal);
771 				ip += 2;
772 				narg -= 2;
773 				break;
774 			case SYS_wait4:
775 				print_number(ip, narg, c);
776 				print_number(ip, narg, c);
777 				/*
778 				 * A flags value of zero is valid for
779 				 * wait4() but not for wait6(), so
780 				 * handle zero special here.
781 				 */
782 				if (*ip == 0) {
783 					print_number(ip, narg, c);
784 				} else {
785 					putchar(',');
786 					wait6optname(*ip);
787 					ip++;
788 					narg--;
789 				}
790 				break;
791 			case SYS_wait6:
792 				putchar('(');
793 				idtypename(*ip, decimal);
794 				c = ',';
795 				ip++;
796 				narg--;
797 				print_number(ip, narg, c);
798 				print_number(ip, narg, c);
799 				putchar(',');
800 				wait6optname(*ip);
801 				ip++;
802 				narg--;
803 				break;
804 			case SYS_chmod:
805 			case SYS_fchmod:
806 			case SYS_lchmod:
807 				print_number(ip, narg, c);
808 				putchar(',');
809 				modename(*ip);
810 				ip++;
811 				narg--;
812 				break;
813 			case SYS_mknod:
814 			case SYS_mknodat:
815 				print_number(ip, narg, c);
816 				putchar(',');
817 				modename(*ip);
818 				ip++;
819 				narg--;
820 				break;
821 			case SYS_getfsstat:
822 				print_number(ip, narg, c);
823 				print_number(ip, narg, c);
824 				putchar(',');
825 				getfsstatflagsname(*ip);
826 				ip++;
827 				narg--;
828 				break;
829 			case SYS_mount:
830 				print_number(ip, narg, c);
831 				print_number(ip, narg, c);
832 				putchar(',');
833 				mountflagsname(*ip);
834 				ip++;
835 				narg--;
836 				break;
837 			case SYS_unmount:
838 				print_number(ip, narg, c);
839 				putchar(',');
840 				mountflagsname(*ip);
841 				ip++;
842 				narg--;
843 				break;
844 			case SYS_recvmsg:
845 			case SYS_sendmsg:
846 				print_number(ip, narg, c);
847 				print_number(ip, narg, c);
848 				putchar(',');
849 				sendrecvflagsname(*ip);
850 				ip++;
851 				narg--;
852 				break;
853 			case SYS_recvfrom:
854 			case SYS_sendto:
855 				print_number(ip, narg, c);
856 				print_number(ip, narg, c);
857 				print_number(ip, narg, c);
858 				putchar(',');
859 				sendrecvflagsname(*ip);
860 				ip++;
861 				narg--;
862 				break;
863 			case SYS_chflags:
864 			case SYS_fchflags:
865 			case SYS_lchflags:
866 				print_number(ip, narg, c);
867 				putchar(',');
868 				modename(*ip);
869 				ip++;
870 				narg--;
871 				break;
872 			case SYS_kill:
873 				print_number(ip, narg, c);
874 				putchar(',');
875 				signame(*ip);
876 				ip++;
877 				narg--;
878 				break;
879 			case SYS_reboot:
880 				putchar('(');
881 				rebootoptname(*ip);
882 				ip++;
883 				narg--;
884 				break;
885 			case SYS_umask:
886 				putchar('(');
887 				modename(*ip);
888 				ip++;
889 				narg--;
890 				break;
891 			case SYS_msync:
892 				print_number(ip, narg, c);
893 				print_number(ip, narg, c);
894 				putchar(',');
895 				msyncflagsname(*ip);
896 				ip++;
897 				narg--;
898 				break;
899 #ifdef SYS_freebsd6_mmap
900 			case SYS_freebsd6_mmap:
901 				print_number(ip, narg, c);
902 				print_number(ip, narg, c);
903 				putchar(',');
904 				mmapprotname(*ip);
905 				putchar(',');
906 				ip++;
907 				narg--;
908 				mmapflagsname(*ip);
909 				ip++;
910 				narg--;
911 				break;
912 #endif
913 			case SYS_mmap:
914 				print_number(ip, narg, c);
915 				print_number(ip, narg, c);
916 				putchar(',');
917 				mmapprotname(*ip);
918 				putchar(',');
919 				ip++;
920 				narg--;
921 				mmapflagsname(*ip);
922 				ip++;
923 				narg--;
924 				break;
925 			case SYS_mprotect:
926 				print_number(ip, narg, c);
927 				print_number(ip, narg, c);
928 				putchar(',');
929 				mmapprotname(*ip);
930 				ip++;
931 				narg--;
932 				break;
933 			case SYS_madvise:
934 				print_number(ip, narg, c);
935 				print_number(ip, narg, c);
936 				putchar(',');
937 				madvisebehavname(*ip);
938 				ip++;
939 				narg--;
940 				break;
941 			case SYS_setpriority:
942 				print_number(ip, narg, c);
943 				print_number(ip, narg, c);
944 				putchar(',');
945 				prioname(*ip);
946 				ip++;
947 				narg--;
948 				break;
949 			case SYS_fcntl:
950 				print_number(ip, narg, c);
951 				putchar(',');
952 				fcntlcmdname(ip[0], ip[1], decimal);
953 				ip += 2;
954 				narg -= 2;
955 				break;
956 			case SYS_socket: {
957 				int sockdomain;
958 				putchar('(');
959 				sockdomain = *ip;
960 				sockdomainname(sockdomain);
961 				ip++;
962 				narg--;
963 				putchar(',');
964 				socktypenamewithflags(*ip);
965 				ip++;
966 				narg--;
967 				if (sockdomain == PF_INET ||
968 				    sockdomain == PF_INET6) {
969 					putchar(',');
970 					sockipprotoname(*ip);
971 					ip++;
972 					narg--;
973 				}
974 				c = ',';
975 				break;
976 			}
977 			case SYS_setsockopt:
978 			case SYS_getsockopt:
979 				print_number(ip, narg, c);
980 				putchar(',');
981 				sockoptlevelname(*ip, decimal);
982 				if (*ip == SOL_SOCKET) {
983 					ip++;
984 					narg--;
985 					putchar(',');
986 					sockoptname(*ip);
987 				}
988 				ip++;
989 				narg--;
990 				break;
991 #ifdef SYS_freebsd6_lseek
992 			case SYS_freebsd6_lseek:
993 				print_number(ip, narg, c);
994 				/* Hidden 'pad' argument, not in lseek(2) */
995 				print_number(ip, narg, c);
996 				print_number(ip, narg, c);
997 				putchar(',');
998 				whencename(*ip);
999 				ip++;
1000 				narg--;
1001 				break;
1002 #endif
1003 			case SYS_lseek:
1004 				print_number(ip, narg, c);
1005 				/* Hidden 'pad' argument, not in lseek(2) */
1006 				print_number(ip, narg, c);
1007 				putchar(',');
1008 				whencename(*ip);
1009 				ip++;
1010 				narg--;
1011 				break;
1012 			case SYS_flock:
1013 				print_number(ip, narg, c);
1014 				putchar(',');
1015 				flockname(*ip);
1016 				ip++;
1017 				narg--;
1018 				break;
1019 			case SYS_mkfifo:
1020 			case SYS_mkfifoat:
1021 			case SYS_mkdir:
1022 			case SYS_mkdirat:
1023 				print_number(ip, narg, c);
1024 				putchar(',');
1025 				modename(*ip);
1026 				ip++;
1027 				narg--;
1028 				break;
1029 			case SYS_shutdown:
1030 				print_number(ip, narg, c);
1031 				putchar(',');
1032 				shutdownhowname(*ip);
1033 				ip++;
1034 				narg--;
1035 				break;
1036 			case SYS_socketpair:
1037 				putchar('(');
1038 				sockdomainname(*ip);
1039 				ip++;
1040 				narg--;
1041 				putchar(',');
1042 				socktypenamewithflags(*ip);
1043 				ip++;
1044 				narg--;
1045 				c = ',';
1046 				break;
1047 			case SYS_getrlimit:
1048 			case SYS_setrlimit:
1049 				putchar('(');
1050 				rlimitname(*ip);
1051 				ip++;
1052 				narg--;
1053 				c = ',';
1054 				break;
1055 			case SYS_quotactl:
1056 				print_number(ip, narg, c);
1057 				putchar(',');
1058 				quotactlname(*ip);
1059 				ip++;
1060 				narg--;
1061 				c = ',';
1062 				break;
1063 			case SYS_nfssvc:
1064 				putchar('(');
1065 				nfssvcname(*ip);
1066 				ip++;
1067 				narg--;
1068 				c = ',';
1069 				break;
1070 			case SYS_rtprio:
1071 				putchar('(');
1072 				rtprioname(*ip);
1073 				ip++;
1074 				narg--;
1075 				c = ',';
1076 				break;
1077 			case SYS___semctl:
1078 				print_number(ip, narg, c);
1079 				print_number(ip, narg, c);
1080 				putchar(',');
1081 				semctlname(*ip);
1082 				ip++;
1083 				narg--;
1084 				break;
1085 			case SYS_semget:
1086 				print_number(ip, narg, c);
1087 				print_number(ip, narg, c);
1088 				putchar(',');
1089 				semgetname(*ip);
1090 				ip++;
1091 				narg--;
1092 				break;
1093 			case SYS_msgctl:
1094 				print_number(ip, narg, c);
1095 				putchar(',');
1096 				shmctlname(*ip);
1097 				ip++;
1098 				narg--;
1099 				break;
1100 			case SYS_shmat:
1101 				print_number(ip, narg, c);
1102 				print_number(ip, narg, c);
1103 				putchar(',');
1104 				shmatname(*ip);
1105 				ip++;
1106 				narg--;
1107 				break;
1108 			case SYS_shmctl:
1109 				print_number(ip, narg, c);
1110 				putchar(',');
1111 				shmctlname(*ip);
1112 				ip++;
1113 				narg--;
1114 				break;
1115 			case SYS_shm_open:
1116 				print_number(ip, narg, c);
1117 				putchar(',');
1118 				flagsname(ip[0]);
1119 				printf(",0%o", (unsigned int)ip[1]);
1120 				ip += 3;
1121 				narg -= 3;
1122 				break;
1123 			case SYS_minherit:
1124 				print_number(ip, narg, c);
1125 				print_number(ip, narg, c);
1126 				putchar(',');
1127 				minheritname(*ip);
1128 				ip++;
1129 				narg--;
1130 				break;
1131 			case SYS_rfork:
1132 				putchar('(');
1133 				rforkname(*ip);
1134 				ip++;
1135 				narg--;
1136 				c = ',';
1137 				break;
1138 			case SYS_lio_listio:
1139 				putchar('(');
1140 				lio_listioname(*ip);
1141 				ip++;
1142 				narg--;
1143 				c = ',';
1144 				break;
1145 			case SYS_mlockall:
1146 				putchar('(');
1147 				mlockallname(*ip);
1148 				ip++;
1149 				narg--;
1150 				break;
1151 			case SYS_sched_setscheduler:
1152 				print_number(ip, narg, c);
1153 				putchar(',');
1154 				schedpolicyname(*ip);
1155 				ip++;
1156 				narg--;
1157 				break;
1158 			case SYS_sched_get_priority_max:
1159 			case SYS_sched_get_priority_min:
1160 				putchar('(');
1161 				schedpolicyname(*ip);
1162 				ip++;
1163 				narg--;
1164 				break;
1165 			case SYS_sendfile:
1166 				print_number(ip, narg, c);
1167 				print_number(ip, narg, c);
1168 				print_number(ip, narg, c);
1169 				print_number(ip, narg, c);
1170 				print_number(ip, narg, c);
1171 				print_number(ip, narg, c);
1172 				putchar(',');
1173 				sendfileflagsname(*(int *)ip);
1174 				ip++;
1175 				narg--;
1176 				break;
1177 			case SYS_kldsym:
1178 				print_number(ip, narg, c);
1179 				putchar(',');
1180 				kldsymcmdname(*ip);
1181 				ip++;
1182 				narg--;
1183 				break;
1184 			case SYS_sigprocmask:
1185 				putchar('(');
1186 				sigprocmaskhowname(*ip);
1187 				ip++;
1188 				narg--;
1189 				c = ',';
1190 				break;
1191 			case SYS___acl_get_file:
1192 			case SYS___acl_set_file:
1193 			case SYS___acl_get_fd:
1194 			case SYS___acl_set_fd:
1195 			case SYS___acl_delete_file:
1196 			case SYS___acl_delete_fd:
1197 			case SYS___acl_aclcheck_file:
1198 			case SYS___acl_aclcheck_fd:
1199 			case SYS___acl_get_link:
1200 			case SYS___acl_set_link:
1201 			case SYS___acl_delete_link:
1202 			case SYS___acl_aclcheck_link:
1203 				print_number(ip, narg, c);
1204 				putchar(',');
1205 				acltypename(*ip);
1206 				ip++;
1207 				narg--;
1208 				break;
1209 			case SYS_sigaction:
1210 				putchar('(');
1211 				signame(*ip);
1212 				ip++;
1213 				narg--;
1214 				c = ',';
1215 				break;
1216 			case SYS_extattrctl:
1217 				print_number(ip, narg, c);
1218 				putchar(',');
1219 				extattrctlname(*ip);
1220 				ip++;
1221 				narg--;
1222 				break;
1223 			case SYS_nmount:
1224 				print_number(ip, narg, c);
1225 				print_number(ip, narg, c);
1226 				putchar(',');
1227 				mountflagsname(*ip);
1228 				ip++;
1229 				narg--;
1230 				break;
1231 			case SYS_thr_create:
1232 				print_number(ip, narg, c);
1233 				print_number(ip, narg, c);
1234 				putchar(',');
1235 				thrcreateflagsname(*ip);
1236 				ip++;
1237 				narg--;
1238 				break;
1239 			case SYS_thr_kill:
1240 				print_number(ip, narg, c);
1241 				putchar(',');
1242 				signame(*ip);
1243 				ip++;
1244 				narg--;
1245 				break;
1246 			case SYS_kldunloadf:
1247 				print_number(ip, narg, c);
1248 				putchar(',');
1249 				kldunloadfflagsname(*ip);
1250 				ip++;
1251 				narg--;
1252 				break;
1253 			case SYS_linkat:
1254 			case SYS_renameat:
1255 			case SYS_symlinkat:
1256 				print_number(ip, narg, c);
1257 				putchar(',');
1258 				atfdname(*ip, decimal);
1259 				ip++;
1260 				narg--;
1261 				break;
1262 			case SYS_cap_fcntls_limit:
1263 				print_number(ip, narg, c);
1264 				putchar(',');
1265 				arg = *ip;
1266 				ip++;
1267 				narg--;
1268 				capfcntlname(arg);
1269 				break;
1270 			case SYS_posix_fadvise:
1271 				print_number(ip, narg, c);
1272 				print_number(ip, narg, c);
1273 				print_number(ip, narg, c);
1274 				(void)putchar(',');
1275 				fadvisebehavname((int)*ip);
1276 				ip++;
1277 				narg--;
1278 				break;
1279 			case SYS_procctl:
1280 				putchar('(');
1281 				idtypename(*ip, decimal);
1282 				c = ',';
1283 				ip++;
1284 				narg--;
1285 				print_number(ip, narg, c);
1286 				putchar(',');
1287 				procctlcmdname(*ip);
1288 				ip++;
1289 				narg--;
1290 				break;
1291 			case SYS__umtx_op:
1292 				print_number(ip, narg, c);
1293 				putchar(',');
1294 				umtxopname(*ip);
1295 				switch (*ip) {
1296 				case UMTX_OP_CV_WAIT:
1297 					ip++;
1298 					narg--;
1299 					putchar(',');
1300 					umtxcvwaitflags(*ip);
1301 					break;
1302 				case UMTX_OP_RW_RDLOCK:
1303 					ip++;
1304 					narg--;
1305 					putchar(',');
1306 					umtxrwlockflags(*ip);
1307 					break;
1308 				}
1309 				ip++;
1310 				narg--;
1311 			}
1312 		}
1313 		while (narg > 0) {
1314 			print_number(ip, narg, c);
1315 		}
1316 		putchar(')');
1317 	}
1318 	putchar('\n');
1319 }
1320 
1321 void
1322 ktrsysret(struct ktr_sysret *ktr, u_int flags)
1323 {
1324 	register_t ret = ktr->ktr_retval;
1325 	int error = ktr->ktr_error;
1326 	int code = ktr->ktr_code;
1327 
1328 	if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
1329 	    (code >= nsyscalls || code < 0))
1330 		printf("[%d] ", code);
1331 	else {
1332 		printf("%s", syscallnames[code]);
1333 		if (syscallno)
1334 			printf("[%d]", code);
1335 		printf(" ");
1336 	}
1337 
1338 	if (error == 0) {
1339 		if (fancy) {
1340 			printf("%ld", (long)ret);
1341 			if (ret < 0 || ret > 9)
1342 				printf("/%#lx", (unsigned long)ret);
1343 		} else {
1344 			if (decimal)
1345 				printf("%ld", (long)ret);
1346 			else
1347 				printf("%#lx", (unsigned long)ret);
1348 		}
1349 	} else if (error == ERESTART)
1350 		printf("RESTART");
1351 	else if (error == EJUSTRETURN)
1352 		printf("JUSTRETURN");
1353 	else {
1354 		printf("-1 errno %d", ktr->ktr_error);
1355 		if (fancy)
1356 			printf(" %s", strerror(ktr->ktr_error));
1357 	}
1358 	putchar('\n');
1359 }
1360 
1361 void
1362 ktrnamei(char *cp, int len)
1363 {
1364 	printf("\"%.*s\"\n", len, cp);
1365 }
1366 
1367 void
1368 hexdump(char *p, int len, int screenwidth)
1369 {
1370 	int n, i;
1371 	int width;
1372 
1373 	width = 0;
1374 	do {
1375 		width += 2;
1376 		i = 13;			/* base offset */
1377 		i += (width / 2) + 1;	/* spaces every second byte */
1378 		i += (width * 2);	/* width of bytes */
1379 		i += 3;			/* "  |" */
1380 		i += width;		/* each byte */
1381 		i += 1;			/* "|" */
1382 	} while (i < screenwidth);
1383 	width -= 2;
1384 
1385 	for (n = 0; n < len; n += width) {
1386 		for (i = n; i < n + width; i++) {
1387 			if ((i % width) == 0) {	/* beginning of line */
1388 				printf("       0x%04x", i);
1389 			}
1390 			if ((i % 2) == 0) {
1391 				printf(" ");
1392 			}
1393 			if (i < len)
1394 				printf("%02x", p[i] & 0xff);
1395 			else
1396 				printf("  ");
1397 		}
1398 		printf("  |");
1399 		for (i = n; i < n + width; i++) {
1400 			if (i >= len)
1401 				break;
1402 			if (p[i] >= ' ' && p[i] <= '~')
1403 				printf("%c", p[i]);
1404 			else
1405 				printf(".");
1406 		}
1407 		printf("|\n");
1408 	}
1409 	if ((i % width) != 0)
1410 		printf("\n");
1411 }
1412 
1413 void
1414 visdump(char *dp, int datalen, int screenwidth)
1415 {
1416 	int col = 0;
1417 	char *cp;
1418 	int width;
1419 	char visbuf[5];
1420 
1421 	printf("       \"");
1422 	col = 8;
1423 	for (;datalen > 0; datalen--, dp++) {
1424 		 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1425 		cp = visbuf;
1426 		/*
1427 		 * Keep track of printables and
1428 		 * space chars (like fold(1)).
1429 		 */
1430 		if (col == 0) {
1431 			putchar('\t');
1432 			col = 8;
1433 		}
1434 		switch(*cp) {
1435 		case '\n':
1436 			col = 0;
1437 			putchar('\n');
1438 			continue;
1439 		case '\t':
1440 			width = 8 - (col&07);
1441 			break;
1442 		default:
1443 			width = strlen(cp);
1444 		}
1445 		if (col + width > (screenwidth-2)) {
1446 			printf("\\\n\t");
1447 			col = 8;
1448 		}
1449 		col += width;
1450 		do {
1451 			putchar(*cp++);
1452 		} while (*cp);
1453 	}
1454 	if (col == 0)
1455 		printf("       ");
1456 	printf("\"\n");
1457 }
1458 
1459 void
1460 ktrgenio(struct ktr_genio *ktr, int len)
1461 {
1462 	int datalen = len - sizeof (struct ktr_genio);
1463 	char *dp = (char *)ktr + sizeof (struct ktr_genio);
1464 	static int screenwidth = 0;
1465 	int i, binary;
1466 
1467 	printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1468 		ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1469 		datalen == 1 ? "" : "s");
1470 	if (suppressdata)
1471 		return;
1472 	if (screenwidth == 0) {
1473 		struct winsize ws;
1474 
1475 		if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1476 		    ws.ws_col > 8)
1477 			screenwidth = ws.ws_col;
1478 		else
1479 			screenwidth = 80;
1480 	}
1481 	if (maxdata && datalen > maxdata)
1482 		datalen = maxdata;
1483 
1484 	for (i = 0, binary = 0; i < datalen && binary == 0; i++)  {
1485 		if (dp[i] >= 32 && dp[i] < 127)
1486 			continue;
1487 		if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1488 			continue;
1489 		binary = 1;
1490 	}
1491 	if (binary)
1492 		hexdump(dp, datalen, screenwidth);
1493 	else
1494 		visdump(dp, datalen, screenwidth);
1495 }
1496 
1497 const char *signames[] = {
1498 	"NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT",	/*  1 - 6  */
1499 	"EMT", "FPE", "KILL", "BUS", "SEGV", "SYS",		/*  7 - 12 */
1500 	"PIPE", "ALRM",  "TERM", "URG", "STOP", "TSTP",		/* 13 - 18 */
1501 	"CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU",		/* 19 - 24 */
1502 	"XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1",	/* 25 - 30 */
1503 	"USR2", NULL,						/* 31 - 32 */
1504 };
1505 
1506 void
1507 ktrpsig(struct ktr_psig *psig)
1508 {
1509 	if (psig->signo > 0 && psig->signo < NSIG)
1510 		printf("SIG%s ", signames[psig->signo]);
1511 	else
1512 		printf("SIG %d ", psig->signo);
1513 	if (psig->action == SIG_DFL) {
1514 		printf("SIG_DFL code=");
1515 		sigcodename(psig->signo, psig->code);
1516 		putchar('\n');
1517 	} else {
1518 		printf("caught handler=0x%lx mask=0x%x code=",
1519 		    (u_long)psig->action, psig->mask.__bits[0]);
1520 		sigcodename(psig->signo, psig->code);
1521 		putchar('\n');
1522 	}
1523 }
1524 
1525 void
1526 ktrcsw_old(struct ktr_csw_old *cs)
1527 {
1528 	printf("%s %s\n", cs->out ? "stop" : "resume",
1529 		cs->user ? "user" : "kernel");
1530 }
1531 
1532 void
1533 ktrcsw(struct ktr_csw *cs)
1534 {
1535 	printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1536 	    cs->user ? "user" : "kernel", cs->wmesg);
1537 }
1538 
1539 void
1540 ktruser(int len, void *p)
1541 {
1542 	unsigned char *cp;
1543 
1544 	if (kdump_print_utrace(stdout, p, len, decimal)) {
1545 		printf("\n");
1546 		return;
1547 	}
1548 
1549 	printf("%d ", len);
1550 	cp = p;
1551 	while (len--)
1552 		if (decimal)
1553 			printf(" %d", *cp++);
1554 		else
1555 			printf(" %02x", *cp++);
1556 	printf("\n");
1557 }
1558 
1559 void
1560 ktrcaprights(cap_rights_t *rightsp)
1561 {
1562 
1563 	printf("cap_rights_t ");
1564 	capname(rightsp);
1565 	printf("\n");
1566 }
1567 
1568 void
1569 ktrsockaddr(struct sockaddr *sa)
1570 {
1571 /*
1572  TODO: Support additional address families
1573 	#include <netnatm/natm.h>
1574 	struct sockaddr_natm	*natm;
1575 	#include <netsmb/netbios.h>
1576 	struct sockaddr_nb	*nb;
1577 */
1578 	char addr[64];
1579 
1580 	/*
1581 	 * note: ktrstruct() has already verified that sa points to a
1582 	 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1583 	 * sa->sa_len bytes long.
1584 	 */
1585 	printf("struct sockaddr { ");
1586 	sockfamilyname(sa->sa_family);
1587 	printf(", ");
1588 
1589 #define check_sockaddr_len(n)					\
1590 	if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) {	\
1591 		printf("invalid");				\
1592 		break;						\
1593 	}
1594 
1595 	switch(sa->sa_family) {
1596 	case AF_INET: {
1597 		struct sockaddr_in sa_in;
1598 
1599 		memset(&sa_in, 0, sizeof(sa_in));
1600 		memcpy(&sa_in, sa, sa->sa_len);
1601 		check_sockaddr_len(in);
1602 		inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1603 		printf("%s:%u", addr, ntohs(sa_in.sin_port));
1604 		break;
1605 	}
1606 	case AF_INET6: {
1607 		struct sockaddr_in6 sa_in6;
1608 
1609 		memset(&sa_in6, 0, sizeof(sa_in6));
1610 		memcpy(&sa_in6, sa, sa->sa_len);
1611 		check_sockaddr_len(in6);
1612 		getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1613 		    addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1614 		printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1615 		break;
1616 	}
1617 	case AF_UNIX: {
1618 		struct sockaddr_un sa_un;
1619 
1620 		memset(&sa_un, 0, sizeof(sa_un));
1621 		memcpy(&sa_un, sa, sa->sa_len);
1622 		printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1623 		break;
1624 	}
1625 	default:
1626 		printf("unknown address family");
1627 	}
1628 	printf(" }\n");
1629 }
1630 
1631 void
1632 ktrstat(struct stat *statp)
1633 {
1634 	char mode[12], timestr[PATH_MAX + 4];
1635 	struct passwd *pwd;
1636 	struct group  *grp;
1637 	struct tm *tm;
1638 
1639 	/*
1640 	 * note: ktrstruct() has already verified that statp points to a
1641 	 * buffer exactly sizeof(struct stat) bytes long.
1642 	 */
1643 	printf("struct stat {");
1644 	printf("dev=%ju, ino=%ju, ",
1645 		(uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1646 	if (resolv == 0)
1647 		printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1648 	else {
1649 		strmode(statp->st_mode, mode);
1650 		printf("mode=%s, ", mode);
1651 	}
1652 	printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1653 	if (resolv == 0) {
1654 		pwd = NULL;
1655 	} else {
1656 #ifdef HAVE_LIBCAPSICUM
1657 		if (cappwd != NULL)
1658 			pwd = cap_getpwuid(cappwd, statp->st_uid);
1659 		else
1660 #endif
1661 			pwd = getpwuid(statp->st_uid);
1662 	}
1663 	if (pwd == NULL)
1664 		printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1665 	else
1666 		printf("uid=\"%s\", ", pwd->pw_name);
1667 	if (resolv == 0) {
1668 		grp = NULL;
1669 	} else {
1670 #ifdef HAVE_LIBCAPSICUM
1671 		if (capgrp != NULL)
1672 			grp = cap_getgrgid(capgrp, statp->st_gid);
1673 		else
1674 #endif
1675 			grp = getgrgid(statp->st_gid);
1676 	}
1677 	if (grp == NULL)
1678 		printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1679 	else
1680 		printf("gid=\"%s\", ", grp->gr_name);
1681 	printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1682 	printf("atime=");
1683 	if (resolv == 0)
1684 		printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1685 	else {
1686 		tm = localtime(&statp->st_atim.tv_sec);
1687 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1688 		printf("\"%s\"", timestr);
1689 	}
1690 	if (statp->st_atim.tv_nsec != 0)
1691 		printf(".%09ld, ", statp->st_atim.tv_nsec);
1692 	else
1693 		printf(", ");
1694 	printf("stime=");
1695 	if (resolv == 0)
1696 		printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1697 	else {
1698 		tm = localtime(&statp->st_mtim.tv_sec);
1699 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1700 		printf("\"%s\"", timestr);
1701 	}
1702 	if (statp->st_mtim.tv_nsec != 0)
1703 		printf(".%09ld, ", statp->st_mtim.tv_nsec);
1704 	else
1705 		printf(", ");
1706 	printf("ctime=");
1707 	if (resolv == 0)
1708 		printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1709 	else {
1710 		tm = localtime(&statp->st_ctim.tv_sec);
1711 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1712 		printf("\"%s\"", timestr);
1713 	}
1714 	if (statp->st_ctim.tv_nsec != 0)
1715 		printf(".%09ld, ", statp->st_ctim.tv_nsec);
1716 	else
1717 		printf(", ");
1718 	printf("birthtime=");
1719 	if (resolv == 0)
1720 		printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1721 	else {
1722 		tm = localtime(&statp->st_birthtim.tv_sec);
1723 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1724 		printf("\"%s\"", timestr);
1725 	}
1726 	if (statp->st_birthtim.tv_nsec != 0)
1727 		printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1728 	else
1729 		printf(", ");
1730 	printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1731 		(uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1732 		(intmax_t)statp->st_blocks, statp->st_flags);
1733 	printf(" }\n");
1734 }
1735 
1736 void
1737 ktrstruct(char *buf, size_t buflen)
1738 {
1739 	char *name, *data;
1740 	size_t namelen, datalen;
1741 	int i;
1742 	cap_rights_t rights;
1743 	struct stat sb;
1744 	struct sockaddr_storage ss;
1745 
1746 	for (name = buf, namelen = 0;
1747 	     namelen < buflen && name[namelen] != '\0';
1748 	     ++namelen)
1749 		/* nothing */;
1750 	if (namelen == buflen)
1751 		goto invalid;
1752 	if (name[namelen] != '\0')
1753 		goto invalid;
1754 	data = buf + namelen + 1;
1755 	datalen = buflen - namelen - 1;
1756 	if (datalen == 0)
1757 		goto invalid;
1758 	/* sanity check */
1759 	for (i = 0; i < (int)namelen; ++i)
1760 		if (!isalpha(name[i]))
1761 			goto invalid;
1762 	if (strcmp(name, "caprights") == 0) {
1763 		if (datalen != sizeof(cap_rights_t))
1764 			goto invalid;
1765 		memcpy(&rights, data, datalen);
1766 		ktrcaprights(&rights);
1767 	} else if (strcmp(name, "stat") == 0) {
1768 		if (datalen != sizeof(struct stat))
1769 			goto invalid;
1770 		memcpy(&sb, data, datalen);
1771 		ktrstat(&sb);
1772 	} else if (strcmp(name, "sockaddr") == 0) {
1773 		if (datalen > sizeof(ss))
1774 			goto invalid;
1775 		memcpy(&ss, data, datalen);
1776 		if (datalen != ss.ss_len)
1777 			goto invalid;
1778 		ktrsockaddr((struct sockaddr *)&ss);
1779 	} else {
1780 		printf("unknown structure\n");
1781 	}
1782 	return;
1783 invalid:
1784 	printf("invalid record\n");
1785 }
1786 
1787 void
1788 ktrcapfail(struct ktr_cap_fail *ktr)
1789 {
1790 	switch (ktr->cap_type) {
1791 	case CAPFAIL_NOTCAPABLE:
1792 		/* operation on fd with insufficient capabilities */
1793 		printf("operation requires ");
1794 		capname(&ktr->cap_needed);
1795 		printf(", descriptor holds ");
1796 		capname(&ktr->cap_held);
1797 		break;
1798 	case CAPFAIL_INCREASE:
1799 		/* requested more capabilities than fd already has */
1800 		printf("attempt to increase capabilities from ");
1801 		capname(&ktr->cap_held);
1802 		printf(" to ");
1803 		capname(&ktr->cap_needed);
1804 		break;
1805 	case CAPFAIL_SYSCALL:
1806 		/* called restricted syscall */
1807 		printf("disallowed system call");
1808 		break;
1809 	case CAPFAIL_LOOKUP:
1810 		/* used ".." in strict-relative mode */
1811 		printf("restricted VFS lookup");
1812 		break;
1813 	default:
1814 		printf("unknown capability failure: ");
1815 		capname(&ktr->cap_needed);
1816 		printf(" ");
1817 		capname(&ktr->cap_held);
1818 		break;
1819 	}
1820 	printf("\n");
1821 }
1822 
1823 void
1824 ktrfault(struct ktr_fault *ktr)
1825 {
1826 
1827 	printf("0x%jx ", (uintmax_t)ktr->vaddr);
1828 	vmprotname(ktr->type);
1829 	printf("\n");
1830 }
1831 
1832 void
1833 ktrfaultend(struct ktr_faultend *ktr)
1834 {
1835 
1836 	vmresultname(ktr->result);
1837 	printf("\n");
1838 }
1839 
1840 #if defined(__amd64__) || defined(__i386__)
1841 
1842 #if defined(__amd64__)
1843 #define	NLINUX_SYSCALLS(v)		((v) & SV_ILP32 ?		\
1844 	    nitems(linux32_syscallnames) : nitems(linux_syscallnames))
1845 #define	LINUX_SYSCALLNAMES(v, i)	((v) & SV_ILP32 ?		\
1846 	    linux32_syscallnames[i] : linux_syscallnames[i])
1847 #else
1848 #define	NLINUX_SYSCALLS(v)		(nitems(linux_syscallnames))
1849 #define	LINUX_SYSCALLNAMES(v, i)	(linux_syscallnames[i])
1850 #endif
1851 
1852 void
1853 linux_ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags)
1854 {
1855 	int narg = ktr->ktr_narg;
1856 	unsigned code = ktr->ktr_code;
1857 	register_t *ip;
1858 
1859 	if (ktr->ktr_code < 0 || code >= NLINUX_SYSCALLS(sv_flags))
1860 		printf("[%d]", ktr->ktr_code);
1861 	else {
1862 		printf("%s", LINUX_SYSCALLNAMES(sv_flags, ktr->ktr_code));
1863 		if (syscallno)
1864 			printf("[%d]", ktr->ktr_code);
1865 	}
1866 	ip = &ktr->ktr_args[0];
1867 	if (narg) {
1868 		char c = '(';
1869 		while (narg > 0)
1870 			print_number(ip, narg, c);
1871 		putchar(')');
1872 	}
1873 	putchar('\n');
1874 }
1875 
1876 void
1877 linux_ktrsysret(struct ktr_sysret *ktr, u_int sv_flags)
1878 {
1879 	register_t ret = ktr->ktr_retval;
1880 	unsigned code = ktr->ktr_code;
1881 	int error = ktr->ktr_error;
1882 
1883 	if (ktr->ktr_code < 0 || code >= NLINUX_SYSCALLS(sv_flags))
1884 		printf("[%d] ", ktr->ktr_code);
1885 	else {
1886 		printf("%s ", LINUX_SYSCALLNAMES(sv_flags, code));
1887 		if (syscallno)
1888 			printf("[%d]", code);
1889 		printf(" ");
1890 	}
1891 
1892 	if (error == 0) {
1893 		if (fancy) {
1894 			printf("%ld", (long)ret);
1895 			if (ret < 0 || ret > 9)
1896 				printf("/%#lx", (unsigned long)ret);
1897 		} else {
1898 			if (decimal)
1899 				printf("%ld", (long)ret);
1900 			else
1901 				printf("%#lx", (unsigned long)ret);
1902 		}
1903 	} else if (error == ERESTART)
1904 		printf("RESTART");
1905 	else if (error == EJUSTRETURN)
1906 		printf("JUSTRETURN");
1907 	else {
1908 		if (ktr->ktr_error <= ELAST + 1)
1909 			error = abs(bsd_to_linux_errno[ktr->ktr_error]);
1910 		else
1911 			error = 999;
1912 		printf("-1 errno %d", error);
1913 		if (fancy)
1914 			printf(" %s", strerror(ktr->ktr_error));
1915 	}
1916 	putchar('\n');
1917 }
1918 #endif
1919 
1920 void
1921 usage(void)
1922 {
1923 	fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
1924 	    "[-m maxdata] [-p pid] [-t trstr]\n");
1925 	exit(1);
1926 }
1927