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