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/errno.h>
54 #include <sys/ktrace.h>
55 #include <sys/ioctl.h>
56 #include <sys/socket.h>
58 #include <sys/sysent.h>
60 #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>
85 #include "kdump_subr.h"
87 u_int abidump(struct ktr_header *);
88 int fetchprocinfo(struct ktr_header *, u_int *);
89 int fread_tail(void *, int, int);
90 void dumpheader(struct ktr_header *);
91 void ktrsyscall(struct ktr_syscall *, u_int);
92 void ktrsysret(struct ktr_sysret *, u_int);
93 void ktrnamei(char *, int);
94 void hexdump(char *, int, int);
95 void visdump(char *, int, int);
96 void ktrgenio(struct ktr_genio *, int);
97 void ktrpsig(struct ktr_psig *);
98 void ktrcsw(struct ktr_csw *);
99 void ktrcsw_old(struct ktr_csw_old *);
100 void ktruser_malloc(unsigned char *);
101 void ktruser_rtld(int, unsigned char *);
102 void ktruser(int, unsigned char *);
103 void ktrsockaddr(struct sockaddr *);
104 void ktrstat(struct stat *);
105 void ktrstruct(char *, size_t);
106 void ktrfault(struct ktr_fault *);
107 void ktrfaultend(struct ktr_faultend *);
109 void ioctlname(unsigned long, int);
111 int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
112 resolv = 0, abiflag = 0, syscallno = 0;
113 const char *tracefile = DEF_TRACEFILE;
114 struct ktr_header ktr_header;
116 #define TIME_FORMAT "%b %e %T %Y"
117 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
119 #define print_number(i,n,c) do { \
121 printf("%c%jd", c, (intmax_t)*i); \
123 printf("%c%#jx", c, (uintmax_t)(u_register_t)*i); \
129 #if defined(__amd64__) || defined(__i386__)
131 void linux_ktrsyscall(struct ktr_syscall *);
132 void linux_ktrsysret(struct ktr_sysret *);
133 extern char *linux_syscallnames[];
134 extern int nlinux_syscalls;
138 * Linux syscalls return negative errno's, we do positive and map them
140 static int bsd_to_linux_errno[ELAST + 1] = {
141 -0, -1, -2, -3, -4, -5, -6, -7, -8, -9,
142 -10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
143 -20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
144 -30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
145 -90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
146 -100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
147 -110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
148 -116, -66, -6, -6, -6, -6, -6, -37, -38, -9,
149 -6, -6, -43, -42, -75,-125, -84, -95, -16, -74,
156 TAILQ_ENTRY(proc_info) info;
161 TAILQ_HEAD(trace_procs, proc_info) trace_procs;
164 main(int argc, char *argv[])
166 int ch, ktrlen, size;
168 int trpoints = ALL_POINTS;
173 setlocale(LC_CTYPE, "");
175 while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1)
190 maxdata = atoi(optarg);
208 timestamp = 3; /* elapsed timestamp */
214 timestamp = 2; /* relative timestamp */
220 trpoints = getpoints(optarg);
222 errx(1, "unknown trace point in %s", optarg);
231 m = malloc(size = 1025);
233 errx(1, "%s", strerror(ENOMEM));
234 if (!freopen(tracefile, "r", stdin))
235 err(1, "%s", tracefile);
236 TAILQ_INIT(&trace_procs);
238 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
239 if (ktr_header.ktr_type & KTR_DROP) {
240 ktr_header.ktr_type &= ~KTR_DROP;
241 if (!drop_logged && threads) {
243 "%6jd %6jd %-8.*s Events dropped.\n",
244 (intmax_t)ktr_header.ktr_pid,
245 ktr_header.ktr_tid > 0 ?
246 (intmax_t)ktr_header.ktr_tid : 0,
247 MAXCOMLEN, ktr_header.ktr_comm);
249 } else if (!drop_logged) {
250 printf("%6jd %-8.*s Events dropped.\n",
251 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
252 ktr_header.ktr_comm);
256 if (trpoints & (1<<ktr_header.ktr_type))
257 if (pid == 0 || ktr_header.ktr_pid == pid ||
258 ktr_header.ktr_tid == pid)
259 dumpheader(&ktr_header);
260 if ((ktrlen = ktr_header.ktr_len) < 0)
261 errx(1, "bogus length 0x%x", ktrlen);
263 m = realloc(m, ktrlen+1);
265 errx(1, "%s", strerror(ENOMEM));
268 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
269 errx(1, "data too short");
270 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
272 sv_flags = abidump(&ktr_header);
273 if (pid && ktr_header.ktr_pid != pid &&
274 ktr_header.ktr_tid != pid)
276 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
279 switch (ktr_header.ktr_type) {
281 #if defined(__amd64__) || defined(__i386__)
282 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
283 linux_ktrsyscall((struct ktr_syscall *)m);
286 ktrsyscall((struct ktr_syscall *)m, sv_flags);
289 #if defined(__amd64__) || defined(__i386__)
290 if ((sv_flags & SV_ABI_MASK) == SV_ABI_LINUX)
291 linux_ktrsysret((struct ktr_sysret *)m);
294 ktrsysret((struct ktr_sysret *)m, sv_flags);
301 ktrgenio((struct ktr_genio *)m, ktrlen);
304 ktrpsig((struct ktr_psig *)m);
307 if (ktrlen == sizeof(struct ktr_csw_old))
308 ktrcsw_old((struct ktr_csw_old *)m);
310 ktrcsw((struct ktr_csw *)m);
316 ktrstruct(m, ktrlen);
319 ktrfault((struct ktr_fault *)m);
322 ktrfaultend((struct ktr_faultend *)m);
335 fread_tail(void *buf, int size, int num)
339 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
347 fetchprocinfo(struct ktr_header *kth, u_int *flags)
349 struct proc_info *pi;
351 switch (kth->ktr_type) {
353 TAILQ_FOREACH(pi, &trace_procs, info) {
354 if (pi->pid == kth->ktr_pid) {
355 TAILQ_REMOVE(&trace_procs, pi, info);
359 pi = malloc(sizeof(struct proc_info));
361 errx(1, "%s", strerror(ENOMEM));
362 pi->sv_flags = *flags;
363 pi->pid = kth->ktr_pid;
364 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
368 TAILQ_FOREACH(pi, &trace_procs, info) {
369 if (pi->pid == kth->ktr_pid) {
370 TAILQ_REMOVE(&trace_procs, pi, info);
382 abidump(struct ktr_header *kth)
384 struct proc_info *pi;
389 TAILQ_FOREACH(pi, &trace_procs, info) {
390 if (pi->pid == kth->ktr_pid) {
391 flags = pi->sv_flags;
399 switch (flags & SV_ABI_MASK) {
419 printf("%s%s ", abi, arch);
425 dumpheader(struct ktr_header *kth)
427 static char unknown[64];
428 static struct timeval prevtime, temp;
431 switch (kth->ktr_type) {
470 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
475 * The ktr_tid field was previously the ktr_buffer field, which held
476 * the kernel pointer value for the buffer associated with data
477 * following the record header. It now holds a threadid, but only
478 * for trace files after the change. Older trace files still contain
479 * kernel pointers. Detect this and suppress the results by printing
480 * negative tid's as 0.
483 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
484 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
485 MAXCOMLEN, kth->ktr_comm);
487 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
490 if (timestamp == 3) {
491 if (prevtime.tv_sec == 0)
492 prevtime = kth->ktr_time;
493 timevalsub(&kth->ktr_time, &prevtime);
495 if (timestamp == 2) {
496 temp = kth->ktr_time;
497 timevalsub(&kth->ktr_time, &prevtime);
500 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
501 kth->ktr_time.tv_usec);
506 #include <sys/syscall.h>
508 #include <sys/kern/syscalls.c>
510 int nsyscalls = sizeof (syscallnames) / sizeof (syscallnames[0]);
513 ktrsyscall(struct ktr_syscall *ktr, u_int flags)
515 int narg = ktr->ktr_narg;
518 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
519 (ktr->ktr_code >= nsyscalls || ktr->ktr_code < 0))
520 printf("[%d]", ktr->ktr_code);
522 printf("%s", syscallnames[ktr->ktr_code]);
524 printf("[%d]", ktr->ktr_code);
526 ip = &ktr->ktr_args[0];
530 (flags == 0 || (flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
531 switch (ktr->ktr_code) {
533 print_number(ip, narg, c);
535 ioctlname(*ip, decimal);
550 print_number(ip, narg, c);
557 print_number(ip, narg, c);
559 flagsandmodename(ip[0], ip[1], decimal);
564 print_number(ip, narg, c);
565 print_number(ip, narg, c);
567 * A flags value of zero is valid for
568 * wait4() but not for wait6(), so
569 * handle zero special here.
572 print_number(ip, narg, c);
582 idtypename(*ip, decimal);
586 print_number(ip, narg, c);
587 print_number(ip, narg, c);
596 print_number(ip, narg, c);
603 print_number(ip, narg, c);
610 print_number(ip, narg, c);
611 print_number(ip, narg, c);
613 getfsstatflagsname(*ip);
618 print_number(ip, narg, c);
619 print_number(ip, narg, c);
626 print_number(ip, narg, c);
634 print_number(ip, narg, c);
635 print_number(ip, narg, c);
637 sendrecvflagsname(*ip);
643 print_number(ip, narg, c);
644 print_number(ip, narg, c);
645 print_number(ip, narg, c);
647 sendrecvflagsname(*ip);
654 print_number(ip, narg, c);
661 print_number(ip, narg, c);
680 print_number(ip, narg, c);
681 print_number(ip, narg, c);
687 #ifdef SYS_freebsd6_mmap
688 case SYS_freebsd6_mmap:
689 print_number(ip, narg, c);
690 print_number(ip, narg, c);
702 print_number(ip, narg, c);
703 print_number(ip, narg, c);
714 print_number(ip, narg, c);
715 print_number(ip, narg, c);
722 print_number(ip, narg, c);
723 print_number(ip, narg, c);
725 madvisebehavname(*ip);
729 case SYS_setpriority:
730 print_number(ip, narg, c);
731 print_number(ip, narg, c);
738 print_number(ip, narg, c);
740 fcntlcmdname(ip[0], ip[1], decimal);
748 sockdomainname(sockdomain);
755 if (sockdomain == PF_INET ||
756 sockdomain == PF_INET6) {
758 sockipprotoname(*ip);
767 print_number(ip, narg, c);
769 sockoptlevelname(*ip, decimal);
770 if (*ip == SOL_SOCKET) {
779 #ifdef SYS_freebsd6_lseek
780 case SYS_freebsd6_lseek:
781 print_number(ip, narg, c);
782 /* Hidden 'pad' argument, not in lseek(2) */
783 print_number(ip, narg, c);
784 print_number(ip, narg, c);
792 print_number(ip, narg, c);
793 /* Hidden 'pad' argument, not in lseek(2) */
794 print_number(ip, narg, c);
801 print_number(ip, narg, c);
809 print_number(ip, narg, c);
816 print_number(ip, narg, c);
818 shutdownhowname(*ip);
842 print_number(ip, narg, c);
864 print_number(ip, narg, c);
865 print_number(ip, narg, c);
872 print_number(ip, narg, c);
873 print_number(ip, narg, c);
880 print_number(ip, narg, c);
887 print_number(ip, narg, c);
888 print_number(ip, narg, c);
895 print_number(ip, narg, c);
902 print_number(ip, narg, c);
903 print_number(ip, narg, c);
929 case SYS_sched_setscheduler:
930 print_number(ip, narg, c);
932 schedpolicyname(*ip);
936 case SYS_sched_get_priority_max:
937 case SYS_sched_get_priority_min:
939 schedpolicyname(*ip);
944 print_number(ip, narg, c);
945 print_number(ip, narg, c);
946 print_number(ip, narg, c);
947 print_number(ip, narg, c);
948 print_number(ip, narg, c);
949 print_number(ip, narg, c);
951 sendfileflagsname(*ip);
956 print_number(ip, narg, c);
962 case SYS_sigprocmask:
964 sigprocmaskhowname(*ip);
969 case SYS___acl_get_file:
970 case SYS___acl_set_file:
971 case SYS___acl_get_fd:
972 case SYS___acl_set_fd:
973 case SYS___acl_delete_file:
974 case SYS___acl_delete_fd:
975 case SYS___acl_aclcheck_file:
976 case SYS___acl_aclcheck_fd:
977 case SYS___acl_get_link:
978 case SYS___acl_set_link:
979 case SYS___acl_delete_link:
980 case SYS___acl_aclcheck_link:
981 print_number(ip, narg, c);
995 print_number(ip, narg, c);
1002 print_number(ip, narg, c);
1003 print_number(ip, narg, c);
1005 mountflagsname(*ip);
1009 case SYS_thr_create:
1010 print_number(ip, narg, c);
1011 print_number(ip, narg, c);
1013 thrcreateflagsname(*ip);
1018 print_number(ip, narg, c);
1024 case SYS_kldunloadf:
1025 print_number(ip, narg, c);
1027 kldunloadfflagsname(*ip);
1031 case SYS_posix_fadvise:
1032 print_number(ip, narg, c);
1033 print_number(ip, narg, c);
1034 print_number(ip, narg, c);
1036 fadvisebehavname((int)*ip);
1042 idtypename(*ip, decimal);
1046 print_number(ip, narg, c);
1048 procctlcmdname(*ip);
1055 print_number(ip, narg, c);
1063 ktrsysret(struct ktr_sysret *ktr, u_int flags)
1065 register_t ret = ktr->ktr_retval;
1066 int error = ktr->ktr_error;
1067 int code = ktr->ktr_code;
1069 if ((flags != 0 && ((flags & SV_ABI_MASK) != SV_ABI_FREEBSD)) ||
1070 (code >= nsyscalls || code < 0))
1071 printf("[%d] ", code);
1073 printf("%s", syscallnames[code]);
1075 printf("[%d]", code);
1081 printf("%ld", (long)ret);
1082 if (ret < 0 || ret > 9)
1083 printf("/%#lx", (unsigned long)ret);
1086 printf("%ld", (long)ret);
1088 printf("%#lx", (unsigned long)ret);
1090 } else if (error == ERESTART)
1092 else if (error == EJUSTRETURN)
1093 printf("JUSTRETURN");
1095 printf("-1 errno %d", ktr->ktr_error);
1097 printf(" %s", strerror(ktr->ktr_error));
1103 ktrnamei(char *cp, int len)
1105 printf("\"%.*s\"\n", len, cp);
1109 hexdump(char *p, int len, int screenwidth)
1117 i = 13; /* base offset */
1118 i += (width / 2) + 1; /* spaces every second byte */
1119 i += (width * 2); /* width of bytes */
1121 i += width; /* each byte */
1123 } while (i < screenwidth);
1126 for (n = 0; n < len; n += width) {
1127 for (i = n; i < n + width; i++) {
1128 if ((i % width) == 0) { /* beginning of line */
1129 printf(" 0x%04x", i);
1135 printf("%02x", p[i] & 0xff);
1140 for (i = n; i < n + width; i++) {
1143 if (p[i] >= ' ' && p[i] <= '~')
1150 if ((i % width) != 0)
1155 visdump(char *dp, int datalen, int screenwidth)
1164 for (;datalen > 0; datalen--, dp++) {
1165 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1168 * Keep track of printables and
1169 * space chars (like fold(1)).
1181 width = 8 - (col&07);
1186 if (col + width > (screenwidth-2)) {
1201 ktrgenio(struct ktr_genio *ktr, int len)
1203 int datalen = len - sizeof (struct ktr_genio);
1204 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1205 static int screenwidth = 0;
1208 if (screenwidth == 0) {
1211 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1213 screenwidth = ws.ws_col;
1217 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1218 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1219 datalen == 1 ? "" : "s");
1222 if (maxdata && datalen > maxdata)
1225 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1226 if (dp[i] >= 32 && dp[i] < 127)
1228 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1233 hexdump(dp, datalen, screenwidth);
1235 visdump(dp, datalen, screenwidth);
1238 const char *signames[] = {
1239 "NULL", "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", /* 1 - 6 */
1240 "EMT", "FPE", "KILL", "BUS", "SEGV", "SYS", /* 7 - 12 */
1241 "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP", /* 13 - 18 */
1242 "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 19 - 24 */
1243 "XFSZ", "VTALRM", "PROF", "WINCH", "29", "USR1", /* 25 - 30 */
1244 "USR2", NULL, /* 31 - 32 */
1248 ktrpsig(struct ktr_psig *psig)
1250 if (psig->signo > 0 && psig->signo < NSIG)
1251 printf("SIG%s ", signames[psig->signo]);
1253 printf("SIG %d ", psig->signo);
1254 if (psig->action == SIG_DFL)
1255 printf("SIG_DFL code=0x%x\n", psig->code);
1257 printf("caught handler=0x%lx mask=0x%x code=0x%x\n",
1258 (u_long)psig->action, psig->mask.__bits[0], psig->code);
1263 ktrcsw_old(struct ktr_csw_old *cs)
1265 printf("%s %s\n", cs->out ? "stop" : "resume",
1266 cs->user ? "user" : "kernel");
1270 ktrcsw(struct ktr_csw *cs)
1272 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1273 cs->user ? "user" : "kernel", cs->wmesg);
1276 #define UTRACE_DLOPEN_START 1
1277 #define UTRACE_DLOPEN_STOP 2
1278 #define UTRACE_DLCLOSE_START 3
1279 #define UTRACE_DLCLOSE_STOP 4
1280 #define UTRACE_LOAD_OBJECT 5
1281 #define UTRACE_UNLOAD_OBJECT 6
1282 #define UTRACE_ADD_RUNDEP 7
1283 #define UTRACE_PRELOAD_FINISHED 8
1284 #define UTRACE_INIT_CALL 9
1285 #define UTRACE_FINI_CALL 10
1287 struct utrace_rtld {
1288 char sig[4]; /* 'RTLD' */
1294 char name[MAXPATHLEN];
1298 ktruser_rtld(int len, unsigned char *p)
1300 struct utrace_rtld *ut = (struct utrace_rtld *)p;
1304 switch (ut->event) {
1305 case UTRACE_DLOPEN_START:
1307 printf("dlopen(%s, ", ut->name);
1308 switch (mode & RTLD_MODEMASK) {
1313 printf("RTLD_LAZY");
1316 printf("%#x", mode & RTLD_MODEMASK);
1318 if (mode & RTLD_GLOBAL)
1319 printf(" | RTLD_GLOBAL");
1320 if (mode & RTLD_TRACE)
1321 printf(" | RTLD_TRACE");
1322 if (mode & ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE))
1323 printf(" | %#x", mode &
1324 ~(RTLD_MODEMASK | RTLD_GLOBAL | RTLD_TRACE));
1327 case UTRACE_DLOPEN_STOP:
1328 printf("%p = dlopen(%s) ref %d\n", ut->handle, ut->name,
1331 case UTRACE_DLCLOSE_START:
1332 printf("dlclose(%p) (%s, %d)\n", ut->handle, ut->name,
1335 case UTRACE_DLCLOSE_STOP:
1336 printf("dlclose(%p) finished\n", ut->handle);
1338 case UTRACE_LOAD_OBJECT:
1339 printf("RTLD: loaded %p @ %p - %p (%s)\n", ut->handle,
1340 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1343 case UTRACE_UNLOAD_OBJECT:
1344 printf("RTLD: unloaded %p @ %p - %p (%s)\n", ut->handle,
1345 ut->mapbase, (char *)ut->mapbase + ut->mapsize - 1,
1348 case UTRACE_ADD_RUNDEP:
1349 parent = ut->mapbase;
1350 printf("RTLD: %p now depends on %p (%s, %d)\n", parent,
1351 ut->handle, ut->name, ut->refcnt);
1353 case UTRACE_PRELOAD_FINISHED:
1354 printf("RTLD: LD_PRELOAD finished\n");
1356 case UTRACE_INIT_CALL:
1357 printf("RTLD: init %p for %p (%s)\n", ut->mapbase, ut->handle,
1360 case UTRACE_FINI_CALL:
1361 printf("RTLD: fini %p for %p (%s)\n", ut->mapbase, ut->handle,
1367 printf("RTLD: %d ", len);
1370 printf(" %d", *p++);
1372 printf(" %02x", *p++);
1377 struct utrace_malloc {
1384 ktruser_malloc(unsigned char *p)
1386 struct utrace_malloc *ut = (struct utrace_malloc *)p;
1388 if (ut->p == (void *)(intptr_t)(-1))
1389 printf("malloc_init()\n");
1390 else if (ut->s == 0)
1391 printf("free(%p)\n", ut->p);
1392 else if (ut->p == NULL)
1393 printf("%p = malloc(%zu)\n", ut->r, ut->s);
1395 printf("%p = realloc(%p, %zu)\n", ut->r, ut->p, ut->s);
1399 ktruser(int len, unsigned char *p)
1402 if (len >= 8 && bcmp(p, "RTLD", 4) == 0) {
1403 ktruser_rtld(len, p);
1407 if (len == sizeof(struct utrace_malloc)) {
1415 printf(" %d", *p++);
1417 printf(" %02x", *p++);
1422 ktrsockaddr(struct sockaddr *sa)
1425 TODO: Support additional address families
1426 #include <netnatm/natm.h>
1427 struct sockaddr_natm *natm;
1428 #include <netsmb/netbios.h>
1429 struct sockaddr_nb *nb;
1434 * note: ktrstruct() has already verified that sa points to a
1435 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1436 * sa->sa_len bytes long.
1438 printf("struct sockaddr { ");
1439 sockfamilyname(sa->sa_family);
1442 #define check_sockaddr_len(n) \
1443 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1444 printf("invalid"); \
1448 switch(sa->sa_family) {
1450 struct sockaddr_in sa_in;
1452 memset(&sa_in, 0, sizeof(sa_in));
1453 memcpy(&sa_in, sa, sizeof(sa));
1454 check_sockaddr_len(in);
1455 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1456 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1460 case AF_APPLETALK: {
1461 struct sockaddr_at sa_at;
1462 struct netrange *nr;
1464 memset(&sa_at, 0, sizeof(sa_at));
1465 memcpy(&sa_at, sa, sizeof(sa));
1466 check_sockaddr_len(at);
1467 nr = &sa_at.sat_range.r_netrange;
1468 printf("%d.%d, %d-%d, %d", ntohs(sa_at.sat_addr.s_net),
1469 sa_at.sat_addr.s_node, ntohs(nr->nr_firstnet),
1470 ntohs(nr->nr_lastnet), nr->nr_phase);
1475 struct sockaddr_in6 sa_in6;
1477 memset(&sa_in6, 0, sizeof(sa_in6));
1478 memcpy(&sa_in6, sa, sizeof(sa));
1479 check_sockaddr_len(in6);
1480 inet_ntop(AF_INET6, &sa_in6.sin6_addr, addr, sizeof addr);
1481 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1486 struct sockaddr_ipx sa_ipx;
1488 memset(&sa_ipx, 0, sizeof(sa_ipx));
1489 memcpy(&sa_ipx, sa, sizeof(sa));
1490 check_sockaddr_len(ipx);
1491 /* XXX wish we had ipx_ntop */
1492 printf("%s", ipx_ntoa(sa_ipx.sipx_addr));
1498 struct sockaddr_un sa_un;
1500 memset(&sa_un, 0, sizeof(sa_un));
1501 memcpy(&sa_un, sa, sizeof(sa));
1502 check_sockaddr_len(un);
1503 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1507 printf("unknown address family");
1513 ktrstat(struct stat *statp)
1515 char mode[12], timestr[PATH_MAX + 4];
1521 * note: ktrstruct() has already verified that statp points to a
1522 * buffer exactly sizeof(struct stat) bytes long.
1524 printf("struct stat {");
1525 printf("dev=%ju, ino=%ju, ",
1526 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1528 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1530 strmode(statp->st_mode, mode);
1531 printf("mode=%s, ", mode);
1533 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1534 if (resolv == 0 || (pwd = getpwuid(statp->st_uid)) == NULL)
1535 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1537 printf("uid=\"%s\", ", pwd->pw_name);
1538 if (resolv == 0 || (grp = getgrgid(statp->st_gid)) == NULL)
1539 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1541 printf("gid=\"%s\", ", grp->gr_name);
1542 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1545 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1547 tm = localtime(&statp->st_atim.tv_sec);
1548 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1549 printf("\"%s\"", timestr);
1551 if (statp->st_atim.tv_nsec != 0)
1552 printf(".%09ld, ", statp->st_atim.tv_nsec);
1557 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1559 tm = localtime(&statp->st_mtim.tv_sec);
1560 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1561 printf("\"%s\"", timestr);
1563 if (statp->st_mtim.tv_nsec != 0)
1564 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1569 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1571 tm = localtime(&statp->st_ctim.tv_sec);
1572 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1573 printf("\"%s\"", timestr);
1575 if (statp->st_ctim.tv_nsec != 0)
1576 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1579 printf("birthtime=");
1581 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1583 tm = localtime(&statp->st_birthtim.tv_sec);
1584 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1585 printf("\"%s\"", timestr);
1587 if (statp->st_birthtim.tv_nsec != 0)
1588 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1591 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1592 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1593 (intmax_t)statp->st_blocks, statp->st_flags);
1598 ktrstruct(char *buf, size_t buflen)
1601 size_t namelen, datalen;
1604 struct sockaddr_storage ss;
1606 for (name = buf, namelen = 0;
1607 namelen < buflen && name[namelen] != '\0';
1610 if (namelen == buflen)
1612 if (name[namelen] != '\0')
1614 data = buf + namelen + 1;
1615 datalen = buflen - namelen - 1;
1619 for (i = 0; i < (int)namelen; ++i)
1620 if (!isalpha(name[i]))
1622 if (strcmp(name, "stat") == 0) {
1623 if (datalen != sizeof(struct stat))
1625 memcpy(&sb, data, datalen);
1627 } else if (strcmp(name, "sockaddr") == 0) {
1628 if (datalen > sizeof(ss))
1630 memcpy(&ss, data, datalen);
1631 if (datalen < sizeof(struct sockaddr) ||
1632 datalen != ss.ss_len)
1634 ktrsockaddr((struct sockaddr *)&ss);
1636 printf("unknown structure\n");
1640 printf("invalid record\n");
1644 ktrfault(struct ktr_fault *ktr)
1647 printf("0x%jx ", ktr->vaddr);
1648 vmprotname(ktr->type);
1653 ktrfaultend(struct ktr_faultend *ktr)
1656 vmresultname(ktr->result);
1660 #if defined(__amd64__) || defined(__i386__)
1662 linux_ktrsyscall(struct ktr_syscall *ktr)
1664 int narg = ktr->ktr_narg;
1667 if (ktr->ktr_code >= nlinux_syscalls || ktr->ktr_code < 0)
1668 printf("[%d]", ktr->ktr_code);
1670 printf("%s", linux_syscallnames[ktr->ktr_code]);
1672 printf("[%d]", ktr->ktr_code);
1674 ip = &ktr->ktr_args[0];
1678 print_number(ip, narg, c);
1685 linux_ktrsysret(struct ktr_sysret *ktr)
1687 register_t ret = ktr->ktr_retval;
1688 int error = ktr->ktr_error;
1689 int code = ktr->ktr_code;
1691 if (code >= nlinux_syscalls || code < 0)
1692 printf("[%d] ", code);
1694 printf("%s", linux_syscallnames[code]);
1696 printf("[%d]", code);
1702 printf("%ld", (long)ret);
1703 if (ret < 0 || ret > 9)
1704 printf("/%#lx", (unsigned long)ret);
1707 printf("%ld", (long)ret);
1709 printf("%#lx", (unsigned long)ret);
1711 } else if (error == ERESTART)
1713 else if (error == EJUSTRETURN)
1714 printf("JUSTRETURN");
1716 if (ktr->ktr_error <= ELAST + 1)
1717 error = abs(bsd_to_linux_errno[ktr->ktr_error]);
1720 printf("-1 errno %d", error);
1722 printf(" %s", strerror(ktr->ktr_error));
1731 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
1732 "[-m maxdata] [-p pid] [-t trstr]\n");