rev 12397 : 8170981: Possible access to char array with negative index

   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     // The print below assumes that the flag name is 40 characters or less.
 473     // This works for most flags, but there are exceptions. Our longest flag
 474     // name right now is UseAdaptiveGenerationSizePolicyAtMajorCollection and
 475     // its minor collection buddy. These are 48 characters. We use a buffer of
 476     // 10 spaces below to adjust the space between the flag value and the
 477     // column of flag type and origin that is printed in the end of the line.
 478     char spaces[10 + 1] = "          ";
 479     st->print("%9s %-40s = ", _type, _name);
 480 
 481     if (is_bool()) {
 482       st->print("%-20s", get_bool() ? "true" : "false");
 483     } else if (is_int()) {
 484       st->print("%-20d", get_int());
 485     } else if (is_uint()) {
 486       st->print("%-20u", get_uint());
 487     } else if (is_intx()) {
 488       st->print(INTX_FORMAT_W(-20), get_intx());
 489     } else if (is_uintx()) {
 490       st->print(UINTX_FORMAT_W(-20), get_uintx());
 491     } else if (is_uint64_t()) {
 492       st->print(UINT64_FORMAT_W(-20), get_uint64_t());
 493     } else if (is_size_t()) {
 494       st->print(SIZE_FORMAT_W(-20), get_size_t());
 495     } else if (is_double()) {
 496       st->print("%-20f", get_double());
 497     } else if (is_ccstr()) {
 498       const char* cp = get_ccstr();
 499       if (cp != NULL) {
 500         const char* eol;
 501         while ((eol = strchr(cp, '\n')) != NULL) {
 502           size_t llen = pointer_delta(eol, cp, sizeof(char));
 503           st->print("%.*s", (int)llen, cp);
 504           st->cr();
 505           cp = eol+1;
 506           st->print("%5s %-35s += ", "", _name);
 507         }
 508         st->print("%-20s", cp);
 509       }
 510       else st->print("%-20s", "");
 511     }
 512     assert(strlen(_name) < 50, "Flag name is longer than expected");
 513     spaces[50 - MAX2((size_t)40, strlen(_name))] = '\0';


 514     st->print("%s", spaces);

 515     print_kind_and_origin(st);
 516 
 517 #ifndef PRODUCT
 518     if (withComments) {
 519       st->print("%s", _doc);
 520     }
 521 #endif
 522 
 523     st->cr();
 524 
 525   } else if (!is_bool() && !is_ccstr()) {
 526     st->print("%9s %-50s ", _type, _name);
 527 
 528     RangeStrFunc func = NULL;
 529     if (is_int()) {
 530       func = Flag::get_int_default_range_str;
 531     } else if (is_uint()) {
 532       func = Flag::get_uint_default_range_str;
 533     } else if (is_intx()) {
 534       func = Flag::get_intx_default_range_str;
 535     } else if (is_uintx()) {
 536       func = Flag::get_uintx_default_range_str;
 537     } else if (is_uint64_t()) {
 538       func = Flag::get_uint64_t_default_range_str;
 539     } else if (is_size_t()) {
 540       func = Flag::get_size_t_default_range_str;
 541     } else if (is_double()) {
 542       func = Flag::get_double_default_range_str;
 543     } else {
 544       ShouldNotReachHere();
 545     }
 546     CommandLineFlagRangeList::print(st, _name, func);
 547 
 548     st->print(" %-16s", " ");
 549     print_kind_and_origin(st);
 550 
 551 #ifndef PRODUCT
 552     if (withComments) {
 553       st->print("%s", _doc);
 554     }
 555 #endif
 556 
 557     st->cr();
 558   }
 559 }
 560 
 561 void Flag::print_kind_and_origin(outputStream* st) {
 562   struct Data {
 563     int flag;
 564     const char* name;
 565   };
 566 
 567   Data data[] = {
 568       { KIND_JVMCI, "JVMCI" },
 569       { KIND_C1, "C1" },
 570       { KIND_C2, "C2" },
 571       { KIND_ARCH, "ARCH" },
 572       { KIND_SHARK, "SHARK" },
 573       { KIND_PLATFORM_DEPENDENT, "pd" },
 574       { KIND_PRODUCT, "product" },
 575       { KIND_MANAGEABLE, "manageable" },
 576       { KIND_DIAGNOSTIC, "diagnostic" },
 577       { KIND_EXPERIMENTAL, "experimental" },
 578       { KIND_COMMERCIAL, "commercial" },
 579       { KIND_NOT_PRODUCT, "notproduct" },
 580       { KIND_DEVELOP, "develop" },
 581       { KIND_LP64_PRODUCT, "lp64_product" },
 582       { KIND_READ_WRITE, "rw" },
 583       { -1, "" }
 584   };
 585 
 586   if ((_flags & KIND_MASK) != 0) {
 587     bool is_first = true;
 588     const size_t buffer_size = 64;
 589     size_t buffer_used = 0;
 590     char kind[buffer_size];
 591 
 592     jio_snprintf(kind, buffer_size, "{");
 593     buffer_used++;
 594     for (int i = 0; data[i].flag != -1; i++) {
 595       Data d = data[i];
 596       if ((_flags & d.flag) != 0) {
 597         if (is_first) {
 598           is_first = false;
 599         } else {
 600           assert(buffer_used + 1 < buffer_size, "Too small buffer");
 601           jio_snprintf(kind + buffer_used, buffer_size - buffer_used, " ");
 602           buffer_used++;
 603         }
 604         size_t length = strlen(d.name);
 605         assert(buffer_used + length < buffer_size, "Too small buffer");
 606         jio_snprintf(kind + buffer_used, buffer_size - buffer_used, "%s", d.name);
 607         buffer_used += length;
 608       }
 609     }
 610     assert(buffer_used + 2 <= buffer_size, "Too small buffer");
 611     jio_snprintf(kind + buffer_used, buffer_size - buffer_used, "}");
 612     st->print("%20s", kind);
 613   }
 614 
 615   int origin = _flags & VALUE_ORIGIN_MASK;
 616   st->print(" {");
 617   switch(origin) {
 618     case DEFAULT:
 619       st->print("default"); break;
 620     case COMMAND_LINE:
 621       st->print("command line"); break;
 622     case ENVIRON_VAR:
 623       st->print("environment"); break;
 624     case CONFIG_FILE:
 625       st->print("config file"); break;
 626     case MANAGEMENT:
 627       st->print("management"); break;
 628     case ERGONOMIC:
 629       if (_flags & ORIG_COMMAND_LINE) {
 630         st->print("command line, ");
 631       }
 632       st->print("ergonomic"); break;
 633     case ATTACH_ON_DEMAND:
 634       st->print("attach"); break;
 635     case INTERNAL:
 636       st->print("internal"); break;
 637   }
 638   st->print("}");
 639 }
 640 
 641 void Flag::print_as_flag(outputStream* st) {
 642   if (is_bool()) {
 643     st->print("-XX:%s%s", get_bool() ? "+" : "-", _name);
 644   } else if (is_int()) {
 645     st->print("-XX:%s=%d", _name, get_int());
 646   } else if (is_uint()) {
 647     st->print("-XX:%s=%u", _name, get_uint());
 648   } else if (is_intx()) {
 649     st->print("-XX:%s=" INTX_FORMAT, _name, get_intx());
 650   } else if (is_uintx()) {
 651     st->print("-XX:%s=" UINTX_FORMAT, _name, get_uintx());
 652   } else if (is_uint64_t()) {
 653     st->print("-XX:%s=" UINT64_FORMAT, _name, get_uint64_t());
 654   } else if (is_size_t()) {
 655     st->print("-XX:%s=" SIZE_FORMAT, _name, get_size_t());
 656   } else if (is_double()) {
 657     st->print("-XX:%s=%f", _name, get_double());
 658   } else if (is_ccstr()) {
 659     st->print("-XX:%s=", _name);
 660     const char* cp = get_ccstr();
 661     if (cp != NULL) {
 662       // Need to turn embedded '\n's back into separate arguments
 663       // Not so efficient to print one character at a time,
 664       // but the choice is to do the transformation to a buffer
 665       // and print that.  And this need not be efficient.
 666       for (; *cp != '\0'; cp += 1) {
 667         switch (*cp) {
 668           default:
 669             st->print("%c", *cp);
 670             break;
 671           case '\n':
 672             st->print(" -XX:%s=", _name);
 673             break;
 674         }
 675       }
 676     }
 677   } else {
 678     ShouldNotReachHere();
 679   }
 680 }
 681 
 682 const char* Flag::flag_error_str(Flag::Error error) {
 683   switch (error) {
 684     case Flag::MISSING_NAME: return "MISSING_NAME";
 685     case Flag::MISSING_VALUE: return "MISSING_VALUE";
 686     case Flag::NON_WRITABLE: return "NON_WRITABLE";
 687     case Flag::OUT_OF_BOUNDS: return "OUT_OF_BOUNDS";
 688     case Flag::VIOLATES_CONSTRAINT: return "VIOLATES_CONSTRAINT";
 689     case Flag::INVALID_FLAG: return "INVALID_FLAG";
 690     case Flag::ERR_OTHER: return "ERR_OTHER";
 691     case Flag::SUCCESS: return "SUCCESS";
 692     default: ShouldNotReachHere(); return "NULL";
 693   }
 694 }
 695 
 696 // 4991491 do not "optimize out" the was_set false values: omitting them
 697 // tickles a Microsoft compiler bug causing flagTable to be malformed
 698 
 699 #define RUNTIME_PRODUCT_FLAG_STRUCT(     type, name, value, doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_PRODUCT) },
 700 #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) },
 701 #define RUNTIME_DIAGNOSTIC_FLAG_STRUCT(  type, name, value, doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_DIAGNOSTIC) },
 702 #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) },
 703 #define RUNTIME_EXPERIMENTAL_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_EXPERIMENTAL) },
 704 #define RUNTIME_MANAGEABLE_FLAG_STRUCT(  type, name, value, doc) { #type, XSTR(name), &name,         NOT_PRODUCT_ARG(doc) Flag::Flags(Flag::DEFAULT | Flag::KIND_MANAGEABLE) },
 705 #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) },
 706 #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) },
 707 #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) },
 708 #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) },
 709 
 710 #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) },
 711 #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) },
 712 #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) },
 713 #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) },
 714 #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) },
 715 #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) },
 716 #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) },
 717 #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) },
 718 
 719 #ifdef _LP64
 720 #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) },
 721 #else
 722 #define RUNTIME_LP64_PRODUCT_FLAG_STRUCT(type, name, value, doc) /* flag is constant */
 723 #endif // _LP64
 724 
 725 #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) },
 726 #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) },
 727 #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) },
 728 #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) },
 729 #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) },
 730 #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) },
 731 #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) },
 732 
 733 #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) },
 734 #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) },
 735 #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) },
 736 #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) },
 737 #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) },
 738 #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) },
 739 #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) },
 740 #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) },
 741 
 742 #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) },
 743 #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) },
 744 #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) },
 745 #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) },
 746 #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) },
 747 
 748 #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) },
 749 #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) },
 750 #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) },
 751 #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) },
 752 #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) },
 753 #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) },
 754 #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) },
 755 
 756 static Flag flagTable[] = {
 757  RUNTIME_FLAGS(RUNTIME_DEVELOP_FLAG_STRUCT, \
 758                RUNTIME_PD_DEVELOP_FLAG_STRUCT, \
 759                RUNTIME_PRODUCT_FLAG_STRUCT, \
 760                RUNTIME_PD_PRODUCT_FLAG_STRUCT, \
 761                RUNTIME_DIAGNOSTIC_FLAG_STRUCT, \
 762                RUNTIME_PD_DIAGNOSTIC_FLAG_STRUCT, \
 763                RUNTIME_EXPERIMENTAL_FLAG_STRUCT, \
 764                RUNTIME_NOTPRODUCT_FLAG_STRUCT, \
 765                RUNTIME_MANAGEABLE_FLAG_STRUCT, \
 766                RUNTIME_PRODUCT_RW_FLAG_STRUCT, \
 767                RUNTIME_LP64_PRODUCT_FLAG_STRUCT, \
 768                IGNORE_RANGE, \
 769                IGNORE_CONSTRAINT, \
 770                IGNORE_WRITEABLE)
 771  RUNTIME_OS_FLAGS(RUNTIME_DEVELOP_FLAG_STRUCT, \
 772                   RUNTIME_PD_DEVELOP_FLAG_STRUCT, \
 773                   RUNTIME_PRODUCT_FLAG_STRUCT, \
 774                   RUNTIME_PD_PRODUCT_FLAG_STRUCT, \
 775                   RUNTIME_DIAGNOSTIC_FLAG_STRUCT, \
 776                   RUNTIME_PD_DIAGNOSTIC_FLAG_STRUCT, \
 777                   RUNTIME_NOTPRODUCT_FLAG_STRUCT, \
 778                   IGNORE_RANGE, \
 779                   IGNORE_CONSTRAINT, \
 780                   IGNORE_WRITEABLE)
 781 #if INCLUDE_ALL_GCS
 782  G1_FLAGS(RUNTIME_DEVELOP_FLAG_STRUCT, \
 783           RUNTIME_PD_DEVELOP_FLAG_STRUCT, \
 784           RUNTIME_PRODUCT_FLAG_STRUCT, \
 785           RUNTIME_PD_PRODUCT_FLAG_STRUCT, \
 786           RUNTIME_DIAGNOSTIC_FLAG_STRUCT, \
 787           RUNTIME_PD_DIAGNOSTIC_FLAG_STRUCT, \
 788           RUNTIME_EXPERIMENTAL_FLAG_STRUCT, \
 789           RUNTIME_NOTPRODUCT_FLAG_STRUCT, \
 790           RUNTIME_MANAGEABLE_FLAG_STRUCT, \
 791           RUNTIME_PRODUCT_RW_FLAG_STRUCT, \
 792           IGNORE_RANGE, \
 793           IGNORE_CONSTRAINT, \
 794           IGNORE_WRITEABLE)
 795 #endif // INCLUDE_ALL_GCS
 796 #if INCLUDE_JVMCI
 797  JVMCI_FLAGS(JVMCI_DEVELOP_FLAG_STRUCT, \
 798              JVMCI_PD_DEVELOP_FLAG_STRUCT, \
 799              JVMCI_PRODUCT_FLAG_STRUCT, \
 800              JVMCI_PD_PRODUCT_FLAG_STRUCT, \
 801              JVMCI_DIAGNOSTIC_FLAG_STRUCT, \
 802              JVMCI_PD_DIAGNOSTIC_FLAG_STRUCT, \
 803              JVMCI_EXPERIMENTAL_FLAG_STRUCT, \
 804              JVMCI_NOTPRODUCT_FLAG_STRUCT, \
 805              IGNORE_RANGE, \
 806              IGNORE_CONSTRAINT, \
 807              IGNORE_WRITEABLE)
 808 #endif // INCLUDE_JVMCI
 809 #ifdef COMPILER1
 810  C1_FLAGS(C1_DEVELOP_FLAG_STRUCT, \
 811           C1_PD_DEVELOP_FLAG_STRUCT, \
 812           C1_PRODUCT_FLAG_STRUCT, \
 813           C1_PD_PRODUCT_FLAG_STRUCT, \
 814           C1_DIAGNOSTIC_FLAG_STRUCT, \
 815           C1_PD_DIAGNOSTIC_FLAG_STRUCT, \
 816           C1_NOTPRODUCT_FLAG_STRUCT, \
 817           IGNORE_RANGE, \
 818           IGNORE_CONSTRAINT, \
 819           IGNORE_WRITEABLE)
 820 #endif // COMPILER1
 821 #ifdef COMPILER2
 822  C2_FLAGS(C2_DEVELOP_FLAG_STRUCT, \
 823           C2_PD_DEVELOP_FLAG_STRUCT, \
 824           C2_PRODUCT_FLAG_STRUCT, \
 825           C2_PD_PRODUCT_FLAG_STRUCT, \
 826           C2_DIAGNOSTIC_FLAG_STRUCT, \
 827           C2_PD_DIAGNOSTIC_FLAG_STRUCT, \
 828           C2_EXPERIMENTAL_FLAG_STRUCT, \
 829           C2_NOTPRODUCT_FLAG_STRUCT, \
 830           IGNORE_RANGE, \
 831           IGNORE_CONSTRAINT, \
 832           IGNORE_WRITEABLE)
 833 #endif // COMPILER2
 834 #ifdef SHARK
 835  SHARK_FLAGS(SHARK_DEVELOP_FLAG_STRUCT, \
 836              SHARK_PD_DEVELOP_FLAG_STRUCT, \
 837              SHARK_PRODUCT_FLAG_STRUCT, \
 838              SHARK_PD_PRODUCT_FLAG_STRUCT, \
 839              SHARK_DIAGNOSTIC_FLAG_STRUCT, \
 840              SHARK_PD_DIAGNOSTIC_FLAG_STRUCT, \
 841              SHARK_NOTPRODUCT_FLAG_STRUCT, \
 842              IGNORE_RANGE, \
 843              IGNORE_CONSTRAINT, \
 844              IGNORE_WRITEABLE)
 845 #endif // SHARK
 846  ARCH_FLAGS(ARCH_DEVELOP_FLAG_STRUCT, \
 847             ARCH_PRODUCT_FLAG_STRUCT, \
 848             ARCH_DIAGNOSTIC_FLAG_STRUCT, \
 849             ARCH_EXPERIMENTAL_FLAG_STRUCT, \
 850             ARCH_NOTPRODUCT_FLAG_STRUCT, \
 851             IGNORE_RANGE, \
 852             IGNORE_CONSTRAINT, \
 853             IGNORE_WRITEABLE)
 854  FLAGTABLE_EXT
 855  {0, NULL, NULL}
 856 };
 857 
 858 Flag* Flag::flags = flagTable;
 859 size_t Flag::numFlags = (sizeof(flagTable) / sizeof(Flag));
 860 
 861 inline bool str_equal(const char* s, const char* q, size_t len) {
 862   // s is null terminated, q is not!
 863   if (strlen(s) != (unsigned int) len) return false;
 864   return strncmp(s, q, len) == 0;
 865 }
 866 
 867 // Search the flag table for a named flag
 868 Flag* Flag::find_flag(const char* name, size_t length, bool allow_locked, bool return_flag) {
 869   for (Flag* current = &flagTable[0]; current->_name != NULL; current++) {
 870     if (str_equal(current->_name, name, length)) {
 871       // Found a matching entry.
 872       // Don't report notproduct and develop flags in product builds.
 873       if (current->is_constant_in_binary()) {
 874         return (return_flag ? current : NULL);
 875       }
 876       // Report locked flags only if allowed.
 877       if (!(current->is_unlocked() || current->is_unlocker())) {
 878         if (!allow_locked) {
 879           // disable use of locked flags, e.g. diagnostic, experimental,
 880           // commercial... until they are explicitly unlocked
 881           return NULL;
 882         }
 883       }
 884       return current;
 885     }
 886   }
 887   // Flag name is not in the flag table
 888   return NULL;
 889 }
 890 
 891 // Compute string similarity based on Dice's coefficient
 892 static float str_similar(const char* str1, const char* str2, size_t len2) {
 893   int len1 = (int) strlen(str1);
 894   int total = len1 + (int) len2;
 895 
 896   int hit = 0;
 897 
 898   for (int i = 0; i < len1 -1; ++i) {
 899     for (int j = 0; j < (int) len2 -1; ++j) {
 900       if ((str1[i] == str2[j]) && (str1[i+1] == str2[j+1])) {
 901         ++hit;
 902         break;
 903       }
 904     }
 905   }
 906 
 907   return 2.0f * (float) hit / (float) total;
 908 }
 909 
 910 Flag* Flag::fuzzy_match(const char* name, size_t length, bool allow_locked) {
 911   float VMOptionsFuzzyMatchSimilarity = 0.7f;
 912   Flag* match = NULL;
 913   float score;
 914   float max_score = -1;
 915 
 916   for (Flag* current = &flagTable[0]; current->_name != NULL; current++) {
 917     score = str_similar(current->_name, name, length);
 918     if (score > max_score) {
 919       max_score = score;
 920       match = current;
 921     }
 922   }
 923 
 924   if (!(match->is_unlocked() || match->is_unlocker())) {
 925     if (!allow_locked) {
 926       return NULL;
 927     }
 928   }
 929 
 930   if (max_score < VMOptionsFuzzyMatchSimilarity) {
 931     return NULL;
 932   }
 933 
 934   return match;
 935 }
 936 
 937 // Returns the address of the index'th element
 938 static Flag* address_of_flag(CommandLineFlagWithType flag) {
 939   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 940   return &Flag::flags[flag];
 941 }
 942 
 943 bool CommandLineFlagsEx::is_default(CommandLineFlag flag) {
 944   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 945   Flag* f = &Flag::flags[flag];
 946   return f->is_default();
 947 }
 948 
 949 bool CommandLineFlagsEx::is_ergo(CommandLineFlag flag) {
 950   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 951   Flag* f = &Flag::flags[flag];
 952   return f->is_ergonomic();
 953 }
 954 
 955 bool CommandLineFlagsEx::is_cmdline(CommandLineFlag flag) {
 956   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 957   Flag* f = &Flag::flags[flag];
 958   return f->is_command_line();
 959 }
 960 
 961 bool CommandLineFlags::wasSetOnCmdline(const char* name, bool* value) {
 962   Flag* result = Flag::find_flag((char*)name, strlen(name));
 963   if (result == NULL) return false;
 964   *value = result->is_command_line();
 965   return true;
 966 }
 967 
 968 void CommandLineFlagsEx::setOnCmdLine(CommandLineFlagWithType flag) {
 969   Flag* faddr = address_of_flag(flag);
 970   assert(faddr != NULL, "Unknown flag");
 971   faddr->set_command_line();
 972 }
 973 
 974 template<class E, class T>
 975 static void trace_flag_changed(const char* name, const T old_value, const T new_value, const Flag::Flags origin) {
 976   E e;
 977   e.set_name(name);
 978   e.set_oldValue(old_value);
 979   e.set_newValue(new_value);
 980   e.set_origin(origin);
 981   e.commit();
 982 }
 983 
 984 static Flag::Error apply_constraint_and_check_range_bool(const char* name, bool new_value, bool verbose) {
 985   Flag::Error status = Flag::SUCCESS;
 986   CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
 987   if (constraint != NULL) {
 988     status = constraint->apply_bool(new_value, verbose);
 989   }
 990   return status;
 991 }
 992 
 993 Flag::Error CommandLineFlags::boolAt(const char* name, size_t len, bool* value, bool allow_locked, bool return_flag) {
 994   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
 995   if (result == NULL) return Flag::INVALID_FLAG;
 996   if (!result->is_bool()) return Flag::WRONG_FORMAT;
 997   *value = result->get_bool();
 998   return Flag::SUCCESS;
 999 }
