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 #endif // INCLUDE_ALL_GCS
  43 #ifdef COMPILER1
  44 #include "c1/c1_globals.hpp"
  45 #endif
  46 #if INCLUDE_JVMCI
  47 #include "jvmci/jvmci_globals.hpp"
  48 #endif
  49 #ifdef COMPILER2
  50 #include "opto/c2_globals.hpp"
  51 #endif
  52 #ifdef SHARK
  53 #include "shark/shark_globals.hpp"
  54 #endif
  55 
  56 RUNTIME_FLAGS(MATERIALIZE_DEVELOPER_FLAG, \
  57               MATERIALIZE_PD_DEVELOPER_FLAG, \
  58               MATERIALIZE_PRODUCT_FLAG, \
  59               MATERIALIZE_PD_PRODUCT_FLAG, \
  60               MATERIALIZE_DIAGNOSTIC_FLAG, \
  61               MATERIALIZE_PD_DIAGNOSTIC_FLAG, \
  62               MATERIALIZE_EXPERIMENTAL_FLAG, \
  63               MATERIALIZE_NOTPRODUCT_FLAG, \
  64               MATERIALIZE_MANAGEABLE_FLAG, \
  65               MATERIALIZE_PRODUCT_RW_FLAG, \
  66               MATERIALIZE_LP64_PRODUCT_FLAG, \
  67               IGNORE_RANGE, \
  68               IGNORE_CONSTRAINT, \
  69               IGNORE_WRITEABLE)
  70 
  71 RUNTIME_OS_FLAGS(MATERIALIZE_DEVELOPER_FLAG, \
  72                  MATERIALIZE_PD_DEVELOPER_FLAG, \
  73                  MATERIALIZE_PRODUCT_FLAG, \
  74                  MATERIALIZE_PD_PRODUCT_FLAG, \
  75                  MATERIALIZE_DIAGNOSTIC_FLAG, \
  76                  MATERIALIZE_PD_DIAGNOSTIC_FLAG, \
  77                  MATERIALIZE_NOTPRODUCT_FLAG, \
  78                  IGNORE_RANGE, \
  79                  IGNORE_CONSTRAINT, \
  80                  IGNORE_WRITEABLE)
  81 
  82 ARCH_FLAGS(MATERIALIZE_DEVELOPER_FLAG, \
  83            MATERIALIZE_PRODUCT_FLAG, \
  84            MATERIALIZE_DIAGNOSTIC_FLAG, \
  85            MATERIALIZE_EXPERIMENTAL_FLAG, \
  86            MATERIALIZE_NOTPRODUCT_FLAG, \
  87            IGNORE_RANGE, \
  88            IGNORE_CONSTRAINT, \
  89            IGNORE_WRITEABLE)
  90 
  91 MATERIALIZE_FLAGS_EXT
  92 
  93 #define DEFAULT_RANGE_STR_CHUNK_SIZE 64
  94 static char* create_range_str(const char *fmt, ...) {
  95   static size_t string_length = DEFAULT_RANGE_STR_CHUNK_SIZE;
  96   static char* range_string = NEW_C_HEAP_ARRAY(char, string_length, mtLogging);
  97 
  98   int size_needed = 0;
  99   do {
 100     va_list args;
 101     va_start(args, fmt);
 102     size_needed = jio_vsnprintf(range_string, string_length, fmt, args);
 103     va_end(args);
 104 
 105     if (size_needed < 0) {
 106       string_length += DEFAULT_RANGE_STR_CHUNK_SIZE;
 107       range_string = REALLOC_C_HEAP_ARRAY(char, range_string, string_length, mtLogging);
 108       guarantee(range_string != NULL, "create_range_str string should not be NULL");
 109     }
 110   } while (size_needed < 0);
 111 
 112   return range_string;
 113 }
 114 
 115 const char* Flag::get_int_default_range_str() {
 116   return create_range_str("[ " INT32_FORMAT_W(-25) " ... " INT32_FORMAT_W(25) " ]", INT_MIN, INT_MAX);
 117 }
 118 
 119 const char* Flag::get_uint_default_range_str() {
 120   return create_range_str("[ " UINT32_FORMAT_W(-25) " ... " UINT32_FORMAT_W(25) " ]", 0, UINT_MAX);
 121 }
 122 
 123 const char* Flag::get_intx_default_range_str() {
 124   return create_range_str("[ " INTX_FORMAT_W(-25) " ... " INTX_FORMAT_W(25) " ]", min_intx, max_intx);
 125 }
 126 
 127 const char* Flag::get_uintx_default_range_str() {
 128   return create_range_str("[ " UINTX_FORMAT_W(-25) " ... " UINTX_FORMAT_W(25) " ]", 0, max_uintx);
 129 }
 130 
 131 const char* Flag::get_uint64_t_default_range_str() {
 132   return create_range_str("[ " UINT64_FORMAT_W(-25) " ... " UINT64_FORMAT_W(25) " ]", 0, uint64_t(max_juint));
 133 }
 134 
 135 const char* Flag::get_size_t_default_range_str() {
 136   return create_range_str("[ " SIZE_FORMAT_W(-25) " ... " SIZE_FORMAT_W(25) " ]", 0, SIZE_MAX);
 137 }
 138 
 139 const char* Flag::get_double_default_range_str() {
 140   return create_range_str("[ %-25.3f ... %25.3f ]", DBL_MIN, DBL_MAX);
 141 }
 142 
 143 static bool is_product_build() {
 144 #ifdef PRODUCT
 145   return true;
 146 #else
 147   return false;
 148 #endif
 149 }
 150 
 151 Flag::Error Flag::check_writable(bool changed) {
 152   if (is_constant_in_binary()) {
 153     fatal("flag is constant: %s", _name);
 154   }
 155 
 156   Flag::Error error = Flag::SUCCESS;
 157   if (changed) {
 158     CommandLineFlagWriteable* writeable = CommandLineFlagWriteableList::find(_name);
 159     if (writeable) {
 160       if (writeable->is_writeable() == false) {
 161         switch (writeable->type())
 162         {
 163           case CommandLineFlagWriteable::Once:
 164             error = Flag::SET_ONLY_ONCE;
 165             jio_fprintf(defaultStream::error_stream(), "Error: %s may not be set more than once\n", _name);
 166             break;
 167           case CommandLineFlagWriteable::CommandLineOnly:
 168             error = Flag::COMMAND_LINE_ONLY;
 169             jio_fprintf(defaultStream::error_stream(), "Error: %s may be modified only from commad line\n", _name);
 170             break;
 171           default:
 172             ShouldNotReachHere();
 173             break;
 174         }
 175       }
 176       writeable->mark_once();
 177     }
 178   }
 179   return error;
 180 }
 181 
 182 bool Flag::is_bool() const {
 183   return strcmp(_type, "bool") == 0;
 184 }
 185 
 186 bool Flag::get_bool() const {
 187   return *((bool*) _addr);
 188 }
 189 
 190 Flag::Error Flag::set_bool(bool value) {
 191   Flag::Error error = check_writable(value!=get_bool());
 192   if (error == Flag::SUCCESS) {
 193     *((bool*) _addr) = value;
 194   }
 195   return error;
 196 }
 197 
 198 bool Flag::is_int() const {
 199   return strcmp(_type, "int")  == 0;
 200 }
 201 
 202 int Flag::get_int() const {
 203   return *((int*) _addr);
 204 }
 205 
 206 Flag::Error Flag::set_int(int value) {
 207   Flag::Error error = check_writable(value!=get_int());
 208   if (error == Flag::SUCCESS) {
 209     *((int*) _addr) = value;
 210   }
 211   return error;
 212 }
 213 
 214 bool Flag::is_uint() const {
 215   return strcmp(_type, "uint")  == 0;
 216 }
 217 
 218 uint Flag::get_uint() const {
 219   return *((uint*) _addr);
 220 }
 221 
 222 Flag::Error Flag::set_uint(uint value) {
 223   Flag::Error error = check_writable(value!=get_uint());
 224   if (error == Flag::SUCCESS) {
 225     *((uint*) _addr) = value;
 226   }
 227   return error;
 228 }
 229 
 230 bool Flag::is_intx() const {
 231   return strcmp(_type, "intx")  == 0;
 232 }
 233 
 234 intx Flag::get_intx() const {
 235   return *((intx*) _addr);
 236 }
 237 
 238 Flag::Error Flag::set_intx(intx value) {
 239   Flag::Error error = check_writable(value!=get_intx());
 240   if (error == Flag::SUCCESS) {
 241     *((intx*) _addr) = value;
 242   }
 243   return error;
 244 }
 245 
 246 bool Flag::is_uintx() const {
 247   return strcmp(_type, "uintx") == 0;
 248 }
 249 
 250 uintx Flag::get_uintx() const {
 251   return *((uintx*) _addr);
 252 }
 253 
 254 Flag::Error Flag::set_uintx(uintx value) {
 255   Flag::Error error = check_writable(value!=get_uintx());
 256   if (error == Flag::SUCCESS) {
 257     *((uintx*) _addr) = value;
 258   }
 259   return error;
 260 }
 261 
 262 bool Flag::is_uint64_t() const {
 263   return strcmp(_type, "uint64_t") == 0;
 264 }
 265 
 266 uint64_t Flag::get_uint64_t() const {
 267   return *((uint64_t*) _addr);
 268 }
 269 
 270 Flag::Error Flag::set_uint64_t(uint64_t value) {
 271   Flag::Error error = check_writable(value!=get_uint64_t());
 272   if (error == Flag::SUCCESS) {
 273     *((uint64_t*) _addr) = value;
 274   }
 275   return error;
 276 }
 277 
 278 bool Flag::is_size_t() const {
 279   return strcmp(_type, "size_t") == 0;
 280 }
 281 
 282 size_t Flag::get_size_t() const {
 283   return *((size_t*) _addr);
 284 }
 285 
 286 Flag::Error Flag::set_size_t(size_t value) {
 287   Flag::Error error = check_writable(value!=get_size_t());
 288   if (error == Flag::SUCCESS) {
 289     *((size_t*) _addr) = value;
 290   }
 291   return error;
 292 }
 293 
 294 bool Flag::is_double() const {
 295   return strcmp(_type, "double") == 0;
 296 }
 297 
 298 double Flag::get_double() const {
 299   return *((double*) _addr);
 300 }
 301 
 302 Flag::Error Flag::set_double(double value) {
 303   Flag::Error error = check_writable(value!=get_double());
 304   if (error == Flag::SUCCESS) {
 305     *((double*) _addr) = value;
 306   }
 307   return error;
 308 }
 309 
 310 bool Flag::is_ccstr() const {
 311   return strcmp(_type, "ccstr") == 0 || strcmp(_type, "ccstrlist") == 0;
 312 }
 313 
 314 bool Flag::ccstr_accumulates() const {
 315   return strcmp(_type, "ccstrlist") == 0;
 316 }
 317 
 318 ccstr Flag::get_ccstr() const {
 319   return *((ccstr*) _addr);
 320 }
 321 
 322 Flag::Error Flag::set_ccstr(ccstr value) {
 323   Flag::Error error = check_writable(value!=get_ccstr());
 324   if (error == Flag::SUCCESS) {
 325     *((ccstr*) _addr) = value;
 326   }
 327   return error;
 328 }
 329 
 330 
 331 Flag::Flags Flag::get_origin() {
 332   return Flags(_flags & VALUE_ORIGIN_MASK);
 333 }
 334 
 335 void Flag::set_origin(Flags origin) {
 336   assert((origin & VALUE_ORIGIN_MASK) == origin, "sanity");
 337   Flags new_origin = Flags((origin == COMMAND_LINE) ? Flags(origin | ORIG_COMMAND_LINE) : origin);
 338   _flags = Flags((_flags & ~VALUE_ORIGIN_MASK) | new_origin);
 339 }
 340 
 341 bool Flag::is_default() {
 342   return (get_origin() == DEFAULT);
 343 }
 344 
 345 bool Flag::is_ergonomic() {
 346   return (get_origin() == ERGONOMIC);
 347 }
 348 
 349 bool Flag::is_command_line() {
 350   return (_flags & ORIG_COMMAND_LINE) != 0;
 351 }
 352 
 353 void Flag::set_command_line() {
 354   _flags = Flags(_flags | ORIG_COMMAND_LINE);
 355 }
 356 
 357 bool Flag::is_product() const {
 358   return (_flags & KIND_PRODUCT) != 0;
 359 }
 360 
 361 bool Flag::is_manageable() const {
 362   return (_flags & KIND_MANAGEABLE) != 0;
 363 }
 364 
 365 bool Flag::is_diagnostic() const {
 366   return (_flags & KIND_DIAGNOSTIC) != 0;
 367 }
 368 
 369 bool Flag::is_experimental() const {
 370   return (_flags & KIND_EXPERIMENTAL) != 0;
 371 }
 372 
 373 bool Flag::is_notproduct() const {
 374   return (_flags & KIND_NOT_PRODUCT) != 0;
 375 }
 376 
 377 bool Flag::is_develop() const {
 378   return (_flags & KIND_DEVELOP) != 0;
 379 }
 380 
 381 bool Flag::is_read_write() const {
 382   return (_flags & KIND_READ_WRITE) != 0;
 383 }
 384 
 385 bool Flag::is_commercial() const {
 386   return (_flags & KIND_COMMERCIAL) != 0;
 387 }
 388 
 389 /**
 390  * Returns if this flag is a constant in the binary.  Right now this is
 391  * true for notproduct and develop flags in product builds.
 392  */
 393 bool Flag::is_constant_in_binary() const {
 394 #ifdef PRODUCT
 395     return is_notproduct() || is_develop();
 396 #else
 397     return false;
 398 #endif
 399 }
 400 
 401 bool Flag::is_unlocker() const {
 402   return strcmp(_name, "UnlockDiagnosticVMOptions") == 0     ||
 403          strcmp(_name, "UnlockExperimentalVMOptions") == 0   ||
 404          is_unlocker_ext();
 405 }
 406 
 407 bool Flag::is_unlocked() const {
 408   if (is_diagnostic()) {
 409     return UnlockDiagnosticVMOptions;
 410   }
 411   if (is_experimental()) {
 412     return UnlockExperimentalVMOptions;
 413   }
 414   return is_unlocked_ext();
 415 }
 416 
 417 void Flag::unlock_diagnostic() {
 418   assert(is_diagnostic(), "sanity");
 419   _flags = Flags(_flags & ~KIND_DIAGNOSTIC);
 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   get_locked_message_ext(buf, buflen);
 451   return Flag::NONE;
 452 }
 453 
 454 bool Flag::is_writeable() const {
 455   return is_manageable() || (is_product() && is_read_write()) || is_writeable_ext();
 456 }
 457 
 458 // All flags except "manageable" are assumed to be internal flags.
 459 // Long term, we need to define a mechanism to specify which flags
 460 // are external/stable and change this function accordingly.
 461 bool Flag::is_external() const {
 462   return is_manageable() || is_external_ext();
 463 }
 464 
 465 void Flag::print_on(outputStream* st, bool withComments, bool printRanges) {
 466   // Don't print notproduct and develop flags in a product build.
 467   if (is_constant_in_binary()) {
 468     return;
 469   }
 470 
 471   if (!printRanges) {
 472     // Use some named constants to make code more readable.
 473     const unsigned int nSpaces    = 10;
 474     const unsigned int maxFlagLen = 40 + nSpaces;
 475 
 476     // The print below assumes that the flag name is 40 characters or less.
 477     // This works for most flags, but there are exceptions. Our longest flag
 478     // name right now is UseAdaptiveGenerationSizePolicyAtMajorCollection and
 479     // its minor collection buddy. These are 48 characters. We use a buffer of
 480     // nSpaces spaces below to adjust the space between the flag value and the
 481     // column of flag type and origin that is printed in the end of the line.
 482     char spaces[nSpaces + 1] = "          ";
 483     st->print("%9s %-*s = ", _type, maxFlagLen-nSpaces, _name);
 484 
 485     if (is_bool()) {
 486       st->print("%-20s", get_bool() ? "true" : "false");
 487     } else if (is_int()) {
 488       st->print("%-20d", get_int());
 489     } else if (is_uint()) {
 490       st->print("%-20u", get_uint());
 491     } else if (is_intx()) {
 492       st->print(INTX_FORMAT_W(-20), get_intx());
 493     } else if (is_uintx()) {
 494       st->print(UINTX_FORMAT_W(-20), get_uintx());
 495     } else if (is_uint64_t()) {
 496       st->print(UINT64_FORMAT_W(-20), get_uint64_t());
 497     } else if (is_size_t()) {
 498       st->print(SIZE_FORMAT_W(-20), get_size_t());
 499     } else if (is_double()) {
 500       st->print("%-20f", get_double());
 501     } else if (is_ccstr()) {
 502       const char* cp = get_ccstr();
 503       if (cp != NULL) {
 504         const char* eol;
 505         while ((eol = strchr(cp, '\n')) != NULL) {
 506           size_t llen = pointer_delta(eol, cp, sizeof(char));
 507           st->print("%.*s", (int)llen, cp);
 508           st->cr();
 509           cp = eol+1;
 510           st->print("%5s %-35s += ", "", _name);
 511         }
 512         st->print("%-20s", cp);
 513       }
 514       else st->print("%-20s", "");
 515     }
 516     // Make sure we do not punch a '\0' at a negative char array index.
 517     unsigned int nameLen = (unsigned int)strlen(_name);
 518     if (nameLen <= maxFlagLen) {
 519       spaces[maxFlagLen - MAX2(maxFlagLen-nSpaces, nameLen)] = '\0';
 520       st->print("%s", spaces);
 521     }
 522     print_kind_and_origin(st);
 523 
 524 #ifndef PRODUCT
 525     if (withComments) {
 526       st->print("%s", _doc);
 527     }
 528 #endif
 529 
 530     st->cr();
 531 
 532   } else if (!is_bool() && !is_ccstr()) {
 533     st->print("%9s %-50s ", _type, _name);
 534 
 535     RangeStrFunc func = NULL;
 536     if (is_int()) {
 537       func = Flag::get_int_default_range_str;
 538     } else if (is_uint()) {
 539       func = Flag::get_uint_default_range_str;
 540     } else if (is_intx()) {
 541       func = Flag::get_intx_default_range_str;
 542     } else if (is_uintx()) {
 543       func = Flag::get_uintx_default_range_str;
 544     } else if (is_uint64_t()) {
 545       func = Flag::get_uint64_t_default_range_str;
 546     } else if (is_size_t()) {
 547       func = Flag::get_size_t_default_range_str;
 548     } else if (is_double()) {
 549       func = Flag::get_double_default_range_str;
 550     } else {
 551       ShouldNotReachHere();
 552     }
 553     CommandLineFlagRangeList::print(st, _name, func);
 554 
 555     st->print(" %-16s", " ");
 556     print_kind_and_origin(st);
 557 
 558 #ifndef PRODUCT
 559     if (withComments) {
 560       st->print("%s", _doc);
 561     }
 562 #endif
 563 
 564     st->cr();
 565   }
 566 }
 567 
 568 void Flag::print_kind_and_origin(outputStream* st) {
 569   struct Data {
 570     int flag;
 571     const char* name;
 572   };
 573 
 574   Data data[] = {
 575       { KIND_JVMCI, "JVMCI" },
 576       { KIND_C1, "C1" },
 577       { KIND_C2, "C2" },
 578       { KIND_ARCH, "ARCH" },
 579       { KIND_SHARK, "SHARK" },
 580       { KIND_PLATFORM_DEPENDENT, "pd" },
 581       { KIND_PRODUCT, "product" },
 582       { KIND_MANAGEABLE, "manageable" },
 583       { KIND_DIAGNOSTIC, "diagnostic" },
 584       { KIND_EXPERIMENTAL, "experimental" },
 585       { KIND_COMMERCIAL, "commercial" },
 586       { KIND_NOT_PRODUCT, "notproduct" },
 587       { KIND_DEVELOP, "develop" },
 588       { KIND_LP64_PRODUCT, "lp64_product" },
 589       { KIND_READ_WRITE, "rw" },
 590       { -1, "" }
 591   };
 592 
 593   if ((_flags & KIND_MASK) != 0) {
 594     bool is_first = true;
 595     const size_t buffer_size = 64;
 596     size_t buffer_used = 0;
 597     char kind[buffer_size];
 598 
 599     jio_snprintf(kind, buffer_size, "{");
 600     buffer_used++;
 601     for (int i = 0; data[i].flag != -1; i++) {
 602       Data d = data[i];
 603       if ((_flags & d.flag) != 0) {
 604         if (is_first) {
 605           is_first = false;
 606         } else {
 607           assert(buffer_used + 1 < buffer_size, "Too small buffer");
 608           jio_snprintf(kind + buffer_used, buffer_size - buffer_used, " ");
 609           buffer_used++;
 610         }
 611         size_t length = strlen(d.name);
 612         assert(buffer_used + length < buffer_size, "Too small buffer");
 613         jio_snprintf(kind + buffer_used, buffer_size - buffer_used, "%s", d.name);
 614         buffer_used += length;
 615       }
 616     }
 617     assert(buffer_used + 2 <= buffer_size, "Too small buffer");
 618     jio_snprintf(kind + buffer_used, buffer_size - buffer_used, "}");
 619     st->print("%20s", kind);
 620   }
 621 
 622   int origin = _flags & VALUE_ORIGIN_MASK;
 623   st->print(" {");
 624   switch(origin) {
 625     case DEFAULT:
 626       st->print("default"); break;
 627     case COMMAND_LINE:
 628       st->print("command line"); break;
 629     case ENVIRON_VAR:
 630       st->print("environment"); break;
 631     case CONFIG_FILE:
 632       st->print("config file"); break;
 633     case MANAGEMENT:
 634       st->print("management"); break;
 635     case ERGONOMIC:
 636       if (_flags & ORIG_COMMAND_LINE) {
 637         st->print("command line, ");
 638       }
 639       st->print("ergonomic"); break;
 640     case ATTACH_ON_DEMAND:
 641       st->print("attach"); break;
 642     case INTERNAL:
 643       st->print("internal"); break;
 644   }
 645   st->print("}");
 646 }
 647 
 648 void Flag::print_as_flag(outputStream* st) {
 649   if (is_bool()) {
 650     st->print("-XX:%s%s", get_bool() ? "+" : "-", _name);
 651   } else if (is_int()) {
 652     st->print("-XX:%s=%d", _name, get_int());
 653   } else if (is_uint()) {
 654     st->print("-XX:%s=%u", _name, get_uint());
 655   } else if (is_intx()) {
 656     st->print("-XX:%s=" INTX_FORMAT, _name, get_intx());
 657   } else if (is_uintx()) {
 658     st->print("-XX:%s=" UINTX_FORMAT, _name, get_uintx());
 659   } else if (is_uint64_t()) {
 660     st->print("-XX:%s=" UINT64_FORMAT, _name, get_uint64_t());
 661   } else if (is_size_t()) {
 662     st->print("-XX:%s=" SIZE_FORMAT, _name, get_size_t());
 663   } else if (is_double()) {
 664     st->print("-XX:%s=%f", _name, get_double());
 665   } else if (is_ccstr()) {
 666     st->print("-XX:%s=", _name);
 667     const char* cp = get_ccstr();
 668     if (cp != NULL) {
 669       // Need to turn embedded '\n's back into separate arguments
 670       // Not so efficient to print one character at a time,
 671       // but the choice is to do the transformation to a buffer
 672       // and print that.  And this need not be efficient.
 673       for (; *cp != '\0'; cp += 1) {
 674         switch (*cp) {
 675           default:
 676             st->print("%c", *cp);
 677             break;
 678           case '\n':
 679             st->print(" -XX:%s=", _name);
 680             break;
 681         }
 682       }
 683     }
 684   } else {
 685     ShouldNotReachHere();
 686   }
 687 }
 688 
 689 const char* Flag::flag_error_str(Flag::Error error) {
 690   switch (error) {
 691     case Flag::MISSING_NAME: return "MISSING_NAME";
 692     case Flag::MISSING_VALUE: return "MISSING_VALUE";
 693     case Flag::NON_WRITABLE: return "NON_WRITABLE";
 694     case Flag::OUT_OF_BOUNDS: return "OUT_OF_BOUNDS";
 695     case Flag::VIOLATES_CONSTRAINT: return "VIOLATES_CONSTRAINT";
 696     case Flag::INVALID_FLAG: return "INVALID_FLAG";
 697     case Flag::ERR_OTHER: return "ERR_OTHER";
 698     case Flag::SUCCESS: return "SUCCESS";
 699     default: ShouldNotReachHere(); return "NULL";
 700   }
 701 }
 702 
 703 // 4991491 do not "optimize out" the was_set false values: omitting them
 704 // tickles a Microsoft compiler bug causing flagTable to be malformed
 705 
 706 #define RUNTIME_PRODUCT_FLAG_STRUCT(     type, name, value, doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_PRODUCT) },
 707 #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) },
 708 #define RUNTIME_DIAGNOSTIC_FLAG_STRUCT(  type, name, value, doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_DIAGNOSTIC) },
 709 #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) },
 710 #define RUNTIME_EXPERIMENTAL_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_EXPERIMENTAL) },
 711 #define RUNTIME_MANAGEABLE_FLAG_STRUCT(  type, name, value, doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_MANAGEABLE) },
 712 #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) },
 713 #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) },
 714 #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) },
 715 #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) },
 716 
 717 #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) },
 718 #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) },
 719 #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) },
 720 #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) },
 721 #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) },
 722 #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) },
 723 #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) },
 724 #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) },
 725 
 726 #ifdef _LP64
 727 #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) },
 728 #else
 729 #define RUNTIME_LP64_PRODUCT_FLAG_STRUCT(type, name, value, doc) /* flag is constant */
 730 #endif // _LP64
 731 
 732 #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) },
 733 #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) },
 734 #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) },
 735 #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) },
 736 #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) },
 737 #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) },
 738 #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) },
 739 
 740 #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) },
 741 #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) },
 742 #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) },
 743 #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) },
 744 #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) },
 745 #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) },
 746 #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) },
 747 #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) },
 748 
 749 #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) },
 750 #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) },
 751 #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) },
 752 #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) },
 753 #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) },
 754 
 755 #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) },
 756 #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) },
 757 #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) },
 758 #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) },
 759 #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) },
 760 #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) },
 761 #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) },
 762 
 763 static Flag flagTable[] = {
 764  RUNTIME_FLAGS(RUNTIME_DEVELOP_FLAG_STRUCT, \
 765                RUNTIME_PD_DEVELOP_FLAG_STRUCT, \
 766                RUNTIME_PRODUCT_FLAG_STRUCT, \
 767                RUNTIME_PD_PRODUCT_FLAG_STRUCT, \
 768                RUNTIME_DIAGNOSTIC_FLAG_STRUCT, \
 769                RUNTIME_PD_DIAGNOSTIC_FLAG_STRUCT, \
 770                RUNTIME_EXPERIMENTAL_FLAG_STRUCT, \
 771                RUNTIME_NOTPRODUCT_FLAG_STRUCT, \
 772                RUNTIME_MANAGEABLE_FLAG_STRUCT, \
 773                RUNTIME_PRODUCT_RW_FLAG_STRUCT, \
 774                RUNTIME_LP64_PRODUCT_FLAG_STRUCT, \
 775                IGNORE_RANGE, \
 776                IGNORE_CONSTRAINT, \
 777                IGNORE_WRITEABLE)
 778  RUNTIME_OS_FLAGS(RUNTIME_DEVELOP_FLAG_STRUCT, \
 779                   RUNTIME_PD_DEVELOP_FLAG_STRUCT, \
 780                   RUNTIME_PRODUCT_FLAG_STRUCT, \
 781                   RUNTIME_PD_PRODUCT_FLAG_STRUCT, \
 782                   RUNTIME_DIAGNOSTIC_FLAG_STRUCT, \
 783                   RUNTIME_PD_DIAGNOSTIC_FLAG_STRUCT, \
 784                   RUNTIME_NOTPRODUCT_FLAG_STRUCT, \
 785                   IGNORE_RANGE, \
 786                   IGNORE_CONSTRAINT, \
 787                   IGNORE_WRITEABLE)
 788 #if INCLUDE_ALL_GCS
 789  G1_FLAGS(RUNTIME_DEVELOP_FLAG_STRUCT, \













 790           RUNTIME_PD_DEVELOP_FLAG_STRUCT, \
 791           RUNTIME_PRODUCT_FLAG_STRUCT, \
 792           RUNTIME_PD_PRODUCT_FLAG_STRUCT, \
 793           RUNTIME_DIAGNOSTIC_FLAG_STRUCT, \
 794           RUNTIME_PD_DIAGNOSTIC_FLAG_STRUCT, \
 795           RUNTIME_EXPERIMENTAL_FLAG_STRUCT, \
 796           RUNTIME_NOTPRODUCT_FLAG_STRUCT, \
 797           RUNTIME_MANAGEABLE_FLAG_STRUCT, \
 798           RUNTIME_PRODUCT_RW_FLAG_STRUCT, \
 799           IGNORE_RANGE, \
 800           IGNORE_CONSTRAINT, \
 801           IGNORE_WRITEABLE)
 802 #endif // INCLUDE_ALL_GCS
 803 #if INCLUDE_JVMCI
 804  JVMCI_FLAGS(JVMCI_DEVELOP_FLAG_STRUCT, \
 805              JVMCI_PD_DEVELOP_FLAG_STRUCT, \
 806              JVMCI_PRODUCT_FLAG_STRUCT, \
 807              JVMCI_PD_PRODUCT_FLAG_STRUCT, \
 808              JVMCI_DIAGNOSTIC_FLAG_STRUCT, \
 809              JVMCI_PD_DIAGNOSTIC_FLAG_STRUCT, \
 810              JVMCI_EXPERIMENTAL_FLAG_STRUCT, \
 811              JVMCI_NOTPRODUCT_FLAG_STRUCT, \
 812              IGNORE_RANGE, \
 813              IGNORE_CONSTRAINT, \
 814              IGNORE_WRITEABLE)
 815 #endif // INCLUDE_JVMCI
 816 #ifdef COMPILER1
 817  C1_FLAGS(C1_DEVELOP_FLAG_STRUCT, \
 818           C1_PD_DEVELOP_FLAG_STRUCT, \
 819           C1_PRODUCT_FLAG_STRUCT, \
 820           C1_PD_PRODUCT_FLAG_STRUCT, \
 821           C1_DIAGNOSTIC_FLAG_STRUCT, \
 822           C1_PD_DIAGNOSTIC_FLAG_STRUCT, \
 823           C1_NOTPRODUCT_FLAG_STRUCT, \
 824           IGNORE_RANGE, \
 825           IGNORE_CONSTRAINT, \
 826           IGNORE_WRITEABLE)
 827 #endif // COMPILER1
 828 #ifdef COMPILER2
 829  C2_FLAGS(C2_DEVELOP_FLAG_STRUCT, \
 830           C2_PD_DEVELOP_FLAG_STRUCT, \
 831           C2_PRODUCT_FLAG_STRUCT, \
 832           C2_PD_PRODUCT_FLAG_STRUCT, \
 833           C2_DIAGNOSTIC_FLAG_STRUCT, \
 834           C2_PD_DIAGNOSTIC_FLAG_STRUCT, \
 835           C2_EXPERIMENTAL_FLAG_STRUCT, \
 836           C2_NOTPRODUCT_FLAG_STRUCT, \
 837           IGNORE_RANGE, \
 838           IGNORE_CONSTRAINT, \
 839           IGNORE_WRITEABLE)
 840 #endif // COMPILER2
 841 #ifdef SHARK
 842  SHARK_FLAGS(SHARK_DEVELOP_FLAG_STRUCT, \
 843              SHARK_PD_DEVELOP_FLAG_STRUCT, \
 844              SHARK_PRODUCT_FLAG_STRUCT, \
 845              SHARK_PD_PRODUCT_FLAG_STRUCT, \
 846              SHARK_DIAGNOSTIC_FLAG_STRUCT, \
 847              SHARK_PD_DIAGNOSTIC_FLAG_STRUCT, \
 848              SHARK_NOTPRODUCT_FLAG_STRUCT, \
 849              IGNORE_RANGE, \
 850              IGNORE_CONSTRAINT, \
 851              IGNORE_WRITEABLE)
 852 #endif // SHARK
 853  ARCH_FLAGS(ARCH_DEVELOP_FLAG_STRUCT, \
 854             ARCH_PRODUCT_FLAG_STRUCT, \
 855             ARCH_DIAGNOSTIC_FLAG_STRUCT, \
 856             ARCH_EXPERIMENTAL_FLAG_STRUCT, \
 857             ARCH_NOTPRODUCT_FLAG_STRUCT, \
 858             IGNORE_RANGE, \
 859             IGNORE_CONSTRAINT, \
 860             IGNORE_WRITEABLE)
 861  FLAGTABLE_EXT
 862  {0, NULL, NULL}
 863 };
 864 
 865 Flag* Flag::flags = flagTable;
 866 size_t Flag::numFlags = (sizeof(flagTable) / sizeof(Flag));
 867 
 868 inline bool str_equal(const char* s, const char* q, size_t len) {
 869   // s is null terminated, q is not!
 870   if (strlen(s) != (unsigned int) len) return false;
 871   return strncmp(s, q, len) == 0;
 872 }
 873 
 874 // Search the flag table for a named flag
 875 Flag* Flag::find_flag(const char* name, size_t length, bool allow_locked, bool return_flag) {
 876   for (Flag* current = &flagTable[0]; current->_name != NULL; current++) {
 877     if (str_equal(current->_name, name, length)) {
 878       // Found a matching entry.
 879       // Don't report notproduct and develop flags in product builds.
 880       if (current->is_constant_in_binary()) {
 881         return (return_flag ? current : NULL);
 882       }
 883       // Report locked flags only if allowed.
 884       if (!(current->is_unlocked() || current->is_unlocker())) {
 885         if (!allow_locked) {
 886           // disable use of locked flags, e.g. diagnostic, experimental,
 887           // commercial... until they are explicitly unlocked
 888           return NULL;
 889         }
 890       }
 891       return current;
 892     }
 893   }
 894   // Flag name is not in the flag table
 895   return NULL;
 896 }
 897 
 898 // Compute string similarity based on Dice's coefficient
 899 static float str_similar(const char* str1, const char* str2, size_t len2) {
 900   int len1 = (int) strlen(str1);
 901   int total = len1 + (int) len2;
 902 
 903   int hit = 0;
 904 
 905   for (int i = 0; i < len1 -1; ++i) {
 906     for (int j = 0; j < (int) len2 -1; ++j) {
 907       if ((str1[i] == str2[j]) && (str1[i+1] == str2[j+1])) {
 908         ++hit;
 909         break;
 910       }
 911     }
 912   }
 913 
 914   return 2.0f * (float) hit / (float) total;
 915 }
 916 
 917 Flag* Flag::fuzzy_match(const char* name, size_t length, bool allow_locked) {
 918   float VMOptionsFuzzyMatchSimilarity = 0.7f;
 919   Flag* match = NULL;
 920   float score;
 921   float max_score = -1;
 922 
 923   for (Flag* current = &flagTable[0]; current->_name != NULL; current++) {
 924     score = str_similar(current->_name, name, length);
 925     if (score > max_score) {
 926       max_score = score;
 927       match = current;
 928     }
 929   }
 930 
 931   if (!(match->is_unlocked() || match->is_unlocker())) {
 932     if (!allow_locked) {
 933       return NULL;
 934     }
 935   }
 936 
 937   if (max_score < VMOptionsFuzzyMatchSimilarity) {
 938     return NULL;
 939   }
 940 
 941   return match;
 942 }
 943 
 944 // Returns the address of the index'th element
 945 static Flag* address_of_flag(CommandLineFlagWithType flag) {
 946   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 947   return &Flag::flags[flag];
 948 }
 949 
 950 bool CommandLineFlagsEx::is_default(CommandLineFlag flag) {
 951   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 952   Flag* f = &Flag::flags[flag];
 953   return f->is_default();
 954 }
 955 
 956 bool CommandLineFlagsEx::is_ergo(CommandLineFlag flag) {
 957   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 958   Flag* f = &Flag::flags[flag];
 959   return f->is_ergonomic();
 960 }
 961 
 962 bool CommandLineFlagsEx::is_cmdline(CommandLineFlag flag) {
 963   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 964   Flag* f = &Flag::flags[flag];
 965   return f->is_command_line();
 966 }
 967 
 968 bool CommandLineFlags::wasSetOnCmdline(const char* name, bool* value) {
 969   Flag* result = Flag::find_flag((char*)name, strlen(name));
 970   if (result == NULL) return false;
 971   *value = result->is_command_line();
 972   return true;
 973 }
 974 
 975 void CommandLineFlagsEx::setOnCmdLine(CommandLineFlagWithType flag) {
 976   Flag* faddr = address_of_flag(flag);
 977   assert(faddr != NULL, "Unknown flag");
 978   faddr->set_command_line();
 979 }
 980 
 981 template<class E, class T>
 982 static void trace_flag_changed(const char* name, const T old_value, const T new_value, const Flag::Flags origin) {
 983   E e;
 984   e.set_name(name);
 985   e.set_oldValue(old_value);
 986   e.set_newValue(new_value);
 987   e.set_origin(origin);
 988   e.commit();
 989 }
 990 
 991 static Flag::Error apply_constraint_and_check_range_bool(const char* name, bool new_value, bool verbose) {
 992   Flag::Error status = Flag::SUCCESS;
 993   CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
 994   if (constraint != NULL) {
 995     status = constraint->apply_bool(new_value, verbose);
 996   }
 997   return status;
 998 }
 999 
