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>
44 #include <sys/ktrace.h>
45 #include <sys/ioctl.h>
46 #include <sys/ptrace.h>
56 #include "kdump_subr.h"
58 extern const char *ioctlname(u_long);
60 static int dumpheader(struct ktr_header *);
61 static int fread_tail(void *, int, int);
62 static void ktrcsw(struct ktr_csw *);
63 static void ktrgenio(struct ktr_genio *, int);
64 static void ktrnamei(char *, int);
65 static void ktrpsig(struct ktr_psig *);
66 static void ktrsyscall(struct ktr_syscall *);
67 static void ktrsysret(struct ktr_sysret *);
68 static void ktruser(int, unsigned char *);
69 static void ktruser_malloc(int, unsigned char *);
70 static void ktruser_rtld(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;
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,"acf:djlm:np:RTt:")) != -1)
106 timestamp = 2; /* relative timestamp */
116 maxdata = atoi(optarg);
122 do_pid = strtoul(optarg, &cp, 0);
124 errx(1,"invalid number %s", optarg);
127 timestamp = 2; /* relative timestamp */
133 trpoints = getpoints(optarg);
135 errx(1, "unknown trace point in %s", optarg);
144 m = (void *)malloc(size = 1025);
146 errx(1, "%s", strerror(ENOMEM));
147 if (!freopen(tracefile, "r", stdin))
148 err(1, "%s", tracefile);
149 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
150 if (trpoints & (1 << ktr_header.ktr_type) &&
151 (do_pid == -1 || ktr_header.ktr_pid == do_pid))
152 col = dumpheader(&ktr_header);
155 if ((ktrlen = ktr_header.ktr_len) < 0)
156 errx(1, "bogus length 0x%x", ktrlen);
158 m = (void *)realloc(m, ktrlen+1);
160 errx(1, "%s", strerror(ENOMEM));
163 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
164 errx(1, "data too short");
165 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
169 switch (ktr_header.ktr_type) {
171 ktrsyscall((struct ktr_syscall *)m);
174 ktrsysret((struct ktr_sysret *)m);
180 ktrgenio((struct ktr_genio *)m, ktrlen);
183 ktrpsig((struct ktr_psig *)m);
186 ktrcsw((struct ktr_csw *)m);
193 (void)fflush(stdout);
199 fread_tail(void *buf, int size, int num)
203 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
211 dumpheader(struct ktr_header *kth)
213 static char unknown[64];
214 static struct timeval prevtime, temp;
218 switch (kth->ktr_type) {
241 (void)sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
245 if (kth->ktr_tid || (kth->ktr_flags & KTRH_THREADED) || fixedformat)
246 col = printf("%5d:%-4d", kth->ktr_pid, kth->ktr_tid);
248 col = printf("%5d", kth->ktr_pid);
250 col += printf(" %2d", KTRH_CPUID_DECODE(kth->ktr_flags));
251 col += printf(" %-8.*s ", MAXCOMLEN, kth->ktr_comm);
253 if (timestamp == 2) {
254 temp = kth->ktr_time;
255 timevalsub(&kth->ktr_time, &prevtime);
258 col += printf("%ld.%06ld ",
259 kth->ktr_time.tv_sec, kth->ktr_time.tv_usec);
261 col += printf("%s ", type);
265 #include <sys/syscall.h>
267 #include <sys/kern/syscalls.c>
269 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
271 static const char *ptrace_ops[] = {
272 "PT_TRACE_ME", "PT_READ_I", "PT_READ_D", "PT_READ_U",
273 "PT_WRITE_I", "PT_WRITE_D", "PT_WRITE_U", "PT_CONTINUE",
274 "PT_KILL", "PT_STEP", "PT_ATTACH", "PT_DETACH",
278 ktrsyscall(struct ktr_syscall *ktr)
280 int narg = ktr->ktr_narg;
283 if (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0)
284 (void)printf("[%d]", ktr->ktr_code);
286 (void)printf("%s", syscallnames[ktr->ktr_code]);
287 ip = &ktr->ktr_args[0];
292 #define print_number(i,n,c) do { \
294 (void)printf("%c%ld", c, (long)*i); \
296 (void)printf("%c%#lx", c, (long)*i); \
302 if (ktr->ktr_code == SYS_ioctl) {
304 print_number(ip,narg,c);
305 if ((cp = ioctlname(*ip)) != NULL)
306 (void)printf(",%s", cp);
309 (void)printf(",%ld", (long)*ip);
311 (void)printf(",%#lx ", (long)*ip);
316 } else if (ktr->ktr_code == SYS_access) {
317 print_number(ip,narg,c);
319 accessmodename ((int)*ip);
322 } else if (ktr->ktr_code == SYS_open ||
323 ktr->ktr_code == SYS_mq_open) {
326 print_number(ip,narg,c);
330 flagsandmodename (flags, mode, decimal);
333 } else if (ktr->ktr_code == SYS_wait4) {
334 print_number(ip,narg,c);
335 print_number(ip,narg,c);
337 wait4optname ((int)*ip);
340 } else if (ktr->ktr_code == SYS_chmod ||
341 ktr->ktr_code == SYS_fchmod ||
342 ktr->ktr_code == SYS_lchmod) {
343 print_number(ip,narg,c);
348 } else if (ktr->ktr_code == SYS_mknod) {
349 print_number(ip,narg,c);
354 } else if (ktr->ktr_code == SYS_getfsstat) {
355 print_number(ip,narg,c);
356 print_number(ip,narg,c);
358 getfsstatflagsname ((int)*ip);
361 } else if (ktr->ktr_code == SYS_mount) {
362 print_number(ip,narg,c);
363 print_number(ip,narg,c);
365 mountflagsname ((int)*ip);
368 } else if (ktr->ktr_code == SYS_unmount) {
369 print_number(ip,narg,c);
371 mountflagsname ((int)*ip);
374 } else if (ktr->ktr_code == SYS_recvmsg ||
375 ktr->ktr_code == SYS_sendmsg) {
376 print_number(ip,narg,c);
377 print_number(ip,narg,c);
379 sendrecvflagsname ((int)*ip);
382 } else if (ktr->ktr_code == SYS_recvfrom ||
383 ktr->ktr_code == SYS_sendto) {
384 print_number(ip,narg,c);
385 print_number(ip,narg,c);
386 print_number(ip,narg,c);
388 sendrecvflagsname ((int)*ip);
391 } else if (ktr->ktr_code == SYS_chflags ||
392 ktr->ktr_code == SYS_fchflags) {
393 print_number(ip,narg,c);
398 } else if (ktr->ktr_code == SYS_kill) {
399 print_number(ip,narg,c);
404 } else if (ktr->ktr_code == SYS_reboot) {
406 rebootoptname((int)*ip);
409 } else if (ktr->ktr_code == SYS_umask) {
414 } else if (ktr->ktr_code == SYS_msync) {
415 print_number(ip,narg,c);
416 print_number(ip,narg,c);
418 msyncflagsname((int)*ip);
421 } else if (ktr->ktr_code == SYS_mmap) {
422 print_number(ip,narg,c);
423 print_number(ip,narg,c);
425 mmapprotname ((int)*ip);
429 mmapflagsname ((int)*ip);
432 } else if (ktr->ktr_code == SYS_mprotect) {
433 print_number(ip,narg,c);
434 print_number(ip,narg,c);
436 mmapprotname ((int)*ip);
439 } else if (ktr->ktr_code == SYS_madvise) {
440 print_number(ip,narg,c);
441 print_number(ip,narg,c);
443 madvisebehavname((int)*ip);
446 } else if (ktr->ktr_code == SYS_setpriority) {
452 print_number(ip,narg,c);
453 print_number(ip,narg,c);
454 } else if (ktr->ktr_code == SYS_fcntl) {
457 print_number(ip,narg,c);
461 fcntlcmdname(cmd, arg, decimal);
464 } else if (ktr->ktr_code == SYS_socket) {
466 sockdomainname((int)*ip);
470 socktypename((int)*ip);
474 } else if (ktr->ktr_code == SYS_setsockopt ||
475 ktr->ktr_code == SYS_getsockopt) {
476 print_number(ip,narg,c);
478 sockoptlevelname((int)*ip, decimal);
482 sockoptname((int)*ip);
485 } else if (ktr->ktr_code == SYS_lseek) {
486 print_number(ip,narg,c);
487 /* Hidden 'pad' argument, not in lseek(2) */
488 print_number(ip,narg,c);
489 print_number(ip,narg,c);
491 whencename ((int)*ip);
494 } else if (ktr->ktr_code == SYS_flock) {
495 print_number(ip,narg,c);
500 } else if (ktr->ktr_code == SYS_mkfifo ||
501 ktr->ktr_code == SYS_mkdir) {
502 print_number(ip,narg,c);
507 } else if (ktr->ktr_code == SYS_shutdown) {
508 print_number(ip,narg,c);
510 shutdownhowname((int)*ip);
513 } else if (ktr->ktr_code == SYS_socketpair) {
515 sockdomainname((int)*ip);
519 socktypename((int)*ip);
523 } else if (ktr->ktr_code == SYS_getrlimit ||
524 ktr->ktr_code == SYS_setrlimit) {
526 rlimitname((int)*ip);
530 } else if (ktr->ktr_code == SYS_quotactl) {
531 print_number(ip,narg,c);
532 quotactlname((int)*ip);
536 } else if (ktr->ktr_code == SYS_rtprio) {
538 rtprioname((int)*ip);
542 } else if (ktr->ktr_code == SYS___semctl) {
543 print_number(ip,narg,c);
544 print_number(ip,narg,c);
545 semctlname((int)*ip);
548 } else if (ktr->ktr_code == SYS_semget) {
549 print_number(ip,narg,c);
550 print_number(ip,narg,c);
551 semgetname((int)*ip);
554 } else if (ktr->ktr_code == SYS_msgctl) {
555 print_number(ip,narg,c);
556 shmctlname((int)*ip);
559 } else if (ktr->ktr_code == SYS_shmat) {
560 print_number(ip,narg,c);
561 print_number(ip,narg,c);
565 } else if (ktr->ktr_code == SYS_shmctl) {
566 print_number(ip,narg,c);
567 shmctlname((int)*ip);
570 } else if (ktr->ktr_code == SYS_minherit) {
571 print_number(ip,narg,c);
572 print_number(ip,narg,c);
573 minheritname((int)*ip);
576 } else if (ktr->ktr_code == SYS_rfork) {
582 } else if (ktr->ktr_code == SYS_lio_listio) {
584 lio_listioname((int)*ip);
588 } else if (ktr->ktr_code == SYS_mlockall) {
590 mlockallname((int)*ip);
593 } else if (ktr->ktr_code == SYS_sched_setscheduler) {
594 print_number(ip,narg,c);
595 schedpolicyname((int)*ip);
598 } else if (ktr->ktr_code == SYS_sched_get_priority_max ||
599 ktr->ktr_code == SYS_sched_get_priority_min) {
601 schedpolicyname((int)*ip);
604 } else if (ktr->ktr_code == SYS_sendfile) {
605 print_number(ip,narg,c);
606 print_number(ip,narg,c);
607 print_number(ip,narg,c);
608 print_number(ip,narg,c);
609 print_number(ip,narg,c);
610 print_number(ip,narg,c);
611 sendfileflagsname((int)*ip);
614 } else if (ktr->ktr_code == SYS_kldsym) {
615 print_number(ip,narg,c);
616 kldsymcmdname((int)*ip);
619 } else if (ktr->ktr_code == SYS_sigprocmask) {
621 sigprocmaskhowname((int)*ip);
625 } else if (ktr->ktr_code == SYS___acl_get_file ||
626 ktr->ktr_code == SYS___acl_set_file ||
627 ktr->ktr_code == SYS___acl_get_fd ||
628 ktr->ktr_code == SYS___acl_set_fd ||
629 ktr->ktr_code == SYS___acl_delete_file ||
630 ktr->ktr_code == SYS___acl_delete_fd ||
631 ktr->ktr_code == SYS___acl_aclcheck_file ||
632 ktr->ktr_code == SYS___acl_aclcheck_fd) {
633 print_number(ip,narg,c);
634 acltypename((int)*ip);
637 } else if (ktr->ktr_code == SYS_sigaction) {
643 } else if (ktr->ktr_code == SYS_extattrctl) {
644 print_number(ip,narg,c);
645 extattrctlname((int)*ip);
648 } else if (ktr->ktr_code == SYS_ptrace) {
649 if (*ip < (register_t)(sizeof(ptrace_ops) /
650 sizeof(ptrace_ops[0])) && *ip >= 0)
651 (void)printf("(%s", ptrace_ops[*ip]);
653 else if (*ip == PT_GETREGS)
654 (void)printf("(%s", "PT_GETREGS");
657 else if (*ip == PT_SETREGS)
658 (void)printf("(%s", "PT_SETREGS");
661 else if (*ip == PT_GETFPREGS)
662 (void)printf("(%s", "PT_GETFPREGS");
665 else if (*ip == PT_SETFPREGS)
666 (void)printf("(%s", "PT_SETFPREGS");
669 else if (*ip == PT_GETDBREGS)
670 (void)printf("(%s", "PT_GETDBREGS");
673 else if (*ip == PT_SETDBREGS)
674 (void)printf("(%s", "PT_SETDBREGS");
677 (void)printf("(%ld", (long)*ip);
684 print_number(ip,narg,c);
692 ktrsysret(struct ktr_sysret *ktr)
694 register_t ret = ktr->ktr_retval;
695 int error = ktr->ktr_error;
696 int code = ktr->ktr_code;
698 if (code >= nsyscalls || code < 0)
699 (void)printf("[%d] ", code);
701 (void)printf("%s ", syscallnames[code]);
705 (void)printf("%ld", (long)ret);
706 if (ret < 0 || ret > 9)
707 (void)printf("/%#lx", (long)ret);
710 (void)printf("%ld", (long)ret);
712 (void)printf("%#lx", (long)ret);
714 } else if (error == ERESTART)
715 (void)printf("RESTART");
716 else if (error == EJUSTRETURN)
717 (void)printf("JUSTRETURN");
719 (void)printf("-1 errno %d", ktr->ktr_error);
721 (void)printf(" %s", strerror(ktr->ktr_error));
727 ktrnamei(char *cp, int len)
729 (void)printf("\"%.*s\"\n", len, cp);
733 ktrgenio(struct ktr_genio *ktr, int len)
735 int datalen = len - sizeof (struct ktr_genio);
736 char *dp = (char *)ktr + sizeof (struct ktr_genio);
741 static int screenwidth = 0;
743 if (screenwidth == 0) {
746 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
748 screenwidth = ws.ws_col;
752 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
753 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
754 datalen == 1 ? "" : "s");
755 if (maxdata && datalen > maxdata)
759 for (;datalen > 0; datalen--, dp++) {
760 (void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
763 * Keep track of printables and
764 * space chars (like fold(1)).
776 width = 8 - (col&07);
781 if (col + width > (screenwidth-2)) {
782 (void)printf("\\\n\t");
787 (void)putchar(*cp++);
792 (void)printf("\"\n");
795 const char *signames[NSIG] = {
796 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
797 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
798 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
799 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
800 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
801 "USR2", NULL, /* 31 - 32 */
805 ktrpsig(struct ktr_psig *psig)
807 (void)printf("SIG%s ", signames[psig->signo]);
808 if (psig->action == SIG_DFL)
809 (void)printf("SIG_DFL\n");
811 (void)printf("caught handler=0x%lx mask=0x%x code=0x%x\n",
812 (u_long)psig->action, psig->mask.__bits[0], psig->code);
816 ktrcsw(struct ktr_csw *cs)
818 (void)printf("%s %s\n", cs->out ? "stop" : "resume",
819 cs->user ? "user" : "kernel");
822 #define UTRACE_DLOPEN_START 1
823 #define UTRACE_DLOPEN_STOP 2
824 #define UTRACE_DLCLOSE_START 3
825 #define UTRACE_DLCLOSE_STOP 4
826 #define UTRACE_LOAD_OBJECT 5
827 #define UTRACE_UNLOAD_OBJECT 6
828 #define UTRACE_ADD_RUNDEP 7
829 #define UTRACE_PRELOAD_FINISHED 8
830 #define UTRACE_INIT_CALL 9
831 #define UTRACE_FINI_CALL 10
834 char sig[4]; /* 'RTLD' */
840 char name[MAXPATHLEN];
844 ktruser_rtld(int len, unsigned char *p)
846 struct utrace_rtld *ut = (struct utrace_rtld *)p;
851 case UTRACE_DLOPEN_START:
853 printf("dlopen(%s, ", ut->name);
854 switch (mode & RTLD_MODEMASK) {
862 printf("%#x", mode & RTLD_MODEMASK);
864 if (mode & RTLD_GLOBAL)
865 printf(" | RTLD_GLOBAL");
866 if (mode & RTLD_TRACE)
867 printf(" | RTLD_TRACE");
868 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
869 printf(" | %#x", mode &
870 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
873 case UTRACE_DLOPEN_STOP:
874 printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
877 case UTRACE_DLCLOSE_START:
878 printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
881 case UTRACE_DLCLOSE_STOP:
882 printf("dlclose(%p) finished\n", ut->handle);
884 case UTRACE_LOAD_OBJECT:
885 printf("RTLD: loaded %p @ %p - %p (%s)\n", ut->handle,
886 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
889 case UTRACE_UNLOAD_OBJECT:
890 printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
891 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
894 case UTRACE_ADD_RUNDEP:
895 parent = ut->mapbase;
896 printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
897 ut->handle, ut->name, ut->refcnt);
899 case UTRACE_PRELOAD_FINISHED:
900 printf("RTLD: LD_PRELOAD finished\n");
902 case UTRACE_INIT_CALL:
903 printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
906 case UTRACE_FINI_CALL:
907 printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
913 printf("RTLD: %d ", len);
918 printf(" %02x", *p++);
923 struct utrace_malloc {
930 ktruser_malloc(int len __unused, unsigned char *p)
932 struct utrace_malloc *ut = (struct utrace_malloc *)p;
935 if (ut->s == 0 && ut->r == NULL)
936 printf("malloc_init()\n");
938 printf("%p = malloc(%zu)\n", ut->r, ut->s);
941 printf("free(%p)\n", ut->p);
943 printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
948 ktruser(int len, unsigned char *p)
951 if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
952 ktruser_rtld(len, p);
956 if (len == sizeof(struct utrace_malloc)) {
957 ktruser_malloc(len, p);
961 (void)printf("%d ", len);
963 (void)printf(" %02x", *p++);
970 (void)fprintf(stderr,
971 "usage: kdump [-dnlRT] [-f trfile] [-m maxdata] [-t [cnisuw]] [-p pid]\n");
976 timevalsub(struct timeval *t1, struct timeval *t2)
978 t1->tv_sec -= t2->tv_sec;
979 t1->tv_usec -= t2->tv_usec;
984 timevalfix(struct timeval *t1)
986 if (t1->tv_usec < 0) {
988 t1->tv_usec += 1000000;
990 if (t1->tv_usec >= 1000000) {
992 t1->tv_usec -= 1000000;