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>
90 #include "kdump_subr.h"
92 u_int abidump(struct ktr_header *);
93 int fetchprocinfo(struct ktr_header *, u_int *);
94 int fread_tail(void *, int, int);
95 void dumpheader(struct ktr_header *);
96 void ktrsyscall(struct ktr_syscall *, u_int);
97 void ktrsysret(struct ktr_sysret *, u_int);
98 void ktrnamei(char *, int);
99 void hexdump(char *, int, int);
100 void visdump(char *, int, int);
101 void ktrgenio(struct ktr_genio *, int);
102 void ktrpsig(struct ktr_psig *);
103 void ktrcsw(struct ktr_csw *);
104 void ktrcsw_old(struct ktr_csw_old *);
105 void ktruser_malloc(void *);
106 void ktruser_rtld(int, void *);
107 void ktruser(int, void *);
108 void ktrcaprights(cap_rights_t *);
109 void ktrsockaddr(struct sockaddr *);
110 void ktrstat(struct stat *);
111 void ktrstruct(char *, size_t);
112 void ktrcapfail(struct ktr_cap_fail *);
113 void ktrfault(struct ktr_fault *);
114 void ktrfaultend(struct ktr_faultend *);
115 void limitfd(int fd);
117 void ioctlname(unsigned long, int);
119 extern const char *signames[], *syscallnames[];
120 extern int nsyscalls;
122 static int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
123 resolv = 0, abiflag = 0, syscallno = 0;
124 static const char *tracefile = DEF_TRACEFILE;
125 static struct ktr_header ktr_header;
127 #define TIME_FORMAT "%b %e %T %Y"
128 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
130 #define print_number(i,n,c) do { \
132 printf("%c%jd", c, (intmax_t)*i); \
134 printf("%c%#jx", c, (uintmax_t)(u_register_t)*i); \
140 #if defined(__amd64__) || defined(__i386__)
142 void linux_ktrsyscall(struct ktr_syscall *);
143 void linux_ktrsysret(struct ktr_sysret *);
144 extern const char *linux_syscallnames[];
146 #include <linux_syscalls.c>
147 static int nlinux_syscalls = sizeof(linux_syscallnames) / \
148 sizeof(linux_syscallnames[0]);
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 static TAILQ_HEAD(trace_procs, proc_info) trace_procs;
182 * Cache NLS data before entering capability mode.
183 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
185 (void)catopen("libc", NL_CAT_LOCALE);
194 * Allow localtime(3) to cache /etc/localtime content before entering
196 * XXXPJD: There should be localtime_init() in libc.
199 (void)localtime(<ime);
203 main(int argc, char *argv[])
205 int ch, ktrlen, size;
207 int trpoints = ALL_POINTS;
212 setlocale(LC_CTYPE, "");
214 while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1)
229 maxdata = atoi(optarg);
247 timestamp = 3; /* elapsed timestamp */
253 timestamp = 2; /* relative timestamp */
259 trpoints = getpoints(optarg);
261 errx(1, "unknown trace point in %s", optarg);
270 m = malloc(size = 1025);
272 errx(1, "%s", strerror(ENOMEM));
273 if (!freopen(tracefile, "r", stdin))
274 err(1, "%s", tracefile);
280 if (cap_enter() < 0 && errno != ENOSYS)
281 err(1, "unable to enter capability mode");
283 limitfd(STDIN_FILENO);
284 limitfd(STDOUT_FILENO);
285 limitfd(STDERR_FILENO);
287 TAILQ_INIT(&trace_procs);
289 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
290 if (ktr_header.ktr_type & KTR_DROP) {
291 ktr_header.ktr_type &= ~KTR_DROP;
292 if (!drop_logged && threads) {
294 "%6jd %6jd %-8.*s Events dropped.\n",
295 (intmax_t)ktr_header.ktr_pid,
296 ktr_header.ktr_tid > 0 ?
297 (intmax_t)ktr_header.ktr_tid : 0,
298 MAXCOMLEN, ktr_header.ktr_comm);
300 } else if (!drop_logged) {
301 printf("%6jd %-8.*s Events dropped.\n",
302 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
303 ktr_header.ktr_comm);
307 if (trpoints & (1<<ktr_header.ktr_type))
308 if (pid == 0 || ktr_header.ktr_pid == pid ||
309 ktr_header.ktr_tid == pid)
310 dumpheader(&ktr_header);
311 if ((ktrlen = ktr_header.ktr_len) < 0)
312 errx(1, "bogus length 0x%x", ktrlen);
314 m = realloc(m, ktrlen+1);
316 errx(1, "%s", strerror(ENOMEM));
319 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
320 errx(1, "data too short");
321 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
323 sv_flags = abidump(&ktr_header);
324 if (pid && ktr_header.ktr_pid != pid &&
325 ktr_header.ktr_tid != pid)
327 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
330 switch (ktr_header.ktr_type) {
332 #if defined(__amd64__) || defined(__i386__)
333 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
334 linux_ktrsyscall((struct ktr_syscall *)m);
337 ktrsyscall((struct ktr_syscall *)m, sv_flags);
340 #if defined(__amd64__) || defined(__i386__)
341 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
342 linux_ktrsysret((struct ktr_sysret *)m);
345 ktrsysret((struct ktr_sysret *)m, sv_flags);
352 ktrgenio((struct ktr_genio *)m, ktrlen);
355 ktrpsig((struct ktr_psig *)m);
358 if (ktrlen == sizeof(struct ktr_csw_old))
359 ktrcsw_old((struct ktr_csw_old *)m);
361 ktrcsw((struct ktr_csw *)m);
367 ktrstruct(m, ktrlen);
370 ktrcapfail((struct ktr_cap_fail *)m);
373 ktrfault((struct ktr_fault *)m);
376 ktrfaultend((struct ktr_faultend *)m);
394 cap_rights_init(&rights, CAP_FSTAT);
399 cap_rights_set(&rights, CAP_READ);
402 cap_rights_set(&rights, CAP_IOCTL, CAP_WRITE);
403 cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
406 cap_rights_set(&rights, CAP_WRITE);
408 cap_rights_set(&rights, CAP_IOCTL);
416 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
417 err(1, "unable to limit rights for descriptor %d", fd);
418 if (cmd != 0 && cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
419 err(1, "unable to limit ioctls for descriptor %d", fd);
423 fread_tail(void *buf, int size, int num)
427 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
435 fetchprocinfo(struct ktr_header *kth, u_int *flags)
437 struct proc_info *pi;
439 switch (kth->ktr_type) {
441 TAILQ_FOREACH(pi, &trace_procs, info) {
442 if (pi->pid == kth->ktr_pid) {
443 TAILQ_REMOVE(&trace_procs, pi, info);
447 pi = malloc(sizeof(struct proc_info));
449 errx(1, "%s", strerror(ENOMEM));
450 pi->sv_flags = *flags;
451 pi->pid = kth->ktr_pid;
452 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
456 TAILQ_FOREACH(pi, &trace_procs, info) {
457 if (pi->pid == kth->ktr_pid) {
458 TAILQ_REMOVE(&trace_procs, pi, info);
470 abidump(struct ktr_header *kth)
472 struct proc_info *pi;
477 TAILQ_FOREACH(pi, &trace_procs, info) {
478 if (pi->pid == kth->ktr_pid) {
479 flags = pi->sv_flags;
487 switch (flags & SV_ABI_MASK) {
507 printf("%s%s ", abi, arch);
513 dumpheader(struct ktr_header *kth)
515 static char unknown[64];
516 static struct timeval prevtime, temp;
519 switch (kth->ktr_type) {
561 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
566 * The ktr_tid field was previously the ktr_buffer field, which held
567 * the kernel pointer value for the buffer associated with data
568 * following the record header. It now holds a threadid, but only
569 * for trace files after the change. Older trace files still contain
570 * kernel pointers. Detect this and suppress the results by printing
571 * negative tid's as 0.
574 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
575 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
576 MAXCOMLEN, kth->ktr_comm);
578 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
581 if (timestamp == 3) {
582 if (prevtime.tv_sec == 0)
583 prevtime = kth->ktr_time;
584 timevalsub(&kth->ktr_time, &prevtime);
586 if (timestamp == 2) {
587 temp = kth->ktr_time;
588 timevalsub(&kth->ktr_time, &prevtime);
591 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
592 kth->ktr_time.tv_usec);
597 #include <sys/syscall.h>
599 #include <sys/kern/syscalls.c>
601 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
604 ktrsyscall(struct ktr_syscall *ktr, u_int flags)
606 int narg = ktr->ktr_narg;
610 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
611 (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0))
612 printf("[%d]", ktr->ktr_code);
614 printf("%s", syscallnames[ktr->ktr_code]);
616 printf("[%d]", ktr->ktr_code);
618 ip = &ktr->ktr_args[0];
622 (flags == 0 || (flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
623 switch (ktr->ktr_code) {
640 atfdname(*ip, decimal);
646 switch (ktr->ktr_code) {
648 print_number(ip, narg, c);
650 ioctlname(*ip, decimal);
666 print_number(ip, narg, c);
674 print_number(ip, narg, c);
676 flagsandmodename(ip[0], ip[1], decimal);
681 print_number(ip, narg, c);
682 print_number(ip, narg, c);
684 * A flags value of zero is valid for
685 * wait4() but not for wait6(), so
686 * handle zero special here.
689 print_number(ip, narg, c);
699 idtypename(*ip, decimal);
703 print_number(ip, narg, c);
704 print_number(ip, narg, c);
713 print_number(ip, narg, c);
721 print_number(ip, narg, c);
728 print_number(ip, narg, c);
729 print_number(ip, narg, c);
731 getfsstatflagsname(*ip);
736 print_number(ip, narg, c);
737 print_number(ip, narg, c);
744 print_number(ip, narg, c);
752 print_number(ip, narg, c);
753 print_number(ip, narg, c);
755 sendrecvflagsname(*ip);
761 print_number(ip, narg, c);
762 print_number(ip, narg, c);
763 print_number(ip, narg, c);
765 sendrecvflagsname(*ip);
772 print_number(ip, narg, c);
779 print_number(ip, narg, c);
798 print_number(ip, narg, c);
799 print_number(ip, narg, c);
805 #ifdef SYS_freebsd6_mmap
806 case SYS_freebsd6_mmap:
807 print_number(ip, narg, c);
808 print_number(ip, narg, c);
820 print_number(ip, narg, c);
821 print_number(ip, narg, c);
832 print_number(ip, narg, c);
833 print_number(ip, narg, c);
840 print_number(ip, narg, c);
841 print_number(ip, narg, c);
843 madvisebehavname(*ip);
847 case SYS_setpriority:
848 print_number(ip, narg, c);
849 print_number(ip, narg, c);
856 print_number(ip, narg, c);
858 fcntlcmdname(ip[0], ip[1], decimal);
866 sockdomainname(sockdomain);
870 socktypenamewithflags(*ip);
873 if (sockdomain == PF_INET ||
874 sockdomain == PF_INET6) {
876 sockipprotoname(*ip);
885 print_number(ip, narg, c);
887 sockoptlevelname(*ip, decimal);
888 if (*ip == SOL_SOCKET) {
897 #ifdef SYS_freebsd6_lseek
898 case SYS_freebsd6_lseek:
899 print_number(ip, narg, c);
900 /* Hidden 'pad' argument, not in lseek(2) */
901 print_number(ip, narg, c);
902 print_number(ip, narg, c);
910 print_number(ip, narg, c);
911 /* Hidden 'pad' argument, not in lseek(2) */
912 print_number(ip, narg, c);
919 print_number(ip, narg, c);
929 print_number(ip, narg, c);
936 print_number(ip, narg, c);
938 shutdownhowname(*ip);
948 socktypenamewithflags(*ip);
962 print_number(ip, narg, c);
984 print_number(ip, narg, c);
985 print_number(ip, narg, c);
992 print_number(ip, narg, c);
993 print_number(ip, narg, c);
1000 print_number(ip, narg, c);
1007 print_number(ip, narg, c);
1008 print_number(ip, narg, c);
1015 print_number(ip, narg, c);
1022 print_number(ip, narg, c);
1025 printf(",0%o", (unsigned int)ip[1]);
1030 print_number(ip, narg, c);
1031 print_number(ip, narg, c);
1044 case SYS_lio_listio:
1046 lio_listioname(*ip);
1057 case SYS_sched_setscheduler:
1058 print_number(ip, narg, c);
1060 schedpolicyname(*ip);
1064 case SYS_sched_get_priority_max:
1065 case SYS_sched_get_priority_min:
1067 schedpolicyname(*ip);
1072 print_number(ip, narg, c);
1073 print_number(ip, narg, c);
1074 print_number(ip, narg, c);
1075 print_number(ip, narg, c);
1076 print_number(ip, narg, c);
1077 print_number(ip, narg, c);
1079 sendfileflagsname(*(int *)ip);
1084 print_number(ip, narg, c);
1090 case SYS_sigprocmask:
1092 sigprocmaskhowname(*ip);
1097 case SYS___acl_get_file:
1098 case SYS___acl_set_file:
1099 case SYS___acl_get_fd:
1100 case SYS___acl_set_fd:
1101 case SYS___acl_delete_file:
1102 case SYS___acl_delete_fd:
1103 case SYS___acl_aclcheck_file:
1104 case SYS___acl_aclcheck_fd:
1105 case SYS___acl_get_link:
1106 case SYS___acl_set_link:
1107 case SYS___acl_delete_link:
1108 case SYS___acl_aclcheck_link:
1109 print_number(ip, narg, c);
1122 case SYS_extattrctl:
1123 print_number(ip, narg, c);
1125 extattrctlname(*ip);
1130 print_number(ip, narg, c);
1131 print_number(ip, narg, c);
1133 mountflagsname(*ip);
1137 case SYS_thr_create:
1138 print_number(ip, narg, c);
1139 print_number(ip, narg, c);
1141 thrcreateflagsname(*ip);
1146 print_number(ip, narg, c);
1152 case SYS_kldunloadf:
1153 print_number(ip, narg, c);
1155 kldunloadfflagsname(*ip);
1162 print_number(ip, narg, c);
1164 atfdname(*ip, decimal);
1168 case SYS_cap_fcntls_limit:
1169 print_number(ip, narg, c);
1176 case SYS_posix_fadvise:
1177 print_number(ip, narg, c);
1178 print_number(ip, narg, c);
1179 print_number(ip, narg, c);
1181 fadvisebehavname((int)*ip);
1187 idtypename(*ip, decimal);
1191 print_number(ip, narg, c);
1193 procctlcmdname(*ip);
1198 print_number(ip, narg, c);
1202 case UMTX_OP_CV_WAIT:
1206 umtxcvwaitflags(*ip);
1208 case UMTX_OP_RW_RDLOCK:
1212 umtxrwlockflags(*ip);
1220 print_number(ip, narg, c);
1228 ktrsysret(struct ktr_sysret *ktr, u_int flags)
1230 register_t ret = ktr->ktr_retval;
1231 int error = ktr->ktr_error;
1232 int code = ktr->ktr_code;
1234 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
1235 (code >= nsyscalls || code < 0))
1236 printf("[%d] ", code);
1238 printf("%s", syscallnames[code]);
1240 printf("[%d]", code);
1246 printf("%ld", (long)ret);
1247 if (ret < 0 || ret > 9)
1248 printf("/%#lx", (unsigned long)ret);
1251 printf("%ld", (long)ret);
1253 printf("%#lx", (unsigned long)ret);
1255 } else if (error == ERESTART)
1257 else if (error == EJUSTRETURN)
1258 printf("JUSTRETURN");
1260 printf("-1 errno %d", ktr->ktr_error);
1262 printf(" %s", strerror(ktr->ktr_error));
1268 ktrnamei(char *cp, int len)
1270 printf("\"%.*s\"\n", len, cp);
1274 hexdump(char *p, int len, int screenwidth)
1282 i = 13; /* base offset */
1283 i += (width / 2) + 1; /* spaces every second byte */
1284 i += (width * 2); /* width of bytes */
1286 i += width; /* each byte */
1288 } while (i < screenwidth);
1291 for (n = 0; n < len; n += width) {
1292 for (i = n; i < n + width; i++) {
1293 if ((i % width) == 0) { /* beginning of line */
1294 printf(" 0x%04x", i);
1300 printf("%02x", p[i] & 0xff);
1305 for (i = n; i < n + width; i++) {
1308 if (p[i] >= ' ' && p[i] <= '~')
1315 if ((i % width) != 0)
1320 visdump(char *dp, int datalen, int screenwidth)
1329 for (;datalen > 0; datalen--, dp++) {
1330 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1333 * Keep track of printables and
1334 * space chars (like fold(1)).
1346 width = 8 - (col&07);
1351 if (col + width > (screenwidth-2)) {
1366 ktrgenio(struct ktr_genio *ktr, int len)
1368 int datalen = len - sizeof (struct ktr_genio);
1369 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1370 static int screenwidth = 0;
1373 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1374 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1375 datalen == 1 ? "" : "s");
1378 if (screenwidth == 0) {
1381 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1383 screenwidth = ws.ws_col;
1387 if (maxdata && datalen > maxdata)
1390 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1391 if (dp[i] >= 32 && dp[i] < 127)
1393 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1398 hexdump(dp, datalen, screenwidth);
1400 visdump(dp, datalen, screenwidth);
1403 const char *signames[] = {
1404 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
1405 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
1406 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
1407 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
1408 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
1409 "USR2", NULL, /* 31 - 32 */
1413 ktrpsig(struct ktr_psig *psig)
1415 if (psig->signo > 0 && psig->signo < NSIG)
1416 printf("SIG%s ", signames[psig->signo]);
1418 printf("SIG %d ", psig->signo);
1419 if (psig->action == SIG_DFL) {
1420 printf("SIG_DFL code=");
1421 sigcodename(psig->signo, psig->code);
1424 printf("caught handler=0x%lx mask=0x%x code=",
1425 (u_long)psig->action, psig->mask.__bits[0]);
1426 sigcodename(psig->signo, psig->code);
1432 ktrcsw_old(struct ktr_csw_old *cs)
1434 printf("%s %s\n", cs->out ? "stop" : "resume",
1435 cs->user ? "user" : "kernel");
1439 ktrcsw(struct ktr_csw *cs)
1441 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1442 cs->user ? "user" : "kernel", cs->wmesg);
1445 #define UTRACE_DLOPEN_START 1
1446 #define UTRACE_DLOPEN_STOP 2
1447 #define UTRACE_DLCLOSE_START 3
1448 #define UTRACE_DLCLOSE_STOP 4
1449 #define UTRACE_LOAD_OBJECT 5
1450 #define UTRACE_UNLOAD_OBJECT 6
1451 #define UTRACE_ADD_RUNDEP 7
1452 #define UTRACE_PRELOAD_FINISHED 8
1453 #define UTRACE_INIT_CALL 9
1454 #define UTRACE_FINI_CALL 10
1455 #define UTRACE_DLSYM_START 11
1456 #define UTRACE_DLSYM_STOP 12
1458 struct utrace_rtld {
1459 char sig[4]; /* 'RTLD' */
1465 char name[MAXPATHLEN];
1469 ktruser_rtld(int len, void *p)
1471 struct utrace_rtld *ut = p;
1476 switch (ut->event) {
1477 case UTRACE_DLOPEN_START:
1479 printf("dlopen(%s, ", ut->name);
1480 switch (mode & RTLD_MODEMASK) {
1485 printf("RTLD_LAZY");
1488 printf("%#x", mode & RTLD_MODEMASK);
1490 if (mode & RTLD_GLOBAL)
1491 printf(" | RTLD_GLOBAL");
1492 if (mode & RTLD_TRACE)
1493 printf(" | RTLD_TRACE");
1494 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
1495 printf(" | %#x", mode &
1496 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
1499 case UTRACE_DLOPEN_STOP:
1500 printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
1503 case UTRACE_DLCLOSE_START:
1504 printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
1507 case UTRACE_DLCLOSE_STOP:
1508 printf("dlclose(%p) finished\n", ut->handle);
1510 case UTRACE_LOAD_OBJECT:
1511 printf("RTLD: loaded %p @ %p - %p (%s)\n", ut->handle,
1512 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1515 case UTRACE_UNLOAD_OBJECT:
1516 printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
1517 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1520 case UTRACE_ADD_RUNDEP:
1521 parent = ut->mapbase;
1522 printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
1523 ut->handle, ut->name, ut->refcnt);
1525 case UTRACE_PRELOAD_FINISHED:
1526 printf("RTLD: LD_PRELOAD finished\n");
1528 case UTRACE_INIT_CALL:
1529 printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
1532 case UTRACE_FINI_CALL:
1533 printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
1536 case UTRACE_DLSYM_START:
1537 printf("RTLD: dlsym(%p, %s)\n", ut->handle, ut->name);
1539 case UTRACE_DLSYM_STOP:
1540 printf("RTLD: %p = dlsym(%p, %s)\n", ut->mapbase, ut->handle,
1547 printf("RTLD: %d ", len);
1550 printf(" %d", *cp++);
1552 printf(" %02x", *cp++);
1557 struct utrace_malloc {
1564 ktruser_malloc(void *p)
1566 struct utrace_malloc *ut = p;
1568 if (ut->p == (void *)(intptr_t)(-1))
1569 printf("malloc_init()\n");
1570 else if (ut->s == 0)
1571 printf("free(%p)\n", ut->p);
1572 else if (ut->p == NULL)
1573 printf("%p = malloc(%zu)\n", ut->r, ut->s);
1575 printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
1579 ktruser(int len, void *p)
1583 if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
1584 ktruser_rtld(len, p);
1588 if (len == sizeof(struct utrace_malloc)) {
1597 printf(" %d", *cp++);
1599 printf(" %02x", *cp++);
1604 ktrcaprights(cap_rights_t *rightsp)
1607 printf("cap_rights_t ");
1613 ktrsockaddr(struct sockaddr *sa)
1616 TODO: Support additional address families
1617 #include <netnatm/natm.h>
1618 struct sockaddr_natm *natm;
1619 #include <netsmb/netbios.h>
1620 struct sockaddr_nb *nb;
1625 * note: ktrstruct() has already verified that sa points to a
1626 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1627 * sa->sa_len bytes long.
1629 printf("struct sockaddr { ");
1630 sockfamilyname(sa->sa_family);
1633 #define check_sockaddr_len(n) \
1634 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1635 printf("invalid"); \
1639 switch(sa->sa_family) {
1641 struct sockaddr_in sa_in;
1643 memset(&sa_in, 0, sizeof(sa_in));
1644 memcpy(&sa_in, sa, sa->sa_len);
1645 check_sockaddr_len(in);
1646 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1647 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1651 case AF_APPLETALK: {
1652 struct sockaddr_at sa_at;
1653 struct netrange *nr;
1655 memset(&sa_at, 0, sizeof(sa_at));
1656 memcpy(&sa_at, sa, sa->sa_len);
1657 check_sockaddr_len(at);
1658 nr = &sa_at.sat_range.r_netrange;
1659 printf("%d.%d, %d-%d, %d", ntohs(sa_at.sat_addr.s_net),
1660 sa_at.sat_addr.s_node, ntohs(nr->nr_firstnet),
1661 ntohs(nr->nr_lastnet), nr->nr_phase);
1666 struct sockaddr_in6 sa_in6;
1668 memset(&sa_in6, 0, sizeof(sa_in6));
1669 memcpy(&sa_in6, sa, sa->sa_len);
1670 check_sockaddr_len(in6);
1671 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1672 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1673 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1678 struct sockaddr_ipx sa_ipx;
1680 memset(&sa_ipx, 0, sizeof(sa_ipx));
1681 memcpy(&sa_ipx, sa, sa->sa_len);
1682 check_sockaddr_len(ipx);
1683 /* XXX wish we had ipx_ntop */
1684 printf("%s", ipx_ntoa(sa_ipx.sipx_addr));
1690 struct sockaddr_un sa_un;
1692 memset(&sa_un, 0, sizeof(sa_un));
1693 memcpy(&sa_un, sa, sa->sa_len);
1694 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1698 printf("unknown address family");
1704 ktrstat(struct stat *statp)
1706 char mode[12], timestr[PATH_MAX + 4];
1712 * note: ktrstruct() has already verified that statp points to a
1713 * buffer exactly sizeof(struct stat) bytes long.
1715 printf("struct stat {");
1716 printf("dev=%ju, ino=%ju, ",
1717 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1719 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1721 strmode(statp->st_mode, mode);
1722 printf("mode=%s, ", mode);
1724 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1725 if (resolv == 0 || (pwd = getpwuid(statp->st_uid)) == NULL)
1726 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1728 printf("uid=\"%s\", ", pwd->pw_name);
1729 if (resolv == 0 || (grp = getgrgid(statp->st_gid)) == NULL)
1730 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1732 printf("gid=\"%s\", ", grp->gr_name);
1733 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1736 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1738 tm = localtime(&statp->st_atim.tv_sec);
1739 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1740 printf("\"%s\"", timestr);
1742 if (statp->st_atim.tv_nsec != 0)
1743 printf(".%09ld, ", statp->st_atim.tv_nsec);
1748 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1750 tm = localtime(&statp->st_mtim.tv_sec);
1751 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1752 printf("\"%s\"", timestr);
1754 if (statp->st_mtim.tv_nsec != 0)
1755 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1760 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1762 tm = localtime(&statp->st_ctim.tv_sec);
1763 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1764 printf("\"%s\"", timestr);
1766 if (statp->st_ctim.tv_nsec != 0)
1767 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1770 printf("birthtime=");
1772 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1774 tm = localtime(&statp->st_birthtim.tv_sec);
1775 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1776 printf("\"%s\"", timestr);
1778 if (statp->st_birthtim.tv_nsec != 0)
1779 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1782 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1783 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1784 (intmax_t)statp->st_blocks, statp->st_flags);
1789 ktrstruct(char *buf, size_t buflen)
1792 size_t namelen, datalen;
1794 cap_rights_t rights;
1796 struct sockaddr_storage ss;
1798 for (name = buf, namelen = 0;
1799 namelen < buflen && name[namelen] != '\0';
1802 if (namelen == buflen)
1804 if (name[namelen] != '\0')
1806 data = buf + namelen + 1;
1807 datalen = buflen - namelen - 1;
1811 for (i = 0; i < (int)namelen; ++i)
1812 if (!isalpha(name[i]))
1814 if (strcmp(name, "caprights") == 0) {
1815 if (datalen != sizeof(cap_rights_t))
1817 memcpy(&rights, data, datalen);
1818 ktrcaprights(&rights);
1819 } else if (strcmp(name, "stat") == 0) {
1820 if (datalen != sizeof(struct stat))
1822 memcpy(&sb, data, datalen);
1824 } else if (strcmp(name, "sockaddr") == 0) {
1825 if (datalen > sizeof(ss))
1827 memcpy(&ss, data, datalen);
1828 if (datalen != ss.ss_len)
1830 ktrsockaddr((struct sockaddr *)&ss);
1832 printf("unknown structure\n");
1836 printf("invalid record\n");
1840 ktrcapfail(struct ktr_cap_fail *ktr)
1842 switch (ktr->cap_type) {
1843 case CAPFAIL_NOTCAPABLE:
1844 /* operation on fd with insufficient capabilities */
1845 printf("operation requires ");
1846 capname(&ktr->cap_needed);
1847 printf(", process holds ");
1848 capname(&ktr->cap_held);
1850 case CAPFAIL_INCREASE:
1851 /* requested more capabilities than fd already has */
1852 printf("attempt to increase capabilities from ");
1853 capname(&ktr->cap_held);
1855 capname(&ktr->cap_needed);
1857 case CAPFAIL_SYSCALL:
1858 /* called restricted syscall */
1859 printf("disallowed system call");
1861 case CAPFAIL_LOOKUP:
1862 /* used ".." in strict-relative mode */
1863 printf("restricted VFS lookup");
1866 printf("unknown capability failure: ");
1867 capname(&ktr->cap_needed);
1869 capname(&ktr->cap_held);
1876 ktrfault(struct ktr_fault *ktr)
1879 printf("0x%jx ", (uintmax_t)ktr->vaddr);
1880 vmprotname(ktr->type);
1885 ktrfaultend(struct ktr_faultend *ktr)
1888 vmresultname(ktr->result);
1892 #if defined(__amd64__) || defined(__i386__)
1894 linux_ktrsyscall(struct ktr_syscall *ktr)
1896 int narg = ktr->ktr_narg;
1899 if (ktr->ktr_code >= nlinux_syscalls || ktr->ktr_code < 0)
1900 printf("[%d]", ktr->ktr_code);
1902 printf("%s", linux_syscallnames[ktr->ktr_code]);
1904 printf("[%d]", ktr->ktr_code);
1906 ip = &ktr->ktr_args[0];
1910 print_number(ip, narg, c);
1917 linux_ktrsysret(struct ktr_sysret *ktr)
1919 register_t ret = ktr->ktr_retval;
1920 int error = ktr->ktr_error;
1921 int code = ktr->ktr_code;
1923 if (code >= nlinux_syscalls || code < 0)
1924 printf("[%d] ", code);
1926 printf("%s", linux_syscallnames[code]);
1928 printf("[%d]", code);
1934 printf("%ld", (long)ret);
1935 if (ret < 0 || ret > 9)
1936 printf("/%#lx", (unsigned long)ret);
1939 printf("%ld", (long)ret);
1941 printf("%#lx", (unsigned long)ret);
1943 } else if (error == ERESTART)
1945 else if (error == EJUSTRETURN)
1946 printf("JUSTRETURN");
1948 if (ktr->ktr_error <= ELAST + 1)
1949 error = abs(bsd_to_linux_errno[ktr->ktr_error]);
1952 printf("-1 errno %d", error);
1954 printf(" %s", strerror(ktr->ktr_error));
1963 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
1964 "[-m maxdata] [-p pid] [-t trstr]\n");