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