]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/kdump/mksubr
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.bin / kdump / mksubr
1 #!/bin/sh
2 #
3 # $FreeBSD$
4 #
5 # Generates kdump_subr.c
6 # mkioctls is a special-purpose script, and works fine as it is
7 # now, so it remains independent. The idea behind how it generates
8 # its list was heavily borrowed here.
9 #
10 # Some functions here are automatically generated. This can mean
11 # the user will see unusual kdump output or errors while building
12 # if the underlying .h files are changed significantly.
13 #
14 # Key:
15 # AUTO: Completely auto-generated with either the "or" or the "switch"
16 # method.
17 # AUTO - Special: Generated automatically, but with some extra commands
18 # that the auto_*_type() functions are inappropriate for.
19 # MANUAL: Manually entered and must therefore be manually updated.
20
21 set -e
22
23 LC_ALL=C; export LC_ALL
24
25 if [ -z "$1" ]
26 then
27         echo "usage: sh $0 include-dir"
28         exit 1
29 fi
30 include_dir=$1
31
32 #
33 # Automatically generates a C function that will print out the
34 # numeric input as a pipe-delimited string of the appropriate
35 # #define keys. ex:
36 # S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH
37 # The XOR is necessary to prevent including the "0"-value in every
38 # line.
39 #
40 auto_or_type () {
41         local name grep file
42         name=$1
43         grep=$2
44         file=$3
45
46         cat <<_EOF_
47 /* AUTO */
48 void
49 $name (int arg)
50 {
51         int     or = 0;
52 _EOF_
53         egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
54                 $include_dir/$file | \
55         awk '{ for (i = 1; i <= NF; i++) \
56                 if ($i ~ /define/) \
57                         break; \
58                 ++i; \
59                 printf "\tif(!((arg>0)^((%s)>0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
60 cat <<_EOF_
61         if (or == 0)
62                 (void)printf("<invalid>%ld", (long)arg);
63 }
64
65 _EOF_
66 }
67
68 #
69 # Automatically generates a C function used when the argument
70 # maps to a single, specific #definition
71 #
72 auto_switch_type () {
73         local name grep file
74         name=$1
75         grep=$2
76         file=$3
77
78         cat <<_EOF_
79 /* AUTO */
80 void
81 $name (int arg)
82 {
83         switch (arg) {
84 _EOF_
85         egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
86                 $include_dir/$file | \
87         awk '{ for (i = 1; i <= NF; i++) \
88                 if ($i ~ /define/) \
89                         break; \
90                 ++i; \
91                 printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }'
92 cat <<_EOF_
93         default: /* Should not reach */
94                 (void)printf("<invalid=%ld>", (long)arg);
95         }
96 }
97
98 _EOF_
99 }
100
101 #
102 # Automatically generates a C function used when the argument
103 # maps to a #definition
104 #
105 auto_if_type () {
106         local name grep file
107         name=$1
108         grep=$2
109         file=$3
110
111         cat <<_EOF_
112 /* AUTO */
113 void
114 $name (int arg)
115 {
116 _EOF_
117         egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
118                 $include_dir/$file | \
119         awk '{ printf "\t"; \
120                 if (NR > 1) \
121                         printf "else " ; \
122                 printf "if (arg == %s) \n\t\tprintf(\"%s\");\n", $2, $2 }'
123 cat <<_EOF_
124         else /* Should not reach */
125                 (void)printf("<invalid=%ld>", (long)arg);
126 }
127
128 _EOF_
129 }
130
131 # C start
132
133 cat <<_EOF_
134 #include <stdio.h>
135 #include <sys/fcntl.h>
136 #include <sys/stat.h>
137 #include <sys/unistd.h>
138 #include <sys/mman.h>
139 #include <sys/wait.h>
140 #define _KERNEL
141 #include <sys/socket.h>
142 #undef _KERNEL
143 #include <netinet/in.h>
144 #include <sys/param.h>
145 #include <sys/mount.h>
146 #include <sys/ptrace.h>
147 #include <sys/resource.h>
148 #include <sys/reboot.h>
149 #include <sched.h>
150 #include <sys/linker.h>
151 #define _KERNEL
152 #include <sys/thr.h>
153 #undef _KERNEL
154 #include <sys/extattr.h>
155 #include <sys/acl.h>
156 #include <aio.h>
157 #include <sys/sem.h>
158 #include <sys/ipc.h>
159 #include <sys/rtprio.h>
160 #include <sys/shm.h>
161 #include <nfsserver/nfs.h>
162 #include <ufs/ufs/quota.h>
163
164 #include "kdump_subr.h"
165
166 /*
167  * These are simple support macros. print_or utilizes a variable
168  * defined in the calling function to track whether or not it should
169  * print a logical-OR character ('|') before a string. if_print_or
170  * simply handles the necessary "if" statement used in many lines
171  * of this file.
172  */
173 #define print_or(str,orflag) do {                  \\
174         if (orflag) putchar('|'); else orflag = 1; \\
175         printf (str); }                            \\
176         while (0)
177 #define if_print_or(i,flag,orflag) do {            \\
178         if ((i & flag) == flag)                    \\
179         print_or(#flag,orflag); }                  \\
180         while (0)
181
182 /* MANUAL */
183 extern char *signames[]; /* from kdump.c */
184 void
185 signame (int sig)
186 {
187         if (sig > 0 && sig < NSIG)
188                 (void)printf("SIG%s",signames[sig]);
189         else
190                 (void)printf("SIG %d", sig);
191 }
192
193 /* MANUAL */
194 void
195 semctlname (int cmd)
196 {
197         switch (cmd) {
198         case GETNCNT:
199                 (void)printf("GETNCNT");
200                 break;
201         case GETPID:
202                 (void)printf("GETPID");
203                 break;
204         case GETVAL:
205                 (void)printf("GETVAL");
206                 break;
207         case GETALL:
208                 (void)printf("GETALL");
209                 break;
210         case GETZCNT:
211                 (void)printf("GETZCNT");
212                 break;
213         case SETVAL:
214                 (void)printf("SETVAL");
215                 break;
216         case SETALL:
217                 (void)printf("SETALL");
218                 break;
219         case IPC_RMID:
220                 (void)printf("IPC_RMID");
221                 break;
222         case IPC_SET:
223                 (void)printf("IPC_SET");
224                 break;
225         case IPC_STAT:
226                 (void)printf("IPC_STAT");
227                 break;
228         default: /* Should not reach */
229                 (void)printf("<invalid=%ld>", (long)cmd);
230         }
231 }
232
233 /* MANUAL */
234 void
235 shmctlname (int cmd) {
236         switch (cmd) {
237         case IPC_RMID:
238                 (void)printf("IPC_RMID");
239                 break;
240         case IPC_SET:
241                 (void)printf("IPC_SET");
242                 break;
243         case IPC_STAT:
244                 (void)printf("IPC_STAT");
245                 break;
246         default: /* Should not reach */
247                 (void)printf("<invalid=%ld>", (long)cmd);
248         }
249 }
250
251 /* MANUAL */
252 void
253 semgetname (int flag) {
254         int     or = 0;
255         if_print_or(flag, SEM_R, or);
256         if_print_or(flag, SEM_A, or);
257         if_print_or(flag, (SEM_R>>3), or);
258         if_print_or(flag, (SEM_A>>3), or);
259         if_print_or(flag, (SEM_R>>6), or);
260         if_print_or(flag, (SEM_A>>6), or);
261 }
262
263 /*
264  * MANUAL
265  *
266  * Only used by SYS_open. Unless O_CREAT is set in flags, the
267  * mode argument is unused (and often bogus and misleading).
268  */
269 void
270 flagsandmodename (int flags, int mode, int decimal) {
271         flagsname (flags);
272         (void)putchar(',');
273         if ((flags & O_CREAT) == O_CREAT) {
274                 modename (mode);
275         } else {
276                 if (decimal) {
277                         (void)printf("<unused>%ld", (long)mode);
278                 } else {
279                         (void)printf("<unused>%#lx", (long)mode);
280                 }
281         }
282 }
283
284 /*
285  * MANUAL
286  *
287  * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value
288  * referring to a line in /etc/protocols . It might be appropriate
289  * to use getprotoent(3) here.
290  */
291 void
292 sockoptlevelname (int level, int decimal)
293 {
294         if (level == SOL_SOCKET) {
295                 (void)printf("SOL_SOCKET");
296         } else {
297                 if (decimal) {
298                         (void)printf("%ld", (long)level);
299                 } else {
300                         (void)printf("%#lx", (long)level);
301                 }
302         }
303 }
304
305 _EOF_
306
307 auto_or_type "modename" "S_[A-Z]+[[:space:]]+[0-6]{7}" "sys/stat.h"
308 auto_or_type "flagsname" "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h"
309 auto_or_type "accessmodename" "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+" "sys/unistd.h"
310 auto_or_type "mmapprotname" "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
311 auto_or_type "mmapflagsname" "MAP_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
312 auto_or_type "wait4optname" "W[A-Z]+[[:space:]]+[0-9]+" "sys/wait.h"
313 auto_or_type "getfsstatflagsname" "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*" "sys/mount.h"
314 auto_or_type "mountflagsname" "MNT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mount.h"
315 auto_or_type "rebootoptname" "RB_[A-Z]+[[:space:]]+0x[0-9]+" "sys/reboot.h"
316 auto_or_type "flockname" "LOCK_[A-Z]+[[:space:]]+0x[0-9]+" "sys/fcntl.h"
317 auto_or_type "thrcreateflagsname" "THR_[A-Z]+[[:space:]]+0x[0-9]+" "sys/thr.h"
318 auto_or_type "mlockallname" "MCL_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
319 auto_or_type "shmatname" "SHM_[A-Z]+[[:space:]]+[0-9]{6}+" "sys/shm.h"
320 auto_or_type "rforkname" "RF[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)" "sys/unistd.h"
321 auto_or_type "nfssvcname" "NFSSVC_[A-Z]+[[:space:]]+0x[0-9]+" "nfsserver/nfs.h"
322
323 auto_switch_type "whencename" "SEEK_[A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h"
324 auto_switch_type "rlimitname" "RLIMIT_[A-Z]+[[:space:]]+[0-9]+" "sys/resource.h"
325 auto_switch_type "shutdownhowname" "SHUT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h"
326 auto_switch_type "prioname" "PRIO_[A-Z]+[[:space:]]+[0-9]" "sys/resource.h"
327 auto_switch_type "madvisebehavname" "_?MADV_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
328 auto_switch_type "msyncflagsname" "MS_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
329 auto_switch_type "schedpolicyname" "SCHED_[A-Z]+[[:space:]]+[0-9]+" "sched.h"
330 auto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
331 auto_switch_type "extattrctlname" "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h"
332 auto_switch_type "kldsymcmdname" "KLDSYM_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
333 auto_switch_type "sendfileflagsname" "SF_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h"
334 auto_switch_type "acltypename" "ACL_TYPE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/acl.h"
335 auto_switch_type "sigprocmaskhowname" "SIG_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h"
336 auto_switch_type "lio_listioname" "LIO_(NO)?WAIT[[:space:]]+[0-9]+" "aio.h"
337 auto_switch_type "minheritname" "INHERIT_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
338 auto_switch_type "quotactlname" "Q_[A-Z]+[[:space:]]+0x[0-9]+" "ufs/ufs/quota.h"
339 auto_if_type "sockdomainname" "PF_[[:alnum:]]+[[:space:]]+" "sys/socket.h"
340 auto_if_type "sockfamilyname" "AF_[[:alnum:]]+[[:space:]]+" "sys/socket.h"
341 auto_if_type "sockipprotoname" "IPPROTO_[[:alnum:]]+[[:space:]]+" "netinet/in.h"
342 auto_switch_type "sockoptname" "SO_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h"
343 auto_switch_type "socktypename" "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*" "sys/socket.h"
344 auto_switch_type "ptraceopname" "PT_[[:alnum:]]+[[:space:]]+[0-9]+" "sys/ptrace.h"
345
346 cat <<_EOF_
347 /*
348  * AUTO - Special
349  * F_ is used to specify fcntl commands as well as arguments. Both sets are
350  * grouped in fcntl.h, and this awk script grabs the first group.
351  */
352 void
353 fcntlcmdname (int cmd, int arg, int decimal)
354 {
355         switch (cmd) {
356 _EOF_
357 egrep "^#[[:space:]]*define[[:space:]]+F_[A-Z]+[[:space:]]+[0-9]+[[:space:]]*" \
358         $include_dir/sys/fcntl.h | \
359         awk 'BEGIN { o=0 } { for (i = 1; i <= NF; i++) \
360                 if ($i ~ /define/) \
361                         break; \
362                 ++i; \
363                 if (o <= $(i+1)) \
364                         printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i; \
365                 else \
366                         exit; \
367                 o = $(i+1) }'
368 cat <<_EOF_
369         default: /* Should not reach */
370                 (void)printf("<invalid=%ld>", (long)cmd);
371         }
372         (void)putchar(',');
373         if (cmd == F_GETFD || cmd == F_SETFD) {
374                 if (arg == FD_CLOEXEC)
375                         (void)printf("FD_CLOEXEC");
376                 else if (arg == 0)
377                         (void)printf("0");
378                 else {
379                         if (decimal)
380                                 (void)printf("<invalid>%ld", (long)arg);
381                         else
382                                 (void)printf("<invalid>%#lx", (long)arg);
383                 }
384         } else if (cmd == F_SETFL) {
385                 flagsname(arg);
386         } else {
387                 if (decimal)
388                         (void)printf("%ld", (long)arg);
389                 else
390                         (void)printf("%#lx", (long)arg);
391         }
392 }
393
394 /*
395  * AUTO - Special
396  *
397  * The only reason this is not fully automated is due to the
398  * grep -v RTP_PRIO statement. A better egrep line should
399  * make this capable of being a auto_switch_type() function.
400  */
401 void
402 rtprioname (int func)
403 {
404         switch (func) {
405 _EOF_
406 egrep "^#[[:space:]]*define[[:space:]]+RTP_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" \
407         $include_dir/sys/rtprio.h | grep -v RTP_PRIO | \
408         awk '{ for (i = 1; i <= NF; i++) \
409                 if ($i ~ /define/) \
410                         break; \
411                 ++i; \
412                 printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }'
413 cat <<_EOF_
414         default: /* Should not reach */
415                 (void)printf("<invalid=%ld>", (long)func);
416         }
417 }
418
419 /*
420  * AUTO - Special
421  *
422  * The send and recv functions have a flags argument which can be
423  * set to 0. There is no corresponding #define. The auto_ functions
424  * detect this as "invalid", which is incorrect here.
425  */
426 void
427 sendrecvflagsname (int flags)
428 {
429         int     or = 0;
430
431         if (flags == 0) {
432                 (void)printf("0");
433                 return;
434         }
435 _EOF_
436 egrep "^#[[:space:]]*define[[:space:]]+MSG_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \
437         awk '{ for (i = 1; i <= NF; i++) \
438                 if ($i ~ /define/) \
439                         break; \
440                 ++i; \
441                 printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
442 cat <<_EOF_
443 }
444
445 _EOF_