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 $
38 #define _KERNEL_STRUCTURES
40 #include <sys/errno.h>
41 #include <sys/param.h>
42 #include <sys/errno.h>
45 #include <sys/ktrace.h>
46 #include <sys/ioctl.h>
47 #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 ktruser_rtld(int, unsigned char *);
72 static void timevalfix(struct timeval *);
73 static void timevalsub(struct timeval *, struct timeval *);
74 static void usage(void);
76 int timestamp, decimal, fancy = 1, tail, maxdata = 64;
78 const char *tracefile = DEF_TRACEFILE;
79 struct ktr_header ktr_header;
81 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
84 main(int argc, char **argv)
86 int ch, col, ktrlen, size;
89 int trpoints = ALL_POINTS;
92 (void) setlocale(LC_CTYPE, "");
94 while ((ch = getopt(argc,argv,"f:djlm:np:RTt:")) != -1)
109 maxdata = atoi(optarg);
115 do_pid = strtoul(optarg, &cp, 0);
117 errx(1,"invalid number %s", optarg);
120 timestamp = 2; /* relative timestamp */
126 trpoints = getpoints(optarg);
128 errx(1, "unknown trace point in %s", optarg);
137 m = (void *)malloc(size = 1025);
139 errx(1, "%s", strerror(ENOMEM));
140 if (!freopen(tracefile, "r", stdin))
141 err(1, "%s", tracefile);
142 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
143 if (trpoints & (1 << ktr_header.ktr_type) &&
144 (do_pid == -1 || ktr_header.ktr_pid == do_pid))
145 col = dumpheader(&ktr_header);
148 if ((ktrlen = ktr_header.ktr_len) < 0)
149 errx(1, "bogus length 0x%x", ktrlen);
151 m = (void *)realloc(m, ktrlen+1);
153 errx(1, "%s", strerror(ENOMEM));
156 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
157 errx(1, "data too short");
158 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
162 switch (ktr_header.ktr_type) {
164 ktrsyscall((struct ktr_syscall *)m);
167 ktrsysret((struct ktr_sysret *)m);
173 ktrgenio((struct ktr_genio *)m, ktrlen);
176 ktrpsig((struct ktr_psig *)m);
179 ktrcsw((struct ktr_csw *)m);
186 (void)fflush(stdout);
192 fread_tail(void *buf, int size, int num)
196 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
204 dumpheader(struct ktr_header *kth)
206 static char unknown[64];
207 static struct timeval prevtime, temp;
211 switch (kth->ktr_type) {
234 (void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
238 if (kth->ktr_tid || (kth->ktr_flags & KTRH_THREADED) || fixedformat)
239 col = printf("%5d:%-4d", kth->ktr_pid, kth->ktr_tid);
241 col = printf("%5d", kth->ktr_pid);
242 col += printf(" %-8.*s ", MAXCOMLEN, kth->ktr_comm);
244 if (timestamp == 2) {
245 temp = kth->ktr_time;
246 timevalsub(&kth->ktr_time, &prevtime);
249 col += printf("%ld.%06ld ",
250 kth->ktr_time.tv_sec, kth->ktr_time.tv_usec);
252 col += printf("%s ", type);
256 #include <sys/syscall.h>
258 #include <sys/kern/syscalls.c>
260 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
262 static const char *ptrace_ops[] = {
263 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U",
264 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE",
265 "PT_KILL", "PT_STEP", "PT_ATTACH", "PT_DETACH",
269 ktrsyscall(struct ktr_syscall *ktr)
271 int narg = ktr->ktr_narg;
274 if (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0)
275 (void)printf("[%d]", ktr->ktr_code);
277 (void)printf("%s", syscallnames[ktr->ktr_code]);
278 ip = &ktr->ktr_args[0];
283 #define print_number(i,n,c) do { \
285 (void)printf("%c%ld", c, (long)*i); \
287 (void)printf("%c%#lx", c, (long)*i); \
293 if (ktr->ktr_code == SYS_ioctl) {
295 print_number(ip,narg,c);
296 if ((cp = ioctlname(*ip)) != NULL)
297 (void)printf(",%s", cp);
300 (void)printf(",%ld", (long)*ip);
302 (void)printf(",%#lx ", (long)*ip);
307 } else if (ktr->ktr_code == SYS_access) {
308 print_number(ip,narg,c);
310 accessmodename ((int)*ip);
313 } else if (ktr->ktr_code == SYS_open ||
314 ktr->ktr_code == SYS_mq_open) {
317 print_number(ip,narg,c);
321 flagsandmodename (flags, mode, decimal);
324 } else if (ktr->ktr_code == SYS_wait4) {
325 print_number(ip,narg,c);
326 print_number(ip,narg,c);
328 wait4optname ((int)*ip);
331 } else if (ktr->ktr_code == SYS_chmod ||
332 ktr->ktr_code == SYS_fchmod ||
333 ktr->ktr_code == SYS_lchmod) {
334 print_number(ip,narg,c);
339 } else if (ktr->ktr_code == SYS_mknod) {
340 print_number(ip,narg,c);
345 } else if (ktr->ktr_code == SYS_getfsstat) {
346 print_number(ip,narg,c);
347 print_number(ip,narg,c);
349 getfsstatflagsname ((int)*ip);
352 } else if (ktr->ktr_code == SYS_mount) {
353 print_number(ip,narg,c);
354 print_number(ip,narg,c);
356 mountflagsname ((int)*ip);
359 } else if (ktr->ktr_code == SYS_unmount) {
360 print_number(ip,narg,c);
362 mountflagsname ((int)*ip);
365 } else if (ktr->ktr_code == SYS_recvmsg ||
366 ktr->ktr_code == SYS_sendmsg) {
367 print_number(ip,narg,c);
368 print_number(ip,narg,c);
370 sendrecvflagsname ((int)*ip);
373 } else if (ktr->ktr_code == SYS_recvfrom ||
374 ktr->ktr_code == SYS_sendto) {
375 print_number(ip,narg,c);
376 print_number(ip,narg,c);
377 print_number(ip,narg,c);
379 sendrecvflagsname ((int)*ip);
382 } else if (ktr->ktr_code == SYS_chflags ||
383 ktr->ktr_code == SYS_fchflags) {
384 print_number(ip,narg,c);
389 } else if (ktr->ktr_code == SYS_kill) {
390 print_number(ip,narg,c);
395 } else if (ktr->ktr_code == SYS_reboot) {
397 rebootoptname((int)*ip);
400 } else if (ktr->ktr_code == SYS_umask) {
405 } else if (ktr->ktr_code == SYS_msync) {
406 print_number(ip,narg,c);
407 print_number(ip,narg,c);
409 msyncflagsname((int)*ip);
412 } else if (ktr->ktr_code == SYS_mmap) {
413 print_number(ip,narg,c);
414 print_number(ip,narg,c);
416 mmapprotname ((int)*ip);
420 mmapflagsname ((int)*ip);
423 } else if (ktr->ktr_code == SYS_mprotect) {
424 print_number(ip,narg,c);
425 print_number(ip,narg,c);
427 mmapprotname ((int)*ip);
430 } else if (ktr->ktr_code == SYS_madvise) {
431 print_number(ip,narg,c);
432 print_number(ip,narg,c);
434 madvisebehavname((int)*ip);
437 } else if (ktr->ktr_code == SYS_setpriority) {
443 print_number(ip,narg,c);
444 print_number(ip,narg,c);
445 } else if (ktr->ktr_code == SYS_fcntl) {
448 print_number(ip,narg,c);
452 fcntlcmdname(cmd, arg, decimal);
455 } else if (ktr->ktr_code == SYS_socket) {
457 sockdomainname((int)*ip);
461 socktypename((int)*ip);
465 } else if (ktr->ktr_code == SYS_setsockopt ||
466 ktr->ktr_code == SYS_getsockopt) {
467 print_number(ip,narg,c);
469 sockoptlevelname((int)*ip, decimal);
473 sockoptname((int)*ip);
476 } else if (ktr->ktr_code == SYS_lseek) {
477 print_number(ip,narg,c);
478 /* Hidden 'pad' argument, not in lseek(2) */
479 print_number(ip,narg,c);
480 print_number(ip,narg,c);
482 whencename ((int)*ip);
485 } else if (ktr->ktr_code == SYS_flock) {
486 print_number(ip,narg,c);
491 } else if (ktr->ktr_code == SYS_mkfifo ||
492 ktr->ktr_code == SYS_mkdir) {
493 print_number(ip,narg,c);
498 } else if (ktr->ktr_code == SYS_shutdown) {
499 print_number(ip,narg,c);
501 shutdownhowname((int)*ip);
504 } else if (ktr->ktr_code == SYS_socketpair) {
506 sockdomainname((int)*ip);
510 socktypename((int)*ip);
514 } else if (ktr->ktr_code == SYS_getrlimit ||
515 ktr->ktr_code == SYS_setrlimit) {
517 rlimitname((int)*ip);
521 } else if (ktr->ktr_code == SYS_quotactl) {
522 print_number(ip,narg,c);
523 quotactlname((int)*ip);
527 } else if (ktr->ktr_code == SYS_rtprio) {
529 rtprioname((int)*ip);
533 } else if (ktr->ktr_code == SYS___semctl) {
534 print_number(ip,narg,c);
535 print_number(ip,narg,c);
536 semctlname((int)*ip);
539 } else if (ktr->ktr_code == SYS_semget) {
540 print_number(ip,narg,c);
541 print_number(ip,narg,c);
542 semgetname((int)*ip);
545 } else if (ktr->ktr_code == SYS_msgctl) {
546 print_number(ip,narg,c);
547 shmctlname((int)*ip);
550 } else if (ktr->ktr_code == SYS_shmat) {
551 print_number(ip,narg,c);
552 print_number(ip,narg,c);
556 } else if (ktr->ktr_code == SYS_shmctl) {
557 print_number(ip,narg,c);
558 shmctlname((int)*ip);
561 } else if (ktr->ktr_code == SYS_minherit) {
562 print_number(ip,narg,c);
563 print_number(ip,narg,c);
564 minheritname((int)*ip);
567 } else if (ktr->ktr_code == SYS_rfork) {
573 } else if (ktr->ktr_code == SYS_lio_listio) {
575 lio_listioname((int)*ip);
579 } else if (ktr->ktr_code == SYS_mlockall) {
581 mlockallname((int)*ip);
584 } else if (ktr->ktr_code == SYS_sched_setscheduler) {
585 print_number(ip,narg,c);
586 schedpolicyname((int)*ip);
589 } else if (ktr->ktr_code == SYS_sched_get_priority_max ||
590 ktr->ktr_code == SYS_sched_get_priority_min) {
592 schedpolicyname((int)*ip);
595 } else if (ktr->ktr_code == SYS_sendfile) {
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 print_number(ip,narg,c);
601 print_number(ip,narg,c);
602 sendfileflagsname((int)*ip);
605 } else if (ktr->ktr_code == SYS_kldsym) {
606 print_number(ip,narg,c);
607 kldsymcmdname((int)*ip);
610 } else if (ktr->ktr_code == SYS_sigprocmask) {
612 sigprocmaskhowname((int)*ip);
616 } else if (ktr->ktr_code == SYS___acl_get_file ||
617 ktr->ktr_code == SYS___acl_set_file ||
618 ktr->ktr_code == SYS___acl_get_fd ||
619 ktr->ktr_code == SYS___acl_set_fd ||
620 ktr->ktr_code == SYS___acl_delete_file ||
621 ktr->ktr_code == SYS___acl_delete_fd ||
622 ktr->ktr_code == SYS___acl_aclcheck_file ||
623 ktr->ktr_code == SYS___acl_aclcheck_fd) {
624 print_number(ip,narg,c);
625 acltypename((int)*ip);
628 } else if (ktr->ktr_code == SYS_sigaction) {
634 } else if (ktr->ktr_code == SYS_extattrctl) {
635 print_number(ip,narg,c);
636 extattrctlname((int)*ip);
639 } else if (ktr->ktr_code == SYS_ptrace) {
640 if (*ip < (register_t)(sizeof(ptrace_ops) /
641 sizeof(ptrace_ops[0])) && *ip >= 0)
642 (void)printf("(%s", ptrace_ops[*ip]);
644 else if (*ip == PT_GETREGS)
645 (void)printf("(%s", "PT_GETREGS");
648 else if (*ip == PT_SETREGS)
649 (void)printf("(%s", "PT_SETREGS");
652 else if (*ip == PT_GETFPREGS)
653 (void)printf("(%s", "PT_GETFPREGS");
656 else if (*ip == PT_SETFPREGS)
657 (void)printf("(%s", "PT_SETFPREGS");
660 else if (*ip == PT_GETDBREGS)
661 (void)printf("(%s", "PT_GETDBREGS");
664 else if (*ip == PT_SETDBREGS)
665 (void)printf("(%s", "PT_SETDBREGS");
668 (void)printf("(%ld", (long)*ip);
675 print_number(ip,narg,c);
683 ktrsysret(struct ktr_sysret *ktr)
685 register_t ret = ktr->ktr_retval;
686 int error = ktr->ktr_error;
687 int code = ktr->ktr_code;
689 if (code >= nsyscalls || code < 0)
690 (void)printf("[%d] ", code);
692 (void)printf("%s ", syscallnames[code]);
696 (void)printf("%ld", (long)ret);
697 if (ret < 0 || ret > 9)
698 (void)printf("/%#lx", (long)ret);
701 (void)printf("%ld", (long)ret);
703 (void)printf("%#lx", (long)ret);
705 } else if (error == ERESTART)
706 (void)printf("RESTART");
707 else if (error == EJUSTRETURN)
708 (void)printf("JUSTRETURN");
710 (void)printf("-1 errno %d", ktr->ktr_error);
712 (void)printf(" %s", strerror(ktr->ktr_error));
718 ktrnamei(char *cp, int len)
720 (void)printf("\"%.*s\"\n", len, cp);
724 ktrgenio(struct ktr_genio *ktr, int len)
726 int datalen = len - sizeof (struct ktr_genio);
727 char *dp = (char *)ktr + sizeof (struct ktr_genio);
732 static int screenwidth = 0;
734 if (screenwidth == 0) {
737 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
739 screenwidth = ws.ws_col;
743 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
744 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
745 datalen == 1 ? "" : "s");
746 if (maxdata && datalen > maxdata)
750 for (;datalen > 0; datalen--, dp++) {
751 (void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
754 * Keep track of printables and
755 * space chars (like fold(1)).
767 width = 8 - (col&07);
772 if (col + width > (screenwidth-2)) {
773 (void)printf("\\\n\t");
778 (void)putchar(*cp++);
783 (void)printf("\"\n");
786 const char *signames[NSIG] = {
787 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
788 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
789 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
790 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
791 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
792 "USR2", NULL, /* 31 - 32 */
796 ktrpsig(struct ktr_psig *psig)
798 (void)printf("SIG%s ", signames[psig->signo]);
799 if (psig->action == SIG_DFL)
800 (void)printf("SIG_DFL\n");
802 (void)printf("caught handler=0x%lx mask=0x%x code=0x%x\n",
803 (u_long)psig->action, psig->mask.__bits[0], psig->code);
807 ktrcsw(struct ktr_csw *cs)
809 (void)printf("%s %s\n", cs->out ? "stop" : "resume",
810 cs->user ? "user" : "kernel");
813 #define UTRACE_DLOPEN_START 1
814 #define UTRACE_DLOPEN_STOP 2
815 #define UTRACE_DLCLOSE_START 3
816 #define UTRACE_DLCLOSE_STOP 4
817 #define UTRACE_LOAD_OBJECT 5
818 #define UTRACE_UNLOAD_OBJECT 6
819 #define UTRACE_ADD_RUNDEP 7
820 #define UTRACE_PRELOAD_FINISHED 8
821 #define UTRACE_INIT_CALL 9
822 #define UTRACE_FINI_CALL 10
825 char sig[4]; /* 'RTLD' */
831 char name[MAXPATHLEN];
835 ktruser_rtld(int len, unsigned char *p)
837 struct utrace_rtld *ut = (struct utrace_rtld *)p;
842 case UTRACE_DLOPEN_START:
844 printf("dlopen(%s, ", ut->name);
845 switch (mode & RTLD_MODEMASK) {
853 printf("%#x", mode & RTLD_MODEMASK);
855 if (mode & RTLD_GLOBAL)
856 printf(" | RTLD_GLOBAL");
857 if (mode & RTLD_TRACE)
858 printf(" | RTLD_TRACE");
859 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
860 printf(" | %#x", mode &
861 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
864 case UTRACE_DLOPEN_STOP:
865 printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
868 case UTRACE_DLCLOSE_START:
869 printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
872 case UTRACE_DLCLOSE_STOP:
873 printf("dlclose(%p) finished\n", ut->handle);
875 case UTRACE_LOAD_OBJECT:
876 printf("RTLD: loaded %p @ %p - %p (%s)\n", ut->handle,
877 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
880 case UTRACE_UNLOAD_OBJECT:
881 printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
882 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
885 case UTRACE_ADD_RUNDEP:
886 parent = ut->mapbase;
887 printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
888 ut->handle, ut->name, ut->refcnt);
890 case UTRACE_PRELOAD_FINISHED:
891 printf("RTLD: LD_PRELOAD finished\n");
893 case UTRACE_INIT_CALL:
894 printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
897 case UTRACE_FINI_CALL:
898 printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
904 printf("RTLD: %d ", len);
909 printf(" %02x", *p++);
914 struct utrace_malloc {
921 ktruser_malloc(int len __unused, unsigned char *p)
923 struct utrace_malloc *ut = (struct utrace_malloc *)p;
926 if (ut->s == 0 && ut->r == NULL)
927 printf("malloc_init()\n");
929 printf("%p = malloc(%zu)\n", ut->r, ut->s);
932 printf("free(%p)\n", ut->p);
934 printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
939 ktruser(int len, unsigned char *p)
942 if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
943 ktruser_rtld(len, p);
947 if (len == sizeof(struct utrace_malloc)) {
948 ktruser_malloc(len, p);
952 (void)printf("%d ", len);
954 (void)printf(" %02x", *p++);
961 (void)fprintf(stderr,
962 "usage: kdump [-dnlRT] [-f trfile] [-m maxdata] [-t [cnisuw]] [-p pid]\n");
967 timevalsub(struct timeval *t1, struct timeval *t2)
969 t1->tv_sec -= t2->tv_sec;
970 t1->tv_usec -= t2->tv_usec;
975 timevalfix(struct timeval *t1)
977 if (t1->tv_usec < 0) {
979 t1->tv_usec += 1000000;
981 if (t1->tv_usec >= 1000000) {
983 t1->tv_usec -= 1000000;