1000 
1001 Flag::Error CommandLineFlags::boolAtPut(Flag* flag, bool* value, Flag::Flags origin) {
1002   const char* name;
1003   if (flag == NULL) return Flag::INVALID_FLAG;
1004   if (!flag->is_bool()) return Flag::WRONG_FORMAT;
1005   name = flag->_name;
1006   Flag::Error check = apply_constraint_and_check_range_bool(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1007   if (check != Flag::SUCCESS) return check;
1008   bool old_value = flag->get_bool();
1009   trace_flag_changed<EventBooleanFlagChanged, bool>(name, old_value, *value, origin);
1010   check = flag->set_bool(*value);
1011   *value = old_value;
1012   flag->set_origin(origin);
1013   return check;
1014 }
1015 
1016 Flag::Error CommandLineFlags::boolAtPut(const char* name, size_t len, bool* value, Flag::Flags origin) {
1017   Flag* result = Flag::find_flag(name, len);
1018   return boolAtPut(result, value, origin);
1019 }
1020 
1021 Flag::Error CommandLineFlagsEx::boolAtPut(CommandLineFlagWithType flag, bool value, Flag::Flags origin) {
1022   Flag* faddr = address_of_flag(flag);
1023   guarantee(faddr != NULL && faddr->is_bool(), "wrong flag type");
1024   return CommandLineFlags::boolAtPut(faddr, &value, origin);
1025 }
1026 
1027 static Flag::Error apply_constraint_and_check_range_int(const char* name, int new_value, bool verbose) {
1028   Flag::Error status = Flag::SUCCESS;
1029   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1030   if (range != NULL) {
1031     status = range->check_int(new_value, verbose);
1032   }
1033   if (status == Flag::SUCCESS) {
1034     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1035     if (constraint != NULL) {
1036       status = constraint->apply_int(new_value, verbose);
1037     }
1038   }
1039   return status;
1040 }
1041 
1042 Flag::Error CommandLineFlags::intAt(const char* name, size_t len, int* value, bool allow_locked, bool return_flag) {
1043   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1044   if (result == NULL) return Flag::INVALID_FLAG;
1045   if (!result->is_int()) return Flag::WRONG_FORMAT;
1046   *value = result->get_int();
1047   return Flag::SUCCESS;
1048 }
1049 
1050 Flag::Error CommandLineFlags::intAtPut(Flag* flag, int* value, Flag::Flags origin) {
1051   const char* name;
1052   if (flag == NULL) return Flag::INVALID_FLAG;
1053   if (!flag->is_int()) return Flag::WRONG_FORMAT;
1054   name = flag->_name;
1055   Flag::Error check = apply_constraint_and_check_range_int(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1056   if (check != Flag::SUCCESS) return check;
1057   int old_value = flag->get_int();
1058   trace_flag_changed<EventIntFlagChanged, s4>(name, old_value, *value, origin);
1059   check = flag->set_int(*value);
1060   *value = old_value;
1061   flag->set_origin(origin);
1062   return check;
1063 }
1064 
1065 Flag::Error CommandLineFlags::intAtPut(const char* name, size_t len, int* value, Flag::Flags origin) {
1066   Flag* result = Flag::find_flag(name, len);
1067   return intAtPut(result, value, origin);
1068 }
1069 
1070 Flag::Error CommandLineFlagsEx::intAtPut(CommandLineFlagWithType flag, int value, Flag::Flags origin) {
1071   Flag* faddr = address_of_flag(flag);
1072   guarantee(faddr != NULL && faddr->is_int(), "wrong flag type");
1073   return CommandLineFlags::intAtPut(faddr, &value, origin);
1074 }
1075 
1076 static Flag::Error apply_constraint_and_check_range_uint(const char* name, uint new_value, bool verbose) {
1077   Flag::Error status = Flag::SUCCESS;
1078   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1079   if (range != NULL) {
1080     status = range->check_uint(new_value, verbose);
1081   }
1082   if (status == Flag::SUCCESS) {
1083     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1084     if (constraint != NULL) {
1085       status = constraint->apply_uint(new_value, verbose);
1086     }
1087   }
1088   return status;
1089 }
1090 
1091 Flag::Error CommandLineFlags::uintAt(const char* name, size_t len, uint* value, bool allow_locked, bool return_flag) {
1092   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1093   if (result == NULL) return Flag::INVALID_FLAG;
1094   if (!result->is_uint()) return Flag::WRONG_FORMAT;
1095   *value = result->get_uint();
1096   return Flag::SUCCESS;
1097 }
1098 
1099 Flag::Error CommandLineFlags::uintAtPut(Flag* flag, uint* value, Flag::Flags origin) {
1100   const char* name;
1101   if (flag == NULL) return Flag::INVALID_FLAG;
1102   if (!flag->is_uint()) return Flag::WRONG_FORMAT;
1103   name = flag->_name;
1104   Flag::Error check = apply_constraint_and_check_range_uint(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1105   if (check != Flag::SUCCESS) return check;
1106   uint old_value = flag->get_uint();
1107   trace_flag_changed<EventUnsignedIntFlagChanged, u4>(name, old_value, *value, origin);
1108   check = flag->set_uint(*value);
1109   *value = old_value;
1110   flag->set_origin(origin);
1111   return check;
1112 }
1113 
1114 Flag::Error CommandLineFlags::uintAtPut(const char* name, size_t len, uint* value, Flag::Flags origin) {
1115   Flag* result = Flag::find_flag(name, len);
1116   return uintAtPut(result, value, origin);
1117 }
1118 
1119 Flag::Error CommandLineFlagsEx::uintAtPut(CommandLineFlagWithType flag, uint value, Flag::Flags origin) {
1120   Flag* faddr = address_of_flag(flag);
1121   guarantee(faddr != NULL && faddr->is_uint(), "wrong flag type");
1122   return CommandLineFlags::uintAtPut(faddr, &value, origin);
1123 }
1124 
1125 Flag::Error CommandLineFlags::intxAt(const char* name, size_t len, intx* value, bool allow_locked, bool return_flag) {
1126   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1127   if (result == NULL) return Flag::INVALID_FLAG;
1128   if (!result->is_intx()) return Flag::WRONG_FORMAT;
1129   *value = result->get_intx();
1130   return Flag::SUCCESS;
1131 }
1132 
1133 static Flag::Error apply_constraint_and_check_range_intx(const char* name, intx new_value, bool verbose) {
1134   Flag::Error status = Flag::SUCCESS;
1135   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1136   if (range != NULL) {
1137     status = range->check_intx(new_value, verbose);
1138   }
1139   if (status == Flag::SUCCESS) {
1140     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1141     if (constraint != NULL) {
1142       status = constraint->apply_intx(new_value, verbose);
1143     }
1144   }
1145   return status;
1146 }
1147 
1148 Flag::Error CommandLineFlags::intxAtPut(Flag* flag, intx* value, Flag::Flags origin) {
1149   const char* name;
1150   if (flag == NULL) return Flag::INVALID_FLAG;
1151   if (!flag->is_intx()) return Flag::WRONG_FORMAT;
1152   name = flag->_name;
1153   Flag::Error check = apply_constraint_and_check_range_intx(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1154   if (check != Flag::SUCCESS) return check;
1155   intx old_value = flag->get_intx();
1156   trace_flag_changed<EventLongFlagChanged, intx>(name, old_value, *value, origin);
1157   check = flag->set_intx(*value);
1158   *value = old_value;
1159   flag->set_origin(origin);
1160   return check;
1161 }
1162 
1163 Flag::Error CommandLineFlags::intxAtPut(const char* name, size_t len, intx* value, Flag::Flags origin) {
1164   Flag* result = Flag::find_flag(name, len);
1165   return intxAtPut(result, value, origin);
1166 }
1167 
1168 Flag::Error CommandLineFlagsEx::intxAtPut(CommandLineFlagWithType flag, intx value, Flag::Flags origin) {
1169   Flag* faddr = address_of_flag(flag);
1170   guarantee(faddr != NULL && faddr->is_intx(), "wrong flag type");
1171   return CommandLineFlags::intxAtPut(faddr, &value, origin);
1172 }
1173 
1174 Flag::Error CommandLineFlags::uintxAt(const char* name, size_t len, uintx* value, bool allow_locked, bool return_flag) {
1175   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1176   if (result == NULL) return Flag::INVALID_FLAG;
1177   if (!result->is_uintx()) return Flag::WRONG_FORMAT;
1178   *value = result->get_uintx();
1179   return Flag::SUCCESS;
1180 }
1181 
1182 static Flag::Error apply_constraint_and_check_range_uintx(const char* name, uintx new_value, bool verbose) {
1183   Flag::Error status = Flag::SUCCESS;
1184   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1185   if (range != NULL) {
1186     status = range->check_uintx(new_value, verbose);
1187   }
1188   if (status == Flag::SUCCESS) {
1189     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1190     if (constraint != NULL) {
1191       status = constraint->apply_uintx(new_value, verbose);
1192     }
1193   }
1194   return status;
1195 }
1196 
1197 Flag::Error CommandLineFlags::uintxAtPut(Flag* flag, uintx* value, Flag::Flags origin) {
1198   const char* name;
1199   if (flag == NULL) return Flag::INVALID_FLAG;
1200   if (!flag->is_uintx()) return Flag::WRONG_FORMAT;
1201   name = flag->_name;
1202   Flag::Error check = apply_constraint_and_check_range_uintx(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1203   if (check != Flag::SUCCESS) return check;
1204   uintx old_value = flag->get_uintx();
1205   trace_flag_changed<EventUnsignedLongFlagChanged, u8>(name, old_value, *value, origin);
1206   check = flag->set_uintx(*value);
1207   *value = old_value;
1208   flag->set_origin(origin);
1209   return check;
1210 }
1211 
1212 Flag::Error CommandLineFlags::uintxAtPut(const char* name, size_t len, uintx* value, Flag::Flags origin) {
1213   Flag* result = Flag::find_flag(name, len);
1214   return uintxAtPut(result, value, origin);
1215 }
1216 
1217 Flag::Error CommandLineFlagsEx::uintxAtPut(CommandLineFlagWithType flag, uintx value, Flag::Flags origin) {
1218   Flag* faddr = address_of_flag(flag);
1219   guarantee(faddr != NULL && faddr->is_uintx(), "wrong flag type");
1220   return CommandLineFlags::uintxAtPut(faddr, &value, origin);
1221 }
1222 
1223 Flag::Error CommandLineFlags::uint64_tAt(const char* name, size_t len, uint64_t* value, bool allow_locked, bool return_flag) {
1224   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1225   if (result == NULL) return Flag::INVALID_FLAG;
1226   if (!result->is_uint64_t()) return Flag::WRONG_FORMAT;
1227   *value = result->get_uint64_t();
1228   return Flag::SUCCESS;
1229 }
1230 
1231 static Flag::Error apply_constraint_and_check_range_uint64_t(const char* name, uint64_t new_value, bool verbose) {
1232   Flag::Error status = Flag::SUCCESS;
1233   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1234   if (range != NULL) {
1235     status = range->check_uint64_t(new_value, verbose);
1236   }
1237   if (status == Flag::SUCCESS) {
1238     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1239     if (constraint != NULL) {
1240       status = constraint->apply_uint64_t(new_value, verbose);
1241     }
1242   }
1243   return status;
1244 }
1245 
1246 Flag::Error CommandLineFlags::uint64_tAtPut(Flag* flag, uint64_t* value, Flag::Flags origin) {
1247   const char* name;
1248   if (flag == NULL) return Flag::INVALID_FLAG;
1249   if (!flag->is_uint64_t()) return Flag::WRONG_FORMAT;
1250   name = flag->_name;
1251   Flag::Error check = apply_constraint_and_check_range_uint64_t(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1252   if (check != Flag::SUCCESS) return check;
1253   uint64_t old_value = flag->get_uint64_t();
1254   trace_flag_changed<EventUnsignedLongFlagChanged, u8>(name, old_value, *value, origin);
1255   check = flag->set_uint64_t(*value);
1256   *value = old_value;
1257   flag->set_origin(origin);
1258   return check;
1259 }
1260 
1261 Flag::Error CommandLineFlags::uint64_tAtPut(const char* name, size_t len, uint64_t* value, Flag::Flags origin) {
1262   Flag* result = Flag::find_flag(name, len);
1263   return uint64_tAtPut(result, value, origin);
1264 }
1265 
1266 Flag::Error CommandLineFlagsEx::uint64_tAtPut(CommandLineFlagWithType flag, uint64_t value, Flag::Flags origin) {
1267   Flag* faddr = address_of_flag(flag);
1268   guarantee(faddr != NULL && faddr->is_uint64_t(), "wrong flag type");
1269   return CommandLineFlags::uint64_tAtPut(faddr, &value, origin);
1270 }
1271 
1272 Flag::Error CommandLineFlags::size_tAt(const char* name, size_t len, size_t* value, bool allow_locked, bool return_flag) {
1273   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1274   if (result == NULL) return Flag::INVALID_FLAG;
1275   if (!result->is_size_t()) return Flag::WRONG_FORMAT;
1276   *value = result->get_size_t();
1277   return Flag::SUCCESS;
1278 }
1279 
1280 static Flag::Error apply_constraint_and_check_range_size_t(const char* name, size_t new_value, bool verbose) {
1281   Flag::Error status = Flag::SUCCESS;
1282   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1283   if (range != NULL) {
1284     status = range->check_size_t(new_value, verbose);
1285   }
1286   if (status == Flag::SUCCESS) {
1287     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1288     if (constraint != NULL) {
1289       status = constraint->apply_size_t(new_value, verbose);
1290     }
1291   }
1292   return status;
1293 }
1294 
1295 
1296 Flag::Error CommandLineFlags::size_tAtPut(Flag* flag, size_t* value, Flag::Flags origin) {
1297   const char* name;
1298   if (flag == NULL) return Flag::INVALID_FLAG;
1299   if (!flag->is_size_t()) return Flag::WRONG_FORMAT;
1300   name = flag->_name;
1301   Flag::Error check = apply_constraint_and_check_range_size_t(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1302   if (check != Flag::SUCCESS) return check;
1303   size_t old_value = flag->get_size_t();
1304   trace_flag_changed<EventUnsignedLongFlagChanged, u8>(name, old_value, *value, origin);
1305   check = flag->set_size_t(*value);
1306   *value = old_value;
1307   flag->set_origin(origin);
1308   return check;
1309 }
1310 
1311 Flag::Error CommandLineFlags::size_tAtPut(const char* name, size_t len, size_t* value, Flag::Flags origin) {
1312   Flag* result = Flag::find_flag(name, len);
1313   return size_tAtPut(result, value, origin);
1314 }
1315 
1316 Flag::Error CommandLineFlagsEx::size_tAtPut(CommandLineFlagWithType flag, size_t value, Flag::Flags origin) {
1317   Flag* faddr = address_of_flag(flag);
1318   guarantee(faddr != NULL && faddr->is_size_t(), "wrong flag type");
1319   return CommandLineFlags::size_tAtPut(faddr, &value, origin);
1320 }
1321 
1322 Flag::Error CommandLineFlags::doubleAt(const char* name, size_t len, double* value, bool allow_locked, bool return_flag) {
1323   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1324   if (result == NULL) return Flag::INVALID_FLAG;
1325   if (!result->is_double()) return Flag::WRONG_FORMAT;
1326   *value = result->get_double();
1327   return Flag::SUCCESS;
1328 }
1329 
1330 static Flag::Error apply_constraint_and_check_range_double(const char* name, double new_value, bool verbose) {
1331   Flag::Error status = Flag::SUCCESS;
1332   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
1333   if (range != NULL) {
1334     status = range->check_double(new_value, verbose);
1335   }
1336   if (status == Flag::SUCCESS) {
1337     CommandLineFlagConstraint* constraint = CommandLineFlagConstraintList::find_if_needs_check(name);
1338     if (constraint != NULL) {
1339       status = constraint->apply_double(new_value, verbose);
1340     }
1341   }
1342   return status;
1343 }
1344 
1345 Flag::Error CommandLineFlags::doubleAtPut(Flag* flag, double* value, Flag::Flags origin) {
1346   const char* name;
1347   if (flag == NULL) return Flag::INVALID_FLAG;
1348   if (!flag->is_double()) return Flag::WRONG_FORMAT;
1349   name = flag->_name;
1350   Flag::Error check = apply_constraint_and_check_range_double(name, *value, !CommandLineFlagConstraintList::validated_after_ergo());
1351   if (check != Flag::SUCCESS) return check;
1352   double old_value = flag->get_double();
1353   trace_flag_changed<EventDoubleFlagChanged, double>(name, old_value, *value, origin);
1354   check = flag->set_double(*value);
1355   *value = old_value;
1356   flag->set_origin(origin);
1357   return check;
1358 }
1359 
1360 Flag::Error CommandLineFlags::doubleAtPut(const char* name, size_t len, double* value, Flag::Flags origin) {
1361   Flag* result = Flag::find_flag(name, len);
1362   return doubleAtPut(result, value, origin);
1363 }
1364 
1365 Flag::Error CommandLineFlagsEx::doubleAtPut(CommandLineFlagWithType flag, double value, Flag::Flags origin) {
1366   Flag* faddr = address_of_flag(flag);
1367   guarantee(faddr != NULL && faddr->is_double(), "wrong flag type");
1368   return CommandLineFlags::doubleAtPut(faddr, &value, origin);
1369 }
1370 
1371 Flag::Error CommandLineFlags::ccstrAt(const char* name, size_t len, ccstr* value, bool allow_locked, bool return_flag) {
1372   Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
1373   if (result == NULL) return Flag::INVALID_FLAG;
1374   if (!result->is_ccstr()) return Flag::WRONG_FORMAT;
1375   *value = result->get_ccstr();
1376   return Flag::SUCCESS;
1377 }
1378 
1379 Flag::Error CommandLineFlags::ccstrAtPut(const char* name, size_t len, ccstr* value, Flag::Flags origin) {
1380   Flag* result = Flag::find_flag(name, len);
1381   if (result == NULL) return Flag::INVALID_FLAG;
1382   if (!result->is_ccstr()) return Flag::WRONG_FORMAT;
1383   ccstr old_value = result->get_ccstr();
1384   trace_flag_changed<EventStringFlagChanged, const char*>(name, old_value, *value, origin);
1385   char* new_value = NULL;
1386   if (*value != NULL) {
1387     new_value = os::strdup_check_oom(*value);
1388   }
1389   Flag::Error check = result->set_ccstr(new_value);
1390   if (result->is_default() && old_value != NULL) {
1391     // Prior value is NOT heap allocated, but was a literal constant.
1392     old_value = os::strdup_check_oom(old_value);
1393   }
1394   *value = old_value;
1395   result->set_origin(origin);
1396   return check;
1397 }
1398 
1399 Flag::Error CommandLineFlagsEx::ccstrAtPut(CommandLineFlagWithType flag, ccstr value, Flag::Flags origin) {
1400   Flag* faddr = address_of_flag(flag);
1401   guarantee(faddr != NULL && faddr->is_ccstr(), "wrong flag type");
1402   ccstr old_value = faddr->get_ccstr();
1403   trace_flag_changed<EventStringFlagChanged, const char*>(faddr->_name, old_value, value, origin);
1404   char* new_value = os::strdup_check_oom(value);
1405   Flag::Error check = faddr->set_ccstr(new_value);
1406   if (!faddr->is_default() && old_value != NULL) {
1407     // Prior value is heap allocated so free it.
1408     FREE_C_HEAP_ARRAY(char, old_value);
1409   }
1410   faddr->set_origin(origin);
1411   return check;
1412 }
1413 
1414 extern "C" {
1415   static int compare_flags(const void* void_a, const void* void_b) {
1416     return strcmp((*((Flag**) void_a))->_name, (*((Flag**) void_b))->_name);
1417   }
1418 }
1419 
1420 void CommandLineFlags::printSetFlags(outputStream* out) {
1421   // Print which flags were set on the command line
1422   // note: this method is called before the thread structure is in place
1423   //       which means resource allocation cannot be used.
1424 
1425   // The last entry is the null entry.
1426   const size_t length = Flag::numFlags - 1;
1427 
1428   // Sort
1429   Flag** array = NEW_C_HEAP_ARRAY(Flag*, length, mtArguments);
1430   for (size_t i = 0; i < length; i++) {
1431     array[i] = &flagTable[i];
1432   }
1433   qsort(array, length, sizeof(Flag*), compare_flags);
1434 
1435   // Print
1436   for (size_t i = 0; i < length; i++) {
1437     if (array[i]->get_origin() /* naked field! */) {
1438       array[i]->print_as_flag(out);
1439       out->print(" ");
1440     }
1441   }
1442   out->cr();
1443   FREE_C_HEAP_ARRAY(Flag*, array);
1444 }
1445 
1446 #ifndef PRODUCT
1447 
1448 void CommandLineFlags::verify() {
1449   assert(Arguments::check_vm_args_consistency(), "Some flag settings conflict");
1450 }
1451 
1452 #endif // PRODUCT
1453 
1454 void CommandLineFlags::printFlags(outputStream* out, bool withComments, bool printRanges) {
1455   // Print the flags sorted by name
1456   // note: this method is called before the thread structure is in place
1457   //       which means resource allocation cannot be used.
1458 
1459   // The last entry is the null entry.
1460   const size_t length = Flag::numFlags - 1;
1461 
1462   // Sort
1463   Flag** array = NEW_C_HEAP_ARRAY(Flag*, length, mtArguments);
1464   for (size_t i = 0; i < length; i++) {
1465     array[i] = &flagTable[i];
1466   }
1467   qsort(array, length, sizeof(Flag*), compare_flags);
1468 
1469   // Print
1470   if (!printRanges) {
1471     out->print_cr("[Global flags]");
1472   } else {
1473     out->print_cr("[Global flags ranges]");
1474   }
1475 
1476   for (size_t i = 0; i < length; i++) {
1477     if (array[i]->is_unlocked()) {
1478       array[i]->print_on(out, withComments, printRanges);
1479     }
1480   }
1481   FREE_C_HEAP_ARRAY(Flag*, array);
1482 }
--- EOF ---