1 /*
   2  * Copyright (c) 1997, 2008, 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 "incls/_precompiled.incl"
  26 # include "incls/_globals.cpp.incl"
  27 
  28 
  29 RUNTIME_FLAGS(MATERIALIZE_DEVELOPER_FLAG, MATERIALIZE_PD_DEVELOPER_FLAG, \
  30               MATERIALIZE_PRODUCT_FLAG, MATERIALIZE_PD_PRODUCT_FLAG, \
  31               MATERIALIZE_DIAGNOSTIC_FLAG, MATERIALIZE_EXPERIMENTAL_FLAG, \
  32               MATERIALIZE_NOTPRODUCT_FLAG, \
  33               MATERIALIZE_MANAGEABLE_FLAG, MATERIALIZE_PRODUCT_RW_FLAG, \
  34               MATERIALIZE_LP64_PRODUCT_FLAG)
  35 
  36 RUNTIME_OS_FLAGS(MATERIALIZE_DEVELOPER_FLAG, MATERIALIZE_PD_DEVELOPER_FLAG, \
  37                  MATERIALIZE_PRODUCT_FLAG, MATERIALIZE_PD_PRODUCT_FLAG, \
  38                  MATERIALIZE_DIAGNOSTIC_FLAG, MATERIALIZE_NOTPRODUCT_FLAG)
  39 
  40 bool Flag::is_unlocker() const {
  41   return strcmp(name, "UnlockDiagnosticVMOptions") == 0     ||
  42          strcmp(name, "UnlockExperimentalVMOptions") == 0;
  43 
  44 }
  45 
  46 bool Flag::is_unlocked() const {
  47   if (strcmp(kind, "{diagnostic}") == 0) {
  48     return UnlockDiagnosticVMOptions;
  49   } else if (strcmp(kind, "{experimental}") == 0 ||
  50              strcmp(kind, "{C2 experimental}") == 0) {
  51     return UnlockExperimentalVMOptions;
  52   } else {
  53     return true;
  54   }
  55 }
  56 
  57 bool Flag::is_writeable() const {
  58   return (strcmp(kind, "{manageable}") == 0 || strcmp(kind, "{product rw}") == 0);
  59 }
  60 
  61 // All flags except "manageable" are assumed internal flags.
  62 // Long term, we need to define a mechanism to specify which flags
  63 // are external/stable and change this function accordingly.
  64 bool Flag::is_external() const {
  65   return (strcmp(kind, "{manageable}") == 0);
  66 }
  67 
  68 // Length of format string (e.g. "%.1234s") for printing ccstr below
  69 #define FORMAT_BUFFER_LEN 16
  70 
  71 void Flag::print_on(outputStream* st) {
  72   st->print("%5s %-35s %c= ", type, name, (origin != DEFAULT ? ':' : ' '));
  73   if (is_bool())     st->print("%-16s", get_bool() ? "true" : "false");
  74   if (is_intx())     st->print("%-16ld", get_intx());
  75   if (is_uintx())    st->print("%-16lu", get_uintx());
  76   if (is_uint64_t()) st->print("%-16lu", get_uint64_t());
  77   if (is_ccstr()) {
  78     const char* cp = get_ccstr();
  79     if (cp != NULL) {
  80       const char* eol;
  81       while ((eol = strchr(cp, '\n')) != NULL) {
  82         char format_buffer[FORMAT_BUFFER_LEN];
  83         size_t llen = pointer_delta(eol, cp, sizeof(char));
  84         jio_snprintf(format_buffer, FORMAT_BUFFER_LEN,
  85                      "%%." SIZE_FORMAT "s", llen);
  86         st->print(format_buffer, cp);
  87         st->cr();
  88         cp = eol+1;
  89         st->print("%5s %-35s += ", "", name);
  90       }
  91       st->print("%-16s", cp);
  92     }
  93   }
  94   st->print(" %s", kind);
  95   st->cr();
  96 }
  97 
  98 void Flag::print_as_flag(outputStream* st) {
  99   if (is_bool()) {
 100     st->print("-XX:%s%s", get_bool() ? "+" : "-", name);
 101   } else if (is_intx()) {
 102     st->print("-XX:%s=" INTX_FORMAT, name, get_intx());
 103   } else if (is_uintx()) {
 104     st->print("-XX:%s=" UINTX_FORMAT, name, get_uintx());
 105   } else if (is_uint64_t()) {
 106     st->print("-XX:%s=" UINT64_FORMAT, name, get_uint64_t());
 107   } else if (is_ccstr()) {
 108     st->print("-XX:%s=", name);
 109     const char* cp = get_ccstr();
 110     if (cp != NULL) {
 111       // Need to turn embedded '\n's back into separate arguments
 112       // Not so efficient to print one character at a time,
 113       // but the choice is to do the transformation to a buffer
 114       // and print that.  And this need not be efficient.
 115       for (; *cp != '\0'; cp += 1) {
 116         switch (*cp) {
 117           default:
 118             st->print("%c", *cp);
 119             break;
 120           case '\n':
 121             st->print(" -XX:%s=", name);
 122             break;
 123         }
 124       }
 125     }
 126   } else {
 127     ShouldNotReachHere();
 128   }
 129 }
 130 
 131 // 4991491 do not "optimize out" the was_set false values: omitting them
 132 // tickles a Microsoft compiler bug causing flagTable to be malformed
 133 
 134 #define RUNTIME_PRODUCT_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{product}", DEFAULT },
 135 #define RUNTIME_PD_PRODUCT_FLAG_STRUCT(type, name, doc)     { #type, XSTR(name), &name, "{pd product}", DEFAULT },
 136 #define RUNTIME_DIAGNOSTIC_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{diagnostic}", DEFAULT },
 137 #define RUNTIME_EXPERIMENTAL_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{experimental}", DEFAULT },
 138 #define RUNTIME_MANAGEABLE_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{manageable}", DEFAULT },
 139 #define RUNTIME_PRODUCT_RW_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{product rw}", DEFAULT },
 140 
 141 #ifdef PRODUCT
 142   #define RUNTIME_DEVELOP_FLAG_STRUCT(type, name, value, doc) /* flag is constant */
 143   #define RUNTIME_PD_DEVELOP_FLAG_STRUCT(type, name, doc)     /* flag is constant */
 144   #define RUNTIME_NOTPRODUCT_FLAG_STRUCT(type, name, value, doc)
 145 #else
 146   #define RUNTIME_DEVELOP_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "", DEFAULT },
 147   #define RUNTIME_PD_DEVELOP_FLAG_STRUCT(type, name, doc)     { #type, XSTR(name), &name, "{pd}", DEFAULT },
 148   #define RUNTIME_NOTPRODUCT_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{notproduct}", DEFAULT },
 149 #endif
 150 
 151 #ifdef _LP64
 152   #define RUNTIME_LP64_PRODUCT_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{lp64_product}", DEFAULT },
 153 #else
 154   #define RUNTIME_LP64_PRODUCT_FLAG_STRUCT(type, name, value, doc) /* flag is constant */
 155 #endif // _LP64
 156 
 157 #define C1_PRODUCT_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{C1 product}", DEFAULT },
 158 #define C1_PD_PRODUCT_FLAG_STRUCT(type, name, doc)     { #type, XSTR(name), &name, "{C1 pd product}", DEFAULT },
 159 #ifdef PRODUCT
 160   #define C1_DEVELOP_FLAG_STRUCT(type, name, value, doc) /* flag is constant */
 161   #define C1_PD_DEVELOP_FLAG_STRUCT(type, name, doc)     /* flag is constant */
 162   #define C1_NOTPRODUCT_FLAG_STRUCT(type, name, value, doc)
 163 #else
 164   #define C1_DEVELOP_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{C1}", DEFAULT },
 165   #define C1_PD_DEVELOP_FLAG_STRUCT(type, name, doc)     { #type, XSTR(name), &name, "{C1 pd}", DEFAULT },
 166   #define C1_NOTPRODUCT_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{C1 notproduct}", DEFAULT },
 167 #endif
 168 
 169 
 170 #define C2_PRODUCT_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{C2 product}", DEFAULT },
 171 #define C2_PD_PRODUCT_FLAG_STRUCT(type, name, doc)     { #type, XSTR(name), &name, "{C2 pd product}", DEFAULT },
 172 #define C2_DIAGNOSTIC_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{C2 diagnostic}", DEFAULT },
 173 #define C2_EXPERIMENTAL_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{C2 experimental}", DEFAULT },
 174 #ifdef PRODUCT
 175   #define C2_DEVELOP_FLAG_STRUCT(type, name, value, doc) /* flag is constant */
 176   #define C2_PD_DEVELOP_FLAG_STRUCT(type, name, doc)     /* flag is constant */
 177   #define C2_NOTPRODUCT_FLAG_STRUCT(type, name, value, doc)
 178 #else
 179   #define C2_DEVELOP_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{C2}", DEFAULT },
 180   #define C2_PD_DEVELOP_FLAG_STRUCT(type, name, doc)     { #type, XSTR(name), &name, "{C2 pd}", DEFAULT },
 181   #define C2_NOTPRODUCT_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{C2 notproduct}", DEFAULT },
 182 #endif
 183 
 184 #define SHARK_PRODUCT_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{Shark product}", DEFAULT },
 185 #define SHARK_PD_PRODUCT_FLAG_STRUCT(type, name, doc)     { #type, XSTR(name), &name, "{Shark pd product}", DEFAULT },
 186 #define SHARK_DIAGNOSTIC_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{Shark diagnostic}", DEFAULT },
 187 #ifdef PRODUCT
 188   #define SHARK_DEVELOP_FLAG_STRUCT(type, name, value, doc) /* flag is constant */
 189   #define SHARK_PD_DEVELOP_FLAG_STRUCT(type, name, doc)     /* flag is constant */
 190   #define SHARK_NOTPRODUCT_FLAG_STRUCT(type, name, value, doc)
 191 #else
 192   #define SHARK_DEVELOP_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{Shark}", DEFAULT },
 193   #define SHARK_PD_DEVELOP_FLAG_STRUCT(type, name, doc)     { #type, XSTR(name), &name, "{Shark pd}", DEFAULT },
 194   #define SHARK_NOTPRODUCT_FLAG_STRUCT(type, name, value, doc) { #type, XSTR(name), &name, "{Shark notproduct}", DEFAULT },
 195 #endif
 196 
 197 static Flag flagTable[] = {
 198  RUNTIME_FLAGS(RUNTIME_DEVELOP_FLAG_STRUCT, RUNTIME_PD_DEVELOP_FLAG_STRUCT, RUNTIME_PRODUCT_FLAG_STRUCT, RUNTIME_PD_PRODUCT_FLAG_STRUCT, RUNTIME_DIAGNOSTIC_FLAG_STRUCT, RUNTIME_EXPERIMENTAL_FLAG_STRUCT, RUNTIME_NOTPRODUCT_FLAG_STRUCT, RUNTIME_MANAGEABLE_FLAG_STRUCT, RUNTIME_PRODUCT_RW_FLAG_STRUCT, RUNTIME_LP64_PRODUCT_FLAG_STRUCT)
 199  RUNTIME_OS_FLAGS(RUNTIME_DEVELOP_FLAG_STRUCT, RUNTIME_PD_DEVELOP_FLAG_STRUCT, RUNTIME_PRODUCT_FLAG_STRUCT, RUNTIME_PD_PRODUCT_FLAG_STRUCT, RUNTIME_DIAGNOSTIC_FLAG_STRUCT, RUNTIME_NOTPRODUCT_FLAG_STRUCT)
 200 #ifndef SERIALGC
 201  G1_FLAGS(RUNTIME_DEVELOP_FLAG_STRUCT, RUNTIME_PD_DEVELOP_FLAG_STRUCT, RUNTIME_PRODUCT_FLAG_STRUCT, RUNTIME_PD_PRODUCT_FLAG_STRUCT, RUNTIME_DIAGNOSTIC_FLAG_STRUCT, RUNTIME_EXPERIMENTAL_FLAG_STRUCT, RUNTIME_NOTPRODUCT_FLAG_STRUCT, RUNTIME_MANAGEABLE_FLAG_STRUCT, RUNTIME_PRODUCT_RW_FLAG_STRUCT)
 202 #endif // SERIALGC
 203 #ifdef COMPILER1
 204  C1_FLAGS(C1_DEVELOP_FLAG_STRUCT, C1_PD_DEVELOP_FLAG_STRUCT, C1_PRODUCT_FLAG_STRUCT, C1_PD_PRODUCT_FLAG_STRUCT, C1_NOTPRODUCT_FLAG_STRUCT)
 205 #endif
 206 #ifdef COMPILER2
 207  C2_FLAGS(C2_DEVELOP_FLAG_STRUCT, C2_PD_DEVELOP_FLAG_STRUCT, C2_PRODUCT_FLAG_STRUCT, C2_PD_PRODUCT_FLAG_STRUCT, C2_DIAGNOSTIC_FLAG_STRUCT, C2_EXPERIMENTAL_FLAG_STRUCT, C2_NOTPRODUCT_FLAG_STRUCT)
 208 #endif
 209 #ifdef SHARK
 210  SHARK_FLAGS(SHARK_DEVELOP_FLAG_STRUCT, SHARK_PD_DEVELOP_FLAG_STRUCT, SHARK_PRODUCT_FLAG_STRUCT, SHARK_PD_PRODUCT_FLAG_STRUCT, SHARK_DIAGNOSTIC_FLAG_STRUCT, SHARK_NOTPRODUCT_FLAG_STRUCT)
 211 #endif
 212  {0, NULL, NULL}
 213 };
 214 
 215 Flag* Flag::flags = flagTable;
 216 size_t Flag::numFlags = (sizeof(flagTable) / sizeof(Flag));
 217 
 218 inline bool str_equal(const char* s, char* q, size_t len) {
 219   // s is null terminated, q is not!
 220   if (strlen(s) != (unsigned int) len) return false;
 221   return strncmp(s, q, len) == 0;
 222 }
 223 
 224 Flag* Flag::find_flag(char* name, size_t length) {
 225   for (Flag* current = &flagTable[0]; current->name; current++) {
 226     if (str_equal(current->name, name, length)) {
 227       if (!(current->is_unlocked() || current->is_unlocker())) {
 228         // disable use of diagnostic or experimental flags until they
 229         // are explicitly unlocked
 230         return NULL;
 231       }
 232       return current;
 233     }
 234   }
 235   return NULL;
 236 }
 237 
 238 // Returns the address of the index'th element
 239 static Flag* address_of_flag(CommandLineFlagWithType flag) {
 240   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 241   return &Flag::flags[flag];
 242 }
 243 
 244 bool CommandLineFlagsEx::is_default(CommandLineFlag flag) {
 245   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 246   Flag* f = &Flag::flags[flag];
 247   return (f->origin == DEFAULT);
 248 }
 249 
 250 bool CommandLineFlagsEx::is_ergo(CommandLineFlag flag) {
 251   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 252   Flag* f = &Flag::flags[flag];
 253   return (f->origin == ERGONOMIC);
 254 }
 255 
 256 bool CommandLineFlagsEx::is_cmdline(CommandLineFlag flag) {
 257   assert((size_t)flag < Flag::numFlags, "bad command line flag index");
 258   Flag* f = &Flag::flags[flag];
 259   return (f->origin == COMMAND_LINE);
 260 }
 261 
 262 bool CommandLineFlags::wasSetOnCmdline(const char* name, bool* value) {
 263   Flag* result = Flag::find_flag((char*)name, strlen(name));
 264   if (result == NULL) return false;
 265   *value = (result->origin == COMMAND_LINE);
 266   return true;
 267 }
 268 
 269 bool CommandLineFlags::boolAt(char* name, size_t len, bool* value) {
 270   Flag* result = Flag::find_flag(name, len);
 271   if (result == NULL) return false;
 272   if (!result->is_bool()) return false;
 273   *value = result->get_bool();
 274   return true;
 275 }
 276 
 277 bool CommandLineFlags::boolAtPut(char* name, size_t len, bool* value, FlagValueOrigin origin) {
 278   Flag* result = Flag::find_flag(name, len);
 279   if (result == NULL) return false;
 280   if (!result->is_bool()) return false;
 281   bool old_value = result->get_bool();
 282   result->set_bool(*value);
 283   *value = old_value;
 284   result->origin = origin;
 285   return true;
 286 }
 287 
 288 void CommandLineFlagsEx::boolAtPut(CommandLineFlagWithType flag, bool value, FlagValueOrigin origin) {
 289   Flag* faddr = address_of_flag(flag);
 290   guarantee(faddr != NULL && faddr->is_bool(), "wrong flag type");
 291   faddr->set_bool(value);
 292   faddr->origin = origin;
 293 }
 294 
 295 bool CommandLineFlags::intxAt(char* name, size_t len, intx* value) {
 296   Flag* result = Flag::find_flag(name, len);
 297   if (result == NULL) return false;
 298   if (!result->is_intx()) return false;
 299   *value = result->get_intx();
 300   return true;
 301 }
 302 
 303 bool CommandLineFlags::intxAtPut(char* name, size_t len, intx* value, FlagValueOrigin origin) {
 304   Flag* result = Flag::find_flag(name, len);
 305   if (result == NULL) return false;
 306   if (!result->is_intx()) return false;
 307   intx old_value = result->get_intx();
 308   result->set_intx(*value);
 309   *value = old_value;
 310   result->origin = origin;
 311   return true;
 312 }
 313 
 314 void CommandLineFlagsEx::intxAtPut(CommandLineFlagWithType flag, intx value, FlagValueOrigin origin) {
 315   Flag* faddr = address_of_flag(flag);
 316   guarantee(faddr != NULL && faddr->is_intx(), "wrong flag type");
 317   faddr->set_intx(value);
 318   faddr->origin = origin;
 319 }
 320 
 321 bool CommandLineFlags::uintxAt(char* name, size_t len, uintx* value) {
 322   Flag* result = Flag::find_flag(name, len);
 323   if (result == NULL) return false;
 324   if (!result->is_uintx()) return false;
 325   *value = result->get_uintx();
 326   return true;
 327 }
 328 
 329 bool CommandLineFlags::uintxAtPut(char* name, size_t len, uintx* value, FlagValueOrigin origin) {
 330   Flag* result = Flag::find_flag(name, len);
 331   if (result == NULL) return false;
 332   if (!result->is_uintx()) return false;
 333   uintx old_value = result->get_uintx();
 334   result->set_uintx(*value);
 335   *value = old_value;
 336   result->origin = origin;
 337   return true;
 338 }
 339 
 340 void CommandLineFlagsEx::uintxAtPut(CommandLineFlagWithType flag, uintx value, FlagValueOrigin origin) {
 341   Flag* faddr = address_of_flag(flag);
 342   guarantee(faddr != NULL && faddr->is_uintx(), "wrong flag type");
 343   faddr->set_uintx(value);
 344   faddr->origin = origin;
 345 }
 346 
 347 bool CommandLineFlags::uint64_tAt(char* name, size_t len, uint64_t* value) {
 348   Flag* result = Flag::find_flag(name, len);
 349   if (result == NULL) return false;
 350   if (!result->is_uint64_t()) return false;
 351   *value = result->get_uint64_t();
 352   return true;
 353 }
 354 
 355 bool CommandLineFlags::uint64_tAtPut(char* name, size_t len, uint64_t* value, FlagValueOrigin origin) {
 356   Flag* result = Flag::find_flag(name, len);
 357   if (result == NULL) return false;
 358   if (!result->is_uint64_t()) return false;
 359   uint64_t old_value = result->get_uint64_t();
 360   result->set_uint64_t(*value);
 361   *value = old_value;
 362   result->origin = origin;
 363   return true;
 364 }
 365 
 366 void CommandLineFlagsEx::uint64_tAtPut(CommandLineFlagWithType flag, uint64_t value, FlagValueOrigin origin) {
 367   Flag* faddr = address_of_flag(flag);
 368   guarantee(faddr != NULL && faddr->is_uint64_t(), "wrong flag type");
 369   faddr->set_uint64_t(value);
 370   faddr->origin = origin;
 371 }
 372 
 373 bool CommandLineFlags::doubleAt(char* name, size_t len, double* value) {
 374   Flag* result = Flag::find_flag(name, len);
 375   if (result == NULL) return false;
 376   if (!result->is_double()) return false;
 377   *value = result->get_double();
 378   return true;
 379 }
 380 
 381 bool CommandLineFlags::doubleAtPut(char* name, size_t len, double* value, FlagValueOrigin origin) {
 382   Flag* result = Flag::find_flag(name, len);
 383   if (result == NULL) return false;
 384   if (!result->is_double()) return false;
 385   double old_value = result->get_double();
 386   result->set_double(*value);
 387   *value = old_value;
 388   result->origin = origin;
 389   return true;
 390 }
 391 
 392 void CommandLineFlagsEx::doubleAtPut(CommandLineFlagWithType flag, double value, FlagValueOrigin origin) {
 393   Flag* faddr = address_of_flag(flag);
 394   guarantee(faddr != NULL && faddr->is_double(), "wrong flag type");
 395   faddr->set_double(value);
 396   faddr->origin = origin;
 397 }
 398 
 399 bool CommandLineFlags::ccstrAt(char* name, size_t len, ccstr* value) {
 400   Flag* result = Flag::find_flag(name, len);
 401   if (result == NULL) return false;
 402   if (!result->is_ccstr()) return false;
 403   *value = result->get_ccstr();
 404   return true;
 405 }
 406 
 407 // Contract:  Flag will make private copy of the incoming value.
 408 // Outgoing value is always malloc-ed, and caller MUST call free.
 409 bool CommandLineFlags::ccstrAtPut(char* name, size_t len, ccstr* value, FlagValueOrigin origin) {
 410   Flag* result = Flag::find_flag(name, len);
 411   if (result == NULL) return false;
 412   if (!result->is_ccstr()) return false;
 413   ccstr old_value = result->get_ccstr();
 414   char* new_value = NULL;
 415   if (*value != NULL) {
 416     new_value = NEW_C_HEAP_ARRAY(char, strlen(*value)+1);
 417     strcpy(new_value, *value);
 418   }
 419   result->set_ccstr(new_value);
 420   if (result->origin == DEFAULT && old_value != NULL) {
 421     // Prior value is NOT heap allocated, but was a literal constant.
 422     char* old_value_to_free = NEW_C_HEAP_ARRAY(char, strlen(old_value)+1);
 423     strcpy(old_value_to_free, old_value);
 424     old_value = old_value_to_free;
 425   }
 426   *value = old_value;
 427   result->origin = origin;
 428   return true;
 429 }
 430 
 431 // Contract:  Flag will make private copy of the incoming value.
 432 void CommandLineFlagsEx::ccstrAtPut(CommandLineFlagWithType flag, ccstr value, FlagValueOrigin origin) {
 433   Flag* faddr = address_of_flag(flag);
 434   guarantee(faddr != NULL && faddr->is_ccstr(), "wrong flag type");
 435   ccstr old_value = faddr->get_ccstr();
 436   char* new_value = NEW_C_HEAP_ARRAY(char, strlen(value)+1);
 437   strcpy(new_value, value);
 438   faddr->set_ccstr(new_value);
 439   if (faddr->origin != DEFAULT && old_value != NULL) {
 440     // Prior value is heap allocated so free it.
 441     FREE_C_HEAP_ARRAY(char, old_value);
 442   }
 443   faddr->origin = origin;
 444 }
 445 
 446 extern "C" {
 447   static int compare_flags(const void* void_a, const void* void_b) {
 448     return strcmp((*((Flag**) void_a))->name, (*((Flag**) void_b))->name);
 449   }
 450 }
 451 
 452 void CommandLineFlags::printSetFlags() {
 453   // Print which flags were set on the command line
 454   // note: this method is called before the thread structure is in place
 455   //       which means resource allocation cannot be used.
 456 
 457   // Compute size
 458   int length= 0;
 459   while (flagTable[length].name != NULL) length++;
 460 
 461   // Sort
 462   Flag** array = NEW_C_HEAP_ARRAY(Flag*, length);
 463   for (int index = 0; index < length; index++) {
 464     array[index] = &flagTable[index];
 465   }
 466   qsort(array, length, sizeof(Flag*), compare_flags);
 467 
 468   // Print
 469   for (int i = 0; i < length; i++) {
 470     if (array[i]->origin /* naked field! */) {
 471       array[i]->print_as_flag(tty);
 472       tty->print(" ");
 473     }
 474   }
 475   tty->cr();
 476   FREE_C_HEAP_ARRAY(Flag*, array);
 477 }
 478 
 479 #ifndef PRODUCT
 480 
 481 
 482 void CommandLineFlags::verify() {
 483   assert(Arguments::check_vm_args_consistency(), "Some flag settings conflict");
 484 }
 485 
 486 #endif // PRODUCT
 487 
 488 void CommandLineFlags::printFlags() {
 489   // Print the flags sorted by name
 490   // note: this method is called before the thread structure is in place
 491   //       which means resource allocation cannot be used.
 492 
 493   // Compute size
 494   int length= 0;
 495   while (flagTable[length].name != NULL) length++;
 496 
 497   // Sort
 498   Flag** array = NEW_C_HEAP_ARRAY(Flag*, length);
 499   for (int index = 0; index < length; index++) {
 500     array[index] = &flagTable[index];
 501   }
 502   qsort(array, length, sizeof(Flag*), compare_flags);
 503 
 504   // Print
 505   tty->print_cr("[Global flags]");
 506   for (int i = 0; i < length; i++) {
 507     if (array[i]->is_unlocked()) {
 508       array[i]->print_on(tty);
 509     }
 510   }
 511   FREE_C_HEAP_ARRAY(Flag*, array);
 512 }