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