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