Home | History | Annotate | Line # | Download | only in kdump
kdump.c revision 1.22
      1 /*	$NetBSD: kdump.c,v 1.22 1998/10/18 17:43:43 christos 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.22 1998/10/18 17:43:43 christos 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 
     70 int timestamp, decimal, fancy = 1, tail, maxdata;
     71 char *tracefile = DEF_TRACEFILE;
     72 struct ktr_header ktr_header;
     73 
     74 #define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
     75 
     76 #include <sys/syscall.h>
     77 
     78 #include "../../sys/compat/freebsd/freebsd_syscall.h"
     79 #include "../../sys/compat/hpux/hpux_syscall.h"
     80 #include "../../sys/compat/ibcs2/ibcs2_syscall.h"
     81 #include "../../sys/compat/linux/linux_syscall.h"
     82 #include "../../sys/compat/osf1/osf1_syscall.h"
     83 #include "../../sys/compat/sunos/sunos_syscall.h"
     84 #include "../../sys/compat/svr4/svr4_syscall.h"
     85 #include "../../sys/compat/ultrix/ultrix_syscall.h"
     86 
     87 #define KTRACE
     88 #include "../../sys/kern/syscalls.c"
     89 
     90 #include "../../sys/compat/freebsd/freebsd_syscalls.c"
     91 #include "../../sys/compat/hpux/hpux_syscalls.c"
     92 #include "../../sys/compat/ibcs2/ibcs2_syscalls.c"
     93 #include "../../sys/compat/linux/linux_syscalls.c"
     94 #include "../../sys/compat/osf1/osf1_syscalls.c"
     95 #include "../../sys/compat/sunos/sunos_syscalls.c"
     96 #include "../../sys/compat/svr4/svr4_syscalls.c"
     97 #include "../../sys/compat/ultrix/ultrix_syscalls.c"
     98 
     99 #include "../../sys/compat/hpux/hpux_error.c"
    100 #include "../../sys/compat/svr4/svr4_error.c"
    101 #include "../../sys/compat/ibcs2/ibcs2_error.c"
    102 #include "../../sys/compat/linux/common/linux_error.c"
    103 #undef KTRACE
    104 
    105 struct emulation {
    106 	char *name;		/* Emulation name */
    107 	char **sysnames;	/* Array of system call names */
    108 	int  nsysnames;		/* Number of */
    109 	int  *errno;		/* Array of error number mapping */
    110 	int  nerrno;		/* number of elements in array */
    111 };
    112 
    113 #define NELEM(a) (sizeof(a) / sizeof(a[0]))
    114 
    115 static struct emulation emulations[] = {
    116 	{   "netbsd",	       syscallnames,         SYS_MAXSYSCALL,
    117 	        NULL,		        0 },
    118 	{  "freebsd",  freebsd_syscallnames, FREEBSD_SYS_MAXSYSCALL,
    119 	        NULL,		        0 },
    120 	{     "hpux",	  hpux_syscallnames,    HPUX_SYS_MAXSYSCALL,
    121 	  hpux_error,   NELEM(hpux_error) },
    122 	{    "ibcs2",    ibcs2_syscallnames,   IBCS2_SYS_MAXSYSCALL,
    123 	 ibcs2_error,  NELEM(ibcs2_error) },
    124 	{    "linux",    linux_syscallnames,   LINUX_SYS_MAXSYSCALL,
    125 	 linux_error,  NELEM(linux_error) },
    126 	{     "osf1",     osf1_syscallnames,    OSF1_SYS_MAXSYSCALL,
    127 	        NULL,		        0 },
    128 	{    "sunos",    sunos_syscallnames,   SUNOS_SYS_MAXSYSCALL,
    129 	        NULL,		        0 },
    130 	{     "svr4",     svr4_syscallnames,    SVR4_SYS_MAXSYSCALL,
    131 	  svr4_error,   NELEM(svr4_error) },
    132 	{   "ultrix",   ultrix_syscallnames,  ULTRIX_SYS_MAXSYSCALL,
    133 	        NULL,			0 },
    134 	{       NULL,		       NULL,		          0,
    135 	        NULL,			0 }
    136 };
    137 
    138 struct emulation *current;
    139 
    140 static const char *ptrace_ops[] = {
    141 	"PT_TRACE_ME",	"PT_READ_I",	"PT_READ_D",	"PT_READ_U",
    142 	"PT_WRITE_I",	"PT_WRITE_D",	"PT_WRITE_U",	"PT_CONTINUE",
    143 	"PT_KILL",	"PT_ATTACH",	"PT_DETACH",
    144 };
    145 
    146 int	main __P((int, char **));
    147 int	fread_tail __P((char *, int, int));
    148 void	dumpheader __P((struct ktr_header *));
    149 void	ioctldecode __P((u_long));
    150 void	ktrsyscall __P((struct ktr_syscall *));
    151 void	ktrsysret __P((struct ktr_sysret *));
    152 void	ktrnamei __P((char *, int));
    153 void	ktremul __P((char *, int));
    154 void	ktrgenio __P((struct ktr_genio *, int));
    155 void	ktrpsig __P((struct ktr_psig *));
    156 void	ktrcsw __P((struct ktr_csw *));
    157 void	usage __P((void));
    158 void	setemul __P((char *));
    159 void	eprint __P((int));
    160 char	*ioctlname __P((long));
    161 
    162 int
    163 main(argc, argv)
    164 	int argc;
    165 	char *argv[];
    166 {
    167 	int ch, ktrlen, size;
    168 	void *m;
    169 	int trpoints = ALL_POINTS;
    170 
    171 	current = &emulations[0];	/* NetBSD */
    172 
    173 	while ((ch = getopt(argc, argv, "e:f:dlm:nRTt:")) != -1)
    174 		switch (ch) {
    175 		case 'e':
    176 			setemul(optarg);
    177 			break;
    178 		case 'f':
    179 			tracefile = optarg;
    180 			break;
    181 		case 'd':
    182 			decimal = 1;
    183 			break;
    184 		case 'l':
    185 			tail = 1;
    186 			break;
    187 		case 'm':
    188 			maxdata = atoi(optarg);
    189 			break;
    190 		case 'n':
    191 			fancy = 0;
    192 			break;
    193 		case 'R':
    194 			timestamp = 2;	/* relative timestamp */
    195 			break;
    196 		case 'T':
    197 			timestamp = 1;
    198 			break;
    199 		case 't':
    200 			trpoints = getpoints(optarg);
    201 			if (trpoints < 0)
    202 				errx(1, "unknown trace point in %s", optarg);
    203 			break;
    204 		default:
    205 			usage();
    206 		}
    207 	argv += optind;
    208 	argc -= optind;
    209 
    210 	if (argc > 1)
    211 		usage();
    212 
    213 	m = malloc(size = 1025);
    214 	if (m == NULL)
    215 		errx(1, "%s", strerror(ENOMEM));
    216 	if (!freopen(tracefile, "r", stdin))
    217 		err(1, "%s", tracefile);
    218 	while (fread_tail((char *)&ktr_header, sizeof(struct ktr_header), 1)) {
    219 		if (trpoints & (1<<ktr_header.ktr_type))
    220 			dumpheader(&ktr_header);
    221 		if ((ktrlen = ktr_header.ktr_len) < 0)
    222 			errx(1, "bogus length 0x%x", ktrlen);
    223 		if (ktrlen > size) {
    224 			m = (void *)realloc(m, ktrlen+1);
    225 			if (m == NULL)
    226 				errx(1, "%s", strerror(ENOMEM));
    227 			size = ktrlen;
    228 		}
    229 		if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
    230 			errx(1, "data too short");
    231 		if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
    232 			continue;
    233 		switch (ktr_header.ktr_type) {
    234 		case KTR_SYSCALL:
    235 			ktrsyscall((struct ktr_syscall *)m);
    236 			break;
    237 		case KTR_SYSRET:
    238 			ktrsysret((struct ktr_sysret *)m);
    239 			break;
    240 		case KTR_NAMEI:
    241 			ktrnamei(m, ktrlen);
    242 			break;
    243 		case KTR_GENIO:
    244 			ktrgenio((struct ktr_genio *)m, ktrlen);
    245 			break;
    246 		case KTR_PSIG:
    247 			ktrpsig((struct ktr_psig *)m);
    248 			break;
    249 		case KTR_CSW:
    250 			ktrcsw((struct ktr_csw *)m);
    251 			break;
    252 		case KTR_EMUL:
    253 			ktremul(m, ktrlen);
    254 			break;
    255 		}
    256 		if (tail)
    257 			(void)fflush(stdout);
    258 	}
    259 	return (0);
    260 }
    261 
    262 int
    263 fread_tail(buf, size, num)
    264 	char *buf;
    265 	int num, size;
    266 {
    267 	int i;
    268 
    269 	while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
    270 		(void)sleep(1);
    271 		clearerr(stdin);
    272 	}
    273 	return (i);
    274 }
    275 
    276 void
    277 dumpheader(kth)
    278 	struct ktr_header *kth;
    279 {
    280 	char unknown[64], *type;
    281 	static struct timeval prevtime;
    282 	struct timeval temp;
    283 
    284 	switch (kth->ktr_type) {
    285 	case KTR_SYSCALL:
    286 		type = "CALL";
    287 		break;
    288 	case KTR_SYSRET:
    289 		type = "RET ";
    290 		break;
    291 	case KTR_NAMEI:
    292 		type = "NAMI";
    293 		break;
    294 	case KTR_GENIO:
    295 		type = "GIO ";
    296 		break;
    297 	case KTR_PSIG:
    298 		type = "PSIG";
    299 		break;
    300 	case KTR_CSW:
    301 		type = "CSW";
    302 		break;
    303 	case KTR_EMUL:
    304 		type = "EMUL";
    305 		break;
    306 	default:
    307 		(void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
    308 		type = unknown;
    309 	}
    310 
    311 	(void)printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
    312 	if (timestamp) {
    313 		if (timestamp == 2) {
    314 			timersub(&kth->ktr_time, &prevtime, &temp);
    315 			prevtime = kth->ktr_time;
    316 		} else
    317 			temp = kth->ktr_time;
    318 		(void)printf("%ld.%06ld ", temp.tv_sec, temp.tv_usec);
    319 	}
    320 	(void)printf("%s  ", type);
    321 }
    322 
    323 void
    324 ioctldecode(cmd)
    325 	u_long cmd;
    326 {
    327 	char dirbuf[4], *dir = dirbuf;
    328 
    329 	if (cmd & IOC_IN)
    330 		*dir++ = 'W';
    331 	if (cmd & IOC_OUT)
    332 		*dir++ = 'R';
    333 	*dir = '\0';
    334 
    335 	printf(decimal ? ",_IO%s('%c',%ld" : ",_IO%s('%c',%#lx",
    336 	    dirbuf, (cmd >> 8) & 0xff, cmd & 0xff);
    337 	if ((cmd & IOC_VOID) == 0)
    338 		printf(decimal ? ",%ld)" : ",%#lx)", (cmd >> 16) & 0xff);
    339 	else
    340 		printf(")");
    341 }
    342 
    343 void
    344 ktrsyscall(ktr)
    345 	struct ktr_syscall *ktr;
    346 {
    347 	int argsize = ktr->ktr_argsize;
    348 	register_t *ap;
    349 
    350 	if (ktr->ktr_code >= current->nsysnames || ktr->ktr_code < 0)
    351 		(void)printf("[%d]", ktr->ktr_code);
    352 	else
    353 		(void)printf("%s", current->sysnames[ktr->ktr_code]);
    354 	ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
    355 	if (argsize) {
    356 		char c = '(';
    357 		if (fancy) {
    358 			if (ktr->ktr_code == SYS_ioctl) {
    359 				char *cp;
    360 				if (decimal)
    361 					(void)printf("(%ld", (long)*ap);
    362 				else
    363 					(void)printf("(%#lx", (long)*ap);
    364 				ap++;
    365 				argsize -= sizeof(register_t);
    366 				if ((cp = ioctlname(*ap)) != NULL)
    367 					(void)printf(",%s", cp);
    368 				else
    369 					ioctldecode(*ap);
    370 				c = ',';
    371 				ap++;
    372 				argsize -= sizeof(register_t);
    373 			} else if (ktr->ktr_code == SYS_ptrace) {
    374 				if (*ap >= 0 && *ap <=
    375 				    sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
    376 					(void)printf("(%s", ptrace_ops[*ap]);
    377 				else
    378 					(void)printf("(%ld", (long)*ap);
    379 				c = ',';
    380 				ap++;
    381 				argsize -= sizeof(register_t);
    382 			}
    383 		}
    384 		while (argsize) {
    385 			if (decimal)
    386 				(void)printf("%c%ld", c, (long)*ap);
    387 			else
    388 				(void)printf("%c%#lx", c, (long)*ap);
    389 			c = ',';
    390 			ap++;
    391 			argsize -= sizeof(register_t);
    392 		}
    393 		(void)putchar(')');
    394 	}
    395 	(void)putchar('\n');
    396 }
    397 
    398 void
    399 ktrsysret(ktr)
    400 	struct ktr_sysret *ktr;
    401 {
    402 	int ret = ktr->ktr_retval;
    403 	int error = ktr->ktr_error;
    404 	int code = ktr->ktr_code;
    405 
    406 	if (code >= current->nsysnames || code < 0)
    407 		(void)printf("[%d] ", code);
    408 	else
    409 		(void)printf("%s ", current->sysnames[code]);
    410 
    411 	switch (error) {
    412 	case 0:
    413 		if (fancy) {
    414 			(void)printf("%d", ret);
    415 			if (ret < 0 || ret > 9)
    416 				(void)printf("/%#x", ret);
    417 		} else {
    418 			if (decimal)
    419 				(void)printf("%d", ret);
    420 			else
    421 				(void)printf("%#x", ret);
    422 		}
    423 		break;
    424 
    425 	default:
    426 		eprint(error);
    427 		break;
    428 	}
    429 	(void)putchar('\n');
    430 
    431 }
    432 
    433 /*
    434  * We print the original emulation's error numerically, but we
    435  * translate it to netbsd to print it symbolically.
    436  */
    437 void
    438 eprint(e)
    439 	int e;
    440 {
    441 	int i = e;
    442 
    443 	if (current->errno) {
    444 
    445 		/* No remapping for ERESTART and EJUSTRETURN */
    446 		/* Kludge for linux that has negative error numbers */
    447 		if (current->errno[2] > 0 && e < 0)
    448 			goto normal;
    449 
    450 		for (i = 0; i < current->nerrno; i++)
    451 			if (e == current->errno[i])
    452 				break;
    453 
    454 		if (i == current->nerrno) {
    455 			printf("-1 unknown errno %d", e);
    456 			return;
    457 		}
    458 	}
    459 
    460 normal:
    461 	switch (i) {
    462 	case ERESTART:
    463 		(void)printf("RESTART");
    464 		break;
    465 
    466 	case EJUSTRETURN:
    467 		(void)printf("JUSTRETURN");
    468 		break;
    469 
    470 	default:
    471 		(void)printf("-1 errno %d", e);
    472 		if (fancy)
    473 			(void)printf(" %s", strerror(i));
    474 	}
    475 }
    476 
    477 void
    478 ktrnamei(cp, len)
    479 	char *cp;
    480 	int len;
    481 {
    482 
    483 	(void)printf("\"%.*s\"\n", len, cp);
    484 }
    485 
    486 void
    487 ktremul(cp, len)
    488 	char *cp;
    489 	int len;
    490 {
    491 	char name[1024];
    492 
    493 	if (len >= sizeof(name))
    494 		errx(1, "Emulation name too long");
    495 
    496 	strncpy(name, cp, len);
    497 	name[len] = '\0';
    498 	(void)printf("\"%s\"\n", name);
    499 
    500 	setemul(name);
    501 }
    502 
    503 void
    504 ktrgenio(ktr, len)
    505 	struct ktr_genio *ktr;
    506 	int len;
    507 {
    508 	int datalen = len - sizeof (struct ktr_genio);
    509 	char *dp = (char *)ktr + sizeof (struct ktr_genio);
    510 	char *cp;
    511 	int col = 0;
    512 	int width;
    513 	char visbuf[5];
    514 	static int screenwidth = 0;
    515 
    516 	if (screenwidth == 0) {
    517 		struct winsize ws;
    518 
    519 		if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
    520 		    ws.ws_col > 8)
    521 			screenwidth = ws.ws_col;
    522 		else
    523 			screenwidth = 80;
    524 	}
    525 	printf("fd %d %s %d bytes\n", ktr->ktr_fd,
    526 		ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
    527 	if (maxdata && datalen > maxdata)
    528 		datalen = maxdata;
    529 	(void)printf("       \"");
    530 	col = 8;
    531 	for (; datalen > 0; datalen--, dp++) {
    532 		(void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
    533 		cp = visbuf;
    534 		/*
    535 		 * Keep track of printables and
    536 		 * space chars (like fold(1)).
    537 		 */
    538 		if (col == 0) {
    539 			(void)putchar('\t');
    540 			col = 8;
    541 		}
    542 		switch(*cp) {
    543 		case '\n':
    544 			col = 0;
    545 			(void)putchar('\n');
    546 			continue;
    547 		case '\t':
    548 			width = 8 - (col&07);
    549 			break;
    550 		default:
    551 			width = strlen(cp);
    552 		}
    553 		if (col + width > (screenwidth-2)) {
    554 			(void)printf("\\\n\t");
    555 			col = 8;
    556 		}
    557 		col += width;
    558 		do {
    559 			(void)putchar(*cp++);
    560 		} while (*cp);
    561 	}
    562 	if (col == 0)
    563 		(void)printf("       ");
    564 	(void)printf("\"\n");
    565 }
    566 
    567 void
    568 ktrpsig(psig)
    569 	struct ktr_psig *psig;
    570 {
    571 	int signo, first;
    572 
    573 	(void)printf("SIG%s ", sys_signame[psig->signo]);
    574 	if (psig->action == SIG_DFL)
    575 		(void)printf("SIG_DFL\n");
    576 	else {
    577 		(void)printf("caught handler=0x%lx mask=(",
    578 		    (u_long)psig->action);
    579 		first = 1;
    580 		for (signo = 1; signo < NSIG; signo++) {
    581 			if (sigismember(&psig->mask, signo)) {
    582 				if (first)
    583 					first = 0;
    584 				else
    585 					(void)printf(",");
    586 				(void)printf("%d", signo);
    587 			}
    588 		}
    589 		(void)printf(") code=0x%x\n", psig->code);
    590 	}
    591 }
    592 
    593 void
    594 ktrcsw(cs)
    595 	struct ktr_csw *cs;
    596 {
    597 
    598 	(void)printf("%s %s\n", cs->out ? "stop" : "resume",
    599 	    cs->user ? "user" : "kernel");
    600 }
    601 
    602 void
    603 usage()
    604 {
    605 
    606 	(void)fprintf(stderr,
    607 "usage: kdump [-dnlRT] [-e emulation] [-f trfile] [-m maxdata] [-t [cnis]]\n");
    608 	exit(1);
    609 }
    610 
    611 void
    612 setemul(name)
    613 	char *name;
    614 {
    615 	int i;
    616 
    617 	for (i = 0; emulations[i].name != NULL; i++)
    618 		if (strcmp(emulations[i].name, name) == 0) {
    619 			current = &emulations[i];
    620 			return;
    621 		}
    622 	warnx("Emulation `%s' unknown", name);
    623 }
    624