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