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 #include <sys/param.h>
52 #include <sys/capsicum.h>
53 #include <sys/errno.h>
56 #include <sys/event.h>
57 #include <sys/ktrace.h>
58 #include <sys/ioctl.h>
59 #include <sys/socket.h>
61 #include <sys/sysent.h>
64 #include <sys/queue.h>
69 #include <arpa/inet.h>
70 #include <netinet/in.h>
72 #include <capsicum_helpers.h>
84 #include <sysdecode.h>
91 #include <libcasper.h>
93 #include <casper/cap_grp.h>
94 #include <casper/cap_pwd.h>
97 u_int abidump(struct ktr_header *);
98 int fetchprocinfo(struct ktr_header *, u_int *);
99 int fread_tail(void *, int, int);
100 void dumpheader(struct ktr_header *);
101 void ktrsyscall(struct ktr_syscall *, u_int);
102 void ktrsysret(struct ktr_sysret *, u_int);
103 void ktrnamei(char *, int);
104 void hexdump(char *, int, int);
105 void visdump(char *, int, int);
106 void ktrgenio(struct ktr_genio *, int);
107 void ktrpsig(struct ktr_psig *);
108 void ktrcsw(struct ktr_csw *);
109 void ktrcsw_old(struct ktr_csw_old *);
110 void ktruser(int, void *);
111 void ktrcaprights(cap_rights_t *);
112 void ktritimerval(struct itimerval *it);
113 void ktrsockaddr(struct sockaddr *);
114 void ktrstat(struct stat *);
115 void ktrstruct(char *, size_t);
116 void ktrcapfail(struct ktr_cap_fail *);
117 void ktrfault(struct ktr_fault *);
118 void ktrfaultend(struct ktr_faultend *);
119 void ktrkevent(struct kevent *);
120 void ktrstructarray(struct ktr_struct_array *, size_t);
123 #define TIMESTAMP_NONE 0x0
124 #define TIMESTAMP_ABSOLUTE 0x1
125 #define TIMESTAMP_ELAPSED 0x2
126 #define TIMESTAMP_RELATIVE 0x4
128 static int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
129 resolv = 0, abiflag = 0, syscallno = 0;
130 static const char *tracefile = DEF_TRACEFILE;
131 static struct ktr_header ktr_header;
133 #define TIME_FORMAT "%b %e %T %Y"
134 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
136 #define print_number64(first,i,n,c) do { \
139 if (quad_align && (((ptrdiff_t)((i) - (first))) & 1) == 1) { \
143 if (quad_slots == 2) \
144 __v = (uint64_t)(uint32_t)(i)[0] | \
145 ((uint64_t)(uint32_t)(i)[1]) << 32; \
147 __v = (uint64_t)*(i); \
149 printf("%c%jd", (c), (intmax_t)__v); \
151 printf("%c%#jx", (c), (uintmax_t)__v); \
157 #define print_number(i,n,c) do { \
159 printf("%c%jd", c, (intmax_t)*i); \
161 printf("%c%#jx", c, (uintmax_t)(u_register_t)*i); \
169 TAILQ_ENTRY(proc_info) info;
174 static TAILQ_HEAD(trace_procs, proc_info) trace_procs;
177 static cap_channel_t *cappwd, *capgrp;
185 * Cache NLS data before entering capability mode.
186 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
188 (void)catopen("libc", NL_CAT_LOCALE);
197 * Allow localtime(3) to cache /etc/localtime content before entering
199 * XXXPJD: There should be localtime_init() in libc.
202 (void)localtime(<ime);
207 cappwdgrp_setup(cap_channel_t **cappwdp, cap_channel_t **capgrpp)
209 cap_channel_t *capcas, *cappwdloc, *capgrploc;
210 const char *cmds[1], *fields[1];
213 if (capcas == NULL) {
214 err(1, "unable to create casper process");
217 cappwdloc = cap_service_open(capcas, "system.pwd");
218 capgrploc = cap_service_open(capcas, "system.grp");
219 /* Casper capability no longer needed. */
221 if (cappwdloc == NULL || capgrploc == NULL) {
222 if (cappwdloc == NULL)
223 warn("unable to open system.pwd service");
224 if (capgrploc == NULL)
225 warn("unable to open system.grp service");
228 /* Limit system.pwd to only getpwuid() function and pw_name field. */
229 cmds[0] = "getpwuid";
230 if (cap_pwd_limit_cmds(cappwdloc, cmds, 1) < 0)
231 err(1, "unable to limit system.pwd service");
232 fields[0] = "pw_name";
233 if (cap_pwd_limit_fields(cappwdloc, fields, 1) < 0)
234 err(1, "unable to limit system.pwd service");
235 /* Limit system.grp to only getgrgid() function and gr_name field. */
236 cmds[0] = "getgrgid";
237 if (cap_grp_limit_cmds(capgrploc, cmds, 1) < 0)
238 err(1, "unable to limit system.grp service");
239 fields[0] = "gr_name";
240 if (cap_grp_limit_fields(capgrploc, fields, 1) < 0)
241 err(1, "unable to limit system.grp service");
243 *cappwdp = cappwdloc;
244 *capgrpp = capgrploc;
247 #endif /* WITH_CASPER */
250 print_integer_arg(const char *(*decoder)(int), int value)
254 str = decoder(value);
259 printf("<invalid=%d>", value);
261 printf("<invalid=%#x>", value);
265 /* Like print_integer_arg but unknown values are treated as valid. */
267 print_integer_arg_valid(const char *(*decoder)(int), int value)
271 str = decoder(value);
278 printf("%#x", value);
283 print_mask_arg(bool (*decoder)(FILE *, int, int *), int value)
288 printf("%#x<", value);
289 invalid = !decoder(stdout, value, &rem);
292 printf("<invalid>%u", rem);
296 print_mask_arg0(bool (*decoder)(FILE *, int, int *), int value)
305 printf("%#x<", value);
306 invalid = !decoder(stdout, value, &rem);
309 printf("<invalid>%u", rem);
313 decode_fileflags(fflags_t value)
322 printf("%#x<", value);
323 invalid = !sysdecode_fileflags(stdout, value, &rem);
326 printf("<invalid>%u", rem);
330 decode_filemode(int value)
339 printf("%#o<", value);
340 invalid = !sysdecode_filemode(stdout, value, &rem);
343 printf("<invalid>%u", rem);
347 print_mask_arg32(bool (*decoder)(FILE *, uint32_t, uint32_t *), uint32_t value)
352 printf("%#x<", value);
353 invalid = !decoder(stdout, value, &rem);
356 printf("<invalid>%u", rem);
360 print_mask_argul(bool (*decoder)(FILE *, u_long, u_long *), u_long value)
369 printf("%#lx<", value);
370 invalid = !decoder(stdout, value, &rem);
373 printf("<invalid>%lu", rem);
377 main(int argc, char *argv[])
379 int ch, ktrlen, size;
381 int trpoints = ALL_POINTS;
386 setlocale(LC_CTYPE, "");
388 timestamp = TIMESTAMP_NONE;
390 while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1)
405 maxdata = atoi(optarg);
423 timestamp |= TIMESTAMP_ELAPSED;
429 timestamp |= TIMESTAMP_RELATIVE;
432 timestamp |= TIMESTAMP_ABSOLUTE;
435 trpoints = getpoints(optarg);
437 errx(1, "unknown trace point in %s", optarg);
446 m = malloc(size = 1025);
448 errx(1, "%s", strerror(ENOMEM));
449 if (strcmp(tracefile, "-") != 0)
450 if (!freopen(tracefile, "r", stdin))
451 err(1, "%s", tracefile);
457 if (cappwdgrp_setup(&cappwd, &capgrp) < 0) {
462 if (resolv == 0 || (cappwd != NULL && capgrp != NULL)) {
463 if (cap_enter() < 0 && errno != ENOSYS)
464 err(1, "unable to enter capability mode");
468 if (cap_enter() < 0 && errno != ENOSYS)
469 err(1, "unable to enter capability mode");
472 if (caph_limit_stdio() == -1)
473 err(1, "unable to limit stdio");
475 TAILQ_INIT(&trace_procs);
477 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
478 if (ktr_header.ktr_type & KTR_DROP) {
479 ktr_header.ktr_type &= ~KTR_DROP;
480 if (!drop_logged && threads) {
482 "%6jd %6jd %-8.*s Events dropped.\n",
483 (intmax_t)ktr_header.ktr_pid,
484 ktr_header.ktr_tid > 0 ?
485 (intmax_t)ktr_header.ktr_tid : 0,
486 MAXCOMLEN, ktr_header.ktr_comm);
488 } else if (!drop_logged) {
489 printf("%6jd %-8.*s Events dropped.\n",
490 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
491 ktr_header.ktr_comm);
495 if (trpoints & (1<<ktr_header.ktr_type))
496 if (pid == 0 || ktr_header.ktr_pid == pid ||
497 ktr_header.ktr_tid == pid)
498 dumpheader(&ktr_header);
499 if ((ktrlen = ktr_header.ktr_len) < 0)
500 errx(1, "bogus length 0x%x", ktrlen);
502 m = realloc(m, ktrlen+1);
504 errx(1, "%s", strerror(ENOMEM));
507 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
508 errx(1, "data too short");
509 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
511 sv_flags = abidump(&ktr_header);
512 if (pid && ktr_header.ktr_pid != pid &&
513 ktr_header.ktr_tid != pid)
515 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
518 switch (ktr_header.ktr_type) {
520 ktrsyscall((struct ktr_syscall *)m, sv_flags);
523 ktrsysret((struct ktr_sysret *)m, sv_flags);
530 ktrgenio((struct ktr_genio *)m, ktrlen);
533 ktrpsig((struct ktr_psig *)m);
536 if (ktrlen == sizeof(struct ktr_csw_old))
537 ktrcsw_old((struct ktr_csw_old *)m);
539 ktrcsw((struct ktr_csw *)m);
545 ktrstruct(m, ktrlen);
548 ktrcapfail((struct ktr_cap_fail *)m);
551 ktrfault((struct ktr_fault *)m);
554 ktrfaultend((struct ktr_faultend *)m);
556 case KTR_STRUCT_ARRAY:
557 ktrstructarray((struct ktr_struct_array *)m, ktrlen);
570 fread_tail(void *buf, int size, int num)
574 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
582 fetchprocinfo(struct ktr_header *kth, u_int *flags)
584 struct proc_info *pi;
586 switch (kth->ktr_type) {
588 TAILQ_FOREACH(pi, &trace_procs, info) {
589 if (pi->pid == kth->ktr_pid) {
590 TAILQ_REMOVE(&trace_procs, pi, info);
594 pi = malloc(sizeof(struct proc_info));
596 errx(1, "%s", strerror(ENOMEM));
597 pi->sv_flags = *flags;
598 pi->pid = kth->ktr_pid;
599 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
603 TAILQ_FOREACH(pi, &trace_procs, info) {
604 if (pi->pid == kth->ktr_pid) {
605 TAILQ_REMOVE(&trace_procs, pi, info);
617 abidump(struct ktr_header *kth)
619 struct proc_info *pi;
624 TAILQ_FOREACH(pi, &trace_procs, info) {
625 if (pi->pid == kth->ktr_pid) {
626 flags = pi->sv_flags;
634 switch (flags & SV_ABI_MASK) {
641 case SV_ABI_CLOUDABI:
651 else if (flags & SV_ILP32)
656 printf("%s%s ", abi, arch);
662 dumpheader(struct ktr_header *kth)
664 static char unknown[64];
665 static struct timeval prevtime, prevtime_e;
670 switch (kth->ktr_type) {
693 case KTR_STRUCT_ARRAY:
713 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
718 * The ktr_tid field was previously the ktr_buffer field, which held
719 * the kernel pointer value for the buffer associated with data
720 * following the record header. It now holds a threadid, but only
721 * for trace files after the change. Older trace files still contain
722 * kernel pointers. Detect this and suppress the results by printing
723 * negative tid's as 0.
726 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
727 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
728 MAXCOMLEN, kth->ktr_comm);
730 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
733 if (timestamp & TIMESTAMP_ABSOLUTE) {
734 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
735 kth->ktr_time.tv_usec);
737 if (timestamp & TIMESTAMP_ELAPSED) {
738 if (prevtime_e.tv_sec == 0)
739 prevtime_e = kth->ktr_time;
740 timersub(&kth->ktr_time, &prevtime_e, &temp);
741 printf("%jd.%06ld ", (intmax_t)temp.tv_sec,
744 if (timestamp & TIMESTAMP_RELATIVE) {
745 if (prevtime.tv_sec == 0)
746 prevtime = kth->ktr_time;
747 if (timercmp(&kth->ktr_time, &prevtime, <)) {
748 timersub(&prevtime, &kth->ktr_time, &temp);
751 timersub(&kth->ktr_time, &prevtime, &temp);
754 prevtime = kth->ktr_time;
755 printf("%s%jd.%06ld ", sign, (intmax_t)temp.tv_sec,
762 #include <sys/syscall.h>
765 ioctlname(unsigned long val)
769 str = sysdecode_ioctlname(val);
778 static enum sysdecode_abi
779 syscallabi(u_int sv_flags)
783 return (SYSDECODE_ABI_FREEBSD);
784 switch (sv_flags & SV_ABI_MASK) {
786 return (SYSDECODE_ABI_FREEBSD);
787 #if defined(__amd64__) || defined(__i386__)
790 if (sv_flags & SV_ILP32)
791 return (SYSDECODE_ABI_LINUX32);
793 return (SYSDECODE_ABI_LINUX);
795 #if defined(__aarch64__) || defined(__amd64__)
796 case SV_ABI_CLOUDABI:
797 return (SYSDECODE_ABI_CLOUDABI64);
800 return (SYSDECODE_ABI_UNKNOWN);
805 syscallname(u_int code, u_int sv_flags)
809 name = sysdecode_syscallname(syscallabi(sv_flags), code);
811 printf("[%d]", code);
815 printf("[%d]", code);
820 print_signal(int signo)
824 signame = sysdecode_signal(signo);
826 printf("%s", signame);
828 printf("SIG %d", signo);
832 ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags)
834 int narg = ktr->ktr_narg;
835 register_t *ip, *first;
837 int quad_align, quad_slots;
839 syscallname(ktr->ktr_code, sv_flags);
840 ip = first = &ktr->ktr_args[0];
845 (sv_flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
847 if (sv_flags & SV_ILP32) {
854 switch (ktr->ktr_code) {
873 print_integer_arg_valid(sysdecode_atfd, *ip);
879 switch (ktr->ktr_code) {
881 print_number(ip, narg, c);
891 print_integer_arg(sysdecode_ptrace_request, *ip);
899 print_number(ip, narg, c);
901 print_mask_arg(sysdecode_access_mode, *ip);
907 print_number(ip, narg, c);
909 print_mask_arg(sysdecode_open_flags, ip[0]);
910 if ((ip[0] & O_CREAT) == O_CREAT) {
912 decode_filemode(ip[1]);
918 print_number(ip, narg, c);
919 print_number(ip, narg, c);
921 print_mask_arg0(sysdecode_wait4_options, *ip);
927 print_integer_arg(sysdecode_idtype, *ip);
931 print_number64(first, ip, narg, c);
932 print_number(ip, narg, c);
934 print_mask_arg(sysdecode_wait6_options, *ip);
942 print_number(ip, narg, c);
944 decode_filemode(*ip);
949 print_number(ip, narg, c);
951 decode_filemode(*ip);
956 print_number(ip, narg, c);
957 print_number(ip, narg, c);
959 print_integer_arg(sysdecode_getfsstat_mode, *ip);
964 print_number(ip, narg, c);
965 print_number(ip, narg, c);
967 print_mask_arg(sysdecode_mount_flags, *ip);
972 print_number(ip, narg, c);
974 print_mask_arg(sysdecode_mount_flags, *ip);
980 print_number(ip, narg, c);
981 print_number(ip, narg, c);
983 print_mask_arg0(sysdecode_msg_flags, *ip);
989 print_number(ip, narg, c);
990 print_number(ip, narg, c);
991 print_number(ip, narg, c);
993 print_mask_arg0(sysdecode_msg_flags, *ip);
1001 print_number(ip, narg, c);
1003 decode_fileflags(*ip);
1008 print_number(ip, narg, c);
1016 print_mask_arg(sysdecode_reboot_howto, *ip);
1022 decode_filemode(*ip);
1027 print_number(ip, narg, c);
1028 print_number(ip, narg, c);
1030 print_mask_arg(sysdecode_msync_flags, *ip);
1034 #ifdef SYS_freebsd6_mmap
1035 case SYS_freebsd6_mmap:
1036 print_number(ip, narg, c);
1037 print_number(ip, narg, c);
1039 print_mask_arg(sysdecode_mmap_prot, *ip);
1043 print_mask_arg(sysdecode_mmap_flags, *ip);
1049 print_number(ip, narg, c);
1050 print_number(ip, narg, c);
1052 print_mask_arg(sysdecode_mmap_prot, *ip);
1056 print_mask_arg(sysdecode_mmap_flags, *ip);
1061 print_number(ip, narg, c);
1062 print_number(ip, narg, c);
1064 print_mask_arg(sysdecode_mmap_prot, *ip);
1069 print_number(ip, narg, c);
1070 print_number(ip, narg, c);
1072 print_integer_arg(sysdecode_madvice, *ip);
1079 print_number(ip, narg, c);
1081 print_integer_arg(sysdecode_pathconf_name, *ip);
1085 case SYS_getpriority:
1086 case SYS_setpriority:
1088 print_integer_arg(sysdecode_prio_which, *ip);
1094 print_number(ip, narg, c);
1096 print_integer_arg(sysdecode_fcntl_cmd, ip[0]);
1097 if (sysdecode_fcntl_arg_p(ip[0])) {
1099 if (ip[0] == F_SETFL)
1101 sysdecode_fcntl_fileflags,
1104 sysdecode_fcntl_arg(stdout,
1115 print_integer_arg(sysdecode_socketdomain,
1120 print_mask_arg(sysdecode_socket_type, *ip);
1123 if (sockdomain == PF_INET ||
1124 sockdomain == PF_INET6) {
1126 print_integer_arg(sysdecode_ipproto,
1134 case SYS_setsockopt:
1135 case SYS_getsockopt: {
1138 print_number(ip, narg, c);
1140 print_integer_arg_valid(sysdecode_sockopt_level,
1142 str = sysdecode_sockopt_name(ip[0], ip[1]);
1152 #ifdef SYS_freebsd6_lseek
1153 case SYS_freebsd6_lseek:
1154 print_number(ip, narg, c);
1155 /* Hidden 'pad' argument, not in lseek(2) */
1156 print_number(ip, narg, c);
1157 print_number64(first, ip, narg, c);
1159 print_integer_arg(sysdecode_whence, *ip);
1165 print_number(ip, narg, c);
1166 print_number64(first, ip, narg, c);
1168 print_integer_arg(sysdecode_whence, *ip);
1173 print_number(ip, narg, c);
1175 print_mask_arg(sysdecode_flock_operation, *ip);
1183 print_number(ip, narg, c);
1185 decode_filemode(*ip);
1190 print_number(ip, narg, c);
1192 print_integer_arg(sysdecode_shutdown_how, *ip);
1196 case SYS_socketpair:
1198 print_integer_arg(sysdecode_socketdomain, *ip);
1202 print_mask_arg(sysdecode_socket_type, *ip);
1210 print_integer_arg(sysdecode_rlimit, *ip);
1217 print_integer_arg(sysdecode_getrusage_who, *ip);
1223 print_number(ip, narg, c);
1225 if (!sysdecode_quotactl_cmd(stdout, *ip)) {
1227 printf("<invalid=%d>", (int)*ip);
1229 printf("<invalid=%#x>",
1238 print_integer_arg(sysdecode_nfssvc_flags, *ip);
1244 case SYS_rtprio_thread:
1246 print_integer_arg(sysdecode_rtprio_function,
1253 print_number(ip, narg, c);
1254 print_number(ip, narg, c);
1256 print_integer_arg(sysdecode_semctl_cmd, *ip);
1261 print_number(ip, narg, c);
1262 print_number(ip, narg, c);
1264 print_mask_arg(sysdecode_semget_flags, *ip);
1269 print_number(ip, narg, c);
1271 print_integer_arg(sysdecode_msgctl_cmd, *ip);
1276 print_number(ip, narg, c);
1277 print_number(ip, narg, c);
1279 print_mask_arg(sysdecode_shmat_flags, *ip);
1284 print_number(ip, narg, c);
1286 print_integer_arg(sysdecode_shmctl_cmd, *ip);
1291 print_number(ip, narg, c);
1293 print_mask_arg(sysdecode_open_flags, ip[0]);
1295 decode_filemode(ip[1]);
1300 print_number(ip, narg, c);
1301 print_number(ip, narg, c);
1303 print_integer_arg(sysdecode_minherit_inherit,
1310 print_mask_arg(sysdecode_rfork_flags, *ip);
1315 case SYS_lio_listio:
1317 print_integer_arg(sysdecode_lio_listio_mode,
1325 print_mask_arg(sysdecode_mlockall_flags, *ip);
1329 case SYS_sched_setscheduler:
1330 print_number(ip, narg, c);
1332 print_integer_arg(sysdecode_scheduler_policy,
1337 case SYS_sched_get_priority_max:
1338 case SYS_sched_get_priority_min:
1340 print_integer_arg(sysdecode_scheduler_policy,
1346 print_number(ip, narg, c);
1347 print_number(ip, narg, c);
1348 print_number(ip, narg, c);
1349 print_number(ip, narg, c);
1350 print_number(ip, narg, c);
1351 print_number(ip, narg, c);
1353 print_mask_arg(sysdecode_sendfile_flags, *ip);
1358 print_number(ip, narg, c);
1360 print_integer_arg(sysdecode_kldsym_cmd, *ip);
1364 case SYS_sigprocmask:
1366 print_integer_arg(sysdecode_sigprocmask_how,
1372 case SYS___acl_get_file:
1373 case SYS___acl_set_file:
1374 case SYS___acl_get_fd:
1375 case SYS___acl_set_fd:
1376 case SYS___acl_delete_file:
1377 case SYS___acl_delete_fd:
1378 case SYS___acl_aclcheck_file:
1379 case SYS___acl_aclcheck_fd:
1380 case SYS___acl_get_link:
1381 case SYS___acl_set_link:
1382 case SYS___acl_delete_link:
1383 case SYS___acl_aclcheck_link:
1384 print_number(ip, narg, c);
1386 print_integer_arg(sysdecode_acltype, *ip);
1397 case SYS_extattrctl:
1398 print_number(ip, narg, c);
1400 print_integer_arg(sysdecode_extattrnamespace,
1406 print_number(ip, narg, c);
1407 print_number(ip, narg, c);
1409 print_mask_arg(sysdecode_mount_flags, *ip);
1413 case SYS_thr_create:
1414 print_number(ip, narg, c);
1415 print_number(ip, narg, c);
1417 print_mask_arg(sysdecode_thr_create_flags, *ip);
1422 print_number(ip, narg, c);
1428 case SYS_kldunloadf:
1429 print_number(ip, narg, c);
1431 print_integer_arg(sysdecode_kldunload_flags,
1439 print_number(ip, narg, c);
1441 print_integer_arg_valid(sysdecode_atfd, *ip);
1444 print_number(ip, narg, c);
1446 case SYS_cap_fcntls_limit:
1447 print_number(ip, narg, c);
1452 print_mask_arg32(sysdecode_cap_fcntlrights, arg);
1454 case SYS_posix_fadvise:
1455 print_number(ip, narg, c);
1456 print_number(ip, narg, c);
1457 print_number(ip, narg, c);
1459 print_integer_arg(sysdecode_fadvice, *ip);
1465 print_integer_arg(sysdecode_idtype, *ip);
1469 print_number64(first, ip, narg, c);
1471 print_integer_arg(sysdecode_procctl_cmd, *ip);
1476 print_number(ip, narg, c);
1478 print_integer_arg(sysdecode_umtx_op, *ip);
1480 case UMTX_OP_CV_WAIT:
1485 sysdecode_umtx_cvwait_flags, *ip);
1487 case UMTX_OP_RW_RDLOCK:
1492 sysdecode_umtx_rwlock_flags, *ip);
1500 print_number(ip, narg, c);
1501 print_number64(first, ip, narg, c);
1504 print_number(ip, narg, c);
1505 print_number(ip, narg, c);
1506 print_number(ip, narg, c);
1510 print_number(ip, narg, c);
1511 print_number(ip, narg, c);
1514 print_number(ip, narg, c);
1518 print_integer_arg(sysdecode_sysarch_number, *ip);
1524 switch (ktr->ktr_code) {
1534 print_mask_arg0(sysdecode_atflags, *ip);
1541 print_number(ip, narg, c);
1549 ktrsysret(struct ktr_sysret *ktr, u_int sv_flags)
1551 register_t ret = ktr->ktr_retval;
1552 int error = ktr->ktr_error;
1554 syscallname(ktr->ktr_code, sv_flags);
1559 printf("%ld", (long)ret);
1560 if (ret < 0 || ret > 9)
1561 printf("/%#lx", (unsigned long)ret);
1564 printf("%ld", (long)ret);
1566 printf("%#lx", (unsigned long)ret);
1568 } else if (error == ERESTART)
1570 else if (error == EJUSTRETURN)
1571 printf("JUSTRETURN");
1573 printf("-1 errno %d", sysdecode_freebsd_to_abi_errno(
1574 syscallabi(sv_flags), error));
1576 printf(" %s", strerror(ktr->ktr_error));
1582 ktrnamei(char *cp, int len)
1584 printf("\"%.*s\"\n", len, cp);
1588 hexdump(char *p, int len, int screenwidth)
1596 i = 13; /* base offset */
1597 i += (width / 2) + 1; /* spaces every second byte */
1598 i += (width * 2); /* width of bytes */
1600 i += width; /* each byte */
1602 } while (i < screenwidth);
1605 for (n = 0; n < len; n += width) {
1606 for (i = n; i < n + width; i++) {
1607 if ((i % width) == 0) { /* beginning of line */
1608 printf(" 0x%04x", i);
1614 printf("%02x", p[i] & 0xff);
1619 for (i = n; i < n + width; i++) {
1622 if (p[i] >= ' ' && p[i] <= '~')
1629 if ((i % width) != 0)
1634 visdump(char *dp, int datalen, int screenwidth)
1643 for (;datalen > 0; datalen--, dp++) {
1644 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1647 * Keep track of printables and
1648 * space chars (like fold(1)).
1660 width = 8 - (col&07);
1665 if (col + width > (screenwidth-2)) {
1680 ktrgenio(struct ktr_genio *ktr, int len)
1682 int datalen = len - sizeof (struct ktr_genio);
1683 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1684 static int screenwidth = 0;
1687 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1688 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1689 datalen == 1 ? "" : "s");
1692 if (screenwidth == 0) {
1695 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1697 screenwidth = ws.ws_col;
1701 if (maxdata && datalen > maxdata)
1704 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1705 if (dp[i] >= 32 && dp[i] < 127)
1707 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1712 hexdump(dp, datalen, screenwidth);
1714 visdump(dp, datalen, screenwidth);
1718 ktrpsig(struct ktr_psig *psig)
1722 print_signal(psig->signo);
1723 if (psig->action == SIG_DFL) {
1726 printf(" caught handler=0x%lx mask=0x%x",
1727 (u_long)psig->action, psig->mask.__bits[0]);
1730 str = sysdecode_sigcode(psig->signo, psig->code);
1734 printf("<invalid=%#x>", psig->code);
1739 ktrcsw_old(struct ktr_csw_old *cs)
1741 printf("%s %s\n", cs->out ? "stop" : "resume",
1742 cs->user ? "user" : "kernel");
1746 ktrcsw(struct ktr_csw *cs)
1748 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1749 cs->user ? "user" : "kernel", cs->wmesg);
1753 ktruser(int len, void *p)
1757 if (sysdecode_utrace(stdout, p, len)) {
1766 printf(" %d", *cp++);
1768 printf(" %02x", *cp++);
1773 ktrcaprights(cap_rights_t *rightsp)
1776 printf("cap_rights_t ");
1777 sysdecode_cap_rights(stdout, rightsp);
1782 ktrtimeval(struct timeval *tv)
1785 printf("{%ld, %ld}", (long)tv->tv_sec, tv->tv_usec);
1789 ktritimerval(struct itimerval *it)
1792 printf("itimerval { .interval = ");
1793 ktrtimeval(&it->it_interval);
1794 printf(", .value = ");
1795 ktrtimeval(&it->it_value);
1800 ktrsockaddr(struct sockaddr *sa)
1803 TODO: Support additional address families
1804 #include <netsmb/netbios.h>
1805 struct sockaddr_nb *nb;
1811 * note: ktrstruct() has already verified that sa points to a
1812 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1813 * sa->sa_len bytes long.
1815 printf("struct sockaddr { ");
1816 str = sysdecode_sockaddr_family(sa->sa_family);
1820 printf("<invalid=%d>", sa->sa_family);
1823 #define check_sockaddr_len(n) \
1824 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1825 printf("invalid"); \
1829 switch(sa->sa_family) {
1831 struct sockaddr_in sa_in;
1833 memset(&sa_in, 0, sizeof(sa_in));
1834 memcpy(&sa_in, sa, sa->sa_len);
1835 check_sockaddr_len(in);
1836 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1837 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1841 struct sockaddr_in6 sa_in6;
1843 memset(&sa_in6, 0, sizeof(sa_in6));
1844 memcpy(&sa_in6, sa, sa->sa_len);
1845 check_sockaddr_len(in6);
1846 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1847 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1848 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1852 struct sockaddr_un sa_un;
1854 memset(&sa_un, 0, sizeof(sa_un));
1855 memcpy(&sa_un, sa, sa->sa_len);
1856 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1860 printf("unknown address family");
1866 ktrstat(struct stat *statp)
1868 char mode[12], timestr[PATH_MAX + 4];
1874 * note: ktrstruct() has already verified that statp points to a
1875 * buffer exactly sizeof(struct stat) bytes long.
1877 printf("struct stat {");
1878 printf("dev=%ju, ino=%ju, ",
1879 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1881 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1883 strmode(statp->st_mode, mode);
1884 printf("mode=%s, ", mode);
1886 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1892 pwd = cap_getpwuid(cappwd, statp->st_uid);
1895 pwd = getpwuid(statp->st_uid);
1898 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1900 printf("uid=\"%s\", ", pwd->pw_name);
1906 grp = cap_getgrgid(capgrp, statp->st_gid);
1909 grp = getgrgid(statp->st_gid);
1912 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1914 printf("gid=\"%s\", ", grp->gr_name);
1915 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1918 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1920 tm = localtime(&statp->st_atim.tv_sec);
1921 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1922 printf("\"%s\"", timestr);
1924 if (statp->st_atim.tv_nsec != 0)
1925 printf(".%09ld, ", statp->st_atim.tv_nsec);
1930 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1932 tm = localtime(&statp->st_mtim.tv_sec);
1933 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1934 printf("\"%s\"", timestr);
1936 if (statp->st_mtim.tv_nsec != 0)
1937 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1942 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1944 tm = localtime(&statp->st_ctim.tv_sec);
1945 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1946 printf("\"%s\"", timestr);
1948 if (statp->st_ctim.tv_nsec != 0)
1949 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1952 printf("birthtime=");
1954 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1956 tm = localtime(&statp->st_birthtim.tv_sec);
1957 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1958 printf("\"%s\"", timestr);
1960 if (statp->st_birthtim.tv_nsec != 0)
1961 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1964 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1965 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1966 (intmax_t)statp->st_blocks, statp->st_flags);
1971 ktrstruct(char *buf, size_t buflen)
1974 size_t namelen, datalen;
1976 cap_rights_t rights;
1977 struct itimerval it;
1979 struct sockaddr_storage ss;
1981 for (name = buf, namelen = 0;
1982 namelen < buflen && name[namelen] != '\0';
1985 if (namelen == buflen)
1987 if (name[namelen] != '\0')
1989 data = buf + namelen + 1;
1990 datalen = buflen - namelen - 1;
1994 for (i = 0; i < (int)namelen; ++i)
1995 if (!isalpha(name[i]))
1997 if (strcmp(name, "caprights") == 0) {
1998 if (datalen != sizeof(cap_rights_t))
2000 memcpy(&rights, data, datalen);
2001 ktrcaprights(&rights);
2002 } else if (strcmp(name, "itimerval") == 0) {
2003 if (datalen != sizeof(struct itimerval))
2005 memcpy(&it, data, datalen);
2007 } else if (strcmp(name, "stat") == 0) {
2008 if (datalen != sizeof(struct stat))
2010 memcpy(&sb, data, datalen);
2012 } else if (strcmp(name, "sockaddr") == 0) {
2013 if (datalen > sizeof(ss))
2015 memcpy(&ss, data, datalen);
2016 if (datalen != ss.ss_len)
2018 ktrsockaddr((struct sockaddr *)&ss);
2020 printf("unknown structure\n");
2024 printf("invalid record\n");
2028 ktrcapfail(struct ktr_cap_fail *ktr)
2030 switch (ktr->cap_type) {
2031 case CAPFAIL_NOTCAPABLE:
2032 /* operation on fd with insufficient capabilities */
2033 printf("operation requires ");
2034 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2035 printf(", descriptor holds ");
2036 sysdecode_cap_rights(stdout, &ktr->cap_held);
2038 case CAPFAIL_INCREASE:
2039 /* requested more capabilities than fd already has */
2040 printf("attempt to increase capabilities from ");
2041 sysdecode_cap_rights(stdout, &ktr->cap_held);
2043 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2045 case CAPFAIL_SYSCALL:
2046 /* called restricted syscall */
2047 printf("disallowed system call");
2049 case CAPFAIL_LOOKUP:
2050 /* used ".." in strict-relative mode */
2051 printf("restricted VFS lookup");
2054 printf("unknown capability failure: ");
2055 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2057 sysdecode_cap_rights(stdout, &ktr->cap_held);
2064 ktrfault(struct ktr_fault *ktr)
2067 printf("0x%jx ", (uintmax_t)ktr->vaddr);
2068 print_mask_arg(sysdecode_vmprot, ktr->type);
2073 ktrfaultend(struct ktr_faultend *ktr)
2077 str = sysdecode_vmresult(ktr->result);
2081 printf("<invalid=%d>", ktr->result);
2086 ktrkevent(struct kevent *kev)
2090 switch (kev->filter) {
2096 case EVFILT_PROCDESC:
2098 printf("%ju", (uintmax_t)kev->ident);
2101 print_signal(kev->ident);
2104 printf("%p", (void *)kev->ident);
2106 printf(", filter=");
2107 print_integer_arg(sysdecode_kevent_filter, kev->filter);
2109 print_mask_arg0(sysdecode_kevent_flags, kev->flags);
2110 printf(", fflags=");
2111 sysdecode_kevent_fflags(stdout, kev->filter, kev->fflags,
2113 printf(", data=%#jx, udata=%p }", (uintmax_t)kev->data, kev->udata);
2117 ktrstructarray(struct ktr_struct_array *ksa, size_t buflen)
2121 size_t namelen, datalen;
2125 buflen -= sizeof(*ksa);
2126 for (name = (char *)(ksa + 1), namelen = 0;
2127 namelen < buflen && name[namelen] != '\0';
2130 if (namelen == buflen)
2132 if (name[namelen] != '\0')
2135 for (i = 0; i < (int)namelen; ++i)
2136 if (!isalnum(name[i]) && name[i] != '_')
2138 data = name + namelen + 1;
2139 datalen = buflen - namelen - 1;
2140 printf("struct %s[] = { ", name);
2142 for (; datalen >= ksa->struct_size;
2143 data += ksa->struct_size, datalen -= ksa->struct_size) {
2148 if (strcmp(name, "kevent") == 0) {
2149 if (ksa->struct_size != sizeof(kev))
2151 memcpy(&kev, data, sizeof(kev));
2153 } else if (strcmp(name, "kevent_freebsd11") == 0) {
2154 struct kevent_freebsd11 kev11;
2156 if (ksa->struct_size != sizeof(kev11))
2158 memcpy(&kev11, data, sizeof(kev11));
2159 memset(&kev, 0, sizeof(kev));
2160 kev.ident = kev11.ident;
2161 kev.filter = kev11.filter;
2162 kev.flags = kev11.flags;
2163 kev.fflags = kev11.fflags;
2164 kev.data = kev11.data;
2165 kev.udata = kev11.udata;
2167 #ifdef _WANT_KEVENT32
2168 } else if (strcmp(name, "kevent32") == 0) {
2169 struct kevent32 kev32;
2171 if (ksa->struct_size != sizeof(kev32))
2173 memcpy(&kev32, data, sizeof(kev32));
2174 memset(&kev, 0, sizeof(kev));
2175 kev.ident = kev32.ident;
2176 kev.filter = kev32.filter;
2177 kev.flags = kev32.flags;
2178 kev.fflags = kev32.fflags;
2179 #if BYTE_ORDER == BIG_ENDIAN
2180 kev.data = kev32.data2 | ((int64_t)kev32.data1 << 32);
2182 kev.data = kev32.data1 | ((int64_t)kev32.data2 << 32);
2184 kev.udata = (void *)(uintptr_t)kev32.udata;
2186 } else if (strcmp(name, "kevent32_freebsd11") == 0) {
2187 struct kevent32_freebsd11 kev32;
2189 if (ksa->struct_size != sizeof(kev32))
2191 memcpy(&kev32, data, sizeof(kev32));
2192 memset(&kev, 0, sizeof(kev));
2193 kev.ident = kev32.ident;
2194 kev.filter = kev32.filter;
2195 kev.flags = kev32.flags;
2196 kev.fflags = kev32.fflags;
2197 kev.data = kev32.data;
2198 kev.udata = (void *)(uintptr_t)kev32.udata;
2202 printf("<unknown structure> }\n");
2209 printf("invalid record\n");
2212 printf("<bad size> }\n");
2219 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
2220 "[-m maxdata] [-p pid] [-t trstr]\n");