]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.bin/kdump/mksubr
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.bin / kdump / mksubr
1 set -e
2
3 # Generates kdump_subr.c
4 # mkioctls is a special-purpose script, and works fine as it is
5 # now, so it remains independent. The idea behind how it generates
6 # its list was heavily borrowed here.
7 #
8 # Some functions here are automatically generated. This can mean
9 # the user will see unusual kdump output or errors while building
10 # if the underlying .h files are changed significantly.
11 #
12 # Key:
13 # AUTO: Completely auto-generated with either the "or" or the "switch"
14 # method.
15 # AUTO - Special: Generated automatically, but with some extra commands
16 # that the auto_*_type() functions are inappropriate for.
17 # MANUAL: Manually entered and must therefore be manually updated.
18
19 # $FreeBSD$
20
21 LC_ALL=C; export LC_ALL
22
23 if [ -z "$1" ]
24 then
25         echo "usage: sh $0 include-dir"
26         exit 1
27 fi
28 include_dir=$1
29
30 #
31 # Automatically generates a C function that will print out the
32 # numeric input as a pipe-delimited string of the appropriate
33 # #define keys. ex:
34 # S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH
35 # The XOR is necessary to prevent including the "0"-value in every
36 # line.
37 #
38 auto_or_type () {
39         local name grep file
40         name=$1
41         grep=$2
42         file=$3
43
44         cat <<_EOF_
45 /* AUTO */
46 void
47 $name (int arg)
48 {
49         int     or = 0;
50 _EOF_
51         egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
52                 $include_dir/$file | \
53         awk '{ for (i = 1; i <= NF; i++) \
54                 if ($i ~ /define/) \
55                         break; \
56                 ++i; \
57                 printf "\tif(!((arg>0)^((%s)>0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
58 cat <<_EOF_
59         if (or == 0)
60                 (void)printf("<invalid>%ld", (long)arg);
61 }
62
63 _EOF_
64 }
65
66 #
67 # Automatically generates a C function used when the argument
68 # maps to a single, specific #definition
69 #
70 auto_switch_type () {
71         local name grep file
72         name=$1
73         grep=$2
74         file=$3
75
76         cat <<_EOF_
77 /* AUTO */
78 void
79 $name (int arg)
80 {
81         switch (arg) {
82 _EOF_
83         egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
84                 $include_dir/$file | \
85         awk '{ for (i = 1; i <= NF; i++) \
86                 if ($i ~ /define/) \
87                         break; \
88                 ++i; \
89                 printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }'
90 cat <<_EOF_
91         default: /* Should not reach */
92                 (void)printf("<invalid=%ld>", (long)arg);
93         }
94 }
95
96 _EOF_
97 }
98
99 #
100 # Automatically generates a C function used when the argument
101 # maps to a #definition
102 #
103 auto_if_type () {
104         local name grep file
105         name=$1
106         grep=$2
107         file=$3
108
109         cat <<_EOF_
110 /* AUTO */
111 void
112 $name (int arg)
113 {
114 _EOF_
115         egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
116                 $include_dir/$file | \
117         awk '{ printf "\t"; \
118                 if (NR > 1) \
119                         printf "else " ; \
120                 printf "if (arg == %s) \n\t\tprintf(\"%s\");\n", $2, $2 }'
121 cat <<_EOF_
122         else /* Should not reach */
123                 (void)printf("<invalid=%ld>", (long)arg);
124 }
125
126 _EOF_
127 }
128
129 # C start
130
131 cat <<_EOF_
132 #include <stdio.h>
133 #include <sys/fcntl.h>
134 #include <sys/stat.h>
135 #include <sys/unistd.h>
136 #include <sys/mman.h>
137 #include <sys/wait.h>
138 #define _KERNEL
139 #include <sys/socket.h>
140 #undef _KERNEL
141 #include <netinet/in.h>
142 #include <sys/param.h>
143 #include <sys/mount.h>
144 #include <sys/ptrace.h>
145 #include <sys/resource.h>
146 #include <sys/reboot.h>
147 #include <sched.h>
148 #include <sys/linker.h>
149 #define _KERNEL
150 #include <sys/thr.h>
151 #undef _KERNEL
152 #include <sys/kse.h>
153 #include <sys/extattr.h>
154 #include <sys/acl.h>
155 #include <aio.h>
156 #include <sys/sem.h>
157 #include <sys/ipc.h>
158 #include <sys/rtprio.h>
159 #include <sys/shm.h>
160 #include <nfsserver/nfs.h>
161 #include <ufs/ufs/quota.h>
162
163 #include "kdump_subr.h"
164
165 /*
166  * These are simple support macros. print_or utilizes a variable
167  * defined in the calling function to track whether or not it should
168  * print a logical-OR character ('|') before a string. if_print_or
169  * simply handles the necessary "if" statement used in many lines
170  * of this file.
171  */
172 #define print_or(str,orflag) do {                  \\
173         if (orflag) putchar('|'); else orflag = 1; \\
174         printf (str); }                            \\
175         while (0)
176 #define if_print_or(i,flag,orflag) do {            \\
177         if ((i & flag) == flag)                    \\
178         print_or(#flag,orflag); }                  \\
179         while (0)
180
181 /* MANUAL */
182 extern char *signames[]; /* from kdump.c */
183 void
184 signame (int sig)
185 {
186         if (sig > 0 && sig < NSIG)
187                 (void)printf("SIG%s",signames[sig]);
188         else
189                 (void)printf("SIG %d", sig);
190 }
191
192 /* MANUAL */
193 void
194 semctlname (int cmd)
195 {
196         switch (cmd) {
197         case GETNCNT:
198                 (void)printf("GETNCNT");
199                 break;
200         case GETPID:
201                 (void)printf("GETPID");
202                 break;
203         case GETVAL:
204                 (void)printf("GETVAL");
205                 break;
206         case GETALL:
207                 (void)printf("GETALL");
208                 break;
209         case GETZCNT:
210                 (void)printf("GETZCNT");
211                 break;
212         case SETVAL:
213                 (void)printf("SETVAL");
214                 break;
215         case SETALL:
216                 (void)printf("SETALL");
217                 break;
218         case IPC_RMID:
219                 (void)printf("IPC_RMID");
220                 break;
221         case IPC_SET:
222                 (void)printf("IPC_SET");
223                 break;
224         case IPC_STAT:
225                 (void)printf("IPC_STAT");
226                 break;
227         default: /* Should not reach */
228                 (void)printf("<invalid=%ld>", (long)cmd);
229         }
230 }
231
232 /* MANUAL */
233 void
234 shmctlname (int cmd) {
235         switch (cmd) {
236         case IPC_RMID:
237                 (void)printf("IPC_RMID");
238                 break;
239         case IPC_SET:
240                 (void)printf("IPC_SET");
241                 break;
242         case IPC_STAT:
243                 (void)printf("IPC_STAT");
244                 break;
245         default: /* Should not reach */
246                 (void)printf("<invalid=%ld>", (long)cmd);
247         }
248 }
249
250 /* MANUAL */
251 void
252 semgetname (int flag) {
253         int     or = 0;
254         if_print_or(flag, SEM_R, or);
255         if_print_or(flag, SEM_A, or);
256         if_print_or(flag, (SEM_R>>3), or);
257         if_print_or(flag, (SEM_A>>3), or);
258         if_print_or(flag, (SEM_R>>6), or);
259         if_print_or(flag, (SEM_A>>6), or);
260 }
261
262 /*
263  * MANUAL
264  *
265  * Only used by SYS_open. Unless O_CREAT is set in flags, the
266  * mode argument is unused (and often bogus and misleading).
267  */
268 void
269 flagsandmodename (int flags, int mode, int decimal) {
270         flagsname (flags);
271         (void)putchar(',');
272         if ((flags & O_CREAT) == O_CREAT) {
273                 modename (mode);
274         } else {
275                 if (decimal) {
276                         (void)printf("<unused>%ld", (long)mode);
277                 } else {
278                         (void)printf("<unused>%#lx", (long)mode);
279                 }
280         }
281 }
282
283 /*
284  * MANUAL
285  *
286  * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value
287  * referring to a line in /etc/protocols . It might be appropriate
288  * to use getprotoent(3) here.
289  */
290 void
291 sockoptlevelname (int level, int decimal)
292 {
293         if (level == SOL_SOCKET) {
294                 (void)printf("SOL_SOCKET");
295         } else {
296                 if (decimal) {
297                         (void)printf("%ld", (long)level);
298                 } else {
299                         (void)printf("%#lx", (long)level);
300                 }
301         }
302 }
303
304 _EOF_
305
306 auto_or_type "modename" "S_[A-Z]+[[:space:]]+[0-6]{7}" "sys/stat.h"
307 auto_or_type "flagsname" "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h"
308 auto_or_type "accessmodename" "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+" "sys/unistd.h"
309 auto_or_type "mmapprotname" "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
310 auto_or_type "mmapflagsname" "MAP_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
311 auto_or_type "wait4optname" "W[A-Z]+[[:space:]]+[0-9]+" "sys/wait.h"
312 auto_or_type "getfsstatflagsname" "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*" "sys/mount.h"
313 auto_or_type "mountflagsname" "MNT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mount.h"
314 auto_or_type "rebootoptname" "RB_[A-Z]+[[:space:]]+0x[0-9]+" "sys/reboot.h"
315 auto_or_type "flockname" "LOCK_[A-Z]+[[:space:]]+0x[0-9]+" "sys/fcntl.h"
316 auto_or_type "thrcreateflagsname" "THR_[A-Z]+[[:space:]]+0x[0-9]+" "sys/thr.h"
317 auto_or_type "mlockallname" "MCL_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
318 auto_or_type "shmatname" "SHM_[A-Z]+[[:space:]]+[0-9]{6}+" "sys/shm.h"
319 auto_or_type "rforkname" "RF[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)" "sys/unistd.h"
320 auto_or_type "nfssvcname" "NFSSVC_[A-Z]+[[:space:]]+0x[0-9]+" "nfsserver/nfs.h"
321
322 auto_switch_type "whencename" "SEEK_[A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h"
323 auto_switch_type "rlimitname" "RLIMIT_[A-Z]+[[:space:]]+[0-9]+" "sys/resource.h"
324 auto_switch_type "shutdownhowname" "SHUT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h"
325 auto_switch_type "prioname" "PRIO_[A-Z]+[[:space:]]+[0-9]" "sys/resource.h"
326 auto_switch_type "madvisebehavname" "_?MADV_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
327 auto_switch_type "msyncflagsname" "MS_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
328 auto_switch_type "schedpolicyname" "SCHED_[A-Z]+[[:space:]]+[0-9]+" "sched.h"
329 auto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
330 auto_switch_type "ksethrcmdname" "KSE_INTR_[A-Z]+[[:space:]]+[0-9]+" "sys/kse.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_