mksubr revision 213479
1#!/bin/sh 2# 3# $FreeBSD: head/usr.bin/kdump/mksubr 213479 2010-10-06 12:41:42Z netchild $ 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 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 (void)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 76 name=$1 77 grep=$2 78 file=$3 79 80 cat <<_EOF_ 81/* AUTO */ 82void 83$name (int arg) 84{ 85 switch (arg) { 86_EOF_ 87 egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \ 88 $include_dir/$file | \ 89 awk '{ for (i = 1; i <= NF; i++) \ 90 if ($i ~ /define/) \ 91 break; \ 92 ++i; \ 93 printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }' 94cat <<_EOF_ 95 default: /* Should not reach */ 96 (void)printf("<invalid=%ld>", (long)arg); 97 } 98} 99 100_EOF_ 101} 102 103# 104# Automatically generates a C function used when the argument 105# maps to a #definition 106# 107auto_if_type () { 108 local name grep file 109 name=$1 110 grep=$2 111 file=$3 112 113 cat <<_EOF_ 114/* AUTO */ 115void 116$name (int arg) 117{ 118_EOF_ 119 egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \ 120 $include_dir/$file | \ 121 awk '{ printf "\t"; \ 122 if (NR > 1) \ 123 printf "else " ; \ 124 printf "if (arg == %s) \n\t\tprintf(\"%s\");\n", $2, $2 }' 125cat <<_EOF_ 126 else /* Should not reach */ 127 (void)printf("<invalid=%ld>", (long)arg); 128} 129 130_EOF_ 131} 132 133# C start 134 135cat <<_EOF_ 136#include <stdio.h> 137#include <sys/fcntl.h> 138#include <sys/stat.h> 139#include <sys/unistd.h> 140#include <sys/mman.h> 141#include <sys/wait.h> 142#define _KERNEL 143#include <sys/socket.h> 144#undef _KERNEL 145#include <netinet/in.h> 146#include <sys/param.h> 147#include <sys/mount.h> 148#include <sys/ptrace.h> 149#include <sys/resource.h> 150#include <sys/reboot.h> 151#include <sched.h> 152#include <sys/linker.h> 153#define _KERNEL 154#include <sys/thr.h> 155#undef _KERNEL 156#include <sys/extattr.h> 157#include <sys/acl.h> 158#include <aio.h> 159#include <sys/sem.h> 160#include <sys/ipc.h> 161#include <sys/rtprio.h> 162#include <sys/shm.h> 163#include <nfsserver/nfs.h> 164#include <ufs/ufs/quota.h> 165 166#include "kdump_subr.h" 167 168/* 169 * These are simple support macros. print_or utilizes a variable 170 * defined in the calling function to track whether or not it should 171 * print a logical-OR character ('|') before a string. if_print_or 172 * simply handles the necessary "if" statement used in many lines 173 * of this file. 174 */ 175#define print_or(str,orflag) do { \\ 176 if (orflag) putchar('|'); else orflag = 1; \\ 177 printf (str); } \\ 178 while (0) 179#define if_print_or(i,flag,orflag) do { \\ 180 if ((i & flag) == flag) \\ 181 print_or(#flag,orflag); } \\ 182 while (0) 183 184/* MANUAL */ 185extern char *signames[]; /* from kdump.c */ 186void 187signame (int sig) 188{ 189 if (sig > 0 && sig < NSIG) 190 (void)printf("SIG%s",signames[sig]); 191 else 192 (void)printf("SIG %d", sig); 193} 194 195/* MANUAL */ 196void 197semctlname (int cmd) 198{ 199 switch (cmd) { 200 case GETNCNT: 201 (void)printf("GETNCNT"); 202 break; 203 case GETPID: 204 (void)printf("GETPID"); 205 break; 206 case GETVAL: 207 (void)printf("GETVAL"); 208 break; 209 case GETALL: 210 (void)printf("GETALL"); 211 break; 212 case GETZCNT: 213 (void)printf("GETZCNT"); 214 break; 215 case SETVAL: 216 (void)printf("SETVAL"); 217 break; 218 case SETALL: 219 (void)printf("SETALL"); 220 break; 221 case IPC_RMID: 222 (void)printf("IPC_RMID"); 223 break; 224 case IPC_SET: 225 (void)printf("IPC_SET"); 226 break; 227 case IPC_STAT: 228 (void)printf("IPC_STAT"); 229 break; 230 default: /* Should not reach */ 231 (void)printf("<invalid=%ld>", (long)cmd); 232 } 233} 234 235/* MANUAL */ 236void 237shmctlname (int cmd) { 238 switch (cmd) { 239 case IPC_RMID: 240 (void)printf("IPC_RMID"); 241 break; 242 case IPC_SET: 243 (void)printf("IPC_SET"); 244 break; 245 case IPC_STAT: 246 (void)printf("IPC_STAT"); 247 break; 248 default: /* Should not reach */ 249 (void)printf("<invalid=%ld>", (long)cmd); 250 } 251} 252 253/* MANUAL */ 254void 255semgetname (int flag) { 256 int or = 0; 257 if_print_or(flag, SEM_R, or); 258 if_print_or(flag, SEM_A, or); 259 if_print_or(flag, (SEM_R>>3), or); 260 if_print_or(flag, (SEM_A>>3), or); 261 if_print_or(flag, (SEM_R>>6), or); 262 if_print_or(flag, (SEM_A>>6), or); 263} 264 265/* 266 * MANUAL 267 * 268 * Only used by SYS_open. Unless O_CREAT is set in flags, the 269 * mode argument is unused (and often bogus and misleading). 270 */ 271void 272flagsandmodename (int flags, int mode, int decimal) { 273 flagsname (flags); 274 (void)putchar(','); 275 if ((flags & O_CREAT) == O_CREAT) { 276 modename (mode); 277 } else { 278 if (decimal) { 279 (void)printf("<unused>%ld", (long)mode); 280 } else { 281 (void)printf("<unused>%#lx", (long)mode); 282 } 283 } 284} 285 286/* 287 * MANUAL 288 * 289 * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value 290 * referring to a line in /etc/protocols . It might be appropriate 291 * to use getprotoent(3) here. 292 */ 293void 294sockoptlevelname (int level, int decimal) 295{ 296 if (level == SOL_SOCKET) { 297 (void)printf("SOL_SOCKET"); 298 } else { 299 if (decimal) { 300 (void)printf("%ld", (long)level); 301 } else { 302 (void)printf("%#lx", (long)level); 303 } 304 } 305} 306 307_EOF_ 308 309auto_or_type "modename" "S_[A-Z]+[[:space:]]+[0-6]{7}" "sys/stat.h" 310auto_or_type "flagsname" "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h" 311auto_or_type "accessmodename" "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+" "sys/unistd.h" 312auto_or_type "mmapprotname" "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h" 313auto_or_type "mmapflagsname" "MAP_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h" 314auto_or_type "wait4optname" "W[A-Z]+[[:space:]]+[0-9]+" "sys/wait.h" 315auto_or_type "getfsstatflagsname" "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*" "sys/mount.h" 316auto_or_type "mountflagsname" "MNT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mount.h" 317auto_or_type "rebootoptname" "RB_[A-Z]+[[:space:]]+0x[0-9]+" "sys/reboot.h" 318auto_or_type "flockname" "LOCK_[A-Z]+[[:space:]]+0x[0-9]+" "sys/fcntl.h" 319auto_or_type "thrcreateflagsname" "THR_[A-Z]+[[:space:]]+0x[0-9]+" "sys/thr.h" 320auto_or_type "mlockallname" "MCL_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h" 321auto_or_type "shmatname" "SHM_[A-Z]+[[:space:]]+[0-9]{6}+" "sys/shm.h" 322auto_or_type "rforkname" "RF[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)" "sys/unistd.h" 323auto_or_type "nfssvcname" "NFSSVC_[A-Z]+[[:space:]]+0x[0-9]+" "nfsserver/nfs.h" 324 325auto_switch_type "whencename" "SEEK_[A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h" 326auto_switch_type "rlimitname" "RLIMIT_[A-Z]+[[:space:]]+[0-9]+" "sys/resource.h" 327auto_switch_type "shutdownhowname" "SHUT_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h" 328auto_switch_type "prioname" "PRIO_[A-Z]+[[:space:]]+[0-9]" "sys/resource.h" 329auto_switch_type "madvisebehavname" "_?MADV_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h" 330auto_switch_type "msyncflagsname" "MS_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h" 331auto_switch_type "schedpolicyname" "SCHED_[A-Z]+[[:space:]]+[0-9]+" "sched.h" 332auto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.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-Z4_]+[[: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 "sockfamilyname" "AF_[[:alnum:]]+[[:space:]]+" "sys/socket.h" 343auto_if_type "sockipprotoname" "IPPROTO_[[:alnum:]]+[[:space:]]+" "netinet/in.h" 344auto_switch_type "sockoptname" "SO_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h" 345auto_switch_type "socktypename" "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*" "sys/socket.h" 346auto_switch_type "ptraceopname" "PT_[[:alnum:]]+[[:space:]]+[0-9]+" "sys/ptrace.h" 347 348cat <<_EOF_ 349/* 350 * AUTO - Special 351 * F_ is used to specify fcntl commands as well as arguments. Both sets are 352 * grouped in fcntl.h, and this awk script grabs the first group. 353 */ 354void 355fcntlcmdname (int cmd, int arg, int decimal) 356{ 357 switch (cmd) { 358_EOF_ 359egrep "^#[[:space:]]*define[[:space:]]+F_[A-Z]+[[:space:]]+[0-9]+[[:space:]]*" \ 360 $include_dir/sys/fcntl.h | \ 361 awk 'BEGIN { o=0 } { for (i = 1; i <= NF; i++) \ 362 if ($i ~ /define/) \ 363 break; \ 364 ++i; \ 365 if (o <= $(i+1)) \ 366 printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i; \ 367 else \ 368 exit; \ 369 o = $(i+1) }' 370cat <<_EOF_ 371 default: /* Should not reach */ 372 (void)printf("<invalid=%ld>", (long)cmd); 373 } 374 (void)putchar(','); 375 if (cmd == F_GETFD || cmd == F_SETFD) { 376 if (arg == FD_CLOEXEC) 377 (void)printf("FD_CLOEXEC"); 378 else if (arg == 0) 379 (void)printf("0"); 380 else { 381 if (decimal) 382 (void)printf("<invalid>%ld", (long)arg); 383 else 384 (void)printf("<invalid>%#lx", (long)arg); 385 } 386 } else if (cmd == F_SETFL) { 387 flagsname(arg); 388 } else { 389 if (decimal) 390 (void)printf("%ld", (long)arg); 391 else 392 (void)printf("%#lx", (long)arg); 393 } 394} 395 396/* 397 * AUTO - Special 398 * 399 * The only reason this is not fully automated is due to the 400 * grep -v RTP_PRIO statement. A better egrep line should 401 * make this capable of being a auto_switch_type() function. 402 */ 403void 404rtprioname (int func) 405{ 406 switch (func) { 407_EOF_ 408egrep "^#[[:space:]]*define[[:space:]]+RTP_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" \ 409 $include_dir/sys/rtprio.h | grep -v RTP_PRIO | \ 410 awk '{ for (i = 1; i <= NF; i++) \ 411 if ($i ~ /define/) \ 412 break; \ 413 ++i; \ 414 printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }' 415cat <<_EOF_ 416 default: /* Should not reach */ 417 (void)printf("<invalid=%ld>", (long)func); 418 } 419} 420 421/* 422 * AUTO - Special 423 * 424 * The send and recv functions have a flags argument which can be 425 * set to 0. There is no corresponding #define. The auto_ functions 426 * detect this as "invalid", which is incorrect here. 427 */ 428void 429sendrecvflagsname (int flags) 430{ 431 int or = 0; 432 433 if (flags == 0) { 434 (void)printf("0"); 435 return; 436 } 437 438 printf("%#x<", flags); 439_EOF_ 440egrep "^#[[:space:]]*define[[:space:]]+MSG_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \ 441 awk '{ for (i = 1; i <= NF; i++) \ 442 if ($i ~ /define/) \ 443 break; \ 444 ++i; \ 445 printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }' 446cat <<_EOF_ 447 printf(">"); 448} 449 450_EOF_ 451