Home | History | Annotate | Line # | Download | only in kdump
kdump.c revision 1.71
      1 /*	$NetBSD: kdump.c,v 1.71 2004/01/12 13:39:56 mrg 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.71 2004/01/12 13:39:56 mrg 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;
    390 	const struct emulation *emul = cur_emul;
    391 	register_t *ap;
    392 	char c;
    393 	char *cp;
    394 	const char *sys_name;
    395 
    396 	if (emul->flags & EMUL_FLAG_NETBSD32)
    397 		/* XXX: should use register32_t ? */
    398 		argcount = ktr->ktr_argsize / (sizeof (register_t) / 2);
    399 	else
    400 		argcount = ktr->ktr_argsize / sizeof (register_t);
    401 	emul_changed = 0;
    402 
    403 	if (((ktr->ktr_code >= emul->nsysnames || ktr->ktr_code < 0)
    404 	    && (mach_traps_dispatch(&ktr->ktr_code, &emul) == 0)) ||
    405 	    numeric) {
    406 		sys_name = "?";
    407 		(void)printf("[%d]", ktr->ktr_code);
    408 	} else {
    409 		sys_name = emul->sysnames[ktr->ktr_code];
    410 		(void)printf("%s", sys_name);
    411 	}
    412 	ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
    413 	if (argcount) {
    414 		c = '(';
    415 		if (plain) {
    416 			;
    417 
    418 		} else if (strcmp(sys_name, "exit") == 0) {
    419 			ectx_delete();
    420 
    421 		} else if ((strcmp(sys_name, "ioctl") == 0 ||
    422 			    strcmp(sys_name, "netbsd32_ioctl") == 0) &&
    423 			   argcount >= 2) {
    424 			if (decimal || *ap <= 9)
    425 				(void)printf("(%ld", (long)*ap);
    426 			else
    427 				(void)printf("(%#lx", (long)*ap);
    428 			ap++;
    429 			argcount--;
    430 			if ((cp = ioctlname(*ap)) != NULL)
    431 				(void)printf(",%s", cp);
    432 			else
    433 				ioctldecode(*ap);
    434 			ap++;
    435 			argcount--;
    436 			c = ',';
    437 
    438 		} else if (strcmp(sys_name, "kill") == 0 && argcount >= 2) {
    439 			if (decimal || *ap <= 9)
    440 				(void)printf("(%ld, SIG%s",
    441 				    (long)ap[0], signame(ap[1], 1));
    442 			else
    443 				(void)printf("(%#lx, SIG%s",
    444 				    (long)ap[0], signame(ap[1], 1));
    445 			ap += 2;
    446 			argcount -= 2;
    447 			c = ',';
    448 
    449 		} else if (strcmp(sys_name, "ptrace") == 0 && argcount >= 1) {
    450 			if (strcmp(emul->name, "linux") == 0) {
    451 				  if (*ap >= 0 && *ap <=
    452 				      sizeof(linux_ptrace_ops) /
    453 				      sizeof(linux_ptrace_ops[0]))
    454 					(void)printf("(%s",
    455 					    linux_ptrace_ops[*ap]);
    456 				  else
    457 					(void)printf("(%ld", (long)*ap);
    458 			} else {
    459 				  if (*ap >= 0 && *ap <=
    460 				    sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
    461 					(void)printf("(%s", ptrace_ops[*ap]);
    462 				  else
    463 					(void)printf("(%ld", (long)*ap);
    464 			}
    465 			ap++;
    466 			argcount--;
    467 			c = ',';
    468 
    469 		}
    470 		while (argcount > 0) {
    471 			if (decimal || *ap <= 9)
    472 				(void)printf("%c%ld", c, (long)*ap);
    473 			else
    474 				(void)printf("%c%#lx", c, (long)*ap);
    475 			ap++;
    476 			argcount--;
    477 			c = ',';
    478 		}
    479 		(void)putchar(')');
    480 	}
    481 	(void)putchar('\n');
    482 }
    483 
    484 void
    485 ktrsysret(ktr, len)
    486 	struct ktr_sysret *ktr;
    487 	int len;
    488 {
    489 	const struct emulation *emul;
    490 	int error = ktr->ktr_error;
    491 	int code = ktr->ktr_code;
    492 
    493 	if (emul_changed)  {
    494 		/* In order to get system call name right in execve return */
    495 		emul = prev_emul;
    496 		emul_changed = 0;
    497 	} else
    498 		emul = cur_emul;
    499 
    500 	if ((code >= emul->nsysnames || code < 0 || plain > 1)
    501 	    && (mach_traps_dispatch(&code, &emul) == 0))
    502 		(void)printf("[%d] ", code);
    503 	else
    504 		(void)printf("%s ", emul->sysnames[code]);
    505 
    506 	switch (error) {
    507 	case 0:
    508 		rprint(ktr->ktr_retval);
    509 		if (len > offsetof(struct ktr_sysret, ktr_retval_1) &&
    510 		    ktr->ktr_retval_1 != 0) {
    511 			(void)printf(", ");
    512 			rprint(ktr->ktr_retval_1);
    513 		}
    514 		break;
    515 
    516 	default:
    517 		eprint(error);
    518 		break;
    519 	}
    520 	(void)putchar('\n');
    521 }
    522 
    523 void
    524 rprint(register_t ret)
    525 {
    526 	if (!plain) {
    527 		(void)printf("%ld", (long)ret);
    528 		if (ret < 0 || ret > 9)
    529 			(void)printf("/%#lx", (long)ret);
    530 	} else {
    531 		if (decimal || ret <= 9)
    532 			(void)printf("%ld", (long)ret);
    533 		else
    534 			(void)printf("%#lx", (long)ret);
    535 	}
    536 }
    537 
    538 /*
    539  * We print the original emulation's error numerically, but we
    540  * translate it to netbsd to print it symbolically.
    541  */
    542 void
    543 eprint(e)
    544 	int e;
    545 {
    546 	int i = e;
    547 
    548 	if (cur_emul->errnomap) {
    549 
    550 		/* No remapping for ERESTART and EJUSTRETURN */
    551 		/* Kludge for linux that has negative error numbers */
    552 		if (cur_emul->errnomap[2] > 0 && e < 0)
    553 			goto normal;
    554 
    555 		for (i = 0; i < cur_emul->nerrnomap; i++)
    556 			if (e == cur_emul->errnomap[i])
    557 				break;
    558 
    559 		if (i == cur_emul->nerrnomap) {
    560 			printf("-1 unknown errno %d", e);
    561 			return;
    562 		}
    563 	}
    564 
    565 normal:
    566 	switch (i) {
    567 	case ERESTART:
    568 		(void)printf("RESTART");
    569 		break;
    570 
    571 	case EJUSTRETURN:
    572 		(void)printf("JUSTRETURN");
    573 		break;
    574 
    575 	default:
    576 		(void)printf("-1 errno %d", e);
    577 		if (!plain)
    578 			(void)printf(" %s", strerror(i));
    579 	}
    580 }
    581 
    582 void
    583 ktrnamei(cp, len)
    584 	char *cp;
    585 	int len;
    586 {
    587 
    588 	(void)printf("\"%.*s\"\n", len, cp);
    589 }
    590 
    591 void
    592 ktremul(name, len, bufsize)
    593 	char *name;
    594 	int len, bufsize;
    595 {
    596 	if (len >= bufsize)
    597 		len = bufsize - 1;
    598 
    599 	name[len] = '\0';
    600 	setemul(name, ktr_header.ktr_pid, 1);
    601 	emul_changed = 1;
    602 
    603 	(void)printf("\"%s\"\n", name);
    604 }
    605 
    606 static void
    607 hexdump_buf(vdp, datalen, word_sz)
    608 	const void *vdp;
    609 	int datalen;
    610 	int word_sz;
    611 {
    612 	const char hex[] = "0123456789abcdef";
    613 	char chars[16];
    614 	char bytes[16 * 3 + 4];
    615 	const unsigned char *dp = vdp;
    616 	const unsigned char *datalim = dp + datalen;
    617 	const unsigned char *line_end;
    618 	int off, l, c;
    619 	char *cp, *bp;
    620 	int divmask = word_sz - 1;	/* block size in bytes */
    621 	int gdelim = 3;			/* gap between blocks */
    622 	int bsize = 2;			/* increment for each byte */
    623 	int width;
    624 #if _BYTE_ORDER == _LITTLE_ENDIAN
    625 	int bswap = word_sz - 1;
    626 #else
    627 #define	bswap 0
    628 #endif
    629 
    630 	switch (word_sz) {
    631 	case 2:
    632 		gdelim = 2;
    633 		break;
    634 	case 1:
    635 		divmask = 7;
    636 		bsize = 3;
    637 		gdelim = 1;
    638 		break;
    639 	default:
    640 		break;
    641 	}
    642 	width = 16 * bsize + (16 / (divmask + 1)) * gdelim;
    643 	if (word_size != 1)
    644 		width += 2;
    645 
    646 	for (off = 0; dp < datalim; off += l) {
    647 		memset(bytes, ' ', sizeof bytes);
    648 		line_end = dp + 16;
    649 		if (line_end > datalim)
    650 			line_end = datalim;
    651 
    652 		for (l = 0, bp = bytes, cp = chars; dp < line_end; l++) {
    653 			c = *dp++;
    654 			if ((l & divmask) == 0)
    655 				bp += gdelim;
    656 			bp[(l ^ bswap) * bsize] = hex[c >> 4];
    657 			bp[(l ^ bswap) * bsize + 1] = hex[c & 0xf];
    658 			*cp++ = isgraph(c) ? c : '.';
    659 		};
    660 
    661 		printf("\t%-5.3x%.*s%.*s\n", off, width, bytes, l, chars);
    662 	}
    663 }
    664 
    665 static void
    666 visdump_buf(const void *vdp, int datalen, int col)
    667 {
    668 	const unsigned char *dp = vdp;
    669 	char *cp;
    670 	int width;
    671 	char visbuf[5];
    672 	static int screenwidth = 0;
    673 
    674 	if (screenwidth == 0) {
    675 		struct winsize ws;
    676 
    677 		if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
    678 		    ws.ws_col > 8)
    679 			screenwidth = ws.ws_col;
    680 		else
    681 			screenwidth = 80;
    682 	}
    683 
    684 	(void)printf("\"");
    685 	col++;
    686 	for (; datalen > 0; datalen--, dp++) {
    687 		(void)svis(visbuf, *dp, VIS_CSTYLE,
    688 		    datalen > 1 ? *(dp + 1) : 0, "\"");
    689 		cp = visbuf;
    690 		/*
    691 		 * Keep track of printables and
    692 		 * space chars (like fold(1)).
    693 		 */
    694 		if (col == 0) {
    695 			(void)putchar('\t');
    696 			col = 8;
    697 		}
    698 		switch(*cp) {
    699 		case '\n':
    700 			col = 0;
    701 			(void)putchar('\n');
    702 			continue;
    703 		case '\t':
    704 			width = 8 - (col & 07);
    705 			break;
    706 		default:
    707 			width = strlen(cp);
    708 		}
    709 		if (col + width > (screenwidth - 2)) {
    710 			(void)printf("\\\n\t");
    711 			col = 8;
    712 		}
    713 		col += width;
    714 		do {
    715 			(void)putchar(*cp++);
    716 		} while (*cp);
    717 	}
    718 	if (col == 0)
    719 		(void)printf("       ");
    720 	(void)printf("\"\n");
    721 }
    722 
    723 void
    724 ktrgenio(ktr, len)
    725 	struct ktr_genio *ktr;
    726 	int len;
    727 {
    728 	int datalen = len - sizeof (struct ktr_genio);
    729 	char *dp = (char *)ktr + sizeof (struct ktr_genio);
    730 
    731 	printf("fd %d %s %d bytes\n", ktr->ktr_fd,
    732 		ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
    733 	if (maxdata == 0)
    734 		return;
    735 	if (maxdata > 0 && datalen > maxdata)
    736 		datalen = maxdata;
    737 	if (word_size) {
    738 		hexdump_buf(dp, datalen, word_size);
    739 		return;
    740 	}
    741 	(void)printf("       ");
    742 	visdump_buf(dp, datalen, 7);
    743 }
    744 
    745 void
    746 ktrpsig(v, len)
    747 	void *v;
    748 	int len;
    749 {
    750 	int signo, first;
    751 	struct {
    752 		struct ktr_psig ps;
    753 		siginfo_t si;
    754 	} *psig = v;
    755 	siginfo_t *si = &psig->si;
    756 	const char *code;
    757 
    758 	(void)printf("SIG%s ", signame(psig->ps.signo, 0));
    759 	if (psig->ps.action == SIG_DFL)
    760 		(void)printf("SIG_DFL");
    761 	else {
    762 		(void)printf("caught handler=%p mask=(", psig->ps.action);
    763 		first = 1;
    764 		for (signo = 1; signo < NSIG; signo++) {
    765 			if (sigismember(&psig->ps.mask, signo)) {
    766 				if (first)
    767 					first = 0;
    768 				else
    769 					(void)printf(",");
    770 				(void)printf("%d", signo);
    771 			}
    772 		}
    773 		(void)printf(")");
    774 	}
    775 	switch (len) {
    776 	case sizeof(struct ktr_psig):
    777 		if (psig->ps.code)
    778 			printf(" code=0x%x", psig->ps.code);
    779 		printf(psig->ps.action == SIG_DFL ? "\n" : ")\n");
    780 		return;
    781 	case sizeof(*psig):
    782 		if (si->si_code == 0) {
    783 			printf(": code=SI_USER sent by pid=%d, uid=%d)\n",
    784 			    si->si_pid, si->si_uid);
    785 			return;
    786 		}
    787 
    788 		if (si->si_code < 0) {
    789 			switch (si->si_code) {
    790 			case SI_TIMER:
    791 				printf(": code=SI_TIMER sigval %p)\n",
    792 				    si->si_sigval.sival_ptr);
    793 				return;
    794 			case SI_QUEUE:
    795 				code = "SI_QUEUE";
    796 				break;
    797 			case SI_ASYNCIO:
    798 				code = "SI_ASYNCIO";
    799 				break;
    800 			case SI_MESGQ:
    801 				code = "SI_MESGQ";
    802 				break;
    803 			default:
    804 				code = NULL;
    805 				break;
    806 			}
    807 			if (code)
    808 				printf(": code=%s unimplemented)\n", code);
    809 			else
    810 				printf(": code=%d unimplemented)\n",
    811 				    si->si_code);
    812 			return;
    813 		}
    814 
    815 		code = siginfocodename(si->si_signo, si->si_code);
    816 		switch (si->si_signo) {
    817 		case SIGCHLD:
    818 			printf(": code=%s child pid=%d, uid=%d, "
    819 			    " status=%u, utime=%lu, stime=%lu)\n",
    820 			    code, si->si_pid,
    821 			    si->si_uid, si->si_status,
    822 			    (unsigned long) si->si_utime,
    823 			    (unsigned long) si->si_stime);
    824 			return;
    825 		case SIGILL:
    826 		case SIGFPE:
    827 		case SIGSEGV:
    828 		case SIGBUS:
    829 		case SIGTRAP:
    830 			printf(": code=%s, addr=%p, trap=%d)\n",
    831 			    code, si->si_addr, si->si_trap);
    832 			return;
    833 		case SIGIO:
    834 			printf(": code=%s, fd=%d, band=%lx)\n",
    835 			    code, si->si_fd, si->si_band);
    836 			return;
    837 		default:
    838 			printf(": code=%s, errno=%d)\n",
    839 			    code, si->si_errno);
    840 			return;
    841 		}
    842 		/*NOTREACHED*/
    843 	default:
    844 		warnx("Unhandled size %d for ktrpsig\n", len);
    845 		break;
    846 	}
    847 }
    848 
    849 void
    850 ktrcsw(cs)
    851 	struct ktr_csw *cs;
    852 {
    853 
    854 	(void)printf("%s %s\n", cs->out ? "stop" : "resume",
    855 	    cs->user ? "user" : "kernel");
    856 }
    857 
    858 void
    859 ktruser(usr, len)
    860 	struct ktr_user *usr;
    861 	int len;
    862 {
    863 	int i;
    864 	unsigned char *dta;
    865 
    866 	printf("\"%.*s: %d, ", KTR_USER_MAXIDLEN, usr->ktr_id, len);
    867 	dta = (unsigned char *)usr;
    868 	for(i=sizeof(struct ktr_user); i < len; i++)
    869 		printf("%02x", (unsigned int) dta[i]);
    870 	printf("\"\n");
    871 }
    872 
    873 void
    874 ktrmmsg(mmsg, len)
    875 	struct ktr_mmsg *mmsg;
    876 	int len;
    877 {
    878 	const char *service_name;
    879 	char *reply;
    880 	int id;
    881 
    882 	id = mmsg->ktr_id;
    883 	if ((id / 100) % 2) {  /* Message reply */
    884 		reply = " reply";
    885 		id -= 100;
    886 	} else {
    887 		reply = "";
    888 	}
    889 
    890 	if ((service_name = mach_service_name(id)) != NULL)
    891 		printf("%s%s [%d]\n", service_name, reply, mmsg->ktr_id);
    892 	else
    893 		printf("unknown service%s [%d]\n", reply, mmsg->ktr_id);
    894 
    895 	hexdump_buf(mmsg, len, word_size ? word_size : 4);
    896 }
    897 
    898 void
    899 ktrmool(mool, len)
    900 	struct ktr_mool *mool;
    901 	int len;
    902 {
    903 	size_t size = mool->size;
    904 
    905 	printf("%ld/0x%lx bytes at %p\n",
    906 	    (u_long)size, (u_long)size, mool->uaddr);
    907 	mool++;
    908 	hexdump_buf(mool, size, word_size ? word_size : 4);
    909 }
    910 
    911 static const char *
    912 signame(long sig, int xlat)
    913 {
    914 	static char buf[64];
    915 	if (sig == 0)
    916 		return " 0";
    917 	else if (sig < 0 || sig >= NSIG) {
    918 		(void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig);
    919 		return buf;
    920 	} else
    921 		return sys_signame[(xlat && cur_emul->signalmap != NULL) ?
    922 		    cur_emul->signalmap[sig] : sig];
    923 }
    924 
    925 void
    926 usage()
    927 {
    928 
    929 	(void)fprintf(stderr, "usage: kdump [-dlNnRT] [-e emulation] "
    930 	   "[-f file] [-m maxdata] [-p pid]\n             [-t trstr] "
    931 	   "[-x | -X size] [file]\n");
    932 	exit(1);
    933 }
    934