BSD 4_4 release
[unix-history] / usr / src / usr.bin / kdump / kdump.c
CommitLineData
2bfb1112 1/*-
ad787160
C
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
af3069b3 4 *
ad787160
C
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
af3069b3
MT
32 */
33
34#ifndef lint
ad787160
C
35static char copyright[] =
36"@(#) Copyright (c) 1988, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
af3069b3
MT
38#endif /* not lint */
39
40#ifndef lint
ad787160 41static char sccsid[] = "@(#)kdump.c 8.1 (Berkeley) 6/6/93";
af3069b3
MT
42#endif /* not lint */
43
d1c87cd4 44#include <sys/param.h>
961a0908 45#include <sys/errno.h>
d1c87cd4
KB
46#include <sys/time.h>
47#include <sys/uio.h>
48#include <sys/ktrace.h>
cf9db849 49#include <sys/ioctl.h>
64fe3091 50#include <sys/ptrace.h>
2bfb1112 51#define KERNEL
d1c87cd4 52#include <sys/errno.h>
2bfb1112 53#undef KERNEL
d1c87cd4
KB
54#include <vis.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
2bfb1112 58#include "ktrace.h"
af3069b3 59
8b4e7b52 60int timestamp, decimal, fancy = 1, tail, maxdata;
f4d5acdc 61char *tracefile = DEF_TRACEFILE;
af3069b3 62struct ktr_header ktr_header;
af3069b3
MT
63
64#define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
65
66main(argc, argv)
d1c87cd4 67 int argc;
af3069b3
MT
68 char *argv[];
69{
70 extern int optind;
71 extern char *optarg;
d1c87cd4
KB
72 int ch, ktrlen, size;
73 register void *m;
cf9db849 74 int trpoints = ALL_POINTS;
af3069b3 75
d1c87cd4 76 while ((ch = getopt(argc,argv,"f:dlm:nRTt:")) != EOF)
af3069b3 77 switch((char)ch) {
8b4e7b52
MT
78 case 'f':
79 tracefile = optarg;
80 break;
81 case 'd':
82 decimal = 1;
83 break;
8b4e7b52
MT
84 case 'l':
85 tail = 1;
86 break;
d1c87cd4
KB
87 case 'm':
88 maxdata = atoi(optarg);
89 break;
90 case 'n':
91 fancy = 0;
8b4e7b52 92 break;
cf9db849
MT
93 case 'R':
94 timestamp = 2; /* relative timestamp */
95 break;
d1c87cd4
KB
96 case 'T':
97 timestamp = 1;
98 break;
99 case 't':
100 trpoints = getpoints(optarg);
101 if (trpoints < 0) {
102 (void)fprintf(stderr,
103 "kdump: unknown trace point in %s\n",
104 optarg);
105 exit(1);
106 }
8b4e7b52
MT
107 break;
108 default:
d1c87cd4 109 usage();
af3069b3 110 }
d1c87cd4
KB
111 argv += optind;
112 argc -= optind;
af3069b3 113
d1c87cd4
KB
114 if (argc > 1)
115 usage();
116
117 m = (void *)malloc(size = 1025);
af3069b3 118 if (m == NULL) {
d1c87cd4 119 (void)fprintf(stderr, "kdump: %s.\n", strerror(ENOMEM));
af3069b3
MT
120 exit(1);
121 }
961a0908
KB
122 if (!freopen(tracefile, "r", stdin)) {
123 (void)fprintf(stderr,
124 "kdump: %s: %s.\n", tracefile, strerror(errno));
125 exit(1);
126 }
d1c87cd4 127 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
cf9db849 128 if (trpoints & (1<<ktr_header.ktr_type))
f4d5acdc 129 dumpheader(&ktr_header);
3812fdb0 130 if ((ktrlen = ktr_header.ktr_len) < 0) {
d1c87cd4
KB
131 (void)fprintf(stderr,
132 "kdump: bogus length 0x%x\n", ktrlen);
af3069b3
MT
133 exit(1);
134 }
135 if (ktrlen > size) {
d1c87cd4 136 m = (void *)realloc(m, ktrlen+1);
af3069b3 137 if (m == NULL) {
d1c87cd4
KB
138 (void)fprintf(stderr,
139 "kdump: %s.\n", strerror(ENOMEM));
af3069b3
MT
140 exit(1);
141 }
142 size = ktrlen;
143 }
d1c87cd4
KB
144 if (ktrlen && fread_tail(m, ktrlen, 1) == 0) {
145 (void)fprintf(stderr, "kdump: data too short.\n");
af3069b3
MT
146 exit(1);
147 }
cf9db849 148 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
f4d5acdc 149 continue;
af3069b3
MT
150 switch (ktr_header.ktr_type) {
151 case KTR_SYSCALL:
d1c87cd4 152 ktrsyscall((struct ktr_syscall *)m);
af3069b3
MT
153 break;
154 case KTR_SYSRET:
d1c87cd4 155 ktrsysret((struct ktr_sysret *)m);
af3069b3
MT
156 break;
157 case KTR_NAMEI:
158 ktrnamei(m, ktrlen);
159 break;
b4a09a32
MT
160 case KTR_GENIO:
161 ktrgenio((struct ktr_genio *)m, ktrlen);
162 break;
cf9db849 163 case KTR_PSIG:
d1c87cd4 164 ktrpsig((struct ktr_psig *)m);
cf9db849 165 break;
8b7840bb
MT
166 case KTR_CSW:
167 ktrcsw((struct ktr_csw *)m);
168 break;
af3069b3 169 }
8b4e7b52 170 if (tail)
d1c87cd4 171 (void)fflush(stdout);
af3069b3
MT
172 }
173}
174
d1c87cd4 175fread_tail(buf, size, num)
b4a09a32 176 char *buf;
d1c87cd4 177 int num, size;
b4a09a32
MT
178{
179 int i;
8b4e7b52 180
d1c87cd4
KB
181 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
182 (void)sleep(1);
183 clearerr(stdin);
b4a09a32 184 }
8b4e7b52 185 return (i);
b4a09a32
MT
186}
187
af3069b3
MT
188dumpheader(kth)
189 struct ktr_header *kth;
190{
191 static char unknown[64];
cf9db849 192 static struct timeval prevtime, temp;
af3069b3
MT
193 char *type;
194
195 switch (kth->ktr_type) {
196 case KTR_SYSCALL:
f4d5acdc 197 type = "CALL";
af3069b3
MT
198 break;
199 case KTR_SYSRET:
f4d5acdc 200 type = "RET ";
af3069b3
MT
201 break;
202 case KTR_NAMEI:
f4d5acdc 203 type = "NAMI";
af3069b3 204 break;
b4a09a32 205 case KTR_GENIO:
f4d5acdc 206 type = "GIO ";
b4a09a32 207 break;
cf9db849
MT
208 case KTR_PSIG:
209 type = "PSIG";
210 break;
8b7840bb
MT
211 case KTR_CSW:
212 type = "CSW";
213 break;
af3069b3 214 default:
d1c87cd4 215 (void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
af3069b3
MT
216 type = unknown;
217 }
218
d1c87cd4 219 (void)printf("%6d %-8s ", kth->ktr_pid, kth->ktr_comm);
cf9db849
MT
220 if (timestamp) {
221 if (timestamp == 2) {
222 temp = kth->ktr_time;
223 timevalsub(&kth->ktr_time, &prevtime);
224 prevtime = temp;
225 }
d1c87cd4
KB
226 (void)printf("%ld.%06ld ",
227 kth->ktr_time.tv_sec, kth->ktr_time.tv_usec);
cf9db849 228 }
d1c87cd4 229 (void)printf("%s ", type);
af3069b3
MT
230}
231
f4d5acdc
MT
232#include <sys/syscall.h>
233#define KTRACE
8b4e7b52 234#include "/sys/kern/syscalls.c"
f4d5acdc 235#undef KTRACE
af3069b3
MT
236int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
237
64fe3091
KB
238static 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",
242};
243
d1c87cd4 244ktrsyscall(ktr)
af3069b3
MT
245 register struct ktr_syscall *ktr;
246{
247 register narg = ktr->ktr_narg;
248 register int *ip;
f4d5acdc 249 char *ioctlname();
af3069b3
MT
250
251 if (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0)
d1c87cd4 252 (void)printf("[%d]", ktr->ktr_code);
af3069b3 253 else
d1c87cd4 254 (void)printf("%s", syscallnames[ktr->ktr_code]);
af3069b3 255 ip = (int *)((char *)ktr + sizeof(struct ktr_syscall));
f4d5acdc
MT
256 if (narg) {
257 char c = '(';
64fe3091
KB
258 if (fancy) {
259 if (ktr->ktr_code == SYS_ioctl) {
260 char *cp;
f4d5acdc 261 if (decimal)
64fe3091
KB
262 (void)printf("(%d", *ip);
263 else
264 (void)printf("(%#x", *ip);
265 ip++;
266 narg--;
267 if ((cp = ioctlname(*ip)) != NULL)
268 (void)printf(",%s", cp);
269 else {
270 if (decimal)
271 (void)printf(",%d", *ip);
272 else
273 (void)printf(",%#x ", *ip);
274 }
275 c = ',';
276 ip++;
277 narg--;
278 } else if (ktr->ktr_code == SYS_ptrace) {
279 if (*ip <= PT_STEP && *ip >= 0)
280 (void)printf("(%s", ptrace_ops[*ip]);
f4d5acdc 281 else
64fe3091
KB
282 (void)printf("(%d", *ip);
283 c = ',';
284 ip++;
285 narg--;
f4d5acdc 286 }
f4d5acdc
MT
287 }
288 while (narg) {
289 if (decimal)
d1c87cd4 290 (void)printf("%c%d", c, *ip);
f4d5acdc 291 else
d1c87cd4 292 (void)printf("%c%#x", c, *ip);
f4d5acdc 293 c = ',';
d1c87cd4
KB
294 ip++;
295 narg--;
f4d5acdc 296 }
d1c87cd4 297 (void)putchar(')');
f4d5acdc 298 }
d1c87cd4 299 (void)putchar('\n');
af3069b3
MT
300}
301
d1c87cd4 302ktrsysret(ktr)
af3069b3
MT
303 struct ktr_sysret *ktr;
304{
2bfb1112
MT
305 register int ret = ktr->ktr_retval;
306 register int error = ktr->ktr_error;
307 register int code = ktr->ktr_code;
af3069b3 308
2bfb1112 309 if (code >= nsyscalls || code < 0)
d1c87cd4 310 (void)printf("[%d] ", code);
af3069b3 311 else
d1c87cd4 312 (void)printf("%s ", syscallnames[code]);
2bfb1112
MT
313
314 if (error == 0) {
f4d5acdc 315 if (fancy) {
d1c87cd4 316 (void)printf("%d", ret);
f4d5acdc 317 if (ret < 0 || ret > 9)
d1c87cd4 318 (void)printf("/%#x", ret);
f4d5acdc
MT
319 } else {
320 if (decimal)
d1c87cd4 321 (void)printf("%d", ret);
f4d5acdc 322 else
d1c87cd4 323 (void)printf("%#x", ret);
f4d5acdc 324 }
2bfb1112 325 } else if (error == ERESTART)
d1c87cd4 326 (void)printf("RESTART");
2bfb1112 327 else if (error == EJUSTRETURN)
d1c87cd4 328 (void)printf("JUSTRETURN");
2bfb1112 329 else {
d1c87cd4 330 (void)printf("-1 errno %d", ktr->ktr_error);
2bfb1112 331 if (fancy)
d1c87cd4 332 (void)printf(" %s", strerror(ktr->ktr_error));
f4d5acdc 333 }
d1c87cd4 334 (void)putchar('\n');
af3069b3
MT
335}
336
f4d5acdc
MT
337ktrnamei(cp, len)
338 char *cp;
339{
d1c87cd4 340 (void)printf("\"%.*s\"\n", len, cp);
b4a09a32
MT
341}
342
343ktrgenio(ktr, len)
344 struct ktr_genio *ktr;
345{
cf9db849
MT
346 register int datalen = len - sizeof (struct ktr_genio);
347 register char *dp = (char *)ktr + sizeof (struct ktr_genio);
348 register char *cp;
f4d5acdc 349 register int col = 0;
cf9db849 350 register width;
2bfb1112 351 char visbuf[5];
cf9db849
MT
352 static screenwidth = 0;
353
354 if (screenwidth == 0) {
355 struct winsize ws;
b4a09a32 356
cf9db849
MT
357 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
358 ws.ws_col > 8)
359 screenwidth = ws.ws_col;
360 else
361 screenwidth = 80;
362 }
f4d5acdc
MT
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)
366 datalen = maxdata;
d1c87cd4 367 (void)printf(" \"");
cf9db849
MT
368 col = 8;
369 for (;datalen > 0; datalen--, dp++) {
2bfb1112
MT
370 (void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
371 cp = visbuf;
cf9db849
MT
372 /*
373 * Keep track of printables and
374 * space chars (like fold(1)).
375 */
f4d5acdc 376 if (col == 0) {
d1c87cd4 377 (void)putchar('\t');
cf9db849 378 col = 8;
b4a09a32 379 }
cf9db849
MT
380 switch(*cp) {
381 case '\n':
382 col = 0;
d1c87cd4 383 (void)putchar('\n');
f4d5acdc 384 continue;
cf9db849
MT
385 case '\t':
386 width = 8 - (col&07);
387 break;
388 default:
389 width = strlen(cp);
f4d5acdc 390 }
cf9db849 391 if (col + width > (screenwidth-2)) {
d1c87cd4 392 (void)printf("\\\n\t");
cf9db849 393 col = 8;
f4d5acdc 394 }
cf9db849
MT
395 col += width;
396 do {
d1c87cd4 397 (void)putchar(*cp++);
cf9db849
MT
398 } while (*cp);
399 }
400 if (col == 0)
d1c87cd4
KB
401 (void)printf(" ");
402 (void)printf("\"\n");
cf9db849
MT
403}
404
cf9db849
MT
405char *signames[] = {
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 */
412};
413
d1c87cd4 414ktrpsig(psig)
cf9db849
MT
415 struct ktr_psig *psig;
416{
d1c87cd4 417 (void)printf("SIG%s ", signames[psig->signo]);
cf9db849 418 if (psig->action == SIG_DFL)
d1c87cd4
KB
419 (void)printf("SIG_DFL\n");
420 else
421 (void)printf("caught handler=0x%x mask=0x%x code=0x%x\n",
422 (u_int)psig->action, psig->mask, psig->code);
423}
424
8b7840bb
MT
425ktrcsw(cs)
426 struct ktr_csw *cs;
427{
428 (void)printf("%s %s\n", cs->out ? "stop" : "resume",
429 cs->user ? "user" : "kernel");
430}
431
d1c87cd4
KB
432usage()
433{
434 (void)fprintf(stderr,
435 "usage: kdump [-dnlRT] [-f trfile] [-m maxdata] [-t [cnis]]\n");
436 exit(1);
af3069b3 437}