Home | History | Annotate | Line # | Download | only in kdump
kdump.c revision 1.82
      1 /*	$NetBSD: kdump.c,v 1.82 2005/10/18 01:49:18 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
     35 	The Regents of the University of California.  All rights reserved.\n");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)kdump.c	8.4 (Berkeley) 4/28/95";
     41 #else
     42 __RCSID("$NetBSD: kdump.c,v 1.82 2005/10/18 01:49:18 christos Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #define _KERNEL
     48 #include <sys/errno.h>
     49 #undef _KERNEL
     50 #include <sys/time.h>
     51 #include <sys/uio.h>
     52 #include <sys/ktrace.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/ptrace.h>
     55 
     56 #include <ctype.h>
     57 #include <err.h>
     58 #include <signal.h>
     59 #include <stddef.h>
     60 #include <stdio.h>
     61 #include <stdlib.h>
     62 #include <string.h>
     63 #include <unistd.h>
     64 #include <vis.h>
     65 
     66 #include "ktrace.h"
     67 #include "setemul.h"
     68 
     69 #include <sys/syscall.h>
     70 
     71 static int timestamp, decimal, plain, tail, maxdata = -1, numeric;
     72 static int word_size = 0;
     73 static pid_t do_pid = -1;
     74 static const char *tracefile = NULL;
     75 static struct ktr_header ktr_header;
     76 static int emul_changed = 0;
     77 
     78 #define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
     79 #define small(v)	(((long)(v) >= 0) && ((long)(v) < 10))
     80 
     81 static const char * const ptrace_ops[] = {
     82 	"PT_TRACE_ME",	"PT_READ_I",	"PT_READ_D",	"PT_READ_U",
     83 	"PT_WRITE_I",	"PT_WRITE_D",	"PT_WRITE_U",	"PT_CONTINUE",
     84 	"PT_KILL",	"PT_ATTACH",	"PT_DETACH",	"PT_IO",
     85 	"PT_DUMPCORE",	"PT_LWPINFO"
     86 };
     87 
     88 #ifdef PT_MACHDEP_STRINGS
     89 static const char * const ptrace_machdep_ops[] = { PT_MACHDEP_STRINGS };
     90 #endif
     91 
     92 static const char * const linux_ptrace_ops[] = {
     93 	"PTRACE_TRACEME",
     94 	"PTRACE_PEEKTEXT", "PTRACE_PEEKDATA", "PTRACE_PEEKUSER",
     95 	"PTRACE_POKETEXT", "PTRACE_POKEDATA", "PTRACE_POKEUSER",
     96 	"PTRACE_CONT", "PTRACE_KILL", "PTRACE_SINGLESTEP",
     97 	NULL, NULL,
     98 	"PTRACE_GETREGS", "PTRACE_SETREGS", "PTRACE_GETFPREGS",
     99 	"PTRACE_SETFPREGS", "PTRACE_ATTACH", "PTRACE_DETACH",
    100 	"PTRACE_SYSCALL",
    101 };
    102 
    103 int	main(int, char **);
    104 static int	fread_tail(void *, size_t, size_t);
    105 static int	dumpheader(struct ktr_header *);
    106 static void	output_long(u_long, int);
    107 static void	ioctldecode(u_long);
    108 static void	ktrsyscall(struct ktr_syscall *);
    109 static void	ktrsysret(struct ktr_sysret *, int);
    110 static void	ktrnamei(char *, int);
    111 static void	ktremul(char *, int, int);
    112 static void	ktrgenio(struct ktr_genio *, int);
    113 static void	ktrpsig(void *, int);
    114 static void	ktrcsw(struct ktr_csw *);
    115 static void	ktruser(struct ktr_user *, int);
    116 static void	ktrmmsg(struct ktr_mmsg *, int);
    117 static void	ktrmool(struct ktr_mool *, int);
    118 static void	usage(void) __attribute__((__noreturn__));
    119 static void	eprint(int);
    120 static void	rprint(register_t);
    121 static const char *signame(long, int);
    122 static void hexdump_buf(const void *, int, int);
    123 static void visdump_buf(const void *, int, int);
    124 
    125 int
    126 main(int argc, char **argv)
    127 {
    128 	int ch, ktrlen, size;
    129 	void *m;
    130 	int trpoints = 0;
    131 	int trset = 0;
    132 	const char *emul_name = "netbsd";
    133 	int col;
    134 	char *cp;
    135 
    136 	setprogname(argv[0]);
    137 	while ((ch = getopt(argc, argv, "e:f:dlm:Nnp:RTt:xX:")) != -1) {
    138 		switch (ch) {
    139 		case 'e':
    140 			emul_name = strdup(optarg); /* it's safer to copy it */
    141 			break;
    142 		case 'f':
    143 			tracefile = optarg;
    144 			break;
    145 		case 'd':
    146 			decimal = 1;
    147 			break;
    148 		case 'l':
    149 			tail = 1;
    150 			break;
    151 		case 'p':
    152 			do_pid = strtoul(optarg, &cp, 0);
    153 			if (*cp != 0)
    154 				errx(1,"invalid number %s", optarg);
    155 			break;
    156 		case 'm':
    157 			maxdata = strtoul(optarg, &cp, 0);
    158 			if (*cp != 0)
    159 				errx(1,"invalid number %s", optarg);
    160 			break;
    161 		case 'N':
    162 			numeric++;
    163 			break;
    164 		case 'n':
    165 			plain++;
    166 			break;
    167 		case 'R':
    168 			timestamp = 2;	/* relative timestamp */
    169 			break;
    170 		case 'T':
    171 			timestamp = 1;
    172 			break;
    173 		case 't':
    174 			trset = 1;
    175 			trpoints = getpoints(trpoints, optarg);
    176 			if (trpoints < 0)
    177 				errx(1, "unknown trace point in %s", optarg);
    178 			break;
    179 		case 'x':
    180 			word_size = 1;
    181 			break;
    182 		case 'X':
    183 			word_size = strtoul(optarg, &cp, 0);
    184 			if (*cp != 0 || word_size & (word_size - 1) ||
    185 			    word_size > 16 || word_size <= 0)
    186 				errx(1, "argument to -X must be "
    187 				    "1, 2, 4, 8 or 16");
    188 			break;
    189 		default:
    190 			usage();
    191 		}
    192 	}
    193 	argv += optind;
    194 	argc -= optind;
    195 
    196 	if (!trset)
    197 		trpoints = ALL_POINTS;
    198 
    199 	if (tracefile == NULL) {
    200 		if (argc == 1) {
    201 			tracefile = argv[0];
    202 			argv++;
    203 			argc--;
    204 		} else
    205 			tracefile = DEF_TRACEFILE;
    206 	}
    207 
    208 	if (argc > 0)
    209 		usage();
    210 
    211 	setemul(emul_name, 0, 0);
    212 	mach_lookup_emul();
    213 
    214 	m = malloc(size = 1024);
    215 	if (m == NULL)
    216 		errx(1, "malloc: %s", strerror(ENOMEM));
    217 	if (!freopen(tracefile, "r", stdin))
    218 		err(1, "%s", tracefile);
    219 	while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
    220 		if (trpoints & (1 << ktr_header.ktr_type) &&
    221 		    (do_pid == -1 || ktr_header.ktr_pid == do_pid))
    222 			col = dumpheader(&ktr_header);
    223 		else
    224 			col = -1;
    225 		if ((ktrlen = ktr_header.ktr_len) < 0)
    226 			errx(1, "bogus length 0x%x", ktrlen);
    227 		if (ktrlen > size) {
    228 			while (ktrlen > size)
    229 				size *= 2;
    230 			m = realloc(m, size);
    231 			if (m == NULL)
    232 				errx(1, "realloc: %s", strerror(ENOMEM));
    233 		}
    234 		if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
    235 			errx(1, "data too short");
    236 		if (col == -1)
    237 			continue;
    238 
    239 		/* update context to match currently processed record */
    240 		ectx_sanify(ktr_header.ktr_pid);
    241 
    242 		switch (ktr_header.ktr_type) {
    243 		case KTR_SYSCALL:
    244 			ktrsyscall(m);
    245 			break;
    246 		case KTR_SYSRET:
    247 			ktrsysret(m, ktrlen);
    248 			break;
    249 		case KTR_NAMEI:
    250 			ktrnamei(m, ktrlen);
    251 			break;
    252 		case KTR_GENIO:
    253 			ktrgenio(m, ktrlen);
    254 			break;
    255 		case KTR_PSIG:
    256 			ktrpsig(m, ktrlen);
    257 			break;
    258 		case KTR_CSW:
    259 			ktrcsw(m);
    260 			break;
    261 		case KTR_EMUL:
    262 			ktremul(m, ktrlen, size);
    263 			break;
    264 		case KTR_USER:
    265 			ktruser(m, ktrlen);
    266 			break;
    267 		case KTR_MMSG:
    268 			ktrmmsg(m, ktrlen);
    269 			break;
    270 		case KTR_MOOL:
    271 			ktrmool(m, ktrlen);
    272 			break;
    273 		case KTR_EXEC_ARG:
    274 		case KTR_EXEC_ENV:
    275 			visdump_buf(m, ktrlen, col);
    276 			break;
    277 		default:
    278 			putchar('\n');
    279 			hexdump_buf(m, ktrlen, word_size ? word_size : 1);
    280 		}
    281 		if (tail)
    282 			(void)fflush(stdout);
    283 	}
    284 	return (0);
    285 }
    286 
    287 static int
    288 fread_tail(void *buf, size_t num, size_t size)
    289 {
    290 	int i;
    291 
    292 	while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
    293 		(void)sleep(1);
    294 		clearerr(stdin);
    295 	}
    296 	return (i);
    297 }
    298 
    299 static int
    300 dumpheader(struct ktr_header *kth)
    301 {
    302 	char unknown[64];
    303 	const char *type;
    304 	static struct timeval prevtime;
    305 	struct timeval temp;
    306 	int col;
    307 
    308 	switch (kth->ktr_type) {
    309 	case KTR_SYSCALL:
    310 		type = "CALL";
    311 		break;
    312 	case KTR_SYSRET:
    313 		type = "RET ";
    314 		break;
    315 	case KTR_NAMEI:
    316 		type = "NAMI";
    317 		break;
    318 	case KTR_GENIO:
    319 		type = "GIO ";
    320 		break;
    321 	case KTR_PSIG:
    322 		type = "PSIG";
    323 		break;
    324 	case KTR_CSW:
    325 		type = "CSW ";
    326 		break;
    327 	case KTR_EMUL:
    328 		type = "EMUL";
    329 		break;
    330 	case KTR_USER:
    331 		type = "USER";
    332 		break;
    333 	case KTR_MMSG:
    334 		type = "MMSG";
    335 		break;
    336 	case KTR_MOOL:
    337 		type = "MOOL";
    338 		break;
    339 	case KTR_EXEC_ENV:
    340 		type = "ENV";
    341 		break;
    342 	case KTR_EXEC_ARG:
    343 		type = "ARG";
    344 		break;
    345 	default:
    346 		(void)snprintf(unknown, sizeof(unknown), "UNKNOWN(%d)",
    347 		    kth->ktr_type);
    348 		type = unknown;
    349 	}
    350 
    351 	col = printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
    352 	if (timestamp) {
    353 		if (timestamp == 2) {
    354 			if (prevtime.tv_sec == 0)
    355 				temp.tv_sec = temp.tv_usec = 0;
    356 			else
    357 				timersub(&kth->ktr_time, &prevtime, &temp);
    358 			prevtime = kth->ktr_time;
    359 		} else
    360 			temp = kth->ktr_time;
    361 		col += printf("%ld.%06ld ",
    362 		    (long int)temp.tv_sec, (long int)temp.tv_usec);
    363 	}
    364 	col += printf("%-4s  ", type);
    365 	return col;
    366 }
    367 
    368 static void
    369 output_long(u_long it, int as_x)
    370 {
    371 	if (cur_emul->flags & EMUL_FLAG_NETBSD32)
    372 		printf(as_x ? "%#x" : "%d", (u_int)it);
    373 	else
    374 		printf(as_x ? "%#lx" : "%ld", it);
    375 }
    376 
    377 static void
    378 ioctldecode(u_long cmd)
    379 {
    380 	char dirbuf[4], *dir = dirbuf;
    381 
    382 	if (cmd & IOC_IN)
    383 		*dir++ = 'W';
    384 	if (cmd & IOC_OUT)
    385 		*dir++ = 'R';
    386 	*dir = '\0';
    387 
    388 	printf(",_IO%s('%c',", dirbuf, (int) ((cmd >> 8) & 0xff));
    389 	output_long(cmd & 0xff, decimal == 0);
    390 	if ((cmd & IOC_VOID) == 0) {
    391 		putchar(',');
    392 		output_long((cmd >> 16) & 0xff, decimal == 0);
    393 	}
    394 	putchar(')');
    395 }
    396 
    397 static void
    398 ktrsyscall(struct ktr_syscall *ktr)
    399 {
    400 	int argcount;
    401 	const struct emulation *emul = cur_emul;
    402 	register_t *ap;
    403 	char c;
    404 	const char *cp;
    405 	const char *sys_name;
    406 
    407 	argcount = ktr->ktr_argsize / sizeof (*ap);
    408 
    409 	emul_changed = 0;
    410 
    411 	if (numeric ||
    412 	    ((ktr->ktr_code >= emul->nsysnames || ktr->ktr_code < 0) &&
    413 	    mach_traps_dispatch(&ktr->ktr_code, &emul) == 0)) {
    414 		sys_name = "?";
    415 		(void)printf("[%d]", ktr->ktr_code);
    416 	} else {
    417 		sys_name = emul->sysnames[ktr->ktr_code];
    418 		(void)printf("%s", sys_name);
    419 	}
    420 #ifdef _LP64
    421 #define NETBSD32_	"netbsd32_"
    422 	if (cur_emul->flags & EMUL_FLAG_NETBSD32) {
    423 		size_t len = strlen(NETBSD32_);
    424 		if (strncmp(sys_name, NETBSD32_, len) == 0)
    425 			sys_name += len;
    426 	}
    427 #undef NETBSD32_
    428 #endif
    429 
    430 	ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
    431 	if (argcount) {
    432 		c = '(';
    433 		if (plain) {
    434 			;
    435 
    436 		} else if (strcmp(sys_name, "exit") == 0) {
    437 			ectx_delete();
    438 
    439 		} else if (strcmp(sys_name, "ioctl") == 0 && argcount >= 2) {
    440 			(void)putchar('(');
    441 			output_long((long)*ap, !(decimal || small(*ap)));
    442 			ap++;
    443 			argcount--;
    444 			if ((cp = ioctlname(*ap)) != NULL)
    445 				(void)printf(",%s", cp);
    446 			else
    447 				ioctldecode(*ap);
    448 			ap++;
    449 			argcount--;
    450 			c = ',';
    451 
    452 		} else if ((strstr(sys_name, "sigaction") != NULL ||
    453 		    strstr(sys_name, "sigvec") != NULL) && argcount >= 1) {
    454 			(void)printf("(SIG%s", signame(ap[0], 1));
    455 			ap += 1;
    456 			argcount -= 1;
    457 			c = ',';
    458 
    459 		} else if ((strcmp(sys_name, "kill") == 0 ||
    460 		    strcmp(sys_name, "killpg") == 0) && argcount >= 2) {
    461 			putchar('(');
    462 			output_long((long)ap[0], !(decimal || small(*ap)));
    463 			(void)printf(", SIG%s", signame(ap[1], 1));
    464 			ap += 2;
    465 			argcount -= 2;
    466 			c = ',';
    467 
    468 		} else if (strcmp(sys_name, "ptrace") == 0 && argcount >= 1) {
    469 			putchar('(');
    470 			if (strcmp(emul->name, "linux") == 0) {
    471 				if (*ap >= 0 && *ap <
    472 				    sizeof(linux_ptrace_ops) /
    473 				    sizeof(linux_ptrace_ops[0]))
    474 					(void)printf("%s",
    475 					    linux_ptrace_ops[*ap]);
    476 				else
    477 					output_long((long)*ap, 1);
    478 			} else {
    479 				if (*ap >= 0 && *ap <
    480 				    sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
    481 					(void)printf("%s", ptrace_ops[*ap]);
    482 #ifdef PT_MACHDEP_STRINGS
    483 				else if (*ap >= PT_FIRSTMACH &&
    484 				    *ap - PT_FIRSTMACH <
    485 						sizeof(ptrace_machdep_ops) /
    486 						sizeof(ptrace_machdep_ops[0]))
    487 					(void)printf("%s", ptrace_machdep_ops[*ap - PT_FIRSTMACH]);
    488 #endif
    489 				else
    490 					output_long((long)*ap, 1);
    491 			}
    492 			ap++;
    493 			argcount--;
    494 			c = ',';
    495 
    496 		}
    497 		while (argcount > 0) {
    498 			putchar(c);
    499 			output_long((long)*ap, !(decimal || small(*ap)));
    500 			ap++;
    501 			argcount--;
    502 			c = ',';
    503 		}
    504 		(void)putchar(')');
    505 	}
    506 	(void)putchar('\n');
    507 }
    508 
    509 static void
    510 ktrsysret(struct ktr_sysret *ktr, int len)
    511 {
    512 	const struct emulation *emul;
    513 	int error = ktr->ktr_error;
    514 	int code = ktr->ktr_code;
    515 
    516 	if (emul_changed)  {
    517 		/* In order to get system call name right in execve return */
    518 		emul = prev_emul;
    519 		emul_changed = 0;
    520 	} else
    521 		emul = cur_emul;
    522 
    523 	if ((code >= emul->nsysnames || code < 0 || plain > 1) &&
    524 	    (mach_traps_dispatch(&code, &emul) == 0))
    525 		(void)printf("[%d] ", code);
    526 	else
    527 		(void)printf("%s ", emul->sysnames[code]);
    528 
    529 	switch (error) {
    530 	case 0:
    531 		rprint(ktr->ktr_retval);
    532 		if (len > offsetof(struct ktr_sysret, ktr_retval_1) &&
    533 		    ktr->ktr_retval_1 != 0) {
    534 			(void)printf(", ");
    535 			rprint(ktr->ktr_retval_1);
    536 		}
    537 		break;
    538 
    539 	default:
    540 		eprint(error);
    541 		break;
    542 	}
    543 	(void)putchar('\n');
    544 }
    545 
    546 static void
    547 rprint(register_t ret)
    548 {
    549 
    550 	if (!plain) {
    551 		(void)printf("%ld", (long)ret);
    552 		if (!small(ret))
    553 			(void)printf("/%#lx", (long)ret);
    554 	} else {
    555 		if (decimal || small(ret))
    556 			(void)printf("%ld", (long)ret);
    557 		else
    558 			(void)printf("%#lx", (long)ret);
    559 	}
    560 }
    561 
    562 /*
    563  * We print the original emulation's error numerically, but we
    564  * translate it to netbsd to print it symbolically.
    565  */
    566 static void
    567 eprint(int e)
    568 {
    569 	int i = e;
    570 
    571 	if (cur_emul->errnomap) {
    572 
    573 		/* No remapping for ERESTART and EJUSTRETURN */
    574 		/* Kludge for linux that has negative error numbers */
    575 		if (cur_emul->errnomap[2] > 0 && e < 0)
    576 			goto normal;
    577 
    578 		for (i = 0; i < cur_emul->nerrnomap; i++)
    579 			if (e == cur_emul->errnomap[i])
    580 				break;
    581 
    582 		if (i == cur_emul->nerrnomap) {
    583 			printf("-1 unknown errno %d", e);
    584 			return;
    585 		}
    586 	}
    587 
    588 normal:
    589 	switch (i) {
    590 	case ERESTART:
    591 		(void)printf("RESTART");
    592 		break;
    593 
    594 	case EJUSTRETURN:
    595 		(void)printf("JUSTRETURN");
    596 		break;
    597 
    598 	default:
    599 		(void)printf("-1 errno %d", e);
    600 		if (!plain)
    601 			(void)printf(" %s", strerror(i));
    602 	}
    603 }
    604 
    605 static void
    606 ktrnamei(char *cp, int len)
    607 {
    608 
    609 	(void)printf("\"%.*s\"\n", len, cp);
    610 }
    611 
    612 static void
    613 ktremul(char *name, int len, int bufsize)
    614 {
    615 
    616 	if (len >= bufsize)
    617 		len = bufsize - 1;
    618 
    619 	name[len] = '\0';
    620 	setemul(name, ktr_header.ktr_pid, 1);
    621 	emul_changed = 1;
    622 
    623 	(void)printf("\"%s\"\n", name);
    624 }
    625 
    626 static void
    627 hexdump_buf(const void *vdp, int datalen, int word_sz)
    628 {
    629 	const char hex[] = "0123456789abcdef";
    630 	char chars[16], prev[16];
    631 	char bytes[16 * 3 + 4];
    632 	const unsigned char *dp = vdp;
    633 	const unsigned char *datalim = dp + datalen;
    634 	const unsigned char *line_end;
    635 	int off, l = 0, c;
    636 	char *cp, *bp;
    637 	int divmask = word_sz - 1;	/* block size in bytes */
    638 	int gdelim = 3;			/* gap between blocks */
    639 	int bsize = 2;			/* increment for each byte */
    640 	int width;
    641 	int dupl = 0;
    642 #if _BYTE_ORDER == _LITTLE_ENDIAN
    643 	int bswap = word_sz - 1;
    644 #else
    645 #define	bswap 0
    646 #endif
    647 
    648 	switch (word_sz) {
    649 	case 2:
    650 		gdelim = 2;
    651 		break;
    652 	case 1:
    653 		divmask = 7;
    654 		bsize = 3;
    655 		gdelim = 1;
    656 		break;
    657 	default:
    658 		break;
    659 	}
    660 	width = 16 * bsize + (16 / (divmask + 1)) * gdelim;
    661 	if (word_sz != 1)
    662 		width += 2;
    663 
    664 	for (off = 0; dp < datalim; off += l) {
    665 		memset(bytes, ' ', sizeof bytes);
    666 		line_end = dp + 16;
    667 		if (line_end >= datalim) {
    668 			line_end = datalim;
    669 			dupl |= 1;	/* need to print */
    670 		} else {
    671 			if (dupl == 0 || memcmp(dp, prev, sizeof chars))
    672 				dupl |= 1;
    673 		}
    674 
    675 		if (!(dupl & 1)) {
    676 			/* This is a duplicate of the line above, count 'em */
    677 			dupl += 2;
    678 			dp = line_end;
    679 			continue;
    680 		}
    681 
    682 		if (dupl > 3) {
    683 			/* previous line as a duplicate */
    684 			if (dupl == 5)
    685 				/* Only one duplicate, print line */
    686 				printf("\t%-5.3x%.*s%.*s\n",
    687 					off - l, width, bytes, l, chars);
    688 			else
    689 				printf("\t%.*s\n",
    690 					snprintf(NULL, 0, "%3x", off), "*****");
    691 		}
    692 
    693 		for (l = 0, bp = bytes, cp = chars; dp < line_end; l++) {
    694 			c = *dp++;
    695 			prev[l] = c;
    696 			if ((l & divmask) == 0)
    697 				bp += gdelim;
    698 			bp[(l ^ bswap) * bsize] = hex[c >> 4];
    699 			bp[(l ^ bswap) * bsize + 1] = hex[c & 0xf];
    700 			*cp++ = isgraph(c) ? c : '.';
    701 		}
    702 
    703 		printf("\t%-5.3x%.*s%.*s\n", off, width, bytes, l, chars);
    704 		dupl = 2;
    705 	}
    706 }
    707 
    708 static void
    709 visdump_buf(const void *vdp, int datalen, int col)
    710 {
    711 	const unsigned char *dp = vdp;
    712 	char *cp;
    713 	int width;
    714 	char visbuf[5];
    715 	static int screenwidth = 0;
    716 
    717 	if (screenwidth == 0) {
    718 		struct winsize ws;
    719 
    720 		if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
    721 		    ws.ws_col > 8)
    722 			screenwidth = ws.ws_col;
    723 		else
    724 			screenwidth = 80;
    725 	}
    726 
    727 	(void)printf("\"");
    728 	col++;
    729 	for (; datalen > 0; datalen--, dp++) {
    730 		(void)svis(visbuf, *dp, VIS_CSTYLE,
    731 		    datalen > 1 ? *(dp + 1) : 0, "\"");
    732 		cp = visbuf;
    733 		/*
    734 		 * Keep track of printables and
    735 		 * space chars (like fold(1)).
    736 		 */
    737 		if (col == 0) {
    738 			(void)putchar('\t');
    739 			col = 8;
    740 		}
    741 		switch (*cp) {
    742 		case '\n':
    743 			col = 0;
    744 			(void)putchar('\n');
    745 			continue;
    746 		case '\t':
    747 			width = 8 - (col & 07);
    748 			break;
    749 		default:
    750 			width = strlen(cp);
    751 		}
    752 		if (col + width > (screenwidth - 2)) {
    753 			(void)printf("\\\n\t");
    754 			col = 8;
    755 			if (*cp == '\t')
    756 				width = 8;
    757 		}
    758 		col += width;
    759 		do {
    760 			(void)putchar(*cp++);
    761 		} while (*cp);
    762 	}
    763 	if (col == 0)
    764 		(void)printf("       ");
    765 	(void)printf("\"\n");
    766 }
    767 
    768 static void
    769 ktrgenio(struct ktr_genio *ktr, int len)
    770 {
    771 	int datalen = len - sizeof (struct ktr_genio);
    772 	char *dp = (char *)ktr + sizeof (struct ktr_genio);
    773 
    774 	printf("fd %d %s %d bytes\n", ktr->ktr_fd,
    775 	    ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
    776 	if (maxdata == 0)
    777 		return;
    778 	if (maxdata > 0 && datalen > maxdata)
    779 		datalen = maxdata;
    780 	if (word_size) {
    781 		hexdump_buf(dp, datalen, word_size);
    782 		return;
    783 	}
    784 	(void)printf("       ");
    785 	visdump_buf(dp, datalen, 7);
    786 }
    787 
    788 static void
    789 ktrpsig(void *v, int len)
    790 {
    791 	int signo, first;
    792 	struct {
    793 		struct ktr_psig ps;
    794 		siginfo_t si;
    795 	} *psig = v;
    796 	siginfo_t *si = &psig->si;
    797 	const char *code;
    798 
    799 	(void)printf("SIG%s ", signame(psig->ps.signo, 0));
    800 	if (psig->ps.action == SIG_DFL)
    801 		(void)printf("SIG_DFL");
    802 	else {
    803 		(void)printf("caught handler=%p mask=(", psig->ps.action);
    804 		first = 1;
    805 		for (signo = 1; signo < NSIG; signo++) {
    806 			if (sigismember(&psig->ps.mask, signo)) {
    807 				if (first)
    808 					first = 0;
    809 				else
    810 					(void)printf(",");
    811 				(void)printf("%d", signo);
    812 			}
    813 		}
    814 		(void)printf(")");
    815 	}
    816 	switch (len) {
    817 	case sizeof(struct ktr_psig):
    818 		if (psig->ps.code)
    819 			printf(" code=0x%x", psig->ps.code);
    820 		printf(psig->ps.action == SIG_DFL ? "\n" : ")\n");
    821 		return;
    822 	case sizeof(*psig):
    823 		if (si->si_code == 0) {
    824 			printf(": code=SI_USER sent by pid=%d, uid=%d)\n",
    825 			    si->si_pid, si->si_uid);
    826 			return;
    827 		}
    828 
    829 		if (si->si_code < 0) {
    830 			switch (si->si_code) {
    831 			case SI_TIMER:
    832 				printf(": code=SI_TIMER sigval %p)\n",
    833 				    si->si_sigval.sival_ptr);
    834 				return;
    835 			case SI_QUEUE:
    836 				code = "SI_QUEUE";
    837 				break;
    838 			case SI_ASYNCIO:
    839 				code = "SI_ASYNCIO";
    840 				break;
    841 			case SI_MESGQ:
    842 				code = "SI_MESGQ";
    843 				break;
    844 			default:
    845 				code = NULL;
    846 				break;
    847 			}
    848 			if (code)
    849 				printf(": code=%s unimplemented)\n", code);
    850 			else
    851 				printf(": code=%d unimplemented)\n",
    852 				    si->si_code);
    853 			return;
    854 		}
    855 
    856 		code = siginfocodename(si->si_signo, si->si_code);
    857 		switch (si->si_signo) {
    858 		case SIGCHLD:
    859 			printf(": code=%s child pid=%d, uid=%d, "
    860 			    " status=%u, utime=%lu, stime=%lu)\n",
    861 			    code, si->si_pid,
    862 			    si->si_uid, si->si_status,
    863 			    (unsigned long) si->si_utime,
    864 			    (unsigned long) si->si_stime);
    865 			return;
    866 		case SIGILL:
    867 		case SIGFPE:
    868 		case SIGSEGV:
    869 		case SIGBUS:
    870 		case SIGTRAP:
    871 			printf(": code=%s, addr=%p, trap=%d)\n",
    872 			    code, si->si_addr, si->si_trap);
    873 			return;
    874 		case SIGIO:
    875 			printf(": code=%s, fd=%d, band=%lx)\n",
    876 			    code, si->si_fd, si->si_band);
    877 			return;
    878 		default:
    879 			printf(": code=%s, errno=%d)\n",
    880 			    code, si->si_errno);
    881 			return;
    882 		}
    883 		/*NOTREACHED*/
    884 	default:
    885 		warnx("Unhandled size %d for ktrpsig\n", len);
    886 		break;
    887 	}
    888 }
    889 
    890 static void
    891 ktrcsw(struct ktr_csw *cs)
    892 {
    893 
    894 	(void)printf("%s %s\n", cs->out ? "stop" : "resume",
    895 	    cs->user ? "user" : "kernel");
    896 }
    897 
    898 static void
    899 ktruser(struct ktr_user *usr, int len)
    900 {
    901 	int i;
    902 	unsigned char *dta;
    903 
    904 	printf("\"%.*s: %d, ", KTR_USER_MAXIDLEN, usr->ktr_id, len);
    905 	dta = (unsigned char *)usr;
    906 	for (i = sizeof(struct ktr_user); i < len; i++)
    907 		printf("%02x", (unsigned int) dta[i]);
    908 	printf("\"\n");
    909 }
    910 
    911 static void
    912 ktrmmsg(struct ktr_mmsg *mmsg, int len)
    913 {
    914 	const char *service_name;
    915 	const char *reply;
    916 	int id;
    917 
    918 	id = mmsg->ktr_id;
    919 	if ((id / 100) % 2) {  /* Message reply */
    920 		reply = " reply";
    921 		id -= 100;
    922 	} else {
    923 		reply = "";
    924 	}
    925 
    926 	if ((service_name = mach_service_name(id)) != NULL)
    927 		printf("%s%s [%d]\n", service_name, reply, mmsg->ktr_id);
    928 	else
    929 		printf("unknown service%s [%d]\n", reply, mmsg->ktr_id);
    930 
    931 	hexdump_buf(mmsg, len, word_size ? word_size : 4);
    932 }
    933 
    934 static void
    935 ktrmool(struct ktr_mool *mool, int len)
    936 {
    937 	size_t size = mool->size;
    938 
    939 	printf("%ld/0x%lx bytes at %p\n",
    940 	    (u_long)size, (u_long)size, mool->uaddr);
    941 	mool++;
    942 	hexdump_buf(mool, size, word_size ? word_size : 4);
    943 }
    944 
    945 static const char *
    946 signame(long sig, int xlat)
    947 {
    948 	static char buf[64];
    949 
    950 	if (sig == 0)
    951 		return " 0";
    952 	else if (sig < 0 || sig >= NSIG) {
    953 		(void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig);
    954 		return buf;
    955 	} else
    956 		return sys_signame[(xlat && cur_emul->signalmap != NULL) ?
    957 		    cur_emul->signalmap[sig] : sig];
    958 }
    959 
    960 static void
    961 usage(void)
    962 {
    963 
    964 	(void)fprintf(stderr, "Usage: %s [-dlNnRT] [-e emulation] "
    965 	   "[-f file] [-m maxdata] [-p pid]\n             [-t trstr] "
    966 	   "[-x | -X size] [file]\n", getprogname());
    967 	exit(1);
    968 }
    969