mksubr revision 175936
1#!/bin/sh 2# 3# $FreeBSD: head/usr.bin/kdump/mksubr 175936 2008-02-03 11:33:27Z des $ 4# 5# Generates kdump_subr.c 6# mkioctls is a special-purpose script, and works fine as it is 7# now, so it remains independent. The idea behind how it generates 8# its list was heavily borrowed here. 9# 10# Some functions here are automatically generated. This can mean 11# the user will see unusual kdump output or errors while building 12# if the underlying .h files are changed significantly. 13# 14# Key: 15# AUTO: Completely auto-generated with either the "or" or the "switch" 16# method. 17# AUTO - Special: Generated automatically, but with some extra commands 18# that the auto_*_type() functions are inappropriate for. 19# MANUAL: Manually entered and must therefore be manually updated. 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_EOF_ 53 egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \ 54 $include_dir/$file | \ 55 awk '{ for (i = 1; i <= NF; i++) \ 56 if ($i ~ /define/) \ 57 break; \ 58 ++i; \ 59 printf "\tif(!((arg>0)^((%s)>0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }' 60cat <<_EOF_ 61 if (or == 0) 62 (void)printf("<invalid>%ld", (long)arg); 63} 64 65_EOF_ 66} 67 68# 69# Automatically generates a C function used when the argument 70# maps to a single, specific #definition 71# 72auto_switch_type () { 73 local name grep file 74 name=$1 75 grep=$2 76 file=$3 77 78 cat <<_EOF_ 79/* AUTO */ 80void 81$name (int arg) 82{ 83 switch (arg) { 84_EOF_ 85 egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \ 86 $include_dir/$file | \ 87 awk '{ for (i = 1; i <= NF; i++) \ 88 if ($i ~ /define/) \ 89 break; \ 90 ++i; \ 91 printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }' 92cat <<_EOF_ 93 default: /* Should not reach */ 94 (void)printf("<invalid=%ld>", (long)arg); 95 } 96} 97 98_EOF_ 99} 100 101# 102# Automatically generates a C function used when the argument 103# maps to a #definition 104# 105auto_if_type () { 106 local name grep file 107 name=$1 108 grep=$2 109 file=$3 110 111 cat <<_EOF_ 112/* AUTO */ 113void 114$name (int arg) 115{ 116_EOF_ 117 egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \ 118 $include_dir/$file | \ 119 awk '{ printf "\t"; \ 120 if (NR > 1) \ 121 printf "else " ; \ 122 printf "if (arg == %s) \n\t\tprintf(\"%s\");\n", $2, $2 }' 123cat <<_EOF_ 124 else /* Should not reach */ 125 (void)printf("<invalid=%ld>", (long)arg); 126} 127 128_EOF_ 129} 130 131# C start 132 133cat <<_EOF_ 134#include <stdio.h> 135#include <sys/fcntl.h> 136#include <sys/stat.h> 137#include <sys/unistd.h> 138#include <sys/mman.h> 139#include <sys/wait.h> 140#define _KERNEL 141#include <sys/socket.h> 142#undef _KERNEL 143#include <netinet/in.h> 144#include <sys/param.h> 145#include <sys/mount.h> 146#include <sys/ptrace.h> 147#include <sys/resource.h> 148#include <sys/reboot.h> 149#include <sched.h> 150#include <sys/linker.h> 151#define _KERNEL 152#include <sys/thr.h> 153#undef _KERNEL 154#include <sys/kse.h> 155#include <sys/extattr.h> 156#include <sys/acl.h> 157#include <aio.h> 158#include <sys/sem.h> 159#include <sys/ipc.h> 160#include <sys/rtprio.h> 161#include <sys/shm.h> 162#include <nfsserver/nfs.h> 163#include <ufs/ufs/quota.h> 164 165#include "kdump_subr.h" 166 167/* 168 * These are simple support macros. print_or utilizes a variable 169 * defined in the calling function to track whether or not it should 170 * print a logical-OR character ('|') before a string. if_print_or 171 * simply handles the necessary "if" statement used in many lines 172 * of this file. 173 */ 174#define print_or(str,orflag) do { \\ 175 if (orflag) putchar('|'); else orflag = 1; \\ 176 printf (str); } \\ 177 while (0) 178#define if_print_or(i,flag,orflag) do { \\ 179 if ((i & flag) == flag) \\ 180 print_or(#flag,orflag); } \\ 181 while (0) 182 183/* MANUAL */ 184extern char *signames[]; /* from kdump.c */ 185void 186signame (int sig) 187{ 188 if (sig > 0 && sig < NSIG) 189 (void)printf("SIG%s",signames[sig]); 190 else 191 (void)printf("SIG %d", sig); 192} 193 194/* MANUAL */ 195void 196semctlname (int cmd) 197{ 198 switch (cmd) { 199 case GETNCNT: 200 (void)printf("GETNCNT"); 201 break; 202 case GETPID: 203 (void)printf("GETPID"); 204 break; 205 case GETVAL: 206 (void)printf("GETVAL"); 207 break; 208 case GETALL: 209 (void)printf("GETALL"); 210 break; 211 case GETZCNT: 212 (void)printf("GETZCNT"); 213 break; 214 case SETVAL: 215 (void)printf("SETVAL"); 216 break; 217 case SETALL: 218 (void)printf("SETALL"); 219 break; 220 case IPC_RMID: 221 (void)printf("IPC_RMID"); 222 break; 223 case IPC_SET: 224 (void)printf("IPC_SET"); 225 break; 226 case IPC_STAT: 227 (void)printf("IPC_STAT"); 228 break; 229 default: /* Should not reach */ 230 (void)printf("<invalid=%ld>", (long)cmd); 231 } 232} 233 234/* MANUAL */ 235void 236shmctlname (int cmd) { 237 switch (cmd) { 238 case IPC_RMID: 239 (void)printf("IPC_RMID"); 240 break; 241 case IPC_SET: 242 (void)printf("IPC_SET"); 243 break; 244 case IPC_STAT: 245 (void)printf("IPC_STAT"); 246 break; 247 default: /* Should not reach */ 248 (void)printf("<invalid=%ld>", (long)cmd); 249 } 250} 251 252/* MANUAL */ 253void 254semgetname (int flag) { 255 int or = 0; 256 if_print_or(flag, SEM_R, or); 257 if_print_or(flag, SEM_A, or); 258 if_print_or(flag, (SEM_R>>3), or); 259 if_print_or(flag, (SEM_A>>3), or); 260 if_print_or(flag, (SEM_R>>6), or); 261 if_print_or(flag, (SEM_A>>6), or); 262} 263 264/* 265 * MANUAL 266 * 267 * Only used by SYS_open. Unless O_CREAT is set in flags, the 268 * mode argument is unused (and often bogus and misleading). 269 */ 270void 271flagsandmodename (int flags, int mode, int decimal) { 272 flagsname (flags); 273 (void)putchar(','); 274 if ((flags & O_CREAT) == O_CREAT) { 275 modename (mode); 276 } else { 277 if (decimal) { 278 (void)printf("<unused>%ld", (long)mode); 279 } else { 280 (void)printf("<unused>%#lx", (long)mode); 281 } 282 } 283} 284 285/* 286 * MANUAL 287 * 288 * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value 289 * referring to a line in /etc/protocols . It might be appropriate 290 * to use getprotoent(3) here. 291 */ 292void 293sockoptlevelname (int level, int decimal) 294{ 295 if (level == SOL_SOCKET) { 296 (void)printf("SOL_SOCKET"); 297 } else { 298 if (decimal) { 299 (void)printf("%ld", (long)level); 300 } else { 301 (void)printf("%#lx", (long)level); 302 } 303 } 304} 305 306_EOF_ 307 308auto_or_type "modename" "S_[A-Z]+[[:space:]]+[0-6]{7}" "sys/stat.h" 309auto_or_type "flagsname" "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h" 310auto_or_type "accessmodename" "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+" "sys/unistd.h" 311auto_or_type "mmapprotname" "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h" 312auto_or_type "mmapflagsname" "MAP_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h" 313auto_or_type "wait4optname" "W[A-Z]+[[:space:]]+[0-9]+" "sys/wait.h" 314auto_or_type "getfsstatflagsname" "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*" "sys/mount.h" 315auto_or_type "mountflagsname" "MNT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mount.h" 316auto_or_type "rebootoptname" "RB_[A-Z]+[[:space:]]+0x[0-9]+" "sys/reboot.h" 317auto_or_type "flockname" "LOCK_[A-Z]+[[:space:]]+0x[0-9]+" "sys/fcntl.h" 318auto_or_type "thrcreateflagsname" "THR_[A-Z]+[[:space:]]+0x[0-9]+" "sys/thr.h" 319auto_or_type "mlockallname" "MCL_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h" 320auto_or_type "shmatname" "SHM_[A-Z]+[[:space:]]+[0-9]{6}+" "sys/shm.h" 321auto_or_type "rforkname" "RF[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)" "sys/unistd.h" 322auto_or_type "nfssvcname" "NFSSVC_[A-Z]+[[:space:]]+0x[0-9]+" "nfsserver/nfs.h" 323 324auto_switch_type "whencename" "SEEK_[A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h" 325auto_switch_type "rlimitname" "RLIMIT_[A-Z]+[[:space:]]+[0-9]+" "sys/resource.h" 326auto_switch_type "shutdownhowname" "SHUT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h" 327auto_switch_type "prioname" "PRIO_[A-Z]+[[:space:]]+[0-9]" "sys/resource.h" 328auto_switch_type "madvisebehavname" "_?MADV_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h" 329auto_switch_type "msyncflagsname" "MS_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h" 330auto_switch_type "schedpolicyname" "SCHED_[A-Z]+[[:space:]]+[0-9]+" "sched.h" 331auto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h" 332auto_switch_type "ksethrcmdname" "KSE_INTR_[A-Z]+[[:space:]]+[0-9]+" "sys/kse.h" 333auto_switch_type "extattrctlname" "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h" 334auto_switch_type "kldsymcmdname" "KLDSYM_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h" 335auto_switch_type "sendfileflagsname" "SF_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h" 336auto_switch_type "acltypename" "ACL_TYPE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/acl.h" 337auto_switch_type "sigprocmaskhowname" "SIG_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h" 338auto_switch_type "lio_listioname" "LIO_(NO)?WAIT[[:space:]]+[0-9]+" "aio.h" 339auto_switch_type "minheritname" "INHERIT_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h" 340auto_switch_type "quotactlname" "Q_[A-Z]+[[:space:]]+0x[0-9]+" "ufs/ufs/quota.h" 341auto_if_type "sockdomainname" "PF_[[:alnum:]]+[[:space:]]+" "sys/socket.h" 342auto_if_type "sockipprotoname" "IPPROTO_[[:alnum:]]+[[:space:]]+" "netinet/in.h" 343auto_switch_type "sockoptname" "SO_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h" 344auto_switch_type "socktypename" "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*" "sys/socket.h" 345auto_switch_type "ptraceopname" "PT_[[:alnum:]]+[[:space:]]+[0-9]+" "sys/ptrace.h" 346 347cat <<_EOF_ 348/* 349 * AUTO - Special 350 * F_ is used to specify fcntl commands as well as arguments. Both sets are 351 * grouped in fcntl.h, and this awk script grabs the first group. 352 */ 353void 354fcntlcmdname (int cmd, int arg, int decimal) 355{ 356 switch (cmd) { 357_EOF_ 358egrep "^#[[:space:]]*define[[:space:]]+F_[A-Z]+[[:space:]]+[0-9]+[[:space:]]*" \ 359 $include_dir/sys/fcntl.h | \ 360 awk 'BEGIN { o=0 } { for (i = 1; i <= NF; i++) \ 361 if ($i ~ /define/) \ 362 break; \ 363 ++i; \ 364 if (o <= $(i+1)) \ 365 printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i; \ 366 else \ 367 exit; \ 368 o = $(i+1) }' 369cat <<_EOF_ 370 default: /* Should not reach */ 371 (void)printf("<invalid=%ld>", (long)cmd); 372 } 373 (void)putchar(','); 374 if (cmd == F_GETFD || cmd == F_SETFD) { 375 if (arg == FD_CLOEXEC) 376 (void)printf("FD_CLOEXEC"); 377 else if (arg == 0) 378 (void)printf("0"); 379 else { 380 if (decimal) 381 (void)printf("<invalid>%ld", (long)arg); 382 else 383 (void)printf("<invalid>%#lx", (long)arg); 384 } 385 } else if (cmd == F_SETFL) { 386 flagsname(arg); 387 } else { 388 if (decimal) 389 (void)printf("%ld", (long)arg); 390 else 391 (void)printf("%#lx", (long)arg); 392 } 393} 394 395/* 396 * AUTO - Special 397 * 398 * The only reason this is not fully automated is due to the 399 * grep -v RTP_PRIO statement. A better egrep line should 400 * make this capable of being a auto_switch_type() function. 401 */ 402void 403rtprioname (int func) 404{ 405 switch (func) { 406_EOF_ 407egrep "^#[[:space:]]*define[[:space:]]+RTP_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" \ 408 $include_dir/sys/rtprio.h | grep -v RTP_PRIO | \ 409 awk '{ for (i = 1; i <= NF; i++) \ 410 if ($i ~ /define/) \ 411 break; \ 412 ++i; \ 413 printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }' 414cat <<_EOF_ 415 default: /* Should not reach */ 416 (void)printf("<invalid=%ld>", (long)func); 417 } 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_EOF_ 437egrep "^#[[:space:]]*define[[:space:]]+MSG_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \ 438 awk '{ for (i = 1; i <= NF; i++) \ 439 if ($i ~ /define/) \ 440 break; \ 441 ++i; \ 442 printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }' 443cat <<_EOF_ 444} 445 446_EOF_ 447