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 fprintf(stderr, "capability mode sandbox enabled\n");
358 TAILQ_INIT(&trace_procs);
360 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
361 if (ktr_header.ktr_type & KTR_DROP) {
362 ktr_header.ktr_type &= ~KTR_DROP;
363 if (!drop_logged && threads) {
365 "%6jd %6jd %-8.*s Events dropped.\n",
366 (intmax_t)ktr_header.ktr_pid,
367 ktr_header.ktr_tid > 0 ?
368 (intmax_t)ktr_header.ktr_tid : 0,
369 MAXCOMLEN, ktr_header.ktr_comm);
371 } else if (!drop_logged) {
372 printf("%6jd %-8.*s Events dropped.\n",
373 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
374 ktr_header.ktr_comm);
378 if (trpoints & (1<<ktr_header.ktr_type))
379 if (pid == 0 || ktr_header.ktr_pid == pid ||
380 ktr_header.ktr_tid == pid)
381 dumpheader(&ktr_header);
382 if ((ktrlen = ktr_header.ktr_len) < 0)
383 errx(1, "bogus length 0x%x", ktrlen);
385 m = realloc(m, ktrlen+1);
387 errx(1, "%s", strerror(ENOMEM));
390 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
391 errx(1, "data too short");
392 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
394 sv_flags = abidump(&ktr_header);
395 if (pid && ktr_header.ktr_pid != pid &&
396 ktr_header.ktr_tid != pid)
398 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
401 switch (ktr_header.ktr_type) {
403 #if defined(__amd64__) || defined(__i386__)
404 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
405 linux_ktrsyscall((struct ktr_syscall *)m);
408 ktrsyscall((struct ktr_syscall *)m, sv_flags);
411 #if defined(__amd64__) || defined(__i386__)
412 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
413 linux_ktrsysret((struct ktr_sysret *)m);
416 ktrsysret((struct ktr_sysret *)m, sv_flags);
423 ktrgenio((struct ktr_genio *)m, ktrlen);
426 ktrpsig((struct ktr_psig *)m);
429 if (ktrlen == sizeof(struct ktr_csw_old))
430 ktrcsw_old((struct ktr_csw_old *)m);
432 ktrcsw((struct ktr_csw *)m);
438 ktrstruct(m, ktrlen);
441 ktrcapfail((struct ktr_cap_fail *)m);
444 ktrfault((struct ktr_fault *)m);
447 ktrfaultend((struct ktr_faultend *)m);
465 cap_rights_init(&rights, CAP_FSTAT);
470 cap_rights_set(&rights, CAP_READ);
473 cap_rights_set(&rights, CAP_IOCTL, CAP_WRITE);
474 cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
477 cap_rights_set(&rights, CAP_WRITE);
479 cap_rights_set(&rights, CAP_IOCTL);
487 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
488 err(1, "unable to limit rights for descriptor %d", fd);
489 if (cmd != 0 && cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
490 err(1, "unable to limit ioctls for descriptor %d", fd);
494 fread_tail(void *buf, int size, int num)
498 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
506 fetchprocinfo(struct ktr_header *kth, u_int *flags)
508 struct proc_info *pi;
510 switch (kth->ktr_type) {
512 TAILQ_FOREACH(pi, &trace_procs, info) {
513 if (pi->pid == kth->ktr_pid) {
514 TAILQ_REMOVE(&trace_procs, pi, info);
518 pi = malloc(sizeof(struct proc_info));
520 errx(1, "%s", strerror(ENOMEM));
521 pi->sv_flags = *flags;
522 pi->pid = kth->ktr_pid;
523 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
527 TAILQ_FOREACH(pi, &trace_procs, info) {
528 if (pi->pid == kth->ktr_pid) {
529 TAILQ_REMOVE(&trace_procs, pi, info);
541 abidump(struct ktr_header *kth)
543 struct proc_info *pi;
548 TAILQ_FOREACH(pi, &trace_procs, info) {
549 if (pi->pid == kth->ktr_pid) {
550 flags = pi->sv_flags;
558 switch (flags & SV_ABI_MASK) {
578 printf("%s%s ", abi, arch);
584 dumpheader(struct ktr_header *kth)
586 static char unknown[64];
587 static struct timeval prevtime, prevtime_e, temp;
590 switch (kth->ktr_type) {
632 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
637 * The ktr_tid field was previously the ktr_buffer field, which held
638 * the kernel pointer value for the buffer associated with data
639 * following the record header. It now holds a threadid, but only
640 * for trace files after the change. Older trace files still contain
641 * kernel pointers. Detect this and suppress the results by printing
642 * negative tid's as 0.
645 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
646 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
647 MAXCOMLEN, kth->ktr_comm);
649 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
652 if (timestamp & TIMESTAMP_ABSOLUTE) {
653 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
654 kth->ktr_time.tv_usec);
656 if (timestamp & TIMESTAMP_ELAPSED) {
657 if (prevtime_e.tv_sec == 0)
658 prevtime_e = kth->ktr_time;
659 timevalsub(&kth->ktr_time, &prevtime_e);
660 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
661 kth->ktr_time.tv_usec);
662 timevaladd(&kth->ktr_time, &prevtime_e);
664 if (timestamp & TIMESTAMP_RELATIVE) {
665 temp = kth->ktr_time;
666 timevalsub(&kth->ktr_time, &prevtime);
668 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
669 kth->ktr_time.tv_usec);
675 #include <sys/syscall.h>
677 #include <sys/kern/syscalls.c>
679 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
682 ktrsyscall(struct ktr_syscall *ktr, u_int flags)
684 int narg = ktr->ktr_narg;
688 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
689 (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0))
690 printf("[%d]", ktr->ktr_code);
692 printf("%s", syscallnames[ktr->ktr_code]);
694 printf("[%d]", ktr->ktr_code);
696 ip = &ktr->ktr_args[0];
700 (flags == 0 || (flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
701 switch (ktr->ktr_code) {
719 atfdname(*ip, decimal);
725 switch (ktr->ktr_code) {
727 print_number(ip, narg, c);
729 ioctlname(*ip, decimal);
745 print_number(ip, narg, c);
753 print_number(ip, narg, c);
755 flagsandmodename(ip[0], ip[1], decimal);
760 print_number(ip, narg, c);
761 print_number(ip, narg, c);
763 * A flags value of zero is valid for
764 * wait4() but not for wait6(), so
765 * handle zero special here.
768 print_number(ip, narg, c);
778 idtypename(*ip, decimal);
782 print_number(ip, narg, c);
783 print_number(ip, narg, c);
792 print_number(ip, narg, c);
800 print_number(ip, narg, c);
807 print_number(ip, narg, c);
808 print_number(ip, narg, c);
810 getfsstatflagsname(*ip);
815 print_number(ip, narg, c);
816 print_number(ip, narg, c);
823 print_number(ip, narg, c);
831 print_number(ip, narg, c);
832 print_number(ip, narg, c);
834 sendrecvflagsname(*ip);
840 print_number(ip, narg, c);
841 print_number(ip, narg, c);
842 print_number(ip, narg, c);
844 sendrecvflagsname(*ip);
851 print_number(ip, narg, c);
858 print_number(ip, narg, c);
877 print_number(ip, narg, c);
878 print_number(ip, narg, c);
884 #ifdef SYS_freebsd6_mmap
885 case SYS_freebsd6_mmap:
886 print_number(ip, narg, c);
887 print_number(ip, narg, c);
899 print_number(ip, narg, c);
900 print_number(ip, narg, c);
911 print_number(ip, narg, c);
912 print_number(ip, narg, c);
919 print_number(ip, narg, c);
920 print_number(ip, narg, c);
922 madvisebehavname(*ip);
926 case SYS_setpriority:
927 print_number(ip, narg, c);
928 print_number(ip, narg, c);
935 print_number(ip, narg, c);
937 fcntlcmdname(ip[0], ip[1], decimal);
945 sockdomainname(sockdomain);
949 socktypenamewithflags(*ip);
952 if (sockdomain == PF_INET ||
953 sockdomain == PF_INET6) {
955 sockipprotoname(*ip);
964 print_number(ip, narg, c);
966 sockoptlevelname(*ip, decimal);
967 if (*ip == SOL_SOCKET) {
976 #ifdef SYS_freebsd6_lseek
977 case SYS_freebsd6_lseek:
978 print_number(ip, narg, c);
979 /* Hidden 'pad' argument, not in lseek(2) */
980 print_number(ip, narg, c);
981 print_number(ip, narg, c);
989 print_number(ip, narg, c);
990 /* Hidden 'pad' argument, not in lseek(2) */
991 print_number(ip, narg, c);
998 print_number(ip, narg, c);
1008 print_number(ip, narg, c);
1015 print_number(ip, narg, c);
1017 shutdownhowname(*ip);
1021 case SYS_socketpair:
1023 sockdomainname(*ip);
1027 socktypenamewithflags(*ip);
1041 print_number(ip, narg, c);
1063 print_number(ip, narg, c);
1064 print_number(ip, narg, c);
1071 print_number(ip, narg, c);
1072 print_number(ip, narg, c);
1079 print_number(ip, narg, c);
1086 print_number(ip, narg, c);
1087 print_number(ip, narg, c);
1094 print_number(ip, narg, c);
1101 print_number(ip, narg, c);
1104 printf(",0%o", (unsigned int)ip[1]);
1109 print_number(ip, narg, c);
1110 print_number(ip, narg, c);
1123 case SYS_lio_listio:
1125 lio_listioname(*ip);
1136 case SYS_sched_setscheduler:
1137 print_number(ip, narg, c);
1139 schedpolicyname(*ip);
1143 case SYS_sched_get_priority_max:
1144 case SYS_sched_get_priority_min:
1146 schedpolicyname(*ip);
1151 print_number(ip, narg, c);
1152 print_number(ip, narg, c);
1153 print_number(ip, narg, c);
1154 print_number(ip, narg, c);
1155 print_number(ip, narg, c);
1156 print_number(ip, narg, c);
1158 sendfileflagsname(*(int *)ip);
1163 print_number(ip, narg, c);
1169 case SYS_sigprocmask:
1171 sigprocmaskhowname(*ip);
1176 case SYS___acl_get_file:
1177 case SYS___acl_set_file:
1178 case SYS___acl_get_fd:
1179 case SYS___acl_set_fd:
1180 case SYS___acl_delete_file:
1181 case SYS___acl_delete_fd:
1182 case SYS___acl_aclcheck_file:
1183 case SYS___acl_aclcheck_fd:
1184 case SYS___acl_get_link:
1185 case SYS___acl_set_link:
1186 case SYS___acl_delete_link:
1187 case SYS___acl_aclcheck_link:
1188 print_number(ip, narg, c);
1201 case SYS_extattrctl:
1202 print_number(ip, narg, c);
1204 extattrctlname(*ip);
1209 print_number(ip, narg, c);
1210 print_number(ip, narg, c);
1212 mountflagsname(*ip);
1216 case SYS_thr_create:
1217 print_number(ip, narg, c);
1218 print_number(ip, narg, c);
1220 thrcreateflagsname(*ip);
1225 print_number(ip, narg, c);
1231 case SYS_kldunloadf:
1232 print_number(ip, narg, c);
1234 kldunloadfflagsname(*ip);
1241 print_number(ip, narg, c);
1243 atfdname(*ip, decimal);
1247 case SYS_cap_fcntls_limit:
1248 print_number(ip, narg, c);
1255 case SYS_posix_fadvise:
1256 print_number(ip, narg, c);
1257 print_number(ip, narg, c);
1258 print_number(ip, narg, c);
1260 fadvisebehavname((int)*ip);
1266 idtypename(*ip, decimal);
1270 print_number(ip, narg, c);
1272 procctlcmdname(*ip);
1277 print_number(ip, narg, c);
1281 case UMTX_OP_CV_WAIT:
1285 umtxcvwaitflags(*ip);
1287 case UMTX_OP_RW_RDLOCK:
1291 umtxrwlockflags(*ip);
1299 print_number(ip, narg, c);
1307 ktrsysret(struct ktr_sysret *ktr, u_int flags)
1309 register_t ret = ktr->ktr_retval;
1310 int error = ktr->ktr_error;
1311 int code = ktr->ktr_code;
1313 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
1314 (code >= nsyscalls || code < 0))
1315 printf("[%d] ", code);
1317 printf("%s", syscallnames[code]);
1319 printf("[%d]", code);
1325 printf("%ld", (long)ret);
1326 if (ret < 0 || ret > 9)
1327 printf("/%#lx", (unsigned long)ret);
1330 printf("%ld", (long)ret);
1332 printf("%#lx", (unsigned long)ret);
1334 } else if (error == ERESTART)
1336 else if (error == EJUSTRETURN)
1337 printf("JUSTRETURN");
1339 printf("-1 errno %d", ktr->ktr_error);
1341 printf(" %s", strerror(ktr->ktr_error));
1347 ktrnamei(char *cp, int len)
1349 printf("\"%.*s\"\n", len, cp);
1353 hexdump(char *p, int len, int screenwidth)
1361 i = 13; /* base offset */
1362 i += (width / 2) + 1; /* spaces every second byte */
1363 i += (width * 2); /* width of bytes */
1365 i += width; /* each byte */
1367 } while (i < screenwidth);
1370 for (n = 0; n < len; n += width) {
1371 for (i = n; i < n + width; i++) {
1372 if ((i % width) == 0) { /* beginning of line */
1373 printf(" 0x%04x", i);
1379 printf("%02x", p[i] & 0xff);
1384 for (i = n; i < n + width; i++) {
1387 if (p[i] >= ' ' && p[i] <= '~')
1394 if ((i % width) != 0)
1399 visdump(char *dp, int datalen, int screenwidth)
1408 for (;datalen > 0; datalen--, dp++) {
1409 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1412 * Keep track of printables and
1413 * space chars (like fold(1)).
1425 width = 8 - (col&07);
1430 if (col + width > (screenwidth-2)) {
1445 ktrgenio(struct ktr_genio *ktr, int len)
1447 int datalen = len - sizeof (struct ktr_genio);
1448 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1449 static int screenwidth = 0;
1452 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1453 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1454 datalen == 1 ? "" : "s");
1457 if (screenwidth == 0) {
1460 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1462 screenwidth = ws.ws_col;
1466 if (maxdata && datalen > maxdata)
1469 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1470 if (dp[i] >= 32 && dp[i] < 127)
1472 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1477 hexdump(dp, datalen, screenwidth);
1479 visdump(dp, datalen, screenwidth);
1482 const char *signames[] = {
1483 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
1484 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
1485 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
1486 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
1487 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
1488 "USR2", NULL, /* 31 - 32 */
1492 ktrpsig(struct ktr_psig *psig)
1494 if (psig->signo > 0 && psig->signo < NSIG)
1495 printf("SIG%s ", signames[psig->signo]);
1497 printf("SIG %d ", psig->signo);
1498 if (psig->action == SIG_DFL) {
1499 printf("SIG_DFL code=");
1500 sigcodename(psig->signo, psig->code);
1503 printf("caught handler=0x%lx mask=0x%x code=",
1504 (u_long)psig->action, psig->mask.__bits[0]);
1505 sigcodename(psig->signo, psig->code);
1511 ktrcsw_old(struct ktr_csw_old *cs)
1513 printf("%s %s\n", cs->out ? "stop" : "resume",
1514 cs->user ? "user" : "kernel");
1518 ktrcsw(struct ktr_csw *cs)
1520 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1521 cs->user ? "user" : "kernel", cs->wmesg);
1524 #define UTRACE_DLOPEN_START 1
1525 #define UTRACE_DLOPEN_STOP 2
1526 #define UTRACE_DLCLOSE_START 3
1527 #define UTRACE_DLCLOSE_STOP 4
1528 #define UTRACE_LOAD_OBJECT 5
1529 #define UTRACE_UNLOAD_OBJECT 6
1530 #define UTRACE_ADD_RUNDEP 7
1531 #define UTRACE_PRELOAD_FINISHED 8
1532 #define UTRACE_INIT_CALL 9
1533 #define UTRACE_FINI_CALL 10
1534 #define UTRACE_DLSYM_START 11
1535 #define UTRACE_DLSYM_STOP 12
1537 struct utrace_rtld {
1538 char sig[4]; /* 'RTLD' */
1544 char name[MAXPATHLEN];
1548 ktruser_rtld(int len, void *p)
1550 struct utrace_rtld *ut = p;
1555 switch (ut->event) {
1556 case UTRACE_DLOPEN_START:
1558 printf("dlopen(%s, ", ut->name);
1559 switch (mode & RTLD_MODEMASK) {
1564 printf("RTLD_LAZY");
1567 printf("%#x", mode & RTLD_MODEMASK);
1569 if (mode & RTLD_GLOBAL)
1570 printf(" | RTLD_GLOBAL");
1571 if (mode & RTLD_TRACE)
1572 printf(" | RTLD_TRACE");
1573 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
1574 printf(" | %#x", mode &
1575 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
1578 case UTRACE_DLOPEN_STOP:
1579 printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
1582 case UTRACE_DLCLOSE_START:
1583 printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
1586 case UTRACE_DLCLOSE_STOP:
1587 printf("dlclose(%p) finished\n", ut->handle);
1589 case UTRACE_LOAD_OBJECT:
1590 printf("RTLD: loaded %p @ %p - %p (%s)\n", ut->handle,
1591 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1594 case UTRACE_UNLOAD_OBJECT:
1595 printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
1596 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1599 case UTRACE_ADD_RUNDEP:
1600 parent = ut->mapbase;
1601 printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
1602 ut->handle, ut->name, ut->refcnt);
1604 case UTRACE_PRELOAD_FINISHED:
1605 printf("RTLD: LD_PRELOAD finished\n");
1607 case UTRACE_INIT_CALL:
1608 printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
1611 case UTRACE_FINI_CALL:
1612 printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
1615 case UTRACE_DLSYM_START:
1616 printf("RTLD: dlsym(%p, %s)\n", ut->handle, ut->name);
1618 case UTRACE_DLSYM_STOP:
1619 printf("RTLD: %p = dlsym(%p, %s)\n", ut->mapbase, ut->handle,
1626 printf("RTLD: %d ", len);
1629 printf(" %d", *cp++);
1631 printf(" %02x", *cp++);
1636 struct utrace_malloc {
1643 ktruser_malloc(void *p)
1645 struct utrace_malloc *ut = p;
1647 if (ut->p == (void *)(intptr_t)(-1))
1648 printf("malloc_init()\n");
1649 else if (ut->s == 0)
1650 printf("free(%p)\n", ut->p);
1651 else if (ut->p == NULL)
1652 printf("%p = malloc(%zu)\n", ut->r, ut->s);
1654 printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
1658 ktruser(int len, void *p)
1662 if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
1663 ktruser_rtld(len, p);
1667 if (len == sizeof(struct utrace_malloc)) {
1676 printf(" %d", *cp++);
1678 printf(" %02x", *cp++);
1683 ktrcaprights(cap_rights_t *rightsp)
1686 printf("cap_rights_t ");
1692 ktrsockaddr(struct sockaddr *sa)
1695 TODO: Support additional address families
1696 #include <netnatm/natm.h>
1697 struct sockaddr_natm *natm;
1698 #include <netsmb/netbios.h>
1699 struct sockaddr_nb *nb;
1704 * note: ktrstruct() has already verified that sa points to a
1705 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1706 * sa->sa_len bytes long.
1708 printf("struct sockaddr { ");
1709 sockfamilyname(sa->sa_family);
1712 #define check_sockaddr_len(n) \
1713 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1714 printf("invalid"); \
1718 switch(sa->sa_family) {
1720 struct sockaddr_in sa_in;
1722 memset(&sa_in, 0, sizeof(sa_in));
1723 memcpy(&sa_in, sa, sa->sa_len);
1724 check_sockaddr_len(in);
1725 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1726 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1730 struct sockaddr_in6 sa_in6;
1732 memset(&sa_in6, 0, sizeof(sa_in6));
1733 memcpy(&sa_in6, sa, sa->sa_len);
1734 check_sockaddr_len(in6);
1735 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1736 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1737 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1741 struct sockaddr_un sa_un;
1743 memset(&sa_un, 0, sizeof(sa_un));
1744 memcpy(&sa_un, sa, sa->sa_len);
1745 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1749 printf("unknown address family");
1755 ktrstat(struct stat *statp)
1757 char mode[12], timestr[PATH_MAX + 4];
1763 * note: ktrstruct() has already verified that statp points to a
1764 * buffer exactly sizeof(struct stat) bytes long.
1766 printf("struct stat {");
1767 printf("dev=%ju, ino=%ju, ",
1768 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1770 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1772 strmode(statp->st_mode, mode);
1773 printf("mode=%s, ", mode);
1775 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1779 #ifdef HAVE_LIBCAPSICUM
1781 pwd = cap_getpwuid(cappwd, statp->st_uid);
1784 pwd = getpwuid(statp->st_uid);
1787 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1789 printf("uid=\"%s\", ", pwd->pw_name);
1793 #ifdef HAVE_LIBCAPSICUM
1795 grp = cap_getgrgid(capgrp, statp->st_gid);
1798 grp = getgrgid(statp->st_gid);
1801 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1803 printf("gid=\"%s\", ", grp->gr_name);
1804 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1807 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1809 tm = localtime(&statp->st_atim.tv_sec);
1810 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1811 printf("\"%s\"", timestr);
1813 if (statp->st_atim.tv_nsec != 0)
1814 printf(".%09ld, ", statp->st_atim.tv_nsec);
1819 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1821 tm = localtime(&statp->st_mtim.tv_sec);
1822 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1823 printf("\"%s\"", timestr);
1825 if (statp->st_mtim.tv_nsec != 0)
1826 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1831 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1833 tm = localtime(&statp->st_ctim.tv_sec);
1834 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1835 printf("\"%s\"", timestr);
1837 if (statp->st_ctim.tv_nsec != 0)
1838 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1841 printf("birthtime=");
1843 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1845 tm = localtime(&statp->st_birthtim.tv_sec);
1846 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1847 printf("\"%s\"", timestr);
1849 if (statp->st_birthtim.tv_nsec != 0)
1850 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1853 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1854 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1855 (intmax_t)statp->st_blocks, statp->st_flags);
1860 ktrstruct(char *buf, size_t buflen)
1863 size_t namelen, datalen;
1865 cap_rights_t rights;
1867 struct sockaddr_storage ss;
1869 for (name = buf, namelen = 0;
1870 namelen < buflen && name[namelen] != '\0';
1873 if (namelen == buflen)
1875 if (name[namelen] != '\0')
1877 data = buf + namelen + 1;
1878 datalen = buflen - namelen - 1;
1882 for (i = 0; i < (int)namelen; ++i)
1883 if (!isalpha(name[i]))
1885 if (strcmp(name, "caprights") == 0) {
1886 if (datalen != sizeof(cap_rights_t))
1888 memcpy(&rights, data, datalen);
1889 ktrcaprights(&rights);
1890 } else if (strcmp(name, "stat") == 0) {
1891 if (datalen != sizeof(struct stat))
1893 memcpy(&sb, data, datalen);
1895 } else if (strcmp(name, "sockaddr") == 0) {
1896 if (datalen > sizeof(ss))
1898 memcpy(&ss, data, datalen);
1899 if (datalen != ss.ss_len)
1901 ktrsockaddr((struct sockaddr *)&ss);
1903 printf("unknown structure\n");
1907 printf("invalid record\n");
1911 ktrcapfail(struct ktr_cap_fail *ktr)
1913 switch (ktr->cap_type) {
1914 case CAPFAIL_NOTCAPABLE:
1915 /* operation on fd with insufficient capabilities */
1916 printf("operation requires ");
1917 capname(&ktr->cap_needed);
1918 printf(", descriptor holds ");
1919 capname(&ktr->cap_held);
1921 case CAPFAIL_INCREASE:
1922 /* requested more capabilities than fd already has */
1923 printf("attempt to increase capabilities from ");
1924 capname(&ktr->cap_held);
1926 capname(&ktr->cap_needed);
1928 case CAPFAIL_SYSCALL:
1929 /* called restricted syscall */
1930 printf("disallowed system call");
1932 case CAPFAIL_LOOKUP:
1933 /* used ".." in strict-relative mode */
1934 printf("restricted VFS lookup");
1937 printf("unknown capability failure: ");
1938 capname(&ktr->cap_needed);
1940 capname(&ktr->cap_held);
1947 ktrfault(struct ktr_fault *ktr)
1950 printf("0x%jx ", (uintmax_t)ktr->vaddr);
1951 vmprotname(ktr->type);
1956 ktrfaultend(struct ktr_faultend *ktr)
1959 vmresultname(ktr->result);
1963 #if defined(__amd64__) || defined(__i386__)
1965 linux_ktrsyscall(struct ktr_syscall *ktr)
1967 int narg = ktr->ktr_narg;
1970 if (ktr->ktr_code >= nlinux_syscalls || ktr->ktr_code < 0)
1971 printf("[%d]", ktr->ktr_code);
1973 printf("%s", linux_syscallnames[ktr->ktr_code]);
1975 printf("[%d]", ktr->ktr_code);
1977 ip = &ktr->ktr_args[0];
1981 print_number(ip, narg, c);
1988 linux_ktrsysret(struct ktr_sysret *ktr)
1990 register_t ret = ktr->ktr_retval;
1991 int error = ktr->ktr_error;
1992 int code = ktr->ktr_code;
1994 if (code >= nlinux_syscalls || code < 0)
1995 printf("[%d] ", code);
1997 printf("%s", linux_syscallnames[code]);
1999 printf("[%d]", code);
2005 printf("%ld", (long)ret);
2006 if (ret < 0 || ret > 9)
2007 printf("/%#lx", (unsigned long)ret);
2010 printf("%ld", (long)ret);
2012 printf("%#lx", (unsigned long)ret);
2014 } else if (error == ERESTART)
2016 else if (error == EJUSTRETURN)
2017 printf("JUSTRETURN");
2019 if (ktr->ktr_error <= ELAST + 1)
2020 error = abs(bsd_to_linux_errno[ktr->ktr_error]);
2023 printf("-1 errno %d", error);
2025 printf(" %s", strerror(ktr->ktr_error));
2034 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
2035 "[-m maxdata] [-p pid] [-t trstr]\n");