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 default: 715 abi = "U"; 716 break; 717 } 718 719 if ((sv_flags & SV_LP64) != 0) 720 arch = "64"; 721 else if ((sv_flags & SV_ILP32) != 0) 722 arch = "32"; 723 else 724 arch = "00"; 725 726 printf("%s%s ", abi, arch); 727 } 728 } 729 730 #include <sys/syscall.h> 731 732 static void 733 ioctlname(unsigned long val) 734 { 735 const char *str; 736 737 str = sysdecode_ioctlname(val); 738 if (str != NULL) 739 printf("%s", str); 740 else if (decimal) 741 printf("%lu", val); 742 else 743 printf("%#lx", val); 744 } 745 746 static enum sysdecode_abi 747 syscallabi(u_int sv_flags) 748 { 749 750 if (sv_flags == 0) 751 return (SYSDECODE_ABI_FREEBSD); 752 switch (sv_flags & SV_ABI_MASK) { 753 case SV_ABI_FREEBSD: 754 return (SYSDECODE_ABI_FREEBSD); 755 case SV_ABI_LINUX: 756 #ifdef __LP64__ 757 if (sv_flags & SV_ILP32) 758 return (SYSDECODE_ABI_LINUX32); 759 #endif 760 return (SYSDECODE_ABI_LINUX); 761 default: 762 return (SYSDECODE_ABI_UNKNOWN); 763 } 764 } 765 766 static void 767 syscallname(u_int code, u_int sv_flags) 768 { 769 const char *name; 770 771 name = sysdecode_syscallname(syscallabi(sv_flags), code); 772 if (name == NULL) 773 printf("[%d]", code); 774 else { 775 printf("%s", name); 776 if (syscallno) 777 printf("[%d]", code); 778 } 779 } 780 781 static void 782 print_signal(int signo) 783 { 784 const char *signame; 785 786 signame = sysdecode_signal(signo); 787 if (signame != NULL) 788 printf("%s", signame); 789 else 790 printf("SIG %d", signo); 791 } 792 793 void 794 ktrsyscall(struct ktr_syscall *ktr, u_int sv_flags) 795 { 796 int narg = ktr->ktr_narg; 797 register_t *ip, *first; 798 intmax_t arg; 799 int quad_align, quad_slots; 800 801 syscallname(ktr->ktr_code, sv_flags); 802 ip = first = &ktr->ktr_args[0]; 803 if (narg) { 804 char c = '('; 805 if (fancy && 806 (sv_flags == 0 || 807 (sv_flags & SV_ABI_MASK) == SV_ABI_FREEBSD)) { 808 quad_align = 0; 809 if (sv_flags & SV_ILP32) { 810 #ifdef __powerpc__ 811 quad_align = 1; 812 #endif 813 quad_slots = 2; 814 } else 815 quad_slots = 1; 816 switch (ktr->ktr_code) { 817 case SYS_bindat: 818 case SYS_chflagsat: 819 case SYS_connectat: 820 case SYS_faccessat: 821 case SYS_fchmodat: 822 case SYS_fchownat: 823 case SYS_fstatat: 824 case SYS_futimesat: 825 case SYS_linkat: 826 case SYS_mkdirat: 827 case SYS_mkfifoat: 828 case SYS_mknodat: 829 case SYS_openat: 830 case SYS_readlinkat: 831 case SYS_renameat: 832 case SYS_unlinkat: 833 case SYS_utimensat: 834 putchar('('); 835 print_integer_arg_valid(sysdecode_atfd, *ip); 836 c = ','; 837 ip++; 838 narg--; 839 break; 840 } 841 switch (ktr->ktr_code) { 842 case SYS_ioctl: { 843 print_number(ip, narg, c); 844 putchar(c); 845 ioctlname(*ip); 846 c = ','; 847 ip++; 848 narg--; 849 break; 850 } 851 case SYS_ptrace: 852 putchar('('); 853 print_integer_arg(sysdecode_ptrace_request, *ip); 854 c = ','; 855 ip++; 856 narg--; 857 break; 858 case SYS_access: 859 case SYS_eaccess: 860 case SYS_faccessat: 861 print_number(ip, narg, c); 862 putchar(','); 863 print_mask_arg(sysdecode_access_mode, *ip); 864 ip++; 865 narg--; 866 break; 867 case SYS_open: 868 case SYS_openat: 869 print_number(ip, narg, c); 870 putchar(','); 871 print_mask_arg(sysdecode_open_flags, ip[0]); 872 if ((ip[0] & O_CREAT) == O_CREAT) { 873 putchar(','); 874 decode_filemode(ip[1]); 875 } 876 ip += 2; 877 narg -= 2; 878 break; 879 case SYS_wait4: 880 print_number(ip, narg, c); 881 print_number(ip, narg, c); 882 putchar(','); 883 print_mask_arg0(sysdecode_wait4_options, *ip); 884 ip++; 885 narg--; 886 break; 887 case SYS_wait6: 888 putchar('('); 889 print_integer_arg(sysdecode_idtype, *ip); 890 c = ','; 891 ip++; 892 narg--; 893 print_number64(first, ip, narg, c); 894 print_number(ip, narg, c); 895 putchar(','); 896 print_mask_arg(sysdecode_wait6_options, *ip); 897 ip++; 898 narg--; 899 break; 900 case SYS_chmod: 901 case SYS_fchmod: 902 case SYS_lchmod: 903 case SYS_fchmodat: 904 print_number(ip, narg, c); 905 putchar(','); 906 decode_filemode(*ip); 907 ip++; 908 narg--; 909 break; 910 case SYS_mknodat: 911 print_number(ip, narg, c); 912 putchar(','); 913 decode_filemode(*ip); 914 ip++; 915 narg--; 916 break; 917 case SYS_getfsstat: 918 print_number(ip, narg, c); 919 print_number(ip, narg, c); 920 putchar(','); 921 print_integer_arg(sysdecode_getfsstat_mode, *ip); 922 ip++; 923 narg--; 924 break; 925 case SYS_mount: 926 print_number(ip, narg, c); 927 print_number(ip, narg, c); 928 putchar(','); 929 print_mask_arg(sysdecode_mount_flags, *ip); 930 ip++; 931 narg--; 932 break; 933 case SYS_unmount: 934 print_number(ip, narg, c); 935 putchar(','); 936 print_mask_arg(sysdecode_mount_flags, *ip); 937 ip++; 938 narg--; 939 break; 940 case SYS_recvmsg: 941 case SYS_sendmsg: 942 print_number(ip, narg, c); 943 print_number(ip, narg, c); 944 putchar(','); 945 print_mask_arg0(sysdecode_msg_flags, *ip); 946 ip++; 947 narg--; 948 break; 949 case SYS_recvfrom: 950 case SYS_sendto: 951 print_number(ip, narg, c); 952 print_number(ip, narg, c); 953 print_number(ip, narg, c); 954 putchar(','); 955 print_mask_arg0(sysdecode_msg_flags, *ip); 956 ip++; 957 narg--; 958 break; 959 case SYS_chflags: 960 case SYS_chflagsat: 961 case SYS_fchflags: 962 case SYS_lchflags: 963 print_number(ip, narg, c); 964 putchar(','); 965 decode_fileflags(*ip); 966 ip++; 967 narg--; 968 break; 969 case SYS_kill: 970 print_number(ip, narg, c); 971 putchar(','); 972 print_signal(*ip); 973 ip++; 974 narg--; 975 break; 976 case SYS_reboot: 977 putchar('('); 978 print_mask_arg(sysdecode_reboot_howto, *ip); 979 ip++; 980 narg--; 981 break; 982 case SYS_umask: 983 putchar('('); 984 decode_filemode(*ip); 985 ip++; 986 narg--; 987 break; 988 case SYS_msync: 989 print_number(ip, narg, c); 990 print_number(ip, narg, c); 991 putchar(','); 992 print_mask_arg(sysdecode_msync_flags, *ip); 993 ip++; 994 narg--; 995 break; 996 #ifdef SYS_freebsd6_mmap 997 case SYS_freebsd6_mmap: 998 print_number(ip, narg, c); 999 print_number(ip, narg, c); 1000 putchar(','); 1001 print_mask_arg(sysdecode_mmap_prot, *ip); 1002 putchar(','); 1003 ip++; 1004 narg--; 1005 print_mask_arg(sysdecode_mmap_flags, *ip); 1006 ip++; 1007 narg--; 1008 break; 1009 #endif 1010 case SYS_mmap: 1011 print_number(ip, narg, c); 1012 print_number(ip, narg, c); 1013 putchar(','); 1014 print_mask_arg(sysdecode_mmap_prot, *ip); 1015 putchar(','); 1016 ip++; 1017 narg--; 1018 print_mask_arg(sysdecode_mmap_flags, *ip); 1019 ip++; 1020 narg--; 1021 break; 1022 case SYS_mprotect: 1023 print_number(ip, narg, c); 1024 print_number(ip, narg, c); 1025 putchar(','); 1026 print_mask_arg(sysdecode_mmap_prot, *ip); 1027 ip++; 1028 narg--; 1029 break; 1030 case SYS_madvise: 1031 print_number(ip, narg, c); 1032 print_number(ip, narg, c); 1033 putchar(','); 1034 print_integer_arg(sysdecode_madvice, *ip); 1035 ip++; 1036 narg--; 1037 break; 1038 case SYS_pathconf: 1039 case SYS_lpathconf: 1040 case SYS_fpathconf: 1041 print_number(ip, narg, c); 1042 putchar(','); 1043 print_integer_arg(sysdecode_pathconf_name, *ip); 1044 ip++; 1045 narg--; 1046 break; 1047 case SYS_getpriority: 1048 case SYS_setpriority: 1049 putchar('('); 1050 print_integer_arg(sysdecode_prio_which, *ip); 1051 c = ','; 1052 ip++; 1053 narg--; 1054 break; 1055 case SYS_fcntl: 1056 print_number(ip, narg, c); 1057 putchar(','); 1058 print_integer_arg(sysdecode_fcntl_cmd, ip[0]); 1059 if (sysdecode_fcntl_arg_p(ip[0])) { 1060 putchar(','); 1061 if (ip[0] == F_SETFL) 1062 print_mask_arg( 1063 sysdecode_fcntl_fileflags, 1064 ip[1]); 1065 else 1066 sysdecode_fcntl_arg(stdout, 1067 ip[0], ip[1], 1068 decimal ? 10 : 16); 1069 } 1070 ip += 2; 1071 narg -= 2; 1072 break; 1073 case SYS_socket: { 1074 int sockdomain; 1075 putchar('('); 1076 sockdomain = *ip; 1077 print_integer_arg(sysdecode_socketdomain, 1078 sockdomain); 1079 ip++; 1080 narg--; 1081 putchar(','); 1082 print_mask_arg(sysdecode_socket_type, *ip); 1083 ip++; 1084 narg--; 1085 if (sockdomain == PF_INET || 1086 sockdomain == PF_INET6) { 1087 putchar(','); 1088 print_integer_arg(sysdecode_ipproto, 1089 *ip); 1090 ip++; 1091 narg--; 1092 } 1093 c = ','; 1094 break; 1095 } 1096 case SYS_setsockopt: 1097 case SYS_getsockopt: { 1098 const char *str; 1099 1100 print_number(ip, narg, c); 1101 putchar(','); 1102 print_integer_arg_valid(sysdecode_sockopt_level, 1103 *ip); 1104 str = sysdecode_sockopt_name(ip[0], ip[1]); 1105 if (str != NULL) { 1106 printf(",%s", str); 1107 ip++; 1108 narg--; 1109 } 1110 ip++; 1111 narg--; 1112 break; 1113 } 1114 #ifdef SYS_freebsd6_lseek 1115 case SYS_freebsd6_lseek: 1116 print_number(ip, narg, c); 1117 /* Hidden 'pad' argument, not in lseek(2) */ 1118 print_number(ip, narg, c); 1119 print_number64(first, ip, narg, c); 1120 putchar(','); 1121 print_integer_arg(sysdecode_whence, *ip); 1122 ip++; 1123 narg--; 1124 break; 1125 #endif 1126 case SYS_lseek: 1127 print_number(ip, narg, c); 1128 print_number64(first, ip, narg, c); 1129 putchar(','); 1130 print_integer_arg(sysdecode_whence, *ip); 1131 ip++; 1132 narg--; 1133 break; 1134 case SYS_flock: 1135 print_number(ip, narg, c); 1136 putchar(','); 1137 print_mask_arg(sysdecode_flock_operation, *ip); 1138 ip++; 1139 narg--; 1140 break; 1141 case SYS_mkfifo: 1142 case SYS_mkfifoat: 1143 case SYS_mkdir: 1144 case SYS_mkdirat: 1145 print_number(ip, narg, c); 1146 putchar(','); 1147 decode_filemode(*ip); 1148 ip++; 1149 narg--; 1150 break; 1151 case SYS_shutdown: 1152 print_number(ip, narg, c); 1153 putchar(','); 1154 print_integer_arg(sysdecode_shutdown_how, *ip); 1155 ip++; 1156 narg--; 1157 break; 1158 case SYS_socketpair: 1159 putchar('('); 1160 print_integer_arg(sysdecode_socketdomain, *ip); 1161 ip++; 1162 narg--; 1163 putchar(','); 1164 print_mask_arg(sysdecode_socket_type, *ip); 1165 ip++; 1166 narg--; 1167 c = ','; 1168 break; 1169 case SYS_getrlimit: 1170 case SYS_setrlimit: 1171 putchar('('); 1172 print_integer_arg(sysdecode_rlimit, *ip); 1173 ip++; 1174 narg--; 1175 c = ','; 1176 break; 1177 case SYS_getrusage: 1178 putchar('('); 1179 print_integer_arg(sysdecode_getrusage_who, *ip); 1180 ip++; 1181 narg--; 1182 c = ','; 1183 break; 1184 case SYS_quotactl: 1185 print_number(ip, narg, c); 1186 putchar(','); 1187 if (!sysdecode_quotactl_cmd(stdout, *ip)) { 1188 if (decimal) 1189 printf("<invalid=%d>", (int)*ip); 1190 else 1191 printf("<invalid=%#x>", 1192 (int)*ip); 1193 } 1194 ip++; 1195 narg--; 1196 c = ','; 1197 break; 1198 case SYS_nfssvc: 1199 putchar('('); 1200 print_integer_arg(sysdecode_nfssvc_flags, *ip); 1201 ip++; 1202 narg--; 1203 c = ','; 1204 break; 1205 case SYS_rtprio: 1206 case SYS_rtprio_thread: 1207 putchar('('); 1208 print_integer_arg(sysdecode_rtprio_function, 1209 *ip); 1210 ip++; 1211 narg--; 1212 c = ','; 1213 break; 1214 case SYS___semctl: 1215 print_number(ip, narg, c); 1216 print_number(ip, narg, c); 1217 putchar(','); 1218 print_integer_arg(sysdecode_semctl_cmd, *ip); 1219 ip++; 1220 narg--; 1221 break; 1222 case SYS_semget: 1223 print_number(ip, narg, c); 1224 print_number(ip, narg, c); 1225 putchar(','); 1226 print_mask_arg(sysdecode_semget_flags, *ip); 1227 ip++; 1228 narg--; 1229 break; 1230 case SYS_msgctl: 1231 print_number(ip, narg, c); 1232 putchar(','); 1233 print_integer_arg(sysdecode_msgctl_cmd, *ip); 1234 ip++; 1235 narg--; 1236 break; 1237 case SYS_shmat: 1238 print_number(ip, narg, c); 1239 print_number(ip, narg, c); 1240 putchar(','); 1241 print_mask_arg(sysdecode_shmat_flags, *ip); 1242 ip++; 1243 narg--; 1244 break; 1245 case SYS_shmctl: 1246 print_number(ip, narg, c); 1247 putchar(','); 1248 print_integer_arg(sysdecode_shmctl_cmd, *ip); 1249 ip++; 1250 narg--; 1251 break; 1252 #ifdef SYS_freebsd12_shm_open 1253 case SYS_freebsd12_shm_open: 1254 if (ip[0] == (uintptr_t)SHM_ANON) { 1255 printf("(SHM_ANON"); 1256 ip++; 1257 } else { 1258 print_number(ip, narg, c); 1259 } 1260 putchar(','); 1261 print_mask_arg(sysdecode_open_flags, ip[0]); 1262 putchar(','); 1263 decode_filemode(ip[1]); 1264 ip += 2; 1265 narg -= 2; 1266 break; 1267 #endif 1268 case SYS_shm_open2: 1269 if (ip[0] == (uintptr_t)SHM_ANON) { 1270 printf("(SHM_ANON"); 1271 ip++; 1272 } else { 1273 print_number(ip, narg, c); 1274 } 1275 putchar(','); 1276 print_mask_arg(sysdecode_open_flags, ip[0]); 1277 putchar(','); 1278 decode_filemode(ip[1]); 1279 putchar(','); 1280 print_mask_arg(sysdecode_shmflags, ip[2]); 1281 ip += 3; 1282 narg -= 3; 1283 break; 1284 case SYS_minherit: 1285 print_number(ip, narg, c); 1286 print_number(ip, narg, c); 1287 putchar(','); 1288 print_integer_arg(sysdecode_minherit_inherit, 1289 *ip); 1290 ip++; 1291 narg--; 1292 break; 1293 case SYS_rfork: 1294 putchar('('); 1295 print_mask_arg(sysdecode_rfork_flags, *ip); 1296 ip++; 1297 narg--; 1298 c = ','; 1299 break; 1300 case SYS_lio_listio: 1301 putchar('('); 1302 print_integer_arg(sysdecode_lio_listio_mode, 1303 *ip); 1304 ip++; 1305 narg--; 1306 c = ','; 1307 break; 1308 case SYS_mlockall: 1309 putchar('('); 1310 print_mask_arg(sysdecode_mlockall_flags, *ip); 1311 ip++; 1312 narg--; 1313 break; 1314 case SYS_sched_setscheduler: 1315 print_number(ip, narg, c); 1316 putchar(','); 1317 print_integer_arg(sysdecode_scheduler_policy, 1318 *ip); 1319 ip++; 1320 narg--; 1321 break; 1322 case SYS_sched_get_priority_max: 1323 case SYS_sched_get_priority_min: 1324 putchar('('); 1325 print_integer_arg(sysdecode_scheduler_policy, 1326 *ip); 1327 ip++; 1328 narg--; 1329 break; 1330 case SYS_sendfile: 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 print_number(ip, narg, c); 1336 print_number(ip, narg, c); 1337 putchar(','); 1338 print_mask_arg(sysdecode_sendfile_flags, *ip); 1339 ip++; 1340 narg--; 1341 break; 1342 case SYS_kldsym: 1343 print_number(ip, narg, c); 1344 putchar(','); 1345 print_integer_arg(sysdecode_kldsym_cmd, *ip); 1346 ip++; 1347 narg--; 1348 break; 1349 case SYS_sigprocmask: 1350 putchar('('); 1351 print_integer_arg(sysdecode_sigprocmask_how, 1352 *ip); 1353 ip++; 1354 narg--; 1355 c = ','; 1356 break; 1357 case SYS___acl_get_file: 1358 case SYS___acl_set_file: 1359 case SYS___acl_get_fd: 1360 case SYS___acl_set_fd: 1361 case SYS___acl_delete_file: 1362 case SYS___acl_delete_fd: 1363 case SYS___acl_aclcheck_file: 1364 case SYS___acl_aclcheck_fd: 1365 case SYS___acl_get_link: 1366 case SYS___acl_set_link: 1367 case SYS___acl_delete_link: 1368 case SYS___acl_aclcheck_link: 1369 print_number(ip, narg, c); 1370 putchar(','); 1371 print_integer_arg(sysdecode_acltype, *ip); 1372 ip++; 1373 narg--; 1374 break; 1375 case SYS_sigaction: 1376 putchar('('); 1377 print_signal(*ip); 1378 ip++; 1379 narg--; 1380 c = ','; 1381 break; 1382 case SYS_extattrctl: 1383 print_number(ip, narg, c); 1384 putchar(','); 1385 print_integer_arg(sysdecode_extattrnamespace, 1386 *ip); 1387 ip++; 1388 narg--; 1389 break; 1390 case SYS_nmount: 1391 print_number(ip, narg, c); 1392 print_number(ip, narg, c); 1393 putchar(','); 1394 print_mask_arg(sysdecode_mount_flags, *ip); 1395 ip++; 1396 narg--; 1397 break; 1398 case SYS_thr_create: 1399 print_number(ip, narg, c); 1400 print_number(ip, narg, c); 1401 putchar(','); 1402 print_mask_arg(sysdecode_thr_create_flags, *ip); 1403 ip++; 1404 narg--; 1405 break; 1406 case SYS_thr_kill: 1407 print_number(ip, narg, c); 1408 putchar(','); 1409 print_signal(*ip); 1410 ip++; 1411 narg--; 1412 break; 1413 case SYS_kldunloadf: 1414 print_number(ip, narg, c); 1415 putchar(','); 1416 print_integer_arg(sysdecode_kldunload_flags, 1417 *ip); 1418 ip++; 1419 narg--; 1420 break; 1421 case SYS_linkat: 1422 case SYS_renameat: 1423 case SYS_symlinkat: 1424 print_number(ip, narg, c); 1425 putchar(','); 1426 print_integer_arg_valid(sysdecode_atfd, *ip); 1427 ip++; 1428 narg--; 1429 print_number(ip, narg, c); 1430 break; 1431 case SYS_cap_fcntls_limit: 1432 print_number(ip, narg, c); 1433 putchar(','); 1434 arg = *ip; 1435 ip++; 1436 narg--; 1437 print_mask_arg32(sysdecode_cap_fcntlrights, arg); 1438 break; 1439 case SYS_posix_fadvise: 1440 print_number(ip, narg, c); 1441 print_number(ip, narg, c); 1442 print_number(ip, narg, c); 1443 (void)putchar(','); 1444 print_integer_arg(sysdecode_fadvice, *ip); 1445 ip++; 1446 narg--; 1447 break; 1448 case SYS_procctl: 1449 putchar('('); 1450 print_integer_arg(sysdecode_idtype, *ip); 1451 c = ','; 1452 ip++; 1453 narg--; 1454 print_number64(first, ip, narg, c); 1455 putchar(','); 1456 print_integer_arg(sysdecode_procctl_cmd, *ip); 1457 ip++; 1458 narg--; 1459 break; 1460 case SYS__umtx_op: { 1461 int op; 1462 1463 print_number(ip, narg, c); 1464 putchar(','); 1465 if (print_mask_arg_part(sysdecode_umtx_op_flags, 1466 *ip, &op)) 1467 putchar('|'); 1468 print_integer_arg(sysdecode_umtx_op, op); 1469 putchar('>'); 1470 switch (*ip) { 1471 case UMTX_OP_CV_WAIT: 1472 ip++; 1473 narg--; 1474 putchar(','); 1475 print_mask_argul( 1476 sysdecode_umtx_cvwait_flags, *ip); 1477 break; 1478 case UMTX_OP_RW_RDLOCK: 1479 ip++; 1480 narg--; 1481 putchar(','); 1482 print_mask_argul( 1483 sysdecode_umtx_rwlock_flags, *ip); 1484 break; 1485 } 1486 ip++; 1487 narg--; 1488 break; 1489 } 1490 case SYS_ftruncate: 1491 case SYS_truncate: 1492 print_number(ip, narg, c); 1493 print_number64(first, ip, narg, c); 1494 break; 1495 case SYS_fchownat: 1496 print_number(ip, narg, c); 1497 print_number(ip, narg, c); 1498 print_number(ip, narg, c); 1499 break; 1500 case SYS_fstatat: 1501 case SYS_utimensat: 1502 print_number(ip, narg, c); 1503 print_number(ip, narg, c); 1504 break; 1505 case SYS_unlinkat: 1506 print_number(ip, narg, c); 1507 break; 1508 case SYS_sysarch: 1509 putchar('('); 1510 print_integer_arg(sysdecode_sysarch_number, *ip); 1511 ip++; 1512 narg--; 1513 c = ','; 1514 break; 1515 } 1516 switch (ktr->ktr_code) { 1517 case SYS_chflagsat: 1518 case SYS_fchownat: 1519 case SYS_faccessat: 1520 case SYS_fchmodat: 1521 case SYS_fstatat: 1522 case SYS_linkat: 1523 case SYS_unlinkat: 1524 case SYS_utimensat: 1525 putchar(','); 1526 print_mask_arg0(sysdecode_atflags, *ip); 1527 ip++; 1528 narg--; 1529 break; 1530 } 1531 } 1532 while (narg > 0) { 1533 print_number(ip, narg, c); 1534 } 1535 putchar(')'); 1536 } 1537 putchar('\n'); 1538 } 1539 1540 void 1541 ktrsysret(struct ktr_sysret *ktr, u_int sv_flags) 1542 { 1543 register_t ret = ktr->ktr_retval; 1544 int error = ktr->ktr_error; 1545 1546 syscallname(ktr->ktr_code, sv_flags); 1547 printf(" "); 1548 1549 if (error == 0) { 1550 if (fancy) { 1551 printf("%ld", (long)ret); 1552 if (ret < 0 || ret > 9) 1553 printf("/%#lx", (unsigned long)ret); 1554 } else { 1555 if (decimal) 1556 printf("%ld", (long)ret); 1557 else 1558 printf("%#lx", (unsigned long)ret); 1559 } 1560 } else if (error == ERESTART) 1561 printf("RESTART"); 1562 else if (error == EJUSTRETURN) 1563 printf("JUSTRETURN"); 1564 else { 1565 printf("-1 errno %d", sysdecode_freebsd_to_abi_errno( 1566 syscallabi(sv_flags), error)); 1567 if (fancy) 1568 printf(" %s", strerror(ktr->ktr_error)); 1569 } 1570 putchar('\n'); 1571 } 1572 1573 void 1574 ktrnamei(char *cp, int len) 1575 { 1576 printf("\"%.*s\"\n", len, cp); 1577 } 1578 1579 void 1580 hexdump(char *p, int len, int screenwidth) 1581 { 1582 int n, i; 1583 int width; 1584 1585 width = 0; 1586 do { 1587 width += 2; 1588 i = 13; /* base offset */ 1589 i += (width / 2) + 1; /* spaces every second byte */ 1590 i += (width * 2); /* width of bytes */ 1591 i += 3; /* " |" */ 1592 i += width; /* each byte */ 1593 i += 1; /* "|" */ 1594 } while (i < screenwidth); 1595 width -= 2; 1596 1597 for (n = 0; n < len; n += width) { 1598 for (i = n; i < n + width; i++) { 1599 if ((i % width) == 0) { /* beginning of line */ 1600 printf(" 0x%04x", i); 1601 } 1602 if ((i % 2) == 0) { 1603 printf(" "); 1604 } 1605 if (i < len) 1606 printf("%02x", p[i] & 0xff); 1607 else 1608 printf(" "); 1609 } 1610 printf(" |"); 1611 for (i = n; i < n + width; i++) { 1612 if (i >= len) 1613 break; 1614 if (p[i] >= ' ' && p[i] <= '~') 1615 printf("%c", p[i]); 1616 else 1617 printf("."); 1618 } 1619 printf("|\n"); 1620 } 1621 if ((i % width) != 0) 1622 printf("\n"); 1623 } 1624 1625 void 1626 visdump(char *dp, int datalen, int screenwidth) 1627 { 1628 int col = 0; 1629 char *cp; 1630 int width; 1631 char visbuf[5]; 1632 1633 printf(" \""); 1634 col = 8; 1635 for (;datalen > 0; datalen--, dp++) { 1636 vis(visbuf, *dp, VIS_CSTYLE | VIS_NOLOCALE, *(dp+1)); 1637 cp = visbuf; 1638 /* 1639 * Keep track of printables and 1640 * space chars (like fold(1)). 1641 */ 1642 if (col == 0) { 1643 putchar('\t'); 1644 col = 8; 1645 } 1646 switch(*cp) { 1647 case '\n': 1648 col = 0; 1649 putchar('\n'); 1650 continue; 1651 case '\t': 1652 width = 8 - (col&07); 1653 break; 1654 default: 1655 width = strlen(cp); 1656 } 1657 if (col + width > (screenwidth-2)) { 1658 printf("\\\n\t"); 1659 col = 8; 1660 } 1661 col += width; 1662 do { 1663 putchar(*cp++); 1664 } while (*cp); 1665 } 1666 if (col == 0) 1667 printf(" "); 1668 printf("\"\n"); 1669 } 1670 1671 void 1672 ktrgenio(struct ktr_genio *ktr, int len) 1673 { 1674 int datalen = len - sizeof (struct ktr_genio); 1675 char *dp = (char *)ktr + sizeof (struct ktr_genio); 1676 static int screenwidth = 0; 1677 int i, binary; 1678 1679 printf("fd %d %s %d byte%s\n", ktr->ktr_fd, 1680 ktr->ktr_rw == UIO_READ ? "read" : "wrote", datalen, 1681 datalen == 1 ? "" : "s"); 1682 if (suppressdata) 1683 return; 1684 if (screenwidth == 0) { 1685 struct winsize ws; 1686 1687 if (fancy && ioctl(fileno(stderr), TIOCGWINSZ, &ws) != -1 && 1688 ws.ws_col > 8) 1689 screenwidth = ws.ws_col; 1690 else 1691 screenwidth = 80; 1692 } 1693 if (maxdata && datalen > maxdata) 1694 datalen = maxdata; 1695 1696 for (i = 0, binary = 0; i < datalen && binary == 0; i++) { 1697 if (dp[i] >= 32 && dp[i] < 127) 1698 continue; 1699 if (dp[i] == 10 || dp[i] == 13 || dp[i] == 0 || dp[i] == 9) 1700 continue; 1701 binary = 1; 1702 } 1703 if (binary) 1704 hexdump(dp, datalen, screenwidth); 1705 else 1706 visdump(dp, datalen, screenwidth); 1707 } 1708 1709 void 1710 ktrpsig(struct ktr_psig *psig) 1711 { 1712 const char *str; 1713 1714 print_signal(psig->signo); 1715 if (psig->action == SIG_DFL) { 1716 printf(" SIG_DFL"); 1717 } else { 1718 printf(" caught handler=0x%lx mask=0x%x", 1719 (u_long)psig->action, psig->mask.__bits[0]); 1720 } 1721 printf(" code="); 1722 str = sysdecode_sigcode(psig->signo, psig->code); 1723 if (str != NULL) 1724 printf("%s", str); 1725 else 1726 printf("<invalid=%#x>", psig->code); 1727 putchar('\n'); 1728 } 1729 1730 void 1731 ktrcsw_old(struct ktr_csw_old *cs) 1732 { 1733 printf("%s %s\n", cs->out ? "stop" : "resume", 1734 cs->user ? "user" : "kernel"); 1735 } 1736 1737 void 1738 ktrcsw(struct ktr_csw *cs) 1739 { 1740 printf("%s %s \"%s\"\n", cs->out ? "stop" : "resume", 1741 cs->user ? "user" : "kernel", cs->wmesg); 1742 } 1743 1744 void 1745 ktruser(int len, void *p) 1746 { 1747 unsigned char *cp; 1748 1749 if (sysdecode_utrace(stdout, p, len)) { 1750 printf("\n"); 1751 return; 1752 } 1753 1754 printf("%d ", len); 1755 cp = p; 1756 while (len--) 1757 if (decimal) 1758 printf(" %d", *cp++); 1759 else 1760 printf(" %02x", *cp++); 1761 printf("\n"); 1762 } 1763 1764 void 1765 ktrcaprights(cap_rights_t *rightsp) 1766 { 1767 1768 printf("cap_rights_t "); 1769 sysdecode_cap_rights(stdout, rightsp); 1770 printf("\n"); 1771 } 1772 1773 static void 1774 ktrtimeval(struct timeval *tv) 1775 { 1776 1777 printf("{%ld, %ld}", (long)tv->tv_sec, tv->tv_usec); 1778 } 1779 1780 void 1781 ktritimerval(struct itimerval *it) 1782 { 1783 1784 printf("itimerval { .interval = "); 1785 ktrtimeval(&it->it_interval); 1786 printf(", .value = "); 1787 ktrtimeval(&it->it_value); 1788 printf(" }\n"); 1789 } 1790 1791 void 1792 ktrsockaddr(struct sockaddr *sa) 1793 { 1794 /* 1795 TODO: Support additional address families 1796 #include <netsmb/netbios.h> 1797 struct sockaddr_nb *nb; 1798 */ 1799 const char *str; 1800 char addr[64]; 1801 1802 /* 1803 * note: ktrstruct() has already verified that sa points to a 1804 * buffer at least sizeof(struct sockaddr) bytes long and exactly 1805 * sa->sa_len bytes long. 1806 */ 1807 printf("struct sockaddr { "); 1808 str = sysdecode_sockaddr_family(sa->sa_family); 1809 if (str != NULL) 1810 printf("%s", str); 1811 else 1812 printf("<invalid=%d>", sa->sa_family); 1813 printf(", "); 1814 1815 #define check_sockaddr_len(n) \ 1816 if (sa_##n.s##n##_len < sizeof(struct sockaddr_##n)) { \ 1817 printf("invalid"); \ 1818 break; \ 1819 } 1820 1821 switch(sa->sa_family) { 1822 case AF_INET: { 1823 struct sockaddr_in sa_in; 1824 1825 memset(&sa_in, 0, sizeof(sa_in)); 1826 memcpy(&sa_in, sa, sa->sa_len); 1827 check_sockaddr_len(in); 1828 inet_ntop(AF_INET, &sa_in.sin_addr, addr, sizeof addr); 1829 printf("%s:%u", addr, ntohs(sa_in.sin_port)); 1830 break; 1831 } 1832 case AF_INET6: { 1833 struct sockaddr_in6 sa_in6; 1834 1835 memset(&sa_in6, 0, sizeof(sa_in6)); 1836 memcpy(&sa_in6, sa, sa->sa_len); 1837 check_sockaddr_len(in6); 1838 getnameinfo((struct sockaddr *)&sa_in6, sizeof(sa_in6), 1839 addr, sizeof(addr), NULL, 0, NI_NUMERICHOST); 1840 printf("[%s]:%u", addr, htons(sa_in6.sin6_port)); 1841 break; 1842 } 1843 case AF_UNIX: { 1844 struct sockaddr_un sa_un; 1845 1846 memset(&sa_un, 0, sizeof(sa_un)); 1847 memcpy(&sa_un, sa, sa->sa_len); 1848 printf("%.*s", (int)sizeof(sa_un.sun_path), sa_un.sun_path); 1849 break; 1850 } 1851 default: 1852 printf("unknown address family"); 1853 } 1854 printf(" }\n"); 1855 } 1856 1857 void 1858 ktrstat(struct stat *statp) 1859 { 1860 char mode[12], timestr[PATH_MAX + 4]; 1861 struct passwd *pwd; 1862 struct group *grp; 1863 struct tm *tm; 1864 1865 /* 1866 * note: ktrstruct() has already verified that statp points to a 1867 * buffer exactly sizeof(struct stat) bytes long. 1868 */ 1869 printf("struct stat {"); 1870 printf("dev=%ju, ino=%ju, ", 1871 (uintmax_t)statp->st_dev, (uintmax_t)statp->st_ino); 1872 if (!resolv) 1873 printf("mode=0%jo, ", (uintmax_t)statp->st_mode); 1874 else { 1875 strmode(statp->st_mode, mode); 1876 printf("mode=%s, ", mode); 1877 } 1878 printf("nlink=%ju, ", (uintmax_t)statp->st_nlink); 1879 if (!resolv) { 1880 pwd = NULL; 1881 } else { 1882 #ifdef WITH_CASPER 1883 if (cappwd != NULL) 1884 pwd = cap_getpwuid(cappwd, statp->st_uid); 1885 else 1886 #endif 1887 pwd = getpwuid(statp->st_uid); 1888 } 1889 if (pwd == NULL) 1890 printf("uid=%ju, ", (uintmax_t)statp->st_uid); 1891 else 1892 printf("uid=\"%s\", ", pwd->pw_name); 1893 if (!resolv) { 1894 grp = NULL; 1895 } else { 1896 #ifdef WITH_CASPER 1897 if (capgrp != NULL) 1898 grp = cap_getgrgid(capgrp, statp->st_gid); 1899 else 1900 #endif 1901 grp = getgrgid(statp->st_gid); 1902 } 1903 if (grp == NULL) 1904 printf("gid=%ju, ", (uintmax_t)statp->st_gid); 1905 else 1906 printf("gid=\"%s\", ", grp->gr_name); 1907 printf("rdev=%ju, ", (uintmax_t)statp->st_rdev); 1908 printf("atime="); 1909 if (!resolv) 1910 printf("%jd", (intmax_t)statp->st_atim.tv_sec); 1911 else { 1912 tm = localtime(&statp->st_atim.tv_sec); 1913 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm); 1914 printf("\"%s\"", timestr); 1915 } 1916 if (statp->st_atim.tv_nsec != 0) 1917 printf(".%09ld, ", statp->st_atim.tv_nsec); 1918 else 1919 printf(", "); 1920 printf("mtime="); 1921 if (!resolv) 1922 printf("%jd", (intmax_t)statp->st_mtim.tv_sec); 1923 else { 1924 tm = localtime(&statp->st_mtim.tv_sec); 1925 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm); 1926 printf("\"%s\"", timestr); 1927 } 1928 if (statp->st_mtim.tv_nsec != 0) 1929 printf(".%09ld, ", statp->st_mtim.tv_nsec); 1930 else 1931 printf(", "); 1932 printf("ctime="); 1933 if (!resolv) 1934 printf("%jd", (intmax_t)statp->st_ctim.tv_sec); 1935 else { 1936 tm = localtime(&statp->st_ctim.tv_sec); 1937 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm); 1938 printf("\"%s\"", timestr); 1939 } 1940 if (statp->st_ctim.tv_nsec != 0) 1941 printf(".%09ld, ", statp->st_ctim.tv_nsec); 1942 else 1943 printf(", "); 1944 printf("birthtime="); 1945 if (!resolv) 1946 printf("%jd", (intmax_t)statp->st_birthtim.tv_sec); 1947 else { 1948 tm = localtime(&statp->st_birthtim.tv_sec); 1949 strftime(timestr, sizeof(timestr), TIME_FORMAT, tm); 1950 printf("\"%s\"", timestr); 1951 } 1952 if (statp->st_birthtim.tv_nsec != 0) 1953 printf(".%09ld, ", statp->st_birthtim.tv_nsec); 1954 else 1955 printf(", "); 1956 printf("size=%jd, blksize=%ju, blocks=%jd, flags=0x%x", 1957 (uintmax_t)statp->st_size, (uintmax_t)statp->st_blksize, 1958 (intmax_t)statp->st_blocks, statp->st_flags); 1959 printf(" }\n"); 1960 } 1961 1962 void 1963 ktrstruct(char *buf, size_t buflen) 1964 { 1965 char *name, *data; 1966 size_t namelen, datalen; 1967 int i; 1968 cap_rights_t rights; 1969 struct itimerval it; 1970 struct stat sb; 1971 struct sockaddr_storage ss; 1972 1973 for (name = buf, namelen = 0; 1974 namelen < buflen && name[namelen] != '\0'; 1975 ++namelen) 1976 /* nothing */; 1977 if (namelen == buflen) 1978 goto invalid; 1979 if (name[namelen] != '\0') 1980 goto invalid; 1981 data = buf + namelen + 1; 1982 datalen = buflen - namelen - 1; 1983 if (datalen == 0) 1984 goto invalid; 1985 /* sanity check */ 1986 for (i = 0; i < (int)namelen; ++i) 1987 if (!isalpha(name[i])) 1988 goto invalid; 1989 if (strcmp(name, "caprights") == 0) { 1990 if (datalen != sizeof(cap_rights_t)) 1991 goto invalid; 1992 memcpy(&rights, data, datalen); 1993 ktrcaprights(&rights); 1994 } else if (strcmp(name, "itimerval") == 0) { 1995 if (datalen != sizeof(struct itimerval)) 1996 goto invalid; 1997 memcpy(&it, data, datalen); 1998 ktritimerval(&it); 1999 } else if (strcmp(name, "stat") == 0) { 2000 if (datalen != sizeof(struct stat)) 2001 goto invalid; 2002 memcpy(&sb, data, datalen); 2003 ktrstat(&sb); 2004 } else if (strcmp(name, "sockaddr") == 0) { 2005 if (datalen > sizeof(ss)) 2006 goto invalid; 2007 memcpy(&ss, data, datalen); 2008 if (datalen != ss.ss_len) 2009 goto invalid; 2010 ktrsockaddr((struct sockaddr *)&ss); 2011 } else { 2012 printf("unknown structure\n"); 2013 } 2014 return; 2015 invalid: 2016 printf("invalid record\n"); 2017 } 2018 2019 void 2020 ktrcapfail(struct ktr_cap_fail *ktr) 2021 { 2022 switch (ktr->cap_type) { 2023 case CAPFAIL_NOTCAPABLE: 2024 /* operation on fd with insufficient capabilities */ 2025 printf("operation requires "); 2026 sysdecode_cap_rights(stdout, &ktr->cap_needed); 2027 printf(", descriptor holds "); 2028 sysdecode_cap_rights(stdout, &ktr->cap_held); 2029 break; 2030 case CAPFAIL_INCREASE: 2031 /* requested more capabilities than fd already has */ 2032 printf("attempt to increase capabilities from "); 2033 sysdecode_cap_rights(stdout, &ktr->cap_held); 2034 printf(" to "); 2035 sysdecode_cap_rights(stdout, &ktr->cap_needed); 2036 break; 2037 case CAPFAIL_SYSCALL: 2038 /* called restricted syscall */ 2039 printf("disallowed system call"); 2040 break; 2041 case CAPFAIL_LOOKUP: 2042 /* absolute or AT_FDCWD path, ".." path, etc. */ 2043 printf("restricted VFS lookup"); 2044 break; 2045 default: 2046 printf("unknown capability failure: "); 2047 sysdecode_cap_rights(stdout, &ktr->cap_needed); 2048 printf(" "); 2049 sysdecode_cap_rights(stdout, &ktr->cap_held); 2050 break; 2051 } 2052 printf("\n"); 2053 } 2054 2055 void 2056 ktrfault(struct ktr_fault *ktr) 2057 { 2058 2059 printf("0x%jx ", (uintmax_t)ktr->vaddr); 2060 print_mask_arg(sysdecode_vmprot, ktr->type); 2061 printf("\n"); 2062 } 2063 2064 void 2065 ktrfaultend(struct ktr_faultend *ktr) 2066 { 2067 const char *str; 2068 2069 str = sysdecode_vmresult(ktr->result); 2070 if (str != NULL) 2071 printf("%s", str); 2072 else 2073 printf("<invalid=%d>", ktr->result); 2074 printf("\n"); 2075 } 2076 2077 void 2078 ktrkevent(struct kevent *kev) 2079 { 2080 2081 printf("{ ident="); 2082 switch (kev->filter) { 2083 case EVFILT_READ: 2084 case EVFILT_WRITE: 2085 case EVFILT_VNODE: 2086 case EVFILT_PROC: 2087 case EVFILT_TIMER: 2088 case EVFILT_PROCDESC: 2089 case EVFILT_EMPTY: 2090 printf("%ju", (uintmax_t)kev->ident); 2091 break; 2092 case EVFILT_SIGNAL: 2093 print_signal(kev->ident); 2094 break; 2095 default: 2096 printf("%p", (void *)kev->ident); 2097 } 2098 printf(", filter="); 2099 print_integer_arg(sysdecode_kevent_filter, kev->filter); 2100 printf(", flags="); 2101 print_mask_arg0(sysdecode_kevent_flags, kev->flags); 2102 printf(", fflags="); 2103 sysdecode_kevent_fflags(stdout, kev->filter, kev->fflags, 2104 decimal ? 10 : 16); 2105 printf(", data=%#jx, udata=%p }", (uintmax_t)kev->data, kev->udata); 2106 } 2107 2108 void 2109 ktrstructarray(struct ktr_struct_array *ksa, size_t buflen) 2110 { 2111 struct kevent kev; 2112 char *name, *data; 2113 size_t namelen, datalen; 2114 int i; 2115 bool first; 2116 2117 buflen -= sizeof(*ksa); 2118 for (name = (char *)(ksa + 1), namelen = 0; 2119 namelen < buflen && name[namelen] != '\0'; 2120 ++namelen) 2121 /* nothing */; 2122 if (namelen == buflen) 2123 goto invalid; 2124 if (name[namelen] != '\0') 2125 goto invalid; 2126 /* sanity check */ 2127 for (i = 0; i < (int)namelen; ++i) 2128 if (!isalnum(name[i]) && name[i] != '_') 2129 goto invalid; 2130 data = name + namelen + 1; 2131 datalen = buflen - namelen - 1; 2132 printf("struct %s[] = { ", name); 2133 first = true; 2134 for (; datalen >= ksa->struct_size; 2135 data += ksa->struct_size, datalen -= ksa->struct_size) { 2136 if (!first) 2137 printf("\n "); 2138 else 2139 first = false; 2140 if (strcmp(name, "kevent") == 0) { 2141 if (ksa->struct_size != sizeof(kev)) 2142 goto bad_size; 2143 memcpy(&kev, data, sizeof(kev)); 2144 ktrkevent(&kev); 2145 } else if (strcmp(name, "kevent_freebsd11") == 0) { 2146 struct kevent_freebsd11 kev11; 2147 2148 if (ksa->struct_size != sizeof(kev11)) 2149 goto bad_size; 2150 memcpy(&kev11, data, sizeof(kev11)); 2151 memset(&kev, 0, sizeof(kev)); 2152 kev.ident = kev11.ident; 2153 kev.filter = kev11.filter; 2154 kev.flags = kev11.flags; 2155 kev.fflags = kev11.fflags; 2156 kev.data = kev11.data; 2157 kev.udata = kev11.udata; 2158 ktrkevent(&kev); 2159 #ifdef _WANT_KEVENT32 2160 } else if (strcmp(name, "kevent32") == 0) { 2161 struct kevent32 kev32; 2162 2163 if (ksa->struct_size != sizeof(kev32)) 2164 goto bad_size; 2165 memcpy(&kev32, data, sizeof(kev32)); 2166 memset(&kev, 0, sizeof(kev)); 2167 kev.ident = kev32.ident; 2168 kev.filter = kev32.filter; 2169 kev.flags = kev32.flags; 2170 kev.fflags = kev32.fflags; 2171 #if BYTE_ORDER == BIG_ENDIAN 2172 kev.data = kev32.data2 | ((int64_t)kev32.data1 << 32); 2173 #else 2174 kev.data = kev32.data1 | ((int64_t)kev32.data2 << 32); 2175 #endif 2176 kev.udata = (void *)(uintptr_t)kev32.udata; 2177 ktrkevent(&kev); 2178 } else if (strcmp(name, "kevent32_freebsd11") == 0) { 2179 struct kevent32_freebsd11 kev32; 2180 2181 if (ksa->struct_size != sizeof(kev32)) 2182 goto bad_size; 2183 memcpy(&kev32, data, sizeof(kev32)); 2184 memset(&kev, 0, sizeof(kev)); 2185 kev.ident = kev32.ident; 2186 kev.filter = kev32.filter; 2187 kev.flags = kev32.flags; 2188 kev.fflags = kev32.fflags; 2189 kev.data = kev32.data; 2190 kev.udata = (void *)(uintptr_t)kev32.udata; 2191 ktrkevent(&kev); 2192 #endif 2193 } else { 2194 printf("<unknown structure> }\n"); 2195 return; 2196 } 2197 } 2198 printf(" }\n"); 2199 return; 2200 invalid: 2201 printf("invalid record\n"); 2202 return; 2203 bad_size: 2204 printf("<bad size> }\n"); 2205 return; 2206 } 2207 2208 void 2209 usage(void) 2210 { 2211 fprintf(stderr, "usage: kdump [-dEnlHRrSsTA] [-f trfile] " 2212 "[-m maxdata] [-p pid] [-t trstr]\n"); 2213 exit(1); 2214 } 2215