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