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