mksubr revision 1.9
1#!/bin/sh 2# $OpenBSD: mksubr,v 1.9 2012/03/19 09:05:39 guenther 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# 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 61 cat <<_EOF_ 62/* AUTO */ 63void 64$name (int arg) 65{ 66 int or = 0; 67 printf("%#x<", arg); 68_EOF_ 69 egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \ 70 $include_dir/$file | \ 71 awk '{ for (i = 1; i <= NF; i++) \ 72 if ($i ~ /define/) \ 73 break; \ 74 ++i; \ 75 printf "\tif(!((arg>0)^((%s)>0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }' 76cat <<_EOF_ 77 printf(">"); 78 if (or == 0) 79 (void)printf("<invalid>%ld", (long)arg); 80} 81 82_EOF_ 83} 84 85# 86# Like auto_or_type(), but a zero value is valid and prints as "0<>" 87# 88auto_orz_type () { 89 local name grep file 90 name=$1 91 grep=$2 92 file=$3 93 94 cat <<_EOF_ 95/* AUTO */ 96void 97$name (int arg) 98{ 99 int or = 0; 100 if (arg == 0) { 101 printf("0<>"); 102 return; 103 } 104 printf("%#x<", arg); 105_EOF_ 106 egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \ 107 $include_dir/$file | \ 108 awk '{ for (i = 1; i <= NF; i++) \ 109 if ($i ~ /define/) \ 110 break; \ 111 ++i; \ 112 printf "\tif(!((arg>0)^((%s)>0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }' 113cat <<_EOF_ 114 printf(">"); 115 if (or == 0) 116 (void)printf("<invalid>%ld", (long)arg); 117} 118 119_EOF_ 120} 121 122# 123# Automatically generates a C function used when the argument 124# maps to a single, specific #definition 125# 126auto_switch_type () { 127 local name grep file 128 name=$1 129 grep=$2 130 file=$3 131 132 cat <<_EOF_ 133/* AUTO */ 134void 135$name (int arg) 136{ 137 switch (arg) { 138_EOF_ 139 egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \ 140 $include_dir/$file | \ 141 awk '{ for (i = 1; i <= NF; i++) \ 142 if ($i ~ /define/) \ 143 break; \ 144 ++i; \ 145 printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }' 146cat <<_EOF_ 147 default: /* Should not reach */ 148 (void)printf("<invalid=%ld>", (long)arg); 149 } 150} 151 152_EOF_ 153} 154 155# 156# Automatically generates a C function used when the argument 157# maps to a #definition 158# 159auto_if_type () { 160 local name grep file 161 name=$1 162 grep=$2 163 file=$3 164 165 cat <<_EOF_ 166/* AUTO */ 167void 168$name (int arg) 169{ 170_EOF_ 171 egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \ 172 $include_dir/$file | \ 173 awk '{ printf "\t"; \ 174 if (NR > 1) \ 175 printf "else " ; \ 176 printf "if (arg == %s) \n\t\tprintf(\"%s\");\n", $2, $2 }' 177cat <<_EOF_ 178 else /* Should not reach */ 179 (void)printf("<invalid=%ld>", (long)arg); 180} 181 182_EOF_ 183} 184 185# C start 186 187cat <<_EOF_ 188#include <stdio.h> 189#include <sys/param.h> 190#include <sys/fcntl.h> 191#include <sys/stat.h> 192#include <sys/unistd.h> 193#include <sys/mman.h> 194#include <sys/wait.h> 195#include <sys/proc.h> 196#define _KERNEL 197#define COMPAT_43 198#include <sys/socket.h> 199#undef _KERNEL 200#include <netinet/in.h> 201#include <sys/param.h> 202#include <sys/mount.h> 203#include <sys/ptrace.h> 204#include <sys/resource.h> 205#include <sys/reboot.h> 206#include <sched.h> 207#if 0 208#include <sys/linker.h> 209#define _KERNEL 210#include <sys/thr.h> 211#undef _KERNEL 212#include <sys/extattr.h> 213#include <sys/acl.h> 214#include <aio.h> 215#endif 216#include <sys/sem.h> 217#include <sys/ipc.h> 218#if 0 219#include <sys/rtprio.h> 220#endif 221#include <sys/shm.h> 222#if 0 223#include <nfsserver/nfs.h> 224#endif 225#include <ufs/ufs/quota.h> 226 227#include "kdump_subr.h" 228 229/* 230 * These are simple support macros. print_or utilizes a variable 231 * defined in the calling function to track whether or not it should 232 * print a logical-OR character ('|') before a string. if_print_or 233 * simply handles the necessary "if" statement used in many lines 234 * of this file. 235 */ 236#define print_or(str,orflag) do { \\ 237 if (orflag) putchar('|'); else orflag = 1; \\ 238 printf ("%s", str); } \\ 239 while (0) 240#define if_print_or(i,flag,orflag) do { \\ 241 if ((i & flag) == flag) \\ 242 print_or(#flag,orflag); } \\ 243 while (0) 244 245/* MANUAL */ 246extern const char *const sys_signame[NSIG]; 247void 248signame (int sig) 249{ 250 if (sig > 0 && sig < NSIG) 251 (void)printf("SIG%s", sys_signame[sig]); 252 else 253 (void)printf("SIG %d", sig); 254} 255 256/* MANUAL */ 257void 258sigset (int ss) 259{ 260 int or = 0; 261 int cnt = 0; 262 int i; 263 264 for (i = 1; i < NSIG; i++) 265 if (sigismember(&ss, i)) 266 cnt++; 267 if (cnt > (NSIG-1)/2) { 268 ss = ~ss; 269 putchar('~'); 270 } 271 272 if (ss == 0) { 273 (void)printf("0<>"); 274 return; 275 } 276 277 printf("%#x<", ss); 278 for (i = 1; i < NSIG; i++) 279 if (sigismember(&ss, i)) { 280 if (or) putchar('|'); else or=1; 281 signame(i); 282 } 283 printf(">"); 284} 285 286/* MANUAL */ 287void 288semctlname (int cmd) 289{ 290 switch (cmd) { 291 case GETNCNT: 292 (void)printf("GETNCNT"); 293 break; 294 case GETPID: 295 (void)printf("GETPID"); 296 break; 297 case GETVAL: 298 (void)printf("GETVAL"); 299 break; 300 case GETALL: 301 (void)printf("GETALL"); 302 break; 303 case GETZCNT: 304 (void)printf("GETZCNT"); 305 break; 306 case SETVAL: 307 (void)printf("SETVAL"); 308 break; 309 case SETALL: 310 (void)printf("SETALL"); 311 break; 312 case IPC_RMID: 313 (void)printf("IPC_RMID"); 314 break; 315 case IPC_SET: 316 (void)printf("IPC_SET"); 317 break; 318 case IPC_STAT: 319 (void)printf("IPC_STAT"); 320 break; 321 default: /* Should not reach */ 322 (void)printf("<invalid=%ld>", (long)cmd); 323 } 324} 325 326/* MANUAL */ 327void 328shmctlname (int cmd) { 329 switch (cmd) { 330 case IPC_RMID: 331 (void)printf("IPC_RMID"); 332 break; 333 case IPC_SET: 334 (void)printf("IPC_SET"); 335 break; 336 case IPC_STAT: 337 (void)printf("IPC_STAT"); 338 break; 339 default: /* Should not reach */ 340 (void)printf("<invalid=%ld>", (long)cmd); 341 } 342} 343 344/* MANUAL */ 345void 346semgetname (int flag) { 347 int or = 0; 348 if_print_or(flag, IPC_CREAT, or); 349 if_print_or(flag, IPC_EXCL, or); 350 if_print_or(flag, SEM_R, or); 351 if_print_or(flag, SEM_A, or); 352 if_print_or(flag, (SEM_R>>3), or); 353 if_print_or(flag, (SEM_A>>3), or); 354 if_print_or(flag, (SEM_R>>6), or); 355 if_print_or(flag, (SEM_A>>6), or); 356} 357 358/* 359 * MANUAL 360 * 361 * Only used by SYS_open. Unless O_CREAT is set in flags, the 362 * mode argument is unused (and often bogus and misleading). 363 */ 364void 365flagsandmodename (int flags, int mode) { 366 flagsname (flags); 367 (void)putchar(','); 368 if ((flags & O_CREAT) == O_CREAT) { 369 modename (mode); 370 } else { 371 if (decimal) { 372 (void)printf("<unused>%ld", (long)mode); 373 } else { 374 (void)printf("<unused>%#lx", (long)mode); 375 } 376 } 377} 378 379/* 380 * MANUAL 381 * 382 * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value 383 * referring to a line in /etc/protocols . It might be appropriate 384 * to use getprotoent(3) here. 385 */ 386void 387sockoptlevelname (int level) 388{ 389 if (level == SOL_SOCKET) { 390 (void)printf("SOL_SOCKET"); 391 } else { 392 if (decimal) { 393 (void)printf("%ld", (long)level); 394 } else { 395 (void)printf("%#lx", (long)level); 396 } 397 } 398} 399 400_EOF_ 401 402auto_orz_type "modename" "S_[A-Z]+[[:space:]]+[0-6]{7}" "sys/stat.h" 403auto_or_type "flagsname" "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h" 404auto_orz_type "atflagsname" "AT_[A-Z_]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h" 405auto_or_type "accessmodename" "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+" "sys/unistd.h" 406auto_or_type "mmapprotname" "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h" 407auto_or_type "mmapflagsname" "MAP_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h" 408auto_orz_type "wait4optname" "W[A-Z]+[[:space:]]+[0-9]+" "sys/wait.h" 409#auto_or_type "timerflagsname" "TIMER_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/time.h" 410#auto_or_type "getfsstatflagsname" "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*" "sys/mount.h" 411#auto_or_type "mountflagsname" "MNT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mount.h" 412#auto_or_type "rebootoptname" "RB_[A-Z]+[[:space:]]+0x[0-9]+" "sys/reboot.h" 413auto_or_type "flockname" "LOCK_[A-Z]+[[:space:]]+0x[0-9]+" "sys/fcntl.h" 414#auto_or_type "thrcreateflagsname" "THR_[A-Z]+[[:space:]]+0x[0-9]+" "sys/thr.h" 415auto_or_type "mlockallname" "MCL_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h" 416auto_orz_type "shmatname" "SHM_[A-Z]+[[:space:]]+[0-9]{6}" "sys/shm.h" 417#auto_or_type "rforkname" "RF[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)" "sys/unistd.h" 418#auto_or_type "nfssvcname" "NFSSVC_[A-Z]+[[:space:]]+0x[0-9]+" "nfsserver/nfs.h" 419# 420auto_switch_type "whencename" "SEEK_[A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h" 421auto_switch_type "rlimitname" "RLIMIT_[A-Z]+[[:space:]]+[0-9]+" "sys/resource.h" 422#auto_switch_type "shutdownhowname" "SHUT_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h" 423#auto_switch_type "prioname" "PRIO_[A-Z]+[[:space:]]+[0-9]" "sys/resource.h" 424auto_switch_type "madvisebehavname" "_?MADV_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h" 425auto_switch_type "msyncflagsname" "MS_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h" 426auto_switch_type "clockname" "CLOCK_[A-Z]+[[:space:]]+[0-9]+" "sys/time.h" 427#auto_switch_type "schedpolicyname" "SCHED_[A-Z]+[[:space:]]+[0-9]+" "sched.h" 428#auto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h" 429#auto_switch_type "extattrctlname" "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h" 430#auto_switch_type "kldsymcmdname" "KLDSYM_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h" 431#auto_switch_type "sendfileflagsname" "SF_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h" 432#auto_switch_type "acltypename" "ACL_TYPE_[A-Z4_]+[[:space:]]+0x[0-9]+" "sys/acl.h" 433auto_orz_type "sigactionflagname" "SA_[A-Z]+[[:space:]]+0x[0-9]+" "sys/signal.h" 434auto_switch_type "sigprocmaskhowname" "SIG_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h" 435auto_switch_type "sigill_name" "ILL_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h" 436auto_switch_type "sigtrap_name" "TRAP_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h" 437auto_switch_type "sigemt_name" "EMT_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h" 438auto_switch_type "sigfpe_name" "FPE_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h" 439auto_switch_type "sigbus_name" "BUS_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h" 440auto_switch_type "sigsegv_name" "SEGV_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h" 441auto_switch_type "sigchld_name" "CLD_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h" 442#auto_switch_type "lio_listioname" "LIO_(NO)?WAIT[[:space:]]+[0-9]+" "aio.h" 443auto_switch_type "minheritname" "INHERIT_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h" 444#auto_switch_type "quotactlname" "Q_[A-Z]+[[:space:]]+0x[0-9]+" "ufs/ufs/quota.h" 445auto_if_type "sockdomainname" "PF_[[:alnum:]]+[[:space:]]+" "sys/socket.h" 446auto_if_type "sockfamilyname" "AF_[[:alnum:]]+[[:space:]]+" "sys/socket.h" 447auto_if_type "sockipprotoname" "IPPROTO_[[:alnum:]]+[[:space:]]+" "netinet/in.h" 448auto_switch_type "sockoptname" "SO_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h" 449auto_switch_type "socktypename" "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*" "sys/socket.h" 450#auto_switch_type "ptraceopname" "PT_[[:alnum:]_]+[[:space:]]+[0-9]+" "sys/ptrace.h" 451 452cat <<_EOF_ 453/* 454 * AUTO - Special 455 * F_ is used to specify fcntl commands as well as arguments. Both sets are 456 * grouped in fcntl.h, and this awk script grabs the first group. 457 */ 458void 459fcntlcmdname (int cmd, int arg) 460{ 461 switch (cmd) { 462_EOF_ 463egrep "^#[[:space:]]*define[[:space:]]+F_[A-Z_]+[[:space:]]+[0-9]+[[:space:]]*" \ 464 $include_dir/sys/fcntl.h | \ 465 awk 'BEGIN { o=0 } { for (i = 1; i <= NF; i++) \ 466 if ($i ~ /define/) \ 467 break; \ 468 ++i; \ 469 if (o <= $(i+1)) \ 470 printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i; \ 471 else \ 472 exit; \ 473 o = $(i+1) }' 474cat <<_EOF_ 475 default: /* Should not reach */ 476 (void)printf("<invalid=%ld>", (long)cmd); 477 } 478 (void)putchar(','); 479 if (cmd == F_GETFD || cmd == F_SETFD) { 480 if (arg == FD_CLOEXEC) 481 (void)printf("FD_CLOEXEC"); 482 else if (arg == 0) 483 (void)printf("0"); 484 else { 485 if (decimal) 486 (void)printf("<invalid>%ld", (long)arg); 487 else 488 (void)printf("<invalid>%#lx", (long)arg); 489 } 490 } else if (cmd == F_SETFL) { 491 flagsname(arg); 492 } else { 493 if (decimal) 494 (void)printf("%ld", (long)arg); 495 else 496 (void)printf("%#lx", (long)arg); 497 } 498} 499 500/* 501 * AUTO - Special 502 * 503 * The send and recv functions have a flags argument which can be 504 * set to 0. There is no corresponding #define. The auto_ functions 505 * detect this as "invalid", which is incorrect here. 506 */ 507void 508sendrecvflagsname (int flags) 509{ 510 int or = 0; 511 512 if (flags == 0) { 513 (void)printf("0"); 514 return; 515 } 516 517 printf("%#x<", flags); 518_EOF_ 519egrep "^#[[:space:]]*define[[:space:]]+MSG_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \ 520 awk '{ for (i = 1; i <= NF; i++) \ 521 if ($i ~ /define/) \ 522 break; \ 523 ++i; \ 524 printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }' 525cat <<_EOF_ 526 printf(">"); 527} 528 529_EOF_ 530