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