xref: /freebsd/usr.bin/kdump/kdump.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
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[] = "@(#)kdump.c	8.1 (Berkeley) 6/6/93";
42 #endif /* not lint */
43 
44 #define KERNEL
45 extern int errno;
46 #include <sys/errno.h>
47 #undef KERNEL
48 #include <sys/param.h>
49 #include <sys/errno.h>
50 #include <sys/time.h>
51 #include <sys/uio.h>
52 #include <sys/ktrace.h>
53 #include <sys/ioctl.h>
54 #include <sys/ptrace.h>
55 #include <vis.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include "ktrace.h"
60 
61 int timestamp, decimal, fancy = 1, tail, maxdata;
62 char *tracefile = DEF_TRACEFILE;
63 struct ktr_header ktr_header;
64 
65 #define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
66 
67 main(argc, argv)
68 	int argc;
69 	char *argv[];
70 {
71 	extern int optind;
72 	extern char *optarg;
73 	int ch, ktrlen, size;
74 	register void *m;
75 	int trpoints = ALL_POINTS;
76 
77 	while ((ch = getopt(argc,argv,"f:dlm:nRTt:")) != EOF)
78 		switch((char)ch) {
79 		case 'f':
80 			tracefile = optarg;
81 			break;
82 		case 'd':
83 			decimal = 1;
84 			break;
85 		case 'l':
86 			tail = 1;
87 			break;
88 		case 'm':
89 			maxdata = atoi(optarg);
90 			break;
91 		case 'n':
92 			fancy = 0;
93 			break;
94 		case 'R':
95 			timestamp = 2;	/* relative timestamp */
96 			break;
97 		case 'T':
98 			timestamp = 1;
99 			break;
100 		case 't':
101 			trpoints = getpoints(optarg);
102 			if (trpoints < 0) {
103 				(void)fprintf(stderr,
104 				    "kdump: unknown trace point in %s\n",
105 				    optarg);
106 				exit(1);
107 			}
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 		(void)fprintf(stderr, "kdump: %s.\n", strerror(ENOMEM));
121 		exit(1);
122 	}
123 	if (!freopen(tracefile, "r", stdin)) {
124 		(void)fprintf(stderr,
125 		    "kdump: %s: %s.\n", tracefile, strerror(errno));
126 		exit(1);
127 	}
128 	while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
129 		if (trpoints & (1<<ktr_header.ktr_type))
130 			dumpheader(&ktr_header);
131 		if ((ktrlen = ktr_header.ktr_len) < 0) {
132 			(void)fprintf(stderr,
133 			    "kdump: bogus length 0x%x\n", ktrlen);
134 			exit(1);
135 		}
136 		if (ktrlen > size) {
137 			m = (void *)realloc(m, ktrlen+1);
138 			if (m == NULL) {
139 				(void)fprintf(stderr,
140 				    "kdump: %s.\n", strerror(ENOMEM));
141 				exit(1);
142 			}
143 			size = ktrlen;
144 		}
145 		if (ktrlen && fread_tail(m, ktrlen, 1) == 0) {
146 			(void)fprintf(stderr, "kdump: data too short.\n");
147 			exit(1);
148 		}
149 		if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
150 			continue;
151 		switch (ktr_header.ktr_type) {
152 		case KTR_SYSCALL:
153 			ktrsyscall((struct ktr_syscall *)m);
154 			break;
155 		case KTR_SYSRET:
156 			ktrsysret((struct ktr_sysret *)m);
157 			break;
158 		case KTR_NAMEI:
159 			ktrnamei(m, ktrlen);
160 			break;
161 		case KTR_GENIO:
162 			ktrgenio((struct ktr_genio *)m, ktrlen);
163 			break;
164 		case KTR_PSIG:
165 			ktrpsig((struct ktr_psig *)m);
166 			break;
167 		case KTR_CSW:
168 			ktrcsw((struct ktr_csw *)m);
169 			break;
170 		}
171 		if (tail)
172 			(void)fflush(stdout);
173 	}
174 }
175 
176 fread_tail(buf, size, num)
177 	char *buf;
178 	int num, size;
179 {
180 	int i;
181 
182 	while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
183 		(void)sleep(1);
184 		clearerr(stdin);
185 	}
186 	return (i);
187 }
188 
189 dumpheader(kth)
190 	struct ktr_header *kth;
191 {
192 	static char unknown[64];
193 	static struct timeval prevtime, temp;
194 	char *type;
195 
196 	switch (kth->ktr_type) {
197 	case KTR_SYSCALL:
198 		type = "CALL";
199 		break;
200 	case KTR_SYSRET:
201 		type = "RET ";
202 		break;
203 	case KTR_NAMEI:
204 		type = "NAMI";
205 		break;
206 	case KTR_GENIO:
207 		type = "GIO ";
208 		break;
209 	case KTR_PSIG:
210 		type = "PSIG";
211 		break;
212 	case KTR_CSW:
213 		type = "CSW";
214 		break;
215 	default:
216 		(void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
217 		type = unknown;
218 	}
219 
220 	(void)printf("%6d %-8s ", kth->ktr_pid, kth->ktr_comm);
221 	if (timestamp) {
222 		if (timestamp == 2) {
223 			temp = kth->ktr_time;
224 			timevalsub(&kth->ktr_time, &prevtime);
225 			prevtime = temp;
226 		}
227 		(void)printf("%ld.%06ld ",
228 		    kth->ktr_time.tv_sec, kth->ktr_time.tv_usec);
229 	}
230 	(void)printf("%s  ", type);
231 }
232 
233 #include <sys/syscall.h>
234 #define KTRACE
235 #include <sys/kern/syscalls.c>
236 #undef KTRACE
237 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
238 
239 static char *ptrace_ops[] = {
240 	"PT_TRACE_ME",	"PT_READ_I",	"PT_READ_D",	"PT_READ_U",
241 	"PT_WRITE_I",	"PT_WRITE_D",	"PT_WRITE_U",	"PT_CONTINUE",
242 	"PT_KILL",	"PT_STEP",
243 };
244 
245 ktrsyscall(ktr)
246 	register struct ktr_syscall *ktr;
247 {
248 	register narg = ktr->ktr_narg;
249 	register int *ip;
250 	char *ioctlname();
251 
252 	if (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0)
253 		(void)printf("[%d]", ktr->ktr_code);
254 	else
255 		(void)printf("%s", syscallnames[ktr->ktr_code]);
256 	ip = (int *)((char *)ktr + sizeof(struct ktr_syscall));
257 	if (narg) {
258 		char c = '(';
259 		if (fancy) {
260 			if (ktr->ktr_code == SYS_ioctl) {
261 				char *cp;
262 				if (decimal)
263 					(void)printf("(%d", *ip);
264 				else
265 					(void)printf("(%#x", *ip);
266 				ip++;
267 				narg--;
268 				if ((cp = ioctlname(*ip)) != NULL)
269 					(void)printf(",%s", cp);
270 				else {
271 					if (decimal)
272 						(void)printf(",%d", *ip);
273 					else
274 						(void)printf(",%#x ", *ip);
275 				}
276 				c = ',';
277 				ip++;
278 				narg--;
279 			} else if (ktr->ktr_code == SYS_ptrace) {
280 				if (*ip <= PT_STEP && *ip >= 0)
281 					(void)printf("(%s", ptrace_ops[*ip]);
282 				else
283 					(void)printf("(%d", *ip);
284 				c = ',';
285 				ip++;
286 				narg--;
287 			}
288 		}
289 		while (narg) {
290 			if (decimal)
291 				(void)printf("%c%d", c, *ip);
292 			else
293 				(void)printf("%c%#x", c, *ip);
294 			c = ',';
295 			ip++;
296 			narg--;
297 		}
298 		(void)putchar(')');
299 	}
300 	(void)putchar('\n');
301 }
302 
303 ktrsysret(ktr)
304 	struct ktr_sysret *ktr;
305 {
306 	register int ret = ktr->ktr_retval;
307 	register int error = ktr->ktr_error;
308 	register int code = ktr->ktr_code;
309 
310 	if (code >= nsyscalls || code < 0)
311 		(void)printf("[%d] ", code);
312 	else
313 		(void)printf("%s ", syscallnames[code]);
314 
315 	if (error == 0) {
316 		if (fancy) {
317 			(void)printf("%d", ret);
318 			if (ret < 0 || ret > 9)
319 				(void)printf("/%#x", ret);
320 		} else {
321 			if (decimal)
322 				(void)printf("%d", ret);
323 			else
324 				(void)printf("%#x", ret);
325 		}
326 	} else if (error == ERESTART)
327 		(void)printf("RESTART");
328 	else if (error == EJUSTRETURN)
329 		(void)printf("JUSTRETURN");
330 	else {
331 		(void)printf("-1 errno %d", ktr->ktr_error);
332 		if (fancy)
333 			(void)printf(" %s", strerror(ktr->ktr_error));
334 	}
335 	(void)putchar('\n');
336 }
337 
338 ktrnamei(cp, len)
339 	char *cp;
340 {
341 	(void)printf("\"%.*s\"\n", len, cp);
342 }
343 
344 ktrgenio(ktr, len)
345 	struct ktr_genio *ktr;
346 {
347 	register int datalen = len - sizeof (struct ktr_genio);
348 	register char *dp = (char *)ktr + sizeof (struct ktr_genio);
349 	register char *cp;
350 	register int col = 0;
351 	register width;
352 	char visbuf[5];
353 	static screenwidth = 0;
354 
355 	if (screenwidth == 0) {
356 		struct winsize ws;
357 
358 		if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
359 		    ws.ws_col > 8)
360 			screenwidth = ws.ws_col;
361 		else
362 			screenwidth = 80;
363 	}
364 	printf("fd %d %s %d bytes\n", ktr->ktr_fd,
365 		ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
366 	if (maxdata && datalen > maxdata)
367 		datalen = maxdata;
368 	(void)printf("       \"");
369 	col = 8;
370 	for (;datalen > 0; datalen--, dp++) {
371 		(void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
372 		cp = visbuf;
373 		/*
374 		 * Keep track of printables and
375 		 * space chars (like fold(1)).
376 		 */
377 		if (col == 0) {
378 			(void)putchar('\t');
379 			col = 8;
380 		}
381 		switch(*cp) {
382 		case '\n':
383 			col = 0;
384 			(void)putchar('\n');
385 			continue;
386 		case '\t':
387 			width = 8 - (col&07);
388 			break;
389 		default:
390 			width = strlen(cp);
391 		}
392 		if (col + width > (screenwidth-2)) {
393 			(void)printf("\\\n\t");
394 			col = 8;
395 		}
396 		col += width;
397 		do {
398 			(void)putchar(*cp++);
399 		} while (*cp);
400 	}
401 	if (col == 0)
402 		(void)printf("       ");
403 	(void)printf("\"\n");
404 }
405 
406 char *signames[] = {
407 	"NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT",	/*  1 - 6  */
408 	"EMT", "FPE", "KILL", "BUS", "SEGV", "SYS",		/*  7 - 12 */
409 	"PIPE", "ALRM",  "TERM", "URG", "STOP", "TSTP",		/* 13 - 18 */
410 	"CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU",		/* 19 - 24 */
411 	"XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1",	/* 25 - 30 */
412 	"USR2", NULL,						/* 31 - 32 */
413 };
414 
415 ktrpsig(psig)
416 	struct ktr_psig *psig;
417 {
418 	(void)printf("SIG%s ", signames[psig->signo]);
419 	if (psig->action == SIG_DFL)
420 		(void)printf("SIG_DFL\n");
421 	else
422 		(void)printf("caught handler=0x%x mask=0x%x code=0x%x\n",
423 		    (u_int)psig->action, psig->mask, psig->code);
424 }
425 
426 ktrcsw(cs)
427 	struct ktr_csw *cs;
428 {
429 	(void)printf("%s %s\n", cs->out ? "stop" : "resume",
430 		cs->user ? "user" : "kernel");
431 }
432 
433 usage()
434 {
435 	(void)fprintf(stderr,
436 	    "usage: kdump [-dnlRT] [-f trfile] [-m maxdata] [-t [cnis]]\n");
437 	exit(1);
438 }
439