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