1 /*
   2  * Copyright (c) 1997, 2014, 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 #include "precompiled.hpp"
  26 #include "memory/allocation.inline.hpp"
  27 #include "oops/oop.inline.hpp"
  28 #include "runtime/arguments.hpp"
  29 #include "runtime/globals.hpp"
  30 #include "runtime/globals_extension.hpp"
  31 #include "utilities/ostream.hpp"
  32 #include "utilities/macros.hpp"
  33 #include "utilities/top.hpp"
  34 #include "trace/tracing.hpp"
  35 #if INCLUDE_ALL_GCS
  36 #include "gc_implementation/g1/g1_globals.hpp"
  37 #endif // INCLUDE_ALL_GCS
  38 #ifdef COMPILER1
  39 #include "c1/c1_globals.hpp"
  40 #endif
  41 #ifdef COMPILER2
  42 #include "opto/c2_globals.hpp"
  43 #endif
  44 #ifdef SHARK
  45 #include "shark/shark_globals.hpp"
  46 #endif
  47 
  48 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  49 
  50 RUNTIME_FLAGS(MATERIALIZE_DEVELOPER_FLAG, MATERIALIZE_PD_DEVELOPER_FLAG, \
  51               MATERIALIZE_PRODUCT_FLAG, MATERIALIZE_PD_PRODUCT_FLAG, \
  52               MATERIALIZE_DIAGNOSTIC_FLAG, MATERIALIZE_EXPERIMENTAL_FLAG, \
  53               MATERIALIZE_NOTPRODUCT_FLAG, \
  54               MATERIALIZE_MANAGEABLE_FLAG, MATERIALIZE_PRODUCT_RW_FLAG, \
  55               MATERIALIZE_LP64_PRODUCT_FLAG)
  56 
  57 RUNTIME_OS_FLAGS(MATERIALIZE_DEVELOPER_FLAG, MATERIALIZE_PD_DEVELOPER_FLAG, \
  58                  MATERIALIZE_PRODUCT_FLAG, MATERIALIZE_PD_PRODUCT_FLAG, \
  59                  MATERIALIZE_DIAGNOSTIC_FLAG, MATERIALIZE_NOTPRODUCT_FLAG)
  60 
  61 ARCH_FLAGS(MATERIALIZE_DEVELOPER_FLAG, MATERIALIZE_PRODUCT_FLAG, \
  62            MATERIALIZE_DIAGNOSTIC_FLAG, MATERIALIZE_EXPERIMENTAL_FLAG, \
  63            MATERIALIZE_NOTPRODUCT_FLAG)
  64 
  65 MATERIALIZE_FLAGS_EXT
  66 
  67 
  68 static bool is_product_build() {
  69 #ifdef PRODUCT
  70   return true;
  71 #else
  72   return false;
  73 #endif
  74 }
  75 
  76 void Flag::check_writable() {
  77   if (is_constant_in_binary()) {
  78     fatal(err_msg("flag is constant: %s", _name));
  79   }
  80 }
  81 
  82 bool Flag::is_bool() const {
  83   return strcmp(_type, "bool") == 0;
  84 }
  85 
  86 bool Flag::get_bool() const {
  87   return *((bool*) _addr);
  88 }
  89 
  90 void Flag::set_bool(bool value) {
  91   check_writable();
  92   *((bool*) _addr) = value;
  93 }
  94 
  95 bool Flag::is_intx() const {
  96   return strcmp(_type, "intx")  == 0;
  97 }
  98 
  99 intx Flag::get_intx() const {
 100   return *((intx*) _addr);
 101 }
 102 
 103 void Flag::set_intx(intx value) {
 104   check_writable();
 105   *((intx*) _addr) = value;
 106 }
 107 
 108 bool Flag::is_uintx() const {
 109   return strcmp(_type, "uintx") == 0;
 110 }
 111 
 112 uintx Flag::get_uintx() const {
 113   return *((uintx*) _addr);
 114 }
 115 
 116 void Flag::set_uintx(uintx value) {
 117   check_writable();
 118   *((uintx*) _addr) = value;
 119 }
 120 
 121 bool Flag::is_uint64_t() const {
 122   return strcmp(_type, "uint64_t") == 0;
 123 }
 124 
 125 uint64_t Flag::get_uint64_t() const {
 126   return *((uint64_t*) _addr);
 127 }
 128 
 129 void Flag::set_uint64_t(uint64_t value) {
 130   check_writable();
 131   *((uint64_t*) _addr) = value;
 132 }
 133 
 134 bool Flag::is_double() const {
 135   return strcmp(_type, "double") == 0;
 136 }
 137 
 138 double Flag::get_double() const {
 139   return *((double*) _addr);
 140 }
 141 
 142 void Flag::set_double(double value) {
 143   check_writable();
 144   *((double*) _addr) = value;
 145 }
 146 
 147 bool Flag::is_ccstr() const {
 148   return strcmp(_type, "ccstr") == 0 || strcmp(_type, "ccstrlist") == 0;
 149 }
 150 
 151 bool Flag::ccstr_accumulates() const {
 152   return strcmp(_type, "ccstrlist") == 0;
 153 }
 154 
 155 ccstr Flag::get_ccstr() const {
 156   return *((ccstr*) _addr);
 157 }
 158 
 159 void Flag::set_ccstr(ccstr value) {
 160   check_writable();
 161   *((ccstr*) _addr) = value;
 162 }
 163 
 164 
 165 Flag::Flags Flag::get_origin() {
 166   return Flags(_flags & VALUE_ORIGIN_MASK);
 167 }
 168 
 169 void Flag::set_origin(Flags origin) {
 170   assert((origin & VALUE_ORIGIN_MASK) == origin, "sanity");
 171   _flags = Flags((_flags & ~VALUE_ORIGIN_MASK) | origin);
 172 }
 173 
 174 bool Flag::is_default() {
 175   return (get_origin() == DEFAULT);
 176 }
 177 
 178 bool Flag::is_ergonomic() {
 179   return (get_origin() == ERGONOMIC);
 180 }
 181 
 182 bool Flag::is_command_line() {
 183   return (get_origin() == COMMAND_LINE);
 184 }
 185 
 186 bool Flag::is_product() const {
 187   return (_flags & KIND_PRODUCT) != 0;
 188 }
 189 
 190 bool Flag::is_manageable() const {
 191   return (_flags & KIND_MANAGEABLE) != 0;
 192 }
 193 
 194 bool Flag::is_diagnostic() const {
 195   return (_flags & KIND_DIAGNOSTIC) != 0;
 196 }
 197 
 198 bool Flag::is_experimental() const {
 199   return (_flags & KIND_EXPERIMENTAL) != 0;
 200 }
 201 
 202 bool Flag::is_notproduct() const {
 203   return (_flags & KIND_NOT_PRODUCT) != 0;
 204 }
 205 
 206 bool Flag::is_develop() const {
 207   return (_flags & KIND_DEVELOP) != 0;
 208 }
 209 
 210 bool Flag::is_read_write() const {
 211   return (_flags & KIND_READ_WRITE) != 0;
 212 }
 213 
 214 bool Flag::is_commercial() const {
 215   return (_flags & KIND_COMMERCIAL) != 0;
 216 }
 217 
 218 /**
 219  * Returns if this flag is a constant in the binary.  Right now this is
 220  * true for notproduct and develop flags in product builds.
 221  */
 222 bool Flag::is_constant_in_binary() const {
 223 #ifdef PRODUCT
 224     return is_notproduct() || is_develop();
 225 #else
 226     return false;
 227 #endif
 228 }
 229 
 230 bool Flag::is_unlocker() const {
 231   return strcmp(_name, "UnlockDiagnosticVMOptions") == 0     ||
 232          strcmp(_name, "UnlockExperimentalVMOptions") == 0   ||
 233          is_unlocker_ext();
 234 }
 235 
 236 bool Flag::is_unlocked() const {
 237   if (is_diagnostic()) {
 238     return UnlockDiagnosticVMOptions;
 239   }
 240   if (is_experimental()) {
 241     return UnlockExperimentalVMOptions;
 242   }
 243   return is_unlocked_ext();
 244 }
 245 
 246 void Flag::unlock_diagnostic() {
 247   assert(is_diagnostic(), "sanity");
 248   _flags = Flags(_flags & ~KIND_DIAGNOSTIC);
 249 }
 250 
 251 // Get custom message for this locked flag, or return NULL if
 252 // none is available.
 253 void Flag::get_locked_message(char* buf, int buflen) const {
 254   buf[0] = '\0';
 255   if (is_diagnostic() && !is_unlocked()) {
 256     jio_snprintf(buf, buflen, "Error: VM option '%s' is diagnostic and must be enabled via -XX:+UnlockDiagnosticVMOptions.\n",
 257                  _name);
 258     return;
 259   }
 260   if (is_experimental() && !is_unlocked()) {
 261     jio_snprintf(buf, buflen, "Error: VM option '%s' is experimental and must be enabled via -XX:+UnlockExperimentalVMOptions.\n",
 262                  _name);
 263     return;
 264   }
 265   if (is_develop() && is_product_build()) {
 266     jio_snprintf(buf, buflen, "Error: VM option '%s' is develop and is available only in debug version of VM.\n",
 267                  _name);
 268     return;
 269   }
 270   if (is_notproduct() && is_product_build()) {
 271     jio_snprintf(buf, buflen, "Error: VM option '%s' is notproduct and is available only in debug version of VM.\n",
 272                  _name);
 273     return;
 274   }
 275   get_locked_message_ext(buf, buflen);
 276 }
 277 
 278 bool Flag::is_writeable() const {
 279   return is_manageable() || (is_product() && is_read_write()) || is_writeable_ext();
 280 }
 281 
 282 // All flags except "manageable" are assumed to be internal flags.
 283 // Long term, we need to define a mechanism to specify which flags
 284 // are external/stable and change this function accordingly.
 285 bool Flag::is_external() const {
 286   return is_manageable() || is_external_ext();
 287 }
 288 
 289 
 290 // Length of format string (e.g. "%.1234s") for printing ccstr below
 291 #define FORMAT_BUFFER_LEN 16
 292 
 293 PRAGMA_FORMAT_NONLITERAL_IGNORED_EXTERNAL
 294 void Flag::print_on(outputStream* st, bool withComments) {
 295   // Don't print notproduct and develop flags in a product build.
 296   if (is_constant_in_binary()) {
 297     return;
 298   }
 299 
 300   st->print("%9s %-40s %c= ", _type, _name, (!is_default() ? ':' : ' '));
 301 
 302   if (is_bool()) {
 303     st->print("%-16s", get_bool() ? "true" : "false");
 304   }
 305   if (is_intx()) {
 306     st->print("%-16ld", get_intx());
 307   }
 308   if (is_uintx()) {
 309     st->print("%-16lu", get_uintx());
 310   }
 311   if (is_uint64_t()) {
 312     st->print("%-16lu", get_uint64_t());
 313   }
 314   if (is_double()) {
 315     st->print("%-16f", get_double());
 316   }
 317   if (is_ccstr()) {
 318     const char* cp = get_ccstr();
 319     if (cp != NULL) {
 320       const char* eol;
 321       while ((eol = strchr(cp, '\n')) != NULL) {
 322         char format_buffer[FORMAT_BUFFER_LEN];
 323         size_t llen = pointer_delta(eol, cp, sizeof(char));
 324         jio_snprintf(format_buffer, FORMAT_BUFFER_LEN,
 325             "%%." SIZE_FORMAT "s", llen);
 326 PRAGMA_DIAG_PUSH
 327 PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL
 328         st->print(format_buffer, cp);
 329 PRAGMA_DIAG_POP
 330         st->cr();
 331         cp = eol+1;
 332         st->print("%5s %-35s += ", "", _name);
 333       }
 334       st->print("%-16s", cp);
 335     }
 336     else st->print("%-16s", "");
 337   }
 338 
 339   st->print("%-20s", " ");
 340   print_kind(st);
 341 
 342   if (withComments) {
 343 #ifndef PRODUCT
 344     st->print("%s", _doc);
 345 #endif
 346   }
 347   st->cr();
 348 }
 349 
 350 void Flag::print_kind(outputStream* st) {
 351   struct Data {
 352     int flag;
 353     const char* name;
 354   };
 355 
 356   Data data[] = {
 357       { KIND_C1, "C1" },
 358       { KIND_C2, "C2" },
 359       { KIND_ARCH, "ARCH" },
 360       { KIND_SHARK, "SHARK" },
 361       { KIND_PLATFORM_DEPENDENT, "pd" },
 362       { KIND_PRODUCT, "product" },
 363       { KIND_MANAGEABLE, "manageable" },
 364       { KIND_DIAGNOSTIC, "diagnostic" },
 365       { KIND_EXPERIMENTAL, "experimental" },
 366       { KIND_COMMERCIAL, "commercial" },
 367       { KIND_NOT_PRODUCT, "notproduct" },
 368       { KIND_DEVELOP, "develop" },
 369       { KIND_LP64_PRODUCT, "lp64_product" },
 370       { KIND_READ_WRITE, "rw" },
 371       { -1, "" }
 372   };
 373 
 374   if ((_flags & KIND_MASK) != 0) {
 375     st->print("{");
 376     bool is_first = true;
 377 
 378     for (int i = 0; data[i].flag != -1; i++) {
 379       Data d = data[i];
 380       if ((_flags & d.flag) != 0) {
 381         if (is_first) {
 382           is_first = false;
 383         } else {
 384           st->print(" ");
 385         }
 386         st->print("%s", d.name);
 387       }
 388     }
 389 
 390     st->print("}");
 391   }
 392 }
 393 
 394 void Flag::print_as_flag(outputStream* st) {
 395   if (is_bool()) {
 396     st->print("-XX:%s%s", get_bool() ? "+" : "-", _name);
 397   } else if (is_intx()) {
 398     st->print("-XX:%s=" INTX_FORMAT, _name, get_intx());
 399   } else if (is_uintx()) {
 400     st->print("-XX:%s=" UINTX_FORMAT, _name, get_uintx());
 401   } else if (is_uint64_t()) {
 402     st->print("-XX:%s=" UINT64_FORMAT, _name, get_uint64_t());
 403   } else if (is_double()) {
 404     st->print("-XX:%s=%f", _name, get_double());
 405   } else if (is_ccstr()) {
 406     st->print("-XX:%s=", _name);
 407     const char* cp = get_ccstr();
 408     if (cp != NULL) {
 409       // Need to turn embedded '\n's back into separate arguments
 410       // Not so efficient to print one character at a time,
 411       // but the choice is to do the transformation to a buffer
 412       // and print that.  And this need not be efficient.
 413       for (; *cp != '\0'; cp += 1) {
 414         switch (*cp) {
 415           default:
 416             st->print("%c", *cp);
 417             break;
 418           case '\n':
 419             st->print(" -XX:%s=", _name);
 420             break;
 421         }
 422       }
 423     }
 424   } else {
 425     ShouldNotReachHere();
 426   }
 427 }
 428 
 429 // 4991491 do not "optimize out" the was_set false values: omitting them
 430 // tickles a Microsoft compiler bug causing flagTable to be malformed
 431 
 432 #define NAME(name) NOT_PRODUCT(&name) PRODUCT_ONLY(&CONST_##name)
 433 
 434 #define RUNTIME_PRODUCT_FLAG_STRUCT(     type, name, value, doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_PRODUCT) },
 435 #define RUNTIME_PD_PRODUCT_FLAG_STRUCT(  type, name,        doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_PRODUCT | Flag::KIND_PLATFORM_DEPENDENT) },
 436 #define RUNTIME_DIAGNOSTIC_FLAG_STRUCT(  type, name, value, doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_DIAGNOSTIC) },
 437 #define RUNTIME_EXPERIMENTAL_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_EXPERIMENTAL) },
 438 #define RUNTIME_MANAGEABLE_FLAG_STRUCT(  type, name, value, doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_MANAGEABLE) },
 439 #define RUNTIME_PRODUCT_RW_FLAG_STRUCT(  type, name, value, doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_PRODUCT | Flag::KIND_READ_WRITE) },
 440 #define RUNTIME_DEVELOP_FLAG_STRUCT(     type, name, value, doc) { #type, XSTR(name), NAME(name), NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_DEVELOP) },
 441 #define RUNTIME_PD_DEVELOP_FLAG_STRUCT(  type, name,        doc) { #type, XSTR(name), NAME(name), NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_DEVELOP | Flag::KIND_PLATFORM_DEPENDENT) },
 442 #define RUNTIME_NOTPRODUCT_FLAG_STRUCT(  type, name, value, doc) { #type, XSTR(name), NAME(name), NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_NOT_PRODUCT) },
 443 
 444 #ifdef _LP64
 445 #define RUNTIME_LP64_PRODUCT_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_LP64_PRODUCT) },
 446 #else
 447 #define RUNTIME_LP64_PRODUCT_FLAG_STRUCT(type, name, value, doc) /* flag is constant */
 448 #endif // _LP64
 449 
 450 #define C1_PRODUCT_FLAG_STRUCT(          type, name, value, doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C1 | Flag::KIND_PRODUCT) },
 451 #define C1_PD_PRODUCT_FLAG_STRUCT(       type, name,        doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C1 | Flag::KIND_PRODUCT | Flag::KIND_PLATFORM_DEPENDENT) },
 452 #define C1_DIAGNOSTIC_FLAG_STRUCT(       type, name, value, doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C1 | Flag::KIND_DIAGNOSTIC) },
 453 #define C1_DEVELOP_FLAG_STRUCT(          type, name, value, doc) { #type, XSTR(name), NAME(name), NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C1 | Flag::KIND_DEVELOP) },
 454 #define C1_PD_DEVELOP_FLAG_STRUCT(       type, name,        doc) { #type, XSTR(name), NAME(name), NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C1 | Flag::KIND_DEVELOP | Flag::KIND_PLATFORM_DEPENDENT) },
 455 #define C1_NOTPRODUCT_FLAG_STRUCT(       type, name, value, doc) { #type, XSTR(name), NAME(name), NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C1 | Flag::KIND_NOT_PRODUCT) },
 456 
 457 #define C2_PRODUCT_FLAG_STRUCT(          type, name, value, doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C2 | Flag::KIND_PRODUCT) },
 458 #define C2_PD_PRODUCT_FLAG_STRUCT(       type, name,        doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C2 | Flag::KIND_PRODUCT | Flag::KIND_PLATFORM_DEPENDENT) },
 459 #define C2_DIAGNOSTIC_FLAG_STRUCT(       type, name, value, doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C2 | Flag::KIND_DIAGNOSTIC) },
 460 #define C2_EXPERIMENTAL_FLAG_STRUCT(     type, name, value, doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C2 | Flag::KIND_EXPERIMENTAL) },
 461 #define C2_DEVELOP_FLAG_STRUCT(          type, name, value, doc) { #type, XSTR(name), NAME(name), NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C2 | Flag::KIND_DEVELOP) },
 462 #define C2_PD_DEVELOP_FLAG_STRUCT(       type, name,        doc) { #type, XSTR(name), NAME(name), NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C2 | Flag::KIND_DEVELOP | Flag::KIND_PLATFORM_DEPENDENT) },
 463 #define C2_NOTPRODUCT_FLAG_STRUCT(       type, name, value, doc) { #type, XSTR(name), NAME(name), NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C2 | Flag::KIND_NOT_PRODUCT) },
 464 
 465 #define ARCH_PRODUCT_FLAG_STRUCT(        type, name, value, doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_ARCH | Flag::KIND_PRODUCT) },
 466 #define ARCH_DIAGNOSTIC_FLAG_STRUCT(     type, name, value, doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_ARCH | Flag::KIND_DIAGNOSTIC) },
 467 #define ARCH_EXPERIMENTAL_FLAG_STRUCT(   type, name, value, doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_ARCH | Flag::KIND_EXPERIMENTAL) },
 468 #define ARCH_DEVELOP_FLAG_STRUCT(        type, name, value, doc) { #type, XSTR(name), NAME(name), NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_ARCH | Flag::KIND_DEVELOP) },
 469 #define ARCH_NOTPRODUCT_FLAG_STRUCT(     type, name, value, doc) { #type, XSTR(name), NAME(name), NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_ARCH | Flag::KIND_NOT_PRODUCT) },
 470 
 471 #define SHARK_PRODUCT_FLAG_STRUCT(       type, name, value, doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_SHARK | Flag::KIND_PRODUCT) },
 472 #define SHARK_PD_PRODUCT_FLAG_STRUCT(    type, name,        doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_SHARK | Flag::KIND_PRODUCT | Flag::KIND_PLATFORM_DEPENDENT) },
 473 #define SHARK_DIAGNOSTIC_FLAG_STRUCT(    type, name, value, doc) { #type, XSTR(name), &name,      NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_SHARK | Flag::KIND_DIAGNOSTIC) },
 474 #define SHARK_DEVELOP_FLAG_STRUCT(       type, name, value, doc) { #type, XSTR(name), NAME(name), NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_SHARK | Flag::KIND_DEVELOP) },
 475 #define SHARK_PD_DEVELOP_FLAG_STRUCT(    type, name,        doc) { #type, XSTR(name), NAME(name), NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_SHARK | Flag::KIND_DEVELOP | Flag::KIND_PLATFORM_DEPENDENT) },
 476 #define SHARK_NOTPRODUCT_FLAG_STRUCT(    type, name, value, doc) { #type, XSTR(name), NAME(name), NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_SHARK | Flag::KIND_NOT_PRODUCT) },
 477 
 478 static Flag flagTable[] = {
 479  RUNTIME_FLAGS(RUNTIME_DEVELOP_FLAG_STRUCT, RUNTIME_PD_DEVELOP_FLAG_STRUCT, RUNTIME_PRODUCT_FLAG_STRUCT, RUNTIME_PD_PRODUCT_FLAG_STRUCT, RUNTIME_DIAGNOSTIC_FLAG_STRUCT, RUNTIME_EXPERIMENTAL_FLAG_STRUCT, RUNTIME_NOTPRODUCT_FLAG_STRUCT, RUNTIME_MANAGEABLE_FLAG_STRUCT, RUNTIME_PRODUCT_RW_FLAG_STRUCT, RUNTIME_LP64_PRODUCT_FLAG_STRUCT)
 480  RUNTIME_OS_FLAGS(RUNTIME_DEVELOP_FLAG_STRUCT, RUNTIME_PD_DEVELOP_FLAG_STRUCT, RUNTIME_PRODUCT_FLAG_STRUCT, RUNTIME_PD_PRODUCT_FLAG_STRUCT, RUNTIME_DIAGNOSTIC_FLAG_STRUCT, RUNTIME_NOTPRODUCT_FLAG_STRUCT)
 481 #if INCLUDE_ALL_GCS
 482  G1_FLAGS(RUNTIME_DEVELOP_FLAG_STRUCT, RUNTIME_PD_DEVELOP_FLAG_STRUCT, RUNTIME_PRODUCT_FLAG_STRUCT, RUNTIME_PD_PRODUCT_FLAG_STRUCT, RUNTIME_DIAGNOSTIC_FLAG_STRUCT, RUNTIME_EXPERIMENTAL_FLAG_STRUCT, RUNTIME_NOTPRODUCT_FLAG_STRUCT, RUNTIME_MANAGEABLE_FLAG_STRUCT, RUNTIME_PRODUCT_RW_FLAG_STRUCT)
 483 #endif // INCLUDE_ALL_GCS
 484 #ifdef COMPILER1
 485  C1_FLAGS(C1_DEVELOP_FLAG_STRUCT, C1_PD_DEVELOP_FLAG_STRUCT, C1_PRODUCT_FLAG_STRUCT, C1_PD_PRODUCT_FLAG_STRUCT, C1_DIAGNOSTIC_FLAG_STRUCT, C1_NOTPRODUCT_FLAG_STRUCT)
 486 #endif
 487 #ifdef COMPILER2
 488  C2_FLAGS(C2_DEVELOP_FLAG_STRUCT, C2_PD_DEVELOP_FLAG_STRUCT, C2_PRODUCT_FLAG_STRUCT, C2_PD_PRODUCT_FLAG_STRUCT, C2_DIAGNOSTIC_FLAG_STRUCT, C2_EXPERIMENTAL_FLAG_STRUCT, C2_NOTPRODUCT_FLAG_STRUCT)
 489 #endif
 490 #ifdef SHARK
 491  SHARK_FLAGS(SHARK_DEVELOP_FLAG_STRUCT, SHARK_PD_DEVELOP_FLAG_STRUCT, SHARK_PRODUCT_FLAG_STRUCT, SHARK_PD_PRODUCT_FLAG_STRUCT, SHARK_DIAGNOSTIC_FLAG_STRUCT, SHARK_NOTPRODUCT_FLAG_STRUCT)
 492 #endif
 493  ARCH_FLAGS(ARCH_DEVELOP_FLAG_STRUCT, ARCH_PRODUCT_FLAG_STRUCT, ARCH_DIAGNOSTIC_FLAG_STRUCT, ARCH_EXPERIMENTAL_FLAG_STRUCT, ARCH_NOTPRODUCT_FLAG_STRUCT)
 494  FLAGTABLE_EXT
 495  {0, NULL, NULL}
 496 };
 497 
 498 Flag* Flag::flags = flagTable;
 499 size_t Flag::numFlags = (sizeof(flagTable) / sizeof(Flag));
 500 
 501 inline bool str_equal(const char* s, const char* q, size_t len) {
 502   // s is null terminated, q is not!
 503   if (strlen(s) != (unsigned int) len) return false;
 504   return strncmp(s, q, len) == 0;
 505 }
 506 
 507 // Search the flag table for a named flag
 508 Flag* Flag::find_flag(const char* name, size_t length, bool allow_locked, bool return_flag) {
 509   for (Flag* current = &flagTable[0]; current->_name != NULL; current++) {
 510     if (str_equal(current->_name, name, length)) {
 511       // Found a matching entry.
 512       // Don't report notproduct and develop flags in product builds.
 513       if (current->is_constant_in_binary()) {
 514         return (return_flag == true ? current : NULL);
 515       }
 516       // Report locked flags only if allowed.
 517       if (!(current->is_unlocked() || current->is_unlocker())) {
 518         if (!allow_locked) {
 519           // disable use of locked flags, e.g. diagnostic, experimental,
 520           // commercial... until they are explicitly unlocked
 521           return NULL;
 522         }
 523       }
 524       return current;
 525     }
 526   }
 527   // Flag name is not in the flag table
 528   return NULL;
 529 }
 530 
 531 // Compute string similarity based on Dice's coefficient
 532 static float str_similar(const char* str1, const char* str2, size_t len2) {
 533   int len1 = (int) strlen(str1);
 534   int total = len1 + (int) len2;
 535 
 536   int hit = 0;
 537 
 538   for (int i = 0; i < len1 -1; ++i) {
 539     for (int j = 0; j < (int) len2 -1; ++j) {
 540       if ((str1[i] == str2[j]) && (str1[i+1] == str2[j+1])) {
 541         ++hit;
 542         break;
 543       }
 544     }
 545   }
 546 
 547   return 2.0f * (float) hit / (float) total;
 548 }
 549 
 550 Flag* Flag::fuzzy_match(const char* name, size_t length, bool allow_locked) {
 551   float VMOptionsFuzzyMatchSimilarity = 0.7f;
 552   Flag* match = NULL;
 553   float score;
 554   float max_score = -1;
 555 
 556   for (Flag* current = &flagTable[0]; current->_name != NULL; current++) {
 557     score = str_similar(current->_name, name, length);
 558     if (score > max_score) {
 559       max_score = score;
 560       match = current;
 561     }
 562   }
 563 
 564   if (!(match->is_unlocked() || match->is_unlocker())) {
 565     if (!allow_locked) {
 566       return NULL;
 567     }
 568   }
 569 
 570   if (max_score < VMOptionsFuzzyMatchSimilarity) {
 571     return NULL;
 572   }
 573 
 574   return match;
 575 }
 576 
 577 // Returns the address of the index'th element
 578 static Flag* address_of_flag(CommandLineFlagWithType flag) {
 579   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 580   return &Flag::flags[flag];
 581 }
 582 
 583 bool CommandLineFlagsEx::is_default(CommandLineFlag flag) {
 584   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 585   Flag* f = &Flag::flags[flag];
 586   return f->is_default();
 587 }
 588 
 589 bool CommandLineFlagsEx::is_ergo(CommandLineFlag flag) {
 590   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 591   Flag* f = &Flag::flags[flag];
 592   return f->is_ergonomic();
 593 }
 594 
 595 bool CommandLineFlagsEx::is_cmdline(CommandLineFlag flag) {
 596   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 597   Flag* f = &Flag::flags[flag];
 598   return f->is_command_line();
 599 }
 600 
 601 bool CommandLineFlags::wasSetOnCmdline(const char* name, bool* value) {
 602   Flag* result = Flag::find_flag((char*)name, strlen(name));
 603   if (result == NULL) return false;
 604   *value = result->is_command_line();
 605   return true;
 606 }
 607 
 608 template<class E, class T>
 609 static void trace_flag_changed(const char* name, const T old_value, const T new_value, const Flag::Flags origin)
 610 {
 611   E e;
 612   e.set_name(name);
 613   e.set_old_value(old_value);
 614   e.set_new_value(new_value);
 615   e.set_origin(origin);
 616   e.commit();
 617 }
 618 
 619 bool CommandLineFlags::boolAt(const char* name, size_t len, bool* value) {
 620   Flag* result = Flag::find_flag(name, len);
 621   if (result == NULL) return false;
 622   if (!result->is_bool()) return false;
 623   *value = result->get_bool();
 624   return true;
 625 }
 626 
 627 bool CommandLineFlags::boolAtPut(const char* name, size_t len, bool* value, Flag::Flags origin) {
 628   Flag* result = Flag::find_flag(name, len);
 629   if (result == NULL) return false;
 630   if (!result->is_bool()) return false;
 631   bool old_value = result->get_bool();
 632   trace_flag_changed<EventBooleanFlagChanged, bool>(name, old_value, *value, origin);
 633   result->set_bool(*value);
 634   *value = old_value;
 635   result->set_origin(origin);
 636   return true;
 637 }
 638 
 639 void CommandLineFlagsEx::boolAtPut(CommandLineFlagWithType flag, bool value, Flag::Flags origin) {
 640   Flag* faddr = address_of_flag(flag);
 641   guarantee(faddr != NULL && faddr->is_bool(), "wrong flag type");
 642   trace_flag_changed<EventBooleanFlagChanged, bool>(faddr->_name, faddr->get_bool(), value, origin);
 643   faddr->set_bool(value);
 644   faddr->set_origin(origin);
 645 }
 646 
 647 bool CommandLineFlags::intxAt(const char* name, size_t len, intx* value) {
 648   Flag* result = Flag::find_flag(name, len);
 649   if (result == NULL) return false;
 650   if (!result->is_intx()) return false;
 651   *value = result->get_intx();
 652   return true;
 653 }
 654 
 655 bool CommandLineFlags::intxAtPut(const char* name, size_t len, intx* value, Flag::Flags origin) {
 656   Flag* result = Flag::find_flag(name, len);
 657   if (result == NULL) return false;
 658   if (!result->is_intx()) return false;
 659   intx old_value = result->get_intx();
 660   trace_flag_changed<EventLongFlagChanged, s8>(name, old_value, *value, origin);
 661   result->set_intx(*value);
 662   *value = old_value;
 663   result->set_origin(origin);
 664   return true;
 665 }
 666 
 667 void CommandLineFlagsEx::intxAtPut(CommandLineFlagWithType flag, intx value, Flag::Flags origin) {
 668   Flag* faddr = address_of_flag(flag);
 669   guarantee(faddr != NULL && faddr->is_intx(), "wrong flag type");
 670   trace_flag_changed<EventLongFlagChanged, s8>(faddr->_name, faddr->get_intx(), value, origin);
 671   faddr->set_intx(value);
 672   faddr->set_origin(origin);
 673 }
 674 
 675 bool CommandLineFlags::uintxAt(const char* name, size_t len, uintx* value) {
 676   Flag* result = Flag::find_flag(name, len);
 677   if (result == NULL) return false;
 678   if (!result->is_uintx()) return false;
 679   *value = result->get_uintx();
 680   return true;
 681 }
 682 
 683 bool CommandLineFlags::uintxAtPut(const char* name, size_t len, uintx* value, Flag::Flags origin) {
 684   Flag* result = Flag::find_flag(name, len);
 685   if (result == NULL) return false;
 686   if (!result->is_uintx()) return false;
 687   uintx old_value = result->get_uintx();
 688   trace_flag_changed<EventUnsignedLongFlagChanged, u8>(name, old_value, *value, origin);
 689   result->set_uintx(*value);
 690   *value = old_value;
 691   result->set_origin(origin);
 692   return true;
 693 }
 694 
 695 void CommandLineFlagsEx::uintxAtPut(CommandLineFlagWithType flag, uintx value, Flag::Flags origin) {
 696   Flag* faddr = address_of_flag(flag);
 697   guarantee(faddr != NULL && faddr->is_uintx(), "wrong flag type");
 698   trace_flag_changed<EventUnsignedLongFlagChanged, u8>(faddr->_name, faddr->get_uintx(), value, origin);
 699   faddr->set_uintx(value);
 700   faddr->set_origin(origin);
 701 }
 702 
 703 bool CommandLineFlags::uint64_tAt(const char* name, size_t len, uint64_t* value) {
 704   Flag* result = Flag::find_flag(name, len);
 705   if (result == NULL) return false;
 706   if (!result->is_uint64_t()) return false;
 707   *value = result->get_uint64_t();
 708   return true;
 709 }
 710 
 711 bool CommandLineFlags::uint64_tAtPut(const char* name, size_t len, uint64_t* value, Flag::Flags origin) {
 712   Flag* result = Flag::find_flag(name, len);
 713   if (result == NULL) return false;
 714   if (!result->is_uint64_t()) return false;
 715   uint64_t old_value = result->get_uint64_t();
 716   trace_flag_changed<EventUnsignedLongFlagChanged, u8>(name, old_value, *value, origin);
 717   result->set_uint64_t(*value);
 718   *value = old_value;
 719   result->set_origin(origin);
 720   return true;
 721 }
 722 
 723 void CommandLineFlagsEx::uint64_tAtPut(CommandLineFlagWithType flag, uint64_t value, Flag::Flags origin) {
 724   Flag* faddr = address_of_flag(flag);
 725   guarantee(faddr != NULL && faddr->is_uint64_t(), "wrong flag type");
 726   trace_flag_changed<EventUnsignedLongFlagChanged, u8>(faddr->_name, faddr->get_uint64_t(), value, origin);
 727   faddr->set_uint64_t(value);
 728   faddr->set_origin(origin);
 729 }
 730 
 731 bool CommandLineFlags::doubleAt(const char* name, size_t len, double* value) {
 732   Flag* result = Flag::find_flag(name, len);
 733   if (result == NULL) return false;
 734   if (!result->is_double()) return false;
 735   *value = result->get_double();
 736   return true;
 737 }
 738 
 739 bool CommandLineFlags::doubleAtPut(const char* name, size_t len, double* value, Flag::Flags origin) {
 740   Flag* result = Flag::find_flag(name, len);
 741   if (result == NULL) return false;
 742   if (!result->is_double()) return false;
 743   double old_value = result->get_double();
 744   trace_flag_changed<EventDoubleFlagChanged, double>(name, old_value, *value, origin);
 745   result->set_double(*value);
 746   *value = old_value;
 747   result->set_origin(origin);
 748   return true;
 749 }
 750 
 751 void CommandLineFlagsEx::doubleAtPut(CommandLineFlagWithType flag, double value, Flag::Flags origin) {
 752   Flag* faddr = address_of_flag(flag);
 753   guarantee(faddr != NULL && faddr->is_double(), "wrong flag type");
 754   trace_flag_changed<EventDoubleFlagChanged, double>(faddr->_name, faddr->get_double(), value, origin);
 755   faddr->set_double(value);
 756   faddr->set_origin(origin);
 757 }
 758 
 759 bool CommandLineFlags::ccstrAt(const char* name, size_t len, ccstr* value) {
 760   Flag* result = Flag::find_flag(name, len);
 761   if (result == NULL) return false;
 762   if (!result->is_ccstr()) return false;
 763   *value = result->get_ccstr();
 764   return true;
 765 }
 766 
 767 bool CommandLineFlags::ccstrAtPut(const char* name, size_t len, ccstr* value, Flag::Flags origin) {
 768   Flag* result = Flag::find_flag(name, len);
 769   if (result == NULL) return false;
 770   if (!result->is_ccstr()) return false;
 771   ccstr old_value = result->get_ccstr();
 772   trace_flag_changed<EventStringFlagChanged, const char*>(name, old_value, *value, origin);
 773   char* new_value = NULL;
 774   if (*value != NULL) {
 775     new_value = NEW_C_HEAP_ARRAY(char, strlen(*value)+1, mtInternal);
 776     strcpy(new_value, *value);
 777   }
 778   result->set_ccstr(new_value);
 779   if (result->is_default() && old_value != NULL) {
 780     // Prior value is NOT heap allocated, but was a literal constant.
 781     char* old_value_to_free = NEW_C_HEAP_ARRAY(char, strlen(old_value)+1, mtInternal);
 782     strcpy(old_value_to_free, old_value);
 783     old_value = old_value_to_free;
 784   }
 785   *value = old_value;
 786   result->set_origin(origin);
 787   return true;
 788 }
 789 
 790 void CommandLineFlagsEx::ccstrAtPut(CommandLineFlagWithType flag, ccstr value, Flag::Flags origin) {
 791   Flag* faddr = address_of_flag(flag);
 792   guarantee(faddr != NULL && faddr->is_ccstr(), "wrong flag type");
 793   ccstr old_value = faddr->get_ccstr();
 794   trace_flag_changed<EventStringFlagChanged, const char*>(faddr->_name, old_value, value, origin);
 795   char* new_value = NEW_C_HEAP_ARRAY(char, strlen(value)+1, mtInternal);
 796   strcpy(new_value, value);
 797   faddr->set_ccstr(new_value);
 798   if (!faddr->is_default() && old_value != NULL) {
 799     // Prior value is heap allocated so free it.
 800     FREE_C_HEAP_ARRAY(char, old_value, mtInternal);
 801   }
 802   faddr->set_origin(origin);
 803 }
 804 
 805 extern "C" {
 806   static int compare_flags(const void* void_a, const void* void_b) {
 807     return strcmp((*((Flag**) void_a))->_name, (*((Flag**) void_b))->_name);
 808   }
 809 }
 810 
 811 void CommandLineFlags::printSetFlags(outputStream* out) {
 812   // Print which flags were set on the command line
 813   // note: this method is called before the thread structure is in place
 814   //       which means resource allocation cannot be used.
 815 
 816   // The last entry is the null entry.
 817   const size_t length = Flag::numFlags - 1;
 818 
 819   // Sort
 820   Flag** array = NEW_C_HEAP_ARRAY(Flag*, length, mtInternal);
 821   for (size_t i = 0; i < length; i++) {
 822     array[i] = &flagTable[i];
 823   }
 824   qsort(array, length, sizeof(Flag*), compare_flags);
 825 
 826   // Print
 827   for (size_t i = 0; i < length; i++) {
 828     if (array[i]->get_origin() /* naked field! */) {
 829       array[i]->print_as_flag(out);
 830       out->print(" ");
 831     }
 832   }
 833   out->cr();
 834   FREE_C_HEAP_ARRAY(Flag*, array, mtInternal);
 835 }
 836 
 837 #ifndef PRODUCT
 838 
 839 
 840 void CommandLineFlags::verify() {
 841   assert(Arguments::check_vm_args_consistency(), "Some flag settings conflict");
 842 }
 843 
 844 #endif // PRODUCT
 845 
 846 void CommandLineFlags::printFlags(outputStream* out, bool withComments) {
 847   // Print the flags sorted by name
 848   // note: this method is called before the thread structure is in place
 849   //       which means resource allocation cannot be used.
 850 
 851   // The last entry is the null entry.
 852   const size_t length = Flag::numFlags - 1;
 853 
 854   // Sort
 855   Flag** array = NEW_C_HEAP_ARRAY(Flag*, length, mtInternal);
 856   for (size_t i = 0; i < length; i++) {
 857     array[i] = &flagTable[i];
 858   }
 859   qsort(array, length, sizeof(Flag*), compare_flags);
 860 
 861   // Print
 862   out->print_cr("[Global flags]");
 863   for (size_t i = 0; i < length; i++) {
 864     if (array[i]->is_unlocked()) {
 865       array[i]->print_on(out, withComments);
 866     }
 867   }
 868   FREE_C_HEAP_ARRAY(Flag*, array, mtInternal);
 869 }