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