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