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;
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 ||
313 ktr->ktr_code == SYS_mq_open) {
316 print_number(ip,narg,c);
320 flagsandmodename (flags, mode, decimal);
323 } else if (ktr->ktr_code == SYS_wait4) {
324 print_number(ip,narg,c);
325 print_number(ip,narg,c);
327 wait4optname ((int)*ip);
330 } else if (ktr->ktr_code == SYS_chmod ||
331 ktr->ktr_code == SYS_fchmod ||
332 ktr->ktr_code == SYS_lchmod) {
333 print_number(ip,narg,c);
338 } else if (ktr->ktr_code == SYS_mknod) {
339 print_number(ip,narg,c);
344 } else if (ktr->ktr_code == SYS_getfsstat) {
345 print_number(ip,narg,c);
346 print_number(ip,narg,c);
348 getfsstatflagsname ((int)*ip);
351 } else if (ktr->ktr_code == SYS_mount) {
352 print_number(ip,narg,c);
353 print_number(ip,narg,c);
355 mountflagsname ((int)*ip);
358 } else if (ktr->ktr_code == SYS_unmount) {
359 print_number(ip,narg,c);
361 mountflagsname ((int)*ip);
364 } else if (ktr->ktr_code == SYS_recvmsg ||
365 ktr->ktr_code == SYS_sendmsg) {
366 print_number(ip,narg,c);
367 print_number(ip,narg,c);
369 sendrecvflagsname ((int)*ip);
372 } else if (ktr->ktr_code == SYS_recvfrom ||
373 ktr->ktr_code == SYS_sendto) {
374 print_number(ip,narg,c);
375 print_number(ip,narg,c);
376 print_number(ip,narg,c);
378 sendrecvflagsname ((int)*ip);
381 } else if (ktr->ktr_code == SYS_chflags ||
382 ktr->ktr_code == SYS_fchflags) {
383 print_number(ip,narg,c);
388 } else if (ktr->ktr_code == SYS_kill) {
389 print_number(ip,narg,c);
394 } else if (ktr->ktr_code == SYS_reboot) {
396 rebootoptname((int)*ip);
399 } else if (ktr->ktr_code == SYS_umask) {
404 } else if (ktr->ktr_code == SYS_msync) {
405 print_number(ip,narg,c);
406 print_number(ip,narg,c);
408 msyncflagsname((int)*ip);
411 } else if (ktr->ktr_code == SYS_mmap) {
412 print_number(ip,narg,c);
413 print_number(ip,narg,c);
415 mmapprotname ((int)*ip);
419 mmapflagsname ((int)*ip);
422 } else if (ktr->ktr_code == SYS_mprotect) {
423 print_number(ip,narg,c);
424 print_number(ip,narg,c);
426 mmapprotname ((int)*ip);
429 } else if (ktr->ktr_code == SYS_madvise) {
430 print_number(ip,narg,c);
431 print_number(ip,narg,c);
433 madvisebehavname((int)*ip);
436 } else if (ktr->ktr_code == SYS_setpriority) {
442 print_number(ip,narg,c);
443 print_number(ip,narg,c);
444 } else if (ktr->ktr_code == SYS_fcntl) {
447 print_number(ip,narg,c);
451 fcntlcmdname(cmd, arg, decimal);
454 } else if (ktr->ktr_code == SYS_socket) {
456 sockdomainname((int)*ip);
460 socktypename((int)*ip);
464 } else if (ktr->ktr_code == SYS_setsockopt ||
465 ktr->ktr_code == SYS_getsockopt) {
466 print_number(ip,narg,c);
468 sockoptlevelname((int)*ip, decimal);
472 sockoptname((int)*ip);
475 } else if (ktr->ktr_code == SYS_lseek) {
476 print_number(ip,narg,c);
477 /* Hidden 'pad' argument, not in lseek(2) */
478 print_number(ip,narg,c);
479 print_number(ip,narg,c);
481 whencename ((int)*ip);
484 } else if (ktr->ktr_code == SYS_flock) {
485 print_number(ip,narg,c);
490 } else if (ktr->ktr_code == SYS_mkfifo ||
491 ktr->ktr_code == SYS_mkdir) {
492 print_number(ip,narg,c);
497 } else if (ktr->ktr_code == SYS_shutdown) {
498 print_number(ip,narg,c);
500 shutdownhowname((int)*ip);
503 } else if (ktr->ktr_code == SYS_socketpair) {
505 sockdomainname((int)*ip);
509 socktypename((int)*ip);
513 } else if (ktr->ktr_code == SYS_getrlimit ||
514 ktr->ktr_code == SYS_setrlimit) {
516 rlimitname((int)*ip);
520 } else if (ktr->ktr_code == SYS_quotactl) {
521 print_number(ip,narg,c);
522 quotactlname((int)*ip);
526 } else if (ktr->ktr_code == SYS_rtprio) {
528 rtprioname((int)*ip);
532 } else if (ktr->ktr_code == SYS___semctl) {
533 print_number(ip,narg,c);
534 print_number(ip,narg,c);
535 semctlname((int)*ip);
538 } else if (ktr->ktr_code == SYS_semget) {
539 print_number(ip,narg,c);
540 print_number(ip,narg,c);
541 semgetname((int)*ip);
544 } else if (ktr->ktr_code == SYS_msgctl) {
545 print_number(ip,narg,c);
546 shmctlname((int)*ip);
549 } else if (ktr->ktr_code == SYS_shmat) {
550 print_number(ip,narg,c);
551 print_number(ip,narg,c);
555 } else if (ktr->ktr_code == SYS_shmctl) {
556 print_number(ip,narg,c);
557 shmctlname((int)*ip);
560 } else if (ktr->ktr_code == SYS_minherit) {
561 print_number(ip,narg,c);
562 print_number(ip,narg,c);
563 minheritname((int)*ip);
566 } else if (ktr->ktr_code == SYS_rfork) {
572 } else if (ktr->ktr_code == SYS_lio_listio) {
574 lio_listioname((int)*ip);
578 } else if (ktr->ktr_code == SYS_mlockall) {
580 mlockallname((int)*ip);
583 } else if (ktr->ktr_code == SYS_sched_setscheduler) {
584 print_number(ip,narg,c);
585 schedpolicyname((int)*ip);
588 } else if (ktr->ktr_code == SYS_sched_get_priority_max ||
589 ktr->ktr_code == SYS_sched_get_priority_min) {
591 schedpolicyname((int)*ip);
594 } else if (ktr->ktr_code == SYS_sendfile) {
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 print_number(ip,narg,c);
601 sendfileflagsname((int)*ip);
604 } else if (ktr->ktr_code == SYS_kldsym) {
605 print_number(ip,narg,c);
606 kldsymcmdname((int)*ip);
609 } else if (ktr->ktr_code == SYS_sigprocmask) {
611 sigprocmaskhowname((int)*ip);
615 } else if (ktr->ktr_code == SYS___acl_get_file ||
616 ktr->ktr_code == SYS___acl_set_file ||
617 ktr->ktr_code == SYS___acl_get_fd ||
618 ktr->ktr_code == SYS___acl_set_fd ||
619 ktr->ktr_code == SYS___acl_delete_file ||
620 ktr->ktr_code == SYS___acl_delete_fd ||
621 ktr->ktr_code == SYS___acl_aclcheck_file ||
622 ktr->ktr_code == SYS___acl_aclcheck_fd) {
623 print_number(ip,narg,c);
624 acltypename((int)*ip);
627 } else if (ktr->ktr_code == SYS_sigaction) {
633 } else if (ktr->ktr_code == SYS_extattrctl) {
634 print_number(ip,narg,c);
635 extattrctlname((int)*ip);
638 } else if (ktr->ktr_code == SYS_ptrace) {
639 if (*ip < (register_t)(sizeof(ptrace_ops) /
640 sizeof(ptrace_ops[0])) && *ip >= 0)
641 (void)printf("(%s", ptrace_ops[*ip]);
643 else if (*ip == PT_GETREGS)
644 (void)printf("(%s", "PT_GETREGS");
647 else if (*ip == PT_SETREGS)
648 (void)printf("(%s", "PT_SETREGS");
651 else if (*ip == PT_GETFPREGS)
652 (void)printf("(%s", "PT_GETFPREGS");
655 else if (*ip == PT_SETFPREGS)
656 (void)printf("(%s", "PT_SETFPREGS");
659 else if (*ip == PT_GETDBREGS)
660 (void)printf("(%s", "PT_GETDBREGS");
663 else if (*ip == PT_SETDBREGS)
664 (void)printf("(%s", "PT_SETDBREGS");
667 (void)printf("(%ld", (long)*ip);
674 print_number(ip,narg,c);
682 ktrsysret(struct ktr_sysret *ktr)
684 register_t ret = ktr->ktr_retval;
685 int error = ktr->ktr_error;
686 int code = ktr->ktr_code;
688 if (code >= nsyscalls || code < 0)
689 (void)printf("[%d] ", code);
691 (void)printf("%s ", syscallnames[code]);
695 (void)printf("%ld", (long)ret);
696 if (ret < 0 || ret > 9)
697 (void)printf("/%#lx", (long)ret);
700 (void)printf("%ld", (long)ret);
702 (void)printf("%#lx", (long)ret);
704 } else if (error == ERESTART)
705 (void)printf("RESTART");
706 else if (error == EJUSTRETURN)
707 (void)printf("JUSTRETURN");
709 (void)printf("-1 errno %d", ktr->ktr_error);
711 (void)printf(" %s", strerror(ktr->ktr_error));
717 ktrnamei(char *cp, int len)
719 (void)printf("\"%.*s\"\n", len, cp);
723 ktrgenio(struct ktr_genio *ktr, int len)
725 int datalen = len - sizeof (struct ktr_genio);
726 char *dp = (char *)ktr + sizeof (struct ktr_genio);
731 static int screenwidth = 0;
733 if (screenwidth == 0) {
736 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
738 screenwidth = ws.ws_col;
742 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
743 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
744 datalen == 1 ? "" : "s");
745 if (maxdata && datalen > maxdata)
749 for (;datalen > 0; datalen--, dp++) {
750 (void) vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
753 * Keep track of printables and
754 * space chars (like fold(1)).
766 width = 8 - (col&07);
771 if (col + width > (screenwidth-2)) {
772 (void)printf("\\\n\t");
777 (void)putchar(*cp++);
782 (void)printf("\"\n");
785 const char *signames[NSIG] = {
786 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
787 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
788 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
789 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
790 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
791 "USR2", NULL, /* 31 - 32 */
795 ktrpsig(struct ktr_psig *psig)
797 (void)printf("SIG%s ", signames[psig->signo]);
798 if (psig->action == SIG_DFL)
799 (void)printf("SIG_DFL\n");
801 (void)printf("caught handler=0x%lx mask=0x%x code=0x%x\n",
802 (u_long)psig->action, psig->mask.__bits[0], psig->code);
806 ktrcsw(struct ktr_csw *cs)
808 (void)printf("%s %s\n", cs->out ? "stop" : "resume",
809 cs->user ? "user" : "kernel");
812 #define UTRACE_DLOPEN_START 1
813 #define UTRACE_DLOPEN_STOP 2
814 #define UTRACE_DLCLOSE_START 3
815 #define UTRACE_DLCLOSE_STOP 4
816 #define UTRACE_LOAD_OBJECT 5
817 #define UTRACE_UNLOAD_OBJECT 6
818 #define UTRACE_ADD_RUNDEP 7
819 #define UTRACE_PRELOAD_FINISHED 8
820 #define UTRACE_INIT_CALL 9
821 #define UTRACE_FINI_CALL 10
824 char sig[4]; /* 'RTLD' */
830 char name[MAXPATHLEN];
834 ktruser_rtld(int len, unsigned char *p)
836 struct utrace_rtld *ut = (struct utrace_rtld *)p;
841 case UTRACE_DLOPEN_START:
843 printf("dlopen(%s, ", ut->name);
844 switch (mode & RTLD_MODEMASK) {
852 printf("%#x", mode & RTLD_MODEMASK);
854 if (mode & RTLD_GLOBAL)
855 printf(" | RTLD_GLOBAL");
856 if (mode & RTLD_TRACE)
857 printf(" | RTLD_TRACE");
858 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
859 printf(" | %#x", mode &
860 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
863 case UTRACE_DLOPEN_STOP:
864 printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
867 case UTRACE_DLCLOSE_START:
868 printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
871 case UTRACE_DLCLOSE_STOP:
872 printf("dlclose(%p) finished\n", ut->handle);
874 case UTRACE_LOAD_OBJECT:
875 printf("RTLD: loaded %p @ %p - %p (%s)\n", ut->handle,
876 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
879 case UTRACE_UNLOAD_OBJECT:
880 printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
881 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
884 case UTRACE_ADD_RUNDEP:
885 parent = ut->mapbase;
886 printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
887 ut->handle, ut->name, ut->refcnt);
889 case UTRACE_PRELOAD_FINISHED:
890 printf("RTLD: LD_PRELOAD finished\n");
892 case UTRACE_INIT_CALL:
893 printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
896 case UTRACE_FINI_CALL:
897 printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
903 printf("RTLD: %d ", len);
908 printf(" %02x", *p++);
913 struct utrace_malloc {
920 ktruser_malloc(int len __unused, unsigned char *p)
922 struct utrace_malloc *ut = (struct utrace_malloc *)p;
925 if (ut->s == 0 && ut->r == NULL)
926 printf("malloc_init()\n");
928 printf("%p = malloc(%zu)\n", ut->r, ut->s);
931 printf("free(%p)\n", ut->p);
933 printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
938 ktruser(int len, unsigned char *p)
941 if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
942 ktruser_rtld(len, p);
946 if (len == sizeof(struct utrace_malloc)) {
947 ktruser_malloc(len, p);
951 (void)printf("%d ", len);
953 (void)printf(" %02x", *p++);
960 (void)fprintf(stderr,
961 "usage: kdump [-dnlRT] [-f trfile] [-m maxdata] [-t [cnisuw]] [-p pid]\n");
966 timevalsub(struct timeval *t1, struct timeval *t2)
968 t1->tv_sec -= t2->tv_sec;
969 t1->tv_usec -= t2->tv_usec;
974 timevalfix(struct timeval *t1)
976 if (t1->tv_usec < 0) {
978 t1->tv_usec += 1000000;
980 if (t1->tv_usec >= 1000000) {
982 t1->tv_usec -= 1000000;