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