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