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