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 __FBSDID("$FreeBSD$");
46 #define _WANT_KERNEL_ERRNO
48 #define _WANT_KEVENT32
50 #define _WANT_FREEBSD11_KEVENT
51 #define _WANT_FREEBSD_BITSET
52 #include <sys/param.h>
53 #include <sys/capsicum.h>
54 #include <sys/_bitset.h>
55 #include <sys/bitset.h>
56 #include <sys/errno.h>
59 #include <sys/event.h>
60 #include <sys/ktrace.h>
62 #include <sys/ioctl.h>
63 #include <sys/socket.h>
65 #include <sys/sysent.h>
68 #include <sys/queue.h>
73 #include <arpa/inet.h>
74 #include <netinet/in.h>
76 #include <capsicum_helpers.h>
88 #include <sysdecode.h>
96 #include <libcasper.h>
98 #include <casper/cap_grp.h>
99 #include <casper/cap_pwd.h>
102 int fetchprocinfo(struct ktr_header *, u_int *);
103 u_int findabi(struct ktr_header *);
104 int fread_tail(void *, int, int);
105 void dumpheader(struct ktr_header *, u_int);
106 void dumptimeval(struct ktr_header_v0 *kth);
107 void dumptimespec(struct ktr_header *kth);
108 void ktrsyscall(struct ktr_syscall *, u_int);
109 void ktrsysret(struct ktr_sysret *, u_int);
110 void ktrnamei(char *, int);
111 void hexdump(char *, int, int);
112 void visdump(char *, int, int);
113 void ktrgenio(struct ktr_genio *, int);
114 void ktrpsig(struct ktr_psig *);
115 void ktrcsw(struct ktr_csw *);
116 void ktrcsw_old(struct ktr_csw_old *);
117 void ktruser(int, void *);
118 void ktrcaprights(cap_rights_t *);
119 void ktritimerval(struct itimerval *it);
120 void ktrsockaddr(struct sockaddr *);
121 void ktrstat(struct stat *);
122 void ktrstruct(char *, size_t);
123 void ktrcapfail(struct ktr_cap_fail *);
124 void ktrfault(struct ktr_fault *);
125 void ktrfaultend(struct ktr_faultend *);
126 void ktrkevent(struct kevent *);
127 void ktrstructarray(struct ktr_struct_array *, size_t);
128 void ktrbitset(char *, struct bitset *, size_t);
129 void ktrsyscall_freebsd(struct ktr_syscall *ktr, register_t **resip,
130 int *resnarg, char *resc, u_int sv_flags);
133 #define TIMESTAMP_NONE 0x0
134 #define TIMESTAMP_ABSOLUTE 0x1
135 #define TIMESTAMP_ELAPSED 0x2
136 #define TIMESTAMP_RELATIVE 0x4
138 bool decimal, fancy = true, resolv;
139 static bool abiflag, suppressdata, syscallno, tail, threads, cpuflag;
140 static int timestamp, maxdata;
141 static const char *tracefile = DEF_TRACEFILE;
142 static struct ktr_header ktr_header;
143 static short version;
145 #define TIME_FORMAT "%b %e %T %Y"
146 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
150 TAILQ_ENTRY(proc_info) info;
155 static TAILQ_HEAD(trace_procs, proc_info) trace_procs;
158 static cap_channel_t *cappwd, *capgrp;
161 cappwdgrp_setup(cap_channel_t **cappwdp, cap_channel_t **capgrpp)
163 cap_channel_t *capcas, *cappwdloc, *capgrploc;
164 const char *cmds[1], *fields[1];
167 if (capcas == NULL) {
168 err(1, "unable to create casper process");
171 cappwdloc = cap_service_open(capcas, "system.pwd");
172 capgrploc = cap_service_open(capcas, "system.grp");
173 /* Casper capability no longer needed. */
175 if (cappwdloc == NULL || capgrploc == NULL) {
176 if (cappwdloc == NULL)
177 warn("unable to open system.pwd service");
178 if (capgrploc == NULL)
179 warn("unable to open system.grp service");
182 /* Limit system.pwd to only getpwuid() function and pw_name field. */
183 cmds[0] = "getpwuid";
184 if (cap_pwd_limit_cmds(cappwdloc, cmds, 1) < 0)
185 err(1, "unable to limit system.pwd service");
186 fields[0] = "pw_name";
187 if (cap_pwd_limit_fields(cappwdloc, fields, 1) < 0)
188 err(1, "unable to limit system.pwd service");
189 /* Limit system.grp to only getgrgid() function and gr_name field. */
190 cmds[0] = "getgrgid";
191 if (cap_grp_limit_cmds(capgrploc, cmds, 1) < 0)
192 err(1, "unable to limit system.grp service");
193 fields[0] = "gr_name";
194 if (cap_grp_limit_fields(capgrploc, fields, 1) < 0)
195 err(1, "unable to limit system.grp service");
197 *cappwdp = cappwdloc;
198 *capgrpp = capgrploc;
201 #endif /* WITH_CASPER */
204 print_integer_arg(const char *(*decoder)(int), int value)
208 str = decoder(value);
213 printf("<invalid=%d>", value);
215 printf("<invalid=%#x>", value);
219 /* Like print_integer_arg but unknown values are treated as valid. */
221 print_integer_arg_valid(const char *(*decoder)(int), int value)
225 str = decoder(value);
232 printf("%#x", value);
237 print_mask_arg_part(bool (*decoder)(FILE *, int, int *), int value, int *rem)
240 printf("%#x<", value);
241 return (decoder(stdout, value, rem));
245 print_mask_arg(bool (*decoder)(FILE *, int, int *), int value)
250 invalid = !print_mask_arg_part(decoder, value, &rem);
253 printf("<invalid>%u", rem);
257 print_mask_arg0(bool (*decoder)(FILE *, int, int *), int value)
266 printf("%#x<", value);
267 invalid = !decoder(stdout, value, &rem);
270 printf("<invalid>%u", rem);
274 decode_fileflags(fflags_t value)
283 printf("%#x<", value);
284 invalid = !sysdecode_fileflags(stdout, value, &rem);
287 printf("<invalid>%u", rem);
291 decode_filemode(int value)
300 printf("%#o<", value);
301 invalid = !sysdecode_filemode(stdout, value, &rem);
304 printf("<invalid>%u", rem);
308 print_mask_arg32(bool (*decoder)(FILE *, uint32_t, uint32_t *), uint32_t value)
313 printf("%#x<", value);
314 invalid = !decoder(stdout, value, &rem);
317 printf("<invalid>%u", rem);
321 print_mask_argul(bool (*decoder)(FILE *, u_long, u_long *), u_long value)
330 printf("%#lx<", value);
331 invalid = !decoder(stdout, value, &rem);
334 printf("<invalid>%lu", rem);
338 main(int argc, char *argv[])
340 int ch, ktrlen, size;
342 int trpoints = ALL_POINTS;
347 setlocale(LC_CTYPE, "");
349 timestamp = TIMESTAMP_NONE;
351 while ((ch = getopt(argc,argv,"f:cdElm:np:AHRrSsTt:")) != -1)
369 maxdata = atoi(optarg);
387 timestamp |= TIMESTAMP_ELAPSED;
393 timestamp |= TIMESTAMP_RELATIVE;
396 timestamp |= TIMESTAMP_ABSOLUTE;
399 trpoints = getpoints(optarg);
401 errx(1, "unknown trace point in %s", optarg);
410 m = malloc(size = 1025);
412 errx(1, "%s", strerror(ENOMEM));
413 if (strcmp(tracefile, "-") != 0)
414 if (!freopen(tracefile, "r", stdin))
415 err(1, "%s", tracefile);
417 caph_cache_catpages();
422 if (cappwdgrp_setup(&cappwd, &capgrp) < 0) {
427 if (!resolv || (cappwd != NULL && capgrp != NULL)) {
428 if (caph_enter() < 0)
429 err(1, "unable to enter capability mode");
433 if (caph_enter() < 0)
434 err(1, "unable to enter capability mode");
437 if (caph_limit_stdio() == -1)
438 err(1, "unable to limit stdio");
440 TAILQ_INIT(&trace_procs);
442 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
443 if (ktr_header.ktr_type & KTR_VERSIONED) {
444 ktr_header.ktr_type &= ~KTR_VERSIONED;
445 version = ktr_header.ktr_version;
447 version = KTR_VERSION0;
448 if (ktr_header.ktr_type & KTR_DROP) {
449 ktr_header.ktr_type &= ~KTR_DROP;
450 if (!drop_logged && threads) {
452 "%6jd %6jd %-8.*s Events dropped.\n",
453 (intmax_t)ktr_header.ktr_pid,
454 ktr_header.ktr_tid > 0 ?
455 (intmax_t)ktr_header.ktr_tid : 0,
456 MAXCOMLEN, ktr_header.ktr_comm);
458 } else if (!drop_logged) {
459 printf("%6jd %-8.*s Events dropped.\n",
460 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
461 ktr_header.ktr_comm);
465 if ((ktrlen = ktr_header.ktr_len) < 0)
466 errx(1, "bogus length 0x%x", ktrlen);
468 m = realloc(m, ktrlen+1);
470 errx(1, "%s", strerror(ENOMEM));
473 if (version == KTR_VERSION0 &&
474 fseek(stdin, KTR_OFFSET_V0, SEEK_CUR) < 0)
475 errx(1, "%s", strerror(errno));
476 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
477 errx(1, "data too short");
478 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
480 if (pid && ktr_header.ktr_pid != pid &&
481 ktr_header.ktr_tid != pid)
483 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
485 sv_flags = findabi(&ktr_header);
486 dumpheader(&ktr_header, sv_flags);
488 switch (ktr_header.ktr_type) {
490 ktrsyscall((struct ktr_syscall *)m, sv_flags);
493 ktrsysret((struct ktr_sysret *)m, sv_flags);
500 ktrgenio((struct ktr_genio *)m, ktrlen);
503 ktrpsig((struct ktr_psig *)m);
506 if (ktrlen == sizeof(struct ktr_csw_old))
507 ktrcsw_old((struct ktr_csw_old *)m);
509 ktrcsw((struct ktr_csw *)m);
515 ktrstruct(m, ktrlen);
518 ktrcapfail((struct ktr_cap_fail *)m);
521 ktrfault((struct ktr_fault *)m);
524 ktrfaultend((struct ktr_faultend *)m);
526 case KTR_STRUCT_ARRAY:
527 ktrstructarray((struct ktr_struct_array *)m, ktrlen);
540 fread_tail(void *buf, int size, int num)
544 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
552 fetchprocinfo(struct ktr_header *kth, u_int *flags)
554 struct proc_info *pi;
556 switch (kth->ktr_type) {
558 TAILQ_FOREACH(pi, &trace_procs, info) {
559 if (pi->pid == kth->ktr_pid) {
560 TAILQ_REMOVE(&trace_procs, pi, info);
564 pi = malloc(sizeof(struct proc_info));
566 errx(1, "%s", strerror(ENOMEM));
567 pi->sv_flags = *flags;
568 pi->pid = kth->ktr_pid;
569 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
573 TAILQ_FOREACH(pi, &trace_procs, info) {
574 if (pi->pid == kth->ktr_pid) {
575 TAILQ_REMOVE(&trace_procs, pi, info);
587 findabi(struct ktr_header *kth)
589 struct proc_info *pi;
591 TAILQ_FOREACH(pi, &trace_procs, info) {
592 if (pi->pid == kth->ktr_pid) {
593 return (pi->sv_flags);
600 dumptimeval(struct ktr_header_v0 *kth)
602 static struct timeval prevtime, prevtime_e;
606 if (timestamp & TIMESTAMP_ABSOLUTE) {
607 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
608 kth->ktr_time.tv_usec);
610 if (timestamp & TIMESTAMP_ELAPSED) {
611 if (prevtime_e.tv_sec == 0)
612 prevtime_e = kth->ktr_time;
613 timersub(&kth->ktr_time, &prevtime_e, &temp);
614 printf("%jd.%06ld ", (intmax_t)temp.tv_sec,
617 if (timestamp & TIMESTAMP_RELATIVE) {
618 if (prevtime.tv_sec == 0)
619 prevtime = kth->ktr_time;
620 if (timercmp(&kth->ktr_time, &prevtime, <)) {
621 timersub(&prevtime, &kth->ktr_time, &temp);
624 timersub(&kth->ktr_time, &prevtime, &temp);
627 prevtime = kth->ktr_time;
628 printf("%s%jd.%06ld ", sign, (intmax_t)temp.tv_sec,
634 dumptimespec(struct ktr_header *kth)
636 static struct timespec prevtime, prevtime_e;
637 struct timespec temp;
640 if (timestamp & TIMESTAMP_ABSOLUTE) {
641 printf("%jd.%09ld ", (intmax_t)kth->ktr_time.tv_sec,
642 kth->ktr_time.tv_nsec);
644 if (timestamp & TIMESTAMP_ELAPSED) {
645 if (prevtime_e.tv_sec == 0)
646 prevtime_e = kth->ktr_time;
647 timespecsub(&kth->ktr_time, &prevtime_e, &temp);
648 printf("%jd.%09ld ", (intmax_t)temp.tv_sec,
651 if (timestamp & TIMESTAMP_RELATIVE) {
652 if (prevtime.tv_sec == 0)
653 prevtime = kth->ktr_time;
654 if (timespeccmp(&kth->ktr_time, &prevtime, <)) {
655 timespecsub(&prevtime, &kth->ktr_time, &temp);
658 timespecsub(&kth->ktr_time, &prevtime, &temp);
661 prevtime = kth->ktr_time;
662 printf("%s%jd.%09ld ", sign, (intmax_t)temp.tv_sec,
668 dumpheader(struct ktr_header *kth, u_int sv_flags)
670 static char unknown[64];
675 switch (kth->ktr_type) {
698 case KTR_STRUCT_ARRAY:
714 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
719 * The ktr_tid field was previously the ktr_buffer field, which held
720 * the kernel pointer value for the buffer associated with data
721 * following the record header. It now holds a threadid, but only
722 * for trace files after the change. Older trace files still contain
723 * kernel pointers. Detect this and suppress the results by printing
724 * negative tid's as 0.
727 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
728 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
729 MAXCOMLEN, kth->ktr_comm);
731 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
734 if (version == KTR_VERSION0)
735 dumptimeval((struct ktr_header_v0 *)kth);
739 if (cpuflag && version > KTR_VERSION0)
740 printf("%3d ", kth->ktr_cpu);
743 switch (sv_flags & SV_ABI_MASK) {
755 if ((sv_flags & SV_LP64) != 0)
757 else if ((sv_flags & SV_ILP32) != 0)
762 printf("%s%s ", abi, arch);
766 #include <sys/syscall.h>
769 ioctlname(unsigned long val)
773 str = sysdecode_ioctlname(val);
782 static enum sysdecode_abi
783 syscallabi(u_int sv_flags)
787 return (SYSDECODE_ABI_FREEBSD);
788 switch (sv_flags & SV_ABI_MASK) {
790 return (SYSDECODE_ABI_FREEBSD);
793 if (sv_flags & SV_ILP32)
794 return (SYSDECODE_ABI_LINUX32);
796 return (SYSDECODE_ABI_LINUX);
798 return (SYSDECODE_ABI_UNKNOWN);
803 syscallname(u_int code, u_int sv_flags)
807 name = sysdecode_syscallname(syscallabi(sv_flags), code);
809 printf("[%d]", code);
813 printf("[%d]", code);
818 print_signal(int signo)
822 signame = sysdecode_signal(signo);
824 printf("%s", signame);
826 printf("SIG %d", signo);
830 ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags)
832 int narg = ktr->ktr_narg;
835 syscallname(ktr->ktr_code, sv_flags);
836 ip = &ktr->ktr_args[0];
840 switch (sv_flags & SV_ABI_MASK) {
842 ktrsyscall_freebsd(ktr, &ip, &narg, &c,
845 #ifdef SYSDECODE_HAVE_LINUX
848 if (sv_flags & SV_ILP32)
849 ktrsyscall_linux32(ktr, &ip,
853 ktrsyscall_linux(ktr, &ip, &narg, &c);
855 #endif /* SYSDECODE_HAVE_LINUX */
859 print_number(ip, narg, c);
866 ktrsyscall_freebsd(struct ktr_syscall *ktr, register_t **resip,
867 int *resnarg, char *resc, u_int sv_flags)
869 int narg = ktr->ktr_narg;
870 register_t *ip, *first;
872 int quad_align, quad_slots;
874 ip = first = &ktr->ktr_args[0];
878 if (sv_flags & SV_ILP32) {
885 switch (ktr->ktr_code) {
904 print_integer_arg_valid(sysdecode_atfd, *ip);
910 switch (ktr->ktr_code) {
912 print_number(ip, narg, c);
922 print_integer_arg(sysdecode_ptrace_request, *ip);
930 print_number(ip, narg, c);
932 print_mask_arg(sysdecode_access_mode, *ip);
936 case SYS_close_range:
937 print_number(ip, narg, c);
938 print_number(ip, narg, c);
940 print_mask_arg(sysdecode_close_range_flags, *ip);
946 print_number(ip, narg, c);
948 print_mask_arg(sysdecode_open_flags, ip[0]);
949 if ((ip[0] & O_CREAT) == O_CREAT) {
951 decode_filemode(ip[1]);
957 print_number(ip, narg, c);
958 print_number(ip, narg, c);
960 print_mask_arg0(sysdecode_wait4_options, *ip);
966 print_integer_arg(sysdecode_idtype, *ip);
970 print_number64(first, ip, narg, c);
971 print_number(ip, narg, c);
973 print_mask_arg(sysdecode_wait6_options, *ip);
981 print_number(ip, narg, c);
983 decode_filemode(*ip);
988 print_number(ip, narg, c);
990 decode_filemode(*ip);
995 print_number(ip, narg, c);
996 print_number(ip, narg, c);
998 print_integer_arg(sysdecode_getfsstat_mode, *ip);
1003 print_number(ip, narg, c);
1004 print_number(ip, narg, c);
1006 print_mask_arg(sysdecode_mount_flags, *ip);
1011 print_number(ip, narg, c);
1013 print_mask_arg(sysdecode_mount_flags, *ip);
1019 print_number(ip, narg, c);
1020 print_number(ip, narg, c);
1022 print_mask_arg0(sysdecode_msg_flags, *ip);
1028 print_number(ip, narg, c);
1029 print_number(ip, narg, c);
1030 print_number(ip, narg, c);
1032 print_mask_arg0(sysdecode_msg_flags, *ip);
1040 print_number(ip, narg, c);
1042 decode_fileflags(*ip);
1047 print_number(ip, narg, c);
1055 print_mask_arg(sysdecode_reboot_howto, *ip);
1061 decode_filemode(*ip);
1066 print_number(ip, narg, c);
1067 print_number(ip, narg, c);
1069 print_mask_arg(sysdecode_msync_flags, *ip);
1073 #ifdef SYS_freebsd6_mmap
1074 case SYS_freebsd6_mmap:
1075 print_number(ip, narg, c);
1076 print_number(ip, narg, c);
1078 print_mask_arg(sysdecode_mmap_prot, *ip);
1082 print_mask_arg(sysdecode_mmap_flags, *ip);
1088 print_number(ip, narg, c);
1089 print_number(ip, narg, c);
1091 print_mask_arg(sysdecode_mmap_prot, *ip);
1095 print_mask_arg(sysdecode_mmap_flags, *ip);
1100 print_number(ip, narg, c);
1101 print_number(ip, narg, c);
1103 print_mask_arg(sysdecode_mmap_prot, *ip);
1108 print_number(ip, narg, c);
1109 print_number(ip, narg, c);
1111 print_integer_arg(sysdecode_madvice, *ip);
1118 print_number(ip, narg, c);
1120 print_integer_arg(sysdecode_pathconf_name, *ip);
1124 case SYS_getpriority:
1125 case SYS_setpriority:
1127 print_integer_arg(sysdecode_prio_which, *ip);
1133 print_number(ip, narg, c);
1135 print_integer_arg(sysdecode_fcntl_cmd, ip[0]);
1136 if (sysdecode_fcntl_arg_p(ip[0])) {
1138 if (ip[0] == F_SETFL)
1140 sysdecode_fcntl_fileflags,
1143 sysdecode_fcntl_arg(stdout,
1154 print_integer_arg(sysdecode_socketdomain,
1159 print_mask_arg(sysdecode_socket_type, *ip);
1162 if (sockdomain == PF_INET ||
1163 sockdomain == PF_INET6) {
1165 print_integer_arg(sysdecode_ipproto,
1173 case SYS_setsockopt:
1174 case SYS_getsockopt: {
1177 print_number(ip, narg, c);
1179 print_integer_arg_valid(sysdecode_sockopt_level,
1181 str = sysdecode_sockopt_name(ip[0], ip[1]);
1191 #ifdef SYS_freebsd6_lseek
1192 case SYS_freebsd6_lseek:
1193 print_number(ip, narg, c);
1194 /* Hidden 'pad' argument, not in lseek(2) */
1195 print_number(ip, narg, c);
1196 print_number64(first, ip, narg, c);
1198 print_integer_arg(sysdecode_whence, *ip);
1204 print_number(ip, narg, c);
1205 print_number64(first, ip, narg, c);
1207 print_integer_arg(sysdecode_whence, *ip);
1212 print_number(ip, narg, c);
1214 print_mask_arg(sysdecode_flock_operation, *ip);
1222 print_number(ip, narg, c);
1224 decode_filemode(*ip);
1229 print_number(ip, narg, c);
1231 print_integer_arg(sysdecode_shutdown_how, *ip);
1235 case SYS_socketpair:
1237 print_integer_arg(sysdecode_socketdomain, *ip);
1241 print_mask_arg(sysdecode_socket_type, *ip);
1249 print_integer_arg(sysdecode_rlimit, *ip);
1256 print_integer_arg(sysdecode_getrusage_who, *ip);
1262 print_number(ip, narg, c);
1264 if (!sysdecode_quotactl_cmd(stdout, *ip)) {
1266 printf("<invalid=%d>", (int)*ip);
1268 printf("<invalid=%#x>",
1277 print_integer_arg(sysdecode_nfssvc_flags, *ip);
1283 case SYS_rtprio_thread:
1285 print_integer_arg(sysdecode_rtprio_function,
1292 print_number(ip, narg, c);
1293 print_number(ip, narg, c);
1295 print_integer_arg(sysdecode_semctl_cmd, *ip);
1300 print_number(ip, narg, c);
1301 print_number(ip, narg, c);
1303 print_mask_arg(sysdecode_semget_flags, *ip);
1308 print_number(ip, narg, c);
1310 print_integer_arg(sysdecode_msgctl_cmd, *ip);
1315 print_number(ip, narg, c);
1316 print_number(ip, narg, c);
1318 print_mask_arg(sysdecode_shmat_flags, *ip);
1323 print_number(ip, narg, c);
1325 print_integer_arg(sysdecode_shmctl_cmd, *ip);
1329 #ifdef SYS_freebsd12_shm_open
1330 case SYS_freebsd12_shm_open:
1331 if (ip[0] == (uintptr_t)SHM_ANON) {
1332 printf("(SHM_ANON");
1335 print_number(ip, narg, c);
1338 print_mask_arg(sysdecode_open_flags, ip[0]);
1340 decode_filemode(ip[1]);
1346 if (ip[0] == (uintptr_t)SHM_ANON) {
1347 printf("(SHM_ANON");
1350 print_number(ip, narg, c);
1353 print_mask_arg(sysdecode_open_flags, ip[0]);
1355 decode_filemode(ip[1]);
1357 print_mask_arg(sysdecode_shmflags, ip[2]);
1362 print_number(ip, narg, c);
1363 print_number(ip, narg, c);
1365 print_integer_arg(sysdecode_minherit_inherit,
1372 print_mask_arg(sysdecode_rfork_flags, *ip);
1377 case SYS_lio_listio:
1379 print_integer_arg(sysdecode_lio_listio_mode,
1387 print_mask_arg(sysdecode_mlockall_flags, *ip);
1391 case SYS_sched_setscheduler:
1392 print_number(ip, narg, c);
1394 print_integer_arg(sysdecode_scheduler_policy,
1399 case SYS_sched_get_priority_max:
1400 case SYS_sched_get_priority_min:
1402 print_integer_arg(sysdecode_scheduler_policy,
1408 print_number(ip, narg, c);
1409 print_number(ip, narg, c);
1410 print_number(ip, narg, c);
1411 print_number(ip, narg, c);
1412 print_number(ip, narg, c);
1413 print_number(ip, narg, c);
1415 print_mask_arg(sysdecode_sendfile_flags, *ip);
1420 print_number(ip, narg, c);
1422 print_integer_arg(sysdecode_kldsym_cmd, *ip);
1426 case SYS_sigprocmask:
1428 print_integer_arg(sysdecode_sigprocmask_how,
1434 case SYS___acl_get_file:
1435 case SYS___acl_set_file:
1436 case SYS___acl_get_fd:
1437 case SYS___acl_set_fd:
1438 case SYS___acl_delete_file:
1439 case SYS___acl_delete_fd:
1440 case SYS___acl_aclcheck_file:
1441 case SYS___acl_aclcheck_fd:
1442 case SYS___acl_get_link:
1443 case SYS___acl_set_link:
1444 case SYS___acl_delete_link:
1445 case SYS___acl_aclcheck_link:
1446 print_number(ip, narg, c);
1448 print_integer_arg(sysdecode_acltype, *ip);
1459 case SYS_extattrctl:
1460 print_number(ip, narg, c);
1462 print_integer_arg(sysdecode_extattrnamespace,
1468 print_number(ip, narg, c);
1469 print_number(ip, narg, c);
1471 print_mask_arg(sysdecode_mount_flags, *ip);
1475 case SYS_thr_create:
1476 print_number(ip, narg, c);
1477 print_number(ip, narg, c);
1479 print_mask_arg(sysdecode_thr_create_flags, *ip);
1484 print_number(ip, narg, c);
1490 case SYS_kldunloadf:
1491 print_number(ip, narg, c);
1493 print_integer_arg(sysdecode_kldunload_flags,
1501 print_number(ip, narg, c);
1503 print_integer_arg_valid(sysdecode_atfd, *ip);
1506 print_number(ip, narg, c);
1508 case SYS_cap_fcntls_limit:
1509 print_number(ip, narg, c);
1514 print_mask_arg32(sysdecode_cap_fcntlrights, arg);
1516 case SYS_posix_fadvise:
1517 print_number(ip, narg, c);
1518 print_number(ip, narg, c);
1519 print_number(ip, narg, c);
1521 print_integer_arg(sysdecode_fadvice, *ip);
1527 print_integer_arg(sysdecode_idtype, *ip);
1531 print_number64(first, ip, narg, c);
1533 print_integer_arg(sysdecode_procctl_cmd, *ip);
1537 case SYS__umtx_op: {
1540 print_number(ip, narg, c);
1542 if (print_mask_arg_part(sysdecode_umtx_op_flags,
1545 print_integer_arg(sysdecode_umtx_op, op);
1548 case UMTX_OP_CV_WAIT:
1553 sysdecode_umtx_cvwait_flags, *ip);
1555 case UMTX_OP_RW_RDLOCK:
1560 sysdecode_umtx_rwlock_flags, *ip);
1569 print_number(ip, narg, c);
1570 print_number64(first, ip, narg, c);
1573 print_number(ip, narg, c);
1574 print_number(ip, narg, c);
1575 print_number(ip, narg, c);
1579 print_number(ip, narg, c);
1580 print_number(ip, narg, c);
1583 print_number(ip, narg, c);
1587 print_integer_arg(sysdecode_sysarch_number, *ip);
1595 print_integer_arg(sysdecode_itimer, *ip);
1601 switch (ktr->ktr_code) {
1611 print_mask_arg0(sysdecode_atflags, *ip);
1622 ktrsysret(struct ktr_sysret *ktr, u_int sv_flags)
1624 register_t ret = ktr->ktr_retval;
1625 int error = ktr->ktr_error;
1627 syscallname(ktr->ktr_code, sv_flags);
1632 printf("%ld", (long)ret);
1633 if (ret < 0 || ret > 9)
1634 printf("/%#lx", (unsigned long)ret);
1637 printf("%ld", (long)ret);
1639 printf("%#lx", (unsigned long)ret);
1641 } else if (error == ERESTART)
1643 else if (error == EJUSTRETURN)
1644 printf("JUSTRETURN");
1646 printf("-1 errno %d", sysdecode_freebsd_to_abi_errno(
1647 syscallabi(sv_flags), error));
1649 printf(" %s", strerror(ktr->ktr_error));
1655 ktrnamei(char *cp, int len)
1657 printf("\"%.*s\"\n", len, cp);
1661 hexdump(char *p, int len, int screenwidth)
1669 i = 13; /* base offset */
1670 i += (width / 2) + 1; /* spaces every second byte */
1671 i += (width * 2); /* width of bytes */
1673 i += width; /* each byte */
1675 } while (i < screenwidth);
1678 for (n = 0; n < len; n += width) {
1679 for (i = n; i < n + width; i++) {
1680 if ((i % width) == 0) { /* beginning of line */
1681 printf(" 0x%04x", i);
1687 printf("%02x", p[i] & 0xff);
1692 for (i = n; i < n + width; i++) {
1695 if (p[i] >= ' ' && p[i] <= '~')
1702 if ((i % width) != 0)
1707 visdump(char *dp, int datalen, int screenwidth)
1716 for (;datalen > 0; datalen--, dp++) {
1717 vis(visbuf, *dp, VIS_CSTYLE | VIS_NOLOCALE, *(dp+1));
1720 * Keep track of printables and
1721 * space chars (like fold(1)).
1733 width = 8 - (col&07);
1738 if (col + width > (screenwidth-2)) {
1753 ktrgenio(struct ktr_genio *ktr, int len)
1755 int datalen = len - sizeof (struct ktr_genio);
1756 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1757 static int screenwidth = 0;
1760 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1761 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1762 datalen == 1 ? "" : "s");
1765 if (screenwidth == 0) {
1768 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1770 screenwidth = ws.ws_col;
1774 if (maxdata && datalen > maxdata)
1777 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1778 if (dp[i] >= 32 && dp[i] < 127)
1780 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1785 hexdump(dp, datalen, screenwidth);
1787 visdump(dp, datalen, screenwidth);
1791 ktrpsig(struct ktr_psig *psig)
1795 print_signal(psig->signo);
1796 if (psig->action == SIG_DFL) {
1799 printf(" caught handler=0x%lx mask=0x%x",
1800 (u_long)psig->action, psig->mask.__bits[0]);
1803 str = sysdecode_sigcode(psig->signo, psig->code);
1807 printf("<invalid=%#x>", psig->code);
1812 ktrcsw_old(struct ktr_csw_old *cs)
1814 printf("%s %s\n", cs->out ? "stop" : "resume",
1815 cs->user ? "user" : "kernel");
1819 ktrcsw(struct ktr_csw *cs)
1821 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1822 cs->user ? "user" : "kernel", cs->wmesg);
1826 ktruser(int len, void *p)
1830 if (sysdecode_utrace(stdout, p, len)) {
1839 printf(" %d", *cp++);
1841 printf(" %02x", *cp++);
1846 ktrcaprights(cap_rights_t *rightsp)
1849 printf("cap_rights_t ");
1850 sysdecode_cap_rights(stdout, rightsp);
1855 ktrtimeval(struct timeval *tv)
1858 printf("{%ld, %ld}", (long)tv->tv_sec, tv->tv_usec);
1862 ktritimerval(struct itimerval *it)
1865 printf("itimerval { .interval = ");
1866 ktrtimeval(&it->it_interval);
1867 printf(", .value = ");
1868 ktrtimeval(&it->it_value);
1873 ktrsockaddr(struct sockaddr *sa)
1876 TODO: Support additional address families
1877 #include <netsmb/netbios.h>
1878 struct sockaddr_nb *nb;
1884 * note: ktrstruct() has already verified that sa points to a
1885 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1886 * sa->sa_len bytes long.
1888 printf("struct sockaddr { ");
1889 str = sysdecode_sockaddr_family(sa->sa_family);
1893 printf("<invalid=%d>", sa->sa_family);
1896 #define check_sockaddr_len(n) \
1897 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1898 printf("invalid"); \
1902 switch(sa->sa_family) {
1904 struct sockaddr_in sa_in;
1906 memset(&sa_in, 0, sizeof(sa_in));
1907 memcpy(&sa_in, sa, sa->sa_len);
1908 check_sockaddr_len(in);
1909 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1910 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1914 struct sockaddr_in6 sa_in6;
1916 memset(&sa_in6, 0, sizeof(sa_in6));
1917 memcpy(&sa_in6, sa, sa->sa_len);
1918 check_sockaddr_len(in6);
1919 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1920 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1921 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1925 struct sockaddr_un sa_un;
1927 memset(&sa_un, 0, sizeof(sa_un));
1928 memcpy(&sa_un, sa, sa->sa_len);
1929 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1933 printf("unknown address family");
1939 ktrstat(struct stat *statp)
1941 char mode[12], timestr[PATH_MAX + 4];
1947 * note: ktrstruct() has already verified that statp points to a
1948 * buffer exactly sizeof(struct stat) bytes long.
1950 printf("struct stat {");
1951 printf("dev=%ju, ino=%ju, ",
1952 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1954 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1956 strmode(statp->st_mode, mode);
1957 printf("mode=%s, ", mode);
1959 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1965 pwd = cap_getpwuid(cappwd, statp->st_uid);
1968 pwd = getpwuid(statp->st_uid);
1971 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1973 printf("uid=\"%s\", ", pwd->pw_name);
1979 grp = cap_getgrgid(capgrp, statp->st_gid);
1982 grp = getgrgid(statp->st_gid);
1985 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1987 printf("gid=\"%s\", ", grp->gr_name);
1988 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1991 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1993 tm = localtime(&statp->st_atim.tv_sec);
1994 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1995 printf("\"%s\"", timestr);
1997 if (statp->st_atim.tv_nsec != 0)
1998 printf(".%09ld, ", statp->st_atim.tv_nsec);
2003 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
2005 tm = localtime(&statp->st_mtim.tv_sec);
2006 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
2007 printf("\"%s\"", timestr);
2009 if (statp->st_mtim.tv_nsec != 0)
2010 printf(".%09ld, ", statp->st_mtim.tv_nsec);
2015 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
2017 tm = localtime(&statp->st_ctim.tv_sec);
2018 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
2019 printf("\"%s\"", timestr);
2021 if (statp->st_ctim.tv_nsec != 0)
2022 printf(".%09ld, ", statp->st_ctim.tv_nsec);
2025 printf("birthtime=");
2027 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
2029 tm = localtime(&statp->st_birthtim.tv_sec);
2030 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
2031 printf("\"%s\"", timestr);
2033 if (statp->st_birthtim.tv_nsec != 0)
2034 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
2037 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
2038 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
2039 (intmax_t)statp->st_blocks, statp->st_flags);
2044 ktrbitset(char *name, struct bitset *set, size_t setlen)
2048 if (setlen > INT32_MAX)
2050 maxi = setlen * CHAR_BIT;
2051 printf("%s [ ", name);
2052 for (i = 0; i < maxi; i++) {
2053 if (!BIT_ISSET(setlen, i, set))
2062 printf(" empty ]\n");
2068 ktrstruct(char *buf, size_t buflen)
2071 size_t namelen, datalen;
2073 cap_rights_t rights;
2074 struct itimerval it;
2076 struct sockaddr_storage ss;
2079 for (name = buf, namelen = 0;
2080 namelen < buflen && name[namelen] != '\0';
2083 if (namelen == buflen)
2085 if (name[namelen] != '\0')
2087 data = buf + namelen + 1;
2088 datalen = buflen - namelen - 1;
2092 for (i = 0; i < (int)namelen; ++i)
2093 if (!isalpha(name[i]) && name[i] != '_')
2095 if (strcmp(name, "caprights") == 0) {
2096 if (datalen != sizeof(cap_rights_t))
2098 memcpy(&rights, data, datalen);
2099 ktrcaprights(&rights);
2100 } else if (strcmp(name, "itimerval") == 0) {
2101 if (datalen != sizeof(struct itimerval))
2103 memcpy(&it, data, datalen);
2105 } else if (strcmp(name, "stat") == 0) {
2106 if (datalen != sizeof(struct stat))
2108 memcpy(&sb, data, datalen);
2110 } else if (strcmp(name, "sockaddr") == 0) {
2111 if (datalen > sizeof(ss))
2113 memcpy(&ss, data, datalen);
2114 if (datalen != ss.ss_len)
2116 ktrsockaddr((struct sockaddr *)&ss);
2117 } else if (strcmp(name, "cpuset_t") == 0) {
2120 set = malloc(datalen);
2122 errx(1, "%s", strerror(ENOMEM));
2123 memcpy(set, data, datalen);
2124 ktrbitset(name, set, datalen);
2127 #ifdef SYSDECODE_HAVE_LINUX
2128 if (ktrstruct_linux(name, data, datalen) == false)
2130 printf("unknown structure\n");
2134 printf("invalid record\n");
2138 ktrcapfail(struct ktr_cap_fail *ktr)
2140 switch (ktr->cap_type) {
2141 case CAPFAIL_NOTCAPABLE:
2142 /* operation on fd with insufficient capabilities */
2143 printf("operation requires ");
2144 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2145 printf(", descriptor holds ");
2146 sysdecode_cap_rights(stdout, &ktr->cap_held);
2148 case CAPFAIL_INCREASE:
2149 /* requested more capabilities than fd already has */
2150 printf("attempt to increase capabilities from ");
2151 sysdecode_cap_rights(stdout, &ktr->cap_held);
2153 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2155 case CAPFAIL_SYSCALL:
2156 /* called restricted syscall */
2157 printf("disallowed system call");
2159 case CAPFAIL_LOOKUP:
2160 /* absolute or AT_FDCWD path, ".." path, etc. */
2161 printf("restricted VFS lookup");
2164 printf("unknown capability failure: ");
2165 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2167 sysdecode_cap_rights(stdout, &ktr->cap_held);
2174 ktrfault(struct ktr_fault *ktr)
2177 printf("0x%jx ", (uintmax_t)ktr->vaddr);
2178 print_mask_arg(sysdecode_vmprot, ktr->type);
2183 ktrfaultend(struct ktr_faultend *ktr)
2187 str = sysdecode_vmresult(ktr->result);
2191 printf("<invalid=%d>", ktr->result);
2196 ktrkevent(struct kevent *kev)
2200 switch (kev->filter) {
2206 case EVFILT_PROCDESC:
2208 printf("%ju", (uintmax_t)kev->ident);
2211 print_signal(kev->ident);
2214 printf("%p", (void *)kev->ident);
2216 printf(", filter=");
2217 print_integer_arg(sysdecode_kevent_filter, kev->filter);
2219 print_mask_arg0(sysdecode_kevent_flags, kev->flags);
2220 printf(", fflags=");
2221 sysdecode_kevent_fflags(stdout, kev->filter, kev->fflags,
2223 printf(", data=%#jx, udata=%p }", (uintmax_t)kev->data, kev->udata);
2227 ktrstructarray(struct ktr_struct_array *ksa, size_t buflen)
2231 size_t namelen, datalen;
2235 buflen -= sizeof(*ksa);
2236 for (name = (char *)(ksa + 1), namelen = 0;
2237 namelen < buflen && name[namelen] != '\0';
2240 if (namelen == buflen)
2242 if (name[namelen] != '\0')
2245 for (i = 0; i < (int)namelen; ++i)
2246 if (!isalnum(name[i]) && name[i] != '_')
2248 data = name + namelen + 1;
2249 datalen = buflen - namelen - 1;
2250 printf("struct %s[] = { ", name);
2252 for (; datalen >= ksa->struct_size;
2253 data += ksa->struct_size, datalen -= ksa->struct_size) {
2258 if (strcmp(name, "kevent") == 0) {
2259 if (ksa->struct_size != sizeof(kev))
2261 memcpy(&kev, data, sizeof(kev));
2263 } else if (strcmp(name, "freebsd11_kevent") == 0) {
2264 struct freebsd11_kevent kev11;
2266 if (ksa->struct_size != sizeof(kev11))
2268 memcpy(&kev11, data, sizeof(kev11));
2269 memset(&kev, 0, sizeof(kev));
2270 kev.ident = kev11.ident;
2271 kev.filter = kev11.filter;
2272 kev.flags = kev11.flags;
2273 kev.fflags = kev11.fflags;
2274 kev.data = kev11.data;
2275 kev.udata = kev11.udata;
2277 #ifdef _WANT_KEVENT32
2278 } else if (strcmp(name, "kevent32") == 0) {
2279 struct kevent32 kev32;
2281 if (ksa->struct_size != sizeof(kev32))
2283 memcpy(&kev32, data, sizeof(kev32));
2284 memset(&kev, 0, sizeof(kev));
2285 kev.ident = kev32.ident;
2286 kev.filter = kev32.filter;
2287 kev.flags = kev32.flags;
2288 kev.fflags = kev32.fflags;
2289 #if BYTE_ORDER == BIG_ENDIAN
2290 kev.data = kev32.data2 | ((int64_t)kev32.data1 << 32);
2292 kev.data = kev32.data1 | ((int64_t)kev32.data2 << 32);
2294 kev.udata = (void *)(uintptr_t)kev32.udata;
2296 } else if (strcmp(name, "freebsd11_kevent32") == 0) {
2297 struct freebsd11_kevent32 kev32;
2299 if (ksa->struct_size != sizeof(kev32))
2301 memcpy(&kev32, data, sizeof(kev32));
2302 memset(&kev, 0, sizeof(kev));
2303 kev.ident = kev32.ident;
2304 kev.filter = kev32.filter;
2305 kev.flags = kev32.flags;
2306 kev.fflags = kev32.fflags;
2307 kev.data = kev32.data;
2308 kev.udata = (void *)(uintptr_t)kev32.udata;
2312 printf("<unknown structure> }\n");
2319 printf("invalid record\n");
2322 printf("<bad size> }\n");
2329 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
2330 "[-m maxdata] [-p pid] [-t trstr]\n");