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