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>
67 #include <arpa/inet.h>
68 #include <netinet/in.h>
80 #include <sysdecode.h>
86 #include "kdump_subr.h"
89 #include <libcasper.h>
91 #include <casper/cap_grp.h>
92 #include <casper/cap_pwd.h>
95 u_int abidump(struct ktr_header *);
96 int fetchprocinfo(struct ktr_header *, u_int *);
97 int fread_tail(void *, int, int);
98 void dumpheader(struct ktr_header *);
99 void ktrsyscall(struct ktr_syscall *, u_int);
100 void ktrsysret(struct ktr_sysret *, u_int);
101 void ktrnamei(char *, int);
102 void hexdump(char *, int, int);
103 void visdump(char *, int, int);
104 void ktrgenio(struct ktr_genio *, int);
105 void ktrpsig(struct ktr_psig *);
106 void ktrcsw(struct ktr_csw *);
107 void ktrcsw_old(struct ktr_csw_old *);
108 void ktruser(int, void *);
109 void ktrcaprights(cap_rights_t *);
110 void ktritimerval(struct itimerval *it);
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);
120 #define TIMESTAMP_NONE 0x0
121 #define TIMESTAMP_ABSOLUTE 0x1
122 #define TIMESTAMP_ELAPSED 0x2
123 #define TIMESTAMP_RELATIVE 0x4
125 extern const char *signames[];
127 static int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
128 resolv = 0, abiflag = 0, syscallno = 0;
129 static const char *tracefile = DEF_TRACEFILE;
130 static struct ktr_header ktr_header;
132 #define TIME_FORMAT "%b %e %T %Y"
133 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
135 #define print_number(i,n,c) do { \
137 printf("%c%jd", c, (intmax_t)*i); \
139 printf("%c%#jx", c, (uintmax_t)(u_register_t)*i); \
147 TAILQ_ENTRY(proc_info) info;
152 static TAILQ_HEAD(trace_procs, proc_info) trace_procs;
154 #ifdef HAVE_LIBCASPER
155 static cap_channel_t *cappwd, *capgrp;
163 * Cache NLS data before entering capability mode.
164 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
166 (void)catopen("libc", NL_CAT_LOCALE);
175 * Allow localtime(3) to cache /etc/localtime content before entering
177 * XXXPJD: There should be localtime_init() in libc.
180 (void)localtime(<ime);
183 #ifdef HAVE_LIBCASPER
185 cappwdgrp_setup(cap_channel_t **cappwdp, cap_channel_t **capgrpp)
187 cap_channel_t *capcas, *cappwdloc, *capgrploc;
188 const char *cmds[1], *fields[1];
191 if (capcas == NULL) {
192 err(1, "unable to create casper process");
195 cappwdloc = cap_service_open(capcas, "system.pwd");
196 capgrploc = cap_service_open(capcas, "system.grp");
197 /* Casper capability no longer needed. */
199 if (cappwdloc == NULL || capgrploc == NULL) {
200 if (cappwdloc == NULL)
201 warn("unable to open system.pwd service");
202 if (capgrploc == NULL)
203 warn("unable to open system.grp service");
206 /* Limit system.pwd to only getpwuid() function and pw_name field. */
207 cmds[0] = "getpwuid";
208 if (cap_pwd_limit_cmds(cappwdloc, cmds, 1) < 0)
209 err(1, "unable to limit system.pwd service");
210 fields[0] = "pw_name";
211 if (cap_pwd_limit_fields(cappwdloc, fields, 1) < 0)
212 err(1, "unable to limit system.pwd service");
213 /* Limit system.grp to only getgrgid() function and gr_name field. */
214 cmds[0] = "getgrgid";
215 if (cap_grp_limit_cmds(capgrploc, cmds, 1) < 0)
216 err(1, "unable to limit system.grp service");
217 fields[0] = "gr_name";
218 if (cap_grp_limit_fields(capgrploc, fields, 1) < 0)
219 err(1, "unable to limit system.grp service");
221 *cappwdp = cappwdloc;
222 *capgrpp = capgrploc;
225 #endif /* HAVE_LIBCASPER */
228 main(int argc, char *argv[])
230 int ch, ktrlen, size;
232 int trpoints = ALL_POINTS;
237 setlocale(LC_CTYPE, "");
239 timestamp = TIMESTAMP_NONE;
241 while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1)
256 maxdata = atoi(optarg);
274 timestamp |= TIMESTAMP_ELAPSED;
280 timestamp |= TIMESTAMP_RELATIVE;
283 timestamp |= TIMESTAMP_ABSOLUTE;
286 trpoints = getpoints(optarg);
288 errx(1, "unknown trace point in %s", optarg);
297 m = malloc(size = 1025);
299 errx(1, "%s", strerror(ENOMEM));
300 if (strcmp(tracefile, "-") != 0)
301 if (!freopen(tracefile, "r", stdin))
302 err(1, "%s", tracefile);
306 #ifdef HAVE_LIBCASPER
308 if (cappwdgrp_setup(&cappwd, &capgrp) < 0) {
313 if (resolv == 0 || (cappwd != NULL && capgrp != NULL)) {
314 if (cap_enter() < 0 && errno != ENOSYS)
315 err(1, "unable to enter capability mode");
319 if (cap_enter() < 0 && errno != ENOSYS)
320 err(1, "unable to enter capability mode");
323 limitfd(STDIN_FILENO);
324 limitfd(STDOUT_FILENO);
325 limitfd(STDERR_FILENO);
327 TAILQ_INIT(&trace_procs);
329 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
330 if (ktr_header.ktr_type & KTR_DROP) {
331 ktr_header.ktr_type &= ~KTR_DROP;
332 if (!drop_logged && threads) {
334 "%6jd %6jd %-8.*s Events dropped.\n",
335 (intmax_t)ktr_header.ktr_pid,
336 ktr_header.ktr_tid > 0 ?
337 (intmax_t)ktr_header.ktr_tid : 0,
338 MAXCOMLEN, ktr_header.ktr_comm);
340 } else if (!drop_logged) {
341 printf("%6jd %-8.*s Events dropped.\n",
342 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
343 ktr_header.ktr_comm);
347 if (trpoints & (1<<ktr_header.ktr_type))
348 if (pid == 0 || ktr_header.ktr_pid == pid ||
349 ktr_header.ktr_tid == pid)
350 dumpheader(&ktr_header);
351 if ((ktrlen = ktr_header.ktr_len) < 0)
352 errx(1, "bogus length 0x%x", ktrlen);
354 m = realloc(m, ktrlen+1);
356 errx(1, "%s", strerror(ENOMEM));
359 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
360 errx(1, "data too short");
361 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
363 sv_flags = abidump(&ktr_header);
364 if (pid && ktr_header.ktr_pid != pid &&
365 ktr_header.ktr_tid != pid)
367 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
370 switch (ktr_header.ktr_type) {
372 ktrsyscall((struct ktr_syscall *)m, sv_flags);
375 ktrsysret((struct ktr_sysret *)m, sv_flags);
382 ktrgenio((struct ktr_genio *)m, ktrlen);
385 ktrpsig((struct ktr_psig *)m);
388 if (ktrlen == sizeof(struct ktr_csw_old))
389 ktrcsw_old((struct ktr_csw_old *)m);
391 ktrcsw((struct ktr_csw *)m);
397 ktrstruct(m, ktrlen);
400 ktrcapfail((struct ktr_cap_fail *)m);
403 ktrfault((struct ktr_fault *)m);
406 ktrfaultend((struct ktr_faultend *)m);
424 cap_rights_init(&rights, CAP_FSTAT);
429 cap_rights_set(&rights, CAP_READ);
432 cap_rights_set(&rights, CAP_IOCTL, CAP_WRITE);
433 cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
436 cap_rights_set(&rights, CAP_WRITE);
438 cap_rights_set(&rights, CAP_IOCTL);
446 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
447 err(1, "unable to limit rights for descriptor %d", fd);
448 if (cmd != 0 && cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
449 err(1, "unable to limit ioctls for descriptor %d", fd);
453 fread_tail(void *buf, int size, int num)
457 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
465 fetchprocinfo(struct ktr_header *kth, u_int *flags)
467 struct proc_info *pi;
469 switch (kth->ktr_type) {
471 TAILQ_FOREACH(pi, &trace_procs, info) {
472 if (pi->pid == kth->ktr_pid) {
473 TAILQ_REMOVE(&trace_procs, pi, info);
477 pi = malloc(sizeof(struct proc_info));
479 errx(1, "%s", strerror(ENOMEM));
480 pi->sv_flags = *flags;
481 pi->pid = kth->ktr_pid;
482 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
486 TAILQ_FOREACH(pi, &trace_procs, info) {
487 if (pi->pid == kth->ktr_pid) {
488 TAILQ_REMOVE(&trace_procs, pi, info);
500 abidump(struct ktr_header *kth)
502 struct proc_info *pi;
507 TAILQ_FOREACH(pi, &trace_procs, info) {
508 if (pi->pid == kth->ktr_pid) {
509 flags = pi->sv_flags;
517 switch (flags & SV_ABI_MASK) {
524 case SV_ABI_CLOUDABI:
534 else if (flags & SV_ILP32)
539 printf("%s%s ", abi, arch);
545 dumpheader(struct ktr_header *kth)
547 static char unknown[64];
548 static struct timeval prevtime, prevtime_e, temp;
552 switch (kth->ktr_type) {
594 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
599 * The ktr_tid field was previously the ktr_buffer field, which held
600 * the kernel pointer value for the buffer associated with data
601 * following the record header. It now holds a threadid, but only
602 * for trace files after the change. Older trace files still contain
603 * kernel pointers. Detect this and suppress the results by printing
604 * negative tid's as 0.
607 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
608 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
609 MAXCOMLEN, kth->ktr_comm);
611 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
614 if (timestamp & TIMESTAMP_ABSOLUTE) {
615 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
616 kth->ktr_time.tv_usec);
618 if (timestamp & TIMESTAMP_ELAPSED) {
619 if (prevtime_e.tv_sec == 0)
620 prevtime_e = kth->ktr_time;
621 timevalsub(&kth->ktr_time, &prevtime_e);
622 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
623 kth->ktr_time.tv_usec);
624 timevaladd(&kth->ktr_time, &prevtime_e);
626 if (timestamp & TIMESTAMP_RELATIVE) {
627 if (prevtime.tv_sec == 0)
628 prevtime = kth->ktr_time;
629 temp = kth->ktr_time;
630 timevalsub(&kth->ktr_time, &prevtime);
631 if ((intmax_t)kth->ktr_time.tv_sec < 0) {
632 kth->ktr_time = prevtime;
634 timevalsub(&kth->ktr_time, &prevtime);
640 printf("%s%jd.%06ld ", sign, (intmax_t)kth->ktr_time.tv_sec,
641 kth->ktr_time.tv_usec);
647 #include <sys/syscall.h>
650 ioctlname(unsigned long val)
654 str = sysdecode_ioctlname(val);
663 static enum sysdecode_abi
664 syscallabi(u_int sv_flags)
668 return (SYSDECODE_ABI_FREEBSD);
669 switch (sv_flags & SV_ABI_MASK) {
671 return (SYSDECODE_ABI_FREEBSD);
672 #if defined(__amd64__) || defined(__i386__)
675 if (sv_flags & SV_ILP32)
676 return (SYSDECODE_ABI_LINUX32);
678 return (SYSDECODE_ABI_LINUX);
680 #if defined(__aarch64__) || defined(__amd64__)
681 case SV_ABI_CLOUDABI:
682 return (SYSDECODE_ABI_CLOUDABI64);
685 return (SYSDECODE_ABI_UNKNOWN);
690 syscallname(u_int code, u_int sv_flags)
694 name = sysdecode_syscallname(syscallabi(sv_flags), code);
696 printf("[%d]", code);
700 printf("[%d]", code);
705 ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags)
707 int narg = ktr->ktr_narg;
711 syscallname(ktr->ktr_code, sv_flags);
712 ip = &ktr->ktr_args[0];
717 (sv_flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
718 switch (ktr->ktr_code) {
736 atfdname(*ip, decimal);
742 switch (ktr->ktr_code) {
744 print_number(ip, narg, c);
762 print_number(ip, narg, c);
770 print_number(ip, narg, c);
772 flagsandmodename(ip[0], ip[1], decimal);
777 print_number(ip, narg, c);
778 print_number(ip, narg, c);
780 * A flags value of zero is valid for
781 * wait4() but not for wait6(), so
782 * handle zero special here.
785 print_number(ip, narg, c);
795 idtypename(*ip, decimal);
799 print_number(ip, narg, c);
800 print_number(ip, narg, c);
810 print_number(ip, narg, c);
818 print_number(ip, narg, c);
825 print_number(ip, narg, c);
826 print_number(ip, narg, c);
828 getfsstatflagsname(*ip);
833 print_number(ip, narg, c);
834 print_number(ip, narg, c);
841 print_number(ip, narg, c);
849 print_number(ip, narg, c);
850 print_number(ip, narg, c);
852 sendrecvflagsname(*ip);
858 print_number(ip, narg, c);
859 print_number(ip, narg, c);
860 print_number(ip, narg, c);
862 sendrecvflagsname(*ip);
869 print_number(ip, narg, c);
876 print_number(ip, narg, c);
895 print_number(ip, narg, c);
896 print_number(ip, narg, c);
902 #ifdef SYS_freebsd6_mmap
903 case SYS_freebsd6_mmap:
904 print_number(ip, narg, c);
905 print_number(ip, narg, c);
917 print_number(ip, narg, c);
918 print_number(ip, narg, c);
929 print_number(ip, narg, c);
930 print_number(ip, narg, c);
937 print_number(ip, narg, c);
938 print_number(ip, narg, c);
940 madvisebehavname(*ip);
944 case SYS_setpriority:
945 print_number(ip, narg, c);
946 print_number(ip, narg, c);
953 print_number(ip, narg, c);
955 fcntlcmdname(ip[0], ip[1], decimal);
963 sockdomainname(sockdomain);
967 socktypenamewithflags(*ip);
970 if (sockdomain == PF_INET ||
971 sockdomain == PF_INET6) {
973 sockipprotoname(*ip);
982 print_number(ip, narg, c);
984 sockoptlevelname(*ip, decimal);
985 if (*ip == SOL_SOCKET) {
994 #ifdef SYS_freebsd6_lseek
995 case SYS_freebsd6_lseek:
996 print_number(ip, narg, c);
997 /* Hidden 'pad' argument, not in lseek(2) */
998 print_number(ip, narg, c);
999 print_number(ip, narg, c);
1007 print_number(ip, narg, c);
1008 /* Hidden 'pad' argument, not in lseek(2) */
1009 print_number(ip, narg, c);
1016 print_number(ip, narg, c);
1026 print_number(ip, narg, c);
1033 print_number(ip, narg, c);
1035 shutdownhowname(*ip);
1039 case SYS_socketpair:
1041 sockdomainname(*ip);
1045 socktypenamewithflags(*ip);
1059 print_number(ip, narg, c);
1081 print_number(ip, narg, c);
1082 print_number(ip, narg, c);
1089 print_number(ip, narg, c);
1090 print_number(ip, narg, c);
1097 print_number(ip, narg, c);
1104 print_number(ip, narg, c);
1105 print_number(ip, narg, c);
1112 print_number(ip, narg, c);
1119 print_number(ip, narg, c);
1122 printf(",0%o", (unsigned int)ip[1]);
1127 print_number(ip, narg, c);
1128 print_number(ip, narg, c);
1141 case SYS_lio_listio:
1143 lio_listioname(*ip);
1154 case SYS_sched_setscheduler:
1155 print_number(ip, narg, c);
1157 schedpolicyname(*ip);
1161 case SYS_sched_get_priority_max:
1162 case SYS_sched_get_priority_min:
1164 schedpolicyname(*ip);
1169 print_number(ip, narg, c);
1170 print_number(ip, narg, c);
1171 print_number(ip, narg, c);
1172 print_number(ip, narg, c);
1173 print_number(ip, narg, c);
1174 print_number(ip, narg, c);
1176 sendfileflagsname(*(int *)ip);
1181 print_number(ip, narg, c);
1187 case SYS_sigprocmask:
1189 sigprocmaskhowname(*ip);
1194 case SYS___acl_get_file:
1195 case SYS___acl_set_file:
1196 case SYS___acl_get_fd:
1197 case SYS___acl_set_fd:
1198 case SYS___acl_delete_file:
1199 case SYS___acl_delete_fd:
1200 case SYS___acl_aclcheck_file:
1201 case SYS___acl_aclcheck_fd:
1202 case SYS___acl_get_link:
1203 case SYS___acl_set_link:
1204 case SYS___acl_delete_link:
1205 case SYS___acl_aclcheck_link:
1206 print_number(ip, narg, c);
1219 case SYS_extattrctl:
1220 print_number(ip, narg, c);
1222 extattrctlname(*ip);
1227 print_number(ip, narg, c);
1228 print_number(ip, narg, c);
1230 mountflagsname(*ip);
1234 case SYS_thr_create:
1235 print_number(ip, narg, c);
1236 print_number(ip, narg, c);
1238 thrcreateflagsname(*ip);
1243 print_number(ip, narg, c);
1249 case SYS_kldunloadf:
1250 print_number(ip, narg, c);
1252 kldunloadfflagsname(*ip);
1259 print_number(ip, narg, c);
1261 atfdname(*ip, decimal);
1265 case SYS_cap_fcntls_limit:
1266 print_number(ip, narg, c);
1273 case SYS_posix_fadvise:
1274 print_number(ip, narg, c);
1275 print_number(ip, narg, c);
1276 print_number(ip, narg, c);
1278 fadvisebehavname((int)*ip);
1284 idtypename(*ip, decimal);
1288 print_number(ip, narg, c);
1290 procctlcmdname(*ip);
1295 print_number(ip, narg, c);
1299 case UMTX_OP_CV_WAIT:
1303 umtxcvwaitflags(*ip);
1305 case UMTX_OP_RW_RDLOCK:
1309 umtxrwlockflags(*ip);
1317 print_number(ip, narg, c);
1325 ktrsysret(struct ktr_sysret *ktr, u_int sv_flags)
1327 register_t ret = ktr->ktr_retval;
1328 int error = ktr->ktr_error;
1330 syscallname(ktr->ktr_code, sv_flags);
1335 printf("%ld", (long)ret);
1336 if (ret < 0 || ret > 9)
1337 printf("/%#lx", (unsigned long)ret);
1340 printf("%ld", (long)ret);
1342 printf("%#lx", (unsigned long)ret);
1344 } else if (error == ERESTART)
1346 else if (error == EJUSTRETURN)
1347 printf("JUSTRETURN");
1349 printf("-1 errno %d", sysdecode_freebsd_to_abi_errno(
1350 syscallabi(sv_flags), 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);
1536 ktruser(int len, void *p)
1540 if (sysdecode_utrace(stdout, p, len)) {
1549 printf(" %d", *cp++);
1551 printf(" %02x", *cp++);
1556 ktrcaprights(cap_rights_t *rightsp)
1559 printf("cap_rights_t ");
1565 ktrtimeval(struct timeval *tv)
1568 printf("{%ld, %ld}", (long)tv->tv_sec, tv->tv_usec);
1572 ktritimerval(struct itimerval *it)
1575 printf("itimerval { .interval = ");
1576 ktrtimeval(&it->it_interval);
1577 printf(", .value = ");
1578 ktrtimeval(&it->it_value);
1583 ktrsockaddr(struct sockaddr *sa)
1586 TODO: Support additional address families
1587 #include <netnatm/natm.h>
1588 struct sockaddr_natm *natm;
1589 #include <netsmb/netbios.h>
1590 struct sockaddr_nb *nb;
1595 * note: ktrstruct() has already verified that sa points to a
1596 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1597 * sa->sa_len bytes long.
1599 printf("struct sockaddr { ");
1600 sockfamilyname(sa->sa_family);
1603 #define check_sockaddr_len(n) \
1604 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1605 printf("invalid"); \
1609 switch(sa->sa_family) {
1611 struct sockaddr_in sa_in;
1613 memset(&sa_in, 0, sizeof(sa_in));
1614 memcpy(&sa_in, sa, sa->sa_len);
1615 check_sockaddr_len(in);
1616 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1617 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1621 struct sockaddr_in6 sa_in6;
1623 memset(&sa_in6, 0, sizeof(sa_in6));
1624 memcpy(&sa_in6, sa, sa->sa_len);
1625 check_sockaddr_len(in6);
1626 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1627 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1628 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1632 struct sockaddr_un sa_un;
1634 memset(&sa_un, 0, sizeof(sa_un));
1635 memcpy(&sa_un, sa, sa->sa_len);
1636 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1640 printf("unknown address family");
1646 ktrstat(struct stat *statp)
1648 char mode[12], timestr[PATH_MAX + 4];
1654 * note: ktrstruct() has already verified that statp points to a
1655 * buffer exactly sizeof(struct stat) bytes long.
1657 printf("struct stat {");
1658 printf("dev=%ju, ino=%ju, ",
1659 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1661 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1663 strmode(statp->st_mode, mode);
1664 printf("mode=%s, ", mode);
1666 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1670 #ifdef HAVE_LIBCASPER
1672 pwd = cap_getpwuid(cappwd, statp->st_uid);
1675 pwd = getpwuid(statp->st_uid);
1678 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1680 printf("uid=\"%s\", ", pwd->pw_name);
1684 #ifdef HAVE_LIBCASPER
1686 grp = cap_getgrgid(capgrp, statp->st_gid);
1689 grp = getgrgid(statp->st_gid);
1692 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1694 printf("gid=\"%s\", ", grp->gr_name);
1695 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1698 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1700 tm = localtime(&statp->st_atim.tv_sec);
1701 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1702 printf("\"%s\"", timestr);
1704 if (statp->st_atim.tv_nsec != 0)
1705 printf(".%09ld, ", statp->st_atim.tv_nsec);
1710 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1712 tm = localtime(&statp->st_mtim.tv_sec);
1713 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1714 printf("\"%s\"", timestr);
1716 if (statp->st_mtim.tv_nsec != 0)
1717 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1722 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1724 tm = localtime(&statp->st_ctim.tv_sec);
1725 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1726 printf("\"%s\"", timestr);
1728 if (statp->st_ctim.tv_nsec != 0)
1729 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1732 printf("birthtime=");
1734 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1736 tm = localtime(&statp->st_birthtim.tv_sec);
1737 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1738 printf("\"%s\"", timestr);
1740 if (statp->st_birthtim.tv_nsec != 0)
1741 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1744 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1745 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1746 (intmax_t)statp->st_blocks, statp->st_flags);
1751 ktrstruct(char *buf, size_t buflen)
1754 size_t namelen, datalen;
1756 cap_rights_t rights;
1757 struct itimerval it;
1759 struct sockaddr_storage ss;
1761 for (name = buf, namelen = 0;
1762 namelen < buflen && name[namelen] != '\0';
1765 if (namelen == buflen)
1767 if (name[namelen] != '\0')
1769 data = buf + namelen + 1;
1770 datalen = buflen - namelen - 1;
1774 for (i = 0; i < (int)namelen; ++i)
1775 if (!isalpha(name[i]))
1777 if (strcmp(name, "caprights") == 0) {
1778 if (datalen != sizeof(cap_rights_t))
1780 memcpy(&rights, data, datalen);
1781 ktrcaprights(&rights);
1782 } else if (strcmp(name, "itimerval") == 0) {
1783 if (datalen != sizeof(struct itimerval))
1785 memcpy(&it, data, datalen);
1787 } else if (strcmp(name, "stat") == 0) {
1788 if (datalen != sizeof(struct stat))
1790 memcpy(&sb, data, datalen);
1792 } else if (strcmp(name, "sockaddr") == 0) {
1793 if (datalen > sizeof(ss))
1795 memcpy(&ss, data, datalen);
1796 if (datalen != ss.ss_len)
1798 ktrsockaddr((struct sockaddr *)&ss);
1800 printf("unknown structure\n");
1804 printf("invalid record\n");
1808 ktrcapfail(struct ktr_cap_fail *ktr)
1810 switch (ktr->cap_type) {
1811 case CAPFAIL_NOTCAPABLE:
1812 /* operation on fd with insufficient capabilities */
1813 printf("operation requires ");
1814 capname(&ktr->cap_needed);
1815 printf(", descriptor holds ");
1816 capname(&ktr->cap_held);
1818 case CAPFAIL_INCREASE:
1819 /* requested more capabilities than fd already has */
1820 printf("attempt to increase capabilities from ");
1821 capname(&ktr->cap_held);
1823 capname(&ktr->cap_needed);
1825 case CAPFAIL_SYSCALL:
1826 /* called restricted syscall */
1827 printf("disallowed system call");
1829 case CAPFAIL_LOOKUP:
1830 /* used ".." in strict-relative mode */
1831 printf("restricted VFS lookup");
1834 printf("unknown capability failure: ");
1835 capname(&ktr->cap_needed);
1837 capname(&ktr->cap_held);
1844 ktrfault(struct ktr_fault *ktr)
1847 printf("0x%jx ", (uintmax_t)ktr->vaddr);
1848 vmprotname(ktr->type);
1853 ktrfaultend(struct ktr_faultend *ktr)
1856 vmresultname(ktr->result);
1863 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
1864 "[-m maxdata] [-p pid] [-t trstr]\n");