kdump.c revision 1.58 1 /* $NetBSD: kdump.c,v 1.58 2003/07/17 09:05:13 dsl 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.58 2003/07/17 09:05:13 dsl 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 int 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 static void hexdump_buf(const void *, int);
120 static void visdump_buf(const void *, int, int);
121
122 int
123 main(argc, argv)
124 int argc;
125 char *argv[];
126 {
127 int ch, ktrlen, size;
128 void *m;
129 int trpoints = 0;
130 int trset = 0;
131 const char *emul_name = "netbsd";
132 int col;
133
134 while ((ch = getopt(argc, argv, "e:f:dlm:Nnp:RTt:x")) != -1)
135 switch (ch) {
136 case 'e':
137 emul_name = strdup(optarg); /* it's safer to copy it */
138 break;
139 case 'f':
140 tracefile = optarg;
141 break;
142 case 'd':
143 decimal = 1;
144 break;
145 case 'l':
146 tail = 1;
147 break;
148 case 'p':
149 do_pid = atoi(optarg);
150 break;
151 case 'm':
152 maxdata = atoi(optarg);
153 break;
154 case 'N':
155 numeric++;
156 break;
157 case 'n':
158 plain++;
159 break;
160 case 'R':
161 timestamp = 2; /* relative timestamp */
162 break;
163 case 'T':
164 timestamp = 1;
165 break;
166 case 't':
167 trset = 1;
168 trpoints = getpoints(trpoints, optarg);
169 if (trpoints < 0)
170 errx(1, "unknown trace point in %s", optarg);
171 break;
172 case 'x':
173 hexdump = 1;
174 break;
175 default:
176 usage();
177 }
178 argv += optind;
179 argc -= optind;
180
181 if (!trset)
182 trpoints = ALL_POINTS;
183
184 if (tracefile == NULL) {
185 if (argc == 1) {
186 tracefile = argv[0];
187 argv++;
188 argc--;
189 }
190 else
191 tracefile = DEF_TRACEFILE;
192 }
193
194 if (argc > 0)
195 usage();
196
197 setemul(emul_name, 0, 0);
198 mach_lookup_emul();
199
200 m = malloc(size = 1024);
201 if (m == NULL)
202 errx(1, "malloc: %s", strerror(ENOMEM));
203 if (!freopen(tracefile, "r", stdin))
204 err(1, "%s", tracefile);
205 while (fread_tail((char *)&ktr_header, sizeof(struct ktr_header), 1)) {
206 if (trpoints & (1<<ktr_header.ktr_type)
207 && (do_pid == -1 || ktr_header.ktr_pid == do_pid))
208 col = dumpheader(&ktr_header);
209 else
210 col = -1;
211 if ((ktrlen = ktr_header.ktr_len) < 0)
212 errx(1, "bogus length 0x%x", ktrlen);
213 if (ktrlen > size) {
214 while (ktrlen > size)
215 size *= 2;
216 m = realloc(m, size);
217 if (m == NULL)
218 errx(1, "realloc: %s", strerror(ENOMEM));
219 }
220 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
221 errx(1, "data too short");
222 if (col == -1)
223 continue;
224
225 /* update context to match currently processed record */
226 ectx_sanify(ktr_header.ktr_pid);
227
228 switch (ktr_header.ktr_type) {
229 case KTR_SYSCALL:
230 ktrsyscall(m);
231 break;
232 case KTR_SYSRET:
233 ktrsysret(m, ktrlen);
234 break;
235 case KTR_NAMEI:
236 ktrnamei(m, ktrlen);
237 break;
238 case KTR_GENIO:
239 ktrgenio(m, ktrlen);
240 break;
241 case KTR_PSIG:
242 ktrpsig(m);
243 break;
244 case KTR_CSW:
245 ktrcsw(m);
246 break;
247 case KTR_EMUL:
248 ktremul(m, ktrlen, size);
249 break;
250 case KTR_USER:
251 ktruser(m, ktrlen);
252 break;
253 case KTR_MMSG:
254 ktrmmsg(m, ktrlen);
255 break;
256 case KTR_EXEC_ARG:
257 case KTR_EXEC_ENV:
258 visdump_buf(m, ktrlen, col);
259 break;
260 default:
261 printf("\n");
262 hexdump_buf(m, ktrlen);
263 }
264 if (tail)
265 (void)fflush(stdout);
266 }
267 return (0);
268 }
269
270 int
271 fread_tail(buf, size, num)
272 char *buf;
273 int num, size;
274 {
275 int i;
276
277 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
278 (void)sleep(1);
279 clearerr(stdin);
280 }
281 return (i);
282 }
283
284 int
285 dumpheader(kth)
286 struct ktr_header *kth;
287 {
288 char unknown[64], *type;
289 static struct timeval prevtime;
290 struct timeval temp;
291 int col;
292
293 switch (kth->ktr_type) {
294 case KTR_SYSCALL:
295 type = "CALL";
296 break;
297 case KTR_SYSRET:
298 type = "RET ";
299 break;
300 case KTR_NAMEI:
301 type = "NAMI";
302 break;
303 case KTR_GENIO:
304 type = "GIO ";
305 break;
306 case KTR_PSIG:
307 type = "PSIG";
308 break;
309 case KTR_CSW:
310 type = "CSW";
311 break;
312 case KTR_EMUL:
313 type = "EMUL";
314 break;
315 case KTR_USER:
316 type = "USER";
317 break;
318 case KTR_MMSG:
319 type = "MMSG";
320 break;
321 case KTR_EXEC_ENV:
322 type = "ENV";
323 break;
324 case KTR_EXEC_ARG:
325 type = "ARG";
326 break;
327 default:
328 (void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
329 type = unknown;
330 }
331
332 col = printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
333 if (timestamp) {
334 if (timestamp == 2) {
335 if (prevtime.tv_sec == 0)
336 temp.tv_sec = temp.tv_usec = 0;
337 else
338 timersub(&kth->ktr_time, &prevtime, &temp);
339 prevtime = kth->ktr_time;
340 } else
341 temp = kth->ktr_time;
342 col += printf("%ld.%06ld ",
343 (long int)temp.tv_sec, (long int)temp.tv_usec);
344 }
345 col += printf("%-4s ", type);
346 return col;
347 }
348
349 void
350 ioctldecode(cmd)
351 u_long cmd;
352 {
353 char dirbuf[4], *dir = dirbuf;
354
355 if (cmd & IOC_IN)
356 *dir++ = 'W';
357 if (cmd & IOC_OUT)
358 *dir++ = 'R';
359 *dir = '\0';
360
361 printf(decimal ? ",_IO%s('%c',%ld" : ",_IO%s('%c',%#lx",
362 dirbuf, (int) ((cmd >> 8) & 0xff), cmd & 0xff);
363 if ((cmd & IOC_VOID) == 0)
364 printf(decimal ? ",%ld)" : ",%#lx)", (cmd >> 16) & 0xff);
365 else
366 printf(")");
367 }
368
369 void
370 ktrsyscall(ktr)
371 struct ktr_syscall *ktr;
372 {
373 int argsize = ktr->ktr_argsize;
374 const struct emulation *revelant = current;
375 register_t *ap;
376
377 if (((ktr->ktr_code >= revelant->nsysnames || ktr->ktr_code < 0)
378 && (mach_traps_dispatch(&ktr->ktr_code, &revelant) == 0)) ||
379 numeric)
380 (void)printf("[%d]", ktr->ktr_code);
381 else
382 (void)printf("%s", revelant->sysnames[ktr->ktr_code]);
383 ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
384 if (argsize) {
385 char c = '(';
386 if (!plain) {
387 char *cp;
388
389 switch (ktr->ktr_code) {
390 case SYS_ioctl:
391 if (decimal)
392 (void)printf("(%ld", (long)*ap);
393 else
394 (void)printf("(%#lx", (long)*ap);
395 ap++;
396 argsize -= sizeof(register_t);
397 if ((cp = ioctlname(*ap)) != NULL)
398 (void)printf(",%s", cp);
399 else
400 ioctldecode(*ap);
401 c = ',';
402 ap++;
403 argsize -= sizeof(register_t);
404 break;
405
406 case SYS_ptrace:
407 if (strcmp(revelant->name, "linux") == 0) {
408 if (*ap >= 0 && *ap <=
409 sizeof(linux_ptrace_ops) /
410 sizeof(linux_ptrace_ops[0]))
411 (void)printf("(%s",
412 linux_ptrace_ops[*ap]);
413 else
414 (void)printf("(%ld", (long)*ap);
415 } else {
416 if (*ap >= 0 && *ap <=
417 sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
418 (void)printf("(%s", ptrace_ops[*ap]);
419 else
420 (void)printf("(%ld", (long)*ap);
421 }
422 c = ',';
423 ap++;
424 argsize -= sizeof(register_t);
425 break;
426
427 case SYS_kill:
428 if (decimal)
429 (void)printf("(%ld, SIG%s",
430 (long)ap[0], signame(ap[1], 1));
431 else
432 (void)printf("(%#lx, SIG%s",
433 (long)ap[0], signame(ap[1], 1));
434 ap += 2;
435 argsize -= 2 * sizeof(register_t);
436 break;
437
438 default:
439 /* No special handling */
440 break;
441 }
442 }
443 while (argsize) {
444 if (decimal)
445 (void)printf("%c%ld", c, (long)*ap);
446 else
447 (void)printf("%c%#lx", c, (long)*ap);
448 c = ',';
449 ap++;
450 argsize -= sizeof(register_t);
451 }
452 (void)putchar(')');
453 }
454 (void)putchar('\n');
455 }
456
457 void
458 ktrsysret(ktr, len)
459 struct ktr_sysret *ktr;
460 int len;
461 {
462 const struct emulation *revelant;
463 int error = ktr->ktr_error;
464 int code = ktr->ktr_code;
465
466 if (emul_changed)
467 revelant = previous;
468 else
469 revelant = current;
470 emul_changed = 0;
471
472 if ((code >= revelant->nsysnames || code < 0 || plain > 1)
473 && (mach_traps_dispatch(&code, &revelant) == 0))
474 (void)printf("[%d] ", code);
475 else
476 (void)printf("%s ", revelant->sysnames[code]);
477
478 switch (error) {
479 case 0:
480 rprint(ktr->ktr_retval);
481 if (len > offsetof(struct ktr_sysret, ktr_retval_1) &&
482 ktr->ktr_retval_1 != 0) {
483 (void)printf(", ");
484 rprint(ktr->ktr_retval_1);
485 }
486 break;
487
488 default:
489 eprint(error);
490 break;
491 }
492 (void)putchar('\n');
493 }
494
495 void
496 rprint(register_t ret)
497 {
498 if (!plain) {
499 (void)printf("%ld", (long)ret);
500 if (ret < 0 || ret > 9)
501 (void)printf("/%#lx", (long)ret);
502 } else {
503 if (decimal)
504 (void)printf("%ld", (long)ret);
505 else
506 (void)printf("%#lx", (long)ret);
507 }
508 }
509
510 /*
511 * We print the original emulation's error numerically, but we
512 * translate it to netbsd to print it symbolically.
513 */
514 void
515 eprint(e)
516 int e;
517 {
518 int i = e;
519
520 if (current->errnomap) {
521
522 /* No remapping for ERESTART and EJUSTRETURN */
523 /* Kludge for linux that has negative error numbers */
524 if (current->errnomap[2] > 0 && e < 0)
525 goto normal;
526
527 for (i = 0; i < current->nerrnomap; i++)
528 if (e == current->errnomap[i])
529 break;
530
531 if (i == current->nerrnomap) {
532 printf("-1 unknown errno %d", e);
533 return;
534 }
535 }
536
537 normal:
538 switch (i) {
539 case ERESTART:
540 (void)printf("RESTART");
541 break;
542
543 case EJUSTRETURN:
544 (void)printf("JUSTRETURN");
545 break;
546
547 default:
548 (void)printf("-1 errno %d", e);
549 if (!plain)
550 (void)printf(" %s", strerror(i));
551 }
552 }
553
554 void
555 ktrnamei(cp, len)
556 char *cp;
557 int len;
558 {
559
560 (void)printf("\"%.*s\"\n", len, cp);
561 }
562
563 void
564 ktremul(name, len, bufsize)
565 char *name;
566 int len, bufsize;
567 {
568 if (len >= bufsize)
569 len = bufsize - 1;
570
571 name[len] = '\0';
572 setemul(name, ktr_header.ktr_pid, 1);
573 emul_changed = 1;
574
575 (void)printf("\"%s\"\n", name);
576 }
577
578 static void
579 hexdump_buf(const void *vdp, int datalen)
580 {
581 char chars[16];
582 const unsigned char *dp = vdp;
583 int line_end, off, l, c;
584 char *cp;
585
586 for (off = 0; off < datalen;) {
587 line_end = off + 16;
588 if (line_end > datalen)
589 line_end = datalen;
590 printf("\t%3.3x ", off);
591 for (l = 0, cp = chars; off < line_end; off++) {
592 c = *dp++;
593 if ((off & 7) == 0)
594 l += printf(" ");
595 l += printf(" %2.2x", c);
596 *cp++ = isgraph(c) ? c : '.';
597 };
598 printf("%*s %.*s\n", 50 - l, "", (int)(cp - chars), chars);
599 }
600 }
601
602 static void
603 visdump_buf(const void *vdp, int datalen, int col)
604 {
605 const unsigned char *dp = vdp;
606 char *cp;
607 int width;
608 char visbuf[5];
609 static int screenwidth = 0;
610
611 if (screenwidth == 0) {
612 struct winsize ws;
613
614 if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
615 ws.ws_col > 8)
616 screenwidth = ws.ws_col;
617 else
618 screenwidth = 80;
619 }
620
621 (void)printf("\"");
622 col++;
623 for (; datalen > 0; datalen--, dp++) {
624 (void)svis(visbuf, *dp, VIS_CSTYLE,
625 datalen > 1 ? *(dp + 1) : 0, "\"");
626 cp = visbuf;
627 /*
628 * Keep track of printables and
629 * space chars (like fold(1)).
630 */
631 if (col == 0) {
632 (void)putchar('\t');
633 col = 8;
634 }
635 switch(*cp) {
636 case '\n':
637 col = 0;
638 (void)putchar('\n');
639 continue;
640 case '\t':
641 width = 8 - (col & 07);
642 break;
643 default:
644 width = strlen(cp);
645 }
646 if (col + width > (screenwidth - 2)) {
647 (void)printf("\\\n\t");
648 col = 8;
649 }
650 col += width;
651 do {
652 (void)putchar(*cp++);
653 } while (*cp);
654 }
655 if (col == 0)
656 (void)printf(" ");
657 (void)printf("\"\n");
658 }
659
660 void
661 ktrgenio(ktr, len)
662 struct ktr_genio *ktr;
663 int len;
664 {
665 int datalen = len - sizeof (struct ktr_genio);
666 char *dp = (char *)ktr + sizeof (struct ktr_genio);
667
668 printf("fd %d %s %d bytes\n", ktr->ktr_fd,
669 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
670 if (maxdata == 0)
671 return;
672 if (maxdata > 0 && datalen > maxdata)
673 datalen = maxdata;
674 if (hexdump) {
675 hexdump_buf(dp, datalen);
676 return;
677 }
678 (void)printf(" ");
679 visdump_buf(dp, datalen, 7);
680 }
681
682 void
683 ktrpsig(psig)
684 struct ktr_psig *psig;
685 {
686 int signo, first;
687
688 (void)printf("SIG%s ", signame(psig->signo, 0));
689 if (psig->action == SIG_DFL)
690 (void)printf("SIG_DFL\n");
691 else {
692 (void)printf("caught handler=0x%lx mask=(",
693 (u_long)psig->action);
694 first = 1;
695 for (signo = 1; signo < NSIG; signo++) {
696 if (sigismember(&psig->mask, signo)) {
697 if (first)
698 first = 0;
699 else
700 (void)printf(",");
701 (void)printf("%d", signo);
702 }
703 }
704 (void)printf(") code=0x%x\n", psig->code);
705 }
706 }
707
708 void
709 ktrcsw(cs)
710 struct ktr_csw *cs;
711 {
712
713 (void)printf("%s %s\n", cs->out ? "stop" : "resume",
714 cs->user ? "user" : "kernel");
715 }
716
717 void
718 ktruser(usr, len)
719 struct ktr_user *usr;
720 int len;
721 {
722 int i;
723 unsigned char *dta;
724
725 printf("\"%.*s: %d, ", KTR_USER_MAXIDLEN, usr->ktr_id, len);
726 dta = (unsigned char *)usr;
727 for(i=sizeof(struct ktr_user); i < len; i++)
728 printf("%02x", (unsigned int) dta[i]);
729 printf("\"\n");
730 }
731
732 void
733 ktrmmsg(mmsg, len)
734 struct ktr_mmsg *mmsg;
735 int len;
736 {
737 printf("id %d [0x%x -> 0x%x] flags 0x%x\n",
738 mmsg->ktr_id, mmsg->ktr_local_port,
739 mmsg->ktr_remote_port, mmsg->ktr_bits);
740
741 hexdump_buf(mmsg, len);
742 }
743
744 static const char *
745 signame(long sig, int xlat)
746 {
747 static char buf[64];
748 if (sig == 0)
749 return " 0";
750 else if (sig < 0 || sig >= NSIG) {
751 (void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig);
752 return buf;
753 } else
754 return sys_signame[(xlat && current->signalmap != NULL) ?
755 current->signalmap[sig] : sig];
756 }
757
758 void
759 usage()
760 {
761
762 (void)fprintf(stderr, "usage: kdump [-dlNnRTx] [-e emulation] "
763 "[-f file] [-m maxdata] [-p pid]\n [-t trstr] "
764 "[file]\n");
765 exit(1);
766 }
767