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