kdump.c revision 1.53 1 /* $NetBSD: kdump.c,v 1.53 2003/07/11 13:06:11 wiz Exp $ */
2
3 /*-
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)kdump.c 8.4 (Berkeley) 4/28/95";
45 #else
46 __RCSID("$NetBSD: kdump.c,v 1.53 2003/07/11 13:06:11 wiz Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #define _KERNEL
52 #include <sys/errno.h>
53 #undef _KERNEL
54 #include <sys/time.h>
55 #include <sys/uio.h>
56 #include <sys/ktrace.h>
57 #include <sys/ioctl.h>
58 #include <sys/ptrace.h>
59
60 #include <ctype.h>
61 #include <err.h>
62 #include <signal.h>
63 #include <stddef.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68 #include <vis.h>
69
70 #include "ktrace.h"
71 #include "setemul.h"
72
73 #include <sys/syscall.h>
74
75 int timestamp, decimal, plain, tail, maxdata = -1, numeric;
76 int hexdump;
77 pid_t do_pid = -1;
78 const char *tracefile = NULL;
79 struct ktr_header ktr_header;
80 int emul_changed = 0;
81
82 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
83
84 static const char *ptrace_ops[] = {
85 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U",
86 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE",
87 "PT_KILL", "PT_ATTACH", "PT_DETACH",
88 };
89
90 static const char *linux_ptrace_ops[] = {
91 "PTRACE_TRACEME",
92 "PTRACE_PEEKTEXT", "PTRACE_PEEKDATA", "PTRACE_PEEKUSER",
93 "PTRACE_POKETEXT", "PTRACE_POKEDATA", "PTRACE_POKEUSER",
94 "PTRACE_CONT", "PTRACE_KILL", "PTRACE_SINGLESTEP",
95 NULL, NULL,
96 "PTRACE_GETREGS", "PTRACE_SETREGS", "PTRACE_GETFPREGS",
97 "PTRACE_SETFPREGS", "PTRACE_ATTACH", "PTRACE_DETACH",
98 "PTRACE_SYSCALL",
99 };
100
101 int main __P((int, char **));
102 int fread_tail __P((char *, int, int));
103 void dumpheader __P((struct ktr_header *));
104 void ioctldecode __P((u_long));
105 void ktrsyscall __P((struct ktr_syscall *));
106 void ktrsysret __P((struct ktr_sysret *, int));
107 void ktrnamei __P((char *, int));
108 void ktremul __P((char *, int, int));
109 void ktrgenio __P((struct ktr_genio *, int));
110 void ktrpsig __P((struct ktr_psig *));
111 void ktrcsw __P((struct ktr_csw *));
112 void ktruser __P((struct ktr_user *, int));
113 void ktrmmsg __P((struct ktr_mmsg *, int));
114 void usage __P((void));
115 void eprint __P((int));
116 void rprint __P((register_t));
117 char *ioctlname __P((long));
118 static const char *signame __P((long, int));
119
120 int
121 main(argc, argv)
122 int argc;
123 char *argv[];
124 {
125 int ch, ktrlen, size;
126 void *m;
127 int trpoints = ALL_POINTS;
128 const char *emul_name = "netbsd";
129
130 while ((ch = getopt(argc, argv, "e:f:dlm:Nnp:RTt:x")) != -1)
131 switch (ch) {
132 case 'e':
133 emul_name = strdup(optarg); /* it's safer to copy it */
134 break;
135 case 'f':
136 tracefile = optarg;
137 break;
138 case 'd':
139 decimal = 1;
140 break;
141 case 'l':
142 tail = 1;
143 break;
144 case 'p':
145 do_pid = atoi(optarg);
146 break;
147 case 'm':
148 maxdata = atoi(optarg);
149 break;
150 case 'N':
151 numeric++;
152 break;
153 case 'n':
154 plain++;
155 break;
156 case 'R':
157 timestamp = 2; /* relative timestamp */
158 break;
159 case 'T':
160 timestamp = 1;
161 break;
162 case 't':
163 trpoints = getpoints(optarg);
164 if (trpoints < 0)
165 errx(1, "unknown trace point in %s", optarg);
166 break;
167 case 'x':
168 hexdump = 1;
169 break;
170 default:
171 usage();
172 }
173 argv += optind;
174 argc -= optind;
175
176 if (tracefile == NULL) {
177 if (argc == 1) {
178 tracefile = argv[0];
179 argv++;
180 argc--;
181 }
182 else
183 tracefile = DEF_TRACEFILE;
184 }
185
186 if (argc > 0)
187 usage();
188
189 setemul(emul_name, 0, 0);
190 mach_lookup_emul();
191
192 m = malloc(size = 1024);
193 if (m == NULL)
194 errx(1, "malloc: %s", strerror(ENOMEM));
195 if (!freopen(tracefile, "r", stdin))
196 err(1, "%s", tracefile);
197 while (fread_tail((char *)&ktr_header, sizeof(struct ktr_header), 1)) {
198 if (trpoints & (1<<ktr_header.ktr_type))
199 if (do_pid == -1 || ktr_header.ktr_pid == do_pid)
200 dumpheader(&ktr_header);
201 if ((ktrlen = ktr_header.ktr_len) < 0)
202 errx(1, "bogus length 0x%x", ktrlen);
203 if (ktrlen > size) {
204 while(ktrlen > size) size *= 2;
205 m = (void *)realloc(m, size);
206 if (m == NULL)
207 errx(1, "realloc: %s", strerror(ENOMEM));
208 }
209 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
210 errx(1, "data too short");
211 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
212 continue;
213
214 /* update context to match currently processed record */
215 if (do_pid != -1 && ktr_header.ktr_pid != do_pid)
216 continue;
217 ectx_sanify(ktr_header.ktr_pid);
218
219 switch (ktr_header.ktr_type) {
220 case KTR_SYSCALL:
221 ktrsyscall((struct ktr_syscall *)m);
222 break;
223 case KTR_SYSRET:
224 ktrsysret((struct ktr_sysret *)m, ktrlen);
225 break;
226 case KTR_NAMEI:
227 ktrnamei(m, ktrlen);
228 break;
229 case KTR_GENIO:
230 ktrgenio((struct ktr_genio *)m, ktrlen);
231 break;
232 case KTR_PSIG:
233 ktrpsig((struct ktr_psig *)m);
234 break;
235 case KTR_CSW:
236 ktrcsw((struct ktr_csw *)m);
237 break;
238 case KTR_EMUL:
239 ktremul(m, ktrlen, size);
240 break;
241 case KTR_USER:
242 ktruser((struct ktr_user *)m, ktrlen);
243 break;
244 case KTR_MMSG:
245 ktrmmsg((struct ktr_mmsg *)m, ktrlen);
246 break;
247 }
248 if (tail)
249 (void)fflush(stdout);
250 }
251 return (0);
252 }
253
254 int
255 fread_tail(buf, size, num)
256 char *buf;
257 int num, size;
258 {
259 int i;
260
261 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
262 (void)sleep(1);
263 clearerr(stdin);
264 }
265 return (i);
266 }
267
268 void
269 dumpheader(kth)
270 struct ktr_header *kth;
271 {
272 char unknown[64], *type;
273 static struct timeval prevtime;
274 struct timeval temp;
275
276 switch (kth->ktr_type) {
277 case KTR_SYSCALL:
278 type = "CALL";
279 break;
280 case KTR_SYSRET:
281 type = "RET ";
282 break;
283 case KTR_NAMEI:
284 type = "NAMI";
285 break;
286 case KTR_GENIO:
287 type = "GIO ";
288 break;
289 case KTR_PSIG:
290 type = "PSIG";
291 break;
292 case KTR_CSW:
293 type = "CSW";
294 break;
295 case KTR_EMUL:
296 type = "EMUL";
297 break;
298 case KTR_USER:
299 type = "USER";
300 break;
301 case KTR_MMSG:
302 type = "MMSG";
303 break;
304 default:
305 (void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
306 type = unknown;
307 }
308
309 (void)printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
310 if (timestamp) {
311 if (timestamp == 2) {
312 timersub(&kth->ktr_time, &prevtime, &temp);
313 prevtime = kth->ktr_time;
314 } else
315 temp = kth->ktr_time;
316 (void)printf("%ld.%06ld ",
317 (long int)temp.tv_sec, (long int)temp.tv_usec);
318 }
319 (void)printf("%s ", type);
320 }
321
322 void
323 ioctldecode(cmd)
324 u_long cmd;
325 {
326 char dirbuf[4], *dir = dirbuf;
327
328 if (cmd & IOC_IN)
329 *dir++ = 'W';
330 if (cmd & IOC_OUT)
331 *dir++ = 'R';
332 *dir = '\0';
333
334 printf(decimal ? ",_IO%s('%c',%ld" : ",_IO%s('%c',%#lx",
335 dirbuf, (int) ((cmd >> 8) & 0xff), cmd & 0xff);
336 if ((cmd & IOC_VOID) == 0)
337 printf(decimal ? ",%ld)" : ",%#lx)", (cmd >> 16) & 0xff);
338 else
339 printf(")");
340 }
341
342 void
343 ktrsyscall(ktr)
344 struct ktr_syscall *ktr;
345 {
346 int argsize = ktr->ktr_argsize;
347 const struct emulation *revelant = current;
348 register_t *ap;
349
350 if (((ktr->ktr_code >= revelant->nsysnames || ktr->ktr_code < 0)
351 && (mach_traps_dispatch(&ktr->ktr_code, &revelant) == 0)) ||
352 numeric)
353 (void)printf("[%d]", ktr->ktr_code);
354 else
355 (void)printf("%s", revelant->sysnames[ktr->ktr_code]);
356 ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
357 if (argsize) {
358 char c = '(';
359 if (!plain) {
360 char *cp;
361
362 switch (ktr->ktr_code) {
363 case SYS_ioctl:
364 if (decimal)
365 (void)printf("(%ld", (long)*ap);
366 else
367 (void)printf("(%#lx", (long)*ap);
368 ap++;
369 argsize -= sizeof(register_t);
370 if ((cp = ioctlname(*ap)) != NULL)
371 (void)printf(",%s", cp);
372 else
373 ioctldecode(*ap);
374 c = ',';
375 ap++;
376 argsize -= sizeof(register_t);
377 break;
378
379 case SYS_ptrace:
380 if (strcmp(revelant->name, "linux") == 0) {
381 if (*ap >= 0 && *ap <=
382 sizeof(linux_ptrace_ops) /
383 sizeof(linux_ptrace_ops[0]))
384 (void)printf("(%s",
385 linux_ptrace_ops[*ap]);
386 else
387 (void)printf("(%ld", (long)*ap);
388 } else {
389 if (*ap >= 0 && *ap <=
390 sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
391 (void)printf("(%s", ptrace_ops[*ap]);
392 else
393 (void)printf("(%ld", (long)*ap);
394 }
395 c = ',';
396 ap++;
397 argsize -= sizeof(register_t);
398 break;
399
400 case SYS_kill:
401 if (decimal)
402 (void)printf("(%ld, SIG%s",
403 (long)ap[0], signame(ap[1], 1));
404 else
405 (void)printf("(%#lx, SIG%s",
406 (long)ap[0], signame(ap[1], 1));
407 ap += 2;
408 argsize -= 2 * sizeof(register_t);
409 break;
410
411 default:
412 /* No special handling */
413 break;
414 }
415 }
416 while (argsize) {
417 if (decimal)
418 (void)printf("%c%ld", c, (long)*ap);
419 else
420 (void)printf("%c%#lx", c, (long)*ap);
421 c = ',';
422 ap++;
423 argsize -= sizeof(register_t);
424 }
425 (void)putchar(')');
426 }
427 (void)putchar('\n');
428 }
429
430 void
431 ktrsysret(ktr, len)
432 struct ktr_sysret *ktr;
433 int len;
434 {
435 const struct emulation *revelant;
436 int error = ktr->ktr_error;
437 int code = ktr->ktr_code;
438
439 if (emul_changed)
440 revelant = previous;
441 else
442 revelant = current;
443 emul_changed = 0;
444
445 if ((code >= revelant->nsysnames || code < 0 || plain > 1)
446 && (mach_traps_dispatch(&code, &revelant) == 0))
447 (void)printf("[%d] ", code);
448 else
449 (void)printf("%s ", revelant->sysnames[code]);
450
451 switch (error) {
452 case 0:
453 rprint(ktr->ktr_retval);
454 if (len > offsetof(struct ktr_sysret, ktr_retval_1) &&
455 ktr->ktr_retval_1 != 0) {
456 (void)printf(", ");
457 rprint(ktr->ktr_retval_1);
458 }
459 break;
460
461 default:
462 eprint(error);
463 break;
464 }
465 (void)putchar('\n');
466 }
467
468 void
469 rprint(register_t ret)
470 {
471 if (!plain) {
472 (void)printf("%ld", (long)ret);
473 if (ret < 0 || ret > 9)
474 (void)printf("/%#lx", (long)ret);
475 } else {
476 if (decimal)
477 (void)printf("%ld", (long)ret);
478 else
479 (void)printf("%#lx", (long)ret);
480 }
481 }
482
483 /*
484 * We print the original emulation's error numerically, but we
485 * translate it to netbsd to print it symbolically.
486 */
487 void
488 eprint(e)
489 int e;
490 {
491 int i = e;
492
493 if (current->errnomap) {
494
495 /* No remapping for ERESTART and EJUSTRETURN */
496 /* Kludge for linux that has negative error numbers */
497 if (current->errnomap[2] > 0 && e < 0)
498 goto normal;
499
500 for (i = 0; i < current->nerrnomap; i++)
501 if (e == current->errnomap[i])
502 break;
503
504 if (i == current->nerrnomap) {
505 printf("-1 unknown errno %d", e);
506 return;
507 }
508 }
509
510 normal:
511 switch (i) {
512 case ERESTART:
513 (void)printf("RESTART");
514 break;
515
516 case EJUSTRETURN:
517 (void)printf("JUSTRETURN");
518 break;
519
520 default:
521 (void)printf("-1 errno %d", e);
522 if (!plain)
523 (void)printf(" %s", strerror(i));
524 }
525 }
526
527 void
528 ktrnamei(cp, len)
529 char *cp;
530 int len;
531 {
532
533 (void)printf("\"%.*s\"\n", len, cp);
534 }
535
536 void
537 ktremul(name, len, bufsize)
538 char *name;
539 int len, bufsize;
540 {
541 if (len >= bufsize)
542 len = bufsize - 1;
543
544 name[len] = '\0';
545 setemul(name, ktr_header.ktr_pid, 1);
546 emul_changed = 1;
547
548 (void)printf("\"%s\"\n", name);
549 }
550
551 static void
552 hexdump_buf(void *vdp, int datalen)
553 {
554 char chars[16];
555 unsigned char *dp = vdp;
556 int line_len, i, l, c;
557 char *cp;
558
559 for (; datalen > 0; datalen -= 16) {
560 line_len = 16;
561 printf("\t");
562 if (line_len > datalen)
563 line_len = datalen;
564 cp = chars;
565 l = 0;
566 for (i = 0; i < line_len; i++) {
567 c = *dp++;
568 if (i == 8)
569 l += printf(" ");
570 l += printf("%2.2x ", c);
571 *cp++ = isgraph(c) ? c : '.';
572 } while (--i);
573 printf("%*s %.*s\n", 50 - l, "", cp - chars, chars);
574 }
575 }
576
577 void
578 ktrgenio(ktr, len)
579 struct ktr_genio *ktr;
580 int len;
581 {
582 int datalen = len - sizeof (struct ktr_genio);
583 char *dp = (char *)ktr + sizeof (struct ktr_genio);
584 char *cp;
585 int col = 0;
586 int width;
587 char visbuf[5];
588 static int screenwidth = 0;
589
590 if (screenwidth == 0) {
591 struct winsize ws;
592
593 if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
594 ws.ws_col > 8)
595 screenwidth = ws.ws_col;
596 else
597 screenwidth = 80;
598 }
599 printf("fd %d %s %d bytes\n", ktr->ktr_fd,
600 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
601 if (maxdata == 0)
602 return;
603 if (maxdata > 0 && datalen > maxdata)
604 datalen = maxdata;
605 if (hexdump) {
606 hexdump_buf(dp, datalen);
607 return;
608 }
609 (void)printf(" \"");
610 col = 8;
611 for (; datalen > 0; datalen--, dp++) {
612 (void) vis(visbuf, *dp, VIS_CSTYLE, datalen>1?*(dp+1):0);
613 cp = visbuf;
614 /*
615 * Keep track of printables and
616 * space chars (like fold(1)).
617 */
618 if (col == 0) {
619 (void)putchar('\t');
620 col = 8;
621 }
622 switch(*cp) {
623 case '\n':
624 col = 0;
625 (void)putchar('\n');
626 continue;
627 case '\t':
628 width = 8 - (col&07);
629 break;
630 default:
631 width = strlen(cp);
632 }
633 if (col + width > (screenwidth-2)) {
634 (void)printf("\\\n\t");
635 col = 8;
636 }
637 col += width;
638 do {
639 (void)putchar(*cp++);
640 } while (*cp);
641 }
642 if (col == 0)
643 (void)printf(" ");
644 (void)printf("\"\n");
645 }
646
647 void
648 ktrpsig(psig)
649 struct ktr_psig *psig;
650 {
651 int signo, first;
652
653 (void)printf("SIG%s ", signame(psig->signo, 0));
654 if (psig->action == SIG_DFL)
655 (void)printf("SIG_DFL\n");
656 else {
657 (void)printf("caught handler=0x%lx mask=(",
658 (u_long)psig->action);
659 first = 1;
660 for (signo = 1; signo < NSIG; signo++) {
661 if (sigismember(&psig->mask, signo)) {
662 if (first)
663 first = 0;
664 else
665 (void)printf(",");
666 (void)printf("%d", signo);
667 }
668 }
669 (void)printf(") code=0x%x\n", psig->code);
670 }
671 }
672
673 void
674 ktrcsw(cs)
675 struct ktr_csw *cs;
676 {
677
678 (void)printf("%s %s\n", cs->out ? "stop" : "resume",
679 cs->user ? "user" : "kernel");
680 }
681
682 void
683 ktruser(usr, len)
684 struct ktr_user *usr;
685 int len;
686 {
687 int i;
688 unsigned char *dta;
689
690 printf("\"%.*s: %d, ", KTR_USER_MAXIDLEN, usr->ktr_id, len);
691 dta = (unsigned char *)usr;
692 for(i=sizeof(struct ktr_user); i < len; i++)
693 printf("%02x", (unsigned int) dta[i]);
694 printf("\"\n");
695 }
696
697 void
698 ktrmmsg(mmsg, len)
699 struct ktr_mmsg *mmsg;
700 int len;
701 {
702 int i,j;
703 unsigned char *data;
704 int row_len = 16;
705 int aligned_len;
706
707 printf("id %d [0x%x -> 0x%x] %d bytes, flags 0x%x",
708 mmsg->ktr_id, mmsg->ktr_local_port,
709 mmsg->ktr_remote_port, mmsg->ktr_size, mmsg->ktr_bits);
710
711 data = (unsigned char *)mmsg;
712 aligned_len = (len & ~(row_len - 1)) + row_len;
713 for (i = 0; i < aligned_len; i += row_len) {
714 printf("\n\t0x%04x ", i);
715
716 for (j = 0; j < row_len; j += sizeof(int))
717 if ((i + j) < len)
718 printf("0x%08x ", *((int *)&data[i + j]));
719 else
720 printf(" ");
721
722 printf(" ");
723
724 for (j = 0; j < row_len; j++) {
725 if ((i + j) < len) {
726 if (isprint(data[i + j]))
727 printf("%c", data[i + j]);
728 else
729 printf(".");
730 } else {
731 printf(" ");
732 }
733 }
734 }
735
736 if (aligned_len != sizeof(struct ktr_mmsg))
737 printf("\n");
738 }
739
740 static const char *
741 signame(long sig, int xlat)
742 {
743 static char buf[64];
744 if (sig == 0)
745 return " 0";
746 else if (sig < 0 || sig >= NSIG) {
747 (void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig);
748 return buf;
749 } else
750 return sys_signame[(xlat && current->signalmap != NULL) ?
751 current->signalmap[sig] : sig];
752 }
753
754 void
755 usage()
756 {
757
758 (void)fprintf(stderr, "usage: kdump [-dlNnRTx] [-e emulation] "
759 "[-f file] [-m maxdata] [-p pid]\n [-t trstr] "
760 "[file]\n");
761 exit(1);
762 }
763