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>
65 #include <sys/types.h>
66 #include <netipx/ipx.h>
69 #include <netatalk/at.h>
71 #include <arpa/inet.h>
72 #include <netinet/in.h>
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(void *);
107 void ktruser_rtld(int, void *);
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);
119 void ioctlname(unsigned long, int);
121 extern const char *signames[], *syscallnames[];
122 extern int nsyscalls;
124 static int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
125 resolv = 0, abiflag = 0, syscallno = 0;
126 static const char *tracefile = DEF_TRACEFILE;
127 static struct ktr_header ktr_header;
129 #define TIME_FORMAT "%b %e %T %Y"
130 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
132 #define print_number64(first,i,n,c) do { \
135 if (quad_align && (((ptrdiff_t)((i) - (first))) & 1) == 1) { \
139 if (quad_slots == 2) \
140 __v = (uint64_t)(uint32_t)(i)[0] | \
141 ((uint64_t)(uint32_t)(i)[1]) << 32; \
143 __v = (uint64_t)*(i); \
145 printf("%c%jd", (c), (intmax_t)__v); \
147 printf("%c%#jx", (c), (uintmax_t)__v); \
153 #define print_number(i,n,c) do { \
155 printf("%c%jd", c, (intmax_t)*i); \
157 printf("%c%#jx", c, (uintmax_t)(u_register_t)*i); \
163 #if defined(__amd64__) || defined(__i386__)
165 void linux_ktrsyscall(struct ktr_syscall *, u_int);
166 void linux_ktrsysret(struct ktr_sysret *, u_int);
167 extern const char *linux_syscallnames[];
169 #include <linux_syscalls.c>
173 * Linux syscalls return negative errno's, we do positive and map them
175 static int bsd_to_linux_errno[ELAST + 1] = {
176 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9,
177 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
178 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
179 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
180 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
181 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
182 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
183 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9,
184 -6, -6, -43, -42, -75,-125, -84, -95, -16, -74,
189 #if defined(__amd64__)
190 extern const char *linux32_syscallnames[];
192 #include <linux32_syscalls.c>
197 TAILQ_ENTRY(proc_info) info;
202 static TAILQ_HEAD(trace_procs, proc_info) trace_procs;
209 * Cache NLS data before entering capability mode.
210 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
212 (void)catopen("libc", NL_CAT_LOCALE);
221 * Allow localtime(3) to cache /etc/localtime content before entering
223 * XXXPJD: There should be localtime_init() in libc.
226 (void)localtime(<ime);
230 main(int argc, char *argv[])
232 int ch, ktrlen, size;
234 int trpoints = ALL_POINTS;
239 setlocale(LC_CTYPE, "");
241 while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1)
256 maxdata = atoi(optarg);
274 timestamp = 3; /* elapsed timestamp */
280 timestamp = 2; /* relative timestamp */
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 (!freopen(tracefile, "r", stdin))
301 err(1, "%s", tracefile);
307 if (cap_enter() < 0 && errno != ENOSYS)
308 err(1, "unable to enter capability mode");
310 limitfd(STDIN_FILENO);
311 limitfd(STDOUT_FILENO);
312 limitfd(STDERR_FILENO);
314 TAILQ_INIT(&trace_procs);
316 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
317 if (ktr_header.ktr_type & KTR_DROP) {
318 ktr_header.ktr_type &= ~KTR_DROP;
319 if (!drop_logged && threads) {
321 "%6jd %6jd %-8.*s Events dropped.\n",
322 (intmax_t)ktr_header.ktr_pid,
323 ktr_header.ktr_tid > 0 ?
324 (intmax_t)ktr_header.ktr_tid : 0,
325 MAXCOMLEN, ktr_header.ktr_comm);
327 } else if (!drop_logged) {
328 printf("%6jd %-8.*s Events dropped.\n",
329 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
330 ktr_header.ktr_comm);
334 if (trpoints & (1<<ktr_header.ktr_type))
335 if (pid == 0 || ktr_header.ktr_pid == pid ||
336 ktr_header.ktr_tid == pid)
337 dumpheader(&ktr_header);
338 if ((ktrlen = ktr_header.ktr_len) < 0)
339 errx(1, "bogus length 0x%x", ktrlen);
341 m = realloc(m, ktrlen+1);
343 errx(1, "%s", strerror(ENOMEM));
346 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
347 errx(1, "data too short");
348 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
350 sv_flags = abidump(&ktr_header);
351 if (pid && ktr_header.ktr_pid != pid &&
352 ktr_header.ktr_tid != pid)
354 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
357 switch (ktr_header.ktr_type) {
359 #if defined(__amd64__) || defined(__i386__)
360 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
361 linux_ktrsyscall((struct ktr_syscall *)m,
365 ktrsyscall((struct ktr_syscall *)m, sv_flags);
368 #if defined(__amd64__) || defined(__i386__)
369 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
370 linux_ktrsysret((struct ktr_sysret *)m,
374 ktrsysret((struct ktr_sysret *)m, sv_flags);
381 ktrgenio((struct ktr_genio *)m, ktrlen);
384 ktrpsig((struct ktr_psig *)m);
387 if (ktrlen == sizeof(struct ktr_csw_old))
388 ktrcsw_old((struct ktr_csw_old *)m);
390 ktrcsw((struct ktr_csw *)m);
396 ktrstruct(m, ktrlen);
399 ktrcapfail((struct ktr_cap_fail *)m);
402 ktrfault((struct ktr_fault *)m);
405 ktrfaultend((struct ktr_faultend *)m);
423 cap_rights_init(&rights, CAP_FSTAT);
428 cap_rights_set(&rights, CAP_READ);
431 cap_rights_set(&rights, CAP_IOCTL, CAP_WRITE);
432 cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
435 cap_rights_set(&rights, CAP_WRITE);
437 cap_rights_set(&rights, CAP_IOCTL);
445 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
446 err(1, "unable to limit rights for descriptor %d", fd);
447 if (cmd != 0 && cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
448 err(1, "unable to limit ioctls for descriptor %d", fd);
452 fread_tail(void *buf, int size, int num)
456 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
464 fetchprocinfo(struct ktr_header *kth, u_int *flags)
466 struct proc_info *pi;
468 switch (kth->ktr_type) {
470 TAILQ_FOREACH(pi, &trace_procs, info) {
471 if (pi->pid == kth->ktr_pid) {
472 TAILQ_REMOVE(&trace_procs, pi, info);
476 pi = malloc(sizeof(struct proc_info));
478 errx(1, "%s", strerror(ENOMEM));
479 pi->sv_flags = *flags;
480 pi->pid = kth->ktr_pid;
481 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
485 TAILQ_FOREACH(pi, &trace_procs, info) {
486 if (pi->pid == kth->ktr_pid) {
487 TAILQ_REMOVE(&trace_procs, pi, info);
499 abidump(struct ktr_header *kth)
501 struct proc_info *pi;
506 TAILQ_FOREACH(pi, &trace_procs, info) {
507 if (pi->pid == kth->ktr_pid) {
508 flags = pi->sv_flags;
516 switch (flags & SV_ABI_MASK) {
536 printf("%s%s ", abi, arch);
542 dumpheader(struct ktr_header *kth)
544 static char unknown[64];
545 static struct timeval prevtime, temp;
548 switch (kth->ktr_type) {
590 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
595 * The ktr_tid field was previously the ktr_buffer field, which held
596 * the kernel pointer value for the buffer associated with data
597 * following the record header. It now holds a threadid, but only
598 * for trace files after the change. Older trace files still contain
599 * kernel pointers. Detect this and suppress the results by printing
600 * negative tid's as 0.
603 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
604 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
605 MAXCOMLEN, kth->ktr_comm);
607 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
610 if (timestamp == 3) {
611 if (prevtime.tv_sec == 0)
612 prevtime = kth->ktr_time;
613 timevalsub(&kth->ktr_time, &prevtime);
615 if (timestamp == 2) {
616 temp = kth->ktr_time;
617 timevalsub(&kth->ktr_time, &prevtime);
620 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
621 kth->ktr_time.tv_usec);
626 #include <sys/syscall.h>
628 #include <sys/kern/syscalls.c>
630 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
633 ktrsyscall(struct ktr_syscall *ktr, u_int flags)
635 int narg = ktr->ktr_narg;
636 register_t *ip, *first;
638 int quad_align, quad_slots;
640 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
641 (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0))
642 printf("[%d]", ktr->ktr_code);
644 printf("%s", syscallnames[ktr->ktr_code]);
646 printf("[%d]", ktr->ktr_code);
648 ip = first = &ktr->ktr_args[0];
652 (flags == 0 || (flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
654 if (flags & SV_ILP32) {
661 switch (ktr->ktr_code) {
679 atfdname(*ip, decimal);
685 switch (ktr->ktr_code) {
687 print_number(ip, narg, c);
689 ioctlname(*ip, decimal);
705 print_number(ip, narg, c);
713 print_number(ip, narg, c);
715 flagsandmodename(ip[0], ip[1], decimal);
720 print_number(ip, narg, c);
721 print_number(ip, narg, c);
723 * A flags value of zero is valid for
724 * wait4() but not for wait6(), so
725 * handle zero special here.
728 print_number(ip, narg, c);
738 idtypename(*ip, decimal);
742 print_number64(first, ip, narg, c);
743 print_number(ip, narg, c);
752 print_number(ip, narg, c);
760 print_number(ip, narg, c);
767 print_number(ip, narg, c);
768 print_number(ip, narg, c);
770 getfsstatflagsname(*ip);
775 print_number(ip, narg, c);
776 print_number(ip, narg, c);
783 print_number(ip, narg, c);
791 print_number(ip, narg, c);
792 print_number(ip, narg, c);
794 sendrecvflagsname(*ip);
800 print_number(ip, narg, c);
801 print_number(ip, narg, c);
802 print_number(ip, narg, c);
804 sendrecvflagsname(*ip);
811 print_number(ip, narg, c);
818 print_number(ip, narg, c);
837 print_number(ip, narg, c);
838 print_number(ip, narg, c);
844 #ifdef SYS_freebsd6_mmap
845 case SYS_freebsd6_mmap:
846 print_number(ip, narg, c);
847 print_number(ip, narg, c);
859 print_number(ip, narg, c);
860 print_number(ip, narg, c);
871 print_number(ip, narg, c);
872 print_number(ip, narg, c);
879 print_number(ip, narg, c);
880 print_number(ip, narg, c);
882 madvisebehavname(*ip);
886 case SYS_setpriority:
887 print_number(ip, narg, c);
888 print_number(ip, narg, c);
895 print_number(ip, narg, c);
897 fcntlcmdname(ip[0], ip[1], decimal);
905 sockdomainname(sockdomain);
909 socktypenamewithflags(*ip);
912 if (sockdomain == PF_INET ||
913 sockdomain == PF_INET6) {
915 sockipprotoname(*ip);
924 print_number(ip, narg, c);
926 sockoptlevelname(*ip, decimal);
927 if (*ip == SOL_SOCKET) {
936 #ifdef SYS_freebsd6_lseek
937 case SYS_freebsd6_lseek:
938 print_number(ip, narg, c);
939 /* Hidden 'pad' argument, not in lseek(2) */
940 print_number(ip, narg, c);
941 print_number64(first, ip, narg, c);
949 print_number(ip, narg, c);
950 print_number64(first, ip, narg, c);
957 print_number(ip, narg, c);
967 print_number(ip, narg, c);
974 print_number(ip, narg, c);
976 shutdownhowname(*ip);
986 socktypenamewithflags(*ip);
1000 print_number(ip, narg, c);
1022 print_number(ip, narg, c);
1023 print_number(ip, narg, c);
1030 print_number(ip, narg, c);
1031 print_number(ip, narg, c);
1038 print_number(ip, narg, c);
1045 print_number(ip, narg, c);
1046 print_number(ip, narg, c);
1053 print_number(ip, narg, c);
1060 print_number(ip, narg, c);
1063 printf(",0%o", (unsigned int)ip[1]);
1068 print_number(ip, narg, c);
1069 print_number(ip, narg, c);
1082 case SYS_lio_listio:
1084 lio_listioname(*ip);
1095 case SYS_sched_setscheduler:
1096 print_number(ip, narg, c);
1098 schedpolicyname(*ip);
1102 case SYS_sched_get_priority_max:
1103 case SYS_sched_get_priority_min:
1105 schedpolicyname(*ip);
1110 print_number(ip, narg, c);
1111 print_number(ip, narg, c);
1112 print_number(ip, narg, c);
1113 print_number(ip, narg, c);
1114 print_number(ip, narg, c);
1115 print_number(ip, narg, c);
1117 sendfileflagsname(*(int *)ip);
1122 print_number(ip, narg, c);
1128 case SYS_sigprocmask:
1130 sigprocmaskhowname(*ip);
1135 case SYS___acl_get_file:
1136 case SYS___acl_set_file:
1137 case SYS___acl_get_fd:
1138 case SYS___acl_set_fd:
1139 case SYS___acl_delete_file:
1140 case SYS___acl_delete_fd:
1141 case SYS___acl_aclcheck_file:
1142 case SYS___acl_aclcheck_fd:
1143 case SYS___acl_get_link:
1144 case SYS___acl_set_link:
1145 case SYS___acl_delete_link:
1146 case SYS___acl_aclcheck_link:
1147 print_number(ip, narg, c);
1160 case SYS_extattrctl:
1161 print_number(ip, narg, c);
1163 extattrctlname(*ip);
1168 print_number(ip, narg, c);
1169 print_number(ip, narg, c);
1171 mountflagsname(*ip);
1175 case SYS_thr_create:
1176 print_number(ip, narg, c);
1177 print_number(ip, narg, c);
1179 thrcreateflagsname(*ip);
1184 print_number(ip, narg, c);
1190 case SYS_kldunloadf:
1191 print_number(ip, narg, c);
1193 kldunloadfflagsname(*ip);
1200 print_number(ip, narg, c);
1202 atfdname(*ip, decimal);
1206 case SYS_cap_fcntls_limit:
1207 print_number(ip, narg, c);
1214 case SYS_posix_fadvise:
1215 print_number(ip, narg, c);
1216 print_number(ip, narg, c);
1217 print_number(ip, narg, c);
1219 fadvisebehavname((int)*ip);
1225 idtypename(*ip, decimal);
1229 print_number64(first, ip, narg, c);
1231 procctlcmdname(*ip);
1236 print_number(ip, narg, c);
1240 case UMTX_OP_CV_WAIT:
1244 umtxcvwaitflags(*ip);
1246 case UMTX_OP_RW_RDLOCK:
1250 umtxrwlockflags(*ip);
1258 print_number(ip, narg, c);
1259 print_number64(first, ip, narg, c);
1264 print_number(ip, narg, c);
1272 ktrsysret(struct ktr_sysret *ktr, u_int flags)
1274 register_t ret = ktr->ktr_retval;
1275 int error = ktr->ktr_error;
1276 int code = ktr->ktr_code;
1278 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
1279 (code >= nsyscalls || code < 0))
1280 printf("[%d] ", code);
1282 printf("%s", syscallnames[code]);
1284 printf("[%d]", code);
1290 printf("%ld", (long)ret);
1291 if (ret < 0 || ret > 9)
1292 printf("/%#lx", (unsigned long)ret);
1295 printf("%ld", (long)ret);
1297 printf("%#lx", (unsigned long)ret);
1299 } else if (error == ERESTART)
1301 else if (error == EJUSTRETURN)
1302 printf("JUSTRETURN");
1304 printf("-1 errno %d", ktr->ktr_error);
1306 printf(" %s", strerror(ktr->ktr_error));
1312 ktrnamei(char *cp, int len)
1314 printf("\"%.*s\"\n", len, cp);
1318 hexdump(char *p, int len, int screenwidth)
1326 i = 13; /* base offset */
1327 i += (width / 2) + 1; /* spaces every second byte */
1328 i += (width * 2); /* width of bytes */
1330 i += width; /* each byte */
1332 } while (i < screenwidth);
1335 for (n = 0; n < len; n += width) {
1336 for (i = n; i < n + width; i++) {
1337 if ((i % width) == 0) { /* beginning of line */
1338 printf(" 0x%04x", i);
1344 printf("%02x", p[i] & 0xff);
1349 for (i = n; i < n + width; i++) {
1352 if (p[i] >= ' ' && p[i] <= '~')
1359 if ((i % width) != 0)
1364 visdump(char *dp, int datalen, int screenwidth)
1373 for (;datalen > 0; datalen--, dp++) {
1374 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1377 * Keep track of printables and
1378 * space chars (like fold(1)).
1390 width = 8 - (col&07);
1395 if (col + width > (screenwidth-2)) {
1410 ktrgenio(struct ktr_genio *ktr, int len)
1412 int datalen = len - sizeof (struct ktr_genio);
1413 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1414 static int screenwidth = 0;
1417 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1418 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1419 datalen == 1 ? "" : "s");
1422 if (screenwidth == 0) {
1425 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1427 screenwidth = ws.ws_col;
1431 if (maxdata && datalen > maxdata)
1434 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1435 if (dp[i] >= 32 && dp[i] < 127)
1437 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1442 hexdump(dp, datalen, screenwidth);
1444 visdump(dp, datalen, screenwidth);
1447 const char *signames[] = {
1448 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
1449 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
1450 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
1451 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
1452 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
1453 "USR2", NULL, /* 31 - 32 */
1457 ktrpsig(struct ktr_psig *psig)
1459 if (psig->signo > 0 && psig->signo < NSIG)
1460 printf("SIG%s ", signames[psig->signo]);
1462 printf("SIG %d ", psig->signo);
1463 if (psig->action == SIG_DFL) {
1464 printf("SIG_DFL code=");
1465 sigcodename(psig->signo, psig->code);
1468 printf("caught handler=0x%lx mask=0x%x code=",
1469 (u_long)psig->action, psig->mask.__bits[0]);
1470 sigcodename(psig->signo, psig->code);
1476 ktrcsw_old(struct ktr_csw_old *cs)
1478 printf("%s %s\n", cs->out ? "stop" : "resume",
1479 cs->user ? "user" : "kernel");
1483 ktrcsw(struct ktr_csw *cs)
1485 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1486 cs->user ? "user" : "kernel", cs->wmesg);
1489 #define UTRACE_DLOPEN_START 1
1490 #define UTRACE_DLOPEN_STOP 2
1491 #define UTRACE_DLCLOSE_START 3
1492 #define UTRACE_DLCLOSE_STOP 4
1493 #define UTRACE_LOAD_OBJECT 5
1494 #define UTRACE_UNLOAD_OBJECT 6
1495 #define UTRACE_ADD_RUNDEP 7
1496 #define UTRACE_PRELOAD_FINISHED 8
1497 #define UTRACE_INIT_CALL 9
1498 #define UTRACE_FINI_CALL 10
1499 #define UTRACE_DLSYM_START 11
1500 #define UTRACE_DLSYM_STOP 12
1502 struct utrace_rtld {
1503 char sig[4]; /* 'RTLD' */
1509 char name[MAXPATHLEN];
1513 ktruser_rtld(int len, void *p)
1515 struct utrace_rtld *ut = p;
1520 switch (ut->event) {
1521 case UTRACE_DLOPEN_START:
1523 printf("dlopen(%s, ", ut->name);
1524 switch (mode & RTLD_MODEMASK) {
1529 printf("RTLD_LAZY");
1532 printf("%#x", mode & RTLD_MODEMASK);
1534 if (mode & RTLD_GLOBAL)
1535 printf(" | RTLD_GLOBAL");
1536 if (mode & RTLD_TRACE)
1537 printf(" | RTLD_TRACE");
1538 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
1539 printf(" | %#x", mode &
1540 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
1543 case UTRACE_DLOPEN_STOP:
1544 printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
1547 case UTRACE_DLCLOSE_START:
1548 printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
1551 case UTRACE_DLCLOSE_STOP:
1552 printf("dlclose(%p) finished\n", ut->handle);
1554 case UTRACE_LOAD_OBJECT:
1555 printf("RTLD: loaded %p @ %p - %p (%s)\n", ut->handle,
1556 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1559 case UTRACE_UNLOAD_OBJECT:
1560 printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
1561 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1564 case UTRACE_ADD_RUNDEP:
1565 parent = ut->mapbase;
1566 printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
1567 ut->handle, ut->name, ut->refcnt);
1569 case UTRACE_PRELOAD_FINISHED:
1570 printf("RTLD: LD_PRELOAD finished\n");
1572 case UTRACE_INIT_CALL:
1573 printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
1576 case UTRACE_FINI_CALL:
1577 printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
1580 case UTRACE_DLSYM_START:
1581 printf("RTLD: dlsym(%p, %s)\n", ut->handle, ut->name);
1583 case UTRACE_DLSYM_STOP:
1584 printf("RTLD: %p = dlsym(%p, %s)\n", ut->mapbase, ut->handle,
1591 printf("RTLD: %d ", len);
1594 printf(" %d", *cp++);
1596 printf(" %02x", *cp++);
1601 struct utrace_malloc {
1608 ktruser_malloc(void *p)
1610 struct utrace_malloc *ut = p;
1612 if (ut->p == (void *)(intptr_t)(-1))
1613 printf("malloc_init()\n");
1614 else if (ut->s == 0)
1615 printf("free(%p)\n", ut->p);
1616 else if (ut->p == NULL)
1617 printf("%p = malloc(%zu)\n", ut->r, ut->s);
1619 printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
1623 ktruser(int len, void *p)
1627 if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
1628 ktruser_rtld(len, p);
1632 if (len == sizeof(struct utrace_malloc)) {
1641 printf(" %d", *cp++);
1643 printf(" %02x", *cp++);
1648 ktrcaprights(cap_rights_t *rightsp)
1651 printf("cap_rights_t ");
1657 ktrtimeval(struct timeval *tv)
1660 printf("{%ld, %ld}", (long)tv->tv_sec, tv->tv_usec);
1664 ktritimerval(struct itimerval *it)
1667 printf("itimerval { .interval = ");
1668 ktrtimeval(&it->it_interval);
1669 printf(", .value = ");
1670 ktrtimeval(&it->it_value);
1675 ktrsockaddr(struct sockaddr *sa)
1678 TODO: Support additional address families
1679 #include <netnatm/natm.h>
1680 struct sockaddr_natm *natm;
1681 #include <netsmb/netbios.h>
1682 struct sockaddr_nb *nb;
1687 * note: ktrstruct() has already verified that sa points to a
1688 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1689 * sa->sa_len bytes long.
1691 printf("struct sockaddr { ");
1692 sockfamilyname(sa->sa_family);
1695 #define check_sockaddr_len(n) \
1696 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1697 printf("invalid"); \
1701 switch(sa->sa_family) {
1703 struct sockaddr_in sa_in;
1705 memset(&sa_in, 0, sizeof(sa_in));
1706 memcpy(&sa_in, sa, sa->sa_len);
1707 check_sockaddr_len(in);
1708 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1709 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1713 case AF_APPLETALK: {
1714 struct sockaddr_at sa_at;
1715 struct netrange *nr;
1717 memset(&sa_at, 0, sizeof(sa_at));
1718 memcpy(&sa_at, sa, sa->sa_len);
1719 check_sockaddr_len(at);
1720 nr = &sa_at.sat_range.r_netrange;
1721 printf("%d.%d, %d-%d, %d", ntohs(sa_at.sat_addr.s_net),
1722 sa_at.sat_addr.s_node, ntohs(nr->nr_firstnet),
1723 ntohs(nr->nr_lastnet), nr->nr_phase);
1728 struct sockaddr_in6 sa_in6;
1730 memset(&sa_in6, 0, sizeof(sa_in6));
1731 memcpy(&sa_in6, sa, sa->sa_len);
1732 check_sockaddr_len(in6);
1733 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1734 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1735 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1740 struct sockaddr_ipx sa_ipx;
1742 memset(&sa_ipx, 0, sizeof(sa_ipx));
1743 memcpy(&sa_ipx, sa, sa->sa_len);
1744 check_sockaddr_len(ipx);
1745 /* XXX wish we had ipx_ntop */
1746 printf("%s", ipx_ntoa(sa_ipx.sipx_addr));
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);
1787 if (resolv == 0 || (pwd = getpwuid(statp->st_uid)) == NULL)
1788 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1790 printf("uid=\"%s\", ", pwd->pw_name);
1791 if (resolv == 0 || (grp = getgrgid(statp->st_gid)) == NULL)
1792 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1794 printf("gid=\"%s\", ", grp->gr_name);
1795 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1798 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1800 tm = localtime(&statp->st_atim.tv_sec);
1801 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1802 printf("\"%s\"", timestr);
1804 if (statp->st_atim.tv_nsec != 0)
1805 printf(".%09ld, ", statp->st_atim.tv_nsec);
1810 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1812 tm = localtime(&statp->st_mtim.tv_sec);
1813 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1814 printf("\"%s\"", timestr);
1816 if (statp->st_mtim.tv_nsec != 0)
1817 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1822 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1824 tm = localtime(&statp->st_ctim.tv_sec);
1825 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1826 printf("\"%s\"", timestr);
1828 if (statp->st_ctim.tv_nsec != 0)
1829 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1832 printf("birthtime=");
1834 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1836 tm = localtime(&statp->st_birthtim.tv_sec);
1837 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1838 printf("\"%s\"", timestr);
1840 if (statp->st_birthtim.tv_nsec != 0)
1841 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1844 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1845 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1846 (intmax_t)statp->st_blocks, statp->st_flags);
1851 ktrstruct(char *buf, size_t buflen)
1854 size_t namelen, datalen;
1856 cap_rights_t rights;
1857 struct itimerval it;
1859 struct sockaddr_storage ss;
1861 for (name = buf, namelen = 0;
1862 namelen < buflen && name[namelen] != '\0';
1865 if (namelen == buflen)
1867 if (name[namelen] != '\0')
1869 data = buf + namelen + 1;
1870 datalen = buflen - namelen - 1;
1874 for (i = 0; i < (int)namelen; ++i)
1875 if (!isalpha(name[i]))
1877 if (strcmp(name, "caprights") == 0) {
1878 if (datalen != sizeof(cap_rights_t))
1880 memcpy(&rights, data, datalen);
1881 ktrcaprights(&rights);
1882 } else if (strcmp(name, "itimerval") == 0) {
1883 if (datalen != sizeof(struct itimerval))
1885 memcpy(&it, data, datalen);
1887 } else if (strcmp(name, "stat") == 0) {
1888 if (datalen != sizeof(struct stat))
1890 memcpy(&sb, data, datalen);
1892 } else if (strcmp(name, "sockaddr") == 0) {
1893 if (datalen > sizeof(ss))
1895 memcpy(&ss, data, datalen);
1896 if (datalen != ss.ss_len)
1898 ktrsockaddr((struct sockaddr *)&ss);
1900 printf("unknown structure\n");
1904 printf("invalid record\n");
1908 ktrcapfail(struct ktr_cap_fail *ktr)
1910 switch (ktr->cap_type) {
1911 case CAPFAIL_NOTCAPABLE:
1912 /* operation on fd with insufficient capabilities */
1913 printf("operation requires ");
1914 capname(&ktr->cap_needed);
1915 printf(", process holds ");
1916 capname(&ktr->cap_held);
1918 case CAPFAIL_INCREASE:
1919 /* requested more capabilities than fd already has */
1920 printf("attempt to increase capabilities from ");
1921 capname(&ktr->cap_held);
1923 capname(&ktr->cap_needed);
1925 case CAPFAIL_SYSCALL:
1926 /* called restricted syscall */
1927 printf("disallowed system call");
1929 case CAPFAIL_LOOKUP:
1930 /* used ".." in strict-relative mode */
1931 printf("restricted VFS lookup");
1934 printf("unknown capability failure: ");
1935 capname(&ktr->cap_needed);
1937 capname(&ktr->cap_held);
1944 ktrfault(struct ktr_fault *ktr)
1947 printf("0x%jx ", (uintmax_t)ktr->vaddr);
1948 vmprotname(ktr->type);
1953 ktrfaultend(struct ktr_faultend *ktr)
1956 vmresultname(ktr->result);
1960 #if defined(__amd64__) || defined(__i386__)
1962 #if defined(__amd64__)
1963 #define NLINUX_SYSCALLS(v) ((v) & SV_ILP32 ? \
1964 nitems(linux32_syscallnames) : nitems(linux_syscallnames))
1965 #define LINUX_SYSCALLNAMES(v, i) ((v) & SV_ILP32 ? \
1966 linux32_syscallnames[i] : linux_syscallnames[i])
1968 #define NLINUX_SYSCALLS(v) (nitems(linux_syscallnames))
1969 #define LINUX_SYSCALLNAMES(v, i) (linux_syscallnames[i])
1973 linux_ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags)
1975 int narg = ktr->ktr_narg;
1976 unsigned code = ktr->ktr_code;
1979 if (ktr->ktr_code < 0 || code >= NLINUX_SYSCALLS(sv_flags))
1980 printf("[%d]", ktr->ktr_code);
1982 printf("%s", LINUX_SYSCALLNAMES(sv_flags, ktr->ktr_code));
1984 printf("[%d]", ktr->ktr_code);
1986 ip = &ktr->ktr_args[0];
1990 print_number(ip, narg, c);
1997 linux_ktrsysret(struct ktr_sysret *ktr, u_int sv_flags)
1999 register_t ret = ktr->ktr_retval;
2000 unsigned code = ktr->ktr_code;
2001 int error = ktr->ktr_error;
2003 if (ktr->ktr_code < 0 || code >= NLINUX_SYSCALLS(sv_flags))
2004 printf("[%d] ", ktr->ktr_code);
2006 printf("%s ", LINUX_SYSCALLNAMES(sv_flags, code));
2008 printf("[%d]", code);
2014 printf("%ld", (long)ret);
2015 if (ret < 0 || ret > 9)
2016 printf("/%#lx", (unsigned long)ret);
2019 printf("%ld", (long)ret);
2021 printf("%#lx", (unsigned long)ret);
2023 } else if (error == ERESTART)
2025 else if (error == EJUSTRETURN)
2026 printf("JUSTRETURN");
2028 if (ktr->ktr_error <= ELAST + 1)
2029 error = abs(bsd_to_linux_errno[ktr->ktr_error]);
2032 printf("-1 errno %d", error);
2034 printf(" %s", strerror(ktr->ktr_error));
2043 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
2044 "[-m maxdata] [-p pid] [-t trstr]\n");