Home | History | Annotate | Line # | Download | only in kdump
kdump.c revision 1.5
      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.5 1995/03/21 14:20:18 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 			timersub(&kth->ktr_time, &prevtime, &temp);
    211 			prevtime = kth->ktr_time;
    212 		} else
    213 			temp = kth->ktr_time;
    214 		(void)printf("%ld.%06ld ", temp.tv_sec, temp.tv_usec);
    215 	}
    216 	(void)printf("%s  ", type);
    217 }
    218 
    219 #include <sys/syscall.h>
    220 #define KTRACE
    221 #include "/sys/kern/syscalls.c"
    222 #undef KTRACE
    223 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
    224 
    225 static char *ptrace_ops[] = {
    226 	"PT_TRACE_ME",	"PT_READ_I",	"PT_READ_D",	"PT_READ_U",
    227 	"PT_WRITE_I",	"PT_WRITE_D",	"PT_WRITE_U",	"PT_CONTINUE",
    228 	"PT_KILL",	"PT_ATTACH",	"PT_DETACH",
    229 };
    230 
    231 ktrsyscall(ktr)
    232 	register struct ktr_syscall *ktr;
    233 {
    234 	register narg = ktr->ktr_narg;
    235 	register int *ip;
    236 	char *ioctlname();
    237 
    238 	if (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0)
    239 		(void)printf("[%d]", ktr->ktr_code);
    240 	else
    241 		(void)printf("%s", syscallnames[ktr->ktr_code]);
    242 	ip = (int *)((char *)ktr + sizeof(struct ktr_syscall));
    243 	if (narg) {
    244 		char c = '(';
    245 		if (fancy) {
    246 			if (ktr->ktr_code == SYS_ioctl) {
    247 				char *cp;
    248 				if (decimal)
    249 					(void)printf("(%d", *ip);
    250 				else
    251 					(void)printf("(%#x", *ip);
    252 				ip++;
    253 				narg--;
    254 				if ((cp = ioctlname(*ip)) != NULL)
    255 					(void)printf(",%s", cp);
    256 				else {
    257 					if (decimal)
    258 						(void)printf(",%d", *ip);
    259 					else
    260 						(void)printf(",%#x ", *ip);
    261 				}
    262 				c = ',';
    263 				ip++;
    264 				narg--;
    265 			} else if (ktr->ktr_code == SYS_ptrace) {
    266 				if (*ip <=sizeof(ptrace_ops)/sizeof(ptrace_ops[0]) && *ip >= 0)
    267 					(void)printf("(%s", ptrace_ops[*ip]);
    268 				else
    269 					(void)printf("(%d", *ip);
    270 				c = ',';
    271 				ip++;
    272 				narg--;
    273 			}
    274 		}
    275 		while (narg) {
    276 			if (decimal)
    277 				(void)printf("%c%d", c, *ip);
    278 			else
    279 				(void)printf("%c%#x", c, *ip);
    280 			c = ',';
    281 			ip++;
    282 			narg--;
    283 		}
    284 		(void)putchar(')');
    285 	}
    286 	(void)putchar('\n');
    287 }
    288 
    289 ktrsysret(ktr)
    290 	struct ktr_sysret *ktr;
    291 {
    292 	register int ret = ktr->ktr_retval;
    293 	register int error = ktr->ktr_error;
    294 	register int code = ktr->ktr_code;
    295 
    296 	if (code >= nsyscalls || code < 0)
    297 		(void)printf("[%d] ", code);
    298 	else
    299 		(void)printf("%s ", syscallnames[code]);
    300 
    301 	if (error == 0) {
    302 		if (fancy) {
    303 			(void)printf("%d", ret);
    304 			if (ret < 0 || ret > 9)
    305 				(void)printf("/%#x", ret);
    306 		} else {
    307 			if (decimal)
    308 				(void)printf("%d", ret);
    309 			else
    310 				(void)printf("%#x", ret);
    311 		}
    312 	} else if (error == ERESTART)
    313 		(void)printf("RESTART");
    314 	else if (error == EJUSTRETURN)
    315 		(void)printf("JUSTRETURN");
    316 	else {
    317 		(void)printf("-1 errno %d", ktr->ktr_error);
    318 		if (fancy)
    319 			(void)printf(" %s", strerror(ktr->ktr_error));
    320 	}
    321 	(void)putchar('\n');
    322 }
    323 
    324 ktrnamei(cp, len)
    325 	char *cp;
    326 {
    327 	(void)printf("\"%.*s\"\n", len, cp);
    328 }
    329 
    330 ktrgenio(ktr, len)
    331 	struct ktr_genio *ktr;
    332 {
    333 	register int datalen = len - sizeof (struct ktr_genio);
    334 	register char *dp = (char *)ktr + sizeof (struct ktr_genio);
    335 	register char *cp;
    336 	register int col = 0;
    337 	register width;
    338 	char visbuf[5];
    339 	static screenwidth = 0;
    340 
    341 	if (screenwidth == 0) {
    342 		struct winsize ws;
    343 
    344 		if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
    345 		    ws.ws_col > 8)
    346 			screenwidth = ws.ws_col;
    347 		else
    348 			screenwidth = 80;
    349 	}
    350 	printf("fd %d %s %d bytes\n", ktr->ktr_fd,
    351 		ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
    352 	if (maxdata && datalen > maxdata)
    353 		datalen = maxdata;
    354 	(void)printf("       \"");
    355 	col = 8;
    356 	for (; datalen > 0; datalen--, dp++) {
    357 		(void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
    358 		cp = visbuf;
    359 		/*
    360 		 * Keep track of printables and
    361 		 * space chars (like fold(1)).
    362 		 */
    363 		if (col == 0) {
    364 			(void)putchar('\t');
    365 			col = 8;
    366 		}
    367 		switch(*cp) {
    368 		case '\n':
    369 			col = 0;
    370 			(void)putchar('\n');
    371 			continue;
    372 		case '\t':
    373 			width = 8 - (col&07);
    374 			break;
    375 		default:
    376 			width = strlen(cp);
    377 		}
    378 		if (col + width > (screenwidth-2)) {
    379 			(void)printf("\\\n\t");
    380 			col = 8;
    381 		}
    382 		col += width;
    383 		do {
    384 			(void)putchar(*cp++);
    385 		} while (*cp);
    386 	}
    387 	if (col == 0)
    388 		(void)printf("       ");
    389 	(void)printf("\"\n");
    390 }
    391 
    392 ktrpsig(psig)
    393 	struct ktr_psig *psig;
    394 {
    395 	(void)printf("SIG%s ", sys_signame[psig->signo]);
    396 	if (psig->action == SIG_DFL)
    397 		(void)printf("SIG_DFL\n");
    398 	else
    399 		(void)printf("caught handler=0x%x mask=0x%x code=0x%x\n",
    400 		    (u_int)psig->action, psig->mask, psig->code);
    401 }
    402 
    403 ktrcsw(cs)
    404 	struct ktr_csw *cs;
    405 {
    406 	(void)printf("%s %s\n", cs->out ? "stop" : "resume",
    407 	    cs->user ? "user" : "kernel");
    408 }
    409 
    410 usage()
    411 {
    412 
    413 	(void)fprintf(stderr,
    414 	    "usage: kdump [-dnlRT] [-f trfile] [-m maxdata] [-t [cnis]]\n");
    415 	exit(1);
    416 }
    417