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
33 * @(#) Copyright (c) 1988, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)kdump.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.bin/kdump/kdump.c,v 1.29 2006/05/20 14:27:22 netchild Exp $
36 * $DragonFly: src/usr.bin/kdump/kdump.c,v 1.10 2008/01/07 01:35:43 corecode Exp $
39 #define _KERNEL_STRUCTURES
41 #include <sys/errno.h>
42 #include <sys/param.h>
43 #include <sys/errno.h>
46 #include <sys/ktrace.h>
47 #include <sys/ioctl.h>
48 #include <sys/ptrace.h>
57 #include "kdump_subr.h"
59 int timestamp
, decimal
, fancy
= 1, tail
, maxdata
= 64;
61 char *tracefile
= DEF_TRACEFILE
;
62 struct ktr_header ktr_header
;
64 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
66 main(int argc
, char **argv
)
68 int ch
, col
, ktrlen
, size
;
71 int trpoints
= ALL_POINTS
;
74 (void) setlocale(LC_CTYPE
, "");
76 while ((ch
= getopt(argc
,argv
,"f:djlm:np:RTt:")) != -1)
91 maxdata
= atoi(optarg
);
97 do_pid
= strtoul(optarg
, &cp
, 0);
99 errx(1,"invalid number %s", 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 (do_pid
== -1 || ktr_header
.ktr_pid
== do_pid
))
127 col
= dumpheader(&ktr_header
);
130 if ((ktrlen
= ktr_header
.ktr_len
) < 0)
131 errx(1, "bogus length 0x%x", ktrlen
);
133 m
= (void *)realloc(m
, ktrlen
+1);
135 errx(1, "%s", strerror(ENOMEM
));
138 if (ktrlen
&& fread_tail(m
, ktrlen
, 1) == 0)
139 errx(1, "data too short");
140 if ((trpoints
& (1<<ktr_header
.ktr_type
)) == 0)
144 switch (ktr_header
.ktr_type
) {
146 ktrsyscall((struct ktr_syscall
*)m
);
149 ktrsysret((struct ktr_sysret
*)m
);
155 ktrgenio((struct ktr_genio
*)m
, ktrlen
);
158 ktrpsig((struct ktr_psig
*)m
);
161 ktrcsw((struct ktr_csw
*)m
);
168 (void)fflush(stdout
);
172 fread_tail(char *buf
, int size
, int num
)
176 while ((i
= fread(buf
, size
, num
, stdin
)) == 0 && tail
) {
183 dumpheader(struct ktr_header
*kth
)
185 static char unknown
[64];
186 static struct timeval prevtime
, temp
;
190 switch (kth
->ktr_type
) {
213 (void)sprintf(unknown
, "UNKNOWN(%d)", kth
->ktr_type
);
217 if (kth
->ktr_tid
|| (kth
->ktr_flags
& KTRH_THREADED
) || fixedformat
)
218 col
= printf("%5d:%-4d", kth
->ktr_pid
, kth
->ktr_tid
);
220 col
= printf("%5d", kth
->ktr_pid
);
221 col
+= printf(" %-8.*s ", MAXCOMLEN
, kth
->ktr_comm
);
223 if (timestamp
== 2) {
224 temp
= kth
->ktr_time
;
225 timevalsub(&kth
->ktr_time
, &prevtime
);
228 col
+= printf("%ld.%06ld ",
229 kth
->ktr_time
.tv_sec
, kth
->ktr_time
.tv_usec
);
231 col
+= printf("%s ", type
);
235 #include <sys/syscall.h>
237 #include <sys/kern/syscalls.c>
239 int nsyscalls
= sizeof (syscallnames
) / sizeof (syscallnames
[0]);
241 static char *ptrace_ops
[] = {
242 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U",
243 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE",
244 "PT_KILL", "PT_STEP", "PT_ATTACH", "PT_DETACH",
247 ktrsyscall(register struct ktr_syscall
*ktr
)
249 register narg
= ktr
->ktr_narg
;
250 register register_t
*ip
;
253 if (ktr
->ktr_code
>= nsyscalls
|| ktr
->ktr_code
< 0)
254 (void)printf("[%d]", ktr
->ktr_code
);
256 (void)printf("%s", syscallnames
[ktr
->ktr_code
]);
257 ip
= &ktr
->ktr_args
[0];
262 #define print_number(i,n,c) do { \
264 (void)printf("%c%ld", c, (long)*i); \
266 (void)printf("%c%#lx", c, (long)*i); \
272 if (ktr
->ktr_code
== SYS_ioctl
) {
274 print_number(ip
,narg
,c
);
275 if ((cp
= ioctlname(*ip
)) != NULL
)
276 (void)printf(",%s", cp
);
279 (void)printf(",%ld", (long)*ip
);
281 (void)printf(",%#lx ", (long)*ip
);
286 } else if (ktr
->ktr_code
== SYS_access
) {
287 print_number(ip
,narg
,c
);
289 accessmodename ((int)*ip
);
292 } else if (ktr
->ktr_code
== SYS_open
) {
295 print_number(ip
,narg
,c
);
299 flagsandmodename (flags
, mode
, decimal
);
302 } else if (ktr
->ktr_code
== SYS_wait4
) {
303 print_number(ip
,narg
,c
);
304 print_number(ip
,narg
,c
);
306 wait4optname ((int)*ip
);
309 } else if (ktr
->ktr_code
== SYS_chmod
||
310 ktr
->ktr_code
== SYS_fchmod
||
311 ktr
->ktr_code
== SYS_lchmod
) {
312 print_number(ip
,narg
,c
);
317 } else if (ktr
->ktr_code
== SYS_mknod
) {
318 print_number(ip
,narg
,c
);
323 } else if (ktr
->ktr_code
== SYS_getfsstat
) {
324 print_number(ip
,narg
,c
);
325 print_number(ip
,narg
,c
);
327 getfsstatflagsname ((int)*ip
);
330 } else if (ktr
->ktr_code
== SYS_mount
) {
331 print_number(ip
,narg
,c
);
332 print_number(ip
,narg
,c
);
334 mountflagsname ((int)*ip
);
337 } else if (ktr
->ktr_code
== SYS_unmount
) {
338 print_number(ip
,narg
,c
);
340 mountflagsname ((int)*ip
);
343 } else if (ktr
->ktr_code
== SYS_recvmsg
||
344 ktr
->ktr_code
== SYS_sendmsg
) {
345 print_number(ip
,narg
,c
);
346 print_number(ip
,narg
,c
);
348 sendrecvflagsname ((int)*ip
);
351 } else if (ktr
->ktr_code
== SYS_recvfrom
||
352 ktr
->ktr_code
== SYS_sendto
) {
353 print_number(ip
,narg
,c
);
354 print_number(ip
,narg
,c
);
355 print_number(ip
,narg
,c
);
357 sendrecvflagsname ((int)*ip
);
360 } else if (ktr
->ktr_code
== SYS_chflags
||
361 ktr
->ktr_code
== SYS_fchflags
) {
362 print_number(ip
,narg
,c
);
367 } else if (ktr
->ktr_code
== SYS_kill
) {
368 print_number(ip
,narg
,c
);
373 } else if (ktr
->ktr_code
== SYS_reboot
) {
375 rebootoptname((int)*ip
);
378 } else if (ktr
->ktr_code
== SYS_umask
) {
383 } else if (ktr
->ktr_code
== SYS_msync
) {
384 print_number(ip
,narg
,c
);
385 print_number(ip
,narg
,c
);
387 msyncflagsname((int)*ip
);
390 } else if (ktr
->ktr_code
== SYS_mmap
) {
391 print_number(ip
,narg
,c
);
392 print_number(ip
,narg
,c
);
394 mmapprotname ((int)*ip
);
398 mmapflagsname ((int)*ip
);
401 } else if (ktr
->ktr_code
== SYS_mprotect
) {
402 print_number(ip
,narg
,c
);
403 print_number(ip
,narg
,c
);
405 mmapprotname ((int)*ip
);
408 } else if (ktr
->ktr_code
== SYS_madvise
) {
409 print_number(ip
,narg
,c
);
410 print_number(ip
,narg
,c
);
412 madvisebehavname((int)*ip
);
415 } else if (ktr
->ktr_code
== SYS_setpriority
) {
416 print_number(ip
,narg
,c
);
417 print_number(ip
,narg
,c
);
422 } else if (ktr
->ktr_code
== SYS_fcntl
) {
425 print_number(ip
,narg
,c
);
429 fcntlcmdname(cmd
, arg
, decimal
);
432 } else if (ktr
->ktr_code
== SYS_socket
) {
434 sockdomainname((int)*ip
);
438 socktypename((int)*ip
);
442 } else if (ktr
->ktr_code
== SYS_setsockopt
||
443 ktr
->ktr_code
== SYS_getsockopt
) {
444 print_number(ip
,narg
,c
);
446 sockoptlevelname((int)*ip
, decimal
);
450 sockoptname((int)*ip
);
453 } else if (ktr
->ktr_code
== SYS_lseek
) {
454 print_number(ip
,narg
,c
);
455 /* Hidden 'pad' argument, not in lseek(2) */
456 print_number(ip
,narg
,c
);
457 print_number(ip
,narg
,c
);
459 whencename ((int)*ip
);
462 } else if (ktr
->ktr_code
== SYS_flock
) {
463 print_number(ip
,narg
,c
);
468 } else if (ktr
->ktr_code
== SYS_mkfifo
||
469 ktr
->ktr_code
== SYS_mkdir
) {
470 print_number(ip
,narg
,c
);
475 } else if (ktr
->ktr_code
== SYS_shutdown
) {
476 print_number(ip
,narg
,c
);
478 shutdownhowname((int)*ip
);
481 } else if (ktr
->ktr_code
== SYS_socketpair
) {
483 sockdomainname((int)*ip
);
487 socktypename((int)*ip
);
491 } else if (ktr
->ktr_code
== SYS_getrlimit
||
492 ktr
->ktr_code
== SYS_setrlimit
) {
494 rlimitname((int)*ip
);
498 } else if (ktr
->ktr_code
== SYS_quotactl
) {
499 print_number(ip
,narg
,c
);
500 quotactlname((int)*ip
);
504 } else if (ktr
->ktr_code
== SYS_rtprio
) {
506 rtprioname((int)*ip
);
510 } else if (ktr
->ktr_code
== SYS___semctl
) {
511 print_number(ip
,narg
,c
);
512 print_number(ip
,narg
,c
);
513 semctlname((int)*ip
);
516 } else if (ktr
->ktr_code
== SYS_semget
) {
517 print_number(ip
,narg
,c
);
518 print_number(ip
,narg
,c
);
519 semgetname((int)*ip
);
522 } else if (ktr
->ktr_code
== SYS_msgctl
) {
523 print_number(ip
,narg
,c
);
524 shmctlname((int)*ip
);
527 } else if (ktr
->ktr_code
== SYS_shmat
) {
528 print_number(ip
,narg
,c
);
529 print_number(ip
,narg
,c
);
533 } else if (ktr
->ktr_code
== SYS_shmctl
) {
534 print_number(ip
,narg
,c
);
535 shmctlname((int)*ip
);
538 } else if (ktr
->ktr_code
== SYS_minherit
) {
539 print_number(ip
,narg
,c
);
540 print_number(ip
,narg
,c
);
541 minheritname((int)*ip
);
544 } else if (ktr
->ktr_code
== SYS_rfork
) {
550 } else if (ktr
->ktr_code
== SYS_lio_listio
) {
552 lio_listioname((int)*ip
);
556 } else if (ktr
->ktr_code
== SYS_mlockall
) {
558 mlockallname((int)*ip
);
561 } else if (ktr
->ktr_code
== SYS_sched_setscheduler
) {
562 print_number(ip
,narg
,c
);
563 schedpolicyname((int)*ip
);
566 } else if (ktr
->ktr_code
== SYS_sched_get_priority_max
||
567 ktr
->ktr_code
== SYS_sched_get_priority_min
) {
569 schedpolicyname((int)*ip
);
572 } else if (ktr
->ktr_code
== SYS_sendfile
) {
573 print_number(ip
,narg
,c
);
574 print_number(ip
,narg
,c
);
575 print_number(ip
,narg
,c
);
576 print_number(ip
,narg
,c
);
577 print_number(ip
,narg
,c
);
578 print_number(ip
,narg
,c
);
579 sendfileflagsname((int)*ip
);
582 } else if (ktr
->ktr_code
== SYS_kldsym
) {
583 print_number(ip
,narg
,c
);
584 kldsymcmdname((int)*ip
);
587 } else if (ktr
->ktr_code
== SYS_sigprocmask
) {
589 sigprocmaskhowname((int)*ip
);
593 } else if (ktr
->ktr_code
== SYS___acl_get_file
||
594 ktr
->ktr_code
== SYS___acl_set_file
||
595 ktr
->ktr_code
== SYS___acl_get_fd
||
596 ktr
->ktr_code
== SYS___acl_set_fd
||
597 ktr
->ktr_code
== SYS___acl_delete_file
||
598 ktr
->ktr_code
== SYS___acl_delete_fd
||
599 ktr
->ktr_code
== SYS___acl_aclcheck_file
||
600 ktr
->ktr_code
== SYS___acl_aclcheck_fd
) {
601 print_number(ip
,narg
,c
);
602 acltypename((int)*ip
);
605 } else if (ktr
->ktr_code
== SYS_sigaction
) {
611 } else if (ktr
->ktr_code
== SYS_extattrctl
) {
612 print_number(ip
,narg
,c
);
613 extattrctlname((int)*ip
);
616 } else if (ktr
->ktr_code
== SYS_ptrace
) {
617 if (*ip
< sizeof(ptrace_ops
) /
618 sizeof(ptrace_ops
[0]) && *ip
>= 0)
619 (void)printf("(%s", ptrace_ops
[*ip
]);
621 else if (*ip
== PT_GETREGS
)
622 (void)printf("(%s", "PT_GETREGS");
625 else if (*ip
== PT_SETREGS
)
626 (void)printf("(%s", "PT_SETREGS");
629 else if (*ip
== PT_GETFPREGS
)
630 (void)printf("(%s", "PT_GETFPREGS");
633 else if (*ip
== PT_SETFPREGS
)
634 (void)printf("(%s", "PT_SETFPREGS");
637 else if (*ip
== PT_GETDBREGS
)
638 (void)printf("(%s", "PT_GETDBREGS");
641 else if (*ip
== PT_SETDBREGS
)
642 (void)printf("(%s", "PT_SETDBREGS");
645 (void)printf("(%ld", (long)*ip
);
652 print_number(ip
,narg
,c
);
659 ktrsysret(struct ktr_sysret
*ktr
)
661 register register_t ret
= ktr
->ktr_retval
;
662 register int error
= ktr
->ktr_error
;
663 register int code
= ktr
->ktr_code
;
665 if (code
>= nsyscalls
|| code
< 0)
666 (void)printf("[%d] ", code
);
668 (void)printf("%s ", syscallnames
[code
]);
672 (void)printf("%d", ret
);
673 if (ret
< 0 || ret
> 9)
674 (void)printf("/%#lx", (long)ret
);
677 (void)printf("%ld", (long)ret
);
679 (void)printf("%#lx", (long)ret
);
681 } else if (error
== ERESTART
)
682 (void)printf("RESTART");
683 else if (error
== EJUSTRETURN
)
684 (void)printf("JUSTRETURN");
686 (void)printf("-1 errno %d", ktr
->ktr_error
);
688 (void)printf(" %s", strerror(ktr
->ktr_error
));
693 ktrnamei(char *cp
, int len
)
695 (void)printf("\"%.*s\"\n", len
, cp
);
698 ktrgenio(struct ktr_genio
*ktr
, int len
)
700 register int datalen
= len
- sizeof (struct ktr_genio
);
701 register char *dp
= (char *)ktr
+ sizeof (struct ktr_genio
);
703 register int col
= 0;
706 static screenwidth
= 0;
708 if (screenwidth
== 0) {
711 if (fancy
&& ioctl(fileno(stderr
), TIOCGWINSZ
, &ws
) != -1 &&
713 screenwidth
= ws
.ws_col
;
717 printf("fd %d %s %d byte%s\n", ktr
->ktr_fd
,
718 ktr
->ktr_rw
== UIO_READ
? "read" : "wrote", datalen
,
719 datalen
== 1 ? "" : "s");
720 if (maxdata
&& datalen
> maxdata
)
724 for (;datalen
> 0; datalen
--, dp
++) {
725 (void) vis(visbuf
, *dp
, VIS_CSTYLE
, *(dp
+1));
728 * Keep track of printables and
729 * space chars (like fold(1)).
741 width
= 8 - (col
&07);
746 if (col
+ width
> (screenwidth
-2)) {
747 (void)printf("\\\n\t");
752 (void)putchar(*cp
++);
757 (void)printf("\"\n");
760 char *signames
[NSIG
] = {
761 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
762 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
763 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
764 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
765 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
766 "USR2", NULL
, /* 31 - 32 */
769 ktrpsig(struct ktr_psig
*psig
)
771 (void)printf("SIG%s ", signames
[psig
->signo
]);
772 if (psig
->action
== SIG_DFL
)
773 (void)printf("SIG_DFL\n");
775 (void)printf("caught handler=0x%lx mask=0x%x code=0x%x\n",
776 (u_long
)psig
->action
, psig
->mask
, psig
->code
);
779 ktrcsw(struct ktr_csw
*cs
)
781 (void)printf("%s %s\n", cs
->out
? "stop" : "resume",
782 cs
->user
? "user" : "kernel");
785 ktruser(int len
, unsigned char *p
)
787 (void)printf("%d ", len
);
789 (void)printf(" %02x", *p
++);
796 (void)fprintf(stderr
,
797 "usage: kdump [-dnlRT] [-f trfile] [-m maxdata] [-t [cnisuw]] [-p pid]\n");
801 timevalsub(struct timeval
*t1
, struct timeval
*t2
)
803 t1
->tv_sec
-= t2
->tv_sec
;
804 t1
->tv_usec
-= t2
->tv_usec
;
808 timevalfix(struct timeval
*t1
)
810 if (t1
->tv_usec
< 0) {
812 t1
->tv_usec
+= 1000000;
814 if (t1
->tv_usec
>= 1000000) {
816 t1
->tv_usec
-= 1000000;