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$");
44 #define _WANT_KERNEL_ERRNO
46 #define _WANT_KEVENT32
48 #include <sys/param.h>
49 #include <sys/capsicum.h>
50 #include <sys/errno.h>
53 #include <sys/event.h>
54 #include <sys/ktrace.h>
55 #include <sys/ioctl.h>
56 #include <sys/socket.h>
58 #include <sys/sysent.h>
61 #include <sys/queue.h>
66 #include <arpa/inet.h>
67 #include <netinet/in.h>
80 #include <sysdecode.h>
88 #include <libcasper.h>
90 #include <casper/cap_grp.h>
91 #include <casper/cap_pwd.h>
94 int fetchprocinfo(struct ktr_header *, u_int *);
95 u_int findabi(struct ktr_header *);
96 int fread_tail(void *, int, int);
97 void dumpheader(struct ktr_header *, u_int);
98 void ktrsyscall(struct ktr_syscall *, u_int);
99 void ktrsysret(struct ktr_sysret *, u_int);
100 void ktrnamei(char *, int);
101 void hexdump(char *, int, int);
102 void visdump(char *, int, int);
103 void ktrgenio(struct ktr_genio *, int);
104 void ktrpsig(struct ktr_psig *);
105 void ktrcsw(struct ktr_csw *);
106 void ktrcsw_old(struct ktr_csw_old *);
107 void ktruser(int, void *);
108 void ktrcaprights(cap_rights_t *);
109 void ktritimerval(struct itimerval *it);
110 void ktrsockaddr(struct sockaddr *);
111 void ktrstat(struct stat *);
112 void ktrstruct(char *, size_t);
113 void ktrcapfail(struct ktr_cap_fail *);
114 void ktrfault(struct ktr_fault *);
115 void ktrfaultend(struct ktr_faultend *);
116 void ktrkevent(struct kevent *);
117 void ktrstructarray(struct ktr_struct_array *, size_t);
118 void limitfd(int fd);
121 #define TIMESTAMP_NONE 0x0
122 #define TIMESTAMP_ABSOLUTE 0x1
123 #define TIMESTAMP_ELAPSED 0x2
124 #define TIMESTAMP_RELATIVE 0x4
126 static bool abiflag, decimal, fancy = true, resolv, suppressdata, syscallno,
128 static int timestamp, maxdata;
129 static const char *tracefile = DEF_TRACEFILE;
130 static struct ktr_header ktr_header;
132 #define TIME_FORMAT "%b %e %T %Y"
133 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
135 #define print_number64(first,i,n,c) do { \
138 if (quad_align && (((ptrdiff_t)((i) - (first))) & 1) == 1) { \
142 if (quad_slots == 2) \
143 __v = (uint64_t)(uint32_t)(i)[0] | \
144 ((uint64_t)(uint32_t)(i)[1]) << 32; \
146 __v = (uint64_t)*(i); \
148 printf("%c%jd", (c), (intmax_t)__v); \
150 printf("%c%#jx", (c), (uintmax_t)__v); \
156 #define print_number(i,n,c) do { \
158 printf("%c%jd", c, (intmax_t)*i); \
160 printf("%c%#jx", c, (uintmax_t)(u_register_t)*i); \
168 TAILQ_ENTRY(proc_info) info;
173 static TAILQ_HEAD(trace_procs, proc_info) trace_procs;
175 #ifdef HAVE_LIBCASPER
176 static cap_channel_t *cappwd, *capgrp;
184 * Cache NLS data before entering capability mode.
185 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
187 (void)catopen("libc", NL_CAT_LOCALE);
196 * Allow localtime(3) to cache /etc/localtime content before entering
198 * XXXPJD: There should be localtime_init() in libc.
201 (void)localtime(<ime);
204 #ifdef HAVE_LIBCASPER
206 cappwdgrp_setup(cap_channel_t **cappwdp, cap_channel_t **capgrpp)
208 cap_channel_t *capcas, *cappwdloc, *capgrploc;
209 const char *cmds[1], *fields[1];
212 if (capcas == NULL) {
213 err(1, "unable to create casper process");
216 cappwdloc = cap_service_open(capcas, "system.pwd");
217 capgrploc = cap_service_open(capcas, "system.grp");
218 /* Casper capability no longer needed. */
220 if (cappwdloc == NULL || capgrploc == NULL) {
221 if (cappwdloc == NULL)
222 warn("unable to open system.pwd service");
223 if (capgrploc == NULL)
224 warn("unable to open system.grp service");
227 /* Limit system.pwd to only getpwuid() function and pw_name field. */
228 cmds[0] = "getpwuid";
229 if (cap_pwd_limit_cmds(cappwdloc, cmds, 1) < 0)
230 err(1, "unable to limit system.pwd service");
231 fields[0] = "pw_name";
232 if (cap_pwd_limit_fields(cappwdloc, fields, 1) < 0)
233 err(1, "unable to limit system.pwd service");
234 /* Limit system.grp to only getgrgid() function and gr_name field. */
235 cmds[0] = "getgrgid";
236 if (cap_grp_limit_cmds(capgrploc, cmds, 1) < 0)
237 err(1, "unable to limit system.grp service");
238 fields[0] = "gr_name";
239 if (cap_grp_limit_fields(capgrploc, fields, 1) < 0)
240 err(1, "unable to limit system.grp service");
242 *cappwdp = cappwdloc;
243 *capgrpp = capgrploc;
246 #endif /* HAVE_LIBCASPER */
249 print_integer_arg(const char *(*decoder)(int), int value)
253 str = decoder(value);
258 printf("<invalid=%d>", value);
260 printf("<invalid=%#x>", value);
264 /* Like print_integer_arg but unknown values are treated as valid. */
266 print_integer_arg_valid(const char *(*decoder)(int), int value)
270 str = decoder(value);
277 printf("%#x", value);
282 print_mask_arg(bool (*decoder)(FILE *, int, int *), int value)
287 printf("%#x<", value);
288 invalid = !decoder(stdout, value, &rem);
291 printf("<invalid>%u", rem);
295 print_mask_arg0(bool (*decoder)(FILE *, int, int *), int value)
304 printf("%#x<", value);
305 invalid = !decoder(stdout, value, &rem);
308 printf("<invalid>%u", rem);
312 decode_fileflags(fflags_t value)
321 printf("%#x<", value);
322 invalid = !sysdecode_fileflags(stdout, value, &rem);
325 printf("<invalid>%u", rem);
329 decode_filemode(int value)
338 printf("%#o<", value);
339 invalid = !sysdecode_filemode(stdout, value, &rem);
342 printf("<invalid>%u", rem);
346 print_mask_arg32(bool (*decoder)(FILE *, uint32_t, uint32_t *), uint32_t value)
351 printf("%#x<", value);
352 invalid = !decoder(stdout, value, &rem);
355 printf("<invalid>%u", rem);
359 print_mask_argul(bool (*decoder)(FILE *, u_long, u_long *), u_long value)
368 printf("%#lx<", value);
369 invalid = !decoder(stdout, value, &rem);
372 printf("<invalid>%lu", rem);
376 main(int argc, char *argv[])
378 int ch, ktrlen, size;
380 int trpoints = ALL_POINTS;
385 setlocale(LC_CTYPE, "");
387 timestamp = TIMESTAMP_NONE;
389 while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1)
404 maxdata = atoi(optarg);
422 timestamp |= TIMESTAMP_ELAPSED;
428 timestamp |= TIMESTAMP_RELATIVE;
431 timestamp |= TIMESTAMP_ABSOLUTE;
434 trpoints = getpoints(optarg);
436 errx(1, "unknown trace point in %s", optarg);
445 m = malloc(size = 1025);
447 errx(1, "%s", strerror(ENOMEM));
448 if (strcmp(tracefile, "-") != 0)
449 if (!freopen(tracefile, "r", stdin))
450 err(1, "%s", tracefile);
454 #ifdef HAVE_LIBCASPER
456 if (cappwdgrp_setup(&cappwd, &capgrp) < 0) {
461 if (!resolv || (cappwd != NULL && capgrp != NULL)) {
462 if (cap_enter() < 0 && errno != ENOSYS)
463 err(1, "unable to enter capability mode");
467 if (cap_enter() < 0 && errno != ENOSYS)
468 err(1, "unable to enter capability mode");
471 limitfd(STDIN_FILENO);
472 limitfd(STDOUT_FILENO);
473 limitfd(STDERR_FILENO);
475 TAILQ_INIT(&trace_procs);
477 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
478 if (ktr_header.ktr_type & KTR_DROP) {
479 ktr_header.ktr_type &= ~KTR_DROP;
480 if (!drop_logged && threads) {
482 "%6jd %6jd %-8.*s Events dropped.\n",
483 (intmax_t)ktr_header.ktr_pid,
484 ktr_header.ktr_tid > 0 ?
485 (intmax_t)ktr_header.ktr_tid : 0,
486 MAXCOMLEN, ktr_header.ktr_comm);
488 } else if (!drop_logged) {
489 printf("%6jd %-8.*s Events dropped.\n",
490 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
491 ktr_header.ktr_comm);
495 if ((ktrlen = ktr_header.ktr_len) < 0)
496 errx(1, "bogus length 0x%x", ktrlen);
498 m = realloc(m, ktrlen+1);
500 errx(1, "%s", strerror(ENOMEM));
503 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
504 errx(1, "data too short");
505 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
507 if (pid && ktr_header.ktr_pid != pid &&
508 ktr_header.ktr_tid != pid)
510 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
512 sv_flags = findabi(&ktr_header);
513 dumpheader(&ktr_header, sv_flags);
515 switch (ktr_header.ktr_type) {
517 ktrsyscall((struct ktr_syscall *)m, sv_flags);
520 ktrsysret((struct ktr_sysret *)m, sv_flags);
527 ktrgenio((struct ktr_genio *)m, ktrlen);
530 ktrpsig((struct ktr_psig *)m);
533 if (ktrlen == sizeof(struct ktr_csw_old))
534 ktrcsw_old((struct ktr_csw_old *)m);
536 ktrcsw((struct ktr_csw *)m);
542 ktrstruct(m, ktrlen);
545 ktrcapfail((struct ktr_cap_fail *)m);
548 ktrfault((struct ktr_fault *)m);
551 ktrfaultend((struct ktr_faultend *)m);
553 case KTR_STRUCT_ARRAY:
554 ktrstructarray((struct ktr_struct_array *)m, ktrlen);
572 cap_rights_init(&rights, CAP_FSTAT);
577 cap_rights_set(&rights, CAP_READ);
580 cap_rights_set(&rights, CAP_IOCTL, CAP_WRITE);
581 cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
584 cap_rights_set(&rights, CAP_WRITE);
586 cap_rights_set(&rights, CAP_IOCTL);
594 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
595 err(1, "unable to limit rights for descriptor %d", fd);
596 if (cmd != 0 && cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
597 err(1, "unable to limit ioctls for descriptor %d", fd);
601 fread_tail(void *buf, int size, int num)
605 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
613 fetchprocinfo(struct ktr_header *kth, u_int *flags)
615 struct proc_info *pi;
617 switch (kth->ktr_type) {
619 TAILQ_FOREACH(pi, &trace_procs, info) {
620 if (pi->pid == kth->ktr_pid) {
621 TAILQ_REMOVE(&trace_procs, pi, info);
625 pi = malloc(sizeof(struct proc_info));
627 errx(1, "%s", strerror(ENOMEM));
628 pi->sv_flags = *flags;
629 pi->pid = kth->ktr_pid;
630 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
634 TAILQ_FOREACH(pi, &trace_procs, info) {
635 if (pi->pid == kth->ktr_pid) {
636 TAILQ_REMOVE(&trace_procs, pi, info);
648 findabi(struct ktr_header *kth)
650 struct proc_info *pi;
652 TAILQ_FOREACH(pi, &trace_procs, info) {
653 if (pi->pid == kth->ktr_pid) {
654 return (pi->sv_flags);
661 dumpheader(struct ktr_header *kth, u_int sv_flags)
663 static char unknown[64];
664 static struct timeval prevtime, prevtime_e;
671 switch (kth->ktr_type) {
694 case KTR_STRUCT_ARRAY:
710 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
715 * The ktr_tid field was previously the ktr_buffer field, which held
716 * the kernel pointer value for the buffer associated with data
717 * following the record header. It now holds a threadid, but only
718 * for trace files after the change. Older trace files still contain
719 * kernel pointers. Detect this and suppress the results by printing
720 * negative tid's as 0.
723 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
724 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
725 MAXCOMLEN, kth->ktr_comm);
727 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
730 if (timestamp & TIMESTAMP_ABSOLUTE) {
731 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
732 kth->ktr_time.tv_usec);
734 if (timestamp & TIMESTAMP_ELAPSED) {
735 if (prevtime_e.tv_sec == 0)
736 prevtime_e = kth->ktr_time;
737 timersub(&kth->ktr_time, &prevtime_e, &temp);
738 printf("%jd.%06ld ", (intmax_t)temp.tv_sec,
741 if (timestamp & TIMESTAMP_RELATIVE) {
742 if (prevtime.tv_sec == 0)
743 prevtime = kth->ktr_time;
744 if (timercmp(&kth->ktr_time, &prevtime, <)) {
745 timersub(&prevtime, &kth->ktr_time, &temp);
748 timersub(&kth->ktr_time, &prevtime, &temp);
751 prevtime = kth->ktr_time;
752 printf("%s%jd.%06ld ", sign, (intmax_t)temp.tv_sec,
758 switch (sv_flags & SV_ABI_MASK) {
765 case SV_ABI_CLOUDABI:
773 if ((sv_flags & SV_LP64) != 0)
775 else if ((sv_flags & SV_ILP32) != 0)
780 printf("%s%s ", abi, arch);
784 #include <sys/syscall.h>
787 ioctlname(unsigned long val)
791 str = sysdecode_ioctlname(val);
800 static enum sysdecode_abi
801 syscallabi(u_int sv_flags)
805 return (SYSDECODE_ABI_FREEBSD);
806 switch (sv_flags & SV_ABI_MASK) {
808 return (SYSDECODE_ABI_FREEBSD);
809 #if defined(__amd64__) || defined(__i386__)
812 if (sv_flags & SV_ILP32)
813 return (SYSDECODE_ABI_LINUX32);
815 return (SYSDECODE_ABI_LINUX);
817 #if defined(__aarch64__) || defined(__amd64__)
818 case SV_ABI_CLOUDABI:
819 return (SYSDECODE_ABI_CLOUDABI64);
822 return (SYSDECODE_ABI_UNKNOWN);
827 syscallname(u_int code, u_int sv_flags)
831 name = sysdecode_syscallname(syscallabi(sv_flags), code);
833 printf("[%d]", code);
837 printf("[%d]", code);
842 print_signal(int signo)
846 signame = sysdecode_signal(signo);
848 printf("%s", signame);
850 printf("SIG %d", signo);
854 ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags)
856 int narg = ktr->ktr_narg;
857 register_t *ip, *first;
859 int quad_align, quad_slots;
861 syscallname(ktr->ktr_code, sv_flags);
862 ip = first = &ktr->ktr_args[0];
867 (sv_flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
869 if (sv_flags & SV_ILP32) {
876 switch (ktr->ktr_code) {
895 print_integer_arg_valid(sysdecode_atfd, *ip);
901 switch (ktr->ktr_code) {
903 print_number(ip, narg, c);
913 print_integer_arg(sysdecode_ptrace_request, *ip);
921 print_number(ip, narg, c);
923 print_mask_arg(sysdecode_access_mode, *ip);
929 print_number(ip, narg, c);
931 print_mask_arg(sysdecode_open_flags, ip[0]);
932 if ((ip[0] & O_CREAT) == O_CREAT) {
934 decode_filemode(ip[1]);
940 print_number(ip, narg, c);
941 print_number(ip, narg, c);
943 print_mask_arg0(sysdecode_wait4_options, *ip);
949 print_integer_arg(sysdecode_idtype, *ip);
953 print_number64(first, ip, narg, c);
954 print_number(ip, narg, c);
956 print_mask_arg(sysdecode_wait6_options, *ip);
964 print_number(ip, narg, c);
966 decode_filemode(*ip);
972 print_number(ip, narg, c);
974 decode_filemode(*ip);
979 print_number(ip, narg, c);
980 print_number(ip, narg, c);
982 print_integer_arg(sysdecode_getfsstat_mode, *ip);
987 print_number(ip, narg, c);
988 print_number(ip, narg, c);
990 print_mask_arg(sysdecode_mount_flags, *ip);
995 print_number(ip, narg, c);
997 print_mask_arg(sysdecode_mount_flags, *ip);
1003 print_number(ip, narg, c);
1004 print_number(ip, narg, c);
1006 print_mask_arg0(sysdecode_msg_flags, *ip);
1012 print_number(ip, narg, c);
1013 print_number(ip, narg, c);
1014 print_number(ip, narg, c);
1016 print_mask_arg0(sysdecode_msg_flags, *ip);
1024 print_number(ip, narg, c);
1026 decode_fileflags(*ip);
1031 print_number(ip, narg, c);
1039 print_mask_arg(sysdecode_reboot_howto, *ip);
1045 decode_filemode(*ip);
1050 print_number(ip, narg, c);
1051 print_number(ip, narg, c);
1053 print_mask_arg(sysdecode_msync_flags, *ip);
1057 #ifdef SYS_freebsd6_mmap
1058 case SYS_freebsd6_mmap:
1059 print_number(ip, narg, c);
1060 print_number(ip, narg, c);
1062 print_mask_arg(sysdecode_mmap_prot, *ip);
1066 print_mask_arg(sysdecode_mmap_flags, *ip);
1072 print_number(ip, narg, c);
1073 print_number(ip, narg, c);
1075 print_mask_arg(sysdecode_mmap_prot, *ip);
1079 print_mask_arg(sysdecode_mmap_flags, *ip);
1084 print_number(ip, narg, c);
1085 print_number(ip, narg, c);
1087 print_mask_arg(sysdecode_mmap_prot, *ip);
1092 print_number(ip, narg, c);
1093 print_number(ip, narg, c);
1095 print_integer_arg(sysdecode_madvice, *ip);
1102 print_number(ip, narg, c);
1104 print_integer_arg(sysdecode_pathconf_name, *ip);
1108 case SYS_getpriority:
1109 case SYS_setpriority:
1111 print_integer_arg(sysdecode_prio_which, *ip);
1117 print_number(ip, narg, c);
1119 print_integer_arg(sysdecode_fcntl_cmd, ip[0]);
1120 if (sysdecode_fcntl_arg_p(ip[0])) {
1122 if (ip[0] == F_SETFL)
1124 sysdecode_fcntl_fileflags,
1127 sysdecode_fcntl_arg(stdout,
1138 print_integer_arg(sysdecode_socketdomain,
1143 print_mask_arg(sysdecode_socket_type, *ip);
1146 if (sockdomain == PF_INET ||
1147 sockdomain == PF_INET6) {
1149 print_integer_arg(sysdecode_ipproto,
1157 case SYS_setsockopt:
1158 case SYS_getsockopt: {
1161 print_number(ip, narg, c);
1163 print_integer_arg_valid(sysdecode_sockopt_level,
1165 str = sysdecode_sockopt_name(ip[0], ip[1]);
1175 #ifdef SYS_freebsd6_lseek
1176 case SYS_freebsd6_lseek:
1177 print_number(ip, narg, c);
1178 /* Hidden 'pad' argument, not in lseek(2) */
1179 print_number(ip, narg, c);
1180 print_number64(first, ip, narg, c);
1182 print_integer_arg(sysdecode_whence, *ip);
1188 print_number(ip, narg, c);
1189 print_number64(first, ip, narg, c);
1191 print_integer_arg(sysdecode_whence, *ip);
1196 print_number(ip, narg, c);
1198 print_mask_arg(sysdecode_flock_operation, *ip);
1206 print_number(ip, narg, c);
1208 decode_filemode(*ip);
1213 print_number(ip, narg, c);
1215 print_integer_arg(sysdecode_shutdown_how, *ip);
1219 case SYS_socketpair:
1221 print_integer_arg(sysdecode_socketdomain, *ip);
1225 print_mask_arg(sysdecode_socket_type, *ip);
1233 print_integer_arg(sysdecode_rlimit, *ip);
1240 print_integer_arg(sysdecode_getrusage_who, *ip);
1246 print_number(ip, narg, c);
1248 if (!sysdecode_quotactl_cmd(stdout, *ip)) {
1250 printf("<invalid=%d>", (int)*ip);
1252 printf("<invalid=%#x>",
1261 print_integer_arg(sysdecode_nfssvc_flags, *ip);
1267 case SYS_rtprio_thread:
1269 print_integer_arg(sysdecode_rtprio_function,
1276 print_number(ip, narg, c);
1277 print_number(ip, narg, c);
1279 print_integer_arg(sysdecode_semctl_cmd, *ip);
1284 print_number(ip, narg, c);
1285 print_number(ip, narg, c);
1287 print_mask_arg(sysdecode_semget_flags, *ip);
1292 print_number(ip, narg, c);
1294 print_integer_arg(sysdecode_msgctl_cmd, *ip);
1299 print_number(ip, narg, c);
1300 print_number(ip, narg, c);
1302 print_mask_arg(sysdecode_shmat_flags, *ip);
1307 print_number(ip, narg, c);
1309 print_integer_arg(sysdecode_shmctl_cmd, *ip);
1314 print_number(ip, narg, c);
1316 print_mask_arg(sysdecode_open_flags, ip[0]);
1318 decode_filemode(ip[1]);
1323 print_number(ip, narg, c);
1324 print_number(ip, narg, c);
1326 print_integer_arg(sysdecode_minherit_inherit,
1333 print_mask_arg(sysdecode_rfork_flags, *ip);
1338 case SYS_lio_listio:
1340 print_integer_arg(sysdecode_lio_listio_mode,
1348 print_mask_arg(sysdecode_mlockall_flags, *ip);
1352 case SYS_sched_setscheduler:
1353 print_number(ip, narg, c);
1355 print_integer_arg(sysdecode_scheduler_policy,
1360 case SYS_sched_get_priority_max:
1361 case SYS_sched_get_priority_min:
1363 print_integer_arg(sysdecode_scheduler_policy,
1369 print_number(ip, narg, c);
1370 print_number(ip, narg, c);
1371 print_number(ip, narg, c);
1372 print_number(ip, narg, c);
1373 print_number(ip, narg, c);
1374 print_number(ip, narg, c);
1376 print_mask_arg(sysdecode_sendfile_flags, *ip);
1381 print_number(ip, narg, c);
1383 print_integer_arg(sysdecode_kldsym_cmd, *ip);
1387 case SYS_sigprocmask:
1389 print_integer_arg(sysdecode_sigprocmask_how,
1395 case SYS___acl_get_file:
1396 case SYS___acl_set_file:
1397 case SYS___acl_get_fd:
1398 case SYS___acl_set_fd:
1399 case SYS___acl_delete_file:
1400 case SYS___acl_delete_fd:
1401 case SYS___acl_aclcheck_file:
1402 case SYS___acl_aclcheck_fd:
1403 case SYS___acl_get_link:
1404 case SYS___acl_set_link:
1405 case SYS___acl_delete_link:
1406 case SYS___acl_aclcheck_link:
1407 print_number(ip, narg, c);
1409 print_integer_arg(sysdecode_acltype, *ip);
1420 case SYS_extattrctl:
1421 print_number(ip, narg, c);
1423 print_integer_arg(sysdecode_extattrnamespace,
1429 print_number(ip, narg, c);
1430 print_number(ip, narg, c);
1432 print_mask_arg(sysdecode_mount_flags, *ip);
1436 case SYS_thr_create:
1437 print_number(ip, narg, c);
1438 print_number(ip, narg, c);
1440 print_mask_arg(sysdecode_thr_create_flags, *ip);
1445 print_number(ip, narg, c);
1451 case SYS_kldunloadf:
1452 print_number(ip, narg, c);
1454 print_integer_arg(sysdecode_kldunload_flags,
1462 print_number(ip, narg, c);
1464 print_integer_arg_valid(sysdecode_atfd, *ip);
1467 print_number(ip, narg, c);
1469 case SYS_cap_fcntls_limit:
1470 print_number(ip, narg, c);
1475 print_mask_arg32(sysdecode_cap_fcntlrights, arg);
1477 case SYS_posix_fadvise:
1478 print_number(ip, narg, c);
1479 print_number(ip, narg, c);
1480 print_number(ip, narg, c);
1482 print_integer_arg(sysdecode_fadvice, *ip);
1488 print_integer_arg(sysdecode_idtype, *ip);
1492 print_number64(first, ip, narg, c);
1494 print_integer_arg(sysdecode_procctl_cmd, *ip);
1499 print_number(ip, narg, c);
1501 print_integer_arg(sysdecode_umtx_op, *ip);
1503 case UMTX_OP_CV_WAIT:
1508 sysdecode_umtx_cvwait_flags, *ip);
1510 case UMTX_OP_RW_RDLOCK:
1515 sysdecode_umtx_rwlock_flags, *ip);
1523 print_number(ip, narg, c);
1524 print_number64(first, ip, narg, c);
1527 print_number(ip, narg, c);
1528 print_number(ip, narg, c);
1529 print_number(ip, narg, c);
1533 print_number(ip, narg, c);
1534 print_number(ip, narg, c);
1537 print_number(ip, narg, c);
1541 print_integer_arg(sysdecode_sysarch_number, *ip);
1547 switch (ktr->ktr_code) {
1557 print_mask_arg0(sysdecode_atflags, *ip);
1564 print_number(ip, narg, c);
1572 ktrsysret(struct ktr_sysret *ktr, u_int sv_flags)
1574 register_t ret = ktr->ktr_retval;
1575 int error = ktr->ktr_error;
1577 syscallname(ktr->ktr_code, sv_flags);
1582 printf("%ld", (long)ret);
1583 if (ret < 0 || ret > 9)
1584 printf("/%#lx", (unsigned long)ret);
1587 printf("%ld", (long)ret);
1589 printf("%#lx", (unsigned long)ret);
1591 } else if (error == ERESTART)
1593 else if (error == EJUSTRETURN)
1594 printf("JUSTRETURN");
1596 printf("-1 errno %d", sysdecode_freebsd_to_abi_errno(
1597 syscallabi(sv_flags), error));
1599 printf(" %s", strerror(ktr->ktr_error));
1605 ktrnamei(char *cp, int len)
1607 printf("\"%.*s\"\n", len, cp);
1611 hexdump(char *p, int len, int screenwidth)
1619 i = 13; /* base offset */
1620 i += (width / 2) + 1; /* spaces every second byte */
1621 i += (width * 2); /* width of bytes */
1623 i += width; /* each byte */
1625 } while (i < screenwidth);
1628 for (n = 0; n < len; n += width) {
1629 for (i = n; i < n + width; i++) {
1630 if ((i % width) == 0) { /* beginning of line */
1631 printf(" 0x%04x", i);
1637 printf("%02x", p[i] & 0xff);
1642 for (i = n; i < n + width; i++) {
1645 if (p[i] >= ' ' && p[i] <= '~')
1652 if ((i % width) != 0)
1657 visdump(char *dp, int datalen, int screenwidth)
1666 for (;datalen > 0; datalen--, dp++) {
1667 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1670 * Keep track of printables and
1671 * space chars (like fold(1)).
1683 width = 8 - (col&07);
1688 if (col + width > (screenwidth-2)) {
1703 ktrgenio(struct ktr_genio *ktr, int len)
1705 int datalen = len - sizeof (struct ktr_genio);
1706 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1707 static int screenwidth = 0;
1710 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1711 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1712 datalen == 1 ? "" : "s");
1715 if (screenwidth == 0) {
1718 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1720 screenwidth = ws.ws_col;
1724 if (maxdata && datalen > maxdata)
1727 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1728 if (dp[i] >= 32 && dp[i] < 127)
1730 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1735 hexdump(dp, datalen, screenwidth);
1737 visdump(dp, datalen, screenwidth);
1741 ktrpsig(struct ktr_psig *psig)
1745 print_signal(psig->signo);
1746 if (psig->action == SIG_DFL) {
1749 printf(" caught handler=0x%lx mask=0x%x",
1750 (u_long)psig->action, psig->mask.__bits[0]);
1753 str = sysdecode_sigcode(psig->signo, psig->code);
1757 printf("<invalid=%#x>", psig->code);
1762 ktrcsw_old(struct ktr_csw_old *cs)
1764 printf("%s %s\n", cs->out ? "stop" : "resume",
1765 cs->user ? "user" : "kernel");
1769 ktrcsw(struct ktr_csw *cs)
1771 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1772 cs->user ? "user" : "kernel", cs->wmesg);
1776 ktruser(int len, void *p)
1780 if (sysdecode_utrace(stdout, p, len)) {
1789 printf(" %d", *cp++);
1791 printf(" %02x", *cp++);
1796 ktrcaprights(cap_rights_t *rightsp)
1799 printf("cap_rights_t ");
1800 sysdecode_cap_rights(stdout, rightsp);
1805 ktrtimeval(struct timeval *tv)
1808 printf("{%ld, %ld}", (long)tv->tv_sec, tv->tv_usec);
1812 ktritimerval(struct itimerval *it)
1815 printf("itimerval { .interval = ");
1816 ktrtimeval(&it->it_interval);
1817 printf(", .value = ");
1818 ktrtimeval(&it->it_value);
1823 ktrsockaddr(struct sockaddr *sa)
1826 TODO: Support additional address families
1827 #include <netnatm/natm.h>
1828 struct sockaddr_natm *natm;
1829 #include <netsmb/netbios.h>
1830 struct sockaddr_nb *nb;
1836 * note: ktrstruct() has already verified that sa points to a
1837 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1838 * sa->sa_len bytes long.
1840 printf("struct sockaddr { ");
1841 str = sysdecode_sockaddr_family(sa->sa_family);
1845 printf("<invalid=%d>", sa->sa_family);
1848 #define check_sockaddr_len(n) \
1849 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1850 printf("invalid"); \
1854 switch(sa->sa_family) {
1856 struct sockaddr_in sa_in;
1858 memset(&sa_in, 0, sizeof(sa_in));
1859 memcpy(&sa_in, sa, sa->sa_len);
1860 check_sockaddr_len(in);
1861 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1862 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1866 struct sockaddr_in6 sa_in6;
1868 memset(&sa_in6, 0, sizeof(sa_in6));
1869 memcpy(&sa_in6, sa, sa->sa_len);
1870 check_sockaddr_len(in6);
1871 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1872 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1873 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1877 struct sockaddr_un sa_un;
1879 memset(&sa_un, 0, sizeof(sa_un));
1880 memcpy(&sa_un, sa, sa->sa_len);
1881 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1885 printf("unknown address family");
1891 ktrstat(struct stat *statp)
1893 char mode[12], timestr[PATH_MAX + 4];
1899 * note: ktrstruct() has already verified that statp points to a
1900 * buffer exactly sizeof(struct stat) bytes long.
1902 printf("struct stat {");
1903 printf("dev=%ju, ino=%ju, ",
1904 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1906 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1908 strmode(statp->st_mode, mode);
1909 printf("mode=%s, ", mode);
1911 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1915 #ifdef HAVE_LIBCASPER
1917 pwd = cap_getpwuid(cappwd, statp->st_uid);
1920 pwd = getpwuid(statp->st_uid);
1923 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1925 printf("uid=\"%s\", ", pwd->pw_name);
1929 #ifdef HAVE_LIBCASPER
1931 grp = cap_getgrgid(capgrp, statp->st_gid);
1934 grp = getgrgid(statp->st_gid);
1937 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1939 printf("gid=\"%s\", ", grp->gr_name);
1940 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1943 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1945 tm = localtime(&statp->st_atim.tv_sec);
1946 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1947 printf("\"%s\"", timestr);
1949 if (statp->st_atim.tv_nsec != 0)
1950 printf(".%09ld, ", statp->st_atim.tv_nsec);
1955 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1957 tm = localtime(&statp->st_mtim.tv_sec);
1958 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1959 printf("\"%s\"", timestr);
1961 if (statp->st_mtim.tv_nsec != 0)
1962 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1967 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1969 tm = localtime(&statp->st_ctim.tv_sec);
1970 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1971 printf("\"%s\"", timestr);
1973 if (statp->st_ctim.tv_nsec != 0)
1974 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1977 printf("birthtime=");
1979 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1981 tm = localtime(&statp->st_birthtim.tv_sec);
1982 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1983 printf("\"%s\"", timestr);
1985 if (statp->st_birthtim.tv_nsec != 0)
1986 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1989 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1990 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1991 (intmax_t)statp->st_blocks, statp->st_flags);
1996 ktrstruct(char *buf, size_t buflen)
1999 size_t namelen, datalen;
2001 cap_rights_t rights;
2002 struct itimerval it;
2004 struct sockaddr_storage ss;
2006 for (name = buf, namelen = 0;
2007 namelen < buflen && name[namelen] != '\0';
2010 if (namelen == buflen)
2012 if (name[namelen] != '\0')
2014 data = buf + namelen + 1;
2015 datalen = buflen - namelen - 1;
2019 for (i = 0; i < (int)namelen; ++i)
2020 if (!isalpha(name[i]))
2022 if (strcmp(name, "caprights") == 0) {
2023 if (datalen != sizeof(cap_rights_t))
2025 memcpy(&rights, data, datalen);
2026 ktrcaprights(&rights);
2027 } else if (strcmp(name, "itimerval") == 0) {
2028 if (datalen != sizeof(struct itimerval))
2030 memcpy(&it, data, datalen);
2032 } else if (strcmp(name, "stat") == 0) {
2033 if (datalen != sizeof(struct stat))
2035 memcpy(&sb, data, datalen);
2037 } else if (strcmp(name, "sockaddr") == 0) {
2038 if (datalen > sizeof(ss))
2040 memcpy(&ss, data, datalen);
2041 if (datalen != ss.ss_len)
2043 ktrsockaddr((struct sockaddr *)&ss);
2045 printf("unknown structure\n");
2049 printf("invalid record\n");
2053 ktrcapfail(struct ktr_cap_fail *ktr)
2055 switch (ktr->cap_type) {
2056 case CAPFAIL_NOTCAPABLE:
2057 /* operation on fd with insufficient capabilities */
2058 printf("operation requires ");
2059 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2060 printf(", descriptor holds ");
2061 sysdecode_cap_rights(stdout, &ktr->cap_held);
2063 case CAPFAIL_INCREASE:
2064 /* requested more capabilities than fd already has */
2065 printf("attempt to increase capabilities from ");
2066 sysdecode_cap_rights(stdout, &ktr->cap_held);
2068 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2070 case CAPFAIL_SYSCALL:
2071 /* called restricted syscall */
2072 printf("disallowed system call");
2074 case CAPFAIL_LOOKUP:
2075 /* used ".." in strict-relative mode */
2076 printf("restricted VFS lookup");
2079 printf("unknown capability failure: ");
2080 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2082 sysdecode_cap_rights(stdout, &ktr->cap_held);
2089 ktrfault(struct ktr_fault *ktr)
2092 printf("0x%jx ", (uintmax_t)ktr->vaddr);
2093 print_mask_arg(sysdecode_vmprot, ktr->type);
2098 ktrfaultend(struct ktr_faultend *ktr)
2102 str = sysdecode_vmresult(ktr->result);
2106 printf("<invalid=%d>", ktr->result);
2111 ktrkevent(struct kevent *kev)
2115 switch (kev->filter) {
2121 case EVFILT_PROCDESC:
2122 printf("%ju", (uintmax_t)kev->ident);
2125 print_signal(kev->ident);
2128 printf("%p", (void *)kev->ident);
2130 printf(", filter=");
2131 print_integer_arg(sysdecode_kevent_filter, kev->filter);
2133 print_mask_arg0(sysdecode_kevent_flags, kev->flags);
2134 printf(", fflags=");
2135 sysdecode_kevent_fflags(stdout, kev->filter, kev->fflags,
2137 printf(", data=%#jx, udata=%p }", (uintmax_t)kev->data, kev->udata);
2141 ktrstructarray(struct ktr_struct_array *ksa, size_t buflen)
2145 size_t namelen, datalen;
2149 buflen -= sizeof(*ksa);
2150 for (name = (char *)(ksa + 1), namelen = 0;
2151 namelen < buflen && name[namelen] != '\0';
2154 if (namelen == buflen)
2156 if (name[namelen] != '\0')
2159 for (i = 0; i < (int)namelen; ++i)
2160 if (!isalnum(name[i]) && name[i] != '_')
2162 data = name + namelen + 1;
2163 datalen = buflen - namelen - 1;
2164 printf("struct %s[] = { ", name);
2166 for (; datalen >= ksa->struct_size;
2167 data += ksa->struct_size, datalen -= ksa->struct_size) {
2172 if (strcmp(name, "kevent") == 0) {
2173 if (ksa->struct_size != sizeof(kev))
2175 memcpy(&kev, data, sizeof(kev));
2177 #ifdef _WANT_KEVENT32
2178 } else if (strcmp(name, "kevent32") == 0) {
2179 struct kevent32 kev32;
2181 if (ksa->struct_size != sizeof(kev32))
2183 memcpy(&kev32, data, sizeof(kev32));
2184 memset(&kev, 0, sizeof(kev));
2185 kev.ident = kev32.ident;
2186 kev.filter = kev32.filter;
2187 kev.flags = kev32.flags;
2188 kev.fflags = kev32.fflags;
2189 kev.data = kev32.data;
2190 kev.udata = (void *)(uintptr_t)kev32.udata;
2194 printf("<unknown structure> }\n");
2201 printf("invalid record\n");
2204 printf("<bad size> }\n");
2211 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
2212 "[-m maxdata] [-p pid] [-t trstr]\n");