1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 18 * 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 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef lint 33 static const char copyright[] = 34 "@(#) Copyright (c) 1988, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"; 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)kdump.c 8.1 (Berkeley) 6/6/93"; 41 #endif 42 #endif /* not lint */ 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #define _WANT_KERNEL_ERRNO 47 #ifdef __LP64__ 48 #define _WANT_KEVENT32 49 #endif 50 #define _WANT_FREEBSD11_KEVENT 51 #include <sys/param.h> 52 #include <sys/capsicum.h> 53 #include <sys/errno.h> 54 #include <sys/time.h> 55 #include <sys/uio.h> 56 #include <sys/event.h> 57 #include <sys/ktrace.h> 58 #include <sys/ioctl.h> 59 #include <sys/socket.h> 60 #include <sys/stat.h> 61 #include <sys/sysent.h> 62 #include <sys/umtx.h> 63 #include <sys/un.h> 64 #include <sys/queue.h> 65 #include <sys/wait.h> 66 #ifdef WITH_CASPER 67 #include <sys/nv.h> 68 #endif 69 #include <arpa/inet.h> 70 #include <netinet/in.h> 71 #include <ctype.h> 72 #include <capsicum_helpers.h> 73 #include <err.h> 74 #include <grp.h> 75 #include <inttypes.h> 76 #include <locale.h> 77 #include <netdb.h> 78 #include <nl_types.h> 79 #include <pwd.h> 80 #include <stddef.h> 81 #include <stdio.h> 82 #include <stdlib.h> 83 #include <string.h> 84 #include <sysdecode.h> 85 #include <time.h> 86 #include <unistd.h> 87 #include <vis.h> 88 #include "ktrace.h" 89 90 #ifdef WITH_CASPER 91 #include <libcasper.h> 92 93 #include <casper/cap_grp.h> 94 #include <casper/cap_pwd.h> 95 #endif 96 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); 121 void usage(void); 122 123 #define TIMESTAMP_NONE 0x0 124 #define TIMESTAMP_ABSOLUTE 0x1 125 #define TIMESTAMP_ELAPSED 0x2 126 #define TIMESTAMP_RELATIVE 0x4 127 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; 132 133 #define TIME_FORMAT "%b %e %T %Y" 134 #define eqs(s1, s2) (strcmp((s1), (s2)) == 0) 135 136 #define print_number64(first,i,n,c) do { \ 137 uint64_t __v; \ 138 \ 139 if (quad_align && (((ptrdiff_t)((i) - (first))) & 1) == 1) { \ 140 (i)++; \ 141 (n)--; \ 142 } \ 143 if (quad_slots == 2) \ 144 __v = (uint64_t)(uint32_t)(i)[0] | \ 145 ((uint64_t)(uint32_t)(i)[1]) << 32; \ 146 else \ 147 __v = (uint64_t)*(i); \ 148 if (decimal) \ 149 printf("%c%jd", (c), (intmax_t)__v); \ 150 else \ 151 printf("%c%#jx", (c), (uintmax_t)__v); \ 152 (i) += quad_slots; \ 153 (n) -= quad_slots; \ 154 (c) = ','; \ 155 } while (0) 156 157 #define print_number(i,n,c) do { \ 158 if (decimal) \ 159 printf("%c%jd", c, (intmax_t)*i); \ 160 else \ 161 printf("%c%#jx", c, (uintmax_t)(u_register_t)*i); \ 162 i++; \ 163 n--; \ 164 c = ','; \ 165 } while (0) 166 167 struct proc_info 168 { 169 TAILQ_ENTRY(proc_info) info; 170 u_int sv_flags; 171 pid_t pid; 172 }; 173 174 static TAILQ_HEAD(trace_procs, proc_info) trace_procs; 175 176 #ifdef WITH_CASPER 177 static cap_channel_t *cappwd, *capgrp; 178 179 static int 180 cappwdgrp_setup(cap_channel_t **cappwdp, cap_channel_t **capgrpp) 181 { 182 cap_channel_t *capcas, *cappwdloc, *capgrploc; 183 const char *cmds[1], *fields[1]; 184 185 capcas = cap_init(); 186 if (capcas == NULL) { 187 err(1, "unable to create casper process"); 188 exit(1); 189 } 190 cappwdloc = cap_service_open(capcas, "system.pwd"); 191 capgrploc = cap_service_open(capcas, "system.grp"); 192 /* Casper capability no longer needed. */ 193 cap_close(capcas); 194 if (cappwdloc == NULL || capgrploc == NULL) { 195 if (cappwdloc == NULL) 196 warn("unable to open system.pwd service"); 197 if (capgrploc == NULL) 198 warn("unable to open system.grp service"); 199 exit(1); 200 } 201 /* Limit system.pwd to only getpwuid() function and pw_name field. */ 202 cmds[0] = "getpwuid"; 203 if (cap_pwd_limit_cmds(cappwdloc, cmds, 1) < 0) 204 err(1, "unable to limit system.pwd service"); 205 fields[0] = "pw_name"; 206 if (cap_pwd_limit_fields(cappwdloc, fields, 1) < 0) 207 err(1, "unable to limit system.pwd service"); 208 /* Limit system.grp to only getgrgid() function and gr_name field. */ 209 cmds[0] = "getgrgid"; 210 if (cap_grp_limit_cmds(capgrploc, cmds, 1) < 0) 211 err(1, "unable to limit system.grp service"); 212 fields[0] = "gr_name"; 213 if (cap_grp_limit_fields(capgrploc, fields, 1) < 0) 214 err(1, "unable to limit system.grp service"); 215 216 *cappwdp = cappwdloc; 217 *capgrpp = capgrploc; 218 return (0); 219 } 220 #endif /* WITH_CASPER */ 221 222 static void 223 print_integer_arg(const char *(*decoder)(int), int value) 224 { 225 const char *str; 226 227 str = decoder(value); 228 if (str != NULL) 229 printf("%s", str); 230 else { 231 if (decimal) 232 printf("<invalid=%d>", value); 233 else 234 printf("<invalid=%#x>", value); 235 } 236 } 237 238 /* Like print_integer_arg but unknown values are treated as valid. */ 239 static void 240 print_integer_arg_valid(const char *(*decoder)(int), int value) 241 { 242 const char *str; 243 244 str = decoder(value); 245 if (str != NULL) 246 printf("%s", str); 247 else { 248 if (decimal) 249 printf("%d", value); 250 else 251 printf("%#x", value); 252 } 253 } 254 255 static void 256 print_mask_arg(bool (*decoder)(FILE *, int, int *), int value) 257 { 258 bool invalid; 259 int rem; 260 261 printf("%#x<", value); 262 invalid = !decoder(stdout, value, &rem); 263 printf(">"); 264 if (invalid) 265 printf("<invalid>%u", rem); 266 } 267 268 static void 269 print_mask_arg0(bool (*decoder)(FILE *, int, int *), int value) 270 { 271 bool invalid; 272 int rem; 273 274 if (value == 0) { 275 printf("0"); 276 return; 277 } 278 printf("%#x<", value); 279 invalid = !decoder(stdout, value, &rem); 280 printf(">"); 281 if (invalid) 282 printf("<invalid>%u", rem); 283 } 284 285 static void 286 decode_fileflags(fflags_t value) 287 { 288 bool invalid; 289 fflags_t rem; 290 291 if (value == 0) { 292 printf("0"); 293 return; 294 } 295 printf("%#x<", value); 296 invalid = !sysdecode_fileflags(stdout, value, &rem); 297 printf(">"); 298 if (invalid) 299 printf("<invalid>%u", rem); 300 } 301 302 static void 303 decode_filemode(int value) 304 { 305 bool invalid; 306 int rem; 307 308 if (value == 0) { 309 printf("0"); 310 return; 311 } 312 printf("%#o<", value); 313 invalid = !sysdecode_filemode(stdout, value, &rem); 314 printf(">"); 315 if (invalid) 316 printf("<invalid>%u", rem); 317 } 318 319 static void 320 print_mask_arg32(bool (*decoder)(FILE *, uint32_t, uint32_t *), uint32_t value) 321 { 322 bool invalid; 323 uint32_t rem; 324 325 printf("%#x<", value); 326 invalid = !decoder(stdout, value, &rem); 327 printf(">"); 328 if (invalid) 329 printf("<invalid>%u", rem); 330 } 331 332 static void 333 print_mask_argul(bool (*decoder)(FILE *, u_long, u_long *), u_long value) 334 { 335 bool invalid; 336 u_long rem; 337 338 if (value == 0) { 339 printf("0"); 340 return; 341 } 342 printf("%#lx<", value); 343 invalid = !decoder(stdout, value, &rem); 344 printf(">"); 345 if (invalid) 346 printf("<invalid>%lu", rem); 347 } 348 349 int 350 main(int argc, char *argv[]) 351 { 352 int ch, ktrlen, size; 353 void *m; 354 int trpoints = ALL_POINTS; 355 int drop_logged; 356 pid_t pid = 0; 357 u_int sv_flags; 358 359 setlocale(LC_CTYPE, ""); 360 361 timestamp = TIMESTAMP_NONE; 362 363 while ((ch = getopt(argc,argv,"f:dElm:np:AHRrSsTt:")) != -1) 364 switch (ch) { 365 case 'A': 366 abiflag = 1; 367 break; 368 case 'f': 369 tracefile = optarg; 370 break; 371 case 'd': 372 decimal = 1; 373 break; 374 case 'l': 375 tail = 1; 376 break; 377 case 'm': 378 maxdata = atoi(optarg); 379 break; 380 case 'n': 381 fancy = 0; 382 break; 383 case 'p': 384 pid = atoi(optarg); 385 break; 386 case 'r': 387 resolv = 1; 388 break; 389 case 'S': 390 syscallno = 1; 391 break; 392 case 's': 393 suppressdata = 1; 394 break; 395 case 'E': 396 timestamp |= TIMESTAMP_ELAPSED; 397 break; 398 case 'H': 399 threads = 1; 400 break; 401 case 'R': 402 timestamp |= TIMESTAMP_RELATIVE; 403 break; 404 case 'T': 405 timestamp |= TIMESTAMP_ABSOLUTE; 406 break; 407 case 't': 408 trpoints = getpoints(optarg); 409 if (trpoints < 0) 410 errx(1, "unknown trace point in %s", optarg); 411 break; 412 default: 413 usage(); 414 } 415 416 if (argc > optind) 417 usage(); 418 419 m = malloc(size = 1025); 420 if (m == NULL) 421 errx(1, "%s", strerror(ENOMEM)); 422 if (strcmp(tracefile, "-") != 0) 423 if (!freopen(tracefile, "r", stdin)) 424 err(1, "%s", tracefile); 425 426 caph_cache_catpages(); 427 caph_cache_tzdata(); 428 429 #ifdef WITH_CASPER 430 if (resolv != 0) { 431 if (cappwdgrp_setup(&cappwd, &capgrp) < 0) { 432 cappwd = NULL; 433 capgrp = NULL; 434 } 435 } 436 if (resolv == 0 || (cappwd != NULL && capgrp != NULL)) { 437 if (caph_enter() < 0) 438 err(1, "unable to enter capability mode"); 439 } 440 #else 441 if (resolv == 0) { 442 if (caph_enter() < 0) 443 err(1, "unable to enter capability mode"); 444 } 445 #endif 446 if (caph_limit_stdio() == -1) 447 err(1, "unable to limit stdio"); 448 449 TAILQ_INIT(&trace_procs); 450 drop_logged = 0; 451 while (fread_tail(&ktr_header, sizeof(struct ktr_header), 1)) { 452 if (ktr_header.ktr_type & KTR_DROP) { 453 ktr_header.ktr_type &= ~KTR_DROP; 454 if (!drop_logged && threads) { 455 printf( 456 "%6jd %6jd %-8.*s Events dropped.\n", 457 (intmax_t)ktr_header.ktr_pid, 458 ktr_header.ktr_tid > 0 ? 459 (intmax_t)ktr_header.ktr_tid : 0, 460 MAXCOMLEN, ktr_header.ktr_comm); 461 drop_logged = 1; 462 } else if (!drop_logged) { 463 printf("%6jd %-8.*s Events dropped.\n", 464 (intmax_t)ktr_header.ktr_pid, MAXCOMLEN, 465 ktr_header.ktr_comm); 466 drop_logged = 1; 467 } 468 } 469 if (trpoints & (1<<ktr_header.ktr_type)) 470 if (pid == 0 || ktr_header.ktr_pid == pid || 471 ktr_header.ktr_tid == pid) 472 dumpheader(&ktr_header); 473 if ((ktrlen = ktr_header.ktr_len) < 0) 474 errx(1, "bogus length 0x%x", ktrlen); 475 if (ktrlen > size) { 476 m = realloc(m, ktrlen+1); 477 if (m == NULL) 478 errx(1, "%s", strerror(ENOMEM)); 479 size = ktrlen; 480 } 481 if (ktrlen && fread_tail(m, ktrlen, 1) == 0) 482 errx(1, "data too short"); 483 if (fetchprocinfo(&ktr_header, (u_int *)m) != 0) 484 continue; 485 sv_flags = abidump(&ktr_header); 486 if (pid && ktr_header.ktr_pid != pid && 487 ktr_header.ktr_tid != pid) 488 continue; 489 if ((trpoints & (1<<ktr_header.ktr_type)) == 0) 490 continue; 491 drop_logged = 0; 492 switch (ktr_header.ktr_type) { 493 case KTR_SYSCALL: 494 ktrsyscall((struct ktr_syscall *)m, sv_flags); 495 break; 496 case KTR_SYSRET: 497 ktrsysret((struct ktr_sysret *)m, sv_flags); 498 break; 499 case KTR_NAMEI: 500 case KTR_SYSCTL: 501 ktrnamei(m, ktrlen); 502 break; 503 case KTR_GENIO: 504 ktrgenio((struct ktr_genio *)m, ktrlen); 505 break; 506 case KTR_PSIG: 507 ktrpsig((struct ktr_psig *)m); 508 break; 509 case KTR_CSW: 510 if (ktrlen == sizeof(struct ktr_csw_old)) 511 ktrcsw_old((struct ktr_csw_old *)m); 512 else 513 ktrcsw((struct ktr_csw *)m); 514 break; 515 case KTR_USER: 516 ktruser(ktrlen, m); 517 break; 518 case KTR_STRUCT: 519 ktrstruct(m, ktrlen); 520 break; 521 case KTR_CAPFAIL: 522 ktrcapfail((struct ktr_cap_fail *)m); 523 break; 524 case KTR_FAULT: 525 ktrfault((struct ktr_fault *)m); 526 break; 527 case KTR_FAULTEND: 528 ktrfaultend((struct ktr_faultend *)m); 529 break; 530 case KTR_STRUCT_ARRAY: 531 ktrstructarray((struct ktr_struct_array *)m, ktrlen); 532 break; 533 default: 534 printf("\n"); 535 break; 536 } 537 if (tail) 538 fflush(stdout); 539 } 540 return 0; 541 } 542 543 int 544 fread_tail(void *buf, int size, int num) 545 { 546 int i; 547 548 while ((i = fread(buf, size, num, stdin)) == 0 && tail) { 549 sleep(1); 550 clearerr(stdin); 551 } 552 return (i); 553 } 554 555 int 556 fetchprocinfo(struct ktr_header *kth, u_int *flags) 557 { 558 struct proc_info *pi; 559 560 switch (kth->ktr_type) { 561 case KTR_PROCCTOR: 562 TAILQ_FOREACH(pi, &trace_procs, info) { 563 if (pi->pid == kth->ktr_pid) { 564 TAILQ_REMOVE(&trace_procs, pi, info); 565 break; 566 } 567 } 568 pi = malloc(sizeof(struct proc_info)); 569 if (pi == NULL) 570 errx(1, "%s", strerror(ENOMEM)); 571 pi->sv_flags = *flags; 572 pi->pid = kth->ktr_pid; 573 TAILQ_INSERT_TAIL(&trace_procs, pi, info); 574 return (1); 575 576 case KTR_PROCDTOR: 577 TAILQ_FOREACH(pi, &trace_procs, info) { 578 if (pi->pid == kth->ktr_pid) { 579 TAILQ_REMOVE(&trace_procs, pi, info); 580 free(pi); 581 break; 582 } 583 } 584 return (1); 585 } 586 587 return (0); 588 } 589 590 u_int 591 abidump(struct ktr_header *kth) 592 { 593 struct proc_info *pi; 594 const char *abi; 595 const char *arch; 596 u_int flags = 0; 597 598 TAILQ_FOREACH(pi, &trace_procs, info) { 599 if (pi->pid == kth->ktr_pid) { 600 flags = pi->sv_flags; 601 break; 602 } 603 } 604 605 if (abiflag == 0) 606 return (flags); 607 608 switch (flags & SV_ABI_MASK) { 609 case SV_ABI_LINUX: 610 abi = "L"; 611 break; 612 case SV_ABI_FREEBSD: 613 abi = "F"; 614 break; 615 case SV_ABI_CLOUDABI: 616 abi = "C"; 617 break; 618 default: 619 abi = "U"; 620 break; 621 } 622 623 if (flags & SV_LP64) 624 arch = "64"; 625 else if (flags & SV_ILP32) 626 arch = "32"; 627 else 628 arch = "00"; 629 630 printf("%s%s ", abi, arch); 631 632 return (flags); 633 } 634 635 void 636 dumpheader(struct ktr_header *kth) 637 { 638 static char unknown[64]; 639 static struct timeval prevtime, prevtime_e; 640 struct timeval temp; 641 const char *type; 642 const char *sign; 643 644 switch (kth->ktr_type) { 645 case KTR_SYSCALL: 646 type = "CALL"; 647 break; 648 case KTR_SYSRET: 649 type = "RET "; 650 break; 651 case KTR_NAMEI: 652 type = "NAMI"; 653 break; 654 case KTR_GENIO: 655 type = "GIO "; 656 break; 657 case KTR_PSIG: 658 type = "PSIG"; 659 break; 660 case KTR_CSW: 661 type = "CSW "; 662 break; 663 case KTR_USER: 664 type = "USER"; 665 break; 666 case KTR_STRUCT: 667 case KTR_STRUCT_ARRAY: 668 type = "STRU"; 669 break; 670 case KTR_SYSCTL: 671 type = "SCTL"; 672 break; 673 case KTR_PROCCTOR: 674 /* FALLTHROUGH */ 675 case KTR_PROCDTOR: 676 return; 677 case KTR_CAPFAIL: 678 type = "CAP "; 679 break; 680 case KTR_FAULT: 681 type = "PFLT"; 682 break; 683 case KTR_FAULTEND: 684 type = "PRET"; 685 break; 686 default: 687 sprintf(unknown, "UNKNOWN(%d)", kth->ktr_type); 688 type = unknown; 689 } 690 691 /* 692 * The ktr_tid field was previously the ktr_buffer field, which held 693 * the kernel pointer value for the buffer associated with data 694 * following the record header. It now holds a threadid, but only 695 * for trace files after the change. Older trace files still contain 696 * kernel pointers. Detect this and suppress the results by printing 697 * negative tid's as 0. 698 */ 699 if (threads) 700 printf("%6jd %6jd %-8.*s ", (intmax_t)kth->ktr_pid, 701 kth->ktr_tid > 0 ? (intmax_t)kth->ktr_tid : 0, 702 MAXCOMLEN, kth->ktr_comm); 703 else 704 printf("%6jd %-8.*s ", (intmax_t)kth->ktr_pid, MAXCOMLEN, 705 kth->ktr_comm); 706 if (timestamp) { 707 if (timestamp & TIMESTAMP_ABSOLUTE) { 708 printf("%jd.%06ld ", (intmax_t)kth->ktr_time.tv_sec, 709 kth->ktr_time.tv_usec); 710 } 711 if (timestamp & TIMESTAMP_ELAPSED) { 712 if (prevtime_e.tv_sec == 0) 713 prevtime_e = kth->ktr_time; 714 timersub(&kth->ktr_time, &prevtime_e, &temp); 715 printf("%jd.%06ld ", (intmax_t)temp.tv_sec, 716 temp.tv_usec); 717 } 718 if (timestamp & TIMESTAMP_RELATIVE) { 719 if (prevtime.tv_sec == 0) 720 prevtime = kth->ktr_time; 721 if (timercmp(&kth->ktr_time, &prevtime, <)) { 722 timersub(&prevtime, &kth->ktr_time, &temp); 723 sign = "-"; 724 } else { 725 timersub(&kth->ktr_time, &prevtime, &temp); 726 sign = ""; 727 } 728 prevtime = kth->ktr_time; 729 printf("%s%jd.%06ld ", sign, (intmax_t)temp.tv_sec, 730 temp.tv_usec); 731 } 732 } 733 printf("%s ", type); 734 } 735 736 #include <sys/syscall.h> 737 738 static void 739 ioctlname(unsigned long val) 740 { 741 const char *str; 742 743 str = sysdecode_ioctlname(val); 744 if (str != NULL) 745 printf("%s", str); 746 else if (decimal) 747 printf("%lu", val); 748 else 749 printf("%#lx", val); 750 } 751 752 static enum sysdecode_abi 753 syscallabi(u_int sv_flags) 754 { 755 756 if (sv_flags == 0) 757 return (SYSDECODE_ABI_FREEBSD); 758 switch (sv_flags & SV_ABI_MASK) { 759 case SV_ABI_FREEBSD: 760 return (SYSDECODE_ABI_FREEBSD); 761 case SV_ABI_LINUX: 762 #ifdef __LP64__ 763 if (sv_flags & SV_ILP32) 764 return (SYSDECODE_ABI_LINUX32); 765 #endif 766 return (SYSDECODE_ABI_LINUX); 767 case SV_ABI_CLOUDABI: 768 return (SYSDECODE_ABI_CLOUDABI64); 769 default: 770 return (SYSDECODE_ABI_UNKNOWN); 771 } 772 } 773 774 static void 775 syscallname(u_int code, u_int sv_flags) 776 { 777 const char *name; 778 779 name = sysdecode_syscallname(syscallabi(sv_flags), code); 780 if (name == NULL) 781 printf("[%d]", code); 782 else { 783 printf("%s", name); 784 if (syscallno) 785 printf("[%d]", code); 786 } 787 } 788 789 static void 790 print_signal(int signo) 791 { 792 const char *signame; 793 794 signame = sysdecode_signal(signo); 795 if (signame != NULL) 796 printf("%s", signame); 797 else 798 printf("SIG %d", signo); 799 } 800 801 void 802 ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags) 803 { 804 int narg = ktr->ktr_narg; 805 register_t *ip, *first; 806 intmax_t arg; 807 int quad_align, quad_slots; 808 809 syscallname(ktr->ktr_code, sv_flags); 810 ip = first = &ktr->ktr_args[0]; 811 if (narg) { 812 char c = '('; 813 if (fancy && 814 (sv_flags == 0 || 815 (sv_flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) { 816 quad_align = 0; 817 if (sv_flags & SV_ILP32) { 818 #ifdef __powerpc__ 819 quad_align = 1; 820 #endif 821 quad_slots = 2; 822 } else 823 quad_slots = 1; 824 switch (ktr->ktr_code) { 825 case SYS_bindat: 826 case SYS_chflagsat: 827 case SYS_connectat: 828 case SYS_faccessat: 829 case SYS_fchmodat: 830 case SYS_fchownat: 831 case SYS_fstatat: 832 case SYS_futimesat: 833 case SYS_linkat: 834 case SYS_mkdirat: 835 case SYS_mkfifoat: 836 case SYS_mknodat: 837 case SYS_openat: 838 case SYS_readlinkat: 839 case SYS_renameat: 840 case SYS_unlinkat: 841 case SYS_utimensat: 842 putchar('('); 843 print_integer_arg_valid(sysdecode_atfd, *ip); 844 c = ','; 845 ip++; 846 narg--; 847 break; 848 } 849 switch (ktr->ktr_code) { 850 case SYS_ioctl: { 851 print_number(ip, narg, c); 852 putchar(c); 853 ioctlname(*ip); 854 c = ','; 855 ip++; 856 narg--; 857 break; 858 } 859 case SYS_ptrace: 860 putchar('('); 861 print_integer_arg(sysdecode_ptrace_request, *ip); 862 c = ','; 863 ip++; 864 narg--; 865 break; 866 case SYS_access: 867 case SYS_eaccess: 868 case SYS_faccessat: 869 print_number(ip, narg, c); 870 putchar(','); 871 print_mask_arg(sysdecode_access_mode, *ip); 872 ip++; 873 narg--; 874 break; 875 case SYS_open: 876 case SYS_openat: 877 print_number(ip, narg, c); 878 putchar(','); 879 print_mask_arg(sysdecode_open_flags, ip[0]); 880 if ((ip[0] & O_CREAT) == O_CREAT) { 881 putchar(','); 882 decode_filemode(ip[1]); 883 } 884 ip += 2; 885 narg -= 2; 886 break; 887 case SYS_wait4: 888 print_number(ip, narg, c); 889 print_number(ip, narg, c); 890 putchar(','); 891 print_mask_arg0(sysdecode_wait4_options, *ip); 892 ip++; 893 narg--; 894 break; 895 case SYS_wait6: 896 putchar('('); 897 print_integer_arg(sysdecode_idtype, *ip); 898 c = ','; 899 ip++; 900 narg--; 901 print_number64(first, ip, narg, c); 902 print_number(ip, narg, c); 903 putchar(','); 904 print_mask_arg(sysdecode_wait6_options, *ip); 905 ip++; 906 narg--; 907 break; 908 case SYS_chmod: 909 case SYS_fchmod: 910 case SYS_lchmod: 911 case SYS_fchmodat: 912 print_number(ip, narg, c); 913 putchar(','); 914 decode_filemode(*ip); 915 ip++; 916 narg--; 917 break; 918 case SYS_mknodat: 919 print_number(ip, narg, c); 920 putchar(','); 921 decode_filemode(*ip); 922 ip++; 923 narg--; 924 break; 925 case SYS_getfsstat: 926 print_number(ip, narg, c); 927 print_number(ip, narg, c); 928 putchar(','); 929 print_integer_arg(sysdecode_getfsstat_mode, *ip); 930 ip++; 931 narg--; 932 break; 933 case SYS_mount: 934 print_number(ip, narg, c); 935 print_number(ip, narg, c); 936 putchar(','); 937 print_mask_arg(sysdecode_mount_flags, *ip); 938 ip++; 939 narg--; 940 break; 941 case SYS_unmount: 942 print_number(ip, narg, c); 943 putchar(','); 944 print_mask_arg(sysdecode_mount_flags, *ip); 945 ip++; 946 narg--; 947 break; 948 case SYS_recvmsg: 949 case SYS_sendmsg: 950 print_number(ip, narg, c); 951 print_number(ip, narg, c); 952 putchar(','); 953 print_mask_arg0(sysdecode_msg_flags, *ip); 954 ip++; 955 narg--; 956 break; 957 case SYS_recvfrom: 958 case SYS_sendto: 959 print_number(ip, narg, c); 960 print_number(ip, narg, c); 961 print_number(ip, narg, c); 962 putchar(','); 963 print_mask_arg0(sysdecode_msg_flags, *ip); 964 ip++; 965 narg--; 966 break; 967 case SYS_chflags: 968 case SYS_chflagsat: 969 case SYS_fchflags: 970 case SYS_lchflags: 971 print_number(ip, narg, c); 972 putchar(','); 973 decode_fileflags(*ip); 974 ip++; 975 narg--; 976 break; 977 case SYS_kill: 978 print_number(ip, narg, c); 979 putchar(','); 980 print_signal(*ip); 981 ip++; 982 narg--; 983 break; 984 case SYS_reboot: 985 putchar('('); 986 print_mask_arg(sysdecode_reboot_howto, *ip); 987 ip++; 988 narg--; 989 break; 990 case SYS_umask: 991 putchar('('); 992 decode_filemode(*ip); 993 ip++; 994 narg--; 995 break; 996 case SYS_msync: 997 print_number(ip, narg, c); 998 print_number(ip, narg, c); 999 putchar(','); 1000 print_mask_arg(sysdecode_msync_flags, *ip); 1001 ip++; 1002 narg--; 1003 break; 1004 #ifdef SYS_freebsd6_mmap 1005 case SYS_freebsd6_mmap: 1006 print_number(ip, narg, c); 1007 print_number(ip, narg, c); 1008 putchar(','); 1009 print_mask_arg(sysdecode_mmap_prot, *ip); 1010 putchar(','); 1011 ip++; 1012 narg--; 1013 print_mask_arg(sysdecode_mmap_flags, *ip); 1014 ip++; 1015 narg--; 1016 break; 1017 #endif 1018 case SYS_mmap: 1019 print_number(ip, narg, c); 1020 print_number(ip, narg, c); 1021 putchar(','); 1022 print_mask_arg(sysdecode_mmap_prot, *ip); 1023 putchar(','); 1024 ip++; 1025 narg--; 1026 print_mask_arg(sysdecode_mmap_flags, *ip); 1027 ip++; 1028 narg--; 1029 break; 1030 case SYS_mprotect: 1031 print_number(ip, narg, c); 1032 print_number(ip, narg, c); 1033 putchar(','); 1034 print_mask_arg(sysdecode_mmap_prot, *ip); 1035 ip++; 1036 narg--; 1037 break; 1038 case SYS_madvise: 1039 print_number(ip, narg, c); 1040 print_number(ip, narg, c); 1041 putchar(','); 1042 print_integer_arg(sysdecode_madvice, *ip); 1043 ip++; 1044 narg--; 1045 break; 1046 case SYS_pathconf: 1047 case SYS_lpathconf: 1048 case SYS_fpathconf: 1049 print_number(ip, narg, c); 1050 putchar(','); 1051 print_integer_arg(sysdecode_pathconf_name, *ip); 1052 ip++; 1053 narg--; 1054 break; 1055 case SYS_getpriority: 1056 case SYS_setpriority: 1057 putchar('('); 1058 print_integer_arg(sysdecode_prio_which, *ip); 1059 c = ','; 1060 ip++; 1061 narg--; 1062 break; 1063 case SYS_fcntl: 1064 print_number(ip, narg, c); 1065 putchar(','); 1066 print_integer_arg(sysdecode_fcntl_cmd, ip[0]); 1067 if (sysdecode_fcntl_arg_p(ip[0])) { 1068 putchar(','); 1069 if (ip[0] == F_SETFL) 1070 print_mask_arg( 1071 sysdecode_fcntl_fileflags, 1072 ip[1]); 1073 else 1074 sysdecode_fcntl_arg(stdout, 1075 ip[0], ip[1], 1076 decimal ? 10 : 16); 1077 } 1078 ip += 2; 1079 narg -= 2; 1080 break; 1081 case SYS_socket: { 1082 int sockdomain; 1083 putchar('('); 1084 sockdomain = *ip; 1085 print_integer_arg(sysdecode_socketdomain, 1086 sockdomain); 1087 ip++; 1088 narg--; 1089 putchar(','); 1090 print_mask_arg(sysdecode_socket_type, *ip); 1091 ip++; 1092 narg--; 1093 if (sockdomain == PF_INET || 1094 sockdomain == PF_INET6) { 1095 putchar(','); 1096 print_integer_arg(sysdecode_ipproto, 1097 *ip); 1098 ip++; 1099 narg--; 1100 } 1101 c = ','; 1102 break; 1103 } 1104 case SYS_setsockopt: 1105 case SYS_getsockopt: { 1106 const char *str; 1107 1108 print_number(ip, narg, c); 1109 putchar(','); 1110 print_integer_arg_valid(sysdecode_sockopt_level, 1111 *ip); 1112 str = sysdecode_sockopt_name(ip[0], ip[1]); 1113 if (str != NULL) { 1114 printf(",%s", str); 1115 ip++; 1116 narg--; 1117 } 1118 ip++; 1119 narg--; 1120 break; 1121 } 1122 #ifdef SYS_freebsd6_lseek 1123 case SYS_freebsd6_lseek: 1124 print_number(ip, narg, c); 1125 /* Hidden 'pad' argument, not in lseek(2) */ 1126 print_number(ip, narg, c); 1127 print_number64(first, ip, narg, c); 1128 putchar(','); 1129 print_integer_arg(sysdecode_whence, *ip); 1130 ip++; 1131 narg--; 1132 break; 1133 #endif 1134 case SYS_lseek: 1135 print_number(ip, narg, c); 1136 print_number64(first, ip, narg, c); 1137 putchar(','); 1138 print_integer_arg(sysdecode_whence, *ip); 1139 ip++; 1140 narg--; 1141 break; 1142 case SYS_flock: 1143 print_number(ip, narg, c); 1144 putchar(','); 1145 print_mask_arg(sysdecode_flock_operation, *ip); 1146 ip++; 1147 narg--; 1148 break; 1149 case SYS_mkfifo: 1150 case SYS_mkfifoat: 1151 case SYS_mkdir: 1152 case SYS_mkdirat: 1153 print_number(ip, narg, c); 1154 putchar(','); 1155 decode_filemode(*ip); 1156 ip++; 1157 narg--; 1158 break; 1159 case SYS_shutdown: 1160 print_number(ip, narg, c); 1161 putchar(','); 1162 print_integer_arg(sysdecode_shutdown_how, *ip); 1163 ip++; 1164 narg--; 1165 break; 1166 case SYS_socketpair: 1167 putchar('('); 1168 print_integer_arg(sysdecode_socketdomain, *ip); 1169 ip++; 1170 narg--; 1171 putchar(','); 1172 print_mask_arg(sysdecode_socket_type, *ip); 1173 ip++; 1174 narg--; 1175 c = ','; 1176 break; 1177 case SYS_getrlimit: 1178 case SYS_setrlimit: 1179 putchar('('); 1180 print_integer_arg(sysdecode_rlimit, *ip); 1181 ip++; 1182 narg--; 1183 c = ','; 1184 break; 1185 case SYS_getrusage: 1186 putchar('('); 1187 print_integer_arg(sysdecode_getrusage_who, *ip); 1188 ip++; 1189 narg--; 1190 c = ','; 1191 break; 1192 case SYS_quotactl: 1193 print_number(ip, narg, c); 1194 putchar(','); 1195 if (!sysdecode_quotactl_cmd(stdout, *ip)) { 1196 if (decimal) 1197 printf("<invalid=%d>", (int)*ip); 1198 else 1199 printf("<invalid=%#x>", 1200 (int)*ip); 1201 } 1202 ip++; 1203 narg--; 1204 c = ','; 1205 break; 1206 case SYS_nfssvc: 1207 putchar('('); 1208 print_integer_arg(sysdecode_nfssvc_flags, *ip); 1209 ip++; 1210 narg--; 1211 c = ','; 1212 break; 1213 case SYS_rtprio: 1214 case SYS_rtprio_thread: 1215 putchar('('); 1216 print_integer_arg(sysdecode_rtprio_function, 1217 *ip); 1218 ip++; 1219 narg--; 1220 c = ','; 1221 break; 1222 case SYS___semctl: 1223 print_number(ip, narg, c); 1224 print_number(ip, narg, c); 1225 putchar(','); 1226 print_integer_arg(sysdecode_semctl_cmd, *ip); 1227 ip++; 1228 narg--; 1229 break; 1230 case SYS_semget: 1231 print_number(ip, narg, c); 1232 print_number(ip, narg, c); 1233 putchar(','); 1234 print_mask_arg(sysdecode_semget_flags, *ip); 1235 ip++; 1236 narg--; 1237 break; 1238 case SYS_msgctl: 1239 print_number(ip, narg, c); 1240 putchar(','); 1241 print_integer_arg(sysdecode_msgctl_cmd, *ip); 1242 ip++; 1243 narg--; 1244 break; 1245 case SYS_shmat: 1246 print_number(ip, narg, c); 1247 print_number(ip, narg, c); 1248 putchar(','); 1249 print_mask_arg(sysdecode_shmat_flags, *ip); 1250 ip++; 1251 narg--; 1252 break; 1253 case SYS_shmctl: 1254 print_number(ip, narg, c); 1255 putchar(','); 1256 print_integer_arg(sysdecode_shmctl_cmd, *ip); 1257 ip++; 1258 narg--; 1259 break; 1260 case SYS_shm_open: 1261 print_number(ip, narg, c); 1262 putchar(','); 1263 print_mask_arg(sysdecode_open_flags, ip[0]); 1264 putchar(','); 1265 decode_filemode(ip[1]); 1266 ip += 2; 1267 narg -= 2; 1268 break; 1269 case SYS_minherit: 1270 print_number(ip, narg, c); 1271 print_number(ip, narg, c); 1272 putchar(','); 1273 print_integer_arg(sysdecode_minherit_inherit, 1274 *ip); 1275 ip++; 1276 narg--; 1277 break; 1278 case SYS_rfork: 1279 putchar('('); 1280 print_mask_arg(sysdecode_rfork_flags, *ip); 1281 ip++; 1282 narg--; 1283 c = ','; 1284 break; 1285 case SYS_lio_listio: 1286 putchar('('); 1287 print_integer_arg(sysdecode_lio_listio_mode, 1288 *ip); 1289 ip++; 1290 narg--; 1291 c = ','; 1292 break; 1293 case SYS_mlockall: 1294 putchar('('); 1295 print_mask_arg(sysdecode_mlockall_flags, *ip); 1296 ip++; 1297 narg--; 1298 break; 1299 case SYS_sched_setscheduler: 1300 print_number(ip, narg, c); 1301 putchar(','); 1302 print_integer_arg(sysdecode_scheduler_policy, 1303 *ip); 1304 ip++; 1305 narg--; 1306 break; 1307 case SYS_sched_get_priority_max: 1308 case SYS_sched_get_priority_min: 1309 putchar('('); 1310 print_integer_arg(sysdecode_scheduler_policy, 1311 *ip); 1312 ip++; 1313 narg--; 1314 break; 1315 case SYS_sendfile: 1316 print_number(ip, narg, c); 1317 print_number(ip, narg, c); 1318 print_number(ip, narg, c); 1319 print_number(ip, narg, c); 1320 print_number(ip, narg, c); 1321 print_number(ip, narg, c); 1322 putchar(','); 1323 print_mask_arg(sysdecode_sendfile_flags, *ip); 1324 ip++; 1325 narg--; 1326 break; 1327 case SYS_kldsym: 1328 print_number(ip, narg, c); 1329 putchar(','); 1330 print_integer_arg(sysdecode_kldsym_cmd, *ip); 1331 ip++; 1332 narg--; 1333 break; 1334 case SYS_sigprocmask: 1335 putchar('('); 1336 print_integer_arg(sysdecode_sigprocmask_how, 1337 *ip); 1338 ip++; 1339 narg--; 1340 c = ','; 1341 break; 1342 case SYS___acl_get_file: 1343 case SYS___acl_set_file: 1344 case SYS___acl_get_fd: 1345 case SYS___acl_set_fd: 1346 case SYS___acl_delete_file: 1347 case SYS___acl_delete_fd: 1348 case SYS___acl_aclcheck_file: 1349 case SYS___acl_aclcheck_fd: 1350 case SYS___acl_get_link: 1351 case SYS___acl_set_link: 1352 case SYS___acl_delete_link: 1353 case SYS___acl_aclcheck_link: 1354 print_number(ip, narg, c); 1355 putchar(','); 1356 print_integer_arg(sysdecode_acltype, *ip); 1357 ip++; 1358 narg--; 1359 break; 1360 case SYS_sigaction: 1361 putchar('('); 1362 print_signal(*ip); 1363 ip++; 1364 narg--; 1365 c = ','; 1366 break; 1367 case SYS_extattrctl: 1368 print_number(ip, narg, c); 1369 putchar(','); 1370 print_integer_arg(sysdecode_extattrnamespace, 1371 *ip); 1372 ip++; 1373 narg--; 1374 break; 1375 case SYS_nmount: 1376 print_number(ip, narg, c); 1377 print_number(ip, narg, c); 1378 putchar(','); 1379 print_mask_arg(sysdecode_mount_flags, *ip); 1380 ip++; 1381 narg--; 1382 break; 1383 case SYS_thr_create: 1384 print_number(ip, narg, c); 1385 print_number(ip, narg, c); 1386 putchar(','); 1387 print_mask_arg(sysdecode_thr_create_flags, *ip); 1388 ip++; 1389 narg--; 1390 break; 1391 case SYS_thr_kill: 1392 print_number(ip, narg, c); 1393 putchar(','); 1394 print_signal(*ip); 1395 ip++; 1396 narg--; 1397 break; 1398 case SYS_kldunloadf: 1399 print_number(ip, narg, c); 1400 putchar(','); 1401 print_integer_arg(sysdecode_kldunload_flags, 1402 *ip); 1403 ip++; 1404 narg--; 1405 break; 1406 case SYS_linkat: 1407 case SYS_renameat: 1408 case SYS_symlinkat: 1409 print_number(ip, narg, c); 1410 putchar(','); 1411 print_integer_arg_valid(sysdecode_atfd, *ip); 1412 ip++; 1413 narg--; 1414 print_number(ip, narg, c); 1415 break; 1416 case SYS_cap_fcntls_limit: 1417 print_number(ip, narg, c); 1418 putchar(','); 1419 arg = *ip; 1420 ip++; 1421 narg--; 1422 print_mask_arg32(sysdecode_cap_fcntlrights, arg); 1423 break; 1424 case SYS_posix_fadvise: 1425 print_number(ip, narg, c); 1426 print_number(ip, narg, c); 1427 print_number(ip, narg, c); 1428 (void)putchar(','); 1429 print_integer_arg(sysdecode_fadvice, *ip); 1430 ip++; 1431 narg--; 1432 break; 1433 case SYS_procctl: 1434 putchar('('); 1435 print_integer_arg(sysdecode_idtype, *ip); 1436 c = ','; 1437 ip++; 1438 narg--; 1439 print_number64(first, ip, narg, c); 1440 putchar(','); 1441 print_integer_arg(sysdecode_procctl_cmd, *ip); 1442 ip++; 1443 narg--; 1444 break; 1445 case SYS__umtx_op: 1446 print_number(ip, narg, c); 1447 putchar(','); 1448 print_integer_arg(sysdecode_umtx_op, *ip); 1449 switch (*ip) { 1450 case UMTX_OP_CV_WAIT: 1451 ip++; 1452 narg--; 1453 putchar(','); 1454 print_mask_argul( 1455 sysdecode_umtx_cvwait_flags, *ip); 1456 break; 1457 case UMTX_OP_RW_RDLOCK: 1458 ip++; 1459 narg--; 1460 putchar(','); 1461 print_mask_argul( 1462 sysdecode_umtx_rwlock_flags, *ip); 1463 break; 1464 } 1465 ip++; 1466 narg--; 1467 break; 1468 case SYS_ftruncate: 1469 case SYS_truncate: 1470 print_number(ip, narg, c); 1471 print_number64(first, ip, narg, c); 1472 break; 1473 case SYS_fchownat: 1474 print_number(ip, narg, c); 1475 print_number(ip, narg, c); 1476 print_number(ip, narg, c); 1477 break; 1478 case SYS_fstatat: 1479 case SYS_utimensat: 1480 print_number(ip, narg, c); 1481 print_number(ip, narg, c); 1482 break; 1483 case SYS_unlinkat: 1484 print_number(ip, narg, c); 1485 break; 1486 case SYS_sysarch: 1487 putchar('('); 1488 print_integer_arg(sysdecode_sysarch_number, *ip); 1489 ip++; 1490 narg--; 1491 c = ','; 1492 break; 1493 } 1494 switch (ktr->ktr_code) { 1495 case SYS_chflagsat: 1496 case SYS_fchownat: 1497 case SYS_faccessat: 1498 case SYS_fchmodat: 1499 case SYS_fstatat: 1500 case SYS_linkat: 1501 case SYS_unlinkat: 1502 case SYS_utimensat: 1503 putchar(','); 1504 print_mask_arg0(sysdecode_atflags, *ip); 1505 ip++; 1506 narg--; 1507 break; 1508 } 1509 } 1510 while (narg > 0) { 1511 print_number(ip, narg, c); 1512 } 1513 putchar(')'); 1514 } 1515 putchar('\n'); 1516 } 1517 1518 void 1519 ktrsysret(struct ktr_sysret *ktr, u_int sv_flags) 1520 { 1521 register_t ret = ktr->ktr_retval; 1522 int error = ktr->ktr_error; 1523 1524 syscallname(ktr->ktr_code, sv_flags); 1525 printf(" "); 1526 1527 if (error == 0) { 1528 if (fancy) { 1529 printf("%ld", (long)ret); 1530 if (ret < 0 || ret > 9) 1531 printf("/%#lx", (unsigned long)ret); 1532 } else { 1533 if (decimal) 1534 printf("%ld", (long)ret); 1535 else 1536 printf("%#lx", (unsigned long)ret); 1537 } 1538 } else if (error == ERESTART) 1539 printf("RESTART"); 1540 else if (error == EJUSTRETURN) 1541 printf("JUSTRETURN"); 1542 else { 1543 printf("-1 errno %d", sysdecode_freebsd_to_abi_errno( 1544 syscallabi(sv_flags), error)); 1545 if (fancy) 1546 printf(" %s", strerror(ktr->ktr_error)); 1547 } 1548 putchar('\n'); 1549 } 1550 1551 void 1552 ktrnamei(char *cp, int len) 1553 { 1554 printf("\"%.*s\"\n", len, cp); 1555 } 1556 1557 void 1558 hexdump(char *p, int len, int screenwidth) 1559 { 1560 int n, i; 1561 int width; 1562 1563 width = 0; 1564 do { 1565 width += 2; 1566 i = 13; /* base offset */ 1567 i += (width / 2) + 1; /* spaces every second byte */ 1568 i += (width * 2); /* width of bytes */ 1569 i += 3; /* " |" */ 1570 i += width; /* each byte */ 1571 i += 1; /* "|" */ 1572 } while (i < screenwidth); 1573 width -= 2; 1574 1575 for (n = 0; n < len; n += width) { 1576 for (i = n; i < n + width; i++) { 1577 if ((i % width) == 0) { /* beginning of line */ 1578 printf(" 0x%04x", i); 1579 } 1580 if ((i % 2) == 0) { 1581 printf(" "); 1582 } 1583 if (i < len) 1584 printf("%02x", p[i] & 0xff); 1585 else 1586 printf(" "); 1587 } 1588 printf(" |"); 1589 for (i = n; i < n + width; i++) { 1590 if (i >= len) 1591 break; 1592 if (p[i] >= ' ' && p[i] <= '~') 1593 printf("%c", p[i]); 1594 else 1595 printf("."); 1596 } 1597 printf("|\n"); 1598 } 1599 if ((i % width) != 0) 1600 printf("\n"); 1601 } 1602 1603 void 1604 visdump(char *dp, int datalen, int screenwidth) 1605 { 1606 int col = 0; 1607 char *cp; 1608 int width; 1609 char visbuf[5]; 1610 1611 printf(" \""); 1612 col = 8; 1613 for (;datalen > 0; datalen--, dp++) { 1614 vis(visbuf, *dp, VIS_CSTYLE, *(dp+1)); 1615 cp = visbuf; 1616 /* 1617 * Keep track of printables and 1618 * space chars (like fold(1)). 1619 */ 1620 if (col == 0) { 1621 putchar('\t'); 1622 col = 8; 1623 } 1624 switch(*cp) { 1625 case '\n': 1626 col = 0; 1627 putchar('\n'); 1628 continue; 1629 case '\t': 1630 width = 8 - (col&07); 1631 break; 1632 default: 1633 width = strlen(cp); 1634 } 1635 if (col + width > (screenwidth-2)) { 1636 printf("\\\n\t"); 1637 col = 8; 1638 } 1639 col += width; 1640 do { 1641 putchar(*cp++); 1642 } while (*cp); 1643 } 1644 if (col == 0) 1645 printf(" "); 1646 printf("\"\n"); 1647 } 1648 1649 void 1650 ktrgenio(struct ktr_genio *ktr, int len) 1651 { 1652 int datalen = len - sizeof (struct ktr_genio); 1653 char *dp = (char *)ktr + sizeof (struct ktr_genio); 1654 static int screenwidth = 0; 1655 int i, binary; 1656 1657 printf("fd %d %s %d byte%s\n", ktr->ktr_fd, 1658 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen, 1659 datalen == 1 ? "" : "s"); 1660 if (suppressdata) 1661 return; 1662 if (screenwidth == 0) { 1663 struct winsize ws; 1664 1665 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 && 1666 ws.ws_col > 8) 1667 screenwidth = ws.ws_col; 1668 else 1669 screenwidth = 80; 1670 } 1671 if (maxdata && datalen > maxdata) 1672 datalen = maxdata; 1673 1674 for (i = 0, binary = 0; i < datalen && binary == 0; i++) { 1675 if (dp[i] >= 32 && dp[i] < 127) 1676 continue; 1677 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9) 1678 continue; 1679 binary = 1; 1680 } 1681 if (binary) 1682 hexdump(dp, datalen, screenwidth); 1683 else 1684 visdump(dp, datalen, screenwidth); 1685 } 1686 1687 void 1688 ktrpsig(struct ktr_psig *psig) 1689 { 1690 const char *str; 1691 1692 print_signal(psig->signo); 1693 if (psig->action == SIG_DFL) { 1694 printf(" SIG_DFL"); 1695 } else { 1696 printf(" caught handler=0x%lx mask=0x%x", 1697 (u_long)psig->action, psig->mask.__bits[0]); 1698 } 1699 printf(" code="); 1700 str = sysdecode_sigcode(psig->signo, psig->code); 1701 if (str != NULL) 1702 printf("%s", str); 1703 else 1704 printf("<invalid=%#x>", psig->code); 1705 putchar('\n'); 1706 } 1707 1708 void 1709 ktrcsw_old(struct ktr_csw_old *cs) 1710 { 1711 printf("%s %s\n", cs->out ? "stop" : "resume", 1712 cs->user ? "user" : "kernel"); 1713 } 1714 1715 void 1716 ktrcsw(struct ktr_csw *cs) 1717 { 1718 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume", 1719 cs->user ? "user" : "kernel", cs->wmesg); 1720 } 1721 1722 void 1723 ktruser(int len, void *p) 1724 { 1725 unsigned char *cp; 1726 1727 if (sysdecode_utrace(stdout, p, len)) { 1728 printf("\n"); 1729 return; 1730 } 1731 1732 printf("%d ", len); 1733 cp = p; 1734 while (len--) 1735 if (decimal) 1736 printf(" %d", *cp++); 1737 else 1738 printf(" %02x", *cp++); 1739 printf("\n"); 1740 } 1741 1742 void 1743 ktrcaprights(cap_rights_t *rightsp) 1744 { 1745 1746 printf("cap_rights_t "); 1747 sysdecode_cap_rights(stdout, rightsp); 1748 printf("\n"); 1749 } 1750 1751 static void 1752 ktrtimeval(struct timeval *tv) 1753 { 1754 1755 printf("{%ld, %ld}", (long)tv->tv_sec, tv->tv_usec); 1756 } 1757 1758 void 1759 ktritimerval(struct itimerval *it) 1760 { 1761 1762 printf("itimerval { .interval = "); 1763 ktrtimeval(&it->it_interval); 1764 printf(", .value = "); 1765 ktrtimeval(&it->it_value); 1766 printf(" }\n"); 1767 } 1768 1769 void 1770 ktrsockaddr(struct sockaddr *sa) 1771 { 1772 /* 1773 TODO: Support additional address families 1774 #include <netsmb/netbios.h> 1775 struct sockaddr_nb *nb; 1776 */ 1777 const char *str; 1778 char addr[64]; 1779 1780 /* 1781 * note: ktrstruct() has already verified that sa points to a 1782 * buffer at least sizeof(struct sockaddr) bytes long and exactly 1783 * sa->sa_len bytes long. 1784 */ 1785 printf("struct sockaddr { "); 1786 str = sysdecode_sockaddr_family(sa->sa_family); 1787 if (str != NULL) 1788 printf("%s", str); 1789 else 1790 printf("<invalid=%d>", sa->sa_family); 1791 printf(", "); 1792 1793 #define check_sockaddr_len(n) \ 1794 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \ 1795 printf("invalid"); \ 1796 break; \ 1797 } 1798 1799 switch(sa->sa_family) { 1800 case AF_INET: { 1801 struct sockaddr_in sa_in; 1802 1803 memset(&sa_in, 0, sizeof(sa_in)); 1804 memcpy(&sa_in, sa, sa->sa_len); 1805 check_sockaddr_len(in); 1806 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr); 1807 printf("%s:%u", addr, ntohs(sa_in.sin_port)); 1808 break; 1809 } 1810 case AF_INET6: { 1811 struct sockaddr_in6 sa_in6; 1812 1813 memset(&sa_in6, 0, sizeof(sa_in6)); 1814 memcpy(&sa_in6, sa, sa->sa_len); 1815 check_sockaddr_len(in6); 1816 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6), 1817 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST); 1818 printf("[%s]:%u", addr, htons(sa_in6.sin6_port)); 1819 break; 1820 } 1821 case AF_UNIX: { 1822 struct sockaddr_un sa_un; 1823 1824 memset(&sa_un, 0, sizeof(sa_un)); 1825 memcpy(&sa_un, sa, sa->sa_len); 1826 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path); 1827 break; 1828 } 1829 default: 1830 printf("unknown address family"); 1831 } 1832 printf(" }\n"); 1833 } 1834 1835 void 1836 ktrstat(struct stat *statp) 1837 { 1838 char mode[12], timestr[PATH_MAX + 4]; 1839 struct passwd *pwd; 1840 struct group *grp; 1841 struct tm *tm; 1842 1843 /* 1844 * note: ktrstruct() has already verified that statp points to a 1845 * buffer exactly sizeof(struct stat) bytes long. 1846 */ 1847 printf("struct stat {"); 1848 printf("dev=%ju, ino=%ju, ", 1849 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino); 1850 if (resolv == 0) 1851 printf("mode=0%jo, ", (uintmax_t)statp->st_mode); 1852 else { 1853 strmode(statp->st_mode, mode); 1854 printf("mode=%s, ", mode); 1855 } 1856 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink); 1857 if (resolv == 0) { 1858 pwd = NULL; 1859 } else { 1860 #ifdef WITH_CASPER 1861 if (cappwd != NULL) 1862 pwd = cap_getpwuid(cappwd, statp->st_uid); 1863 else 1864 #endif 1865 pwd = getpwuid(statp->st_uid); 1866 } 1867 if (pwd == NULL) 1868 printf("uid=%ju, ", (uintmax_t)statp->st_uid); 1869 else 1870 printf("uid=\"%s\", ", pwd->pw_name); 1871 if (resolv == 0) { 1872 grp = NULL; 1873 } else { 1874 #ifdef WITH_CASPER 1875 if (capgrp != NULL) 1876 grp = cap_getgrgid(capgrp, statp->st_gid); 1877 else 1878 #endif 1879 grp = getgrgid(statp->st_gid); 1880 } 1881 if (grp == NULL) 1882 printf("gid=%ju, ", (uintmax_t)statp->st_gid); 1883 else 1884 printf("gid=\"%s\", ", grp->gr_name); 1885 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev); 1886 printf("atime="); 1887 if (resolv == 0) 1888 printf("%jd", (intmax_t)statp->st_atim.tv_sec); 1889 else { 1890 tm = localtime(&statp->st_atim.tv_sec); 1891 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm); 1892 printf("\"%s\"", timestr); 1893 } 1894 if (statp->st_atim.tv_nsec != 0) 1895 printf(".%09ld, ", statp->st_atim.tv_nsec); 1896 else 1897 printf(", "); 1898 printf("mtime="); 1899 if (resolv == 0) 1900 printf("%jd", (intmax_t)statp->st_mtim.tv_sec); 1901 else { 1902 tm = localtime(&statp->st_mtim.tv_sec); 1903 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm); 1904 printf("\"%s\"", timestr); 1905 } 1906 if (statp->st_mtim.tv_nsec != 0) 1907 printf(".%09ld, ", statp->st_mtim.tv_nsec); 1908 else 1909 printf(", "); 1910 printf("ctime="); 1911 if (resolv == 0) 1912 printf("%jd", (intmax_t)statp->st_ctim.tv_sec); 1913 else { 1914 tm = localtime(&statp->st_ctim.tv_sec); 1915 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm); 1916 printf("\"%s\"", timestr); 1917 } 1918 if (statp->st_ctim.tv_nsec != 0) 1919 printf(".%09ld, ", statp->st_ctim.tv_nsec); 1920 else 1921 printf(", "); 1922 printf("birthtime="); 1923 if (resolv == 0) 1924 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec); 1925 else { 1926 tm = localtime(&statp->st_birthtim.tv_sec); 1927 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm); 1928 printf("\"%s\"", timestr); 1929 } 1930 if (statp->st_birthtim.tv_nsec != 0) 1931 printf(".%09ld, ", statp->st_birthtim.tv_nsec); 1932 else 1933 printf(", "); 1934 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x", 1935 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize, 1936 (intmax_t)statp->st_blocks, statp->st_flags); 1937 printf(" }\n"); 1938 } 1939 1940 void 1941 ktrstruct(char *buf, size_t buflen) 1942 { 1943 char *name, *data; 1944 size_t namelen, datalen; 1945 int i; 1946 cap_rights_t rights; 1947 struct itimerval it; 1948 struct stat sb; 1949 struct sockaddr_storage ss; 1950 1951 for (name = buf, namelen = 0; 1952 namelen < buflen && name[namelen] != '\0'; 1953 ++namelen) 1954 /* nothing */; 1955 if (namelen == buflen) 1956 goto invalid; 1957 if (name[namelen] != '\0') 1958 goto invalid; 1959 data = buf + namelen + 1; 1960 datalen = buflen - namelen - 1; 1961 if (datalen == 0) 1962 goto invalid; 1963 /* sanity check */ 1964 for (i = 0; i < (int)namelen; ++i) 1965 if (!isalpha(name[i])) 1966 goto invalid; 1967 if (strcmp(name, "caprights") == 0) { 1968 if (datalen != sizeof(cap_rights_t)) 1969 goto invalid; 1970 memcpy(&rights, data, datalen); 1971 ktrcaprights(&rights); 1972 } else if (strcmp(name, "itimerval") == 0) { 1973 if (datalen != sizeof(struct itimerval)) 1974 goto invalid; 1975 memcpy(&it, data, datalen); 1976 ktritimerval(&it); 1977 } else if (strcmp(name, "stat") == 0) { 1978 if (datalen != sizeof(struct stat)) 1979 goto invalid; 1980 memcpy(&sb, data, datalen); 1981 ktrstat(&sb); 1982 } else if (strcmp(name, "sockaddr") == 0) { 1983 if (datalen > sizeof(ss)) 1984 goto invalid; 1985 memcpy(&ss, data, datalen); 1986 if (datalen != ss.ss_len) 1987 goto invalid; 1988 ktrsockaddr((struct sockaddr *)&ss); 1989 } else { 1990 printf("unknown structure\n"); 1991 } 1992 return; 1993 invalid: 1994 printf("invalid record\n"); 1995 } 1996 1997 void 1998 ktrcapfail(struct ktr_cap_fail *ktr) 1999 { 2000 switch (ktr->cap_type) { 2001 case CAPFAIL_NOTCAPABLE: 2002 /* operation on fd with insufficient capabilities */ 2003 printf("operation requires "); 2004 sysdecode_cap_rights(stdout, &ktr->cap_needed); 2005 printf(", descriptor holds "); 2006 sysdecode_cap_rights(stdout, &ktr->cap_held); 2007 break; 2008 case CAPFAIL_INCREASE: 2009 /* requested more capabilities than fd already has */ 2010 printf("attempt to increase capabilities from "); 2011 sysdecode_cap_rights(stdout, &ktr->cap_held); 2012 printf(" to "); 2013 sysdecode_cap_rights(stdout, &ktr->cap_needed); 2014 break; 2015 case CAPFAIL_SYSCALL: 2016 /* called restricted syscall */ 2017 printf("disallowed system call"); 2018 break; 2019 case CAPFAIL_LOOKUP: 2020 /* used ".." in strict-relative mode */ 2021 printf("restricted VFS lookup"); 2022 break; 2023 default: 2024 printf("unknown capability failure: "); 2025 sysdecode_cap_rights(stdout, &ktr->cap_needed); 2026 printf(" "); 2027 sysdecode_cap_rights(stdout, &ktr->cap_held); 2028 break; 2029 } 2030 printf("\n"); 2031 } 2032 2033 void 2034 ktrfault(struct ktr_fault *ktr) 2035 { 2036 2037 printf("0x%jx ", (uintmax_t)ktr->vaddr); 2038 print_mask_arg(sysdecode_vmprot, ktr->type); 2039 printf("\n"); 2040 } 2041 2042 void 2043 ktrfaultend(struct ktr_faultend *ktr) 2044 { 2045 const char *str; 2046 2047 str = sysdecode_vmresult(ktr->result); 2048 if (str != NULL) 2049 printf("%s", str); 2050 else 2051 printf("<invalid=%d>", ktr->result); 2052 printf("\n"); 2053 } 2054 2055 void 2056 ktrkevent(struct kevent *kev) 2057 { 2058 2059 printf("{ ident="); 2060 switch (kev->filter) { 2061 case EVFILT_READ: 2062 case EVFILT_WRITE: 2063 case EVFILT_VNODE: 2064 case EVFILT_PROC: 2065 case EVFILT_TIMER: 2066 case EVFILT_PROCDESC: 2067 case EVFILT_EMPTY: 2068 printf("%ju", (uintmax_t)kev->ident); 2069 break; 2070 case EVFILT_SIGNAL: 2071 print_signal(kev->ident); 2072 break; 2073 default: 2074 printf("%p", (void *)kev->ident); 2075 } 2076 printf(", filter="); 2077 print_integer_arg(sysdecode_kevent_filter, kev->filter); 2078 printf(", flags="); 2079 print_mask_arg0(sysdecode_kevent_flags, kev->flags); 2080 printf(", fflags="); 2081 sysdecode_kevent_fflags(stdout, kev->filter, kev->fflags, 2082 decimal ? 10 : 16); 2083 printf(", data=%#jx, udata=%p }", (uintmax_t)kev->data, kev->udata); 2084 } 2085 2086 void 2087 ktrstructarray(struct ktr_struct_array *ksa, size_t buflen) 2088 { 2089 struct kevent kev; 2090 char *name, *data; 2091 size_t namelen, datalen; 2092 int i; 2093 bool first; 2094 2095 buflen -= sizeof(*ksa); 2096 for (name = (char *)(ksa + 1), namelen = 0; 2097 namelen < buflen && name[namelen] != '\0'; 2098 ++namelen) 2099 /* nothing */; 2100 if (namelen == buflen) 2101 goto invalid; 2102 if (name[namelen] != '\0') 2103 goto invalid; 2104 /* sanity check */ 2105 for (i = 0; i < (int)namelen; ++i) 2106 if (!isalnum(name[i]) && name[i] != '_') 2107 goto invalid; 2108 data = name + namelen + 1; 2109 datalen = buflen - namelen - 1; 2110 printf("struct %s[] = { ", name); 2111 first = true; 2112 for (; datalen >= ksa->struct_size; 2113 data += ksa->struct_size, datalen -= ksa->struct_size) { 2114 if (!first) 2115 printf("\n "); 2116 else 2117 first = false; 2118 if (strcmp(name, "kevent") == 0) { 2119 if (ksa->struct_size != sizeof(kev)) 2120 goto bad_size; 2121 memcpy(&kev, data, sizeof(kev)); 2122 ktrkevent(&kev); 2123 } else if (strcmp(name, "kevent_freebsd11") == 0) { 2124 struct kevent_freebsd11 kev11; 2125 2126 if (ksa->struct_size != sizeof(kev11)) 2127 goto bad_size; 2128 memcpy(&kev11, data, sizeof(kev11)); 2129 memset(&kev, 0, sizeof(kev)); 2130 kev.ident = kev11.ident; 2131 kev.filter = kev11.filter; 2132 kev.flags = kev11.flags; 2133 kev.fflags = kev11.fflags; 2134 kev.data = kev11.data; 2135 kev.udata = kev11.udata; 2136 ktrkevent(&kev); 2137 #ifdef _WANT_KEVENT32 2138 } else if (strcmp(name, "kevent32") == 0) { 2139 struct kevent32 kev32; 2140 2141 if (ksa->struct_size != sizeof(kev32)) 2142 goto bad_size; 2143 memcpy(&kev32, data, sizeof(kev32)); 2144 memset(&kev, 0, sizeof(kev)); 2145 kev.ident = kev32.ident; 2146 kev.filter = kev32.filter; 2147 kev.flags = kev32.flags; 2148 kev.fflags = kev32.fflags; 2149 #if BYTE_ORDER == BIG_ENDIAN 2150 kev.data = kev32.data2 | ((int64_t)kev32.data1 << 32); 2151 #else 2152 kev.data = kev32.data1 | ((int64_t)kev32.data2 << 32); 2153 #endif 2154 kev.udata = (void *)(uintptr_t)kev32.udata; 2155 ktrkevent(&kev); 2156 } else if (strcmp(name, "kevent32_freebsd11") == 0) { 2157 struct kevent32_freebsd11 kev32; 2158 2159 if (ksa->struct_size != sizeof(kev32)) 2160 goto bad_size; 2161 memcpy(&kev32, data, sizeof(kev32)); 2162 memset(&kev, 0, sizeof(kev)); 2163 kev.ident = kev32.ident; 2164 kev.filter = kev32.filter; 2165 kev.flags = kev32.flags; 2166 kev.fflags = kev32.fflags; 2167 kev.data = kev32.data; 2168 kev.udata = (void *)(uintptr_t)kev32.udata; 2169 ktrkevent(&kev); 2170 #endif 2171 } else { 2172 printf("<unknown structure> }\n"); 2173 return; 2174 } 2175 } 2176 printf(" }\n"); 2177 return; 2178 invalid: 2179 printf("invalid record\n"); 2180 return; 2181 bad_size: 2182 printf("<bad size> }\n"); 2183 return; 2184 } 2185 2186 void 2187 usage(void) 2188 { 2189 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] " 2190 "[-m maxdata] [-p pid] [-t trstr]\n"); 2191 exit(1); 2192 } 2193