]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - usr.bin/kdump/mksubr
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / usr.bin / kdump / mksubr
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 "David Kirchner" <dpk@dpk.net>. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 # 1. Redistributions of source code must retain the above copyright
9 #    notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 #    notice, this list of conditions and the following disclaimer in the
12 #    documentation and/or other materials provided with the distribution.
13 #
14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 # SUCH DAMAGE.
25 #
26 # $FreeBSD$
27 #
28 # Generates kdump_subr.c
29 # mkioctls is a special-purpose script, and works fine as it is
30 # now, so it remains independent. The idea behind how it generates
31 # its list was heavily borrowed here.
32 #
33 # Some functions here are automatically generated. This can mean
34 # the user will see unusual kdump output or errors while building
35 # if the underlying .h files are changed significantly.
36 #
37 # Key:
38 # AUTO: Completely auto-generated with either the "or" or the "switch"
39 # method.
40 # AUTO - Special: Generated automatically, but with some extra commands
41 # that the auto_*_type() functions are inappropriate for.
42 # MANUAL: Manually entered and must therefore be manually updated.
43
44 set -e
45
46 LC_ALL=C; export LC_ALL
47
48 if [ -z "$1" ]
49 then
50         echo "usage: sh $0 include-dir"
51         exit 1
52 fi
53 include_dir=$1
54
55 #
56 # Automatically generates a C function that will print out the
57 # numeric input as a pipe-delimited string of the appropriate
58 # #define keys. ex:
59 # S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH
60 # The XOR is necessary to prevent including the "0"-value in every
61 # line.
62 #
63 auto_or_type () {
64         local name grep file
65         name=$1
66         grep=$2
67         file=$3
68
69         cat <<_EOF_
70 /* AUTO */
71 void
72 $name(intmax_t arg)
73 {
74         int or = 0;
75         printf("%#jx<", (uintmax_t)arg);
76 _EOF_
77         egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
78                 $include_dir/$file | \
79         awk '{ for (i = 1; i <= NF; i++) \
80                 if ($i ~ /define/) \
81                         break; \
82                 ++i; \
83                 printf "\tif (!((arg > 0) ^ ((%s) > 0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
84 cat <<_EOF_
85         printf(">");
86         if (or == 0)
87                 printf("<invalid>%jd", arg);
88 }
89
90 _EOF_
91 }
92
93 #
94 # Automatically generates a C function used when the argument
95 # maps to a single, specific #definition
96 #
97 auto_switch_type () {
98         local name grep file
99         name=$1
100         grep=$2
101         file=$3
102
103         cat <<_EOF_
104 /* AUTO */
105 void
106 $name(intmax_t arg)
107 {
108         switch (arg) {
109 _EOF_
110         egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
111                 $include_dir/$file | \
112         awk '{ for (i = 1; i <= NF; i++) \
113                 if ($i ~ /define/) \
114                         break; \
115                 ++i; \
116                 printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i }'
117 cat <<_EOF_
118         default: /* Should not reach */
119                 printf("<invalid=%jd>", arg);
120         }
121 }
122
123 _EOF_
124 }
125
126 #
127 # Automatically generates a C function used when the argument
128 # maps to a #definition
129 #
130 auto_if_type () {
131         local name grep file
132         name=$1
133         grep=$2
134         file=$3
135
136         cat <<_EOF_
137 /* AUTO */
138 void
139 $name(intmax_t arg)
140 {
141 _EOF_
142         egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
143                 $include_dir/$file | \
144         awk '{ printf "\t"; \
145                 if (NR > 1) \
146                         printf "else " ; \
147                 printf "if (arg == %s) \n\t\tprintf(\"%s\");\n", $2, $2 }'
148 cat <<_EOF_
149         else /* Should not reach */
150                 printf("<invalid=%jd>", arg);
151 }
152
153 _EOF_
154 }
155
156 # C start
157
158 cat <<_EOF_
159 #include <errno.h>
160 #include <stdint.h>
161 #include <stdio.h>
162 #include <sys/fcntl.h>
163 #include <sys/stat.h>
164 #include <sys/unistd.h>
165 #include <sys/mman.h>
166 #include <sys/wait.h>
167 #define _KERNEL
168 #include <sys/socket.h>
169 #undef _KERNEL
170 #include <netinet/in.h>
171 #include <sys/param.h>
172 #include <sys/mount.h>
173 #include <sys/procctl.h>
174 #include <sys/ptrace.h>
175 #include <sys/resource.h>
176 #include <sys/reboot.h>
177 #include <sched.h>
178 #include <sys/linker.h>
179 #define _KERNEL
180 #include <sys/thr.h>
181 #undef _KERNEL
182 #include <sys/extattr.h>
183 #include <sys/acl.h>
184 #include <aio.h>
185 #include <sys/sem.h>
186 #include <sys/ipc.h>
187 #include <sys/rtprio.h>
188 #include <sys/shm.h>
189 #include <machine/atomic.h>
190 #include <sys/umtx.h>
191 #include <nfsserver/nfs.h>
192 #include <ufs/ufs/quota.h>
193 #include <sys/capsicum.h>
194 #include <vm/vm.h>
195 #include <vm/vm_param.h>
196
197 #include "kdump_subr.h"
198
199 /*
200  * These are simple support macros. print_or utilizes a variable
201  * defined in the calling function to track whether or not it should
202  * print a logical-OR character ('|') before a string. if_print_or
203  * simply handles the necessary "if" statement used in many lines
204  * of this file.
205  */
206 #define print_or(str,orflag) do {                  \\
207         if (orflag) putchar('|'); else orflag = 1; \\
208         printf (str); }                            \\
209         while (0)
210 #define if_print_or(i,flag,orflag) do {            \\
211         if ((i & flag) == flag)                    \\
212         print_or(#flag,orflag); }                  \\
213         while (0)
214
215 /* MANUAL */
216 void
217 atfdname(int fd, int decimal)
218 {
219         if (fd == AT_FDCWD)
220                 printf("AT_FDCWD");
221         else if (decimal)
222                 printf("%d", fd);
223         else
224                 printf("%#x", fd);
225 }
226
227 /* MANUAL */
228 extern char *signames[]; /* from kdump.c */
229 void
230 signame(int sig)
231 {
232         if (sig > 0 && sig < NSIG)
233                 printf("SIG%s",signames[sig]);
234         else
235                 printf("SIG %d", sig);
236 }
237
238 /* MANUAL */
239 void
240 semctlname(int cmd)
241 {
242         switch (cmd) {
243         case GETNCNT:
244                 printf("GETNCNT");
245                 break;
246         case GETPID:
247                 printf("GETPID");
248                 break;
249         case GETVAL:
250                 printf("GETVAL");
251                 break;
252         case GETALL:
253                 printf("GETALL");
254                 break;
255         case GETZCNT:
256                 printf("GETZCNT");
257                 break;
258         case SETVAL:
259                 printf("SETVAL");
260                 break;
261         case SETALL:
262                 printf("SETALL");
263                 break;
264         case IPC_RMID:
265                 printf("IPC_RMID");
266                 break;
267         case IPC_SET:
268                 printf("IPC_SET");
269                 break;
270         case IPC_STAT:
271                 printf("IPC_STAT");
272                 break;
273         default: /* Should not reach */
274                 printf("<invalid=%d>", cmd);
275         }
276 }
277
278 /* MANUAL */
279 void
280 shmctlname(int cmd)
281 {
282         switch (cmd) {
283         case IPC_RMID:
284                 printf("IPC_RMID");
285                 break;
286         case IPC_SET:
287                 printf("IPC_SET");
288                 break;
289         case IPC_STAT:
290                 printf("IPC_STAT");
291                 break;
292         default: /* Should not reach */
293                 printf("<invalid=%d>", cmd);
294         }
295 }
296
297 /* MANUAL */
298 void
299 semgetname(int flag)
300 {
301         int or = 0;
302         if_print_or(flag, IPC_CREAT, or);
303         if_print_or(flag, IPC_EXCL, or);
304         if_print_or(flag, SEM_R, or);
305         if_print_or(flag, SEM_A, or);
306         if_print_or(flag, (SEM_R>>3), or);
307         if_print_or(flag, (SEM_A>>3), or);
308         if_print_or(flag, (SEM_R>>6), or);
309         if_print_or(flag, (SEM_A>>6), or);
310 }
311
312 /*
313  * MANUAL
314  *
315  * Only used by SYS_open. Unless O_CREAT is set in flags, the
316  * mode argument is unused (and often bogus and misleading).
317  */
318 void
319 flagsandmodename(int flags, int mode, int decimal)
320 {
321         flagsname(flags);
322         putchar(',');
323         if ((flags & O_CREAT) == O_CREAT) {
324                 modename (mode);
325         } else {
326                 if (decimal) {
327                         printf("<unused>%d", mode);
328                 } else {
329                         printf("<unused>%#x", (unsigned int)mode);
330                 }
331         }
332 }
333
334 /* MANUAL */
335 void
336 idtypename(idtype_t idtype, int decimal)
337 {
338         switch(idtype) {
339         case P_PID:
340                 printf("P_PID");
341                 break;
342         case P_PPID:
343                 printf("P_PPID");
344                 break;
345         case P_PGID:
346                 printf("P_PGID");
347                 break;
348         case P_SID:
349                 printf("P_SID");
350                 break;
351         case P_CID:
352                 printf("P_CID");
353                 break;
354         case P_UID:
355                 printf("P_UID");
356                 break;
357         case P_GID:
358                 printf("P_GID");
359                 break;
360         case P_ALL:
361                 printf("P_ALL");
362                 break;
363         case P_LWPID:
364                 printf("P_LWPID");
365                 break;
366         case P_TASKID:
367                 printf("P_TASKID");
368                 break;
369         case P_PROJID:
370                 printf("P_PROJID");
371                 break;
372         case P_POOLID:
373                 printf("P_POOLID");
374                 break;
375         case P_JAILID:
376                 printf("P_JAILID");
377                 break;
378         case P_CTID:
379                 printf("P_CTID");
380                 break;
381         case P_CPUID:
382                 printf("P_CPUID");
383                 break;
384         case P_PSETID:
385                 printf("P_PSETID");
386                 break;
387         default:
388                 if (decimal) {
389                         printf("%d", idtype);
390                 } else {
391                         printf("%#x", idtype);
392                 }
393         }
394 }
395
396 /*
397  * MANUAL
398  *
399  * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value
400  * referring to a line in /etc/protocols . It might be appropriate
401  * to use getprotoent(3) here.
402  */
403 void
404 sockoptlevelname(int level, int decimal)
405 {
406         if (level == SOL_SOCKET) {
407                 printf("SOL_SOCKET");
408         } else {
409                 if (decimal) {
410                         printf("%d", level);
411                 } else {
412                         printf("%#x", (unsigned int)level);
413                 }
414         }
415 }
416
417 /*
418  * MANUAL
419  *
420  * Used for page fault type.  Cannot use auto_or_type since the macro
421  * values contain a cast.  Also, VM_PROT_NONE has to be handled specially.
422  */
423 void
424 vmprotname (int type)
425 {
426         int     or = 0;
427
428         if (type == VM_PROT_NONE) {
429                 (void)printf("VM_PROT_NONE");
430                 return;
431         }
432         if_print_or(type, VM_PROT_READ, or);
433         if_print_or(type, VM_PROT_WRITE, or);
434         if_print_or(type, VM_PROT_EXECUTE, or);
435         if_print_or(type, VM_PROT_COPY, or);
436 }
437
438 /*
439  * MANUAL
440  */
441 void
442 socktypenamewithflags(int type)
443 {
444         if (type & SOCK_CLOEXEC)
445                 printf("SOCK_CLOEXEC|"), type &= ~SOCK_CLOEXEC;
446         if (type & SOCK_NONBLOCK)
447                 printf("SOCK_NONBLOCK|"), type &= ~SOCK_NONBLOCK;
448         socktypename(type);
449 }
450 _EOF_
451
452 auto_or_type     "accessmodename"      "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+"         "sys/unistd.h"
453 auto_switch_type "acltypename"         "ACL_TYPE_[A-Z4_]+[[:space:]]+0x[0-9]+"        "sys/acl.h"
454 auto_or_type     "capfcntlname"        "CAP_FCNTL_[A-Z]+[[:space:]]+\(1"              "sys/capsicum.h"
455 auto_switch_type "extattrctlname"      "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h"
456 auto_switch_type "fadvisebehavname"    "POSIX_FADV_[A-Z]+[[:space:]]+[0-9]+"          "sys/fcntl.h"
457 auto_or_type     "flagsname"           "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+"           "sys/fcntl.h"
458 auto_or_type     "flockname"           "LOCK_[A-Z]+[[:space:]]+0x[0-9]+"              "sys/fcntl.h"
459 auto_or_type     "getfsstatflagsname"  "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*"            "sys/mount.h"
460 auto_switch_type "kldsymcmdname"       "KLDSYM_[A-Z]+[[:space:]]+[0-9]+"              "sys/linker.h"
461 auto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+"       "sys/linker.h"
462 auto_switch_type "lio_listioname"      "LIO_(NO)?WAIT[[:space:]]+[0-9]+"              "aio.h"
463 auto_switch_type "madvisebehavname"    "_?MADV_[A-Z]+[[:space:]]+[0-9]+"              "sys/mman.h"
464 auto_switch_type "minheritname"        "INHERIT_[A-Z]+[[:space:]]+[0-9]+"             "sys/mman.h"
465 auto_or_type     "mlockallname"        "MCL_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/mman.h"
466 auto_or_type     "mmapprotname"        "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+"        "sys/mman.h"
467 auto_or_type     "modename"            "S_[A-Z]+[[:space:]]+[0-6]{7}"                 "sys/stat.h"
468 auto_or_type     "mountflagsname"      "MNT_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/mount.h"
469 auto_switch_type "msyncflagsname"      "MS_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/mman.h"
470 auto_or_type     "nfssvcname"          "NFSSVC_[A-Z0-9]+[[:space:]]+0x[0-9]+"            "nfs/nfssvc.h"
471 auto_switch_type "prioname"            "PRIO_[A-Z]+[[:space:]]+[0-9]"                 "sys/resource.h"
472 auto_switch_type "procctlcmdname"      "PROC_[A-Z]+[[:space:]]+[0-9]"                 "sys/procctl.h"
473 auto_switch_type "ptraceopname"        "PT_[[:alnum:]_]+[[:space:]]+[0-9]+"           "sys/ptrace.h"
474 auto_switch_type "quotactlname"        "Q_[A-Z]+[[:space:]]+0x[0-9]+"                 "ufs/ufs/quota.h"
475 auto_or_type     "rebootoptname"       "RB_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/reboot.h"
476 auto_or_type     "rforkname"           "RF[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)"       "sys/unistd.h"
477 auto_switch_type "rlimitname"          "RLIMIT_[A-Z]+[[:space:]]+[0-9]+"              "sys/resource.h"
478 auto_switch_type "schedpolicyname"     "SCHED_[A-Z]+[[:space:]]+[0-9]+"               "sched.h"
479 auto_switch_type "sendfileflagsname"   "SF_[A-Z]+[[:space:]]+[0-9]+"                  "sys/socket.h"
480 auto_or_type     "shmatname"           "SHM_[A-Z]+[[:space:]]+[0-9]{6}+"              "sys/shm.h"
481 auto_switch_type "shutdownhowname"     "SHUT_[A-Z]+[[:space:]]+[0-9]+"                "sys/socket.h"
482 auto_switch_type "sigbuscodename"      "BUS_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
483 auto_switch_type "sigchldcodename"     "CLD_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
484 auto_switch_type "sigfpecodename"      "FPE_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
485 auto_switch_type "sigprocmaskhowname"  "SIG_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
486 auto_switch_type "sigillcodename"      "ILL_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
487 auto_switch_type "sigsegvcodename"     "SEGV_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
488 auto_switch_type "sigtrapcodename"     "TRAP_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
489 auto_if_type     "sockdomainname"      "PF_[[:alnum:]]+[[:space:]]+"                  "sys/socket.h"
490 auto_if_type     "sockfamilyname"      "AF_[[:alnum:]]+[[:space:]]+"                  "sys/socket.h"
491 auto_if_type     "sockipprotoname"     "IPPROTO_[[:alnum:]]+[[:space:]]+"             "netinet/in.h"
492 auto_switch_type "sockoptname"         "SO_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/socket.h"
493 auto_switch_type "socktypename"        "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*"          "sys/socket.h"
494 auto_or_type     "thrcreateflagsname"  "THR_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/thr.h"
495 auto_switch_type "umtxopname"          "UMTX_OP_[[:alnum:]_]+[[:space:]]+[0-9]+"            "sys/umtx.h"
496 auto_switch_type "vmresultname"        "KERN_[A-Z]+[[:space:]]+[0-9]+"                "vm/vm_param.h"
497 auto_or_type     "wait6optname"        "W[A-Z]+[[:space:]]+[0-9]+"                    "sys/wait.h"
498 auto_switch_type "whencename"          "SEEK_[A-Z]+[[:space:]]+[0-9]+"                "sys/unistd.h"
499
500 cat <<_EOF_
501 /*
502  * AUTO - Special
503  * F_ is used to specify fcntl commands as well as arguments. Both sets are
504  * grouped in fcntl.h, and this awk script grabs the first group.
505  */
506 void
507 fcntlcmdname(int cmd, int arg, int decimal)
508 {
509         switch (cmd) {
510 _EOF_
511 egrep "^#[[:space:]]*define[[:space:]]+F_[A-Z0-9_]+[[:space:]]+[0-9]+[[:space:]]*" \
512         $include_dir/sys/fcntl.h | \
513         awk 'BEGIN { o=0 } { for (i = 1; i <= NF; i++) \
514                 if ($i ~ /define/) \
515                         break; \
516                 ++i; \
517                 if (o <= $(i+1)) \
518                         printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i; \
519                 else \
520                         exit; \
521                 o = $(i+1) }'
522 cat <<_EOF_
523         default: /* Should not reach */
524                 printf("<invalid=%d>", cmd);
525         }
526         putchar(',');
527         if (cmd == F_GETFD || cmd == F_SETFD) {
528                 if (arg == FD_CLOEXEC)
529                         printf("FD_CLOEXEC");
530                 else if (arg == 0)
531                         printf("0");
532                 else {
533                         if (decimal)
534                                 printf("<invalid>%d", arg);
535                         else
536                                 printf("<invalid>%#x", (unsigned int)arg);
537                 }
538         } else if (cmd == F_SETFL) {
539                 flagsname(arg);
540         } else {
541                 if (decimal)
542                         printf("%d", arg);
543                 else
544                         printf("%#x", (unsigned int)arg);
545         }
546 }
547
548 /*
549  * AUTO - Special
550  *
551  * The MAP_ALIGNED flag requires special handling.
552  */
553 void
554 mmapflagsname(int flags)
555 {
556         int align;
557         int or = 0;
558         printf("%#x<", flags);
559 _EOF_
560 egrep "^#[[:space:]]*define[[:space:]]+MAP_[A-Z_]+[[:space:]]+0x[0-9A-Fa-f]+[[:space:]]*" \
561         $include_dir/sys/mman.h | grep -v MAP_ALIGNED | \
562         awk '{ for (i = 1; i <= NF; i++) \
563                 if ($i ~ /define/) \
564                         break; \
565                 ++i; \
566                 printf "\tif (!((flags > 0) ^ ((%s) > 0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
567 cat <<_EOF_
568 #ifdef MAP_32BIT
569         if (!((flags > 0) ^ ((MAP_32BIT) > 0)))
570                 if_print_or(flags, MAP_32BIT, or);
571 #endif
572         align = flags & MAP_ALIGNMENT_MASK;
573         if (align != 0) {
574                 if (align == MAP_ALIGNED_SUPER)
575                         print_or("MAP_ALIGNED_SUPER", or);
576                 else {
577                         print_or("MAP_ALIGNED", or);
578                         printf("(%d)", align >> MAP_ALIGNMENT_SHIFT);
579                 }
580         }
581         printf(">");
582         if (or == 0)
583                 printf("<invalid>%d", flags);
584 }
585
586 /*
587  * AUTO - Special
588  *
589  * The only reason this is not fully automated is due to the
590  * grep -v RTP_PRIO statement. A better egrep line should
591  * make this capable of being a auto_switch_type() function.
592  */
593 void
594 rtprioname(int func)
595 {
596         switch (func) {
597 _EOF_
598 egrep "^#[[:space:]]*define[[:space:]]+RTP_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" \
599         $include_dir/sys/rtprio.h | grep -v RTP_PRIO | \
600         awk '{ for (i = 1; i <= NF; i++) \
601                 if ($i ~ /define/) \
602                         break; \
603                 ++i; \
604                 printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i }'
605 cat <<_EOF_
606         default: /* Should not reach */
607                 printf("<invalid=%d>", func);
608         }
609 }
610
611 /*
612  * AUTO - Special
613  *
614  * The send and recv functions have a flags argument which can be
615  * set to 0. There is no corresponding #define. The auto_ functions
616  * detect this as "invalid", which is incorrect here.
617  */
618 void
619 sendrecvflagsname(int flags)
620 {
621         int or = 0;
622
623         if (flags == 0) {
624                 printf("0");
625                 return;
626         }
627
628         printf("%#x<", flags);
629 _EOF_
630 egrep "^#[[:space:]]*define[[:space:]]+MSG_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \
631         awk '{ for (i = 1; i <= NF; i++) \
632                 if ($i ~ /define/) \
633                         break; \
634                 ++i; \
635                 printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
636 cat <<_EOF_
637         printf(">");
638 }
639
640 /*
641  * AUTO - Special
642  *
643  * Check general codes first, then defer to signal-specific codes.
644  */
645 void
646 sigcodename(int sig, int code)
647 {
648         switch (code) {
649 _EOF_
650 egrep "^#[[:space:]]*define[[:space:]]+SI_[A-Z]+[[:space:]]+0(x[0-9abcdef]+)?[[:space:]]*" \
651         $include_dir/sys/signal.h | grep -v SI_UNDEFINED | \
652         awk '{ for (i = 1; i <= NF; i++) \
653                 if ($i ~ /define/) \
654                         break; \
655                 ++i; \
656                 printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i }'
657 cat <<_EOF_
658         default:
659                 switch (sig) {
660                 case SIGILL:
661                         sigillcodename(code);
662                         break;
663                 case SIGBUS:
664                         sigbuscodename(code);
665                         break;
666                 case SIGSEGV:
667                         sigsegvcodename(code);
668                         break;
669                 case SIGFPE:
670                         sigfpecodename(code);
671                         break;
672                 case SIGTRAP:
673                         sigtrapcodename(code);
674                         break;
675                 case SIGCHLD:
676                         sigchldcodename(code);
677                         break;
678                 default:
679                         printf("<invalid=%#x>", code);
680                 }
681         }
682 }
683
684 /*
685  * AUTO - Special
686  *
687  * Just print 0 as 0.
688  */
689 void
690 umtxcvwaitflags(intmax_t arg)
691 {
692         int or = 0;
693         if (arg == 0) {
694                 printf("0");
695                 return;
696         }
697         printf("%#jx<", (uintmax_t)arg);
698 _EOF_
699         egrep "^#[[:space:]]*define[[:space:]]+CVWAIT_[A-Z_]+[[:space:]]+0x[0-9]+[[:space:]]*" \
700                 $include_dir/sys/umtx.h | \
701         awk '{ for (i = 1; i <= NF; i++) \
702                 if ($i ~ /define/) \
703                         break; \
704                 ++i; \
705                 printf "\tif (!((arg > 0) ^ ((%s) > 0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
706 cat <<_EOF_
707         printf(">");
708         if (or == 0)
709                 printf("<invalid>%jd", arg);
710 }
711
712
713 /*
714  * AUTO - Special
715  *
716  * Just print 0 as 0.
717  */
718 void
719 umtxrwlockflags(intmax_t arg)
720 {
721         int or = 0;
722         if (arg == 0) {
723                 printf("0");
724                 return;
725         }
726         printf("%#jx<", (uintmax_t)arg);
727 _EOF_
728         egrep "^#[[:space:]]*define[[:space:]]+URWLOCK_PREFER_READER[[:space:]]+0x[0-9]+[[:space:]]*" \
729                 $include_dir/sys/umtx.h | \
730         awk '{ for (i = 1; i <= NF; i++) \
731                 if ($i ~ /define/) \
732                         break; \
733                 ++i; \
734                 printf "\tif (!((arg > 0) ^ ((%s) > 0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
735 cat <<_EOF_
736         printf(">");
737         if (or == 0)
738                 printf("<invalid>%jd", arg);
739 }
740 _EOF_
741 egrep '#define[[:space:]]+CAP_[A-Z_]+[[:space:]]+CAPRIGHT\([0-9],[[:space:]]+0x[0-9]{16}ULL\)' \
742         $include_dir/sys/capsicum.h | \
743         sed -E 's/[     ]+/ /g' | \
744         awk -F '[   \(,\)]' '
745         BEGIN {
746                 printf "void\n"
747                 printf "capname(const cap_rights_t *rightsp)\n"
748                 printf "{\n"
749                 printf "\tint comma = 0;\n\n"
750                 printf "\tprintf(\"<\");\n"
751         }
752         {
753                 printf "\tif ((rightsp->cr_rights[%s] & %s) == %s) {\n", $4, $2, $2
754                 printf "\t\tif (comma) printf(\",\"); else comma = 1;\n"
755                 printf "\t\tprintf(\"%s\");\n", $2
756                 printf "\t}\n"
757         }
758         END {
759                 printf "\tprintf(\">\");\n"
760                 printf "}\n"
761         }'