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