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_malloc(void *);
109 void ktruser_rtld(int, void *);
110 void ktruser(int, void *);
111 void ktrcaprights(cap_rights_t *);
112 void ktrsockaddr(struct sockaddr *);
113 void ktrstat(struct stat *);
114 void ktrstruct(char *, size_t);
115 void ktrcapfail(struct ktr_cap_fail *);
116 void ktrfault(struct ktr_fault *);
117 void ktrfaultend(struct ktr_faultend *);
118 void limitfd(int fd);
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[];
128 static int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
129 resolv = 0, abiflag = 0, syscallno = 0;
130 static const char *tracefile = DEF_TRACEFILE;
131 static struct ktr_header ktr_header;
133 #define TIME_FORMAT "%b %e %T %Y"
134 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
136 #define print_number(i,n,c) do { \
138 printf("%c%jd", c, (intmax_t)*i); \
140 printf("%c%#jx", c, (uintmax_t)(u_register_t)*i); \
148 TAILQ_ENTRY(proc_info) info;
153 static TAILQ_HEAD(trace_procs, proc_info) trace_procs;
155 #ifdef HAVE_LIBCASPER
156 static cap_channel_t *cappwd, *capgrp;
164 * Cache NLS data before entering capability mode.
165 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
167 (void)catopen("libc", NL_CAT_LOCALE);
176 * Allow localtime(3) to cache /etc/localtime content before entering
178 * XXXPJD: There should be localtime_init() in libc.
181 (void)localtime(<ime);
184 #ifdef HAVE_LIBCASPER
186 cappwdgrp_setup(cap_channel_t **cappwdp, cap_channel_t **capgrpp)
188 cap_channel_t *capcas, *cappwdloc, *capgrploc;
189 const char *cmds[1], *fields[1];
192 if (capcas == NULL) {
193 err(1, "unable to create casper process");
196 cappwdloc = cap_service_open(capcas, "system.pwd");
197 capgrploc = cap_service_open(capcas, "system.grp");
198 /* Casper capability no longer needed. */
200 if (cappwdloc == NULL || capgrploc == NULL) {
201 if (cappwdloc == NULL)
202 warn("unable to open system.pwd service");
203 if (capgrploc == NULL)
204 warn("unable to open system.grp service");
207 /* Limit system.pwd to only getpwuid() function and pw_name field. */
208 cmds[0] = "getpwuid";
209 if (cap_pwd_limit_cmds(cappwdloc, cmds, 1) < 0)
210 err(1, "unable to limit system.pwd service");
211 fields[0] = "pw_name";
212 if (cap_pwd_limit_fields(cappwdloc, fields, 1) < 0)
213 err(1, "unable to limit system.pwd service");
214 /* Limit system.grp to only getgrgid() function and gr_name field. */
215 cmds[0] = "getgrgid";
216 if (cap_grp_limit_cmds(capgrploc, cmds, 1) < 0)
217 err(1, "unable to limit system.grp service");
218 fields[0] = "gr_name";
219 if (cap_grp_limit_fields(capgrploc, fields, 1) < 0)
220 err(1, "unable to limit system.grp service");
222 *cappwdp = cappwdloc;
223 *capgrpp = capgrploc;
226 #endif /* HAVE_LIBCASPER */
229 main(int argc, char *argv[])
231 int ch, ktrlen, size;
233 int trpoints = ALL_POINTS;
238 setlocale(LC_CTYPE, "");
240 timestamp = TIMESTAMP_NONE;
242 while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1)
257 maxdata = atoi(optarg);
275 timestamp |= TIMESTAMP_ELAPSED;
281 timestamp |= TIMESTAMP_RELATIVE;
284 timestamp |= TIMESTAMP_ABSOLUTE;
287 trpoints = getpoints(optarg);
289 errx(1, "unknown trace point in %s", optarg);
298 m = malloc(size = 1025);
300 errx(1, "%s", strerror(ENOMEM));
301 if (strcmp(tracefile, "-") != 0)
302 if (!freopen(tracefile, "r", stdin))
303 err(1, "%s", tracefile);
307 #ifdef HAVE_LIBCASPER
309 if (cappwdgrp_setup(&cappwd, &capgrp) < 0) {
314 if (resolv == 0 || (cappwd != NULL && capgrp != NULL)) {
315 if (cap_enter() < 0 && errno != ENOSYS)
316 err(1, "unable to enter capability mode");
320 if (cap_enter() < 0 && errno != ENOSYS)
321 err(1, "unable to enter capability mode");
324 limitfd(STDIN_FILENO);
325 limitfd(STDOUT_FILENO);
326 limitfd(STDERR_FILENO);
328 TAILQ_INIT(&trace_procs);
330 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
331 if (ktr_header.ktr_type & KTR_DROP) {
332 ktr_header.ktr_type &= ~KTR_DROP;
333 if (!drop_logged && threads) {
335 "%6jd %6jd %-8.*s Events dropped.\n",
336 (intmax_t)ktr_header.ktr_pid,
337 ktr_header.ktr_tid > 0 ?
338 (intmax_t)ktr_header.ktr_tid : 0,
339 MAXCOMLEN, ktr_header.ktr_comm);
341 } else if (!drop_logged) {
342 printf("%6jd %-8.*s Events dropped.\n",
343 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
344 ktr_header.ktr_comm);
348 if (trpoints & (1<<ktr_header.ktr_type))
349 if (pid == 0 || ktr_header.ktr_pid == pid ||
350 ktr_header.ktr_tid == pid)
351 dumpheader(&ktr_header);
352 if ((ktrlen = ktr_header.ktr_len) < 0)
353 errx(1, "bogus length 0x%x", ktrlen);
355 m = realloc(m, ktrlen+1);
357 errx(1, "%s", strerror(ENOMEM));
360 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
361 errx(1, "data too short");
362 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
364 sv_flags = abidump(&ktr_header);
365 if (pid && ktr_header.ktr_pid != pid &&
366 ktr_header.ktr_tid != pid)
368 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
371 switch (ktr_header.ktr_type) {
373 ktrsyscall((struct ktr_syscall *)m, sv_flags);
376 ktrsysret((struct ktr_sysret *)m, sv_flags);
383 ktrgenio((struct ktr_genio *)m, ktrlen);
386 ktrpsig((struct ktr_psig *)m);
389 if (ktrlen == sizeof(struct ktr_csw_old))
390 ktrcsw_old((struct ktr_csw_old *)m);
392 ktrcsw((struct ktr_csw *)m);
398 ktrstruct(m, ktrlen);
401 ktrcapfail((struct ktr_cap_fail *)m);
404 ktrfault((struct ktr_fault *)m);
407 ktrfaultend((struct ktr_faultend *)m);
425 cap_rights_init(&rights, CAP_FSTAT);
430 cap_rights_set(&rights, CAP_READ);
433 cap_rights_set(&rights, CAP_IOCTL, CAP_WRITE);
434 cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
437 cap_rights_set(&rights, CAP_WRITE);
439 cap_rights_set(&rights, CAP_IOCTL);
447 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
448 err(1, "unable to limit rights for descriptor %d", fd);
449 if (cmd != 0 && cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
450 err(1, "unable to limit ioctls for descriptor %d", fd);
454 fread_tail(void *buf, int size, int num)
458 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
466 fetchprocinfo(struct ktr_header *kth, u_int *flags)
468 struct proc_info *pi;
470 switch (kth->ktr_type) {
472 TAILQ_FOREACH(pi, &trace_procs, info) {
473 if (pi->pid == kth->ktr_pid) {
474 TAILQ_REMOVE(&trace_procs, pi, info);
478 pi = malloc(sizeof(struct proc_info));
480 errx(1, "%s", strerror(ENOMEM));
481 pi->sv_flags = *flags;
482 pi->pid = kth->ktr_pid;
483 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
487 TAILQ_FOREACH(pi, &trace_procs, info) {
488 if (pi->pid == kth->ktr_pid) {
489 TAILQ_REMOVE(&trace_procs, pi, info);
501 abidump(struct ktr_header *kth)
503 struct proc_info *pi;
508 TAILQ_FOREACH(pi, &trace_procs, info) {
509 if (pi->pid == kth->ktr_pid) {
510 flags = pi->sv_flags;
518 switch (flags & SV_ABI_MASK) {
525 case SV_ABI_CLOUDABI:
535 else if (flags & SV_ILP32)
540 printf("%s%s ", abi, arch);
546 dumpheader(struct ktr_header *kth)
548 static char unknown[64];
549 static struct timeval prevtime, prevtime_e, temp;
553 switch (kth->ktr_type) {
595 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
600 * The ktr_tid field was previously the ktr_buffer field, which held
601 * the kernel pointer value for the buffer associated with data
602 * following the record header. It now holds a threadid, but only
603 * for trace files after the change. Older trace files still contain
604 * kernel pointers. Detect this and suppress the results by printing
605 * negative tid's as 0.
608 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
609 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
610 MAXCOMLEN, kth->ktr_comm);
612 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
615 if (timestamp & TIMESTAMP_ABSOLUTE) {
616 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
617 kth->ktr_time.tv_usec);
619 if (timestamp & TIMESTAMP_ELAPSED) {
620 if (prevtime_e.tv_sec == 0)
621 prevtime_e = kth->ktr_time;
622 timevalsub(&kth->ktr_time, &prevtime_e);
623 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
624 kth->ktr_time.tv_usec);
625 timevaladd(&kth->ktr_time, &prevtime_e);
627 if (timestamp & TIMESTAMP_RELATIVE) {
628 if (prevtime.tv_sec == 0)
629 prevtime = kth->ktr_time;
630 temp = kth->ktr_time;
631 timevalsub(&kth->ktr_time, &prevtime);
632 if ((intmax_t)kth->ktr_time.tv_sec < 0) {
633 kth->ktr_time = prevtime;
635 timevalsub(&kth->ktr_time, &prevtime);
641 printf("%s%jd.%06ld ", sign, (intmax_t)kth->ktr_time.tv_sec,
642 kth->ktr_time.tv_usec);
648 #include <sys/syscall.h>
651 ioctlname(unsigned long val)
655 str = sysdecode_ioctlname(val);
664 static enum sysdecode_abi
665 syscallabi(u_int sv_flags)
669 return (SYSDECODE_ABI_FREEBSD);
670 switch (sv_flags & SV_ABI_MASK) {
672 return (SYSDECODE_ABI_FREEBSD);
673 #if defined(__amd64__) || defined(__i386__)
676 if (sv_flags & SV_ILP32)
677 return (SYSDECODE_ABI_LINUX32);
679 return (SYSDECODE_ABI_LINUX);
681 #if defined(__aarch64__) || defined(__amd64__)
682 case SV_ABI_CLOUDABI:
683 return (SYSDECODE_ABI_CLOUDABI64);
686 return (SYSDECODE_ABI_UNKNOWN);
691 syscallname(u_int code, u_int sv_flags)
695 name = sysdecode_syscallname(syscallabi(sv_flags), code);
697 printf("[%d]", code);
701 printf("[%d]", code);
706 ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags)
708 int narg = ktr->ktr_narg;
712 syscallname(ktr->ktr_code, sv_flags);
713 ip = &ktr->ktr_args[0];
718 (sv_flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
719 switch (ktr->ktr_code) {
737 atfdname(*ip, decimal);
743 switch (ktr->ktr_code) {
745 print_number(ip, narg, c);
763 print_number(ip, narg, c);
771 print_number(ip, narg, c);
773 flagsandmodename(ip[0], ip[1], decimal);
778 print_number(ip, narg, c);
779 print_number(ip, narg, c);
781 * A flags value of zero is valid for
782 * wait4() but not for wait6(), so
783 * handle zero special here.
786 print_number(ip, narg, c);
796 idtypename(*ip, decimal);
800 print_number(ip, narg, c);
801 print_number(ip, narg, c);
811 print_number(ip, narg, c);
819 print_number(ip, narg, c);
826 print_number(ip, narg, c);
827 print_number(ip, narg, c);
829 getfsstatflagsname(*ip);
834 print_number(ip, narg, c);
835 print_number(ip, narg, c);
842 print_number(ip, narg, c);
850 print_number(ip, narg, c);
851 print_number(ip, narg, c);
853 sendrecvflagsname(*ip);
859 print_number(ip, narg, c);
860 print_number(ip, narg, c);
861 print_number(ip, narg, c);
863 sendrecvflagsname(*ip);
870 print_number(ip, narg, c);
877 print_number(ip, narg, c);
896 print_number(ip, narg, c);
897 print_number(ip, narg, c);
903 #ifdef SYS_freebsd6_mmap
904 case SYS_freebsd6_mmap:
905 print_number(ip, narg, c);
906 print_number(ip, narg, c);
918 print_number(ip, narg, c);
919 print_number(ip, narg, c);
930 print_number(ip, narg, c);
931 print_number(ip, narg, c);
938 print_number(ip, narg, c);
939 print_number(ip, narg, c);
941 madvisebehavname(*ip);
945 case SYS_setpriority:
946 print_number(ip, narg, c);
947 print_number(ip, narg, c);
954 print_number(ip, narg, c);
956 fcntlcmdname(ip[0], ip[1], decimal);
964 sockdomainname(sockdomain);
968 socktypenamewithflags(*ip);
971 if (sockdomain == PF_INET ||
972 sockdomain == PF_INET6) {
974 sockipprotoname(*ip);
983 print_number(ip, narg, c);
985 sockoptlevelname(*ip, decimal);
986 if (*ip == SOL_SOCKET) {
995 #ifdef SYS_freebsd6_lseek
996 case SYS_freebsd6_lseek:
997 print_number(ip, narg, c);
998 /* Hidden 'pad' argument, not in lseek(2) */
999 print_number(ip, narg, c);
1000 print_number(ip, narg, c);
1008 print_number(ip, narg, c);
1009 /* Hidden 'pad' argument, not in lseek(2) */
1010 print_number(ip, narg, c);
1017 print_number(ip, narg, c);
1027 print_number(ip, narg, c);
1034 print_number(ip, narg, c);
1036 shutdownhowname(*ip);
1040 case SYS_socketpair:
1042 sockdomainname(*ip);
1046 socktypenamewithflags(*ip);
1060 print_number(ip, narg, c);
1082 print_number(ip, narg, c);
1083 print_number(ip, narg, c);
1090 print_number(ip, narg, c);
1091 print_number(ip, narg, c);
1098 print_number(ip, narg, c);
1105 print_number(ip, narg, c);
1106 print_number(ip, narg, c);
1113 print_number(ip, narg, c);
1120 print_number(ip, narg, c);
1123 printf(",0%o", (unsigned int)ip[1]);
1128 print_number(ip, narg, c);
1129 print_number(ip, narg, c);
1142 case SYS_lio_listio:
1144 lio_listioname(*ip);
1155 case SYS_sched_setscheduler:
1156 print_number(ip, narg, c);
1158 schedpolicyname(*ip);
1162 case SYS_sched_get_priority_max:
1163 case SYS_sched_get_priority_min:
1165 schedpolicyname(*ip);
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);
1175 print_number(ip, narg, c);
1177 sendfileflagsname(*(int *)ip);
1182 print_number(ip, narg, c);
1188 case SYS_sigprocmask:
1190 sigprocmaskhowname(*ip);
1195 case SYS___acl_get_file:
1196 case SYS___acl_set_file:
1197 case SYS___acl_get_fd:
1198 case SYS___acl_set_fd:
1199 case SYS___acl_delete_file:
1200 case SYS___acl_delete_fd:
1201 case SYS___acl_aclcheck_file:
1202 case SYS___acl_aclcheck_fd:
1203 case SYS___acl_get_link:
1204 case SYS___acl_set_link:
1205 case SYS___acl_delete_link:
1206 case SYS___acl_aclcheck_link:
1207 print_number(ip, narg, c);
1220 case SYS_extattrctl:
1221 print_number(ip, narg, c);
1223 extattrctlname(*ip);
1228 print_number(ip, narg, c);
1229 print_number(ip, narg, c);
1231 mountflagsname(*ip);
1235 case SYS_thr_create:
1236 print_number(ip, narg, c);
1237 print_number(ip, narg, c);
1239 thrcreateflagsname(*ip);
1244 print_number(ip, narg, c);
1250 case SYS_kldunloadf:
1251 print_number(ip, narg, c);
1253 kldunloadfflagsname(*ip);
1260 print_number(ip, narg, c);
1262 atfdname(*ip, decimal);
1266 case SYS_cap_fcntls_limit:
1267 print_number(ip, narg, c);
1274 case SYS_posix_fadvise:
1275 print_number(ip, narg, c);
1276 print_number(ip, narg, c);
1277 print_number(ip, narg, c);
1279 fadvisebehavname((int)*ip);
1285 idtypename(*ip, decimal);
1289 print_number(ip, narg, c);
1291 procctlcmdname(*ip);
1296 print_number(ip, narg, c);
1300 case UMTX_OP_CV_WAIT:
1304 umtxcvwaitflags(*ip);
1306 case UMTX_OP_RW_RDLOCK:
1310 umtxrwlockflags(*ip);
1318 print_number(ip, narg, c);
1326 ktrsysret(struct ktr_sysret *ktr, u_int sv_flags)
1328 register_t ret = ktr->ktr_retval;
1329 int error = ktr->ktr_error;
1331 syscallname(ktr->ktr_code, sv_flags);
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", sysdecode_freebsd_to_abi_errno(
1351 syscallabi(sv_flags), error));
1353 printf(" %s", strerror(ktr->ktr_error));
1359 ktrnamei(char *cp, int len)
1361 printf("\"%.*s\"\n", len, cp);
1365 hexdump(char *p, int len, int screenwidth)
1373 i = 13; /* base offset */
1374 i += (width / 2) + 1; /* spaces every second byte */
1375 i += (width * 2); /* width of bytes */
1377 i += width; /* each byte */
1379 } while (i < screenwidth);
1382 for (n = 0; n < len; n += width) {
1383 for (i = n; i < n + width; i++) {
1384 if ((i % width) == 0) { /* beginning of line */
1385 printf(" 0x%04x", i);
1391 printf("%02x", p[i] & 0xff);
1396 for (i = n; i < n + width; i++) {
1399 if (p[i] >= ' ' && p[i] <= '~')
1406 if ((i % width) != 0)
1411 visdump(char *dp, int datalen, int screenwidth)
1420 for (;datalen > 0; datalen--, dp++) {
1421 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1424 * Keep track of printables and
1425 * space chars (like fold(1)).
1437 width = 8 - (col&07);
1442 if (col + width > (screenwidth-2)) {
1457 ktrgenio(struct ktr_genio *ktr, int len)
1459 int datalen = len - sizeof (struct ktr_genio);
1460 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1461 static int screenwidth = 0;
1464 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1465 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1466 datalen == 1 ? "" : "s");
1469 if (screenwidth == 0) {
1472 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1474 screenwidth = ws.ws_col;
1478 if (maxdata && datalen > maxdata)
1481 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1482 if (dp[i] >= 32 && dp[i] < 127)
1484 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1489 hexdump(dp, datalen, screenwidth);
1491 visdump(dp, datalen, screenwidth);
1494 const char *signames[] = {
1495 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
1496 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
1497 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
1498 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
1499 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
1500 "USR2", NULL, /* 31 - 32 */
1504 ktrpsig(struct ktr_psig *psig)
1506 if (psig->signo > 0 && psig->signo < NSIG)
1507 printf("SIG%s ", signames[psig->signo]);
1509 printf("SIG %d ", psig->signo);
1510 if (psig->action == SIG_DFL) {
1511 printf("SIG_DFL code=");
1512 sigcodename(psig->signo, psig->code);
1515 printf("caught handler=0x%lx mask=0x%x code=",
1516 (u_long)psig->action, psig->mask.__bits[0]);
1517 sigcodename(psig->signo, psig->code);
1523 ktrcsw_old(struct ktr_csw_old *cs)
1525 printf("%s %s\n", cs->out ? "stop" : "resume",
1526 cs->user ? "user" : "kernel");
1530 ktrcsw(struct ktr_csw *cs)
1532 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1533 cs->user ? "user" : "kernel", cs->wmesg);
1537 ktruser(int len, void *p)
1541 if (sysdecode_utrace(stdout, p, len)) {
1550 printf(" %d", *cp++);
1552 printf(" %02x", *cp++);
1557 ktrcaprights(cap_rights_t *rightsp)
1560 printf("cap_rights_t ");
1566 ktrsockaddr(struct sockaddr *sa)
1569 TODO: Support additional address families
1570 #include <netnatm/natm.h>
1571 struct sockaddr_natm *natm;
1572 #include <netsmb/netbios.h>
1573 struct sockaddr_nb *nb;
1578 * note: ktrstruct() has already verified that sa points to a
1579 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1580 * sa->sa_len bytes long.
1582 printf("struct sockaddr { ");
1583 sockfamilyname(sa->sa_family);
1586 #define check_sockaddr_len(n) \
1587 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1588 printf("invalid"); \
1592 switch(sa->sa_family) {
1594 struct sockaddr_in sa_in;
1596 memset(&sa_in, 0, sizeof(sa_in));
1597 memcpy(&sa_in, sa, sa->sa_len);
1598 check_sockaddr_len(in);
1599 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1600 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1604 struct sockaddr_in6 sa_in6;
1606 memset(&sa_in6, 0, sizeof(sa_in6));
1607 memcpy(&sa_in6, sa, sa->sa_len);
1608 check_sockaddr_len(in6);
1609 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1610 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1611 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1615 struct sockaddr_un sa_un;
1617 memset(&sa_un, 0, sizeof(sa_un));
1618 memcpy(&sa_un, sa, sa->sa_len);
1619 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1623 printf("unknown address family");
1629 ktrstat(struct stat *statp)
1631 char mode[12], timestr[PATH_MAX + 4];
1637 * note: ktrstruct() has already verified that statp points to a
1638 * buffer exactly sizeof(struct stat) bytes long.
1640 printf("struct stat {");
1641 printf("dev=%ju, ino=%ju, ",
1642 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1644 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1646 strmode(statp->st_mode, mode);
1647 printf("mode=%s, ", mode);
1649 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1653 #ifdef HAVE_LIBCASPER
1655 pwd = cap_getpwuid(cappwd, statp->st_uid);
1658 pwd = getpwuid(statp->st_uid);
1661 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1663 printf("uid=\"%s\", ", pwd->pw_name);
1667 #ifdef HAVE_LIBCASPER
1669 grp = cap_getgrgid(capgrp, statp->st_gid);
1672 grp = getgrgid(statp->st_gid);
1675 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1677 printf("gid=\"%s\", ", grp->gr_name);
1678 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1681 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1683 tm = localtime(&statp->st_atim.tv_sec);
1684 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1685 printf("\"%s\"", timestr);
1687 if (statp->st_atim.tv_nsec != 0)
1688 printf(".%09ld, ", statp->st_atim.tv_nsec);
1693 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1695 tm = localtime(&statp->st_mtim.tv_sec);
1696 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1697 printf("\"%s\"", timestr);
1699 if (statp->st_mtim.tv_nsec != 0)
1700 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1705 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1707 tm = localtime(&statp->st_ctim.tv_sec);
1708 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1709 printf("\"%s\"", timestr);
1711 if (statp->st_ctim.tv_nsec != 0)
1712 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1715 printf("birthtime=");
1717 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1719 tm = localtime(&statp->st_birthtim.tv_sec);
1720 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1721 printf("\"%s\"", timestr);
1723 if (statp->st_birthtim.tv_nsec != 0)
1724 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1727 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1728 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1729 (intmax_t)statp->st_blocks, statp->st_flags);
1734 ktrstruct(char *buf, size_t buflen)
1737 size_t namelen, datalen;
1739 cap_rights_t rights;
1741 struct sockaddr_storage ss;
1743 for (name = buf, namelen = 0;
1744 namelen < buflen && name[namelen] != '\0';
1747 if (namelen == buflen)
1749 if (name[namelen] != '\0')
1751 data = buf + namelen + 1;
1752 datalen = buflen - namelen - 1;
1756 for (i = 0; i < (int)namelen; ++i)
1757 if (!isalpha(name[i]))
1759 if (strcmp(name, "caprights") == 0) {
1760 if (datalen != sizeof(cap_rights_t))
1762 memcpy(&rights, data, datalen);
1763 ktrcaprights(&rights);
1764 } else if (strcmp(name, "stat") == 0) {
1765 if (datalen != sizeof(struct stat))
1767 memcpy(&sb, data, datalen);
1769 } else if (strcmp(name, "sockaddr") == 0) {
1770 if (datalen > sizeof(ss))
1772 memcpy(&ss, data, datalen);
1773 if (datalen != ss.ss_len)
1775 ktrsockaddr((struct sockaddr *)&ss);
1777 printf("unknown structure\n");
1781 printf("invalid record\n");
1785 ktrcapfail(struct ktr_cap_fail *ktr)
1787 switch (ktr->cap_type) {
1788 case CAPFAIL_NOTCAPABLE:
1789 /* operation on fd with insufficient capabilities */
1790 printf("operation requires ");
1791 capname(&ktr->cap_needed);
1792 printf(", descriptor holds ");
1793 capname(&ktr->cap_held);
1795 case CAPFAIL_INCREASE:
1796 /* requested more capabilities than fd already has */
1797 printf("attempt to increase capabilities from ");
1798 capname(&ktr->cap_held);
1800 capname(&ktr->cap_needed);
1802 case CAPFAIL_SYSCALL:
1803 /* called restricted syscall */
1804 printf("disallowed system call");
1806 case CAPFAIL_LOOKUP:
1807 /* used ".." in strict-relative mode */
1808 printf("restricted VFS lookup");
1811 printf("unknown capability failure: ");
1812 capname(&ktr->cap_needed);
1814 capname(&ktr->cap_held);
1821 ktrfault(struct ktr_fault *ktr)
1824 printf("0x%jx ", (uintmax_t)ktr->vaddr);
1825 vmprotname(ktr->type);
1830 ktrfaultend(struct ktr_faultend *ktr)
1833 vmresultname(ktr->result);
1840 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
1841 "[-m maxdata] [-p pid] [-t trstr]\n");