1 /* 2 * Copyright (c) 2006 "David Kirchner" <dpk@dpk.net>. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #define L2CAP_SOCKET_CHECKED 30 31 #include <sys/types.h> 32 #include <sys/acl.h> 33 #include <sys/capsicum.h> 34 #include <sys/extattr.h> 35 #include <sys/linker.h> 36 #include <sys/mman.h> 37 #include <sys/mount.h> 38 #include <sys/procctl.h> 39 #include <sys/ptrace.h> 40 #include <sys/reboot.h> 41 #include <sys/resource.h> 42 #include <sys/rtprio.h> 43 #include <sys/sem.h> 44 #include <sys/shm.h> 45 #include <sys/socket.h> 46 #include <sys/stat.h> 47 #include <sys/thr.h> 48 #include <sys/umtx.h> 49 #include <machine/sysarch.h> 50 #include <netinet/in.h> 51 #include <netinet/sctp.h> 52 #include <netinet/tcp.h> 53 #include <netinet/udp.h> 54 #include <netinet/udplite.h> 55 #include <nfsserver/nfs.h> 56 #include <ufs/ufs/quota.h> 57 #include <vm/vm.h> 58 #include <vm/vm_param.h> 59 #include <aio.h> 60 #include <fcntl.h> 61 #include <sched.h> 62 #include <stdbool.h> 63 #include <stdio.h> 64 #include <stdlib.h> 65 #include <strings.h> 66 #include <sysdecode.h> 67 #include <unistd.h> 68 #include <sys/bitstring.h> 69 #include <netgraph/bluetooth/include/ng_hci.h> 70 #include <netgraph/bluetooth/include/ng_l2cap.h> 71 #include <netgraph/bluetooth/include/ng_btsocket.h> 72 73 /* 74 * This is taken from the xlat tables originally in truss which were 75 * in turn taken from strace. 76 */ 77 struct name_table { 78 uintmax_t val; 79 const char *str; 80 }; 81 82 #define X(a) { a, #a }, 83 #define XEND { 0, NULL } 84 85 #define TABLE_START(n) static struct name_table n[] = { 86 #define TABLE_ENTRY X 87 #define TABLE_END XEND }; 88 89 #include "tables.h" 90 91 #undef TABLE_START 92 #undef TABLE_ENTRY 93 #undef TABLE_END 94 95 /* 96 * These are simple support macros. print_or utilizes a variable 97 * defined in the calling function to track whether or not it should 98 * print a logical-OR character ('|') before a string. if_print_or 99 * simply handles the necessary "if" statement used in many lines 100 * of this file. 101 */ 102 #define print_or(fp,str,orflag) do { \ 103 if (orflag) fputc(fp, '|'); else orflag = true; \ 104 fprintf(fp, str); } \ 105 while (0) 106 #define if_print_or(fp,i,flag,orflag) do { \ 107 if ((i & flag) == flag) \ 108 print_or(fp,#flag,orflag); } \ 109 while (0) 110 111 static const char * 112 lookup_value(struct name_table *table, uintmax_t val) 113 { 114 115 for (; table->str != NULL; table++) 116 if (table->val == val) 117 return (table->str); 118 return (NULL); 119 } 120 121 /* 122 * Used when the value maps to a bitmask of #definition values in the 123 * table. This is a helper routine which outputs a symbolic mask of 124 * matched masks. Multiple masks are separated by a pipe ('|'). 125 * The value is modified on return to only hold unmatched bits. 126 */ 127 static void 128 print_mask_part(FILE *fp, struct name_table *table, uintmax_t *valp, 129 bool *printed) 130 { 131 uintmax_t rem; 132 133 rem = *valp; 134 for (; table->str != NULL; table++) { 135 if ((table->val & rem) == table->val) { 136 /* 137 * Only print a zero mask if the raw value is 138 * zero. 139 */ 140 if (table->val == 0 && *valp != 0) 141 continue; 142 fprintf(fp, "%s%s", *printed ? "|" : "", table->str); 143 *printed = true; 144 rem &= ~table->val; 145 } 146 } 147 148 *valp = rem; 149 } 150 151 /* 152 * Used when the value maps to a bitmask of #definition values in the 153 * table. The return value is true if something was printed. If 154 * rem is not NULL, *rem holds any bits not decoded if something was 155 * printed. If nothing was printed and rem is not NULL, *rem holds 156 * the original value. 157 */ 158 static bool 159 print_mask_int(FILE *fp, struct name_table *table, int ival, int *rem) 160 { 161 uintmax_t val; 162 bool printed; 163 164 printed = false; 165 val = (unsigned)ival; 166 print_mask_part(fp, table, &val, &printed); 167 if (rem != NULL) 168 *rem = val; 169 return (printed); 170 } 171 172 /* 173 * Used for a mask of optional flags where a value of 0 is valid. 174 */ 175 static bool 176 print_mask_0(FILE *fp, struct name_table *table, int val, int *rem) 177 { 178 179 if (val == 0) { 180 fputs("0", fp); 181 if (rem != NULL) 182 *rem = 0; 183 return (true); 184 } 185 return (print_mask_int(fp, table, val, rem)); 186 } 187 188 /* 189 * Like print_mask_0 but for a unsigned long instead of an int. 190 */ 191 static bool 192 print_mask_0ul(FILE *fp, struct name_table *table, u_long lval, u_long *rem) 193 { 194 uintmax_t val; 195 bool printed; 196 197 if (lval == 0) { 198 fputs("0", fp); 199 if (rem != NULL) 200 *rem = 0; 201 return (true); 202 } 203 204 printed = false; 205 val = lval; 206 print_mask_part(fp, table, &val, &printed); 207 if (rem != NULL) 208 *rem = val; 209 return (printed); 210 } 211 212 static void 213 print_integer(FILE *fp, int val, int base) 214 { 215 216 switch (base) { 217 case 8: 218 fprintf(fp, "0%o", val); 219 break; 220 case 10: 221 fprintf(fp, "%d", val); 222 break; 223 case 16: 224 fprintf(fp, "0x%x", val); 225 break; 226 default: 227 abort2("bad base", 0, NULL); 228 break; 229 } 230 } 231 232 static bool 233 print_value(FILE *fp, struct name_table *table, uintmax_t val) 234 { 235 const char *str; 236 237 str = lookup_value(table, val); 238 if (str != NULL) { 239 fputs(str, fp); 240 return (true); 241 } 242 return (false); 243 } 244 245 const char * 246 sysdecode_atfd(int fd) 247 { 248 249 if (fd == AT_FDCWD) 250 return ("AT_FDCWD"); 251 return (NULL); 252 } 253 254 bool 255 sysdecode_atflags(FILE *fp, int flag, int *rem) 256 { 257 258 return (print_mask_int(fp, atflags, flag, rem)); 259 } 260 261 static struct name_table semctlops[] = { 262 X(GETNCNT) X(GETPID) X(GETVAL) X(GETALL) X(GETZCNT) X(SETVAL) X(SETALL) 263 X(IPC_RMID) X(IPC_SET) X(IPC_STAT) XEND 264 }; 265 266 const char * 267 sysdecode_semctl_cmd(int cmd) 268 { 269 270 return (lookup_value(semctlops, cmd)); 271 } 272 273 static struct name_table shmctlops[] = { 274 X(IPC_RMID) X(IPC_SET) X(IPC_STAT) XEND 275 }; 276 277 const char * 278 sysdecode_shmctl_cmd(int cmd) 279 { 280 281 return (lookup_value(shmctlops, cmd)); 282 } 283 284 const char * 285 sysdecode_msgctl_cmd(int cmd) 286 { 287 288 return (sysdecode_shmctl_cmd(cmd)); 289 } 290 291 static struct name_table semgetflags[] = { 292 X(IPC_CREAT) X(IPC_EXCL) X(SEM_R) X(SEM_A) X((SEM_R>>3)) X((SEM_A>>3)) 293 X((SEM_R>>6)) X((SEM_A>>6)) XEND 294 }; 295 296 bool 297 sysdecode_semget_flags(FILE *fp, int flag, int *rem) 298 { 299 300 return (print_mask_int(fp, semgetflags, flag, rem)); 301 } 302 303 static struct name_table idtypes[] = { 304 X(P_PID) X(P_PPID) X(P_PGID) X(P_SID) X(P_CID) X(P_UID) X(P_GID) 305 X(P_ALL) X(P_LWPID) X(P_TASKID) X(P_PROJID) X(P_POOLID) X(P_JAILID) 306 X(P_CTID) X(P_CPUID) X(P_PSETID) XEND 307 }; 308 309 /* XXX: idtype is really an idtype_t */ 310 const char * 311 sysdecode_idtype(int idtype) 312 { 313 314 return (lookup_value(idtypes, idtype)); 315 } 316 317 /* 318 * [g|s]etsockopt's level argument can either be SOL_SOCKET or a 319 * protocol-specific value. 320 */ 321 const char * 322 sysdecode_sockopt_level(int level) 323 { 324 const char *str; 325 326 if (level == SOL_SOCKET) 327 return ("SOL_SOCKET"); 328 329 /* SOL_* constants for Bluetooth sockets. */ 330 str = lookup_value(ngbtsolevel, level); 331 if (str != NULL) 332 return (str); 333 334 /* 335 * IP and Infiniband sockets use IP protocols as levels. Not all 336 * protocols are valid but it is simpler to just allow all of them. 337 * 338 * XXX: IPPROTO_IP == 0, but UNIX domain sockets use a level of 0 339 * for private options. 340 */ 341 str = sysdecode_ipproto(level); 342 if (str != NULL) 343 return (str); 344 345 return (NULL); 346 } 347 348 bool 349 sysdecode_vmprot(FILE *fp, int type, int *rem) 350 { 351 352 return (print_mask_int(fp, vmprot, type, rem)); 353 } 354 355 static struct name_table sockflags[] = { 356 X(SOCK_CLOEXEC) X(SOCK_NONBLOCK) XEND 357 }; 358 359 bool 360 sysdecode_socket_type(FILE *fp, int type, int *rem) 361 { 362 const char *str; 363 uintmax_t val; 364 bool printed; 365 366 str = lookup_value(socktype, type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)); 367 if (str != NULL) { 368 fputs(str, fp); 369 *rem = 0; 370 printed = true; 371 } else { 372 *rem = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK); 373 printed = false; 374 } 375 val = type & (SOCK_CLOEXEC | SOCK_NONBLOCK); 376 print_mask_part(fp, sockflags, &val, &printed); 377 return (printed); 378 } 379 380 bool 381 sysdecode_access_mode(FILE *fp, int mode, int *rem) 382 { 383 384 return (print_mask_int(fp, accessmode, mode, rem)); 385 } 386 387 /* XXX: 'type' is really an acl_type_t. */ 388 const char * 389 sysdecode_acltype(int type) 390 { 391 392 return (lookup_value(acltype, type)); 393 } 394 395 bool 396 sysdecode_cap_fcntlrights(FILE *fp, uint32_t rights, uint32_t *rem) 397 { 398 399 return (print_mask_int(fp, capfcntl, rights, rem)); 400 } 401 402 const char * 403 sysdecode_extattrnamespace(int namespace) 404 { 405 406 return (lookup_value(extattrns, namespace)); 407 } 408 409 const char * 410 sysdecode_fadvice(int advice) 411 { 412 413 return (lookup_value(fadvisebehav, advice)); 414 } 415 416 bool 417 sysdecode_open_flags(FILE *fp, int flags, int *rem) 418 { 419 bool printed; 420 int mode; 421 uintmax_t val; 422 423 mode = flags & O_ACCMODE; 424 flags &= ~O_ACCMODE; 425 switch (mode) { 426 case O_RDONLY: 427 if (flags & O_EXEC) { 428 flags &= ~O_EXEC; 429 fputs("O_EXEC", fp); 430 } else 431 fputs("O_RDONLY", fp); 432 printed = true; 433 mode = 0; 434 break; 435 case O_WRONLY: 436 fputs("O_WRONLY", fp); 437 printed = true; 438 mode = 0; 439 break; 440 case O_RDWR: 441 fputs("O_RDWR", fp); 442 printed = true; 443 mode = 0; 444 break; 445 default: 446 printed = false; 447 } 448 val = (unsigned)flags; 449 print_mask_part(fp, openflags, &val, &printed); 450 if (rem != NULL) 451 *rem = val | mode; 452 return (printed); 453 } 454 455 bool 456 sysdecode_fcntl_fileflags(FILE *fp, int flags, int *rem) 457 { 458 bool printed; 459 int oflags; 460 461 /* 462 * The file flags used with F_GETFL/F_SETFL mostly match the 463 * flags passed to open(2). However, a few open-only flag 464 * bits have been repurposed for fcntl-only flags. 465 */ 466 oflags = flags & ~(O_NOFOLLOW | FRDAHEAD); 467 printed = sysdecode_open_flags(fp, oflags, rem); 468 if (flags & O_NOFOLLOW) { 469 fprintf(fp, "%sFPOIXSHM", printed ? "|" : ""); 470 printed = true; 471 } 472 if (flags & FRDAHEAD) { 473 fprintf(fp, "%sFRDAHEAD", printed ? "|" : ""); 474 printed = true; 475 } 476 return (printed); 477 } 478 479 bool 480 sysdecode_flock_operation(FILE *fp, int operation, int *rem) 481 { 482 483 return (print_mask_int(fp, flockops, operation, rem)); 484 } 485 486 static struct name_table getfsstatmode[] = { 487 X(MNT_WAIT) X(MNT_NOWAIT) XEND 488 }; 489 490 const char * 491 sysdecode_getfsstat_mode(int mode) 492 { 493 494 return (lookup_value(getfsstatmode, mode)); 495 } 496 497 const char * 498 sysdecode_getrusage_who(int who) 499 { 500 501 return (lookup_value(rusage, who)); 502 } 503 504 const char * 505 sysdecode_kldsym_cmd(int cmd) 506 { 507 508 return (lookup_value(kldsymcmd, cmd)); 509 } 510 511 const char * 512 sysdecode_kldunload_flags(int flags) 513 { 514 515 return (lookup_value(kldunloadfflags, flags)); 516 } 517 518 const char * 519 sysdecode_lio_listio_mode(int mode) 520 { 521 522 return (lookup_value(lio_listiomodes, mode)); 523 } 524 525 const char * 526 sysdecode_madvice(int advice) 527 { 528 529 return (lookup_value(madvisebehav, advice)); 530 } 531 532 const char * 533 sysdecode_minherit_inherit(int inherit) 534 { 535 536 return (lookup_value(minheritflags, inherit)); 537 } 538 539 bool 540 sysdecode_mlockall_flags(FILE *fp, int flags, int *rem) 541 { 542 543 return (print_mask_int(fp, mlockallflags, flags, rem)); 544 } 545 546 bool 547 sysdecode_mmap_prot(FILE *fp, int prot, int *rem) 548 { 549 550 return (print_mask_int(fp, mmapprot, prot, rem)); 551 } 552 553 bool 554 sysdecode_fileflags(FILE *fp, fflags_t flags, fflags_t *rem) 555 { 556 557 return (print_mask_0(fp, fileflags, flags, rem)); 558 } 559 560 bool 561 sysdecode_filemode(FILE *fp, int mode, int *rem) 562 { 563 564 return (print_mask_0(fp, filemode, mode, rem)); 565 } 566 567 bool 568 sysdecode_mount_flags(FILE *fp, int flags, int *rem) 569 { 570 571 return (print_mask_int(fp, mountflags, flags, rem)); 572 } 573 574 bool 575 sysdecode_msync_flags(FILE *fp, int flags, int *rem) 576 { 577 578 return (print_mask_int(fp, msyncflags, flags, rem)); 579 } 580 581 const char * 582 sysdecode_nfssvc_flags(int flags) 583 { 584 585 return (lookup_value(nfssvcflags, flags)); 586 } 587 588 static struct name_table pipe2flags[] = { 589 X(O_CLOEXEC) X(O_NONBLOCK) XEND 590 }; 591 592 bool 593 sysdecode_pipe2_flags(FILE *fp, int flags, int *rem) 594 { 595 596 return (print_mask_0(fp, pipe2flags, flags, rem)); 597 } 598 599 const char * 600 sysdecode_prio_which(int which) 601 { 602 603 return (lookup_value(prio, which)); 604 } 605 606 const char * 607 sysdecode_procctl_cmd(int cmd) 608 { 609 610 return (lookup_value(procctlcmd, cmd)); 611 } 612 613 const char * 614 sysdecode_ptrace_request(int request) 615 { 616 617 return (lookup_value(ptraceop, request)); 618 } 619 620 static struct name_table quotatypes[] = { 621 X(GRPQUOTA) X(USRQUOTA) XEND 622 }; 623 624 bool 625 sysdecode_quotactl_cmd(FILE *fp, int cmd) 626 { 627 const char *primary, *type; 628 629 primary = lookup_value(quotactlcmds, cmd >> SUBCMDSHIFT); 630 if (primary == NULL) 631 return (false); 632 fprintf(fp, "QCMD(%s,", primary); 633 type = lookup_value(quotatypes, cmd & SUBCMDMASK); 634 if (type != NULL) 635 fprintf(fp, "%s", type); 636 else 637 fprintf(fp, "%#x", cmd & SUBCMDMASK); 638 fprintf(fp, ")"); 639 return (true); 640 } 641 642 bool 643 sysdecode_reboot_howto(FILE *fp, int howto, int *rem) 644 { 645 bool printed; 646 647 /* 648 * RB_AUTOBOOT is special in that its value is zero, but it is 649 * also an implied argument if a different operation is not 650 * requested via RB_HALT, RB_POWERCYCLE, RB_POWEROFF, or 651 * RB_REROOT. 652 */ 653 if (howto != 0 && (howto & (RB_HALT | RB_POWEROFF | RB_REROOT | 654 RB_POWERCYCLE)) == 0) { 655 fputs("RB_AUTOBOOT|", fp); 656 printed = true; 657 } else 658 printed = false; 659 return (print_mask_int(fp, rebootopt, howto, rem) || printed); 660 } 661 662 bool 663 sysdecode_rfork_flags(FILE *fp, int flags, int *rem) 664 { 665 666 return (print_mask_int(fp, rforkflags, flags, rem)); 667 } 668 669 const char * 670 sysdecode_rlimit(int resource) 671 { 672 673 return (lookup_value(rlimit, resource)); 674 } 675 676 const char * 677 sysdecode_scheduler_policy(int policy) 678 { 679 680 return (lookup_value(schedpolicy, policy)); 681 } 682 683 bool 684 sysdecode_sendfile_flags(FILE *fp, int flags, int *rem) 685 { 686 687 return (print_mask_int(fp, sendfileflags, flags, rem)); 688 } 689 690 bool 691 sysdecode_shmat_flags(FILE *fp, int flags, int *rem) 692 { 693 694 return (print_mask_int(fp, shmatflags, flags, rem)); 695 } 696 697 const char * 698 sysdecode_shutdown_how(int how) 699 { 700 701 return (lookup_value(shutdownhow, how)); 702 } 703 704 const char * 705 sysdecode_sigbus_code(int si_code) 706 { 707 708 return (lookup_value(sigbuscode, si_code)); 709 } 710 711 const char * 712 sysdecode_sigchld_code(int si_code) 713 { 714 715 return (lookup_value(sigchldcode, si_code)); 716 } 717 718 const char * 719 sysdecode_sigfpe_code(int si_code) 720 { 721 722 return (lookup_value(sigfpecode, si_code)); 723 } 724 725 const char * 726 sysdecode_sigill_code(int si_code) 727 { 728 729 return (lookup_value(sigillcode, si_code)); 730 } 731 732 const char * 733 sysdecode_sigsegv_code(int si_code) 734 { 735 736 return (lookup_value(sigsegvcode, si_code)); 737 } 738 739 const char * 740 sysdecode_sigtrap_code(int si_code) 741 { 742 743 return (lookup_value(sigtrapcode, si_code)); 744 } 745 746 const char * 747 sysdecode_sigprocmask_how(int how) 748 { 749 750 return (lookup_value(sigprocmaskhow, how)); 751 } 752 753 const char * 754 sysdecode_socketdomain(int domain) 755 { 756 757 return (lookup_value(sockdomain, domain)); 758 } 759 760 const char * 761 sysdecode_socket_protocol(int domain, int protocol) 762 { 763 764 switch (domain) { 765 case PF_INET: 766 case PF_INET6: 767 return (lookup_value(sockipproto, protocol)); 768 default: 769 return (NULL); 770 } 771 } 772 773 const char * 774 sysdecode_sockaddr_family(int sa_family) 775 { 776 777 return (lookup_value(sockfamily, sa_family)); 778 } 779 780 const char * 781 sysdecode_ipproto(int protocol) 782 { 783 784 return (lookup_value(sockipproto, protocol)); 785 } 786 787 const char * 788 sysdecode_sockopt_name(int level, int optname) 789 { 790 791 if (level == SOL_SOCKET) 792 return (lookup_value(sockopt, optname)); 793 if (level == IPPROTO_IP) 794 /* XXX: UNIX domain socket options use a level of 0 also. */ 795 return (lookup_value(sockoptip, optname)); 796 if (level == IPPROTO_IPV6) 797 return (lookup_value(sockoptipv6, optname)); 798 if (level == IPPROTO_SCTP) 799 return (lookup_value(sockoptsctp, optname)); 800 if (level == IPPROTO_TCP) 801 return (lookup_value(sockopttcp, optname)); 802 if (level == IPPROTO_UDP) 803 return (lookup_value(sockoptudp, optname)); 804 if (level == IPPROTO_UDPLITE) 805 return (lookup_value(sockoptudplite, optname)); 806 return (NULL); 807 } 808 809 bool 810 sysdecode_thr_create_flags(FILE *fp, int flags, int *rem) 811 { 812 813 return (print_mask_int(fp, thrcreateflags, flags, rem)); 814 } 815 816 const char * 817 sysdecode_umtx_op(int op) 818 { 819 820 return (lookup_value(umtxop, op)); 821 } 822 823 const char * 824 sysdecode_vmresult(int result) 825 { 826 827 return (lookup_value(vmresult, result)); 828 } 829 830 bool 831 sysdecode_wait4_options(FILE *fp, int options, int *rem) 832 { 833 bool printed; 834 int opt6; 835 836 /* A flags value of 0 is normal. */ 837 if (options == 0) { 838 fputs("0", fp); 839 if (rem != NULL) 840 *rem = 0; 841 return (true); 842 } 843 844 /* 845 * These flags are implicit and aren't valid flags for wait4() 846 * directly (though they don't fail with EINVAL). 847 */ 848 opt6 = options & (WEXITED | WTRAPPED); 849 options &= ~opt6; 850 printed = print_mask_int(fp, wait6opt, options, rem); 851 if (rem != NULL) 852 *rem |= opt6; 853 return (printed); 854 } 855 856 bool 857 sysdecode_wait6_options(FILE *fp, int options, int *rem) 858 { 859 860 return (print_mask_int(fp, wait6opt, options, rem)); 861 } 862 863 const char * 864 sysdecode_whence(int whence) 865 { 866 867 return (lookup_value(seekwhence, whence)); 868 } 869 870 const char * 871 sysdecode_fcntl_cmd(int cmd) 872 { 873 874 return (lookup_value(fcntlcmd, cmd)); 875 } 876 877 static struct name_table fcntl_fd_arg[] = { 878 X(FD_CLOEXEC) X(0) XEND 879 }; 880 881 bool 882 sysdecode_fcntl_arg_p(int cmd) 883 { 884 885 switch (cmd) { 886 case F_GETFD: 887 case F_GETFL: 888 case F_GETOWN: 889 return (false); 890 default: 891 return (true); 892 } 893 } 894 895 void 896 sysdecode_fcntl_arg(FILE *fp, int cmd, uintptr_t arg, int base) 897 { 898 int rem; 899 900 switch (cmd) { 901 case F_SETFD: 902 if (!print_value(fp, fcntl_fd_arg, arg)) 903 print_integer(fp, arg, base); 904 break; 905 case F_SETFL: 906 if (!sysdecode_fcntl_fileflags(fp, arg, &rem)) 907 fprintf(fp, "%#x", rem); 908 else if (rem != 0) 909 fprintf(fp, "|%#x", rem); 910 break; 911 case F_GETLK: 912 case F_SETLK: 913 case F_SETLKW: 914 fprintf(fp, "%p", (void *)arg); 915 break; 916 default: 917 print_integer(fp, arg, base); 918 break; 919 } 920 } 921 922 bool 923 sysdecode_mmap_flags(FILE *fp, int flags, int *rem) 924 { 925 uintmax_t val; 926 bool printed; 927 int align; 928 929 /* 930 * MAP_ALIGNED can't be handled directly by print_mask_int(). 931 * MAP_32BIT is also problematic since it isn't defined for 932 * all platforms. 933 */ 934 printed = false; 935 align = flags & MAP_ALIGNMENT_MASK; 936 val = (unsigned)flags & ~MAP_ALIGNMENT_MASK; 937 print_mask_part(fp, mmapflags, &val, &printed); 938 #ifdef MAP_32BIT 939 if (val & MAP_32BIT) { 940 fprintf(fp, "%sMAP_32BIT", printed ? "|" : ""); 941 printed = true; 942 val &= ~MAP_32BIT; 943 } 944 #endif 945 if (align != 0) { 946 if (printed) 947 fputc('|', fp); 948 if (align == MAP_ALIGNED_SUPER) 949 fputs("MAP_ALIGNED_SUPER", fp); 950 else 951 fprintf(fp, "MAP_ALIGNED(%d)", 952 align >> MAP_ALIGNMENT_SHIFT); 953 printed = true; 954 } 955 if (rem != NULL) 956 *rem = val; 957 return (printed); 958 } 959 960 const char * 961 sysdecode_pathconf_name(int name) 962 { 963 964 return (lookup_value(pathconfname, name)); 965 } 966 967 const char * 968 sysdecode_rtprio_function(int function) 969 { 970 971 return (lookup_value(rtpriofuncs, function)); 972 } 973 974 bool 975 sysdecode_msg_flags(FILE *fp, int flags, int *rem) 976 { 977 978 return (print_mask_0(fp, msgflags, flags, rem)); 979 } 980 981 const char * 982 sysdecode_sigcode(int sig, int si_code) 983 { 984 const char *str; 985 986 str = lookup_value(sigcode, si_code); 987 if (str != NULL) 988 return (str); 989 990 switch (sig) { 991 case SIGILL: 992 return (sysdecode_sigill_code(si_code)); 993 case SIGBUS: 994 return (sysdecode_sigbus_code(si_code)); 995 case SIGSEGV: 996 return (sysdecode_sigsegv_code(si_code)); 997 case SIGFPE: 998 return (sysdecode_sigfpe_code(si_code)); 999 case SIGTRAP: 1000 return (sysdecode_sigtrap_code(si_code)); 1001 case SIGCHLD: 1002 return (sysdecode_sigchld_code(si_code)); 1003 default: 1004 return (NULL); 1005 } 1006 } 1007 1008 const char * 1009 sysdecode_sysarch_number(int number) 1010 { 1011 1012 return (lookup_value(sysarchnum, number)); 1013 } 1014 1015 bool 1016 sysdecode_umtx_cvwait_flags(FILE *fp, u_long flags, u_long *rem) 1017 { 1018 1019 return (print_mask_0ul(fp, umtxcvwaitflags, flags, rem)); 1020 } 1021 1022 bool 1023 sysdecode_umtx_rwlock_flags(FILE *fp, u_long flags, u_long *rem) 1024 { 1025 1026 return (print_mask_0ul(fp, umtxrwlockflags, flags, rem)); 1027 } 1028 1029 void 1030 sysdecode_cap_rights(FILE *fp, cap_rights_t *rightsp) 1031 { 1032 struct name_table *t; 1033 int i; 1034 bool comma; 1035 1036 for (i = 0; i < CAPARSIZE(rightsp); i++) { 1037 if (CAPIDXBIT(rightsp->cr_rights[i]) != 1 << i) { 1038 fprintf(fp, "invalid cap_rights_t"); 1039 return; 1040 } 1041 } 1042 comma = false; 1043 for (t = caprights; t->str != NULL; t++) { 1044 if (cap_rights_is_set(rightsp, t->val)) { 1045 fprintf(fp, "%s%s", comma ? "," : "", t->str); 1046 comma = true; 1047 } 1048 } 1049 } 1050