Home | History | Annotate | Line # | Download | only in kdump
kdump.c revision 1.77
      1 /*	$NetBSD: kdump.c,v 1.77 2004/03/07 17:20:53 dsl Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
     35 	The Regents of the University of California.  All rights reserved.\n");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)kdump.c	8.4 (Berkeley) 4/28/95";
     41 #else
     42 __RCSID("$NetBSD: kdump.c,v 1.77 2004/03/07 17:20:53 dsl Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #define _KERNEL
     48 #include <sys/errno.h>
     49 #undef _KERNEL
     50 #include <sys/time.h>
     51 #include <sys/uio.h>
     52 #include <sys/ktrace.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/ptrace.h>
     55 
     56 #include <ctype.h>
     57 #include <err.h>
     58 #include <signal.h>
     59 #include <stddef.h>
     60 #include <stdio.h>
     61 #include <stdlib.h>
     62 #include <string.h>
     63 #include <unistd.h>
     64 #include <vis.h>
     65 
     66 #include "ktrace.h"
     67 #include "setemul.h"
     68 
     69 #include <sys/syscall.h>
     70 
     71 int timestamp, decimal, plain, tail, maxdata = -1, numeric;
     72 int word_size = 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 #define small(v)	(((long)(v) >= 0) && ((long)(v) < 10))
     80 
     81 static const char *ptrace_ops[] = {
     82 	"PT_TRACE_ME",	"PT_READ_I",	"PT_READ_D",	"PT_READ_U",
     83 	"PT_WRITE_I",	"PT_WRITE_D",	"PT_WRITE_U",	"PT_CONTINUE",
     84 	"PT_KILL",	"PT_ATTACH",	"PT_DETACH",
     85 };
     86 
     87 static const char *linux_ptrace_ops[] = {
     88 	"PTRACE_TRACEME",
     89 	"PTRACE_PEEKTEXT", "PTRACE_PEEKDATA", "PTRACE_PEEKUSER",
     90 	"PTRACE_POKETEXT", "PTRACE_POKEDATA", "PTRACE_POKEUSER",
     91 	"PTRACE_CONT", "PTRACE_KILL", "PTRACE_SINGLESTEP",
     92 	NULL, NULL,
     93 	"PTRACE_GETREGS", "PTRACE_SETREGS", "PTRACE_GETFPREGS",
     94 	"PTRACE_SETFPREGS", "PTRACE_ATTACH", "PTRACE_DETACH",
     95 	"PTRACE_SYSCALL",
     96 };
     97 
     98 int	main(int, char **);
     99 int	fread_tail(char *, int, int);
    100 int	dumpheader(struct ktr_header *);
    101 void	output_long(u_long, int);
    102 void	ioctldecode(u_long);
    103 void	ktrsyscall(struct ktr_syscall *);
    104 void	ktrsyscall_size(struct ktr_syscall *);
    105 void	ktrsysret(struct ktr_sysret *, int);
    106 void	ktrnamei(char *, int);
    107 void	ktremul(char *, int, int);
    108 void	ktrgenio(struct ktr_genio *, int);
    109 void	ktrpsig(void *, int);
    110 void	ktrcsw(struct ktr_csw *);
    111 void	ktruser(struct ktr_user *, int);
    112 void	ktrmmsg(struct ktr_mmsg *, int);
    113 void	ktrmool(struct ktr_mool *, int);
    114 void	usage(void);
    115 void	eprint(int);
    116 void	rprint(register_t);
    117 char	*ioctlname(long);
    118 static const char *signame(long, int);
    119 static void hexdump_buf(const void *, int, int);
    120 static void visdump_buf(const void *, int, int);
    121 
    122 int
    123 main(argc, argv)
    124 	int argc;
    125 	char **argv;
    126 {
    127 	int ch, ktrlen, size;
    128 	void *m;
    129 	int trpoints = 0;
    130 	int trset = 0;
    131 	const char *emul_name = "netbsd";
    132 	int col;
    133 	char *cp;
    134 
    135 	while ((ch = getopt(argc, argv, "e:f:dlm:Nnp:RTt:xX:")) != -1) {
    136 		switch (ch) {
    137 		case 'e':
    138 			emul_name = strdup(optarg); /* it's safer to copy it */
    139 			break;
    140 		case 'f':
    141 			tracefile = optarg;
    142 			break;
    143 		case 'd':
    144 			decimal = 1;
    145 			break;
    146 		case 'l':
    147 			tail = 1;
    148 			break;
    149 		case 'p':
    150 			do_pid = strtoul(optarg, &cp, 0);
    151 			if (*cp != 0)
    152 				errx(1,"invalid number %s", optarg);
    153 			break;
    154 		case 'm':
    155 			maxdata = strtoul(optarg, &cp, 0);
    156 			if (*cp != 0)
    157 				errx(1,"invalid number %s", optarg);
    158 			break;
    159 		case 'N':
    160 			numeric++;
    161 			break;
    162 		case 'n':
    163 			plain++;
    164 			break;
    165 		case 'R':
    166 			timestamp = 2;	/* relative timestamp */
    167 			break;
    168 		case 'T':
    169 			timestamp = 1;
    170 			break;
    171 		case 't':
    172 			trset = 1;
    173 			trpoints = getpoints(trpoints, optarg);
    174 			if (trpoints < 0)
    175 				errx(1, "unknown trace point in %s", optarg);
    176 			break;
    177 		case 'x':
    178 			word_size = 1;
    179 			break;
    180 		case 'X':
    181 			word_size = strtoul(optarg, &cp, 0);
    182 			if (*cp != 0 || word_size & (word_size - 1) ||
    183 			    word_size > 16 || word_size <= 0)
    184 				errx(1, "argument to -X must be "
    185 				    "1, 2, 4, 8 or 16");
    186 			break;
    187 		default:
    188 			usage();
    189 		}
    190 	}
    191 	argv += optind;
    192 	argc -= optind;
    193 
    194 	if (!trset)
    195 		trpoints = ALL_POINTS;
    196 
    197 	if (tracefile == NULL) {
    198 		if (argc == 1) {
    199 			tracefile = argv[0];
    200 			argv++;
    201 			argc--;
    202 		} else
    203 			tracefile = DEF_TRACEFILE;
    204 	}
    205 
    206 	if (argc > 0)
    207 		usage();
    208 
    209 	setemul(emul_name, 0, 0);
    210 	mach_lookup_emul();
    211 
    212 	m = malloc(size = 1024);
    213 	if (m == NULL)
    214 		errx(1, "malloc: %s", strerror(ENOMEM));
    215 	if (!freopen(tracefile, "r", stdin))
    216 		err(1, "%s", tracefile);
    217 	while (fread_tail((char *)&ktr_header, sizeof(struct ktr_header), 1)) {
    218 		if (trpoints & (1 << ktr_header.ktr_type) &&
    219 		    (do_pid == -1 || ktr_header.ktr_pid == do_pid))
    220 			col = dumpheader(&ktr_header);
    221 		else
    222 			col = -1;
    223 		if ((ktrlen = ktr_header.ktr_len) < 0)
    224 			errx(1, "bogus length 0x%x", ktrlen);
    225 		if (ktrlen > size) {
    226 			while (ktrlen > size)
    227 				size *= 2;
    228 			m = realloc(m, size);
    229 			if (m == NULL)
    230 				errx(1, "realloc: %s", strerror(ENOMEM));
    231 		}
    232 		if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
    233 			errx(1, "data too short");
    234 		if (col == -1)
    235 			continue;
    236 
    237 		/* update context to match currently processed record */
    238 		ectx_sanify(ktr_header.ktr_pid);
    239 
    240 		switch (ktr_header.ktr_type) {
    241 		case KTR_SYSCALL:
    242 			ktrsyscall(m);
    243 			break;
    244 		case KTR_SYSRET:
    245 			ktrsysret(m, ktrlen);
    246 			break;
    247 		case KTR_NAMEI:
    248 			ktrnamei(m, ktrlen);
    249 			break;
    250 		case KTR_GENIO:
    251 			ktrgenio(m, ktrlen);
    252 			break;
    253 		case KTR_PSIG:
    254 			ktrpsig(m, ktrlen);
    255 			break;
    256 		case KTR_CSW:
    257 			ktrcsw(m);
    258 			break;
    259 		case KTR_EMUL:
    260 			ktremul(m, ktrlen, size);
    261 			break;
    262 		case KTR_USER:
    263 			ktruser(m, ktrlen);
    264 			break;
    265 		case KTR_MMSG:
    266 			ktrmmsg(m, ktrlen);
    267 			break;
    268 		case KTR_MOOL:
    269 			ktrmool(m, ktrlen);
    270 			break;
    271 		case KTR_EXEC_ARG:
    272 		case KTR_EXEC_ENV:
    273 			visdump_buf(m, ktrlen, col);
    274 			break;
    275 		default:
    276 			putchar('\n');
    277 			hexdump_buf(m, ktrlen, word_size);
    278 		}
    279 		if (tail)
    280 			(void)fflush(stdout);
    281 	}
    282 	return (0);
    283 }
    284 
    285 int
    286 fread_tail(buf, size, num)
    287 	char *buf;
    288 	int num, size;
    289 {
    290 	int i;
    291 
    292 	while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
    293 		(void)sleep(1);
    294 		clearerr(stdin);
    295 	}
    296 	return (i);
    297 }
    298 
    299 int
    300 dumpheader(kth)
    301 	struct ktr_header *kth;
    302 {
    303 	char unknown[64], *type;
    304 	static struct timeval prevtime;
    305 	struct timeval temp;
    306 	int col;
    307 
    308 	switch (kth->ktr_type) {
    309 	case KTR_SYSCALL:
    310 		type = "CALL";
    311 		break;
    312 	case KTR_SYSRET:
    313 		type = "RET ";
    314 		break;
    315 	case KTR_NAMEI:
    316 		type = "NAMI";
    317 		break;
    318 	case KTR_GENIO:
    319 		type = "GIO ";
    320 		break;
    321 	case KTR_PSIG:
    322 		type = "PSIG";
    323 		break;
    324 	case KTR_CSW:
    325 		type = "CSW ";
    326 		break;
    327 	case KTR_EMUL:
    328 		type = "EMUL";
    329 		break;
    330 	case KTR_USER:
    331 		type = "USER";
    332 		break;
    333 	case KTR_MMSG:
    334 		type = "MMSG";
    335 		break;
    336 	case KTR_MOOL:
    337 		type = "MOOL";
    338 		break;
    339 	case KTR_EXEC_ENV:
    340 		type = "ENV";
    341 		break;
    342 	case KTR_EXEC_ARG:
    343 		type = "ARG";
    344 		break;
    345 	default:
    346 		(void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
    347 		type = unknown;
    348 	}
    349 
    350 	col = printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
    351 	if (timestamp) {
    352 		if (timestamp == 2) {
    353 			if (prevtime.tv_sec == 0)
    354 				temp.tv_sec = temp.tv_usec = 0;
    355 			else
    356 				timersub(&kth->ktr_time, &prevtime, &temp);
    357 			prevtime = kth->ktr_time;
    358 		} else
    359 			temp = kth->ktr_time;
    360 		col += printf("%ld.%06ld ",
    361 		    (long int)temp.tv_sec, (long int)temp.tv_usec);
    362 	}
    363 	col += printf("%-4s  ", type);
    364 	return col;
    365 }
    366 
    367 void
    368 output_long(it, as_x)
    369 	u_long it;
    370 	int as_x;
    371 {
    372 	if (cur_emul->flags & EMUL_FLAG_NETBSD32)
    373 		printf(as_x ? "%#x" : "%d", (u_int)it);
    374 	else
    375 		printf(as_x ? "%#lx" : "%ld", it);
    376 }
    377 
    378 void
    379 ioctldecode(cmd)
    380 	u_long cmd;
    381 {
    382 	char dirbuf[4], *dir = dirbuf;
    383 
    384 	if (cmd & IOC_IN)
    385 		*dir++ = 'W';
    386 	if (cmd & IOC_OUT)
    387 		*dir++ = 'R';
    388 	*dir = '\0';
    389 
    390 	printf(",_IO%s('%c',", dirbuf, (int) ((cmd >> 8) & 0xff));
    391 	output_long(cmd & 0xff, decimal == 0);
    392 	if ((cmd & IOC_VOID) == 0) {
    393 		putchar(',');
    394 		output_long((cmd >> 16) & 0xff, decimal == 0);
    395 	}
    396 	putchar(')');
    397 }
    398 
    399 void
    400 ktrsyscall(ktr)
    401 	struct ktr_syscall *ktr;
    402 {
    403 	int argcount;
    404 	const struct emulation *emul = cur_emul;
    405 	register_t *ap;
    406 	char c;
    407 	char *cp;
    408 	const char *sys_name;
    409 
    410 	argcount = ktr->ktr_argsize / sizeof (*ap);
    411 
    412 	emul_changed = 0;
    413 
    414 	if (numeric ||
    415 	    ((ktr->ktr_code >= emul->nsysnames || ktr->ktr_code < 0) &&
    416 	    mach_traps_dispatch(&ktr->ktr_code, &emul) == 0)) {
    417 		sys_name = "?";
    418 		(void)printf("[%d]", ktr->ktr_code);
    419 	} else {
    420 		sys_name = emul->sysnames[ktr->ktr_code];
    421 		(void)printf("%s", sys_name);
    422 	}
    423 #ifdef _LP64
    424 #define NETBSD32_	"netbsd32_"
    425 	if (cur_emul->flags & EMUL_FLAG_NETBSD32) {
    426 		size_t len = strlen(NETBSD32_);
    427 		if (strncmp(sys_name, NETBSD32_, len) == 0)
    428 			sys_name += len;
    429 	}
    430 #undef NETBSD32_
    431 #endif
    432 
    433 	ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
    434 	if (argcount) {
    435 		c = '(';
    436 		if (plain) {
    437 			;
    438 
    439 		} else if (strcmp(sys_name, "exit") == 0) {
    440 			ectx_delete();
    441 
    442 		} else if (strcmp(sys_name, "ioctl") == 0 && argcount >= 2) {
    443 			(void)putchar('(');
    444 			output_long((long)*ap, !(decimal || small(*ap)));
    445 			ap++;
    446 			argcount--;
    447 			if ((cp = ioctlname(*ap)) != NULL)
    448 				(void)printf(",%s", cp);
    449 			else
    450 				ioctldecode(*ap);
    451 			ap++;
    452 			argcount--;
    453 			c = ',';
    454 
    455 		} else if (strcmp(sys_name, "kill") == 0 && argcount >= 2) {
    456 			putchar('(');
    457 			output_long((long)ap[0], !(decimal || small(*ap)));
    458 			(void)printf(", SIG%s", signame(ap[1], 1));
    459 			ap += 2;
    460 			argcount -= 2;
    461 			c = ',';
    462 
    463 		} else if (strcmp(sys_name, "ptrace") == 0 && argcount >= 1) {
    464 			putchar('(');
    465 			if (strcmp(emul->name, "linux") == 0) {
    466 				if (*ap >= 0 && *ap <
    467 				    sizeof(linux_ptrace_ops) /
    468 				    sizeof(linux_ptrace_ops[0]))
    469 					(void)printf("%s",
    470 					    linux_ptrace_ops[*ap]);
    471 				else
    472 					output_long((long)*ap, 1);
    473 			} else {
    474 				if (*ap >= 0 && *ap <
    475 				    sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
    476 					(void)printf("%s", ptrace_ops[*ap]);
    477 				else
    478 					output_long((long)*ap, 1);
    479 			}
    480 			ap++;
    481 			argcount--;
    482 			c = ',';
    483 
    484 		}
    485 		while (argcount > 0) {
    486 			putchar(c);
    487 			output_long((long)*ap, !(decimal || small(*ap)));
    488 			ap++;
    489 			argcount--;
    490 			c = ',';
    491 		}
    492 		(void)putchar(')');
    493 	}
    494 	(void)putchar('\n');
    495 }
    496 
    497 void
    498 ktrsysret(ktr, len)
    499 	struct ktr_sysret *ktr;
    500 	int len;
    501 {
    502 	const struct emulation *emul;
    503 	int error = ktr->ktr_error;
    504 	int code = ktr->ktr_code;
    505 
    506 	if (emul_changed)  {
    507 		/* In order to get system call name right in execve return */
    508 		emul = prev_emul;
    509 		emul_changed = 0;
    510 	} else
    511 		emul = cur_emul;
    512 
    513 	if ((code >= emul->nsysnames || code < 0 || plain > 1) &&
    514 	    (mach_traps_dispatch(&code, &emul) == 0))
    515 		(void)printf("[%d] ", code);
    516 	else
    517 		(void)printf("%s ", emul->sysnames[code]);
    518 
    519 	switch (error) {
    520 	case 0:
    521 		rprint(ktr->ktr_retval);
    522 		if (len > offsetof(struct ktr_sysret, ktr_retval_1) &&
    523 		    ktr->ktr_retval_1 != 0) {
    524 			(void)printf(", ");
    525 			rprint(ktr->ktr_retval_1);
    526 		}
    527 		break;
    528 
    529 	default:
    530 		eprint(error);
    531 		break;
    532 	}
    533 	(void)putchar('\n');
    534 }
    535 
    536 void
    537 rprint(register_t ret)
    538 {
    539 
    540 	if (!plain) {
    541 		(void)printf("%ld", (long)ret);
    542 		if (!small(ret))
    543 			(void)printf("/%#lx", (long)ret);
    544 	} else {
    545 		if (decimal || small(ret))
    546 			(void)printf("%ld", (long)ret);
    547 		else
    548 			(void)printf("%#lx", (long)ret);
    549 	}
    550 }
    551 
    552 /*
    553  * We print the original emulation's error numerically, but we
    554  * translate it to netbsd to print it symbolically.
    555  */
    556 void
    557 eprint(e)
    558 	int e;
    559 {
    560 	int i = e;
    561 
    562 	if (cur_emul->errnomap) {
    563 
    564 		/* No remapping for ERESTART and EJUSTRETURN */
    565 		/* Kludge for linux that has negative error numbers */
    566 		if (cur_emul->errnomap[2] > 0 && e < 0)
    567 			goto normal;
    568 
    569 		for (i = 0; i < cur_emul->nerrnomap; i++)
    570 			if (e == cur_emul->errnomap[i])
    571 				break;
    572 
    573 		if (i == cur_emul->nerrnomap) {
    574 			printf("-1 unknown errno %d", e);
    575 			return;
    576 		}
    577 	}
    578 
    579 normal:
    580 	switch (i) {
    581 	case ERESTART:
    582 		(void)printf("RESTART");
    583 		break;
    584 
    585 	case EJUSTRETURN:
    586 		(void)printf("JUSTRETURN");
    587 		break;
    588 
    589 	default:
    590 		(void)printf("-1 errno %d", e);
    591 		if (!plain)
    592 			(void)printf(" %s", strerror(i));
    593 	}
    594 }
    595 
    596 void
    597 ktrnamei(cp, len)
    598 	char *cp;
    599 	int len;
    600 {
    601 
    602 	(void)printf("\"%.*s\"\n", len, cp);
    603 }
    604 
    605 void
    606 ktremul(name, len, bufsize)
    607 	char *name;
    608 	int len, bufsize;
    609 {
    610 
    611 	if (len >= bufsize)
    612 		len = bufsize - 1;
    613 
    614 	name[len] = '\0';
    615 	setemul(name, ktr_header.ktr_pid, 1);
    616 	emul_changed = 1;
    617 
    618 	(void)printf("\"%s\"\n", name);
    619 }
    620 
    621 static void
    622 hexdump_buf(vdp, datalen, word_sz)
    623 	const void *vdp;
    624 	int datalen;
    625 	int word_sz;
    626 {
    627 	const char hex[] = "0123456789abcdef";
    628 	char chars[16], prev[16];
    629 	char bytes[16 * 3 + 4];
    630 	const unsigned char *dp = vdp;
    631 	const unsigned char *datalim = dp + datalen;
    632 	const unsigned char *line_end;
    633 	int off, l, c;
    634 	char *cp, *bp;
    635 	int divmask = word_sz - 1;	/* block size in bytes */
    636 	int gdelim = 3;			/* gap between blocks */
    637 	int bsize = 2;			/* increment for each byte */
    638 	int width;
    639 	int dupl = 0;
    640 #if _BYTE_ORDER == _LITTLE_ENDIAN
    641 	int bswap = word_sz - 1;
    642 #else
    643 #define	bswap 0
    644 #endif
    645 
    646 	switch (word_sz) {
    647 	case 2:
    648 		gdelim = 2;
    649 		break;
    650 	case 1:
    651 		divmask = 7;
    652 		bsize = 3;
    653 		gdelim = 1;
    654 		break;
    655 	default:
    656 		break;
    657 	}
    658 	width = 16 * bsize + (16 / (divmask + 1)) * gdelim;
    659 	if (word_size != 1)
    660 		width += 2;
    661 
    662 	for (off = 0; dp < datalim; off += l) {
    663 		memset(bytes, ' ', sizeof bytes);
    664 		line_end = dp + 16;
    665 		if (line_end >= datalim) {
    666 			line_end = datalim;
    667 			dupl |= 1;	/* need to print */
    668 		} else {
    669 			if (dupl == 0 || memcmp(dp, prev, sizeof chars))
    670 				dupl |= 1;
    671 		}
    672 
    673 		if (!(dupl & 1)) {
    674 			/* This is a duplicate of the line above, count 'em */
    675 			dupl += 2;
    676 			dp = line_end;
    677 			continue;
    678 		}
    679 
    680 		if (dupl > 3) {
    681 			/* previous line as a duplicate */
    682 			if (dupl == 5)
    683 				/* Only one duplicate, print line */
    684 				printf("\t%-5.3x%.*s%.*s\n",
    685 					off - l, width, bytes, l, chars);
    686 			else
    687 				printf("\t%.*s\n",
    688 					snprintf(NULL, 0, "%3x", off), "*****");
    689 		}
    690 
    691 		for (l = 0, bp = bytes, cp = chars; dp < line_end; l++) {
    692 			c = *dp++;
    693 			prev[l] = c;
    694 			if ((l & divmask) == 0)
    695 				bp += gdelim;
    696 			bp[(l ^ bswap) * bsize] = hex[c >> 4];
    697 			bp[(l ^ bswap) * bsize + 1] = hex[c & 0xf];
    698 			*cp++ = isgraph(c) ? c : '.';
    699 		}
    700 
    701 		printf("\t%-5.3x%.*s%.*s\n", off, width, bytes, l, chars);
    702 		dupl = 2;
    703 	}
    704 }
    705 
    706 static void
    707 visdump_buf(const void *vdp, int datalen, int col)
    708 {
    709 	const unsigned char *dp = vdp;
    710 	char *cp;
    711 	int width;
    712 	char visbuf[5];
    713 	static int screenwidth = 0;
    714 
    715 	if (screenwidth == 0) {
    716 		struct winsize ws;
    717 
    718 		if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
    719 		    ws.ws_col > 8)
    720 			screenwidth = ws.ws_col;
    721 		else
    722 			screenwidth = 80;
    723 	}
    724 
    725 	(void)printf("\"");
    726 	col++;
    727 	for (; datalen > 0; datalen--, dp++) {
    728 		(void)svis(visbuf, *dp, VIS_CSTYLE,
    729 		    datalen > 1 ? *(dp + 1) : 0, "\"");
    730 		cp = visbuf;
    731 		/*
    732 		 * Keep track of printables and
    733 		 * space chars (like fold(1)).
    734 		 */
    735 		if (col == 0) {
    736 			(void)putchar('\t');
    737 			col = 8;
    738 		}
    739 		switch (*cp) {
    740 		case '\n':
    741 			col = 0;
    742 			(void)putchar('\n');
    743 			continue;
    744 		case '\t':
    745 			width = 8 - (col & 07);
    746 			break;
    747 		default:
    748 			width = strlen(cp);
    749 		}
    750 		if (col + width > (screenwidth - 2)) {
    751 			(void)printf("\\\n\t");
    752 			col = 8;
    753 			if (*cp == '\t')
    754 				width = 8;
    755 		}
    756 		col += width;
    757 		do {
    758 			(void)putchar(*cp++);
    759 		} while (*cp);
    760 	}
    761 	if (col == 0)
    762 		(void)printf("       ");
    763 	(void)printf("\"\n");
    764 }
    765 
    766 void
    767 ktrgenio(ktr, len)
    768 	struct ktr_genio *ktr;
    769 	int len;
    770 {
    771 	int datalen = len - sizeof (struct ktr_genio);
    772 	char *dp = (char *)ktr + sizeof (struct ktr_genio);
    773 
    774 	printf("fd %d %s %d bytes\n", ktr->ktr_fd,
    775 	    ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
    776 	if (maxdata == 0)
    777 		return;
    778 	if (maxdata > 0 && datalen > maxdata)
    779 		datalen = maxdata;
    780 	if (word_size) {
    781 		hexdump_buf(dp, datalen, word_size);
    782 		return;
    783 	}
    784 	(void)printf("       ");
    785 	visdump_buf(dp, datalen, 7);
    786 }
    787 
    788 void
    789 ktrpsig(v, len)
    790 	void *v;
    791 	int len;
    792 {
    793 	int signo, first;
    794 	struct {
    795 		struct ktr_psig ps;
    796 		siginfo_t si;
    797 	} *psig = v;
    798 	siginfo_t *si = &psig->si;
    799 	const char *code;
    800 
    801 	(void)printf("SIG%s ", signame(psig->ps.signo, 0));
    802 	if (psig->ps.action == SIG_DFL)
    803 		(void)printf("SIG_DFL");
    804 	else {
    805 		(void)printf("caught handler=%p mask=(", psig->ps.action);
    806 		first = 1;
    807 		for (signo = 1; signo < NSIG; signo++) {
    808 			if (sigismember(&psig->ps.mask, signo)) {
    809 				if (first)
    810 					first = 0;
    811 				else
    812 					(void)printf(",");
    813 				(void)printf("%d", signo);
    814 			}
    815 		}
    816 		(void)printf(")");
    817 	}
    818 	switch (len) {
    819 	case sizeof(struct ktr_psig):
    820 		if (psig->ps.code)
    821 			printf(" code=0x%x", psig->ps.code);
    822 		printf(psig->ps.action == SIG_DFL ? "\n" : ")\n");
    823 		return;
    824 	case sizeof(*psig):
    825 		if (si->si_code == 0) {
    826 			printf(": code=SI_USER sent by pid=%d, uid=%d)\n",
    827 			    si->si_pid, si->si_uid);
    828 			return;
    829 		}
    830 
    831 		if (si->si_code < 0) {
    832 			switch (si->si_code) {
    833 			case SI_TIMER:
    834 				printf(": code=SI_TIMER sigval %p)\n",
    835 				    si->si_sigval.sival_ptr);
    836 				return;
    837 			case SI_QUEUE:
    838 				code = "SI_QUEUE";
    839 				break;
    840 			case SI_ASYNCIO:
    841 				code = "SI_ASYNCIO";
    842 				break;
    843 			case SI_MESGQ:
    844 				code = "SI_MESGQ";
    845 				break;
    846 			default:
    847 				code = NULL;
    848 				break;
    849 			}
    850 			if (code)
    851 				printf(": code=%s unimplemented)\n", code);
    852 			else
    853 				printf(": code=%d unimplemented)\n",
    854 				    si->si_code);
    855 			return;
    856 		}
    857 
    858 		code = siginfocodename(si->si_signo, si->si_code);
    859 		switch (si->si_signo) {
    860 		case SIGCHLD:
    861 			printf(": code=%s child pid=%d, uid=%d, "
    862 			    " status=%u, utime=%lu, stime=%lu)\n",
    863 			    code, si->si_pid,
    864 			    si->si_uid, si->si_status,
    865 			    (unsigned long) si->si_utime,
    866 			    (unsigned long) si->si_stime);
    867 			return;
    868 		case SIGILL:
    869 		case SIGFPE:
    870 		case SIGSEGV:
    871 		case SIGBUS:
    872 		case SIGTRAP:
    873 			printf(": code=%s, addr=%p, trap=%d)\n",
    874 			    code, si->si_addr, si->si_trap);
    875 			return;
    876 		case SIGIO:
    877 			printf(": code=%s, fd=%d, band=%lx)\n",
    878 			    code, si->si_fd, si->si_band);
    879 			return;
    880 		default:
    881 			printf(": code=%s, errno=%d)\n",
    882 			    code, si->si_errno);
    883 			return;
    884 		}
    885 		/*NOTREACHED*/
    886 	default:
    887 		warnx("Unhandled size %d for ktrpsig\n", len);
    888 		break;
    889 	}
    890 }
    891 
    892 void
    893 ktrcsw(cs)
    894 	struct ktr_csw *cs;
    895 {
    896 
    897 	(void)printf("%s %s\n", cs->out ? "stop" : "resume",
    898 	    cs->user ? "user" : "kernel");
    899 }
    900 
    901 void
    902 ktruser(usr, len)
    903 	struct ktr_user *usr;
    904 	int len;
    905 {
    906 	int i;
    907 	unsigned char *dta;
    908 
    909 	printf("\"%.*s: %d, ", KTR_USER_MAXIDLEN, usr->ktr_id, len);
    910 	dta = (unsigned char *)usr;
    911 	for (i = sizeof(struct ktr_user); i < len; i++)
    912 		printf("%02x", (unsigned int) dta[i]);
    913 	printf("\"\n");
    914 }
    915 
    916 void
    917 ktrmmsg(mmsg, len)
    918 	struct ktr_mmsg *mmsg;
    919 	int len;
    920 {
    921 	const char *service_name;
    922 	char *reply;
    923 	int id;
    924 
    925 	id = mmsg->ktr_id;
    926 	if ((id / 100) % 2) {  /* Message reply */
    927 		reply = " reply";
    928 		id -= 100;
    929 	} else {
    930 		reply = "";
    931 	}
    932 
    933 	if ((service_name = mach_service_name(id)) != NULL)
    934 		printf("%s%s [%d]\n", service_name, reply, mmsg->ktr_id);
    935 	else
    936 		printf("unknown service%s [%d]\n", reply, mmsg->ktr_id);
    937 
    938 	hexdump_buf(mmsg, len, word_size ? word_size : 4);
    939 }
    940 
    941 void
    942 ktrmool(mool, len)
    943 	struct ktr_mool *mool;
    944 	int len;
    945 {
    946 	size_t size = mool->size;
    947 
    948 	printf("%ld/0x%lx bytes at %p\n",
    949 	    (u_long)size, (u_long)size, mool->uaddr);
    950 	mool++;
    951 	hexdump_buf(mool, size, word_size ? word_size : 4);
    952 }
    953 
    954 static const char *
    955 signame(long sig, int xlat)
    956 {
    957 	static char buf[64];
    958 
    959 	if (sig == 0)
    960 		return " 0";
    961 	else if (sig < 0 || sig >= NSIG) {
    962 		(void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig);
    963 		return buf;
    964 	} else
    965 		return sys_signame[(xlat && cur_emul->signalmap != NULL) ?
    966 		    cur_emul->signalmap[sig] : sig];
    967 }
    968 
    969 void
    970 usage()
    971 {
    972 
    973 	(void)fprintf(stderr, "usage: kdump [-dlNnRT] [-e emulation] "
    974 	   "[-f file] [-m maxdata] [-p pid]\n             [-t trstr] "
    975 	   "[-x | -X size] [file]\n");
    976 	exit(1);
    977 }
    978