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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1988, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)kdump.c 8.1 (Berkeley) 6/6/93";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
46 #include <sys/errno.h>
48 #include <sys/param.h>
49 #include <sys/capsicum.h>
50 #include <sys/errno.h>
55 #include <sys/ktrace.h>
56 #include <sys/ioctl.h>
57 #include <sys/socket.h>
59 #include <sys/sysent.h>
62 #include <sys/queue.h>
64 #include <arpa/inet.h>
65 #include <netinet/in.h>
71 #ifdef HAVE_LIBCAPSICUM
72 #include <libcapsicum.h>
73 #include <libcapsicum_grp.h>
74 #include <libcapsicum_pwd.h>
75 #include <libcapsicum_service.h>
80 #ifdef HAVE_LIBCAPSICUM
92 #include "kdump_subr.h"
94 u_int abidump(struct ktr_header *);
95 int fetchprocinfo(struct ktr_header *, u_int *);
96 int fread_tail(void *, int, int);
97 void dumpheader(struct ktr_header *);
98 void ktrsyscall(struct ktr_syscall *, u_int);
99 void ktrsysret(struct ktr_sysret *, u_int);
100 void ktrnamei(char *, int);
101 void hexdump(char *, int, int);
102 void visdump(char *, int, int);
103 void ktrgenio(struct ktr_genio *, int);
104 void ktrpsig(struct ktr_psig *);
105 void ktrcsw(struct ktr_csw *);
106 void ktrcsw_old(struct ktr_csw_old *);
107 void ktruser_malloc(void *);
108 void ktruser_rtld(int, void *);
109 void ktruser(int, void *);
110 void ktrcaprights(cap_rights_t *);
111 void ktrsockaddr(struct sockaddr *);
112 void ktrstat(struct stat *);
113 void ktrstruct(char *, size_t);
114 void ktrcapfail(struct ktr_cap_fail *);
115 void ktrfault(struct ktr_fault *);
116 void ktrfaultend(struct ktr_faultend *);
117 void limitfd(int fd);
119 void ioctlname(unsigned long, int);
121 #define TIMESTAMP_NONE 0x0
122 #define TIMESTAMP_ABSOLUTE 0x1
123 #define TIMESTAMP_ELAPSED 0x2
124 #define TIMESTAMP_RELATIVE 0x4
126 extern const char *signames[], *syscallnames[];
127 extern int nsyscalls;
129 static int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
130 resolv = 0, abiflag = 0, syscallno = 0;
131 static const char *tracefile = DEF_TRACEFILE;
132 static struct ktr_header ktr_header;
134 #define TIME_FORMAT "%b %e %T %Y"
135 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
137 #define print_number(i,n,c) do { \
139 printf("%c%jd", c, (intmax_t)*i); \
141 printf("%c%#jx", c, (uintmax_t)(u_register_t)*i); \
147 #if defined(__amd64__) || defined(__i386__)
149 void linux_ktrsyscall(struct ktr_syscall *);
150 void linux_ktrsysret(struct ktr_sysret *);
151 extern const char *linux_syscallnames[];
153 #include <linux_syscalls.c>
154 static int nlinux_syscalls = sizeof(linux_syscallnames) / \
155 sizeof(linux_syscallnames[0]);
159 * Linux syscalls return negative errno's, we do positive and map them
161 static int bsd_to_linux_errno[ELAST + 1] = {
162 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9,
163 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
164 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
165 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
166 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
167 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
168 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
169 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9,
170 -6, -6, -43, -42, -75,-125, -84, -95, -16, -74,
177 TAILQ_ENTRY(proc_info) info;
182 static TAILQ_HEAD(trace_procs, proc_info) trace_procs;
184 #ifdef HAVE_LIBCAPSICUM
185 static cap_channel_t *cappwd, *capgrp;
193 * Cache NLS data before entering capability mode.
194 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
196 (void)catopen("libc", NL_CAT_LOCALE);
205 * Allow localtime(3) to cache /etc/localtime content before entering
207 * XXXPJD: There should be localtime_init() in libc.
210 (void)localtime(<ime);
213 #ifdef HAVE_LIBCAPSICUM
215 cappwdgrp_setup(cap_channel_t **cappwdp, cap_channel_t **capgrpp)
217 cap_channel_t *capcas, *cappwdloc, *capgrploc;
218 const char *cmds[1], *fields[1];
221 if (capcas == NULL) {
222 warn("unable to contact casperd");
225 cappwdloc = cap_service_open(capcas, "system.pwd");
226 capgrploc = cap_service_open(capcas, "system.grp");
227 /* Casper capability no longer needed. */
229 if (cappwdloc == NULL || capgrploc == NULL) {
230 if (cappwdloc == NULL)
231 warn("unable to open system.pwd service");
232 if (capgrploc == NULL)
233 warn("unable to open system.grp service");
236 /* Limit system.pwd to only getpwuid() function and pw_name field. */
237 cmds[0] = "getpwuid";
238 if (cap_pwd_limit_cmds(cappwdloc, cmds, 1) < 0)
239 err(1, "unable to limit system.pwd service");
240 fields[0] = "pw_name";
241 if (cap_pwd_limit_fields(cappwdloc, fields, 1) < 0)
242 err(1, "unable to limit system.pwd service");
243 /* Limit system.grp to only getgrgid() function and gr_name field. */
244 cmds[0] = "getgrgid";
245 if (cap_grp_limit_cmds(capgrploc, cmds, 1) < 0)
246 err(1, "unable to limit system.grp service");
247 fields[0] = "gr_name";
248 if (cap_grp_limit_fields(capgrploc, fields, 1) < 0)
249 err(1, "unable to limit system.grp service");
251 *cappwdp = cappwdloc;
252 *capgrpp = capgrploc;
255 #endif /* HAVE_LIBCAPSICUM */
258 main(int argc, char *argv[])
260 int ch, ktrlen, size;
262 int trpoints = ALL_POINTS;
267 setlocale(LC_CTYPE, "");
269 timestamp = TIMESTAMP_NONE;
271 while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1)
286 maxdata = atoi(optarg);
304 timestamp |= TIMESTAMP_ELAPSED;
310 timestamp |= TIMESTAMP_RELATIVE;
313 timestamp |= TIMESTAMP_ABSOLUTE;
316 trpoints = getpoints(optarg);
318 errx(1, "unknown trace point in %s", optarg);
327 m = malloc(size = 1025);
329 errx(1, "%s", strerror(ENOMEM));
330 if (!freopen(tracefile, "r", stdin))
331 err(1, "%s", tracefile);
335 #ifdef HAVE_LIBCAPSICUM
337 if (cappwdgrp_setup(&cappwd, &capgrp) < 0) {
342 if (resolv == 0 || (cappwd != NULL && capgrp != NULL)) {
343 if (cap_enter() < 0 && errno != ENOSYS)
344 err(1, "unable to enter capability mode");
348 if (cap_enter() < 0 && errno != ENOSYS)
349 err(1, "unable to enter capability mode");
352 limitfd(STDIN_FILENO);
353 limitfd(STDOUT_FILENO);
354 limitfd(STDERR_FILENO);
356 TAILQ_INIT(&trace_procs);
358 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
359 if (ktr_header.ktr_type & KTR_DROP) {
360 ktr_header.ktr_type &= ~KTR_DROP;
361 if (!drop_logged && threads) {
363 "%6jd %6jd %-8.*s Events dropped.\n",
364 (intmax_t)ktr_header.ktr_pid,
365 ktr_header.ktr_tid > 0 ?
366 (intmax_t)ktr_header.ktr_tid : 0,
367 MAXCOMLEN, ktr_header.ktr_comm);
369 } else if (!drop_logged) {
370 printf("%6jd %-8.*s Events dropped.\n",
371 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
372 ktr_header.ktr_comm);
376 if (trpoints & (1<<ktr_header.ktr_type))
377 if (pid == 0 || ktr_header.ktr_pid == pid ||
378 ktr_header.ktr_tid == pid)
379 dumpheader(&ktr_header);
380 if ((ktrlen = ktr_header.ktr_len) < 0)
381 errx(1, "bogus length 0x%x", ktrlen);
383 m = realloc(m, ktrlen+1);
385 errx(1, "%s", strerror(ENOMEM));
388 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
389 errx(1, "data too short");
390 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
392 sv_flags = abidump(&ktr_header);
393 if (pid && ktr_header.ktr_pid != pid &&
394 ktr_header.ktr_tid != pid)
396 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
399 switch (ktr_header.ktr_type) {
401 #if defined(__amd64__) || defined(__i386__)
402 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
403 linux_ktrsyscall((struct ktr_syscall *)m);
406 ktrsyscall((struct ktr_syscall *)m, sv_flags);
409 #if defined(__amd64__) || defined(__i386__)
410 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
411 linux_ktrsysret((struct ktr_sysret *)m);
414 ktrsysret((struct ktr_sysret *)m, sv_flags);
421 ktrgenio((struct ktr_genio *)m, ktrlen);
424 ktrpsig((struct ktr_psig *)m);
427 if (ktrlen == sizeof(struct ktr_csw_old))
428 ktrcsw_old((struct ktr_csw_old *)m);
430 ktrcsw((struct ktr_csw *)m);
436 ktrstruct(m, ktrlen);
439 ktrcapfail((struct ktr_cap_fail *)m);
442 ktrfault((struct ktr_fault *)m);
445 ktrfaultend((struct ktr_faultend *)m);
463 cap_rights_init(&rights, CAP_FSTAT);
468 cap_rights_set(&rights, CAP_READ);
471 cap_rights_set(&rights, CAP_IOCTL, CAP_WRITE);
472 cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
475 cap_rights_set(&rights, CAP_WRITE);
477 cap_rights_set(&rights, CAP_IOCTL);
485 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
486 err(1, "unable to limit rights for descriptor %d", fd);
487 if (cmd != 0 && cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
488 err(1, "unable to limit ioctls for descriptor %d", fd);
492 fread_tail(void *buf, int size, int num)
496 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
504 fetchprocinfo(struct ktr_header *kth, u_int *flags)
506 struct proc_info *pi;
508 switch (kth->ktr_type) {
510 TAILQ_FOREACH(pi, &trace_procs, info) {
511 if (pi->pid == kth->ktr_pid) {
512 TAILQ_REMOVE(&trace_procs, pi, info);
516 pi = malloc(sizeof(struct proc_info));
518 errx(1, "%s", strerror(ENOMEM));
519 pi->sv_flags = *flags;
520 pi->pid = kth->ktr_pid;
521 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
525 TAILQ_FOREACH(pi, &trace_procs, info) {
526 if (pi->pid == kth->ktr_pid) {
527 TAILQ_REMOVE(&trace_procs, pi, info);
539 abidump(struct ktr_header *kth)
541 struct proc_info *pi;
546 TAILQ_FOREACH(pi, &trace_procs, info) {
547 if (pi->pid == kth->ktr_pid) {
548 flags = pi->sv_flags;
556 switch (flags & SV_ABI_MASK) {
576 printf("%s%s ", abi, arch);
582 dumpheader(struct ktr_header *kth)
584 static char unknown[64];
585 static struct timeval prevtime, prevtime_e, temp;
589 switch (kth->ktr_type) {
631 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
636 * The ktr_tid field was previously the ktr_buffer field, which held
637 * the kernel pointer value for the buffer associated with data
638 * following the record header. It now holds a threadid, but only
639 * for trace files after the change. Older trace files still contain
640 * kernel pointers. Detect this and suppress the results by printing
641 * negative tid's as 0.
644 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
645 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
646 MAXCOMLEN, kth->ktr_comm);
648 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
651 if (timestamp & TIMESTAMP_ABSOLUTE) {
652 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
653 kth->ktr_time.tv_usec);
655 if (timestamp & TIMESTAMP_ELAPSED) {
656 if (prevtime_e.tv_sec == 0)
657 prevtime_e = kth->ktr_time;
658 timevalsub(&kth->ktr_time, &prevtime_e);
659 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
660 kth->ktr_time.tv_usec);
661 timevaladd(&kth->ktr_time, &prevtime_e);
663 if (timestamp & TIMESTAMP_RELATIVE) {
664 if (prevtime.tv_sec == 0)
665 prevtime = kth->ktr_time;
666 temp = kth->ktr_time;
667 timevalsub(&kth->ktr_time, &prevtime);
668 if ((intmax_t)kth->ktr_time.tv_sec < 0) {
669 kth->ktr_time = prevtime;
671 timevalsub(&kth->ktr_time, &prevtime);
677 printf("%s%jd.%06ld ", sign, (intmax_t)kth->ktr_time.tv_sec,
678 kth->ktr_time.tv_usec);
684 #include <sys/syscall.h>
686 #include <sys/kern/syscalls.c>
688 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
691 ktrsyscall(struct ktr_syscall *ktr, u_int flags)
693 int narg = ktr->ktr_narg;
697 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
698 (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0))
699 printf("[%d]", ktr->ktr_code);
701 printf("%s", syscallnames[ktr->ktr_code]);
703 printf("[%d]", ktr->ktr_code);
705 ip = &ktr->ktr_args[0];
709 (flags == 0 || (flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
710 switch (ktr->ktr_code) {
728 atfdname(*ip, decimal);
734 switch (ktr->ktr_code) {
736 print_number(ip, narg, c);
738 ioctlname(*ip, decimal);
754 print_number(ip, narg, c);
762 print_number(ip, narg, c);
764 flagsandmodename(ip[0], ip[1], decimal);
769 print_number(ip, narg, c);
770 print_number(ip, narg, c);
772 * A flags value of zero is valid for
773 * wait4() but not for wait6(), so
774 * handle zero special here.
777 print_number(ip, narg, c);
787 idtypename(*ip, decimal);
791 print_number(ip, narg, c);
792 print_number(ip, narg, c);
801 print_number(ip, narg, c);
809 print_number(ip, narg, c);
816 print_number(ip, narg, c);
817 print_number(ip, narg, c);
819 getfsstatflagsname(*ip);
824 print_number(ip, narg, c);
825 print_number(ip, narg, c);
832 print_number(ip, narg, c);
840 print_number(ip, narg, c);
841 print_number(ip, narg, c);
843 sendrecvflagsname(*ip);
849 print_number(ip, narg, c);
850 print_number(ip, narg, c);
851 print_number(ip, narg, c);
853 sendrecvflagsname(*ip);
860 print_number(ip, narg, c);
867 print_number(ip, narg, c);
886 print_number(ip, narg, c);
887 print_number(ip, narg, c);
893 #ifdef SYS_freebsd6_mmap
894 case SYS_freebsd6_mmap:
895 print_number(ip, narg, c);
896 print_number(ip, narg, c);
908 print_number(ip, narg, c);
909 print_number(ip, narg, c);
920 print_number(ip, narg, c);
921 print_number(ip, narg, c);
928 print_number(ip, narg, c);
929 print_number(ip, narg, c);
931 madvisebehavname(*ip);
935 case SYS_setpriority:
936 print_number(ip, narg, c);
937 print_number(ip, narg, c);
944 print_number(ip, narg, c);
946 fcntlcmdname(ip[0], ip[1], decimal);
954 sockdomainname(sockdomain);
958 socktypenamewithflags(*ip);
961 if (sockdomain == PF_INET ||
962 sockdomain == PF_INET6) {
964 sockipprotoname(*ip);
973 print_number(ip, narg, c);
975 sockoptlevelname(*ip, decimal);
976 if (*ip == SOL_SOCKET) {
985 #ifdef SYS_freebsd6_lseek
986 case SYS_freebsd6_lseek:
987 print_number(ip, narg, c);
988 /* Hidden 'pad' argument, not in lseek(2) */
989 print_number(ip, narg, c);
990 print_number(ip, narg, c);
998 print_number(ip, narg, c);
999 /* Hidden 'pad' argument, not in lseek(2) */
1000 print_number(ip, narg, c);
1007 print_number(ip, narg, c);
1017 print_number(ip, narg, c);
1024 print_number(ip, narg, c);
1026 shutdownhowname(*ip);
1030 case SYS_socketpair:
1032 sockdomainname(*ip);
1036 socktypenamewithflags(*ip);
1050 print_number(ip, narg, c);
1072 print_number(ip, narg, c);
1073 print_number(ip, narg, c);
1080 print_number(ip, narg, c);
1081 print_number(ip, narg, c);
1088 print_number(ip, narg, c);
1095 print_number(ip, narg, c);
1096 print_number(ip, narg, c);
1103 print_number(ip, narg, c);
1110 print_number(ip, narg, c);
1113 printf(",0%o", (unsigned int)ip[1]);
1118 print_number(ip, narg, c);
1119 print_number(ip, narg, c);
1132 case SYS_lio_listio:
1134 lio_listioname(*ip);
1145 case SYS_sched_setscheduler:
1146 print_number(ip, narg, c);
1148 schedpolicyname(*ip);
1152 case SYS_sched_get_priority_max:
1153 case SYS_sched_get_priority_min:
1155 schedpolicyname(*ip);
1160 print_number(ip, narg, c);
1161 print_number(ip, narg, c);
1162 print_number(ip, narg, c);
1163 print_number(ip, narg, c);
1164 print_number(ip, narg, c);
1165 print_number(ip, narg, c);
1167 sendfileflagsname(*(int *)ip);
1172 print_number(ip, narg, c);
1178 case SYS_sigprocmask:
1180 sigprocmaskhowname(*ip);
1185 case SYS___acl_get_file:
1186 case SYS___acl_set_file:
1187 case SYS___acl_get_fd:
1188 case SYS___acl_set_fd:
1189 case SYS___acl_delete_file:
1190 case SYS___acl_delete_fd:
1191 case SYS___acl_aclcheck_file:
1192 case SYS___acl_aclcheck_fd:
1193 case SYS___acl_get_link:
1194 case SYS___acl_set_link:
1195 case SYS___acl_delete_link:
1196 case SYS___acl_aclcheck_link:
1197 print_number(ip, narg, c);
1210 case SYS_extattrctl:
1211 print_number(ip, narg, c);
1213 extattrctlname(*ip);
1218 print_number(ip, narg, c);
1219 print_number(ip, narg, c);
1221 mountflagsname(*ip);
1225 case SYS_thr_create:
1226 print_number(ip, narg, c);
1227 print_number(ip, narg, c);
1229 thrcreateflagsname(*ip);
1234 print_number(ip, narg, c);
1240 case SYS_kldunloadf:
1241 print_number(ip, narg, c);
1243 kldunloadfflagsname(*ip);
1250 print_number(ip, narg, c);
1252 atfdname(*ip, decimal);
1256 case SYS_cap_fcntls_limit:
1257 print_number(ip, narg, c);
1264 case SYS_posix_fadvise:
1265 print_number(ip, narg, c);
1266 print_number(ip, narg, c);
1267 print_number(ip, narg, c);
1269 fadvisebehavname((int)*ip);
1275 idtypename(*ip, decimal);
1279 print_number(ip, narg, c);
1281 procctlcmdname(*ip);
1286 print_number(ip, narg, c);
1290 case UMTX_OP_CV_WAIT:
1294 umtxcvwaitflags(*ip);
1296 case UMTX_OP_RW_RDLOCK:
1300 umtxrwlockflags(*ip);
1308 print_number(ip, narg, c);
1316 ktrsysret(struct ktr_sysret *ktr, u_int flags)
1318 register_t ret = ktr->ktr_retval;
1319 int error = ktr->ktr_error;
1320 int code = ktr->ktr_code;
1322 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
1323 (code >= nsyscalls || code < 0))
1324 printf("[%d] ", code);
1326 printf("%s", syscallnames[code]);
1328 printf("[%d]", code);
1334 printf("%ld", (long)ret);
1335 if (ret < 0 || ret > 9)
1336 printf("/%#lx", (unsigned long)ret);
1339 printf("%ld", (long)ret);
1341 printf("%#lx", (unsigned long)ret);
1343 } else if (error == ERESTART)
1345 else if (error == EJUSTRETURN)
1346 printf("JUSTRETURN");
1348 printf("-1 errno %d", ktr->ktr_error);
1350 printf(" %s", strerror(ktr->ktr_error));
1356 ktrnamei(char *cp, int len)
1358 printf("\"%.*s\"\n", len, cp);
1362 hexdump(char *p, int len, int screenwidth)
1370 i = 13; /* base offset */
1371 i += (width / 2) + 1; /* spaces every second byte */
1372 i += (width * 2); /* width of bytes */
1374 i += width; /* each byte */
1376 } while (i < screenwidth);
1379 for (n = 0; n < len; n += width) {
1380 for (i = n; i < n + width; i++) {
1381 if ((i % width) == 0) { /* beginning of line */
1382 printf(" 0x%04x", i);
1388 printf("%02x", p[i] & 0xff);
1393 for (i = n; i < n + width; i++) {
1396 if (p[i] >= ' ' && p[i] <= '~')
1403 if ((i % width) != 0)
1408 visdump(char *dp, int datalen, int screenwidth)
1417 for (;datalen > 0; datalen--, dp++) {
1418 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1421 * Keep track of printables and
1422 * space chars (like fold(1)).
1434 width = 8 - (col&07);
1439 if (col + width > (screenwidth-2)) {
1454 ktrgenio(struct ktr_genio *ktr, int len)
1456 int datalen = len - sizeof (struct ktr_genio);
1457 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1458 static int screenwidth = 0;
1461 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1462 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1463 datalen == 1 ? "" : "s");
1466 if (screenwidth == 0) {
1469 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1471 screenwidth = ws.ws_col;
1475 if (maxdata && datalen > maxdata)
1478 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1479 if (dp[i] >= 32 && dp[i] < 127)
1481 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1486 hexdump(dp, datalen, screenwidth);
1488 visdump(dp, datalen, screenwidth);
1491 const char *signames[] = {
1492 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
1493 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
1494 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
1495 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
1496 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
1497 "USR2", NULL, /* 31 - 32 */
1501 ktrpsig(struct ktr_psig *psig)
1503 if (psig->signo > 0 && psig->signo < NSIG)
1504 printf("SIG%s ", signames[psig->signo]);
1506 printf("SIG %d ", psig->signo);
1507 if (psig->action == SIG_DFL) {
1508 printf("SIG_DFL code=");
1509 sigcodename(psig->signo, psig->code);
1512 printf("caught handler=0x%lx mask=0x%x code=",
1513 (u_long)psig->action, psig->mask.__bits[0]);
1514 sigcodename(psig->signo, psig->code);
1520 ktrcsw_old(struct ktr_csw_old *cs)
1522 printf("%s %s\n", cs->out ? "stop" : "resume",
1523 cs->user ? "user" : "kernel");
1527 ktrcsw(struct ktr_csw *cs)
1529 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1530 cs->user ? "user" : "kernel", cs->wmesg);
1533 #define UTRACE_DLOPEN_START 1
1534 #define UTRACE_DLOPEN_STOP 2
1535 #define UTRACE_DLCLOSE_START 3
1536 #define UTRACE_DLCLOSE_STOP 4
1537 #define UTRACE_LOAD_OBJECT 5
1538 #define UTRACE_UNLOAD_OBJECT 6
1539 #define UTRACE_ADD_RUNDEP 7
1540 #define UTRACE_PRELOAD_FINISHED 8
1541 #define UTRACE_INIT_CALL 9
1542 #define UTRACE_FINI_CALL 10
1543 #define UTRACE_DLSYM_START 11
1544 #define UTRACE_DLSYM_STOP 12
1546 struct utrace_rtld {
1547 char sig[4]; /* 'RTLD' */
1553 char name[MAXPATHLEN];
1557 ktruser_rtld(int len, void *p)
1559 struct utrace_rtld *ut = p;
1564 switch (ut->event) {
1565 case UTRACE_DLOPEN_START:
1567 printf("dlopen(%s, ", ut->name);
1568 switch (mode & RTLD_MODEMASK) {
1573 printf("RTLD_LAZY");
1576 printf("%#x", mode & RTLD_MODEMASK);
1578 if (mode & RTLD_GLOBAL)
1579 printf(" | RTLD_GLOBAL");
1580 if (mode & RTLD_TRACE)
1581 printf(" | RTLD_TRACE");
1582 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
1583 printf(" | %#x", mode &
1584 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
1587 case UTRACE_DLOPEN_STOP:
1588 printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
1591 case UTRACE_DLCLOSE_START:
1592 printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
1595 case UTRACE_DLCLOSE_STOP:
1596 printf("dlclose(%p) finished\n", ut->handle);
1598 case UTRACE_LOAD_OBJECT:
1599 printf("RTLD: loaded %p @ %p - %p (%s)\n", ut->handle,
1600 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1603 case UTRACE_UNLOAD_OBJECT:
1604 printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
1605 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1608 case UTRACE_ADD_RUNDEP:
1609 parent = ut->mapbase;
1610 printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
1611 ut->handle, ut->name, ut->refcnt);
1613 case UTRACE_PRELOAD_FINISHED:
1614 printf("RTLD: LD_PRELOAD finished\n");
1616 case UTRACE_INIT_CALL:
1617 printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
1620 case UTRACE_FINI_CALL:
1621 printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
1624 case UTRACE_DLSYM_START:
1625 printf("RTLD: dlsym(%p, %s)\n", ut->handle, ut->name);
1627 case UTRACE_DLSYM_STOP:
1628 printf("RTLD: %p = dlsym(%p, %s)\n", ut->mapbase, ut->handle,
1635 printf("RTLD: %d ", len);
1638 printf(" %d", *cp++);
1640 printf(" %02x", *cp++);
1645 struct utrace_malloc {
1652 ktruser_malloc(void *p)
1654 struct utrace_malloc *ut = p;
1656 if (ut->p == (void *)(intptr_t)(-1))
1657 printf("malloc_init()\n");
1658 else if (ut->s == 0)
1659 printf("free(%p)\n", ut->p);
1660 else if (ut->p == NULL)
1661 printf("%p = malloc(%zu)\n", ut->r, ut->s);
1663 printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
1667 ktruser(int len, void *p)
1671 if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
1672 ktruser_rtld(len, p);
1676 if (len == sizeof(struct utrace_malloc)) {
1685 printf(" %d", *cp++);
1687 printf(" %02x", *cp++);
1692 ktrcaprights(cap_rights_t *rightsp)
1695 printf("cap_rights_t ");
1701 ktrsockaddr(struct sockaddr *sa)
1704 TODO: Support additional address families
1705 #include <netnatm/natm.h>
1706 struct sockaddr_natm *natm;
1707 #include <netsmb/netbios.h>
1708 struct sockaddr_nb *nb;
1713 * note: ktrstruct() has already verified that sa points to a
1714 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1715 * sa->sa_len bytes long.
1717 printf("struct sockaddr { ");
1718 sockfamilyname(sa->sa_family);
1721 #define check_sockaddr_len(n) \
1722 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1723 printf("invalid"); \
1727 switch(sa->sa_family) {
1729 struct sockaddr_in sa_in;
1731 memset(&sa_in, 0, sizeof(sa_in));
1732 memcpy(&sa_in, sa, sa->sa_len);
1733 check_sockaddr_len(in);
1734 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1735 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1739 struct sockaddr_in6 sa_in6;
1741 memset(&sa_in6, 0, sizeof(sa_in6));
1742 memcpy(&sa_in6, sa, sa->sa_len);
1743 check_sockaddr_len(in6);
1744 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1745 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1746 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1750 struct sockaddr_un sa_un;
1752 memset(&sa_un, 0, sizeof(sa_un));
1753 memcpy(&sa_un, sa, sa->sa_len);
1754 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1758 printf("unknown address family");
1764 ktrstat(struct stat *statp)
1766 char mode[12], timestr[PATH_MAX + 4];
1772 * note: ktrstruct() has already verified that statp points to a
1773 * buffer exactly sizeof(struct stat) bytes long.
1775 printf("struct stat {");
1776 printf("dev=%ju, ino=%ju, ",
1777 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1779 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1781 strmode(statp->st_mode, mode);
1782 printf("mode=%s, ", mode);
1784 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1788 #ifdef HAVE_LIBCAPSICUM
1790 pwd = cap_getpwuid(cappwd, statp->st_uid);
1793 pwd = getpwuid(statp->st_uid);
1796 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1798 printf("uid=\"%s\", ", pwd->pw_name);
1802 #ifdef HAVE_LIBCAPSICUM
1804 grp = cap_getgrgid(capgrp, statp->st_gid);
1807 grp = getgrgid(statp->st_gid);
1810 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1812 printf("gid=\"%s\", ", grp->gr_name);
1813 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1816 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1818 tm = localtime(&statp->st_atim.tv_sec);
1819 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1820 printf("\"%s\"", timestr);
1822 if (statp->st_atim.tv_nsec != 0)
1823 printf(".%09ld, ", statp->st_atim.tv_nsec);
1828 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1830 tm = localtime(&statp->st_mtim.tv_sec);
1831 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1832 printf("\"%s\"", timestr);
1834 if (statp->st_mtim.tv_nsec != 0)
1835 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1840 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1842 tm = localtime(&statp->st_ctim.tv_sec);
1843 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1844 printf("\"%s\"", timestr);
1846 if (statp->st_ctim.tv_nsec != 0)
1847 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1850 printf("birthtime=");
1852 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1854 tm = localtime(&statp->st_birthtim.tv_sec);
1855 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1856 printf("\"%s\"", timestr);
1858 if (statp->st_birthtim.tv_nsec != 0)
1859 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1862 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1863 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1864 (intmax_t)statp->st_blocks, statp->st_flags);
1869 ktrstruct(char *buf, size_t buflen)
1872 size_t namelen, datalen;
1874 cap_rights_t rights;
1876 struct sockaddr_storage ss;
1878 for (name = buf, namelen = 0;
1879 namelen < buflen && name[namelen] != '\0';
1882 if (namelen == buflen)
1884 if (name[namelen] != '\0')
1886 data = buf + namelen + 1;
1887 datalen = buflen - namelen - 1;
1891 for (i = 0; i < (int)namelen; ++i)
1892 if (!isalpha(name[i]))
1894 if (strcmp(name, "caprights") == 0) {
1895 if (datalen != sizeof(cap_rights_t))
1897 memcpy(&rights, data, datalen);
1898 ktrcaprights(&rights);
1899 } else if (strcmp(name, "stat") == 0) {
1900 if (datalen != sizeof(struct stat))
1902 memcpy(&sb, data, datalen);
1904 } else if (strcmp(name, "sockaddr") == 0) {
1905 if (datalen > sizeof(ss))
1907 memcpy(&ss, data, datalen);
1908 if (datalen != ss.ss_len)
1910 ktrsockaddr((struct sockaddr *)&ss);
1912 printf("unknown structure\n");
1916 printf("invalid record\n");
1920 ktrcapfail(struct ktr_cap_fail *ktr)
1922 switch (ktr->cap_type) {
1923 case CAPFAIL_NOTCAPABLE:
1924 /* operation on fd with insufficient capabilities */
1925 printf("operation requires ");
1926 capname(&ktr->cap_needed);
1927 printf(", descriptor holds ");
1928 capname(&ktr->cap_held);
1930 case CAPFAIL_INCREASE:
1931 /* requested more capabilities than fd already has */
1932 printf("attempt to increase capabilities from ");
1933 capname(&ktr->cap_held);
1935 capname(&ktr->cap_needed);
1937 case CAPFAIL_SYSCALL:
1938 /* called restricted syscall */
1939 printf("disallowed system call");
1941 case CAPFAIL_LOOKUP:
1942 /* used ".." in strict-relative mode */
1943 printf("restricted VFS lookup");
1946 printf("unknown capability failure: ");
1947 capname(&ktr->cap_needed);
1949 capname(&ktr->cap_held);
1956 ktrfault(struct ktr_fault *ktr)
1959 printf("0x%jx ", (uintmax_t)ktr->vaddr);
1960 vmprotname(ktr->type);
1965 ktrfaultend(struct ktr_faultend *ktr)
1968 vmresultname(ktr->result);
1972 #if defined(__amd64__) || defined(__i386__)
1974 linux_ktrsyscall(struct ktr_syscall *ktr)
1976 int narg = ktr->ktr_narg;
1979 if (ktr->ktr_code >= nlinux_syscalls || ktr->ktr_code < 0)
1980 printf("[%d]", ktr->ktr_code);
1982 printf("%s", linux_syscallnames[ktr->ktr_code]);
1984 printf("[%d]", ktr->ktr_code);
1986 ip = &ktr->ktr_args[0];
1990 print_number(ip, narg, c);
1997 linux_ktrsysret(struct ktr_sysret *ktr)
1999 register_t ret = ktr->ktr_retval;
2000 int error = ktr->ktr_error;
2001 int code = ktr->ktr_code;
2003 if (code >= nlinux_syscalls || code < 0)
2004 printf("[%d] ", code);
2006 printf("%s", linux_syscallnames[code]);
2008 printf("[%d]", code);
2014 printf("%ld", (long)ret);
2015 if (ret < 0 || ret > 9)
2016 printf("/%#lx", (unsigned long)ret);
2019 printf("%ld", (long)ret);
2021 printf("%#lx", (unsigned long)ret);
2023 } else if (error == ERESTART)
2025 else if (error == EJUSTRETURN)
2026 printf("JUSTRETURN");
2028 if (ktr->ktr_error <= ELAST + 1)
2029 error = abs(bsd_to_linux_errno[ktr->ktr_error]);
2032 printf("-1 errno %d", error);
2034 printf(" %s", strerror(ktr->ktr_error));
2043 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
2044 "[-m maxdata] [-p pid] [-t trstr]\n");