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>
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(unsigned char *);
106 void ktruser_rtld(int, unsigned char *);
107 void ktruser(int, unsigned char *);
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 int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
120 resolv = 0, abiflag = 0, syscallno = 0;
121 const char *tracefile = DEF_TRACEFILE;
122 struct ktr_header ktr_header;
124 #define TIME_FORMAT "%b %e %T %Y"
125 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
127 #define print_number(i,n,c) do { \
129 printf("%c%jd", c, (intmax_t)*i); \
131 printf("%c%#jx", c, (uintmax_t)(u_register_t)*i); \
137 #if defined(__amd64__) || defined(__i386__)
139 void linux_ktrsyscall(struct ktr_syscall *);
140 void linux_ktrsysret(struct ktr_sysret *);
141 extern char *linux_syscallnames[];
142 extern int nlinux_syscalls;
146 * Linux syscalls return negative errno's, we do positive and map them
148 static int bsd_to_linux_errno[ELAST + 1] = {
149 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9,
150 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
151 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
152 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
153 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
154 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
155 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
156 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9,
157 -6, -6, -43, -42, -75,-125, -84, -95, -16, -74,
164 TAILQ_ENTRY(proc_info) info;
169 TAILQ_HEAD(trace_procs, proc_info) trace_procs;
176 * Cache NLS data before entering capability mode.
177 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
179 (void)catopen("libc", NL_CAT_LOCALE);
188 * Allow localtime(3) to cache /etc/localtime content before entering
190 * XXXPJD: There should be localtime_init() in libc.
193 (void)localtime(<ime);
197 main(int argc, char *argv[])
199 int ch, ktrlen, size;
201 int trpoints = ALL_POINTS;
206 setlocale(LC_CTYPE, "");
208 while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1)
223 maxdata = atoi(optarg);
241 timestamp = 3; /* elapsed timestamp */
247 timestamp = 2; /* relative timestamp */
253 trpoints = getpoints(optarg);
255 errx(1, "unknown trace point in %s", optarg);
264 m = malloc(size = 1025);
266 errx(1, "%s", strerror(ENOMEM));
267 if (!freopen(tracefile, "r", stdin))
268 err(1, "%s", tracefile);
274 if (cap_enter() < 0 && errno != ENOSYS)
275 err(1, "unable to enter capability mode");
277 limitfd(STDIN_FILENO);
278 limitfd(STDOUT_FILENO);
279 limitfd(STDERR_FILENO);
281 TAILQ_INIT(&trace_procs);
283 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
284 if (ktr_header.ktr_type & KTR_DROP) {
285 ktr_header.ktr_type &= ~KTR_DROP;
286 if (!drop_logged && threads) {
288 "%6jd %6jd %-8.*s Events dropped.\n",
289 (intmax_t)ktr_header.ktr_pid,
290 ktr_header.ktr_tid > 0 ?
291 (intmax_t)ktr_header.ktr_tid : 0,
292 MAXCOMLEN, ktr_header.ktr_comm);
294 } else if (!drop_logged) {
295 printf("%6jd %-8.*s Events dropped.\n",
296 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
297 ktr_header.ktr_comm);
301 if (trpoints & (1<<ktr_header.ktr_type))
302 if (pid == 0 || ktr_header.ktr_pid == pid ||
303 ktr_header.ktr_tid == pid)
304 dumpheader(&ktr_header);
305 if ((ktrlen = ktr_header.ktr_len) < 0)
306 errx(1, "bogus length 0x%x", ktrlen);
308 m = realloc(m, ktrlen+1);
310 errx(1, "%s", strerror(ENOMEM));
313 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
314 errx(1, "data too short");
315 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
317 sv_flags = abidump(&ktr_header);
318 if (pid && ktr_header.ktr_pid != pid &&
319 ktr_header.ktr_tid != pid)
321 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
324 switch (ktr_header.ktr_type) {
326 #if defined(__amd64__) || defined(__i386__)
327 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
328 linux_ktrsyscall((struct ktr_syscall *)m);
331 ktrsyscall((struct ktr_syscall *)m, sv_flags);
334 #if defined(__amd64__) || defined(__i386__)
335 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
336 linux_ktrsysret((struct ktr_sysret *)m);
339 ktrsysret((struct ktr_sysret *)m, sv_flags);
346 ktrgenio((struct ktr_genio *)m, ktrlen);
349 ktrpsig((struct ktr_psig *)m);
352 if (ktrlen == sizeof(struct ktr_csw_old))
353 ktrcsw_old((struct ktr_csw_old *)m);
355 ktrcsw((struct ktr_csw *)m);
361 ktrstruct(m, ktrlen);
364 ktrcapfail((struct ktr_cap_fail *)m);
367 ktrfault((struct ktr_fault *)m);
370 ktrfaultend((struct ktr_faultend *)m);
388 cap_rights_init(&rights, CAP_FSTAT);
393 cap_rights_set(&rights, CAP_READ);
396 cap_rights_set(&rights, CAP_IOCTL, CAP_WRITE);
397 cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
400 cap_rights_set(&rights, CAP_WRITE);
402 cap_rights_set(&rights, CAP_IOCTL);
410 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
411 err(1, "unable to limit rights for descriptor %d", fd);
412 if (cmd != -1 && cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
413 err(1, "unable to limit ioctls for descriptor %d", fd);
417 fread_tail(void *buf, int size, int num)
421 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
429 fetchprocinfo(struct ktr_header *kth, u_int *flags)
431 struct proc_info *pi;
433 switch (kth->ktr_type) {
435 TAILQ_FOREACH(pi, &trace_procs, info) {
436 if (pi->pid == kth->ktr_pid) {
437 TAILQ_REMOVE(&trace_procs, pi, info);
441 pi = malloc(sizeof(struct proc_info));
443 errx(1, "%s", strerror(ENOMEM));
444 pi->sv_flags = *flags;
445 pi->pid = kth->ktr_pid;
446 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
450 TAILQ_FOREACH(pi, &trace_procs, info) {
451 if (pi->pid == kth->ktr_pid) {
452 TAILQ_REMOVE(&trace_procs, pi, info);
464 abidump(struct ktr_header *kth)
466 struct proc_info *pi;
471 TAILQ_FOREACH(pi, &trace_procs, info) {
472 if (pi->pid == kth->ktr_pid) {
473 flags = pi->sv_flags;
481 switch (flags & SV_ABI_MASK) {
501 printf("%s%s ", abi, arch);
507 dumpheader(struct ktr_header *kth)
509 static char unknown[64];
510 static struct timeval prevtime, temp;
513 switch (kth->ktr_type) {
555 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
560 * The ktr_tid field was previously the ktr_buffer field, which held
561 * the kernel pointer value for the buffer associated with data
562 * following the record header. It now holds a threadid, but only
563 * for trace files after the change. Older trace files still contain
564 * kernel pointers. Detect this and suppress the results by printing
565 * negative tid's as 0.
568 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
569 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
570 MAXCOMLEN, kth->ktr_comm);
572 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
575 if (timestamp == 3) {
576 if (prevtime.tv_sec == 0)
577 prevtime = kth->ktr_time;
578 timevalsub(&kth->ktr_time, &prevtime);
580 if (timestamp == 2) {
581 temp = kth->ktr_time;
582 timevalsub(&kth->ktr_time, &prevtime);
585 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
586 kth->ktr_time.tv_usec);
591 #include <sys/syscall.h>
593 #include <sys/kern/syscalls.c>
595 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
598 ktrsyscall(struct ktr_syscall *ktr, u_int flags)
600 int narg = ktr->ktr_narg;
604 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
605 (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0))
606 printf("[%d]", ktr->ktr_code);
608 printf("%s", syscallnames[ktr->ktr_code]);
610 printf("[%d]", ktr->ktr_code);
612 ip = &ktr->ktr_args[0];
616 (flags == 0 || (flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
617 switch (ktr->ktr_code) {
634 atfdname(*ip, decimal);
640 switch (ktr->ktr_code) {
642 print_number(ip, narg, c);
644 ioctlname(*ip, decimal);
660 print_number(ip, narg, c);
668 print_number(ip, narg, c);
670 flagsandmodename(ip[0], ip[1], decimal);
675 print_number(ip, narg, c);
676 print_number(ip, narg, c);
678 * A flags value of zero is valid for
679 * wait4() but not for wait6(), so
680 * handle zero special here.
683 print_number(ip, narg, c);
693 idtypename(*ip, decimal);
697 print_number(ip, narg, c);
698 print_number(ip, narg, c);
707 print_number(ip, narg, c);
715 print_number(ip, narg, c);
722 print_number(ip, narg, c);
723 print_number(ip, narg, c);
725 getfsstatflagsname(*ip);
730 print_number(ip, narg, c);
731 print_number(ip, narg, c);
738 print_number(ip, narg, c);
746 print_number(ip, narg, c);
747 print_number(ip, narg, c);
749 sendrecvflagsname(*ip);
755 print_number(ip, narg, c);
756 print_number(ip, narg, c);
757 print_number(ip, narg, c);
759 sendrecvflagsname(*ip);
766 print_number(ip, narg, c);
773 print_number(ip, narg, c);
792 print_number(ip, narg, c);
793 print_number(ip, narg, c);
799 #ifdef SYS_freebsd6_mmap
800 case SYS_freebsd6_mmap:
801 print_number(ip, narg, c);
802 print_number(ip, narg, c);
814 print_number(ip, narg, c);
815 print_number(ip, narg, c);
826 print_number(ip, narg, c);
827 print_number(ip, narg, c);
834 print_number(ip, narg, c);
835 print_number(ip, narg, c);
837 madvisebehavname(*ip);
841 case SYS_setpriority:
842 print_number(ip, narg, c);
843 print_number(ip, narg, c);
850 print_number(ip, narg, c);
852 fcntlcmdname(ip[0], ip[1], decimal);
860 sockdomainname(sockdomain);
864 socktypenamewithflags(*ip);
867 if (sockdomain == PF_INET ||
868 sockdomain == PF_INET6) {
870 sockipprotoname(*ip);
879 print_number(ip, narg, c);
881 sockoptlevelname(*ip, decimal);
882 if (*ip == SOL_SOCKET) {
891 #ifdef SYS_freebsd6_lseek
892 case SYS_freebsd6_lseek:
893 print_number(ip, narg, c);
894 /* Hidden 'pad' argument, not in lseek(2) */
895 print_number(ip, narg, c);
896 print_number(ip, narg, c);
904 print_number(ip, narg, c);
905 /* Hidden 'pad' argument, not in lseek(2) */
906 print_number(ip, narg, c);
913 print_number(ip, narg, c);
923 print_number(ip, narg, c);
930 print_number(ip, narg, c);
932 shutdownhowname(*ip);
942 socktypenamewithflags(*ip);
956 print_number(ip, narg, c);
978 print_number(ip, narg, c);
979 print_number(ip, narg, c);
986 print_number(ip, narg, c);
987 print_number(ip, narg, c);
994 print_number(ip, narg, c);
1001 print_number(ip, narg, c);
1002 print_number(ip, narg, c);
1009 print_number(ip, narg, c);
1016 print_number(ip, narg, c);
1019 printf(",0%o", ip[1]);
1024 print_number(ip, narg, c);
1025 print_number(ip, narg, c);
1038 case SYS_lio_listio:
1040 lio_listioname(*ip);
1051 case SYS_sched_setscheduler:
1052 print_number(ip, narg, c);
1054 schedpolicyname(*ip);
1058 case SYS_sched_get_priority_max:
1059 case SYS_sched_get_priority_min:
1061 schedpolicyname(*ip);
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);
1071 print_number(ip, narg, c);
1073 sendfileflagsname(*ip);
1078 print_number(ip, narg, c);
1084 case SYS_sigprocmask:
1086 sigprocmaskhowname(*ip);
1091 case SYS___acl_get_file:
1092 case SYS___acl_set_file:
1093 case SYS___acl_get_fd:
1094 case SYS___acl_set_fd:
1095 case SYS___acl_delete_file:
1096 case SYS___acl_delete_fd:
1097 case SYS___acl_aclcheck_file:
1098 case SYS___acl_aclcheck_fd:
1099 case SYS___acl_get_link:
1100 case SYS___acl_set_link:
1101 case SYS___acl_delete_link:
1102 case SYS___acl_aclcheck_link:
1103 print_number(ip, narg, c);
1116 case SYS_extattrctl:
1117 print_number(ip, narg, c);
1119 extattrctlname(*ip);
1124 print_number(ip, narg, c);
1125 print_number(ip, narg, c);
1127 mountflagsname(*ip);
1131 case SYS_thr_create:
1132 print_number(ip, narg, c);
1133 print_number(ip, narg, c);
1135 thrcreateflagsname(*ip);
1140 print_number(ip, narg, c);
1146 case SYS_kldunloadf:
1147 print_number(ip, narg, c);
1149 kldunloadfflagsname(*ip);
1156 print_number(ip, narg, c);
1158 atfdname(*ip, decimal);
1162 case SYS_cap_fcntls_limit:
1163 print_number(ip, narg, c);
1170 case SYS_posix_fadvise:
1171 print_number(ip, narg, c);
1172 print_number(ip, narg, c);
1173 print_number(ip, narg, c);
1175 fadvisebehavname((int)*ip);
1181 idtypename(*ip, decimal);
1185 print_number(ip, narg, c);
1187 procctlcmdname(*ip);
1192 print_number(ip, narg, c);
1196 case UMTX_OP_CV_WAIT:
1200 umtxcvwaitflags(*ip);
1202 case UMTX_OP_RW_RDLOCK:
1206 umtxrwlockflags(*ip);
1214 print_number(ip, narg, c);
1222 ktrsysret(struct ktr_sysret *ktr, u_int flags)
1224 register_t ret = ktr->ktr_retval;
1225 int error = ktr->ktr_error;
1226 int code = ktr->ktr_code;
1228 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
1229 (code >= nsyscalls || code < 0))
1230 printf("[%d] ", code);
1232 printf("%s", syscallnames[code]);
1234 printf("[%d]", code);
1240 printf("%ld", (long)ret);
1241 if (ret < 0 || ret > 9)
1242 printf("/%#lx", (unsigned long)ret);
1245 printf("%ld", (long)ret);
1247 printf("%#lx", (unsigned long)ret);
1249 } else if (error == ERESTART)
1251 else if (error == EJUSTRETURN)
1252 printf("JUSTRETURN");
1254 printf("-1 errno %d", ktr->ktr_error);
1256 printf(" %s", strerror(ktr->ktr_error));
1262 ktrnamei(char *cp, int len)
1264 printf("\"%.*s\"\n", len, cp);
1268 hexdump(char *p, int len, int screenwidth)
1276 i = 13; /* base offset */
1277 i += (width / 2) + 1; /* spaces every second byte */
1278 i += (width * 2); /* width of bytes */
1280 i += width; /* each byte */
1282 } while (i < screenwidth);
1285 for (n = 0; n < len; n += width) {
1286 for (i = n; i < n + width; i++) {
1287 if ((i % width) == 0) { /* beginning of line */
1288 printf(" 0x%04x", i);
1294 printf("%02x", p[i] & 0xff);
1299 for (i = n; i < n + width; i++) {
1302 if (p[i] >= ' ' && p[i] <= '~')
1309 if ((i % width) != 0)
1314 visdump(char *dp, int datalen, int screenwidth)
1323 for (;datalen > 0; datalen--, dp++) {
1324 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1327 * Keep track of printables and
1328 * space chars (like fold(1)).
1340 width = 8 - (col&07);
1345 if (col + width > (screenwidth-2)) {
1360 ktrgenio(struct ktr_genio *ktr, int len)
1362 int datalen = len - sizeof (struct ktr_genio);
1363 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1364 static int screenwidth = 0;
1367 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1368 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1369 datalen == 1 ? "" : "s");
1372 if (screenwidth == 0) {
1375 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1377 screenwidth = ws.ws_col;
1381 if (maxdata && datalen > maxdata)
1384 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1385 if (dp[i] >= 32 && dp[i] < 127)
1387 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1392 hexdump(dp, datalen, screenwidth);
1394 visdump(dp, datalen, screenwidth);
1397 const char *signames[] = {
1398 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
1399 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
1400 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
1401 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
1402 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
1403 "USR2", NULL, /* 31 - 32 */
1407 ktrpsig(struct ktr_psig *psig)
1409 if (psig->signo > 0 && psig->signo < NSIG)
1410 printf("SIG%s ", signames[psig->signo]);
1412 printf("SIG %d ", psig->signo);
1413 if (psig->action == SIG_DFL) {
1414 printf("SIG_DFL code=");
1415 sigcodename(psig->signo, psig->code);
1418 printf("caught handler=0x%lx mask=0x%x code=",
1419 (u_long)psig->action, psig->mask.__bits[0]);
1420 sigcodename(psig->signo, psig->code);
1426 ktrcsw_old(struct ktr_csw_old *cs)
1428 printf("%s %s\n", cs->out ? "stop" : "resume",
1429 cs->user ? "user" : "kernel");
1433 ktrcsw(struct ktr_csw *cs)
1435 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1436 cs->user ? "user" : "kernel", cs->wmesg);
1439 #define UTRACE_DLOPEN_START 1
1440 #define UTRACE_DLOPEN_STOP 2
1441 #define UTRACE_DLCLOSE_START 3
1442 #define UTRACE_DLCLOSE_STOP 4
1443 #define UTRACE_LOAD_OBJECT 5
1444 #define UTRACE_UNLOAD_OBJECT 6
1445 #define UTRACE_ADD_RUNDEP 7
1446 #define UTRACE_PRELOAD_FINISHED 8
1447 #define UTRACE_INIT_CALL 9
1448 #define UTRACE_FINI_CALL 10
1450 struct utrace_rtld {
1451 char sig[4]; /* 'RTLD' */
1457 char name[MAXPATHLEN];
1461 ktruser_rtld(int len, unsigned char *p)
1463 struct utrace_rtld *ut = (struct utrace_rtld *)p;
1467 switch (ut->event) {
1468 case UTRACE_DLOPEN_START:
1470 printf("dlopen(%s, ", ut->name);
1471 switch (mode & RTLD_MODEMASK) {
1476 printf("RTLD_LAZY");
1479 printf("%#x", mode & RTLD_MODEMASK);
1481 if (mode & RTLD_GLOBAL)
1482 printf(" | RTLD_GLOBAL");
1483 if (mode & RTLD_TRACE)
1484 printf(" | RTLD_TRACE");
1485 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
1486 printf(" | %#x", mode &
1487 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
1490 case UTRACE_DLOPEN_STOP:
1491 printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
1494 case UTRACE_DLCLOSE_START:
1495 printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
1498 case UTRACE_DLCLOSE_STOP:
1499 printf("dlclose(%p) finished\n", ut->handle);
1501 case UTRACE_LOAD_OBJECT:
1502 printf("RTLD: loaded %p @ %p - %p (%s)\n", ut->handle,
1503 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1506 case UTRACE_UNLOAD_OBJECT:
1507 printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
1508 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1511 case UTRACE_ADD_RUNDEP:
1512 parent = ut->mapbase;
1513 printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
1514 ut->handle, ut->name, ut->refcnt);
1516 case UTRACE_PRELOAD_FINISHED:
1517 printf("RTLD: LD_PRELOAD finished\n");
1519 case UTRACE_INIT_CALL:
1520 printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
1523 case UTRACE_FINI_CALL:
1524 printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
1530 printf("RTLD: %d ", len);
1533 printf(" %d", *p++);
1535 printf(" %02x", *p++);
1540 struct utrace_malloc {
1547 ktruser_malloc(unsigned char *p)
1549 struct utrace_malloc *ut = (struct utrace_malloc *)p;
1551 if (ut->p == (void *)(intptr_t)(-1))
1552 printf("malloc_init()\n");
1553 else if (ut->s == 0)
1554 printf("free(%p)\n", ut->p);
1555 else if (ut->p == NULL)
1556 printf("%p = malloc(%zu)\n", ut->r, ut->s);
1558 printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
1562 ktruser(int len, unsigned char *p)
1565 if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
1566 ktruser_rtld(len, p);
1570 if (len == sizeof(struct utrace_malloc)) {
1578 printf(" %d", *p++);
1580 printf(" %02x", *p++);
1585 ktrcaprights(cap_rights_t *rightsp)
1588 printf("cap_rights_t ");
1594 ktrsockaddr(struct sockaddr *sa)
1597 TODO: Support additional address families
1598 #include <netnatm/natm.h>
1599 struct sockaddr_natm *natm;
1600 #include <netsmb/netbios.h>
1601 struct sockaddr_nb *nb;
1606 * note: ktrstruct() has already verified that sa points to a
1607 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1608 * sa->sa_len bytes long.
1610 printf("struct sockaddr { ");
1611 sockfamilyname(sa->sa_family);
1614 #define check_sockaddr_len(n) \
1615 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1616 printf("invalid"); \
1620 switch(sa->sa_family) {
1622 struct sockaddr_in sa_in;
1624 memset(&sa_in, 0, sizeof(sa_in));
1625 memcpy(&sa_in, sa, sa->sa_len);
1626 check_sockaddr_len(in);
1627 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1628 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1632 case AF_APPLETALK: {
1633 struct sockaddr_at sa_at;
1634 struct netrange *nr;
1636 memset(&sa_at, 0, sizeof(sa_at));
1637 memcpy(&sa_at, sa, sa->sa_len);
1638 check_sockaddr_len(at);
1639 nr = &sa_at.sat_range.r_netrange;
1640 printf("%d.%d, %d-%d, %d", ntohs(sa_at.sat_addr.s_net),
1641 sa_at.sat_addr.s_node, ntohs(nr->nr_firstnet),
1642 ntohs(nr->nr_lastnet), nr->nr_phase);
1647 struct sockaddr_in6 sa_in6;
1649 memset(&sa_in6, 0, sizeof(sa_in6));
1650 memcpy(&sa_in6, sa, sa->sa_len);
1651 check_sockaddr_len(in6);
1652 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1653 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1654 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1659 struct sockaddr_ipx sa_ipx;
1661 memset(&sa_ipx, 0, sizeof(sa_ipx));
1662 memcpy(&sa_ipx, sa, sa->sa_len);
1663 check_sockaddr_len(ipx);
1664 /* XXX wish we had ipx_ntop */
1665 printf("%s", ipx_ntoa(sa_ipx.sipx_addr));
1671 struct sockaddr_un sa_un;
1673 memset(&sa_un, 0, sizeof(sa_un));
1674 memcpy(&sa_un, sa, sa->sa_len);
1675 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1679 printf("unknown address family");
1685 ktrstat(struct stat *statp)
1687 char mode[12], timestr[PATH_MAX + 4];
1693 * note: ktrstruct() has already verified that statp points to a
1694 * buffer exactly sizeof(struct stat) bytes long.
1696 printf("struct stat {");
1697 printf("dev=%ju, ino=%ju, ",
1698 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1700 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1702 strmode(statp->st_mode, mode);
1703 printf("mode=%s, ", mode);
1705 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1706 if (resolv == 0 || (pwd = getpwuid(statp->st_uid)) == NULL)
1707 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1709 printf("uid=\"%s\", ", pwd->pw_name);
1710 if (resolv == 0 || (grp = getgrgid(statp->st_gid)) == NULL)
1711 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1713 printf("gid=\"%s\", ", grp->gr_name);
1714 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1717 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1719 tm = localtime(&statp->st_atim.tv_sec);
1720 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1721 printf("\"%s\"", timestr);
1723 if (statp->st_atim.tv_nsec != 0)
1724 printf(".%09ld, ", statp->st_atim.tv_nsec);
1729 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1731 tm = localtime(&statp->st_mtim.tv_sec);
1732 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1733 printf("\"%s\"", timestr);
1735 if (statp->st_mtim.tv_nsec != 0)
1736 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1741 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1743 tm = localtime(&statp->st_ctim.tv_sec);
1744 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1745 printf("\"%s\"", timestr);
1747 if (statp->st_ctim.tv_nsec != 0)
1748 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1751 printf("birthtime=");
1753 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1755 tm = localtime(&statp->st_birthtim.tv_sec);
1756 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1757 printf("\"%s\"", timestr);
1759 if (statp->st_birthtim.tv_nsec != 0)
1760 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1763 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1764 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1765 (intmax_t)statp->st_blocks, statp->st_flags);
1770 ktrstruct(char *buf, size_t buflen)
1773 size_t namelen, datalen;
1775 cap_rights_t rights;
1777 struct sockaddr_storage ss;
1779 for (name = buf, namelen = 0;
1780 namelen < buflen && name[namelen] != '\0';
1783 if (namelen == buflen)
1785 if (name[namelen] != '\0')
1787 data = buf + namelen + 1;
1788 datalen = buflen - namelen - 1;
1792 for (i = 0; i < (int)namelen; ++i)
1793 if (!isalpha(name[i]))
1795 if (strcmp(name, "caprights") == 0) {
1796 if (datalen != sizeof(cap_rights_t))
1798 memcpy(&rights, data, datalen);
1799 ktrcaprights(&rights);
1800 } else if (strcmp(name, "stat") == 0) {
1801 if (datalen != sizeof(struct stat))
1803 memcpy(&sb, data, datalen);
1805 } else if (strcmp(name, "sockaddr") == 0) {
1806 if (datalen > sizeof(ss))
1808 memcpy(&ss, data, datalen);
1809 if (datalen != ss.ss_len)
1811 ktrsockaddr((struct sockaddr *)&ss);
1813 printf("unknown structure\n");
1817 printf("invalid record\n");
1821 ktrcapfail(struct ktr_cap_fail *ktr)
1823 switch (ktr->cap_type) {
1824 case CAPFAIL_NOTCAPABLE:
1825 /* operation on fd with insufficient capabilities */
1826 printf("operation requires ");
1827 capname(&ktr->cap_needed);
1828 printf(", process holds ");
1829 capname(&ktr->cap_held);
1831 case CAPFAIL_INCREASE:
1832 /* requested more capabilities than fd already has */
1833 printf("attempt to increase capabilities from ");
1834 capname(&ktr->cap_held);
1836 capname(&ktr->cap_needed);
1838 case CAPFAIL_SYSCALL:
1839 /* called restricted syscall */
1840 printf("disallowed system call");
1842 case CAPFAIL_LOOKUP:
1843 /* used ".." in strict-relative mode */
1844 printf("restricted VFS lookup");
1847 printf("unknown capability failure: ");
1848 capname(&ktr->cap_needed);
1850 capname(&ktr->cap_held);
1857 ktrfault(struct ktr_fault *ktr)
1860 printf("0x%jx ", ktr->vaddr);
1861 vmprotname(ktr->type);
1866 ktrfaultend(struct ktr_faultend *ktr)
1869 vmresultname(ktr->result);
1873 #if defined(__amd64__) || defined(__i386__)
1875 linux_ktrsyscall(struct ktr_syscall *ktr)
1877 int narg = ktr->ktr_narg;
1880 if (ktr->ktr_code >= nlinux_syscalls || ktr->ktr_code < 0)
1881 printf("[%d]", ktr->ktr_code);
1883 printf("%s", linux_syscallnames[ktr->ktr_code]);
1885 printf("[%d]", ktr->ktr_code);
1887 ip = &ktr->ktr_args[0];
1891 print_number(ip, narg, c);
1898 linux_ktrsysret(struct ktr_sysret *ktr)
1900 register_t ret = ktr->ktr_retval;
1901 int error = ktr->ktr_error;
1902 int code = ktr->ktr_code;
1904 if (code >= nlinux_syscalls || code < 0)
1905 printf("[%d] ", code);
1907 printf("%s", linux_syscallnames[code]);
1909 printf("[%d]", code);
1915 printf("%ld", (long)ret);
1916 if (ret < 0 || ret > 9)
1917 printf("/%#lx", (unsigned long)ret);
1920 printf("%ld", (long)ret);
1922 printf("%#lx", (unsigned long)ret);
1924 } else if (error == ERESTART)
1926 else if (error == EJUSTRETURN)
1927 printf("JUSTRETURN");
1929 if (ktr->ktr_error <= ELAST + 1)
1930 error = abs(bsd_to_linux_errno[ktr->ktr_error]);
1933 printf("-1 errno %d", error);
1935 printf(" %s", strerror(ktr->ktr_error));
1944 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
1945 "[-m maxdata] [-p pid] [-t trstr]\n");