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