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