]> git.saurik.com Git - apple/system_cmds.git/blob - kdump.tproj/kdump.c
system_cmds-279.1.tar.gz
[apple/system_cmds.git] / kdump.tproj / kdump.c
1 /*
2 * Copyright (c) 1999, 2000-2001 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*-
25 * Copyright (c) 1988, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57 #ifndef lint
58 static const char copyright[] =
59 "@(#) Copyright (c) 1988, 1993\n\
60 The Regents of the University of California. All rights reserved.\n";
61 #endif /* not lint */
62
63 #ifndef lint
64 #if 0
65 static char sccsid[] = "@(#)kdump.c 8.1 (Berkeley) 6/6/93";
66 #endif
67 static const char rcsid[] =
68 "$FreeBSD: src/usr.bin/kdump/kdump.c,v 1.17 1999/12/29 05:05:33 peter Exp $";
69 #endif /* not lint */
70
71 #define KERNEL
72 extern int errno;
73 #include <sys/errno.h>
74 #undef KERNEL
75 #include <sys/param.h>
76 #include <sys/errno.h>
77 #include <sys/time.h>
78 #include <sys/uio.h>
79 #include <sys/ktrace.h>
80 #include <sys/ioctl.h>
81 #include <sys/ptrace.h>
82 #include <err.h>
83 #include <locale.h>
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <string.h>
87 #include <unistd.h>
88 #include <vis.h>
89 #include "ktrace.h"
90
91 int timestamp, decimal, fancy = 1, tail, maxdata;
92 char *tracefile = DEF_TRACEFILE;
93 struct ktr_header ktr_header;
94
95 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
96
97 main(argc, argv)
98 int argc;
99 char *argv[];
100 {
101 int ch, ktrlen, size;
102 register void *m;
103 int trpoints = ALL_POINTS;
104
105 (void) setlocale(LC_CTYPE, "");
106
107 while ((ch = getopt(argc,argv,"f:dlm:nRTt:")) != -1)
108 switch((char)ch) {
109 case 'f':
110 tracefile = optarg;
111 break;
112 case 'd':
113 decimal = 1;
114 break;
115 case 'l':
116 tail = 1;
117 break;
118 case 'm':
119 maxdata = atoi(optarg);
120 break;
121 case 'n':
122 fancy = 0;
123 break;
124 case 'R':
125 timestamp = 2; /* relative timestamp */
126 break;
127 case 'T':
128 timestamp = 1;
129 break;
130 case 't':
131 trpoints = getpoints(optarg);
132 if (trpoints < 0)
133 errx(1, "unknown trace point in %s", optarg);
134 break;
135 default:
136 usage();
137 }
138
139 if (argc > optind)
140 usage();
141
142 m = (void *)malloc(size = 1025);
143 if (m == NULL)
144 errx(1, "%s", strerror(ENOMEM));
145 if (!freopen(tracefile, "r", stdin))
146 err(1, "%s", tracefile);
147 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
148 if (trpoints & (1<<ktr_header.ktr_type))
149 dumpheader(&ktr_header);
150 if ((ktrlen = ktr_header.ktr_len) < 0)
151 errx(1, "bogus length 0x%x", ktrlen);
152 if (ktrlen > size) {
153 m = (void *)realloc(m, ktrlen+1);
154 if (m == NULL)
155 errx(1, "%s", strerror(ENOMEM));
156 size = ktrlen;
157 }
158 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
159 errx(1, "data too short");
160 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
161 continue;
162 switch (ktr_header.ktr_type) {
163 case KTR_SYSCALL:
164 ktrsyscall((struct ktr_syscall *)m);
165 break;
166 case KTR_SYSRET:
167 ktrsysret((struct ktr_sysret *)m);
168 break;
169 case KTR_NAMEI:
170 ktrnamei(m, ktrlen);
171 break;
172 case KTR_GENIO:
173 ktrgenio((struct ktr_genio *)m, ktrlen);
174 break;
175 case KTR_PSIG:
176 ktrpsig((struct ktr_psig *)m);
177 break;
178 case KTR_CSW:
179 ktrcsw((struct ktr_csw *)m);
180 break;
181 case KTR_USER:
182 ktruser(ktrlen, m);
183 break;
184 }
185 if (tail)
186 (void)fflush(stdout);
187 }
188 }
189
190 fread_tail(buf, size, num)
191 char *buf;
192 int num, size;
193 {
194 int i;
195
196 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
197 (void)sleep(1);
198 clearerr(stdin);
199 }
200 return (i);
201 }
202
203 dumpheader(kth)
204 struct ktr_header *kth;
205 {
206 static char unknown[64];
207 static struct timeval prevtime, temp;
208 char *type;
209
210 switch (kth->ktr_type) {
211 case KTR_SYSCALL:
212 type = "CALL";
213 break;
214 case KTR_SYSRET:
215 type = "RET ";
216 break;
217 case KTR_NAMEI:
218 type = "NAMI";
219 break;
220 case KTR_GENIO:
221 type = "GIO ";
222 break;
223 case KTR_PSIG:
224 type = "PSIG";
225 break;
226 case KTR_CSW:
227 type = "CSW";
228 break;
229 case KTR_USER:
230 type = "USER";
231 break;
232 default:
233 (void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
234 type = unknown;
235 }
236
237 (void)printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
238 if (timestamp) {
239 if (timestamp == 2) {
240 temp = kth->ktr_time;
241 timevalsub(&kth->ktr_time, &prevtime);
242 prevtime = temp;
243 }
244 (void)printf("%ld.%06ld ",
245 kth->ktr_time.tv_sec, kth->ktr_time.tv_usec);
246 }
247 (void)printf("%s ", type);
248 }
249
250 #include <sys/syscall.h>
251 #include "syscalls.c"
252 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
253
254 static char *ptrace_ops[] = {
255 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U",
256 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE",
257 "PT_KILL", "PT_STEP", "PT_ATTACH", "PT_DETACH",
258 };
259
260 ktrsyscall(ktr)
261 register struct ktr_syscall *ktr;
262 {
263 register narg = ktr->ktr_narg;
264 register register_t *ip;
265 char *ioctlname();
266
267 if (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0)
268 (void)printf("[%d]", ktr->ktr_code);
269 else
270 (void)printf("%s", syscallnames[ktr->ktr_code]);
271 ip = &ktr->ktr_args[0];
272 if (narg) {
273 char c = '(';
274 if (fancy) {
275 if (ktr->ktr_code == SYS_ioctl) {
276 char *cp;
277 if (decimal)
278 (void)printf("(%ld", (long)*ip);
279 else
280 (void)printf("(%#lx", (long)*ip);
281 ip++;
282 narg--;
283 if ((cp = ioctlname(*ip)) != NULL)
284 (void)printf(",%s", cp);
285 else {
286 if (decimal)
287 (void)printf(",%ld", (long)*ip);
288 else
289 (void)printf(",%#lx ", (long)*ip);
290 }
291 c = ',';
292 ip++;
293 narg--;
294 } else if (ktr->ktr_code == SYS_ptrace) {
295 if (*ip < sizeof(ptrace_ops) /
296 sizeof(ptrace_ops[0]) && *ip >= 0)
297 (void)printf("(%s", ptrace_ops[*ip]);
298 #ifdef PT_GETREGS
299 else if (*ip == PT_GETREGS)
300 (void)printf("(%s", "PT_GETREGS");
301 #endif
302 #ifdef PT_SETREGS
303 else if (*ip == PT_SETREGS)
304 (void)printf("(%s", "PT_SETREGS");
305 #endif
306 #ifdef PT_GETFPREGS
307 else if (*ip == PT_GETFPREGS)
308 (void)printf("(%s", "PT_GETFPREGS");
309 #endif
310 #ifdef PT_SETFPREGS
311 else if (*ip == PT_SETFPREGS)
312 (void)printf("(%s", "PT_SETFPREGS");
313 #endif
314 #ifdef PT_GETDBREGS
315 else if (*ip == PT_GETDBREGS)
316 (void)printf("(%s", "PT_GETDBREGS");
317 #endif
318 #ifdef PT_SETDBREGS
319 else if (*ip == PT_SETDBREGS)
320 (void)printf("(%s", "PT_SETDBREGS");
321 #endif
322 else
323 (void)printf("(%ld", (long)*ip);
324 c = ',';
325 ip++;
326 narg--;
327 }
328 }
329 while (narg) {
330 if (decimal)
331 (void)printf("%c%ld", c, (long)*ip);
332 else
333 (void)printf("%c%#lx", c, (long)*ip);
334 c = ',';
335 ip++;
336 narg--;
337 }
338 (void)putchar(')');
339 }
340 (void)putchar('\n');
341 }
342
343 ktrsysret(ktr)
344 struct ktr_sysret *ktr;
345 {
346 register register_t ret = ktr->ktr_retval;
347 register int error = ktr->ktr_error;
348 register int code = ktr->ktr_code;
349
350 if (code >= nsyscalls || code < 0)
351 (void)printf("[%d] ", code);
352 else
353 (void)printf("%s ", syscallnames[code]);
354
355 if (error == 0) {
356 if (fancy) {
357 (void)printf("%d", ret);
358 if (ret < 0 || ret > 9)
359 (void)printf("/%#lx", (long)ret);
360 } else {
361 if (decimal)
362 (void)printf("%ld", (long)ret);
363 else
364 (void)printf("%#lx", (long)ret);
365 }
366 } else if (error == ERESTART)
367 (void)printf("RESTART");
368 else if (error == EJUSTRETURN)
369 (void)printf("JUSTRETURN");
370 else {
371 (void)printf("-1 errno %d", ktr->ktr_error);
372 if (fancy)
373 (void)printf(" %s", strerror(ktr->ktr_error));
374 }
375 (void)putchar('\n');
376 }
377
378 ktrnamei(cp, len)
379 char *cp;
380 {
381 (void)printf("\"%.*s\"\n", len, cp);
382 }
383
384 ktrgenio(ktr, len)
385 struct ktr_genio *ktr;
386 {
387 register int datalen = len - sizeof (struct ktr_genio);
388 register char *dp = (char *)ktr + sizeof (struct ktr_genio);
389 register char *cp;
390 register int col = 0;
391 register width;
392 char visbuf[5];
393 static screenwidth = 0;
394
395 if (screenwidth == 0) {
396 struct winsize ws;
397
398 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
399 ws.ws_col > 8)
400 screenwidth = ws.ws_col;
401 else
402 screenwidth = 80;
403 }
404 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
405 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
406 datalen == 1 ? "" : "s");
407 if (maxdata && datalen > maxdata)
408 datalen = maxdata;
409 (void)printf(" \"");
410 col = 8;
411 for (;datalen > 0; datalen--, dp++) {
412 (void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
413 cp = visbuf;
414 /*
415 * Keep track of printables and
416 * space chars (like fold(1)).
417 */
418 if (col == 0) {
419 (void)putchar('\t');
420 col = 8;
421 }
422 switch(*cp) {
423 case '\n':
424 col = 0;
425 (void)putchar('\n');
426 continue;
427 case '\t':
428 width = 8 - (col&07);
429 break;
430 default:
431 width = strlen(cp);
432 }
433 if (col + width > (screenwidth-2)) {
434 (void)printf("\\\n\t");
435 col = 8;
436 }
437 col += width;
438 do {
439 (void)putchar(*cp++);
440 } while (*cp);
441 }
442 if (col == 0)
443 (void)printf(" ");
444 (void)printf("\"\n");
445 }
446
447 char *signames[] = {
448 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
449 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
450 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
451 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
452 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
453 "USR2", NULL, /* 31 - 32 */
454 };
455
456 ktrpsig(psig)
457 struct ktr_psig *psig;
458 {
459 (void)printf("SIG%s ", signames[psig->signo]);
460 if (psig->action == SIG_DFL)
461 (void)printf("SIG_DFL\n");
462 else
463 (void)printf("caught handler=0x%lx mask=0x%x code=0x%x\n",
464 (u_long)psig->action, psig->mask, psig->code);
465 }
466
467 ktrcsw(cs)
468 struct ktr_csw *cs;
469 {
470 (void)printf("%s %s\n", cs->out ? "stop" : "resume",
471 cs->user ? "user" : "kernel");
472 }
473
474 ktruser(len, p)
475 int len;
476 unsigned char *p;
477 {
478 (void)printf("%d ", len);
479 while (len--)
480 (void)printf(" %02x", *p++);
481 (void)printf("\n");
482
483 }
484
485 usage()
486 {
487 (void)fprintf(stderr,
488 "usage: kdump [-dnlRT] [-f trfile] [-m maxdata] [-t [cnisuw]]\n");
489 exit(1);
490 }