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 const char copyright[] =
36 "@(#) Copyright (c) 1988, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)kdump.c 8.1 (Berkeley) 6/6/93";
44 static const char rcsid[] =
45 "$FreeBSD: src/usr.bin/kdump/kdump.c,v 1.17 1999/12/29 05:05:33 peter Exp $";
50 #include <sys/errno.h>
52 #include <sys/param.h>
53 #include <sys/errno.h>
56 #include <sys/ktrace.h>
57 #include <sys/ioctl.h>
58 #include <sys/ptrace.h>
68 int timestamp, decimal, fancy = 1, tail, maxdata;
69 char *tracefile = DEF_TRACEFILE;
70 struct ktr_header ktr_header;
72 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
80 int trpoints = ALL_POINTS;
82 (void) setlocale(LC_CTYPE, "");
84 while ((ch = getopt(argc,argv,"f:dlm:nRTt:")) != -1)
96 maxdata = atoi(optarg);
102 timestamp = 2; /* relative timestamp */
108 trpoints = getpoints(optarg);
110 errx(1, "unknown trace point in %s", optarg);
119 m = (void *)malloc(size = 1025);
121 errx(1, "%s", strerror(ENOMEM));
122 if (!freopen(tracefile, "r", stdin))
123 err(1, "%s", tracefile);
124 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
125 if (trpoints & (1<<ktr_header.ktr_type))
126 dumpheader(&ktr_header);
127 if ((ktrlen = ktr_header.ktr_len) < 0)
128 errx(1, "bogus length 0x%x", ktrlen);
130 m = (void *)realloc(m, ktrlen+1);
132 errx(1, "%s", strerror(ENOMEM));
135 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
136 errx(1, "data too short");
137 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
139 switch (ktr_header.ktr_type) {
141 ktrsyscall((struct ktr_syscall *)m);
144 ktrsysret((struct ktr_sysret *)m);
150 ktrgenio((struct ktr_genio *)m, ktrlen);
153 ktrpsig((struct ktr_psig *)m);
156 ktrcsw((struct ktr_csw *)m);
163 (void)fflush(stdout);
167 fread_tail(buf, size, num)
173 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
181 struct ktr_header *kth;
183 static char unknown[64];
184 static struct timeval prevtime, temp;
187 switch (kth->ktr_type) {
210 (void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
214 (void)printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
216 if (timestamp == 2) {
217 temp = kth->ktr_time;
218 timevalsub(&kth->ktr_time, &prevtime);
221 (void)printf("%ld.%06ld ",
222 kth->ktr_time.tv_sec, kth->ktr_time.tv_usec);
224 (void)printf("%s ", type);
227 #include <sys/syscall.h>
229 #include <sys/kern/syscalls.c>
231 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
233 static char *ptrace_ops[] = {
234 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U",
235 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE",
236 "PT_KILL", "PT_STEP", "PT_ATTACH", "PT_DETACH",
240 register struct ktr_syscall *ktr;
242 register narg = ktr->ktr_narg;
243 register register_t *ip;
246 if (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0)
247 (void)printf("[%d]", ktr->ktr_code);
249 (void)printf("%s", syscallnames[ktr->ktr_code]);
250 ip = &ktr->ktr_args[0];
254 if (ktr->ktr_code == SYS_ioctl) {
257 (void)printf("(%ld", (long)*ip);
259 (void)printf("(%#lx", (long)*ip);
262 if ((cp = ioctlname(*ip)) != NULL)
263 (void)printf(",%s", cp);
266 (void)printf(",%ld", (long)*ip);
268 (void)printf(",%#lx ", (long)*ip);
273 } else if (ktr->ktr_code == SYS_ptrace) {
274 if (*ip < sizeof(ptrace_ops) /
275 sizeof(ptrace_ops[0]) && *ip >= 0)
276 (void)printf("(%s", ptrace_ops[*ip]);
278 else if (*ip == PT_GETREGS)
279 (void)printf("(%s", "PT_GETREGS");
282 else if (*ip == PT_SETREGS)
283 (void)printf("(%s", "PT_SETREGS");
286 else if (*ip == PT_GETFPREGS)
287 (void)printf("(%s", "PT_GETFPREGS");
290 else if (*ip == PT_SETFPREGS)
291 (void)printf("(%s", "PT_SETFPREGS");
294 else if (*ip == PT_GETDBREGS)
295 (void)printf("(%s", "PT_GETDBREGS");
298 else if (*ip == PT_SETDBREGS)
299 (void)printf("(%s", "PT_SETDBREGS");
302 (void)printf("(%ld", (long)*ip);
310 (void)printf("%c%ld", c, (long)*ip);
312 (void)printf("%c%#lx", c, (long)*ip);
323 struct ktr_sysret *ktr;
325 register register_t ret = ktr->ktr_retval;
326 register int error = ktr->ktr_error;
327 register int code = ktr->ktr_code;
329 if (code >= nsyscalls || code < 0)
330 (void)printf("[%d] ", code);
332 (void)printf("%s ", syscallnames[code]);
336 (void)printf("%d", ret);
337 if (ret < 0 || ret > 9)
338 (void)printf("/%#lx", (long)ret);
341 (void)printf("%ld", (long)ret);
343 (void)printf("%#lx", (long)ret);
345 } else if (error == ERESTART)
346 (void)printf("RESTART");
347 else if (error == EJUSTRETURN)
348 (void)printf("JUSTRETURN");
350 (void)printf("-1 errno %d", ktr->ktr_error);
352 (void)printf(" %s", strerror(ktr->ktr_error));
360 (void)printf("\"%.*s\"\n", len, cp);
364 struct ktr_genio *ktr;
366 register int datalen = len - sizeof (struct ktr_genio);
367 register char *dp = (char *)ktr + sizeof (struct ktr_genio);
369 register int col = 0;
372 static screenwidth = 0;
374 if (screenwidth == 0) {
377 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
379 screenwidth = ws.ws_col;
383 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
384 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
385 datalen == 1 ? "" : "s");
386 if (maxdata && datalen > maxdata)
390 for (;datalen > 0; datalen--, dp++) {
391 (void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
394 * Keep track of printables and
395 * space chars (like fold(1)).
407 width = 8 - (col&07);
412 if (col + width > (screenwidth-2)) {
413 (void)printf("\\\n\t");
418 (void)putchar(*cp++);
423 (void)printf("\"\n");
427 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
428 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
429 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
430 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
431 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
432 "USR2", NULL, /* 31 - 32 */
436 struct ktr_psig *psig;
438 (void)printf("SIG%s ", signames[psig->signo]);
439 if (psig->action == SIG_DFL)
440 (void)printf("SIG_DFL\n");
442 (void)printf("caught handler=0x%lx mask=0x%x code=0x%x\n",
443 (u_long)psig->action, psig->mask, psig->code);
449 (void)printf("%s %s\n", cs->out ? "stop" : "resume",
450 cs->user ? "user" : "kernel");
457 (void)printf("%d ", len);
459 (void)printf(" %02x", *p++);
466 (void)fprintf(stderr,
467 "usage: kdump [-dnlRT] [-f trfile] [-m maxdata] [-t [cnisuw]]\n");