Home | History | Annotate | Line # | Download | only in kdump
kdump.c revision 1.10
      1   1.1      cgd /*-
      2   1.1      cgd  * Copyright (c) 1988, 1993
      3   1.1      cgd  *	The Regents of the University of California.  All rights reserved.
      4   1.1      cgd  *
      5   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6   1.1      cgd  * modification, are permitted provided that the following conditions
      7   1.1      cgd  * are met:
      8   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14   1.1      cgd  *    must display the following acknowledgement:
     15   1.1      cgd  *	This product includes software developed by the University of
     16   1.1      cgd  *	California, Berkeley and its contributors.
     17   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18   1.1      cgd  *    may be used to endorse or promote products derived from this software
     19   1.1      cgd  *    without specific prior written permission.
     20   1.1      cgd  *
     21   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31   1.1      cgd  * SUCH DAMAGE.
     32   1.1      cgd  */
     33   1.1      cgd 
     34   1.1      cgd #ifndef lint
     35   1.1      cgd static char copyright[] =
     36   1.1      cgd "@(#) Copyright (c) 1988, 1993\n\
     37   1.1      cgd 	The Regents of the University of California.  All rights reserved.\n";
     38   1.1      cgd #endif /* not lint */
     39   1.1      cgd 
     40   1.1      cgd #ifndef lint
     41   1.3  mycroft /*static char sccsid[] = "from: @(#)kdump.c	8.1 (Berkeley) 6/6/93";*/
     42  1.10      cgd static char *rcsid = "$Id: kdump.c,v 1.10 1995/06/07 07:24:19 cgd Exp $";
     43   1.1      cgd #endif /* not lint */
     44   1.1      cgd 
     45   1.1      cgd #include <sys/param.h>
     46   1.1      cgd #include <sys/errno.h>
     47   1.1      cgd #include <sys/time.h>
     48   1.1      cgd #include <sys/uio.h>
     49   1.1      cgd #include <sys/ktrace.h>
     50   1.1      cgd #include <sys/ioctl.h>
     51   1.1      cgd #include <sys/ptrace.h>
     52   1.8      jtc #define _KERNEL
     53   1.1      cgd #include <sys/errno.h>
     54   1.8      jtc #undef _KERNEL
     55   1.4  mycroft 
     56   1.4  mycroft #include <err.h>
     57   1.4  mycroft #include <signal.h>
     58   1.1      cgd #include <stdio.h>
     59   1.1      cgd #include <stdlib.h>
     60   1.1      cgd #include <string.h>
     61   1.4  mycroft #include <unistd.h>
     62   1.4  mycroft #include <vis.h>
     63   1.4  mycroft 
     64   1.1      cgd #include "ktrace.h"
     65   1.1      cgd 
     66   1.1      cgd int timestamp, decimal, fancy = 1, tail, maxdata;
     67   1.1      cgd char *tracefile = DEF_TRACEFILE;
     68   1.1      cgd struct ktr_header ktr_header;
     69   1.1      cgd 
     70   1.1      cgd #define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
     71   1.1      cgd 
     72   1.4  mycroft int
     73   1.1      cgd main(argc, argv)
     74   1.1      cgd 	int argc;
     75   1.1      cgd 	char *argv[];
     76   1.1      cgd {
     77   1.1      cgd 	int ch, ktrlen, size;
     78   1.1      cgd 	register void *m;
     79   1.1      cgd 	int trpoints = ALL_POINTS;
     80   1.1      cgd 
     81   1.4  mycroft 	while ((ch = getopt(argc, argv, "f:dlm:nRTt:")) != -1)
     82   1.4  mycroft 		switch (ch) {
     83   1.1      cgd 		case 'f':
     84   1.1      cgd 			tracefile = optarg;
     85   1.1      cgd 			break;
     86   1.1      cgd 		case 'd':
     87   1.1      cgd 			decimal = 1;
     88   1.1      cgd 			break;
     89   1.1      cgd 		case 'l':
     90   1.1      cgd 			tail = 1;
     91   1.1      cgd 			break;
     92   1.1      cgd 		case 'm':
     93   1.1      cgd 			maxdata = atoi(optarg);
     94   1.1      cgd 			break;
     95   1.1      cgd 		case 'n':
     96   1.1      cgd 			fancy = 0;
     97   1.1      cgd 			break;
     98   1.1      cgd 		case 'R':
     99   1.1      cgd 			timestamp = 2;	/* relative timestamp */
    100   1.1      cgd 			break;
    101   1.1      cgd 		case 'T':
    102   1.1      cgd 			timestamp = 1;
    103   1.1      cgd 			break;
    104   1.1      cgd 		case 't':
    105   1.1      cgd 			trpoints = getpoints(optarg);
    106   1.4  mycroft 			if (trpoints < 0)
    107   1.4  mycroft 				errx(1, "unknown trace point in %s", optarg);
    108   1.1      cgd 			break;
    109   1.1      cgd 		default:
    110   1.1      cgd 			usage();
    111   1.1      cgd 		}
    112   1.1      cgd 	argv += optind;
    113   1.1      cgd 	argc -= optind;
    114   1.1      cgd 
    115   1.1      cgd 	if (argc > 1)
    116   1.1      cgd 		usage();
    117   1.1      cgd 
    118   1.1      cgd 	m = (void *)malloc(size = 1025);
    119   1.4  mycroft 	if (m == NULL)
    120   1.4  mycroft 		errx(1, "%s", strerror(ENOMEM));
    121   1.4  mycroft 	if (!freopen(tracefile, "r", stdin))
    122   1.4  mycroft 		err(1, "%s", tracefile);
    123   1.1      cgd 	while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
    124   1.1      cgd 		if (trpoints & (1<<ktr_header.ktr_type))
    125   1.1      cgd 			dumpheader(&ktr_header);
    126   1.4  mycroft 		if ((ktrlen = ktr_header.ktr_len) < 0)
    127   1.4  mycroft 			errx(1, "bogus length 0x%x", ktrlen);
    128   1.1      cgd 		if (ktrlen > size) {
    129   1.1      cgd 			m = (void *)realloc(m, ktrlen+1);
    130   1.4  mycroft 			if (m == NULL)
    131   1.4  mycroft 				errx(1, "%s", strerror(ENOMEM));
    132   1.1      cgd 			size = ktrlen;
    133   1.1      cgd 		}
    134   1.4  mycroft 		if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
    135   1.4  mycroft 			errx(1, "data too short");
    136   1.1      cgd 		if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
    137   1.1      cgd 			continue;
    138   1.1      cgd 		switch (ktr_header.ktr_type) {
    139   1.1      cgd 		case KTR_SYSCALL:
    140   1.1      cgd 			ktrsyscall((struct ktr_syscall *)m);
    141   1.1      cgd 			break;
    142   1.1      cgd 		case KTR_SYSRET:
    143   1.1      cgd 			ktrsysret((struct ktr_sysret *)m);
    144   1.1      cgd 			break;
    145   1.1      cgd 		case KTR_NAMEI:
    146   1.1      cgd 			ktrnamei(m, ktrlen);
    147   1.1      cgd 			break;
    148   1.1      cgd 		case KTR_GENIO:
    149   1.1      cgd 			ktrgenio((struct ktr_genio *)m, ktrlen);
    150   1.1      cgd 			break;
    151   1.1      cgd 		case KTR_PSIG:
    152   1.1      cgd 			ktrpsig((struct ktr_psig *)m);
    153   1.1      cgd 			break;
    154   1.1      cgd 		case KTR_CSW:
    155   1.1      cgd 			ktrcsw((struct ktr_csw *)m);
    156   1.1      cgd 			break;
    157   1.1      cgd 		}
    158   1.1      cgd 		if (tail)
    159   1.1      cgd 			(void)fflush(stdout);
    160   1.1      cgd 	}
    161   1.1      cgd }
    162   1.1      cgd 
    163   1.1      cgd fread_tail(buf, size, num)
    164   1.1      cgd 	char *buf;
    165   1.1      cgd 	int num, size;
    166   1.1      cgd {
    167   1.1      cgd 	int i;
    168   1.1      cgd 
    169   1.1      cgd 	while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
    170   1.1      cgd 		(void)sleep(1);
    171   1.1      cgd 		clearerr(stdin);
    172   1.1      cgd 	}
    173   1.1      cgd 	return (i);
    174   1.1      cgd }
    175   1.1      cgd 
    176   1.1      cgd dumpheader(kth)
    177   1.1      cgd 	struct ktr_header *kth;
    178   1.1      cgd {
    179   1.6  mycroft 	char unknown[64], *type;
    180   1.6  mycroft 	static struct timeval prevtime;
    181   1.6  mycroft 	struct timeval temp;
    182   1.1      cgd 
    183   1.1      cgd 	switch (kth->ktr_type) {
    184   1.1      cgd 	case KTR_SYSCALL:
    185   1.1      cgd 		type = "CALL";
    186   1.1      cgd 		break;
    187   1.1      cgd 	case KTR_SYSRET:
    188   1.1      cgd 		type = "RET ";
    189   1.1      cgd 		break;
    190   1.1      cgd 	case KTR_NAMEI:
    191   1.1      cgd 		type = "NAMI";
    192   1.1      cgd 		break;
    193   1.1      cgd 	case KTR_GENIO:
    194   1.1      cgd 		type = "GIO ";
    195   1.1      cgd 		break;
    196   1.1      cgd 	case KTR_PSIG:
    197   1.1      cgd 		type = "PSIG";
    198   1.1      cgd 		break;
    199   1.1      cgd 	case KTR_CSW:
    200   1.1      cgd 		type = "CSW";
    201   1.1      cgd 		break;
    202   1.1      cgd 	default:
    203   1.1      cgd 		(void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
    204   1.1      cgd 		type = unknown;
    205   1.1      cgd 	}
    206   1.1      cgd 
    207  1.10      cgd 	(void)printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
    208   1.1      cgd 	if (timestamp) {
    209   1.1      cgd 		if (timestamp == 2) {
    210   1.5  mycroft 			timersub(&kth->ktr_time, &prevtime, &temp);
    211   1.5  mycroft 			prevtime = kth->ktr_time;
    212   1.5  mycroft 		} else
    213   1.1      cgd 			temp = kth->ktr_time;
    214   1.5  mycroft 		(void)printf("%ld.%06ld ", temp.tv_sec, temp.tv_usec);
    215   1.1      cgd 	}
    216   1.1      cgd 	(void)printf("%s  ", type);
    217   1.1      cgd }
    218   1.1      cgd 
    219   1.1      cgd #include <sys/syscall.h>
    220   1.1      cgd #define KTRACE
    221   1.9      cgd #include "../../sys/kern/syscalls.c"
    222   1.1      cgd #undef KTRACE
    223   1.1      cgd int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
    224   1.1      cgd 
    225   1.1      cgd static char *ptrace_ops[] = {
    226   1.1      cgd 	"PT_TRACE_ME",	"PT_READ_I",	"PT_READ_D",	"PT_READ_U",
    227   1.1      cgd 	"PT_WRITE_I",	"PT_WRITE_D",	"PT_WRITE_U",	"PT_CONTINUE",
    228   1.2  deraadt 	"PT_KILL",	"PT_ATTACH",	"PT_DETACH",
    229   1.1      cgd };
    230   1.1      cgd 
    231   1.1      cgd ktrsyscall(ktr)
    232   1.1      cgd 	register struct ktr_syscall *ktr;
    233   1.1      cgd {
    234   1.7      cgd 	register argsize = ktr->ktr_argsize;
    235   1.7      cgd 	register register_t *ap;
    236   1.1      cgd 	char *ioctlname();
    237   1.1      cgd 
    238   1.1      cgd 	if (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0)
    239   1.1      cgd 		(void)printf("[%d]", ktr->ktr_code);
    240   1.1      cgd 	else
    241   1.1      cgd 		(void)printf("%s", syscallnames[ktr->ktr_code]);
    242   1.7      cgd 	ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
    243   1.7      cgd 	if (argsize) {
    244   1.1      cgd 		char c = '(';
    245   1.1      cgd 		if (fancy) {
    246   1.1      cgd 			if (ktr->ktr_code == SYS_ioctl) {
    247   1.1      cgd 				char *cp;
    248   1.1      cgd 				if (decimal)
    249   1.7      cgd 					(void)printf("(%ld", (long)*ap);
    250   1.1      cgd 				else
    251   1.7      cgd 					(void)printf("(%#lx", (long)*ap);
    252   1.7      cgd 				ap++;
    253   1.7      cgd 				argsize -= sizeof(register_t);
    254   1.7      cgd 				if ((cp = ioctlname(*ap)) != NULL)
    255   1.1      cgd 					(void)printf(",%s", cp);
    256   1.1      cgd 				else {
    257   1.1      cgd 					if (decimal)
    258   1.7      cgd 						(void)printf(",%ld",
    259   1.7      cgd 						    (long)*ap);
    260   1.1      cgd 					else
    261   1.7      cgd 						(void)printf(",%#lx ",
    262   1.7      cgd 						    (long)*ap);
    263   1.1      cgd 				}
    264   1.1      cgd 				c = ',';
    265   1.7      cgd 				ap++;
    266   1.7      cgd 				argsize -= sizeof(register_t);
    267   1.1      cgd 			} else if (ktr->ktr_code == SYS_ptrace) {
    268   1.7      cgd 				if (*ap <=sizeof(ptrace_ops)/sizeof(ptrace_ops[0]) && *ap >= 0)
    269   1.7      cgd 					(void)printf("(%s", ptrace_ops[*ap]);
    270   1.1      cgd 				else
    271   1.7      cgd 					(void)printf("(%ld", (long)*ap);
    272   1.1      cgd 				c = ',';
    273   1.7      cgd 				ap++;
    274   1.7      cgd 				argsize -= sizeof(register_t);
    275   1.1      cgd 			}
    276   1.1      cgd 		}
    277   1.7      cgd 		while (argsize) {
    278   1.1      cgd 			if (decimal)
    279   1.7      cgd 				(void)printf("%c%ld", c, (long)*ap);
    280   1.1      cgd 			else
    281   1.7      cgd 				(void)printf("%c%#lx", c, (long)*ap);
    282   1.1      cgd 			c = ',';
    283   1.7      cgd 			ap++;
    284   1.7      cgd 			argsize -= sizeof(register_t);
    285   1.1      cgd 		}
    286   1.1      cgd 		(void)putchar(')');
    287   1.1      cgd 	}
    288   1.1      cgd 	(void)putchar('\n');
    289   1.1      cgd }
    290   1.1      cgd 
    291   1.1      cgd ktrsysret(ktr)
    292   1.1      cgd 	struct ktr_sysret *ktr;
    293   1.1      cgd {
    294   1.1      cgd 	register int ret = ktr->ktr_retval;
    295   1.1      cgd 	register int error = ktr->ktr_error;
    296   1.1      cgd 	register int code = ktr->ktr_code;
    297   1.1      cgd 
    298   1.1      cgd 	if (code >= nsyscalls || code < 0)
    299   1.1      cgd 		(void)printf("[%d] ", code);
    300   1.1      cgd 	else
    301   1.1      cgd 		(void)printf("%s ", syscallnames[code]);
    302   1.1      cgd 
    303   1.1      cgd 	if (error == 0) {
    304   1.1      cgd 		if (fancy) {
    305   1.1      cgd 			(void)printf("%d", ret);
    306   1.1      cgd 			if (ret < 0 || ret > 9)
    307   1.1      cgd 				(void)printf("/%#x", ret);
    308   1.1      cgd 		} else {
    309   1.1      cgd 			if (decimal)
    310   1.1      cgd 				(void)printf("%d", ret);
    311   1.1      cgd 			else
    312   1.1      cgd 				(void)printf("%#x", ret);
    313   1.1      cgd 		}
    314   1.1      cgd 	} else if (error == ERESTART)
    315   1.1      cgd 		(void)printf("RESTART");
    316   1.1      cgd 	else if (error == EJUSTRETURN)
    317   1.1      cgd 		(void)printf("JUSTRETURN");
    318   1.1      cgd 	else {
    319   1.1      cgd 		(void)printf("-1 errno %d", ktr->ktr_error);
    320   1.1      cgd 		if (fancy)
    321   1.1      cgd 			(void)printf(" %s", strerror(ktr->ktr_error));
    322   1.1      cgd 	}
    323   1.1      cgd 	(void)putchar('\n');
    324   1.1      cgd }
    325   1.1      cgd 
    326   1.1      cgd ktrnamei(cp, len)
    327   1.1      cgd 	char *cp;
    328   1.1      cgd {
    329   1.1      cgd 	(void)printf("\"%.*s\"\n", len, cp);
    330   1.1      cgd }
    331   1.1      cgd 
    332   1.1      cgd ktrgenio(ktr, len)
    333   1.1      cgd 	struct ktr_genio *ktr;
    334   1.1      cgd {
    335   1.1      cgd 	register int datalen = len - sizeof (struct ktr_genio);
    336   1.1      cgd 	register char *dp = (char *)ktr + sizeof (struct ktr_genio);
    337   1.1      cgd 	register char *cp;
    338   1.1      cgd 	register int col = 0;
    339   1.1      cgd 	register width;
    340   1.1      cgd 	char visbuf[5];
    341   1.1      cgd 	static screenwidth = 0;
    342   1.1      cgd 
    343   1.1      cgd 	if (screenwidth == 0) {
    344   1.1      cgd 		struct winsize ws;
    345   1.1      cgd 
    346   1.1      cgd 		if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
    347   1.1      cgd 		    ws.ws_col > 8)
    348   1.1      cgd 			screenwidth = ws.ws_col;
    349   1.1      cgd 		else
    350   1.1      cgd 			screenwidth = 80;
    351   1.1      cgd 	}
    352   1.1      cgd 	printf("fd %d %s %d bytes\n", ktr->ktr_fd,
    353   1.1      cgd 		ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
    354   1.1      cgd 	if (maxdata && datalen > maxdata)
    355   1.1      cgd 		datalen = maxdata;
    356   1.1      cgd 	(void)printf("       \"");
    357   1.1      cgd 	col = 8;
    358   1.4  mycroft 	for (; datalen > 0; datalen--, dp++) {
    359   1.1      cgd 		(void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
    360   1.1      cgd 		cp = visbuf;
    361   1.1      cgd 		/*
    362   1.1      cgd 		 * Keep track of printables and
    363   1.1      cgd 		 * space chars (like fold(1)).
    364   1.1      cgd 		 */
    365   1.1      cgd 		if (col == 0) {
    366   1.1      cgd 			(void)putchar('\t');
    367   1.1      cgd 			col = 8;
    368   1.1      cgd 		}
    369   1.1      cgd 		switch(*cp) {
    370   1.1      cgd 		case '\n':
    371   1.1      cgd 			col = 0;
    372   1.1      cgd 			(void)putchar('\n');
    373   1.1      cgd 			continue;
    374   1.1      cgd 		case '\t':
    375   1.1      cgd 			width = 8 - (col&07);
    376   1.1      cgd 			break;
    377   1.1      cgd 		default:
    378   1.1      cgd 			width = strlen(cp);
    379   1.1      cgd 		}
    380   1.1      cgd 		if (col + width > (screenwidth-2)) {
    381   1.1      cgd 			(void)printf("\\\n\t");
    382   1.1      cgd 			col = 8;
    383   1.1      cgd 		}
    384   1.1      cgd 		col += width;
    385   1.1      cgd 		do {
    386   1.1      cgd 			(void)putchar(*cp++);
    387   1.1      cgd 		} while (*cp);
    388   1.1      cgd 	}
    389   1.1      cgd 	if (col == 0)
    390   1.1      cgd 		(void)printf("       ");
    391   1.1      cgd 	(void)printf("\"\n");
    392   1.1      cgd }
    393   1.1      cgd 
    394   1.1      cgd ktrpsig(psig)
    395   1.1      cgd 	struct ktr_psig *psig;
    396   1.1      cgd {
    397   1.4  mycroft 	(void)printf("SIG%s ", sys_signame[psig->signo]);
    398   1.1      cgd 	if (psig->action == SIG_DFL)
    399   1.1      cgd 		(void)printf("SIG_DFL\n");
    400   1.1      cgd 	else
    401   1.7      cgd 		(void)printf("caught handler=0x%lx mask=0x%x code=0x%x\n",
    402   1.7      cgd 		    (u_long)psig->action, psig->mask, psig->code);
    403   1.1      cgd }
    404   1.1      cgd 
    405   1.1      cgd ktrcsw(cs)
    406   1.1      cgd 	struct ktr_csw *cs;
    407   1.1      cgd {
    408   1.1      cgd 	(void)printf("%s %s\n", cs->out ? "stop" : "resume",
    409   1.4  mycroft 	    cs->user ? "user" : "kernel");
    410   1.1      cgd }
    411   1.1      cgd 
    412   1.1      cgd usage()
    413   1.1      cgd {
    414   1.4  mycroft 
    415   1.1      cgd 	(void)fprintf(stderr,
    416   1.1      cgd 	    "usage: kdump [-dnlRT] [-f trfile] [-m maxdata] [-t [cnis]]\n");
    417   1.1      cgd 	exit(1);
    418   1.1      cgd }
    419