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.13 2008/10/16 01:52:32 swildner 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 extern const char *ioctlname(u_long
);
61 static int dumpheader(struct ktr_header
*);
62 static int fread_tail(void *, int, int);
63 static void ktrcsw(struct ktr_csw
*);
64 static void ktrgenio(struct ktr_genio
*, int);
65 static void ktrnamei(char *, int);
66 static void ktrpsig(struct ktr_psig
*);
67 static void ktrsyscall(struct ktr_syscall
*);
68 static void ktrsysret(struct ktr_sysret
*);
69 static void ktruser(int, unsigned char *);
70 static void ktruser_malloc(int, unsigned char *);
71 static void timevalfix(struct timeval
*);
72 static void timevalsub(struct timeval
*, struct timeval
*);
73 static void usage(void);
75 int timestamp
, decimal
, fancy
= 1, tail
, maxdata
= 64;
77 const char *tracefile
= DEF_TRACEFILE
;
78 struct ktr_header ktr_header
;
80 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
83 main(int argc
, char **argv
)
85 int ch
, col
, ktrlen
, size
;
88 int trpoints
= ALL_POINTS
;
91 (void) setlocale(LC_CTYPE
, "");
93 while ((ch
= getopt(argc
,argv
,"f:djlm:np:RTt:")) != -1)
108 maxdata
= atoi(optarg
);
114 do_pid
= strtoul(optarg
, &cp
, 0);
116 errx(1,"invalid number %s", optarg
);
119 timestamp
= 2; /* relative timestamp */
125 trpoints
= getpoints(optarg
);
127 errx(1, "unknown trace point in %s", optarg
);
136 m
= (void *)malloc(size
= 1025);
138 errx(1, "%s", strerror(ENOMEM
));
139 if (!freopen(tracefile
, "r", stdin
))
140 err(1, "%s", tracefile
);
141 while (fread_tail(&ktr_header
, sizeof(struct ktr_header
), 1)) {
142 if (trpoints
& (1 << ktr_header
.ktr_type
) &&
143 (do_pid
== -1 || ktr_header
.ktr_pid
== do_pid
))
144 col
= dumpheader(&ktr_header
);
147 if ((ktrlen
= ktr_header
.ktr_len
) < 0)
148 errx(1, "bogus length 0x%x", ktrlen
);
150 m
= (void *)realloc(m
, ktrlen
+1);
152 errx(1, "%s", strerror(ENOMEM
));
155 if (ktrlen
&& fread_tail(m
, ktrlen
, 1) == 0)
156 errx(1, "data too short");
157 if ((trpoints
& (1<<ktr_header
.ktr_type
)) == 0)
161 switch (ktr_header
.ktr_type
) {
163 ktrsyscall((struct ktr_syscall
*)m
);
166 ktrsysret((struct ktr_sysret
*)m
);
172 ktrgenio((struct ktr_genio
*)m
, ktrlen
);
175 ktrpsig((struct ktr_psig
*)m
);
178 ktrcsw((struct ktr_csw
*)m
);
185 (void)fflush(stdout
);
191 fread_tail(void *buf
, int size
, int num
)
195 while ((i
= fread(buf
, size
, num
, stdin
)) == 0 && tail
) {
203 dumpheader(struct ktr_header
*kth
)
205 static char unknown
[64];
206 static struct timeval prevtime
, temp
;
210 switch (kth
->ktr_type
) {
233 (void)sprintf(unknown
, "UNKNOWN(%d)", kth
->ktr_type
);
237 if (kth
->ktr_tid
|| (kth
->ktr_flags
& KTRH_THREADED
) || fixedformat
)
238 col
= printf("%5d:%-4d", kth
->ktr_pid
, kth
->ktr_tid
);
240 col
= printf("%5d", kth
->ktr_pid
);
241 col
+= printf(" %-8.*s ", MAXCOMLEN
, kth
->ktr_comm
);
243 if (timestamp
== 2) {
244 temp
= kth
->ktr_time
;
245 timevalsub(&kth
->ktr_time
, &prevtime
);
248 col
+= printf("%ld.%06ld ",
249 kth
->ktr_time
.tv_sec
, kth
->ktr_time
.tv_usec
);
251 col
+= printf("%s ", type
);
255 #include <sys/syscall.h>
257 #include <sys/kern/syscalls.c>
259 int nsyscalls
= sizeof (syscallnames
) / sizeof (syscallnames
[0]);
261 static const char *ptrace_ops
[] = {
262 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U",
263 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE",
264 "PT_KILL", "PT_STEP", "PT_ATTACH", "PT_DETACH",
268 ktrsyscall(struct ktr_syscall
*ktr
)
270 int narg
= ktr
->ktr_narg
;
273 if (ktr
->ktr_code
>= nsyscalls
|| ktr
->ktr_code
< 0)
274 (void)printf("[%d]", ktr
->ktr_code
);
276 (void)printf("%s", syscallnames
[ktr
->ktr_code
]);
277 ip
= &ktr
->ktr_args
[0];
282 #define print_number(i,n,c) do { \
284 (void)printf("%c%ld", c, (long)*i); \
286 (void)printf("%c%#lx", c, (long)*i); \
292 if (ktr
->ktr_code
== SYS_ioctl
) {
294 print_number(ip
,narg
,c
);
295 if ((cp
= ioctlname(*ip
)) != NULL
)
296 (void)printf(",%s", cp
);
299 (void)printf(",%ld", (long)*ip
);
301 (void)printf(",%#lx ", (long)*ip
);
306 } else if (ktr
->ktr_code
== SYS_access
) {
307 print_number(ip
,narg
,c
);
309 accessmodename ((int)*ip
);
312 } else if (ktr
->ktr_code
== SYS_open
) {
315 print_number(ip
,narg
,c
);
319 flagsandmodename (flags
, mode
, decimal
);
322 } else if (ktr
->ktr_code
== SYS_wait4
) {
323 print_number(ip
,narg
,c
);
324 print_number(ip
,narg
,c
);
326 wait4optname ((int)*ip
);
329 } else if (ktr
->ktr_code
== SYS_chmod
||
330 ktr
->ktr_code
== SYS_fchmod
||
331 ktr
->ktr_code
== SYS_lchmod
) {
332 print_number(ip
,narg
,c
);
337 } else if (ktr
->ktr_code
== SYS_mknod
) {
338 print_number(ip
,narg
,c
);
343 } else if (ktr
->ktr_code
== SYS_getfsstat
) {
344 print_number(ip
,narg
,c
);
345 print_number(ip
,narg
,c
);
347 getfsstatflagsname ((int)*ip
);
350 } else if (ktr
->ktr_code
== SYS_mount
) {
351 print_number(ip
,narg
,c
);
352 print_number(ip
,narg
,c
);
354 mountflagsname ((int)*ip
);
357 } else if (ktr
->ktr_code
== SYS_unmount
) {
358 print_number(ip
,narg
,c
);
360 mountflagsname ((int)*ip
);
363 } else if (ktr
->ktr_code
== SYS_recvmsg
||
364 ktr
->ktr_code
== SYS_sendmsg
) {
365 print_number(ip
,narg
,c
);
366 print_number(ip
,narg
,c
);
368 sendrecvflagsname ((int)*ip
);
371 } else if (ktr
->ktr_code
== SYS_recvfrom
||
372 ktr
->ktr_code
== SYS_sendto
) {
373 print_number(ip
,narg
,c
);
374 print_number(ip
,narg
,c
);
375 print_number(ip
,narg
,c
);
377 sendrecvflagsname ((int)*ip
);
380 } else if (ktr
->ktr_code
== SYS_chflags
||
381 ktr
->ktr_code
== SYS_fchflags
) {
382 print_number(ip
,narg
,c
);
387 } else if (ktr
->ktr_code
== SYS_kill
) {
388 print_number(ip
,narg
,c
);
393 } else if (ktr
->ktr_code
== SYS_reboot
) {
395 rebootoptname((int)*ip
);
398 } else if (ktr
->ktr_code
== SYS_umask
) {
403 } else if (ktr
->ktr_code
== SYS_msync
) {
404 print_number(ip
,narg
,c
);
405 print_number(ip
,narg
,c
);
407 msyncflagsname((int)*ip
);
410 } else if (ktr
->ktr_code
== SYS_mmap
) {
411 print_number(ip
,narg
,c
);
412 print_number(ip
,narg
,c
);
414 mmapprotname ((int)*ip
);
418 mmapflagsname ((int)*ip
);
421 } else if (ktr
->ktr_code
== SYS_mprotect
) {
422 print_number(ip
,narg
,c
);
423 print_number(ip
,narg
,c
);
425 mmapprotname ((int)*ip
);
428 } else if (ktr
->ktr_code
== SYS_madvise
) {
429 print_number(ip
,narg
,c
);
430 print_number(ip
,narg
,c
);
432 madvisebehavname((int)*ip
);
435 } else if (ktr
->ktr_code
== SYS_setpriority
) {
441 print_number(ip
,narg
,c
);
442 print_number(ip
,narg
,c
);
443 } else if (ktr
->ktr_code
== SYS_fcntl
) {
446 print_number(ip
,narg
,c
);
450 fcntlcmdname(cmd
, arg
, decimal
);
453 } else if (ktr
->ktr_code
== SYS_socket
) {
455 sockdomainname((int)*ip
);
459 socktypename((int)*ip
);
463 } else if (ktr
->ktr_code
== SYS_setsockopt
||
464 ktr
->ktr_code
== SYS_getsockopt
) {
465 print_number(ip
,narg
,c
);
467 sockoptlevelname((int)*ip
, decimal
);
471 sockoptname((int)*ip
);
474 } else if (ktr
->ktr_code
== SYS_lseek
) {
475 print_number(ip
,narg
,c
);
476 /* Hidden 'pad' argument, not in lseek(2) */
477 print_number(ip
,narg
,c
);
478 print_number(ip
,narg
,c
);
480 whencename ((int)*ip
);
483 } else if (ktr
->ktr_code
== SYS_flock
) {
484 print_number(ip
,narg
,c
);
489 } else if (ktr
->ktr_code
== SYS_mkfifo
||
490 ktr
->ktr_code
== SYS_mkdir
) {
491 print_number(ip
,narg
,c
);
496 } else if (ktr
->ktr_code
== SYS_shutdown
) {
497 print_number(ip
,narg
,c
);
499 shutdownhowname((int)*ip
);
502 } else if (ktr
->ktr_code
== SYS_socketpair
) {
504 sockdomainname((int)*ip
);
508 socktypename((int)*ip
);
512 } else if (ktr
->ktr_code
== SYS_getrlimit
||
513 ktr
->ktr_code
== SYS_setrlimit
) {
515 rlimitname((int)*ip
);
519 } else if (ktr
->ktr_code
== SYS_quotactl
) {
520 print_number(ip
,narg
,c
);
521 quotactlname((int)*ip
);
525 } else if (ktr
->ktr_code
== SYS_rtprio
) {
527 rtprioname((int)*ip
);
531 } else if (ktr
->ktr_code
== SYS___semctl
) {
532 print_number(ip
,narg
,c
);
533 print_number(ip
,narg
,c
);
534 semctlname((int)*ip
);
537 } else if (ktr
->ktr_code
== SYS_semget
) {
538 print_number(ip
,narg
,c
);
539 print_number(ip
,narg
,c
);
540 semgetname((int)*ip
);
543 } else if (ktr
->ktr_code
== SYS_msgctl
) {
544 print_number(ip
,narg
,c
);
545 shmctlname((int)*ip
);
548 } else if (ktr
->ktr_code
== SYS_shmat
) {
549 print_number(ip
,narg
,c
);
550 print_number(ip
,narg
,c
);
554 } else if (ktr
->ktr_code
== SYS_shmctl
) {
555 print_number(ip
,narg
,c
);
556 shmctlname((int)*ip
);
559 } else if (ktr
->ktr_code
== SYS_minherit
) {
560 print_number(ip
,narg
,c
);
561 print_number(ip
,narg
,c
);
562 minheritname((int)*ip
);
565 } else if (ktr
->ktr_code
== SYS_rfork
) {
571 } else if (ktr
->ktr_code
== SYS_lio_listio
) {
573 lio_listioname((int)*ip
);
577 } else if (ktr
->ktr_code
== SYS_mlockall
) {
579 mlockallname((int)*ip
);
582 } else if (ktr
->ktr_code
== SYS_sched_setscheduler
) {
583 print_number(ip
,narg
,c
);
584 schedpolicyname((int)*ip
);
587 } else if (ktr
->ktr_code
== SYS_sched_get_priority_max
||
588 ktr
->ktr_code
== SYS_sched_get_priority_min
) {
590 schedpolicyname((int)*ip
);
593 } else if (ktr
->ktr_code
== SYS_sendfile
) {
594 print_number(ip
,narg
,c
);
595 print_number(ip
,narg
,c
);
596 print_number(ip
,narg
,c
);
597 print_number(ip
,narg
,c
);
598 print_number(ip
,narg
,c
);
599 print_number(ip
,narg
,c
);
600 sendfileflagsname((int)*ip
);
603 } else if (ktr
->ktr_code
== SYS_kldsym
) {
604 print_number(ip
,narg
,c
);
605 kldsymcmdname((int)*ip
);
608 } else if (ktr
->ktr_code
== SYS_sigprocmask
) {
610 sigprocmaskhowname((int)*ip
);
614 } else if (ktr
->ktr_code
== SYS___acl_get_file
||
615 ktr
->ktr_code
== SYS___acl_set_file
||
616 ktr
->ktr_code
== SYS___acl_get_fd
||
617 ktr
->ktr_code
== SYS___acl_set_fd
||
618 ktr
->ktr_code
== SYS___acl_delete_file
||
619 ktr
->ktr_code
== SYS___acl_delete_fd
||
620 ktr
->ktr_code
== SYS___acl_aclcheck_file
||
621 ktr
->ktr_code
== SYS___acl_aclcheck_fd
) {
622 print_number(ip
,narg
,c
);
623 acltypename((int)*ip
);
626 } else if (ktr
->ktr_code
== SYS_sigaction
) {
632 } else if (ktr
->ktr_code
== SYS_extattrctl
) {
633 print_number(ip
,narg
,c
);
634 extattrctlname((int)*ip
);
637 } else if (ktr
->ktr_code
== SYS_ptrace
) {
638 if (*ip
< (register_t
)(sizeof(ptrace_ops
) /
639 sizeof(ptrace_ops
[0])) && *ip
>= 0)
640 (void)printf("(%s", ptrace_ops
[*ip
]);
642 else if (*ip
== PT_GETREGS
)
643 (void)printf("(%s", "PT_GETREGS");
646 else if (*ip
== PT_SETREGS
)
647 (void)printf("(%s", "PT_SETREGS");
650 else if (*ip
== PT_GETFPREGS
)
651 (void)printf("(%s", "PT_GETFPREGS");
654 else if (*ip
== PT_SETFPREGS
)
655 (void)printf("(%s", "PT_SETFPREGS");
658 else if (*ip
== PT_GETDBREGS
)
659 (void)printf("(%s", "PT_GETDBREGS");
662 else if (*ip
== PT_SETDBREGS
)
663 (void)printf("(%s", "PT_SETDBREGS");
666 (void)printf("(%ld", (long)*ip
);
673 print_number(ip
,narg
,c
);
681 ktrsysret(struct ktr_sysret
*ktr
)
683 register_t ret
= ktr
->ktr_retval
;
684 int error
= ktr
->ktr_error
;
685 int code
= ktr
->ktr_code
;
687 if (code
>= nsyscalls
|| code
< 0)
688 (void)printf("[%d] ", code
);
690 (void)printf("%s ", syscallnames
[code
]);
694 (void)printf("%ld", (long)ret
);
695 if (ret
< 0 || ret
> 9)
696 (void)printf("/%#lx", (long)ret
);
699 (void)printf("%ld", (long)ret
);
701 (void)printf("%#lx", (long)ret
);
703 } else if (error
== ERESTART
)
704 (void)printf("RESTART");
705 else if (error
== EJUSTRETURN
)
706 (void)printf("JUSTRETURN");
708 (void)printf("-1 errno %d", ktr
->ktr_error
);
710 (void)printf(" %s", strerror(ktr
->ktr_error
));
716 ktrnamei(char *cp
, int len
)
718 (void)printf("\"%.*s\"\n", len
, cp
);
722 ktrgenio(struct ktr_genio
*ktr
, int len
)
724 int datalen
= len
- sizeof (struct ktr_genio
);
725 char *dp
= (char *)ktr
+ sizeof (struct ktr_genio
);
730 static int screenwidth
= 0;
732 if (screenwidth
== 0) {
735 if (fancy
&& ioctl(fileno(stderr
), TIOCGWINSZ
, &ws
) != -1 &&
737 screenwidth
= ws
.ws_col
;
741 printf("fd %d %s %d byte%s\n", ktr
->ktr_fd
,
742 ktr
->ktr_rw
== UIO_READ
? "read" : "wrote", datalen
,
743 datalen
== 1 ? "" : "s");
744 if (maxdata
&& datalen
> maxdata
)
748 for (;datalen
> 0; datalen
--, dp
++) {
749 (void) vis(visbuf
, *dp
, VIS_CSTYLE
, *(dp
+1));
752 * Keep track of printables and
753 * space chars (like fold(1)).
765 width
= 8 - (col
&07);
770 if (col
+ width
> (screenwidth
-2)) {
771 (void)printf("\\\n\t");
776 (void)putchar(*cp
++);
781 (void)printf("\"\n");
784 const char *signames
[NSIG
] = {
785 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
786 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
787 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
788 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
789 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
790 "USR2", NULL
, /* 31 - 32 */
794 ktrpsig(struct ktr_psig
*psig
)
796 (void)printf("SIG%s ", signames
[psig
->signo
]);
797 if (psig
->action
== SIG_DFL
)
798 (void)printf("SIG_DFL\n");
800 (void)printf("caught handler=0x%lx mask=0x%x code=0x%x\n",
801 (u_long
)psig
->action
, psig
->mask
.__bits
[0], psig
->code
);
805 ktrcsw(struct ktr_csw
*cs
)
807 (void)printf("%s %s\n", cs
->out
? "stop" : "resume",
808 cs
->user
? "user" : "kernel");
811 struct utrace_malloc
{
818 ktruser_malloc(int len __unused
, unsigned char *p
)
820 struct utrace_malloc
*ut
= (struct utrace_malloc
*)p
;
823 if (ut
->s
== 0 && ut
->r
== NULL
)
824 printf("malloc_init()\n");
826 printf("%p = malloc(%zu)\n", ut
->r
, ut
->s
);
829 printf("free(%p)\n", ut
->p
);
831 printf("%p = realloc(%p, %zu)\n", ut
->r
, ut
->p
, ut
->s
);
836 ktruser(int len
, unsigned char *p
)
838 if (len
== sizeof(struct utrace_malloc
)) {
839 ktruser_malloc(len
, p
);
843 (void)printf("%d ", len
);
845 (void)printf(" %02x", *p
++);
852 (void)fprintf(stderr
,
853 "usage: kdump [-dnlRT] [-f trfile] [-m maxdata] [-t [cnisuw]] [-p pid]\n");
858 timevalsub(struct timeval
*t1
, struct timeval
*t2
)
860 t1
->tv_sec
-= t2
->tv_sec
;
861 t1
->tv_usec
-= t2
->tv_usec
;
866 timevalfix(struct timeval
*t1
)
868 if (t1
->tv_usec
< 0) {
870 t1
->tv_usec
+= 1000000;
872 if (t1
->tv_usec
>= 1000000) {
874 t1
->tv_usec
-= 1000000;