2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1988, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)kdump.c 8.1 (Berkeley) 6/6/93";
43 #include <sys/cdefs.h>
44 #define _WANT_KERNEL_ERRNO
46 #define _WANT_KEVENT32
48 #define _WANT_FREEBSD11_KEVENT
49 #define _WANT_FREEBSD_BITSET
50 #include <sys/param.h>
51 #include <sys/capsicum.h>
52 #include <sys/_bitset.h>
53 #include <sys/bitset.h>
54 #include <sys/errno.h>
57 #include <sys/event.h>
58 #include <sys/ktrace.h>
60 #include <sys/ioctl.h>
61 #include <sys/socket.h>
63 #include <sys/sysent.h>
66 #include <sys/queue.h>
71 #include <arpa/inet.h>
72 #include <netinet/in.h>
74 #include <capsicum_helpers.h>
86 #include <sysdecode.h>
94 #include <libcasper.h>
96 #include <casper/cap_grp.h>
97 #include <casper/cap_pwd.h>
100 int fetchprocinfo(struct ktr_header *, u_int *);
101 u_int findabi(struct ktr_header *);
102 int fread_tail(void *, int, int);
103 void dumpheader(struct ktr_header *, u_int);
104 void dumptimeval(struct ktr_header_v0 *kth);
105 void dumptimespec(struct ktr_header *kth);
106 void ktrsyscall(struct ktr_syscall *, u_int);
107 void ktrsysret(struct ktr_sysret *, u_int);
108 void ktrnamei(char *, int);
109 void hexdump(char *, int, int);
110 void visdump(char *, int, int);
111 void ktrgenio(struct ktr_genio *, int);
112 void ktrpsig(struct ktr_psig *);
113 void ktrcsw(struct ktr_csw *);
114 void ktrcsw_old(struct ktr_csw_old *);
115 void ktruser(int, void *);
116 void ktrcaprights(cap_rights_t *);
117 void ktritimerval(struct itimerval *it);
118 void ktrsockaddr(struct sockaddr *);
119 void ktrstat(struct stat *);
120 void ktrstruct(char *, size_t);
121 void ktrcapfail(struct ktr_cap_fail *);
122 void ktrfault(struct ktr_fault *);
123 void ktrfaultend(struct ktr_faultend *);
124 void ktrkevent(struct kevent *);
125 void ktrstructarray(struct ktr_struct_array *, size_t);
126 void ktrbitset(char *, struct bitset *, size_t);
127 void ktrsyscall_freebsd(struct ktr_syscall *ktr, register_t **resip,
128 int *resnarg, char *resc, u_int sv_flags);
131 #define TIMESTAMP_NONE 0x0
132 #define TIMESTAMP_ABSOLUTE 0x1
133 #define TIMESTAMP_ELAPSED 0x2
134 #define TIMESTAMP_RELATIVE 0x4
136 bool decimal, fancy = true, resolv;
137 static bool abiflag, suppressdata, syscallno, tail, threads, cpuflag;
138 static int timestamp, maxdata;
139 static const char *tracefile = DEF_TRACEFILE;
140 static struct ktr_header ktr_header;
141 static short version;
143 #define TIME_FORMAT "%b %e %T %Y"
144 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
148 TAILQ_ENTRY(proc_info) info;
153 static TAILQ_HEAD(trace_procs, proc_info) trace_procs;
156 static cap_channel_t *cappwd, *capgrp;
159 cappwdgrp_setup(cap_channel_t **cappwdp, cap_channel_t **capgrpp)
161 cap_channel_t *capcas, *cappwdloc, *capgrploc;
162 const char *cmds[1], *fields[1];
165 if (capcas == NULL) {
166 err(1, "unable to create casper process");
169 cappwdloc = cap_service_open(capcas, "system.pwd");
170 capgrploc = cap_service_open(capcas, "system.grp");
171 /* Casper capability no longer needed. */
173 if (cappwdloc == NULL || capgrploc == NULL) {
174 if (cappwdloc == NULL)
175 warn("unable to open system.pwd service");
176 if (capgrploc == NULL)
177 warn("unable to open system.grp service");
180 /* Limit system.pwd to only getpwuid() function and pw_name field. */
181 cmds[0] = "getpwuid";
182 if (cap_pwd_limit_cmds(cappwdloc, cmds, 1) < 0)
183 err(1, "unable to limit system.pwd service");
184 fields[0] = "pw_name";
185 if (cap_pwd_limit_fields(cappwdloc, fields, 1) < 0)
186 err(1, "unable to limit system.pwd service");
187 /* Limit system.grp to only getgrgid() function and gr_name field. */
188 cmds[0] = "getgrgid";
189 if (cap_grp_limit_cmds(capgrploc, cmds, 1) < 0)
190 err(1, "unable to limit system.grp service");
191 fields[0] = "gr_name";
192 if (cap_grp_limit_fields(capgrploc, fields, 1) < 0)
193 err(1, "unable to limit system.grp service");
195 *cappwdp = cappwdloc;
196 *capgrpp = capgrploc;
199 #endif /* WITH_CASPER */
202 print_integer_arg(const char *(*decoder)(int), int value)
206 str = decoder(value);
211 printf("<invalid=%d>", value);
213 printf("<invalid=%#x>", value);
217 /* Like print_integer_arg but unknown values are treated as valid. */
219 print_integer_arg_valid(const char *(*decoder)(int), int value)
223 str = decoder(value);
230 printf("%#x", value);
235 print_mask_arg_part(bool (*decoder)(FILE *, int, int *), int value, int *rem)
238 printf("%#x<", value);
239 return (decoder(stdout, value, rem));
243 print_mask_arg(bool (*decoder)(FILE *, int, int *), int value)
248 invalid = !print_mask_arg_part(decoder, value, &rem);
251 printf("<invalid>%u", rem);
255 print_mask_arg0(bool (*decoder)(FILE *, int, int *), int value)
264 printf("%#x<", value);
265 invalid = !decoder(stdout, value, &rem);
268 printf("<invalid>%u", rem);
272 decode_fileflags(fflags_t value)
281 printf("%#x<", value);
282 invalid = !sysdecode_fileflags(stdout, value, &rem);
285 printf("<invalid>%u", rem);
289 decode_filemode(int value)
298 printf("%#o<", value);
299 invalid = !sysdecode_filemode(stdout, value, &rem);
302 printf("<invalid>%u", rem);
306 print_mask_arg32(bool (*decoder)(FILE *, uint32_t, uint32_t *), uint32_t value)
311 printf("%#x<", value);
312 invalid = !decoder(stdout, value, &rem);
315 printf("<invalid>%u", rem);
319 print_mask_argul(bool (*decoder)(FILE *, u_long, u_long *), u_long value)
328 printf("%#lx<", value);
329 invalid = !decoder(stdout, value, &rem);
332 printf("<invalid>%lu", rem);
336 main(int argc, char *argv[])
338 int ch, ktrlen, size;
340 int trpoints = ALL_POINTS;
345 setlocale(LC_CTYPE, "");
347 timestamp = TIMESTAMP_NONE;
349 while ((ch = getopt(argc,argv,"f:cdElm:np:AHRrSsTt:")) != -1)
367 maxdata = atoi(optarg);
385 timestamp |= TIMESTAMP_ELAPSED;
391 timestamp |= TIMESTAMP_RELATIVE;
394 timestamp |= TIMESTAMP_ABSOLUTE;
397 trpoints = getpoints(optarg);
399 errx(1, "unknown trace point in %s", optarg);
408 m = malloc(size = 1025);
410 errx(1, "%s", strerror(ENOMEM));
411 if (strcmp(tracefile, "-") != 0)
412 if (!freopen(tracefile, "r", stdin))
413 err(1, "%s", tracefile);
415 caph_cache_catpages();
420 if (cappwdgrp_setup(&cappwd, &capgrp) < 0) {
425 if (!resolv || (cappwd != NULL && capgrp != NULL)) {
426 if (caph_enter() < 0)
427 err(1, "unable to enter capability mode");
431 if (caph_enter() < 0)
432 err(1, "unable to enter capability mode");
435 if (caph_limit_stdio() == -1)
436 err(1, "unable to limit stdio");
438 TAILQ_INIT(&trace_procs);
440 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
441 if (ktr_header.ktr_type & KTR_VERSIONED) {
442 ktr_header.ktr_type &= ~KTR_VERSIONED;
443 version = ktr_header.ktr_version;
445 version = KTR_VERSION0;
446 if (ktr_header.ktr_type & KTR_DROP) {
447 ktr_header.ktr_type &= ~KTR_DROP;
448 if (!drop_logged && threads) {
450 "%6d %6d %-8.*s Events dropped.\n",
452 ktr_header.ktr_tid > 0 ?
453 (lwpid_t)ktr_header.ktr_tid : 0,
454 MAXCOMLEN, ktr_header.ktr_comm);
456 } else if (!drop_logged) {
457 printf("%6d %-8.*s Events dropped.\n",
458 ktr_header.ktr_pid, MAXCOMLEN,
459 ktr_header.ktr_comm);
463 if ((ktrlen = ktr_header.ktr_len) < 0)
464 errx(1, "bogus length 0x%x", ktrlen);
466 m = realloc(m, ktrlen+1);
468 errx(1, "%s", strerror(ENOMEM));
471 if (version == KTR_VERSION0 &&
472 fseek(stdin, KTR_OFFSET_V0, SEEK_CUR) < 0)
473 errx(1, "%s", strerror(errno));
474 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
475 errx(1, "data too short");
476 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
478 if (pid && ktr_header.ktr_pid != pid &&
479 ktr_header.ktr_tid != pid)
481 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
483 sv_flags = findabi(&ktr_header);
484 dumpheader(&ktr_header, sv_flags);
486 switch (ktr_header.ktr_type) {
488 ktrsyscall((struct ktr_syscall *)m, sv_flags);
491 ktrsysret((struct ktr_sysret *)m, sv_flags);
498 ktrgenio((struct ktr_genio *)m, ktrlen);
501 ktrpsig((struct ktr_psig *)m);
504 if (ktrlen == sizeof(struct ktr_csw_old))
505 ktrcsw_old((struct ktr_csw_old *)m);
507 ktrcsw((struct ktr_csw *)m);
513 ktrstruct(m, ktrlen);
516 ktrcapfail((struct ktr_cap_fail *)m);
519 ktrfault((struct ktr_fault *)m);
522 ktrfaultend((struct ktr_faultend *)m);
524 case KTR_STRUCT_ARRAY:
525 ktrstructarray((struct ktr_struct_array *)m, ktrlen);
538 fread_tail(void *buf, int size, int num)
542 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
550 fetchprocinfo(struct ktr_header *kth, u_int *flags)
552 struct proc_info *pi;
554 switch (kth->ktr_type) {
556 TAILQ_FOREACH(pi, &trace_procs, info) {
557 if (pi->pid == kth->ktr_pid) {
558 TAILQ_REMOVE(&trace_procs, pi, info);
562 pi = malloc(sizeof(struct proc_info));
564 errx(1, "%s", strerror(ENOMEM));
565 pi->sv_flags = *flags;
566 pi->pid = kth->ktr_pid;
567 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
571 TAILQ_FOREACH(pi, &trace_procs, info) {
572 if (pi->pid == kth->ktr_pid) {
573 TAILQ_REMOVE(&trace_procs, pi, info);
585 findabi(struct ktr_header *kth)
587 struct proc_info *pi;
589 TAILQ_FOREACH(pi, &trace_procs, info) {
590 if (pi->pid == kth->ktr_pid) {
591 return (pi->sv_flags);
598 dumptimeval(struct ktr_header_v0 *kth)
600 static struct timeval prevtime, prevtime_e;
604 if (timestamp & TIMESTAMP_ABSOLUTE) {
605 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
606 kth->ktr_time.tv_usec);
608 if (timestamp & TIMESTAMP_ELAPSED) {
609 if (prevtime_e.tv_sec == 0)
610 prevtime_e = kth->ktr_time;
611 timersub(&kth->ktr_time, &prevtime_e, &temp);
612 printf("%jd.%06ld ", (intmax_t)temp.tv_sec,
615 if (timestamp & TIMESTAMP_RELATIVE) {
616 if (prevtime.tv_sec == 0)
617 prevtime = kth->ktr_time;
618 if (timercmp(&kth->ktr_time, &prevtime, <)) {
619 timersub(&prevtime, &kth->ktr_time, &temp);
622 timersub(&kth->ktr_time, &prevtime, &temp);
625 prevtime = kth->ktr_time;
626 printf("%s%jd.%06ld ", sign, (intmax_t)temp.tv_sec,
632 dumptimespec(struct ktr_header *kth)
634 static struct timespec prevtime, prevtime_e;
635 struct timespec temp;
638 if (timestamp & TIMESTAMP_ABSOLUTE) {
639 printf("%jd.%09ld ", (intmax_t)kth->ktr_time.tv_sec,
640 kth->ktr_time.tv_nsec);
642 if (timestamp & TIMESTAMP_ELAPSED) {
643 if (prevtime_e.tv_sec == 0)
644 prevtime_e = kth->ktr_time;
645 timespecsub(&kth->ktr_time, &prevtime_e, &temp);
646 printf("%jd.%09ld ", (intmax_t)temp.tv_sec,
649 if (timestamp & TIMESTAMP_RELATIVE) {
650 if (prevtime.tv_sec == 0)
651 prevtime = kth->ktr_time;
652 if (timespeccmp(&kth->ktr_time, &prevtime, <)) {
653 timespecsub(&prevtime, &kth->ktr_time, &temp);
656 timespecsub(&kth->ktr_time, &prevtime, &temp);
659 prevtime = kth->ktr_time;
660 printf("%s%jd.%09ld ", sign, (intmax_t)temp.tv_sec,
666 dumpheader(struct ktr_header *kth, u_int sv_flags)
668 static char unknown[64];
673 switch (kth->ktr_type) {
696 case KTR_STRUCT_ARRAY:
712 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
717 * The ktr_tid field was previously the ktr_buffer field, which held
718 * the kernel pointer value for the buffer associated with data
719 * following the record header. It now holds a threadid, but only
720 * for trace files after the change. Older trace files still contain
721 * kernel pointers. Detect this and suppress the results by printing
722 * negative tid's as 0.
725 printf("%6d %6d %-8.*s ", kth->ktr_pid,
726 kth->ktr_tid > 0 ? (lwpid_t)kth->ktr_tid : 0,
727 MAXCOMLEN, kth->ktr_comm);
729 printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
731 if (version == KTR_VERSION0)
732 dumptimeval((struct ktr_header_v0 *)kth);
736 if (cpuflag && version > KTR_VERSION0)
737 printf("%3d ", kth->ktr_cpu);
740 switch (sv_flags & SV_ABI_MASK) {
752 if ((sv_flags & SV_LP64) != 0)
754 else if ((sv_flags & SV_ILP32) != 0)
759 printf("%s%s ", abi, arch);
763 #include <sys/syscall.h>
766 ioctlname(unsigned long val)
770 str = sysdecode_ioctlname(val);
779 static enum sysdecode_abi
780 syscallabi(u_int sv_flags)
784 return (SYSDECODE_ABI_FREEBSD);
785 switch (sv_flags & SV_ABI_MASK) {
787 return (SYSDECODE_ABI_FREEBSD);
790 if (sv_flags & SV_ILP32)
791 return (SYSDECODE_ABI_LINUX32);
793 return (SYSDECODE_ABI_LINUX);
795 return (SYSDECODE_ABI_UNKNOWN);
800 syscallname(u_int code, u_int sv_flags)
804 name = sysdecode_syscallname(syscallabi(sv_flags), code);
806 printf("[%d]", code);
810 printf("[%d]", code);
815 print_signal(int signo)
819 signame = sysdecode_signal(signo);
821 printf("%s", signame);
823 printf("SIG %d", signo);
827 ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags)
829 int narg = ktr->ktr_narg;
832 syscallname(ktr->ktr_code, sv_flags);
833 ip = &ktr->ktr_args[0];
837 switch (sv_flags & SV_ABI_MASK) {
839 ktrsyscall_freebsd(ktr, &ip, &narg, &c,
842 #ifdef SYSDECODE_HAVE_LINUX
845 if (sv_flags & SV_ILP32)
846 ktrsyscall_linux32(ktr, &ip,
850 ktrsyscall_linux(ktr, &ip, &narg, &c);
852 #endif /* SYSDECODE_HAVE_LINUX */
856 print_number(ip, narg, c);
863 ktrsyscall_freebsd(struct ktr_syscall *ktr, register_t **resip,
864 int *resnarg, char *resc, u_int sv_flags)
866 int narg = ktr->ktr_narg;
867 register_t *ip, *first;
869 int quad_align, quad_slots;
871 ip = first = &ktr->ktr_args[0];
875 if (sv_flags & SV_ILP32) {
882 switch (ktr->ktr_code) {
901 print_integer_arg_valid(sysdecode_atfd, *ip);
907 switch (ktr->ktr_code) {
909 print_number(ip, narg, c);
919 print_integer_arg(sysdecode_ptrace_request, *ip);
927 print_number(ip, narg, c);
929 print_mask_arg(sysdecode_access_mode, *ip);
933 case SYS_close_range:
934 print_number(ip, narg, c);
935 print_number(ip, narg, c);
937 print_mask_arg(sysdecode_close_range_flags, *ip);
943 print_number(ip, narg, c);
945 print_mask_arg(sysdecode_open_flags, ip[0]);
946 if ((ip[0] & O_CREAT) == O_CREAT) {
948 decode_filemode(ip[1]);
954 print_number(ip, narg, c);
955 print_number(ip, narg, c);
957 print_mask_arg0(sysdecode_wait4_options, *ip);
963 print_integer_arg(sysdecode_idtype, *ip);
967 print_number64(first, ip, narg, c);
968 print_number(ip, narg, c);
970 print_mask_arg(sysdecode_wait6_options, *ip);
978 print_number(ip, narg, c);
980 decode_filemode(*ip);
985 print_number(ip, narg, c);
987 decode_filemode(*ip);
992 print_number(ip, narg, c);
993 print_number(ip, narg, c);
995 print_integer_arg(sysdecode_getfsstat_mode, *ip);
1000 print_number(ip, narg, c);
1001 print_number(ip, narg, c);
1003 print_mask_arg(sysdecode_mount_flags, *ip);
1008 print_number(ip, narg, c);
1010 print_mask_arg(sysdecode_mount_flags, *ip);
1016 print_number(ip, narg, c);
1017 print_number(ip, narg, c);
1019 print_mask_arg0(sysdecode_msg_flags, *ip);
1025 print_number(ip, narg, c);
1026 print_number(ip, narg, c);
1027 print_number(ip, narg, c);
1029 print_mask_arg0(sysdecode_msg_flags, *ip);
1037 print_number(ip, narg, c);
1039 decode_fileflags(*ip);
1044 print_number(ip, narg, c);
1052 print_mask_arg(sysdecode_reboot_howto, *ip);
1058 decode_filemode(*ip);
1063 print_number(ip, narg, c);
1064 print_number(ip, narg, c);
1066 print_mask_arg(sysdecode_msync_flags, *ip);
1070 #ifdef SYS_freebsd6_mmap
1071 case SYS_freebsd6_mmap:
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);
1085 print_number(ip, narg, c);
1086 print_number(ip, narg, c);
1088 print_mask_arg(sysdecode_mmap_prot, *ip);
1092 print_mask_arg(sysdecode_mmap_flags, *ip);
1097 print_number(ip, narg, c);
1098 print_number(ip, narg, c);
1100 print_mask_arg(sysdecode_mmap_prot, *ip);
1105 print_number(ip, narg, c);
1106 print_number(ip, narg, c);
1108 print_integer_arg(sysdecode_madvice, *ip);
1115 print_number(ip, narg, c);
1117 print_integer_arg(sysdecode_pathconf_name, *ip);
1121 case SYS_getpriority:
1122 case SYS_setpriority:
1124 print_integer_arg(sysdecode_prio_which, *ip);
1130 print_number(ip, narg, c);
1132 print_integer_arg(sysdecode_fcntl_cmd, ip[0]);
1133 if (sysdecode_fcntl_arg_p(ip[0])) {
1135 if (ip[0] == F_SETFL)
1137 sysdecode_fcntl_fileflags,
1140 sysdecode_fcntl_arg(stdout,
1151 print_integer_arg(sysdecode_socketdomain,
1156 print_mask_arg(sysdecode_socket_type, *ip);
1159 if (sockdomain == PF_INET ||
1160 sockdomain == PF_INET6) {
1162 print_integer_arg(sysdecode_ipproto,
1170 case SYS_setsockopt:
1171 case SYS_getsockopt: {
1174 print_number(ip, narg, c);
1176 print_integer_arg_valid(sysdecode_sockopt_level,
1178 str = sysdecode_sockopt_name(ip[0], ip[1]);
1188 #ifdef SYS_freebsd6_lseek
1189 case SYS_freebsd6_lseek:
1190 print_number(ip, narg, c);
1191 /* Hidden 'pad' argument, not in lseek(2) */
1192 print_number(ip, narg, c);
1193 print_number64(first, ip, narg, c);
1195 print_integer_arg(sysdecode_whence, *ip);
1201 print_number(ip, narg, c);
1202 print_number64(first, ip, narg, c);
1204 print_integer_arg(sysdecode_whence, *ip);
1209 print_number(ip, narg, c);
1211 print_mask_arg(sysdecode_flock_operation, *ip);
1219 print_number(ip, narg, c);
1221 decode_filemode(*ip);
1226 print_number(ip, narg, c);
1228 print_integer_arg(sysdecode_shutdown_how, *ip);
1232 case SYS_socketpair:
1234 print_integer_arg(sysdecode_socketdomain, *ip);
1238 print_mask_arg(sysdecode_socket_type, *ip);
1246 print_integer_arg(sysdecode_rlimit, *ip);
1253 print_integer_arg(sysdecode_getrusage_who, *ip);
1259 print_number(ip, narg, c);
1261 if (!sysdecode_quotactl_cmd(stdout, *ip)) {
1263 printf("<invalid=%d>", (int)*ip);
1265 printf("<invalid=%#x>",
1274 print_integer_arg(sysdecode_nfssvc_flags, *ip);
1280 case SYS_rtprio_thread:
1282 print_integer_arg(sysdecode_rtprio_function,
1289 print_number(ip, narg, c);
1290 print_number(ip, narg, c);
1292 print_integer_arg(sysdecode_semctl_cmd, *ip);
1297 print_number(ip, narg, c);
1298 print_number(ip, narg, c);
1300 print_mask_arg(sysdecode_semget_flags, *ip);
1305 print_number(ip, narg, c);
1307 print_integer_arg(sysdecode_msgctl_cmd, *ip);
1312 print_number(ip, narg, c);
1313 print_number(ip, narg, c);
1315 print_mask_arg(sysdecode_shmat_flags, *ip);
1320 print_number(ip, narg, c);
1322 print_integer_arg(sysdecode_shmctl_cmd, *ip);
1326 #ifdef SYS_freebsd12_shm_open
1327 case SYS_freebsd12_shm_open:
1328 if (ip[0] == (uintptr_t)SHM_ANON) {
1329 printf("(SHM_ANON");
1332 print_number(ip, narg, c);
1335 print_mask_arg(sysdecode_open_flags, ip[0]);
1337 decode_filemode(ip[1]);
1343 if (ip[0] == (uintptr_t)SHM_ANON) {
1344 printf("(SHM_ANON");
1347 print_number(ip, narg, c);
1350 print_mask_arg(sysdecode_open_flags, ip[0]);
1352 decode_filemode(ip[1]);
1354 print_mask_arg(sysdecode_shmflags, ip[2]);
1359 print_number(ip, narg, c);
1360 print_number(ip, narg, c);
1362 print_integer_arg(sysdecode_minherit_inherit,
1369 print_mask_arg(sysdecode_rfork_flags, *ip);
1374 case SYS_lio_listio:
1376 print_integer_arg(sysdecode_lio_listio_mode,
1384 print_mask_arg(sysdecode_mlockall_flags, *ip);
1388 case SYS_sched_setscheduler:
1389 print_number(ip, narg, c);
1391 print_integer_arg(sysdecode_scheduler_policy,
1396 case SYS_sched_get_priority_max:
1397 case SYS_sched_get_priority_min:
1399 print_integer_arg(sysdecode_scheduler_policy,
1405 print_number(ip, narg, c);
1406 print_number(ip, narg, c);
1407 print_number(ip, narg, c);
1408 print_number(ip, narg, c);
1409 print_number(ip, narg, c);
1410 print_number(ip, narg, c);
1412 print_mask_arg(sysdecode_sendfile_flags, *ip);
1417 print_number(ip, narg, c);
1419 print_integer_arg(sysdecode_kldsym_cmd, *ip);
1423 case SYS_sigprocmask:
1425 print_integer_arg(sysdecode_sigprocmask_how,
1431 case SYS___acl_get_file:
1432 case SYS___acl_set_file:
1433 case SYS___acl_get_fd:
1434 case SYS___acl_set_fd:
1435 case SYS___acl_delete_file:
1436 case SYS___acl_delete_fd:
1437 case SYS___acl_aclcheck_file:
1438 case SYS___acl_aclcheck_fd:
1439 case SYS___acl_get_link:
1440 case SYS___acl_set_link:
1441 case SYS___acl_delete_link:
1442 case SYS___acl_aclcheck_link:
1443 print_number(ip, narg, c);
1445 print_integer_arg(sysdecode_acltype, *ip);
1456 case SYS_extattrctl:
1457 print_number(ip, narg, c);
1459 print_integer_arg(sysdecode_extattrnamespace,
1465 print_number(ip, narg, c);
1466 print_number(ip, narg, c);
1468 print_mask_arg(sysdecode_mount_flags, *ip);
1472 case SYS_thr_create:
1473 print_number(ip, narg, c);
1474 print_number(ip, narg, c);
1476 print_mask_arg(sysdecode_thr_create_flags, *ip);
1481 print_number(ip, narg, c);
1487 case SYS_kldunloadf:
1488 print_number(ip, narg, c);
1490 print_integer_arg(sysdecode_kldunload_flags,
1498 print_number(ip, narg, c);
1500 print_integer_arg_valid(sysdecode_atfd, *ip);
1503 print_number(ip, narg, c);
1505 case SYS_cap_fcntls_limit:
1506 print_number(ip, narg, c);
1511 print_mask_arg32(sysdecode_cap_fcntlrights, arg);
1513 case SYS_posix_fadvise:
1514 print_number(ip, narg, c);
1515 print_number(ip, narg, c);
1516 print_number(ip, narg, c);
1518 print_integer_arg(sysdecode_fadvice, *ip);
1524 print_integer_arg(sysdecode_idtype, *ip);
1528 print_number64(first, ip, narg, c);
1530 print_integer_arg(sysdecode_procctl_cmd, *ip);
1534 case SYS__umtx_op: {
1537 print_number(ip, narg, c);
1539 if (print_mask_arg_part(sysdecode_umtx_op_flags,
1542 print_integer_arg(sysdecode_umtx_op, op);
1545 case UMTX_OP_CV_WAIT:
1550 sysdecode_umtx_cvwait_flags, *ip);
1552 case UMTX_OP_RW_RDLOCK:
1557 sysdecode_umtx_rwlock_flags, *ip);
1566 print_number(ip, narg, c);
1567 print_number64(first, ip, narg, c);
1570 print_number(ip, narg, c);
1571 print_number(ip, narg, c);
1572 print_number(ip, narg, c);
1576 print_number(ip, narg, c);
1577 print_number(ip, narg, c);
1580 print_number(ip, narg, c);
1584 print_integer_arg(sysdecode_sysarch_number, *ip);
1592 print_integer_arg(sysdecode_itimer, *ip);
1598 switch (ktr->ktr_code) {
1608 print_mask_arg0(sysdecode_atflags, *ip);
1619 ktrsysret(struct ktr_sysret *ktr, u_int sv_flags)
1621 register_t ret = ktr->ktr_retval;
1622 int error = ktr->ktr_error;
1624 syscallname(ktr->ktr_code, sv_flags);
1629 printf("%ld", (long)ret);
1630 if (ret < 0 || ret > 9)
1631 printf("/%#lx", (unsigned long)ret);
1634 printf("%ld", (long)ret);
1636 printf("%#lx", (unsigned long)ret);
1638 } else if (error == ERESTART)
1640 else if (error == EJUSTRETURN)
1641 printf("JUSTRETURN");
1643 printf("-1 errno %d", sysdecode_freebsd_to_abi_errno(
1644 syscallabi(sv_flags), error));
1646 printf(" %s", strerror(ktr->ktr_error));
1652 ktrnamei(char *cp, int len)
1654 printf("\"%.*s\"\n", len, cp);
1658 hexdump(char *p, int len, int screenwidth)
1666 i = 13; /* base offset */
1667 i += (width / 2) + 1; /* spaces every second byte */
1668 i += (width * 2); /* width of bytes */
1670 i += width; /* each byte */
1672 } while (i < screenwidth);
1675 for (n = 0; n < len; n += width) {
1676 for (i = n; i < n + width; i++) {
1677 if ((i % width) == 0) { /* beginning of line */
1678 printf(" 0x%04x", i);
1684 printf("%02x", p[i] & 0xff);
1689 for (i = n; i < n + width; i++) {
1692 if (p[i] >= ' ' && p[i] <= '~')
1699 if ((i % width) != 0)
1704 visdump(char *dp, int datalen, int screenwidth)
1713 for (;datalen > 0; datalen--, dp++) {
1714 vis(visbuf, *dp, VIS_CSTYLE | VIS_NOLOCALE, *(dp+1));
1717 * Keep track of printables and
1718 * space chars (like fold(1)).
1730 width = 8 - (col&07);
1735 if (col + width > (screenwidth-2)) {
1750 ktrgenio(struct ktr_genio *ktr, int len)
1752 int datalen = len - sizeof (struct ktr_genio);
1753 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1754 static int screenwidth = 0;
1757 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1758 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1759 datalen == 1 ? "" : "s");
1762 if (screenwidth == 0) {
1765 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1767 screenwidth = ws.ws_col;
1771 if (maxdata && datalen > maxdata)
1774 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1775 if (dp[i] >= 32 && dp[i] < 127)
1777 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1782 hexdump(dp, datalen, screenwidth);
1784 visdump(dp, datalen, screenwidth);
1788 ktrpsig(struct ktr_psig *psig)
1792 print_signal(psig->signo);
1793 if (psig->action == SIG_DFL) {
1796 printf(" caught handler=0x%lx mask=0x%x",
1797 (u_long)psig->action, psig->mask.__bits[0]);
1800 str = sysdecode_sigcode(psig->signo, psig->code);
1804 printf("<invalid=%#x>", psig->code);
1809 ktrcsw_old(struct ktr_csw_old *cs)
1811 printf("%s %s\n", cs->out ? "stop" : "resume",
1812 cs->user ? "user" : "kernel");
1816 ktrcsw(struct ktr_csw *cs)
1818 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1819 cs->user ? "user" : "kernel", cs->wmesg);
1823 ktruser(int len, void *p)
1827 if (sysdecode_utrace(stdout, p, len)) {
1836 printf(" %d", *cp++);
1838 printf(" %02x", *cp++);
1843 ktrcaprights(cap_rights_t *rightsp)
1846 printf("cap_rights_t ");
1847 sysdecode_cap_rights(stdout, rightsp);
1852 ktrtimeval(struct timeval *tv)
1855 printf("{%ld, %ld}", (long)tv->tv_sec, tv->tv_usec);
1859 ktritimerval(struct itimerval *it)
1862 printf("itimerval { .interval = ");
1863 ktrtimeval(&it->it_interval);
1864 printf(", .value = ");
1865 ktrtimeval(&it->it_value);
1870 ktrsockaddr(struct sockaddr *sa)
1873 TODO: Support additional address families
1874 #include <netsmb/netbios.h>
1875 struct sockaddr_nb *nb;
1881 * note: ktrstruct() has already verified that sa points to a
1882 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1883 * sa->sa_len bytes long.
1885 printf("struct sockaddr { ");
1886 str = sysdecode_sockaddr_family(sa->sa_family);
1890 printf("<invalid=%d>", sa->sa_family);
1893 #define check_sockaddr_len(n) \
1894 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1895 printf("invalid"); \
1899 switch(sa->sa_family) {
1901 struct sockaddr_in sa_in;
1903 memset(&sa_in, 0, sizeof(sa_in));
1904 memcpy(&sa_in, sa, sa->sa_len);
1905 check_sockaddr_len(in);
1906 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1907 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1911 struct sockaddr_in6 sa_in6;
1913 memset(&sa_in6, 0, sizeof(sa_in6));
1914 memcpy(&sa_in6, sa, sa->sa_len);
1915 check_sockaddr_len(in6);
1916 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1917 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1918 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1922 struct sockaddr_un sa_un;
1924 memset(&sa_un, 0, sizeof(sa_un));
1925 memcpy(&sa_un, sa, sa->sa_len);
1926 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1930 printf("unknown address family");
1936 ktrstat(struct stat *statp)
1938 char mode[12], timestr[PATH_MAX + 4];
1944 * note: ktrstruct() has already verified that statp points to a
1945 * buffer exactly sizeof(struct stat) bytes long.
1947 printf("struct stat {");
1948 printf("dev=%ju, ino=%ju, ",
1949 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1951 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1953 strmode(statp->st_mode, mode);
1954 printf("mode=%s, ", mode);
1956 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1962 pwd = cap_getpwuid(cappwd, statp->st_uid);
1965 pwd = getpwuid(statp->st_uid);
1968 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1970 printf("uid=\"%s\", ", pwd->pw_name);
1976 grp = cap_getgrgid(capgrp, statp->st_gid);
1979 grp = getgrgid(statp->st_gid);
1982 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1984 printf("gid=\"%s\", ", grp->gr_name);
1985 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1988 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1990 tm = localtime(&statp->st_atim.tv_sec);
1991 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1992 printf("\"%s\"", timestr);
1994 if (statp->st_atim.tv_nsec != 0)
1995 printf(".%09ld, ", statp->st_atim.tv_nsec);
2000 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
2002 tm = localtime(&statp->st_mtim.tv_sec);
2003 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
2004 printf("\"%s\"", timestr);
2006 if (statp->st_mtim.tv_nsec != 0)
2007 printf(".%09ld, ", statp->st_mtim.tv_nsec);
2012 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
2014 tm = localtime(&statp->st_ctim.tv_sec);
2015 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
2016 printf("\"%s\"", timestr);
2018 if (statp->st_ctim.tv_nsec != 0)
2019 printf(".%09ld, ", statp->st_ctim.tv_nsec);
2022 printf("birthtime=");
2024 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
2026 tm = localtime(&statp->st_birthtim.tv_sec);
2027 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
2028 printf("\"%s\"", timestr);
2030 if (statp->st_birthtim.tv_nsec != 0)
2031 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
2034 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
2035 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
2036 (intmax_t)statp->st_blocks, statp->st_flags);
2041 ktrbitset(char *name, struct bitset *set, size_t setlen)
2045 if (setlen > INT32_MAX)
2047 maxi = setlen * CHAR_BIT;
2048 printf("%s [ ", name);
2049 for (i = 0; i < maxi; i++) {
2050 if (!BIT_ISSET(setlen, i, set))
2059 printf(" empty ]\n");
2065 ktrstruct(char *buf, size_t buflen)
2068 size_t namelen, datalen;
2070 cap_rights_t rights;
2071 struct itimerval it;
2073 struct sockaddr_storage ss;
2076 for (name = buf, namelen = 0;
2077 namelen < buflen && name[namelen] != '\0';
2080 if (namelen == buflen)
2082 if (name[namelen] != '\0')
2084 data = buf + namelen + 1;
2085 datalen = buflen - namelen - 1;
2089 for (i = 0; i < (int)namelen; ++i)
2090 if (!isalpha(name[i]) && name[i] != '_')
2092 if (strcmp(name, "caprights") == 0) {
2093 if (datalen != sizeof(cap_rights_t))
2095 memcpy(&rights, data, datalen);
2096 ktrcaprights(&rights);
2097 } else if (strcmp(name, "itimerval") == 0) {
2098 if (datalen != sizeof(struct itimerval))
2100 memcpy(&it, data, datalen);
2102 } else if (strcmp(name, "stat") == 0) {
2103 if (datalen != sizeof(struct stat))
2105 memcpy(&sb, data, datalen);
2107 } else if (strcmp(name, "sockaddr") == 0) {
2108 if (datalen > sizeof(ss))
2110 memcpy(&ss, data, datalen);
2111 if (datalen != ss.ss_len)
2113 ktrsockaddr((struct sockaddr *)&ss);
2114 } else if (strcmp(name, "cpuset_t") == 0) {
2117 set = malloc(datalen);
2119 errx(1, "%s", strerror(ENOMEM));
2120 memcpy(set, data, datalen);
2121 ktrbitset(name, set, datalen);
2124 #ifdef SYSDECODE_HAVE_LINUX
2125 if (ktrstruct_linux(name, data, datalen) == false)
2127 printf("unknown structure\n");
2131 printf("invalid record\n");
2135 ktrcapfail(struct ktr_cap_fail *ktr)
2137 switch (ktr->cap_type) {
2138 case CAPFAIL_NOTCAPABLE:
2139 /* operation on fd with insufficient capabilities */
2140 printf("operation requires ");
2141 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2142 printf(", descriptor holds ");
2143 sysdecode_cap_rights(stdout, &ktr->cap_held);
2145 case CAPFAIL_INCREASE:
2146 /* requested more capabilities than fd already has */
2147 printf("attempt to increase capabilities from ");
2148 sysdecode_cap_rights(stdout, &ktr->cap_held);
2150 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2152 case CAPFAIL_SYSCALL:
2153 /* called restricted syscall */
2154 printf("disallowed system call");
2156 case CAPFAIL_LOOKUP:
2157 /* absolute or AT_FDCWD path, ".." path, etc. */
2158 printf("restricted VFS lookup");
2161 printf("unknown capability failure: ");
2162 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2164 sysdecode_cap_rights(stdout, &ktr->cap_held);
2171 ktrfault(struct ktr_fault *ktr)
2174 printf("0x%jx ", (uintmax_t)ktr->vaddr);
2175 print_mask_arg(sysdecode_vmprot, ktr->type);
2180 ktrfaultend(struct ktr_faultend *ktr)
2184 str = sysdecode_vmresult(ktr->result);
2188 printf("<invalid=%d>", ktr->result);
2193 ktrkevent(struct kevent *kev)
2197 switch (kev->filter) {
2203 case EVFILT_PROCDESC:
2205 printf("%ju", (uintmax_t)kev->ident);
2208 print_signal(kev->ident);
2211 printf("%p", (void *)kev->ident);
2213 printf(", filter=");
2214 print_integer_arg(sysdecode_kevent_filter, kev->filter);
2216 print_mask_arg0(sysdecode_kevent_flags, kev->flags);
2217 printf(", fflags=");
2218 sysdecode_kevent_fflags(stdout, kev->filter, kev->fflags,
2220 printf(", data=%#jx, udata=%p }", (uintmax_t)kev->data, kev->udata);
2224 ktrstructarray(struct ktr_struct_array *ksa, size_t buflen)
2228 size_t namelen, datalen;
2232 buflen -= sizeof(*ksa);
2233 for (name = (char *)(ksa + 1), namelen = 0;
2234 namelen < buflen && name[namelen] != '\0';
2237 if (namelen == buflen)
2239 if (name[namelen] != '\0')
2242 for (i = 0; i < (int)namelen; ++i)
2243 if (!isalnum(name[i]) && name[i] != '_')
2245 data = name + namelen + 1;
2246 datalen = buflen - namelen - 1;
2247 printf("struct %s[] = { ", name);
2249 for (; datalen >= ksa->struct_size;
2250 data += ksa->struct_size, datalen -= ksa->struct_size) {
2255 if (strcmp(name, "kevent") == 0) {
2256 if (ksa->struct_size != sizeof(kev))
2258 memcpy(&kev, data, sizeof(kev));
2260 } else if (strcmp(name, "freebsd11_kevent") == 0) {
2261 struct freebsd11_kevent kev11;
2263 if (ksa->struct_size != sizeof(kev11))
2265 memcpy(&kev11, data, sizeof(kev11));
2266 memset(&kev, 0, sizeof(kev));
2267 kev.ident = kev11.ident;
2268 kev.filter = kev11.filter;
2269 kev.flags = kev11.flags;
2270 kev.fflags = kev11.fflags;
2271 kev.data = kev11.data;
2272 kev.udata = kev11.udata;
2274 #ifdef _WANT_KEVENT32
2275 } else if (strcmp(name, "kevent32") == 0) {
2276 struct kevent32 kev32;
2278 if (ksa->struct_size != sizeof(kev32))
2280 memcpy(&kev32, data, sizeof(kev32));
2281 memset(&kev, 0, sizeof(kev));
2282 kev.ident = kev32.ident;
2283 kev.filter = kev32.filter;
2284 kev.flags = kev32.flags;
2285 kev.fflags = kev32.fflags;
2286 #if BYTE_ORDER == BIG_ENDIAN
2287 kev.data = kev32.data2 | ((int64_t)kev32.data1 << 32);
2289 kev.data = kev32.data1 | ((int64_t)kev32.data2 << 32);
2291 kev.udata = (void *)(uintptr_t)kev32.udata;
2293 } else if (strcmp(name, "freebsd11_kevent32") == 0) {
2294 struct freebsd11_kevent32 kev32;
2296 if (ksa->struct_size != sizeof(kev32))
2298 memcpy(&kev32, data, sizeof(kev32));
2299 memset(&kev, 0, sizeof(kev));
2300 kev.ident = kev32.ident;
2301 kev.filter = kev32.filter;
2302 kev.flags = kev32.flags;
2303 kev.fflags = kev32.fflags;
2304 kev.data = kev32.data;
2305 kev.udata = (void *)(uintptr_t)kev32.udata;
2309 printf("<unknown structure> }\n");
2316 printf("invalid record\n");
2319 printf("<bad size> }\n");
2326 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
2327 "[-m maxdata] [-p pid] [-t trstr]\n");