2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char copyright[] =
36 "@(#) Copyright (c) 1988, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)kdump.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/param.h>
45 #include <sys/errno.h>
48 #include <sys/ktrace.h>
49 #include <sys/ioctl.h>
50 #include <sys/ptrace.h>
52 #include <sys/errno.h>
60 int timestamp, decimal, fancy = 1, tail, maxdata;
61 char *tracefile = DEF_TRACEFILE;
62 struct ktr_header ktr_header;
64 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
74 int trpoints = ALL_POINTS;
76 while ((ch = getopt(argc,argv,"f:dlm:nRTt:")) != EOF)
88 maxdata = atoi(optarg);
94 timestamp = 2; /* relative timestamp */
100 trpoints = getpoints(optarg);
102 (void)fprintf(stderr,
103 "kdump: unknown trace point in %s\n",
117 m = (void *)malloc(size = 1025);
119 (void)fprintf(stderr, "kdump: %s.\n", strerror(ENOMEM));
122 if (!freopen(tracefile, "r", stdin)) {
123 (void)fprintf(stderr,
124 "kdump: %s: %s.\n", tracefile, strerror(errno));
127 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
128 if (trpoints & (1<<ktr_header.ktr_type))
129 dumpheader(&ktr_header);
130 if ((ktrlen = ktr_header.ktr_len) < 0) {
131 (void)fprintf(stderr,
132 "kdump: bogus length 0x%x\n", ktrlen);
136 m = (void *)realloc(m, ktrlen+1);
138 (void)fprintf(stderr,
139 "kdump: %s.\n", strerror(ENOMEM));
144 if (ktrlen && fread_tail(m, ktrlen, 1) == 0) {
145 (void)fprintf(stderr, "kdump: data too short.\n");
148 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
150 switch (ktr_header.ktr_type) {
152 ktrsyscall((struct ktr_syscall *)m);
155 ktrsysret((struct ktr_sysret *)m);
161 ktrgenio((struct ktr_genio *)m, ktrlen);
164 ktrpsig((struct ktr_psig *)m);
167 ktrcsw((struct ktr_csw *)m);
171 (void)fflush(stdout);
175 fread_tail(buf, size, num)
181 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
189 struct ktr_header *kth;
191 static char unknown[64];
192 static struct timeval prevtime, temp;
195 switch (kth->ktr_type) {
215 (void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
219 (void)printf("%6d %-8s ", kth->ktr_pid, kth->ktr_comm);
221 if (timestamp == 2) {
222 temp = kth->ktr_time;
223 timevalsub(&kth->ktr_time, &prevtime);
226 (void)printf("%ld.%06ld ",
227 kth->ktr_time.tv_sec, kth->ktr_time.tv_usec);
229 (void)printf("%s ", type);
232 #include <sys/syscall.h>
234 #include "/sys/kern/syscalls.c"
236 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
238 static char *ptrace_ops[] = {
239 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U",
240 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE",
241 "PT_KILL", "PT_STEP",
245 register struct ktr_syscall *ktr;
247 register narg = ktr->ktr_narg;
251 if (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0)
252 (void)printf("[%d]", ktr->ktr_code);
254 (void)printf("%s", syscallnames[ktr->ktr_code]);
255 ip = (int *)((char *)ktr + sizeof(struct ktr_syscall));
259 if (ktr->ktr_code == SYS_ioctl) {
262 (void)printf("(%d", *ip);
264 (void)printf("(%#x", *ip);
267 if ((cp = ioctlname(*ip)) != NULL)
268 (void)printf(",%s", cp);
271 (void)printf(",%d", *ip);
273 (void)printf(",%#x ", *ip);
278 } else if (ktr->ktr_code == SYS_ptrace) {
279 if (*ip <= PT_STEP && *ip >= 0)
280 (void)printf("(%s", ptrace_ops[*ip]);
282 (void)printf("(%d", *ip);
290 (void)printf("%c%d", c, *ip);
292 (void)printf("%c%#x", c, *ip);
303 struct ktr_sysret *ktr;
305 register int ret = ktr->ktr_retval;
306 register int error = ktr->ktr_error;
307 register int code = ktr->ktr_code;
309 if (code >= nsyscalls || code < 0)
310 (void)printf("[%d] ", code);
312 (void)printf("%s ", syscallnames[code]);
316 (void)printf("%d", ret);
317 if (ret < 0 || ret > 9)
318 (void)printf("/%#x", ret);
321 (void)printf("%d", ret);
323 (void)printf("%#x", ret);
325 } else if (error == ERESTART)
326 (void)printf("RESTART");
327 else if (error == EJUSTRETURN)
328 (void)printf("JUSTRETURN");
330 (void)printf("-1 errno %d", ktr->ktr_error);
332 (void)printf(" %s", strerror(ktr->ktr_error));
340 (void)printf("\"%.*s\"\n", len, cp);
344 struct ktr_genio *ktr;
346 register int datalen = len - sizeof (struct ktr_genio);
347 register char *dp = (char *)ktr + sizeof (struct ktr_genio);
349 register int col = 0;
352 static screenwidth = 0;
354 if (screenwidth == 0) {
357 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
359 screenwidth = ws.ws_col;
363 printf("fd %d %s %d bytes\n", ktr->ktr_fd,
364 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen);
365 if (maxdata && datalen > maxdata)
369 for (;datalen > 0; datalen--, dp++) {
370 (void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
373 * Keep track of printables and
374 * space chars (like fold(1)).
386 width = 8 - (col&07);
391 if (col + width > (screenwidth-2)) {
392 (void)printf("\\\n\t");
397 (void)putchar(*cp++);
402 (void)printf("\"\n");
406 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
407 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
408 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
409 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
410 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
411 "USR2", NULL, /* 31 - 32 */
415 struct ktr_psig *psig;
417 (void)printf("SIG%s ", signames[psig->signo]);
418 if (psig->action == SIG_DFL)
419 (void)printf("SIG_DFL\n");
421 (void)printf("caught handler=0x%x mask=0x%x code=0x%x\n",
422 (u_int)psig->action, psig->mask, psig->code);
428 (void)printf("%s %s\n", cs->out ? "stop" : "resume",
429 cs->user ? "user" : "kernel");
434 (void)fprintf(stderr,
435 "usage: kdump [-dnlRT] [-f trfile] [-m maxdata] [-t [cnis]]\n");