Annotation of src/usr.bin/kdump/kdump.c, Revision 1.19
1.19 ! mickey 1: /* $OpenBSD: kdump.c,v 1.18 2002/07/06 19:14:20 nordin Exp $ */
1.4 deraadt 2:
1.1 deraadt 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: #ifndef lint
37: static char copyright[] =
38: "@(#) 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: #endif
1.19 ! mickey 46: static char *rcsid = "$OpenBSD: kdump.c,v 1.18 2002/07/06 19:14:20 nordin Exp $";
1.1 deraadt 47: #endif /* not lint */
48:
49: #include <sys/param.h>
50: #include <sys/time.h>
51: #include <sys/uio.h>
52: #include <sys/ktrace.h>
53: #include <sys/ioctl.h>
54: #include <sys/ptrace.h>
55: #define _KERNEL
56: #include <sys/errno.h>
57: #undef _KERNEL
58:
59: #include <err.h>
60: #include <signal.h>
61: #include <stdio.h>
62: #include <stdlib.h>
63: #include <string.h>
64: #include <unistd.h>
65: #include <vis.h>
66:
67: #include "ktrace.h"
1.12 espie 68: #include "extern.h"
1.1 deraadt 69:
70: int timestamp, decimal, fancy = 1, tail, maxdata;
71: char *tracefile = DEF_TRACEFILE;
72: struct ktr_header ktr_header;
1.17 deraadt 73: pid_t pid = -1;
1.1 deraadt 74:
75: #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
76:
77: #include <sys/syscall.h>
78:
1.9 deraadt 79: #include "../../sys/compat/bsdos/bsdos_syscall.h"
80: #include "../../sys/compat/freebsd/freebsd_syscall.h"
1.10 kstailey 81: #include "../../sys/compat/netbsd/netbsd_syscall.h"
1.1 deraadt 82: #include "../../sys/compat/hpux/hpux_syscall.h"
83: #include "../../sys/compat/ibcs2/ibcs2_syscall.h"
84: #include "../../sys/compat/linux/linux_syscall.h"
85: #include "../../sys/compat/osf1/osf1_syscall.h"
86: #include "../../sys/compat/sunos/sunos_syscall.h"
87: #include "../../sys/compat/svr4/svr4_syscall.h"
88: #include "../../sys/compat/ultrix/ultrix_syscall.h"
89:
90: #define KTRACE
1.19 ! mickey 91: #define PTRACE
1.7 deraadt 92: #define NFSCLIENT
93: #define NFSSERVER
94: #define SYSVSEM
95: #define SYSVMSG
96: #define SYSVSHM
97: #define LFS
1.19 ! mickey 98: #define UFS_EXTATTR
1.1 deraadt 99: #include "../../sys/kern/syscalls.c"
100:
1.9 deraadt 101: #include "../../sys/compat/bsdos/bsdos_syscalls.c"
102: #include "../../sys/compat/freebsd/freebsd_syscalls.c"
1.10 kstailey 103: #include "../../sys/compat/netbsd/netbsd_syscalls.c"
1.1 deraadt 104: #include "../../sys/compat/hpux/hpux_syscalls.c"
105: #include "../../sys/compat/ibcs2/ibcs2_syscalls.c"
106: #include "../../sys/compat/linux/linux_syscalls.c"
107: #include "../../sys/compat/osf1/osf1_syscalls.c"
108: #include "../../sys/compat/sunos/sunos_syscalls.c"
109: #include "../../sys/compat/svr4/svr4_syscalls.c"
110: #include "../../sys/compat/ultrix/ultrix_syscalls.c"
111: #undef KTRACE
1.19 ! mickey 112: #undef PTRACE
1.7 deraadt 113: #undef NFSCLIENT
114: #undef NFSSERVER
115: #undef SYSVSEM
116: #undef SYSVMSG
117: #undef SYSVSHM
118: #undef LFS
1.19 ! mickey 119: #undef UFS_EXTATTR
1.1 deraadt 120:
121: struct emulation {
122: char *name; /* Emulation name */
123: char **sysnames; /* Array of system call names */
124: int nsysnames; /* Number of */
125: };
126:
127: static struct emulation emulations[] = {
1.9 deraadt 128: { "native", syscallnames, SYS_MAXSYSCALL },
129: { "hpux", hpux_syscallnames, HPUX_SYS_MAXSYSCALL },
130: { "ibcs2", ibcs2_syscallnames, IBCS2_SYS_MAXSYSCALL },
131: { "linux", linux_syscallnames, LINUX_SYS_MAXSYSCALL },
132: { "osf1", osf1_syscallnames, OSF1_SYS_MAXSYSCALL },
133: { "sunos", sunos_syscallnames, SUNOS_SYS_MAXSYSCALL },
134: { "svr4", svr4_syscallnames, SVR4_SYS_MAXSYSCALL },
135: { "ultrix", ultrix_syscallnames, ULTRIX_SYS_MAXSYSCALL },
136: { "bsdos", bsdos_syscallnames, BSDOS_SYS_MAXSYSCALL },
137: { "freebsd", freebsd_syscallnames, FREEBSD_SYS_MAXSYSCALL },
1.10 kstailey 138: { "netbsd", netbsd_syscallnames, NETBSD_SYS_MAXSYSCALL },
1.9 deraadt 139: { NULL, NULL, NULL }
1.1 deraadt 140: };
141:
142: struct emulation *current;
143:
144:
145: static char *ptrace_ops[] = {
146: "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U",
147: "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE",
1.15 art 148: "PT_KILL", "PT_ATTACH", "PT_DETACH", "PT_IO",
1.1 deraadt 149: };
150:
1.13 millert 151: static int fread_tail(void *, int, int);
152: static void dumpheader(struct ktr_header *);
153: static void ktrcsw(struct ktr_csw *);
154: static void ktremul(char *, int);
155: static void ktrgenio(struct ktr_genio *, int);
156: static void ktrnamei(const char *, int);
157: static void ktrpsig(struct ktr_psig *);
158: static void ktrsyscall(struct ktr_syscall *);
159: static void ktrsysret(struct ktr_sysret *);
160: static void setemul(const char *);
161: static void usage(void);
1.12 espie 162:
1.1 deraadt 163: int
1.17 deraadt 164: main(int argc, char *argv[])
1.1 deraadt 165: {
1.17 deraadt 166: int ch, ktrlen, size, silent;
167: int trpoints = ALL_POINTS;
1.12 espie 168: void *m;
1.1 deraadt 169:
1.3 deraadt 170: current = &emulations[0]; /* native */
1.1 deraadt 171:
1.17 deraadt 172: while ((ch = getopt(argc, argv, "e:f:dlm:nRp:Tt:")) != -1)
1.1 deraadt 173: switch (ch) {
174: case 'e':
175: setemul(optarg);
176: break;
177: case 'f':
178: tracefile = optarg;
179: break;
180: case 'd':
181: decimal = 1;
182: break;
183: case 'l':
184: tail = 1;
185: break;
186: case 'm':
187: maxdata = atoi(optarg);
188: break;
189: case 'n':
190: fancy = 0;
191: break;
1.17 deraadt 192: case 'p':
193: pid = atoi(optarg);
194: break;
1.1 deraadt 195: case 'R':
196: timestamp = 2; /* relative timestamp */
197: break;
198: case 'T':
199: timestamp = 1;
200: break;
201: case 't':
202: trpoints = getpoints(optarg);
203: if (trpoints < 0)
204: errx(1, "unknown trace point in %s", optarg);
205: break;
206: default:
207: usage();
208: }
1.5 deraadt 209: if (argc > optind)
1.1 deraadt 210: usage();
211:
212: m = (void *)malloc(size = 1025);
213: if (m == NULL)
214: errx(1, "%s", strerror(ENOMEM));
215: if (!freopen(tracefile, "r", stdin))
216: err(1, "%s", tracefile);
217: while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
1.17 deraadt 218: silent = 0;
219: if (pid != -1 && pid != ktr_header.ktr_pid)
220: silent = 1;
221: if (silent == 0 && trpoints & (1<<ktr_header.ktr_type))
1.1 deraadt 222: dumpheader(&ktr_header);
223: if ((ktrlen = ktr_header.ktr_len) < 0)
224: errx(1, "bogus length 0x%x", ktrlen);
225: if (ktrlen > size) {
226: m = (void *)realloc(m, ktrlen+1);
227: if (m == NULL)
228: errx(1, "%s", strerror(ENOMEM));
229: size = ktrlen;
230: }
231: if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
232: errx(1, "data too short");
1.17 deraadt 233: if (silent)
234: continue;
1.1 deraadt 235: if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
236: continue;
237: switch (ktr_header.ktr_type) {
238: case KTR_SYSCALL:
239: ktrsyscall((struct ktr_syscall *)m);
240: break;
241: case KTR_SYSRET:
242: ktrsysret((struct ktr_sysret *)m);
243: break;
244: case KTR_NAMEI:
245: ktrnamei(m, ktrlen);
246: break;
247: case KTR_GENIO:
248: ktrgenio((struct ktr_genio *)m, ktrlen);
249: break;
250: case KTR_PSIG:
251: ktrpsig((struct ktr_psig *)m);
252: break;
253: case KTR_CSW:
254: ktrcsw((struct ktr_csw *)m);
255: break;
256: case KTR_EMUL:
257: ktremul(m, ktrlen);
258: break;
259: }
260: if (tail)
261: (void)fflush(stdout);
262: }
1.12 espie 263: exit(0);
1.1 deraadt 264: }
265:
1.12 espie 266: static int
1.17 deraadt 267: fread_tail(void *buf, int size, int num)
1.1 deraadt 268: {
269: int i;
270:
271: while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
272: (void)sleep(1);
273: clearerr(stdin);
274: }
275: return (i);
276: }
277:
1.12 espie 278: static void
1.17 deraadt 279: dumpheader(struct ktr_header *kth)
1.1 deraadt 280: {
1.17 deraadt 281: static struct timeval prevtime;
1.1 deraadt 282: char unknown[64], *type;
283: struct timeval temp;
284:
285: switch (kth->ktr_type) {
286: case KTR_SYSCALL:
287: type = "CALL";
288: break;
289: case KTR_SYSRET:
290: type = "RET ";
291: break;
292: case KTR_NAMEI:
293: type = "NAMI";
294: break;
295: case KTR_GENIO:
296: type = "GIO ";
297: break;
298: case KTR_PSIG:
299: type = "PSIG";
300: break;
301: case KTR_CSW:
302: type = "CSW";
303: break;
304: case KTR_EMUL:
305: type = "EMUL";
306: break;
307: default:
1.17 deraadt 308: (void)snprintf(unknown, sizeof unknown, "UNKNOWN(%d)",
309: kth->ktr_type);
1.1 deraadt 310: type = unknown;
311: }
312:
1.16 mpech 313: (void)printf("%6ld %-8.*s ", (long)kth->ktr_pid, MAXCOMLEN,
314: kth->ktr_comm);
1.1 deraadt 315: if (timestamp) {
316: if (timestamp == 2) {
317: timersub(&kth->ktr_time, &prevtime, &temp);
318: prevtime = kth->ktr_time;
319: } else
320: temp = kth->ktr_time;
321: (void)printf("%ld.%06ld ", temp.tv_sec, temp.tv_usec);
322: }
323: (void)printf("%s ", type);
324: }
325:
1.12 espie 326: static void
1.17 deraadt 327: ioctldecode(u_long cmd)
1.2 deraadt 328: {
329: char dirbuf[4], *dir = dirbuf;
330:
1.6 deraadt 331: if (cmd & IOC_IN)
332: *dir++ = 'W';
1.2 deraadt 333: if (cmd & IOC_OUT)
334: *dir++ = 'R';
335: *dir = '\0';
336:
337: printf(decimal ? ",_IO%s('%c',%ld" : ",_IO%s('%c',%#lx",
338: dirbuf, (cmd >> 8) & 0xff, cmd & 0xff);
339: if ((cmd & IOC_VOID) == 0)
340: printf(decimal ? ",%ld)" : ",%#lx)", (cmd >> 16) & 0xff);
341: else
342: printf(")");
343: }
1.1 deraadt 344:
1.12 espie 345: static void
1.17 deraadt 346: ktrsyscall(struct ktr_syscall *ktr)
1.1 deraadt 347: {
1.12 espie 348: int argsize = ktr->ktr_argsize;
349: register_t *ap;
1.1 deraadt 350:
351: if (ktr->ktr_code >= current->nsysnames || ktr->ktr_code < 0)
352: (void)printf("[%d]", ktr->ktr_code);
353: else
354: (void)printf("%s", current->sysnames[ktr->ktr_code]);
355: ap = (register_t *)((char *)ktr + sizeof(struct ktr_syscall));
356: if (argsize) {
357: char c = '(';
358: if (fancy) {
359: if (ktr->ktr_code == SYS_ioctl) {
1.12 espie 360: const char *cp;
1.17 deraadt 361:
1.1 deraadt 362: if (decimal)
363: (void)printf("(%ld", (long)*ap);
364: else
365: (void)printf("(%#lx", (long)*ap);
366: ap++;
367: argsize -= sizeof(register_t);
368: if ((cp = ioctlname(*ap)) != NULL)
369: (void)printf(",%s", cp);
1.2 deraadt 370: else
371: ioctldecode(*ap);
1.1 deraadt 372: c = ',';
373: ap++;
374: argsize -= sizeof(register_t);
375: } else if (ktr->ktr_code == SYS_ptrace) {
376: if (*ap >= 0 && *ap <=
377: sizeof(ptrace_ops) / sizeof(ptrace_ops[0]))
378: (void)printf("(%s", ptrace_ops[*ap]);
379: else
380: (void)printf("(%ld", (long)*ap);
381: c = ',';
382: ap++;
383: argsize -= sizeof(register_t);
384: }
385: }
386: while (argsize) {
387: if (decimal)
388: (void)printf("%c%ld", c, (long)*ap);
389: else
390: (void)printf("%c%#lx", c, (long)*ap);
391: c = ',';
392: ap++;
393: argsize -= sizeof(register_t);
394: }
395: (void)putchar(')');
396: }
397: (void)putchar('\n');
398: }
399:
1.12 espie 400: static void
1.17 deraadt 401: ktrsysret(struct ktr_sysret *ktr)
1.1 deraadt 402: {
1.12 espie 403: int ret = ktr->ktr_retval;
404: int error = ktr->ktr_error;
405: int code = ktr->ktr_code;
1.1 deraadt 406:
407: if (code >= current->nsysnames || code < 0)
408: (void)printf("[%d] ", code);
409: else
410: (void)printf("%s ", current->sysnames[code]);
411:
412: if (error == 0) {
413: if (fancy) {
414: (void)printf("%d", ret);
415: if (ret < 0 || ret > 9)
416: (void)printf("/%#x", ret);
417: } else {
418: if (decimal)
419: (void)printf("%d", ret);
420: else
421: (void)printf("%#x", ret);
422: }
423: } else if (error == ERESTART)
424: (void)printf("RESTART");
425: else if (error == EJUSTRETURN)
426: (void)printf("JUSTRETURN");
427: else {
428: (void)printf("-1 errno %d", ktr->ktr_error);
429: if (fancy)
430: (void)printf(" %s", strerror(ktr->ktr_error));
431: }
432: (void)putchar('\n');
433: }
434:
1.12 espie 435: static void
1.17 deraadt 436: ktrnamei(const char *cp, int len)
1.1 deraadt 437: {
438: (void)printf("\"%.*s\"\n", len, cp);
439: }
440:
1.12 espie 441: static void
1.17 deraadt 442: ktremul(char *cp, int len)
1.1 deraadt 443: {
444: char name[1024];
445:
446: if (len >= sizeof(name))
447: errx(1, "Emulation name too long");
448:
449: strncpy(name, cp, len);
450: name[len] = '\0';
451: (void)printf("\"%s\"\n", name);
452:
453: setemul(name);
454: }
455:
1.12 espie 456: static void
1.17 deraadt 457: ktrgenio(struct ktr_genio *ktr, int len)
1.1 deraadt 458: {
1.17 deraadt 459: char *dp = (char *)ktr + sizeof (struct ktr_genio);
1.12 espie 460: int datalen = len - sizeof (struct ktr_genio);
461: static int screenwidth = 0;
1.17 deraadt 462: int col = 0, width;
463: char visbuf[5], *cp;
1.1 deraadt 464:
465: if (screenwidth == 0) {
466: struct winsize ws;
467:
468: if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
469: ws.ws_col > 8)
470: screenwidth = ws.ws_col;
471: else
472: screenwidth = 80;
473: }
474: printf("fd %d %s %d bytes\n", ktr->ktr_fd,
475: ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
476: if (maxdata && datalen > maxdata)
477: datalen = maxdata;
478: (void)printf(" \"");
479: col = 8;
480: for (; datalen > 0; datalen--, dp++) {
481: (void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
482: cp = visbuf;
1.17 deraadt 483:
1.1 deraadt 484: /*
485: * Keep track of printables and
486: * space chars (like fold(1)).
487: */
488: if (col == 0) {
489: (void)putchar('\t');
490: col = 8;
491: }
1.17 deraadt 492: switch (*cp) {
1.1 deraadt 493: case '\n':
494: col = 0;
495: (void)putchar('\n');
496: continue;
497: case '\t':
498: width = 8 - (col&07);
499: break;
500: default:
501: width = strlen(cp);
502: }
503: if (col + width > (screenwidth-2)) {
504: (void)printf("\\\n\t");
505: col = 8;
506: }
507: col += width;
508: do {
509: (void)putchar(*cp++);
510: } while (*cp);
511: }
512: if (col == 0)
513: (void)printf(" ");
514: (void)printf("\"\n");
515: }
516:
1.12 espie 517: static void
1.17 deraadt 518: ktrpsig(struct ktr_psig *psig)
1.1 deraadt 519: {
520: (void)printf("SIG%s ", sys_signame[psig->signo]);
521: if (psig->action == SIG_DFL)
1.14 deraadt 522: (void)printf("SIG_DFL code %d", psig->code);
1.1 deraadt 523: else
1.14 deraadt 524: (void)printf("caught handler=0x%lx mask=0x%x",
525: (u_long)psig->action, psig->mask);
526: switch (psig->signo) {
527: case SIGSEGV:
528: case SIGILL:
529: case SIGBUS:
530: case SIGFPE:
531: printf(" addr=%p trapno=%d", psig->si.si_addr,
532: psig->si.si_trapno);
533: break;
534: default:
535: break;
536: }
537: printf("\n");
1.1 deraadt 538: }
539:
1.12 espie 540: static void
1.17 deraadt 541: ktrcsw(struct ktr_csw *cs)
1.1 deraadt 542: {
543: (void)printf("%s %s\n", cs->out ? "stop" : "resume",
544: cs->user ? "user" : "kernel");
545: }
546:
1.12 espie 547: static void
1.17 deraadt 548: usage(void)
1.1 deraadt 549: {
550:
1.19 ! mickey 551: extern char *__progname;
! 552: fprintf(stderr, "usage: %s "
! 553: "[-dnlRT] [-e emulation] [-p pid] [-f trfile] [-m maxdata] "
! 554: "[-t [cnis]]\n", __progname);
1.1 deraadt 555: exit(1);
556: }
557:
1.12 espie 558: static void
1.17 deraadt 559: setemul(const char *name)
1.1 deraadt 560: {
561: int i;
1.17 deraadt 562:
1.1 deraadt 563: for (i = 0; emulations[i].name != NULL; i++)
564: if (strcmp(emulations[i].name, name) == 0) {
565: current = &emulations[i];
566: return;
567: }
568: warnx("Emulation `%s' unknown", name);
569: }