xref: /freebsd/lib/libsysdecode/flags.c (revision b1c5f60ce87cc2f179dfb81de507d9b7bf59564c)
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/event.h>
35 #include <sys/extattr.h>
36 #include <sys/linker.h>
37 #include <sys/mman.h>
38 #include <sys/mount.h>
39 #include <sys/procctl.h>
40 #include <sys/ptrace.h>
41 #include <sys/reboot.h>
42 #include <sys/resource.h>
43 #include <sys/rtprio.h>
44 #include <sys/sem.h>
45 #include <sys/shm.h>
46 #include <sys/socket.h>
47 #include <sys/stat.h>
48 #include <sys/thr.h>
49 #include <sys/umtx.h>
50 #include <machine/sysarch.h>
51 #include <netinet/in.h>
52 #include <netinet/sctp.h>
53 #include <netinet/tcp.h>
54 #include <netinet/udp.h>
55 #include <netinet/udplite.h>
56 #include <nfsserver/nfs.h>
57 #include <ufs/ufs/quota.h>
58 #include <vm/vm.h>
59 #include <vm/vm_param.h>
60 #include <aio.h>
61 #include <fcntl.h>
62 #include <sched.h>
63 #include <stdbool.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <strings.h>
67 #include <sysdecode.h>
68 #include <unistd.h>
69 #include <sys/bitstring.h>
70 #include <netgraph/bluetooth/include/ng_hci.h>
71 #include <netgraph/bluetooth/include/ng_l2cap.h>
72 #include <netgraph/bluetooth/include/ng_btsocket.h>
73 
74 /*
75  * This is taken from the xlat tables originally in truss which were
76  * in turn taken from strace.
77  */
78 struct name_table {
79 	uintmax_t val;
80 	const char *str;
81 };
82 
83 #define	X(a)	{ a, #a },
84 #define	XEND	{ 0, NULL }
85 
86 #define	TABLE_START(n)	static struct name_table n[] = {
87 #define	TABLE_ENTRY	X
88 #define	TABLE_END	XEND };
89 
90 #include "tables.h"
91 
92 #undef TABLE_START
93 #undef TABLE_ENTRY
94 #undef TABLE_END
95 
96 /*
97  * These are simple support macros. print_or utilizes a variable
98  * defined in the calling function to track whether or not it should
99  * print a logical-OR character ('|') before a string. if_print_or
100  * simply handles the necessary "if" statement used in many lines
101  * of this file.
102  */
103 #define print_or(fp,str,orflag) do {                     \
104 	if (orflag) fputc(fp, '|'); else orflag = true;  \
105 	fprintf(fp, str); }                              \
106 	while (0)
107 #define if_print_or(fp,i,flag,orflag) do {         \
108 	if ((i & flag) == flag)                    \
109 	print_or(fp,#flag,orflag); }               \
110 	while (0)
111 
112 static const char *
113 lookup_value(struct name_table *table, uintmax_t val)
114 {
115 
116 	for (; table->str != NULL; table++)
117 		if (table->val == val)
118 			return (table->str);
119 	return (NULL);
120 }
121 
122 /*
123  * Used when the value maps to a bitmask of #definition values in the
124  * table.  This is a helper routine which outputs a symbolic mask of
125  * matched masks.  Multiple masks are separated by a pipe ('|').
126  * The value is modified on return to only hold unmatched bits.
127  */
128 static void
129 print_mask_part(FILE *fp, struct name_table *table, uintmax_t *valp,
130     bool *printed)
131 {
132 	uintmax_t rem;
133 
134 	rem = *valp;
135 	for (; table->str != NULL; table++) {
136 		if ((table->val & rem) == table->val) {
137 			/*
138 			 * Only print a zero mask if the raw value is
139 			 * zero.
140 			 */
141 			if (table->val == 0 && *valp != 0)
142 				continue;
143 			fprintf(fp, "%s%s", *printed ? "|" : "", table->str);
144 			*printed = true;
145 			rem &= ~table->val;
146 		}
147 	}
148 
149 	*valp = rem;
150 }
151 
152 /*
153  * Used when the value maps to a bitmask of #definition values in the
154  * table.  The return value is true if something was printed.  If
155  * rem is not NULL, *rem holds any bits not decoded if something was
156  * printed.  If nothing was printed and rem is not NULL, *rem holds
157  * the original value.
158  */
159 static bool
160 print_mask_int(FILE *fp, struct name_table *table, int ival, int *rem)
161 {
162 	uintmax_t val;
163 	bool printed;
164 
165 	printed = false;
166 	val = (unsigned)ival;
167 	print_mask_part(fp, table, &val, &printed);
168 	if (rem != NULL)
169 		*rem = val;
170 	return (printed);
171 }
172 
173 /*
174  * Used for a mask of optional flags where a value of 0 is valid.
175  */
176 static bool
177 print_mask_0(FILE *fp, struct name_table *table, int val, int *rem)
178 {
179 
180 	if (val == 0) {
181 		fputs("0", fp);
182 		if (rem != NULL)
183 			*rem = 0;
184 		return (true);
185 	}
186 	return (print_mask_int(fp, table, val, rem));
187 }
188 
189 /*
190  * Like print_mask_0 but for a unsigned long instead of an int.
191  */
192 static bool
193 print_mask_0ul(FILE *fp, struct name_table *table, u_long lval, u_long *rem)
194 {
195 	uintmax_t val;
196 	bool printed;
197 
198 	if (lval == 0) {
199 		fputs("0", fp);
200 		if (rem != NULL)
201 			*rem = 0;
202 		return (true);
203 	}
204 
205 	printed = false;
206 	val = lval;
207 	print_mask_part(fp, table, &val, &printed);
208 	if (rem != NULL)
209 		*rem = val;
210 	return (printed);
211 }
212 
213 static void
214 print_integer(FILE *fp, int val, int base)
215 {
216 
217 	switch (base) {
218 	case 8:
219 		fprintf(fp, "0%o", val);
220 		break;
221 	case 10:
222 		fprintf(fp, "%d", val);
223 		break;
224 	case 16:
225 		fprintf(fp, "0x%x", val);
226 		break;
227 	default:
228 		abort2("bad base", 0, NULL);
229 		break;
230 	}
231 }
232 
233 static bool
234 print_value(FILE *fp, struct name_table *table, uintmax_t val)
235 {
236 	const char *str;
237 
238 	str = lookup_value(table, val);
239 	if (str != NULL) {
240 		fputs(str, fp);
241 		return (true);
242 	}
243 	return (false);
244 }
245 
246 const char *
247 sysdecode_atfd(int fd)
248 {
249 
250 	if (fd == AT_FDCWD)
251 		return ("AT_FDCWD");
252 	return (NULL);
253 }
254 
255 bool
256 sysdecode_atflags(FILE *fp, int flag, int *rem)
257 {
258 
259 	return (print_mask_int(fp, atflags, flag, rem));
260 }
261 
262 static struct name_table semctlops[] = {
263 	X(GETNCNT) X(GETPID) X(GETVAL) X(GETALL) X(GETZCNT) X(SETVAL) X(SETALL)
264 	X(IPC_RMID) X(IPC_SET) X(IPC_STAT) XEND
265 };
266 
267 const char *
268 sysdecode_semctl_cmd(int cmd)
269 {
270 
271 	return (lookup_value(semctlops, cmd));
272 }
273 
274 static struct name_table shmctlops[] = {
275 	X(IPC_RMID) X(IPC_SET) X(IPC_STAT) XEND
276 };
277 
278 const char *
279 sysdecode_shmctl_cmd(int cmd)
280 {
281 
282 	return (lookup_value(shmctlops, cmd));
283 }
284 
285 const char *
286 sysdecode_msgctl_cmd(int cmd)
287 {
288 
289 	return (sysdecode_shmctl_cmd(cmd));
290 }
291 
292 static struct name_table semgetflags[] = {
293 	X(IPC_CREAT) X(IPC_EXCL) X(SEM_R) X(SEM_A) X((SEM_R>>3)) X((SEM_A>>3))
294 	X((SEM_R>>6)) X((SEM_A>>6)) XEND
295 };
296 
297 bool
298 sysdecode_semget_flags(FILE *fp, int flag, int *rem)
299 {
300 
301 	return (print_mask_int(fp, semgetflags, flag, rem));
302 }
303 
304 static struct name_table idtypes[] = {
305 	X(P_PID) X(P_PPID) X(P_PGID) X(P_SID) X(P_CID) X(P_UID) X(P_GID)
306 	X(P_ALL) X(P_LWPID) X(P_TASKID) X(P_PROJID) X(P_POOLID) X(P_JAILID)
307 	X(P_CTID) X(P_CPUID) X(P_PSETID) XEND
308 };
309 
310 /* XXX: idtype is really an idtype_t */
311 const char *
312 sysdecode_idtype(int idtype)
313 {
314 
315 	return (lookup_value(idtypes, idtype));
316 }
317 
318 /*
319  * [g|s]etsockopt's level argument can either be SOL_SOCKET or a
320  * protocol-specific value.
321  */
322 const char *
323 sysdecode_sockopt_level(int level)
324 {
325 	const char *str;
326 
327 	if (level == SOL_SOCKET)
328 		return ("SOL_SOCKET");
329 
330 	/* SOL_* constants for Bluetooth sockets. */
331 	str = lookup_value(ngbtsolevel, level);
332 	if (str != NULL)
333 		return (str);
334 
335 	/*
336 	 * IP and Infiniband sockets use IP protocols as levels.  Not all
337 	 * protocols are valid but it is simpler to just allow all of them.
338 	 *
339 	 * XXX: IPPROTO_IP == 0, but UNIX domain sockets use a level of 0
340 	 * for private options.
341 	 */
342 	str = sysdecode_ipproto(level);
343 	if (str != NULL)
344 		return (str);
345 
346 	return (NULL);
347 }
348 
349 bool
350 sysdecode_vmprot(FILE *fp, int type, int *rem)
351 {
352 
353 	return (print_mask_int(fp, vmprot, type, rem));
354 }
355 
356 static struct name_table sockflags[] = {
357 	X(SOCK_CLOEXEC) X(SOCK_NONBLOCK) XEND
358 };
359 
360 bool
361 sysdecode_socket_type(FILE *fp, int type, int *rem)
362 {
363 	const char *str;
364 	uintmax_t val;
365 	bool printed;
366 
367 	str = lookup_value(socktype, type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK));
368 	if (str != NULL) {
369 		fputs(str, fp);
370 		*rem = 0;
371 		printed = true;
372 	} else {
373 		*rem = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
374 		printed = false;
375 	}
376 	val = type & (SOCK_CLOEXEC | SOCK_NONBLOCK);
377 	print_mask_part(fp, sockflags, &val, &printed);
378 	return (printed);
379 }
380 
381 bool
382 sysdecode_access_mode(FILE *fp, int mode, int *rem)
383 {
384 
385 	return (print_mask_int(fp, accessmode, mode, rem));
386 }
387 
388 /* XXX: 'type' is really an acl_type_t. */
389 const char *
390 sysdecode_acltype(int type)
391 {
392 
393 	return (lookup_value(acltype, type));
394 }
395 
396 bool
397 sysdecode_cap_fcntlrights(FILE *fp, uint32_t rights, uint32_t *rem)
398 {
399 
400 	return (print_mask_int(fp, capfcntl, rights, rem));
401 }
402 
403 bool
404 sysdecode_close_range_flags(FILE *fp, int flags, int *rem)
405 {
406 
407 	return (print_mask_int(fp, closerangeflags, flags, rem));
408 }
409 
410 const char *
411 sysdecode_extattrnamespace(int namespace)
412 {
413 
414 	return (lookup_value(extattrns, namespace));
415 }
416 
417 const char *
418 sysdecode_fadvice(int advice)
419 {
420 
421 	return (lookup_value(fadvisebehav, advice));
422 }
423 
424 bool
425 sysdecode_open_flags(FILE *fp, int flags, int *rem)
426 {
427 	bool printed;
428 	int mode;
429 	uintmax_t val;
430 
431 	mode = flags & O_ACCMODE;
432 	flags &= ~O_ACCMODE;
433 	switch (mode) {
434 	case O_RDONLY:
435 		if (flags & O_EXEC) {
436 			flags &= ~O_EXEC;
437 			fputs("O_EXEC", fp);
438 		} else
439 			fputs("O_RDONLY", fp);
440 		printed = true;
441 		mode = 0;
442 		break;
443 	case O_WRONLY:
444 		fputs("O_WRONLY", fp);
445 		printed = true;
446 		mode = 0;
447 		break;
448 	case O_RDWR:
449 		fputs("O_RDWR", fp);
450 		printed = true;
451 		mode = 0;
452 		break;
453 	default:
454 		printed = false;
455 	}
456 	val = (unsigned)flags;
457 	print_mask_part(fp, openflags, &val, &printed);
458 	if (rem != NULL)
459 		*rem = val | mode;
460 	return (printed);
461 }
462 
463 bool
464 sysdecode_fcntl_fileflags(FILE *fp, int flags, int *rem)
465 {
466 	bool printed;
467 	int oflags;
468 
469 	/*
470 	 * The file flags used with F_GETFL/F_SETFL mostly match the
471 	 * flags passed to open(2).  However, a few open-only flag
472 	 * bits have been repurposed for fcntl-only flags.
473 	 */
474 	oflags = flags & ~(O_NOFOLLOW | FRDAHEAD);
475 	printed = sysdecode_open_flags(fp, oflags, rem);
476 	if (flags & O_NOFOLLOW) {
477 		fprintf(fp, "%sFPOIXSHM", printed ? "|" : "");
478 		printed = true;
479 	}
480 	if (flags & FRDAHEAD) {
481 		fprintf(fp, "%sFRDAHEAD", printed ? "|" : "");
482 		printed = true;
483 	}
484 	return (printed);
485 }
486 
487 bool
488 sysdecode_flock_operation(FILE *fp, int operation, int *rem)
489 {
490 
491 	return (print_mask_int(fp, flockops, operation, rem));
492 }
493 
494 static struct name_table getfsstatmode[] = {
495 	X(MNT_WAIT) X(MNT_NOWAIT) XEND
496 };
497 
498 const char *
499 sysdecode_getfsstat_mode(int mode)
500 {
501 
502 	return (lookup_value(getfsstatmode, mode));
503 }
504 
505 const char *
506 sysdecode_getrusage_who(int who)
507 {
508 
509 	return (lookup_value(rusage, who));
510 }
511 
512 static struct name_table kevent_user_ffctrl[] = {
513 	X(NOTE_FFNOP) X(NOTE_FFAND) X(NOTE_FFOR) X(NOTE_FFCOPY)
514 	XEND
515 };
516 
517 static struct name_table kevent_rdwr_fflags[] = {
518 	X(NOTE_LOWAT) X(NOTE_FILE_POLL) XEND
519 };
520 
521 static struct name_table kevent_vnode_fflags[] = {
522 	X(NOTE_DELETE) X(NOTE_WRITE) X(NOTE_EXTEND) X(NOTE_ATTRIB)
523 	X(NOTE_LINK) X(NOTE_RENAME) X(NOTE_REVOKE) X(NOTE_OPEN) X(NOTE_CLOSE)
524 	X(NOTE_CLOSE_WRITE) X(NOTE_READ) XEND
525 };
526 
527 static struct name_table kevent_proc_fflags[] = {
528 	X(NOTE_EXIT) X(NOTE_FORK) X(NOTE_EXEC) X(NOTE_TRACK) X(NOTE_TRACKERR)
529 	X(NOTE_CHILD) XEND
530 };
531 
532 static struct name_table kevent_timer_fflags[] = {
533 	X(NOTE_SECONDS) X(NOTE_MSECONDS) X(NOTE_USECONDS) X(NOTE_NSECONDS)
534 	X(NOTE_ABSTIME) XEND
535 };
536 
537 void
538 sysdecode_kevent_fflags(FILE *fp, short filter, int fflags, int base)
539 {
540 	int rem;
541 
542 	if (fflags == 0) {
543 		fputs("0", fp);
544 		return;
545 	}
546 
547 	switch (filter) {
548 	case EVFILT_READ:
549 	case EVFILT_WRITE:
550 		if (!print_mask_int(fp, kevent_rdwr_fflags, fflags, &rem))
551 			fprintf(fp, "%#x", rem);
552 		else if (rem != 0)
553 			fprintf(fp, "|%#x", rem);
554 		break;
555 	case EVFILT_VNODE:
556 		if (!print_mask_int(fp, kevent_vnode_fflags, fflags, &rem))
557 			fprintf(fp, "%#x", rem);
558 		else if (rem != 0)
559 			fprintf(fp, "|%#x", rem);
560 		break;
561 	case EVFILT_PROC:
562 	case EVFILT_PROCDESC:
563 		if (!print_mask_int(fp, kevent_proc_fflags, fflags, &rem))
564 			fprintf(fp, "%#x", rem);
565 		else if (rem != 0)
566 			fprintf(fp, "|%#x", rem);
567 		break;
568 	case EVFILT_TIMER:
569 		if (!print_mask_int(fp, kevent_timer_fflags, fflags, &rem))
570 			fprintf(fp, "%#x", rem);
571 		else if (rem != 0)
572 			fprintf(fp, "|%#x", rem);
573 		break;
574 	case EVFILT_USER: {
575 		unsigned int ctrl, data;
576 
577 		ctrl = fflags & NOTE_FFCTRLMASK;
578 		data = fflags & NOTE_FFLAGSMASK;
579 
580 		if (fflags & NOTE_TRIGGER) {
581 			fputs("NOTE_TRIGGER", fp);
582 			if (fflags == NOTE_TRIGGER)
583 				return;
584 			fputc('|', fp);
585 		}
586 
587 		/*
588 		 * An event with 'ctrl' == NOTE_FFNOP is either a reported
589 		 * (output) event for which only 'data' should be output
590 		 * or a pointless input event.  Assume that pointless
591 		 * input events don't occur in practice.  An event with
592 		 * NOTE_TRIGGER is always an input event.
593 		 */
594 		if (ctrl != NOTE_FFNOP || fflags & NOTE_TRIGGER) {
595 			fprintf(fp, "%s|%#x",
596 			    lookup_value(kevent_user_ffctrl, ctrl), data);
597 		} else {
598 			print_integer(fp, data, base);
599 		}
600 		break;
601 	}
602 	default:
603 		print_integer(fp, fflags, base);
604 		break;
605 	}
606 }
607 
608 bool
609 sysdecode_kevent_flags(FILE *fp, int flags, int *rem)
610 {
611 
612 	return (print_mask_int(fp, keventflags, flags, rem));
613 }
614 
615 const char *
616 sysdecode_kevent_filter(int filter)
617 {
618 
619 	return (lookup_value(keventfilters, filter));
620 }
621 
622 const char *
623 sysdecode_kldsym_cmd(int cmd)
624 {
625 
626 	return (lookup_value(kldsymcmd, cmd));
627 }
628 
629 const char *
630 sysdecode_kldunload_flags(int flags)
631 {
632 
633 	return (lookup_value(kldunloadfflags, flags));
634 }
635 
636 const char *
637 sysdecode_lio_listio_mode(int mode)
638 {
639 
640 	return (lookup_value(lio_listiomodes, mode));
641 }
642 
643 const char *
644 sysdecode_madvice(int advice)
645 {
646 
647 	return (lookup_value(madvisebehav, advice));
648 }
649 
650 const char *
651 sysdecode_minherit_inherit(int inherit)
652 {
653 
654 	return (lookup_value(minheritflags, inherit));
655 }
656 
657 bool
658 sysdecode_mlockall_flags(FILE *fp, int flags, int *rem)
659 {
660 
661 	return (print_mask_int(fp, mlockallflags, flags, rem));
662 }
663 
664 bool
665 sysdecode_mmap_prot(FILE *fp, int prot, int *rem)
666 {
667 	int protm;
668 	bool printed;
669 
670 	printed = false;
671 	protm = PROT_MAX_EXTRACT(prot);
672 	prot &= ~PROT_MAX(protm);
673 	if (protm != 0) {
674 		fputs("PROT_MAX(", fp);
675 		printed = print_mask_int(fp, mmapprot, protm, rem);
676 		fputs(")|", fp);
677 	}
678 	return (print_mask_int(fp, mmapprot, prot, rem) || printed);
679 }
680 
681 bool
682 sysdecode_fileflags(FILE *fp, fflags_t flags, fflags_t *rem)
683 {
684 
685 	return (print_mask_0(fp, fileflags, flags, rem));
686 }
687 
688 bool
689 sysdecode_filemode(FILE *fp, int mode, int *rem)
690 {
691 
692 	return (print_mask_0(fp, filemode, mode, rem));
693 }
694 
695 bool
696 sysdecode_mount_flags(FILE *fp, int flags, int *rem)
697 {
698 
699 	return (print_mask_int(fp, mountflags, flags, rem));
700 }
701 
702 bool
703 sysdecode_msync_flags(FILE *fp, int flags, int *rem)
704 {
705 
706 	return (print_mask_int(fp, msyncflags, flags, rem));
707 }
708 
709 const char *
710 sysdecode_nfssvc_flags(int flags)
711 {
712 
713 	return (lookup_value(nfssvcflags, flags));
714 }
715 
716 static struct name_table pipe2flags[] = {
717 	X(O_CLOEXEC) X(O_NONBLOCK) XEND
718 };
719 
720 bool
721 sysdecode_pipe2_flags(FILE *fp, int flags, int *rem)
722 {
723 
724 	return (print_mask_0(fp, pipe2flags, flags, rem));
725 }
726 
727 const char *
728 sysdecode_prio_which(int which)
729 {
730 
731 	return (lookup_value(prio, which));
732 }
733 
734 const char *
735 sysdecode_procctl_cmd(int cmd)
736 {
737 
738 	return (lookup_value(procctlcmd, cmd));
739 }
740 
741 const char *
742 sysdecode_ptrace_request(int request)
743 {
744 
745 	return (lookup_value(ptraceop, request));
746 }
747 
748 static struct name_table quotatypes[] = {
749 	X(GRPQUOTA) X(USRQUOTA) XEND
750 };
751 
752 bool
753 sysdecode_quotactl_cmd(FILE *fp, int cmd)
754 {
755 	const char *primary, *type;
756 
757 	primary = lookup_value(quotactlcmds, cmd >> SUBCMDSHIFT);
758 	if (primary == NULL)
759 		return (false);
760 	fprintf(fp, "QCMD(%s,", primary);
761 	type = lookup_value(quotatypes, cmd & SUBCMDMASK);
762 	if (type != NULL)
763 		fprintf(fp, "%s", type);
764 	else
765 		fprintf(fp, "%#x", cmd & SUBCMDMASK);
766 	fprintf(fp, ")");
767 	return (true);
768 }
769 
770 bool
771 sysdecode_reboot_howto(FILE *fp, int howto, int *rem)
772 {
773 	bool printed;
774 
775 	/*
776 	 * RB_AUTOBOOT is special in that its value is zero, but it is
777 	 * also an implied argument if a different operation is not
778 	 * requested via RB_HALT, RB_POWERCYCLE, RB_POWEROFF, or
779 	 * RB_REROOT.
780 	 */
781 	if (howto != 0 && (howto & (RB_HALT | RB_POWEROFF | RB_REROOT |
782 	    RB_POWERCYCLE)) == 0) {
783 		fputs("RB_AUTOBOOT|", fp);
784 		printed = true;
785 	} else
786 		printed = false;
787 	return (print_mask_int(fp, rebootopt, howto, rem) || printed);
788 }
789 
790 bool
791 sysdecode_rfork_flags(FILE *fp, int flags, int *rem)
792 {
793 
794 	return (print_mask_int(fp, rforkflags, flags, rem));
795 }
796 
797 const char *
798 sysdecode_rlimit(int resource)
799 {
800 
801 	return (lookup_value(rlimit, resource));
802 }
803 
804 const char *
805 sysdecode_scheduler_policy(int policy)
806 {
807 
808 	return (lookup_value(schedpolicy, policy));
809 }
810 
811 bool
812 sysdecode_sendfile_flags(FILE *fp, int flags, int *rem)
813 {
814 
815 	return (print_mask_int(fp, sendfileflags, flags, rem));
816 }
817 
818 bool
819 sysdecode_shmat_flags(FILE *fp, int flags, int *rem)
820 {
821 
822 	return (print_mask_int(fp, shmatflags, flags, rem));
823 }
824 
825 const char *
826 sysdecode_shutdown_how(int how)
827 {
828 
829 	return (lookup_value(shutdownhow, how));
830 }
831 
832 const char *
833 sysdecode_sigbus_code(int si_code)
834 {
835 
836 	return (lookup_value(sigbuscode, si_code));
837 }
838 
839 const char *
840 sysdecode_sigchld_code(int si_code)
841 {
842 
843 	return (lookup_value(sigchldcode, si_code));
844 }
845 
846 const char *
847 sysdecode_sigfpe_code(int si_code)
848 {
849 
850 	return (lookup_value(sigfpecode, si_code));
851 }
852 
853 const char *
854 sysdecode_sigill_code(int si_code)
855 {
856 
857 	return (lookup_value(sigillcode, si_code));
858 }
859 
860 const char *
861 sysdecode_sigsegv_code(int si_code)
862 {
863 
864 	return (lookup_value(sigsegvcode, si_code));
865 }
866 
867 const char *
868 sysdecode_sigtrap_code(int si_code)
869 {
870 
871 	return (lookup_value(sigtrapcode, si_code));
872 }
873 
874 const char *
875 sysdecode_sigprocmask_how(int how)
876 {
877 
878 	return (lookup_value(sigprocmaskhow, how));
879 }
880 
881 const char *
882 sysdecode_socketdomain(int domain)
883 {
884 
885 	return (lookup_value(sockdomain, domain));
886 }
887 
888 const char *
889 sysdecode_socket_protocol(int domain, int protocol)
890 {
891 
892 	switch (domain) {
893 	case PF_INET:
894 	case PF_INET6:
895 		return (lookup_value(sockipproto, protocol));
896 	default:
897 		return (NULL);
898 	}
899 }
900 
901 const char *
902 sysdecode_sockaddr_family(int sa_family)
903 {
904 
905 	return (lookup_value(sockfamily, sa_family));
906 }
907 
908 const char *
909 sysdecode_ipproto(int protocol)
910 {
911 
912 	return (lookup_value(sockipproto, protocol));
913 }
914 
915 const char *
916 sysdecode_sockopt_name(int level, int optname)
917 {
918 
919 	if (level == SOL_SOCKET)
920 		return (lookup_value(sockopt, optname));
921 	if (level == IPPROTO_IP)
922 		/* XXX: UNIX domain socket options use a level of 0 also. */
923 		return (lookup_value(sockoptip, optname));
924 	if (level == IPPROTO_IPV6)
925 		return (lookup_value(sockoptipv6, optname));
926 	if (level == IPPROTO_SCTP)
927 		return (lookup_value(sockoptsctp, optname));
928 	if (level == IPPROTO_TCP)
929 		return (lookup_value(sockopttcp, optname));
930 	if (level == IPPROTO_UDP)
931 		return (lookup_value(sockoptudp, optname));
932 	if (level == IPPROTO_UDPLITE)
933 		return (lookup_value(sockoptudplite, optname));
934 	return (NULL);
935 }
936 
937 bool
938 sysdecode_thr_create_flags(FILE *fp, int flags, int *rem)
939 {
940 
941 	return (print_mask_int(fp, thrcreateflags, flags, rem));
942 }
943 
944 const char *
945 sysdecode_umtx_op(int op)
946 {
947 
948 	return (lookup_value(umtxop, op));
949 }
950 
951 bool
952 sysdecode_umtx_op_flags(FILE *fp, int op, int *rem)
953 {
954 	uintmax_t val;
955 	bool printed;
956 
957 	printed = false;
958 	val = (unsigned)op;
959 	print_mask_part(fp, umtxopflags, &val, &printed);
960 	if (rem != NULL)
961 		*rem = val;
962 	return (printed);
963 }
964 
965 const char *
966 sysdecode_vmresult(int result)
967 {
968 
969 	return (lookup_value(vmresult, result));
970 }
971 
972 bool
973 sysdecode_wait4_options(FILE *fp, int options, int *rem)
974 {
975 	bool printed;
976 	int opt6;
977 
978 	/* A flags value of 0 is normal. */
979 	if (options == 0) {
980 		fputs("0", fp);
981 		if (rem != NULL)
982 			*rem = 0;
983 		return (true);
984 	}
985 
986 	/*
987 	 * These flags are implicit and aren't valid flags for wait4()
988 	 * directly (though they don't fail with EINVAL).
989 	 */
990 	opt6 = options & (WEXITED | WTRAPPED);
991 	options &= ~opt6;
992 	printed = print_mask_int(fp, wait6opt, options, rem);
993 	if (rem != NULL)
994 		*rem |= opt6;
995 	return (printed);
996 }
997 
998 bool
999 sysdecode_wait6_options(FILE *fp, int options, int *rem)
1000 {
1001 
1002 	return (print_mask_int(fp, wait6opt, options, rem));
1003 }
1004 
1005 const char *
1006 sysdecode_whence(int whence)
1007 {
1008 
1009 	return (lookup_value(seekwhence, whence));
1010 }
1011 
1012 const char *
1013 sysdecode_fcntl_cmd(int cmd)
1014 {
1015 
1016 	return (lookup_value(fcntlcmd, cmd));
1017 }
1018 
1019 static struct name_table fcntl_fd_arg[] = {
1020 	X(FD_CLOEXEC) X(0) XEND
1021 };
1022 
1023 bool
1024 sysdecode_fcntl_arg_p(int cmd)
1025 {
1026 
1027 	switch (cmd) {
1028 	case F_GETFD:
1029 	case F_GETFL:
1030 	case F_GETOWN:
1031 		return (false);
1032 	default:
1033 		return (true);
1034 	}
1035 }
1036 
1037 void
1038 sysdecode_fcntl_arg(FILE *fp, int cmd, uintptr_t arg, int base)
1039 {
1040 	int rem;
1041 
1042 	switch (cmd) {
1043 	case F_SETFD:
1044 		if (!print_value(fp, fcntl_fd_arg, arg))
1045 		    print_integer(fp, arg, base);
1046 		break;
1047 	case F_SETFL:
1048 		if (!sysdecode_fcntl_fileflags(fp, arg, &rem))
1049 			fprintf(fp, "%#x", rem);
1050 		else if (rem != 0)
1051 			fprintf(fp, "|%#x", rem);
1052 		break;
1053 	case F_GETLK:
1054 	case F_SETLK:
1055 	case F_SETLKW:
1056 		fprintf(fp, "%p", (void *)arg);
1057 		break;
1058 	default:
1059 		print_integer(fp, arg, base);
1060 		break;
1061 	}
1062 }
1063 
1064 bool
1065 sysdecode_mmap_flags(FILE *fp, int flags, int *rem)
1066 {
1067 	uintmax_t val;
1068 	bool printed;
1069 	int align;
1070 
1071 	/*
1072 	 * MAP_ALIGNED can't be handled directly by print_mask_int().
1073 	 * MAP_32BIT is also problematic since it isn't defined for
1074 	 * all platforms.
1075 	 */
1076 	printed = false;
1077 	align = flags & MAP_ALIGNMENT_MASK;
1078 	val = (unsigned)flags & ~MAP_ALIGNMENT_MASK;
1079 	print_mask_part(fp, mmapflags, &val, &printed);
1080 #ifdef MAP_32BIT
1081 	if (val & MAP_32BIT) {
1082 		fprintf(fp, "%sMAP_32BIT", printed ? "|" : "");
1083 		printed = true;
1084 		val &= ~MAP_32BIT;
1085 	}
1086 #endif
1087 	if (align != 0) {
1088 		if (printed)
1089 			fputc('|', fp);
1090 		if (align == MAP_ALIGNED_SUPER)
1091 			fputs("MAP_ALIGNED_SUPER", fp);
1092 		else
1093 			fprintf(fp, "MAP_ALIGNED(%d)",
1094 			    align >> MAP_ALIGNMENT_SHIFT);
1095 		printed = true;
1096 	}
1097 	if (rem != NULL)
1098 		*rem = val;
1099 	return (printed);
1100 }
1101 
1102 const char *
1103 sysdecode_pathconf_name(int name)
1104 {
1105 
1106 	return (lookup_value(pathconfname, name));
1107 }
1108 
1109 const char *
1110 sysdecode_rtprio_function(int function)
1111 {
1112 
1113 	return (lookup_value(rtpriofuncs, function));
1114 }
1115 
1116 bool
1117 sysdecode_msg_flags(FILE *fp, int flags, int *rem)
1118 {
1119 
1120 	return (print_mask_0(fp, msgflags, flags, rem));
1121 }
1122 
1123 const char *
1124 sysdecode_sigcode(int sig, int si_code)
1125 {
1126 	const char *str;
1127 
1128 	str = lookup_value(sigcode, si_code);
1129 	if (str != NULL)
1130 		return (str);
1131 
1132 	switch (sig) {
1133 	case SIGILL:
1134 		return (sysdecode_sigill_code(si_code));
1135 	case SIGBUS:
1136 		return (sysdecode_sigbus_code(si_code));
1137 	case SIGSEGV:
1138 		return (sysdecode_sigsegv_code(si_code));
1139 	case SIGFPE:
1140 		return (sysdecode_sigfpe_code(si_code));
1141 	case SIGTRAP:
1142 		return (sysdecode_sigtrap_code(si_code));
1143 	case SIGCHLD:
1144 		return (sysdecode_sigchld_code(si_code));
1145 	default:
1146 		return (NULL);
1147 	}
1148 }
1149 
1150 const char *
1151 sysdecode_sysarch_number(int number)
1152 {
1153 
1154 	return (lookup_value(sysarchnum, number));
1155 }
1156 
1157 bool
1158 sysdecode_umtx_cvwait_flags(FILE *fp, u_long flags, u_long *rem)
1159 {
1160 
1161 	return (print_mask_0ul(fp, umtxcvwaitflags, flags, rem));
1162 }
1163 
1164 bool
1165 sysdecode_umtx_rwlock_flags(FILE *fp, u_long flags, u_long *rem)
1166 {
1167 
1168 	return (print_mask_0ul(fp, umtxrwlockflags, flags, rem));
1169 }
1170 
1171 void
1172 sysdecode_cap_rights(FILE *fp, cap_rights_t *rightsp)
1173 {
1174 	struct name_table *t;
1175 	int i;
1176 	bool comma;
1177 
1178 	for (i = 0; i < CAPARSIZE(rightsp); i++) {
1179 		if (CAPIDXBIT(rightsp->cr_rights[i]) != 1 << i) {
1180 			fprintf(fp, "invalid cap_rights_t");
1181 			return;
1182 		}
1183 	}
1184 	comma = false;
1185 	for (t = caprights; t->str != NULL; t++) {
1186 		if (cap_rights_is_set(rightsp, t->val)) {
1187 			fprintf(fp, "%s%s", comma ? "," : "", t->str);
1188 			comma = true;
1189 		}
1190 	}
1191 }
1192 
1193 static struct name_table cmsgtypeip[] = {
1194 	X(IP_RECVDSTADDR) X(IP_RECVTTL) X(IP_RECVOPTS) X(IP_RECVRETOPTS)
1195 	X(IP_RECVIF) X(IP_RECVTOS) X(IP_FLOWID) X(IP_FLOWTYPE)
1196 	X(IP_RSSBUCKETID) XEND
1197 };
1198 
1199 static struct name_table cmsgtypeipv6[] = {
1200 #if 0
1201 	/* The RFC 2292 defines are kernel space only. */
1202 	X(IPV6_2292PKTINFO) X(IPV6_2292HOPLIMIT) X(IPV6_2292HOPOPTS)
1203 	X(IPV6_2292DSTOPTS) X(IPV6_2292RTHDR) X(IPV6_2292NEXTHOP)
1204 #endif
1205 	X(IPV6_PKTINFO)  X(IPV6_HOPLIMIT) X(IPV6_HOPOPTS)
1206 	X(IPV6_DSTOPTS) X(IPV6_RTHDR) X(IPV6_NEXTHOP)
1207 	X(IPV6_TCLASS) X(IPV6_FLOWID) X(IPV6_FLOWTYPE) X(IPV6_RSSBUCKETID)
1208 	X(IPV6_PATHMTU) X(IPV6_RTHDRDSTOPTS) X(IPV6_USE_MIN_MTU)
1209 	X(IPV6_DONTFRAG) X(IPV6_PREFER_TEMPADDR) XEND
1210 };
1211 
1212 static struct name_table cmsgtypesctp[] = {
1213 	X(SCTP_INIT) X(SCTP_SNDRCV) X(SCTP_EXTRCV) X(SCTP_SNDINFO)
1214 	X(SCTP_RCVINFO) X(SCTP_NXTINFO) X(SCTP_PRINFO) X(SCTP_AUTHINFO)
1215 	X(SCTP_DSTADDRV4) X(SCTP_DSTADDRV6) XEND
1216 };
1217 
1218 const char *
1219 sysdecode_cmsg_type(int cmsg_level, int cmsg_type)
1220 {
1221 
1222 	if (cmsg_level == SOL_SOCKET)
1223 		return (lookup_value(cmsgtypesocket, cmsg_type));
1224 	if (cmsg_level == IPPROTO_IP)
1225 		return (lookup_value(cmsgtypeip, cmsg_type));
1226 	if (cmsg_level == IPPROTO_IPV6)
1227 		return (lookup_value(cmsgtypeipv6, cmsg_type));
1228 	if (cmsg_level == IPPROTO_SCTP)
1229 		return (lookup_value(cmsgtypesctp, cmsg_type));
1230 	return (NULL);
1231 }
1232 
1233 const char *
1234 sysdecode_sctp_pr_policy(int policy)
1235 {
1236 
1237 	return (lookup_value(sctpprpolicy, policy));
1238 }
1239 
1240 static struct name_table sctpsndflags[] = {
1241 	X(SCTP_EOF) X(SCTP_ABORT) X(SCTP_UNORDERED) X(SCTP_ADDR_OVER)
1242 	X(SCTP_SENDALL) X(SCTP_EOR) X(SCTP_SACK_IMMEDIATELY) XEND
1243 };
1244 
1245 bool
1246 sysdecode_sctp_snd_flags(FILE *fp, int flags, int *rem)
1247 {
1248 
1249 	return (print_mask_int(fp, sctpsndflags, flags, rem));
1250 }
1251 
1252 static struct name_table sctprcvflags[] = {
1253 	X(SCTP_UNORDERED) XEND
1254 };
1255 
1256 bool
1257 sysdecode_sctp_rcv_flags(FILE *fp, int flags, int *rem)
1258 {
1259 
1260 	return (print_mask_int(fp, sctprcvflags, flags, rem));
1261 }
1262 
1263 static struct name_table sctpnxtflags[] = {
1264 	X(SCTP_UNORDERED) X(SCTP_COMPLETE) X(SCTP_NOTIFICATION) XEND
1265 };
1266 
1267 bool
1268 sysdecode_sctp_nxt_flags(FILE *fp, int flags, int *rem)
1269 {
1270 
1271 	return (print_mask_int(fp, sctpnxtflags, flags, rem));
1272 }
1273 
1274 static struct name_table sctpsinfoflags[] = {
1275 	X(SCTP_EOF) X(SCTP_ABORT) X(SCTP_UNORDERED) X(SCTP_ADDR_OVER)
1276 	X(SCTP_SENDALL) X(SCTP_EOR) X(SCTP_SACK_IMMEDIATELY) XEND
1277 };
1278 
1279 void
1280 sysdecode_sctp_sinfo_flags(FILE *fp, int sinfo_flags)
1281 {
1282 	const char *temp;
1283 	int rem;
1284 	bool printed;
1285 
1286 	printed = print_mask_0(fp, sctpsinfoflags, sinfo_flags, &rem);
1287 	if (rem & ~SCTP_PR_SCTP_ALL) {
1288 		fprintf(fp, "%s%#x", printed ? "|" : "", rem & ~SCTP_PR_SCTP_ALL);
1289 		printed = true;
1290 		rem &= ~SCTP_PR_SCTP_ALL;
1291 	}
1292 	if (rem != 0) {
1293 		temp = sysdecode_sctp_pr_policy(rem);
1294 		if (temp != NULL) {
1295 			fprintf(fp, "%s%s", printed ? "|" : "", temp);
1296 		} else {
1297 			fprintf(fp, "%s%#x", printed ? "|" : "", rem);
1298 		}
1299 	}
1300 }
1301 
1302 bool
1303 sysdecode_shmflags(FILE *fp, int flags, int *rem)
1304 {
1305 
1306 	return (print_mask_0(fp, shmflags, flags, rem));
1307 }
1308