]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - usr.bin/kdump/mksubr
MFC 255493:
[FreeBSD/stable/9.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 <stdint.h>
160 #include <stdio.h>
161 #include <sys/fcntl.h>
162 #include <sys/stat.h>
163 #include <sys/unistd.h>
164 #include <sys/mman.h>
165 #include <sys/wait.h>
166 #define _KERNEL
167 #include <sys/socket.h>
168 #undef _KERNEL
169 #include <netinet/in.h>
170 #include <sys/param.h>
171 #include <sys/mount.h>
172 #include <sys/ptrace.h>
173 #include <sys/resource.h>
174 #include <sys/reboot.h>
175 #include <sched.h>
176 #include <sys/linker.h>
177 #define _KERNEL
178 #include <sys/thr.h>
179 #undef _KERNEL
180 #include <sys/extattr.h>
181 #include <sys/acl.h>
182 #include <aio.h>
183 #include <sys/sem.h>
184 #include <sys/ipc.h>
185 #include <sys/rtprio.h>
186 #include <sys/shm.h>
187 #include <nfsserver/nfs.h>
188 #include <ufs/ufs/quota.h>
189 #include <vm/vm.h>
190 #include <vm/vm_param.h>
191
192 #include "kdump_subr.h"
193
194 /*
195  * These are simple support macros. print_or utilizes a variable
196  * defined in the calling function to track whether or not it should
197  * print a logical-OR character ('|') before a string. if_print_or
198  * simply handles the necessary "if" statement used in many lines
199  * of this file.
200  */
201 #define print_or(str,orflag) do {                  \\
202         if (orflag) putchar('|'); else orflag = 1; \\
203         printf (str); }                            \\
204         while (0)
205 #define if_print_or(i,flag,orflag) do {            \\
206         if ((i & flag) == flag)                    \\
207         print_or(#flag,orflag); }                  \\
208         while (0)
209
210 /* MANUAL */
211 extern char *signames[]; /* from kdump.c */
212 void
213 signame(int sig)
214 {
215         if (sig > 0 && sig < NSIG)
216                 printf("SIG%s",signames[sig]);
217         else
218                 printf("SIG %d", sig);
219 }
220
221 /* MANUAL */
222 void
223 semctlname(int cmd)
224 {
225         switch (cmd) {
226         case GETNCNT:
227                 printf("GETNCNT");
228                 break;
229         case GETPID:
230                 printf("GETPID");
231                 break;
232         case GETVAL:
233                 printf("GETVAL");
234                 break;
235         case GETALL:
236                 printf("GETALL");
237                 break;
238         case GETZCNT:
239                 printf("GETZCNT");
240                 break;
241         case SETVAL:
242                 printf("SETVAL");
243                 break;
244         case SETALL:
245                 printf("SETALL");
246                 break;
247         case IPC_RMID:
248                 printf("IPC_RMID");
249                 break;
250         case IPC_SET:
251                 printf("IPC_SET");
252                 break;
253         case IPC_STAT:
254                 printf("IPC_STAT");
255                 break;
256         default: /* Should not reach */
257                 printf("<invalid=%d>", cmd);
258         }
259 }
260
261 /* MANUAL */
262 void
263 shmctlname(int cmd)
264 {
265         switch (cmd) {
266         case IPC_RMID:
267                 printf("IPC_RMID");
268                 break;
269         case IPC_SET:
270                 printf("IPC_SET");
271                 break;
272         case IPC_STAT:
273                 printf("IPC_STAT");
274                 break;
275         default: /* Should not reach */
276                 printf("<invalid=%d>", cmd);
277         }
278 }
279
280 /* MANUAL */
281 void
282 semgetname(int flag)
283 {
284         int or = 0;
285         if_print_or(flag, IPC_CREAT, or);
286         if_print_or(flag, IPC_EXCL, or);
287         if_print_or(flag, SEM_R, or);
288         if_print_or(flag, SEM_A, or);
289         if_print_or(flag, (SEM_R>>3), or);
290         if_print_or(flag, (SEM_A>>3), or);
291         if_print_or(flag, (SEM_R>>6), or);
292         if_print_or(flag, (SEM_A>>6), or);
293 }
294
295 /*
296  * MANUAL
297  *
298  * Only used by SYS_open. Unless O_CREAT is set in flags, the
299  * mode argument is unused (and often bogus and misleading).
300  */
301 void
302 flagsandmodename(int flags, int mode, int decimal)
303 {
304         flagsname(flags);
305         putchar(',');
306         if ((flags & O_CREAT) == O_CREAT) {
307                 modename (mode);
308         } else {
309                 if (decimal) {
310                         printf("<unused>%d", mode);
311                 } else {
312                         printf("<unused>%#x", (unsigned int)mode);
313                 }
314         }
315 }
316
317 /* MANUAL */
318 void
319 idtypename(idtype_t idtype, int decimal)
320 {
321         switch(idtype) {
322         case P_PID:
323                 printf("P_PID");
324                 break;
325         case P_PPID:
326                 printf("P_PPID");
327                 break;
328         case P_PGID:
329                 printf("P_PGID");
330                 break;
331         case P_SID:
332                 printf("P_SID");
333                 break;
334         case P_CID:
335                 printf("P_CID");
336                 break;
337         case P_UID:
338                 printf("P_UID");
339                 break;
340         case P_GID:
341                 printf("P_GID");
342                 break;
343         case P_ALL:
344                 printf("P_ALL");
345                 break;
346         case P_LWPID:
347                 printf("P_LWPID");
348                 break;
349         case P_TASKID:
350                 printf("P_TASKID");
351                 break;
352         case P_PROJID:
353                 printf("P_PROJID");
354                 break;
355         case P_POOLID:
356                 printf("P_POOLID");
357                 break;
358         case P_JAILID:
359                 printf("P_JAILID");
360                 break;
361         case P_CTID:
362                 printf("P_CTID");
363                 break;
364         case P_CPUID:
365                 printf("P_CPUID");
366                 break;
367         case P_PSETID:
368                 printf("P_PSETID");
369                 break;
370         default:
371                 if (decimal) {
372                         printf("%d", idtype);
373                 } else {
374                         printf("%#x", idtype);
375                 }
376         }
377 }
378
379 /*
380  * MANUAL
381  *
382  * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value
383  * referring to a line in /etc/protocols . It might be appropriate
384  * to use getprotoent(3) here.
385  */
386 void
387 sockoptlevelname(int level, int decimal)
388 {
389         if (level == SOL_SOCKET) {
390                 printf("SOL_SOCKET");
391         } else {
392                 if (decimal) {
393                         printf("%d", level);
394                 } else {
395                         printf("%#x", (unsigned int)level);
396                 }
397         }
398 }
399
400 /*
401  * MANUAL
402  *
403  * Used for page fault type.  Cannot use auto_or_type since the macro
404  * values contain a cast.  Also, VM_PROT_NONE has to be handled specially.
405  */
406 void
407 vmprotname (int type)
408 {
409         int     or = 0;
410
411         if (type == VM_PROT_NONE) {
412                 (void)printf("VM_PROT_NONE");
413                 return;
414         }
415         if_print_or(type, VM_PROT_READ, or);
416         if_print_or(type, VM_PROT_WRITE, or);
417         if_print_or(type, VM_PROT_EXECUTE, or);
418         if_print_or(type, VM_PROT_COPY, or);
419 }
420 _EOF_
421
422 auto_or_type     "accessmodename"      "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+"         "sys/unistd.h"
423 auto_switch_type "acltypename"         "ACL_TYPE_[A-Z4_]+[[:space:]]+0x[0-9]+"        "sys/acl.h"
424 auto_switch_type "extattrctlname"      "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h"
425 auto_switch_type "fadvisebehavname"    "POSIX_FADV_[A-Z]+[[:space:]]+[0-9]+"          "sys/fcntl.h"
426 auto_or_type     "flagsname"           "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+"           "sys/fcntl.h"
427 auto_or_type     "flockname"           "LOCK_[A-Z]+[[:space:]]+0x[0-9]+"              "sys/fcntl.h"
428 auto_or_type     "getfsstatflagsname"  "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*"            "sys/mount.h"
429 auto_switch_type "kldsymcmdname"       "KLDSYM_[A-Z]+[[:space:]]+[0-9]+"              "sys/linker.h"
430 auto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+"       "sys/linker.h"
431 auto_switch_type "lio_listioname"      "LIO_(NO)?WAIT[[:space:]]+[0-9]+"              "aio.h"
432 auto_switch_type "madvisebehavname"    "_?MADV_[A-Z]+[[:space:]]+[0-9]+"              "sys/mman.h"
433 auto_switch_type "minheritname"        "INHERIT_[A-Z]+[[:space:]]+[0-9]+"             "sys/mman.h"
434 auto_or_type     "mlockallname"        "MCL_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/mman.h"
435 auto_or_type     "mmapprotname"        "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+"        "sys/mman.h"
436 auto_or_type     "modename"            "S_[A-Z]+[[:space:]]+[0-6]{7}"                 "sys/stat.h"
437 auto_or_type     "mountflagsname"      "MNT_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/mount.h"
438 auto_switch_type "msyncflagsname"      "MS_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/mman.h"
439 auto_or_type     "nfssvcname"          "NFSSVC_[A-Z0-9]+[[:space:]]+0x[0-9]+"         "nfs/nfssvc.h"
440 auto_switch_type "prioname"            "PRIO_[A-Z]+[[:space:]]+[0-9]"                 "sys/resource.h"
441 auto_switch_type "ptraceopname"        "PT_[[:alnum:]_]+[[:space:]]+[0-9]+"           "sys/ptrace.h"
442 auto_switch_type "quotactlname"        "Q_[A-Z]+[[:space:]]+0x[0-9]+"                 "ufs/ufs/quota.h"
443 auto_or_type     "rebootoptname"       "RB_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/reboot.h"
444 auto_or_type     "rforkname"           "RF[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)"       "sys/unistd.h"
445 auto_switch_type "rlimitname"          "RLIMIT_[A-Z]+[[:space:]]+[0-9]+"              "sys/resource.h"
446 auto_switch_type "schedpolicyname"     "SCHED_[A-Z]+[[:space:]]+[0-9]+"               "sched.h"
447 auto_switch_type "sendfileflagsname"   "SF_[A-Z]+[[:space:]]+[0-9]+"                  "sys/socket.h"
448 auto_or_type     "shmatname"           "SHM_[A-Z]+[[:space:]]+[0-9]{6}+"              "sys/shm.h"
449 auto_switch_type "shutdownhowname"     "SHUT_[A-Z]+[[:space:]]+[0-9]+"                "sys/socket.h"
450 auto_switch_type "sigprocmaskhowname"  "SIG_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
451 auto_if_type     "sockdomainname"      "PF_[[:alnum:]]+[[:space:]]+"                  "sys/socket.h"
452 auto_if_type     "sockfamilyname"      "AF_[[:alnum:]]+[[:space:]]+"                  "sys/socket.h"
453 auto_if_type     "sockipprotoname"     "IPPROTO_[[:alnum:]]+[[:space:]]+"             "netinet/in.h"
454 auto_switch_type "sockoptname"         "SO_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/socket.h"
455 auto_switch_type "socktypename"        "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*"          "sys/socket.h"
456 auto_or_type     "thrcreateflagsname"  "THR_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/thr.h"
457 auto_switch_type "vmresultname"        "KERN_[A-Z]+[[:space:]]+[0-9]+"                "vm/vm_param.h"
458 auto_or_type     "wait6optname"        "W[A-Z]+[[:space:]]+[0-9]+"                    "sys/wait.h"
459 auto_switch_type "whencename"          "SEEK_[A-Z]+[[:space:]]+[0-9]+"                "sys/unistd.h"
460
461 cat <<_EOF_
462 /*
463  * AUTO - Special
464  * F_ is used to specify fcntl commands as well as arguments. Both sets are
465  * grouped in fcntl.h, and this awk script grabs the first group.
466  */
467 void
468 fcntlcmdname(int cmd, int arg, int decimal)
469 {
470         switch (cmd) {
471 _EOF_
472 egrep "^#[[:space:]]*define[[:space:]]+F_[A-Z]+[[:space:]]+[0-9]+[[:space:]]*" \
473         $include_dir/sys/fcntl.h | \
474         awk 'BEGIN { o=0 } { for (i = 1; i <= NF; i++) \
475                 if ($i ~ /define/) \
476                         break; \
477                 ++i; \
478                 if (o <= $(i+1)) \
479                         printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i; \
480                 else \
481                         exit; \
482                 o = $(i+1) }'
483 cat <<_EOF_
484         default: /* Should not reach */
485                 printf("<invalid=%d>", cmd);
486         }
487         putchar(',');
488         if (cmd == F_GETFD || cmd == F_SETFD) {
489                 if (arg == FD_CLOEXEC)
490                         printf("FD_CLOEXEC");
491                 else if (arg == 0)
492                         printf("0");
493                 else {
494                         if (decimal)
495                                 printf("<invalid>%d", arg);
496                         else
497                                 printf("<invalid>%#x", (unsigned int)arg);
498                 }
499         } else if (cmd == F_SETFL) {
500                 flagsname(arg);
501         } else {
502                 if (decimal)
503                         printf("%d", arg);
504                 else
505                         printf("%#x", (unsigned int)arg);
506         }
507 }
508
509 /*
510  * AUTO - Special
511  *
512  * The MAP_ALIGNED flag requires special handling.
513  */
514 void
515 mmapflagsname(int flags)
516 {
517         int align;
518         int or = 0;
519         printf("%#x<", flags);
520 _EOF_
521 egrep "^#[[:space:]]*define[[:space:]]+MAP_[A-Z_]+[[:space:]]+0x[0-9A-Fa-f]+[[:space:]]*" \
522         $include_dir/sys/mman.h | grep -v MAP_ALIGNED | \
523         awk '{ for (i = 1; i <= NF; i++) \
524                 if ($i ~ /define/) \
525                         break; \
526                 ++i; \
527                 printf "\tif (!((flags > 0) ^ ((%s) > 0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
528 cat <<_EOF_
529         align = flags & MAP_ALIGNMENT_MASK;
530         if (align != 0) {
531                 if (align == MAP_ALIGNED_SUPER)
532                         print_or("MAP_ALIGNED_SUPER", or);
533                 else {
534                         print_or("MAP_ALIGNED", or);
535                         printf("(%d)", align >> MAP_ALIGNMENT_SHIFT);
536                 }
537         }
538         printf(">");
539         if (or == 0)
540                 printf("<invalid>%d", flags);
541 }
542
543 /*
544  * AUTO - Special
545  *
546  * The only reason this is not fully automated is due to the
547  * grep -v RTP_PRIO statement. A better egrep line should
548  * make this capable of being a auto_switch_type() function.
549  */
550 void
551 rtprioname(int func)
552 {
553         switch (func) {
554 _EOF_
555 egrep "^#[[:space:]]*define[[:space:]]+RTP_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" \
556         $include_dir/sys/rtprio.h | grep -v RTP_PRIO | \
557         awk '{ for (i = 1; i <= NF; i++) \
558                 if ($i ~ /define/) \
559                         break; \
560                 ++i; \
561                 printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i }'
562 cat <<_EOF_
563         default: /* Should not reach */
564                 printf("<invalid=%d>", func);
565         }
566 }
567
568 /*
569  * AUTO - Special
570  *
571  * The send and recv functions have a flags argument which can be
572  * set to 0. There is no corresponding #define. The auto_ functions
573  * detect this as "invalid", which is incorrect here.
574  */
575 void
576 sendrecvflagsname(int flags)
577 {
578         int or = 0;
579
580         if (flags == 0) {
581                 printf("0");
582                 return;
583         }
584
585         printf("%#x<", flags);
586 _EOF_
587 egrep "^#[[:space:]]*define[[:space:]]+MSG_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \
588         awk '{ for (i = 1; i <= NF; i++) \
589                 if ($i ~ /define/) \
590                         break; \
591                 ++i; \
592                 printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
593 cat <<_EOF_
594         printf(">");
595 }
596
597 _EOF_