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 * 4. 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
46 #define _WANT_KEVENT32
48 #include <sys/param.h>
49 #include <sys/capsicum.h>
50 #include <sys/errno.h>
53 #include <sys/event.h>
54 #include <sys/ktrace.h>
55 #include <sys/ioctl.h>
56 #include <sys/socket.h>
58 #include <sys/sysent.h>
61 #include <sys/queue.h>
66 #include <arpa/inet.h>
67 #include <netinet/in.h>
80 #include <sysdecode.h>
88 #include <libcasper.h>
90 #include <casper/cap_grp.h>
91 #include <casper/cap_pwd.h>
94 u_int abidump(struct ktr_header *);
95 int fetchprocinfo(struct ktr_header *, u_int *);
96 int fread_tail(void *, int, int);
97 void dumpheader(struct ktr_header *);
98 void ktrsyscall(struct ktr_syscall *, u_int);
99 void ktrsysret(struct ktr_sysret *, u_int);
100 void ktrnamei(char *, int);
101 void hexdump(char *, int, int);
102 void visdump(char *, int, int);
103 void ktrgenio(struct ktr_genio *, int);
104 void ktrpsig(struct ktr_psig *);
105 void ktrcsw(struct ktr_csw *);
106 void ktrcsw_old(struct ktr_csw_old *);
107 void ktruser(int, void *);
108 void ktrcaprights(cap_rights_t *);
109 void ktritimerval(struct itimerval *it);
110 void ktrsockaddr(struct sockaddr *);
111 void ktrstat(struct stat *);
112 void ktrstruct(char *, size_t);
113 void ktrcapfail(struct ktr_cap_fail *);
114 void ktrfault(struct ktr_fault *);
115 void ktrfaultend(struct ktr_faultend *);
116 void ktrkevent(struct kevent *);
117 void ktrstructarray(struct ktr_struct_array *, size_t);
118 void limitfd(int fd);
121 #define TIMESTAMP_NONE 0x0
122 #define TIMESTAMP_ABSOLUTE 0x1
123 #define TIMESTAMP_ELAPSED 0x2
124 #define TIMESTAMP_RELATIVE 0x4
126 static int timestamp, decimal, fancy = 1, suppressdata, tail, threads, maxdata,
127 resolv = 0, abiflag = 0, syscallno = 0;
128 static const char *tracefile = DEF_TRACEFILE;
129 static struct ktr_header ktr_header;
131 #define TIME_FORMAT "%b %e %T %Y"
132 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0)
134 #define print_number64(first,i,n,c) do { \
137 if (quad_align && (((ptrdiff_t)((i) - (first))) & 1) == 1) { \
141 if (quad_slots == 2) \
142 __v = (uint64_t)(uint32_t)(i)[0] | \
143 ((uint64_t)(uint32_t)(i)[1]) << 32; \
145 __v = (uint64_t)*(i); \
147 printf("%c%jd", (c), (intmax_t)__v); \
149 printf("%c%#jx", (c), (uintmax_t)__v); \
155 #define print_number(i,n,c) do { \
157 printf("%c%jd", c, (intmax_t)*i); \
159 printf("%c%#jx", c, (uintmax_t)(u_register_t)*i); \
167 TAILQ_ENTRY(proc_info) info;
172 static TAILQ_HEAD(trace_procs, proc_info) trace_procs;
174 #ifdef HAVE_LIBCASPER
175 static cap_channel_t *cappwd, *capgrp;
183 * Cache NLS data before entering capability mode.
184 * XXXPJD: There should be strerror_init() and strsignal_init() in libc.
186 (void)catopen("libc", NL_CAT_LOCALE);
195 * Allow localtime(3) to cache /etc/localtime content before entering
197 * XXXPJD: There should be localtime_init() in libc.
200 (void)localtime(<ime);
203 #ifdef HAVE_LIBCASPER
205 cappwdgrp_setup(cap_channel_t **cappwdp, cap_channel_t **capgrpp)
207 cap_channel_t *capcas, *cappwdloc, *capgrploc;
208 const char *cmds[1], *fields[1];
211 if (capcas == NULL) {
212 err(1, "unable to create casper process");
215 cappwdloc = cap_service_open(capcas, "system.pwd");
216 capgrploc = cap_service_open(capcas, "system.grp");
217 /* Casper capability no longer needed. */
219 if (cappwdloc == NULL || capgrploc == NULL) {
220 if (cappwdloc == NULL)
221 warn("unable to open system.pwd service");
222 if (capgrploc == NULL)
223 warn("unable to open system.grp service");
226 /* Limit system.pwd to only getpwuid() function and pw_name field. */
227 cmds[0] = "getpwuid";
228 if (cap_pwd_limit_cmds(cappwdloc, cmds, 1) < 0)
229 err(1, "unable to limit system.pwd service");
230 fields[0] = "pw_name";
231 if (cap_pwd_limit_fields(cappwdloc, fields, 1) < 0)
232 err(1, "unable to limit system.pwd service");
233 /* Limit system.grp to only getgrgid() function and gr_name field. */
234 cmds[0] = "getgrgid";
235 if (cap_grp_limit_cmds(capgrploc, cmds, 1) < 0)
236 err(1, "unable to limit system.grp service");
237 fields[0] = "gr_name";
238 if (cap_grp_limit_fields(capgrploc, fields, 1) < 0)
239 err(1, "unable to limit system.grp service");
241 *cappwdp = cappwdloc;
242 *capgrpp = capgrploc;
245 #endif /* HAVE_LIBCASPER */
248 print_integer_arg(const char *(*decoder)(int), int value)
252 str = decoder(value);
257 printf("<invalid=%d>", value);
259 printf("<invalid=%#x>", value);
263 /* Like print_integer_arg but unknown values are treated as valid. */
265 print_integer_arg_valid(const char *(*decoder)(int), int value)
269 str = decoder(value);
276 printf("%#x", value);
281 print_mask_arg(bool (*decoder)(FILE *, int, int *), int value)
286 printf("%#x<", value);
287 invalid = !decoder(stdout, value, &rem);
290 printf("<invalid>%u", rem);
294 print_mask_arg0(bool (*decoder)(FILE *, int, int *), int value)
303 printf("%#x<", value);
304 invalid = !decoder(stdout, value, &rem);
307 printf("<invalid>%u", rem);
311 decode_fileflags(fflags_t value)
320 printf("%#x<", value);
321 invalid = !sysdecode_fileflags(stdout, value, &rem);
324 printf("<invalid>%u", rem);
328 decode_filemode(int value)
337 printf("%#o<", value);
338 invalid = !sysdecode_filemode(stdout, value, &rem);
341 printf("<invalid>%u", rem);
345 print_mask_arg32(bool (*decoder)(FILE *, uint32_t, uint32_t *), uint32_t value)
350 printf("%#x<", value);
351 invalid = !decoder(stdout, value, &rem);
354 printf("<invalid>%u", rem);
358 print_mask_argul(bool (*decoder)(FILE *, u_long, u_long *), u_long value)
367 printf("%#lx<", value);
368 invalid = !decoder(stdout, value, &rem);
371 printf("<invalid>%lu", rem);
375 main(int argc, char *argv[])
377 int ch, ktrlen, size;
379 int trpoints = ALL_POINTS;
384 setlocale(LC_CTYPE, "");
386 timestamp = TIMESTAMP_NONE;
388 while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1)
403 maxdata = atoi(optarg);
421 timestamp |= TIMESTAMP_ELAPSED;
427 timestamp |= TIMESTAMP_RELATIVE;
430 timestamp |= TIMESTAMP_ABSOLUTE;
433 trpoints = getpoints(optarg);
435 errx(1, "unknown trace point in %s", optarg);
444 m = malloc(size = 1025);
446 errx(1, "%s", strerror(ENOMEM));
447 if (strcmp(tracefile, "-") != 0)
448 if (!freopen(tracefile, "r", stdin))
449 err(1, "%s", tracefile);
453 #ifdef HAVE_LIBCASPER
455 if (cappwdgrp_setup(&cappwd, &capgrp) < 0) {
460 if (resolv == 0 || (cappwd != NULL && capgrp != NULL)) {
461 if (cap_enter() < 0 && errno != ENOSYS)
462 err(1, "unable to enter capability mode");
466 if (cap_enter() < 0 && errno != ENOSYS)
467 err(1, "unable to enter capability mode");
470 limitfd(STDIN_FILENO);
471 limitfd(STDOUT_FILENO);
472 limitfd(STDERR_FILENO);
474 TAILQ_INIT(&trace_procs);
476 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) {
477 if (ktr_header.ktr_type & KTR_DROP) {
478 ktr_header.ktr_type &= ~KTR_DROP;
479 if (!drop_logged && threads) {
481 "%6jd %6jd %-8.*s Events dropped.\n",
482 (intmax_t)ktr_header.ktr_pid,
483 ktr_header.ktr_tid > 0 ?
484 (intmax_t)ktr_header.ktr_tid : 0,
485 MAXCOMLEN, ktr_header.ktr_comm);
487 } else if (!drop_logged) {
488 printf("%6jd %-8.*s Events dropped.\n",
489 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN,
490 ktr_header.ktr_comm);
494 if (trpoints & (1<<ktr_header.ktr_type))
495 if (pid == 0 || ktr_header.ktr_pid == pid ||
496 ktr_header.ktr_tid == pid)
497 dumpheader(&ktr_header);
498 if ((ktrlen = ktr_header.ktr_len) < 0)
499 errx(1, "bogus length 0x%x", ktrlen);
501 m = realloc(m, ktrlen+1);
503 errx(1, "%s", strerror(ENOMEM));
506 if (ktrlen && fread_tail(m, ktrlen, 1) == 0)
507 errx(1, "data too short");
508 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0)
510 sv_flags = abidump(&ktr_header);
511 if (pid && ktr_header.ktr_pid != pid &&
512 ktr_header.ktr_tid != pid)
514 if ((trpoints & (1<<ktr_header.ktr_type)) == 0)
517 switch (ktr_header.ktr_type) {
519 ktrsyscall((struct ktr_syscall *)m, sv_flags);
522 ktrsysret((struct ktr_sysret *)m, sv_flags);
529 ktrgenio((struct ktr_genio *)m, ktrlen);
532 ktrpsig((struct ktr_psig *)m);
535 if (ktrlen == sizeof(struct ktr_csw_old))
536 ktrcsw_old((struct ktr_csw_old *)m);
538 ktrcsw((struct ktr_csw *)m);
544 ktrstruct(m, ktrlen);
547 ktrcapfail((struct ktr_cap_fail *)m);
550 ktrfault((struct ktr_fault *)m);
553 ktrfaultend((struct ktr_faultend *)m);
555 case KTR_STRUCT_ARRAY:
556 ktrstructarray((struct ktr_struct_array *)m, ktrlen);
574 cap_rights_init(&rights, CAP_FSTAT);
579 cap_rights_set(&rights, CAP_READ);
582 cap_rights_set(&rights, CAP_IOCTL, CAP_WRITE);
583 cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
586 cap_rights_set(&rights, CAP_WRITE);
588 cap_rights_set(&rights, CAP_IOCTL);
596 if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
597 err(1, "unable to limit rights for descriptor %d", fd);
598 if (cmd != 0 && cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
599 err(1, "unable to limit ioctls for descriptor %d", fd);
603 fread_tail(void *buf, int size, int num)
607 while ((i = fread(buf, size, num, stdin)) == 0 && tail) {
615 fetchprocinfo(struct ktr_header *kth, u_int *flags)
617 struct proc_info *pi;
619 switch (kth->ktr_type) {
621 TAILQ_FOREACH(pi, &trace_procs, info) {
622 if (pi->pid == kth->ktr_pid) {
623 TAILQ_REMOVE(&trace_procs, pi, info);
627 pi = malloc(sizeof(struct proc_info));
629 errx(1, "%s", strerror(ENOMEM));
630 pi->sv_flags = *flags;
631 pi->pid = kth->ktr_pid;
632 TAILQ_INSERT_TAIL(&trace_procs, pi, info);
636 TAILQ_FOREACH(pi, &trace_procs, info) {
637 if (pi->pid == kth->ktr_pid) {
638 TAILQ_REMOVE(&trace_procs, pi, info);
650 abidump(struct ktr_header *kth)
652 struct proc_info *pi;
657 TAILQ_FOREACH(pi, &trace_procs, info) {
658 if (pi->pid == kth->ktr_pid) {
659 flags = pi->sv_flags;
667 switch (flags & SV_ABI_MASK) {
674 case SV_ABI_CLOUDABI:
684 else if (flags & SV_ILP32)
689 printf("%s%s ", abi, arch);
695 dumpheader(struct ktr_header *kth)
697 static char unknown[64];
698 static struct timeval prevtime, prevtime_e;
703 switch (kth->ktr_type) {
726 case KTR_STRUCT_ARRAY:
746 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type);
751 * The ktr_tid field was previously the ktr_buffer field, which held
752 * the kernel pointer value for the buffer associated with data
753 * following the record header. It now holds a threadid, but only
754 * for trace files after the change. Older trace files still contain
755 * kernel pointers. Detect this and suppress the results by printing
756 * negative tid's as 0.
759 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid,
760 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0,
761 MAXCOMLEN, kth->ktr_comm);
763 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN,
766 if (timestamp & TIMESTAMP_ABSOLUTE) {
767 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec,
768 kth->ktr_time.tv_usec);
770 if (timestamp & TIMESTAMP_ELAPSED) {
771 if (prevtime_e.tv_sec == 0)
772 prevtime_e = kth->ktr_time;
773 timersub(&kth->ktr_time, &prevtime_e, &temp);
774 printf("%jd.%06ld ", (intmax_t)temp.tv_sec,
777 if (timestamp & TIMESTAMP_RELATIVE) {
778 if (prevtime.tv_sec == 0)
779 prevtime = kth->ktr_time;
780 if (timercmp(&kth->ktr_time, &prevtime, <)) {
781 timersub(&prevtime, &kth->ktr_time, &temp);
784 timersub(&kth->ktr_time, &prevtime, &temp);
787 prevtime = kth->ktr_time;
788 printf("%s%jd.%06ld ", sign, (intmax_t)temp.tv_sec,
795 #include <sys/syscall.h>
798 ioctlname(unsigned long val)
802 str = sysdecode_ioctlname(val);
811 static enum sysdecode_abi
812 syscallabi(u_int sv_flags)
816 return (SYSDECODE_ABI_FREEBSD);
817 switch (sv_flags & SV_ABI_MASK) {
819 return (SYSDECODE_ABI_FREEBSD);
820 #if defined(__amd64__) || defined(__i386__)
823 if (sv_flags & SV_ILP32)
824 return (SYSDECODE_ABI_LINUX32);
826 return (SYSDECODE_ABI_LINUX);
828 #if defined(__aarch64__) || defined(__amd64__)
829 case SV_ABI_CLOUDABI:
830 return (SYSDECODE_ABI_CLOUDABI64);
833 return (SYSDECODE_ABI_UNKNOWN);
838 syscallname(u_int code, u_int sv_flags)
842 name = sysdecode_syscallname(syscallabi(sv_flags), code);
844 printf("[%d]", code);
848 printf("[%d]", code);
853 print_signal(int signo)
857 signame = sysdecode_signal(signo);
859 printf("%s", signame);
861 printf("SIG %d", signo);
865 ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags)
867 int narg = ktr->ktr_narg;
868 register_t *ip, *first;
870 int quad_align, quad_slots;
872 syscallname(ktr->ktr_code, sv_flags);
873 ip = first = &ktr->ktr_args[0];
878 (sv_flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) {
880 if (sv_flags & SV_ILP32) {
887 switch (ktr->ktr_code) {
906 print_integer_arg_valid(sysdecode_atfd, *ip);
912 switch (ktr->ktr_code) {
914 print_number(ip, narg, c);
924 print_integer_arg(sysdecode_ptrace_request, *ip);
932 print_number(ip, narg, c);
934 print_mask_arg(sysdecode_access_mode, *ip);
940 print_number(ip, narg, c);
942 print_mask_arg(sysdecode_open_flags, ip[0]);
943 if ((ip[0] & O_CREAT) == O_CREAT) {
945 decode_filemode(ip[1]);
951 print_number(ip, narg, c);
952 print_number(ip, narg, c);
954 print_mask_arg0(sysdecode_wait4_options, *ip);
960 print_integer_arg(sysdecode_idtype, *ip);
964 print_number64(first, ip, narg, c);
965 print_number(ip, narg, c);
967 print_mask_arg(sysdecode_wait6_options, *ip);
975 print_number(ip, narg, c);
977 decode_filemode(*ip);
983 print_number(ip, narg, c);
985 decode_filemode(*ip);
990 print_number(ip, narg, c);
991 print_number(ip, narg, c);
993 print_integer_arg(sysdecode_getfsstat_mode, *ip);
998 print_number(ip, narg, c);
999 print_number(ip, narg, c);
1001 print_mask_arg(sysdecode_mount_flags, *ip);
1006 print_number(ip, narg, c);
1008 print_mask_arg(sysdecode_mount_flags, *ip);
1014 print_number(ip, narg, c);
1015 print_number(ip, narg, c);
1017 print_mask_arg0(sysdecode_msg_flags, *ip);
1023 print_number(ip, narg, c);
1024 print_number(ip, narg, c);
1025 print_number(ip, narg, c);
1027 print_mask_arg0(sysdecode_msg_flags, *ip);
1035 print_number(ip, narg, c);
1037 decode_fileflags(*ip);
1042 print_number(ip, narg, c);
1050 print_mask_arg(sysdecode_reboot_howto, *ip);
1056 decode_filemode(*ip);
1061 print_number(ip, narg, c);
1062 print_number(ip, narg, c);
1064 print_mask_arg(sysdecode_msync_flags, *ip);
1068 #ifdef SYS_freebsd6_mmap
1069 case SYS_freebsd6_mmap:
1070 print_number(ip, narg, c);
1071 print_number(ip, narg, c);
1073 print_mask_arg(sysdecode_mmap_prot, *ip);
1077 print_mask_arg(sysdecode_mmap_flags, *ip);
1083 print_number(ip, narg, c);
1084 print_number(ip, narg, c);
1086 print_mask_arg(sysdecode_mmap_prot, *ip);
1090 print_mask_arg(sysdecode_mmap_flags, *ip);
1095 print_number(ip, narg, c);
1096 print_number(ip, narg, c);
1098 print_mask_arg(sysdecode_mmap_prot, *ip);
1103 print_number(ip, narg, c);
1104 print_number(ip, narg, c);
1106 print_integer_arg(sysdecode_madvice, *ip);
1113 print_number(ip, narg, c);
1115 print_integer_arg(sysdecode_pathconf_name, *ip);
1119 case SYS_getpriority:
1120 case SYS_setpriority:
1122 print_integer_arg(sysdecode_prio_which, *ip);
1128 print_number(ip, narg, c);
1130 print_integer_arg(sysdecode_fcntl_cmd, ip[0]);
1131 if (sysdecode_fcntl_arg_p(ip[0])) {
1133 if (ip[0] == F_SETFL)
1135 sysdecode_fcntl_fileflags,
1138 sysdecode_fcntl_arg(stdout,
1149 print_integer_arg(sysdecode_socketdomain,
1154 print_mask_arg(sysdecode_socket_type, *ip);
1157 if (sockdomain == PF_INET ||
1158 sockdomain == PF_INET6) {
1160 print_integer_arg(sysdecode_ipproto,
1168 case SYS_setsockopt:
1169 case SYS_getsockopt: {
1172 print_number(ip, narg, c);
1174 print_integer_arg_valid(sysdecode_sockopt_level,
1176 str = sysdecode_sockopt_name(ip[0], ip[1]);
1186 #ifdef SYS_freebsd6_lseek
1187 case SYS_freebsd6_lseek:
1188 print_number(ip, narg, c);
1189 /* Hidden 'pad' argument, not in lseek(2) */
1190 print_number(ip, narg, c);
1191 print_number64(first, ip, narg, c);
1193 print_integer_arg(sysdecode_whence, *ip);
1199 print_number(ip, narg, c);
1200 print_number64(first, ip, narg, c);
1202 print_integer_arg(sysdecode_whence, *ip);
1207 print_number(ip, narg, c);
1209 print_mask_arg(sysdecode_flock_operation, *ip);
1217 print_number(ip, narg, c);
1219 decode_filemode(*ip);
1224 print_number(ip, narg, c);
1226 print_integer_arg(sysdecode_shutdown_how, *ip);
1230 case SYS_socketpair:
1232 print_integer_arg(sysdecode_socketdomain, *ip);
1236 print_mask_arg(sysdecode_socket_type, *ip);
1244 print_integer_arg(sysdecode_rlimit, *ip);
1251 print_integer_arg(sysdecode_getrusage_who, *ip);
1257 print_number(ip, narg, c);
1259 if (!sysdecode_quotactl_cmd(stdout, *ip)) {
1261 printf("<invalid=%d>", (int)*ip);
1263 printf("<invalid=%#x>",
1272 print_integer_arg(sysdecode_nfssvc_flags, *ip);
1278 case SYS_rtprio_thread:
1280 print_integer_arg(sysdecode_rtprio_function,
1287 print_number(ip, narg, c);
1288 print_number(ip, narg, c);
1290 print_integer_arg(sysdecode_semctl_cmd, *ip);
1295 print_number(ip, narg, c);
1296 print_number(ip, narg, c);
1298 print_mask_arg(sysdecode_semget_flags, *ip);
1303 print_number(ip, narg, c);
1305 print_integer_arg(sysdecode_msgctl_cmd, *ip);
1310 print_number(ip, narg, c);
1311 print_number(ip, narg, c);
1313 print_mask_arg(sysdecode_shmat_flags, *ip);
1318 print_number(ip, narg, c);
1320 print_integer_arg(sysdecode_shmctl_cmd, *ip);
1325 print_number(ip, narg, c);
1327 print_mask_arg(sysdecode_open_flags, ip[0]);
1329 decode_filemode(ip[1]);
1334 print_number(ip, narg, c);
1335 print_number(ip, narg, c);
1337 print_integer_arg(sysdecode_minherit_inherit,
1344 print_mask_arg(sysdecode_rfork_flags, *ip);
1349 case SYS_lio_listio:
1351 print_integer_arg(sysdecode_lio_listio_mode,
1359 print_mask_arg(sysdecode_mlockall_flags, *ip);
1363 case SYS_sched_setscheduler:
1364 print_number(ip, narg, c);
1366 print_integer_arg(sysdecode_scheduler_policy,
1371 case SYS_sched_get_priority_max:
1372 case SYS_sched_get_priority_min:
1374 print_integer_arg(sysdecode_scheduler_policy,
1380 print_number(ip, narg, c);
1381 print_number(ip, narg, c);
1382 print_number(ip, narg, c);
1383 print_number(ip, narg, c);
1384 print_number(ip, narg, c);
1385 print_number(ip, narg, c);
1387 print_mask_arg(sysdecode_sendfile_flags, *ip);
1392 print_number(ip, narg, c);
1394 print_integer_arg(sysdecode_kldsym_cmd, *ip);
1398 case SYS_sigprocmask:
1400 print_integer_arg(sysdecode_sigprocmask_how,
1406 case SYS___acl_get_file:
1407 case SYS___acl_set_file:
1408 case SYS___acl_get_fd:
1409 case SYS___acl_set_fd:
1410 case SYS___acl_delete_file:
1411 case SYS___acl_delete_fd:
1412 case SYS___acl_aclcheck_file:
1413 case SYS___acl_aclcheck_fd:
1414 case SYS___acl_get_link:
1415 case SYS___acl_set_link:
1416 case SYS___acl_delete_link:
1417 case SYS___acl_aclcheck_link:
1418 print_number(ip, narg, c);
1420 print_integer_arg(sysdecode_acltype, *ip);
1431 case SYS_extattrctl:
1432 print_number(ip, narg, c);
1434 print_integer_arg(sysdecode_extattrnamespace,
1440 print_number(ip, narg, c);
1441 print_number(ip, narg, c);
1443 print_mask_arg(sysdecode_mount_flags, *ip);
1447 case SYS_thr_create:
1448 print_number(ip, narg, c);
1449 print_number(ip, narg, c);
1451 print_mask_arg(sysdecode_thr_create_flags, *ip);
1456 print_number(ip, narg, c);
1462 case SYS_kldunloadf:
1463 print_number(ip, narg, c);
1465 print_integer_arg(sysdecode_kldunload_flags,
1473 print_number(ip, narg, c);
1475 print_integer_arg_valid(sysdecode_atfd, *ip);
1478 print_number(ip, narg, c);
1480 case SYS_cap_fcntls_limit:
1481 print_number(ip, narg, c);
1486 print_mask_arg32(sysdecode_cap_fcntlrights, arg);
1488 case SYS_posix_fadvise:
1489 print_number(ip, narg, c);
1490 print_number(ip, narg, c);
1491 print_number(ip, narg, c);
1493 print_integer_arg(sysdecode_fadvice, *ip);
1499 print_integer_arg(sysdecode_idtype, *ip);
1503 print_number64(first, ip, narg, c);
1505 print_integer_arg(sysdecode_procctl_cmd, *ip);
1510 print_number(ip, narg, c);
1512 print_integer_arg(sysdecode_umtx_op, *ip);
1514 case UMTX_OP_CV_WAIT:
1519 sysdecode_umtx_cvwait_flags, *ip);
1521 case UMTX_OP_RW_RDLOCK:
1526 sysdecode_umtx_rwlock_flags, *ip);
1534 print_number(ip, narg, c);
1535 print_number64(first, ip, narg, c);
1538 print_number(ip, narg, c);
1539 print_number(ip, narg, c);
1540 print_number(ip, narg, c);
1544 print_number(ip, narg, c);
1545 print_number(ip, narg, c);
1548 print_number(ip, narg, c);
1552 print_integer_arg(sysdecode_sysarch_number, *ip);
1558 switch (ktr->ktr_code) {
1568 print_mask_arg0(sysdecode_atflags, *ip);
1575 print_number(ip, narg, c);
1583 ktrsysret(struct ktr_sysret *ktr, u_int sv_flags)
1585 register_t ret = ktr->ktr_retval;
1586 int error = ktr->ktr_error;
1588 syscallname(ktr->ktr_code, sv_flags);
1593 printf("%ld", (long)ret);
1594 if (ret < 0 || ret > 9)
1595 printf("/%#lx", (unsigned long)ret);
1598 printf("%ld", (long)ret);
1600 printf("%#lx", (unsigned long)ret);
1602 } else if (error == ERESTART)
1604 else if (error == EJUSTRETURN)
1605 printf("JUSTRETURN");
1607 printf("-1 errno %d", sysdecode_freebsd_to_abi_errno(
1608 syscallabi(sv_flags), error));
1610 printf(" %s", strerror(ktr->ktr_error));
1616 ktrnamei(char *cp, int len)
1618 printf("\"%.*s\"\n", len, cp);
1622 hexdump(char *p, int len, int screenwidth)
1630 i = 13; /* base offset */
1631 i += (width / 2) + 1; /* spaces every second byte */
1632 i += (width * 2); /* width of bytes */
1634 i += width; /* each byte */
1636 } while (i < screenwidth);
1639 for (n = 0; n < len; n += width) {
1640 for (i = n; i < n + width; i++) {
1641 if ((i % width) == 0) { /* beginning of line */
1642 printf(" 0x%04x", i);
1648 printf("%02x", p[i] & 0xff);
1653 for (i = n; i < n + width; i++) {
1656 if (p[i] >= ' ' && p[i] <= '~')
1663 if ((i % width) != 0)
1668 visdump(char *dp, int datalen, int screenwidth)
1677 for (;datalen > 0; datalen--, dp++) {
1678 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1));
1681 * Keep track of printables and
1682 * space chars (like fold(1)).
1694 width = 8 - (col&07);
1699 if (col + width > (screenwidth-2)) {
1714 ktrgenio(struct ktr_genio *ktr, int len)
1716 int datalen = len - sizeof (struct ktr_genio);
1717 char *dp = (char *)ktr + sizeof (struct ktr_genio);
1718 static int screenwidth = 0;
1721 printf("fd %d %s %d byte%s\n", ktr->ktr_fd,
1722 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen,
1723 datalen == 1 ? "" : "s");
1726 if (screenwidth == 0) {
1729 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 &&
1731 screenwidth = ws.ws_col;
1735 if (maxdata && datalen > maxdata)
1738 for (i = 0, binary = 0; i < datalen && binary == 0; i++) {
1739 if (dp[i] >= 32 && dp[i] < 127)
1741 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9)
1746 hexdump(dp, datalen, screenwidth);
1748 visdump(dp, datalen, screenwidth);
1752 ktrpsig(struct ktr_psig *psig)
1756 print_signal(psig->signo);
1757 if (psig->action == SIG_DFL) {
1760 printf(" caught handler=0x%lx mask=0x%x",
1761 (u_long)psig->action, psig->mask.__bits[0]);
1764 str = sysdecode_sigcode(psig->signo, psig->code);
1768 printf("<invalid=%#x>", psig->code);
1773 ktrcsw_old(struct ktr_csw_old *cs)
1775 printf("%s %s\n", cs->out ? "stop" : "resume",
1776 cs->user ? "user" : "kernel");
1780 ktrcsw(struct ktr_csw *cs)
1782 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume",
1783 cs->user ? "user" : "kernel", cs->wmesg);
1787 ktruser(int len, void *p)
1791 if (sysdecode_utrace(stdout, p, len)) {
1800 printf(" %d", *cp++);
1802 printf(" %02x", *cp++);
1807 ktrcaprights(cap_rights_t *rightsp)
1810 printf("cap_rights_t ");
1811 sysdecode_cap_rights(stdout, rightsp);
1816 ktrtimeval(struct timeval *tv)
1819 printf("{%ld, %ld}", (long)tv->tv_sec, tv->tv_usec);
1823 ktritimerval(struct itimerval *it)
1826 printf("itimerval { .interval = ");
1827 ktrtimeval(&it->it_interval);
1828 printf(", .value = ");
1829 ktrtimeval(&it->it_value);
1834 ktrsockaddr(struct sockaddr *sa)
1837 TODO: Support additional address families
1838 #include <netnatm/natm.h>
1839 struct sockaddr_natm *natm;
1840 #include <netsmb/netbios.h>
1841 struct sockaddr_nb *nb;
1847 * note: ktrstruct() has already verified that sa points to a
1848 * buffer at least sizeof(struct sockaddr) bytes long and exactly
1849 * sa->sa_len bytes long.
1851 printf("struct sockaddr { ");
1852 str = sysdecode_sockaddr_family(sa->sa_family);
1856 printf("<invalid=%d>", sa->sa_family);
1859 #define check_sockaddr_len(n) \
1860 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \
1861 printf("invalid"); \
1865 switch(sa->sa_family) {
1867 struct sockaddr_in sa_in;
1869 memset(&sa_in, 0, sizeof(sa_in));
1870 memcpy(&sa_in, sa, sa->sa_len);
1871 check_sockaddr_len(in);
1872 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr);
1873 printf("%s:%u", addr, ntohs(sa_in.sin_port));
1877 struct sockaddr_in6 sa_in6;
1879 memset(&sa_in6, 0, sizeof(sa_in6));
1880 memcpy(&sa_in6, sa, sa->sa_len);
1881 check_sockaddr_len(in6);
1882 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6),
1883 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST);
1884 printf("[%s]:%u", addr, htons(sa_in6.sin6_port));
1888 struct sockaddr_un sa_un;
1890 memset(&sa_un, 0, sizeof(sa_un));
1891 memcpy(&sa_un, sa, sa->sa_len);
1892 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path);
1896 printf("unknown address family");
1902 ktrstat(struct stat *statp)
1904 char mode[12], timestr[PATH_MAX + 4];
1910 * note: ktrstruct() has already verified that statp points to a
1911 * buffer exactly sizeof(struct stat) bytes long.
1913 printf("struct stat {");
1914 printf("dev=%ju, ino=%ju, ",
1915 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino);
1917 printf("mode=0%jo, ", (uintmax_t)statp->st_mode);
1919 strmode(statp->st_mode, mode);
1920 printf("mode=%s, ", mode);
1922 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink);
1926 #ifdef HAVE_LIBCASPER
1928 pwd = cap_getpwuid(cappwd, statp->st_uid);
1931 pwd = getpwuid(statp->st_uid);
1934 printf("uid=%ju, ", (uintmax_t)statp->st_uid);
1936 printf("uid=\"%s\", ", pwd->pw_name);
1940 #ifdef HAVE_LIBCASPER
1942 grp = cap_getgrgid(capgrp, statp->st_gid);
1945 grp = getgrgid(statp->st_gid);
1948 printf("gid=%ju, ", (uintmax_t)statp->st_gid);
1950 printf("gid=\"%s\", ", grp->gr_name);
1951 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev);
1954 printf("%jd", (intmax_t)statp->st_atim.tv_sec);
1956 tm = localtime(&statp->st_atim.tv_sec);
1957 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1958 printf("\"%s\"", timestr);
1960 if (statp->st_atim.tv_nsec != 0)
1961 printf(".%09ld, ", statp->st_atim.tv_nsec);
1966 printf("%jd", (intmax_t)statp->st_mtim.tv_sec);
1968 tm = localtime(&statp->st_mtim.tv_sec);
1969 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1970 printf("\"%s\"", timestr);
1972 if (statp->st_mtim.tv_nsec != 0)
1973 printf(".%09ld, ", statp->st_mtim.tv_nsec);
1978 printf("%jd", (intmax_t)statp->st_ctim.tv_sec);
1980 tm = localtime(&statp->st_ctim.tv_sec);
1981 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1982 printf("\"%s\"", timestr);
1984 if (statp->st_ctim.tv_nsec != 0)
1985 printf(".%09ld, ", statp->st_ctim.tv_nsec);
1988 printf("birthtime=");
1990 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec);
1992 tm = localtime(&statp->st_birthtim.tv_sec);
1993 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm);
1994 printf("\"%s\"", timestr);
1996 if (statp->st_birthtim.tv_nsec != 0)
1997 printf(".%09ld, ", statp->st_birthtim.tv_nsec);
2000 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x",
2001 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize,
2002 (intmax_t)statp->st_blocks, statp->st_flags);
2007 ktrstruct(char *buf, size_t buflen)
2010 size_t namelen, datalen;
2012 cap_rights_t rights;
2013 struct itimerval it;
2015 struct sockaddr_storage ss;
2017 for (name = buf, namelen = 0;
2018 namelen < buflen && name[namelen] != '\0';
2021 if (namelen == buflen)
2023 if (name[namelen] != '\0')
2025 data = buf + namelen + 1;
2026 datalen = buflen - namelen - 1;
2030 for (i = 0; i < (int)namelen; ++i)
2031 if (!isalpha(name[i]))
2033 if (strcmp(name, "caprights") == 0) {
2034 if (datalen != sizeof(cap_rights_t))
2036 memcpy(&rights, data, datalen);
2037 ktrcaprights(&rights);
2038 } else if (strcmp(name, "itimerval") == 0) {
2039 if (datalen != sizeof(struct itimerval))
2041 memcpy(&it, data, datalen);
2043 } else if (strcmp(name, "stat") == 0) {
2044 if (datalen != sizeof(struct stat))
2046 memcpy(&sb, data, datalen);
2048 } else if (strcmp(name, "sockaddr") == 0) {
2049 if (datalen > sizeof(ss))
2051 memcpy(&ss, data, datalen);
2052 if (datalen != ss.ss_len)
2054 ktrsockaddr((struct sockaddr *)&ss);
2056 printf("unknown structure\n");
2060 printf("invalid record\n");
2064 ktrcapfail(struct ktr_cap_fail *ktr)
2066 switch (ktr->cap_type) {
2067 case CAPFAIL_NOTCAPABLE:
2068 /* operation on fd with insufficient capabilities */
2069 printf("operation requires ");
2070 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2071 printf(", descriptor holds ");
2072 sysdecode_cap_rights(stdout, &ktr->cap_held);
2074 case CAPFAIL_INCREASE:
2075 /* requested more capabilities than fd already has */
2076 printf("attempt to increase capabilities from ");
2077 sysdecode_cap_rights(stdout, &ktr->cap_held);
2079 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2081 case CAPFAIL_SYSCALL:
2082 /* called restricted syscall */
2083 printf("disallowed system call");
2085 case CAPFAIL_LOOKUP:
2086 /* used ".." in strict-relative mode */
2087 printf("restricted VFS lookup");
2090 printf("unknown capability failure: ");
2091 sysdecode_cap_rights(stdout, &ktr->cap_needed);
2093 sysdecode_cap_rights(stdout, &ktr->cap_held);
2100 ktrfault(struct ktr_fault *ktr)
2103 printf("0x%jx ", (uintmax_t)ktr->vaddr);
2104 print_mask_arg(sysdecode_vmprot, ktr->type);
2109 ktrfaultend(struct ktr_faultend *ktr)
2113 str = sysdecode_vmresult(ktr->result);
2117 printf("<invalid=%d>", ktr->result);
2122 ktrkevent(struct kevent *kev)
2126 switch (kev->filter) {
2132 case EVFILT_PROCDESC:
2133 printf("%ju", (uintmax_t)kev->ident);
2136 print_signal(kev->ident);
2139 printf("%p", (void *)kev->ident);
2141 printf(", filter=");
2142 print_integer_arg(sysdecode_kevent_filter, kev->filter);
2144 print_mask_arg0(sysdecode_kevent_flags, kev->flags);
2145 printf(", fflags=");
2146 sysdecode_kevent_fflags(stdout, kev->filter, kev->fflags,
2148 printf(", data=%#jx, udata=%p }", (uintmax_t)kev->data, kev->udata);
2152 ktrstructarray(struct ktr_struct_array *ksa, size_t buflen)
2156 size_t namelen, datalen;
2160 buflen -= sizeof(*ksa);
2161 for (name = (char *)(ksa + 1), namelen = 0;
2162 namelen < buflen && name[namelen] != '\0';
2165 if (namelen == buflen)
2167 if (name[namelen] != '\0')
2170 for (i = 0; i < (int)namelen; ++i)
2171 if (!isalnum(name[i]) && name[i] != '_')
2173 data = name + namelen + 1;
2174 datalen = buflen - namelen - 1;
2175 printf("struct %s[] = { ", name);
2177 for (; datalen >= ksa->struct_size;
2178 data += ksa->struct_size, datalen -= ksa->struct_size) {
2183 if (strcmp(name, "kevent") == 0) {
2184 if (ksa->struct_size != sizeof(kev))
2186 memcpy(&kev, data, sizeof(kev));
2188 #ifdef _WANT_KEVENT32
2189 } else if (strcmp(name, "kevent32") == 0) {
2190 struct kevent32 kev32;
2192 if (ksa->struct_size != sizeof(kev32))
2194 memcpy(&kev32, data, sizeof(kev32));
2195 memset(&kev, 0, sizeof(kev));
2196 kev.ident = kev32.ident;
2197 kev.filter = kev32.filter;
2198 kev.flags = kev32.flags;
2199 kev.fflags = kev32.fflags;
2200 kev.data = kev32.data;
2201 kev.udata = (void *)(uintptr_t)kev32.udata;
2205 printf("<unknown structure> }\n");
2212 printf("invalid record\n");
2215 printf("<bad size> }\n");
2222 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] "
2223 "[-m maxdata] [-p pid] [-t trstr]\n");