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