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