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