mksubr revision 1.22
1#!/bin/sh
2# $OpenBSD: mksubr,v 1.22 2014/09/17 19:12:55 guenther Exp $
3#
4# Copyright (c) 2006 David Kirchner <dpk@dpk.net>
5#
6# Permission to use, copy, modify, and distribute this software for any
7# purpose with or without fee is hereby granted, provided that the above
8# copyright notice and this permission notice appear in all copies.
9#
10# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17#
18# $FreeBSD: src/usr.bin/kdump/mksubr,v 1.17 2011/06/06 19:00:38 dchagin Exp $
19#
20# Generates kdump_subr.c
21# mkioctls is a special-purpose script, and works fine as it is
22# now, so it remains independent. The idea behind how it generates
23# its list was heavily borrowed here.
24#
25# Some functions here are automatically generated. This can mean
26# the user will see unusual kdump output or errors while building
27# if the underlying .h files are changed significantly.
28#
29# Key:
30# AUTO: Completely auto-generated with either the "or" or the "switch"
31# method.
32# AUTO - Special: Generated automatically, but with some extra commands
33# that the auto_*_type() functions are inappropriate for.
34# MANUAL: Manually entered and must therefore be manually updated.
35
36set -e
37
38LC_ALL=C; export LC_ALL
39
40if [ -z "$1" ]
41then
42	echo "usage: sh $0 include-dir"
43	exit 1
44fi
45include_dir=$1
46
47#
48# Automatically generates a C function that will print out the
49# numeric input as a pipe-delimited string of the appropriate
50# #define keys. ex:
51# 0x1a4<S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH>
52# The XOR is necessary to prevent including the "0"-value in every
53# line.
54#
55auto_or_type () {
56	local name grep file
57	name=$1
58	grep=$2
59	file=$3
60	format=${4-%#x}
61
62	cat <<_EOF_
63/* AUTO */
64void
65$name (int arg)
66{
67	int	or = 0;
68	printf("$format<", arg);
69_EOF_
70	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
71		$include_dir/$file | \
72	awk '{ for (i = 1; i <= NF; i++) \
73		if ($i ~ /define/) \
74			break; \
75		++i; \
76		printf "\tif(!((arg>0)^((%s)>0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
77cat <<_EOF_
78	printf(">");
79	if (or == 0)
80		(void)printf("<invalid>%ld", (long)arg);
81}
82
83_EOF_
84}
85
86#
87# Like auto_or_type(), but a zero value is valid and prints as "0<>"
88#
89auto_orz_type () {
90	local name grep file
91	name=$1
92	grep=$2
93	file=$3
94	format=${4-%#x}
95
96	cat <<_EOF_
97/* AUTO */
98void
99$name (int arg)
100{
101	int	or = 0;
102	if (arg == 0) {
103		printf("0<>");
104		return;
105	}
106	printf("$format<", arg);
107_EOF_
108	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
109		$include_dir/$file | \
110	awk '{ for (i = 1; i <= NF; i++) \
111		if ($i ~ /define/) \
112			break; \
113		++i; \
114		printf "\tif(!((arg>0)^((%s)>0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
115cat <<_EOF_
116	printf(">");
117	if (or == 0)
118		(void)printf("<invalid>%ld", (long)arg);
119}
120
121_EOF_
122}
123
124#
125# Automatically generates a C function that will print out a
126# file flags input as a pipe-delimited string of the appropriate
127# #define keys. ex:
128# 0x30000<O_RDONLY|O_CLOEXEC|O_DIRECTORY>
129# This is different than the others to handle O_RDONLY correctly when
130# other flags are present and to diagnose an invalid O_ACCMODE value
131#
132auto_fflags_type () {
133	local name grep file
134	name=$1
135	grep=$2
136	file=$3
137
138	cat <<_EOF_
139/* AUTO */
140void
141$name (int arg, int show_accmode)
142{
143	int	or = 0;
144
145	printf("%#x<", arg);
146	if (show_accmode || (arg & O_ACCMODE)) {
147		or = 1;
148		switch (arg & O_ACCMODE) {
149		case O_RDONLY:
150			printf("O_RDONLY");
151			break;
152		case O_WRONLY:
153			printf("O_WRONLY");
154			break;
155		case O_RDWR:
156			printf("O_RDWR");
157			break;
158		default:
159			printf("<invalid>O_ACCMODE");
160			break;
161		}
162	}
163_EOF_
164	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
165		$include_dir/$file | \
166	egrep -v 'O_(RD(ONLY|WR)|WRONLY|ACCMODE)' | \
167	awk '{ for (i = 1; i <= NF; i++) \
168		if ($i ~ /define/) \
169			break; \
170		++i; \
171		printf "\tif_print_or(arg, %s, or);\n", $i }'
172cat <<_EOF_
173	printf(">");
174}
175
176/*
177 * A wrapper of the above to use with pn()
178 */
179void
180flagsname(int flags)
181{
182	doflagsname(flags, 0);
183}
184
185
186_EOF_
187}
188
189
190#
191# Automatically generates a C function used when the argument
192# maps to a single, specific #definition
193#
194auto_switch_type () {
195	local name grep file
196	name=$1
197	grep=$2
198	file=$3
199
200	cat <<_EOF_
201/* AUTO */
202void
203$name (int arg)
204{
205	switch (arg) {
206_EOF_
207	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
208		$include_dir/$file | \
209	awk '{ for (i = 1; i <= NF; i++) \
210		if ($i ~ /define/) \
211			break; \
212		++i; \
213		printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }'
214cat <<_EOF_
215	default: /* Should not reach */
216		(void)printf("<invalid=%ld>", (long)arg);
217	}
218}
219
220_EOF_
221}
222
223#
224# Automatically generates a C function used when the argument
225# maps to a #definition
226#
227auto_if_type () {
228	local name grep file
229	name=$1
230	grep=$2
231	file=$3
232
233	cat <<_EOF_
234/* AUTO */
235void
236$name (int arg)
237{
238_EOF_
239	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
240		$include_dir/$file | \
241	awk '{ printf "\t"; \
242		if (NR > 1) \
243			printf "else " ; \
244		printf "if (arg == %s) \n\t\tprintf(\"%s\");\n", $2, $2 }'
245cat <<_EOF_
246	else /* Should not reach */
247		(void)printf("<invalid=%ld>", (long)arg);
248}
249
250_EOF_
251}
252
253# C start
254
255cat <<_EOF_
256#include <stdio.h>
257#include <sys/param.h>
258#include <sys/fcntl.h>
259#include <sys/stat.h>
260#include <sys/unistd.h>
261#define _KERNEL
262#include <sys/mman.h>
263#undef _KERNEL
264#include <sys/wait.h>
265#include <sys/proc.h>
266#define _KERNEL
267#define COMPAT_43
268#include <sys/socket.h>
269#undef _KERNEL
270#include <netinet/in.h>
271#include <sys/param.h>
272#include <sys/mount.h>
273#include <sys/poll.h>
274#include <sys/ptrace.h>
275#include <sys/resource.h>
276#include <sys/reboot.h>
277#include <sys/uio.h>
278#include <sys/ktrace.h>
279#include <sched.h>
280#if 0
281#include <sys/linker.h>
282#define _KERNEL
283#include <sys/thr.h>
284#undef _KERNEL
285#include <sys/extattr.h>
286#include <sys/acl.h>
287#include <aio.h>
288#endif
289#include <sys/sem.h>
290#include <sys/ipc.h>
291#if 0
292#include <sys/rtprio.h>
293#endif
294#include <sys/shm.h>
295#if 0
296#include <nfsserver/nfs.h>
297#endif
298#include <ufs/ufs/quota.h>
299
300#include "kdump_subr.h"
301
302_EOF_
303
304auto_orz_type "modename" "S_[A-Z]+[[:space:]]+[0-6]{7}" "sys/stat.h" "%#o"
305auto_fflags_type "doflagsname" "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h"
306auto_orz_type "atflagsname" "AT_[A-Z_]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h"
307auto_or_type "accessmodename" "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+" "sys/unistd.h"
308auto_or_type "mmapprotname" "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
309auto_or_type "mmapflagsname" "(__)?MAP_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
310auto_orz_type "wait4optname" "W[A-Z]+[[:space:]]+[0-9]+" "sys/wait.h"
311#auto_or_type "timerflagsname" "TIMER_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/time.h"
312#auto_or_type "getfsstatflagsname" "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*" "sys/mount.h"
313#auto_or_type "mountflagsname" "MNT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mount.h"
314#auto_or_type "rebootoptname" "RB_[A-Z]+[[:space:]]+0x[0-9]+" "sys/reboot.h"
315auto_or_type "flockname" "LOCK_[A-Z]+[[:space:]]+0x[0-9]+" "sys/fcntl.h"
316#auto_or_type "thrcreateflagsname" "THR_[A-Z]+[[:space:]]+0x[0-9]+" "sys/thr.h"
317auto_or_type "mlockallname" "MCL_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
318auto_orz_type "shmatname" "SHM_[A-Z]+[[:space:]]+[0-9]{6}" "sys/shm.h"
319#auto_or_type "nfssvcname" "NFSSVC_[A-Z]+[[:space:]]+0x[0-9]+" "nfsserver/nfs.h"
320#
321auto_switch_type "whencename" "SEEK_[A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h"
322auto_switch_type "pathconfname" "_PC_[_A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h"
323auto_switch_type "rlimitname" "RLIMIT_[A-Z]+[[:space:]]+[0-9]+" "sys/resource.h"
324auto_switch_type "shutdownhowname" "SHUT_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h"
325#auto_switch_type "prioname" "PRIO_[A-Z]+[[:space:]]+[0-9]" "sys/resource.h"
326auto_switch_type "madvisebehavname" "_?MADV_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
327auto_switch_type "msyncflagsname" "MS_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
328auto_switch_type "clocktypename" "CLOCK_[_A-Z]+[[:space:]]+[0-9]+" "sys/_time.h"
329#auto_switch_type "schedpolicyname" "SCHED_[A-Z]+[[:space:]]+[0-9]+" "sched.h"
330#auto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
331#auto_switch_type "extattrctlname" "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h"
332#auto_switch_type "kldsymcmdname" "KLDSYM_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
333#auto_switch_type "sendfileflagsname" "SF_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h"
334#auto_switch_type "acltypename" "ACL_TYPE_[A-Z4_]+[[:space:]]+0x[0-9]+" "sys/acl.h"
335auto_switch_type "rusagewho" "RUSAGE_[A-Z]+[[:space:]]+[-0-9()]+" "sys/resource.h"
336auto_orz_type "sigactionflagname" "SA_[A-Z]+[[:space:]]+0x[0-9]+" "sys/signal.h"
337auto_switch_type "sigprocmaskhowname" "SIG_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h"
338auto_switch_type "sigill_name" "ILL_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
339auto_switch_type "sigtrap_name" "TRAP_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
340auto_switch_type "sigemt_name" "EMT_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
341auto_switch_type "sigfpe_name" "FPE_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
342auto_switch_type "sigbus_name" "BUS_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
343auto_switch_type "sigsegv_name" "SEGV_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
344auto_switch_type "sigchld_name" "CLD_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
345#auto_switch_type "lio_listioname" "LIO_(NO)?WAIT[[:space:]]+[0-9]+" "aio.h"
346auto_switch_type "minheritname" "MAP_INHERIT_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
347#auto_switch_type "quotactlname" "Q_[A-Z]+[[:space:]]+0x[0-9]+" "ufs/ufs/quota.h"
348auto_if_type "sockdomainname" "PF_[[:alnum:]]+[[:space:]]+" "sys/socket.h"
349auto_if_type "sockfamilyname" "AF_[[:alnum:]]+[[:space:]]+" "sys/socket.h"
350auto_if_type "sockipprotoname" "IPPROTO_[[:alnum:]]+[[:space:]]+" "netinet/in.h"
351auto_switch_type "sockoptname" "SO_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h"
352#auto_switch_type "ptraceopname" "PT_[[:alnum:]_]+[[:space:]]+[0-9]+" "sys/ptrace.h"
353auto_orz_type "ktracefacname" "KTRFAC_[^M][[:alnum:]_]+" "sys/ktrace.h"
354auto_switch_type "itimername" "ITIMER_[[:alnum:]_]+" "sys/time.h"
355
356cat <<_EOF_
357/*
358 * AUTO - Special
359 * F_ is used to specify fcntl commands as well as arguments. Both sets are
360 * grouped in fcntl.h, and this awk script grabs the first group.
361 */
362void
363fcntlcmdname (int cmd, int arg)
364{
365	switch (cmd) {
366_EOF_
367egrep "^#[[:space:]]*define[[:space:]]+F_[A-Z_]+[[:space:]]+[0-9]+[[:space:]]*" \
368	$include_dir/sys/fcntl.h | \
369	awk 'BEGIN { o=0 } { for (i = 1; i <= NF; i++) \
370		if ($i ~ /define/) \
371			break; \
372		++i; \
373		if (o <= $(i+1)) \
374			printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i; \
375		else \
376			exit; \
377		o = $(i+1) }'
378cat <<_EOF_
379	default: /* Should not reach */
380		(void)printf("<invalid=%ld>", (long)cmd);
381	}
382	if (cmd == F_SETFD) {
383		(void)putchar(',');
384		if (arg == FD_CLOEXEC)
385			(void)printf("FD_CLOEXEC");
386		else if (arg == 0)
387			(void)printf("0");
388		else {
389			if (decimal)
390				(void)printf("<invalid>%ld", (long)arg);
391			else
392				(void)printf("<invalid>%#lx", (long)arg);
393		}
394
395	} else if (cmd == F_SETFL) {
396		(void)putchar(',');
397		flagsname(arg);
398	} else if (!fancy || (cmd != F_GETFD && cmd != F_GETFL)) {
399		(void)putchar(',');
400		if (decimal)
401			(void)printf("%ld", (long)arg);
402		else
403			(void)printf("%#lx", (long)arg);
404	}
405}
406
407/*
408 * AUTO - Special
409 *
410 * The send and recv functions have a flags argument which can be
411 * set to 0. There is no corresponding #define. The auto_ functions
412 * detect this as "invalid", which is incorrect here.
413 */
414void
415sendrecvflagsname (int flags)
416{
417	int	or = 0;
418
419	if (flags == 0) {
420		(void)printf("0");
421		return;
422	}
423
424	printf("%#x<", flags);
425_EOF_
426egrep "^#[[:space:]]*define[[:space:]]+MSG_[_A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \
427	awk '{ for (i = 1; i <= NF; i++) \
428		if ($i ~ /define/) \
429			break; \
430		++i; \
431		printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
432cat <<_EOF_
433	printf(">");
434}
435
436/*
437 * AUTO - Special
438 *
439 * SOCK_NONBLOCK and SOCK_CLOEXEC are or'ed into the type
440 */
441static void
442dosocktypename (int arg, int show_type)
443{
444	int	type = arg & 0xff;		/* XXX */
445	int	or = 0;
446	
447	printf("%#x<", arg);
448	if (show_type || type) {
449		or = 1;
450		switch (type) {
451_EOF_
452	egrep "^#[[:space:]]*define[[:space:]]+SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*[[:space:]]*" \
453		$include_dir/sys/socket.h | \
454	awk '{ for (i = 1; i <= NF; i++) \
455		if ($i ~ /define/) \
456			break; \
457		++i; \
458		printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }'
459cat <<_EOF_
460		default: /* Should not reach */
461			(void)printf("<invalid=%d>", arg);
462		}
463	}
464
465_EOF_
466	egrep "^#[[:space:]]*define[[:space:]]+SOCK_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" \
467		$include_dir/sys/socket.h | \
468	awk '{ for (i = 1; i <= NF; i++) \
469		if ($i ~ /define/) \
470			break; \
471		++i; \
472		printf "\tif_print_or(arg, %s, or);\n", $i }'
473cat <<_EOF_
474	printf(">");
475}
476
477void
478socktypename (int arg)
479{
480	dosocktypename(arg, 1);
481}
482
483void
484sockflagsname (int arg)
485{
486	dosocktypename(arg, 0);
487}
488
489_EOF_
490