mksubr revision 165461
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 165461 2006-12-22 05:07:21Z rodrigc $
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	if (sig > 0 && sig < NSIG)
155		(void)printf("SIG%s",signames[sig]);
156	else
157		(void)printf("SIG %d", sig);
158}
159
160/* MANUAL */
161void
162semctlname (int cmd)
163{
164	switch (cmd) {
165	case GETNCNT:
166		(void)printf("GETNCNT");
167		break;
168	case GETPID:
169		(void)printf("GETPID");
170		break;
171	case GETVAL:
172		(void)printf("GETVAL");
173		break;
174	case GETALL:
175		(void)printf("GETALL");
176		break;
177	case GETZCNT:
178		(void)printf("GETZCNT");
179		break;
180	case SETVAL:
181		(void)printf("SETVAL");
182		break;
183	case SETALL:
184		(void)printf("SETALL");
185		break;
186	case IPC_RMID:
187		(void)printf("IPC_RMID");
188		break;
189	case IPC_SET:
190		(void)printf("IPC_SET");
191		break;
192	case IPC_STAT:
193		(void)printf("IPC_STAT");
194		break;
195	default: /* Should not reach */
196		(void)printf("<invalid=%ld>", (long)cmd);
197	}
198}
199
200/* MANUAL */
201void
202shmctlname (int cmd) {
203	switch (cmd) {
204	case IPC_RMID:
205		(void)printf("IPC_RMID");
206		break;
207	case IPC_SET:
208		(void)printf("IPC_SET");
209		break;
210	case IPC_STAT:
211		(void)printf("IPC_STAT");
212		break;
213	default: /* Should not reach */
214		(void)printf("<invalid=%ld>", (long)cmd);
215	}
216}
217
218/* MANUAL */
219void
220semgetname (int flag) {
221	int	or = 0;
222	if_print_or(flag, SEM_R, or);
223	if_print_or(flag, SEM_A, or);
224	if_print_or(flag, (SEM_R>>3), or);
225	if_print_or(flag, (SEM_A>>3), or);
226	if_print_or(flag, (SEM_R>>6), or);
227	if_print_or(flag, (SEM_A>>6), or);
228}
229
230/*
231 * MANUAL
232 *
233 * Only used by SYS_open. Unless O_CREAT is set in flags, the
234 * mode argument is unused (and often bogus and misleading).
235 */
236void
237flagsandmodename (int flags, int mode, int decimal) {
238	flagsname (flags);
239	(void)putchar(',');
240	if ((flags & O_CREAT) == O_CREAT) {
241		modename (mode);
242	} else {
243		if (decimal) {
244			(void)printf("<unused>%ld", (long)mode);
245		} else {
246			(void)printf("<unused>%#lx", (long)mode);
247		}
248	}
249}
250
251/*
252 * MANUAL
253 *
254 * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value
255 * referring to a line in /etc/protocols . It might be appropriate
256 * to use getprotoent(3) here.
257 */
258void
259sockoptlevelname (int level, int decimal)
260{
261	if (level == SOL_SOCKET) {
262		(void)printf("SOL_SOCKET");
263	} else {
264		if (decimal) {
265			(void)printf("%ld", (long)level);
266		} else {
267			(void)printf("%#lx", (long)level);
268		}
269	}
270}
271
272_EOF_
273
274auto_or_type "modename" "S_[A-Z]+[[:space:]]+[0-6]{7}" "sys/stat.h"
275auto_or_type "flagsname" "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h"
276auto_or_type "accessmodename" "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+" "sys/unistd.h"
277auto_or_type "mmapprotname" "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
278auto_or_type "mmapflagsname" "MAP_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
279auto_or_type "wait4optname" "W[A-Z]+[[:space:]]+[0-9]+" "sys/wait.h"
280auto_or_type "getfsstatflagsname" "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*" "sys/mount.h"
281auto_or_type "mountflagsname" "MNT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mount.h"
282auto_or_type "rebootoptname" "RB_[A-Z]+[[:space:]]+0x[0-9]+" "sys/reboot.h"
283auto_or_type "flockname" "LOCK_[A-Z]+[[:space:]]+0x[0-9]+" "sys/fcntl.h"
284auto_or_type "sockdomainname" "PF_[A-Z]+[[:space:]]+" "sys/socket.h"
285auto_or_type "thrcreateflagsname" "THR_[A-Z]+[[:space:]]+0x[0-9]+" "sys/thr.h"
286auto_or_type "mlockallname" "MCL_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
287auto_or_type "shmatname" "SHM_[A-Z]+[[:space:]]+[0-9]{6}+" "sys/shm.h"
288auto_or_type "rforkname" "RF[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)" "sys/unistd.h"
289auto_or_type "nfssvcname" "NFSSVC_[A-Z]+[[:space:]]+0x[0-9]+" "nfsserver/nfs.h"
290
291auto_switch_type "whencename" "SEEK_[A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h"
292auto_switch_type "rlimitname" "RLIMIT_[A-Z]+[[:space:]]+[0-9]+" "sys/resource.h"
293auto_switch_type "shutdownhowname" "SHUT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h"
294auto_switch_type "prioname" "PRIO_[A-Z]+[[:space:]]+[0-9]" "sys/resource.h"
295auto_switch_type "madvisebehavname" "_?MADV_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
296auto_switch_type "msyncflagsname" "MS_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
297auto_switch_type "schedpolicyname" "SCHED_[A-Z]+[[:space:]]+[0-9]+" "sched.h"
298auto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
299auto_switch_type "ksethrcmdname" "KSE_INTR_[A-Z]+[[:space:]]+[0-9]+" "sys/kse.h"
300auto_switch_type "extattrctlname" "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h"
301auto_switch_type "kldsymcmdname" "KLDSYM_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
302auto_switch_type "sendfileflagsname" "SF_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h"
303auto_switch_type "acltypename" "ACL_TYPE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/acl.h"
304auto_switch_type "sigprocmaskhowname" "SIG_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h"
305auto_switch_type "lio_listioname" "LIO_(NO)?WAIT[[:space:]]+[0-9]+" "aio.h"
306auto_switch_type "minheritname" "INHERIT_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
307auto_switch_type "quotactlname" "Q_[A-Z]+[[:space:]]+0x[0-9]+" "ufs/ufs/quota.h"
308auto_switch_type "sockoptname" "SO_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h"
309auto_switch_type "socktypename" "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*" "sys/socket.h"
310
311cat <<_EOF_
312/*
313 * AUTO - Special
314 * F_ is used to specify fcntl commands as well as arguments. Both sets are
315 * grouped in fcntl.h, and this awk script grabs the first group.
316 */
317void
318fcntlcmdname (int cmd, int arg, int decimal)
319{
320	switch (cmd) {
321_EOF_
322egrep "^#[[:space:]]*define[[:space:]]+F_[A-Z]+[[:space:]]+[0-9]+[[:space:]]*" \
323	$include_dir/sys/fcntl.h | \
324	awk 'BEGIN { o=0 } { for (i = 1; i <= NF; i++) \
325		if ($i ~ /define/) \
326			break; \
327		++i; \
328		if (o <= $(i+1)) \
329			printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i; \
330		else \
331			exit; \
332		o = $(i+1) }'
333cat <<_EOF_
334	default: /* Should not reach */
335		(void)printf("<invalid=%ld>", (long)cmd);
336	}
337	(void)putchar(',');
338	if (cmd == F_GETFD || cmd == F_SETFD) {
339		if (arg == FD_CLOEXEC)
340			(void)printf("FD_CLOEXEC");
341		else if (arg == 0)
342			(void)printf("0");
343		else {
344			if (decimal)
345				(void)printf("<invalid>%ld", (long)arg);
346			else
347				(void)printf("<invalid>%#lx", (long)arg);
348		}
349	} else if (cmd == F_SETFL) {
350		flagsname(arg);
351	} else {
352		if (decimal)
353			(void)printf("%ld", (long)arg);
354		else
355			(void)printf("%#lx", (long)arg);
356	}
357}
358
359/*
360 * AUTO - Special
361 *
362 * The only reason this is not fully automated is due to the
363 * grep -v RTP_PRIO statement. A better egrep line should
364 * make this capable of being a auto_switch_type() function.
365 */
366void
367rtprioname (int func)
368{
369	switch (func) {
370_EOF_
371egrep "^#[[:space:]]*define[[:space:]]+RTP_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" \
372	$include_dir/sys/rtprio.h | grep -v RTP_PRIO | \
373	awk '{ for (i = 1; i <= NF; i++) \
374		if ($i ~ /define/) \
375			break; \
376		++i; \
377		printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }'
378cat <<_EOF_
379	default: /* Should not reach */
380		(void)printf("<invalid=%ld>", (long)func);
381	}
382}
383
384/*
385 * AUTO - Special
386 *
387 * The send and recv functions have a flags argument which can be
388 * set to 0. There is no corresponding #define. The auto_ functions
389 * detect this as "invalid", which is incorrect here.
390 */
391void
392sendrecvflagsname (int flags)
393{
394	int	or = 0;
395	
396	if (flags == 0) {
397		(void)printf("0");
398		return;
399	}
400_EOF_
401egrep "^#[[:space:]]*define[[:space:]]+MSG_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \
402	awk '{ for (i = 1; i <= NF; i++) \
403		if ($i ~ /define/) \
404			break; \
405		++i; \
406		printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
407cat <<_EOF_
408}
409
410_EOF_
411