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 "%6d %6d %-8.*s Events dropped.\n",
454 ktr_header.ktr_tid > 0 ?
455 (lwpid_t)ktr_header.ktr_tid : 0,
456 MAXCOMLEN, ktr_header.ktr_comm);
458 } else if (!drop_logged) {
459 printf("%6d %-8.*s Events dropped.\n",
460 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("%6d %6d %-8.*s ", kth->ktr_pid,
728 kth->ktr_tid > 0 ? (lwpid_t)kth->ktr_tid : 0,
729 MAXCOMLEN, kth->ktr_comm);
731 printf("%6d %-8.*s ", kth->ktr_pid, MAXCOMLEN, kth->ktr_comm);
733 if (version == KTR_VERSION0)
734 dumptimeval((struct ktr_header_v0 *)kth);
738 if (cpuflag && version > KTR_VERSION0)
739 printf("%3d ", kth->ktr_cpu);
742 switch (sv_flags & SV_ABI_MASK) {
754 if ((sv_flags & SV_LP64) != 0)
756 else if ((sv_flags & SV_ILP32) != 0)
761 printf("%s%s ", abi, arch);
765 #include <sys/syscall.h>
768 ioctlname(unsigned long val)
772 str = sysdecode_ioctlname(val);
781 static enum sysdecode_abi
782 syscallabi(u_int sv_flags)
786 return (SYSDECODE_ABI_FREEBSD);
787 switch (sv_flags & SV_ABI_MASK) {
789 return (SYSDECODE_ABI_FREEBSD);
792 if (sv_flags & SV_ILP32)
793 return (SYSDECODE_ABI_LINUX32);
795 return (SYSDECODE_ABI_LINUX);
797 return (SYSDECODE_ABI_UNKNOWN);
802 syscallname(u_int code, u_int sv_flags)
806 name = sysdecode_syscallname(syscallabi(sv_flags), code);
808 printf("[%d]", code);
812 printf("[%d]", code);
817 print_signal(int signo)
821 signame = sysdecode_signal(signo);
823 printf("%s", signame);
825 printf("SIG %d", signo);
829 ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags)
831 int narg = ktr->ktr_narg;
834 syscallname(ktr->ktr_code, sv_flags);
835 ip = &ktr->ktr_args[0];
839 switch (sv_flags & SV_ABI_MASK) {
841 ktrsyscall_freebsd(ktr, &ip, &narg, &c,
844 #ifdef SYSDECODE_HAVE_LINUX
847 if (sv_flags & SV_ILP32)
848 ktrsyscall_linux32(ktr, &ip,
852 ktrsyscall_linux(ktr, &ip, &narg, &c);
854 #endif /* SYSDECODE_HAVE_LINUX */
858 print_number(ip, narg, c);
865 ktrsyscall_freebsd(struct ktr_syscall *ktr, register_t **resip,
866 int *resnarg, char *resc, u_int sv_flags)
868 int narg = ktr->ktr_narg;
869 register_t *ip, *first;
871 int quad_align, quad_slots;
873 ip = first = &ktr->ktr_args[0];
877 if (sv_flags & SV_ILP32) {
884 switch (ktr->ktr_code) {
903 print_integer_arg_valid(sysdecode_atfd, *ip);
909 switch (ktr->ktr_code) {
911 print_number(ip, narg, c);
921 print_integer_arg(sysdecode_ptrace_request, *ip);
929 print_number(ip, narg, c);
931 print_mask_arg(sysdecode_access_mode, *ip);
935 case SYS_close_range:
936 print_number(ip, narg, c);
937 print_number(ip, narg, c);
939 print_mask_arg(sysdecode_close_range_flags, *ip);
945 print_number(ip, narg, c);
947 print_mask_arg(sysdecode_open_flags, ip[0]);
948 if ((ip[0] & O_CREAT) == O_CREAT) {
950 decode_filemode(ip[1]);
956 print_number(ip, narg, c);
957 print_number(ip, narg, c);
959 print_mask_arg0(sysdecode_wait4_options, *ip);
965 print_integer_arg(sysdecode_idtype, *ip);
969 print_number64(first, ip, narg, c);
970 print_number(ip, narg, c);
972 print_mask_arg(sysdecode_wait6_options, *ip);
980 print_number(ip, narg, c);
982 decode_filemode(*ip);
987 print_number(ip, narg, c);
989 decode_filemode(*ip);
994 print_number(ip, narg, c);
995 print_number(ip, narg, c);
997 print_integer_arg(sysdecode_getfsstat_mode, *ip);
1002 print_number(ip, narg, c);
1003 print_number(ip, narg, c);
1005 print_mask_arg(sysdecode_mount_flags, *ip);
1010 print_number(ip, narg, c);
1012 print_mask_arg(sysdecode_mount_flags, *ip);
1018 print_number(ip, narg, c);
1019 print_number(ip, narg, c);
1021 print_mask_arg0(sysdecode_msg_flags, *ip);
1027 print_number(ip, narg, c);
1028 print_number(ip, narg, c);
1029 print_number(ip, narg, c);
1031 print_mask_arg0(sysdecode_msg_flags, *ip);
1039 print_number(ip, narg, c);
1041 decode_fileflags(*ip);
1046 print_number(ip, narg, c);
1054 print_mask_arg(sysdecode_reboot_howto, *ip);
1060 decode_filemode(*ip);
1065 print_number(ip, narg, c);
1066 print_number(ip, narg, c);
1068 print_mask_arg(sysdecode_msync_flags, *ip);
1072 #ifdef SYS_freebsd6_mmap
1073 case SYS_freebsd6_mmap:
1074 print_number(ip, narg, c);
1075 print_number(ip, narg, c);
1077 print_mask_arg(sysdecode_mmap_prot, *ip);
1081 print_mask_arg(sysdecode_mmap_flags, *ip);
1087 print_number(ip, narg, c);
1088 print_number(ip, narg, c);
1090 print_mask_arg(sysdecode_mmap_prot, *ip);
1094 print_mask_arg(sysdecode_mmap_flags, *ip);
1099 print_number(ip, narg, c);
1100 print_number(ip, narg, c);
1102 print_mask_arg(sysdecode_mmap_prot, *ip);
1107 print_number(ip, narg, c);
1108 print_number(ip, narg, c);
1110 print_integer_arg(sysdecode_madvice, *ip);
1117 print_number(ip, narg, c);
1119 print_integer_arg(sysdecode_pathconf_name, *ip);
1123 case SYS_getpriority:
1124 case SYS_setpriority:
1126 print_integer_arg(sysdecode_prio_which, *ip);
1132 print_number(ip, narg, c);
1134 print_integer_arg(sysdecode_fcntl_cmd, ip[0]);
1135 if (sysdecode_fcntl_arg_p(ip[0])) {
1137 if (ip[0] == F_SETFL)
1139 sysdecode_fcntl_fileflags,
1142 sysdecode_fcntl_arg(stdout,
1153 print_integer_arg(sysdecode_socketdomain,
1158 print_mask_arg(sysdecode_socket_type, *ip);
1161 if (sockdomain == PF_INET ||
1162 sockdomain == PF_INET6) {
1164 print_integer_arg(sysdecode_ipproto,
1172 case SYS_setsockopt:
1173 case SYS_getsockopt: {
1176 print_number(ip, narg, c);
1178 print_integer_arg_valid(sysdecode_sockopt_level,
1180 str = sysdecode_sockopt_name(ip[0], ip[1]);
1190 #ifdef SYS_freebsd6_lseek
1191 case SYS_freebsd6_lseek:
1192 print_number(ip, narg, c);
1193 /* Hidden 'pad' argument, not in lseek(2) */
1194 print_number(ip, narg, c);
1195 print_number64(first, ip, narg, c);
1197 print_integer_arg(sysdecode_whence, *ip);
1203 print_number(ip, narg, c);
1204 print_number64(first, ip, narg, c);
1206 print_integer_arg(sysdecode_whence, *ip);
1211 print_number(ip, narg, c);
1213 print_mask_arg(sysdecode_flock_operation, *ip);
1221 print_number(ip, narg, c);
1223 decode_filemode(*ip);
1228 print_number(ip, narg, c);
1230 print_integer_arg(sysdecode_shutdown_how, *ip);
1234 case SYS_socketpair:
1236 print_integer_arg(sysdecode_socketdomain, *ip);
1240 print_mask_arg(sysdecode_socket_type, *ip);
1248 print_integer_arg(sysdecode_rlimit, *ip);
1255 print_integer_arg(sysdecode_getrusage_who, *ip);
1261 print_number(ip, narg, c);
1263 if (!sysdecode_quotactl_cmd(stdout, *ip)) {
1265 printf("<invalid=%d>", (int)*ip);
1267 printf("<invalid=%#x>",
1276 print_integer_arg(sysdecode_nfssvc_flags, *ip);
1282 case SYS_rtprio_thread:
1284 print_integer_arg(sysdecode_rtprio_function,
1291 print_number(ip, narg, c);
1292 print_number(ip, narg, c);
1294 print_integer_arg(sysdecode_semctl_cmd, *ip);
1299 print_number(ip, narg, c);
1300 print_number(ip, narg, c);
1302 print_mask_arg(sysdecode_semget_flags, *ip);
1307 print_number(ip, narg, c);
1309 print_integer_arg(sysdecode_msgctl_cmd, *ip);
1314 print_number(ip, narg, c);
1315 print_number(ip, narg, c);
1317 print_mask_arg(sysdecode_shmat_flags, *ip);
1322 print_number(ip, narg, c);
1324 print_integer_arg(sysdecode_shmctl_cmd, *ip);
1328 #ifdef SYS_freebsd12_shm_open
1329 case SYS_freebsd12_shm_open:
1330 if (ip[0] == (uintptr_t)SHM_ANON) {
1331 printf("(SHM_ANON");
1334 print_number(ip, narg, c);
1337 print_mask_arg(sysdecode_open_flags, ip[0]);
1339 decode_filemode(ip[1]);
1345 if (ip[0] == (uintptr_t)SHM_ANON) {
1346 printf("(SHM_ANON");
1349 print_number(ip, narg, c);
1352 print_mask_arg(sysdecode_open_flags, ip[0]);
1354 decode_filemode(ip[1]);
1356 print_mask_arg(sysdecode_shmflags, ip[2]);
1361 print_number(ip, narg, c);
1362 print_number(ip, narg, c);
1364 print_integer_arg(sysdecode_minherit_inherit,
1371 print_mask_arg(sysdecode_rfork_flags, *ip);
1376 case SYS_lio_listio:
1378 print_integer_arg(sysdecode_lio_listio_mode,
1386 print_mask_arg(sysdecode_mlockall_flags, *ip);
1390 case SYS_sched_setscheduler:
1391 print_number(ip, narg, c);
1393 print_integer_arg(sysdecode_scheduler_policy,
1398 case SYS_sched_get_priority_max:
1399 case SYS_sched_get_priority_min:
1401 print_integer_arg(sysdecode_scheduler_policy,
1407 print_number(ip, narg, c);
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);
1414 print_mask_arg(sysdecode_sendfile_flags, *ip);
1419 print_number(ip, narg, c);
1421 print_integer_arg(sysdecode_kldsym_cmd, *ip);
1425 case SYS_sigprocmask:
1427 print_integer_arg(sysdecode_sigprocmask_how,
1433 case SYS___acl_get_file:
1434 case SYS___acl_set_file:
1435 case SYS___acl_get_fd:
1436 case SYS___acl_set_fd:
1437 case SYS___acl_delete_file:
1438 case SYS___acl_delete_fd:
1439 case SYS___acl_aclcheck_file:
1440 case SYS___acl_aclcheck_fd:
1441 case SYS___acl_get_link:
1442 case SYS___acl_set_link:
1443 case SYS___acl_delete_link:
1444 case SYS___acl_aclcheck_link:
1445 print_number(ip, narg, c);
1447 print_integer_arg(sysdecode_acltype, *ip);
1458 case SYS_extattrctl:
1459 print_number(ip, narg, c);
1461 print_integer_arg(sysdecode_extattrnamespace,
1467 print_number(ip, narg, c);
1468 print_number(ip, narg, c);
1470 print_mask_arg(sysdecode_mount_flags, *ip);
1474 case SYS_thr_create:
1475 print_number(ip, narg, c);
1476 print_number(ip, narg, c);
1478 print_mask_arg(sysdecode_thr_create_flags, *ip);
1483 print_number(ip, narg, c);
1489 case SYS_kldunloadf:
1490 print_number(ip, narg, c);
1492 print_integer_arg(sysdecode_kldunload_flags,
1500 print_number(ip, narg, c);
1502 print_integer_arg_valid(sysdecode_atfd, *ip);
1505 print_number(ip, narg, c);
1507 case SYS_cap_fcntls_limit:
1508 print_number(ip, narg, c);
1513 print_mask_arg32(sysdecode_cap_fcntlrights, arg);
1515 case SYS_posix_fadvise:
1516 print_number(ip, narg, c);
1517 print_number(ip, narg, c);
1518 print_number(ip, narg, c);
1520 print_integer_arg(sysdecode_fadvice, *ip);
1526 print_integer_arg(sysdecode_idtype, *ip);
1530 print_number64(first, ip, narg, c);
1532 print_integer_arg(sysdecode_procctl_cmd, *ip);
1536 case SYS__umtx_op: {
1539 print_number(ip, narg, c);
1541 if (print_mask_arg_part(sysdecode_umtx_op_flags,
1544 print_integer_arg(sysdecode_umtx_op, op);
1547 case UMTX_OP_CV_WAIT:
1552 sysdecode_umtx_cvwait_flags, *ip);
1554 case UMTX_OP_RW_RDLOCK:
1559 sysdecode_umtx_rwlock_flags, *ip);
1568 print_number(ip, narg, c);
1569 print_number64(first, ip, narg, c);
1572 print_number(ip, narg, c);
1573 print_number(ip, narg, c);
1574 print_number(ip, narg, c);
1578 print_number(ip, narg, c);
1579 print_number(ip, narg, c);
1582 print_number(ip, narg, c);
1586 print_integer_arg(sysdecode_sysarch_number, *ip);
1594 print_integer_arg(sysdecode_itimer, *ip);
1600 switch (ktr->ktr_code) {
1610 print_mask_arg0(sysdecode_atflags, *ip);
1621 ktrsysret(struct ktr_sysret *ktr, u_int sv_flags)
1623 register_t ret = ktr->ktr_retval;
1624 int error = ktr->ktr_error;
1626 syscallname(ktr->ktr_code, sv_flags);
1631 printf("%ld", (long)ret);
1632 if (ret < 0 || ret > 9)
1633 printf("/%#lx", (unsigned long)ret);
1636 printf("%ld", (long)ret);
1638 printf("%#lx", (unsigned long)ret);
1640 } else if (error == ERESTART)
1642 else if (error == EJUSTRETURN)
1643 printf("JUSTRETURN");
1645 printf("-1 errno %d", sysdecode_freebsd_to_abi_errno(
1646 syscallabi(sv_flags), error));
1648 printf(" %s", strerror(ktr->ktr_error));
1654 ktrnamei(char *cp, int len)
1656 printf("\"%.*s\"\n", len, cp);
1660 hexdump(char *p, int len, int screenwidth)
1668 i = 13; /* base offset */
1669 i += (width / 2) + 1; /* spaces every second byte */
1670 i += (width * 2); /* width of bytes */
1672 i += width; /* each byte */
1674 } while (i < screenwidth);
1677 for (n = 0; n < len; n += width) {
1678 for (i = n; i < n + width; i++) {
1679 if ((i % width) == 0) { /* beginning of line */
1680 printf(" 0x%04x", i);
1686 printf("%02x", p[i] & 0xff);
1691 for (i = n; i < n + width; i++) {
1694 if (p[i] >= ' ' && p[i] <= '~')
1701 if ((i % width) != 0)
1706 visdump(char *dp, int datalen, int screenwidth)
1715 for (;datalen > 0; datalen--, dp++) {
1716 vis(visbuf, *dp, VIS_CSTYLE | VIS_NOLOCALE, *(dp+1));
1719 * Keep track of printables and
1720 * space chars (like fold(1)).
1732 width = 8 - (col&07);
1737 if (col + width > (screenwidth-2)) {
1752 ktrgenio(struct ktr_genio *ktr, int len)
1754 int datalen = len - sizeof (struct ktr_genio);
1755 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1756 static int screenwidth = 0;
1759 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1760 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1761 datalen == 1 ? "" : "s");
1764 if (screenwidth == 0) {
1767 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1769 screenwidth = ws.ws_col;
1773 if (maxdata && datalen > maxdata)
1776 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1777 if (dp[i] >= 32 && dp[i] < 127)
1779 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1784 hexdump(dp, datalen, screenwidth);
1786 visdump(dp, datalen, screenwidth);
1790 ktrpsig(struct ktr_psig *psig)
1794 print_signal(psig->signo);
1795 if (psig->action == SIG_DFL) {
1798 printf(" caught handler=0x%lx mask=0x%x",
1799 (u_long)psig->action, psig->mask.__bits[0]);
1802 str = sysdecode_sigcode(psig->signo, psig->code);
1806 printf("<invalid=%#x>", psig->code);
1811 ktrcsw_old(struct ktr_csw_old *cs)
1813 printf("%s %s\n", cs->out ? "stop" : "resume",
1814 cs->user ? "user" : "kernel");
1818 ktrcsw(struct ktr_csw *cs)
1820 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1821 cs->user ? "user" : "kernel", cs->wmesg);
1825 ktruser(int len, void *p)
1829 if (sysdecode_utrace(stdout, p, len)) {
1838 printf(" %d", *cp++);
1840 printf(" %02x", *cp++);
1845 ktrcaprights(cap_rights_t *rightsp)
1848 printf("cap_rights_t ");
1849 sysdecode_cap_rights(stdout, rightsp);
1854 ktrtimeval(struct timeval *tv)
1857 printf("{%ld, %ld}", (long)tv->tv_sec, tv->tv_usec);
1861 ktritimerval(struct itimerval *it)
1864 printf("itimerval { .interval = ");
1865 ktrtimeval(&it->it_interval);
1866 printf(", .value = ");
1867 ktrtimeval(&it->it_value);
1872 ktrsockaddr(struct sockaddr *sa)
1875 TODO: Support additional address families
1876 #include <netsmb/netbios.h>
1877 struct sockaddr_nb *nb;
1883 * note: ktrstruct() has already verified that sa points to a
1884 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1885 * sa->sa_len bytes long.
1887 printf("struct sockaddr { ");
1888 str = sysdecode_sockaddr_family(sa->sa_family);
1892 printf("<invalid=%d>", sa->sa_family);
1895 #define check_sockaddr_len(n) \
1896 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1897 printf("invalid"); \
1901 switch(sa->sa_family) {
1903 struct sockaddr_in sa_in;
1905 memset(&sa_in, 0, sizeof(sa_in));
1906 memcpy(&sa_in, sa, sa->sa_len);
1907 check_sockaddr_len(in);
1908 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1909 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1913 struct sockaddr_in6 sa_in6;
1915 memset(&sa_in6, 0, sizeof(sa_in6));
1916 memcpy(&sa_in6, sa, sa->sa_len);
1917 check_sockaddr_len(in6);
1918 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1919 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1920 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1924 struct sockaddr_un sa_un;
1926 memset(&sa_un, 0, sizeof(sa_un));
1927 memcpy(&sa_un, sa, sa->sa_len);
1928 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1932 printf("unknown address family");
1938 ktrstat(struct stat *statp)
1940 char mode[12], timestr[PATH_MAX + 4];
1946 * note: ktrstruct() has already verified that statp points to a
1947 * buffer exactly sizeof(struct stat) bytes long.
1949 printf("struct stat {");
1950 printf("dev=%ju, ino=%ju, ",
1951 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1953 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1955 strmode(statp->st_mode, mode);
1956 printf("mode=%s, ", mode);
1958 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1964 pwd = cap_getpwuid(cappwd, statp->st_uid);
1967 pwd = getpwuid(statp->st_uid);
1970 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1972 printf("uid=\"%s\", ", pwd->pw_name);
1978 grp = cap_getgrgid(capgrp, statp->st_gid);
1981 grp = getgrgid(statp->st_gid);
1984 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1986 printf("gid=\"%s\", ", grp->gr_name);
1987 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1990 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1992 tm = localtime(&statp->st_atim.tv_sec);
1993 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1994 printf("\"%s\"", timestr);
1996 if (statp->st_atim.tv_nsec != 0)
1997 printf(".%09ld, ", statp->st_atim.tv_nsec);
2002 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
2004 tm = localtime(&statp->st_mtim.tv_sec);
2005 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
2006 printf("\"%s\"", timestr);
2008 if (statp->st_mtim.tv_nsec != 0)
2009 printf(".%09ld, ", statp->st_mtim.tv_nsec);
2014 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
2016 tm = localtime(&statp->st_ctim.tv_sec);
2017 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
2018 printf("\"%s\"", timestr);
2020 if (statp->st_ctim.tv_nsec != 0)
2021 printf(".%09ld, ", statp->st_ctim.tv_nsec);
2024 printf("birthtime=");
2026 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
2028 tm = localtime(&statp->st_birthtim.tv_sec);
2029 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
2030 printf("\"%s\"", timestr);
2032 if (statp->st_birthtim.tv_nsec != 0)
2033 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
2036 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
2037 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
2038 (intmax_t)statp->st_blocks, statp->st_flags);
2043 ktrbitset(char *name, struct bitset *set, size_t setlen)
2047 if (setlen > INT32_MAX)
2049 maxi = setlen * CHAR_BIT;
2050 printf("%s [ ", name);
2051 for (i = 0; i < maxi; i++) {
2052 if (!BIT_ISSET(setlen, i, set))
2061 printf(" empty ]\n");
2067 ktrstruct(char *buf, size_t buflen)
2070 size_t namelen, datalen;
2072 cap_rights_t rights;
2073 struct itimerval it;
2075 struct sockaddr_storage ss;
2078 for (name = buf, namelen = 0;
2079 namelen < buflen && name[namelen] != '\0';
2082 if (namelen == buflen)
2084 if (name[namelen] != '\0')
2086 data = buf + namelen + 1;
2087 datalen = buflen - namelen - 1;
2091 for (i = 0; i < (int)namelen; ++i)
2092 if (!isalpha(name[i]) && name[i] != '_')
2094 if (strcmp(name, "caprights") == 0) {
2095 if (datalen != sizeof(cap_rights_t))
2097 memcpy(&rights, data, datalen);
2098 ktrcaprights(&rights);
2099 } else if (strcmp(name, "itimerval") == 0) {
2100 if (datalen != sizeof(struct itimerval))
2102 memcpy(&it, data, datalen);
2104 } else if (strcmp(name, "stat") == 0) {
2105 if (datalen != sizeof(struct stat))
2107 memcpy(&sb, data, datalen);
2109 } else if (strcmp(name, "sockaddr") == 0) {
2110 if (datalen > sizeof(ss))
2112 memcpy(&ss, data, datalen);
2113 if (datalen != ss.ss_len)
2115 ktrsockaddr((struct sockaddr *)&ss);
2116 } else if (strcmp(name, "cpuset_t") == 0) {
2119 set = malloc(datalen);
2121 errx(1, "%s", strerror(ENOMEM));
2122 memcpy(set, data, datalen);
2123 ktrbitset(name, set, datalen);
2126 #ifdef SYSDECODE_HAVE_LINUX
2127 if (ktrstruct_linux(name, data, datalen) == false)
2129 printf("unknown structure\n");
2133 printf("invalid record\n");
2137 ktrcapfail(struct ktr_cap_fail *ktr)
2139 switch (ktr->cap_type) {
2140 case CAPFAIL_NOTCAPABLE:
2141 /* operation on fd with insufficient capabilities */
2142 printf("operation requires ");
2143 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2144 printf(", descriptor holds ");
2145 sysdecode_cap_rights(stdout, &ktr->cap_held);
2147 case CAPFAIL_INCREASE:
2148 /* requested more capabilities than fd already has */
2149 printf("attempt to increase capabilities from ");
2150 sysdecode_cap_rights(stdout, &ktr->cap_held);
2152 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2154 case CAPFAIL_SYSCALL:
2155 /* called restricted syscall */
2156 printf("disallowed system call");
2158 case CAPFAIL_LOOKUP:
2159 /* absolute or AT_FDCWD path, ".." path, etc. */
2160 printf("restricted VFS lookup");
2163 printf("unknown capability failure: ");
2164 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2166 sysdecode_cap_rights(stdout, &ktr->cap_held);
2173 ktrfault(struct ktr_fault *ktr)
2176 printf("0x%jx ", (uintmax_t)ktr->vaddr);
2177 print_mask_arg(sysdecode_vmprot, ktr->type);
2182 ktrfaultend(struct ktr_faultend *ktr)
2186 str = sysdecode_vmresult(ktr->result);
2190 printf("<invalid=%d>", ktr->result);
2195 ktrkevent(struct kevent *kev)
2199 switch (kev->filter) {
2205 case EVFILT_PROCDESC:
2207 printf("%ju", (uintmax_t)kev->ident);
2210 print_signal(kev->ident);
2213 printf("%p", (void *)kev->ident);
2215 printf(", filter=");
2216 print_integer_arg(sysdecode_kevent_filter, kev->filter);
2218 print_mask_arg0(sysdecode_kevent_flags, kev->flags);
2219 printf(", fflags=");
2220 sysdecode_kevent_fflags(stdout, kev->filter, kev->fflags,
2222 printf(", data=%#jx, udata=%p }", (uintmax_t)kev->data, kev->udata);
2226 ktrstructarray(struct ktr_struct_array *ksa, size_t buflen)
2230 size_t namelen, datalen;
2234 buflen -= sizeof(*ksa);
2235 for (name = (char *)(ksa + 1), namelen = 0;
2236 namelen < buflen && name[namelen] != '\0';
2239 if (namelen == buflen)
2241 if (name[namelen] != '\0')
2244 for (i = 0; i < (int)namelen; ++i)
2245 if (!isalnum(name[i]) && name[i] != '_')
2247 data = name + namelen + 1;
2248 datalen = buflen - namelen - 1;
2249 printf("struct %s[] = { ", name);
2251 for (; datalen >= ksa->struct_size;
2252 data += ksa->struct_size, datalen -= ksa->struct_size) {
2257 if (strcmp(name, "kevent") == 0) {
2258 if (ksa->struct_size != sizeof(kev))
2260 memcpy(&kev, data, sizeof(kev));
2262 } else if (strcmp(name, "freebsd11_kevent") == 0) {
2263 struct freebsd11_kevent kev11;
2265 if (ksa->struct_size != sizeof(kev11))
2267 memcpy(&kev11, data, sizeof(kev11));
2268 memset(&kev, 0, sizeof(kev));
2269 kev.ident = kev11.ident;
2270 kev.filter = kev11.filter;
2271 kev.flags = kev11.flags;
2272 kev.fflags = kev11.fflags;
2273 kev.data = kev11.data;
2274 kev.udata = kev11.udata;
2276 #ifdef _WANT_KEVENT32
2277 } else if (strcmp(name, "kevent32") == 0) {
2278 struct kevent32 kev32;
2280 if (ksa->struct_size != sizeof(kev32))
2282 memcpy(&kev32, data, sizeof(kev32));
2283 memset(&kev, 0, sizeof(kev));
2284 kev.ident = kev32.ident;
2285 kev.filter = kev32.filter;
2286 kev.flags = kev32.flags;
2287 kev.fflags = kev32.fflags;
2288 #if BYTE_ORDER == BIG_ENDIAN
2289 kev.data = kev32.data2 | ((int64_t)kev32.data1 << 32);
2291 kev.data = kev32.data1 | ((int64_t)kev32.data2 << 32);
2293 kev.udata = (void *)(uintptr_t)kev32.udata;
2295 } else if (strcmp(name, "freebsd11_kevent32") == 0) {
2296 struct freebsd11_kevent32 kev32;
2298 if (ksa->struct_size != sizeof(kev32))
2300 memcpy(&kev32, data, sizeof(kev32));
2301 memset(&kev, 0, sizeof(kev));
2302 kev.ident = kev32.ident;
2303 kev.filter = kev32.filter;
2304 kev.flags = kev32.flags;
2305 kev.fflags = kev32.fflags;
2306 kev.data = kev32.data;
2307 kev.udata = (void *)(uintptr_t)kev32.udata;
2311 printf("<unknown structure> }\n");
2318 printf("invalid record\n");
2321 printf("<bad size> }\n");
2328 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
2329 "[-m maxdata] [-p pid] [-t trstr]\n");