xref: /freebsd/usr.bin/kdump/kdump.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
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 <dlfcn.h>
71 #include <err.h>
72 #include <grp.h>
73 #include <inttypes.h>
74 #ifdef HAVE_LIBCAPSICUM
75 #include <libcapsicum.h>
76 #include <libcapsicum_grp.h>
77 #include <libcapsicum_pwd.h>
78 #include <libcapsicum_service.h>
79 #endif
80 #include <locale.h>
81 #include <netdb.h>
82 #include <nl_types.h>
83 #include <pwd.h>
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <string.h>
87 #include <termios.h>
88 #include <time.h>
89 #include <unistd.h>
90 #include <vis.h>
91 #include "ktrace.h"
92 #include "kdump_subr.h"
93 
94 u_int abidump(struct ktr_header *);
95 int fetchprocinfo(struct ktr_header *, u_int *);
96 int fread_tail(void *, int, int);
97 void dumpheader(struct ktr_header *);
98 void ktrsyscall(struct ktr_syscall *, u_int);
99 void ktrsysret(struct ktr_sysret *, u_int);
100 void ktrnamei(char *, int);
101 void hexdump(char *, int, int);
102 void visdump(char *, int, int);
103 void ktrgenio(struct ktr_genio *, int);
104 void ktrpsig(struct ktr_psig *);
105 void ktrcsw(struct ktr_csw *);
106 void ktrcsw_old(struct ktr_csw_old *);
107 void ktruser_malloc(void *);
108 void ktruser_rtld(int, void *);
109 void ktruser(int, void *);
110 void ktrcaprights(cap_rights_t *);
111 void ktrsockaddr(struct sockaddr *);
112 void ktrstat(struct stat *);
113 void ktrstruct(char *, size_t);
114 void ktrcapfail(struct ktr_cap_fail *);
115 void ktrfault(struct ktr_fault *);
116 void ktrfaultend(struct ktr_faultend *);
117 void limitfd(int fd);
118 void usage(void);
119 void ioctlname(unsigned long, 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 #define	UTRACE_DLOPEN_START		1
1540 #define	UTRACE_DLOPEN_STOP		2
1541 #define	UTRACE_DLCLOSE_START		3
1542 #define	UTRACE_DLCLOSE_STOP		4
1543 #define	UTRACE_LOAD_OBJECT		5
1544 #define	UTRACE_UNLOAD_OBJECT		6
1545 #define	UTRACE_ADD_RUNDEP		7
1546 #define	UTRACE_PRELOAD_FINISHED		8
1547 #define	UTRACE_INIT_CALL		9
1548 #define	UTRACE_FINI_CALL		10
1549 #define	UTRACE_DLSYM_START		11
1550 #define	UTRACE_DLSYM_STOP		12
1551 
1552 struct utrace_rtld {
1553 	char sig[4];				/* 'RTLD' */
1554 	int event;
1555 	void *handle;
1556 	void *mapbase;
1557 	size_t mapsize;
1558 	int refcnt;
1559 	char name[MAXPATHLEN];
1560 };
1561 
1562 void
1563 ktruser_rtld(int len, void *p)
1564 {
1565 	struct utrace_rtld *ut = p;
1566 	unsigned char *cp;
1567 	void *parent;
1568 	int mode;
1569 
1570 	switch (ut->event) {
1571 	case UTRACE_DLOPEN_START:
1572 		mode = ut->refcnt;
1573 		printf("dlopen(%s, ", ut->name);
1574 		switch (mode & RTLD_MODEMASK) {
1575 		case RTLD_NOW:
1576 			printf("RTLD_NOW");
1577 			break;
1578 		case RTLD_LAZY:
1579 			printf("RTLD_LAZY");
1580 			break;
1581 		default:
1582 			printf("%#x", mode & RTLD_MODEMASK);
1583 		}
1584 		if (mode & RTLD_GLOBAL)
1585 			printf(" | RTLD_GLOBAL");
1586 		if (mode & RTLD_TRACE)
1587 			printf(" | RTLD_TRACE");
1588 		if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
1589 			printf(" | %#x", mode &
1590 			    ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
1591 		printf(")\n");
1592 		break;
1593 	case UTRACE_DLOPEN_STOP:
1594 		printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
1595 		    ut->refcnt);
1596 		break;
1597 	case UTRACE_DLCLOSE_START:
1598 		printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
1599 		    ut->refcnt);
1600 		break;
1601 	case UTRACE_DLCLOSE_STOP:
1602 		printf("dlclose(%p) finished\n", ut->handle);
1603 		break;
1604 	case UTRACE_LOAD_OBJECT:
1605 		printf("RTLD: loaded   %p @ %p - %p (%s)\n", ut->handle,
1606 		    ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1607 		    ut->name);
1608 		break;
1609 	case UTRACE_UNLOAD_OBJECT:
1610 		printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
1611 		    ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1612 		    ut->name);
1613 		break;
1614 	case UTRACE_ADD_RUNDEP:
1615 		parent = ut->mapbase;
1616 		printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
1617 		    ut->handle, ut->name, ut->refcnt);
1618 		break;
1619 	case UTRACE_PRELOAD_FINISHED:
1620 		printf("RTLD: LD_PRELOAD finished\n");
1621 		break;
1622 	case UTRACE_INIT_CALL:
1623 		printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
1624 		    ut->name);
1625 		break;
1626 	case UTRACE_FINI_CALL:
1627 		printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
1628 		    ut->name);
1629 		break;
1630 	case UTRACE_DLSYM_START:
1631 		printf("RTLD: dlsym(%p, %s)\n", ut->handle, ut->name);
1632 		break;
1633 	case UTRACE_DLSYM_STOP:
1634 		printf("RTLD: %p = dlsym(%p, %s)\n", ut->mapbase, ut->handle,
1635 		    ut->name);
1636 		break;
1637 	default:
1638 		cp = p;
1639 		cp += 4;
1640 		len -= 4;
1641 		printf("RTLD: %d ", len);
1642 		while (len--)
1643 			if (decimal)
1644 				printf(" %d", *cp++);
1645 			else
1646 				printf(" %02x", *cp++);
1647 		printf("\n");
1648 	}
1649 }
1650 
1651 struct utrace_malloc {
1652 	void *p;
1653 	size_t s;
1654 	void *r;
1655 };
1656 
1657 void
1658 ktruser_malloc(void *p)
1659 {
1660 	struct utrace_malloc *ut = p;
1661 
1662 	if (ut->p == (void *)(intptr_t)(-1))
1663 		printf("malloc_init()\n");
1664 	else if (ut->s == 0)
1665 		printf("free(%p)\n", ut->p);
1666 	else if (ut->p == NULL)
1667 		printf("%p = malloc(%zu)\n", ut->r, ut->s);
1668 	else
1669 		printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
1670 }
1671 
1672 void
1673 ktruser(int len, void *p)
1674 {
1675 	unsigned char *cp;
1676 
1677 	if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
1678 		ktruser_rtld(len, p);
1679 		return;
1680 	}
1681 
1682 	if (len == sizeof(struct utrace_malloc)) {
1683 		ktruser_malloc(p);
1684 		return;
1685 	}
1686 
1687 	printf("%d ", len);
1688 	cp = p;
1689 	while (len--)
1690 		if (decimal)
1691 			printf(" %d", *cp++);
1692 		else
1693 			printf(" %02x", *cp++);
1694 	printf("\n");
1695 }
1696 
1697 void
1698 ktrcaprights(cap_rights_t *rightsp)
1699 {
1700 
1701 	printf("cap_rights_t ");
1702 	capname(rightsp);
1703 	printf("\n");
1704 }
1705 
1706 void
1707 ktrsockaddr(struct sockaddr *sa)
1708 {
1709 /*
1710  TODO: Support additional address families
1711 	#include <netnatm/natm.h>
1712 	struct sockaddr_natm	*natm;
1713 	#include <netsmb/netbios.h>
1714 	struct sockaddr_nb	*nb;
1715 */
1716 	char addr[64];
1717 
1718 	/*
1719 	 * note: ktrstruct() has already verified that sa points to a
1720 	 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1721 	 * sa->sa_len bytes long.
1722 	 */
1723 	printf("struct sockaddr { ");
1724 	sockfamilyname(sa->sa_family);
1725 	printf(", ");
1726 
1727 #define check_sockaddr_len(n)					\
1728 	if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) {	\
1729 		printf("invalid");				\
1730 		break;						\
1731 	}
1732 
1733 	switch(sa->sa_family) {
1734 	case AF_INET: {
1735 		struct sockaddr_in sa_in;
1736 
1737 		memset(&sa_in, 0, sizeof(sa_in));
1738 		memcpy(&sa_in, sa, sa->sa_len);
1739 		check_sockaddr_len(in);
1740 		inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1741 		printf("%s:%u", addr, ntohs(sa_in.sin_port));
1742 		break;
1743 	}
1744 	case AF_INET6: {
1745 		struct sockaddr_in6 sa_in6;
1746 
1747 		memset(&sa_in6, 0, sizeof(sa_in6));
1748 		memcpy(&sa_in6, sa, sa->sa_len);
1749 		check_sockaddr_len(in6);
1750 		getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1751 		    addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1752 		printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1753 		break;
1754 	}
1755 	case AF_UNIX: {
1756 		struct sockaddr_un sa_un;
1757 
1758 		memset(&sa_un, 0, sizeof(sa_un));
1759 		memcpy(&sa_un, sa, sa->sa_len);
1760 		printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1761 		break;
1762 	}
1763 	default:
1764 		printf("unknown address family");
1765 	}
1766 	printf(" }\n");
1767 }
1768 
1769 void
1770 ktrstat(struct stat *statp)
1771 {
1772 	char mode[12], timestr[PATH_MAX + 4];
1773 	struct passwd *pwd;
1774 	struct group  *grp;
1775 	struct tm *tm;
1776 
1777 	/*
1778 	 * note: ktrstruct() has already verified that statp points to a
1779 	 * buffer exactly sizeof(struct stat) bytes long.
1780 	 */
1781 	printf("struct stat {");
1782 	printf("dev=%ju, ino=%ju, ",
1783 		(uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1784 	if (resolv == 0)
1785 		printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1786 	else {
1787 		strmode(statp->st_mode, mode);
1788 		printf("mode=%s, ", mode);
1789 	}
1790 	printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1791 	if (resolv == 0) {
1792 		pwd = NULL;
1793 	} else {
1794 #ifdef HAVE_LIBCAPSICUM
1795 		if (cappwd != NULL)
1796 			pwd = cap_getpwuid(cappwd, statp->st_uid);
1797 		else
1798 #endif
1799 			pwd = getpwuid(statp->st_uid);
1800 	}
1801 	if (pwd == NULL)
1802 		printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1803 	else
1804 		printf("uid=\"%s\", ", pwd->pw_name);
1805 	if (resolv == 0) {
1806 		grp = NULL;
1807 	} else {
1808 #ifdef HAVE_LIBCAPSICUM
1809 		if (capgrp != NULL)
1810 			grp = cap_getgrgid(capgrp, statp->st_gid);
1811 		else
1812 #endif
1813 			grp = getgrgid(statp->st_gid);
1814 	}
1815 	if (grp == NULL)
1816 		printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1817 	else
1818 		printf("gid=\"%s\", ", grp->gr_name);
1819 	printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1820 	printf("atime=");
1821 	if (resolv == 0)
1822 		printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1823 	else {
1824 		tm = localtime(&statp->st_atim.tv_sec);
1825 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1826 		printf("\"%s\"", timestr);
1827 	}
1828 	if (statp->st_atim.tv_nsec != 0)
1829 		printf(".%09ld, ", statp->st_atim.tv_nsec);
1830 	else
1831 		printf(", ");
1832 	printf("stime=");
1833 	if (resolv == 0)
1834 		printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1835 	else {
1836 		tm = localtime(&statp->st_mtim.tv_sec);
1837 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1838 		printf("\"%s\"", timestr);
1839 	}
1840 	if (statp->st_mtim.tv_nsec != 0)
1841 		printf(".%09ld, ", statp->st_mtim.tv_nsec);
1842 	else
1843 		printf(", ");
1844 	printf("ctime=");
1845 	if (resolv == 0)
1846 		printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1847 	else {
1848 		tm = localtime(&statp->st_ctim.tv_sec);
1849 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1850 		printf("\"%s\"", timestr);
1851 	}
1852 	if (statp->st_ctim.tv_nsec != 0)
1853 		printf(".%09ld, ", statp->st_ctim.tv_nsec);
1854 	else
1855 		printf(", ");
1856 	printf("birthtime=");
1857 	if (resolv == 0)
1858 		printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1859 	else {
1860 		tm = localtime(&statp->st_birthtim.tv_sec);
1861 		strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1862 		printf("\"%s\"", timestr);
1863 	}
1864 	if (statp->st_birthtim.tv_nsec != 0)
1865 		printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1866 	else
1867 		printf(", ");
1868 	printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1869 		(uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1870 		(intmax_t)statp->st_blocks, statp->st_flags);
1871 	printf(" }\n");
1872 }
1873 
1874 void
1875 ktrstruct(char *buf, size_t buflen)
1876 {
1877 	char *name, *data;
1878 	size_t namelen, datalen;
1879 	int i;
1880 	cap_rights_t rights;
1881 	struct stat sb;
1882 	struct sockaddr_storage ss;
1883 
1884 	for (name = buf, namelen = 0;
1885 	     namelen < buflen && name[namelen] != '\0';
1886 	     ++namelen)
1887 		/* nothing */;
1888 	if (namelen == buflen)
1889 		goto invalid;
1890 	if (name[namelen] != '\0')
1891 		goto invalid;
1892 	data = buf + namelen + 1;
1893 	datalen = buflen - namelen - 1;
1894 	if (datalen == 0)
1895 		goto invalid;
1896 	/* sanity check */
1897 	for (i = 0; i < (int)namelen; ++i)
1898 		if (!isalpha(name[i]))
1899 			goto invalid;
1900 	if (strcmp(name, "caprights") == 0) {
1901 		if (datalen != sizeof(cap_rights_t))
1902 			goto invalid;
1903 		memcpy(&rights, data, datalen);
1904 		ktrcaprights(&rights);
1905 	} else if (strcmp(name, "stat") == 0) {
1906 		if (datalen != sizeof(struct stat))
1907 			goto invalid;
1908 		memcpy(&sb, data, datalen);
1909 		ktrstat(&sb);
1910 	} else if (strcmp(name, "sockaddr") == 0) {
1911 		if (datalen > sizeof(ss))
1912 			goto invalid;
1913 		memcpy(&ss, data, datalen);
1914 		if (datalen != ss.ss_len)
1915 			goto invalid;
1916 		ktrsockaddr((struct sockaddr *)&ss);
1917 	} else {
1918 		printf("unknown structure\n");
1919 	}
1920 	return;
1921 invalid:
1922 	printf("invalid record\n");
1923 }
1924 
1925 void
1926 ktrcapfail(struct ktr_cap_fail *ktr)
1927 {
1928 	switch (ktr->cap_type) {
1929 	case CAPFAIL_NOTCAPABLE:
1930 		/* operation on fd with insufficient capabilities */
1931 		printf("operation requires ");
1932 		capname(&ktr->cap_needed);
1933 		printf(", descriptor holds ");
1934 		capname(&ktr->cap_held);
1935 		break;
1936 	case CAPFAIL_INCREASE:
1937 		/* requested more capabilities than fd already has */
1938 		printf("attempt to increase capabilities from ");
1939 		capname(&ktr->cap_held);
1940 		printf(" to ");
1941 		capname(&ktr->cap_needed);
1942 		break;
1943 	case CAPFAIL_SYSCALL:
1944 		/* called restricted syscall */
1945 		printf("disallowed system call");
1946 		break;
1947 	case CAPFAIL_LOOKUP:
1948 		/* used ".." in strict-relative mode */
1949 		printf("restricted VFS lookup");
1950 		break;
1951 	default:
1952 		printf("unknown capability failure: ");
1953 		capname(&ktr->cap_needed);
1954 		printf(" ");
1955 		capname(&ktr->cap_held);
1956 		break;
1957 	}
1958 	printf("\n");
1959 }
1960 
1961 void
1962 ktrfault(struct ktr_fault *ktr)
1963 {
1964 
1965 	printf("0x%jx ", (uintmax_t)ktr->vaddr);
1966 	vmprotname(ktr->type);
1967 	printf("\n");
1968 }
1969 
1970 void
1971 ktrfaultend(struct ktr_faultend *ktr)
1972 {
1973 
1974 	vmresultname(ktr->result);
1975 	printf("\n");
1976 }
1977 
1978 #if defined(__amd64__) || defined(__i386__)
1979 
1980 #if defined(__amd64__)
1981 #define	NLINUX_SYSCALLS(v)		((v) & SV_ILP32 ?		\
1982 	    nitems(linux32_syscallnames) : nitems(linux_syscallnames))
1983 #define	LINUX_SYSCALLNAMES(v, i)	((v) & SV_ILP32 ?		\
1984 	    linux32_syscallnames[i] : linux_syscallnames[i])
1985 #else
1986 #define	NLINUX_SYSCALLS(v)		(nitems(linux_syscallnames))
1987 #define	LINUX_SYSCALLNAMES(v, i)	(linux_syscallnames[i])
1988 #endif
1989 
1990 void
1991 linux_ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags)
1992 {
1993 	int narg = ktr->ktr_narg;
1994 	unsigned code = ktr->ktr_code;
1995 	register_t *ip;
1996 
1997 	if (ktr->ktr_code < 0 || code >= NLINUX_SYSCALLS(sv_flags))
1998 		printf("[%d]", ktr->ktr_code);
1999 	else {
2000 		printf("%s", LINUX_SYSCALLNAMES(sv_flags, ktr->ktr_code));
2001 		if (syscallno)
2002 			printf("[%d]", ktr->ktr_code);
2003 	}
2004 	ip = &ktr->ktr_args[0];
2005 	if (narg) {
2006 		char c = '(';
2007 		while (narg > 0)
2008 			print_number(ip, narg, c);
2009 		putchar(')');
2010 	}
2011 	putchar('\n');
2012 }
2013 
2014 void
2015 linux_ktrsysret(struct ktr_sysret *ktr, u_int sv_flags)
2016 {
2017 	register_t ret = ktr->ktr_retval;
2018 	unsigned code = ktr->ktr_code;
2019 	int error = ktr->ktr_error;
2020 
2021 	if (ktr->ktr_code < 0 || code >= NLINUX_SYSCALLS(sv_flags))
2022 		printf("[%d] ", ktr->ktr_code);
2023 	else {
2024 		printf("%s ", LINUX_SYSCALLNAMES(sv_flags, code));
2025 		if (syscallno)
2026 			printf("[%d]", code);
2027 		printf(" ");
2028 	}
2029 
2030 	if (error == 0) {
2031 		if (fancy) {
2032 			printf("%ld", (long)ret);
2033 			if (ret < 0 || ret > 9)
2034 				printf("/%#lx", (unsigned long)ret);
2035 		} else {
2036 			if (decimal)
2037 				printf("%ld", (long)ret);
2038 			else
2039 				printf("%#lx", (unsigned long)ret);
2040 		}
2041 	} else if (error == ERESTART)
2042 		printf("RESTART");
2043 	else if (error == EJUSTRETURN)
2044 		printf("JUSTRETURN");
2045 	else {
2046 		if (ktr->ktr_error <= ELAST + 1)
2047 			error = abs(bsd_to_linux_errno[ktr->ktr_error]);
2048 		else
2049 			error = 999;
2050 		printf("-1 errno %d", error);
2051 		if (fancy)
2052 			printf(" %s", strerror(ktr->ktr_error));
2053 	}
2054 	putchar('\n');
2055 }
2056 #endif
2057 
2058 void
2059 usage(void)
2060 {
2061 	fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
2062 	    "[-m maxdata] [-p pid] [-t trstr]\n");
2063 	exit(1);
2064 }
2065