kdump.c revision 1.43 1 /* $NetBSD: kdump.c,v 1.43 2002/11/27 21:26:57 atatat 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.43 2002/11/27 21:26:57 atatat 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 <err.h>
61 #include <signal.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 #include <vis.h>
67
68 #include "ktrace.h"
69 #include "setemul.h"
70
71 #include <sys/syscall.h>
72
73 int timestamp, decimal, plain, tail, maxdata, numeric;
74 pid_t do_pid = -1;
75 const char *tracefile = NULL;
76 struct ktr_header ktr_header;
77 int emul_changed = 0;
78
79 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
80
81 static const char *ptrace_ops[] = {
82 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U",
83 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE",
84 "PT_KILL", "PT_ATTACH", "PT_DETACH",
85 };
86
87 static const char *linux_ptrace_ops[] = {
88 "PTRACE_TRACEME",
89 "PTRACE_PEEKTEXT", "PTRACE_PEEKDATA", "PTRACE_PEEKUSER",
90 "PTRACE_POKETEXT", "PTRACE_POKEDATA", "PTRACE_POKEUSER",
91 "PTRACE_CONT", "PTRACE_KILL", "PTRACE_SINGLESTEP",
92 NULL, NULL,
93 "PTRACE_GETREGS", "PTRACE_SETREGS", "PTRACE_GETFPREGS",
94 "PTRACE_SETFPREGS", "PTRACE_ATTACH", "PTRACE_DETACH",
95 "PTRACE_SYSCALL",
96 };
97
98 int main __P((int, char **));
99 int fread_tail __P((char *, int, int));
100 void dumpheader __P((struct ktr_header *));
101 void ioctldecode __P((u_long));
102 void ktrsyscall __P((struct ktr_syscall *));
103 void ktrsysret __P((struct ktr_sysret *));
104 void ktrnamei __P((char *, int));
105 void ktremul __P((char *, int, int));
106 void ktrgenio __P((struct ktr_genio *, int));
107 void ktrpsig __P((struct ktr_psig *));
108 void ktrcsw __P((struct ktr_csw *));
109 void ktruser __P((struct ktr_user *, int));
110 void usage __P((void));
111 void eprint __P((int));
112 char *ioctlname __P((long));
113 static const char *signame __P((long, int));
114
115 int
116 main(argc, argv)
117 int argc;
118 char *argv[];
119 {
120 int ch, ktrlen, size;
121 void *m;
122 int trpoints = ALL_POINTS;
123 const char *emul_name = "netbsd";
124
125 while ((ch = getopt(argc, argv, "e:f:dlm:Nnp:RTt:")) != -1)
126 switch (ch) {
127 case 'e':
128 emul_name = strdup(optarg); /* it's safer to copy it */
129 break;
130 case 'f':
131 tracefile = optarg;
132 break;
133 case 'd':
134 decimal = 1;
135 break;
136 case 'l':
137 tail = 1;
138 break;
139 case 'p':
140 do_pid = atoi(optarg);
141 break;
142 case 'm':
143 maxdata = atoi(optarg);
144 break;
145 case 'N':
146 numeric++;
147 break;
148 case 'n':
149 plain++;
150 break;
151 case 'R':
152 timestamp = 2; /* relative timestamp */
153 break;
154 case 'T':
155 timestamp = 1;
156 break;
157 case 't':
158 trpoints = getpoints(optarg);
159 if (trpoints < 0)
160 errx(1, "unknown trace point in %s", optarg);
161 break;
162 default:
163 usage();
164 }
165 argv += optind;
166 argc -= optind;
167
168 if (tracefile == NULL) {
169 if (argc == 1) {
170 tracefile = argv[0];
171 argv++;
172 argc--;
173 }
174 else
175 tracefile = DEF_TRACEFILE;
176 }
177
178 if (argc > 0)
179 usage();
180
181 setemul(emul_name, 0, 0);
182 mach_lookup_emul();
183
184 m = malloc(size = 1024);
185 if (m == NULL)
186 errx(1, "malloc: %s", strerror(ENOMEM));
187 if (!freopen(tracefile, "r", stdin))
188 err(1, "%s", tracefile);
189 while (fread_tail((char *)&ktr_header, sizeof(struct ktr_header), 1)) {
190 if (trpoints & (1<<ktr_header.ktr_type))
191 if (do_pid == -1 || ktr_header.ktr_pid == do_pid)
192 dumpheader(&ktr_header);
193 if ((ktrlen = ktr_header.ktr_len) < 0)
194 errx(1, "bogus length 0x%x", ktrlen);
195 if (ktrlen > size) {
196 while(ktrlen > size) size *= 2;
197 m = (void *)realloc(m, size);
198 if (m == NULL)
199 errx(1, "realloc: %s", strerror(ENOMEM));
200 }
201 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
202 errx(1, "data too short");
203 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
204 continue;
205
206 /* update context to match currently processed record */
207 if (do_pid != -1 && ktr_header.ktr_pid != do_pid)
208 continue;
209 ectx_sanify(ktr_header.ktr_pid);
210
211 switch (ktr_header.ktr_type) {
212 case KTR_SYSCALL:
213 ktrsyscall((struct ktr_syscall *)m);
214 break;
215 case KTR_SYSRET:
216 ktrsysret((struct ktr_sysret *)m);
217 break;
218 case KTR_NAMEI:
219 ktrnamei(m, ktrlen);
220 break;
221 case KTR_GENIO:
222 ktrgenio((struct ktr_genio *)m, ktrlen);
223 break;
224 case KTR_PSIG:
225 ktrpsig((struct ktr_psig *)m);
226 break;
227 case KTR_CSW:
228 ktrcsw((struct ktr_csw *)m);
229 break;
230 case KTR_EMUL:
231 ktremul(m, ktrlen, size);
232 break;
233 case KTR_USER:
234 ktruser((struct ktr_user *)m, ktrlen);
235 break;
236 }
237 if (tail)
238 (void)fflush(stdout);
239 }
240 return (0);
241 }
242
243 int
244 fread_tail(buf, size, num)
245 char *buf;
246 int num, size;
247 {
248 int i;
249
250 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
251 (void)sleep(1);
252 clearerr(stdin);
253 }
254 return (i);
255 }
256
257 void
258 dumpheader(kth)
259 struct ktr_header *kth;
260 {
261 char unknown[64], *type;
262 static struct timeval prevtime;
263 struct timeval temp;
264
265 switch (kth->ktr_type) {
266 case KTR_SYSCALL:
267 type = "CALL";
268 break;
269 case KTR_SYSRET:
270 type = "RET ";
271 break;
272 case KTR_NAMEI:
273 type = "NAMI";
274 break;
275 case KTR_GENIO:
276 type = "GIO ";
277 break;
278 case KTR_PSIG:
279 type = "PSIG";
280 break;
281 case KTR_CSW:
282 type = "CSW";
283 break;
284 case KTR_EMUL:
285 type = "EMUL";
286 break;
287 case KTR_USER:
288 type = "USER";
289 break;
290 default:
291 (void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
292 type = unknown;
293 }
294
295 (void)printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
296 if (timestamp) {
297 if (timestamp == 2) {
298 timersub(&kth->ktr_time, &prevtime, &temp);
299 prevtime = kth->ktr_time;
300 } else
301 temp = kth->ktr_time;
302 (void)printf("%ld.%06ld ",
303 (long int)temp.tv_sec, (long int)temp.tv_usec);
304 }
305 (void)printf("%s ", type);
306 }
307
308 void
309 ioctldecode(cmd)
310 u_long cmd;
311 {
312 char dirbuf[4], *dir = dirbuf;
313
314 if (cmd & IOC_IN)
315 *dir++ = 'W';
316 if (cmd & IOC_OUT)
317 *dir++ = 'R';
318 *dir = '\0';
319
320 printf(decimal ? ",_IO%s('%c',%ld" : ",_IO%s('%c',%#lx",
321 dirbuf, (int) ((cmd >> 8) & 0xff), cmd & 0xff);
322 if ((cmd & IOC_VOID) == 0)
323 printf(decimal ? ",%ld)" : ",%#lx)", (cmd >> 16) & 0xff);
324 else
325 printf(")");
326 }
327
328 void
329 ktrsyscall(ktr)
330 struct ktr_syscall *ktr;
331 {
332 int argsize = ktr->ktr_argsize;
333 const struct emulation *revelant = current;
334 register_t *ap;
335
336 if (((ktr->ktr_code >= revelant->nsysnames || ktr->ktr_code < 0)
337 && (mach_traps_dispatch(&ktr->ktr_code, &revelant) == 0)) ||
338 numeric)
339 (void)printf("[%d]", ktr->ktr_code);
340 else
341 (void)printf("%s", revelant->sysnames[ktr->ktr_code]);
342 ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
343 if (argsize) {
344 char c = '(';
345 if (!plain) {
346 char *cp;
347
348 switch (ktr->ktr_code) {
349 case SYS_ioctl:
350 if (decimal)
351 (void)printf("(%ld", (long)*ap);
352 else
353 (void)printf("(%#lx", (long)*ap);
354 ap++;
355 argsize -= sizeof(register_t);
356 if ((cp = ioctlname(*ap)) != NULL)
357 (void)printf(",%s", cp);
358 else
359 ioctldecode(*ap);
360 c = ',';
361 ap++;
362 argsize -= sizeof(register_t);
363 break;
364
365 case SYS_ptrace:
366 if (strcmp(revelant->name, "linux") == 0) {
367 if (*ap >= 0 && *ap <=
368 sizeof(linux_ptrace_ops) /
369 sizeof(linux_ptrace_ops[0]))
370 (void)printf("(%s",
371 linux_ptrace_ops[*ap]);
372 else
373 (void)printf("(%ld", (long)*ap);
374 } else {
375 if (*ap >= 0 && *ap <=
376 sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
377 (void)printf("(%s", ptrace_ops[*ap]);
378 else
379 (void)printf("(%ld", (long)*ap);
380 }
381 c = ',';
382 ap++;
383 argsize -= sizeof(register_t);
384 break;
385
386 case SYS_kill:
387 if (decimal)
388 (void)printf("(%ld, SIG%s",
389 (long)ap[0], signame(ap[1], 1));
390 else
391 (void)printf("(%#lx, SIG%s",
392 (long)ap[0], signame(ap[1], 1));
393 ap += 2;
394 argsize -= 2 * sizeof(register_t);
395 break;
396
397 default:
398 /* No special handling */
399 break;
400 }
401 }
402 while (argsize) {
403 if (decimal)
404 (void)printf("%c%ld", c, (long)*ap);
405 else
406 (void)printf("%c%#lx", c, (long)*ap);
407 c = ',';
408 ap++;
409 argsize -= sizeof(register_t);
410 }
411 (void)putchar(')');
412 }
413 (void)putchar('\n');
414 }
415
416 void
417 ktrsysret(ktr)
418 struct ktr_sysret *ktr;
419 {
420 const struct emulation *revelant;
421 register_t ret = ktr->ktr_retval;
422 int error = ktr->ktr_error;
423 int code = ktr->ktr_code;
424
425 if (emul_changed)
426 revelant = previous;
427 else
428 revelant = current;
429 emul_changed = 0;
430
431 if ((code >= revelant->nsysnames || code < 0 || plain > 1)
432 && (mach_traps_dispatch(&code, &revelant) == 0))
433 (void)printf("[%d] ", code);
434 else
435 (void)printf("%s ", revelant->sysnames[code]);
436
437 switch (error) {
438 case 0:
439 if (!plain) {
440 (void)printf("%ld", (long)ret);
441 if (ret < 0 || ret > 9)
442 (void)printf("/%#lx", (long)ret);
443 } else {
444 if (decimal)
445 (void)printf("%ld", (long)ret);
446 else
447 (void)printf("%#lx", (long)ret);
448 }
449 break;
450
451 default:
452 eprint(error);
453 break;
454 }
455 (void)putchar('\n');
456 }
457
458 /*
459 * We print the original emulation's error numerically, but we
460 * translate it to netbsd to print it symbolically.
461 */
462 void
463 eprint(e)
464 int e;
465 {
466 int i = e;
467
468 if (current->errnomap) {
469
470 /* No remapping for ERESTART and EJUSTRETURN */
471 /* Kludge for linux that has negative error numbers */
472 if (current->errnomap[2] > 0 && e < 0)
473 goto normal;
474
475 for (i = 0; i < current->nerrnomap; i++)
476 if (e == current->errnomap[i])
477 break;
478
479 if (i == current->nerrnomap) {
480 printf("-1 unknown errno %d", e);
481 return;
482 }
483 }
484
485 normal:
486 switch (i) {
487 case ERESTART:
488 (void)printf("RESTART");
489 break;
490
491 case EJUSTRETURN:
492 (void)printf("JUSTRETURN");
493 break;
494
495 default:
496 (void)printf("-1 errno %d", e);
497 if (!plain)
498 (void)printf(" %s", strerror(i));
499 }
500 }
501
502 void
503 ktrnamei(cp, len)
504 char *cp;
505 int len;
506 {
507
508 (void)printf("\"%.*s\"\n", len, cp);
509 }
510
511 void
512 ktremul(name, len, bufsize)
513 char *name;
514 int len, bufsize;
515 {
516 if (len >= bufsize)
517 len = bufsize - 1;
518
519 name[len] = '\0';
520 setemul(name, ktr_header.ktr_pid, 1);
521 emul_changed = 1;
522
523 (void)printf("\"%s\"\n", name);
524 }
525
526 void
527 ktrgenio(ktr, len)
528 struct ktr_genio *ktr;
529 int len;
530 {
531 int datalen = len - sizeof (struct ktr_genio);
532 char *dp = (char *)ktr + sizeof (struct ktr_genio);
533 char *cp;
534 int col = 0;
535 int width;
536 char visbuf[5];
537 static int screenwidth = 0;
538
539 if (screenwidth == 0) {
540 struct winsize ws;
541
542 if (!plain && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
543 ws.ws_col > 8)
544 screenwidth = ws.ws_col;
545 else
546 screenwidth = 80;
547 }
548 printf("fd %d %s %d bytes\n", ktr->ktr_fd,
549 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
550 if (maxdata && datalen > maxdata)
551 datalen = maxdata;
552 (void)printf(" \"");
553 col = 8;
554 for (; datalen > 0; datalen--, dp++) {
555 (void) vis(visbuf, *dp, VIS_CSTYLE, datalen>1?*(dp+1):0);
556 cp = visbuf;
557 /*
558 * Keep track of printables and
559 * space chars (like fold(1)).
560 */
561 if (col == 0) {
562 (void)putchar('\t');
563 col = 8;
564 }
565 switch(*cp) {
566 case '\n':
567 col = 0;
568 (void)putchar('\n');
569 continue;
570 case '\t':
571 width = 8 - (col&07);
572 break;
573 default:
574 width = strlen(cp);
575 }
576 if (col + width > (screenwidth-2)) {
577 (void)printf("\\\n\t");
578 col = 8;
579 }
580 col += width;
581 do {
582 (void)putchar(*cp++);
583 } while (*cp);
584 }
585 if (col == 0)
586 (void)printf(" ");
587 (void)printf("\"\n");
588 }
589
590 void
591 ktrpsig(psig)
592 struct ktr_psig *psig;
593 {
594 int signo, first;
595
596 (void)printf("SIG%s ", signame(psig->signo, 0));
597 if (psig->action == SIG_DFL)
598 (void)printf("SIG_DFL\n");
599 else {
600 (void)printf("caught handler=0x%lx mask=(",
601 (u_long)psig->action);
602 first = 1;
603 for (signo = 1; signo < NSIG; signo++) {
604 if (sigismember(&psig->mask, signo)) {
605 if (first)
606 first = 0;
607 else
608 (void)printf(",");
609 (void)printf("%d", signo);
610 }
611 }
612 (void)printf(") code=0x%x\n", psig->code);
613 }
614 }
615
616 void
617 ktrcsw(cs)
618 struct ktr_csw *cs;
619 {
620
621 (void)printf("%s %s\n", cs->out ? "stop" : "resume",
622 cs->user ? "user" : "kernel");
623 }
624
625 void
626 ktruser(usr, len)
627 struct ktr_user *usr;
628 int len;
629 {
630 int i;
631 unsigned char *dta;
632
633 printf("\"%.*s: %d, ", KTR_USER_MAXIDLEN, usr->ktr_id, len);
634 dta = (unsigned char *)usr;
635 for(i=sizeof(struct ktr_user); i < len; i++)
636 printf("%02x", (unsigned int) dta[i]);
637 printf("\"\n");
638 }
639
640 static const char *
641 signame(long sig, int xlat)
642 {
643 static char buf[64];
644 if (sig <= 0 || sig >= NSIG) {
645 (void)snprintf(buf, sizeof(buf), "*unknown %ld*", sig);
646 return buf;
647 } else
648 return sys_signame[(xlat && current->signalmap != NULL) ?
649 current->signalmap[sig] : sig];
650 }
651
652 void
653 usage()
654 {
655
656 (void)fprintf(stderr,
657 "usage: kdump [-dnlRT] [-e emulation] [-f trfile] [-m maxdata] [-p pid]\n"
658 " [-t [cnis]] [trfile]\n");
659 exit(1);
660 }
661