]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - usr.bin/kdump/mksubr
MFC 233925,236357:
[FreeBSD/stable/8.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 (int arg)
73 {
74         int     or = 0;
75 _EOF_
76         egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
77                 $include_dir/$file | \
78         awk '{ for (i = 1; i <= NF; i++) \
79                 if ($i ~ /define/) \
80                         break; \
81                 ++i; \
82                 printf "\tif(!((arg>0)^((%s)>0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
83 cat <<_EOF_
84         if (or == 0)
85                 (void)printf("<invalid>%ld", (long)arg);
86 }
87
88 _EOF_
89 }
90
91 #
92 # Automatically generates a C function used when the argument
93 # maps to a single, specific #definition
94 #
95 auto_switch_type () {
96         local name grep file
97         name=$1
98         grep=$2
99         file=$3
100
101         cat <<_EOF_
102 /* AUTO */
103 void
104 $name (int arg)
105 {
106         switch (arg) {
107 _EOF_
108         egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
109                 $include_dir/$file | \
110         awk '{ for (i = 1; i <= NF; i++) \
111                 if ($i ~ /define/) \
112                         break; \
113                 ++i; \
114                 printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }'
115 cat <<_EOF_
116         default: /* Should not reach */
117                 (void)printf("<invalid=%ld>", (long)arg);
118         }
119 }
120
121 _EOF_
122 }
123
124 #
125 # Automatically generates a C function used when the argument
126 # maps to a #definition
127 #
128 auto_if_type () {
129         local name grep file
130         name=$1
131         grep=$2
132         file=$3
133
134         cat <<_EOF_
135 /* AUTO */
136 void
137 $name (int arg)
138 {
139 _EOF_
140         egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
141                 $include_dir/$file | \
142         awk '{ printf "\t"; \
143                 if (NR > 1) \
144                         printf "else " ; \
145                 printf "if (arg == %s) \n\t\tprintf(\"%s\");\n", $2, $2 }'
146 cat <<_EOF_
147         else /* Should not reach */
148                 (void)printf("<invalid=%ld>", (long)arg);
149 }
150
151 _EOF_
152 }
153
154 # C start
155
156 cat <<_EOF_
157 #include <stdio.h>
158 #include <sys/fcntl.h>
159 #include <sys/stat.h>
160 #include <sys/unistd.h>
161 #include <sys/mman.h>
162 #include <sys/wait.h>
163 #define _KERNEL
164 #include <sys/socket.h>
165 #undef _KERNEL
166 #include <netinet/in.h>
167 #include <sys/param.h>
168 #include <sys/mount.h>
169 #include <sys/ptrace.h>
170 #include <sys/resource.h>
171 #include <sys/reboot.h>
172 #include <sched.h>
173 #include <sys/linker.h>
174 #define _KERNEL
175 #include <sys/thr.h>
176 #undef _KERNEL
177 #include <sys/extattr.h>
178 #include <sys/acl.h>
179 #include <aio.h>
180 #include <sys/sem.h>
181 #include <sys/ipc.h>
182 #include <sys/rtprio.h>
183 #include <sys/shm.h>
184 #include <nfsserver/nfs.h>
185 #include <ufs/ufs/quota.h>
186 #include <vm/vm.h>
187 #include <vm/vm_param.h>
188
189 #include "kdump_subr.h"
190
191 /*
192  * These are simple support macros. print_or utilizes a variable
193  * defined in the calling function to track whether or not it should
194  * print a logical-OR character ('|') before a string. if_print_or
195  * simply handles the necessary "if" statement used in many lines
196  * of this file.
197  */
198 #define print_or(str,orflag) do {                  \\
199         if (orflag) putchar('|'); else orflag = 1; \\
200         printf (str); }                            \\
201         while (0)
202 #define if_print_or(i,flag,orflag) do {            \\
203         if ((i & flag) == flag)                    \\
204         print_or(#flag,orflag); }                  \\
205         while (0)
206
207 /* MANUAL */
208 extern char *signames[]; /* from kdump.c */
209 void
210 signame (int sig)
211 {
212         if (sig > 0 && sig < NSIG)
213                 (void)printf("SIG%s",signames[sig]);
214         else
215                 (void)printf("SIG %d", sig);
216 }
217
218 /* MANUAL */
219 void
220 semctlname (int cmd)
221 {
222         switch (cmd) {
223         case GETNCNT:
224                 (void)printf("GETNCNT");
225                 break;
226         case GETPID:
227                 (void)printf("GETPID");
228                 break;
229         case GETVAL:
230                 (void)printf("GETVAL");
231                 break;
232         case GETALL:
233                 (void)printf("GETALL");
234                 break;
235         case GETZCNT:
236                 (void)printf("GETZCNT");
237                 break;
238         case SETVAL:
239                 (void)printf("SETVAL");
240                 break;
241         case SETALL:
242                 (void)printf("SETALL");
243                 break;
244         case IPC_RMID:
245                 (void)printf("IPC_RMID");
246                 break;
247         case IPC_SET:
248                 (void)printf("IPC_SET");
249                 break;
250         case IPC_STAT:
251                 (void)printf("IPC_STAT");
252                 break;
253         default: /* Should not reach */
254                 (void)printf("<invalid=%ld>", (long)cmd);
255         }
256 }
257
258 /* MANUAL */
259 void
260 shmctlname (int cmd) {
261         switch (cmd) {
262         case IPC_RMID:
263                 (void)printf("IPC_RMID");
264                 break;
265         case IPC_SET:
266                 (void)printf("IPC_SET");
267                 break;
268         case IPC_STAT:
269                 (void)printf("IPC_STAT");
270                 break;
271         default: /* Should not reach */
272                 (void)printf("<invalid=%ld>", (long)cmd);
273         }
274 }
275
276 /* MANUAL */
277 void
278 semgetname (int flag) {
279         int     or = 0;
280         if_print_or(flag, IPC_CREAT, or);
281         if_print_or(flag, IPC_EXCL, or);
282         if_print_or(flag, SEM_R, or);
283         if_print_or(flag, SEM_A, or);
284         if_print_or(flag, (SEM_R>>3), or);
285         if_print_or(flag, (SEM_A>>3), or);
286         if_print_or(flag, (SEM_R>>6), or);
287         if_print_or(flag, (SEM_A>>6), or);
288 }
289
290 /*
291  * MANUAL
292  *
293  * Only used by SYS_open. Unless O_CREAT is set in flags, the
294  * mode argument is unused (and often bogus and misleading).
295  */
296 void
297 flagsandmodename (int flags, int mode, int decimal) {
298         flagsname (flags);
299         (void)putchar(',');
300         if ((flags & O_CREAT) == O_CREAT) {
301                 modename (mode);
302         } else {
303                 if (decimal) {
304                         (void)printf("<unused>%ld", (long)mode);
305                 } else {
306                         (void)printf("<unused>%#lx", (long)mode);
307                 }
308         }
309 }
310
311 /*
312  * MANUAL
313  *
314  * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value
315  * referring to a line in /etc/protocols . It might be appropriate
316  * to use getprotoent(3) here.
317  */
318 void
319 sockoptlevelname (int level, int decimal)
320 {
321         if (level == SOL_SOCKET) {
322                 (void)printf("SOL_SOCKET");
323         } else {
324                 if (decimal) {
325                         (void)printf("%ld", (long)level);
326                 } else {
327                         (void)printf("%#lx", (long)level);
328                 }
329         }
330 }
331
332 /*
333  * MANUAL
334  *
335  * Used for page fault type.  Cannot use auto_or_type since the macro
336  * values contain a cast.  Also, VM_PROT_NONE has to be handled specially.
337  */
338 void
339 vmprotname (int type)
340 {
341         int     or = 0;
342
343         if (type == VM_PROT_NONE) {
344                 (void)printf("VM_PROT_NONE");
345                 return;
346         }
347         if_print_or(type, VM_PROT_READ, or);
348         if_print_or(type, VM_PROT_WRITE, or);
349         if_print_or(type, VM_PROT_EXECUTE, or);
350         if_print_or(type, VM_PROT_OVERRIDE_WRITE, or);
351 }
352 _EOF_
353
354 auto_or_type "modename" "S_[A-Z]+[[:space:]]+[0-6]{7}" "sys/stat.h"
355 auto_or_type "flagsname" "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h"
356 auto_or_type "accessmodename" "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+" "sys/unistd.h"
357 auto_or_type "mmapprotname" "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
358 auto_or_type "mmapflagsname" "MAP_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
359 auto_or_type "wait4optname" "W[A-Z]+[[:space:]]+[0-9]+" "sys/wait.h"
360 auto_or_type "getfsstatflagsname" "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*" "sys/mount.h"
361 auto_or_type "mountflagsname" "MNT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mount.h"
362 auto_or_type "rebootoptname" "RB_[A-Z]+[[:space:]]+0x[0-9]+" "sys/reboot.h"
363 auto_or_type "flockname" "LOCK_[A-Z]+[[:space:]]+0x[0-9]+" "sys/fcntl.h"
364 auto_or_type "thrcreateflagsname" "THR_[A-Z]+[[:space:]]+0x[0-9]+" "sys/thr.h"
365 auto_or_type "mlockallname" "MCL_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
366 auto_or_type "shmatname" "SHM_[A-Z]+[[:space:]]+[0-9]{6}+" "sys/shm.h"
367 auto_or_type "rforkname" "RF[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)" "sys/unistd.h"
368 auto_or_type "nfssvcname" "NFSSVC_[A-Z]+[[:space:]]+0x[0-9]+" "nfsserver/nfs.h"
369
370 auto_switch_type "whencename" "SEEK_[A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h"
371 auto_switch_type "rlimitname" "RLIMIT_[A-Z]+[[:space:]]+[0-9]+" "sys/resource.h"
372 auto_switch_type "shutdownhowname" "SHUT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h"
373 auto_switch_type "prioname" "PRIO_[A-Z]+[[:space:]]+[0-9]" "sys/resource.h"
374 auto_switch_type "fadvisebehavname" "POSIX_FADV_[A-Z]+[[:space:]]+[0-9]+" "sys/fcntl.h"
375 auto_switch_type "madvisebehavname" "_?MADV_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
376 auto_switch_type "msyncflagsname" "MS_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
377 auto_switch_type "schedpolicyname" "SCHED_[A-Z]+[[:space:]]+[0-9]+" "sched.h"
378 auto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
379 auto_switch_type "extattrctlname" "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h"
380 auto_switch_type "kldsymcmdname" "KLDSYM_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
381 auto_switch_type "sendfileflagsname" "SF_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h"
382 auto_switch_type "acltypename" "ACL_TYPE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/acl.h"
383 auto_switch_type "sigprocmaskhowname" "SIG_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h"
384 auto_switch_type "lio_listioname" "LIO_(NO)?WAIT[[:space:]]+[0-9]+" "aio.h"
385 auto_switch_type "minheritname" "INHERIT_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
386 auto_switch_type "quotactlname" "Q_[A-Z]+[[:space:]]+0x[0-9]+" "ufs/ufs/quota.h"
387 auto_if_type "sockdomainname" "PF_[[:alnum:]]+[[:space:]]+" "sys/socket.h"
388 auto_if_type "sockfamilyname" "AF_[[:alnum:]]+[[:space:]]+" "sys/socket.h"
389 auto_if_type "sockipprotoname" "IPPROTO_[[:alnum:]]+[[:space:]]+" "netinet/in.h"
390 auto_switch_type "sockoptname" "SO_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h"
391 auto_switch_type "socktypename" "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*" "sys/socket.h"
392 auto_switch_type "ptraceopname" "PT_[[:alnum:]_]+[[:space:]]+[0-9]+" "sys/ptrace.h"
393 auto_switch_type "vmresultname" "KERN_[A-Z]+[[:space:]]+[0-9]+" "vm/vm_param.h"
394
395 cat <<_EOF_
396 /*
397  * AUTO - Special
398  * F_ is used to specify fcntl commands as well as arguments. Both sets are
399  * grouped in fcntl.h, and this awk script grabs the first group.
400  */
401 void
402 fcntlcmdname (int cmd, int arg, int decimal)
403 {
404         switch (cmd) {
405 _EOF_
406 egrep "^#[[:space:]]*define[[:space:]]+F_[A-Z]+[[:space:]]+[0-9]+[[:space:]]*" \
407         $include_dir/sys/fcntl.h | \
408         awk 'BEGIN { o=0 } { for (i = 1; i <= NF; i++) \
409                 if ($i ~ /define/) \
410                         break; \
411                 ++i; \
412                 if (o <= $(i+1)) \
413                         printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i; \
414                 else \
415                         exit; \
416                 o = $(i+1) }'
417 cat <<_EOF_
418         default: /* Should not reach */
419                 (void)printf("<invalid=%ld>", (long)cmd);
420         }
421         (void)putchar(',');
422         if (cmd == F_GETFD || cmd == F_SETFD) {
423                 if (arg == FD_CLOEXEC)
424                         (void)printf("FD_CLOEXEC");
425                 else if (arg == 0)
426                         (void)printf("0");
427                 else {
428                         if (decimal)
429                                 (void)printf("<invalid>%ld", (long)arg);
430                         else
431                                 (void)printf("<invalid>%#lx", (long)arg);
432                 }
433         } else if (cmd == F_SETFL) {
434                 flagsname(arg);
435         } else {
436                 if (decimal)
437                         (void)printf("%ld", (long)arg);
438                 else
439                         (void)printf("%#lx", (long)arg);
440         }
441 }
442
443 /*
444  * AUTO - Special
445  *
446  * The only reason this is not fully automated is due to the
447  * grep -v RTP_PRIO statement. A better egrep line should
448  * make this capable of being a auto_switch_type() function.
449  */
450 void
451 rtprioname (int func)
452 {
453         switch (func) {
454 _EOF_
455 egrep "^#[[:space:]]*define[[:space:]]+RTP_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" \
456         $include_dir/sys/rtprio.h | grep -v RTP_PRIO | \
457         awk '{ for (i = 1; i <= NF; i++) \
458                 if ($i ~ /define/) \
459                         break; \
460                 ++i; \
461                 printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }'
462 cat <<_EOF_
463         default: /* Should not reach */
464                 (void)printf("<invalid=%ld>", (long)func);
465         }
466 }
467
468 /*
469  * AUTO - Special
470  *
471  * The send and recv functions have a flags argument which can be
472  * set to 0. There is no corresponding #define. The auto_ functions
473  * detect this as "invalid", which is incorrect here.
474  */
475 void
476 sendrecvflagsname (int flags)
477 {
478         int     or = 0;
479
480         if (flags == 0) {
481                 (void)printf("0");
482                 return;
483         }
484 _EOF_
485 egrep "^#[[:space:]]*define[[:space:]]+MSG_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \
486         awk '{ for (i = 1; i <= NF; i++) \
487                 if ($i ~ /define/) \
488                         break; \
489                 ++i; \
490                 printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
491 cat <<_EOF_
492 }
493
494 _EOF_