1000 Flag::Error CommandLineFlags::boolAt(const char* name, size_t len, bool* value, bool allow_locked, bool return_flag) {
1001   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1002   if (result == NULL) return Flag::INVALID_FLAG;
1003   if (!result->is_bool()) return Flag::WRONG_FORMAT;
1004   *value = result->get_bool();
1005   return Flag::SUCCESS;
1006 }
1007 
1008 Flag::Error CommandLineFlags::boolAtPut(Flag* flag, bool* value, Flag::Flags origin) {
1009   const char* name;
1010   if (flag == NULL) return Flag::INVALID_FLAG;
1011   if (!flag->is_bool()) return Flag::WRONG_FORMAT;
1012   name = flag->_name;
1013   Flag::Error check = apply_constraint_and_check_range_bool(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1014   if (check != Flag::SUCCESS) return check;
1015   bool old_value = flag->get_bool();
1016   trace_flag_changed<EventBooleanFlagChanged, bool>(name, old_value, *value, origin);
1017   check = flag->set_bool(*value);
1018   *value = old_value;
1019   flag->set_origin(origin);
1020   return check;
1021 }
1022 
1023 Flag::Error CommandLineFlags::boolAtPut(const char* name, size_t len, bool* value, Flag::Flags origin) {
1024   Flag* result = Flag::find_flag(name, len);
1025   return boolAtPut(result, value, origin);
1026 }
1027 
1028 Flag::Error CommandLineFlagsEx::boolAtPut(CommandLineFlagWithType flag, bool value, Flag::Flags origin) {
1029   Flag* faddr = address_of_flag(flag);
1030   guarantee(faddr != NULL && faddr->is_bool(), "wrong flag type");
1031   return CommandLineFlags::boolAtPut(faddr, &value, origin);
1032 }
1033 
1034 static Flag::Error apply_constraint_and_check_range_int(const char* name, int new_value, bool verbose) {
1035   Flag::Error status = Flag::SUCCESS;
1036   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1037   if (range != NULL) {
1038     status = range->check_int(new_value, verbose);
1039   }
1040   if (status == Flag::SUCCESS) {
1041     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1042     if (constraint != NULL) {
1043       status = constraint->apply_int(new_value, verbose);
1044     }
1045   }
1046   return status;
1047 }
1048 
1049 Flag::Error CommandLineFlags::intAt(const char* name, size_t len, int* value, bool allow_locked, bool return_flag) {
1050   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1051   if (result == NULL) return Flag::INVALID_FLAG;
1052   if (!result->is_int()) return Flag::WRONG_FORMAT;
1053   *value = result->get_int();
1054   return Flag::SUCCESS;
1055 }
1056 
1057 Flag::Error CommandLineFlags::intAtPut(Flag* flag, int* value, Flag::Flags origin) {
1058   const char* name;
1059   if (flag == NULL) return Flag::INVALID_FLAG;
1060   if (!flag->is_int()) return Flag::WRONG_FORMAT;
1061   name = flag->_name;
1062   Flag::Error check = apply_constraint_and_check_range_int(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1063   if (check != Flag::SUCCESS) return check;
1064   int old_value = flag->get_int();
1065   trace_flag_changed<EventIntFlagChanged, s4>(name, old_value, *value, origin);
1066   check = flag->set_int(*value);
1067   *value = old_value;
1068   flag->set_origin(origin);
1069   return check;
1070 }
1071 
1072 Flag::Error CommandLineFlags::intAtPut(const char* name, size_t len, int* value, Flag::Flags origin) {
1073   Flag* result = Flag::find_flag(name, len);
1074   return intAtPut(result, value, origin);
1075 }
1076 
1077 Flag::Error CommandLineFlagsEx::intAtPut(CommandLineFlagWithType flag, int value, Flag::Flags origin) {
1078   Flag* faddr = address_of_flag(flag);
1079   guarantee(faddr != NULL && faddr->is_int(), "wrong flag type");
1080   return CommandLineFlags::intAtPut(faddr, &value, origin);
1081 }
1082 
1083 static Flag::Error apply_constraint_and_check_range_uint(const char* name, uint new_value, bool verbose) {
1084   Flag::Error status = Flag::SUCCESS;
1085   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1086   if (range != NULL) {
1087     status = range->check_uint(new_value, verbose);
1088   }
1089   if (status == Flag::SUCCESS) {
1090     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1091     if (constraint != NULL) {
1092       status = constraint->apply_uint(new_value, verbose);
1093     }
1094   }
1095   return status;
1096 }
1097 
1098 Flag::Error CommandLineFlags::uintAt(const char* name, size_t len, uint* value, bool allow_locked, bool return_flag) {
1099   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1100   if (result == NULL) return Flag::INVALID_FLAG;
1101   if (!result->is_uint()) return Flag::WRONG_FORMAT;
1102   *value = result->get_uint();
1103   return Flag::SUCCESS;
1104 }
1105 
1106 Flag::Error CommandLineFlags::uintAtPut(Flag* flag, uint* value, Flag::Flags origin) {
1107   const char* name;
1108   if (flag == NULL) return Flag::INVALID_FLAG;
1109   if (!flag->is_uint()) return Flag::WRONG_FORMAT;
1110   name = flag->_name;
1111   Flag::Error check = apply_constraint_and_check_range_uint(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1112   if (check != Flag::SUCCESS) return check;
1113   uint old_value = flag->get_uint();
1114   trace_flag_changed<EventUnsignedIntFlagChanged, u4>(name, old_value, *value, origin);
1115   check = flag->set_uint(*value);
1116   *value = old_value;
1117   flag->set_origin(origin);
1118   return check;
1119 }
1120 
1121 Flag::Error CommandLineFlags::uintAtPut(const char* name, size_t len, uint* value, Flag::Flags origin) {
1122   Flag* result = Flag::find_flag(name, len);
1123   return uintAtPut(result, value, origin);
1124 }
1125 
1126 Flag::Error CommandLineFlagsEx::uintAtPut(CommandLineFlagWithType flag, uint value, Flag::Flags origin) {
1127   Flag* faddr = address_of_flag(flag);
1128   guarantee(faddr != NULL && faddr->is_uint(), "wrong flag type");
1129   return CommandLineFlags::uintAtPut(faddr, &value, origin);
1130 }
1131 
1132 Flag::Error CommandLineFlags::intxAt(const char* name, size_t len, intx* value, bool allow_locked, bool return_flag) {
1133   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1134   if (result == NULL) return Flag::INVALID_FLAG;
1135   if (!result->is_intx()) return Flag::WRONG_FORMAT;
1136   *value = result->get_intx();
1137   return Flag::SUCCESS;
1138 }
1139 
1140 static Flag::Error apply_constraint_and_check_range_intx(const char* name, intx new_value, bool verbose) {
1141   Flag::Error status = Flag::SUCCESS;
1142   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1143   if (range != NULL) {
1144     status = range->check_intx(new_value, verbose);
1145   }
1146   if (status == Flag::SUCCESS) {
1147     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1148     if (constraint != NULL) {
1149       status = constraint->apply_intx(new_value, verbose);
1150     }
1151   }
1152   return status;
1153 }
1154 
1155 Flag::Error CommandLineFlags::intxAtPut(Flag* flag, intx* value, Flag::Flags origin) {
1156   const char* name;
1157   if (flag == NULL) return Flag::INVALID_FLAG;
1158   if (!flag->is_intx()) return Flag::WRONG_FORMAT;
1159   name = flag->_name;
1160   Flag::Error check = apply_constraint_and_check_range_intx(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1161   if (check != Flag::SUCCESS) return check;
1162   intx old_value = flag->get_intx();
1163   trace_flag_changed<EventLongFlagChanged, intx>(name, old_value, *value, origin);
1164   check = flag->set_intx(*value);
1165   *value = old_value;
1166   flag->set_origin(origin);
1167   return check;
1168 }
1169 
1170 Flag::Error CommandLineFlags::intxAtPut(const char* name, size_t len, intx* value, Flag::Flags origin) {
1171   Flag* result = Flag::find_flag(name, len);
1172   return intxAtPut(result, value, origin);
1173 }
1174 
1175 Flag::Error CommandLineFlagsEx::intxAtPut(CommandLineFlagWithType flag, intx value, Flag::Flags origin) {
1176   Flag* faddr = address_of_flag(flag);
1177   guarantee(faddr != NULL && faddr->is_intx(), "wrong flag type");
1178   return CommandLineFlags::intxAtPut(faddr, &value, origin);
1179 }
1180 
1181 Flag::Error CommandLineFlags::uintxAt(const char* name, size_t len, uintx* value, bool allow_locked, bool return_flag) {
1182   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1183   if (result == NULL) return Flag::INVALID_FLAG;
1184   if (!result->is_uintx()) return Flag::WRONG_FORMAT;
1185   *value = result->get_uintx();
1186   return Flag::SUCCESS;
1187 }
1188 
1189 static Flag::Error apply_constraint_and_check_range_uintx(const char* name, uintx new_value, bool verbose) {
1190   Flag::Error status = Flag::SUCCESS;
1191   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1192   if (range != NULL) {
1193     status = range->check_uintx(new_value, verbose);
1194   }
1195   if (status == Flag::SUCCESS) {
1196     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1197     if (constraint != NULL) {
1198       status = constraint->apply_uintx(new_value, verbose);
1199     }
1200   }
1201   return status;
1202 }
1203 
1204 Flag::Error CommandLineFlags::uintxAtPut(Flag* flag, uintx* value, Flag::Flags origin) {
1205   const char* name;
1206   if (flag == NULL) return Flag::INVALID_FLAG;
1207   if (!flag->is_uintx()) return Flag::WRONG_FORMAT;
1208   name = flag->_name;
1209   Flag::Error check = apply_constraint_and_check_range_uintx(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1210   if (check != Flag::SUCCESS) return check;
1211   uintx old_value = flag->get_uintx();
1212   trace_flag_changed<EventUnsignedLongFlagChanged, u8>(name, old_value, *value, origin);
1213   check = flag->set_uintx(*value);
1214   *value = old_value;
1215   flag->set_origin(origin);
1216   return check;
1217 }
1218 
1219 Flag::Error CommandLineFlags::uintxAtPut(const char* name, size_t len, uintx* value, Flag::Flags origin) {
1220   Flag* result = Flag::find_flag(name, len);
1221   return uintxAtPut(result, value, origin);
1222 }
1223 
1224 Flag::Error CommandLineFlagsEx::uintxAtPut(CommandLineFlagWithType flag, uintx value, Flag::Flags origin) {
1225   Flag* faddr = address_of_flag(flag);
1226   guarantee(faddr != NULL && faddr->is_uintx(), "wrong flag type");
1227   return CommandLineFlags::uintxAtPut(faddr, &value, origin);
1228 }
1229 
1230 Flag::Error CommandLineFlags::uint64_tAt(const char* name, size_t len, uint64_t* value, bool allow_locked, bool return_flag) {
1231   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1232   if (result == NULL) return Flag::INVALID_FLAG;
1233   if (!result->is_uint64_t()) return Flag::WRONG_FORMAT;
1234   *value = result->get_uint64_t();
1235   return Flag::SUCCESS;
1236 }
1237 
1238 static Flag::Error apply_constraint_and_check_range_uint64_t(const char* name, uint64_t new_value, bool verbose) {
1239   Flag::Error status = Flag::SUCCESS;
1240   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1241   if (range != NULL) {
1242     status = range->check_uint64_t(new_value, verbose);
1243   }
1244   if (status == Flag::SUCCESS) {
1245     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1246     if (constraint != NULL) {
1247       status = constraint->apply_uint64_t(new_value, verbose);
1248     }
1249   }
1250   return status;
1251 }
1252 
1253 Flag::Error CommandLineFlags::uint64_tAtPut(Flag* flag, uint64_t* value, Flag::Flags origin) {
1254   const char* name;
1255   if (flag == NULL) return Flag::INVALID_FLAG;
1256   if (!flag->is_uint64_t()) return Flag::WRONG_FORMAT;
1257   name = flag->_name;
1258   Flag::Error check = apply_constraint_and_check_range_uint64_t(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1259   if (check != Flag::SUCCESS) return check;
1260   uint64_t old_value = flag->get_uint64_t();
1261   trace_flag_changed<EventUnsignedLongFlagChanged, u8>(name, old_value, *value, origin);
1262   check = flag->set_uint64_t(*value);
1263   *value = old_value;
1264   flag->set_origin(origin);
1265   return check;
1266 }
1267 
1268 Flag::Error CommandLineFlags::uint64_tAtPut(const char* name, size_t len, uint64_t* value, Flag::Flags origin) {
1269   Flag* result = Flag::find_flag(name, len);
1270   return uint64_tAtPut(result, value, origin);
1271 }
1272 
1273 Flag::Error CommandLineFlagsEx::uint64_tAtPut(CommandLineFlagWithType flag, uint64_t value, Flag::Flags origin) {
1274   Flag* faddr = address_of_flag(flag);
1275   guarantee(faddr != NULL && faddr->is_uint64_t(), "wrong flag type");
1276   return CommandLineFlags::uint64_tAtPut(faddr, &value, origin);
1277 }
1278 
1279 Flag::Error CommandLineFlags::size_tAt(const char* name, size_t len, size_t* value, bool allow_locked, bool return_flag) {
1280   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1281   if (result == NULL) return Flag::INVALID_FLAG;
1282   if (!result->is_size_t()) return Flag::WRONG_FORMAT;
1283   *value = result->get_size_t();
1284   return Flag::SUCCESS;
1285 }
1286 
1287 static Flag::Error apply_constraint_and_check_range_size_t(const char* name, size_t new_value, bool verbose) {
1288   Flag::Error status = Flag::SUCCESS;
1289   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1290   if (range != NULL) {
1291     status = range->check_size_t(new_value, verbose);
1292   }
1293   if (status == Flag::SUCCESS) {
1294     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1295     if (constraint != NULL) {
1296       status = constraint->apply_size_t(new_value, verbose);
1297     }
1298   }
1299   return status;
1300 }
1301 
1302 
1303 Flag::Error CommandLineFlags::size_tAtPut(Flag* flag, size_t* value, Flag::Flags origin) {
1304   const char* name;
1305   if (flag == NULL) return Flag::INVALID_FLAG;
1306   if (!flag->is_size_t()) return Flag::WRONG_FORMAT;
1307   name = flag->_name;
1308   Flag::Error check = apply_constraint_and_check_range_size_t(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1309   if (check != Flag::SUCCESS) return check;
1310   size_t old_value = flag->get_size_t();
1311   trace_flag_changed<EventUnsignedLongFlagChanged, u8>(name, old_value, *value, origin);
1312   check = flag->set_size_t(*value);
1313   *value = old_value;
1314   flag->set_origin(origin);
1315   return check;
1316 }
1317 
1318 Flag::Error CommandLineFlags::size_tAtPut(const char* name, size_t len, size_t* value, Flag::Flags origin) {
1319   Flag* result = Flag::find_flag(name, len);
1320   return size_tAtPut(result, value, origin);
1321 }
1322 
1323 Flag::Error CommandLineFlagsEx::size_tAtPut(CommandLineFlagWithType flag, size_t value, Flag::Flags origin) {
1324   Flag* faddr = address_of_flag(flag);
1325   guarantee(faddr != NULL && faddr->is_size_t(), "wrong flag type");
1326   return CommandLineFlags::size_tAtPut(faddr, &value, origin);
1327 }
1328 
1329 Flag::Error CommandLineFlags::doubleAt(const char* name, size_t len, double* value, bool allow_locked, bool return_flag) {
1330   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1331   if (result == NULL) return Flag::INVALID_FLAG;
1332   if (!result->is_double()) return Flag::WRONG_FORMAT;
1333   *value = result->get_double();
1334   return Flag::SUCCESS;
1335 }
1336 
1337 static Flag::Error apply_constraint_and_check_range_double(const char* name, double new_value, bool verbose) {
1338   Flag::Error status = Flag::SUCCESS;
1339   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1340   if (range != NULL) {
1341     status = range->check_double(new_value, verbose);
1342   }
1343   if (status == Flag::SUCCESS) {
1344     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1345     if (constraint != NULL) {
1346       status = constraint->apply_double(new_value, verbose);
1347     }
1348   }
1349   return status;
1350 }
1351 
1352 Flag::Error CommandLineFlags::doubleAtPut(Flag* flag, double* value, Flag::Flags origin) {
1353   const char* name;
1354   if (flag == NULL) return Flag::INVALID_FLAG;
1355   if (!flag->is_double()) return Flag::WRONG_FORMAT;
1356   name = flag->_name;
1357   Flag::Error check = apply_constraint_and_check_range_double(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1358   if (check != Flag::SUCCESS) return check;
1359   double old_value = flag->get_double();
1360   trace_flag_changed<EventDoubleFlagChanged, double>(name, old_value, *value, origin);
1361   check = flag->set_double(*value);
1362   *value = old_value;
1363   flag->set_origin(origin);
1364   return check;
1365 }
1366 
1367 Flag::Error CommandLineFlags::doubleAtPut(const char* name, size_t len, double* value, Flag::Flags origin) {
1368   Flag* result = Flag::find_flag(name, len);
1369   return doubleAtPut(result, value, origin);
1370 }
1371 
1372 Flag::Error CommandLineFlagsEx::doubleAtPut(CommandLineFlagWithType flag, double value, Flag::Flags origin) {
1373   Flag* faddr = address_of_flag(flag);
1374   guarantee(faddr != NULL && faddr->is_double(), "wrong flag type");
1375   return CommandLineFlags::doubleAtPut(faddr, &value, origin);
1376 }
1377 
1378 Flag::Error CommandLineFlags::ccstrAt(const char* name, size_t len, ccstr* value, bool allow_locked, bool return_flag) {
1379   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1380   if (result == NULL) return Flag::INVALID_FLAG;
1381   if (!result->is_ccstr()) return Flag::WRONG_FORMAT;
1382   *value = result->get_ccstr();
1383   return Flag::SUCCESS;
1384 }
1385 
1386 Flag::Error CommandLineFlags::ccstrAtPut(const char* name, size_t len, ccstr* value, Flag::Flags origin) {
1387   Flag* result = Flag::find_flag(name, len);
1388   if (result == NULL) return Flag::INVALID_FLAG;
1389   if (!result->is_ccstr()) return Flag::WRONG_FORMAT;
1390   ccstr old_value = result->get_ccstr();
1391   trace_flag_changed<EventStringFlagChanged, const char*>(name, old_value, *value, origin);
1392   char* new_value = NULL;
1393   if (*value != NULL) {
1394     new_value = os::strdup_check_oom(*value);
1395   }
1396   Flag::Error check = result->set_ccstr(new_value);
1397   if (result->is_default() && old_value != NULL) {
1398     // Prior value is NOT heap allocated, but was a literal constant.
1399     old_value = os::strdup_check_oom(old_value);
1400   }
1401   *value = old_value;
1402   result->set_origin(origin);
1403   return check;
1404 }
1405 
1406 Flag::Error CommandLineFlagsEx::ccstrAtPut(CommandLineFlagWithType flag, ccstr value, Flag::Flags origin) {
1407   Flag* faddr = address_of_flag(flag);
1408   guarantee(faddr != NULL && faddr->is_ccstr(), "wrong flag type");
1409   ccstr old_value = faddr->get_ccstr();
1410   trace_flag_changed<EventStringFlagChanged, const char*>(faddr->_name, old_value, value, origin);
1411   char* new_value = os::strdup_check_oom(value);
1412   Flag::Error check = faddr->set_ccstr(new_value);
1413   if (!faddr->is_default() && old_value != NULL) {
1414     // Prior value is heap allocated so free it.
1415     FREE_C_HEAP_ARRAY(char, old_value);
1416   }
1417   faddr->set_origin(origin);
1418   return check;
1419 }
1420 
1421 extern "C" {
1422   static int compare_flags(const void* void_a, const void* void_b) {
1423     return strcmp((*((Flag**) void_a))->_name, (*((Flag**) void_b))->_name);
1424   }
1425 }
1426 
1427 void CommandLineFlags::printSetFlags(outputStream* out) {
1428   // Print which flags were set on the command line
1429   // note: this method is called before the thread structure is in place
1430   //       which means resource allocation cannot be used.
1431 
1432   // The last entry is the null entry.
1433   const size_t length = Flag::numFlags - 1;
1434 
1435   // Sort
1436   Flag** array = NEW_C_HEAP_ARRAY(Flag*, length, mtArguments);
1437   for (size_t i = 0; i < length; i++) {
1438     array[i] = &flagTable[i];
1439   }
1440   qsort(array, length, sizeof(Flag*), compare_flags);
1441 
1442   // Print
1443   for (size_t i = 0; i < length; i++) {
1444     if (array[i]->get_origin() /* naked field! */) {
1445       array[i]->print_as_flag(out);
1446       out->print(" ");
1447     }
1448   }
1449   out->cr();
1450   FREE_C_HEAP_ARRAY(Flag*, array);
1451 }
1452 
1453 #ifndef PRODUCT
1454 
1455 void CommandLineFlags::verify() {
1456   assert(Arguments::check_vm_args_consistency(), "Some flag settings conflict");
1457 }
1458 
1459 #endif // PRODUCT
1460 
1461 void CommandLineFlags::printFlags(outputStream* out, bool withComments, bool printRanges) {
1462   // Print the flags sorted by name
1463   // note: this method is called before the thread structure is in place
1464   //       which means resource allocation cannot be used.
1465 
1466   // The last entry is the null entry.
1467   const size_t length = Flag::numFlags - 1;
1468 
1469   // Sort
1470   Flag** array = NEW_C_HEAP_ARRAY(Flag*, length, mtArguments);
1471   for (size_t i = 0; i < length; i++) {
1472     array[i] = &flagTable[i];
1473   }
1474   qsort(array, length, sizeof(Flag*), compare_flags);
1475 
1476   // Print
1477   if (!printRanges) {
1478     out->print_cr("[Global flags]");
1479   } else {
1480     out->print_cr("[Global flags ranges]");
1481   }
1482 
1483   for (size_t i = 0; i < length; i++) {
1484     if (array[i]->is_unlocked()) {
1485       array[i]->print_on(out, withComments, printRanges);
1486     }
1487   }
1488   FREE_C_HEAP_ARRAY(Flag*, array);
1489 }
--- EOF ---