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 ktrsockaddr(struct sockaddr *);
107 void ktrstat(struct stat *);
108 void ktrstruct(char *, size_t);
109 void ktrcapfail(struct ktr_cap_fail *);
110 void ktrfault(struct ktr_fault *);
111 void ktrfaultend(struct ktr_faultend *);
112 void limitfd(int fd);
114 void ioctlname(unsigned long, int);
116 int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
117 resolv = 0, abiflag = 0;
118 const char *tracefile = DEF_TRACEFILE;
119 struct ktr_header ktr_header;
121 #define TIME_FORMAT "%b %e %T %Y"
122 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
124 #define print_number(i,n,c) do { \
126 printf("%c%jd", c, (intmax_t)*i); \
128 printf("%c%#jx", c, (uintmax_t)(u_register_t)*i); \
134 #if defined(__amd64__) || defined(__i386__)
136 void linux_ktrsyscall(struct ktr_syscall *);
137 void linux_ktrsysret(struct ktr_sysret *);
138 extern char *linux_syscallnames[];
139 extern int nlinux_syscalls;
143 * Linux syscalls return negative errno's, we do positive and map them
145 static int bsd_to_linux_errno[ELAST + 1] = {
146 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9,
147 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
148 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
149 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
150 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
151 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
152 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
153 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9,
154 -6, -6, -43, -42, -75,-125, -84, -95, -16, -74,
161 TAILQ_ENTRY(proc_info) info;
166 TAILQ_HEAD(trace_procs, proc_info) trace_procs;
173 * Cache NLS data before entering capability mode.
174 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
176 (void)catopen("libc", NL_CAT_LOCALE);
185 * Allow localtime(3) to cache /etc/localtime content before entering
187 * XXXPJD: There should be localtime_init() in libc.
190 (void)localtime(<ime);
194 main(int argc, char *argv[])
196 int ch, ktrlen, size;
198 int trpoints = ALL_POINTS;
203 setlocale(LC_CTYPE, "");
205 while ((ch = getopt(argc,argv,"f:dElm:np:AHRrsTt:")) != -1)
220 maxdata = atoi(optarg);
235 timestamp = 3; /* elapsed timestamp */
241 timestamp = 2; /* relative timestamp */
247 trpoints = getpoints(optarg);
249 errx(1, "unknown trace point in %s", optarg);
258 m = malloc(size = 1025);
260 errx(1, "%s", strerror(ENOMEM));
261 if (!freopen(tracefile, "r", stdin))
262 err(1, "%s", tracefile);
268 if (cap_enter() < 0 && errno != ENOSYS)
269 err(1, "unable to enter capability mode");
271 limitfd(STDIN_FILENO);
272 limitfd(STDOUT_FILENO);
273 limitfd(STDERR_FILENO);
275 TAILQ_INIT(&trace_procs);
277 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
278 if (ktr_header.ktr_type & KTR_DROP) {
279 ktr_header.ktr_type &= ~KTR_DROP;
280 if (!drop_logged && threads) {
282 "%6jd %6jd %-8.*s Events dropped.\n",
283 (intmax_t)ktr_header.ktr_pid,
284 ktr_header.ktr_tid > 0 ?
285 (intmax_t)ktr_header.ktr_tid : 0,
286 MAXCOMLEN, ktr_header.ktr_comm);
288 } else if (!drop_logged) {
289 printf("%6jd %-8.*s Events dropped.\n",
290 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
291 ktr_header.ktr_comm);
295 if (trpoints & (1<<ktr_header.ktr_type))
296 if (pid == 0 || ktr_header.ktr_pid == pid ||
297 ktr_header.ktr_tid == pid)
298 dumpheader(&ktr_header);
299 if ((ktrlen = ktr_header.ktr_len) < 0)
300 errx(1, "bogus length 0x%x", ktrlen);
302 m = realloc(m, ktrlen+1);
304 errx(1, "%s", strerror(ENOMEM));
307 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
308 errx(1, "data too short");
309 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
311 sv_flags = abidump(&ktr_header);
312 if (pid && ktr_header.ktr_pid != pid &&
313 ktr_header.ktr_tid != pid)
315 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
318 switch (ktr_header.ktr_type) {
320 #if defined(__amd64__) || defined(__i386__)
321 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
322 linux_ktrsyscall((struct ktr_syscall *)m);
325 ktrsyscall((struct ktr_syscall *)m, sv_flags);
328 #if defined(__amd64__) || defined(__i386__)
329 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
330 linux_ktrsysret((struct ktr_sysret *)m);
333 ktrsysret((struct ktr_sysret *)m, sv_flags);
340 ktrgenio((struct ktr_genio *)m, ktrlen);
343 ktrpsig((struct ktr_psig *)m);
346 if (ktrlen == sizeof(struct ktr_csw_old))
347 ktrcsw_old((struct ktr_csw_old *)m);
349 ktrcsw((struct ktr_csw *)m);
355 ktrstruct(m, ktrlen);
358 ktrcapfail((struct ktr_cap_fail *)m);
361 ktrfault((struct ktr_fault *)m);
364 ktrfaultend((struct ktr_faultend *)m);
390 rights |= CAP_IOCTL | CAP_WRITE;
391 cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
404 if (cap_rights_limit(fd, rights) < 0 && errno != ENOSYS)
405 err(1, "unable to limit rights for descriptor %d", fd);
406 if (cmd != -1 && cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
407 err(1, "unable to limit ioctls for descriptor %d", fd);
411 fread_tail(void *buf, int size, int num)
415 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
423 fetchprocinfo(struct ktr_header *kth, u_int *flags)
425 struct proc_info *pi;
427 switch (kth->ktr_type) {
429 TAILQ_FOREACH(pi, &trace_procs, info) {
430 if (pi->pid == kth->ktr_pid) {
431 TAILQ_REMOVE(&trace_procs, pi, info);
435 pi = malloc(sizeof(struct proc_info));
437 errx(1, "%s", strerror(ENOMEM));
438 pi->sv_flags = *flags;
439 pi->pid = kth->ktr_pid;
440 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
444 TAILQ_FOREACH(pi, &trace_procs, info) {
445 if (pi->pid == kth->ktr_pid) {
446 TAILQ_REMOVE(&trace_procs, pi, info);
458 abidump(struct ktr_header *kth)
460 struct proc_info *pi;
465 TAILQ_FOREACH(pi, &trace_procs, info) {
466 if (pi->pid == kth->ktr_pid) {
467 flags = pi->sv_flags;
475 switch (flags & SV_ABI_MASK) {
495 printf("%s%s ", abi, arch);
501 dumpheader(struct ktr_header *kth)
503 static char unknown[64];
504 static struct timeval prevtime, temp;
507 switch (kth->ktr_type) {
549 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
554 * The ktr_tid field was previously the ktr_buffer field, which held
555 * the kernel pointer value for the buffer associated with data
556 * following the record header. It now holds a threadid, but only
557 * for trace files after the change. Older trace files still contain
558 * kernel pointers. Detect this and suppress the results by printing
559 * negative tid's as 0.
562 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
563 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
564 MAXCOMLEN, kth->ktr_comm);
566 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
569 if (timestamp == 3) {
570 if (prevtime.tv_sec == 0)
571 prevtime = kth->ktr_time;
572 timevalsub(&kth->ktr_time, &prevtime);
574 if (timestamp == 2) {
575 temp = kth->ktr_time;
576 timevalsub(&kth->ktr_time, &prevtime);
579 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
580 kth->ktr_time.tv_usec);
585 #include <sys/syscall.h>
587 #include <sys/kern/syscalls.c>
589 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
592 ktrsyscall(struct ktr_syscall *ktr, u_int flags)
594 int narg = ktr->ktr_narg;
598 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
599 (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0))
600 printf("[%d]", ktr->ktr_code);
602 printf("%s", syscallnames[ktr->ktr_code]);
603 ip = &ktr->ktr_args[0];
607 (flags == 0 || (flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
608 switch (ktr->ktr_code) {
625 atfdname(*ip, decimal);
631 switch (ktr->ktr_code) {
633 print_number(ip, narg, c);
635 ioctlname(*ip, decimal);
651 print_number(ip, narg, c);
659 print_number(ip, narg, c);
661 flagsandmodename(ip[0], ip[1], decimal);
666 print_number(ip, narg, c);
667 print_number(ip, narg, c);
676 print_number(ip, narg, c);
684 print_number(ip, narg, c);
691 print_number(ip, narg, c);
692 print_number(ip, narg, c);
694 getfsstatflagsname(*ip);
699 print_number(ip, narg, c);
700 print_number(ip, narg, c);
707 print_number(ip, narg, c);
715 print_number(ip, narg, c);
716 print_number(ip, narg, c);
718 sendrecvflagsname(*ip);
724 print_number(ip, narg, c);
725 print_number(ip, narg, c);
726 print_number(ip, narg, c);
728 sendrecvflagsname(*ip);
735 print_number(ip, narg, c);
742 print_number(ip, narg, c);
761 print_number(ip, narg, c);
762 print_number(ip, narg, c);
768 #ifdef SYS_freebsd6_mmap
769 case SYS_freebsd6_mmap:
770 print_number(ip, narg, c);
771 print_number(ip, narg, c);
783 print_number(ip, narg, c);
784 print_number(ip, narg, c);
795 print_number(ip, narg, c);
796 print_number(ip, narg, c);
803 print_number(ip, narg, c);
804 print_number(ip, narg, c);
806 madvisebehavname(*ip);
810 case SYS_setpriority:
811 print_number(ip, narg, c);
812 print_number(ip, narg, c);
819 print_number(ip, narg, c);
821 fcntlcmdname(ip[0], ip[1], decimal);
829 sockdomainname(sockdomain);
833 socktypenamewithflags(*ip);
836 if (sockdomain == PF_INET ||
837 sockdomain == PF_INET6) {
839 sockipprotoname(*ip);
848 print_number(ip, narg, c);
850 sockoptlevelname(*ip, decimal);
851 if (*ip == SOL_SOCKET) {
860 #ifdef SYS_freebsd6_lseek
861 case SYS_freebsd6_lseek:
862 print_number(ip, narg, c);
863 /* Hidden 'pad' argument, not in lseek(2) */
864 print_number(ip, narg, c);
865 print_number(ip, narg, c);
873 print_number(ip, narg, c);
874 /* Hidden 'pad' argument, not in lseek(2) */
875 print_number(ip, narg, c);
882 print_number(ip, narg, c);
892 print_number(ip, narg, c);
899 print_number(ip, narg, c);
901 shutdownhowname(*ip);
911 socktypenamewithflags(*ip);
925 print_number(ip, narg, c);
947 print_number(ip, narg, c);
948 print_number(ip, narg, c);
955 print_number(ip, narg, c);
956 print_number(ip, narg, c);
963 print_number(ip, narg, c);
970 print_number(ip, narg, c);
971 print_number(ip, narg, c);
978 print_number(ip, narg, c);
985 print_number(ip, narg, c);
986 print_number(ip, narg, c);
1001 lio_listioname(*ip);
1012 case SYS_sched_setscheduler:
1013 print_number(ip, narg, c);
1015 schedpolicyname(*ip);
1019 case SYS_sched_get_priority_max:
1020 case SYS_sched_get_priority_min:
1022 schedpolicyname(*ip);
1027 print_number(ip, narg, c);
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);
1034 sendfileflagsname(*ip);
1039 print_number(ip, narg, c);
1045 case SYS_sigprocmask:
1047 sigprocmaskhowname(*ip);
1052 case SYS___acl_get_file:
1053 case SYS___acl_set_file:
1054 case SYS___acl_get_fd:
1055 case SYS___acl_set_fd:
1056 case SYS___acl_delete_file:
1057 case SYS___acl_delete_fd:
1058 case SYS___acl_aclcheck_file:
1059 case SYS___acl_aclcheck_fd:
1060 case SYS___acl_get_link:
1061 case SYS___acl_set_link:
1062 case SYS___acl_delete_link:
1063 case SYS___acl_aclcheck_link:
1064 print_number(ip, narg, c);
1077 case SYS_extattrctl:
1078 print_number(ip, narg, c);
1080 extattrctlname(*ip);
1085 print_number(ip, narg, c);
1086 print_number(ip, narg, c);
1088 mountflagsname(*ip);
1092 case SYS_thr_create:
1093 print_number(ip, narg, c);
1094 print_number(ip, narg, c);
1096 thrcreateflagsname(*ip);
1101 print_number(ip, narg, c);
1107 case SYS_kldunloadf:
1108 print_number(ip, narg, c);
1110 kldunloadfflagsname(*ip);
1117 print_number(ip, narg, c);
1119 atfdname(*ip, decimal);
1124 case SYS_cap_rights_limit:
1125 print_number(ip, narg, c);
1131 * Hack: the second argument is a
1132 * cap_rights_t, which 64 bits wide, so on
1133 * 32-bit systems, it is split between two
1136 * Since sizeof() is not evaluated by the
1137 * preprocessor, we can't use an #ifdef,
1138 * but the compiler will probably optimize
1139 * the code out anyway.
1141 if (sizeof(cap_rights_t) > sizeof(register_t)) {
1142 #if _BYTE_ORDER == _LITTLE_ENDIAN
1143 arg = ((intmax_t)*ip << 32) + arg;
1145 arg = (arg << 32) + *ip;
1152 case SYS_cap_fcntls_limit:
1153 print_number(ip, narg, c);
1160 case SYS_posix_fadvise:
1161 print_number(ip, narg, c);
1162 print_number(ip, narg, c);
1163 print_number(ip, narg, c);
1165 fadvisebehavname((int)*ip);
1172 print_number(ip, narg, c);
1180 ktrsysret(struct ktr_sysret *ktr, u_int flags)
1182 register_t ret = ktr->ktr_retval;
1183 int error = ktr->ktr_error;
1184 int code = ktr->ktr_code;
1186 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
1187 (code >= nsyscalls || code < 0))
1188 printf("[%d] ", code);
1190 printf("%s ", syscallnames[code]);
1194 printf("%ld", (long)ret);
1195 if (ret < 0 || ret > 9)
1196 printf("/%#lx", (unsigned long)ret);
1199 printf("%ld", (long)ret);
1201 printf("%#lx", (unsigned long)ret);
1203 } else if (error == ERESTART)
1205 else if (error == EJUSTRETURN)
1206 printf("JUSTRETURN");
1208 printf("-1 errno %d", ktr->ktr_error);
1210 printf(" %s", strerror(ktr->ktr_error));
1216 ktrnamei(char *cp, int len)
1218 printf("\"%.*s\"\n", len, cp);
1222 hexdump(char *p, int len, int screenwidth)
1230 i = 13; /* base offset */
1231 i += (width / 2) + 1; /* spaces every second byte */
1232 i += (width * 2); /* width of bytes */
1234 i += width; /* each byte */
1236 } while (i < screenwidth);
1239 for (n = 0; n < len; n += width) {
1240 for (i = n; i < n + width; i++) {
1241 if ((i % width) == 0) { /* beginning of line */
1242 printf(" 0x%04x", i);
1248 printf("%02x", p[i] & 0xff);
1253 for (i = n; i < n + width; i++) {
1256 if (p[i] >= ' ' && p[i] <= '~')
1263 if ((i % width) != 0)
1268 visdump(char *dp, int datalen, int screenwidth)
1277 for (;datalen > 0; datalen--, dp++) {
1278 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1281 * Keep track of printables and
1282 * space chars (like fold(1)).
1294 width = 8 - (col&07);
1299 if (col + width > (screenwidth-2)) {
1314 ktrgenio(struct ktr_genio *ktr, int len)
1316 int datalen = len - sizeof (struct ktr_genio);
1317 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1318 static int screenwidth = 0;
1321 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1322 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1323 datalen == 1 ? "" : "s");
1326 if (screenwidth == 0) {
1329 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1331 screenwidth = ws.ws_col;
1335 if (maxdata && datalen > maxdata)
1338 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1339 if (dp[i] >= 32 && dp[i] < 127)
1341 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1346 hexdump(dp, datalen, screenwidth);
1348 visdump(dp, datalen, screenwidth);
1351 const char *signames[] = {
1352 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
1353 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
1354 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
1355 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
1356 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
1357 "USR2", NULL, /* 31 - 32 */
1361 ktrpsig(struct ktr_psig *psig)
1363 if (psig->signo > 0 && psig->signo < NSIG)
1364 printf("SIG%s ", signames[psig->signo]);
1366 printf("SIG %d ", psig->signo);
1367 if (psig->action == SIG_DFL) {
1368 printf("SIG_DFL code=");
1369 sigcodename(psig->signo, psig->code);
1372 printf("caught handler=0x%lx mask=0x%x code=",
1373 (u_long)psig->action, psig->mask.__bits[0]);
1374 sigcodename(psig->signo, psig->code);
1380 ktrcsw_old(struct ktr_csw_old *cs)
1382 printf("%s %s\n", cs->out ? "stop" : "resume",
1383 cs->user ? "user" : "kernel");
1387 ktrcsw(struct ktr_csw *cs)
1389 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1390 cs->user ? "user" : "kernel", cs->wmesg);
1393 #define UTRACE_DLOPEN_START 1
1394 #define UTRACE_DLOPEN_STOP 2
1395 #define UTRACE_DLCLOSE_START 3
1396 #define UTRACE_DLCLOSE_STOP 4
1397 #define UTRACE_LOAD_OBJECT 5
1398 #define UTRACE_UNLOAD_OBJECT 6
1399 #define UTRACE_ADD_RUNDEP 7
1400 #define UTRACE_PRELOAD_FINISHED 8
1401 #define UTRACE_INIT_CALL 9
1402 #define UTRACE_FINI_CALL 10
1404 struct utrace_rtld {
1405 char sig[4]; /* 'RTLD' */
1411 char name[MAXPATHLEN];
1415 ktruser_rtld(int len, unsigned char *p)
1417 struct utrace_rtld *ut = (struct utrace_rtld *)p;
1421 switch (ut->event) {
1422 case UTRACE_DLOPEN_START:
1424 printf("dlopen(%s, ", ut->name);
1425 switch (mode & RTLD_MODEMASK) {
1430 printf("RTLD_LAZY");
1433 printf("%#x", mode & RTLD_MODEMASK);
1435 if (mode & RTLD_GLOBAL)
1436 printf(" | RTLD_GLOBAL");
1437 if (mode & RTLD_TRACE)
1438 printf(" | RTLD_TRACE");
1439 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
1440 printf(" | %#x", mode &
1441 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
1444 case UTRACE_DLOPEN_STOP:
1445 printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
1448 case UTRACE_DLCLOSE_START:
1449 printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
1452 case UTRACE_DLCLOSE_STOP:
1453 printf("dlclose(%p) finished\n", ut->handle);
1455 case UTRACE_LOAD_OBJECT:
1456 printf("RTLD: loaded %p @ %p - %p (%s)\n", ut->handle,
1457 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1460 case UTRACE_UNLOAD_OBJECT:
1461 printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
1462 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1465 case UTRACE_ADD_RUNDEP:
1466 parent = ut->mapbase;
1467 printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
1468 ut->handle, ut->name, ut->refcnt);
1470 case UTRACE_PRELOAD_FINISHED:
1471 printf("RTLD: LD_PRELOAD finished\n");
1473 case UTRACE_INIT_CALL:
1474 printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
1477 case UTRACE_FINI_CALL:
1478 printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
1484 printf("RTLD: %d ", len);
1487 printf(" %d", *p++);
1489 printf(" %02x", *p++);
1494 struct utrace_malloc {
1501 ktruser_malloc(unsigned char *p)
1503 struct utrace_malloc *ut = (struct utrace_malloc *)p;
1505 if (ut->p == (void *)(intptr_t)(-1))
1506 printf("malloc_init()\n");
1507 else if (ut->s == 0)
1508 printf("free(%p)\n", ut->p);
1509 else if (ut->p == NULL)
1510 printf("%p = malloc(%zu)\n", ut->r, ut->s);
1512 printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
1516 ktruser(int len, unsigned char *p)
1519 if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
1520 ktruser_rtld(len, p);
1524 if (len == sizeof(struct utrace_malloc)) {
1532 printf(" %d", *p++);
1534 printf(" %02x", *p++);
1539 ktrsockaddr(struct sockaddr *sa)
1542 TODO: Support additional address families
1543 #include <netnatm/natm.h>
1544 struct sockaddr_natm *natm;
1545 #include <netsmb/netbios.h>
1546 struct sockaddr_nb *nb;
1551 * note: ktrstruct() has already verified that sa points to a
1552 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1553 * sa->sa_len bytes long.
1555 printf("struct sockaddr { ");
1556 sockfamilyname(sa->sa_family);
1559 #define check_sockaddr_len(n) \
1560 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1561 printf("invalid"); \
1565 switch(sa->sa_family) {
1567 struct sockaddr_in sa_in;
1569 memset(&sa_in, 0, sizeof(sa_in));
1570 memcpy(&sa_in, sa, sa->sa_len);
1571 check_sockaddr_len(in);
1572 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1573 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1577 case AF_APPLETALK: {
1578 struct sockaddr_at sa_at;
1579 struct netrange *nr;
1581 memset(&sa_at, 0, sizeof(sa_at));
1582 memcpy(&sa_at, sa, sa->sa_len);
1583 check_sockaddr_len(at);
1584 nr = &sa_at.sat_range.r_netrange;
1585 printf("%d.%d, %d-%d, %d", ntohs(sa_at.sat_addr.s_net),
1586 sa_at.sat_addr.s_node, ntohs(nr->nr_firstnet),
1587 ntohs(nr->nr_lastnet), nr->nr_phase);
1592 struct sockaddr_in6 sa_in6;
1594 memset(&sa_in6, 0, sizeof(sa_in6));
1595 memcpy(&sa_in6, sa, sa->sa_len);
1596 check_sockaddr_len(in6);
1597 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1598 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1599 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1604 struct sockaddr_ipx sa_ipx;
1606 memset(&sa_ipx, 0, sizeof(sa_ipx));
1607 memcpy(&sa_ipx, sa, sa->sa_len);
1608 check_sockaddr_len(ipx);
1609 /* XXX wish we had ipx_ntop */
1610 printf("%s", ipx_ntoa(sa_ipx.sipx_addr));
1616 struct sockaddr_un sa_un;
1618 memset(&sa_un, 0, sizeof(sa_un));
1619 memcpy(&sa_un, sa, sa->sa_len);
1620 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1624 printf("unknown address family");
1630 ktrstat(struct stat *statp)
1632 char mode[12], timestr[PATH_MAX + 4];
1638 * note: ktrstruct() has already verified that statp points to a
1639 * buffer exactly sizeof(struct stat) bytes long.
1641 printf("struct stat {");
1642 strmode(statp->st_mode, mode);
1643 printf("dev=%ju, ino=%ju, mode=%s, nlink=%ju, ",
1644 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino, mode,
1645 (uintmax_t)statp->st_nlink);
1646 if (resolv == 0 || (pwd = getpwuid(statp->st_uid)) == NULL)
1647 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1649 printf("uid=\"%s\", ", pwd->pw_name);
1650 if (resolv == 0 || (grp = getgrgid(statp->st_gid)) == NULL)
1651 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1653 printf("gid=\"%s\", ", grp->gr_name);
1654 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1657 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1659 tm = localtime(&statp->st_atim.tv_sec);
1660 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1661 printf("\"%s\"", timestr);
1663 if (statp->st_atim.tv_nsec != 0)
1664 printf(".%09ld, ", statp->st_atim.tv_nsec);
1669 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1671 tm = localtime(&statp->st_mtim.tv_sec);
1672 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1673 printf("\"%s\"", timestr);
1675 if (statp->st_mtim.tv_nsec != 0)
1676 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1681 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1683 tm = localtime(&statp->st_ctim.tv_sec);
1684 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1685 printf("\"%s\"", timestr);
1687 if (statp->st_ctim.tv_nsec != 0)
1688 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1691 printf("birthtime=");
1693 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1695 tm = localtime(&statp->st_birthtim.tv_sec);
1696 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1697 printf("\"%s\"", timestr);
1699 if (statp->st_birthtim.tv_nsec != 0)
1700 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1703 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1704 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1705 (intmax_t)statp->st_blocks, statp->st_flags);
1710 ktrstruct(char *buf, size_t buflen)
1713 size_t namelen, datalen;
1716 struct sockaddr_storage ss;
1718 for (name = buf, namelen = 0;
1719 namelen < buflen && name[namelen] != '\0';
1722 if (namelen == buflen)
1724 if (name[namelen] != '\0')
1726 data = buf + namelen + 1;
1727 datalen = buflen - namelen - 1;
1731 for (i = 0; i < (int)namelen; ++i)
1732 if (!isalpha(name[i]))
1734 if (strcmp(name, "stat") == 0) {
1735 if (datalen != sizeof(struct stat))
1737 memcpy(&sb, data, datalen);
1739 } else if (strcmp(name, "sockaddr") == 0) {
1740 if (datalen > sizeof(ss))
1742 memcpy(&ss, data, datalen);
1743 if (datalen != ss.ss_len)
1745 ktrsockaddr((struct sockaddr *)&ss);
1747 printf("unknown structure\n");
1751 printf("invalid record\n");
1755 ktrcapfail(struct ktr_cap_fail *ktr)
1757 switch (ktr->cap_type) {
1758 case CAPFAIL_NOTCAPABLE:
1759 /* operation on fd with insufficient capabilities */
1760 printf("operation requires ");
1761 capname((intmax_t)ktr->cap_needed);
1762 printf(", process holds ");
1763 capname((intmax_t)ktr->cap_held);
1765 case CAPFAIL_INCREASE:
1766 /* requested more capabilities than fd already has */
1767 printf("attempt to increase capabilities from ");
1768 capname((intmax_t)ktr->cap_held);
1770 capname((intmax_t)ktr->cap_needed);
1772 case CAPFAIL_SYSCALL:
1773 /* called restricted syscall */
1774 printf("disallowed system call");
1776 case CAPFAIL_LOOKUP:
1777 /* used ".." in strict-relative mode */
1778 printf("restricted VFS lookup");
1781 printf("unknown capability failure: ");
1782 capname((intmax_t)ktr->cap_needed);
1784 capname((intmax_t)ktr->cap_held);
1791 ktrfault(struct ktr_fault *ktr)
1794 printf("0x%jx ", ktr->vaddr);
1795 vmprotname(ktr->type);
1800 ktrfaultend(struct ktr_faultend *ktr)
1803 vmresultname(ktr->result);
1807 #if defined(__amd64__) || defined(__i386__)
1809 linux_ktrsyscall(struct ktr_syscall *ktr)
1811 int narg = ktr->ktr_narg;
1814 if (ktr->ktr_code >= nlinux_syscalls || ktr->ktr_code < 0)
1815 printf("[%d]", ktr->ktr_code);
1817 printf("%s", linux_syscallnames[ktr->ktr_code]);
1818 ip = &ktr->ktr_args[0];
1822 print_number(ip, narg, c);
1829 linux_ktrsysret(struct ktr_sysret *ktr)
1831 register_t ret = ktr->ktr_retval;
1832 int error = ktr->ktr_error;
1833 int code = ktr->ktr_code;
1835 if (code >= nlinux_syscalls || code < 0)
1836 printf("[%d] ", code);
1838 printf("%s ", linux_syscallnames[code]);
1842 printf("%ld", (long)ret);
1843 if (ret < 0 || ret > 9)
1844 printf("/%#lx", (unsigned long)ret);
1847 printf("%ld", (long)ret);
1849 printf("%#lx", (unsigned long)ret);
1851 } else if (error == ERESTART)
1853 else if (error == EJUSTRETURN)
1854 printf("JUSTRETURN");
1856 if (ktr->ktr_error <= ELAST + 1)
1857 error = abs(bsd_to_linux_errno[ktr->ktr_error]);
1860 printf("-1 errno %d", error);
1862 printf(" %s", strerror(ktr->ktr_error));
1871 fprintf(stderr, "usage: kdump [-dEnlHRrsTA] [-f trfile] "
1872 "[-m maxdata] [-p pid] [-t trstr]\n");