1 /*
   2  * Copyright (c) 1998, 2017, 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 "compiler/compilerOracle.hpp"
  27 #include "compiler/methodMatcher.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "memory/oopFactory.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/klass.hpp"
  32 #include "oops/method.hpp"
  33 #include "oops/symbol.hpp"
  34 #include "prims/jvm.h"
  35 #include "runtime/handles.inline.hpp"
  36 #include "runtime/jniHandles.hpp"
  37 #include "runtime/os.hpp"
  38 
  39 enum OptionType {
  40   IntxType,
  41   UintxType,
  42   BoolType,
  43   CcstrType,
  44   DoubleType,
  45   UnknownType
  46 };
  47 
  48 /* Methods to map real type names to OptionType */
  49 template<typename T>
  50 static OptionType get_type_for() {
  51   return UnknownType;
  52 };
  53 
  54 template<> OptionType get_type_for<intx>() {
  55   return IntxType;
  56 }
  57 
  58 template<> OptionType get_type_for<uintx>() {
  59   return UintxType;
  60 }
  61 
  62 template<> OptionType get_type_for<bool>() {
  63   return BoolType;
  64 }
  65 
  66 template<> OptionType get_type_for<ccstr>() {
  67   return CcstrType;
  68 }
  69 
  70 template<> OptionType get_type_for<double>() {
  71   return DoubleType;
  72 }
  73 
  74 // this must parallel the command_names below
  75 enum OracleCommand {
  76   UnknownCommand = -1,
  77   OracleFirstCommand = 0,
  78   BreakCommand = OracleFirstCommand,
  79   PrintCommand,
  80   ExcludeCommand,
  81   InlineCommand,
  82   DontInlineCommand,
  83   CompileOnlyCommand,
  84   LogCommand,
  85   OptionCommand,
  86   QuietCommand,
  87   HelpCommand,
  88   OracleCommandCount
  89 };
  90 
  91 // this must parallel the enum OracleCommand
  92 static const char * command_names[] = {
  93   "break",
  94   "print",
  95   "exclude",
  96   "inline",
  97   "dontinline",
  98   "compileonly",
  99   "log",
 100   "option",
 101   "quiet",
 102   "help"
 103 };
 104 
 105 class MethodMatcher;
 106 class TypedMethodOptionMatcher;
 107 
 108 static BasicMatcher* lists[OracleCommandCount] = { 0, };
 109 static TypedMethodOptionMatcher* option_list = NULL;
 110 static bool any_set = false;
 111 
 112 class TypedMethodOptionMatcher : public MethodMatcher {
 113  private:
 114   TypedMethodOptionMatcher* _next;
 115   const char*   _option;
 116   OptionType    _type;
 117  public:
 118 
 119   union {
 120     bool bool_value;
 121     intx intx_value;
 122     uintx uintx_value;
 123     double double_value;
 124     ccstr ccstr_value;
 125   } _u;
 126 
 127   TypedMethodOptionMatcher() : MethodMatcher(),
 128     _next(NULL),
 129     _type(UnknownType) {
 130       _option = NULL;
 131       memset(&_u, 0, sizeof(_u));
 132   }
 133 
 134   static TypedMethodOptionMatcher* parse_method_pattern(char*& line, const char*& error_msg);
 135   TypedMethodOptionMatcher* match(methodHandle method, const char* opt, OptionType type);
 136 
 137   void init(const char* opt, OptionType type, TypedMethodOptionMatcher* next) {
 138     _next = next;
 139     _type = type;
 140     _option = os::strdup_check_oom(opt);
 141   }
 142 
 143   void set_next(TypedMethodOptionMatcher* next) {_next = next; }
 144   TypedMethodOptionMatcher* next() { return _next; }
 145   OptionType type() { return _type; }
 146   template<typename T> T value();
 147   template<typename T> void set_value(T value);
 148   void print();
 149   void print_all();
 150   TypedMethodOptionMatcher* clone();
 151   ~TypedMethodOptionMatcher();
 152 };
 153 
 154 // A few templated accessors instead of a full template class.
 155 template<> intx TypedMethodOptionMatcher::value<intx>() {
 156   return _u.intx_value;
 157 }
 158 
 159 template<> uintx TypedMethodOptionMatcher::value<uintx>() {
 160   return _u.uintx_value;
 161 }
 162 
 163 template<> bool TypedMethodOptionMatcher::value<bool>() {
 164   return _u.bool_value;
 165 }
 166 
 167 template<> double TypedMethodOptionMatcher::value<double>() {
 168   return _u.double_value;
 169 }
 170 
 171 template<> ccstr TypedMethodOptionMatcher::value<ccstr>() {
 172   return _u.ccstr_value;
 173 }
 174 
 175 template<> void TypedMethodOptionMatcher::set_value(intx value) {
 176   _u.intx_value = value;
 177 }
 178 
 179 template<> void TypedMethodOptionMatcher::set_value(uintx value) {
 180   _u.uintx_value = value;
 181 }
 182 
 183 template<> void TypedMethodOptionMatcher::set_value(double value) {
 184   _u.double_value = value;
 185 }
 186 
 187 template<> void TypedMethodOptionMatcher::set_value(bool value) {
 188   _u.bool_value = value;
 189 }
 190 
 191 template<> void TypedMethodOptionMatcher::set_value(ccstr value) {
 192   _u.ccstr_value = (const ccstr)os::strdup_check_oom(value);
 193 }
 194 
 195 void TypedMethodOptionMatcher::print() {
 196   ttyLocker ttyl;
 197   print_base(tty);
 198   switch (_type) {
 199   case IntxType:
 200     tty->print_cr(" intx %s = " INTX_FORMAT, _option, value<intx>());
 201     break;
 202   case UintxType:
 203     tty->print_cr(" uintx %s = " UINTX_FORMAT, _option, value<uintx>());
 204     break;
 205   case BoolType:
 206     tty->print_cr(" bool %s = %s", _option, value<bool>() ? "true" : "false");
 207     break;
 208   case DoubleType:
 209     tty->print_cr(" double %s = %f", _option, value<double>());
 210     break;
 211   case CcstrType:
 212     tty->print_cr(" const char* %s = '%s'", _option, value<ccstr>());
 213     break;
 214   default:
 215     ShouldNotReachHere();
 216   }
 217 }
 218 
 219 void TypedMethodOptionMatcher::print_all() {
 220    print();
 221    if (_next != NULL) {
 222      tty->print(" ");
 223      _next->print_all();
 224    }
 225  }
 226 
 227 TypedMethodOptionMatcher* TypedMethodOptionMatcher::clone() {
 228   TypedMethodOptionMatcher* m = new TypedMethodOptionMatcher();
 229   m->_class_mode = _class_mode;
 230   m->_class_name = _class_name;
 231   m->_method_mode = _method_mode;
 232   m->_method_name = _method_name;
 233   m->_signature = _signature;
 234   // Need to ref count the symbols
 235   if (_class_name != NULL) {
 236     _class_name->increment_refcount();
 237   }
 238   if (_method_name != NULL) {
 239     _method_name->increment_refcount();
 240   }
 241   if (_signature != NULL) {
 242     _signature->increment_refcount();
 243   }
 244   return m;
 245 }
 246 
 247 TypedMethodOptionMatcher::~TypedMethodOptionMatcher() {
 248   if (_option != NULL) {
 249     os::free((void*)_option);
 250   }
 251 }
 252 
 253 TypedMethodOptionMatcher* TypedMethodOptionMatcher::parse_method_pattern(char*& line, const char*& error_msg) {
 254   assert(error_msg == NULL, "Dont call here with error_msg already set");
 255   TypedMethodOptionMatcher* tom = new TypedMethodOptionMatcher();
 256   MethodMatcher::parse_method_pattern(line, error_msg, tom);
 257   if (error_msg != NULL) {
 258     delete tom;
 259     return NULL;
 260   }
 261   return tom;
 262 }
 263 
 264 TypedMethodOptionMatcher* TypedMethodOptionMatcher::match(methodHandle method, const char* opt, OptionType type) {
 265   TypedMethodOptionMatcher* current = this;
 266   while (current != NULL) {
 267     // Fastest compare first.
 268     if (current->type() == type) {
 269       if (strcmp(current->_option, opt) == 0) {
 270         if (current->matches(method)) {
 271           return current;
 272         }
 273       }
 274     }
 275     current = current->next();
 276   }
 277   return NULL;
 278 }
 279 
 280 template<typename T>
 281 static void add_option_string(TypedMethodOptionMatcher* matcher,
 282                                         const char* option,
 283                                         T value) {
 284   assert(matcher != option_list, "No circular lists please");
 285   matcher->init(option, get_type_for<T>(), option_list);
 286   matcher->set_value<T>(value);
 287   option_list = matcher;
 288   any_set = true;
 289   return;
 290 }
 291 
 292 static bool check_predicate(OracleCommand command, methodHandle method) {
 293   return ((lists[command] != NULL) &&
 294           !method.is_null() &&
 295           lists[command]->match(method));
 296 }
 297 
 298 static void add_predicate(OracleCommand command, BasicMatcher* bm) {
 299   assert(command != OptionCommand, "must use add_option_string");
 300   if (command == LogCommand && !LogCompilation && lists[LogCommand] == NULL) {
 301     tty->print_cr("Warning:  +LogCompilation must be enabled in order for individual methods to be logged.");
 302   }
 303   bm->set_next(lists[command]);
 304   lists[command] = bm;
 305   if ((command != DontInlineCommand) && (command != InlineCommand)) {
 306     any_set = true;
 307   }
 308   return;
 309 }
 310 
 311 template<typename T>
 312 bool CompilerOracle::has_option_value(const methodHandle& method, const char* option, T& value) {
 313   if (option_list != NULL) {
 314     TypedMethodOptionMatcher* m = option_list->match(method, option, get_type_for<T>());
 315     if (m != NULL) {
 316       value = m->value<T>();
 317       return true;
 318     }
 319   }
 320   return false;
 321 }
 322 
 323 bool CompilerOracle::has_any_option() {
 324   return any_set;
 325 }
 326 
 327 // Explicit instantiation for all OptionTypes supported.
 328 template bool CompilerOracle::has_option_value<intx>(const methodHandle& method, const char* option, intx& value);
 329 template bool CompilerOracle::has_option_value<uintx>(const methodHandle& method, const char* option, uintx& value);
 330 template bool CompilerOracle::has_option_value<bool>(const methodHandle& method, const char* option, bool& value);
 331 template bool CompilerOracle::has_option_value<ccstr>(const methodHandle& method, const char* option, ccstr& value);
 332 template bool CompilerOracle::has_option_value<double>(const methodHandle& method, const char* option, double& value);
 333 
 334 bool CompilerOracle::has_option_string(const methodHandle& method, const char* option) {
 335   bool value = false;
 336   has_option_value(method, option, value);
 337   return value;
 338 }
 339 
 340 bool CompilerOracle::should_exclude(const methodHandle& method) {
 341   if (check_predicate(ExcludeCommand, method)) {
 342     return true;
 343   }
 344   if (lists[CompileOnlyCommand] != NULL) {
 345     return !lists[CompileOnlyCommand]->match(method);
 346   }
 347   return false;
 348 }
 349 
 350 bool CompilerOracle::should_inline(const methodHandle& method) {
 351   return (check_predicate(InlineCommand, method));
 352 }
 353 
 354 bool CompilerOracle::should_not_inline(const methodHandle& method) {
 355   return check_predicate(DontInlineCommand, method) || check_predicate(ExcludeCommand, method);
 356 }
 357 
 358 bool CompilerOracle::should_print(const methodHandle& method) {
 359   return check_predicate(PrintCommand, method);
 360 }
 361 
 362 bool CompilerOracle::should_print_methods() {
 363   return lists[PrintCommand] != NULL;
 364 }
 365 
 366 bool CompilerOracle::should_log(const methodHandle& method) {
 367   if (!LogCompilation)            return false;
 368   if (lists[LogCommand] == NULL)  return true;  // by default, log all
 369   return (check_predicate(LogCommand, method));
 370 }
 371 
 372 bool CompilerOracle::should_break_at(const methodHandle& method) {
 373   return check_predicate(BreakCommand, method);
 374 }
 375 
 376 static OracleCommand parse_command_name(const char * line, int* bytes_read) {
 377   assert(ARRAY_SIZE(command_names) == OracleCommandCount,
 378          "command_names size mismatch");
 379 
 380   *bytes_read = 0;
 381   char command[33];
 382   int result = sscanf(line, "%32[a-z]%n", command, bytes_read);
 383   for (uint i = 0; i < ARRAY_SIZE(command_names); i++) {
 384     if (strcmp(command, command_names[i]) == 0) {
 385       return (OracleCommand)i;
 386     }
 387   }
 388   return UnknownCommand;
 389 }
 390 
 391 static void usage() {
 392   tty->cr();
 393   tty->print_cr("The CompileCommand option enables the user of the JVM to control specific");
 394   tty->print_cr("behavior of the dynamic compilers. Many commands require a pattern that defines");
 395   tty->print_cr("the set of methods the command shall be applied to. The CompileCommand");
 396   tty->print_cr("option provides the following commands:");
 397   tty->cr();
 398   tty->print_cr("  break,<pattern>       - debug breakpoint in compiler and in generated code");
 399   tty->print_cr("  print,<pattern>       - print assembly");
 400   tty->print_cr("  exclude,<pattern>     - don't compile or inline");
 401   tty->print_cr("  inline,<pattern>      - always inline");
 402   tty->print_cr("  dontinline,<pattern>  - don't inline");
 403   tty->print_cr("  compileonly,<pattern> - compile only");
 404   tty->print_cr("  log,<pattern>         - log compilation");
 405   tty->print_cr("  option,<pattern>,<option type>,<option name>,<value>");
 406   tty->print_cr("                        - set value of custom option");
 407   tty->print_cr("  option,<pattern>,<bool option name>");
 408   tty->print_cr("                        - shorthand for setting boolean flag");
 409   tty->print_cr("  quiet                 - silence the compile command output");
 410   tty->print_cr("  help                  - print this text");
 411   tty->cr();
 412   tty->print_cr("The preferred format for the method matching pattern is:");
 413   tty->print_cr("  package/Class.method()");
 414   tty->cr();
 415   tty->print_cr("For backward compatibility this form is also allowed:");
 416   tty->print_cr("  package.Class::method()");
 417   tty->cr();
 418   tty->print_cr("The signature can be separated by an optional whitespace or comma:");
 419   tty->print_cr("  package/Class.method ()");
 420   tty->cr();
 421   tty->print_cr("The class and method identifier can be used together with leading or");
 422   tty->print_cr("trailing *'s for a small amount of wildcarding:");
 423   tty->print_cr("  *ackage/Clas*.*etho*()");
 424   tty->cr();
 425   tty->print_cr("It is possible to use more than one CompileCommand on the command line:");
 426   tty->print_cr("  -XX:CompileCommand=exclude,java/*.* -XX:CompileCommand=log,java*.*");
 427   tty->cr();
 428   tty->print_cr("The CompileCommands can be loaded from a file with the flag");
 429   tty->print_cr("-XX:CompileCommandFile=<file> or be added to the file '.hotspot_compiler'");
 430   tty->print_cr("Use the same format in the file as the argument to the CompileCommand flag.");
 431   tty->print_cr("Add one command on each line.");
 432   tty->print_cr("  exclude java/*.*");
 433   tty->print_cr("  option java/*.* ReplayInline");
 434   tty->cr();
 435   tty->print_cr("The following commands have conflicting behavior: 'exclude', 'inline', 'dontinline',");
 436   tty->print_cr("and 'compileonly'. There is no priority of commands. Applying (a subset of) these");
 437   tty->print_cr("commands to the same method results in undefined behavior.");
 438   tty->cr();
 439 };
 440 
 441 // Scan next flag and value in line, return MethodMatcher object on success, NULL on failure.
 442 // On failure, error_msg contains description for the first error.
 443 // For future extensions: set error_msg on first error.
 444 static void scan_flag_and_value(const char* type, const char* line, int& total_bytes_read,
 445                                             TypedMethodOptionMatcher* matcher,
 446                                             char* errorbuf, const int buf_size) {
 447   total_bytes_read = 0;
 448   int bytes_read = 0;
 449   char flag[256];
 450 
 451   // Read flag name.
 452   if (sscanf(line, "%*[ \t]%255[a-zA-Z0-9]%n", flag, &bytes_read) == 1) {
 453     line += bytes_read;
 454     total_bytes_read += bytes_read;
 455 
 456     // Read value.
 457     if (strcmp(type, "intx") == 0) {
 458       intx value;
 459       if (sscanf(line, "%*[ \t]" INTX_FORMAT "%n", &value, &bytes_read) == 1) {
 460         total_bytes_read += bytes_read;
 461         add_option_string(matcher, flag, value);
 462         return;
 463       } else {
 464         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s ", flag, type);
 465       }
 466     } else if (strcmp(type, "uintx") == 0) {
 467       uintx value;
 468       if (sscanf(line, "%*[ \t]" UINTX_FORMAT "%n", &value, &bytes_read) == 1) {
 469         total_bytes_read += bytes_read;
 470         add_option_string(matcher, flag, value);
 471         return;
 472       } else {
 473         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 474       }
 475     } else if (strcmp(type, "ccstr") == 0) {
 476       ResourceMark rm;
 477       char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);
 478       if (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", value, &bytes_read) == 1) {
 479         total_bytes_read += bytes_read;
 480         add_option_string(matcher, flag, (ccstr)value);
 481         return;
 482       } else {
 483         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 484       }
 485     } else if (strcmp(type, "ccstrlist") == 0) {
 486       // Accumulates several strings into one. The internal type is ccstr.
 487       ResourceMark rm;
 488       char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);
 489       char* next_value = value;
 490       if (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", next_value, &bytes_read) == 1) {
 491         total_bytes_read += bytes_read;
 492         line += bytes_read;
 493         next_value += bytes_read;
 494         char* end_value = next_value-1;
 495         while (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", next_value, &bytes_read) == 1) {
 496           total_bytes_read += bytes_read;
 497           line += bytes_read;
 498           *end_value = ' '; // override '\0'
 499           next_value += bytes_read;
 500           end_value = next_value-1;
 501         }
 502         add_option_string(matcher, flag, (ccstr)value);
 503         return;
 504       } else {
 505         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 506       }
 507     } else if (strcmp(type, "bool") == 0) {
 508       char value[256];
 509       if (sscanf(line, "%*[ \t]%255[a-zA-Z]%n", value, &bytes_read) == 1) {
 510         if (strcmp(value, "true") == 0) {
 511           total_bytes_read += bytes_read;
 512           add_option_string(matcher, flag, true);
 513           return;
 514         } else if (strcmp(value, "false") == 0) {
 515           total_bytes_read += bytes_read;
 516           add_option_string(matcher, flag, false);
 517           return;
 518         } else {
 519           jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 520         }
 521       } else {
 522         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 523       }
 524     } else if (strcmp(type, "double") == 0) {
 525       char buffer[2][256];
 526       // Decimal separator '.' has been replaced with ' ' or '/' earlier,
 527       // so read integer and fraction part of double value separately.
 528       if (sscanf(line, "%*[ \t]%255[0-9]%*[ /\t]%255[0-9]%n", buffer[0], buffer[1], &bytes_read) == 2) {
 529         char value[512] = "";
 530         jio_snprintf(value, sizeof(value), "%s.%s", buffer[0], buffer[1]);
 531         total_bytes_read += bytes_read;
 532         add_option_string(matcher, flag, atof(value));
 533         return;
 534       } else {
 535         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 536       }
 537     } else {
 538       jio_snprintf(errorbuf, buf_size, "  Type %s not supported ", type);
 539     }
 540   } else {
 541     jio_snprintf(errorbuf, buf_size, "  Flag name for type %s should be alphanumeric ", type);
 542   }
 543   return;
 544 }
 545 
 546 int skip_whitespace(char* line) {
 547   // Skip any leading spaces
 548   int whitespace_read = 0;
 549   sscanf(line, "%*[ \t]%n", &whitespace_read);
 550   return whitespace_read;
 551 }
 552 
 553 void CompilerOracle::print_parse_error(const char*&  error_msg, char* original_line) {
 554   assert(error_msg != NULL, "Must have error_message");
 555 
 556   ttyLocker ttyl;
 557   tty->print_cr("CompileCommand: An error occurred during parsing");
 558   tty->print_cr("Line: %s", original_line);
 559   tty->print_cr("Error: %s", error_msg);
 560   CompilerOracle::print_tip();
 561 }
 562 
 563 void CompilerOracle::parse_from_line(char* line) {
 564   if (line[0] == '\0') return;
 565   if (line[0] == '#')  return;
 566 
 567   char* original_line = line;
 568   int bytes_read;
 569   OracleCommand command = parse_command_name(line, &bytes_read);
 570   line += bytes_read;
 571   ResourceMark rm;
 572 
 573   if (command == UnknownCommand) {
 574     ttyLocker ttyl;
 575     tty->print_cr("CompileCommand: unrecognized command");
 576     tty->print_cr("  \"%s\"", original_line);
 577     CompilerOracle::print_tip();
 578     return;
 579   }
 580 
 581   if (command == QuietCommand) {
 582     _quiet = true;
 583     return;
 584   }
 585 
 586   if (command == HelpCommand) {
 587     usage();
 588     return;
 589   }
 590 
 591   const char* error_msg = NULL;
 592   if (command == OptionCommand) {
 593     // Look for trailing options.
 594     //
 595     // Two types of trailing options are
 596     // supported:
 597     //
 598     // (1) CompileCommand=option,Klass::method,flag
 599     // (2) CompileCommand=option,Klass::method,type,flag,value
 600     //
 601     // Type (1) is used to enable a boolean flag for a method.
 602     //
 603     // Type (2) is used to support options with a value. Values can have the
 604     // the following types: intx, uintx, bool, ccstr, ccstrlist, and double.
 605     //
 606     // For future extensions: extend scan_flag_and_value()
 607 
 608     char option[256]; // stores flag for Type (1) and type of Type (2)
 609     line++; // skip the ','
 610     TypedMethodOptionMatcher* archetype = TypedMethodOptionMatcher::parse_method_pattern(line, error_msg);
 611     if (archetype == NULL) {
 612       assert(error_msg != NULL, "Must have error_message");
 613       print_parse_error(error_msg, original_line);
 614       return;
 615     }
 616 
 617     line += skip_whitespace(line);
 618 
 619     // This is unnecessarily complex. Should retire multi-option lines and skip while loop
 620     while (sscanf(line, "%255[a-zA-Z0-9]%n", option, &bytes_read) == 1) {
 621       line += bytes_read;
 622 
 623       // typed_matcher is used as a blueprint for each option, deleted at the end
 624       TypedMethodOptionMatcher* typed_matcher = archetype->clone();
 625       if (strcmp(option, "intx") == 0
 626           || strcmp(option, "uintx") == 0
 627           || strcmp(option, "bool") == 0
 628           || strcmp(option, "ccstr") == 0
 629           || strcmp(option, "ccstrlist") == 0
 630           || strcmp(option, "double") == 0
 631           ) {
 632         char errorbuf[1024] = {0};
 633         // Type (2) option: parse flag name and value.
 634         scan_flag_and_value(option, line, bytes_read, typed_matcher, errorbuf, sizeof(errorbuf));
 635         if (*errorbuf != '\0') {
 636           error_msg = errorbuf;
 637           print_parse_error(error_msg, original_line);
 638           return;
 639         }
 640         line += bytes_read;
 641       } else {
 642         // Type (1) option
 643         add_option_string(typed_matcher, option, true);
 644       }
 645       if (typed_matcher != NULL && !_quiet) {
 646         // Print out the last match added
 647         assert(error_msg == NULL, "No error here");
 648         ttyLocker ttyl;
 649         tty->print("CompileCommand: %s ", command_names[command]);
 650         typed_matcher->print();
 651       }
 652       line += skip_whitespace(line);
 653     } // while(
 654     delete archetype;
 655   } else {  // not an OptionCommand)
 656     assert(error_msg == NULL, "Don't call here with error_msg already set");
 657 
 658     BasicMatcher* matcher = BasicMatcher::parse_method_pattern(line, error_msg);
 659     if (error_msg != NULL) {
 660       assert(matcher == NULL, "consistency");
 661       print_parse_error(error_msg, original_line);
 662       return;
 663     }
 664 
 665     add_predicate(command, matcher);
 666     if (!_quiet) {
 667       ttyLocker ttyl;
 668       tty->print("CompileCommand: %s ", command_names[command]);
 669       matcher->print(tty);
 670       tty->cr();
 671     }
 672   }
 673 }
 674 
 675 void CompilerOracle::print_tip() {
 676   tty->cr();
 677   tty->print_cr("Usage: '-XX:CompileCommand=command,\"package/Class.method()\"'");
 678   tty->print_cr("Use:   '-XX:CompileCommand=help' for more information.");
 679   tty->cr();
 680 }
 681 
 682 static const char* default_cc_file = ".hotspot_compiler";
 683 
 684 static const char* cc_file() {
 685 #ifdef ASSERT
 686   if (CompileCommandFile == NULL)
 687     return default_cc_file;
 688 #endif
 689   return CompileCommandFile;
 690 }
 691 
 692 bool CompilerOracle::has_command_file() {
 693   return cc_file() != NULL;
 694 }
 695 
 696 bool CompilerOracle::_quiet = false;
 697 
 698 void CompilerOracle::parse_from_file() {
 699   assert(has_command_file(), "command file must be specified");
 700   FILE* stream = fopen(cc_file(), "rt");
 701   if (stream == NULL) return;
 702 
 703   char token[1024];
 704   int  pos = 0;
 705   int  c = getc(stream);
 706   while(c != EOF && pos < (int)(sizeof(token)-1)) {
 707     if (c == '\n') {
 708       token[pos++] = '\0';
 709       parse_from_line(token);
 710       pos = 0;
 711     } else {
 712       token[pos++] = c;
 713     }
 714     c = getc(stream);
 715   }
 716   token[pos++] = '\0';
 717   parse_from_line(token);
 718 
 719   fclose(stream);
 720 }
 721 
 722 void CompilerOracle::parse_from_string(const char* str, void (*parse_line)(char*)) {
 723   char token[1024];
 724   int  pos = 0;
 725   const char* sp = str;
 726   int  c = *sp++;
 727   while (c != '\0' && pos < (int)(sizeof(token)-1)) {
 728     if (c == '\n') {
 729       token[pos++] = '\0';
 730       parse_line(token);
 731       pos = 0;
 732     } else {
 733       token[pos++] = c;
 734     }
 735     c = *sp++;
 736   }
 737   token[pos++] = '\0';
 738   parse_line(token);
 739 }
 740 
 741 void CompilerOracle::append_comment_to_file(const char* message) {
 742   assert(has_command_file(), "command file must be specified");
 743   fileStream stream(fopen(cc_file(), "at"));
 744   stream.print("# ");
 745   for (int index = 0; message[index] != '\0'; index++) {
 746     stream.put(message[index]);
 747     if (message[index] == '\n') stream.print("# ");
 748   }
 749   stream.cr();
 750 }
 751 
 752 void CompilerOracle::append_exclude_to_file(const methodHandle& method) {
 753   assert(has_command_file(), "command file must be specified");
 754   fileStream stream(fopen(cc_file(), "at"));
 755   stream.print("exclude ");
 756   method->method_holder()->name()->print_symbol_on(&stream);
 757   stream.print(".");
 758   method->name()->print_symbol_on(&stream);
 759   method->signature()->print_symbol_on(&stream);
 760   stream.cr();
 761   stream.cr();
 762 }
 763 
 764 
 765 void compilerOracle_init() {
 766   CompilerOracle::parse_from_string(CompileCommand, CompilerOracle::parse_from_line);
 767   CompilerOracle::parse_from_string(CompileOnly, CompilerOracle::parse_compile_only);
 768   if (CompilerOracle::has_command_file()) {
 769     CompilerOracle::parse_from_file();
 770   } else {
 771     struct stat buf;
 772     if (os::stat(default_cc_file, &buf) == 0) {
 773       warning("%s file is present but has been ignored.  "
 774               "Run with -XX:CompileCommandFile=%s to load the file.",
 775               default_cc_file, default_cc_file);
 776     }
 777   }
 778   if (lists[PrintCommand] != NULL) {
 779     if (PrintAssembly) {
 780       warning("CompileCommand and/or %s file contains 'print' commands, but PrintAssembly is also enabled", default_cc_file);
 781     } else if (FLAG_IS_DEFAULT(DebugNonSafepoints)) {
 782       warning("printing of assembly code is enabled; turning on DebugNonSafepoints to gain additional output");
 783       DebugNonSafepoints = true;
 784     }
 785   }
 786 }
 787 
 788 
 789 void CompilerOracle::parse_compile_only(char * line) {
 790   int i;
 791   char name[1024];
 792   const char* className = NULL;
 793   const char* methodName = NULL;
 794 
 795   bool have_colon = (strstr(line, "::") != NULL);
 796   char method_sep = have_colon ? ':' : '.';
 797 
 798   if (Verbose) {
 799     tty->print_cr("%s", line);
 800   }
 801 
 802   ResourceMark rm;
 803   while (*line != '\0') {
 804     MethodMatcher::Mode c_match = MethodMatcher::Exact;
 805     MethodMatcher::Mode m_match = MethodMatcher::Exact;
 806 
 807     for (i = 0;
 808          i < 1024 && *line != '\0' && *line != method_sep && *line != ',' && !isspace(*line);
 809          line++, i++) {
 810       name[i] = *line;
 811       if (name[i] == '.')  name[i] = '/';  // package prefix uses '/'
 812     }
 813 
 814     if (i > 0) {
 815       char* newName = NEW_RESOURCE_ARRAY( char, i + 1);
 816       if (newName == NULL)
 817         return;
 818       strncpy(newName, name, i);
 819       newName[i] = '\0';
 820 
 821       if (className == NULL) {
 822         className = newName;
 823       } else {
 824         methodName = newName;
 825       }
 826     }
 827 
 828     if (*line == method_sep) {
 829       if (className == NULL) {
 830         className = "";
 831         c_match = MethodMatcher::Any;
 832       }
 833     } else {
 834       // got foo or foo/bar
 835       if (className == NULL) {
 836         ShouldNotReachHere();
 837       } else {
 838         // missing class name handled as "Any" class match
 839         if (className[0] == '\0') {
 840           c_match = MethodMatcher::Any;
 841         }
 842       }
 843     }
 844 
 845     // each directive is terminated by , or NUL or . followed by NUL
 846     if (*line == ',' || *line == '\0' || (line[0] == '.' && line[1] == '\0')) {
 847       if (methodName == NULL) {
 848         methodName = "";
 849         if (*line != method_sep) {
 850           m_match = MethodMatcher::Any;
 851         }
 852       }
 853 
 854       EXCEPTION_MARK;
 855       Symbol* c_name = SymbolTable::new_symbol(className, CHECK);
 856       Symbol* m_name = SymbolTable::new_symbol(methodName, CHECK);
 857       Symbol* signature = NULL;
 858 
 859       BasicMatcher* bm = new BasicMatcher();
 860       bm->init(c_name, c_match, m_name, m_match, signature);
 861       add_predicate(CompileOnlyCommand, bm);
 862       if (PrintVMOptions) {
 863         tty->print("CompileOnly: compileonly ");
 864         lists[CompileOnlyCommand]->print_all(tty);
 865       }
 866 
 867       className = NULL;
 868       methodName = NULL;
 869     }
 870 
 871     line = *line == '\0' ? line : line + 1;
 872   }
 873 }