1#!/bin/sh
2# $OpenBSD: mksubr,v 1.40 2023/08/13 08:29:28 visa 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>%d", 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>%d", 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 * Wrappers of the above to use with pn()
178 */
179void
180flagsname(int flags)
181{
182	doflagsname(flags, 0);
183}
184
185void
186openflagsname(int flags)
187{
188	doflagsname(flags, 1);
189}
190
191
192_EOF_
193}
194
195
196#
197# Automatically generates a C function used when the argument
198# maps to a single, specific #definition
199#
200auto_switch_type () {
201	local name grep file
202	name=$1
203	grep=$2
204	file=$3
205
206	cat <<_EOF_
207/* AUTO */
208void
209$name (int arg)
210{
211	switch (arg) {
212_EOF_
213	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
214		$include_dir/$file | \
215	awk '{ for (i = 1; i <= NF; i++) \
216		if ($i ~ /define/) \
217			break; \
218		++i; \
219		printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }'
220cat <<_EOF_
221	default: /* Should not reach */
222		(void)printf("<invalid=%d>", arg);
223	}
224}
225
226_EOF_
227}
228
229#
230# Automatically generates a C function used when the argument
231# maps to a #definition
232#
233auto_if_type () {
234	local name grep file
235	name=$1
236	grep=$2
237	file=$3
238
239	cat <<_EOF_
240/* AUTO */
241void
242$name (int arg)
243{
244_EOF_
245	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
246		$include_dir/$file | \
247	awk '{ printf "\t"; \
248		if (NR > 1) \
249			printf "else " ; \
250		printf "if (arg == %s) \n\t\tprintf(\"%s\");\n", $2, $2 }'
251cat <<_EOF_
252	else /* Should not reach */
253		(void)printf("<invalid=%d>", arg);
254}
255
256_EOF_
257}
258
259# C start
260
261cat <<_EOF_
262#include <sys/types.h>
263#include <sys/signal.h>
264#include <sys/event.h>
265#include <sys/fcntl.h>
266#include <sys/stat.h>
267#include <sys/unistd.h>
268#define _KERNEL
269#include <sys/mman.h>
270#undef _KERNEL
271#include <sys/wait.h>
272#include <sys/proc.h>
273#include <sys/socket.h>
274#include <netinet/in.h>
275#include <sys/mount.h>
276#include <sys/poll.h>
277#include <sys/ptrace.h>
278#include <sys/resource.h>
279#include <sys/reboot.h>
280#include <sys/uio.h>
281#include <sys/ktrace.h>
282#include <sched.h>
283#if 0
284#include <sys/linker.h>
285#define _KERNEL
286#include <sys/thr.h>
287#undef _KERNEL
288#include <sys/extattr.h>
289#include <sys/acl.h>
290#include <aio.h>
291#endif
292#include <sys/sem.h>
293#include <sys/ipc.h>
294#if 0
295#include <sys/rtprio.h>
296#endif
297#include <sys/shm.h>
298#if 0
299#include <nfsserver/nfs.h>
300#endif
301#include <ufs/ufs/quota.h>
302#include <sys/syslog.h>
303#include <sys/futex.h>
304#include <stdio.h>
305
306#include "kdump_subr.h"
307
308_EOF_
309
310auto_orz_type "modename" "S_[A-Z]+[[:space:]]+[0-6]{7}" "sys/stat.h" "%#o"
311auto_fflags_type "doflagsname" "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h"
312auto_orz_type "atflagsname" "AT_[A-Z_]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h"
313auto_or_type "accessmodename" "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+" "sys/unistd.h"
314auto_or_type "mmapprotname" "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
315auto_or_type "mmapflagsname" "(__)?MAP_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
316auto_orz_type "wait4optname" "W(NOHANG|UNTRACED|CONTINUED)[[:space:]]+[0-9]+" "sys/wait.h"
317auto_or_type "waitidoptname" "W(NO[A-Z]+|[A-T][A-Z]*ED)[[:space:]]+([0-9]+|W[A-Z]+)" "sys/wait.h"
318#auto_or_type "timerflagsname" "TIMER_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/time.h"
319#auto_or_type "getfsstatflagsname" "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*" "sys/mount.h"
320auto_orz_type "mountflagsname" "MNT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mount.h"
321auto_or_type "rebootoptname" "RB_[A-Z]+[[:space:]]+0x[0-9]+" "sys/reboot.h"
322auto_or_type "flockname" "LOCK_[A-Z]+[[:space:]]+0x[0-9]+" "sys/fcntl.h"
323#auto_or_type "thrcreateflagsname" "THR_[A-Z]+[[:space:]]+0x[0-9]+" "sys/thr.h"
324auto_or_type "mlockallname" "MCL_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
325auto_orz_type "shmatname" "SHM_[A-Z]+[[:space:]]+[0-9]{6}" "sys/shm.h"
326#auto_or_type "nfssvcname" "NFSSVC_[A-Z]+[[:space:]]+0x[0-9]+" "nfsserver/nfs.h"
327#
328auto_switch_type "whencename" "SEEK_[A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h"
329auto_switch_type "pathconfname" "_PC_[_A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h"
330auto_switch_type "rlimitname" "RLIMIT_[A-Z]+[[:space:]]+[0-9]+" "sys/resource.h"
331auto_switch_type "shutdownhowname" "SHUT_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h"
332auto_switch_type "prioname" "PRIO_[A-Z]+[[:space:]]+[0-9]" "sys/resource.h"
333auto_switch_type "madvisebehavname" "MADV_[A-Z]+[[:space:]]+[0-9A-Z_]+" "sys/mman.h"
334auto_switch_type "msyncflagsname" "MS_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
335auto_switch_type "clocktypename" "CLOCK_[_A-Z]+[[:space:]]+[0-9]+" "sys/_time.h"
336#auto_switch_type "schedpolicyname" "SCHED_[A-Z]+[[:space:]]+[0-9]+" "sched.h"
337#auto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
338#auto_switch_type "extattrctlname" "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h"
339#auto_switch_type "kldsymcmdname" "KLDSYM_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
340#auto_switch_type "sendfileflagsname" "SF_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h"
341#auto_switch_type "acltypename" "ACL_TYPE_[A-Z4_]+[[:space:]]+0x[0-9]+" "sys/acl.h"
342auto_switch_type "rusagewho" "RUSAGE_[A-Z]+[[:space:]]+[-0-9()]+" "sys/resource.h"
343auto_orz_type "sigactionflagname" "SA_[A-Z]+[[:space:]]+0x[0-9]+" "sys/signal.h"
344auto_switch_type "sigprocmaskhowname" "SIG_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h"
345auto_switch_type "sigill_name" "ILL_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
346auto_switch_type "sigtrap_name" "TRAP_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
347auto_switch_type "sigemt_name" "EMT_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
348auto_switch_type "sigfpe_name" "FPE_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
349auto_switch_type "sigbus_name" "BUS_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
350auto_switch_type "sigsegv_name" "SEGV_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
351auto_switch_type "sigchld_name" "CLD_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
352#auto_switch_type "lio_listioname" "LIO_(NO)?WAIT[[:space:]]+[0-9]+" "aio.h"
353auto_switch_type "minheritname" "MAP_INHERIT_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
354auto_switch_type "quotactlname" "Q_[A-Z]+[[:space:]]+0x[0-9]+" "ufs/ufs/quota.h"
355#auto_if_type "sockdomainname" "PF_[[:alnum:]]+[[:space:]]+" "sys/socket.h"
356auto_if_type "sockfamilyname" "AF_[[:alnum:]]+[[:space:]]+" "sys/socket.h"
357auto_if_type "sockipprotoname" "IPPROTO_[[:alnum:]]+[[:space:]]+" "netinet/in.h"
358auto_switch_type "sockoptname" "SO_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h"
359#auto_switch_type "ptraceopname" "PT_[[:alnum:]_]+[[:space:]]+[0-9]+" "sys/ptrace.h"
360# exclude KTRFAC_MASK
361auto_orz_type "ktracefacname" "KTRFAC_[^M][[:alnum:]_]+" "sys/ktrace.h"
362auto_switch_type "itimername" "ITIMER_[[:alnum:]_]+" "sys/time.h"
363auto_switch_type "evfiltername" "EVFILT_[[:alnum:]_]+[[:space:]]+[(]" "sys/event.h"
364auto_orz_type "pollfdeventname" "POLL[^_][[:alnum:]_]+[[:space:]]+0x" "sys/poll.h"
365# exclude EV_{SYSFLAGS,FLAG1}
366auto_orz_type "evflagsname" "EV_[^S][A-Z]+[[:space:]]+0x" "sys/event.h"
367auto_orz_type "syslogflagname" "LOG_[A-Z]+[[:space:]]+0x0*[1248]0*[[:space:]]" "sys/syslog.h"
368auto_orz_type "futexflagname" "FUTEX_[A-Z_]+[[:space:]]+[0-9]+" "sys/futex.h"
369auto_switch_type "flocktypename" "F_[A-Z]+LCK" "sys/fcntl.h"
370
371cat <<_EOF_
372/*
373 * AUTO - Special
374 * F_ is used to specify fcntl commands as well as arguments. Both sets are
375 * grouped in fcntl.h, and this awk script grabs the first group.
376 */
377void
378fcntlcmdname (int arg)
379{
380	int noarg = 0;
381
382	switch (arg1) {
383_EOF_
384egrep "^#[[:space:]]*define[[:space:]]+F_[A-Z_]+[[:space:]]+[0-9]+[[:space:]]*" \
385	$include_dir/sys/fcntl.h | \
386	awk 'BEGIN { o=0; \
387		noarg["F_GETFD"] = 1; \
388		noarg["F_GETFL"] = 1; \
389		noarg["F_ISATTY"] = 1; \
390		noarg["F_GETOWN"] = 1; \
391	     }{ for (i = 1; i <= NF; i++) \
392		if ($i ~ /define/) \
393			break; \
394		++i; \
395		if (o > $(i+1)) \
396			exit; \
397		printf "\tcase %s:\n\t\t(void)printf(\"%s\");%s\n\t\tbreak;\n", $i, $i, \
398			noarg[$i] ? "\n\t\tnoarg = 1;" : ""; \
399		o = $(i+1) }'
400cat <<_EOF_
401	default: /* Should not reach */
402		(void)printf("<invalid=%d>", arg1);
403	}
404	if (arg1 == F_SETFD) {
405		(void)putchar(',');
406		if (arg == FD_CLOEXEC)
407			(void)printf("FD_CLOEXEC");
408		else if (arg == 0)
409			(void)printf("0");
410		else
411			(void)printf("<invalid>%#x", arg);
412
413	} else if (arg1 == F_SETFL) {
414		(void)putchar(',');
415		doflagsname(arg, 0);
416	} else if (!fancy || !noarg)
417		(void)printf(",%#x", arg);
418}
419
420/*
421 * AUTO - Special
422 *
423 * The send and recv functions have a flags argument which can be
424 * set to 0. There is no corresponding #define. The auto_ functions
425 * detect this as "invalid", which is incorrect here.
426 */
427void
428sendrecvflagsname (int flags)
429{
430	int	or = 0;
431
432	if (flags == 0) {
433		(void)printf("0");
434		return;
435	}
436
437	printf("%#x<", flags);
438_EOF_
439egrep "^#[[:space:]]*define[[:space:]]+MSG_[_A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \
440	awk '{ for (i = 1; i <= NF; i++) \
441		if ($i ~ /define/) \
442			break; \
443		++i; \
444		printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
445cat <<_EOF_
446	printf(">");
447}
448
449/*
450 * AUTO - Special
451 *
452 * SOCK_NONBLOCK and SOCK_CLOEXEC are or'ed into the type
453 */
454static void
455dosocktypename (int arg, int show_type)
456{
457	int	type = arg & 0xff;		/* XXX */
458	int	or = 0;
459	
460	printf("%#x<", arg);
461	if (show_type || type) {
462		or = 1;
463		switch (type) {
464_EOF_
465	egrep "^#[[:space:]]*define[[:space:]]+SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*[[:space:]]*" \
466		$include_dir/sys/socket.h | \
467	awk '{ for (i = 1; i <= NF; i++) \
468		if ($i ~ /define/) \
469			break; \
470		++i; \
471		printf "\t\tcase %s:\n\t\t\t(void)printf(\"%s\");\n\t\t\tbreak;\n", $i, $i }'
472cat <<_EOF_
473		default: /* Should not reach */
474			(void)printf("<invalid=%d>", arg);
475		}
476	}
477
478_EOF_
479	egrep "^#[[:space:]]*define[[:space:]]+SOCK_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" \
480		$include_dir/sys/socket.h | \
481	awk '{ for (i = 1; i <= NF; i++) \
482		if ($i ~ /define/) \
483			break; \
484		++i; \
485		printf "\tif_print_or(arg, %s, or);\n", $i }'
486cat <<_EOF_
487	printf(">");
488}
489
490void
491socktypename (int arg)
492{
493	dosocktypename(arg, 1);
494}
495
496void
497sockflagsname (int arg)
498{
499	dosocktypename(arg, 0);
500}
501
502void
503quotactlcmdname(int cmd)
504{
505	printf("%#x<QCMD(", cmd);
506	quotactlname(cmd >> SUBCMDSHIFT);
507	switch (cmd & SUBCMDMASK) {
508	case USRQUOTA:
509		printf(",%s)>", "USRQUOTA");
510		break;
511	case GRPQUOTA:
512		printf(",%s)>", "GRPQUOTA");
513		break;
514	default:
515		printf(",<invalid>%#x)>", cmd & SUBCMDMASK);
516		break;
517	}
518}
519
520/*
521 * AUTO - Special
522 *
523 * kevent() NOTE_* interpretation depends on the filter type
524 */
525void
526evfflagsname (int filter, int fflags)
527{
528	int	or = 0;
529
530	if (fflags == 0) {
531		printf("0<>");
532		return;
533	}
534	printf("%#x<", fflags);
535	switch (filter) {
536	case EVFILT_READ:
537	case EVFILT_WRITE:
538		if_print_or(fflags, NOTE_LOWAT, or);
539		if_print_or(fflags, NOTE_EOF, or);
540		break;
541	case EVFILT_VNODE:
542_EOF_
543	egrep "^#[[:space:]]*define[[:space:]]+NOTE_.[^O][A-Z]+[[:space:]]+0x[01248]{4}[^[:alnum:]]" \
544		$include_dir/sys/event.h | \
545	awk '{ for (i = 1; i <= NF; i++) \
546		if ($i ~ /define/) \
547			break; \
548		++i; \
549		printf "\t\tif_print_or(fflags, %s, or);\n", $i }'
550cat <<_EOF_
551		break;
552	case EVFILT_PROC:
553_EOF_
554	egrep "^#[[:space:]]*define[[:space:]]+NOTE_[^S][A-Z]+[[:space:]]+0x[01248]{8}" \
555		$include_dir/sys/event.h | \
556	awk '{ for (i = 1; i <= NF; i++) \
557		if ($i ~ /define/) \
558			break; \
559		++i; \
560		printf "\t\tif_print_or(fflags, %s, or);\n", $i }'
561cat <<_EOF_
562		break;
563	case EVFILT_TIMER:
564#define NOTE_TIMER_UNITMASK \
565    (NOTE_SECONDS|NOTE_MSECONDS|NOTE_USECONDS|NOTE_NSECONDS)
566		switch (fflags & NOTE_TIMER_UNITMASK) {
567		case NOTE_SECONDS:
568			printf("NOTE_SECONDS");
569			break;
570		case NOTE_MSECONDS:
571			printf("NOTE_MSECONDS");
572			break;
573		case NOTE_USECONDS:
574			printf("NOTE_USECONDS");
575			break;
576		case NOTE_NSECONDS:
577			printf("NOTE_NSECONDS");
578			break;
579		default:
580			printf("invalid");
581			break;
582		}
583		or = 1;
584		if_print_or(fflags, NOTE_ABSTIME, or);
585		break;
586	}
587	printf(">");
588}
589
590_EOF_
591