2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1988, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)kdump.c 8.1 (Berkeley) 6/6/93";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #define _WANT_KERNEL_ERRNO
45 #include <sys/param.h>
46 #include <sys/capsicum.h>
47 #include <sys/errno.h>
50 #include <sys/ktrace.h>
51 #include <sys/ioctl.h>
52 #include <sys/socket.h>
54 #include <sys/sysent.h>
57 #include <sys/queue.h>
62 #include <arpa/inet.h>
63 #include <netinet/in.h>
65 #include <capsicum_helpers.h>
77 #include <sysdecode.h>
84 #include <libcasper.h>
86 #include <casper/cap_grp.h>
87 #include <casper/cap_pwd.h>
90 u_int abidump(struct ktr_header *);
91 int fetchprocinfo(struct ktr_header *, u_int *);
92 int fread_tail(void *, int, int);
93 void dumpheader(struct ktr_header *);
94 void ktrsyscall(struct ktr_syscall *, u_int);
95 void ktrsysret(struct ktr_sysret *, u_int);
96 void ktrnamei(char *, int);
97 void hexdump(char *, int, int);
98 void visdump(char *, int, int);
99 void ktrgenio(struct ktr_genio *, int);
100 void ktrpsig(struct ktr_psig *);
101 void ktrcsw(struct ktr_csw *);
102 void ktrcsw_old(struct ktr_csw_old *);
103 void ktruser(int, void *);
104 void ktrcaprights(cap_rights_t *);
105 void ktritimerval(struct itimerval *it);
106 void ktrsockaddr(struct sockaddr *);
107 void ktrstat(struct stat *);
108 void ktrstruct(char *, size_t);
109 void ktrcapfail(struct ktr_cap_fail *);
110 void ktrfault(struct ktr_fault *);
111 void ktrfaultend(struct ktr_faultend *);
114 #define TIMESTAMP_NONE 0x0
115 #define TIMESTAMP_ABSOLUTE 0x1
116 #define TIMESTAMP_ELAPSED 0x2
117 #define TIMESTAMP_RELATIVE 0x4
119 static int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
120 resolv = 0, abiflag = 0, syscallno = 0;
121 static const char *tracefile = DEF_TRACEFILE;
122 static struct ktr_header ktr_header;
124 #define TIME_FORMAT "%b %e %T %Y"
125 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
127 #define print_number64(first,i,n,c) do { \
130 if (quad_align && (((ptrdiff_t)((i) - (first))) & 1) == 1) { \
134 if (quad_slots == 2) \
135 __v = (uint64_t)(uint32_t)(i)[0] | \
136 ((uint64_t)(uint32_t)(i)[1]) << 32; \
138 __v = (uint64_t)*(i); \
140 printf("%c%jd", (c), (intmax_t)__v); \
142 printf("%c%#jx", (c), (uintmax_t)__v); \
148 #define print_number(i,n,c) do { \
150 printf("%c%jd", c, (intmax_t)*i); \
152 printf("%c%#jx", c, (uintmax_t)(u_register_t)*i); \
160 TAILQ_ENTRY(proc_info) info;
165 static TAILQ_HEAD(trace_procs, proc_info) trace_procs;
167 #ifdef HAVE_LIBCASPER
168 static cap_channel_t *cappwd, *capgrp;
176 * Cache NLS data before entering capability mode.
177 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
179 (void)catopen("libc", NL_CAT_LOCALE);
188 * Allow localtime(3) to cache /etc/localtime content before entering
190 * XXXPJD: There should be localtime_init() in libc.
193 (void)localtime(<ime);
196 #ifdef HAVE_LIBCASPER
198 cappwdgrp_setup(cap_channel_t **cappwdp, cap_channel_t **capgrpp)
200 cap_channel_t *capcas, *cappwdloc, *capgrploc;
201 const char *cmds[1], *fields[1];
204 if (capcas == NULL) {
205 err(1, "unable to create casper process");
208 cappwdloc = cap_service_open(capcas, "system.pwd");
209 capgrploc = cap_service_open(capcas, "system.grp");
210 /* Casper capability no longer needed. */
212 if (cappwdloc == NULL || capgrploc == NULL) {
213 if (cappwdloc == NULL)
214 warn("unable to open system.pwd service");
215 if (capgrploc == NULL)
216 warn("unable to open system.grp service");
219 /* Limit system.pwd to only getpwuid() function and pw_name field. */
220 cmds[0] = "getpwuid";
221 if (cap_pwd_limit_cmds(cappwdloc, cmds, 1) < 0)
222 err(1, "unable to limit system.pwd service");
223 fields[0] = "pw_name";
224 if (cap_pwd_limit_fields(cappwdloc, fields, 1) < 0)
225 err(1, "unable to limit system.pwd service");
226 /* Limit system.grp to only getgrgid() function and gr_name field. */
227 cmds[0] = "getgrgid";
228 if (cap_grp_limit_cmds(capgrploc, cmds, 1) < 0)
229 err(1, "unable to limit system.grp service");
230 fields[0] = "gr_name";
231 if (cap_grp_limit_fields(capgrploc, fields, 1) < 0)
232 err(1, "unable to limit system.grp service");
234 *cappwdp = cappwdloc;
235 *capgrpp = capgrploc;
238 #endif /* HAVE_LIBCASPER */
241 print_integer_arg(const char *(*decoder)(int), int value)
245 str = decoder(value);
250 printf("<invalid=%d>", value);
252 printf("<invalid=%#x>", value);
256 /* Like print_integer_arg but unknown values are treated as valid. */
258 print_integer_arg_valid(const char *(*decoder)(int), int value)
262 str = decoder(value);
269 printf("%#x", value);
274 print_mask_arg(bool (*decoder)(FILE *, int, int *), int value)
279 printf("%#x<", value);
280 invalid = !decoder(stdout, value, &rem);
283 printf("<invalid>%u", rem);
287 print_mask_arg0(bool (*decoder)(FILE *, int, int *), int value)
296 printf("%#x<", value);
297 invalid = !decoder(stdout, value, &rem);
300 printf("<invalid>%u", rem);
304 decode_fileflags(fflags_t value)
313 printf("%#x<", value);
314 invalid = !sysdecode_fileflags(stdout, value, &rem);
317 printf("<invalid>%u", rem);
321 decode_filemode(int value)
330 printf("%#o<", value);
331 invalid = !sysdecode_filemode(stdout, value, &rem);
334 printf("<invalid>%u", rem);
338 print_mask_arg32(bool (*decoder)(FILE *, uint32_t, uint32_t *), uint32_t value)
343 printf("%#x<", value);
344 invalid = !decoder(stdout, value, &rem);
347 printf("<invalid>%u", rem);
351 print_mask_argul(bool (*decoder)(FILE *, u_long, u_long *), u_long value)
360 printf("%#lx<", value);
361 invalid = !decoder(stdout, value, &rem);
364 printf("<invalid>%lu", rem);
368 main(int argc, char *argv[])
370 int ch, ktrlen, size;
372 int trpoints = ALL_POINTS;
377 setlocale(LC_CTYPE, "");
379 timestamp = TIMESTAMP_NONE;
381 while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1)
396 maxdata = atoi(optarg);
414 timestamp |= TIMESTAMP_ELAPSED;
420 timestamp |= TIMESTAMP_RELATIVE;
423 timestamp |= TIMESTAMP_ABSOLUTE;
426 trpoints = getpoints(optarg);
428 errx(1, "unknown trace point in %s", optarg);
437 m = malloc(size = 1025);
439 errx(1, "%s", strerror(ENOMEM));
440 if (strcmp(tracefile, "-") != 0)
441 if (!freopen(tracefile, "r", stdin))
442 err(1, "%s", tracefile);
446 #ifdef HAVE_LIBCASPER
448 if (cappwdgrp_setup(&cappwd, &capgrp) < 0) {
453 if (resolv == 0 || (cappwd != NULL && capgrp != NULL)) {
454 if (cap_enter() < 0 && errno != ENOSYS)
455 err(1, "unable to enter capability mode");
459 if (cap_enter() < 0 && errno != ENOSYS)
460 err(1, "unable to enter capability mode");
463 if (caph_limit_stdio() == -1)
464 err(1, "unable to limit stdio");
466 TAILQ_INIT(&trace_procs);
468 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
469 if (ktr_header.ktr_type & KTR_DROP) {
470 ktr_header.ktr_type &= ~KTR_DROP;
471 if (!drop_logged && threads) {
473 "%6jd %6jd %-8.*s Events dropped.\n",
474 (intmax_t)ktr_header.ktr_pid,
475 ktr_header.ktr_tid > 0 ?
476 (intmax_t)ktr_header.ktr_tid : 0,
477 MAXCOMLEN, ktr_header.ktr_comm);
479 } else if (!drop_logged) {
480 printf("%6jd %-8.*s Events dropped.\n",
481 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
482 ktr_header.ktr_comm);
486 if (trpoints & (1<<ktr_header.ktr_type))
487 if (pid == 0 || ktr_header.ktr_pid == pid ||
488 ktr_header.ktr_tid == pid)
489 dumpheader(&ktr_header);
490 if ((ktrlen = ktr_header.ktr_len) < 0)
491 errx(1, "bogus length 0x%x", ktrlen);
493 m = realloc(m, ktrlen+1);
495 errx(1, "%s", strerror(ENOMEM));
498 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
499 errx(1, "data too short");
500 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
502 sv_flags = abidump(&ktr_header);
503 if (pid && ktr_header.ktr_pid != pid &&
504 ktr_header.ktr_tid != pid)
506 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
509 switch (ktr_header.ktr_type) {
511 ktrsyscall((struct ktr_syscall *)m, sv_flags);
514 ktrsysret((struct ktr_sysret *)m, sv_flags);
521 ktrgenio((struct ktr_genio *)m, ktrlen);
524 ktrpsig((struct ktr_psig *)m);
527 if (ktrlen == sizeof(struct ktr_csw_old))
528 ktrcsw_old((struct ktr_csw_old *)m);
530 ktrcsw((struct ktr_csw *)m);
536 ktrstruct(m, ktrlen);
539 ktrcapfail((struct ktr_cap_fail *)m);
542 ktrfault((struct ktr_fault *)m);
545 ktrfaultend((struct ktr_faultend *)m);
558 fread_tail(void *buf, int size, int num)
562 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
570 fetchprocinfo(struct ktr_header *kth, u_int *flags)
572 struct proc_info *pi;
574 switch (kth->ktr_type) {
576 TAILQ_FOREACH(pi, &trace_procs, info) {
577 if (pi->pid == kth->ktr_pid) {
578 TAILQ_REMOVE(&trace_procs, pi, info);
582 pi = malloc(sizeof(struct proc_info));
584 errx(1, "%s", strerror(ENOMEM));
585 pi->sv_flags = *flags;
586 pi->pid = kth->ktr_pid;
587 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
591 TAILQ_FOREACH(pi, &trace_procs, info) {
592 if (pi->pid == kth->ktr_pid) {
593 TAILQ_REMOVE(&trace_procs, pi, info);
605 abidump(struct ktr_header *kth)
607 struct proc_info *pi;
612 TAILQ_FOREACH(pi, &trace_procs, info) {
613 if (pi->pid == kth->ktr_pid) {
614 flags = pi->sv_flags;
622 switch (flags & SV_ABI_MASK) {
629 case SV_ABI_CLOUDABI:
639 else if (flags & SV_ILP32)
644 printf("%s%s ", abi, arch);
650 dumpheader(struct ktr_header *kth)
652 static char unknown[64];
653 static struct timeval prevtime, prevtime_e;
658 switch (kth->ktr_type) {
700 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
705 * The ktr_tid field was previously the ktr_buffer field, which held
706 * the kernel pointer value for the buffer associated with data
707 * following the record header. It now holds a threadid, but only
708 * for trace files after the change. Older trace files still contain
709 * kernel pointers. Detect this and suppress the results by printing
710 * negative tid's as 0.
713 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
714 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
715 MAXCOMLEN, kth->ktr_comm);
717 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
720 if (timestamp & TIMESTAMP_ABSOLUTE) {
721 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
722 kth->ktr_time.tv_usec);
724 if (timestamp & TIMESTAMP_ELAPSED) {
725 if (prevtime_e.tv_sec == 0)
726 prevtime_e = kth->ktr_time;
727 timersub(&kth->ktr_time, &prevtime_e, &temp);
728 printf("%jd.%06ld ", (intmax_t)temp.tv_sec,
731 if (timestamp & TIMESTAMP_RELATIVE) {
732 if (prevtime.tv_sec == 0)
733 prevtime = kth->ktr_time;
734 if (timercmp(&kth->ktr_time, &prevtime, <)) {
735 timersub(&prevtime, &kth->ktr_time, &temp);
738 timersub(&kth->ktr_time, &prevtime, &temp);
741 prevtime = kth->ktr_time;
742 printf("%s%jd.%06ld ", sign, (intmax_t)temp.tv_sec,
749 #include <sys/syscall.h>
752 ioctlname(unsigned long val)
756 str = sysdecode_ioctlname(val);
765 static enum sysdecode_abi
766 syscallabi(u_int sv_flags)
770 return (SYSDECODE_ABI_FREEBSD);
771 switch (sv_flags & SV_ABI_MASK) {
773 return (SYSDECODE_ABI_FREEBSD);
774 #if defined(__amd64__) || defined(__i386__)
777 if (sv_flags & SV_ILP32)
778 return (SYSDECODE_ABI_LINUX32);
780 return (SYSDECODE_ABI_LINUX);
782 #if defined(__aarch64__) || defined(__amd64__)
783 case SV_ABI_CLOUDABI:
784 return (SYSDECODE_ABI_CLOUDABI64);
787 return (SYSDECODE_ABI_UNKNOWN);
792 syscallname(u_int code, u_int sv_flags)
796 name = sysdecode_syscallname(syscallabi(sv_flags), code);
798 printf("[%d]", code);
802 printf("[%d]", code);
807 print_signal(int signo)
811 signame = sysdecode_signal(signo);
813 printf("%s", signame);
815 printf("SIG %d", signo);
819 ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags)
821 int narg = ktr->ktr_narg;
822 register_t *ip, *first;
824 int quad_align, quad_slots;
826 syscallname(ktr->ktr_code, sv_flags);
827 ip = first = &ktr->ktr_args[0];
832 (sv_flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
834 if (sv_flags & SV_ILP32) {
841 switch (ktr->ktr_code) {
860 print_integer_arg_valid(sysdecode_atfd, *ip);
866 switch (ktr->ktr_code) {
868 print_number(ip, narg, c);
878 print_integer_arg(sysdecode_ptrace_request, *ip);
886 print_number(ip, narg, c);
888 print_mask_arg(sysdecode_access_mode, *ip);
894 print_number(ip, narg, c);
896 print_mask_arg(sysdecode_open_flags, ip[0]);
897 if ((ip[0] & O_CREAT) == O_CREAT) {
899 decode_filemode(ip[1]);
905 print_number(ip, narg, c);
906 print_number(ip, narg, c);
908 print_mask_arg0(sysdecode_wait4_options, *ip);
914 print_integer_arg(sysdecode_idtype, *ip);
918 print_number64(first, ip, narg, c);
919 print_number(ip, narg, c);
921 print_mask_arg(sysdecode_wait6_options, *ip);
929 print_number(ip, narg, c);
931 decode_filemode(*ip);
936 print_number(ip, narg, c);
938 decode_filemode(*ip);
943 print_number(ip, narg, c);
944 print_number(ip, narg, c);
946 print_integer_arg(sysdecode_getfsstat_mode, *ip);
951 print_number(ip, narg, c);
952 print_number(ip, narg, c);
954 print_mask_arg(sysdecode_mount_flags, *ip);
959 print_number(ip, narg, c);
961 print_mask_arg(sysdecode_mount_flags, *ip);
967 print_number(ip, narg, c);
968 print_number(ip, narg, c);
970 print_mask_arg0(sysdecode_msg_flags, *ip);
976 print_number(ip, narg, c);
977 print_number(ip, narg, c);
978 print_number(ip, narg, c);
980 print_mask_arg0(sysdecode_msg_flags, *ip);
988 print_number(ip, narg, c);
990 decode_fileflags(*ip);
995 print_number(ip, narg, c);
1003 print_mask_arg(sysdecode_reboot_howto, *ip);
1009 decode_filemode(*ip);
1014 print_number(ip, narg, c);
1015 print_number(ip, narg, c);
1017 print_mask_arg(sysdecode_msync_flags, *ip);
1021 #ifdef SYS_freebsd6_mmap
1022 case SYS_freebsd6_mmap:
1023 print_number(ip, narg, c);
1024 print_number(ip, narg, c);
1026 print_mask_arg(sysdecode_mmap_prot, *ip);
1030 print_mask_arg(sysdecode_mmap_flags, *ip);
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);
1048 print_number(ip, narg, c);
1049 print_number(ip, narg, c);
1051 print_mask_arg(sysdecode_mmap_prot, *ip);
1056 print_number(ip, narg, c);
1057 print_number(ip, narg, c);
1059 print_integer_arg(sysdecode_madvice, *ip);
1063 case SYS_setpriority:
1064 print_number(ip, narg, c);
1065 print_number(ip, narg, c);
1067 print_integer_arg(sysdecode_prio_which, *ip);
1072 print_number(ip, narg, c);
1074 print_integer_arg(sysdecode_fcntl_cmd, ip[0]);
1075 if (sysdecode_fcntl_arg_p(ip[0])) {
1077 if (ip[0] == F_SETFL)
1079 sysdecode_fcntl_fileflags,
1082 sysdecode_fcntl_arg(stdout,
1093 print_integer_arg(sysdecode_socketdomain,
1098 print_mask_arg(sysdecode_socket_type, *ip);
1101 if (sockdomain == PF_INET ||
1102 sockdomain == PF_INET6) {
1104 print_integer_arg(sysdecode_ipproto,
1112 case SYS_setsockopt:
1113 case SYS_getsockopt: {
1116 print_number(ip, narg, c);
1118 print_integer_arg_valid(sysdecode_sockopt_level,
1120 str = sysdecode_sockopt_name(ip[0], ip[1]);
1130 #ifdef SYS_freebsd6_lseek
1131 case SYS_freebsd6_lseek:
1132 print_number(ip, narg, c);
1133 /* Hidden 'pad' argument, not in lseek(2) */
1134 print_number(ip, narg, c);
1135 print_number64(first, ip, narg, c);
1137 print_integer_arg(sysdecode_whence, *ip);
1143 print_number(ip, narg, c);
1144 print_number64(first, ip, narg, c);
1146 print_integer_arg(sysdecode_whence, *ip);
1151 print_number(ip, narg, c);
1153 print_mask_arg(sysdecode_flock_operation, *ip);
1161 print_number(ip, narg, c);
1163 decode_filemode(*ip);
1168 print_number(ip, narg, c);
1170 print_integer_arg(sysdecode_shutdown_how, *ip);
1174 case SYS_socketpair:
1176 print_integer_arg(sysdecode_socketdomain, *ip);
1180 print_mask_arg(sysdecode_socket_type, *ip);
1188 print_integer_arg(sysdecode_rlimit, *ip);
1195 print_integer_arg(sysdecode_getrusage_who, *ip);
1201 print_number(ip, narg, c);
1203 if (!sysdecode_quotactl_cmd(stdout, *ip)) {
1205 printf("<invalid=%d>", (int)*ip);
1207 printf("<invalid=%#x>",
1216 print_integer_arg(sysdecode_nfssvc_flags, *ip);
1223 print_integer_arg(sysdecode_rtprio_function,
1230 print_number(ip, narg, c);
1231 print_number(ip, narg, c);
1233 print_integer_arg(sysdecode_semctl_cmd, *ip);
1238 print_number(ip, narg, c);
1239 print_number(ip, narg, c);
1241 print_mask_arg(sysdecode_semget_flags, *ip);
1246 print_number(ip, narg, c);
1248 print_integer_arg(sysdecode_msgctl_cmd, *ip);
1253 print_number(ip, narg, c);
1254 print_number(ip, narg, c);
1256 print_mask_arg(sysdecode_shmat_flags, *ip);
1261 print_number(ip, narg, c);
1263 print_integer_arg(sysdecode_shmctl_cmd, *ip);
1268 print_number(ip, narg, c);
1270 print_mask_arg(sysdecode_open_flags, ip[0]);
1272 decode_filemode(ip[1]);
1277 print_number(ip, narg, c);
1278 print_number(ip, narg, c);
1280 print_integer_arg(sysdecode_minherit_inherit,
1287 print_mask_arg(sysdecode_rfork_flags, *ip);
1292 case SYS_lio_listio:
1294 print_integer_arg(sysdecode_lio_listio_mode,
1302 print_mask_arg(sysdecode_mlockall_flags, *ip);
1306 case SYS_sched_setscheduler:
1307 print_number(ip, narg, c);
1309 print_integer_arg(sysdecode_scheduler_policy,
1314 case SYS_sched_get_priority_max:
1315 case SYS_sched_get_priority_min:
1317 print_integer_arg(sysdecode_scheduler_policy,
1323 print_number(ip, narg, c);
1324 print_number(ip, narg, c);
1325 print_number(ip, narg, c);
1326 print_number(ip, narg, c);
1327 print_number(ip, narg, c);
1328 print_number(ip, narg, c);
1330 print_mask_arg(sysdecode_sendfile_flags, *ip);
1335 print_number(ip, narg, c);
1337 print_integer_arg(sysdecode_kldsym_cmd, *ip);
1341 case SYS_sigprocmask:
1343 print_integer_arg(sysdecode_sigprocmask_how,
1349 case SYS___acl_get_file:
1350 case SYS___acl_set_file:
1351 case SYS___acl_get_fd:
1352 case SYS___acl_set_fd:
1353 case SYS___acl_delete_file:
1354 case SYS___acl_delete_fd:
1355 case SYS___acl_aclcheck_file:
1356 case SYS___acl_aclcheck_fd:
1357 case SYS___acl_get_link:
1358 case SYS___acl_set_link:
1359 case SYS___acl_delete_link:
1360 case SYS___acl_aclcheck_link:
1361 print_number(ip, narg, c);
1363 print_integer_arg(sysdecode_acltype, *ip);
1374 case SYS_extattrctl:
1375 print_number(ip, narg, c);
1377 print_integer_arg(sysdecode_extattrnamespace,
1383 print_number(ip, narg, c);
1384 print_number(ip, narg, c);
1386 print_mask_arg(sysdecode_mount_flags, *ip);
1390 case SYS_thr_create:
1391 print_number(ip, narg, c);
1392 print_number(ip, narg, c);
1394 print_mask_arg(sysdecode_thr_create_flags, *ip);
1399 print_number(ip, narg, c);
1405 case SYS_kldunloadf:
1406 print_number(ip, narg, c);
1408 print_integer_arg(sysdecode_kldunload_flags,
1416 print_number(ip, narg, c);
1418 print_integer_arg_valid(sysdecode_atfd, *ip);
1422 case SYS_cap_fcntls_limit:
1423 print_number(ip, narg, c);
1428 print_mask_arg32(sysdecode_cap_fcntlrights, arg);
1430 case SYS_posix_fadvise:
1431 print_number(ip, narg, c);
1432 print_number(ip, narg, c);
1433 print_number(ip, narg, c);
1435 print_integer_arg(sysdecode_fadvice, *ip);
1441 print_integer_arg(sysdecode_idtype, *ip);
1445 print_number64(first, ip, narg, c);
1447 print_integer_arg(sysdecode_procctl_cmd, *ip);
1452 print_number(ip, narg, c);
1454 print_integer_arg(sysdecode_umtx_op, *ip);
1456 case UMTX_OP_CV_WAIT:
1461 sysdecode_umtx_cvwait_flags, *ip);
1463 case UMTX_OP_RW_RDLOCK:
1468 sysdecode_umtx_rwlock_flags, *ip);
1476 print_number(ip, narg, c);
1477 print_number64(first, ip, narg, c);
1482 print_number(ip, narg, c);
1490 ktrsysret(struct ktr_sysret *ktr, u_int sv_flags)
1492 register_t ret = ktr->ktr_retval;
1493 int error = ktr->ktr_error;
1495 syscallname(ktr->ktr_code, sv_flags);
1500 printf("%ld", (long)ret);
1501 if (ret < 0 || ret > 9)
1502 printf("/%#lx", (unsigned long)ret);
1505 printf("%ld", (long)ret);
1507 printf("%#lx", (unsigned long)ret);
1509 } else if (error == ERESTART)
1511 else if (error == EJUSTRETURN)
1512 printf("JUSTRETURN");
1514 printf("-1 errno %d", sysdecode_freebsd_to_abi_errno(
1515 syscallabi(sv_flags), error));
1517 printf(" %s", strerror(ktr->ktr_error));
1523 ktrnamei(char *cp, int len)
1525 printf("\"%.*s\"\n", len, cp);
1529 hexdump(char *p, int len, int screenwidth)
1537 i = 13; /* base offset */
1538 i += (width / 2) + 1; /* spaces every second byte */
1539 i += (width * 2); /* width of bytes */
1541 i += width; /* each byte */
1543 } while (i < screenwidth);
1546 for (n = 0; n < len; n += width) {
1547 for (i = n; i < n + width; i++) {
1548 if ((i % width) == 0) { /* beginning of line */
1549 printf(" 0x%04x", i);
1555 printf("%02x", p[i] & 0xff);
1560 for (i = n; i < n + width; i++) {
1563 if (p[i] >= ' ' && p[i] <= '~')
1570 if ((i % width) != 0)
1575 visdump(char *dp, int datalen, int screenwidth)
1584 for (;datalen > 0; datalen--, dp++) {
1585 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1588 * Keep track of printables and
1589 * space chars (like fold(1)).
1601 width = 8 - (col&07);
1606 if (col + width > (screenwidth-2)) {
1621 ktrgenio(struct ktr_genio *ktr, int len)
1623 int datalen = len - sizeof (struct ktr_genio);
1624 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1625 static int screenwidth = 0;
1628 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1629 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1630 datalen == 1 ? "" : "s");
1633 if (screenwidth == 0) {
1636 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1638 screenwidth = ws.ws_col;
1642 if (maxdata && datalen > maxdata)
1645 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1646 if (dp[i] >= 32 && dp[i] < 127)
1648 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1653 hexdump(dp, datalen, screenwidth);
1655 visdump(dp, datalen, screenwidth);
1659 ktrpsig(struct ktr_psig *psig)
1663 print_signal(psig->signo);
1664 if (psig->action == SIG_DFL) {
1667 printf(" caught handler=0x%lx mask=0x%x",
1668 (u_long)psig->action, psig->mask.__bits[0]);
1671 str = sysdecode_sigcode(psig->signo, psig->code);
1675 printf("<invalid=%#x>", psig->code);
1680 ktrcsw_old(struct ktr_csw_old *cs)
1682 printf("%s %s\n", cs->out ? "stop" : "resume",
1683 cs->user ? "user" : "kernel");
1687 ktrcsw(struct ktr_csw *cs)
1689 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1690 cs->user ? "user" : "kernel", cs->wmesg);
1694 ktruser(int len, void *p)
1698 if (sysdecode_utrace(stdout, p, len)) {
1707 printf(" %d", *cp++);
1709 printf(" %02x", *cp++);
1714 ktrcaprights(cap_rights_t *rightsp)
1717 printf("cap_rights_t ");
1718 sysdecode_cap_rights(stdout, rightsp);
1723 ktrtimeval(struct timeval *tv)
1726 printf("{%ld, %ld}", (long)tv->tv_sec, tv->tv_usec);
1730 ktritimerval(struct itimerval *it)
1733 printf("itimerval { .interval = ");
1734 ktrtimeval(&it->it_interval);
1735 printf(", .value = ");
1736 ktrtimeval(&it->it_value);
1741 ktrsockaddr(struct sockaddr *sa)
1744 TODO: Support additional address families
1745 #include <netsmb/netbios.h>
1746 struct sockaddr_nb *nb;
1752 * note: ktrstruct() has already verified that sa points to a
1753 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1754 * sa->sa_len bytes long.
1756 printf("struct sockaddr { ");
1757 str = sysdecode_sockaddr_family(sa->sa_family);
1761 printf("<invalid=%d>", sa->sa_family);
1764 #define check_sockaddr_len(n) \
1765 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1766 printf("invalid"); \
1770 switch(sa->sa_family) {
1772 struct sockaddr_in sa_in;
1774 memset(&sa_in, 0, sizeof(sa_in));
1775 memcpy(&sa_in, sa, sa->sa_len);
1776 check_sockaddr_len(in);
1777 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1778 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1782 struct sockaddr_in6 sa_in6;
1784 memset(&sa_in6, 0, sizeof(sa_in6));
1785 memcpy(&sa_in6, sa, sa->sa_len);
1786 check_sockaddr_len(in6);
1787 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1788 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1789 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1793 struct sockaddr_un sa_un;
1795 memset(&sa_un, 0, sizeof(sa_un));
1796 memcpy(&sa_un, sa, sa->sa_len);
1797 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1801 printf("unknown address family");
1807 ktrstat(struct stat *statp)
1809 char mode[12], timestr[PATH_MAX + 4];
1815 * note: ktrstruct() has already verified that statp points to a
1816 * buffer exactly sizeof(struct stat) bytes long.
1818 printf("struct stat {");
1819 printf("dev=%ju, ino=%ju, ",
1820 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1822 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1824 strmode(statp->st_mode, mode);
1825 printf("mode=%s, ", mode);
1827 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1831 #ifdef HAVE_LIBCASPER
1833 pwd = cap_getpwuid(cappwd, statp->st_uid);
1836 pwd = getpwuid(statp->st_uid);
1839 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1841 printf("uid=\"%s\", ", pwd->pw_name);
1845 #ifdef HAVE_LIBCASPER
1847 grp = cap_getgrgid(capgrp, statp->st_gid);
1850 grp = getgrgid(statp->st_gid);
1853 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1855 printf("gid=\"%s\", ", grp->gr_name);
1856 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1859 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1861 tm = localtime(&statp->st_atim.tv_sec);
1862 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1863 printf("\"%s\"", timestr);
1865 if (statp->st_atim.tv_nsec != 0)
1866 printf(".%09ld, ", statp->st_atim.tv_nsec);
1871 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1873 tm = localtime(&statp->st_mtim.tv_sec);
1874 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1875 printf("\"%s\"", timestr);
1877 if (statp->st_mtim.tv_nsec != 0)
1878 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1883 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1885 tm = localtime(&statp->st_ctim.tv_sec);
1886 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1887 printf("\"%s\"", timestr);
1889 if (statp->st_ctim.tv_nsec != 0)
1890 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1893 printf("birthtime=");
1895 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1897 tm = localtime(&statp->st_birthtim.tv_sec);
1898 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1899 printf("\"%s\"", timestr);
1901 if (statp->st_birthtim.tv_nsec != 0)
1902 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
1905 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
1906 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
1907 (intmax_t)statp->st_blocks, statp->st_flags);
1912 ktrstruct(char *buf, size_t buflen)
1915 size_t namelen, datalen;
1917 cap_rights_t rights;
1918 struct itimerval it;
1920 struct sockaddr_storage ss;
1922 for (name = buf, namelen = 0;
1923 namelen < buflen && name[namelen] != '\0';
1926 if (namelen == buflen)
1928 if (name[namelen] != '\0')
1930 data = buf + namelen + 1;
1931 datalen = buflen - namelen - 1;
1935 for (i = 0; i < (int)namelen; ++i)
1936 if (!isalpha(name[i]))
1938 if (strcmp(name, "caprights") == 0) {
1939 if (datalen != sizeof(cap_rights_t))
1941 memcpy(&rights, data, datalen);
1942 ktrcaprights(&rights);
1943 } else if (strcmp(name, "itimerval") == 0) {
1944 if (datalen != sizeof(struct itimerval))
1946 memcpy(&it, data, datalen);
1948 } else if (strcmp(name, "stat") == 0) {
1949 if (datalen != sizeof(struct stat))
1951 memcpy(&sb, data, datalen);
1953 } else if (strcmp(name, "sockaddr") == 0) {
1954 if (datalen > sizeof(ss))
1956 memcpy(&ss, data, datalen);
1957 if (datalen != ss.ss_len)
1959 ktrsockaddr((struct sockaddr *)&ss);
1961 printf("unknown structure\n");
1965 printf("invalid record\n");
1969 ktrcapfail(struct ktr_cap_fail *ktr)
1971 switch (ktr->cap_type) {
1972 case CAPFAIL_NOTCAPABLE:
1973 /* operation on fd with insufficient capabilities */
1974 printf("operation requires ");
1975 sysdecode_cap_rights(stdout, &ktr->cap_needed);
1976 printf(", descriptor holds ");
1977 sysdecode_cap_rights(stdout, &ktr->cap_held);
1979 case CAPFAIL_INCREASE:
1980 /* requested more capabilities than fd already has */
1981 printf("attempt to increase capabilities from ");
1982 sysdecode_cap_rights(stdout, &ktr->cap_held);
1984 sysdecode_cap_rights(stdout, &ktr->cap_needed);
1986 case CAPFAIL_SYSCALL:
1987 /* called restricted syscall */
1988 printf("disallowed system call");
1990 case CAPFAIL_LOOKUP:
1991 /* used ".." in strict-relative mode */
1992 printf("restricted VFS lookup");
1995 printf("unknown capability failure: ");
1996 sysdecode_cap_rights(stdout, &ktr->cap_needed);
1998 sysdecode_cap_rights(stdout, &ktr->cap_held);
2005 ktrfault(struct ktr_fault *ktr)
2008 printf("0x%jx ", (uintmax_t)ktr->vaddr);
2009 print_mask_arg(sysdecode_vmprot, ktr->type);
2014 ktrfaultend(struct ktr_faultend *ktr)
2018 str = sysdecode_vmresult(ktr->result);
2022 printf("<invalid=%d>", ktr->result);
2029 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
2030 "[-m maxdata] [-p pid] [-t trstr]\n");