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>
64 #include <sys/types.h>
65 #include <netipx/ipx.h>
68 #include <netatalk/at.h>
70 #include <arpa/inet.h>
71 #include <netinet/in.h>
89 #include "kdump_subr.h"
91 u_int abidump(struct ktr_header *);
92 int fetchprocinfo(struct ktr_header *, u_int *);
93 int fread_tail(void *, int, int);
94 void dumpheader(struct ktr_header *);
95 void ktrsyscall(struct ktr_syscall *, u_int);
96 void ktrsysret(struct ktr_sysret *, u_int);
97 void ktrnamei(char *, int);
98 void hexdump(char *, int, int);
99 void visdump(char *, int, int);
100 void ktrgenio(struct ktr_genio *, int);
101 void ktrpsig(struct ktr_psig *);
102 void ktrcsw(struct ktr_csw *);
103 void ktrcsw_old(struct ktr_csw_old *);
104 void ktruser_malloc(unsigned char *);
105 void ktruser_rtld(int, unsigned char *);
106 void ktruser(int, unsigned char *);
107 void ktrcaprights(cap_rights_t *);
108 void ktrsockaddr(struct sockaddr *);
109 void ktrstat(struct stat *);
110 void ktrstruct(char *, size_t);
111 void ktrcapfail(struct ktr_cap_fail *);
112 void ktrfault(struct ktr_fault *);
113 void ktrfaultend(struct ktr_faultend *);
114 void limitfd(int fd);
116 void ioctlname(unsigned long, int);
118 int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
119 resolv = 0, abiflag = 0, syscallno = 0;
120 const char *tracefile = DEF_TRACEFILE;
121 struct ktr_header ktr_header;
123 #define TIME_FORMAT "%b %e %T %Y"
124 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
126 #define print_number(i,n,c) do { \
128 printf("%c%jd", c, (intmax_t)*i); \
130 printf("%c%#jx", c, (uintmax_t)(u_register_t)*i); \
136 #if defined(__amd64__) || defined(__i386__)
138 void linux_ktrsyscall(struct ktr_syscall *);
139 void linux_ktrsysret(struct ktr_sysret *);
140 extern char *linux_syscallnames[];
141 extern int nlinux_syscalls;
145 * Linux syscalls return negative errno's, we do positive and map them
147 static int bsd_to_linux_errno[ELAST + 1] = {
148 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9,
149 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
150 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
151 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
152 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
153 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
154 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
155 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9,
156 -6, -6, -43, -42, -75,-125, -84, -95, -16, -74,
163 TAILQ_ENTRY(proc_info) info;
168 TAILQ_HEAD(trace_procs, proc_info) trace_procs;
175 * Cache NLS data before entering capability mode.
176 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
178 (void)catopen("libc", NL_CAT_LOCALE);
187 * Allow localtime(3) to cache /etc/localtime content before entering
189 * XXXPJD: There should be localtime_init() in libc.
192 (void)localtime(<ime);
196 main(int argc, char *argv[])
198 int ch, ktrlen, size;
200 int trpoints = ALL_POINTS;
205 setlocale(LC_CTYPE, "");
207 while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1)
222 maxdata = atoi(optarg);
240 timestamp = 3; /* elapsed timestamp */
246 timestamp = 2; /* relative timestamp */
252 trpoints = getpoints(optarg);
254 errx(1, "unknown trace point in %s", optarg);
263 m = malloc(size = 1025);
265 errx(1, "%s", strerror(ENOMEM));
266 if (!freopen(tracefile, "r", stdin))
267 err(1, "%s", tracefile);
273 if (cap_enter() < 0 && errno != ENOSYS)
274 err(1, "unable to enter capability mode");
276 limitfd(STDIN_FILENO);
277 limitfd(STDOUT_FILENO);
278 limitfd(STDERR_FILENO);
280 TAILQ_INIT(&trace_procs);
282 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
283 if (ktr_header.ktr_type & KTR_DROP) {
284 ktr_header.ktr_type &= ~KTR_DROP;
285 if (!drop_logged && threads) {
287 "%6jd %6jd %-8.*s Events dropped.\n",
288 (intmax_t)ktr_header.ktr_pid,
289 ktr_header.ktr_tid > 0 ?
290 (intmax_t)ktr_header.ktr_tid : 0,
291 MAXCOMLEN, ktr_header.ktr_comm);
293 } else if (!drop_logged) {
294 printf("%6jd %-8.*s Events dropped.\n",
295 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
296 ktr_header.ktr_comm);
300 if (trpoints & (1<<ktr_header.ktr_type))
301 if (pid == 0 || ktr_header.ktr_pid == pid ||
302 ktr_header.ktr_tid == pid)
303 dumpheader(&ktr_header);
304 if ((ktrlen = ktr_header.ktr_len) < 0)
305 errx(1, "bogus length 0x%x", ktrlen);
307 m = realloc(m, ktrlen+1);
309 errx(1, "%s", strerror(ENOMEM));
312 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
313 errx(1, "data too short");
314 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
316 sv_flags = abidump(&ktr_header);
317 if (pid && ktr_header.ktr_pid != pid &&
318 ktr_header.ktr_tid != pid)
320 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
323 switch (ktr_header.ktr_type) {
325 #if defined(__amd64__) || defined(__i386__)
326 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
327 linux_ktrsyscall((struct ktr_syscall *)m);
330 ktrsyscall((struct ktr_syscall *)m, sv_flags);
333 #if defined(__amd64__) || defined(__i386__)
334 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
335 linux_ktrsysret((struct ktr_sysret *)m);
338 ktrsysret((struct ktr_sysret *)m, sv_flags);
345 ktrgenio((struct ktr_genio *)m, ktrlen);
348 ktrpsig((struct ktr_psig *)m);
351 if (ktrlen == sizeof(struct ktr_csw_old))
352 ktrcsw_old((struct ktr_csw_old *)m);
354 ktrcsw((struct ktr_csw *)m);
360 ktrstruct(m, ktrlen);
363 ktrcapfail((struct ktr_cap_fail *)m);
366 ktrfault((struct ktr_fault *)m);
369 ktrfaultend((struct ktr_faultend *)m);
387 cap_rights_init(&rights, CAP_FSTAT);
392 cap_rights_set(&rights, CAP_READ);
395 cap_rights_set(&rights, CAP_IOCTL, CAP_WRITE);
396 cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
399 cap_rights_set(&rights, CAP_WRITE);
401 cap_rights_set(&rights, CAP_IOCTL);
409 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
410 err(1, "unable to limit rights for descriptor %d", fd);
411 if (cmd != -1 && cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
412 err(1, "unable to limit ioctls for descriptor %d", fd);
416 fread_tail(void *buf, int size, int num)
420 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
428 fetchprocinfo(struct ktr_header *kth, u_int *flags)
430 struct proc_info *pi;
432 switch (kth->ktr_type) {
434 TAILQ_FOREACH(pi, &trace_procs, info) {
435 if (pi->pid == kth->ktr_pid) {
436 TAILQ_REMOVE(&trace_procs, pi, info);
440 pi = malloc(sizeof(struct proc_info));
442 errx(1, "%s", strerror(ENOMEM));
443 pi->sv_flags = *flags;
444 pi->pid = kth->ktr_pid;
445 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
449 TAILQ_FOREACH(pi, &trace_procs, info) {
450 if (pi->pid == kth->ktr_pid) {
451 TAILQ_REMOVE(&trace_procs, pi, info);
463 abidump(struct ktr_header *kth)
465 struct proc_info *pi;
470 TAILQ_FOREACH(pi, &trace_procs, info) {
471 if (pi->pid == kth->ktr_pid) {
472 flags = pi->sv_flags;
480 switch (flags & SV_ABI_MASK) {
500 printf("%s%s ", abi, arch);
506 dumpheader(struct ktr_header *kth)
508 static char unknown[64];
509 static struct timeval prevtime, temp;
512 switch (kth->ktr_type) {
554 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
559 * The ktr_tid field was previously the ktr_buffer field, which held
560 * the kernel pointer value for the buffer associated with data
561 * following the record header. It now holds a threadid, but only
562 * for trace files after the change. Older trace files still contain
563 * kernel pointers. Detect this and suppress the results by printing
564 * negative tid's as 0.
567 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
568 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
569 MAXCOMLEN, kth->ktr_comm);
571 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
574 if (timestamp == 3) {
575 if (prevtime.tv_sec == 0)
576 prevtime = kth->ktr_time;
577 timevalsub(&kth->ktr_time, &prevtime);
579 if (timestamp == 2) {
580 temp = kth->ktr_time;
581 timevalsub(&kth->ktr_time, &prevtime);
584 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
585 kth->ktr_time.tv_usec);
590 #include <sys/syscall.h>
592 #include <sys/kern/syscalls.c>
594 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
597 ktrsyscall(struct ktr_syscall *ktr, u_int flags)
599 int narg = ktr->ktr_narg;
603 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
604 (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0))
605 printf("[%d]", ktr->ktr_code);
607 printf("%s", syscallnames[ktr->ktr_code]);
609 printf("[%d]", ktr->ktr_code);
611 ip = &ktr->ktr_args[0];
615 (flags == 0 || (flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
616 switch (ktr->ktr_code) {
633 atfdname(*ip, decimal);
639 switch (ktr->ktr_code) {
641 print_number(ip, narg, c);
643 ioctlname(*ip, decimal);
659 print_number(ip, narg, c);
667 print_number(ip, narg, c);
669 flagsandmodename(ip[0], ip[1], decimal);
674 print_number(ip, narg, c);
675 print_number(ip, narg, c);
677 * A flags value of zero is valid for
678 * wait4() but not for wait6(), so
679 * handle zero special here.
682 print_number(ip, narg, c);
692 idtypename(*ip, decimal);
696 print_number(ip, narg, c);
697 print_number(ip, narg, c);
706 print_number(ip, narg, c);
714 print_number(ip, narg, c);
721 print_number(ip, narg, c);
722 print_number(ip, narg, c);
724 getfsstatflagsname(*ip);
729 print_number(ip, narg, c);
730 print_number(ip, narg, c);
737 print_number(ip, narg, c);
745 print_number(ip, narg, c);
746 print_number(ip, narg, c);
748 sendrecvflagsname(*ip);
754 print_number(ip, narg, c);
755 print_number(ip, narg, c);
756 print_number(ip, narg, c);
758 sendrecvflagsname(*ip);
765 print_number(ip, narg, c);
772 print_number(ip, narg, c);
791 print_number(ip, narg, c);
792 print_number(ip, narg, c);
798 #ifdef SYS_freebsd6_mmap
799 case SYS_freebsd6_mmap:
800 print_number(ip, narg, c);
801 print_number(ip, narg, c);
813 print_number(ip, narg, c);
814 print_number(ip, narg, c);
825 print_number(ip, narg, c);
826 print_number(ip, narg, c);
833 print_number(ip, narg, c);
834 print_number(ip, narg, c);
836 madvisebehavname(*ip);
840 case SYS_setpriority:
841 print_number(ip, narg, c);
842 print_number(ip, narg, c);
849 print_number(ip, narg, c);
851 fcntlcmdname(ip[0], ip[1], decimal);
859 sockdomainname(sockdomain);
863 socktypenamewithflags(*ip);
866 if (sockdomain == PF_INET ||
867 sockdomain == PF_INET6) {
869 sockipprotoname(*ip);
878 print_number(ip, narg, c);
880 sockoptlevelname(*ip, decimal);
881 if (*ip == SOL_SOCKET) {
890 #ifdef SYS_freebsd6_lseek
891 case SYS_freebsd6_lseek:
892 print_number(ip, narg, c);
893 /* Hidden 'pad' argument, not in lseek(2) */
894 print_number(ip, narg, c);
895 print_number(ip, narg, c);
903 print_number(ip, narg, c);
904 /* Hidden 'pad' argument, not in lseek(2) */
905 print_number(ip, narg, c);
912 print_number(ip, narg, c);
922 print_number(ip, narg, c);
929 print_number(ip, narg, c);
931 shutdownhowname(*ip);
941 socktypenamewithflags(*ip);
955 print_number(ip, narg, c);
977 print_number(ip, narg, c);
978 print_number(ip, narg, c);
985 print_number(ip, narg, c);
986 print_number(ip, narg, c);
993 print_number(ip, narg, c);
1000 print_number(ip, narg, c);
1001 print_number(ip, narg, c);
1008 print_number(ip, narg, c);
1015 print_number(ip, narg, c);
1018 printf(",0%o", ip[1]);
1023 print_number(ip, narg, c);
1024 print_number(ip, narg, c);
1037 case SYS_lio_listio:
1039 lio_listioname(*ip);
1050 case SYS_sched_setscheduler:
1051 print_number(ip, narg, c);
1053 schedpolicyname(*ip);
1057 case SYS_sched_get_priority_max:
1058 case SYS_sched_get_priority_min:
1060 schedpolicyname(*ip);
1065 print_number(ip, narg, c);
1066 print_number(ip, narg, c);
1067 print_number(ip, narg, c);
1068 print_number(ip, narg, c);
1069 print_number(ip, narg, c);
1070 print_number(ip, narg, c);
1072 sendfileflagsname(*ip);
1077 print_number(ip, narg, c);
1083 case SYS_sigprocmask:
1085 sigprocmaskhowname(*ip);
1090 case SYS___acl_get_file:
1091 case SYS___acl_set_file:
1092 case SYS___acl_get_fd:
1093 case SYS___acl_set_fd:
1094 case SYS___acl_delete_file:
1095 case SYS___acl_delete_fd:
1096 case SYS___acl_aclcheck_file:
1097 case SYS___acl_aclcheck_fd:
1098 case SYS___acl_get_link:
1099 case SYS___acl_set_link:
1100 case SYS___acl_delete_link:
1101 case SYS___acl_aclcheck_link:
1102 print_number(ip, narg, c);
1115 case SYS_extattrctl:
1116 print_number(ip, narg, c);
1118 extattrctlname(*ip);
1123 print_number(ip, narg, c);
1124 print_number(ip, narg, c);
1126 mountflagsname(*ip);
1130 case SYS_thr_create:
1131 print_number(ip, narg, c);
1132 print_number(ip, narg, c);
1134 thrcreateflagsname(*ip);
1139 print_number(ip, narg, c);
1145 case SYS_kldunloadf:
1146 print_number(ip, narg, c);
1148 kldunloadfflagsname(*ip);
1155 print_number(ip, narg, c);
1157 atfdname(*ip, decimal);
1161 case SYS_cap_fcntls_limit:
1162 print_number(ip, narg, c);
1169 case SYS_posix_fadvise:
1170 print_number(ip, narg, c);
1171 print_number(ip, narg, c);
1172 print_number(ip, narg, c);
1174 fadvisebehavname((int)*ip);
1180 idtypename(*ip, decimal);
1184 print_number(ip, narg, c);
1186 procctlcmdname(*ip);
1193 print_number(ip, narg, c);
1201 ktrsysret(struct ktr_sysret *ktr, u_int flags)
1203 register_t ret = ktr->ktr_retval;
1204 int error = ktr->ktr_error;
1205 int code = ktr->ktr_code;
1207 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
1208 (code >= nsyscalls || code < 0))
1209 printf("[%d] ", code);
1211 printf("%s", syscallnames[code]);
1213 printf("[%d]", code);
1219 printf("%ld", (long)ret);
1220 if (ret < 0 || ret > 9)
1221 printf("/%#lx", (unsigned long)ret);
1224 printf("%ld", (long)ret);
1226 printf("%#lx", (unsigned long)ret);
1228 } else if (error == ERESTART)
1230 else if (error == EJUSTRETURN)
1231 printf("JUSTRETURN");
1233 printf("-1 errno %d", ktr->ktr_error);
1235 printf(" %s", strerror(ktr->ktr_error));
1241 ktrnamei(char *cp, int len)
1243 printf("\"%.*s\"\n", len, cp);
1247 hexdump(char *p, int len, int screenwidth)
1255 i = 13; /* base offset */
1256 i += (width / 2) + 1; /* spaces every second byte */
1257 i += (width * 2); /* width of bytes */
1259 i += width; /* each byte */
1261 } while (i < screenwidth);
1264 for (n = 0; n < len; n += width) {
1265 for (i = n; i < n + width; i++) {
1266 if ((i % width) == 0) { /* beginning of line */
1267 printf(" 0x%04x", i);
1273 printf("%02x", p[i] & 0xff);
1278 for (i = n; i < n + width; i++) {
1281 if (p[i] >= ' ' && p[i] <= '~')
1288 if ((i % width) != 0)
1293 visdump(char *dp, int datalen, int screenwidth)
1302 for (;datalen > 0; datalen--, dp++) {
1303 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1306 * Keep track of printables and
1307 * space chars (like fold(1)).
1319 width = 8 - (col&07);
1324 if (col + width > (screenwidth-2)) {
1339 ktrgenio(struct ktr_genio *ktr, int len)
1341 int datalen = len - sizeof (struct ktr_genio);
1342 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1343 static int screenwidth = 0;
1346 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1347 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1348 datalen == 1 ? "" : "s");
1351 if (screenwidth == 0) {
1354 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1356 screenwidth = ws.ws_col;
1360 if (maxdata && datalen > maxdata)
1363 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1364 if (dp[i] >= 32 && dp[i] < 127)
1366 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1371 hexdump(dp, datalen, screenwidth);
1373 visdump(dp, datalen, screenwidth);
1376 const char *signames[] = {
1377 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
1378 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
1379 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
1380 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
1381 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
1382 "USR2", NULL, /* 31 - 32 */
1386 ktrpsig(struct ktr_psig *psig)
1388 if (psig->signo > 0 && psig->signo < NSIG)
1389 printf("SIG%s ", signames[psig->signo]);
1391 printf("SIG %d ", psig->signo);
1392 if (psig->action == SIG_DFL) {
1393 printf("SIG_DFL code=");
1394 sigcodename(psig->signo, psig->code);
1397 printf("caught handler=0x%lx mask=0x%x code=",
1398 (u_long)psig->action, psig->mask.__bits[0]);
1399 sigcodename(psig->signo, psig->code);
1405 ktrcsw_old(struct ktr_csw_old *cs)
1407 printf("%s %s\n", cs->out ? "stop" : "resume",
1408 cs->user ? "user" : "kernel");
1412 ktrcsw(struct ktr_csw *cs)
1414 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1415 cs->user ? "user" : "kernel", cs->wmesg);
1418 #define UTRACE_DLOPEN_START 1
1419 #define UTRACE_DLOPEN_STOP 2
1420 #define UTRACE_DLCLOSE_START 3
1421 #define UTRACE_DLCLOSE_STOP 4
1422 #define UTRACE_LOAD_OBJECT 5
1423 #define UTRACE_UNLOAD_OBJECT 6
1424 #define UTRACE_ADD_RUNDEP 7
1425 #define UTRACE_PRELOAD_FINISHED 8
1426 #define UTRACE_INIT_CALL 9
1427 #define UTRACE_FINI_CALL 10
1429 struct utrace_rtld {
1430 char sig[4]; /* 'RTLD' */
1436 char name[MAXPATHLEN];
1440 ktruser_rtld(int len, unsigned char *p)
1442 struct utrace_rtld *ut = (struct utrace_rtld *)p;
1446 switch (ut->event) {
1447 case UTRACE_DLOPEN_START:
1449 printf("dlopen(%s, ", ut->name);
1450 switch (mode & RTLD_MODEMASK) {
1455 printf("RTLD_LAZY");
1458 printf("%#x", mode & RTLD_MODEMASK);
1460 if (mode & RTLD_GLOBAL)
1461 printf(" | RTLD_GLOBAL");
1462 if (mode & RTLD_TRACE)
1463 printf(" | RTLD_TRACE");
1464 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
1465 printf(" | %#x", mode &
1466 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
1469 case UTRACE_DLOPEN_STOP:
1470 printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
1473 case UTRACE_DLCLOSE_START:
1474 printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
1477 case UTRACE_DLCLOSE_STOP:
1478 printf("dlclose(%p) finished\n", ut->handle);
1480 case UTRACE_LOAD_OBJECT:
1481 printf("RTLD: loaded %p @ %p - %p (%s)\n", ut->handle,
1482 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1485 case UTRACE_UNLOAD_OBJECT:
1486 printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
1487 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1490 case UTRACE_ADD_RUNDEP:
1491 parent = ut->mapbase;
1492 printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
1493 ut->handle, ut->name, ut->refcnt);
1495 case UTRACE_PRELOAD_FINISHED:
1496 printf("RTLD: LD_PRELOAD finished\n");
1498 case UTRACE_INIT_CALL:
1499 printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
1502 case UTRACE_FINI_CALL:
1503 printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
1509 printf("RTLD: %d ", len);
1512 printf(" %d", *p++);
1514 printf(" %02x", *p++);
1519 struct utrace_malloc {
1526 ktruser_malloc(unsigned char *p)
1528 struct utrace_malloc *ut = (struct utrace_malloc *)p;
1530 if (ut->p == (void *)(intptr_t)(-1))
1531 printf("malloc_init()\n");
1532 else if (ut->s == 0)
1533 printf("free(%p)\n", ut->p);
1534 else if (ut->p == NULL)
1535 printf("%p = malloc(%zu)\n", ut->r, ut->s);
1537 printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
1541 ktruser(int len, unsigned char *p)
1544 if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
1545 ktruser_rtld(len, p);
1549 if (len == sizeof(struct utrace_malloc)) {
1557 printf(" %d", *p++);
1559 printf(" %02x", *p++);
1564 ktrcaprights(cap_rights_t *rightsp)
1567 printf("cap_rights_t ");
1573 ktrsockaddr(struct sockaddr *sa)
1576 TODO: Support additional address families
1577 #include <netnatm/natm.h>
1578 struct sockaddr_natm *natm;
1579 #include <netsmb/netbios.h>
1580 struct sockaddr_nb *nb;
1585 * note: ktrstruct() has already verified that sa points to a
1586 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1587 * sa->sa_len bytes long.
1589 printf("struct sockaddr { ");
1590 sockfamilyname(sa->sa_family);
1593 #define check_sockaddr_len(n) \
1594 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1595 printf("invalid"); \
1599 switch(sa->sa_family) {
1601 struct sockaddr_in sa_in;
1603 memset(&sa_in, 0, sizeof(sa_in));
1604 memcpy(&sa_in, sa, sa->sa_len);
1605 check_sockaddr_len(in);
1606 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1607 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1611 case AF_APPLETALK: {
1612 struct sockaddr_at sa_at;
1613 struct netrange *nr;
1615 memset(&sa_at, 0, sizeof(sa_at));
1616 memcpy(&sa_at, sa, sa->sa_len);
1617 check_sockaddr_len(at);
1618 nr = &sa_at.sat_range.r_netrange;
1619 printf("%d.%d, %d-%d, %d", ntohs(sa_at.sat_addr.s_net),
1620 sa_at.sat_addr.s_node, ntohs(nr->nr_firstnet),
1621 ntohs(nr->nr_lastnet), nr->nr_phase);
1626 struct sockaddr_in6 sa_in6;
1628 memset(&sa_in6, 0, sizeof(sa_in6));
1629 memcpy(&sa_in6, sa, sa->sa_len);
1630 check_sockaddr_len(in6);
1631 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1632 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1633 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1638 struct sockaddr_ipx sa_ipx;
1640 memset(&sa_ipx, 0, sizeof(sa_ipx));
1641 memcpy(&sa_ipx, sa, sa->sa_len);
1642 check_sockaddr_len(ipx);
1643 /* XXX wish we had ipx_ntop */
1644 printf("%s", ipx_ntoa(sa_ipx.sipx_addr));
1650 struct sockaddr_un sa_un;
1652 memset(&sa_un, 0, sizeof(sa_un));
1653 memcpy(&sa_un, sa, sa->sa_len);
1654 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1658 printf("unknown address family");
1664 ktrstat(struct stat *statp)
1666 char mode[12], timestr[PATH_MAX + 4];
1672 * note: ktrstruct() has already verified that statp points to a
1673 * buffer exactly sizeof(struct stat) bytes long.
1675 printf("struct stat {");
1676 printf("dev=%ju, ino=%ju, ",
1677 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1679 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1681 strmode(statp->st_mode, mode);
1682 printf("mode=%s, ", mode);
1684 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1685 if (resolv == 0 || (pwd = getpwuid(statp->st_uid)) == NULL)
1686 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1688 printf("uid=\"%s\", ", pwd->pw_name);
1689 if (resolv == 0 || (grp = getgrgid(statp->st_gid)) == NULL)
1690 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1692 printf("gid=\"%s\", ", grp->gr_name);
1693 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1696 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1698 tm = localtime(&statp->st_atim.tv_sec);
1699 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1700 printf("\"%s\"", timestr);
1702 if (statp->st_atim.tv_nsec != 0)
1703 printf(".%09ld, ", statp->st_atim.tv_nsec);
1708 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1710 tm = localtime(&statp->st_mtim.tv_sec);
1711 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1712 printf("\"%s\"", timestr);
1714 if (statp->st_mtim.tv_nsec != 0)
1715 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1720 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1722 tm = localtime(&statp->st_ctim.tv_sec);
1723 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1724 printf("\"%s\"", timestr);
1726 if (statp->st_ctim.tv_nsec != 0)
1727 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1730 printf("birthtime=");
1732 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1734 tm = localtime(&statp->st_birthtim.tv_sec);
1735 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1736 printf("\"%s\"", timestr);
1738 if (statp->st_birthtim.tv_nsec != 0)
1739 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1742 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1743 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1744 (intmax_t)statp->st_blocks, statp->st_flags);
1749 ktrstruct(char *buf, size_t buflen)
1752 size_t namelen, datalen;
1754 cap_rights_t rights;
1756 struct sockaddr_storage ss;
1758 for (name = buf, namelen = 0;
1759 namelen < buflen && name[namelen] != '\0';
1762 if (namelen == buflen)
1764 if (name[namelen] != '\0')
1766 data = buf + namelen + 1;
1767 datalen = buflen - namelen - 1;
1771 for (i = 0; i < (int)namelen; ++i)
1772 if (!isalpha(name[i]))
1774 if (strcmp(name, "caprights") == 0) {
1775 if (datalen != sizeof(cap_rights_t))
1777 memcpy(&rights, data, datalen);
1778 ktrcaprights(&rights);
1779 } else if (strcmp(name, "stat") == 0) {
1780 if (datalen != sizeof(struct stat))
1782 memcpy(&sb, data, datalen);
1784 } else if (strcmp(name, "sockaddr") == 0) {
1785 if (datalen > sizeof(ss))
1787 memcpy(&ss, data, datalen);
1788 if (datalen != ss.ss_len)
1790 ktrsockaddr((struct sockaddr *)&ss);
1792 printf("unknown structure\n");
1796 printf("invalid record\n");
1800 ktrcapfail(struct ktr_cap_fail *ktr)
1802 switch (ktr->cap_type) {
1803 case CAPFAIL_NOTCAPABLE:
1804 /* operation on fd with insufficient capabilities */
1805 printf("operation requires ");
1806 capname(&ktr->cap_needed);
1807 printf(", process holds ");
1808 capname(&ktr->cap_held);
1810 case CAPFAIL_INCREASE:
1811 /* requested more capabilities than fd already has */
1812 printf("attempt to increase capabilities from ");
1813 capname(&ktr->cap_held);
1815 capname(&ktr->cap_needed);
1817 case CAPFAIL_SYSCALL:
1818 /* called restricted syscall */
1819 printf("disallowed system call");
1821 case CAPFAIL_LOOKUP:
1822 /* used ".." in strict-relative mode */
1823 printf("restricted VFS lookup");
1826 printf("unknown capability failure: ");
1827 capname(&ktr->cap_needed);
1829 capname(&ktr->cap_held);
1836 ktrfault(struct ktr_fault *ktr)
1839 printf("0x%jx ", ktr->vaddr);
1840 vmprotname(ktr->type);
1845 ktrfaultend(struct ktr_faultend *ktr)
1848 vmresultname(ktr->result);
1852 #if defined(__amd64__) || defined(__i386__)
1854 linux_ktrsyscall(struct ktr_syscall *ktr)
1856 int narg = ktr->ktr_narg;
1859 if (ktr->ktr_code >= nlinux_syscalls || ktr->ktr_code < 0)
1860 printf("[%d]", ktr->ktr_code);
1862 printf("%s", linux_syscallnames[ktr->ktr_code]);
1864 printf("[%d]", ktr->ktr_code);
1866 ip = &ktr->ktr_args[0];
1870 print_number(ip, narg, c);
1877 linux_ktrsysret(struct ktr_sysret *ktr)
1879 register_t ret = ktr->ktr_retval;
1880 int error = ktr->ktr_error;
1881 int code = ktr->ktr_code;
1883 if (code >= nlinux_syscalls || code < 0)
1884 printf("[%d] ", code);
1886 printf("%s", linux_syscallnames[code]);
1888 printf("[%d]", code);
1894 printf("%ld", (long)ret);
1895 if (ret < 0 || ret > 9)
1896 printf("/%#lx", (unsigned long)ret);
1899 printf("%ld", (long)ret);
1901 printf("%#lx", (unsigned long)ret);
1903 } else if (error == ERESTART)
1905 else if (error == EJUSTRETURN)
1906 printf("JUSTRETURN");
1908 if (ktr->ktr_error <= ELAST + 1)
1909 error = abs(bsd_to_linux_errno[ktr->ktr_error]);
1912 printf("-1 errno %d", error);
1914 printf(" %s", strerror(ktr->ktr_error));
1923 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
1924 "[-m maxdata] [-p pid] [-t trstr]\n");