mksubr revision 261952
1#!/bin/sh
2#
3# Copyright (c) 2006 "David Kirchner" <dpk@dpk.net>. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26# $FreeBSD: stable/9/usr.bin/kdump/mksubr 261952 2014-02-15 23:23:13Z jilles $
27#
28# Generates kdump_subr.c
29# mkioctls is a special-purpose script, and works fine as it is
30# now, so it remains independent. The idea behind how it generates
31# its list was heavily borrowed here.
32#
33# Some functions here are automatically generated. This can mean
34# the user will see unusual kdump output or errors while building
35# if the underlying .h files are changed significantly.
36#
37# Key:
38# AUTO: Completely auto-generated with either the "or" or the "switch"
39# method.
40# AUTO - Special: Generated automatically, but with some extra commands
41# that the auto_*_type() functions are inappropriate for.
42# MANUAL: Manually entered and must therefore be manually updated.
43
44set -e
45
46LC_ALL=C; export LC_ALL
47
48if [ -z "$1" ]
49then
50	echo "usage: sh $0 include-dir"
51	exit 1
52fi
53include_dir=$1
54
55#
56# Automatically generates a C function that will print out the
57# numeric input as a pipe-delimited string of the appropriate
58# #define keys. ex:
59# S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH
60# The XOR is necessary to prevent including the "0"-value in every
61# line.
62#
63auto_or_type () {
64	local name grep file
65	name=$1
66	grep=$2
67	file=$3
68
69	cat <<_EOF_
70/* AUTO */
71void
72$name(intmax_t arg)
73{
74	int or = 0;
75	printf("%#jx<", (uintmax_t)arg);
76_EOF_
77	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
78		$include_dir/$file | \
79	awk '{ for (i = 1; i <= NF; i++) \
80		if ($i ~ /define/) \
81			break; \
82		++i; \
83		printf "\tif (!((arg > 0) ^ ((%s) > 0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
84cat <<_EOF_
85	printf(">");
86	if (or == 0)
87		printf("<invalid>%jd", arg);
88}
89
90_EOF_
91}
92
93#
94# Automatically generates a C function used when the argument
95# maps to a single, specific #definition
96#
97auto_switch_type () {
98	local name grep file
99	name=$1
100	grep=$2
101	file=$3
102
103	cat <<_EOF_
104/* AUTO */
105void
106$name(intmax_t arg)
107{
108	switch (arg) {
109_EOF_
110	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
111		$include_dir/$file | \
112	awk '{ for (i = 1; i <= NF; i++) \
113		if ($i ~ /define/) \
114			break; \
115		++i; \
116		printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i }'
117cat <<_EOF_
118	default: /* Should not reach */
119		printf("<invalid=%jd>", arg);
120	}
121}
122
123_EOF_
124}
125
126#
127# Automatically generates a C function used when the argument
128# maps to a #definition
129#
130auto_if_type () {
131	local name grep file
132	name=$1
133	grep=$2
134	file=$3
135
136	cat <<_EOF_
137/* AUTO */
138void
139$name(intmax_t arg)
140{
141_EOF_
142	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
143		$include_dir/$file | \
144	awk '{ printf "\t"; \
145		if (NR > 1) \
146			printf "else " ; \
147		printf "if (arg == %s) \n\t\tprintf(\"%s\");\n", $2, $2 }'
148cat <<_EOF_
149	else /* Should not reach */
150		printf("<invalid=%jd>", arg);
151}
152
153_EOF_
154}
155
156# C start
157
158cat <<_EOF_
159#include <stdint.h>
160#include <stdio.h>
161#include <sys/fcntl.h>
162#include <sys/stat.h>
163#include <sys/unistd.h>
164#include <sys/mman.h>
165#include <sys/wait.h>
166#define _KERNEL
167#include <sys/socket.h>
168#undef _KERNEL
169#include <netinet/in.h>
170#include <sys/param.h>
171#include <sys/mount.h>
172#include <sys/procctl.h>
173#include <sys/ptrace.h>
174#include <sys/resource.h>
175#include <sys/reboot.h>
176#include <sched.h>
177#include <sys/linker.h>
178#define _KERNEL
179#include <sys/thr.h>
180#undef _KERNEL
181#include <sys/extattr.h>
182#include <sys/acl.h>
183#include <aio.h>
184#include <sys/sem.h>
185#include <sys/ipc.h>
186#include <sys/rtprio.h>
187#include <sys/shm.h>
188#include <nfsserver/nfs.h>
189#include <ufs/ufs/quota.h>
190#include <vm/vm.h>
191#include <vm/vm_param.h>
192
193#include "kdump_subr.h"
194
195/*
196 * These are simple support macros. print_or utilizes a variable
197 * defined in the calling function to track whether or not it should
198 * print a logical-OR character ('|') before a string. if_print_or
199 * simply handles the necessary "if" statement used in many lines
200 * of this file.
201 */
202#define print_or(str,orflag) do {                  \\
203	if (orflag) putchar('|'); else orflag = 1; \\
204	printf (str); }                            \\
205	while (0)
206#define if_print_or(i,flag,orflag) do {            \\
207	if ((i & flag) == flag)                    \\
208	print_or(#flag,orflag); }                  \\
209	while (0)
210
211/* MANUAL */
212extern char *signames[]; /* from kdump.c */
213void
214signame(int sig)
215{
216	if (sig > 0 && sig < NSIG)
217		printf("SIG%s",signames[sig]);
218	else
219		printf("SIG %d", sig);
220}
221
222/* MANUAL */
223void
224semctlname(int cmd)
225{
226	switch (cmd) {
227	case GETNCNT:
228		printf("GETNCNT");
229		break;
230	case GETPID:
231		printf("GETPID");
232		break;
233	case GETVAL:
234		printf("GETVAL");
235		break;
236	case GETALL:
237		printf("GETALL");
238		break;
239	case GETZCNT:
240		printf("GETZCNT");
241		break;
242	case SETVAL:
243		printf("SETVAL");
244		break;
245	case SETALL:
246		printf("SETALL");
247		break;
248	case IPC_RMID:
249		printf("IPC_RMID");
250		break;
251	case IPC_SET:
252		printf("IPC_SET");
253		break;
254	case IPC_STAT:
255		printf("IPC_STAT");
256		break;
257	default: /* Should not reach */
258		printf("<invalid=%d>", cmd);
259	}
260}
261
262/* MANUAL */
263void
264shmctlname(int cmd)
265{
266	switch (cmd) {
267	case IPC_RMID:
268		printf("IPC_RMID");
269		break;
270	case IPC_SET:
271		printf("IPC_SET");
272		break;
273	case IPC_STAT:
274		printf("IPC_STAT");
275		break;
276	default: /* Should not reach */
277		printf("<invalid=%d>", cmd);
278	}
279}
280
281/* MANUAL */
282void
283semgetname(int flag)
284{
285	int or = 0;
286	if_print_or(flag, IPC_CREAT, or);
287	if_print_or(flag, IPC_EXCL, or);
288	if_print_or(flag, SEM_R, or);
289	if_print_or(flag, SEM_A, or);
290	if_print_or(flag, (SEM_R>>3), or);
291	if_print_or(flag, (SEM_A>>3), or);
292	if_print_or(flag, (SEM_R>>6), or);
293	if_print_or(flag, (SEM_A>>6), or);
294}
295
296/*
297 * MANUAL
298 *
299 * Only used by SYS_open. Unless O_CREAT is set in flags, the
300 * mode argument is unused (and often bogus and misleading).
301 */
302void
303flagsandmodename(int flags, int mode, int decimal)
304{
305	flagsname(flags);
306	putchar(',');
307	if ((flags & O_CREAT) == O_CREAT) {
308		modename (mode);
309	} else {
310		if (decimal) {
311			printf("<unused>%d", mode);
312		} else {
313			printf("<unused>%#x", (unsigned int)mode);
314		}
315	}
316}
317
318/* MANUAL */
319void
320idtypename(idtype_t idtype, int decimal)
321{
322	switch(idtype) {
323	case P_PID:
324		printf("P_PID");
325		break;
326	case P_PPID:
327		printf("P_PPID");
328		break;
329	case P_PGID:
330		printf("P_PGID");
331		break;
332	case P_SID:
333		printf("P_SID");
334		break;
335	case P_CID:
336		printf("P_CID");
337		break;
338	case P_UID:
339		printf("P_UID");
340		break;
341	case P_GID:
342		printf("P_GID");
343		break;
344	case P_ALL:
345		printf("P_ALL");
346		break;
347	case P_LWPID:
348		printf("P_LWPID");
349		break;
350	case P_TASKID:
351		printf("P_TASKID");
352		break;
353	case P_PROJID:
354		printf("P_PROJID");
355		break;
356	case P_POOLID:
357		printf("P_POOLID");
358		break;
359	case P_JAILID:
360		printf("P_JAILID");
361		break;
362	case P_CTID:
363		printf("P_CTID");
364		break;
365	case P_CPUID:
366		printf("P_CPUID");
367		break;
368	case P_PSETID:
369		printf("P_PSETID");
370		break;
371	default:
372		if (decimal) {
373			printf("%d", idtype);
374		} else {
375			printf("%#x", idtype);
376		}
377	}
378}
379
380/*
381 * MANUAL
382 *
383 * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value
384 * referring to a line in /etc/protocols . It might be appropriate
385 * to use getprotoent(3) here.
386 */
387void
388sockoptlevelname(int level, int decimal)
389{
390	if (level == SOL_SOCKET) {
391		printf("SOL_SOCKET");
392	} else {
393		if (decimal) {
394			printf("%d", level);
395		} else {
396			printf("%#x", (unsigned int)level);
397		}
398	}
399}
400
401/*
402 * MANUAL
403 *
404 * Used for page fault type.  Cannot use auto_or_type since the macro
405 * values contain a cast.  Also, VM_PROT_NONE has to be handled specially.
406 */
407void
408vmprotname (int type)
409{
410	int	or = 0;
411
412	if (type == VM_PROT_NONE) {
413		(void)printf("VM_PROT_NONE");
414		return;
415	}
416	if_print_or(type, VM_PROT_READ, or);
417	if_print_or(type, VM_PROT_WRITE, or);
418	if_print_or(type, VM_PROT_EXECUTE, or);
419	if_print_or(type, VM_PROT_COPY, or);
420}
421_EOF_
422
423auto_or_type     "accessmodename"      "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+"         "sys/unistd.h"
424auto_switch_type "acltypename"         "ACL_TYPE_[A-Z4_]+[[:space:]]+0x[0-9]+"        "sys/acl.h"
425auto_switch_type "extattrctlname"      "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h"
426auto_switch_type "fadvisebehavname"    "POSIX_FADV_[A-Z]+[[:space:]]+[0-9]+"          "sys/fcntl.h"
427auto_or_type     "flagsname"           "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+"           "sys/fcntl.h"
428auto_or_type     "flockname"           "LOCK_[A-Z]+[[:space:]]+0x[0-9]+"              "sys/fcntl.h"
429auto_or_type     "getfsstatflagsname"  "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*"            "sys/mount.h"
430auto_switch_type "kldsymcmdname"       "KLDSYM_[A-Z]+[[:space:]]+[0-9]+"              "sys/linker.h"
431auto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+"       "sys/linker.h"
432auto_switch_type "lio_listioname"      "LIO_(NO)?WAIT[[:space:]]+[0-9]+"              "aio.h"
433auto_switch_type "madvisebehavname"    "_?MADV_[A-Z]+[[:space:]]+[0-9]+"              "sys/mman.h"
434auto_switch_type "minheritname"        "INHERIT_[A-Z]+[[:space:]]+[0-9]+"             "sys/mman.h"
435auto_or_type     "mlockallname"        "MCL_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/mman.h"
436auto_or_type     "mmapprotname"        "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+"        "sys/mman.h"
437auto_or_type     "modename"            "S_[A-Z]+[[:space:]]+[0-6]{7}"                 "sys/stat.h"
438auto_or_type     "mountflagsname"      "MNT_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/mount.h"
439auto_switch_type "msyncflagsname"      "MS_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/mman.h"
440auto_or_type     "nfssvcname"          "NFSSVC_[A-Z0-9]+[[:space:]]+0x[0-9]+"         "nfs/nfssvc.h"
441auto_switch_type "prioname"            "PRIO_[A-Z]+[[:space:]]+[0-9]"                 "sys/resource.h"
442auto_switch_type "procctlcmdname"      "PROC_[A-Z]+[[:space:]]+[0-9]"                 "sys/procctl.h"
443auto_switch_type "ptraceopname"        "PT_[[:alnum:]_]+[[:space:]]+[0-9]+"           "sys/ptrace.h"
444auto_switch_type "quotactlname"        "Q_[A-Z]+[[:space:]]+0x[0-9]+"                 "ufs/ufs/quota.h"
445auto_or_type     "rebootoptname"       "RB_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/reboot.h"
446auto_or_type     "rforkname"           "RF[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)"       "sys/unistd.h"
447auto_switch_type "rlimitname"          "RLIMIT_[A-Z]+[[:space:]]+[0-9]+"              "sys/resource.h"
448auto_switch_type "schedpolicyname"     "SCHED_[A-Z]+[[:space:]]+[0-9]+"               "sched.h"
449auto_switch_type "sendfileflagsname"   "SF_[A-Z]+[[:space:]]+[0-9]+"                  "sys/socket.h"
450auto_or_type     "shmatname"           "SHM_[A-Z]+[[:space:]]+[0-9]{6}+"              "sys/shm.h"
451auto_switch_type "shutdownhowname"     "SHUT_[A-Z]+[[:space:]]+[0-9]+"                "sys/socket.h"
452auto_switch_type "sigprocmaskhowname"  "SIG_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
453auto_if_type     "sockdomainname"      "PF_[[:alnum:]]+[[:space:]]+"                  "sys/socket.h"
454auto_if_type     "sockfamilyname"      "AF_[[:alnum:]]+[[:space:]]+"                  "sys/socket.h"
455auto_if_type     "sockipprotoname"     "IPPROTO_[[:alnum:]]+[[:space:]]+"             "netinet/in.h"
456auto_switch_type "sockoptname"         "SO_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/socket.h"
457auto_switch_type "socktypename"        "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*"          "sys/socket.h"
458auto_or_type     "thrcreateflagsname"  "THR_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/thr.h"
459auto_switch_type "vmresultname"        "KERN_[A-Z]+[[:space:]]+[0-9]+"                "vm/vm_param.h"
460auto_or_type     "wait6optname"        "W[A-Z]+[[:space:]]+[0-9]+"                    "sys/wait.h"
461auto_switch_type "whencename"          "SEEK_[A-Z]+[[:space:]]+[0-9]+"                "sys/unistd.h"
462
463cat <<_EOF_
464/*
465 * AUTO - Special
466 * F_ is used to specify fcntl commands as well as arguments. Both sets are
467 * grouped in fcntl.h, and this awk script grabs the first group.
468 */
469void
470fcntlcmdname(int cmd, int arg, int decimal)
471{
472	switch (cmd) {
473_EOF_
474egrep "^#[[:space:]]*define[[:space:]]+F_[A-Z0-9_]+[[:space:]]+[0-9]+[[:space:]]*" \
475	$include_dir/sys/fcntl.h | \
476	awk 'BEGIN { o=0 } { for (i = 1; i <= NF; i++) \
477		if ($i ~ /define/) \
478			break; \
479		++i; \
480		if (o <= $(i+1)) \
481			printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i; \
482		else \
483			exit; \
484		o = $(i+1) }'
485cat <<_EOF_
486	default: /* Should not reach */
487		printf("<invalid=%d>", cmd);
488	}
489	putchar(',');
490	if (cmd == F_GETFD || cmd == F_SETFD) {
491		if (arg == FD_CLOEXEC)
492			printf("FD_CLOEXEC");
493		else if (arg == 0)
494			printf("0");
495		else {
496			if (decimal)
497				printf("<invalid>%d", arg);
498			else
499				printf("<invalid>%#x", (unsigned int)arg);
500		}
501	} else if (cmd == F_SETFL) {
502		flagsname(arg);
503	} else {
504		if (decimal)
505			printf("%d", arg);
506		else
507			printf("%#x", (unsigned int)arg);
508	}
509}
510
511/*
512 * AUTO - Special
513 *
514 * The MAP_ALIGNED flag requires special handling.
515 */
516void
517mmapflagsname(int flags)
518{
519	int align;
520	int or = 0;
521	printf("%#x<", flags);
522_EOF_
523egrep "^#[[:space:]]*define[[:space:]]+MAP_[A-Z_]+[[:space:]]+0x[0-9A-Fa-f]+[[:space:]]*" \
524	$include_dir/sys/mman.h | grep -v MAP_ALIGNED | \
525	awk '{ for (i = 1; i <= NF; i++) \
526		if ($i ~ /define/) \
527			break; \
528		++i; \
529		printf "\tif (!((flags > 0) ^ ((%s) > 0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
530cat <<_EOF_
531	align = flags & MAP_ALIGNMENT_MASK;
532	if (align != 0) {
533		if (align == MAP_ALIGNED_SUPER)
534			print_or("MAP_ALIGNED_SUPER", or);
535		else {
536			print_or("MAP_ALIGNED", or);
537			printf("(%d)", align >> MAP_ALIGNMENT_SHIFT);
538		}
539	}
540	printf(">");
541	if (or == 0)
542		printf("<invalid>%d", flags);
543}
544
545/*
546 * AUTO - Special
547 *
548 * The only reason this is not fully automated is due to the
549 * grep -v RTP_PRIO statement. A better egrep line should
550 * make this capable of being a auto_switch_type() function.
551 */
552void
553rtprioname(int func)
554{
555	switch (func) {
556_EOF_
557egrep "^#[[:space:]]*define[[:space:]]+RTP_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" \
558	$include_dir/sys/rtprio.h | grep -v RTP_PRIO | \
559	awk '{ for (i = 1; i <= NF; i++) \
560		if ($i ~ /define/) \
561			break; \
562		++i; \
563		printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i }'
564cat <<_EOF_
565	default: /* Should not reach */
566		printf("<invalid=%d>", func);
567	}
568}
569
570/*
571 * AUTO - Special
572 *
573 * The send and recv functions have a flags argument which can be
574 * set to 0. There is no corresponding #define. The auto_ functions
575 * detect this as "invalid", which is incorrect here.
576 */
577void
578sendrecvflagsname(int flags)
579{
580	int or = 0;
581
582	if (flags == 0) {
583		printf("0");
584		return;
585	}
586
587	printf("%#x<", flags);
588_EOF_
589egrep "^#[[:space:]]*define[[:space:]]+MSG_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \
590	awk '{ for (i = 1; i <= NF; i++) \
591		if ($i ~ /define/) \
592			break; \
593		++i; \
594		printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
595cat <<_EOF_
596	printf(">");
597}
598
599_EOF_
600