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