1 /*
   2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "memory/allocation.inline.hpp"
  28 #include "oops/oop.inline.hpp"
  29 #include "runtime/arguments.hpp"
  30 #include "runtime/globals.hpp"
  31 #include "runtime/globals_extension.hpp"
  32 #include "runtime/commandLineFlagConstraintList.hpp"
  33 #include "runtime/commandLineFlagWriteableList.hpp"
  34 #include "runtime/commandLineFlagRangeList.hpp"
  35 #include "runtime/os.hpp"
  36 #include "runtime/sharedRuntime.hpp"
  37 #include "trace/tracing.hpp"
  38 #include "utilities/defaultStream.hpp"
  39 #include "utilities/macros.hpp"
  40 #include "utilities/ostream.hpp"
  41 #if INCLUDE_ALL_GCS
  42 #include "gc/g1/g1_globals.hpp"
  43 #endif // INCLUDE_ALL_GCS
  44 #ifdef COMPILER1
  45 #include "c1/c1_globals.hpp"
  46 #endif
  47 #if INCLUDE_JVMCI
  48 #include "jvmci/jvmci_globals.hpp"
  49 #endif
  50 #ifdef COMPILER2
  51 #include "opto/c2_globals.hpp"
  52 #endif
  53 
  54 RUNTIME_FLAGS(MATERIALIZE_DEVELOPER_FLAG, \
  55               MATERIALIZE_PD_DEVELOPER_FLAG, \
  56               MATERIALIZE_PRODUCT_FLAG, \
  57               MATERIALIZE_PD_PRODUCT_FLAG, \
  58               MATERIALIZE_DIAGNOSTIC_FLAG, \
  59               MATERIALIZE_PD_DIAGNOSTIC_FLAG, \
  60               MATERIALIZE_EXPERIMENTAL_FLAG, \
  61               MATERIALIZE_NOTPRODUCT_FLAG, \
  62               MATERIALIZE_MANAGEABLE_FLAG, \
  63               MATERIALIZE_PRODUCT_RW_FLAG, \
  64               MATERIALIZE_LP64_PRODUCT_FLAG, \
  65               IGNORE_RANGE, \
  66               IGNORE_CONSTRAINT, \
  67               IGNORE_WRITEABLE)
  68 
  69 RUNTIME_OS_FLAGS(MATERIALIZE_DEVELOPER_FLAG, \
  70                  MATERIALIZE_PD_DEVELOPER_FLAG, \
  71                  MATERIALIZE_PRODUCT_FLAG, \
  72                  MATERIALIZE_PD_PRODUCT_FLAG, \
  73                  MATERIALIZE_DIAGNOSTIC_FLAG, \
  74                  MATERIALIZE_PD_DIAGNOSTIC_FLAG, \
  75                  MATERIALIZE_NOTPRODUCT_FLAG, \
  76                  IGNORE_RANGE, \
  77                  IGNORE_CONSTRAINT, \
  78                  IGNORE_WRITEABLE)
  79 
  80 ARCH_FLAGS(MATERIALIZE_DEVELOPER_FLAG, \
  81            MATERIALIZE_PRODUCT_FLAG, \
  82            MATERIALIZE_DIAGNOSTIC_FLAG, \
  83            MATERIALIZE_EXPERIMENTAL_FLAG, \
  84            MATERIALIZE_NOTPRODUCT_FLAG, \
  85            IGNORE_RANGE, \
  86            IGNORE_CONSTRAINT, \
  87            IGNORE_WRITEABLE)
  88 
  89 #if INCLUDE_VBC
  90 VALUE_BASED_CLASS_FLAGS(MATERIALIZE_PRODUCT_FLAG)
  91 #endif
  92 
  93 MATERIALIZE_FLAGS_EXT
  94 
  95 #define DEFAULT_RANGE_STR_CHUNK_SIZE 64
  96 static char* create_range_str(const char *fmt, ...) {
  97   static size_t string_length = DEFAULT_RANGE_STR_CHUNK_SIZE;
  98   static char* range_string = NEW_C_HEAP_ARRAY(char, string_length, mtLogging);
  99 
 100   int size_needed = 0;
 101   do {
 102     va_list args;
 103     va_start(args, fmt);
 104     size_needed = jio_vsnprintf(range_string, string_length, fmt, args);
 105     va_end(args);
 106 
 107     if (size_needed < 0) {
 108       string_length += DEFAULT_RANGE_STR_CHUNK_SIZE;
 109       range_string = REALLOC_C_HEAP_ARRAY(char, range_string, string_length, mtLogging);
 110       guarantee(range_string != NULL, "create_range_str string should not be NULL");
 111     }
 112   } while (size_needed < 0);
 113 
 114   return range_string;
 115 }
 116 
 117 const char* Flag::get_int_default_range_str() {
 118   return create_range_str("[ " INT32_FORMAT_W(-25) " ... " INT32_FORMAT_W(25) " ]", INT_MIN, INT_MAX);
 119 }
 120 
 121 const char* Flag::get_uint_default_range_str() {
 122   return create_range_str("[ " UINT32_FORMAT_W(-25) " ... " UINT32_FORMAT_W(25) " ]", 0, UINT_MAX);
 123 }
 124 
 125 const char* Flag::get_intx_default_range_str() {
 126   return create_range_str("[ " INTX_FORMAT_W(-25) " ... " INTX_FORMAT_W(25) " ]", min_intx, max_intx);
 127 }
 128 
 129 const char* Flag::get_uintx_default_range_str() {
 130   return create_range_str("[ " UINTX_FORMAT_W(-25) " ... " UINTX_FORMAT_W(25) " ]", 0, max_uintx);
 131 }
 132 
 133 const char* Flag::get_uint64_t_default_range_str() {
 134   return create_range_str("[ " UINT64_FORMAT_W(-25) " ... " UINT64_FORMAT_W(25) " ]", 0, uint64_t(max_juint));
 135 }
 136 
 137 const char* Flag::get_size_t_default_range_str() {
 138   return create_range_str("[ " SIZE_FORMAT_W(-25) " ... " SIZE_FORMAT_W(25) " ]", 0, SIZE_MAX);
 139 }
 140 
 141 const char* Flag::get_double_default_range_str() {
 142   return create_range_str("[ %-25.3f ... %25.3f ]", DBL_MIN, DBL_MAX);
 143 }
 144 
 145 static bool is_product_build() {
 146 #ifdef PRODUCT
 147   return true;
 148 #else
 149   return false;
 150 #endif
 151 }
 152 
 153 Flag::Error Flag::check_writable(bool changed) {
 154   if (is_constant_in_binary()) {
 155     fatal("flag is constant: %s", _name);
 156   }
 157 
 158   Flag::Error error = Flag::SUCCESS;
 159   if (changed) {
 160     CommandLineFlagWriteable* writeable = CommandLineFlagWriteableList::find(_name);
 161     if (writeable) {
 162       if (writeable->is_writeable() == false) {
 163         switch (writeable->type())
 164         {
 165           case CommandLineFlagWriteable::Once:
 166             error = Flag::SET_ONLY_ONCE;
 167             jio_fprintf(defaultStream::error_stream(), "Error: %s may not be set more than once\n", _name);
 168             break;
 169           case CommandLineFlagWriteable::CommandLineOnly:
 170             error = Flag::COMMAND_LINE_ONLY;
 171             jio_fprintf(defaultStream::error_stream(), "Error: %s may be modified only from commad line\n", _name);
 172             break;
 173           default:
 174             ShouldNotReachHere();
 175             break;
 176         }
 177       }
 178       writeable->mark_once();
 179     }
 180   }
 181   return error;
 182 }
 183 
 184 bool Flag::is_bool() const {
 185   return strcmp(_type, "bool") == 0;
 186 }
 187 
 188 bool Flag::get_bool() const {
 189   return *((bool*) _addr);
 190 }
 191 
 192 Flag::Error Flag::set_bool(bool value) {
 193   Flag::Error error = check_writable(value!=get_bool());
 194   if (error == Flag::SUCCESS) {
 195     *((bool*) _addr) = value;
 196   }
 197   return error;
 198 }
 199 
 200 bool Flag::is_int() const {
 201   return strcmp(_type, "int")  == 0;
 202 }
 203 
 204 int Flag::get_int() const {
 205   return *((int*) _addr);
 206 }
 207 
 208 Flag::Error Flag::set_int(int value) {
 209   Flag::Error error = check_writable(value!=get_int());
 210   if (error == Flag::SUCCESS) {
 211     *((int*) _addr) = value;
 212   }
 213   return error;
 214 }
 215 
 216 bool Flag::is_uint() const {
 217   return strcmp(_type, "uint")  == 0;
 218 }
 219 
 220 uint Flag::get_uint() const {
 221   return *((uint*) _addr);
 222 }
 223 
 224 Flag::Error Flag::set_uint(uint value) {
 225   Flag::Error error = check_writable(value!=get_uint());
 226   if (error == Flag::SUCCESS) {
 227     *((uint*) _addr) = value;
 228   }
 229   return error;
 230 }
 231 
 232 bool Flag::is_intx() const {
 233   return strcmp(_type, "intx")  == 0;
 234 }
 235 
 236 intx Flag::get_intx() const {
 237   return *((intx*) _addr);
 238 }
 239 
 240 Flag::Error Flag::set_intx(intx value) {
 241   Flag::Error error = check_writable(value!=get_intx());
 242   if (error == Flag::SUCCESS) {
 243     *((intx*) _addr) = value;
 244   }
 245   return error;
 246 }
 247 
 248 bool Flag::is_uintx() const {
 249   return strcmp(_type, "uintx") == 0;
 250 }
 251 
 252 uintx Flag::get_uintx() const {
 253   return *((uintx*) _addr);
 254 }
 255 
 256 Flag::Error Flag::set_uintx(uintx value) {
 257   Flag::Error error = check_writable(value!=get_uintx());
 258   if (error == Flag::SUCCESS) {
 259     *((uintx*) _addr) = value;
 260   }
 261   return error;
 262 }
 263 
 264 bool Flag::is_uint64_t() const {
 265   return strcmp(_type, "uint64_t") == 0;
 266 }
 267 
 268 uint64_t Flag::get_uint64_t() const {
 269   return *((uint64_t*) _addr);
 270 }
 271 
 272 Flag::Error Flag::set_uint64_t(uint64_t value) {
 273   Flag::Error error = check_writable(value!=get_uint64_t());
 274   if (error == Flag::SUCCESS) {
 275     *((uint64_t*) _addr) = value;
 276   }
 277   return error;
 278 }
 279 
 280 bool Flag::is_size_t() const {
 281   return strcmp(_type, "size_t") == 0;
 282 }
 283 
 284 size_t Flag::get_size_t() const {
 285   return *((size_t*) _addr);
 286 }
 287 
 288 Flag::Error Flag::set_size_t(size_t value) {
 289   Flag::Error error = check_writable(value!=get_size_t());
 290   if (error == Flag::SUCCESS) {
 291     *((size_t*) _addr) = value;
 292   }
 293   return error;
 294 }
 295 
 296 bool Flag::is_double() const {
 297   return strcmp(_type, "double") == 0;
 298 }
 299 
 300 double Flag::get_double() const {
 301   return *((double*) _addr);
 302 }
 303 
 304 Flag::Error Flag::set_double(double value) {
 305   Flag::Error error = check_writable(value!=get_double());
 306   if (error == Flag::SUCCESS) {
 307     *((double*) _addr) = value;
 308   }
 309   return error;
 310 }
 311 
 312 bool Flag::is_ccstr() const {
 313   return strcmp(_type, "ccstr") == 0 || strcmp(_type, "ccstrlist") == 0;
 314 }
 315 
 316 bool Flag::ccstr_accumulates() const {
 317   return strcmp(_type, "ccstrlist") == 0;
 318 }
 319 
 320 ccstr Flag::get_ccstr() const {
 321   return *((ccstr*) _addr);
 322 }
 323 
 324 Flag::Error Flag::set_ccstr(ccstr value) {
 325   Flag::Error error = check_writable(value!=get_ccstr());
 326   if (error == Flag::SUCCESS) {
 327     *((ccstr*) _addr) = value;
 328   }
 329   return error;
 330 }
 331 
 332 
 333 Flag::Flags Flag::get_origin() {
 334   return Flags(_flags & VALUE_ORIGIN_MASK);
 335 }
 336 
 337 void Flag::set_origin(Flags origin) {
 338   assert((origin & VALUE_ORIGIN_MASK) == origin, "sanity");
 339   Flags new_origin = Flags((origin == COMMAND_LINE) ? Flags(origin | ORIG_COMMAND_LINE) : origin);
 340   _flags = Flags((_flags & ~VALUE_ORIGIN_MASK) | new_origin);
 341 }
 342 
 343 bool Flag::is_default() {
 344   return (get_origin() == DEFAULT);
 345 }
 346 
 347 bool Flag::is_ergonomic() {
 348   return (get_origin() == ERGONOMIC);
 349 }
 350 
 351 bool Flag::is_command_line() {
 352   return (_flags & ORIG_COMMAND_LINE) != 0;
 353 }
 354 
 355 void Flag::set_command_line() {
 356   _flags = Flags(_flags | ORIG_COMMAND_LINE);
 357 }
 358 
 359 bool Flag::is_product() const {
 360   return (_flags & KIND_PRODUCT) != 0;
 361 }
 362 
 363 bool Flag::is_manageable() const {
 364   return (_flags & KIND_MANAGEABLE) != 0;
 365 }
 366 
 367 bool Flag::is_diagnostic() const {
 368   return (_flags & KIND_DIAGNOSTIC) != 0;
 369 }
 370 
 371 bool Flag::is_experimental() const {
 372   return (_flags & KIND_EXPERIMENTAL) != 0;
 373 }
 374 
 375 bool Flag::is_notproduct() const {
 376   return (_flags & KIND_NOT_PRODUCT) != 0;
 377 }
 378 
 379 bool Flag::is_develop() const {
 380   return (_flags & KIND_DEVELOP) != 0;
 381 }
 382 
 383 bool Flag::is_read_write() const {
 384   return (_flags & KIND_READ_WRITE) != 0;
 385 }
 386 
 387 bool Flag::is_commercial() const {
 388   return (_flags & KIND_COMMERCIAL) != 0;
 389 }
 390 
 391 /**
 392  * Returns if this flag is a constant in the binary.  Right now this is
 393  * true for notproduct and develop flags in product builds.
 394  */
 395 bool Flag::is_constant_in_binary() const {
 396 #ifdef PRODUCT
 397     return is_notproduct() || is_develop();
 398 #else
 399     return false;
 400 #endif
 401 }
 402 
 403 bool Flag::is_unlocker() const {
 404   return strcmp(_name, "UnlockDiagnosticVMOptions") == 0     ||
 405          strcmp(_name, "UnlockExperimentalVMOptions") == 0   ||
 406          is_unlocker_ext();
 407 }
 408 
 409 bool Flag::is_unlocked() const {
 410   if (is_diagnostic()) {
 411     return UnlockDiagnosticVMOptions;
 412   }
 413   if (is_experimental()) {
 414     return UnlockExperimentalVMOptions;
 415   }
 416   return is_unlocked_ext();
 417 }
 418 
 419 void Flag::clear_diagnostic() {
 420   assert(is_diagnostic(), "sanity");
 421   _flags = Flags(_flags & ~KIND_DIAGNOSTIC);
 422   assert(!is_diagnostic(), "sanity");
 423 }
 424 
 425 // Get custom message for this locked flag, or NULL if
 426 // none is available. Returns message type produced.
 427 Flag::MsgType Flag::get_locked_message(char* buf, int buflen) const {
 428   buf[0] = '\0';
 429   if (is_diagnostic() && !is_unlocked()) {
 430     jio_snprintf(buf, buflen,
 431                  "Error: VM option '%s' is diagnostic and must be enabled via -XX:+UnlockDiagnosticVMOptions.\n"
 432                  "Error: The unlock option must precede '%s'.\n",
 433                  _name, _name);
 434     return Flag::DIAGNOSTIC_FLAG_BUT_LOCKED;
 435   }
 436   if (is_experimental() && !is_unlocked()) {
 437     jio_snprintf(buf, buflen,
 438                  "Error: VM option '%s' is experimental and must be enabled via -XX:+UnlockExperimentalVMOptions.\n"
 439                  "Error: The unlock option must precede '%s'.\n",
 440                  _name, _name);
 441     return Flag::EXPERIMENTAL_FLAG_BUT_LOCKED;
 442   }
 443   if (is_develop() && is_product_build()) {
 444     jio_snprintf(buf, buflen, "Error: VM option '%s' is develop and is available only in debug version of VM.\n",
 445                  _name);
 446     return Flag::DEVELOPER_FLAG_BUT_PRODUCT_BUILD;
 447   }
 448   if (is_notproduct() && is_product_build()) {
 449     jio_snprintf(buf, buflen, "Error: VM option '%s' is notproduct and is available only in debug version of VM.\n",
 450                  _name);
 451     return Flag::NOTPRODUCT_FLAG_BUT_PRODUCT_BUILD;
 452   }
 453   get_locked_message_ext(buf, buflen);
 454   return Flag::NONE;
 455 }
 456 
 457 bool Flag::is_writeable() const {
 458   return is_manageable() || (is_product() && is_read_write()) || is_writeable_ext();
 459 }
 460 
 461 // All flags except "manageable" are assumed to be internal flags.
 462 // Long term, we need to define a mechanism to specify which flags
 463 // are external/stable and change this function accordingly.
 464 bool Flag::is_external() const {
 465   return is_manageable() || is_external_ext();
 466 }
 467 
 468 void Flag::print_on(outputStream* st, bool withComments, bool printRanges) {
 469   // Don't print notproduct and develop flags in a product build.
 470   if (is_constant_in_binary()) {
 471     return;
 472   }
 473 
 474   if (!printRanges) {
 475     // Use some named constants to make code more readable.
 476     const unsigned int nSpaces    = 10;
 477     const unsigned int maxFlagLen = 40 + nSpaces;
 478 
 479     // The print below assumes that the flag name is 40 characters or less.
 480     // This works for most flags, but there are exceptions. Our longest flag
 481     // name right now is UseAdaptiveGenerationSizePolicyAtMajorCollection and
 482     // its minor collection buddy. These are 48 characters. We use a buffer of
 483     // nSpaces spaces below to adjust the space between the flag value and the
 484     // column of flag type and origin that is printed in the end of the line.
 485     char spaces[nSpaces + 1] = "          ";
 486     st->print("%9s %-*s = ", _type, maxFlagLen-nSpaces, _name);
 487 
 488     if (is_bool()) {
 489       st->print("%-20s", get_bool() ? "true" : "false");
 490     } else if (is_int()) {
 491       st->print("%-20d", get_int());
 492     } else if (is_uint()) {
 493       st->print("%-20u", get_uint());
 494     } else if (is_intx()) {
 495       st->print(INTX_FORMAT_W(-20), get_intx());
 496     } else if (is_uintx()) {
 497       st->print(UINTX_FORMAT_W(-20), get_uintx());
 498     } else if (is_uint64_t()) {
 499       st->print(UINT64_FORMAT_W(-20), get_uint64_t());
 500     } else if (is_size_t()) {
 501       st->print(SIZE_FORMAT_W(-20), get_size_t());
 502     } else if (is_double()) {
 503       st->print("%-20f", get_double());
 504     } else if (is_ccstr()) {
 505       const char* cp = get_ccstr();
 506       if (cp != NULL) {
 507         const char* eol;
 508         while ((eol = strchr(cp, '\n')) != NULL) {
 509           size_t llen = pointer_delta(eol, cp, sizeof(char));
 510           st->print("%.*s", (int)llen, cp);
 511           st->cr();
 512           cp = eol+1;
 513           st->print("%5s %-35s += ", "", _name);
 514         }
 515         st->print("%-20s", cp);
 516       }
 517       else st->print("%-20s", "");
 518     }
 519     // Make sure we do not punch a '\0' at a negative char array index.
 520     unsigned int nameLen = (unsigned int)strlen(_name);
 521     if (nameLen <= maxFlagLen) {
 522       spaces[maxFlagLen - MAX2(maxFlagLen-nSpaces, nameLen)] = '\0';
 523       st->print("%s", spaces);
 524     }
 525     print_kind_and_origin(st);
 526 
 527 #ifndef PRODUCT
 528     if (withComments) {
 529       st->print("%s", _doc);
 530     }
 531 #endif
 532 
 533     st->cr();
 534 
 535   } else if (!is_bool() && !is_ccstr()) {
 536     st->print("%9s %-50s ", _type, _name);
 537 
 538     RangeStrFunc func = NULL;
 539     if (is_int()) {
 540       func = Flag::get_int_default_range_str;
 541     } else if (is_uint()) {
 542       func = Flag::get_uint_default_range_str;
 543     } else if (is_intx()) {
 544       func = Flag::get_intx_default_range_str;
 545     } else if (is_uintx()) {
 546       func = Flag::get_uintx_default_range_str;
 547     } else if (is_uint64_t()) {
 548       func = Flag::get_uint64_t_default_range_str;
 549     } else if (is_size_t()) {
 550       func = Flag::get_size_t_default_range_str;
 551     } else if (is_double()) {
 552       func = Flag::get_double_default_range_str;
 553     } else {
 554       ShouldNotReachHere();
 555     }
 556     CommandLineFlagRangeList::print(st, _name, func);
 557 
 558     st->print(" %-16s", " ");
 559     print_kind_and_origin(st);
 560 
 561 #ifndef PRODUCT
 562     if (withComments) {
 563       st->print("%s", _doc);
 564     }
 565 #endif
 566 
 567     st->cr();
 568   }
 569 }
 570 
 571 void Flag::print_kind_and_origin(outputStream* st) {
 572   struct Data {
 573     int flag;
 574     const char* name;
 575   };
 576 
 577   Data data[] = {
 578       { KIND_JVMCI, "JVMCI" },
 579       { KIND_C1, "C1" },
 580       { KIND_C2, "C2" },
 581       { KIND_ARCH, "ARCH" },
 582       { KIND_PLATFORM_DEPENDENT, "pd" },
 583       { KIND_PRODUCT, "product" },
 584       { KIND_MANAGEABLE, "manageable" },
 585       { KIND_DIAGNOSTIC, "diagnostic" },
 586       { KIND_EXPERIMENTAL, "experimental" },
 587       { KIND_COMMERCIAL, "commercial" },
 588       { KIND_NOT_PRODUCT, "notproduct" },
 589       { KIND_DEVELOP, "develop" },
 590       { KIND_LP64_PRODUCT, "lp64_product" },
 591       { KIND_READ_WRITE, "rw" },
 592       { -1, "" }
 593   };
 594 
 595   if ((_flags & KIND_MASK) != 0) {
 596     bool is_first = true;
 597     const size_t buffer_size = 64;
 598     size_t buffer_used = 0;
 599     char kind[buffer_size];
 600 
 601     jio_snprintf(kind, buffer_size, "{");
 602     buffer_used++;
 603     for (int i = 0; data[i].flag != -1; i++) {
 604       Data d = data[i];
 605       if ((_flags & d.flag) != 0) {
 606         if (is_first) {
 607           is_first = false;
 608         } else {
 609           assert(buffer_used + 1 < buffer_size, "Too small buffer");
 610           jio_snprintf(kind + buffer_used, buffer_size - buffer_used, " ");
 611           buffer_used++;
 612         }
 613         size_t length = strlen(d.name);
 614         assert(buffer_used + length < buffer_size, "Too small buffer");
 615         jio_snprintf(kind + buffer_used, buffer_size - buffer_used, "%s", d.name);
 616         buffer_used += length;
 617       }
 618     }
 619     assert(buffer_used + 2 <= buffer_size, "Too small buffer");
 620     jio_snprintf(kind + buffer_used, buffer_size - buffer_used, "}");
 621     st->print("%20s", kind);
 622   }
 623 
 624   int origin = _flags & VALUE_ORIGIN_MASK;
 625   st->print(" {");
 626   switch(origin) {
 627     case DEFAULT:
 628       st->print("default"); break;
 629     case COMMAND_LINE:
 630       st->print("command line"); break;
 631     case ENVIRON_VAR:
 632       st->print("environment"); break;
 633     case CONFIG_FILE:
 634       st->print("config file"); break;
 635     case MANAGEMENT:
 636       st->print("management"); break;
 637     case ERGONOMIC:
 638       if (_flags & ORIG_COMMAND_LINE) {
 639         st->print("command line, ");
 640       }
 641       st->print("ergonomic"); break;
 642     case ATTACH_ON_DEMAND:
 643       st->print("attach"); break;
 644     case INTERNAL:
 645       st->print("internal"); break;
 646   }
 647   st->print("}");
 648 }
 649 
 650 void Flag::print_as_flag(outputStream* st) {
 651   if (is_bool()) {
 652     st->print("-XX:%s%s", get_bool() ? "+" : "-", _name);
 653   } else if (is_int()) {
 654     st->print("-XX:%s=%d", _name, get_int());
 655   } else if (is_uint()) {
 656     st->print("-XX:%s=%u", _name, get_uint());
 657   } else if (is_intx()) {
 658     st->print("-XX:%s=" INTX_FORMAT, _name, get_intx());
 659   } else if (is_uintx()) {
 660     st->print("-XX:%s=" UINTX_FORMAT, _name, get_uintx());
 661   } else if (is_uint64_t()) {
 662     st->print("-XX:%s=" UINT64_FORMAT, _name, get_uint64_t());
 663   } else if (is_size_t()) {
 664     st->print("-XX:%s=" SIZE_FORMAT, _name, get_size_t());
 665   } else if (is_double()) {
 666     st->print("-XX:%s=%f", _name, get_double());
 667   } else if (is_ccstr()) {
 668     st->print("-XX:%s=", _name);
 669     const char* cp = get_ccstr();
 670     if (cp != NULL) {
 671       // Need to turn embedded '\n's back into separate arguments
 672       // Not so efficient to print one character at a time,
 673       // but the choice is to do the transformation to a buffer
 674       // and print that.  And this need not be efficient.
 675       for (; *cp != '\0'; cp += 1) {
 676         switch (*cp) {
 677           default:
 678             st->print("%c", *cp);
 679             break;
 680           case '\n':
 681             st->print(" -XX:%s=", _name);
 682             break;
 683         }
 684       }
 685     }
 686   } else {
 687     ShouldNotReachHere();
 688   }
 689 }
 690 
 691 const char* Flag::flag_error_str(Flag::Error error) {
 692   switch (error) {
 693     case Flag::MISSING_NAME: return "MISSING_NAME";
 694     case Flag::MISSING_VALUE: return "MISSING_VALUE";
 695     case Flag::NON_WRITABLE: return "NON_WRITABLE";
 696     case Flag::OUT_OF_BOUNDS: return "OUT_OF_BOUNDS";
 697     case Flag::VIOLATES_CONSTRAINT: return "VIOLATES_CONSTRAINT";
 698     case Flag::INVALID_FLAG: return "INVALID_FLAG";
 699     case Flag::ERR_OTHER: return "ERR_OTHER";
 700     case Flag::SUCCESS: return "SUCCESS";
 701     default: ShouldNotReachHere(); return "NULL";
 702   }
 703 }
 704 
 705 // 4991491 do not "optimize out" the was_set false values: omitting them
 706 // tickles a Microsoft compiler bug causing flagTable to be malformed
 707 
 708 #define RUNTIME_PRODUCT_FLAG_STRUCT(     type, name, value, doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_PRODUCT) },
 709 #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) },
 710 #define RUNTIME_DIAGNOSTIC_FLAG_STRUCT(  type, name, value, doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_DIAGNOSTIC) },
 711 #define RUNTIME_PD_DIAGNOSTIC_FLAG_STRUCT(type, name,       doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_DIAGNOSTIC | Flag::KIND_PLATFORM_DEPENDENT) },
 712 #define RUNTIME_EXPERIMENTAL_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_EXPERIMENTAL) },
 713 #define RUNTIME_MANAGEABLE_FLAG_STRUCT(  type, name, value, doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_MANAGEABLE) },
 714 #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) },
 715 #define RUNTIME_DEVELOP_FLAG_STRUCT(     type, name, value, doc) { #type, XSTR(name), (void*) &name, NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_DEVELOP) },
 716 #define RUNTIME_PD_DEVELOP_FLAG_STRUCT(  type, name,        doc) { #type, XSTR(name), (void*) &name, NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_DEVELOP | Flag::KIND_PLATFORM_DEPENDENT) },
 717 #define RUNTIME_NOTPRODUCT_FLAG_STRUCT(  type, name, value, doc) { #type, XSTR(name), (void*) &name, NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_NOT_PRODUCT) },
 718 
 719 #if INCLUDE_VBC
 720 #define VBC_PRODUCT_FLAG_STRUCT(         type, name, value, doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_PRODUCT) },
 721 #endif
 722 
 723 #define JVMCI_PRODUCT_FLAG_STRUCT(       type, name, value, doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_JVMCI | Flag::KIND_PRODUCT) },
 724 #define JVMCI_PD_PRODUCT_FLAG_STRUCT(    type, name,        doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_JVMCI | Flag::KIND_PRODUCT | Flag::KIND_PLATFORM_DEPENDENT) },
 725 #define JVMCI_DIAGNOSTIC_FLAG_STRUCT(    type, name, value, doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_JVMCI | Flag::KIND_DIAGNOSTIC) },
 726 #define JVMCI_PD_DIAGNOSTIC_FLAG_STRUCT( type, name,        doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_JVMCI | Flag::KIND_DIAGNOSTIC | Flag::KIND_PLATFORM_DEPENDENT) },
 727 #define JVMCI_EXPERIMENTAL_FLAG_STRUCT(  type, name, value, doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_JVMCI | Flag::KIND_EXPERIMENTAL) },
 728 #define JVMCI_DEVELOP_FLAG_STRUCT(       type, name, value, doc) { #type, XSTR(name), (void*) &name, NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_JVMCI | Flag::KIND_DEVELOP) },
 729 #define JVMCI_PD_DEVELOP_FLAG_STRUCT(    type, name,        doc) { #type, XSTR(name), (void*) &name, NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_JVMCI | Flag::KIND_DEVELOP | Flag::KIND_PLATFORM_DEPENDENT) },
 730 #define JVMCI_NOTPRODUCT_FLAG_STRUCT(    type, name, value, doc) { #type, XSTR(name), (void*) &name, NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_JVMCI | Flag::KIND_NOT_PRODUCT) },
 731 
 732 #ifdef _LP64
 733 #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) },
 734 #else
 735 #define RUNTIME_LP64_PRODUCT_FLAG_STRUCT(type, name, value, doc) /* flag is constant */
 736 #endif // _LP64
 737 
 738 #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) },
 739 #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) },
 740 #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) },
 741 #define C1_PD_DIAGNOSTIC_FLAG_STRUCT(    type, name,        doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C1 | Flag::KIND_DIAGNOSTIC | Flag::KIND_PLATFORM_DEPENDENT) },
 742 #define C1_DEVELOP_FLAG_STRUCT(          type, name, value, doc) { #type, XSTR(name), (void*) &name, NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C1 | Flag::KIND_DEVELOP) },
 743 #define C1_PD_DEVELOP_FLAG_STRUCT(       type, name,        doc) { #type, XSTR(name), (void*) &name, NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C1 | Flag::KIND_DEVELOP | Flag::KIND_PLATFORM_DEPENDENT) },
 744 #define C1_NOTPRODUCT_FLAG_STRUCT(       type, name, value, doc) { #type, XSTR(name), (void*) &name, NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C1 | Flag::KIND_NOT_PRODUCT) },
 745 
 746 #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) },
 747 #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) },
 748 #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) },
 749 #define C2_PD_DIAGNOSTIC_FLAG_STRUCT(    type, name,        doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C2 | Flag::KIND_DIAGNOSTIC | Flag::KIND_PLATFORM_DEPENDENT) },
 750 #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) },
 751 #define C2_DEVELOP_FLAG_STRUCT(          type, name, value, doc) { #type, XSTR(name), (void*) &name, NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C2 | Flag::KIND_DEVELOP) },
 752 #define C2_PD_DEVELOP_FLAG_STRUCT(       type, name,        doc) { #type, XSTR(name), (void*) &name, NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C2 | Flag::KIND_DEVELOP | Flag::KIND_PLATFORM_DEPENDENT) },
 753 #define C2_NOTPRODUCT_FLAG_STRUCT(       type, name, value, doc) { #type, XSTR(name), (void*) &name, NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_C2 | Flag::KIND_NOT_PRODUCT) },
 754 
 755 #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) },
 756 #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) },
 757 #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) },
 758 #define ARCH_DEVELOP_FLAG_STRUCT(        type, name, value, doc) { #type, XSTR(name), (void*) &name, NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_ARCH | Flag::KIND_DEVELOP) },
 759 #define ARCH_NOTPRODUCT_FLAG_STRUCT(     type, name, value, doc) { #type, XSTR(name), (void*) &name, NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_ARCH | Flag::KIND_NOT_PRODUCT) },
 760 
 761 static Flag flagTable[] = {
 762  RUNTIME_FLAGS(RUNTIME_DEVELOP_FLAG_STRUCT, \
 763                RUNTIME_PD_DEVELOP_FLAG_STRUCT, \
 764                RUNTIME_PRODUCT_FLAG_STRUCT, \
 765                RUNTIME_PD_PRODUCT_FLAG_STRUCT, \
 766                RUNTIME_DIAGNOSTIC_FLAG_STRUCT, \
 767                RUNTIME_PD_DIAGNOSTIC_FLAG_STRUCT, \
 768                RUNTIME_EXPERIMENTAL_FLAG_STRUCT, \
 769                RUNTIME_NOTPRODUCT_FLAG_STRUCT, \
 770                RUNTIME_MANAGEABLE_FLAG_STRUCT, \
 771                RUNTIME_PRODUCT_RW_FLAG_STRUCT, \
 772                RUNTIME_LP64_PRODUCT_FLAG_STRUCT, \
 773                IGNORE_RANGE, \
 774                IGNORE_CONSTRAINT, \
 775                IGNORE_WRITEABLE)
 776  RUNTIME_OS_FLAGS(RUNTIME_DEVELOP_FLAG_STRUCT, \
 777                   RUNTIME_PD_DEVELOP_FLAG_STRUCT, \
 778                   RUNTIME_PRODUCT_FLAG_STRUCT, \
 779                   RUNTIME_PD_PRODUCT_FLAG_STRUCT, \
 780                   RUNTIME_DIAGNOSTIC_FLAG_STRUCT, \
 781                   RUNTIME_PD_DIAGNOSTIC_FLAG_STRUCT, \
 782                   RUNTIME_NOTPRODUCT_FLAG_STRUCT, \
 783                   IGNORE_RANGE, \
 784                   IGNORE_CONSTRAINT, \
 785                   IGNORE_WRITEABLE)
 786 #if INCLUDE_ALL_GCS
 787  G1_FLAGS(RUNTIME_DEVELOP_FLAG_STRUCT, \
 788           RUNTIME_PD_DEVELOP_FLAG_STRUCT, \
 789           RUNTIME_PRODUCT_FLAG_STRUCT, \
 790           RUNTIME_PD_PRODUCT_FLAG_STRUCT, \
 791           RUNTIME_DIAGNOSTIC_FLAG_STRUCT, \
 792           RUNTIME_PD_DIAGNOSTIC_FLAG_STRUCT, \
 793           RUNTIME_EXPERIMENTAL_FLAG_STRUCT, \
 794           RUNTIME_NOTPRODUCT_FLAG_STRUCT, \
 795           RUNTIME_MANAGEABLE_FLAG_STRUCT, \
 796           RUNTIME_PRODUCT_RW_FLAG_STRUCT, \
 797           IGNORE_RANGE, \
 798           IGNORE_CONSTRAINT, \
 799           IGNORE_WRITEABLE)
 800 #endif // INCLUDE_ALL_GCS
 801 #if INCLUDE_VBC
 802   VALUE_BASED_CLASS_FLAGS(VBC_PRODUCT_FLAG_STRUCT)
 803 #endif
 804 #if INCLUDE_JVMCI
 805  JVMCI_FLAGS(JVMCI_DEVELOP_FLAG_STRUCT, \
 806              JVMCI_PD_DEVELOP_FLAG_STRUCT, \
 807              JVMCI_PRODUCT_FLAG_STRUCT, \
 808              JVMCI_PD_PRODUCT_FLAG_STRUCT, \
 809              JVMCI_DIAGNOSTIC_FLAG_STRUCT, \
 810              JVMCI_PD_DIAGNOSTIC_FLAG_STRUCT, \
 811              JVMCI_EXPERIMENTAL_FLAG_STRUCT, \
 812              JVMCI_NOTPRODUCT_FLAG_STRUCT, \
 813              IGNORE_RANGE, \
 814              IGNORE_CONSTRAINT, \
 815              IGNORE_WRITEABLE)
 816 #endif // INCLUDE_JVMCI
 817 #ifdef COMPILER1
 818  C1_FLAGS(C1_DEVELOP_FLAG_STRUCT, \
 819           C1_PD_DEVELOP_FLAG_STRUCT, \
 820           C1_PRODUCT_FLAG_STRUCT, \
 821           C1_PD_PRODUCT_FLAG_STRUCT, \
 822           C1_DIAGNOSTIC_FLAG_STRUCT, \
 823           C1_PD_DIAGNOSTIC_FLAG_STRUCT, \
 824           C1_NOTPRODUCT_FLAG_STRUCT, \
 825           IGNORE_RANGE, \
 826           IGNORE_CONSTRAINT, \
 827           IGNORE_WRITEABLE)
 828 #endif // COMPILER1
 829 #ifdef COMPILER2
 830  C2_FLAGS(C2_DEVELOP_FLAG_STRUCT, \
 831           C2_PD_DEVELOP_FLAG_STRUCT, \
 832           C2_PRODUCT_FLAG_STRUCT, \
 833           C2_PD_PRODUCT_FLAG_STRUCT, \
 834           C2_DIAGNOSTIC_FLAG_STRUCT, \
 835           C2_PD_DIAGNOSTIC_FLAG_STRUCT, \
 836           C2_EXPERIMENTAL_FLAG_STRUCT, \
 837           C2_NOTPRODUCT_FLAG_STRUCT, \
 838           IGNORE_RANGE, \
 839           IGNORE_CONSTRAINT, \
 840           IGNORE_WRITEABLE)
 841 #endif // COMPILER2
 842  ARCH_FLAGS(ARCH_DEVELOP_FLAG_STRUCT, \
 843             ARCH_PRODUCT_FLAG_STRUCT, \
 844             ARCH_DIAGNOSTIC_FLAG_STRUCT, \
 845             ARCH_EXPERIMENTAL_FLAG_STRUCT, \
 846             ARCH_NOTPRODUCT_FLAG_STRUCT, \
 847             IGNORE_RANGE, \
 848             IGNORE_CONSTRAINT, \
 849             IGNORE_WRITEABLE)
 850  FLAGTABLE_EXT
 851  {0, NULL, NULL}
 852 };
 853 
 854 Flag* Flag::flags = flagTable;
 855 size_t Flag::numFlags = (sizeof(flagTable) / sizeof(Flag));
 856 
 857 inline bool str_equal(const char* s, size_t s_len, const char* q, size_t q_len) {
 858   if (s_len != q_len) return false;
 859   return memcmp(s, q, q_len) == 0;
 860 }
 861 
 862 // Search the flag table for a named flag
 863 Flag* Flag::find_flag(const char* name, size_t length, bool allow_locked, bool return_flag) {
 864   for (Flag* current = &flagTable[0]; current->_name != NULL; current++) {
 865     if (str_equal(current->_name, current->get_name_length(), name, length)) {
 866       // Found a matching entry.
 867       // Don't report notproduct and develop flags in product builds.
 868       if (current->is_constant_in_binary()) {
 869         return (return_flag ? current : NULL);
 870       }
 871       // Report locked flags only if allowed.
 872       if (!(current->is_unlocked() || current->is_unlocker())) {
 873         if (!allow_locked) {
 874           // disable use of locked flags, e.g. diagnostic, experimental,
 875           // commercial... until they are explicitly unlocked
 876           return NULL;
 877         }
 878       }
 879       return current;
 880     }
 881   }
 882   // Flag name is not in the flag table
 883   return NULL;
 884 }
 885 
 886 // Get or compute the flag name length
 887 size_t Flag::get_name_length() {
 888   if (_name_len == 0) {
 889     _name_len = strlen(_name);
 890   }
 891   return _name_len;
 892 }
 893 
 894 // Compute string similarity based on Dice's coefficient
 895 static float str_similar(const char* str1, const char* str2, size_t len2) {
 896   int len1 = (int) strlen(str1);
 897   int total = len1 + (int) len2;
 898 
 899   int hit = 0;
 900 
 901   for (int i = 0; i < len1 -1; ++i) {
 902     for (int j = 0; j < (int) len2 -1; ++j) {
 903       if ((str1[i] == str2[j]) && (str1[i+1] == str2[j+1])) {
 904         ++hit;
 905         break;
 906       }
 907     }
 908   }
 909 
 910   return 2.0f * (float) hit / (float) total;
 911 }
 912 
 913 Flag* Flag::fuzzy_match(const char* name, size_t length, bool allow_locked) {
 914   float VMOptionsFuzzyMatchSimilarity = 0.7f;
 915   Flag* match = NULL;
 916   float score;
 917   float max_score = -1;
 918 
 919   for (Flag* current = &flagTable[0]; current->_name != NULL; current++) {
 920     score = str_similar(current->_name, name, length);
 921     if (score > max_score) {
 922       max_score = score;
 923       match = current;
 924     }
 925   }
 926 
 927   if (!(match->is_unlocked() || match->is_unlocker())) {
 928     if (!allow_locked) {
 929       return NULL;
 930     }
 931   }
 932 
 933   if (max_score < VMOptionsFuzzyMatchSimilarity) {
 934     return NULL;
 935   }
 936 
 937   return match;
 938 }
 939 
 940 // Returns the address of the index'th element
 941 static Flag* address_of_flag(CommandLineFlagWithType flag) {
 942   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 943   return &Flag::flags[flag];
 944 }
 945 
 946 bool CommandLineFlagsEx::is_default(CommandLineFlag flag) {
 947   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 948   Flag* f = &Flag::flags[flag];
 949   return f->is_default();
 950 }
 951 
 952 bool CommandLineFlagsEx::is_ergo(CommandLineFlag flag) {
 953   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 954   Flag* f = &Flag::flags[flag];
 955   return f->is_ergonomic();
 956 }
 957 
 958 bool CommandLineFlagsEx::is_cmdline(CommandLineFlag flag) {
 959   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 960   Flag* f = &Flag::flags[flag];
 961   return f->is_command_line();
 962 }
 963 
 964 bool CommandLineFlags::wasSetOnCmdline(const char* name, bool* value) {
 965   Flag* result = Flag::find_flag((char*)name, strlen(name));
 966   if (result == NULL) return false;
 967   *value = result->is_command_line();
 968   return true;
 969 }
 970 
 971 void CommandLineFlagsEx::setOnCmdLine(CommandLineFlagWithType flag) {
 972   Flag* faddr = address_of_flag(flag);
 973   assert(faddr != NULL, "Unknown flag");
 974   faddr->set_command_line();
 975 }
 976 
 977 template<class E, class T>
 978 static void trace_flag_changed(const char* name, const T old_value, const T new_value, const Flag::Flags origin) {
 979   E e;
 980   e.set_name(name);
 981   e.set_oldValue(old_value);
 982   e.set_newValue(new_value);
 983   e.set_origin(origin);
 984   e.commit();
 985 }
 986 
 987 static Flag::Error apply_constraint_and_check_range_bool(const char* name, bool new_value, bool verbose) {
 988   Flag::Error status = Flag::SUCCESS;
 989   CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
 990   if (constraint != NULL) {
 991     status = constraint->apply_bool(new_value, verbose);
 992   }
 993   return status;
 994 }
 995 
 996 Flag::Error CommandLineFlags::boolAt(const char* name, size_t len, bool* value, bool allow_locked, bool return_flag) {
 997   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
 998   if (result == NULL) return Flag::INVALID_FLAG;
 999   if (!result->is_bool()) return Flag::WRONG_FORMAT;
1000   *value = result->get_bool();
1001   return Flag::SUCCESS;
1002 }
1003 
1004 Flag::Error CommandLineFlags::boolAtPut(Flag* flag, bool* value, Flag::Flags origin) {
1005   const char* name;
1006   if (flag == NULL) return Flag::INVALID_FLAG;
1007   if (!flag->is_bool()) return Flag::WRONG_FORMAT;
1008   name = flag->_name;
1009   Flag::Error check = apply_constraint_and_check_range_bool(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1010   if (check != Flag::SUCCESS) return check;
1011   bool old_value = flag->get_bool();
1012   trace_flag_changed<EventBooleanFlagChanged, bool>(name, old_value, *value, origin);
1013   check = flag->set_bool(*value);
1014   *value = old_value;
1015   flag->set_origin(origin);
1016   return check;
1017 }
1018 
1019 Flag::Error CommandLineFlags::boolAtPut(const char* name, size_t len, bool* value, Flag::Flags origin) {
1020   Flag* result = Flag::find_flag(name, len);
1021   return boolAtPut(result, value, origin);
1022 }
1023 
1024 Flag::Error CommandLineFlagsEx::boolAtPut(CommandLineFlagWithType flag, bool value, Flag::Flags origin) {
1025   Flag* faddr = address_of_flag(flag);
1026   guarantee(faddr != NULL && faddr->is_bool(), "wrong flag type");
1027   return CommandLineFlags::boolAtPut(faddr, &value, origin);
1028 }
1029 
1030 static Flag::Error apply_constraint_and_check_range_int(const char* name, int new_value, bool verbose) {
1031   Flag::Error status = Flag::SUCCESS;
1032   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1033   if (range != NULL) {
1034     status = range->check_int(new_value, verbose);
1035   }
1036   if (status == Flag::SUCCESS) {
1037     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1038     if (constraint != NULL) {
1039       status = constraint->apply_int(new_value, verbose);
1040     }
1041   }
1042   return status;
1043 }
1044 
1045 Flag::Error CommandLineFlags::intAt(const char* name, size_t len, int* value, bool allow_locked, bool return_flag) {
1046   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1047   if (result == NULL) return Flag::INVALID_FLAG;
1048   if (!result->is_int()) return Flag::WRONG_FORMAT;
1049   *value = result->get_int();
1050   return Flag::SUCCESS;
1051 }
1052 
1053 Flag::Error CommandLineFlags::intAtPut(Flag* flag, int* value, Flag::Flags origin) {
1054   const char* name;
1055   if (flag == NULL) return Flag::INVALID_FLAG;
1056   if (!flag->is_int()) return Flag::WRONG_FORMAT;
1057   name = flag->_name;
1058   Flag::Error check = apply_constraint_and_check_range_int(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1059   if (check != Flag::SUCCESS) return check;
1060   int old_value = flag->get_int();
1061   trace_flag_changed<EventIntFlagChanged, s4>(name, old_value, *value, origin);
1062   check = flag->set_int(*value);
1063   *value = old_value;
1064   flag->set_origin(origin);
1065   return check;
1066 }
1067 
1068 Flag::Error CommandLineFlags::intAtPut(const char* name, size_t len, int* value, Flag::Flags origin) {
1069   Flag* result = Flag::find_flag(name, len);
1070   return intAtPut(result, value, origin);
1071 }
1072 
1073 Flag::Error CommandLineFlagsEx::intAtPut(CommandLineFlagWithType flag, int value, Flag::Flags origin) {
1074   Flag* faddr = address_of_flag(flag);
1075   guarantee(faddr != NULL && faddr->is_int(), "wrong flag type");
1076   return CommandLineFlags::intAtPut(faddr, &value, origin);
1077 }
1078 
1079 static Flag::Error apply_constraint_and_check_range_uint(const char* name, uint new_value, bool verbose) {
1080   Flag::Error status = Flag::SUCCESS;
1081   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1082   if (range != NULL) {
1083     status = range->check_uint(new_value, verbose);
1084   }
1085   if (status == Flag::SUCCESS) {
1086     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1087     if (constraint != NULL) {
1088       status = constraint->apply_uint(new_value, verbose);
1089     }
1090   }
1091   return status;
1092 }
1093 
1094 Flag::Error CommandLineFlags::uintAt(const char* name, size_t len, uint* value, bool allow_locked, bool return_flag) {
1095   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1096   if (result == NULL) return Flag::INVALID_FLAG;
1097   if (!result->is_uint()) return Flag::WRONG_FORMAT;
1098   *value = result->get_uint();
1099   return Flag::SUCCESS;
1100 }
1101 
1102 Flag::Error CommandLineFlags::uintAtPut(Flag* flag, uint* value, Flag::Flags origin) {
1103   const char* name;
1104   if (flag == NULL) return Flag::INVALID_FLAG;
1105   if (!flag->is_uint()) return Flag::WRONG_FORMAT;
1106   name = flag->_name;
1107   Flag::Error check = apply_constraint_and_check_range_uint(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1108   if (check != Flag::SUCCESS) return check;
1109   uint old_value = flag->get_uint();
1110   trace_flag_changed<EventUnsignedIntFlagChanged, u4>(name, old_value, *value, origin);
1111   check = flag->set_uint(*value);
1112   *value = old_value;
1113   flag->set_origin(origin);
1114   return check;
1115 }
1116 
1117 Flag::Error CommandLineFlags::uintAtPut(const char* name, size_t len, uint* value, Flag::Flags origin) {
1118   Flag* result = Flag::find_flag(name, len);
1119   return uintAtPut(result, value, origin);
1120 }
1121 
1122 Flag::Error CommandLineFlagsEx::uintAtPut(CommandLineFlagWithType flag, uint value, Flag::Flags origin) {
1123   Flag* faddr = address_of_flag(flag);
1124   guarantee(faddr != NULL && faddr->is_uint(), "wrong flag type");
1125   return CommandLineFlags::uintAtPut(faddr, &value, origin);
1126 }
1127 
1128 Flag::Error CommandLineFlags::intxAt(const char* name, size_t len, intx* value, bool allow_locked, bool return_flag) {
1129   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1130   if (result == NULL) return Flag::INVALID_FLAG;
1131   if (!result->is_intx()) return Flag::WRONG_FORMAT;
1132   *value = result->get_intx();
1133   return Flag::SUCCESS;
1134 }
1135 
1136 static Flag::Error apply_constraint_and_check_range_intx(const char* name, intx new_value, bool verbose) {
1137   Flag::Error status = Flag::SUCCESS;
1138   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1139   if (range != NULL) {
1140     status = range->check_intx(new_value, verbose);
1141   }
1142   if (status == Flag::SUCCESS) {
1143     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1144     if (constraint != NULL) {
1145       status = constraint->apply_intx(new_value, verbose);
1146     }
1147   }
1148   return status;
1149 }
1150 
1151 Flag::Error CommandLineFlags::intxAtPut(Flag* flag, intx* value, Flag::Flags origin) {
1152   const char* name;
1153   if (flag == NULL) return Flag::INVALID_FLAG;
1154   if (!flag->is_intx()) return Flag::WRONG_FORMAT;
1155   name = flag->_name;
1156   Flag::Error check = apply_constraint_and_check_range_intx(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1157   if (check != Flag::SUCCESS) return check;
1158   intx old_value = flag->get_intx();
1159   trace_flag_changed<EventLongFlagChanged, intx>(name, old_value, *value, origin);
1160   check = flag->set_intx(*value);
1161   *value = old_value;
1162   flag->set_origin(origin);
1163   return check;
1164 }
1165 
1166 Flag::Error CommandLineFlags::intxAtPut(const char* name, size_t len, intx* value, Flag::Flags origin) {
1167   Flag* result = Flag::find_flag(name, len);
1168   return intxAtPut(result, value, origin);
1169 }
1170 
1171 Flag::Error CommandLineFlagsEx::intxAtPut(CommandLineFlagWithType flag, intx value, Flag::Flags origin) {
1172   Flag* faddr = address_of_flag(flag);
1173   guarantee(faddr != NULL && faddr->is_intx(), "wrong flag type");
1174   return CommandLineFlags::intxAtPut(faddr, &value, origin);
1175 }
1176 
1177 Flag::Error CommandLineFlags::uintxAt(const char* name, size_t len, uintx* value, bool allow_locked, bool return_flag) {
1178   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1179   if (result == NULL) return Flag::INVALID_FLAG;
1180   if (!result->is_uintx()) return Flag::WRONG_FORMAT;
1181   *value = result->get_uintx();
1182   return Flag::SUCCESS;
1183 }
1184 
1185 static Flag::Error apply_constraint_and_check_range_uintx(const char* name, uintx new_value, bool verbose) {
1186   Flag::Error status = Flag::SUCCESS;
1187   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1188   if (range != NULL) {
1189     status = range->check_uintx(new_value, verbose);
1190   }
1191   if (status == Flag::SUCCESS) {
1192     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1193     if (constraint != NULL) {
1194       status = constraint->apply_uintx(new_value, verbose);
1195     }
1196   }
1197   return status;
1198 }
1199 
1200 Flag::Error CommandLineFlags::uintxAtPut(Flag* flag, uintx* value, Flag::Flags origin) {
1201   const char* name;
1202   if (flag == NULL) return Flag::INVALID_FLAG;
1203   if (!flag->is_uintx()) return Flag::WRONG_FORMAT;
1204   name = flag->_name;
1205   Flag::Error check = apply_constraint_and_check_range_uintx(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1206   if (check != Flag::SUCCESS) return check;
1207   uintx old_value = flag->get_uintx();
1208   trace_flag_changed<EventUnsignedLongFlagChanged, u8>(name, old_value, *value, origin);
1209   check = flag->set_uintx(*value);
1210   *value = old_value;
1211   flag->set_origin(origin);
1212   return check;
1213 }
1214 
1215 Flag::Error CommandLineFlags::uintxAtPut(const char* name, size_t len, uintx* value, Flag::Flags origin) {
1216   Flag* result = Flag::find_flag(name, len);
1217   return uintxAtPut(result, value, origin);
1218 }
1219 
1220 Flag::Error CommandLineFlagsEx::uintxAtPut(CommandLineFlagWithType flag, uintx value, Flag::Flags origin) {
1221   Flag* faddr = address_of_flag(flag);
1222   guarantee(faddr != NULL && faddr->is_uintx(), "wrong flag type");
1223   return CommandLineFlags::uintxAtPut(faddr, &value, origin);
1224 }
1225 
1226 Flag::Error CommandLineFlags::uint64_tAt(const char* name, size_t len, uint64_t* value, bool allow_locked, bool return_flag) {
1227   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1228   if (result == NULL) return Flag::INVALID_FLAG;
1229   if (!result->is_uint64_t()) return Flag::WRONG_FORMAT;
1230   *value = result->get_uint64_t();
1231   return Flag::SUCCESS;
1232 }
1233 
1234 static Flag::Error apply_constraint_and_check_range_uint64_t(const char* name, uint64_t new_value, bool verbose) {
1235   Flag::Error status = Flag::SUCCESS;
1236   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1237   if (range != NULL) {
1238     status = range->check_uint64_t(new_value, verbose);
1239   }
1240   if (status == Flag::SUCCESS) {
1241     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1242     if (constraint != NULL) {
1243       status = constraint->apply_uint64_t(new_value, verbose);
1244     }
1245   }
1246   return status;
1247 }
1248 
1249 Flag::Error CommandLineFlags::uint64_tAtPut(Flag* flag, uint64_t* value, Flag::Flags origin) {
1250   const char* name;
1251   if (flag == NULL) return Flag::INVALID_FLAG;
1252   if (!flag->is_uint64_t()) return Flag::WRONG_FORMAT;
1253   name = flag->_name;
1254   Flag::Error check = apply_constraint_and_check_range_uint64_t(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1255   if (check != Flag::SUCCESS) return check;
1256   uint64_t old_value = flag->get_uint64_t();
1257   trace_flag_changed<EventUnsignedLongFlagChanged, u8>(name, old_value, *value, origin);
1258   check = flag->set_uint64_t(*value);
1259   *value = old_value;
1260   flag->set_origin(origin);
1261   return check;
1262 }
1263 
1264 Flag::Error CommandLineFlags::uint64_tAtPut(const char* name, size_t len, uint64_t* value, Flag::Flags origin) {
1265   Flag* result = Flag::find_flag(name, len);
1266   return uint64_tAtPut(result, value, origin);
1267 }
1268 
1269 Flag::Error CommandLineFlagsEx::uint64_tAtPut(CommandLineFlagWithType flag, uint64_t value, Flag::Flags origin) {
1270   Flag* faddr = address_of_flag(flag);
1271   guarantee(faddr != NULL && faddr->is_uint64_t(), "wrong flag type");
1272   return CommandLineFlags::uint64_tAtPut(faddr, &value, origin);
1273 }
1274 
1275 Flag::Error CommandLineFlags::size_tAt(const char* name, size_t len, size_t* value, bool allow_locked, bool return_flag) {
1276   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1277   if (result == NULL) return Flag::INVALID_FLAG;
1278   if (!result->is_size_t()) return Flag::WRONG_FORMAT;
1279   *value = result->get_size_t();
1280   return Flag::SUCCESS;
1281 }
1282 
1283 static Flag::Error apply_constraint_and_check_range_size_t(const char* name, size_t new_value, bool verbose) {
1284   Flag::Error status = Flag::SUCCESS;
1285   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1286   if (range != NULL) {
1287     status = range->check_size_t(new_value, verbose);
1288   }
1289   if (status == Flag::SUCCESS) {
1290     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1291     if (constraint != NULL) {
1292       status = constraint->apply_size_t(new_value, verbose);
1293     }
1294   }
1295   return status;
1296 }
1297 
1298 
1299 Flag::Error CommandLineFlags::size_tAtPut(Flag* flag, size_t* value, Flag::Flags origin) {
1300   const char* name;
1301   if (flag == NULL) return Flag::INVALID_FLAG;
1302   if (!flag->is_size_t()) return Flag::WRONG_FORMAT;
1303   name = flag->_name;
1304   Flag::Error check = apply_constraint_and_check_range_size_t(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1305   if (check != Flag::SUCCESS) return check;
1306   size_t old_value = flag->get_size_t();
1307   trace_flag_changed<EventUnsignedLongFlagChanged, u8>(name, old_value, *value, origin);
1308   check = flag->set_size_t(*value);
1309   *value = old_value;
1310   flag->set_origin(origin);
1311   return check;
1312 }
1313 
1314 Flag::Error CommandLineFlags::size_tAtPut(const char* name, size_t len, size_t* value, Flag::Flags origin) {
1315   Flag* result = Flag::find_flag(name, len);
1316   return size_tAtPut(result, value, origin);
1317 }
1318 
1319 Flag::Error CommandLineFlagsEx::size_tAtPut(CommandLineFlagWithType flag, size_t value, Flag::Flags origin) {
1320   Flag* faddr = address_of_flag(flag);
1321   guarantee(faddr != NULL && faddr->is_size_t(), "wrong flag type");
1322   return CommandLineFlags::size_tAtPut(faddr, &value, origin);
1323 }
1324 
1325 Flag::Error CommandLineFlags::doubleAt(const char* name, size_t len, double* value, bool allow_locked, bool return_flag) {
1326   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1327   if (result == NULL) return Flag::INVALID_FLAG;
1328   if (!result->is_double()) return Flag::WRONG_FORMAT;
1329   *value = result->get_double();
1330   return Flag::SUCCESS;
1331 }
1332 
1333 static Flag::Error apply_constraint_and_check_range_double(const char* name, double new_value, bool verbose) {
1334   Flag::Error status = Flag::SUCCESS;
1335   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1336   if (range != NULL) {
1337     status = range->check_double(new_value, verbose);
1338   }
1339   if (status == Flag::SUCCESS) {
1340     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1341     if (constraint != NULL) {
1342       status = constraint->apply_double(new_value, verbose);
1343     }
1344   }
1345   return status;
1346 }
1347 
1348 Flag::Error CommandLineFlags::doubleAtPut(Flag* flag, double* value, Flag::Flags origin) {
1349   const char* name;
1350   if (flag == NULL) return Flag::INVALID_FLAG;
1351   if (!flag->is_double()) return Flag::WRONG_FORMAT;
1352   name = flag->_name;
1353   Flag::Error check = apply_constraint_and_check_range_double(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1354   if (check != Flag::SUCCESS) return check;
1355   double old_value = flag->get_double();
1356   trace_flag_changed<EventDoubleFlagChanged, double>(name, old_value, *value, origin);
1357   check = flag->set_double(*value);
1358   *value = old_value;
1359   flag->set_origin(origin);
1360   return check;
1361 }
1362 
1363 Flag::Error CommandLineFlags::doubleAtPut(const char* name, size_t len, double* value, Flag::Flags origin) {
1364   Flag* result = Flag::find_flag(name, len);
1365   return doubleAtPut(result, value, origin);
1366 }
1367 
1368 Flag::Error CommandLineFlagsEx::doubleAtPut(CommandLineFlagWithType flag, double value, Flag::Flags origin) {
1369   Flag* faddr = address_of_flag(flag);
1370   guarantee(faddr != NULL && faddr->is_double(), "wrong flag type");
1371   return CommandLineFlags::doubleAtPut(faddr, &value, origin);
1372 }
1373 
1374 Flag::Error CommandLineFlags::ccstrAt(const char* name, size_t len, ccstr* value, bool allow_locked, bool return_flag) {
1375   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1376   if (result == NULL) return Flag::INVALID_FLAG;
1377   if (!result->is_ccstr()) return Flag::WRONG_FORMAT;
1378   *value = result->get_ccstr();
1379   return Flag::SUCCESS;
1380 }
1381 
1382 Flag::Error CommandLineFlags::ccstrAtPut(const char* name, size_t len, ccstr* value, Flag::Flags origin) {
1383   Flag* result = Flag::find_flag(name, len);
1384   if (result == NULL) return Flag::INVALID_FLAG;
1385   if (!result->is_ccstr()) return Flag::WRONG_FORMAT;
1386   ccstr old_value = result->get_ccstr();
1387   trace_flag_changed<EventStringFlagChanged, const char*>(name, old_value, *value, origin);
1388   char* new_value = NULL;
1389   if (*value != NULL) {
1390     new_value = os::strdup_check_oom(*value);
1391   }
1392   Flag::Error check = result->set_ccstr(new_value);
1393   if (result->is_default() && old_value != NULL) {
1394     // Prior value is NOT heap allocated, but was a literal constant.
1395     old_value = os::strdup_check_oom(old_value);
1396   }
1397   *value = old_value;
1398   result->set_origin(origin);
1399   return check;
1400 }
1401 
1402 Flag::Error CommandLineFlagsEx::ccstrAtPut(CommandLineFlagWithType flag, ccstr value, Flag::Flags origin) {
1403   Flag* faddr = address_of_flag(flag);
1404   guarantee(faddr != NULL && faddr->is_ccstr(), "wrong flag type");
1405   ccstr old_value = faddr->get_ccstr();
1406   trace_flag_changed<EventStringFlagChanged, const char*>(faddr->_name, old_value, value, origin);
1407   char* new_value = os::strdup_check_oom(value);
1408   Flag::Error check = faddr->set_ccstr(new_value);
1409   if (!faddr->is_default() && old_value != NULL) {
1410     // Prior value is heap allocated so free it.
1411     FREE_C_HEAP_ARRAY(char, old_value);
1412   }
1413   faddr->set_origin(origin);
1414   return check;
1415 }
1416 
1417 extern "C" {
1418   static int compare_flags(const void* void_a, const void* void_b) {
1419     return strcmp((*((Flag**) void_a))->_name, (*((Flag**) void_b))->_name);
1420   }
1421 }
1422 
1423 void CommandLineFlags::printSetFlags(outputStream* out) {
1424   // Print which flags were set on the command line
1425   // note: this method is called before the thread structure is in place
1426   //       which means resource allocation cannot be used.
1427 
1428   // The last entry is the null entry.
1429   const size_t length = Flag::numFlags - 1;
1430 
1431   // Sort
1432   Flag** array = NEW_C_HEAP_ARRAY(Flag*, length, mtArguments);
1433   for (size_t i = 0; i < length; i++) {
1434     array[i] = &flagTable[i];
1435   }
1436   qsort(array, length, sizeof(Flag*), compare_flags);
1437 
1438   // Print
1439   for (size_t i = 0; i < length; i++) {
1440     if (array[i]->get_origin() /* naked field! */) {
1441       array[i]->print_as_flag(out);
1442       out->print(" ");
1443     }
1444   }
1445   out->cr();
1446   FREE_C_HEAP_ARRAY(Flag*, array);
1447 }
1448 
1449 #ifndef PRODUCT
1450 
1451 void CommandLineFlags::verify() {
1452   assert(Arguments::check_vm_args_consistency(), "Some flag settings conflict");
1453 }
1454 
1455 #endif // PRODUCT
1456 
1457 void CommandLineFlags::printFlags(outputStream* out, bool withComments, bool printRanges) {
1458   // Print the flags sorted by name
1459   // note: this method is called before the thread structure is in place
1460   //       which means resource allocation cannot be used.
1461 
1462   // The last entry is the null entry.
1463   const size_t length = Flag::numFlags - 1;
1464 
1465   // Sort
1466   Flag** array = NEW_C_HEAP_ARRAY(Flag*, length, mtArguments);
1467   for (size_t i = 0; i < length; i++) {
1468     array[i] = &flagTable[i];
1469   }
1470   qsort(array, length, sizeof(Flag*), compare_flags);
1471 
1472   // Print
1473   if (!printRanges) {
1474     out->print_cr("[Global flags]");
1475   } else {
1476     out->print_cr("[Global flags ranges]");
1477   }
1478 
1479   for (size_t i = 0; i < length; i++) {
1480     if (array[i]->is_unlocked()) {
1481       array[i]->print_on(out, withComments, printRanges);
1482     }
1483   }
1484   FREE_C_HEAP_ARRAY(Flag*, array);
1485 }