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