xref: /freebsd/usr.bin/kdump/kdump.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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 const 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 #if 0
42 static char sccsid[] = "@(#)kdump.c	8.1 (Berkeley) 6/6/93";
43 #endif
44 #endif /* not lint */
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #define _KERNEL
49 extern int errno;
50 #include <sys/errno.h>
51 #undef _KERNEL
52 #include <sys/param.h>
53 #include <sys/errno.h>
54 #define _KERNEL
55 #include <sys/time.h>
56 #undef _KERNEL
57 #include <sys/uio.h>
58 #include <sys/ktrace.h>
59 #include <sys/ioctl.h>
60 #include <sys/ptrace.h>
61 #include <err.h>
62 #include <locale.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
67 #include <vis.h>
68 #include "ktrace.h"
69 
70 int fread_tail(void *, int, int);
71 void dumpheader(struct ktr_header *);
72 void ktrsyscall(struct ktr_syscall *);
73 void ktrsysret(struct ktr_sysret *);
74 void ktrnamei(char *, int);
75 void hexdump(char *, int, int);
76 void visdump(char *, int, int);
77 void ktrgenio(struct ktr_genio *, int);
78 void ktrpsig(struct ktr_psig *);
79 void ktrcsw(struct ktr_csw *);
80 void ktruser(int, unsigned char *);
81 void usage(void);
82 
83 int timestamp, decimal, fancy = 1, tail, maxdata;
84 const char *tracefile = DEF_TRACEFILE;
85 struct ktr_header ktr_header;
86 
87 #define eqs(s1, s2)	(strcmp((s1), (s2)) == 0)
88 
89 int
90 main(int argc, char *argv[])
91 {
92 	int ch, ktrlen, size;
93 	void *m;
94 	int trpoints = ALL_POINTS;
95 	int drop_logged;
96 	pid_t pid = 0;
97 
98 	(void) setlocale(LC_CTYPE, "");
99 
100 	while ((ch = getopt(argc,argv,"f:dlm:np:RTt:")) != -1)
101 		switch((char)ch) {
102 		case 'f':
103 			tracefile = optarg;
104 			break;
105 		case 'd':
106 			decimal = 1;
107 			break;
108 		case 'l':
109 			tail = 1;
110 			break;
111 		case 'm':
112 			maxdata = atoi(optarg);
113 			break;
114 		case 'n':
115 			fancy = 0;
116 			break;
117 		case 'p':
118 			pid = atoi(optarg);
119 			break;
120 		case 'R':
121 			timestamp = 2;	/* relative timestamp */
122 			break;
123 		case 'T':
124 			timestamp = 1;
125 			break;
126 		case 't':
127 			trpoints = getpoints(optarg);
128 			if (trpoints < 0)
129 				errx(1, "unknown trace point in %s", optarg);
130 			break;
131 		default:
132 			usage();
133 		}
134 
135 	if (argc > optind)
136 		usage();
137 
138 	m = (void *)malloc(size = 1025);
139 	if (m == NULL)
140 		errx(1, "%s", strerror(ENOMEM));
141 	if (!freopen(tracefile, "r", stdin))
142 		err(1, "%s", tracefile);
143 	drop_logged = 0;
144 	while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
145 		if (ktr_header.ktr_type & KTR_DROP) {
146 			ktr_header.ktr_type &= ~KTR_DROP;
147 			if (!drop_logged) {
148 				(void)printf("%6d %-8.*s Events dropped.\n",
149 				    ktr_header.ktr_pid, MAXCOMLEN,
150 				    ktr_header.ktr_comm);
151 				drop_logged = 1;
152 			}
153 		}
154 		if (trpoints & (1<<ktr_header.ktr_type))
155 			if (pid == 0 || ktr_header.ktr_pid == pid)
156 				dumpheader(&ktr_header);
157 		if ((ktrlen = ktr_header.ktr_len) < 0)
158 			errx(1, "bogus length 0x%x", ktrlen);
159 		if (ktrlen > size) {
160 			m = (void *)realloc(m, ktrlen+1);
161 			if (m == NULL)
162 				errx(1, "%s", strerror(ENOMEM));
163 			size = ktrlen;
164 		}
165 		if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
166 			errx(1, "data too short");
167 		if (pid && ktr_header.ktr_pid != pid)
168 			continue;
169 		if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
170 			continue;
171 		drop_logged = 0;
172 		switch (ktr_header.ktr_type) {
173 		case KTR_SYSCALL:
174 			ktrsyscall((struct ktr_syscall *)m);
175 			break;
176 		case KTR_SYSRET:
177 			ktrsysret((struct ktr_sysret *)m);
178 			break;
179 		case KTR_NAMEI:
180 			ktrnamei(m, ktrlen);
181 			break;
182 		case KTR_GENIO:
183 			ktrgenio((struct ktr_genio *)m, ktrlen);
184 			break;
185 		case KTR_PSIG:
186 			ktrpsig((struct ktr_psig *)m);
187 			break;
188 		case KTR_CSW:
189 			ktrcsw((struct ktr_csw *)m);
190 			break;
191 		case KTR_USER:
192 			ktruser(ktrlen, m);
193 			break;
194 		default:
195 			printf("\n");
196 			break;
197 		}
198 		if (tail)
199 			(void)fflush(stdout);
200 	}
201 	return 0;
202 }
203 
204 int
205 fread_tail(void *buf, int size, int num)
206 {
207 	int i;
208 
209 	while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
210 		(void)sleep(1);
211 		clearerr(stdin);
212 	}
213 	return (i);
214 }
215 
216 void
217 dumpheader(struct ktr_header *kth)
218 {
219 	static char unknown[64];
220 	static struct timeval prevtime, temp;
221 	const char *type;
222 
223 	switch (kth->ktr_type) {
224 	case KTR_SYSCALL:
225 		type = "CALL";
226 		break;
227 	case KTR_SYSRET:
228 		type = "RET ";
229 		break;
230 	case KTR_NAMEI:
231 		type = "NAMI";
232 		break;
233 	case KTR_GENIO:
234 		type = "GIO ";
235 		break;
236 	case KTR_PSIG:
237 		type = "PSIG";
238 		break;
239 	case KTR_CSW:
240 		type = "CSW";
241 		break;
242 	case KTR_USER:
243 		type = "USER";
244 		break;
245 	default:
246 		(void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
247 		type = unknown;
248 	}
249 
250 	(void)printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
251 	if (timestamp) {
252 		if (timestamp == 2) {
253 			temp = kth->ktr_time;
254 			timevalsub(&kth->ktr_time, &prevtime);
255 			prevtime = temp;
256 		}
257 		(void)printf("%ld.%06ld ",
258 		    kth->ktr_time.tv_sec, kth->ktr_time.tv_usec);
259 	}
260 	(void)printf("%s  ", type);
261 }
262 
263 #include <sys/syscall.h>
264 #define KTRACE
265 #include <sys/kern/syscalls.c>
266 #undef KTRACE
267 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
268 
269 static const char *ptrace_ops[] = {
270 	"PT_TRACE_ME",	"PT_READ_I",	"PT_READ_D",	"PT_READ_U",
271 	"PT_WRITE_I",	"PT_WRITE_D",	"PT_WRITE_U",	"PT_CONTINUE",
272 	"PT_KILL",	"PT_STEP",	"PT_ATTACH",	"PT_DETACH",
273 };
274 
275 void
276 ktrsyscall(struct ktr_syscall *ktr)
277 {
278 	int narg = ktr->ktr_narg;
279 	register_t *ip;
280 	const char *ioctlname(u_long);
281 
282 	if (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0)
283 		(void)printf("[%d]", ktr->ktr_code);
284 	else
285 		(void)printf("%s", syscallnames[ktr->ktr_code]);
286 	ip = &ktr->ktr_args[0];
287 	if (narg) {
288 		char c = '(';
289 		if (fancy) {
290 			if (ktr->ktr_code == SYS_ioctl) {
291 				const char *cp;
292 				if (decimal)
293 					(void)printf("(%ld", (long)*ip);
294 				else
295 					(void)printf("(%#lx", (long)*ip);
296 				ip++;
297 				narg--;
298 				if ((cp = ioctlname(*ip)) != NULL)
299 					(void)printf(",%s", cp);
300 				else {
301 					if (decimal)
302 						(void)printf(",%ld", (long)*ip);
303 					else
304 						(void)printf(",%#lx ", (long)*ip);
305 				}
306 				c = ',';
307 				ip++;
308 				narg--;
309 			} else if (ktr->ktr_code == SYS_ptrace) {
310 				if ((size_t)*ip < sizeof(ptrace_ops) /
311 				    sizeof(ptrace_ops[0]) && *ip >= 0)
312 					(void)printf("(%s", ptrace_ops[*ip]);
313 #ifdef PT_GETREGS
314 				else if (*ip == PT_GETREGS)
315 					(void)printf("(%s", "PT_GETREGS");
316 #endif
317 #ifdef PT_SETREGS
318 				else if (*ip == PT_SETREGS)
319 					(void)printf("(%s", "PT_SETREGS");
320 #endif
321 #ifdef PT_GETFPREGS
322 				else if (*ip == PT_GETFPREGS)
323 					(void)printf("(%s", "PT_GETFPREGS");
324 #endif
325 #ifdef PT_SETFPREGS
326 				else if (*ip == PT_SETFPREGS)
327 					(void)printf("(%s", "PT_SETFPREGS");
328 #endif
329 #ifdef PT_GETDBREGS
330 				else if (*ip == PT_GETDBREGS)
331 					(void)printf("(%s", "PT_GETDBREGS");
332 #endif
333 #ifdef PT_SETDBREGS
334 				else if (*ip == PT_SETDBREGS)
335 					(void)printf("(%s", "PT_SETDBREGS");
336 #endif
337 				else
338 					(void)printf("(%ld", (long)*ip);
339 				c = ',';
340 				ip++;
341 				narg--;
342 			}
343 		}
344 		while (narg) {
345 			if (decimal)
346 				(void)printf("%c%ld", c, (long)*ip);
347 			else
348 				(void)printf("%c%#lx", c, (long)*ip);
349 			c = ',';
350 			ip++;
351 			narg--;
352 		}
353 		(void)putchar(')');
354 	}
355 	(void)putchar('\n');
356 }
357 
358 void
359 ktrsysret(struct ktr_sysret *ktr)
360 {
361 	register_t ret = ktr->ktr_retval;
362 	int error = ktr->ktr_error;
363 	int code = ktr->ktr_code;
364 
365 	if (code >= nsyscalls || code < 0)
366 		(void)printf("[%d] ", code);
367 	else
368 		(void)printf("%s ", syscallnames[code]);
369 
370 	if (error == 0) {
371 		if (fancy) {
372 			(void)printf("%d", ret);
373 			if (ret < 0 || ret > 9)
374 				(void)printf("/%#lx", (long)ret);
375 		} else {
376 			if (decimal)
377 				(void)printf("%ld", (long)ret);
378 			else
379 				(void)printf("%#lx", (long)ret);
380 		}
381 	} else if (error == ERESTART)
382 		(void)printf("RESTART");
383 	else if (error == EJUSTRETURN)
384 		(void)printf("JUSTRETURN");
385 	else {
386 		(void)printf("-1 errno %d", ktr->ktr_error);
387 		if (fancy)
388 			(void)printf(" %s", strerror(ktr->ktr_error));
389 	}
390 	(void)putchar('\n');
391 }
392 
393 void
394 ktrnamei(char *cp, int len)
395 {
396 	(void)printf("\"%.*s\"\n", len, cp);
397 }
398 
399 void
400 hexdump(char *p, int len, int screenwidth)
401 {
402 	int n, i;
403 	int width;
404 
405 	width = 0;
406 	do {
407 		width += 2;
408 		i = 13;			/* base offset */
409 		i += (width / 2) + 1;	/* spaces every second byte */
410 		i += (width * 2);	/* width of bytes */
411 		i += 3;			/* "  |" */
412 		i += width;		/* each byte */
413 		i += 1;			/* "|" */
414 	} while (i < screenwidth);
415 	width -= 2;
416 
417 	for (n = 0; n < len; n += width) {
418 		for (i = n; i < n + width; i++) {
419 			if ((i % width) == 0) {	/* beginning of line */
420 				printf("       0x%04x", i);
421 			}
422 			if ((i % 2) == 0) {
423 				printf(" ");
424 			}
425 			if (i < len)
426 				printf("%02x", p[i] & 0xff);
427 			else
428 				printf("  ");
429 		}
430 		printf("  |");
431 		for (i = n; i < n + width; i++) {
432 			if (i >= len)
433 				break;
434 			if (p[i] >= ' ' && p[i] <= '~')
435 				printf("%c", p[i]);
436 			else
437 				printf(".");
438 		}
439 		printf("|\n");
440 	}
441 	if ((i % width) != 0)
442 		printf("\n");
443 }
444 
445 void
446 visdump(char *dp, int datalen, int screenwidth)
447 {
448 	int col = 0;
449 	char *cp;
450 	int width;
451 	char visbuf[5];
452 
453 	(void)printf("       \"");
454 	col = 8;
455 	for (;datalen > 0; datalen--, dp++) {
456 		(void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
457 		cp = visbuf;
458 		/*
459 		 * Keep track of printables and
460 		 * space chars (like fold(1)).
461 		 */
462 		if (col == 0) {
463 			(void)putchar('\t');
464 			col = 8;
465 		}
466 		switch(*cp) {
467 		case '\n':
468 			col = 0;
469 			(void)putchar('\n');
470 			continue;
471 		case '\t':
472 			width = 8 - (col&07);
473 			break;
474 		default:
475 			width = strlen(cp);
476 		}
477 		if (col + width > (screenwidth-2)) {
478 			(void)printf("\\\n\t");
479 			col = 8;
480 		}
481 		col += width;
482 		do {
483 			(void)putchar(*cp++);
484 		} while (*cp);
485 	}
486 	if (col == 0)
487 		(void)printf("       ");
488 	(void)printf("\"\n");
489 }
490 
491 void
492 ktrgenio(struct ktr_genio *ktr, int len)
493 {
494 	int datalen = len - sizeof (struct ktr_genio);
495 	char *dp = (char *)ktr + sizeof (struct ktr_genio);
496 	static int screenwidth = 0;
497 	int i, binary;
498 
499 	if (screenwidth == 0) {
500 		struct winsize ws;
501 
502 		if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
503 		    ws.ws_col > 8)
504 			screenwidth = ws.ws_col;
505 		else
506 			screenwidth = 80;
507 	}
508 	printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
509 		ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
510 		datalen == 1 ? "" : "s");
511 	if (maxdata && datalen > maxdata)
512 		datalen = maxdata;
513 
514 	for (i = 0, binary = 0; i < datalen && binary == 0; i++)  {
515 		if (dp[i] >= 32 && dp[i] < 127)
516 			continue;
517 		if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
518 			continue;
519 		binary = 1;
520 	}
521 	if (binary)
522 		hexdump(dp, datalen, screenwidth);
523 	else
524 		visdump(dp, datalen, screenwidth);
525 }
526 
527 const char *signames[] = {
528 	"NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT",	/*  1 - 6  */
529 	"EMT", "FPE", "KILL", "BUS", "SEGV", "SYS",		/*  7 - 12 */
530 	"PIPE", "ALRM",  "TERM", "URG", "STOP", "TSTP",		/* 13 - 18 */
531 	"CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU",		/* 19 - 24 */
532 	"XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1",	/* 25 - 30 */
533 	"USR2", NULL,						/* 31 - 32 */
534 };
535 
536 void
537 ktrpsig(struct ktr_psig *psig)
538 {
539 	(void)printf("SIG%s ", signames[psig->signo]);
540 	if (psig->action == SIG_DFL)
541 		(void)printf("SIG_DFL\n");
542 	else {
543 		(void)printf("caught handler=0x%lx mask=0x%x code=0x%x\n",
544 		    (u_long)psig->action, psig->mask.__bits[0], psig->code);
545 	}
546 }
547 
548 void
549 ktrcsw(struct ktr_csw *cs)
550 {
551 	(void)printf("%s %s\n", cs->out ? "stop" : "resume",
552 		cs->user ? "user" : "kernel");
553 }
554 
555 void
556 ktruser(int len, unsigned char *p)
557 {
558 	(void)printf("%d ", len);
559 	while (len--)
560 		(void)printf(" %02x", *p++);
561 	(void)printf("\n");
562 
563 }
564 
565 void
566 usage(void)
567 {
568 	(void)fprintf(stderr,
569 	    "usage: kdump [-dnlRT] [-f trfile] [-m maxdata] [-t [cnisuw]]\n");
570 	exit(1);
571 }
572