xref: /dragonfly/usr.bin/kdump/mksubr (revision 65cc0652)
1#!/bin/sh
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: src/usr.bin/kdump/mksubr,v 1.9 2007/04/09 19:16:24 emaste Exp $
20
21set -e
22
23LC_ALL=C; export LC_ALL
24
25if [ -z "$1" ]
26then
27	echo "usage: sh $0 include-dir"
28	exit 1
29fi
30include_dir=$1
31
32#
33# Automatically generates a C function that will print out the
34# numeric input as a pipe-delimited string of the appropriate
35# #define keys. ex:
36# S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH
37# The XOR is necessary to prevent including the "0"-value in every
38# line.
39#
40auto_or_type () {
41	local name grep file
42	name=$1
43	grep=$2
44	file=$3
45
46	cat <<_EOF_
47/* AUTO */
48void
49$name (int arg)
50{
51	int	or = 0;
52	printf("%#x<", arg);
53_EOF_
54	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
55		$include_dir/$file | \
56	awk '{ for (i = 1; i <= NF; i++) \
57		if ($i ~ /define/) \
58			break; \
59		++i; \
60		printf "\tif(!((arg>0)^((%s)>0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
61cat <<_EOF_
62	printf(">");
63	if (or == 0)
64		printf("<invalid>%ld", (long)arg);
65}
66
67_EOF_
68}
69
70#
71# Automatically generates a C function used when the argument
72# maps to a single, specific #definition
73#
74auto_switch_type () {
75	local name grep file noinvdef
76	name=$1
77	grep=$2
78	file=$3
79	noinvdef=$4
80
81	cat <<_EOF_
82/* AUTO */
83void
84$name (int arg)
85{
86	switch (arg) {
87_EOF_
88	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
89		$include_dir/$file | \
90	awk '{ for (i = 1; i <= NF; i++) \
91		if ($i ~ /define/) \
92			break; \
93		++i; \
94		printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i }'
95	if [ -z "$noinvdef" ]; then
96cat <<_EOF_
97	default: /* Should not reach */
98		printf("<invalid=%ld>", (long)arg);
99	}
100}
101
102_EOF_
103	else
104cat <<_EOF_
105	default:
106		printf("%ld", (long)arg);
107	}
108}
109
110_EOF_
111	fi
112}
113
114#
115# Automatically generates a C function used when the argument
116# maps to a #definition
117#
118auto_if_type () {
119	local name grep file
120	name=$1
121	grep=$2
122	file=$3
123
124	cat <<_EOF_
125/* AUTO */
126void
127$name (int arg)
128{
129_EOF_
130	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
131		$include_dir/$file | \
132	awk '{ printf "\t"; \
133		if (NR > 1) \
134			printf "else " ; \
135		printf "if (arg == %s) \n\t\tprintf(\"%s\");\n", $2, $2 }'
136cat <<_EOF_
137	else /* Should not reach */
138		printf("<invalid=%ld>", (long)arg);
139}
140
141_EOF_
142}
143
144# C start
145
146cat <<_EOF_
147#define _KERNEL_STRUCTURES
148
149#include <stdio.h>
150#include <time.h>
151#include <sys/fcntl.h>
152#include <sys/stat.h>
153#include <sys/unistd.h>
154#include <sys/mman.h>
155#include <sys/wait.h>
156#include <sys/socket.h>
157#include <netinet/in.h>
158#include <sys/param.h>
159#include <sys/mount.h>
160#include <sys/resource.h>
161#include <sys/reboot.h>
162#include <sched.h>
163#include <sys/usched.h>
164#include <sys/linker.h>
165#include <sys/extattr.h>
166#include <sys/acl.h>
167#include <aio.h>
168#include <sys/sem.h>
169#include <sys/ipc.h>
170#include <sys/rtprio.h>
171#include <sys/shm.h>
172#include <vfs/ufs/quota.h>
173#include <sys/kenv.h>
174#include <sys/checkpoint.h>
175#include <sys/procctl.h>
176#include <sys/mountctl.h>
177#include <sys/varsym.h>
178
179#include "kdump_subr.h"
180
181/*
182 * These are simple support macros. print_or utilizes a variable
183 * defined in the calling function to track whether or not it should
184 * print a logical-OR character ('|') before a string. if_print_or
185 * simply handles the necessary "if" statement used in many lines
186 * of this file.
187 */
188#define print_or(str,orflag) do {                  \\
189	if (orflag) putchar('|'); else orflag = 1; \\
190	printf (str); }                            \\
191	while (0)
192#define if_print_or(i,flag,orflag) do {            \\
193	if ((i & flag) == flag)                    \\
194	print_or(#flag,orflag); }                  \\
195	while (0)
196
197/* MANUAL */
198extern char *signames[]; /* from kdump.c */
199void
200signame (int sig)
201{
202	if (sig > 0 && sig < NSIG && signames[sig] != NULL)
203		printf("SIG%s",signames[sig]);
204	else
205		printf("SIG %d", sig);
206}
207
208/* MANUAL */
209void
210semctlname (int cmd)
211{
212	switch (cmd) {
213	case GETNCNT:
214		printf("GETNCNT");
215		break;
216	case GETPID:
217		printf("GETPID");
218		break;
219	case GETVAL:
220		printf("GETVAL");
221		break;
222	case GETALL:
223		printf("GETALL");
224		break;
225	case GETZCNT:
226		printf("GETZCNT");
227		break;
228	case SETVAL:
229		printf("SETVAL");
230		break;
231	case SETALL:
232		printf("SETALL");
233		break;
234	case IPC_RMID:
235		printf("IPC_RMID");
236		break;
237	case IPC_SET:
238		printf("IPC_SET");
239		break;
240	case IPC_STAT:
241		printf("IPC_STAT");
242		break;
243	default: /* Should not reach */
244		printf("<invalid=%ld>", (long)cmd);
245	}
246}
247
248/* MANUAL */
249void
250shmctlname (int cmd) {
251	switch (cmd) {
252	case IPC_RMID:
253		printf("IPC_RMID");
254		break;
255	case IPC_SET:
256		printf("IPC_SET");
257		break;
258	case IPC_STAT:
259		printf("IPC_STAT");
260		break;
261	default: /* Should not reach */
262		printf("<invalid=%ld>", (long)cmd);
263	}
264}
265
266/* MANUAL */
267void
268semgetname (int flag) {
269	int	or = 0;
270	if_print_or(flag, SEM_R, or);
271	if_print_or(flag, SEM_A, or);
272	if_print_or(flag, (SEM_R>>3), or);
273	if_print_or(flag, (SEM_A>>3), or);
274	if_print_or(flag, (SEM_R>>6), or);
275	if_print_or(flag, (SEM_A>>6), or);
276}
277
278/*
279 * MANUAL
280 *
281 * Only used by SYS_open. Unless O_CREAT is set in flags, the
282 * mode argument is unused (and often bogus and misleading).
283 */
284void
285flagsandmodename (int flags, int mode, int decimal) {
286	flagsname (flags);
287	putchar(',');
288	if ((flags & O_CREAT) == O_CREAT) {
289		modename (mode);
290	} else {
291		if (decimal) {
292			printf("<unused>%ld", (long)mode);
293		} else {
294			printf("<unused>%#lx", (long)mode);
295		}
296	}
297}
298
299/*
300 * MANUAL
301 *
302 * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value
303 * referring to a line in /etc/protocols . It might be appropriate
304 * to use getprotoent(3) here.
305 */
306void
307sockoptlevelname (int level, int decimal)
308{
309	if (level == SOL_SOCKET) {
310		printf("SOL_SOCKET");
311	} else {
312		if (decimal) {
313			printf("%ld", (long)level);
314		} else {
315			printf("%#lx", (long)level);
316		}
317	}
318}
319
320_EOF_
321
322auto_or_type "modename" "S_[A-Z]+[[:space:]]+[0-6]{7}" "sys/stat.h"
323auto_or_type "flagsname" "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h"
324auto_or_type "accessmodename" "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+" "sys/unistd.h"
325auto_or_type "mmapprotname" "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
326auto_or_type "mmapflagsname" "MAP_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
327auto_or_type "wait4optname" "W[A-Z]+[[:space:]]+[0-9]+" "sys/wait.h"
328auto_or_type "getfsstatflagsname" "MNT_[A-Z]+[[:space:]]+0x[0-9][0-9][0-9][0-9][^0-9]" "sys/mount.h"
329auto_or_type "mountflagsname" "MNT_[A-Z]+[[:space:]]+0x[0-9][0-9][0-9][0-9][0-9]+" "sys/mount.h"
330auto_or_type "rebootoptname" "RB_[A-Z]+[[:space:]]+0x[0-9]+" "sys/reboot.h"
331auto_or_type "flockname" "LOCK_[A-Z]+[[:space:]]+0x[0-9]+" "sys/fcntl.h"
332auto_or_type "mlockallname" "MCL_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
333auto_or_type "shmatname" "SHM_[A-Z]+[[:space:]]+[0-9]{6}+" "sys/shm.h"
334auto_or_type "rforkname" "RF[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)" "sys/unistd.h"
335auto_or_type "chflagsname" "[SU]F_[A-Z]+[[:space:]]+0x[0-9]+" "sys/stat.h"
336auto_or_type "atflagsname" "AT_[A-Z_]+[[:space:]]+[1-9]+[0-9]*" "sys/fcntl.h"
337
338auto_switch_type "whencename" "SEEK_[A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h"
339auto_switch_type "rlimitname" "RLIMIT_[A-Z]+[[:space:]]+[0-9]+" "sys/resource.h"
340auto_switch_type "shutdownhowname" "SHUT_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h"
341auto_switch_type "prioname" "PRIO_[A-Z]+[[:space:]]+[0-9]" "sys/resource.h"
342auto_switch_type "madvisebehavname" "_?MADV_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
343auto_switch_type "msyncflagsname" "MS_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
344auto_switch_type "schedpolicyname" "SCHED_[A-Z]+[[:space:]]+[0-9]+" "sched.h"
345auto_switch_type "extattrctlname" "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h"
346auto_switch_type "kldsymcmdname" "KLDSYM_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
347auto_switch_type "sendfileflagsname" "SF_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h"
348auto_switch_type "acltypename" "ACL_TYPE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/acl.h"
349auto_switch_type "sigprocmaskhowname" "SIG_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h"
350auto_switch_type "lio_listioname" "LIO_(NO)?WAIT[[:space:]]+[0-9]+" "aio.h"
351auto_switch_type "minheritname" "INHERIT_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
352auto_switch_type "quotactlname" "Q_[A-Z]+[[:space:]]+0x[0-9]+" "ufs/ufs/quota.h"
353auto_if_type "sockdomainname" "PF_[[:alnum:]]+[[:space:]]+" "sys/socket.h"
354auto_if_type "sockipprotoname" "IPPROTO_[[:alnum:]]+[[:space:]]+" "netinet/in.h"
355auto_switch_type "sockoptname" "SO_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h"
356auto_switch_type "socktypename" "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*" "sys/socket.h"
357auto_switch_type "clockidname" "CLOCK_[[:alnum:]]+" "time.h" "noinvdef"
358auto_switch_type "pathconfname" "_PC_[[:alnum:]]+" "sys/unistd.h"
359auto_switch_type "uschedcmdname" "USCHED_[[:alnum:]]+" "sys/usched.h"
360auto_switch_type "ckpttypename" "CKPT_[[:alnum:]]+" "sys/checkpoint.h"
361auto_switch_type "procctlcmdname" "PROC_[[:alnum:]]+" "sys/procctl.h"
362auto_switch_type "mountctlopname" "MOUNTCTL_[[:alnum:]]+" "sys/mountctl.h"
363auto_switch_type "varsymlvlname" "VARSYM_[A-Z]+[[:space:]]+[1-9]+[0-9]*" "sys/varsym.h"
364
365cat <<_EOF_
366/*
367 * AUTO - Special
368 * F_ is used to specify fcntl commands as well as arguments. Both sets are
369 * grouped in fcntl.h, and this awk script grabs the first group.
370 */
371void
372fcntlcmdname (int cmd, int arg, int decimal)
373{
374	switch (cmd) {
375_EOF_
376egrep "^#[[:space:]]*define[[:space:]]+F_[A-Z0-9_]+[[:space:]]+[0-9]+[[:space:]]*" \
377	$include_dir/sys/fcntl.h | \
378	awk 'BEGIN { o=0 } { for (i = 1; i <= NF; i++) \
379		if ($i ~ /define/) \
380			break; \
381		++i; \
382		if (o <= $(i+1)) \
383			printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i; \
384		else \
385			exit; \
386		o = $(i+1) }'
387cat <<_EOF_
388	default: /* Should not reach */
389		printf("<invalid=%ld>", (long)cmd);
390	}
391	putchar(',');
392	if (cmd == F_GETFD || cmd == F_SETFD) {
393		if (arg == FD_CLOEXEC)
394			printf("FD_CLOEXEC");
395		else if (arg == 0)
396			printf("0");
397		else {
398			if (decimal)
399				printf("<invalid>%ld", (long)arg);
400			else
401				printf("<invalid>%#lx", (long)arg);
402		}
403	} else if (cmd == F_SETFL) {
404		flagsname(arg);
405	} else {
406		if (decimal)
407			printf("%ld", (long)arg);
408		else
409			printf("%#lx", (long)arg);
410	}
411}
412
413/*
414 * AUTO - Special
415 *
416 * The only reason this is not fully automated is due to the
417 * grep -v RTP_PRIO statement. A better egrep line should
418 * make this capable of being a auto_switch_type() function.
419 */
420void
421rtprioname (int func)
422{
423	switch (func) {
424_EOF_
425egrep "^#[[:space:]]*define[[:space:]]+RTP_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" \
426	$include_dir/sys/rtprio.h | grep -v RTP_PRIO | \
427	awk '{ for (i = 1; i <= NF; i++) \
428		if ($i ~ /define/) \
429			break; \
430		++i; \
431		printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i }'
432cat <<_EOF_
433	default: /* Should not reach */
434		printf("<invalid=%ld>", (long)func);
435	}
436}
437
438/*
439 * AUTO - Special
440 *
441 * The send and recv functions have a flags argument which can be
442 * set to 0. There is no corresponding #define. The auto_ functions
443 * detect this as "invalid", which is incorrect here.
444 */
445void
446sendrecvflagsname (int flags)
447{
448	int	or = 0;
449
450	if (flags == 0) {
451		printf("0");
452		return;
453	}
454
455	printf("%#x<", flags);
456_EOF_
457egrep "^#[[:space:]]*define[[:space:]]+MSG_[A-Z_]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \
458	awk '{ for (i = 1; i <= NF; i++) \
459		if ($i ~ /define/) \
460			break; \
461		++i; \
462		printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
463cat <<_EOF_
464	printf(">");
465}
466
467/*
468 * AUTO - Special
469 *
470 * The only reason this is not fully automated is due to the
471 * grep -vw ... statement. A better egrep line should
472 * make this capable of being a auto_switch_type() function.
473 */
474void
475kenvactname (int act)
476{
477	switch (act) {
478_EOF_
479egrep "^#[[:space:]]*define[[:space:]]+KENV_[A-Z]+[[:space:]]+[0-9]+[[:space:]]*" \
480	$include_dir/sys/kenv.h | grep -vw -e KENV_MNAMELEN -e KENV_MVALLEN | \
481	awk '{ for (i = 1; i <= NF; i++) \
482		if ($i ~ /define/) \
483			break; \
484		++i; \
485		printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i }'
486cat <<_EOF_
487	default: /* Should not reach */
488		printf("<invalid=%ld>", (long)act);
489	}
490}
491_EOF_
492