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