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/capability.h>
50 #include <sys/errno.h>
55 #include <sys/ktrace.h>
56 #include <sys/ioctl.h>
57 #include <sys/socket.h>
59 #include <sys/sysent.h>
61 #include <sys/queue.h>
63 #include <sys/types.h>
64 #include <netipx/ipx.h>
67 #include <netatalk/at.h>
69 #include <arpa/inet.h>
70 #include <netinet/in.h>
88 #include "kdump_subr.h"
90 u_int abidump(struct ktr_header *);
91 int fetchprocinfo(struct ktr_header *, u_int *);
92 int fread_tail(void *, int, int);
93 void dumpheader(struct ktr_header *);
94 void ktrsyscall(struct ktr_syscall *, u_int);
95 void ktrsysret(struct ktr_sysret *, u_int);
96 void ktrnamei(char *, int);
97 void hexdump(char *, int, int);
98 void visdump(char *, int, int);
99 void ktrgenio(struct ktr_genio *, int);
100 void ktrpsig(struct ktr_psig *);
101 void ktrcsw(struct ktr_csw *);
102 void ktrcsw_old(struct ktr_csw_old *);
103 void ktruser_malloc(unsigned char *);
104 void ktruser_rtld(int, unsigned char *);
105 void ktruser(int, unsigned char *);
106 void ktrcaprights(cap_rights_t *);
107 void ktrsockaddr(struct sockaddr *);
108 void ktrstat(struct stat *);
109 void ktrstruct(char *, size_t);
110 void ktrcapfail(struct ktr_cap_fail *);
111 void ktrfault(struct ktr_fault *);
112 void ktrfaultend(struct ktr_faultend *);
113 void limitfd(int fd);
115 void ioctlname(unsigned long, int);
117 int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
118 resolv = 0, abiflag = 0;
119 const char *tracefile = DEF_TRACEFILE;
120 struct ktr_header ktr_header;
122 #define TIME_FORMAT "%b %e %T %Y"
123 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
125 #define print_number(i,n,c) do { \
127 printf("%c%jd", c, (intmax_t)*i); \
129 printf("%c%#jx", c, (uintmax_t)(u_register_t)*i); \
135 #if defined(__amd64__) || defined(__i386__)
137 void linux_ktrsyscall(struct ktr_syscall *);
138 void linux_ktrsysret(struct ktr_sysret *);
139 extern char *linux_syscallnames[];
140 extern int nlinux_syscalls;
144 * Linux syscalls return negative errno's, we do positive and map them
146 static int bsd_to_linux_errno[ELAST + 1] = {
147 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9,
148 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
149 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
150 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
151 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
152 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
153 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
154 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9,
155 -6, -6, -43, -42, -75,-125, -84, -95, -16, -74,
162 TAILQ_ENTRY(proc_info) info;
167 TAILQ_HEAD(trace_procs, proc_info) trace_procs;
174 * Cache NLS data before entering capability mode.
175 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
177 (void)catopen("libc", NL_CAT_LOCALE);
186 * Allow localtime(3) to cache /etc/localtime content before entering
188 * XXXPJD: There should be localtime_init() in libc.
191 (void)localtime(<ime);
195 main(int argc, char *argv[])
197 int ch, ktrlen, size;
199 int trpoints = ALL_POINTS;
204 setlocale(LC_CTYPE, "");
206 while ((ch = getopt(argc,argv,"f:dElm:np:AHRrsTt:")) != -1)
221 maxdata = atoi(optarg);
236 timestamp = 3; /* elapsed timestamp */
242 timestamp = 2; /* relative timestamp */
248 trpoints = getpoints(optarg);
250 errx(1, "unknown trace point in %s", optarg);
259 m = malloc(size = 1025);
261 errx(1, "%s", strerror(ENOMEM));
262 if (!freopen(tracefile, "r", stdin))
263 err(1, "%s", tracefile);
269 if (cap_enter() < 0 && errno != ENOSYS)
270 err(1, "unable to enter capability mode");
272 limitfd(STDIN_FILENO);
273 limitfd(STDOUT_FILENO);
274 limitfd(STDERR_FILENO);
276 TAILQ_INIT(&trace_procs);
278 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
279 if (ktr_header.ktr_type & KTR_DROP) {
280 ktr_header.ktr_type &= ~KTR_DROP;
281 if (!drop_logged && threads) {
283 "%6jd %6jd %-8.*s Events dropped.\n",
284 (intmax_t)ktr_header.ktr_pid,
285 ktr_header.ktr_tid > 0 ?
286 (intmax_t)ktr_header.ktr_tid : 0,
287 MAXCOMLEN, ktr_header.ktr_comm);
289 } else if (!drop_logged) {
290 printf("%6jd %-8.*s Events dropped.\n",
291 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
292 ktr_header.ktr_comm);
296 if (trpoints & (1<<ktr_header.ktr_type))
297 if (pid == 0 || ktr_header.ktr_pid == pid ||
298 ktr_header.ktr_tid == pid)
299 dumpheader(&ktr_header);
300 if ((ktrlen = ktr_header.ktr_len) < 0)
301 errx(1, "bogus length 0x%x", ktrlen);
303 m = realloc(m, ktrlen+1);
305 errx(1, "%s", strerror(ENOMEM));
308 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
309 errx(1, "data too short");
310 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
312 sv_flags = abidump(&ktr_header);
313 if (pid && ktr_header.ktr_pid != pid &&
314 ktr_header.ktr_tid != pid)
316 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
319 switch (ktr_header.ktr_type) {
321 #if defined(__amd64__) || defined(__i386__)
322 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
323 linux_ktrsyscall((struct ktr_syscall *)m);
326 ktrsyscall((struct ktr_syscall *)m, sv_flags);
329 #if defined(__amd64__) || defined(__i386__)
330 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
331 linux_ktrsysret((struct ktr_sysret *)m);
334 ktrsysret((struct ktr_sysret *)m, sv_flags);
341 ktrgenio((struct ktr_genio *)m, ktrlen);
344 ktrpsig((struct ktr_psig *)m);
347 if (ktrlen == sizeof(struct ktr_csw_old))
348 ktrcsw_old((struct ktr_csw_old *)m);
350 ktrcsw((struct ktr_csw *)m);
356 ktrstruct(m, ktrlen);
359 ktrcapfail((struct ktr_cap_fail *)m);
362 ktrfault((struct ktr_fault *)m);
365 ktrfaultend((struct ktr_faultend *)m);
383 cap_rights_init(&rights, CAP_FSTAT);
388 cap_rights_set(&rights, CAP_READ);
391 cap_rights_set(&rights, CAP_IOCTL, CAP_WRITE);
392 cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
395 cap_rights_set(&rights, CAP_WRITE);
397 cap_rights_set(&rights, CAP_IOCTL);
405 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
406 err(1, "unable to limit rights for descriptor %d", fd);
407 if (cmd != -1 && cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
408 err(1, "unable to limit ioctls for descriptor %d", fd);
412 fread_tail(void *buf, int size, int num)
416 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
424 fetchprocinfo(struct ktr_header *kth, u_int *flags)
426 struct proc_info *pi;
428 switch (kth->ktr_type) {
430 TAILQ_FOREACH(pi, &trace_procs, info) {
431 if (pi->pid == kth->ktr_pid) {
432 TAILQ_REMOVE(&trace_procs, pi, info);
436 pi = malloc(sizeof(struct proc_info));
438 errx(1, "%s", strerror(ENOMEM));
439 pi->sv_flags = *flags;
440 pi->pid = kth->ktr_pid;
441 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
445 TAILQ_FOREACH(pi, &trace_procs, info) {
446 if (pi->pid == kth->ktr_pid) {
447 TAILQ_REMOVE(&trace_procs, pi, info);
459 abidump(struct ktr_header *kth)
461 struct proc_info *pi;
466 TAILQ_FOREACH(pi, &trace_procs, info) {
467 if (pi->pid == kth->ktr_pid) {
468 flags = pi->sv_flags;
476 switch (flags & SV_ABI_MASK) {
496 printf("%s%s ", abi, arch);
502 dumpheader(struct ktr_header *kth)
504 static char unknown[64];
505 static struct timeval prevtime, temp;
508 switch (kth->ktr_type) {
550 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
555 * The ktr_tid field was previously the ktr_buffer field, which held
556 * the kernel pointer value for the buffer associated with data
557 * following the record header. It now holds a threadid, but only
558 * for trace files after the change. Older trace files still contain
559 * kernel pointers. Detect this and suppress the results by printing
560 * negative tid's as 0.
563 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
564 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
565 MAXCOMLEN, kth->ktr_comm);
567 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
570 if (timestamp == 3) {
571 if (prevtime.tv_sec == 0)
572 prevtime = kth->ktr_time;
573 timevalsub(&kth->ktr_time, &prevtime);
575 if (timestamp == 2) {
576 temp = kth->ktr_time;
577 timevalsub(&kth->ktr_time, &prevtime);
580 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
581 kth->ktr_time.tv_usec);
586 #include <sys/syscall.h>
588 #include <sys/kern/syscalls.c>
590 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
593 ktrsyscall(struct ktr_syscall *ktr, u_int flags)
595 int narg = ktr->ktr_narg;
599 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
600 (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0))
601 printf("[%d]", ktr->ktr_code);
603 printf("%s", syscallnames[ktr->ktr_code]);
604 ip = &ktr->ktr_args[0];
608 (flags == 0 || (flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
609 switch (ktr->ktr_code) {
626 atfdname(*ip, decimal);
632 switch (ktr->ktr_code) {
634 print_number(ip, narg, c);
636 ioctlname(*ip, decimal);
652 print_number(ip, narg, c);
660 print_number(ip, narg, c);
662 flagsandmodename(ip[0], ip[1], decimal);
667 print_number(ip, narg, c);
668 print_number(ip, narg, c);
677 print_number(ip, narg, c);
685 print_number(ip, narg, c);
692 print_number(ip, narg, c);
693 print_number(ip, narg, c);
695 getfsstatflagsname(*ip);
700 print_number(ip, narg, c);
701 print_number(ip, narg, c);
708 print_number(ip, narg, c);
716 print_number(ip, narg, c);
717 print_number(ip, narg, c);
719 sendrecvflagsname(*ip);
725 print_number(ip, narg, c);
726 print_number(ip, narg, c);
727 print_number(ip, narg, c);
729 sendrecvflagsname(*ip);
736 print_number(ip, narg, c);
743 print_number(ip, narg, c);
762 print_number(ip, narg, c);
763 print_number(ip, narg, c);
769 #ifdef SYS_freebsd6_mmap
770 case SYS_freebsd6_mmap:
771 print_number(ip, narg, c);
772 print_number(ip, narg, c);
784 print_number(ip, narg, c);
785 print_number(ip, narg, c);
796 print_number(ip, narg, c);
797 print_number(ip, narg, c);
804 print_number(ip, narg, c);
805 print_number(ip, narg, c);
807 madvisebehavname(*ip);
811 case SYS_setpriority:
812 print_number(ip, narg, c);
813 print_number(ip, narg, c);
820 print_number(ip, narg, c);
822 fcntlcmdname(ip[0], ip[1], decimal);
830 sockdomainname(sockdomain);
834 socktypenamewithflags(*ip);
837 if (sockdomain == PF_INET ||
838 sockdomain == PF_INET6) {
840 sockipprotoname(*ip);
849 print_number(ip, narg, c);
851 sockoptlevelname(*ip, decimal);
852 if (*ip == SOL_SOCKET) {
861 #ifdef SYS_freebsd6_lseek
862 case SYS_freebsd6_lseek:
863 print_number(ip, narg, c);
864 /* Hidden 'pad' argument, not in lseek(2) */
865 print_number(ip, narg, c);
866 print_number(ip, narg, c);
874 print_number(ip, narg, c);
875 /* Hidden 'pad' argument, not in lseek(2) */
876 print_number(ip, narg, c);
883 print_number(ip, narg, c);
893 print_number(ip, narg, c);
900 print_number(ip, narg, c);
902 shutdownhowname(*ip);
912 socktypenamewithflags(*ip);
926 print_number(ip, narg, c);
948 print_number(ip, narg, c);
949 print_number(ip, narg, c);
956 print_number(ip, narg, c);
957 print_number(ip, narg, c);
964 print_number(ip, narg, c);
971 print_number(ip, narg, c);
972 print_number(ip, narg, c);
979 print_number(ip, narg, c);
986 print_number(ip, narg, c);
987 print_number(ip, narg, c);
1000 case SYS_lio_listio:
1002 lio_listioname(*ip);
1013 case SYS_sched_setscheduler:
1014 print_number(ip, narg, c);
1016 schedpolicyname(*ip);
1020 case SYS_sched_get_priority_max:
1021 case SYS_sched_get_priority_min:
1023 schedpolicyname(*ip);
1028 print_number(ip, narg, c);
1029 print_number(ip, narg, c);
1030 print_number(ip, narg, c);
1031 print_number(ip, narg, c);
1032 print_number(ip, narg, c);
1033 print_number(ip, narg, c);
1035 sendfileflagsname(*ip);
1040 print_number(ip, narg, c);
1046 case SYS_sigprocmask:
1048 sigprocmaskhowname(*ip);
1053 case SYS___acl_get_file:
1054 case SYS___acl_set_file:
1055 case SYS___acl_get_fd:
1056 case SYS___acl_set_fd:
1057 case SYS___acl_delete_file:
1058 case SYS___acl_delete_fd:
1059 case SYS___acl_aclcheck_file:
1060 case SYS___acl_aclcheck_fd:
1061 case SYS___acl_get_link:
1062 case SYS___acl_set_link:
1063 case SYS___acl_delete_link:
1064 case SYS___acl_aclcheck_link:
1065 print_number(ip, narg, c);
1078 case SYS_extattrctl:
1079 print_number(ip, narg, c);
1081 extattrctlname(*ip);
1086 print_number(ip, narg, c);
1087 print_number(ip, narg, c);
1089 mountflagsname(*ip);
1093 case SYS_thr_create:
1094 print_number(ip, narg, c);
1095 print_number(ip, narg, c);
1097 thrcreateflagsname(*ip);
1102 print_number(ip, narg, c);
1108 case SYS_kldunloadf:
1109 print_number(ip, narg, c);
1111 kldunloadfflagsname(*ip);
1118 print_number(ip, narg, c);
1120 atfdname(*ip, decimal);
1124 case SYS_cap_fcntls_limit:
1125 print_number(ip, narg, c);
1132 case SYS_posix_fadvise:
1133 print_number(ip, narg, c);
1134 print_number(ip, narg, c);
1135 print_number(ip, narg, c);
1137 fadvisebehavname((int)*ip);
1144 print_number(ip, narg, c);
1152 ktrsysret(struct ktr_sysret *ktr, u_int flags)
1154 register_t ret = ktr->ktr_retval;
1155 int error = ktr->ktr_error;
1156 int code = ktr->ktr_code;
1158 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
1159 (code >= nsyscalls || code < 0))
1160 printf("[%d] ", code);
1162 printf("%s ", syscallnames[code]);
1166 printf("%ld", (long)ret);
1167 if (ret < 0 || ret > 9)
1168 printf("/%#lx", (unsigned long)ret);
1171 printf("%ld", (long)ret);
1173 printf("%#lx", (unsigned long)ret);
1175 } else if (error == ERESTART)
1177 else if (error == EJUSTRETURN)
1178 printf("JUSTRETURN");
1180 printf("-1 errno %d", ktr->ktr_error);
1182 printf(" %s", strerror(ktr->ktr_error));
1188 ktrnamei(char *cp, int len)
1190 printf("\"%.*s\"\n", len, cp);
1194 hexdump(char *p, int len, int screenwidth)
1202 i = 13; /* base offset */
1203 i += (width / 2) + 1; /* spaces every second byte */
1204 i += (width * 2); /* width of bytes */
1206 i += width; /* each byte */
1208 } while (i < screenwidth);
1211 for (n = 0; n < len; n += width) {
1212 for (i = n; i < n + width; i++) {
1213 if ((i % width) == 0) { /* beginning of line */
1214 printf(" 0x%04x", i);
1220 printf("%02x", p[i] & 0xff);
1225 for (i = n; i < n + width; i++) {
1228 if (p[i] >= ' ' && p[i] <= '~')
1235 if ((i % width) != 0)
1240 visdump(char *dp, int datalen, int screenwidth)
1249 for (;datalen > 0; datalen--, dp++) {
1250 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1253 * Keep track of printables and
1254 * space chars (like fold(1)).
1266 width = 8 - (col&07);
1271 if (col + width > (screenwidth-2)) {
1286 ktrgenio(struct ktr_genio *ktr, int len)
1288 int datalen = len - sizeof (struct ktr_genio);
1289 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1290 static int screenwidth = 0;
1293 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1294 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1295 datalen == 1 ? "" : "s");
1298 if (screenwidth == 0) {
1301 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1303 screenwidth = ws.ws_col;
1307 if (maxdata && datalen > maxdata)
1310 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1311 if (dp[i] >= 32 && dp[i] < 127)
1313 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1318 hexdump(dp, datalen, screenwidth);
1320 visdump(dp, datalen, screenwidth);
1323 const char *signames[] = {
1324 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
1325 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
1326 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
1327 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
1328 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
1329 "USR2", NULL, /* 31 - 32 */
1333 ktrpsig(struct ktr_psig *psig)
1335 if (psig->signo > 0 && psig->signo < NSIG)
1336 printf("SIG%s ", signames[psig->signo]);
1338 printf("SIG %d ", psig->signo);
1339 if (psig->action == SIG_DFL) {
1340 printf("SIG_DFL code=");
1341 sigcodename(psig->signo, psig->code);
1344 printf("caught handler=0x%lx mask=0x%x code=",
1345 (u_long)psig->action, psig->mask.__bits[0]);
1346 sigcodename(psig->signo, psig->code);
1352 ktrcsw_old(struct ktr_csw_old *cs)
1354 printf("%s %s\n", cs->out ? "stop" : "resume",
1355 cs->user ? "user" : "kernel");
1359 ktrcsw(struct ktr_csw *cs)
1361 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1362 cs->user ? "user" : "kernel", cs->wmesg);
1365 #define UTRACE_DLOPEN_START 1
1366 #define UTRACE_DLOPEN_STOP 2
1367 #define UTRACE_DLCLOSE_START 3
1368 #define UTRACE_DLCLOSE_STOP 4
1369 #define UTRACE_LOAD_OBJECT 5
1370 #define UTRACE_UNLOAD_OBJECT 6
1371 #define UTRACE_ADD_RUNDEP 7
1372 #define UTRACE_PRELOAD_FINISHED 8
1373 #define UTRACE_INIT_CALL 9
1374 #define UTRACE_FINI_CALL 10
1376 struct utrace_rtld {
1377 char sig[4]; /* 'RTLD' */
1383 char name[MAXPATHLEN];
1387 ktruser_rtld(int len, unsigned char *p)
1389 struct utrace_rtld *ut = (struct utrace_rtld *)p;
1393 switch (ut->event) {
1394 case UTRACE_DLOPEN_START:
1396 printf("dlopen(%s, ", ut->name);
1397 switch (mode & RTLD_MODEMASK) {
1402 printf("RTLD_LAZY");
1405 printf("%#x", mode & RTLD_MODEMASK);
1407 if (mode & RTLD_GLOBAL)
1408 printf(" | RTLD_GLOBAL");
1409 if (mode & RTLD_TRACE)
1410 printf(" | RTLD_TRACE");
1411 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
1412 printf(" | %#x", mode &
1413 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
1416 case UTRACE_DLOPEN_STOP:
1417 printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
1420 case UTRACE_DLCLOSE_START:
1421 printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
1424 case UTRACE_DLCLOSE_STOP:
1425 printf("dlclose(%p) finished\n", ut->handle);
1427 case UTRACE_LOAD_OBJECT:
1428 printf("RTLD: loaded %p @ %p - %p (%s)\n", ut->handle,
1429 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1432 case UTRACE_UNLOAD_OBJECT:
1433 printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
1434 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1437 case UTRACE_ADD_RUNDEP:
1438 parent = ut->mapbase;
1439 printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
1440 ut->handle, ut->name, ut->refcnt);
1442 case UTRACE_PRELOAD_FINISHED:
1443 printf("RTLD: LD_PRELOAD finished\n");
1445 case UTRACE_INIT_CALL:
1446 printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
1449 case UTRACE_FINI_CALL:
1450 printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
1456 printf("RTLD: %d ", len);
1459 printf(" %d", *p++);
1461 printf(" %02x", *p++);
1466 struct utrace_malloc {
1473 ktruser_malloc(unsigned char *p)
1475 struct utrace_malloc *ut = (struct utrace_malloc *)p;
1477 if (ut->p == (void *)(intptr_t)(-1))
1478 printf("malloc_init()\n");
1479 else if (ut->s == 0)
1480 printf("free(%p)\n", ut->p);
1481 else if (ut->p == NULL)
1482 printf("%p = malloc(%zu)\n", ut->r, ut->s);
1484 printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
1488 ktruser(int len, unsigned char *p)
1491 if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
1492 ktruser_rtld(len, p);
1496 if (len == sizeof(struct utrace_malloc)) {
1504 printf(" %d", *p++);
1506 printf(" %02x", *p++);
1511 ktrcaprights(cap_rights_t *rightsp)
1514 printf("cap_rights_t ");
1520 ktrsockaddr(struct sockaddr *sa)
1523 TODO: Support additional address families
1524 #include <netnatm/natm.h>
1525 struct sockaddr_natm *natm;
1526 #include <netsmb/netbios.h>
1527 struct sockaddr_nb *nb;
1532 * note: ktrstruct() has already verified that sa points to a
1533 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1534 * sa->sa_len bytes long.
1536 printf("struct sockaddr { ");
1537 sockfamilyname(sa->sa_family);
1540 #define check_sockaddr_len(n) \
1541 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1542 printf("invalid"); \
1546 switch(sa->sa_family) {
1548 struct sockaddr_in sa_in;
1550 memset(&sa_in, 0, sizeof(sa_in));
1551 memcpy(&sa_in, sa, sa->sa_len);
1552 check_sockaddr_len(in);
1553 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1554 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1558 case AF_APPLETALK: {
1559 struct sockaddr_at sa_at;
1560 struct netrange *nr;
1562 memset(&sa_at, 0, sizeof(sa_at));
1563 memcpy(&sa_at, sa, sa->sa_len);
1564 check_sockaddr_len(at);
1565 nr = &sa_at.sat_range.r_netrange;
1566 printf("%d.%d, %d-%d, %d", ntohs(sa_at.sat_addr.s_net),
1567 sa_at.sat_addr.s_node, ntohs(nr->nr_firstnet),
1568 ntohs(nr->nr_lastnet), nr->nr_phase);
1573 struct sockaddr_in6 sa_in6;
1575 memset(&sa_in6, 0, sizeof(sa_in6));
1576 memcpy(&sa_in6, sa, sa->sa_len);
1577 check_sockaddr_len(in6);
1578 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1579 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1580 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1585 struct sockaddr_ipx sa_ipx;
1587 memset(&sa_ipx, 0, sizeof(sa_ipx));
1588 memcpy(&sa_ipx, sa, sa->sa_len);
1589 check_sockaddr_len(ipx);
1590 /* XXX wish we had ipx_ntop */
1591 printf("%s", ipx_ntoa(sa_ipx.sipx_addr));
1597 struct sockaddr_un sa_un;
1599 memset(&sa_un, 0, sizeof(sa_un));
1600 memcpy(&sa_un, sa, sa->sa_len);
1601 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1605 printf("unknown address family");
1611 ktrstat(struct stat *statp)
1613 char mode[12], timestr[PATH_MAX + 4];
1619 * note: ktrstruct() has already verified that statp points to a
1620 * buffer exactly sizeof(struct stat) bytes long.
1622 printf("struct stat {");
1623 strmode(statp->st_mode, mode);
1624 printf("dev=%ju, ino=%ju, mode=%s, nlink=%ju, ",
1625 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino, mode,
1626 (uintmax_t)statp->st_nlink);
1627 if (resolv == 0 || (pwd = getpwuid(statp->st_uid)) == NULL)
1628 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1630 printf("uid=\"%s\", ", pwd->pw_name);
1631 if (resolv == 0 || (grp = getgrgid(statp->st_gid)) == NULL)
1632 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1634 printf("gid=\"%s\", ", grp->gr_name);
1635 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1638 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1640 tm = localtime(&statp->st_atim.tv_sec);
1641 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1642 printf("\"%s\"", timestr);
1644 if (statp->st_atim.tv_nsec != 0)
1645 printf(".%09ld, ", statp->st_atim.tv_nsec);
1650 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1652 tm = localtime(&statp->st_mtim.tv_sec);
1653 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1654 printf("\"%s\"", timestr);
1656 if (statp->st_mtim.tv_nsec != 0)
1657 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1662 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1664 tm = localtime(&statp->st_ctim.tv_sec);
1665 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1666 printf("\"%s\"", timestr);
1668 if (statp->st_ctim.tv_nsec != 0)
1669 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1672 printf("birthtime=");
1674 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1676 tm = localtime(&statp->st_birthtim.tv_sec);
1677 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1678 printf("\"%s\"", timestr);
1680 if (statp->st_birthtim.tv_nsec != 0)
1681 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1684 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1685 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1686 (intmax_t)statp->st_blocks, statp->st_flags);
1691 ktrstruct(char *buf, size_t buflen)
1694 size_t namelen, datalen;
1696 cap_rights_t rights;
1698 struct sockaddr_storage ss;
1700 for (name = buf, namelen = 0;
1701 namelen < buflen && name[namelen] != '\0';
1704 if (namelen == buflen)
1706 if (name[namelen] != '\0')
1708 data = buf + namelen + 1;
1709 datalen = buflen - namelen - 1;
1713 for (i = 0; i < (int)namelen; ++i)
1714 if (!isalpha(name[i]))
1716 if (strcmp(name, "caprights") == 0) {
1717 if (datalen != sizeof(cap_rights_t))
1719 memcpy(&rights, data, datalen);
1720 ktrcaprights(&rights);
1721 } else if (strcmp(name, "stat") == 0) {
1722 if (datalen != sizeof(struct stat))
1724 memcpy(&sb, data, datalen);
1726 } else if (strcmp(name, "sockaddr") == 0) {
1727 if (datalen > sizeof(ss))
1729 memcpy(&ss, data, datalen);
1730 if (datalen != ss.ss_len)
1732 ktrsockaddr((struct sockaddr *)&ss);
1734 printf("unknown structure\n");
1738 printf("invalid record\n");
1742 ktrcapfail(struct ktr_cap_fail *ktr)
1744 switch (ktr->cap_type) {
1745 case CAPFAIL_NOTCAPABLE:
1746 /* operation on fd with insufficient capabilities */
1747 printf("operation requires ");
1748 capname(&ktr->cap_needed);
1749 printf(", process holds ");
1750 capname(&ktr->cap_held);
1752 case CAPFAIL_INCREASE:
1753 /* requested more capabilities than fd already has */
1754 printf("attempt to increase capabilities from ");
1755 capname(&ktr->cap_held);
1757 capname(&ktr->cap_needed);
1759 case CAPFAIL_SYSCALL:
1760 /* called restricted syscall */
1761 printf("disallowed system call");
1763 case CAPFAIL_LOOKUP:
1764 /* used ".." in strict-relative mode */
1765 printf("restricted VFS lookup");
1768 printf("unknown capability failure: ");
1769 capname(&ktr->cap_needed);
1771 capname(&ktr->cap_held);
1778 ktrfault(struct ktr_fault *ktr)
1781 printf("0x%jx ", ktr->vaddr);
1782 vmprotname(ktr->type);
1787 ktrfaultend(struct ktr_faultend *ktr)
1790 vmresultname(ktr->result);
1794 #if defined(__amd64__) || defined(__i386__)
1796 linux_ktrsyscall(struct ktr_syscall *ktr)
1798 int narg = ktr->ktr_narg;
1801 if (ktr->ktr_code >= nlinux_syscalls || ktr->ktr_code < 0)
1802 printf("[%d]", ktr->ktr_code);
1804 printf("%s", linux_syscallnames[ktr->ktr_code]);
1805 ip = &ktr->ktr_args[0];
1809 print_number(ip, narg, c);
1816 linux_ktrsysret(struct ktr_sysret *ktr)
1818 register_t ret = ktr->ktr_retval;
1819 int error = ktr->ktr_error;
1820 int code = ktr->ktr_code;
1822 if (code >= nlinux_syscalls || code < 0)
1823 printf("[%d] ", code);
1825 printf("%s ", linux_syscallnames[code]);
1829 printf("%ld", (long)ret);
1830 if (ret < 0 || ret > 9)
1831 printf("/%#lx", (unsigned long)ret);
1834 printf("%ld", (long)ret);
1836 printf("%#lx", (unsigned long)ret);
1838 } else if (error == ERESTART)
1840 else if (error == EJUSTRETURN)
1841 printf("JUSTRETURN");
1843 if (ktr->ktr_error <= ELAST + 1)
1844 error = abs(bsd_to_linux_errno[ktr->ktr_error]);
1847 printf("-1 errno %d", error);
1849 printf(" %s", strerror(ktr->ktr_error));
1858 fprintf(stderr, "usage: kdump [-dEnlHRrsTA] [-f trfile] "
1859 "[-m maxdata] [-p pid] [-t trstr]\n");