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