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