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