1 /*
   2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_RUNTIME_FLAGS_JVMFLAG_HPP
  26 #define SHARE_VM_RUNTIME_FLAGS_JVMFLAG_HPP
  27 
  28 #include "utilities/globalDefinitions.hpp"
  29 #include "utilities/macros.hpp"
  30 
  31 // function type that will construct default range string
  32 typedef const char* (*RangeStrFunc)(void);
  33 
  34 struct JVMFlag {
  35   enum Flags {
  36     // latest value origin
  37     DEFAULT          = 0,
  38     COMMAND_LINE     = 1,
  39     ENVIRON_VAR      = 2,
  40     CONFIG_FILE      = 3,
  41     MANAGEMENT       = 4,
  42     ERGONOMIC        = 5,
  43     ATTACH_ON_DEMAND = 6,
  44     INTERNAL         = 7,
  45 
  46     LAST_VALUE_ORIGIN = INTERNAL,
  47     VALUE_ORIGIN_BITS = 4,
  48     VALUE_ORIGIN_MASK = right_n_bits(VALUE_ORIGIN_BITS),
  49 
  50     // flag kind
  51     KIND_PRODUCT            = 1 << 4,
  52     KIND_MANAGEABLE         = 1 << 5,
  53     KIND_DIAGNOSTIC         = 1 << 6,
  54     KIND_EXPERIMENTAL       = 1 << 7,
  55     KIND_NOT_PRODUCT        = 1 << 8,
  56     KIND_DEVELOP            = 1 << 9,
  57     KIND_PLATFORM_DEPENDENT = 1 << 10,
  58     KIND_READ_WRITE         = 1 << 11,
  59     KIND_C1                 = 1 << 12,
  60     KIND_C2                 = 1 << 13,
  61     KIND_ARCH               = 1 << 14,
  62     KIND_LP64_PRODUCT       = 1 << 15,
  63     KIND_JVMCI              = 1 << 16,
  64 
  65     // set this bit if the flag was set on the command line
  66     ORIG_COMMAND_LINE       = 1 << 17,
  67 
  68     KIND_MASK = ~(VALUE_ORIGIN_MASK | ORIG_COMMAND_LINE)
  69   };
  70 
  71   enum Error {
  72     // no error
  73     SUCCESS = 0,
  74     // flag name is missing
  75     MISSING_NAME,
  76     // flag value is missing
  77     MISSING_VALUE,
  78     // error parsing the textual form of the value
  79     WRONG_FORMAT,
  80     // flag is not writable
  81     NON_WRITABLE,
  82     // flag value is outside of its bounds
  83     OUT_OF_BOUNDS,
  84     // flag value violates its constraint
  85     VIOLATES_CONSTRAINT,
  86     // there is no flag with the given name
  87     INVALID_FLAG,
  88     // the flag can only be set only on command line during invocation of the VM
  89     COMMAND_LINE_ONLY,
  90     // the flag may only be set once
  91     SET_ONLY_ONCE,
  92     // the flag is not writable in this combination of product/debug build
  93     CONSTANT,
  94     // other, unspecified error related to setting the flag
  95     ERR_OTHER
  96   };
  97 
  98   enum MsgType {
  99     NONE = 0,
 100     DIAGNOSTIC_FLAG_BUT_LOCKED,
 101     EXPERIMENTAL_FLAG_BUT_LOCKED,
 102     DEVELOPER_FLAG_BUT_PRODUCT_BUILD,
 103     NOTPRODUCT_FLAG_BUT_PRODUCT_BUILD
 104   };
 105 
 106   const char* _type;
 107   const char* _name;
 108   void* _addr;
 109   NOT_PRODUCT(const char* _doc;)
 110   Flags _flags;
 111   size_t _name_len;
 112 
 113   // points to all Flags static array
 114   static JVMFlag* flags;
 115 
 116   // number of flags
 117   static size_t numFlags;
 118 
 119   static JVMFlag* find_flag(const char* name) { return find_flag(name, strlen(name), true, true); };
 120   static JVMFlag* find_flag(const char* name, size_t length, bool allow_locked = false, bool return_flag = false);
 121   static JVMFlag* fuzzy_match(const char* name, size_t length, bool allow_locked = false);
 122 
 123   static const char* get_int_default_range_str();
 124   static const char* get_uint_default_range_str();
 125   static const char* get_intx_default_range_str();
 126   static const char* get_uintx_default_range_str();
 127   static const char* get_uint64_t_default_range_str();
 128   static const char* get_size_t_default_range_str();
 129   static const char* get_double_default_range_str();
 130 
 131   JVMFlag::Error check_writable(bool changed);
 132 
 133   bool is_bool() const;
 134   bool get_bool() const;
 135   JVMFlag::Error set_bool(bool value);
 136 
 137   bool is_int() const;
 138   int get_int() const;
 139   JVMFlag::Error set_int(int value);
 140 
 141   bool is_uint() const;
 142   uint get_uint() const;
 143   JVMFlag::Error set_uint(uint value);
 144 
 145   bool is_intx() const;
 146   intx get_intx() const;
 147   JVMFlag::Error set_intx(intx value);
 148 
 149   bool is_uintx() const;
 150   uintx get_uintx() const;
 151   JVMFlag::Error set_uintx(uintx value);
 152 
 153   bool is_uint64_t() const;
 154   uint64_t get_uint64_t() const;
 155   JVMFlag::Error set_uint64_t(uint64_t value);
 156 
 157   bool is_size_t() const;
 158   size_t get_size_t() const;
 159   JVMFlag::Error set_size_t(size_t value);
 160 
 161   bool is_double() const;
 162   double get_double() const;
 163   JVMFlag::Error set_double(double value);
 164 
 165   bool is_ccstr() const;
 166   bool ccstr_accumulates() const;
 167   ccstr get_ccstr() const;
 168   JVMFlag::Error set_ccstr(ccstr value);
 169 
 170   Flags get_origin();
 171   void set_origin(Flags origin);
 172 
 173   size_t get_name_length();
 174 
 175   bool is_default();
 176   bool is_ergonomic();
 177   bool is_command_line();
 178   void set_command_line();
 179 
 180   bool is_product() const;
 181   bool is_manageable() const;
 182   bool is_diagnostic() const;
 183   bool is_experimental() const;
 184   bool is_notproduct() const;
 185   bool is_develop() const;
 186   bool is_read_write() const;
 187 
 188   bool is_constant_in_binary() const;
 189 
 190   bool is_unlocker() const;
 191   bool is_unlocked() const;
 192   bool is_writeable() const;
 193   bool is_external() const;
 194 
 195   bool is_unlocker_ext() const;
 196   bool is_unlocked_ext() const;
 197   bool is_writeable_ext() const;
 198   bool is_external_ext() const;
 199 
 200   void clear_diagnostic();
 201 
 202   JVMFlag::MsgType get_locked_message(char*, int) const;
 203   JVMFlag::MsgType get_locked_message_ext(char*, int) const;
 204 
 205   // printRanges will print out flags type, name and range values as expected by -XX:+PrintFlagsRanges
 206   void print_on(outputStream* st, bool withComments = false, bool printRanges = false);
 207   void print_kind(outputStream* st, unsigned int width);
 208   void print_origin(outputStream* st, unsigned int width);
 209   void print_as_flag(outputStream* st);
 210 
 211   static const char* flag_error_str(JVMFlag::Error error);
 212 
 213 public:
 214   static JVMFlag::Error boolAt(const char* name, size_t len, bool* value, bool allow_locked = false, bool return_flag = false);
 215   static JVMFlag::Error boolAt(const char* name, bool* value, bool allow_locked = false, bool return_flag = false)      { return boolAt(name, strlen(name), value, allow_locked, return_flag); }
 216   static JVMFlag::Error boolAtPut(JVMFlag* flag, bool* value, JVMFlag::Flags origin);
 217   static JVMFlag::Error boolAtPut(const char* name, size_t len, bool* value, JVMFlag::Flags origin);
 218   static JVMFlag::Error boolAtPut(const char* name, bool* value, JVMFlag::Flags origin)   { return boolAtPut(name, strlen(name), value, origin); }
 219 
 220   static JVMFlag::Error intAt(const char* name, size_t len, int* value, bool allow_locked = false, bool return_flag = false);
 221   static JVMFlag::Error intAt(const char* name, int* value, bool allow_locked = false, bool return_flag = false)      { return intAt(name, strlen(name), value, allow_locked, return_flag); }
 222   static JVMFlag::Error intAtPut(JVMFlag* flag, int* value, JVMFlag::Flags origin);
 223   static JVMFlag::Error intAtPut(const char* name, size_t len, int* value, JVMFlag::Flags origin);
 224   static JVMFlag::Error intAtPut(const char* name, int* value, JVMFlag::Flags origin)   { return intAtPut(name, strlen(name), value, origin); }
 225 
 226   static JVMFlag::Error uintAt(const char* name, size_t len, uint* value, bool allow_locked = false, bool return_flag = false);
 227   static JVMFlag::Error uintAt(const char* name, uint* value, bool allow_locked = false, bool return_flag = false)      { return uintAt(name, strlen(name), value, allow_locked, return_flag); }
 228   static JVMFlag::Error uintAtPut(JVMFlag* flag, uint* value, JVMFlag::Flags origin);
 229   static JVMFlag::Error uintAtPut(const char* name, size_t len, uint* value, JVMFlag::Flags origin);
 230   static JVMFlag::Error uintAtPut(const char* name, uint* value, JVMFlag::Flags origin)   { return uintAtPut(name, strlen(name), value, origin); }
 231 
 232   static JVMFlag::Error intxAt(const char* name, size_t len, intx* value, bool allow_locked = false, bool return_flag = false);
 233   static JVMFlag::Error intxAt(const char* name, intx* value, bool allow_locked = false, bool return_flag = false)      { return intxAt(name, strlen(name), value, allow_locked, return_flag); }
 234   static JVMFlag::Error intxAtPut(JVMFlag* flag, intx* value, JVMFlag::Flags origin);
 235   static JVMFlag::Error intxAtPut(const char* name, size_t len, intx* value, JVMFlag::Flags origin);
 236   static JVMFlag::Error intxAtPut(const char* name, intx* value, JVMFlag::Flags origin)   { return intxAtPut(name, strlen(name), value, origin); }
 237 
 238   static JVMFlag::Error uintxAt(const char* name, size_t len, uintx* value, bool allow_locked = false, bool return_flag = false);
 239   static JVMFlag::Error uintxAt(const char* name, uintx* value, bool allow_locked = false, bool return_flag = false)    { return uintxAt(name, strlen(name), value, allow_locked, return_flag); }
 240   static JVMFlag::Error uintxAtPut(JVMFlag* flag, uintx* value, JVMFlag::Flags origin);
 241   static JVMFlag::Error uintxAtPut(const char* name, size_t len, uintx* value, JVMFlag::Flags origin);
 242   static JVMFlag::Error uintxAtPut(const char* name, uintx* value, JVMFlag::Flags origin) { return uintxAtPut(name, strlen(name), value, origin); }
 243 
 244   static JVMFlag::Error size_tAt(const char* name, size_t len, size_t* value, bool allow_locked = false, bool return_flag = false);
 245   static JVMFlag::Error size_tAt(const char* name, size_t* value, bool allow_locked = false, bool return_flag = false)    { return size_tAt(name, strlen(name), value, allow_locked, return_flag); }
 246   static JVMFlag::Error size_tAtPut(JVMFlag* flag, size_t* value, JVMFlag::Flags origin);
 247   static JVMFlag::Error size_tAtPut(const char* name, size_t len, size_t* value, JVMFlag::Flags origin);
 248   static JVMFlag::Error size_tAtPut(const char* name, size_t* value, JVMFlag::Flags origin) { return size_tAtPut(name, strlen(name), value, origin); }
 249 
 250   static JVMFlag::Error uint64_tAt(const char* name, size_t len, uint64_t* value, bool allow_locked = false, bool return_flag = false);
 251   static JVMFlag::Error uint64_tAt(const char* name, uint64_t* value, bool allow_locked = false, bool return_flag = false) { return uint64_tAt(name, strlen(name), value, allow_locked, return_flag); }
 252   static JVMFlag::Error uint64_tAtPut(JVMFlag* flag, uint64_t* value, JVMFlag::Flags origin);
 253   static JVMFlag::Error uint64_tAtPut(const char* name, size_t len, uint64_t* value, JVMFlag::Flags origin);
 254   static JVMFlag::Error uint64_tAtPut(const char* name, uint64_t* value, JVMFlag::Flags origin) { return uint64_tAtPut(name, strlen(name), value, origin); }
 255 
 256   static JVMFlag::Error doubleAt(const char* name, size_t len, double* value, bool allow_locked = false, bool return_flag = false);
 257   static JVMFlag::Error doubleAt(const char* name, double* value, bool allow_locked = false, bool return_flag = false)    { return doubleAt(name, strlen(name), value, allow_locked, return_flag); }
 258   static JVMFlag::Error doubleAtPut(JVMFlag* flag, double* value, JVMFlag::Flags origin);
 259   static JVMFlag::Error doubleAtPut(const char* name, size_t len, double* value, JVMFlag::Flags origin);
 260   static JVMFlag::Error doubleAtPut(const char* name, double* value, JVMFlag::Flags origin) { return doubleAtPut(name, strlen(name), value, origin); }
 261 
 262   static JVMFlag::Error ccstrAt(const char* name, size_t len, ccstr* value, bool allow_locked = false, bool return_flag = false);
 263   static JVMFlag::Error ccstrAt(const char* name, ccstr* value, bool allow_locked = false, bool return_flag = false)    { return ccstrAt(name, strlen(name), value, allow_locked, return_flag); }
 264   // Contract:  JVMFlag will make private copy of the incoming value.
 265   // Outgoing value is always malloc-ed, and caller MUST call free.
 266   static JVMFlag::Error ccstrAtPut(const char* name, size_t len, ccstr* value, JVMFlag::Flags origin);
 267   static JVMFlag::Error ccstrAtPut(const char* name, ccstr* value, JVMFlag::Flags origin) { return ccstrAtPut(name, strlen(name), value, origin); }
 268 
 269   // Returns false if name is not a command line flag.
 270   static bool wasSetOnCmdline(const char* name, bool* value);
 271   static void printSetFlags(outputStream* out);
 272 
 273   // printRanges will print out flags type, name and range values as expected by -XX:+PrintFlagsRanges
 274   static void printFlags(outputStream* out, bool withComments, bool printRanges = false, bool skipDefaults = false);
 275   static void printError(bool verbose, const char* msg, ...);
 276 
 277   static void verify() PRODUCT_RETURN;
 278 };
 279 
 280 #endif // SHARE_VM_RUNTIME_FLAGS_JVMFLAG_HPP