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