1 /*
   2  * Copyright (c) 1998, 2014, 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 "memory/allocation.inline.hpp"
  28 #include "memory/oopFactory.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "oops/klass.hpp"
  31 #include "oops/method.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "oops/symbol.hpp"
  34 #include "runtime/handles.inline.hpp"
  35 #include "runtime/jniHandles.hpp"
  36 #include "runtime/os.hpp"
  37 
  38 class MethodMatcher : public CHeapObj<mtCompiler> {
  39  public:
  40   enum Mode {
  41     Exact,
  42     Prefix = 1,
  43     Suffix = 2,
  44     Substring = Prefix | Suffix,
  45     Any,
  46     Unknown = -1
  47   };
  48 
  49  protected:
  50   Symbol*        _class_name;
  51   Symbol*        _method_name;
  52   Symbol*        _signature;
  53   Mode           _class_mode;
  54   Mode           _method_mode;
  55   MethodMatcher* _next;
  56 
  57   static bool match(Symbol* candidate, Symbol* match, Mode match_mode);
  58 
  59   Symbol* class_name() const { return _class_name; }
  60   Symbol* method_name() const { return _method_name; }
  61   Symbol* signature() const { return _signature; }
  62 
  63  public:
  64   MethodMatcher(Symbol* class_name, Mode class_mode,
  65                 Symbol* method_name, Mode method_mode,
  66                 Symbol* signature, MethodMatcher* next);
  67   MethodMatcher(Symbol* class_name, Symbol* method_name, MethodMatcher* next);
  68 
  69   // utility method
  70   MethodMatcher* find(methodHandle method) {
  71     Symbol* class_name  = method->method_holder()->name();
  72     Symbol* method_name = method->name();
  73     for (MethodMatcher* current = this; current != NULL; current = current->_next) {
  74       if (match(class_name, current->class_name(), current->_class_mode) &&
  75           match(method_name, current->method_name(), current->_method_mode) &&
  76           (current->signature() == NULL || current->signature() == method->signature())) {
  77         return current;
  78       }
  79     }
  80     return NULL;
  81   }
  82 
  83   bool match(methodHandle method) {
  84     return find(method) != NULL;
  85   }
  86 
  87   MethodMatcher* next() const { return _next; }
  88 
  89   static void print_symbol(Symbol* h, Mode mode) {
  90     ResourceMark rm;
  91 
  92     if (mode == Suffix || mode == Substring || mode == Any) {
  93       tty->print("*");
  94     }
  95     if (mode != Any) {
  96       h->print_symbol_on(tty);
  97     }
  98     if (mode == Prefix || mode == Substring) {
  99       tty->print("*");
 100     }
 101   }
 102 
 103   void print_base() {
 104     print_symbol(class_name(), _class_mode);
 105     tty->print(".");
 106     print_symbol(method_name(), _method_mode);
 107     if (signature() != NULL) {
 108       signature()->print_symbol_on(tty);
 109     }
 110   }
 111 
 112   virtual void print() {
 113     print_base();
 114     tty->cr();
 115   }
 116 };
 117 
 118 MethodMatcher::MethodMatcher(Symbol* class_name, Symbol* method_name, MethodMatcher* next) {
 119   _class_name  = class_name;
 120   _method_name = method_name;
 121   _next        = next;
 122   _class_mode  = MethodMatcher::Exact;
 123   _method_mode = MethodMatcher::Exact;
 124   _signature   = NULL;
 125 }
 126 
 127 
 128 MethodMatcher::MethodMatcher(Symbol* class_name, Mode class_mode,
 129                              Symbol* method_name, Mode method_mode,
 130                              Symbol* signature, MethodMatcher* next):
 131     _class_mode(class_mode)
 132   , _method_mode(method_mode)
 133   , _next(next)
 134   , _class_name(class_name)
 135   , _method_name(method_name)
 136   , _signature(signature) {
 137 }
 138 
 139 bool MethodMatcher::match(Symbol* candidate, Symbol* match, Mode match_mode) {
 140   if (match_mode == Any) {
 141     return true;
 142   }
 143 
 144   if (match_mode == Exact) {
 145     return candidate == match;
 146   }
 147 
 148   ResourceMark rm;
 149   const char * candidate_string = candidate->as_C_string();
 150   const char * match_string = match->as_C_string();
 151 
 152   switch (match_mode) {
 153   case Prefix:
 154     return strstr(candidate_string, match_string) == candidate_string;
 155 
 156   case Suffix: {
 157     size_t clen = strlen(candidate_string);
 158     size_t mlen = strlen(match_string);
 159     return clen >= mlen && strcmp(candidate_string + clen - mlen, match_string) == 0;
 160   }
 161 
 162   case Substring:
 163     return strstr(candidate_string, match_string) != NULL;
 164 
 165   default:
 166     return false;
 167   }
 168 }
 169 
 170 enum OptionType {
 171   IntxType,
 172   UintxType,
 173   BoolType,
 174   CcstrType,
 175   DoubleType,
 176   UnknownType
 177 };
 178 
 179 /* Methods to map real type names to OptionType */
 180 template<typename T>
 181 static OptionType get_type_for() {
 182   return UnknownType;
 183 };
 184 
 185 template<> OptionType get_type_for<intx>() {
 186   return IntxType;
 187 }
 188 
 189 template<> OptionType get_type_for<uintx>() {
 190   return UintxType;
 191 }
 192 
 193 template<> OptionType get_type_for<bool>() {
 194   return BoolType;
 195 }
 196 
 197 template<> OptionType get_type_for<ccstr>() {
 198   return CcstrType;
 199 }
 200 
 201 template<> OptionType get_type_for<double>() {
 202   return DoubleType;
 203 }
 204 
 205 template<typename T>
 206 static const T copy_value(const T value) {
 207   return value;
 208 }
 209 
 210 template<> const ccstr copy_value<ccstr>(const ccstr value) {
 211   return (const ccstr)os::strdup_check_oom(value);
 212 }
 213 
 214 template <typename T>
 215 class TypedMethodOptionMatcher : public MethodMatcher {
 216   const char* _option;
 217   OptionType _type;
 218   const T _value;
 219 
 220 public:
 221   TypedMethodOptionMatcher(Symbol* class_name, Mode class_mode,
 222                            Symbol* method_name, Mode method_mode,
 223                            Symbol* signature, const char* opt,
 224                            const T value,  MethodMatcher* next) :
 225     MethodMatcher(class_name, class_mode, method_name, method_mode, signature, next),
 226                   _type(get_type_for<T>()), _value(copy_value<T>(value)) {
 227     _option = os::strdup_check_oom(opt);
 228   }
 229 
 230   ~TypedMethodOptionMatcher() {
 231     os::free((void*)_option);
 232   }
 233 
 234   TypedMethodOptionMatcher* match(methodHandle method, const char* opt) {
 235     TypedMethodOptionMatcher* current = this;
 236     while (current != NULL) {
 237       current = (TypedMethodOptionMatcher*)current->find(method);
 238       if (current == NULL) {
 239         return NULL;
 240       }
 241       if (strcmp(current->_option, opt) == 0) {
 242         return current;
 243       }
 244       current = current->next();
 245     }
 246     return NULL;
 247   }
 248 
 249   TypedMethodOptionMatcher* next() {
 250     return (TypedMethodOptionMatcher*)_next;
 251   }
 252 
 253   OptionType get_type(void) {
 254       return _type;
 255   };
 256 
 257   T value() { return _value; }
 258 
 259   void print() {
 260     ttyLocker ttyl;
 261     print_base();
 262     tty->print(" %s", _option);
 263     tty->print(" <unknown option type>");
 264     tty->cr();
 265   }
 266 };
 267 
 268 template<>
 269 void TypedMethodOptionMatcher<intx>::print() {
 270   ttyLocker ttyl;
 271   print_base();
 272   tty->print(" intx %s", _option);
 273   tty->print(" = " INTX_FORMAT, _value);
 274   tty->cr();
 275 };
 276 
 277 template<>
 278 void TypedMethodOptionMatcher<uintx>::print() {
 279   ttyLocker ttyl;
 280   print_base();
 281   tty->print(" uintx %s", _option);
 282   tty->print(" = " UINTX_FORMAT, _value);
 283   tty->cr();
 284 };
 285 
 286 template<>
 287 void TypedMethodOptionMatcher<bool>::print() {
 288   ttyLocker ttyl;
 289   print_base();
 290   tty->print(" bool %s", _option);
 291   tty->print(" = %s", _value ? "true" : "false");
 292   tty->cr();
 293 };
 294 
 295 template<>
 296 void TypedMethodOptionMatcher<ccstr>::print() {
 297   ttyLocker ttyl;
 298   print_base();
 299   tty->print(" const char* %s", _option);
 300   tty->print(" = '%s'", _value);
 301   tty->cr();
 302 };
 303 
 304 template<>
 305 void TypedMethodOptionMatcher<double>::print() {
 306   ttyLocker ttyl;
 307   print_base();
 308   tty->print(" double %s", _option);
 309   tty->print(" = %f", _value);
 310   tty->cr();
 311 };
 312 
 313 // this must parallel the command_names below
 314 enum OracleCommand {
 315   UnknownCommand = -1,
 316   OracleFirstCommand = 0,
 317   BreakCommand = OracleFirstCommand,
 318   PrintCommand,
 319   ExcludeCommand,
 320   InlineCommand,
 321   DontInlineCommand,
 322   CompileOnlyCommand,
 323   LogCommand,
 324   OptionCommand,
 325   QuietCommand,
 326   HelpCommand,
 327   OracleCommandCount
 328 };
 329 
 330 // this must parallel the enum OracleCommand
 331 static const char * command_names[] = {
 332   "break",
 333   "print",
 334   "exclude",
 335   "inline",
 336   "dontinline",
 337   "compileonly",
 338   "log",
 339   "option",
 340   "quiet",
 341   "help"
 342 };
 343 
 344 class MethodMatcher;
 345 static MethodMatcher* lists[OracleCommandCount] = { 0, };
 346 
 347 
 348 static bool check_predicate(OracleCommand command, methodHandle method) {
 349   return ((lists[command] != NULL) &&
 350           !method.is_null() &&
 351           lists[command]->match(method));
 352 }
 353 
 354 
 355 static MethodMatcher* add_predicate(OracleCommand command,
 356                                     Symbol* class_name, MethodMatcher::Mode c_mode,
 357                                     Symbol* method_name, MethodMatcher::Mode m_mode,
 358                                     Symbol* signature) {
 359   assert(command != OptionCommand, "must use add_option_string");
 360   if (command == LogCommand && !LogCompilation && lists[LogCommand] == NULL)
 361     tty->print_cr("Warning:  +LogCompilation must be enabled in order for individual methods to be logged.");
 362   lists[command] = new MethodMatcher(class_name, c_mode, method_name, m_mode, signature, lists[command]);
 363   return lists[command];
 364 }
 365 
 366 template<typename T>
 367 static MethodMatcher* add_option_string(Symbol* class_name, MethodMatcher::Mode c_mode,
 368                                         Symbol* method_name, MethodMatcher::Mode m_mode,
 369                                         Symbol* signature,
 370                                         const char* option,
 371                                         T value) {
 372   lists[OptionCommand] = new TypedMethodOptionMatcher<T>(class_name, c_mode, method_name, m_mode,
 373                                                          signature, option, value, lists[OptionCommand]);
 374   return lists[OptionCommand];
 375 }
 376 
 377 template<typename T>
 378 static bool get_option_value(methodHandle method, const char* option, T& value) {
 379    TypedMethodOptionMatcher<T>* m;
 380    if (lists[OptionCommand] != NULL
 381        && (m = ((TypedMethodOptionMatcher<T>*)lists[OptionCommand])->match(method, option)) != NULL
 382        && m->get_type() == get_type_for<T>()) {
 383        value = m->value();
 384        return true;
 385    } else {
 386      return false;
 387    }
 388 }
 389 
 390 bool CompilerOracle::has_option_string(methodHandle method, const char* option) {
 391   bool value = false;
 392   get_option_value(method, option, value);
 393   return value;
 394 }
 395 
 396 template<typename T>
 397 bool CompilerOracle::has_option_value(methodHandle method, const char* option, T& value) {
 398   return ::get_option_value(method, option, value);
 399 }
 400 
 401 // Explicit instantiation for all OptionTypes supported.
 402 template bool CompilerOracle::has_option_value<intx>(methodHandle method, const char* option, intx& value);
 403 template bool CompilerOracle::has_option_value<uintx>(methodHandle method, const char* option, uintx& value);
 404 template bool CompilerOracle::has_option_value<bool>(methodHandle method, const char* option, bool& value);
 405 template bool CompilerOracle::has_option_value<ccstr>(methodHandle method, const char* option, ccstr& value);
 406 template bool CompilerOracle::has_option_value<double>(methodHandle method, const char* option, double& value);
 407 
 408 bool CompilerOracle::should_exclude(methodHandle method, bool& quietly) {
 409   quietly = true;
 410   if (lists[ExcludeCommand] != NULL) {
 411     if (lists[ExcludeCommand]->match(method)) {
 412       quietly = _quiet;
 413       return true;
 414     }
 415   }
 416 
 417   if (lists[CompileOnlyCommand] != NULL) {
 418     return !lists[CompileOnlyCommand]->match(method);
 419   }
 420   return false;
 421 }
 422 
 423 
 424 bool CompilerOracle::should_inline(methodHandle method) {
 425   return (check_predicate(InlineCommand, method));
 426 }
 427 
 428 
 429 bool CompilerOracle::should_not_inline(methodHandle method) {
 430   return (check_predicate(DontInlineCommand, method));
 431 }
 432 
 433 
 434 bool CompilerOracle::should_print(methodHandle method) {
 435   return (check_predicate(PrintCommand, method));
 436 }
 437 
 438 bool CompilerOracle::should_print_methods() {
 439   return lists[PrintCommand] != NULL;
 440 }
 441 
 442 bool CompilerOracle::should_log(methodHandle method) {
 443   if (!LogCompilation)            return false;
 444   if (lists[LogCommand] == NULL)  return true;  // by default, log all
 445   return (check_predicate(LogCommand, method));
 446 }
 447 
 448 
 449 bool CompilerOracle::should_break_at(methodHandle method) {
 450   return check_predicate(BreakCommand, method);
 451 }
 452 
 453 
 454 static OracleCommand parse_command_name(const char * line, int* bytes_read) {
 455   assert(ARRAY_SIZE(command_names) == OracleCommandCount,
 456          "command_names size mismatch");
 457 
 458   *bytes_read = 0;
 459   char command[33];
 460   int result = sscanf(line, "%32[a-z]%n", command, bytes_read);
 461   for (uint i = 0; i < ARRAY_SIZE(command_names); i++) {
 462     if (strcmp(command, command_names[i]) == 0) {
 463       return (OracleCommand)i;
 464     }
 465   }
 466   return UnknownCommand;
 467 }
 468 
 469 static void usage() {
 470   tty->cr();
 471   tty->print_cr("The CompileCommand option enables the user of the JVM to control specific");
 472   tty->print_cr("behavior of the dynamic compilers. The CompileCommand option defines the");
 473   tty->print_cr("following commands:");
 474   tty->cr();
 475   tty->print_cr("  break,<pattern>       - debug breakpoint in compiler and in generated code");
 476   tty->print_cr("  print,<pattern>       - print assembly of method");
 477   tty->print_cr("  exclude,<pattern>     - don't compile or inline this ");
 478   tty->print_cr("  inline,<pattern>      - always inline this method");
 479   tty->print_cr("  dontinline,<pattern>  - don't inline this method");
 480   tty->print_cr("  compileonly,<pattern> - compile only this method");
 481   tty->print_cr("  log,<pattern>         - log compilation of method");
 482   tty->print_cr("  option,<pattern>,<option type>,<option name>,<value>");
 483   tty->print_cr("                        - set value of custom option");
 484   tty->print_cr("  option,<pattern>,<bool option name>");
 485   tty->print_cr("                        - shorthand for setting boolean flag");
 486   tty->print_cr("  quiet                 - silence the compile command output");
 487   tty->print_cr("  help                  - print this text");
 488   tty->cr();
 489   tty->print_cr("The preferred pattern for referencing a method is:");
 490   tty->print_cr("  \"package/Class.method()\"");
 491   tty->cr();
 492   tty->print_cr("For backwards compatibility this form is also allowed:");
 493   tty->print_cr("  \"package.Class::method()\"");
 494   tty->cr();
 495   tty->print_cr("The signature can be separated by an optional whitespace or comma:");
 496   tty->print_cr("  \"package/Class.method ()\"");
 497   tty->print_cr("  \"package.Class::method ()\"");
 498   tty->print_cr("  \"package/Class,method,()\"");
 499   tty->cr();
 500   tty->print_cr("The class identifier and method can can be used together with leading or");
 501   tty->print_cr("trailing *'s for a small amount of wildcarding:");
 502   tty->print_cr("  \"*ackage/Clas*.*etho*()\"");
 503   tty->cr();
 504   tty->print_cr("It is possible to use more than one CompileCommand:");
 505   tty->print_cr("  -XX:CompileCommand=exclude,\"java/*.*\" -XX:CompileCommand=log,\"java*.*\"");
 506   tty->cr();
 507   tty->print_cr("The CompileCommands can be loaded from a file with the flag");
 508   tty->print_cr("-XX:CompileCommandFile=<file>. Use the same format without the flag:");
 509   tty->print_cr("  exclude,\"java/*.*\"");
 510   tty->print_cr("  log,\"java*.*\"");
 511   tty->cr();
 512   tty->print_cr("The following commands have conflicting behavior: 'exclude', 'inline', 'dontinline',");
 513   tty->print_cr("and 'compileonly'. There is no priority of commands. Applying (a subset of) these");
 514   tty->print_cr("commands to the same method results in undefined behavior.");
 515   tty->cr();
 516 };
 517 
 518 // The characters allowed is based on the JVM specification.
 519 // Some tokens disallowed in the JVMS have some meaning
 520 // when parsing so we need to include them here.
 521 // The parser do not enforce all rules of the JVMS - a successful parse
 522 // does not mean that it is an allowed name. Illegal names will simply
 523 // be ignored since they never can match a class or method.
 524 //
 525 // '\0' and 0xf0-0xff are disallowed in constant string values
 526 // 0x20 ' ', 0x09 '\t' are used in the matching
 527 // 0x5b '[' and 0x5d ']' can not be used by the matcher
 528 // 0x28 '(' and 0x29 ')' are used for the signature
 529 // 0x2e '.' is always replaced before the matching
 530 // 0x2f '/' is only used in the class name (package separator)
 531 
 532 #define RANGEBASE "\x1\x2\x3\x4\x5\x6\x7\x8\xa\xb\xc\xd\xe\xf" \
 533     "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
 534     "\x21\x22\x23\x24\x25\x26\x27\x2a\x2b\x2c\x2d" \
 535     "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" \
 536     "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" \
 537     "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5c\x5e\x5f" \
 538     "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" \
 539     "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f" \
 540     "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" \
 541     "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" \
 542     "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" \
 543     "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" \
 544     "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" \
 545     "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" \
 546     "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
 547 
 548 #define RANGE0 "[*" RANGEBASE "]"
 549 #define RANGESLASH "[*" RANGEBASE "/]"
 550 
 551 static MethodMatcher::Mode check_mode(char name[], const char*& error_msg) {
 552   int match = MethodMatcher::Exact;
 553   while (name[0] == '*') {
 554     match |= MethodMatcher::Suffix;
 555     strcpy(name, name + 1);
 556   }
 557 
 558   if (strcmp(name, "*") == 0) return MethodMatcher::Any;
 559 
 560   size_t len = strlen(name);
 561   while (len > 0 && name[len - 1] == '*') {
 562     match |= MethodMatcher::Prefix;
 563     name[--len] = '\0';
 564   }
 565 
 566   if (strstr(name, "*") != NULL) {
 567     error_msg = "  Embedded * not allowed";
 568     return MethodMatcher::Unknown;
 569   }
 570   return (MethodMatcher::Mode)match;
 571 }
 572 
 573 static bool scan_line(const char * line,
 574                       char class_name[],  MethodMatcher::Mode* c_mode,
 575                       char method_name[], MethodMatcher::Mode* m_mode,
 576                       int* bytes_read, const char*& error_msg) {
 577   *bytes_read = 0;
 578   error_msg = NULL;
 579   if (2 == sscanf(line, "%*[ \t]%255" RANGESLASH "%*[ ]" "%255"  RANGE0 "%n", class_name, method_name, bytes_read)) {
 580     *c_mode = check_mode(class_name, error_msg);
 581     *m_mode = check_mode(method_name, error_msg);
 582     return *c_mode != MethodMatcher::Unknown && *m_mode != MethodMatcher::Unknown;
 583   }
 584   return false;
 585 }
 586 
 587 
 588 
 589 // Scan next flag and value in line, return MethodMatcher object on success, NULL on failure.
 590 // On failure, error_msg contains description for the first error.
 591 // For future extensions: set error_msg on first error.
 592 static MethodMatcher* scan_flag_and_value(const char* type, const char* line, int& total_bytes_read,
 593                                           Symbol* c_name, MethodMatcher::Mode c_match,
 594                                           Symbol* m_name, MethodMatcher::Mode m_match,
 595                                           Symbol* signature,
 596                                           char* errorbuf, const int buf_size) {
 597   total_bytes_read = 0;
 598   int bytes_read = 0;
 599   char flag[256];
 600 
 601   // Read flag name.
 602   if (sscanf(line, "%*[ \t]%255[a-zA-Z0-9]%n", flag, &bytes_read) == 1) {
 603     line += bytes_read;
 604     total_bytes_read += bytes_read;
 605 
 606     // Read value.
 607     if (strcmp(type, "intx") == 0) {
 608       intx value;
 609       if (sscanf(line, "%*[ \t]" INTX_FORMAT "%n", &value, &bytes_read) == 1) {
 610         total_bytes_read += bytes_read;
 611         return add_option_string(c_name, c_match, m_name, m_match, signature, flag, value);
 612       } else {
 613         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s ", flag, type);
 614       }
 615     } else if (strcmp(type, "uintx") == 0) {
 616       uintx value;
 617       if (sscanf(line, "%*[ \t]" UINTX_FORMAT "%n", &value, &bytes_read) == 1) {
 618         total_bytes_read += bytes_read;
 619         return add_option_string(c_name, c_match, m_name, m_match, signature, flag, value);
 620       } else {
 621         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 622       }
 623     } else if (strcmp(type, "ccstr") == 0) {
 624       ResourceMark rm;
 625       char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);
 626       if (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", value, &bytes_read) == 1) {
 627         total_bytes_read += bytes_read;
 628         return add_option_string(c_name, c_match, m_name, m_match, signature, flag, (ccstr)value);
 629       } else {
 630         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 631       }
 632     } else if (strcmp(type, "ccstrlist") == 0) {
 633       // Accumulates several strings into one. The internal type is ccstr.
 634       ResourceMark rm;
 635       char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);
 636       char* next_value = value;
 637       if (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", next_value, &bytes_read) == 1) {
 638         total_bytes_read += bytes_read;
 639         line += bytes_read;
 640         next_value += bytes_read;
 641         char* end_value = next_value-1;
 642         while (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", next_value, &bytes_read) == 1) {
 643           total_bytes_read += bytes_read;
 644           line += bytes_read;
 645           *end_value = ' '; // override '\0'
 646           next_value += bytes_read;
 647           end_value = next_value-1;
 648         }
 649         return add_option_string(c_name, c_match, m_name, m_match, signature, flag, (ccstr)value);
 650       } else {
 651         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 652       }
 653     } else if (strcmp(type, "bool") == 0) {
 654       char value[256];
 655       if (sscanf(line, "%*[ \t]%255[a-zA-Z]%n", value, &bytes_read) == 1) {
 656         if (strcmp(value, "true") == 0) {
 657           total_bytes_read += bytes_read;
 658           return add_option_string(c_name, c_match, m_name, m_match, signature, flag, true);
 659         } else if (strcmp(value, "false") == 0) {
 660           total_bytes_read += bytes_read;
 661           return add_option_string(c_name, c_match, m_name, m_match, signature, flag, false);
 662         } else {
 663           jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 664         }
 665       } else {
 666         jio_snprintf(errorbuf, sizeof(errorbuf), "  Value cannot be read for flag %s of type %s", flag, type);
 667       }
 668     } else if (strcmp(type, "double") == 0) {
 669       char buffer[2][256];
 670       // Decimal separator '.' has been replaced with ' ' or '/' earlier,
 671       // so read integer and fraction part of double value separately.
 672       if (sscanf(line, "%*[ \t]%255[0-9]%*[ /\t]%255[0-9]%n", buffer[0], buffer[1], &bytes_read) == 2) {
 673         char value[512] = "";
 674         strncat(value, buffer[0], 255);
 675         strcat(value, ".");
 676         strncat(value, buffer[1], 255);
 677         total_bytes_read += bytes_read;
 678         return add_option_string(c_name, c_match, m_name, m_match, signature, flag, atof(value));
 679       } else {
 680         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 681       }
 682     } else {
 683       jio_snprintf(errorbuf, sizeof(errorbuf), "  Type %s not supported ", type);
 684     }
 685   } else {
 686     jio_snprintf(errorbuf, sizeof(errorbuf), "  Flag name for type %s should be alphanumeric ", type);
 687   }
 688   return NULL;
 689 }
 690 
 691 void CompilerOracle::parse_from_line(char* line) {
 692   if (line[0] == '\0') return;
 693   if (line[0] == '#')  return;
 694 
 695   bool have_colon = (strstr(line, "::") != NULL);
 696   for (char* lp = line; *lp != '\0'; lp++) {
 697     // Allow '.' to separate the class name from the method name.
 698     // This is the preferred spelling of methods:
 699     //      exclude java/lang/String.indexOf(I)I
 700     // Allow ',' for spaces (eases command line quoting).
 701     //      exclude,java/lang/String.indexOf
 702     // For backward compatibility, allow space as separator also.
 703     //      exclude java/lang/String indexOf
 704     //      exclude,java/lang/String,indexOf
 705     // For easy cut-and-paste of method names, allow VM output format
 706     // as produced by Method::print_short_name:
 707     //      exclude java.lang.String::indexOf
 708     // For simple implementation convenience here, convert them all to space.
 709     if (have_colon) {
 710       if (*lp == '.')  *lp = '/';   // dots build the package prefix
 711       if (*lp == ':')  *lp = ' ';
 712     }
 713     if (*lp == ',' || *lp == '.')  *lp = ' ';
 714   }
 715 
 716   char* original_line = line;
 717   int bytes_read;
 718   OracleCommand command = parse_command_name(line, &bytes_read);
 719   line += bytes_read;
 720   ResourceMark rm;
 721 
 722   if (command == UnknownCommand) {
 723     ttyLocker ttyl;
 724     tty->print_cr("CompileCommand: unrecognized command");
 725     tty->print_cr("  \"%s\"", original_line);
 726     CompilerOracle::print_tip();
 727     return;
 728   }
 729 
 730   if (command == QuietCommand) {
 731     _quiet = true;
 732     return;
 733   }
 734 
 735   if (command == HelpCommand) {
 736     usage();
 737     return;
 738   }
 739 
 740   MethodMatcher::Mode c_match = MethodMatcher::Exact;
 741   MethodMatcher::Mode m_match = MethodMatcher::Exact;
 742   char class_name[256];
 743   char method_name[256];
 744   char sig[1024];
 745   char errorbuf[1024];
 746   const char* error_msg = NULL; // description of first error that appears
 747   MethodMatcher* match = NULL;
 748 
 749   if (scan_line(line, class_name, &c_match, method_name, &m_match, &bytes_read, error_msg)) {
 750     EXCEPTION_MARK;
 751     Symbol* c_name = SymbolTable::new_symbol(class_name, CHECK);
 752     Symbol* m_name = SymbolTable::new_symbol(method_name, CHECK);
 753     Symbol* signature = NULL;
 754 
 755     line += bytes_read;
 756 
 757     // Skip any leading spaces before signature
 758     int whitespace_read = 0;
 759     sscanf(line, "%*[ \t]%n", &whitespace_read);
 760     if (whitespace_read > 0) {
 761       line += whitespace_read;
 762     }
 763 
 764     // there might be a signature following the method.
 765     // signatures always begin with ( so match that by hand
 766     if (1 == sscanf(line, "(%254[[);/" RANGEBASE "]%n", sig + 1, &bytes_read)) {
 767       sig[0] = '(';
 768       line += bytes_read;
 769       signature = SymbolTable::new_symbol(sig, CHECK);
 770     }
 771 
 772     if (command == OptionCommand) {
 773       // Look for trailing options.
 774       //
 775       // Two types of trailing options are
 776       // supported:
 777       //
 778       // (1) CompileCommand=option,Klass::method,flag
 779       // (2) CompileCommand=option,Klass::method,type,flag,value
 780       //
 781       // Type (1) is used to enable a boolean flag for a method.
 782       //
 783       // Type (2) is used to support options with a value. Values can have the
 784       // the following types: intx, uintx, bool, ccstr, ccstrlist, and double.
 785       //
 786       // For future extensions: extend scan_flag_and_value()
 787       char option[256]; // stores flag for Type (1) and type of Type (2)
 788       while (sscanf(line, "%*[ \t]%255[a-zA-Z0-9]%n", option, &bytes_read) == 1) {
 789         if (match != NULL && !_quiet) {
 790           // Print out the last match added
 791           ttyLocker ttyl;
 792           tty->print("CompileCommand: %s ", command_names[command]);
 793           match->print();
 794         }
 795         line += bytes_read;
 796 
 797         if (strcmp(option, "intx") == 0
 798             || strcmp(option, "uintx") == 0
 799             || strcmp(option, "bool") == 0
 800             || strcmp(option, "ccstr") == 0
 801             || strcmp(option, "ccstrlist") == 0
 802             || strcmp(option, "double") == 0
 803             ) {
 804 
 805           // Type (2) option: parse flag name and value.
 806           match = scan_flag_and_value(option, line, bytes_read,
 807                                       c_name, c_match, m_name, m_match, signature,
 808                                       errorbuf, sizeof(errorbuf));
 809           if (match == NULL) {
 810             error_msg = errorbuf;
 811             break;
 812           }
 813           line += bytes_read;
 814         } else {
 815           // Type (1) option
 816           match = add_option_string(c_name, c_match, m_name, m_match, signature, option, true);
 817         }
 818       } // while(
 819     } else {
 820       match = add_predicate(command, c_name, c_match, m_name, m_match, signature);
 821     }
 822   }
 823 
 824   ttyLocker ttyl;
 825   if (error_msg != NULL) {
 826     // an error has happened
 827     tty->print_cr("CompileCommand: An error occured during parsing");
 828     tty->print_cr("  \"%s\"", original_line);
 829     if (error_msg != NULL) {
 830       tty->print_cr("%s", error_msg);
 831     }
 832     CompilerOracle::print_tip();
 833 
 834   } else {
 835     // check for remaining characters
 836     bytes_read = 0;
 837     sscanf(line, "%*[ \t]%n", &bytes_read);
 838     if (line[bytes_read] != '\0') {
 839       tty->print_cr("CompileCommand: Bad pattern");
 840       tty->print_cr("  \"%s\"", original_line);
 841       tty->print_cr("  Unrecognized text %s after command ", line);
 842       CompilerOracle::print_tip();
 843     } else if (match != NULL && !_quiet) {
 844       tty->print("CompileCommand: %s ", command_names[command]);
 845       match->print();
 846     }
 847   }
 848 }
 849 
 850 void CompilerOracle::print_tip() {
 851   tty->cr();
 852   tty->print_cr("Usage: '-XX:CompileCommand=command,\"package/Class.method()\"'");
 853   tty->print_cr("Use:   '-XX:CompileCommand=help' for more information.");
 854   tty->cr();
 855 }
 856 
 857 static const char* default_cc_file = ".hotspot_compiler";
 858 
 859 static const char* cc_file() {
 860 #ifdef ASSERT
 861   if (CompileCommandFile == NULL)
 862     return default_cc_file;
 863 #endif
 864   return CompileCommandFile;
 865 }
 866 
 867 bool CompilerOracle::has_command_file() {
 868   return cc_file() != NULL;
 869 }
 870 
 871 bool CompilerOracle::_quiet = false;
 872 
 873 void CompilerOracle::parse_from_file() {
 874   assert(has_command_file(), "command file must be specified");
 875   FILE* stream = fopen(cc_file(), "rt");
 876   if (stream == NULL) return;
 877 
 878   char token[1024];
 879   int  pos = 0;
 880   int  c = getc(stream);
 881   while(c != EOF && pos < (int)(sizeof(token)-1)) {
 882     if (c == '\n') {
 883       token[pos++] = '\0';
 884       parse_from_line(token);
 885       pos = 0;
 886     } else {
 887       token[pos++] = c;
 888     }
 889     c = getc(stream);
 890   }
 891   token[pos++] = '\0';
 892   parse_from_line(token);
 893 
 894   fclose(stream);
 895 }
 896 
 897 void CompilerOracle::parse_from_string(const char* str, void (*parse_line)(char*)) {
 898   char token[1024];
 899   int  pos = 0;
 900   const char* sp = str;
 901   int  c = *sp++;
 902   while (c != '\0' && pos < (int)(sizeof(token)-1)) {
 903     if (c == '\n') {
 904       token[pos++] = '\0';
 905       parse_line(token);
 906       pos = 0;
 907     } else {
 908       token[pos++] = c;
 909     }
 910     c = *sp++;
 911   }
 912   token[pos++] = '\0';
 913   parse_line(token);
 914 }
 915 
 916 void CompilerOracle::append_comment_to_file(const char* message) {
 917   assert(has_command_file(), "command file must be specified");
 918   fileStream stream(fopen(cc_file(), "at"));
 919   stream.print("# ");
 920   for (int index = 0; message[index] != '\0'; index++) {
 921     stream.put(message[index]);
 922     if (message[index] == '\n') stream.print("# ");
 923   }
 924   stream.cr();
 925 }
 926 
 927 void CompilerOracle::append_exclude_to_file(methodHandle method) {
 928   assert(has_command_file(), "command file must be specified");
 929   fileStream stream(fopen(cc_file(), "at"));
 930   stream.print("exclude ");
 931   method->method_holder()->name()->print_symbol_on(&stream);
 932   stream.print(".");
 933   method->name()->print_symbol_on(&stream);
 934   method->signature()->print_symbol_on(&stream);
 935   stream.cr();
 936   stream.cr();
 937 }
 938 
 939 
 940 void compilerOracle_init() {
 941   CompilerOracle::parse_from_string(CompileCommand, CompilerOracle::parse_from_line);
 942   CompilerOracle::parse_from_string(CompileOnly, CompilerOracle::parse_compile_only);
 943   if (CompilerOracle::has_command_file()) {
 944     CompilerOracle::parse_from_file();
 945   } else {
 946     struct stat buf;
 947     if (os::stat(default_cc_file, &buf) == 0) {
 948       warning("%s file is present but has been ignored.  "
 949               "Run with -XX:CompileCommandFile=%s to load the file.",
 950               default_cc_file, default_cc_file);
 951     }
 952   }
 953   if (lists[PrintCommand] != NULL) {
 954     if (PrintAssembly) {
 955       warning("CompileCommand and/or %s file contains 'print' commands, but PrintAssembly is also enabled", default_cc_file);
 956     } else if (FLAG_IS_DEFAULT(DebugNonSafepoints)) {
 957       warning("printing of assembly code is enabled; turning on DebugNonSafepoints to gain additional output");
 958       DebugNonSafepoints = true;
 959     }
 960   }
 961 }
 962 
 963 
 964 void CompilerOracle::parse_compile_only(char * line) {
 965   int i;
 966   char name[1024];
 967   const char* className = NULL;
 968   const char* methodName = NULL;
 969 
 970   bool have_colon = (strstr(line, "::") != NULL);
 971   char method_sep = have_colon ? ':' : '.';
 972 
 973   if (Verbose) {
 974     tty->print_cr("%s", line);
 975   }
 976 
 977   ResourceMark rm;
 978   while (*line != '\0') {
 979     MethodMatcher::Mode c_match = MethodMatcher::Exact;
 980     MethodMatcher::Mode m_match = MethodMatcher::Exact;
 981 
 982     for (i = 0;
 983          i < 1024 && *line != '\0' && *line != method_sep && *line != ',' && !isspace(*line);
 984          line++, i++) {
 985       name[i] = *line;
 986       if (name[i] == '.')  name[i] = '/';  // package prefix uses '/'
 987     }
 988 
 989     if (i > 0) {
 990       char* newName = NEW_RESOURCE_ARRAY( char, i + 1);
 991       if (newName == NULL)
 992         return;
 993       strncpy(newName, name, i);
 994       newName[i] = '\0';
 995 
 996       if (className == NULL) {
 997         className = newName;
 998         c_match = MethodMatcher::Prefix;
 999       } else {
1000         methodName = newName;
1001       }
1002     }
1003 
1004     if (*line == method_sep) {
1005       if (className == NULL) {
1006         className = "";
1007         c_match = MethodMatcher::Any;
1008       } else {
1009         // foo/bar.blah is an exact match on foo/bar, bar.blah is a suffix match on bar
1010         if (strchr(className, '/') != NULL) {
1011           c_match = MethodMatcher::Exact;
1012         } else {
1013           c_match = MethodMatcher::Suffix;
1014         }
1015       }
1016     } else {
1017       // got foo or foo/bar
1018       if (className == NULL) {
1019         ShouldNotReachHere();
1020       } else {
1021         // got foo or foo/bar
1022         if (strchr(className, '/') != NULL) {
1023           c_match = MethodMatcher::Prefix;
1024         } else if (className[0] == '\0') {
1025           c_match = MethodMatcher::Any;
1026         } else {
1027           c_match = MethodMatcher::Substring;
1028         }
1029       }
1030     }
1031 
1032     // each directive is terminated by , or NUL or . followed by NUL
1033     if (*line == ',' || *line == '\0' || (line[0] == '.' && line[1] == '\0')) {
1034       if (methodName == NULL) {
1035         methodName = "";
1036         if (*line != method_sep) {
1037           m_match = MethodMatcher::Any;
1038         }
1039       }
1040 
1041       EXCEPTION_MARK;
1042       Symbol* c_name = SymbolTable::new_symbol(className, CHECK);
1043       Symbol* m_name = SymbolTable::new_symbol(methodName, CHECK);
1044       Symbol* signature = NULL;
1045 
1046       add_predicate(CompileOnlyCommand, c_name, c_match, m_name, m_match, signature);
1047       if (PrintVMOptions) {
1048         tty->print("CompileOnly: compileonly ");
1049         lists[CompileOnlyCommand]->print();
1050       }
1051 
1052       className = NULL;
1053       methodName = NULL;
1054     }
1055 
1056     line = *line == '\0' ? line : line + 1;
1057   }
1058 }