mksubr revision 177097
1163953Srrs#!/bin/sh
2169382Srrs#
3237896Stuexen# $FreeBSD: head/usr.bin/kdump/mksubr 177097 2008-03-12 11:51:01Z jeff $
4237896Stuexen#
5163953Srrs# Generates kdump_subr.c
6163953Srrs# mkioctls is a special-purpose script, and works fine as it is
7163953Srrs# now, so it remains independent. The idea behind how it generates
8163953Srrs# its list was heavily borrowed here.
9163953Srrs#
10231038Stuexen# Some functions here are automatically generated. This can mean
11163953Srrs# the user will see unusual kdump output or errors while building
12163953Srrs# if the underlying .h files are changed significantly.
13163953Srrs#
14231038Stuexen# Key:
15163953Srrs# AUTO: Completely auto-generated with either the "or" or the "switch"
16163953Srrs# method.
17163953Srrs# AUTO - Special: Generated automatically, but with some extra commands
18163953Srrs# that the auto_*_type() functions are inappropriate for.
19163953Srrs# MANUAL: Manually entered and must therefore be manually updated.
20163953Srrs
21163953Srrsset -e
22163953Srrs
23163953SrrsLC_ALL=C; export LC_ALL
24163953Srrs
25163953Srrsif [ -z "$1" ]
26163953Srrsthen
27163953Srrs	echo "usage: sh $0 include-dir"
28163953Srrs	exit 1
29163953Srrsfi
30163953Srrsinclude_dir=$1
31163953Srrs
32163953Srrs#
33163953Srrs# Automatically generates a C function that will print out the
34163953Srrs# numeric input as a pipe-delimited string of the appropriate
35163953Srrs# #define keys. ex:
36163953Srrs# S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH
37166086Srrs# The XOR is necessary to prevent including the "0"-value in every
38163953Srrs# line.
39163953Srrs#
40163953Srrsauto_or_type () {
41163953Srrs	local name grep file
42167598Srrs	name=$1
43163953Srrs	grep=$2
44163953Srrs	file=$3
45163953Srrs
46163953Srrs	cat <<_EOF_
47163953Srrs/* AUTO */
48163953Srrsvoid
49163953Srrs$name (int arg)
50163953Srrs{
51163953Srrs	int	or = 0;
52185694Srrs_EOF_
53163953Srrs	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
54163953Srrs		$include_dir/$file | \
55163953Srrs	awk '{ for (i = 1; i <= NF; i++) \
56163953Srrs		if ($i ~ /define/) \
57163953Srrs			break; \
58163953Srrs		++i; \
59163953Srrs		printf "\tif(!((arg>0)^((%s)>0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
60169420Srrscat <<_EOF_
61169420Srrs	if (or == 0)
62169420Srrs		(void)printf("<invalid>%ld", (long)arg);
63163953Srrs}
64163953Srrs
65163953Srrs_EOF_
66163953Srrs}
67163953Srrs
68163953Srrs#
69163953Srrs# Automatically generates a C function used when the argument
70163953Srrs# maps to a single, specific #definition
71163953Srrs#
72163953Srrsauto_switch_type () {
73163953Srrs	local name grep file
74163953Srrs	name=$1
75163953Srrs	grep=$2
76179157Srrs	file=$3
77179157Srrs
78179157Srrs	cat <<_EOF_
79179157Srrs/* AUTO */
80179157Srrsvoid
81169420Srrs$name (int arg)
82169420Srrs{
83169420Srrs	switch (arg) {
84163953Srrs_EOF_
85163953Srrs	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
86163953Srrs		$include_dir/$file | \
87163953Srrs	awk '{ for (i = 1; i <= NF; i++) \
88163953Srrs		if ($i ~ /define/) \
89163953Srrs			break; \
90163953Srrs		++i; \
91163953Srrs		printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }'
92169420Srrscat <<_EOF_
93252927Stuexen	default: /* Should not reach */
94169420Srrs		(void)printf("<invalid=%ld>", (long)arg);
95163953Srrs	}
96163953Srrs}
97163953Srrs
98163953Srrs_EOF_
99167598Srrs}
100224641Stuexen
101163953Srrs#
102237888Stuexen# Automatically generates a C function used when the argument
103172090Srrs# maps to a #definition
104163953Srrs#
105224641Stuexenauto_if_type () {
106224641Stuexen	local name grep file
107224641Stuexen	name=$1
108224641Stuexen	grep=$2
109224641Stuexen	file=$3
110224641Stuexen
111224641Stuexen	cat <<_EOF_
112224641Stuexen/* AUTO */
113224641Stuexenvoid
114163953Srrs$name (int arg)
115163953Srrs{
116163953Srrs_EOF_
117163953Srrs	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
118163953Srrs		$include_dir/$file | \
119163953Srrs	awk '{ printf "\t"; \
120163953Srrs		if (NR > 1) \
121179783Srrs			printf "else " ; \
122171943Srrs		printf "if (arg == %s) \n\t\tprintf(\"%s\");\n", $2, $2 }'
123171943Srrscat <<_EOF_
124171943Srrs	else /* Should not reach */
125171943Srrs		(void)printf("<invalid=%ld>", (long)arg);
126171943Srrs}
127171943Srrs
128163953Srrs_EOF_
129163953Srrs}
130163953Srrs
131179783Srrs# C start
132171943Srrs
133171943Srrscat <<_EOF_
134171943Srrs#include <stdio.h>
135171943Srrs#include <sys/fcntl.h>
136171943Srrs#include <sys/stat.h>
137171943Srrs#include <sys/unistd.h>
138163953Srrs#include <sys/mman.h>
139163953Srrs#include <sys/wait.h>
140169420Srrs#define _KERNEL
141252927Stuexen#include <sys/socket.h>
142169420Srrs#undef _KERNEL
143169420Srrs#include <netinet/in.h>
144163953Srrs#include <sys/param.h>
145163953Srrs#include <sys/mount.h>
146163953Srrs#include <sys/ptrace.h>
147163953Srrs#include <sys/resource.h>
148163953Srrs#include <sys/reboot.h>
149163953Srrs#include <sched.h>
150266181Stuexen#include <sys/linker.h>
151163953Srrs#define _KERNEL
152266181Stuexen#include <sys/thr.h>
153266181Stuexen#undef _KERNEL
154165220Srrs#include <sys/extattr.h>
155266181Stuexen#include <sys/acl.h>
156163953Srrs#include <aio.h>
157163953Srrs#include <sys/sem.h>
158163953Srrs#include <sys/ipc.h>
159163953Srrs#include <sys/rtprio.h>
160163953Srrs#include <sys/shm.h>
161214928Stuexen#include <nfsserver/nfs.h>
162214928Stuexen#include <ufs/ufs/quota.h>
163214928Stuexen
164214928Stuexen#include "kdump_subr.h"
165163953Srrs
166163953Srrs/*
167163953Srrs * These are simple support macros. print_or utilizes a variable
168171440Srrs * defined in the calling function to track whether or not it should
169163953Srrs * print a logical-OR character ('|') before a string. if_print_or
170163953Srrs * simply handles the necessary "if" statement used in many lines
171171440Srrs * of this file.
172163953Srrs */
173163953Srrs#define print_or(str,orflag) do {                  \\
174171440Srrs	if (orflag) putchar('|'); else orflag = 1; \\
175171440Srrs	printf (str); }                            \\
176171440Srrs	while (0)
177171440Srrs#define if_print_or(i,flag,orflag) do {            \\
178163953Srrs	if ((i & flag) == flag)                    \\
179163953Srrs	print_or(#flag,orflag); }                  \\
180163953Srrs	while (0)
181163953Srrs
182171440Srrs/* MANUAL */
183171440Srrsextern char *signames[]; /* from kdump.c */
184171440Srrsvoid
185171440Srrssigname (int sig)
186171440Srrs{
187171440Srrs	if (sig > 0 && sig < NSIG)
188171440Srrs		(void)printf("SIG%s",signames[sig]);
189171440Srrs	else
190171440Srrs		(void)printf("SIG %d", sig);
191163953Srrs}
192171440Srrs
193171440Srrs/* MANUAL */
194171440Srrsvoid
195171440Srrssemctlname (int cmd)
196163953Srrs{
197171440Srrs	switch (cmd) {
198171440Srrs	case GETNCNT:
199171440Srrs		(void)printf("GETNCNT");
200171440Srrs		break;
201171440Srrs	case GETPID:
202171440Srrs		(void)printf("GETPID");
203171440Srrs		break;
204171440Srrs	case GETVAL:
205171440Srrs		(void)printf("GETVAL");
206171440Srrs		break;
207171440Srrs	case GETALL:
208171440Srrs		(void)printf("GETALL");
209171440Srrs		break;
210171440Srrs	case GETZCNT:
211171440Srrs		(void)printf("GETZCNT");
212171440Srrs		break;
213171440Srrs	case SETVAL:
214171440Srrs		(void)printf("SETVAL");
215171440Srrs		break;
216171440Srrs	case SETALL:
217171440Srrs		(void)printf("SETALL");
218171440Srrs		break;
219171440Srrs	case IPC_RMID:
220171440Srrs		(void)printf("IPC_RMID");
221171440Srrs		break;
222171440Srrs	case IPC_SET:
223171440Srrs		(void)printf("IPC_SET");
224171440Srrs		break;
225171440Srrs	case IPC_STAT:
226171440Srrs		(void)printf("IPC_STAT");
227171440Srrs		break;
228171440Srrs	default: /* Should not reach */
229171440Srrs		(void)printf("<invalid=%ld>", (long)cmd);
230171440Srrs	}
231171440Srrs}
232171440Srrs
233171440Srrs/* MANUAL */
234171440Srrsvoid
235171440Srrsshmctlname (int cmd) {
236171440Srrs	switch (cmd) {
237171440Srrs	case IPC_RMID:
238171440Srrs		(void)printf("IPC_RMID");
239171440Srrs		break;
240171440Srrs	case IPC_SET:
241171440Srrs		(void)printf("IPC_SET");
242171440Srrs		break;
243171440Srrs	case IPC_STAT:
244171440Srrs		(void)printf("IPC_STAT");
245171440Srrs		break;
246171440Srrs	default: /* Should not reach */
247171440Srrs		(void)printf("<invalid=%ld>", (long)cmd);
248171440Srrs	}
249171440Srrs}
250171440Srrs
251171440Srrs/* MANUAL */
252171440Srrsvoid
253171440Srrssemgetname (int flag) {
254171440Srrs	int	or = 0;
255171440Srrs	if_print_or(flag, SEM_R, or);
256171440Srrs	if_print_or(flag, SEM_A, or);
257171440Srrs	if_print_or(flag, (SEM_R>>3), or);
258171440Srrs	if_print_or(flag, (SEM_A>>3), or);
259171440Srrs	if_print_or(flag, (SEM_R>>6), or);
260171440Srrs	if_print_or(flag, (SEM_A>>6), or);
261171440Srrs}
262171440Srrs
263171440Srrs/*
264171440Srrs * MANUAL
265171440Srrs *
266171440Srrs * Only used by SYS_open. Unless O_CREAT is set in flags, the
267171440Srrs * mode argument is unused (and often bogus and misleading).
268171440Srrs */
269171440Srrsvoid
270171440Srrsflagsandmodename (int flags, int mode, int decimal) {
271171440Srrs	flagsname (flags);
272171440Srrs	(void)putchar(',');
273180387Srrs	if ((flags & O_CREAT) == O_CREAT) {
274171440Srrs		modename (mode);
275171440Srrs	} else {
276171440Srrs		if (decimal) {
277171440Srrs			(void)printf("<unused>%ld", (long)mode);
278171440Srrs		} else {
279171440Srrs			(void)printf("<unused>%#lx", (long)mode);
280171440Srrs		}
281171440Srrs	}
282171440Srrs}
283171440Srrs
284171440Srrs/*
285171440Srrs * MANUAL
286171440Srrs *
287171440Srrs * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value
288171440Srrs * referring to a line in /etc/protocols . It might be appropriate
289171440Srrs * to use getprotoent(3) here.
290171440Srrs */
291171440Srrsvoid
292171440Srrssockoptlevelname (int level, int decimal)
293214928Stuexen{
294163953Srrs	if (level == SOL_SOCKET) {
295163953Srrs		(void)printf("SOL_SOCKET");
296163953Srrs	} else {
297163953Srrs		if (decimal) {
298163953Srrs			(void)printf("%ld", (long)level);
299163953Srrs		} else {
300171440Srrs			(void)printf("%#lx", (long)level);
301171440Srrs		}
302171440Srrs	}
303171440Srrs}
304163953Srrs
305163953Srrs_EOF_
306163953Srrs
307163953Srrsauto_or_type "modename" "S_[A-Z]+[[:space:]]+[0-6]{7}" "sys/stat.h"
308163953Srrsauto_or_type "flagsname" "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h"
309163953Srrsauto_or_type "accessmodename" "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+" "sys/unistd.h"
310163953Srrsauto_or_type "mmapprotname" "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
311163953Srrsauto_or_type "mmapflagsname" "MAP_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
312163953Srrsauto_or_type "wait4optname" "W[A-Z]+[[:space:]]+[0-9]+" "sys/wait.h"
313163953Srrsauto_or_type "getfsstatflagsname" "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*" "sys/mount.h"
314163953Srrsauto_or_type "mountflagsname" "MNT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mount.h"
315163953Srrsauto_or_type "rebootoptname" "RB_[A-Z]+[[:space:]]+0x[0-9]+" "sys/reboot.h"
316163953Srrsauto_or_type "flockname" "LOCK_[A-Z]+[[:space:]]+0x[0-9]+" "sys/fcntl.h"
317163953Srrsauto_or_type "thrcreateflagsname" "THR_[A-Z]+[[:space:]]+0x[0-9]+" "sys/thr.h"
318163953Srrsauto_or_type "mlockallname" "MCL_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
319163953Srrsauto_or_type "shmatname" "SHM_[A-Z]+[[:space:]]+[0-9]{6}+" "sys/shm.h"
320171440Srrsauto_or_type "rforkname" "RF[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)" "sys/unistd.h"
321171440Srrsauto_or_type "nfssvcname" "NFSSVC_[A-Z]+[[:space:]]+0x[0-9]+" "nfsserver/nfs.h"
322163953Srrs
323163953Srrsauto_switch_type "whencename" "SEEK_[A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h"
324163953Srrsauto_switch_type "rlimitname" "RLIMIT_[A-Z]+[[:space:]]+[0-9]+" "sys/resource.h"
325171440Srrsauto_switch_type "shutdownhowname" "SHUT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h"
326171440Srrsauto_switch_type "prioname" "PRIO_[A-Z]+[[:space:]]+[0-9]" "sys/resource.h"
327163953Srrsauto_switch_type "madvisebehavname" "_?MADV_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
328163953Srrsauto_switch_type "msyncflagsname" "MS_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
329163953Srrsauto_switch_type "schedpolicyname" "SCHED_[A-Z]+[[:space:]]+[0-9]+" "sched.h"
330163953Srrsauto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
331163953Srrsauto_switch_type "extattrctlname" "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h"
332163953Srrsauto_switch_type "kldsymcmdname" "KLDSYM_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
333163953Srrsauto_switch_type "sendfileflagsname" "SF_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h"
334212225Srrsauto_switch_type "acltypename" "ACL_TYPE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/acl.h"
335212225Srrsauto_switch_type "sigprocmaskhowname" "SIG_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h"
336212225Srrsauto_switch_type "lio_listioname" "LIO_(NO)?WAIT[[:space:]]+[0-9]+" "aio.h"
337163953Srrsauto_switch_type "minheritname" "INHERIT_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
338163953Srrsauto_switch_type "quotactlname" "Q_[A-Z]+[[:space:]]+0x[0-9]+" "ufs/ufs/quota.h"
339163953Srrsauto_if_type "sockdomainname" "PF_[[:alnum:]]+[[:space:]]+" "sys/socket.h"
340163953Srrsauto_if_type "sockfamilyname" "AF_[[:alnum:]]+[[:space:]]+" "sys/socket.h"
341163953Srrsauto_if_type "sockipprotoname" "IPPROTO_[[:alnum:]]+[[:space:]]+" "netinet/in.h"
342163953Srrsauto_switch_type "sockoptname" "SO_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h"
343163953Srrsauto_switch_type "socktypename" "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*" "sys/socket.h"
344163953Srrsauto_switch_type "ptraceopname" "PT_[[:alnum:]]+[[:space:]]+[0-9]+" "sys/ptrace.h"
345163953Srrs
346212225Srrscat <<_EOF_
347212225Srrs/*
348212225Srrs * AUTO - Special
349163953Srrs * F_ is used to specify fcntl commands as well as arguments. Both sets are
350163953Srrs * grouped in fcntl.h, and this awk script grabs the first group.
351167598Srrs */
352167598Srrsvoid
353167598Srrsfcntlcmdname (int cmd, int arg, int decimal)
354167598Srrs{
355163953Srrs	switch (cmd) {
356163953Srrs_EOF_
357214928Stuexenegrep "^#[[:space:]]*define[[:space:]]+F_[A-Z]+[[:space:]]+[0-9]+[[:space:]]*" \
358214928Stuexen	$include_dir/sys/fcntl.h | \
359163953Srrs	awk 'BEGIN { o=0 } { for (i = 1; i <= NF; i++) \
360214928Stuexen		if ($i ~ /define/) \
361163953Srrs			break; \
362163953Srrs		++i; \
363163953Srrs		if (o <= $(i+1)) \
364163953Srrs			printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i; \
365163953Srrs		else \
366163953Srrs			exit; \
367163953Srrs		o = $(i+1) }'
368163953Srrscat <<_EOF_
369163953Srrs	default: /* Should not reach */
370163953Srrs		(void)printf("<invalid=%ld>", (long)cmd);
371163953Srrs	}
372163953Srrs	(void)putchar(',');
373212225Srrs	if (cmd == F_GETFD || cmd == F_SETFD) {
374212225Srrs		if (arg == FD_CLOEXEC)
375212225Srrs			(void)printf("FD_CLOEXEC");
376163953Srrs		else if (arg == 0)
377163953Srrs			(void)printf("0");
378163953Srrs		else {
379163953Srrs			if (decimal)
380163953Srrs				(void)printf("<invalid>%ld", (long)arg);
381163953Srrs			else
382163953Srrs				(void)printf("<invalid>%#lx", (long)arg);
383163953Srrs		}
384169655Srrs	} else if (cmd == F_SETFL) {
385163953Srrs		flagsname(arg);
386163953Srrs	} else {
387163953Srrs		if (decimal)
388163953Srrs			(void)printf("%ld", (long)arg);
389163953Srrs		else
390163953Srrs			(void)printf("%#lx", (long)arg);
391163953Srrs	}
392163953Srrs}
393163953Srrs
394163953Srrs/*
395163953Srrs * AUTO - Special
396163953Srrs *
397163953Srrs * The only reason this is not fully automated is due to the
398163953Srrs * grep -v RTP_PRIO statement. A better egrep line should
399163953Srrs * make this capable of being a auto_switch_type() function.
400163953Srrs */
401163953Srrsvoid
402163953Srrsrtprioname (int func)
403210599Srrs{
404163953Srrs	switch (func) {
405170642Srrs_EOF_
406170642Srrsegrep "^#[[:space:]]*define[[:space:]]+RTP_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" \
407170642Srrs	$include_dir/sys/rtprio.h | grep -v RTP_PRIO | \
408163953Srrs	awk '{ for (i = 1; i <= NF; i++) \
409163953Srrs		if ($i ~ /define/) \
410163953Srrs			break; \
411163953Srrs		++i; \
412210599Srrs		printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }'
413163953Srrscat <<_EOF_
414171440Srrs	default: /* Should not reach */
415171440Srrs		(void)printf("<invalid=%ld>", (long)func);
416163953Srrs	}
417163953Srrs}
418163953Srrs
419184333Srrs/*
420184333Srrs * AUTO - Special
421182367Srrs *
422182367Srrs * The send and recv functions have a flags argument which can be
423216822Stuexen * set to 0. There is no corresponding #define. The auto_ functions
424182367Srrs * detect this as "invalid", which is incorrect here.
425182367Srrs */
426182367Srrsvoid
427216822Stuexensendrecvflagsname (int flags)
428216825Stuexen{
429182367Srrs	int	or = 0;
430252927Stuexen
431252943Stuexen	if (flags == 0) {
432252942Stuexen		(void)printf("0");
433252942Stuexen		return;
434252942Stuexen	}
435252942Stuexen_EOF_
436182367Srrsegrep "^#[[:space:]]*define[[:space:]]+MSG_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \
437265963Stuexen	awk '{ for (i = 1; i <= NF; i++) \
438182367Srrs		if ($i ~ /define/) \
439182367Srrs			break; \
440182367Srrs		++i; \
441182367Srrs		printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
442182367Srrscat <<_EOF_
443182367Srrs}
444182367Srrs
445216822Stuexen_EOF_
446196260Stuexen