Home | History | Annotate | Line # | Download | only in kdump
kdump.c revision 1.42
      1 /*	$NetBSD: kdump.c,v 1.42 2002/11/15 19:58:05 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)kdump.c	8.4 (Berkeley) 4/28/95";
     45 #else
     46 __RCSID("$NetBSD: kdump.c,v 1.42 2002/11/15 19:58:05 manu Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #define _KERNEL
     52 #include <sys/errno.h>
     53 #undef _KERNEL
     54 #include <sys/time.h>
     55 #include <sys/uio.h>
     56 #include <sys/ktrace.h>
     57 #include <sys/ioctl.h>
     58 #include <sys/ptrace.h>
     59 
     60 #include <err.h>
     61 #include <signal.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <unistd.h>
     66 #include <vis.h>
     67 
     68 #include "ktrace.h"
     69 #include "setemul.h"
     70 
     71 #include <sys/syscall.h>
     72 
     73 int timestamp, decimal, plain, tail, maxdata;
     74 pid_t do_pid = -1;
     75 const char *tracefile = NULL;
     76 struct ktr_header ktr_header;
     77 int emul_changed = 0;
     78 
     79 #define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
     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 __P((int, char **));
     99 int	fread_tail __P((char *, int, int));
    100 void	dumpheader __P((struct ktr_header *));
    101 void	ioctldecode __P((u_long));
    102 void	ktrsyscall __P((struct ktr_syscall *));
    103 void	ktrsysret __P((struct ktr_sysret *));
    104 void	ktrnamei __P((char *, int));
    105 void	ktremul __P((char *, int, int));
    106 void	ktrgenio __P((struct ktr_genio *, int));
    107 void	ktrpsig __P((struct ktr_psig *));
    108 void	ktrcsw __P((struct ktr_csw *));
    109 void	ktruser __P((struct ktr_user *, int));
    110 void	usage __P((void));
    111 void	eprint __P((int));
    112 char	*ioctlname __P((long));
    113 static const char *signame __P((long, int));
    114 
    115 int
    116 main(argc, argv)
    117 	int argc;
    118 	char *argv[];
    119 {
    120 	int ch, ktrlen, size;
    121 	void *m;
    122 	int trpoints = ALL_POINTS;
    123 	const char *emul_name = "netbsd";
    124 
    125 	while ((ch = getopt(argc, argv, "e:f:dlm:np:RTt:")) != -1)
    126 		switch (ch) {
    127 		case 'e':
    128 			emul_name = strdup(optarg); /* it's safer to copy it */
    129 			break;
    130 		case 'f':
    131 			tracefile = optarg;
    132 			break;
    133 		case 'd':
    134 			decimal = 1;
    135 			break;
    136 		case 'l':
    137 			tail = 1;
    138 			break;
    139 		case 'p':
    140 			do_pid = atoi(optarg);
    141 			break;
    142 		case 'm':
    143 			maxdata = atoi(optarg);
    144 			break;
    145 		case 'n':
    146 			plain++;
    147 			break;
    148 		case 'R':
    149 			timestamp = 2;	/* relative timestamp */
    150 			break;
    151 		case 'T':
    152 			timestamp = 1;
    153 			break;
    154 		case 't':
    155 			trpoints = getpoints(optarg);
    156 			if (trpoints < 0)
    157 				errx(1, "unknown trace point in %s", optarg);
    158 			break;
    159 		default:
    160 			usage();
    161 		}
    162 	argv += optind;
    163 	argc -= optind;
    164 
    165 	if (tracefile == NULL) {
    166 		if (argc == 1) {
    167 			tracefile = argv[0];
    168 			argv++;
    169 			argc--;
    170 		}
    171 		else
    172 			tracefile = DEF_TRACEFILE;
    173 	}
    174 
    175 	if (argc > 0)
    176 		usage();
    177 
    178 	setemul(emul_name, 0, 0);
    179 	mach_lookup_emul();
    180 
    181 	m = malloc(size = 1024);
    182 	if (m == NULL)
    183 		errx(1, "malloc: %s", strerror(ENOMEM));
    184 	if (!freopen(tracefile, "r", stdin))
    185 		err(1, "%s", tracefile);
    186 	while (fread_tail((char *)&ktr_header, sizeof(struct ktr_header), 1)) {
    187 		if (trpoints & (1<<ktr_header.ktr_type))
    188 			if (do_pid == -1 || ktr_header.ktr_pid == do_pid)
    189 				dumpheader(&ktr_header);
    190 		if ((ktrlen = ktr_header.ktr_len) < 0)
    191 			errx(1, "bogus length 0x%x", ktrlen);
    192 		if (ktrlen > size) {
    193 			while(ktrlen > size) size *= 2;
    194 			m = (void *)realloc(m, size);
    195 			if (m == NULL)
    196 				errx(1, "realloc: %s", strerror(ENOMEM));
    197 		}
    198 		if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
    199 			errx(1, "data too short");
    200 		if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
    201 			continue;
    202 
    203 		/* update context to match currently processed record */
    204 		if (do_pid != -1 && ktr_header.ktr_pid != do_pid)
    205 			continue;
    206 		ectx_sanify(ktr_header.ktr_pid);
    207 
    208 		switch (ktr_header.ktr_type) {
    209 		case KTR_SYSCALL:
    210 			ktrsyscall((struct ktr_syscall *)m);
    211 			break;
    212 		case KTR_SYSRET:
    213 			ktrsysret((struct ktr_sysret *)m);
    214 			break;
    215 		case KTR_NAMEI:
    216 			ktrnamei(m, ktrlen);
    217 			break;
    218 		case KTR_GENIO:
    219 			ktrgenio((struct ktr_genio *)m, ktrlen);
    220 			break;
    221 		case KTR_PSIG:
    222 			ktrpsig((struct ktr_psig *)m);
    223 			break;
    224 		case KTR_CSW:
    225 			ktrcsw((struct ktr_csw *)m);
    226 			break;
    227 		case KTR_EMUL:
    228 			ktremul(m, ktrlen, size);
    229 			break;
    230 		case KTR_USER:
    231 			ktruser((struct ktr_user *)m, ktrlen);
    232 			break;
    233 		}
    234 		if (tail)
    235 			(void)fflush(stdout);
    236 	}
    237 	return (0);
    238 }
    239 
    240 int
    241 fread_tail(buf, size, num)
    242 	char *buf;
    243 	int num, size;
    244 {
    245 	int i;
    246 
    247 	while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
    248 		(void)sleep(1);
    249 		clearerr(stdin);
    250 	}
    251 	return (i);
    252 }
    253 
    254 void
    255 dumpheader(kth)
    256 	struct ktr_header *kth;
    257 {
    258 	char unknown[64], *type;
    259 	static struct timeval prevtime;
    260 	struct timeval temp;
    261 
    262 	switch (kth->ktr_type) {
    263 	case KTR_SYSCALL:
    264 		type = "CALL";
    265 		break;
    266 	case KTR_SYSRET:
    267 		type = "RET ";
    268 		break;
    269 	case KTR_NAMEI:
    270 		type = "NAMI";
    271 		break;
    272 	case KTR_GENIO:
    273 		type = "GIO ";
    274 		break;
    275 	case KTR_PSIG:
    276 		type = "PSIG";
    277 		break;
    278 	case KTR_CSW:
    279 		type = "CSW";
    280 		break;
    281 	case KTR_EMUL:
    282 		type = "EMUL";
    283 		break;
    284 	case KTR_USER:
    285 		type = "USER";
    286 		break;
    287 	default:
    288 		(void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
    289 		type = unknown;
    290 	}
    291 
    292 	(void)printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
    293 	if (timestamp) {
    294 		if (timestamp == 2) {
    295 			timersub(&kth->ktr_time, &prevtime, &temp);
    296 			prevtime = kth->ktr_time;
    297 		} else
    298 			temp = kth->ktr_time;
    299 		(void)printf("%ld.%06ld ",
    300 		    (long int)temp.tv_sec, (long int)temp.tv_usec);
    301 	}
    302 	(void)printf("%s  ", type);
    303 }
    304 
    305 void
    306 ioctldecode(cmd)
    307 	u_long cmd;
    308 {
    309 	char dirbuf[4], *dir = dirbuf;
    310 
    311 	if (cmd & IOC_IN)
    312 		*dir++ = 'W';
    313 	if (cmd & IOC_OUT)
    314 		*dir++ = 'R';
    315 	*dir = '\0';
    316 
    317 	printf(decimal ? ",_IO%s('%c',%ld" : ",_IO%s('%c',%#lx",
    318 	    dirbuf, (int) ((cmd >> 8) & 0xff), cmd & 0xff);
    319 	if ((cmd & IOC_VOID) == 0)
    320 		printf(decimal ? ",%ld)" : ",%#lx)", (cmd >> 16) & 0xff);
    321 	else
    322 		printf(")");
    323 }
    324 
    325 void
    326 ktrsyscall(ktr)
    327 	struct ktr_syscall *ktr;
    328 {
    329 	int argsize = ktr->ktr_argsize;
    330 	const struct emulation *revelant = current;
    331 	register_t *ap;
    332 
    333 	if ((ktr->ktr_code >= revelant->nsysnames || ktr->ktr_code < 0)
    334 	    && (mach_traps_dispatch(&ktr->ktr_code, &revelant) == 0))
    335 		(void)printf("[%d]", ktr->ktr_code);
    336 	else
    337 		(void)printf("%s", revelant->sysnames[ktr->ktr_code]);
    338 	ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
    339 	if (argsize) {
    340 		char c = '(';
    341 		if (!plain) {
    342 			char *cp;
    343 
    344 			switch (ktr->ktr_code) {
    345 			case SYS_ioctl:
    346 				if (decimal)
    347 					(void)printf("(%ld", (long)*ap);
    348 				else
    349 					(void)printf("(%#lx", (long)*ap);
    350 				ap++;
    351 				argsize -= sizeof(register_t);
    352 				if ((cp = ioctlname(*ap)) != NULL)
    353 					(void)printf(",%s", cp);
    354 				else
    355 					ioctldecode(*ap);
    356 				c = ',';
    357 				ap++;
    358 				argsize -= sizeof(register_t);
    359 				break;
    360 
    361 			case SYS_ptrace:
    362 				if (strcmp(revelant->name, "linux") == 0) {
    363 				  if (*ap >= 0 && *ap <=
    364 				      sizeof(linux_ptrace_ops) /
    365 				      sizeof(linux_ptrace_ops[0]))
    366 					(void)printf("(%s",
    367 					    linux_ptrace_ops[*ap]);
    368 				  else
    369 					(void)printf("(%ld", (long)*ap);
    370 				} else {
    371 				  if (*ap >= 0 && *ap <=
    372 				    sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
    373 					(void)printf("(%s", ptrace_ops[*ap]);
    374 				  else
    375 					(void)printf("(%ld", (long)*ap);
    376 				}
    377 				c = ',';
    378 				ap++;
    379 				argsize -= sizeof(register_t);
    380 				break;
    381 
    382 			case SYS_kill:
    383 				if (decimal)
    384 					(void)printf("(%ld, SIG%s",
    385 					    (long)ap[0], signame(ap[1], 1));
    386 				else
    387 					(void)printf("(%#lx, SIG%s",
    388 					    (long)ap[0], signame(ap[1], 1));
    389 				ap += 2;
    390 				argsize -= 2 * sizeof(register_t);
    391 				break;
    392 
    393 			default:
    394 				/* No special handling */
    395 				break;
    396 			}
    397 		}
    398 		while (argsize) {
    399 			if (decimal)
    400 				(void)printf("%c%ld", c, (long)*ap);
    401 			else
    402 				(void)printf("%c%#lx", c, (long)*ap);
    403 			c = ',';
    404 			ap++;
    405 			argsize -= sizeof(register_t);
    406 		}
    407 		(void)putchar(')');
    408 	}
    409 	(void)putchar('\n');
    410 }
    411 
    412 void
    413 ktrsysret(ktr)
    414 	struct ktr_sysret *ktr;
    415 {
    416 	const struct emulation *revelant;
    417 	register_t ret = ktr->ktr_retval;
    418 	int error = ktr->ktr_error;
    419 	int code = ktr->ktr_code;
    420 
    421 	if (emul_changed)
    422 		revelant = previous;
    423 	else
    424 		revelant = current;
    425 	emul_changed = 0;
    426 
    427 	if ((code >= revelant->nsysnames || code < 0 || plain > 1)
    428 	    && (mach_traps_dispatch(&code, &revelant) == 0))
    429 		(void)printf("[%d] ", code);
    430 	else
    431 		(void)printf("%s ", revelant->sysnames[code]);
    432 
    433 	switch (error) {
    434 	case 0:
    435 		if (!plain) {
    436 			(void)printf("%ld", (long)ret);
    437 			if (ret < 0 || ret > 9)
    438 				(void)printf("/%#lx", (long)ret);
    439 		} else {
    440 			if (decimal)
    441 				(void)printf("%ld", (long)ret);
    442 			else
    443 				(void)printf("%#lx", (long)ret);
    444 		}
    445 		break;
    446 
    447 	default:
    448 		eprint(error);
    449 		break;
    450 	}
    451 	(void)putchar('\n');
    452 }
    453 
    454 /*
    455  * We print the original emulation's error numerically, but we
    456  * translate it to netbsd to print it symbolically.
    457  */
    458 void
    459 eprint(e)
    460 	int e;
    461 {
    462 	int i = e;
    463 
    464 	if (current->errnomap) {
    465 
    466 		/* No remapping for ERESTART and EJUSTRETURN */
    467 		/* Kludge for linux that has negative error numbers */
    468 		if (current->errnomap[2] > 0 && e < 0)
    469 			goto normal;
    470 
    471 		for (i = 0; i < current->nerrnomap; i++)
    472 			if (e == current->errnomap[i])
    473 				break;
    474 
    475 		if (i == current->nerrnomap) {
    476 			printf("-1 unknown errno %d", e);
    477 			return;
    478 		}
    479 	}
    480 
    481 normal:
    482 	switch (i) {
    483 	case ERESTART:
    484 		(void)printf("RESTART");
    485 		break;
    486 
    487 	case EJUSTRETURN:
    488 		(void)printf("JUSTRETURN");
    489 		break;
    490 
    491 	default:
    492 		(void)printf("-1 errno %d", e);
    493 		if (!plain)
    494 			(void)printf(" %s", strerror(i));
    495 	}
    496 }
    497 
    498 void
    499 ktrnamei(cp, len)
    500 	char *cp;
    501 	int len;
    502 {
    503 
    504 	(void)printf("\"%.*s\"\n", len, cp);
    505 }
    506 
    507 void
    508 ktremul(name, len, bufsize)
    509 	char *name;
    510 	int len, bufsize;
    511 {
    512 	if (len >= bufsize)
    513 		len = bufsize - 1;
    514 
    515 	name[len] = '\0';
    516 	setemul(name, ktr_header.ktr_pid, 1);
    517 	emul_changed = 1;
    518 
    519 	(void)printf("\"%s\"\n", name);
    520 }
    521 
    522 void
    523 ktrgenio(ktr, len)
    524 	struct ktr_genio *ktr;
    525 	int len;
    526 {
    527 	int datalen = len - sizeof (struct ktr_genio);
    528 	char *dp = (char *)ktr + sizeof (struct ktr_genio);
    529 	char *cp;
    530 	int col = 0;
    531 	int width;
    532 	char visbuf[5];
    533 	static int screenwidth = 0;
    534 
    535 	if (screenwidth == 0) {
    536 		struct winsize ws;
    537 
    538 		if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
    539 		    ws.ws_col > 8)
    540 			screenwidth = ws.ws_col;
    541 		else
    542 			screenwidth = 80;
    543 	}
    544 	printf("fd %d %s %d bytes\n", ktr->ktr_fd,
    545 		ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
    546 	if (maxdata && datalen > maxdata)
    547 		datalen = maxdata;
    548 	(void)printf("       \"");
    549 	col = 8;
    550 	for (; datalen > 0; datalen--, dp++) {
    551 		(void) vis(visbuf, *dp, VIS_CSTYLE, datalen>1?*(dp+1):0);
    552 		cp = visbuf;
    553 		/*
    554 		 * Keep track of printables and
    555 		 * space chars (like fold(1)).
    556 		 */
    557 		if (col == 0) {
    558 			(void)putchar('\t');
    559 			col = 8;
    560 		}
    561 		switch(*cp) {
    562 		case '\n':
    563 			col = 0;
    564 			(void)putchar('\n');
    565 			continue;
    566 		case '\t':
    567 			width = 8 - (col&07);
    568 			break;
    569 		default:
    570 			width = strlen(cp);
    571 		}
    572 		if (col + width > (screenwidth-2)) {
    573 			(void)printf("\\\n\t");
    574 			col = 8;
    575 		}
    576 		col += width;
    577 		do {
    578 			(void)putchar(*cp++);
    579 		} while (*cp);
    580 	}
    581 	if (col == 0)
    582 		(void)printf("       ");
    583 	(void)printf("\"\n");
    584 }
    585 
    586 void
    587 ktrpsig(psig)
    588 	struct ktr_psig *psig;
    589 {
    590 	int signo, first;
    591 
    592 	(void)printf("SIG%s ", signame(psig->signo, 0));
    593 	if (psig->action == SIG_DFL)
    594 		(void)printf("SIG_DFL\n");
    595 	else {
    596 		(void)printf("caught handler=0x%lx mask=(",
    597 		    (u_long)psig->action);
    598 		first = 1;
    599 		for (signo = 1; signo < NSIG; signo++) {
    600 			if (sigismember(&psig->mask, signo)) {
    601 				if (first)
    602 					first = 0;
    603 				else
    604 					(void)printf(",");
    605 				(void)printf("%d", signo);
    606 			}
    607 		}
    608 		(void)printf(") code=0x%x\n", psig->code);
    609 	}
    610 }
    611 
    612 void
    613 ktrcsw(cs)
    614 	struct ktr_csw *cs;
    615 {
    616 
    617 	(void)printf("%s %s\n", cs->out ? "stop" : "resume",
    618 	    cs->user ? "user" : "kernel");
    619 }
    620 
    621 void
    622 ktruser(usr, len)
    623 	struct ktr_user *usr;
    624 	int len;
    625 {
    626 	int i;
    627 	unsigned char *dta;
    628 
    629 	printf("\"%.*s: %d, ", KTR_USER_MAXIDLEN, usr->ktr_id, len);
    630 	dta = (unsigned char *)usr;
    631 	for(i=sizeof(struct ktr_user); i < len; i++)
    632 		printf("%02x", (unsigned int) dta[i]);
    633 	printf("\"\n");
    634 }
    635 
    636 static const char *
    637 signame(long sig, int xlat)
    638 {
    639 	static char buf[64];
    640 	if (sig <= 0 || sig >= NSIG) {
    641 		(void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig);
    642 		return buf;
    643 	} else
    644 		return sys_signame[(xlat && current->signalmap != NULL) ?
    645 		    current->signalmap[sig] : sig];
    646 }
    647 
    648 void
    649 usage()
    650 {
    651 
    652 	(void)fprintf(stderr,
    653 "usage: kdump [-dnlRT] [-e emulation] [-f trfile] [-m maxdata] [-p pid]\n"
    654 "             [-t [cnis]] [trfile]\n");
    655 	exit(1);
    656 }
    657