]>
git.saurik.com Git - apple/system_cmds.git/blob - kdump.tproj/kdump.c
2 * Copyright (c) 1999, 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1988, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
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.
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
56 #include <sys/cdefs.h>
59 __unused
static const char copyright
[] =
60 "@(#) Copyright (c) 1988, 1993\n\
61 The Regents of the University of California. All rights reserved.\n";
66 __unused
static char sccsid
[] = "@(#)kdump.c 8.1 (Berkeley) 6/6/93";
68 __unused
static const char rcsid
[] =
69 "$FreeBSD: src/usr.bin/kdump/kdump.c,v 1.17 1999/12/29 05:05:33 peter Exp $";
73 #include <sys/param.h>
74 #include <sys/errno.h>
77 #include <sys/ktrace.h>
78 #include <sys/ioctl.h>
79 #include <sys/ptrace.h>
90 #define EJUSTRETURN -2
91 int timestamp
, decimal
, fancy
= 1, tail
, maxdata
;
92 char *tracefile
= DEF_TRACEFILE
;
93 struct ktr_header ktr_header
;
95 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
97 /* Forward declarations */
99 int fread_tail(char *buf
, int size
, int num
);
100 void dumpheader(struct ktr_header
*kth
);
101 void dumpheader(struct ktr_header
*kth
);
102 void ktrsyscall(struct ktr_syscall
*ktr
);
103 void ktrsysret(struct ktr_sysret
*ktr
);
104 void ktrnamei(char *cp
, int len
);
105 void ktrgenio(struct ktr_genio
*ktr
, int len
);
106 void ktrpsig(struct ktr_psig
*psig
);
107 void ktrcsw(struct ktr_csw
*cs
);
108 void ktruser(int len
, unsigned char *p
);
111 main(int argc
, char *argv
[])
113 int ch
, ktrlen
, size
;
115 int trpoints
= ALL_POINTS
;
117 (void) setlocale(LC_CTYPE
, "");
119 while ((ch
= getopt(argc
,argv
,"f:dlm:nRTt:")) != -1)
131 maxdata
= atoi(optarg
);
137 timestamp
= 2; /* relative timestamp */
143 trpoints
= getpoints(optarg
);
145 errx(1, "unknown trace point in %s", optarg
);
154 m
= (void *)malloc(size
= 1025);
156 errx(1, "%s", strerror(ENOMEM
));
157 if (!freopen(tracefile
, "r", stdin
))
158 err(1, "%s", tracefile
);
159 while (fread_tail((char *)&ktr_header
, sizeof(struct ktr_header
), 1)) {
160 if (trpoints
& (1<<ktr_header
.ktr_type
))
161 dumpheader(&ktr_header
);
162 if ((ktrlen
= ktr_header
.ktr_len
) < 0)
163 errx(1, "bogus length 0x%x", ktrlen
);
165 m
= (void *)realloc(m
, ktrlen
+1);
167 errx(1, "%s", strerror(ENOMEM
));
170 if (ktrlen
&& fread_tail(m
, ktrlen
, 1) == 0)
171 errx(1, "data too short");
172 if ((trpoints
& (1<<ktr_header
.ktr_type
)) == 0)
174 switch (ktr_header
.ktr_type
) {
176 ktrsyscall((struct ktr_syscall
*)m
);
179 ktrsysret((struct ktr_sysret
*)m
);
185 ktrgenio((struct ktr_genio
*)m
, ktrlen
);
188 ktrpsig((struct ktr_psig
*)m
);
191 ktrcsw((struct ktr_csw
*)m
);
198 (void)fflush(stdout
);
205 fread_tail(char *buf
, int size
, int num
)
209 while ((i
= fread(buf
, size
, num
, stdin
)) == 0 && tail
) {
217 dumpheader(struct ktr_header
*kth
)
219 static char unknown
[64];
220 static struct timeval prevtime
, temp
;
223 switch (kth
->ktr_type
) {
246 (void)sprintf(unknown
, "UNKNOWN(%d)", kth
->ktr_type
);
250 (void)printf("%6d %-8.*s ", kth
->ktr_pid
, MAXCOMLEN
, kth
->ktr_comm
);
252 if (timestamp
== 2) {
253 temp
= kth
->ktr_time
;
254 timevalsub(&kth
->ktr_time
, &prevtime
);
257 (void)printf("%ld.%06d ",
258 kth
->ktr_time
.tv_sec
, kth
->ktr_time
.tv_usec
);
260 (void)printf("%s ", type
);
263 #include <sys/syscall.h>
264 #include "syscalls.c"
265 int nsyscalls
= sizeof (syscallnames
) / sizeof (syscallnames
[0]);
267 static char *ptrace_ops
[] = {
268 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U",
269 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE",
270 "PT_KILL", "PT_STEP", "PT_ATTACH", "PT_DETACH",
271 "PT_SIGEXC", "PT_THUPDATE", "PT_ATTACHEXC",
275 ktrsyscall(struct ktr_syscall
*ktr
)
277 register_t narg
= ktr
->ktr_narg
;
278 unsigned long long *ip
;
281 if (ktr
->ktr_code
>= nsyscalls
|| ktr
->ktr_code
< 0)
282 (void)printf("[%d]", ktr
->ktr_code
);
284 (void)printf("%s", syscallnames
[ktr
->ktr_code
]);
285 ip
= &ktr
->ktr_args
[0];
289 if (ktr
->ktr_code
== SYS_ioctl
) {
292 (void)printf("(%ld", (long)*ip
);
294 (void)printf("(%#lx", (long)*ip
);
297 if ((cp
= ioctlname(*ip
)) != NULL
)
298 (void)printf(",%s", cp
);
301 (void)printf(",%ld", (long)*ip
);
303 (void)printf(",%#lx ", (long)*ip
);
308 } else if (ktr
->ktr_code
== SYS_ptrace
) {
309 if (*ip
< sizeof(ptrace_ops
) /
310 sizeof(ptrace_ops
[0]) && *ip
>= 0)
311 (void)printf("(%s", ptrace_ops
[*ip
]);
313 else if (*ip
== PT_FORCEQUOTA
)
314 (void)printf("(%s", "PT_FORCEQUOTA");
316 #ifdef PT_DENY_ATTACH
317 else if (*ip
== PT_DENY_ATTACH
)
318 (void)printf("(%s", "PT_DENY_ATTACH");
321 else if (*ip
== PT_FIRSTMACH
)
322 (void)printf("(%s", "PT_FIRSTMACH");
325 (void)printf("(%ld", (long)*ip
);
333 (void)printf("%c%ld", c
, (long)*ip
);
335 (void)printf("%c%#lx", c
, (long)*ip
);
346 ktrsysret(struct ktr_sysret
*ktr
)
348 register_t ret
= ktr
->ktr_retval
;
349 int error
= ktr
->ktr_error
;
350 int code
= ktr
->ktr_code
;
352 if (code
>= nsyscalls
|| code
< 0)
353 (void)printf("[%d] ", code
);
355 (void)printf("%s ", syscallnames
[code
]);
359 (void)printf("%d", ret
);
360 if (ret
< 0 || ret
> 9)
361 (void)printf("/%#lx", (long)ret
);
364 (void)printf("%ld", (long)ret
);
366 (void)printf("%#lx", (long)ret
);
368 } else if (error
== ERESTART
)
369 (void)printf("RESTART");
370 else if (error
== EJUSTRETURN
)
371 (void)printf("JUSTRETURN");
373 (void)printf("-1 errno %d", ktr
->ktr_error
);
375 (void)printf(" %s", strerror(ktr
->ktr_error
));
381 ktrnamei(char *cp
, int len
)
383 (void)printf("\"%.*s\"\n", len
, cp
);
387 ktrgenio(struct ktr_genio
*ktr
, int len
)
389 int datalen
= len
- sizeof (struct ktr_genio
);
390 char *dp
= (char *)ktr
+ sizeof (struct ktr_genio
);
395 static int screenwidth
= 0;
397 if (screenwidth
== 0) {
400 if (fancy
&& ioctl(fileno(stderr
), TIOCGWINSZ
, &ws
) != -1 &&
402 screenwidth
= ws
.ws_col
;
406 printf("fd %d %s %d byte%s\n", ktr
->ktr_fd
,
407 ktr
->ktr_rw
== UIO_READ
? "read" : "wrote", datalen
,
408 datalen
== 1 ? "" : "s");
409 if (maxdata
&& datalen
> maxdata
)
413 for (;datalen
> 0; datalen
--, dp
++) {
414 (void) vis(visbuf
, *dp
, VIS_CSTYLE
, *(dp
+1));
417 * Keep track of printables and
418 * space chars (like fold(1)).
430 width
= 8 - (col
&07);
435 if (col
+ width
> (screenwidth
-2)) {
436 (void)printf("\\\n\t");
441 (void)putchar(*cp
++);
446 (void)printf("\"\n");
450 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", /* 1 - 6 */
451 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
452 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
453 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
454 "XFSZ", "VTALRM", "PROF", "WINCH", "INFO", "USR1", /* 25 - 30 */
455 "USR2", NULL
, /* 31 - 32 */
459 ktrpsig(struct ktr_psig
*psig
)
461 (void)printf("SIG%s ", signames
[psig
->signo
]);
462 if (psig
->action
== SIG_DFL
)
463 (void)printf("SIG_DFL\n");
465 (void)printf("caught handler=0x%lx mask=0x%x code=0x%x\n",
466 (u_long
)psig
->action
, psig
->mask
, psig
->code
);
470 ktrcsw(struct ktr_csw
*cs
)
472 (void)printf("%s %s\n", cs
->out
? "stop" : "resume",
473 cs
->user
? "user" : "kernel");
477 ktruser(int len
, unsigned char *p
)
479 (void)printf("%d ", len
);
481 (void)printf(" %02x", *p
++);
489 (void)fprintf(stderr
,
490 "usage: kdump [-dnlRT] [-f trfile] [-m maxdata] [-t [cnisuw]]\n");