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;
591 switch (kth->ktr_type) {
633 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
638 * The ktr_tid field was previously the ktr_buffer field, which held
639 * the kernel pointer value for the buffer associated with data
640 * following the record header. It now holds a threadid, but only
641 * for trace files after the change. Older trace files still contain
642 * kernel pointers. Detect this and suppress the results by printing
643 * negative tid's as 0.
646 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
647 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
648 MAXCOMLEN, kth->ktr_comm);
650 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
653 if (timestamp & TIMESTAMP_ABSOLUTE) {
654 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
655 kth->ktr_time.tv_usec);
657 if (timestamp & TIMESTAMP_ELAPSED) {
658 if (prevtime_e.tv_sec == 0)
659 prevtime_e = kth->ktr_time;
660 timevalsub(&kth->ktr_time, &prevtime_e);
661 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
662 kth->ktr_time.tv_usec);
663 timevaladd(&kth->ktr_time, &prevtime_e);
665 if (timestamp & TIMESTAMP_RELATIVE) {
666 if (prevtime.tv_sec == 0)
667 prevtime = kth->ktr_time;
668 temp = kth->ktr_time;
669 timevalsub(&kth->ktr_time, &prevtime);
670 if ((intmax_t)kth->ktr_time.tv_sec < 0) {
671 kth->ktr_time = prevtime;
673 timevalsub(&kth->ktr_time, &prevtime);
679 printf("%s%jd.%06ld ", sign, (intmax_t)kth->ktr_time.tv_sec,
680 kth->ktr_time.tv_usec);
686 #include <sys/syscall.h>
688 #include <sys/kern/syscalls.c>
690 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
693 ktrsyscall(struct ktr_syscall *ktr, u_int flags)
695 int narg = ktr->ktr_narg;
699 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
700 (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0))
701 printf("[%d]", ktr->ktr_code);
703 printf("%s", syscallnames[ktr->ktr_code]);
705 printf("[%d]", ktr->ktr_code);
707 ip = &ktr->ktr_args[0];
711 (flags == 0 || (flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
712 switch (ktr->ktr_code) {
730 atfdname(*ip, decimal);
736 switch (ktr->ktr_code) {
738 print_number(ip, narg, c);
740 ioctlname(*ip, decimal);
756 print_number(ip, narg, c);
764 print_number(ip, narg, c);
766 flagsandmodename(ip[0], ip[1], decimal);
771 print_number(ip, narg, c);
772 print_number(ip, narg, c);
774 * A flags value of zero is valid for
775 * wait4() but not for wait6(), so
776 * handle zero special here.
779 print_number(ip, narg, c);
789 idtypename(*ip, decimal);
793 print_number(ip, narg, c);
794 print_number(ip, narg, c);
803 print_number(ip, narg, c);
811 print_number(ip, narg, c);
818 print_number(ip, narg, c);
819 print_number(ip, narg, c);
821 getfsstatflagsname(*ip);
826 print_number(ip, narg, c);
827 print_number(ip, narg, c);
834 print_number(ip, narg, c);
842 print_number(ip, narg, c);
843 print_number(ip, narg, c);
845 sendrecvflagsname(*ip);
851 print_number(ip, narg, c);
852 print_number(ip, narg, c);
853 print_number(ip, narg, c);
855 sendrecvflagsname(*ip);
862 print_number(ip, narg, c);
869 print_number(ip, narg, c);
888 print_number(ip, narg, c);
889 print_number(ip, narg, c);
895 #ifdef SYS_freebsd6_mmap
896 case SYS_freebsd6_mmap:
897 print_number(ip, narg, c);
898 print_number(ip, narg, c);
910 print_number(ip, narg, c);
911 print_number(ip, narg, c);
922 print_number(ip, narg, c);
923 print_number(ip, narg, c);
930 print_number(ip, narg, c);
931 print_number(ip, narg, c);
933 madvisebehavname(*ip);
937 case SYS_setpriority:
938 print_number(ip, narg, c);
939 print_number(ip, narg, c);
946 print_number(ip, narg, c);
948 fcntlcmdname(ip[0], ip[1], decimal);
956 sockdomainname(sockdomain);
960 socktypenamewithflags(*ip);
963 if (sockdomain == PF_INET ||
964 sockdomain == PF_INET6) {
966 sockipprotoname(*ip);
975 print_number(ip, narg, c);
977 sockoptlevelname(*ip, decimal);
978 if (*ip == SOL_SOCKET) {
987 #ifdef SYS_freebsd6_lseek
988 case SYS_freebsd6_lseek:
989 print_number(ip, narg, c);
990 /* Hidden 'pad' argument, not in lseek(2) */
991 print_number(ip, narg, c);
992 print_number(ip, narg, c);
1000 print_number(ip, narg, c);
1001 /* Hidden 'pad' argument, not in lseek(2) */
1002 print_number(ip, narg, c);
1009 print_number(ip, narg, c);
1019 print_number(ip, narg, c);
1026 print_number(ip, narg, c);
1028 shutdownhowname(*ip);
1032 case SYS_socketpair:
1034 sockdomainname(*ip);
1038 socktypenamewithflags(*ip);
1052 print_number(ip, narg, c);
1074 print_number(ip, narg, c);
1075 print_number(ip, narg, c);
1082 print_number(ip, narg, c);
1083 print_number(ip, narg, c);
1090 print_number(ip, narg, c);
1097 print_number(ip, narg, c);
1098 print_number(ip, narg, c);
1105 print_number(ip, narg, c);
1112 print_number(ip, narg, c);
1115 printf(",0%o", (unsigned int)ip[1]);
1120 print_number(ip, narg, c);
1121 print_number(ip, narg, c);
1134 case SYS_lio_listio:
1136 lio_listioname(*ip);
1147 case SYS_sched_setscheduler:
1148 print_number(ip, narg, c);
1150 schedpolicyname(*ip);
1154 case SYS_sched_get_priority_max:
1155 case SYS_sched_get_priority_min:
1157 schedpolicyname(*ip);
1162 print_number(ip, narg, c);
1163 print_number(ip, narg, c);
1164 print_number(ip, narg, c);
1165 print_number(ip, narg, c);
1166 print_number(ip, narg, c);
1167 print_number(ip, narg, c);
1169 sendfileflagsname(*(int *)ip);
1174 print_number(ip, narg, c);
1180 case SYS_sigprocmask:
1182 sigprocmaskhowname(*ip);
1187 case SYS___acl_get_file:
1188 case SYS___acl_set_file:
1189 case SYS___acl_get_fd:
1190 case SYS___acl_set_fd:
1191 case SYS___acl_delete_file:
1192 case SYS___acl_delete_fd:
1193 case SYS___acl_aclcheck_file:
1194 case SYS___acl_aclcheck_fd:
1195 case SYS___acl_get_link:
1196 case SYS___acl_set_link:
1197 case SYS___acl_delete_link:
1198 case SYS___acl_aclcheck_link:
1199 print_number(ip, narg, c);
1212 case SYS_extattrctl:
1213 print_number(ip, narg, c);
1215 extattrctlname(*ip);
1220 print_number(ip, narg, c);
1221 print_number(ip, narg, c);
1223 mountflagsname(*ip);
1227 case SYS_thr_create:
1228 print_number(ip, narg, c);
1229 print_number(ip, narg, c);
1231 thrcreateflagsname(*ip);
1236 print_number(ip, narg, c);
1242 case SYS_kldunloadf:
1243 print_number(ip, narg, c);
1245 kldunloadfflagsname(*ip);
1252 print_number(ip, narg, c);
1254 atfdname(*ip, decimal);
1258 case SYS_cap_fcntls_limit:
1259 print_number(ip, narg, c);
1266 case SYS_posix_fadvise:
1267 print_number(ip, narg, c);
1268 print_number(ip, narg, c);
1269 print_number(ip, narg, c);
1271 fadvisebehavname((int)*ip);
1277 idtypename(*ip, decimal);
1281 print_number(ip, narg, c);
1283 procctlcmdname(*ip);
1288 print_number(ip, narg, c);
1292 case UMTX_OP_CV_WAIT:
1296 umtxcvwaitflags(*ip);
1298 case UMTX_OP_RW_RDLOCK:
1302 umtxrwlockflags(*ip);
1310 print_number(ip, narg, c);
1318 ktrsysret(struct ktr_sysret *ktr, u_int flags)
1320 register_t ret = ktr->ktr_retval;
1321 int error = ktr->ktr_error;
1322 int code = ktr->ktr_code;
1324 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
1325 (code >= nsyscalls || code < 0))
1326 printf("[%d] ", code);
1328 printf("%s", syscallnames[code]);
1330 printf("[%d]", code);
1336 printf("%ld", (long)ret);
1337 if (ret < 0 || ret > 9)
1338 printf("/%#lx", (unsigned long)ret);
1341 printf("%ld", (long)ret);
1343 printf("%#lx", (unsigned long)ret);
1345 } else if (error == ERESTART)
1347 else if (error == EJUSTRETURN)
1348 printf("JUSTRETURN");
1350 printf("-1 errno %d", ktr->ktr_error);
1352 printf(" %s", strerror(ktr->ktr_error));
1358 ktrnamei(char *cp, int len)
1360 printf("\"%.*s\"\n", len, cp);
1364 hexdump(char *p, int len, int screenwidth)
1372 i = 13; /* base offset */
1373 i += (width / 2) + 1; /* spaces every second byte */
1374 i += (width * 2); /* width of bytes */
1376 i += width; /* each byte */
1378 } while (i < screenwidth);
1381 for (n = 0; n < len; n += width) {
1382 for (i = n; i < n + width; i++) {
1383 if ((i % width) == 0) { /* beginning of line */
1384 printf(" 0x%04x", i);
1390 printf("%02x", p[i] & 0xff);
1395 for (i = n; i < n + width; i++) {
1398 if (p[i] >= ' ' && p[i] <= '~')
1405 if ((i % width) != 0)
1410 visdump(char *dp, int datalen, int screenwidth)
1419 for (;datalen > 0; datalen--, dp++) {
1420 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1423 * Keep track of printables and
1424 * space chars (like fold(1)).
1436 width = 8 - (col&07);
1441 if (col + width > (screenwidth-2)) {
1456 ktrgenio(struct ktr_genio *ktr, int len)
1458 int datalen = len - sizeof (struct ktr_genio);
1459 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1460 static int screenwidth = 0;
1463 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1464 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1465 datalen == 1 ? "" : "s");
1468 if (screenwidth == 0) {
1471 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1473 screenwidth = ws.ws_col;
1477 if (maxdata && datalen > maxdata)
1480 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1481 if (dp[i] >= 32 && dp[i] < 127)
1483 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1488 hexdump(dp, datalen, screenwidth);
1490 visdump(dp, datalen, screenwidth);
1493 const char *signames[] = {
1494 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
1495 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
1496 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
1497 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
1498 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
1499 "USR2", NULL, /* 31 - 32 */
1503 ktrpsig(struct ktr_psig *psig)
1505 if (psig->signo > 0 && psig->signo < NSIG)
1506 printf("SIG%s ", signames[psig->signo]);
1508 printf("SIG %d ", psig->signo);
1509 if (psig->action == SIG_DFL) {
1510 printf("SIG_DFL code=");
1511 sigcodename(psig->signo, psig->code);
1514 printf("caught handler=0x%lx mask=0x%x code=",
1515 (u_long)psig->action, psig->mask.__bits[0]);
1516 sigcodename(psig->signo, psig->code);
1522 ktrcsw_old(struct ktr_csw_old *cs)
1524 printf("%s %s\n", cs->out ? "stop" : "resume",
1525 cs->user ? "user" : "kernel");
1529 ktrcsw(struct ktr_csw *cs)
1531 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1532 cs->user ? "user" : "kernel", cs->wmesg);
1535 #define UTRACE_DLOPEN_START 1
1536 #define UTRACE_DLOPEN_STOP 2
1537 #define UTRACE_DLCLOSE_START 3
1538 #define UTRACE_DLCLOSE_STOP 4
1539 #define UTRACE_LOAD_OBJECT 5
1540 #define UTRACE_UNLOAD_OBJECT 6
1541 #define UTRACE_ADD_RUNDEP 7
1542 #define UTRACE_PRELOAD_FINISHED 8
1543 #define UTRACE_INIT_CALL 9
1544 #define UTRACE_FINI_CALL 10
1545 #define UTRACE_DLSYM_START 11
1546 #define UTRACE_DLSYM_STOP 12
1548 struct utrace_rtld {
1549 char sig[4]; /* 'RTLD' */
1555 char name[MAXPATHLEN];
1559 ktruser_rtld(int len, void *p)
1561 struct utrace_rtld *ut = p;
1566 switch (ut->event) {
1567 case UTRACE_DLOPEN_START:
1569 printf("dlopen(%s, ", ut->name);
1570 switch (mode & RTLD_MODEMASK) {
1575 printf("RTLD_LAZY");
1578 printf("%#x", mode & RTLD_MODEMASK);
1580 if (mode & RTLD_GLOBAL)
1581 printf(" | RTLD_GLOBAL");
1582 if (mode & RTLD_TRACE)
1583 printf(" | RTLD_TRACE");
1584 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
1585 printf(" | %#x", mode &
1586 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
1589 case UTRACE_DLOPEN_STOP:
1590 printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
1593 case UTRACE_DLCLOSE_START:
1594 printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
1597 case UTRACE_DLCLOSE_STOP:
1598 printf("dlclose(%p) finished\n", ut->handle);
1600 case UTRACE_LOAD_OBJECT:
1601 printf("RTLD: loaded %p @ %p - %p (%s)\n", ut->handle,
1602 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1605 case UTRACE_UNLOAD_OBJECT:
1606 printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
1607 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1610 case UTRACE_ADD_RUNDEP:
1611 parent = ut->mapbase;
1612 printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
1613 ut->handle, ut->name, ut->refcnt);
1615 case UTRACE_PRELOAD_FINISHED:
1616 printf("RTLD: LD_PRELOAD finished\n");
1618 case UTRACE_INIT_CALL:
1619 printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
1622 case UTRACE_FINI_CALL:
1623 printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
1626 case UTRACE_DLSYM_START:
1627 printf("RTLD: dlsym(%p, %s)\n", ut->handle, ut->name);
1629 case UTRACE_DLSYM_STOP:
1630 printf("RTLD: %p = dlsym(%p, %s)\n", ut->mapbase, ut->handle,
1637 printf("RTLD: %d ", len);
1640 printf(" %d", *cp++);
1642 printf(" %02x", *cp++);
1647 struct utrace_malloc {
1654 ktruser_malloc(void *p)
1656 struct utrace_malloc *ut = p;
1658 if (ut->p == (void *)(intptr_t)(-1))
1659 printf("malloc_init()\n");
1660 else if (ut->s == 0)
1661 printf("free(%p)\n", ut->p);
1662 else if (ut->p == NULL)
1663 printf("%p = malloc(%zu)\n", ut->r, ut->s);
1665 printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
1669 ktruser(int len, void *p)
1673 if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
1674 ktruser_rtld(len, p);
1678 if (len == sizeof(struct utrace_malloc)) {
1687 printf(" %d", *cp++);
1689 printf(" %02x", *cp++);
1694 ktrcaprights(cap_rights_t *rightsp)
1697 printf("cap_rights_t ");
1703 ktrsockaddr(struct sockaddr *sa)
1706 TODO: Support additional address families
1707 #include <netnatm/natm.h>
1708 struct sockaddr_natm *natm;
1709 #include <netsmb/netbios.h>
1710 struct sockaddr_nb *nb;
1715 * note: ktrstruct() has already verified that sa points to a
1716 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1717 * sa->sa_len bytes long.
1719 printf("struct sockaddr { ");
1720 sockfamilyname(sa->sa_family);
1723 #define check_sockaddr_len(n) \
1724 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1725 printf("invalid"); \
1729 switch(sa->sa_family) {
1731 struct sockaddr_in sa_in;
1733 memset(&sa_in, 0, sizeof(sa_in));
1734 memcpy(&sa_in, sa, sa->sa_len);
1735 check_sockaddr_len(in);
1736 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1737 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1741 struct sockaddr_in6 sa_in6;
1743 memset(&sa_in6, 0, sizeof(sa_in6));
1744 memcpy(&sa_in6, sa, sa->sa_len);
1745 check_sockaddr_len(in6);
1746 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1747 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1748 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1752 struct sockaddr_un sa_un;
1754 memset(&sa_un, 0, sizeof(sa_un));
1755 memcpy(&sa_un, sa, sa->sa_len);
1756 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1760 printf("unknown address family");
1766 ktrstat(struct stat *statp)
1768 char mode[12], timestr[PATH_MAX + 4];
1774 * note: ktrstruct() has already verified that statp points to a
1775 * buffer exactly sizeof(struct stat) bytes long.
1777 printf("struct stat {");
1778 printf("dev=%ju, ino=%ju, ",
1779 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1781 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1783 strmode(statp->st_mode, mode);
1784 printf("mode=%s, ", mode);
1786 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1790 #ifdef HAVE_LIBCAPSICUM
1792 pwd = cap_getpwuid(cappwd, statp->st_uid);
1795 pwd = getpwuid(statp->st_uid);
1798 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1800 printf("uid=\"%s\", ", pwd->pw_name);
1804 #ifdef HAVE_LIBCAPSICUM
1806 grp = cap_getgrgid(capgrp, statp->st_gid);
1809 grp = getgrgid(statp->st_gid);
1812 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1814 printf("gid=\"%s\", ", grp->gr_name);
1815 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1818 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1820 tm = localtime(&statp->st_atim.tv_sec);
1821 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1822 printf("\"%s\"", timestr);
1824 if (statp->st_atim.tv_nsec != 0)
1825 printf(".%09ld, ", statp->st_atim.tv_nsec);
1830 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1832 tm = localtime(&statp->st_mtim.tv_sec);
1833 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1834 printf("\"%s\"", timestr);
1836 if (statp->st_mtim.tv_nsec != 0)
1837 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1842 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1844 tm = localtime(&statp->st_ctim.tv_sec);
1845 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1846 printf("\"%s\"", timestr);
1848 if (statp->st_ctim.tv_nsec != 0)
1849 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1852 printf("birthtime=");
1854 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1856 tm = localtime(&statp->st_birthtim.tv_sec);
1857 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1858 printf("\"%s\"", timestr);
1860 if (statp->st_birthtim.tv_nsec != 0)
1861 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1864 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1865 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1866 (intmax_t)statp->st_blocks, statp->st_flags);
1871 ktrstruct(char *buf, size_t buflen)
1874 size_t namelen, datalen;
1876 cap_rights_t rights;
1878 struct sockaddr_storage ss;
1880 for (name = buf, namelen = 0;
1881 namelen < buflen && name[namelen] != '\0';
1884 if (namelen == buflen)
1886 if (name[namelen] != '\0')
1888 data = buf + namelen + 1;
1889 datalen = buflen - namelen - 1;
1893 for (i = 0; i < (int)namelen; ++i)
1894 if (!isalpha(name[i]))
1896 if (strcmp(name, "caprights") == 0) {
1897 if (datalen != sizeof(cap_rights_t))
1899 memcpy(&rights, data, datalen);
1900 ktrcaprights(&rights);
1901 } else if (strcmp(name, "stat") == 0) {
1902 if (datalen != sizeof(struct stat))
1904 memcpy(&sb, data, datalen);
1906 } else if (strcmp(name, "sockaddr") == 0) {
1907 if (datalen > sizeof(ss))
1909 memcpy(&ss, data, datalen);
1910 if (datalen != ss.ss_len)
1912 ktrsockaddr((struct sockaddr *)&ss);
1914 printf("unknown structure\n");
1918 printf("invalid record\n");
1922 ktrcapfail(struct ktr_cap_fail *ktr)
1924 switch (ktr->cap_type) {
1925 case CAPFAIL_NOTCAPABLE:
1926 /* operation on fd with insufficient capabilities */
1927 printf("operation requires ");
1928 capname(&ktr->cap_needed);
1929 printf(", descriptor holds ");
1930 capname(&ktr->cap_held);
1932 case CAPFAIL_INCREASE:
1933 /* requested more capabilities than fd already has */
1934 printf("attempt to increase capabilities from ");
1935 capname(&ktr->cap_held);
1937 capname(&ktr->cap_needed);
1939 case CAPFAIL_SYSCALL:
1940 /* called restricted syscall */
1941 printf("disallowed system call");
1943 case CAPFAIL_LOOKUP:
1944 /* used ".." in strict-relative mode */
1945 printf("restricted VFS lookup");
1948 printf("unknown capability failure: ");
1949 capname(&ktr->cap_needed);
1951 capname(&ktr->cap_held);
1958 ktrfault(struct ktr_fault *ktr)
1961 printf("0x%jx ", (uintmax_t)ktr->vaddr);
1962 vmprotname(ktr->type);
1967 ktrfaultend(struct ktr_faultend *ktr)
1970 vmresultname(ktr->result);
1974 #if defined(__amd64__) || defined(__i386__)
1976 linux_ktrsyscall(struct ktr_syscall *ktr)
1978 int narg = ktr->ktr_narg;
1981 if (ktr->ktr_code >= nlinux_syscalls || ktr->ktr_code < 0)
1982 printf("[%d]", ktr->ktr_code);
1984 printf("%s", linux_syscallnames[ktr->ktr_code]);
1986 printf("[%d]", ktr->ktr_code);
1988 ip = &ktr->ktr_args[0];
1992 print_number(ip, narg, c);
1999 linux_ktrsysret(struct ktr_sysret *ktr)
2001 register_t ret = ktr->ktr_retval;
2002 int error = ktr->ktr_error;
2003 int code = ktr->ktr_code;
2005 if (code >= nlinux_syscalls || code < 0)
2006 printf("[%d] ", code);
2008 printf("%s", linux_syscallnames[code]);
2010 printf("[%d]", code);
2016 printf("%ld", (long)ret);
2017 if (ret < 0 || ret > 9)
2018 printf("/%#lx", (unsigned long)ret);
2021 printf("%ld", (long)ret);
2023 printf("%#lx", (unsigned long)ret);
2025 } else if (error == ERESTART)
2027 else if (error == EJUSTRETURN)
2028 printf("JUSTRETURN");
2030 if (ktr->ktr_error <= ELAST + 1)
2031 error = abs(bsd_to_linux_errno[ktr->ktr_error]);
2034 printf("-1 errno %d", error);
2036 printf(" %s", strerror(ktr->ktr_error));
2045 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
2046 "[-m maxdata] [-p pid] [-t trstr]\n");