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