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