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>
61 #include <sys/queue.h>
63 #include <arpa/inet.h>
64 #include <netinet/in.h>
70 #ifdef HAVE_LIBCAPSICUM
71 #include <libcapsicum.h>
72 #include <libcapsicum_grp.h>
73 #include <libcapsicum_pwd.h>
74 #include <libcapsicum_service.h>
79 #ifdef HAVE_LIBCAPSICUM
91 #include "kdump_subr.h"
93 u_int abidump(struct ktr_header *);
94 int fetchprocinfo(struct ktr_header *, u_int *);
95 int fread_tail(void *, int, int);
96 void dumpheader(struct ktr_header *);
97 void ktrsyscall(struct ktr_syscall *, u_int);
98 void ktrsysret(struct ktr_sysret *, u_int);
99 void ktrnamei(char *, int);
100 void hexdump(char *, int, int);
101 void visdump(char *, int, int);
102 void ktrgenio(struct ktr_genio *, int);
103 void ktrpsig(struct ktr_psig *);
104 void ktrcsw(struct ktr_csw *);
105 void ktrcsw_old(struct ktr_csw_old *);
106 void ktruser_malloc(unsigned char *);
107 void ktruser_rtld(int, unsigned char *);
108 void ktruser(int, unsigned char *);
109 void ktrcaprights(cap_rights_t *);
110 void ktrsockaddr(struct sockaddr *);
111 void ktrstat(struct stat *);
112 void ktrstruct(char *, size_t);
113 void ktrcapfail(struct ktr_cap_fail *);
114 void ktrfault(struct ktr_fault *);
115 void ktrfaultend(struct ktr_faultend *);
116 void limitfd(int fd);
118 void ioctlname(unsigned long, int);
120 #define TIMESTAMP_NONE 0x0
121 #define TIMESTAMP_ABSOLUTE 0x1
122 #define TIMESTAMP_ELAPSED 0x2
123 #define TIMESTAMP_RELATIVE 0x4
125 int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
126 resolv = 0, abiflag = 0, syscallno = 0;
127 const char *tracefile = DEF_TRACEFILE;
128 struct ktr_header ktr_header;
130 #define TIME_FORMAT "%b %e %T %Y"
131 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
133 #define print_number(i,n,c) do { \
135 printf("%c%jd", c, (intmax_t)*i); \
137 printf("%c%#jx", c, (uintmax_t)(u_register_t)*i); \
143 #if defined(__amd64__) || defined(__i386__)
145 void linux_ktrsyscall(struct ktr_syscall *);
146 void linux_ktrsysret(struct ktr_sysret *);
147 extern char *linux_syscallnames[];
148 extern int nlinux_syscalls;
152 * Linux syscalls return negative errno's, we do positive and map them
154 static int bsd_to_linux_errno[ELAST + 1] = {
155 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9,
156 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
157 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
158 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
159 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
160 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
161 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
162 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9,
163 -6, -6, -43, -42, -75,-125, -84, -95, -16, -74,
170 TAILQ_ENTRY(proc_info) info;
175 TAILQ_HEAD(trace_procs, proc_info) trace_procs;
177 #ifdef HAVE_LIBCAPSICUM
178 static cap_channel_t *cappwd, *capgrp;
186 * Cache NLS data before entering capability mode.
187 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
189 (void)catopen("libc", NL_CAT_LOCALE);
198 * Allow localtime(3) to cache /etc/localtime content before entering
200 * XXXPJD: There should be localtime_init() in libc.
203 (void)localtime(<ime);
206 #ifdef HAVE_LIBCAPSICUM
208 cappwdgrp_setup(cap_channel_t **cappwdp, cap_channel_t **capgrpp)
210 cap_channel_t *capcas, *cappwdloc, *capgrploc;
211 const char *cmds[1], *fields[1];
214 if (capcas == NULL) {
215 warn("unable to contact casperd");
218 cappwdloc = cap_service_open(capcas, "system.pwd");
219 capgrploc = cap_service_open(capcas, "system.grp");
220 /* Casper capability no longer needed. */
222 if (cappwdloc == NULL || capgrploc == NULL) {
223 if (cappwdloc == NULL)
224 warn("unable to open system.pwd service");
225 if (capgrploc == NULL)
226 warn("unable to open system.grp service");
229 /* Limit system.pwd to only getpwuid() function and pw_name field. */
230 cmds[0] = "getpwuid";
231 if (cap_pwd_limit_cmds(cappwdloc, cmds, 1) < 0)
232 err(1, "unable to limit system.pwd service");
233 fields[0] = "pw_name";
234 if (cap_pwd_limit_fields(cappwdloc, fields, 1) < 0)
235 err(1, "unable to limit system.pwd service");
236 /* Limit system.grp to only getgrgid() function and gr_name field. */
237 cmds[0] = "getgrgid";
238 if (cap_grp_limit_cmds(capgrploc, cmds, 1) < 0)
239 err(1, "unable to limit system.grp service");
240 fields[0] = "gr_name";
241 if (cap_grp_limit_fields(capgrploc, fields, 1) < 0)
242 err(1, "unable to limit system.grp service");
244 *cappwdp = cappwdloc;
245 *capgrpp = capgrploc;
248 #endif /* HAVE_LIBCAPSICUM */
251 main(int argc, char *argv[])
253 int ch, ktrlen, size;
255 int trpoints = ALL_POINTS;
260 setlocale(LC_CTYPE, "");
262 timestamp = TIMESTAMP_NONE;
264 while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1)
279 maxdata = atoi(optarg);
297 timestamp |= TIMESTAMP_ELAPSED;
303 timestamp |= TIMESTAMP_RELATIVE;
306 timestamp |= TIMESTAMP_ABSOLUTE;
309 trpoints = getpoints(optarg);
311 errx(1, "unknown trace point in %s", optarg);
320 m = malloc(size = 1025);
322 errx(1, "%s", strerror(ENOMEM));
323 if (!freopen(tracefile, "r", stdin))
324 err(1, "%s", tracefile);
328 #ifdef HAVE_LIBCAPSICUM
330 if (cappwdgrp_setup(&cappwd, &capgrp) < 0) {
335 if (resolv == 0 || (cappwd != NULL && capgrp != NULL)) {
336 if (cap_enter() < 0 && errno != ENOSYS)
337 err(1, "unable to enter capability mode");
341 if (cap_enter() < 0 && errno != ENOSYS)
342 err(1, "unable to enter capability mode");
345 limitfd(STDIN_FILENO);
346 limitfd(STDOUT_FILENO);
347 limitfd(STDERR_FILENO);
349 fprintf(stderr, "capability mode sandbox enabled\n");
351 TAILQ_INIT(&trace_procs);
353 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
354 if (ktr_header.ktr_type & KTR_DROP) {
355 ktr_header.ktr_type &= ~KTR_DROP;
356 if (!drop_logged && threads) {
358 "%6jd %6jd %-8.*s Events dropped.\n",
359 (intmax_t)ktr_header.ktr_pid,
360 ktr_header.ktr_tid > 0 ?
361 (intmax_t)ktr_header.ktr_tid : 0,
362 MAXCOMLEN, ktr_header.ktr_comm);
364 } else if (!drop_logged) {
365 printf("%6jd %-8.*s Events dropped.\n",
366 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
367 ktr_header.ktr_comm);
371 if (trpoints & (1<<ktr_header.ktr_type))
372 if (pid == 0 || ktr_header.ktr_pid == pid ||
373 ktr_header.ktr_tid == pid)
374 dumpheader(&ktr_header);
375 if ((ktrlen = ktr_header.ktr_len) < 0)
376 errx(1, "bogus length 0x%x", ktrlen);
378 m = realloc(m, ktrlen+1);
380 errx(1, "%s", strerror(ENOMEM));
383 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
384 errx(1, "data too short");
385 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
387 sv_flags = abidump(&ktr_header);
388 if (pid && ktr_header.ktr_pid != pid &&
389 ktr_header.ktr_tid != pid)
391 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
394 switch (ktr_header.ktr_type) {
396 #if defined(__amd64__) || defined(__i386__)
397 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
398 linux_ktrsyscall((struct ktr_syscall *)m);
401 ktrsyscall((struct ktr_syscall *)m, sv_flags);
404 #if defined(__amd64__) || defined(__i386__)
405 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
406 linux_ktrsysret((struct ktr_sysret *)m);
409 ktrsysret((struct ktr_sysret *)m, sv_flags);
416 ktrgenio((struct ktr_genio *)m, ktrlen);
419 ktrpsig((struct ktr_psig *)m);
422 if (ktrlen == sizeof(struct ktr_csw_old))
423 ktrcsw_old((struct ktr_csw_old *)m);
425 ktrcsw((struct ktr_csw *)m);
431 ktrstruct(m, ktrlen);
434 ktrcapfail((struct ktr_cap_fail *)m);
437 ktrfault((struct ktr_fault *)m);
440 ktrfaultend((struct ktr_faultend *)m);
458 cap_rights_init(&rights, CAP_FSTAT);
463 cap_rights_set(&rights, CAP_READ);
466 cap_rights_set(&rights, CAP_IOCTL, CAP_WRITE);
467 cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
470 cap_rights_set(&rights, CAP_WRITE);
472 cap_rights_set(&rights, CAP_IOCTL);
480 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
481 err(1, "unable to limit rights for descriptor %d", fd);
482 if (cmd != -1 && cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
483 err(1, "unable to limit ioctls for descriptor %d", fd);
487 fread_tail(void *buf, int size, int num)
491 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
499 fetchprocinfo(struct ktr_header *kth, u_int *flags)
501 struct proc_info *pi;
503 switch (kth->ktr_type) {
505 TAILQ_FOREACH(pi, &trace_procs, info) {
506 if (pi->pid == kth->ktr_pid) {
507 TAILQ_REMOVE(&trace_procs, pi, info);
511 pi = malloc(sizeof(struct proc_info));
513 errx(1, "%s", strerror(ENOMEM));
514 pi->sv_flags = *flags;
515 pi->pid = kth->ktr_pid;
516 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
520 TAILQ_FOREACH(pi, &trace_procs, info) {
521 if (pi->pid == kth->ktr_pid) {
522 TAILQ_REMOVE(&trace_procs, pi, info);
534 abidump(struct ktr_header *kth)
536 struct proc_info *pi;
541 TAILQ_FOREACH(pi, &trace_procs, info) {
542 if (pi->pid == kth->ktr_pid) {
543 flags = pi->sv_flags;
551 switch (flags & SV_ABI_MASK) {
571 printf("%s%s ", abi, arch);
577 dumpheader(struct ktr_header *kth)
579 static char unknown[64];
580 static struct timeval prevtime, prevtime_e, temp;
583 switch (kth->ktr_type) {
625 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
630 * The ktr_tid field was previously the ktr_buffer field, which held
631 * the kernel pointer value for the buffer associated with data
632 * following the record header. It now holds a threadid, but only
633 * for trace files after the change. Older trace files still contain
634 * kernel pointers. Detect this and suppress the results by printing
635 * negative tid's as 0.
638 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
639 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
640 MAXCOMLEN, kth->ktr_comm);
642 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
645 if (timestamp & TIMESTAMP_ABSOLUTE) {
646 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
647 kth->ktr_time.tv_usec);
649 if (timestamp & TIMESTAMP_ELAPSED) {
650 if (prevtime_e.tv_sec == 0)
651 prevtime_e = kth->ktr_time;
652 timevalsub(&kth->ktr_time, &prevtime_e);
653 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
654 kth->ktr_time.tv_usec);
655 timevaladd(&kth->ktr_time, &prevtime_e);
657 if (timestamp & TIMESTAMP_RELATIVE) {
658 temp = kth->ktr_time;
659 timevalsub(&kth->ktr_time, &prevtime);
661 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
662 kth->ktr_time.tv_usec);
668 #include <sys/syscall.h>
670 #include <sys/kern/syscalls.c>
672 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
675 ktrsyscall(struct ktr_syscall *ktr, u_int flags)
677 int narg = ktr->ktr_narg;
681 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
682 (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0))
683 printf("[%d]", ktr->ktr_code);
685 printf("%s", syscallnames[ktr->ktr_code]);
687 printf("[%d]", ktr->ktr_code);
689 ip = &ktr->ktr_args[0];
693 (flags == 0 || (flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
694 switch (ktr->ktr_code) {
711 atfdname(*ip, decimal);
717 switch (ktr->ktr_code) {
719 print_number(ip, narg, c);
721 ioctlname(*ip, decimal);
737 print_number(ip, narg, c);
745 print_number(ip, narg, c);
747 flagsandmodename(ip[0], ip[1], decimal);
752 print_number(ip, narg, c);
753 print_number(ip, narg, c);
755 * A flags value of zero is valid for
756 * wait4() but not for wait6(), so
757 * handle zero special here.
760 print_number(ip, narg, c);
770 idtypename(*ip, decimal);
774 print_number(ip, narg, c);
775 print_number(ip, narg, c);
784 print_number(ip, narg, c);
792 print_number(ip, narg, c);
799 print_number(ip, narg, c);
800 print_number(ip, narg, c);
802 getfsstatflagsname(*ip);
807 print_number(ip, narg, c);
808 print_number(ip, narg, c);
815 print_number(ip, narg, c);
823 print_number(ip, narg, c);
824 print_number(ip, narg, c);
826 sendrecvflagsname(*ip);
832 print_number(ip, narg, c);
833 print_number(ip, narg, c);
834 print_number(ip, narg, c);
836 sendrecvflagsname(*ip);
843 print_number(ip, narg, c);
850 print_number(ip, narg, c);
869 print_number(ip, narg, c);
870 print_number(ip, narg, c);
876 #ifdef SYS_freebsd6_mmap
877 case SYS_freebsd6_mmap:
878 print_number(ip, narg, c);
879 print_number(ip, narg, c);
891 print_number(ip, narg, c);
892 print_number(ip, narg, c);
903 print_number(ip, narg, c);
904 print_number(ip, narg, c);
911 print_number(ip, narg, c);
912 print_number(ip, narg, c);
914 madvisebehavname(*ip);
918 case SYS_setpriority:
919 print_number(ip, narg, c);
920 print_number(ip, narg, c);
927 print_number(ip, narg, c);
929 fcntlcmdname(ip[0], ip[1], decimal);
937 sockdomainname(sockdomain);
941 socktypenamewithflags(*ip);
944 if (sockdomain == PF_INET ||
945 sockdomain == PF_INET6) {
947 sockipprotoname(*ip);
956 print_number(ip, narg, c);
958 sockoptlevelname(*ip, decimal);
959 if (*ip == SOL_SOCKET) {
968 #ifdef SYS_freebsd6_lseek
969 case SYS_freebsd6_lseek:
970 print_number(ip, narg, c);
971 /* Hidden 'pad' argument, not in lseek(2) */
972 print_number(ip, narg, c);
973 print_number(ip, narg, c);
981 print_number(ip, narg, c);
982 /* Hidden 'pad' argument, not in lseek(2) */
983 print_number(ip, narg, c);
990 print_number(ip, narg, c);
1000 print_number(ip, narg, c);
1007 print_number(ip, narg, c);
1009 shutdownhowname(*ip);
1013 case SYS_socketpair:
1015 sockdomainname(*ip);
1019 socktypenamewithflags(*ip);
1033 print_number(ip, narg, c);
1055 print_number(ip, narg, c);
1056 print_number(ip, narg, c);
1063 print_number(ip, narg, c);
1064 print_number(ip, narg, c);
1071 print_number(ip, narg, c);
1078 print_number(ip, narg, c);
1079 print_number(ip, narg, c);
1086 print_number(ip, narg, c);
1093 print_number(ip, narg, c);
1094 print_number(ip, narg, c);
1107 case SYS_lio_listio:
1109 lio_listioname(*ip);
1120 case SYS_sched_setscheduler:
1121 print_number(ip, narg, c);
1123 schedpolicyname(*ip);
1127 case SYS_sched_get_priority_max:
1128 case SYS_sched_get_priority_min:
1130 schedpolicyname(*ip);
1135 print_number(ip, narg, c);
1136 print_number(ip, narg, c);
1137 print_number(ip, narg, c);
1138 print_number(ip, narg, c);
1139 print_number(ip, narg, c);
1140 print_number(ip, narg, c);
1142 sendfileflagsname(*ip);
1147 print_number(ip, narg, c);
1153 case SYS_sigprocmask:
1155 sigprocmaskhowname(*ip);
1160 case SYS___acl_get_file:
1161 case SYS___acl_set_file:
1162 case SYS___acl_get_fd:
1163 case SYS___acl_set_fd:
1164 case SYS___acl_delete_file:
1165 case SYS___acl_delete_fd:
1166 case SYS___acl_aclcheck_file:
1167 case SYS___acl_aclcheck_fd:
1168 case SYS___acl_get_link:
1169 case SYS___acl_set_link:
1170 case SYS___acl_delete_link:
1171 case SYS___acl_aclcheck_link:
1172 print_number(ip, narg, c);
1185 case SYS_extattrctl:
1186 print_number(ip, narg, c);
1188 extattrctlname(*ip);
1193 print_number(ip, narg, c);
1194 print_number(ip, narg, c);
1196 mountflagsname(*ip);
1200 case SYS_thr_create:
1201 print_number(ip, narg, c);
1202 print_number(ip, narg, c);
1204 thrcreateflagsname(*ip);
1209 print_number(ip, narg, c);
1215 case SYS_kldunloadf:
1216 print_number(ip, narg, c);
1218 kldunloadfflagsname(*ip);
1225 print_number(ip, narg, c);
1227 atfdname(*ip, decimal);
1231 case SYS_cap_fcntls_limit:
1232 print_number(ip, narg, c);
1239 case SYS_posix_fadvise:
1240 print_number(ip, narg, c);
1241 print_number(ip, narg, c);
1242 print_number(ip, narg, c);
1244 fadvisebehavname((int)*ip);
1250 idtypename(*ip, decimal);
1254 print_number(ip, narg, c);
1256 procctlcmdname(*ip);
1263 print_number(ip, narg, c);
1271 ktrsysret(struct ktr_sysret *ktr, u_int flags)
1273 register_t ret = ktr->ktr_retval;
1274 int error = ktr->ktr_error;
1275 int code = ktr->ktr_code;
1277 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
1278 (code >= nsyscalls || code < 0))
1279 printf("[%d] ", code);
1281 printf("%s", syscallnames[code]);
1283 printf("[%d]", code);
1289 printf("%ld", (long)ret);
1290 if (ret < 0 || ret > 9)
1291 printf("/%#lx", (unsigned long)ret);
1294 printf("%ld", (long)ret);
1296 printf("%#lx", (unsigned long)ret);
1298 } else if (error == ERESTART)
1300 else if (error == EJUSTRETURN)
1301 printf("JUSTRETURN");
1303 printf("-1 errno %d", ktr->ktr_error);
1305 printf(" %s", strerror(ktr->ktr_error));
1311 ktrnamei(char *cp, int len)
1313 printf("\"%.*s\"\n", len, cp);
1317 hexdump(char *p, int len, int screenwidth)
1325 i = 13; /* base offset */
1326 i += (width / 2) + 1; /* spaces every second byte */
1327 i += (width * 2); /* width of bytes */
1329 i += width; /* each byte */
1331 } while (i < screenwidth);
1334 for (n = 0; n < len; n += width) {
1335 for (i = n; i < n + width; i++) {
1336 if ((i % width) == 0) { /* beginning of line */
1337 printf(" 0x%04x", i);
1343 printf("%02x", p[i] & 0xff);
1348 for (i = n; i < n + width; i++) {
1351 if (p[i] >= ' ' && p[i] <= '~')
1358 if ((i % width) != 0)
1363 visdump(char *dp, int datalen, int screenwidth)
1372 for (;datalen > 0; datalen--, dp++) {
1373 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1376 * Keep track of printables and
1377 * space chars (like fold(1)).
1389 width = 8 - (col&07);
1394 if (col + width > (screenwidth-2)) {
1409 ktrgenio(struct ktr_genio *ktr, int len)
1411 int datalen = len - sizeof (struct ktr_genio);
1412 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1413 static int screenwidth = 0;
1416 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1417 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1418 datalen == 1 ? "" : "s");
1421 if (screenwidth == 0) {
1424 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1426 screenwidth = ws.ws_col;
1430 if (maxdata && datalen > maxdata)
1433 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1434 if (dp[i] >= 32 && dp[i] < 127)
1436 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1441 hexdump(dp, datalen, screenwidth);
1443 visdump(dp, datalen, screenwidth);
1446 const char *signames[] = {
1447 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
1448 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
1449 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
1450 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
1451 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
1452 "USR2", NULL, /* 31 - 32 */
1456 ktrpsig(struct ktr_psig *psig)
1458 if (psig->signo > 0 && psig->signo < NSIG)
1459 printf("SIG%s ", signames[psig->signo]);
1461 printf("SIG %d ", psig->signo);
1462 if (psig->action == SIG_DFL) {
1463 printf("SIG_DFL code=");
1464 sigcodename(psig->signo, psig->code);
1467 printf("caught handler=0x%lx mask=0x%x code=",
1468 (u_long)psig->action, psig->mask.__bits[0]);
1469 sigcodename(psig->signo, psig->code);
1475 ktrcsw_old(struct ktr_csw_old *cs)
1477 printf("%s %s\n", cs->out ? "stop" : "resume",
1478 cs->user ? "user" : "kernel");
1482 ktrcsw(struct ktr_csw *cs)
1484 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1485 cs->user ? "user" : "kernel", cs->wmesg);
1488 #define UTRACE_DLOPEN_START 1
1489 #define UTRACE_DLOPEN_STOP 2
1490 #define UTRACE_DLCLOSE_START 3
1491 #define UTRACE_DLCLOSE_STOP 4
1492 #define UTRACE_LOAD_OBJECT 5
1493 #define UTRACE_UNLOAD_OBJECT 6
1494 #define UTRACE_ADD_RUNDEP 7
1495 #define UTRACE_PRELOAD_FINISHED 8
1496 #define UTRACE_INIT_CALL 9
1497 #define UTRACE_FINI_CALL 10
1499 struct utrace_rtld {
1500 char sig[4]; /* 'RTLD' */
1506 char name[MAXPATHLEN];
1510 ktruser_rtld(int len, unsigned char *p)
1512 struct utrace_rtld *ut = (struct utrace_rtld *)p;
1516 switch (ut->event) {
1517 case UTRACE_DLOPEN_START:
1519 printf("dlopen(%s, ", ut->name);
1520 switch (mode & RTLD_MODEMASK) {
1525 printf("RTLD_LAZY");
1528 printf("%#x", mode & RTLD_MODEMASK);
1530 if (mode & RTLD_GLOBAL)
1531 printf(" | RTLD_GLOBAL");
1532 if (mode & RTLD_TRACE)
1533 printf(" | RTLD_TRACE");
1534 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
1535 printf(" | %#x", mode &
1536 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
1539 case UTRACE_DLOPEN_STOP:
1540 printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
1543 case UTRACE_DLCLOSE_START:
1544 printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
1547 case UTRACE_DLCLOSE_STOP:
1548 printf("dlclose(%p) finished\n", ut->handle);
1550 case UTRACE_LOAD_OBJECT:
1551 printf("RTLD: loaded %p @ %p - %p (%s)\n", ut->handle,
1552 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1555 case UTRACE_UNLOAD_OBJECT:
1556 printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
1557 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1560 case UTRACE_ADD_RUNDEP:
1561 parent = ut->mapbase;
1562 printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
1563 ut->handle, ut->name, ut->refcnt);
1565 case UTRACE_PRELOAD_FINISHED:
1566 printf("RTLD: LD_PRELOAD finished\n");
1568 case UTRACE_INIT_CALL:
1569 printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
1572 case UTRACE_FINI_CALL:
1573 printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
1579 printf("RTLD: %d ", len);
1582 printf(" %d", *p++);
1584 printf(" %02x", *p++);
1589 struct utrace_malloc {
1596 ktruser_malloc(unsigned char *p)
1598 struct utrace_malloc *ut = (struct utrace_malloc *)p;
1600 if (ut->p == (void *)(intptr_t)(-1))
1601 printf("malloc_init()\n");
1602 else if (ut->s == 0)
1603 printf("free(%p)\n", ut->p);
1604 else if (ut->p == NULL)
1605 printf("%p = malloc(%zu)\n", ut->r, ut->s);
1607 printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
1611 ktruser(int len, unsigned char *p)
1614 if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
1615 ktruser_rtld(len, p);
1619 if (len == sizeof(struct utrace_malloc)) {
1627 printf(" %d", *p++);
1629 printf(" %02x", *p++);
1634 ktrcaprights(cap_rights_t *rightsp)
1637 printf("cap_rights_t ");
1643 ktrsockaddr(struct sockaddr *sa)
1646 TODO: Support additional address families
1647 #include <netnatm/natm.h>
1648 struct sockaddr_natm *natm;
1649 #include <netsmb/netbios.h>
1650 struct sockaddr_nb *nb;
1655 * note: ktrstruct() has already verified that sa points to a
1656 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1657 * sa->sa_len bytes long.
1659 printf("struct sockaddr { ");
1660 sockfamilyname(sa->sa_family);
1663 #define check_sockaddr_len(n) \
1664 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1665 printf("invalid"); \
1669 switch(sa->sa_family) {
1671 struct sockaddr_in sa_in;
1673 memset(&sa_in, 0, sizeof(sa_in));
1674 memcpy(&sa_in, sa, sa->sa_len);
1675 check_sockaddr_len(in);
1676 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1677 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1681 struct sockaddr_in6 sa_in6;
1683 memset(&sa_in6, 0, sizeof(sa_in6));
1684 memcpy(&sa_in6, sa, sa->sa_len);
1685 check_sockaddr_len(in6);
1686 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1687 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1688 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1692 struct sockaddr_un sa_un;
1694 memset(&sa_un, 0, sizeof(sa_un));
1695 memcpy(&sa_un, sa, sa->sa_len);
1696 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1700 printf("unknown address family");
1706 ktrstat(struct stat *statp)
1708 char mode[12], timestr[PATH_MAX + 4];
1714 * note: ktrstruct() has already verified that statp points to a
1715 * buffer exactly sizeof(struct stat) bytes long.
1717 printf("struct stat {");
1718 printf("dev=%ju, ino=%ju, ",
1719 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1721 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1723 strmode(statp->st_mode, mode);
1724 printf("mode=%s, ", mode);
1726 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1730 #ifdef HAVE_LIBCAPSICUM
1732 pwd = cap_getpwuid(cappwd, statp->st_uid);
1735 pwd = getpwuid(statp->st_uid);
1738 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1740 printf("uid=\"%s\", ", pwd->pw_name);
1744 #ifdef HAVE_LIBCAPSICUM
1746 grp = cap_getgrgid(capgrp, statp->st_gid);
1749 grp = getgrgid(statp->st_gid);
1752 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1754 printf("gid=\"%s\", ", grp->gr_name);
1755 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1758 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1760 tm = localtime(&statp->st_atim.tv_sec);
1761 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1762 printf("\"%s\"", timestr);
1764 if (statp->st_atim.tv_nsec != 0)
1765 printf(".%09ld, ", statp->st_atim.tv_nsec);
1770 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1772 tm = localtime(&statp->st_mtim.tv_sec);
1773 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1774 printf("\"%s\"", timestr);
1776 if (statp->st_mtim.tv_nsec != 0)
1777 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1782 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1784 tm = localtime(&statp->st_ctim.tv_sec);
1785 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1786 printf("\"%s\"", timestr);
1788 if (statp->st_ctim.tv_nsec != 0)
1789 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1792 printf("birthtime=");
1794 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1796 tm = localtime(&statp->st_birthtim.tv_sec);
1797 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1798 printf("\"%s\"", timestr);
1800 if (statp->st_birthtim.tv_nsec != 0)
1801 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1804 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1805 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1806 (intmax_t)statp->st_blocks, statp->st_flags);
1811 ktrstruct(char *buf, size_t buflen)
1814 size_t namelen, datalen;
1816 cap_rights_t rights;
1818 struct sockaddr_storage ss;
1820 for (name = buf, namelen = 0;
1821 namelen < buflen && name[namelen] != '\0';
1824 if (namelen == buflen)
1826 if (name[namelen] != '\0')
1828 data = buf + namelen + 1;
1829 datalen = buflen - namelen - 1;
1833 for (i = 0; i < (int)namelen; ++i)
1834 if (!isalpha(name[i]))
1836 if (strcmp(name, "caprights") == 0) {
1837 if (datalen != sizeof(cap_rights_t))
1839 memcpy(&rights, data, datalen);
1840 ktrcaprights(&rights);
1841 } else if (strcmp(name, "stat") == 0) {
1842 if (datalen != sizeof(struct stat))
1844 memcpy(&sb, data, datalen);
1846 } else if (strcmp(name, "sockaddr") == 0) {
1847 if (datalen > sizeof(ss))
1849 memcpy(&ss, data, datalen);
1850 if (datalen != ss.ss_len)
1852 ktrsockaddr((struct sockaddr *)&ss);
1854 printf("unknown structure\n");
1858 printf("invalid record\n");
1862 ktrcapfail(struct ktr_cap_fail *ktr)
1864 switch (ktr->cap_type) {
1865 case CAPFAIL_NOTCAPABLE:
1866 /* operation on fd with insufficient capabilities */
1867 printf("operation requires ");
1868 capname(&ktr->cap_needed);
1869 printf(", descriptor holds ");
1870 capname(&ktr->cap_held);
1872 case CAPFAIL_INCREASE:
1873 /* requested more capabilities than fd already has */
1874 printf("attempt to increase capabilities from ");
1875 capname(&ktr->cap_held);
1877 capname(&ktr->cap_needed);
1879 case CAPFAIL_SYSCALL:
1880 /* called restricted syscall */
1881 printf("disallowed system call");
1883 case CAPFAIL_LOOKUP:
1884 /* used ".." in strict-relative mode */
1885 printf("restricted VFS lookup");
1888 printf("unknown capability failure: ");
1889 capname(&ktr->cap_needed);
1891 capname(&ktr->cap_held);
1898 ktrfault(struct ktr_fault *ktr)
1901 printf("0x%jx ", ktr->vaddr);
1902 vmprotname(ktr->type);
1907 ktrfaultend(struct ktr_faultend *ktr)
1910 vmresultname(ktr->result);
1914 #if defined(__amd64__) || defined(__i386__)
1916 linux_ktrsyscall(struct ktr_syscall *ktr)
1918 int narg = ktr->ktr_narg;
1921 if (ktr->ktr_code >= nlinux_syscalls || ktr->ktr_code < 0)
1922 printf("[%d]", ktr->ktr_code);
1924 printf("%s", linux_syscallnames[ktr->ktr_code]);
1926 printf("[%d]", ktr->ktr_code);
1928 ip = &ktr->ktr_args[0];
1932 print_number(ip, narg, c);
1939 linux_ktrsysret(struct ktr_sysret *ktr)
1941 register_t ret = ktr->ktr_retval;
1942 int error = ktr->ktr_error;
1943 int code = ktr->ktr_code;
1945 if (code >= nlinux_syscalls || code < 0)
1946 printf("[%d] ", code);
1948 printf("%s", linux_syscallnames[code]);
1950 printf("[%d]", code);
1956 printf("%ld", (long)ret);
1957 if (ret < 0 || ret > 9)
1958 printf("/%#lx", (unsigned long)ret);
1961 printf("%ld", (long)ret);
1963 printf("%#lx", (unsigned long)ret);
1965 } else if (error == ERESTART)
1967 else if (error == EJUSTRETURN)
1968 printf("JUSTRETURN");
1970 if (ktr->ktr_error <= ELAST + 1)
1971 error = abs(bsd_to_linux_errno[ktr->ktr_error]);
1974 printf("-1 errno %d", error);
1976 printf(" %s", strerror(ktr->ktr_error));
1985 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
1986 "[-m maxdata] [-p pid] [-t trstr]\n");