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()->equals(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->equals(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. Many commands require a pattern that defines");
 473   tty->print_cr("the set of methods the command shall be applied to. The CompileCommand");
 474   tty->print_cr("option provides the following commands:");
 475   tty->cr();
 476   tty->print_cr("  break,<pattern>       - debug breakpoint in compiler and in generated code");
 477   tty->print_cr("  print,<pattern>       - print assembly");
 478   tty->print_cr("  exclude,<pattern>     - don't compile or inline");
 479   tty->print_cr("  inline,<pattern>      - always inline");
 480   tty->print_cr("  dontinline,<pattern>  - don't inline");
 481   tty->print_cr("  compileonly,<pattern> - compile only");
 482   tty->print_cr("  log,<pattern>         - log compilation");
 483   tty->print_cr("  option,<pattern>,<option type>,<option name>,<value>");
 484   tty->print_cr("                        - set value of custom option");
 485   tty->print_cr("  option,<pattern>,<bool option name>");
 486   tty->print_cr("                        - shorthand for setting boolean flag");
 487   tty->print_cr("  quiet                 - silence the compile command output");
 488   tty->print_cr("  help                  - print this text");
 489   tty->cr();
 490   tty->print_cr("The preferred format for the method matching pattern is:");
 491   tty->print_cr("  package/Class.method()");
 492   tty->cr();
 493   tty->print_cr("For backward compatibility this form is also allowed:");
 494   tty->print_cr("  package.Class::method()");
 495   tty->cr();
 496   tty->print_cr("The signature can be separated by an optional whitespace or comma:");
 497   tty->print_cr("  package/Class.method ()");
 498   tty->cr();
 499   tty->print_cr("The class and method identifier can be used together with leading or");
 500   tty->print_cr("trailing *'s for a small amount of wildcarding:");
 501   tty->print_cr("  *ackage/Clas*.*etho*()");
 502   tty->cr();
 503   tty->print_cr("It is possible to use more than one CompileCommand on the command line:");
 504   tty->print_cr("  -XX:CompileCommand=exclude,java/*.* -XX:CompileCommand=log,java*.*");
 505   tty->cr();
 506   tty->print_cr("The CompileCommands can be loaded from a file with the flag");
 507   tty->print_cr("-XX:CompileCommandFile=<file> or be added to the file '.hotspot_compiler'");
 508   tty->print_cr("Use the same format in the file as the argument to the CompileCommand flag.");
 509   tty->print_cr("Add one command on each line.");
 510   tty->print_cr("  exclude java/*.*");
 511   tty->print_cr("  option java/*.* ReplayInline");
 512   tty->cr();
 513   tty->print_cr("The following commands have conflicting behavior: 'exclude', 'inline', 'dontinline',");
 514   tty->print_cr("and 'compileonly'. There is no priority of commands. Applying (a subset of) these");
 515   tty->print_cr("commands to the same method results in undefined behavior.");
 516   tty->cr();
 517 };
 518 
 519 // The JVM specification defines the allowed characters.
 520 // Tokens that are disallowed by the JVM specification can have
 521 // a meaning to the parser so we need to include them here.
 522 // The parser does not enforce all rules of the JVMS - a successful parse
 523 // does not mean that it is an allowed name. Illegal names will
 524 // be ignored since they never can match a class or method.
 525 //
 526 // '\0' and 0xf0-0xff are disallowed in constant string values
 527 // 0x20 ' ', 0x09 '\t' and, 0x2c ',' are used in the matching
 528 // 0x5b '[' and 0x5d ']' can not be used because of the matcher
 529 // 0x28 '(' and 0x29 ')' are used for the signature
 530 // 0x2e '.' is always replaced before the matching
 531 // 0x2f '/' is only used in the class name as package separator
 532 
 533 #define RANGEBASE "\x1\x2\x3\x4\x5\x6\x7\x8\xa\xb\xc\xd\xe\xf" \
 534     "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
 535     "\x21\x22\x23\x24\x25\x26\x27\x2a\x2b\x2c\x2d" \
 536     "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" \
 537     "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" \
 538     "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5c\x5e\x5f" \
 539     "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" \
 540     "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f" \
 541     "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" \
 542     "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" \
 543     "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" \
 544     "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" \
 545     "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" \
 546     "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" \
 547     "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
 548 
 549 #define RANGE0 "[*" RANGEBASE "]"
 550 #define RANGESLASH "[*" RANGEBASE "/]"
 551 
 552 static MethodMatcher::Mode check_mode(char name[], const char*& error_msg) {
 553   int match = MethodMatcher::Exact;
 554   while (name[0] == '*') {
 555     match |= MethodMatcher::Suffix;
 556     // Copy remaining string plus NUL to the beginning
 557     memmove(name, name + 1, strlen(name + 1) + 1);
 558   }
 559 
 560   if (strcmp(name, "*") == 0) return MethodMatcher::Any;
 561 
 562   size_t len = strlen(name);
 563   while (len > 0 && name[len - 1] == '*') {
 564     match |= MethodMatcher::Prefix;
 565     name[--len] = '\0';
 566   }
 567 
 568   if (strstr(name, "*") != NULL) {
 569     error_msg = "  Embedded * not allowed";
 570     return MethodMatcher::Unknown;
 571   }
 572   return (MethodMatcher::Mode)match;
 573 }
 574 
 575 static bool scan_line(const char * line,
 576                       char class_name[],  MethodMatcher::Mode* c_mode,
 577                       char method_name[], MethodMatcher::Mode* m_mode,
 578                       int* bytes_read, const char*& error_msg) {
 579   *bytes_read = 0;
 580   error_msg = NULL;
 581   if (2 == sscanf(line, "%*[ \t]%255" RANGESLASH "%*[ ]" "%255"  RANGE0 "%n", class_name, method_name, bytes_read)) {
 582     *c_mode = check_mode(class_name, error_msg);
 583     *m_mode = check_mode(method_name, error_msg);
 584     return *c_mode != MethodMatcher::Unknown && *m_mode != MethodMatcher::Unknown;
 585   }
 586   return false;
 587 }
 588 
 589 
 590 
 591 // Scan next flag and value in line, return MethodMatcher object on success, NULL on failure.
 592 // On failure, error_msg contains description for the first error.
 593 // For future extensions: set error_msg on first error.
 594 static MethodMatcher* scan_flag_and_value(const char* type, const char* line, int& total_bytes_read,
 595                                           Symbol* c_name, MethodMatcher::Mode c_match,
 596                                           Symbol* m_name, MethodMatcher::Mode m_match,
 597                                           Symbol* signature,
 598                                           char* errorbuf, const int buf_size) {
 599   total_bytes_read = 0;
 600   int bytes_read = 0;
 601   char flag[256];
 602 
 603   // Read flag name.
 604   if (sscanf(line, "%*[ \t]%255[a-zA-Z0-9]%n", flag, &bytes_read) == 1) {
 605     line += bytes_read;
 606     total_bytes_read += bytes_read;
 607 
 608     // Read value.
 609     if (strcmp(type, "intx") == 0) {
 610       intx value;
 611       if (sscanf(line, "%*[ \t]" INTX_FORMAT "%n", &value, &bytes_read) == 1) {
 612         total_bytes_read += bytes_read;
 613         return add_option_string(c_name, c_match, m_name, m_match, signature, flag, value);
 614       } else {
 615         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s ", flag, type);
 616       }
 617     } else if (strcmp(type, "uintx") == 0) {
 618       uintx value;
 619       if (sscanf(line, "%*[ \t]" UINTX_FORMAT "%n", &value, &bytes_read) == 1) {
 620         total_bytes_read += bytes_read;
 621         return add_option_string(c_name, c_match, m_name, m_match, signature, flag, value);
 622       } else {
 623         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 624       }
 625     } else if (strcmp(type, "ccstr") == 0) {
 626       ResourceMark rm;
 627       char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);
 628       if (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", value, &bytes_read) == 1) {
 629         total_bytes_read += bytes_read;
 630         return add_option_string(c_name, c_match, m_name, m_match, signature, flag, (ccstr)value);
 631       } else {
 632         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 633       }
 634     } else if (strcmp(type, "ccstrlist") == 0) {
 635       // Accumulates several strings into one. The internal type is ccstr.
 636       ResourceMark rm;
 637       char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);
 638       char* next_value = value;
 639       if (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", next_value, &bytes_read) == 1) {
 640         total_bytes_read += bytes_read;
 641         line += bytes_read;
 642         next_value += bytes_read;
 643         char* end_value = next_value-1;
 644         while (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", next_value, &bytes_read) == 1) {
 645           total_bytes_read += bytes_read;
 646           line += bytes_read;
 647           *end_value = ' '; // override '\0'
 648           next_value += bytes_read;
 649           end_value = next_value-1;
 650         }
 651         return add_option_string(c_name, c_match, m_name, m_match, signature, flag, (ccstr)value);
 652       } else {
 653         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 654       }
 655     } else if (strcmp(type, "bool") == 0) {
 656       char value[256];
 657       if (sscanf(line, "%*[ \t]%255[a-zA-Z]%n", value, &bytes_read) == 1) {
 658         if (strcmp(value, "true") == 0) {
 659           total_bytes_read += bytes_read;
 660           return add_option_string(c_name, c_match, m_name, m_match, signature, flag, true);
 661         } else if (strcmp(value, "false") == 0) {
 662           total_bytes_read += bytes_read;
 663           return add_option_string(c_name, c_match, m_name, m_match, signature, flag, false);
 664         } else {
 665           jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 666         }
 667       } else {
 668         jio_snprintf(errorbuf, sizeof(errorbuf), "  Value cannot be read for flag %s of type %s", flag, type);
 669       }
 670     } else if (strcmp(type, "double") == 0) {
 671       char buffer[2][256];
 672       // Decimal separator '.' has been replaced with ' ' or '/' earlier,
 673       // so read integer and fraction part of double value separately.
 674       if (sscanf(line, "%*[ \t]%255[0-9]%*[ /\t]%255[0-9]%n", buffer[0], buffer[1], &bytes_read) == 2) {
 675         char value[512] = "";
 676         strncat(value, buffer[0], 255);
 677         strcat(value, ".");
 678         strncat(value, buffer[1], 255);
 679         total_bytes_read += bytes_read;
 680         return add_option_string(c_name, c_match, m_name, m_match, signature, flag, atof(value));
 681       } else {
 682         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 683       }
 684     } else {
 685       jio_snprintf(errorbuf, sizeof(errorbuf), "  Type %s not supported ", type);
 686     }
 687   } else {
 688     jio_snprintf(errorbuf, sizeof(errorbuf), "  Flag name for type %s should be alphanumeric ", type);
 689   }
 690   return NULL;
 691 }
 692 
 693 int skip_whitespace(char* line) {
 694   // Skip any leading spaces
 695   int whitespace_read = 0;
 696   sscanf(line, "%*[ \t]%n", &whitespace_read);
 697   return whitespace_read;
 698 }
 699 
 700 void CompilerOracle::parse_from_line(char* line) {
 701   if (line[0] == '\0') return;
 702   if (line[0] == '#')  return;
 703 
 704   bool have_colon = (strstr(line, "::") != NULL);
 705   for (char* lp = line; *lp != '\0'; lp++) {
 706     // Allow '.' to separate the class name from the method name.
 707     // This is the preferred spelling of methods:
 708     //      exclude java/lang/String.indexOf(I)I
 709     // Allow ',' for spaces (eases command line quoting).
 710     //      exclude,java/lang/String.indexOf
 711     // For backward compatibility, allow space as separator also.
 712     //      exclude java/lang/String indexOf
 713     //      exclude,java/lang/String,indexOf
 714     // For easy cut-and-paste of method names, allow VM output format
 715     // as produced by Method::print_short_name:
 716     //      exclude java.lang.String::indexOf
 717     // For simple implementation convenience here, convert them all to space.
 718     if (have_colon) {
 719       if (*lp == '.')  *lp = '/';   // dots build the package prefix
 720       if (*lp == ':')  *lp = ' ';
 721     }
 722     if (*lp == ',' || *lp == '.')  *lp = ' ';
 723   }
 724 
 725   char* original_line = line;
 726   int bytes_read;
 727   OracleCommand command = parse_command_name(line, &bytes_read);
 728   line += bytes_read;
 729   ResourceMark rm;
 730 
 731   if (command == UnknownCommand) {
 732     ttyLocker ttyl;
 733     tty->print_cr("CompileCommand: unrecognized command");
 734     tty->print_cr("  \"%s\"", original_line);
 735     CompilerOracle::print_tip();
 736     return;
 737   }
 738 
 739   if (command == QuietCommand) {
 740     _quiet = true;
 741     return;
 742   }
 743 
 744   if (command == HelpCommand) {
 745     usage();
 746     return;
 747   }
 748 
 749   MethodMatcher::Mode c_match = MethodMatcher::Exact;
 750   MethodMatcher::Mode m_match = MethodMatcher::Exact;
 751   char class_name[256];
 752   char method_name[256];
 753   char sig[1024];
 754   char errorbuf[1024];
 755   const char* error_msg = NULL; // description of first error that appears
 756   MethodMatcher* match = NULL;
 757 
 758   if (scan_line(line, class_name, &c_match, method_name, &m_match, &bytes_read, error_msg)) {
 759     EXCEPTION_MARK;
 760     Symbol* c_name = SymbolTable::new_symbol(class_name, CHECK);
 761     Symbol* m_name = SymbolTable::new_symbol(method_name, CHECK);
 762     Symbol* signature = NULL;
 763 
 764     line += bytes_read;
 765 
 766     // there might be a signature following the method.
 767     // signatures always begin with ( so match that by hand
 768     line += skip_whitespace(line);
 769     if (1 == sscanf(line, "(%254[[);/" RANGEBASE "]%n", sig + 1, &bytes_read)) {
 770       sig[0] = '(';
 771       line += bytes_read;
 772       signature = SymbolTable::new_symbol(sig, CHECK);
 773     }
 774 
 775     if (command == OptionCommand) {
 776       // Look for trailing options.
 777       //
 778       // Two types of trailing options are
 779       // supported:
 780       //
 781       // (1) CompileCommand=option,Klass::method,flag
 782       // (2) CompileCommand=option,Klass::method,type,flag,value
 783       //
 784       // Type (1) is used to enable a boolean flag for a method.
 785       //
 786       // Type (2) is used to support options with a value. Values can have the
 787       // the following types: intx, uintx, bool, ccstr, ccstrlist, and double.
 788       //
 789       // For future extensions: extend scan_flag_and_value()
 790       char option[256]; // stores flag for Type (1) and type of Type (2)
 791 
 792       line += skip_whitespace(line);
 793       while (sscanf(line, "%255[a-zA-Z0-9]%n", option, &bytes_read) == 1) {
 794         if (match != NULL && !_quiet) {
 795           // Print out the last match added
 796           ttyLocker ttyl;
 797           tty->print("CompileCommand: %s ", command_names[command]);
 798           match->print();
 799         }
 800         line += bytes_read;
 801 
 802         if (strcmp(option, "intx") == 0
 803             || strcmp(option, "uintx") == 0
 804             || strcmp(option, "bool") == 0
 805             || strcmp(option, "ccstr") == 0
 806             || strcmp(option, "ccstrlist") == 0
 807             || strcmp(option, "double") == 0
 808             ) {
 809 
 810           // Type (2) option: parse flag name and value.
 811           match = scan_flag_and_value(option, line, bytes_read,
 812                                       c_name, c_match, m_name, m_match, signature,
 813                                       errorbuf, sizeof(errorbuf));
 814           if (match == NULL) {
 815             error_msg = errorbuf;
 816             break;
 817           }
 818           line += bytes_read;
 819         } else {
 820           // Type (1) option
 821           match = add_option_string(c_name, c_match, m_name, m_match, signature, option, true);
 822         }
 823         line += skip_whitespace(line);
 824       } // while(
 825     } else {
 826       match = add_predicate(command, c_name, c_match, m_name, m_match, signature);
 827     }
 828   }
 829 
 830   ttyLocker ttyl;
 831   if (error_msg != NULL) {
 832     // an error has happened
 833     tty->print_cr("CompileCommand: An error occured during parsing");
 834     tty->print_cr("  \"%s\"", original_line);
 835     if (error_msg != NULL) {
 836       tty->print_cr("%s", error_msg);
 837     }
 838     CompilerOracle::print_tip();
 839 
 840   } else {
 841     // check for remaining characters
 842     bytes_read = 0;
 843     sscanf(line, "%*[ \t]%n", &bytes_read);
 844     if (line[bytes_read] != '\0') {
 845       tty->print_cr("CompileCommand: Bad pattern");
 846       tty->print_cr("  \"%s\"", original_line);
 847       tty->print_cr("  Unrecognized text %s after command ", line);
 848       CompilerOracle::print_tip();
 849     } else if (match != NULL && !_quiet) {
 850       tty->print("CompileCommand: %s ", command_names[command]);
 851       match->print();
 852     }
 853   }
 854 }
 855 
 856 void CompilerOracle::print_tip() {
 857   tty->cr();
 858   tty->print_cr("Usage: '-XX:CompileCommand=command,\"package/Class.method()\"'");
 859   tty->print_cr("Use:   '-XX:CompileCommand=help' for more information.");
 860   tty->cr();
 861 }
 862 
 863 static const char* default_cc_file = ".hotspot_compiler";
 864 
 865 static const char* cc_file() {
 866 #ifdef ASSERT
 867   if (CompileCommandFile == NULL)
 868     return default_cc_file;
 869 #endif
 870   return CompileCommandFile;
 871 }
 872 
 873 bool CompilerOracle::has_command_file() {
 874   return cc_file() != NULL;
 875 }
 876 
 877 bool CompilerOracle::_quiet = false;
 878 
 879 void CompilerOracle::parse_from_file() {
 880   assert(has_command_file(), "command file must be specified");
 881   FILE* stream = fopen(cc_file(), "rt");
 882   if (stream == NULL) return;
 883 
 884   char token[1024];
 885   int  pos = 0;
 886   int  c = getc(stream);
 887   while(c != EOF && pos < (int)(sizeof(token)-1)) {
 888     if (c == '\n') {
 889       token[pos++] = '\0';
 890       parse_from_line(token);
 891       pos = 0;
 892     } else {
 893       token[pos++] = c;
 894     }
 895     c = getc(stream);
 896   }
 897   token[pos++] = '\0';
 898   parse_from_line(token);
 899 
 900   fclose(stream);
 901 }
 902 
 903 void CompilerOracle::parse_from_string(const char* str, void (*parse_line)(char*)) {
 904   char token[1024];
 905   int  pos = 0;
 906   const char* sp = str;
 907   int  c = *sp++;
 908   while (c != '\0' && pos < (int)(sizeof(token)-1)) {
 909     if (c == '\n') {
 910       token[pos++] = '\0';
 911       parse_line(token);
 912       pos = 0;
 913     } else {
 914       token[pos++] = c;
 915     }
 916     c = *sp++;
 917   }
 918   token[pos++] = '\0';
 919   parse_line(token);
 920 }
 921 
 922 void CompilerOracle::append_comment_to_file(const char* message) {
 923   assert(has_command_file(), "command file must be specified");
 924   fileStream stream(fopen(cc_file(), "at"));
 925   stream.print("# ");
 926   for (int index = 0; message[index] != '\0'; index++) {
 927     stream.put(message[index]);
 928     if (message[index] == '\n') stream.print("# ");
 929   }
 930   stream.cr();
 931 }
 932 
 933 void CompilerOracle::append_exclude_to_file(methodHandle method) {
 934   assert(has_command_file(), "command file must be specified");
 935   fileStream stream(fopen(cc_file(), "at"));
 936   stream.print("exclude ");
 937   method->method_holder()->name()->print_symbol_on(&stream);
 938   stream.print(".");
 939   method->name()->print_symbol_on(&stream);
 940   method->signature()->print_symbol_on(&stream);
 941   stream.cr();
 942   stream.cr();
 943 }
 944 
 945 
 946 void compilerOracle_init() {
 947   CompilerOracle::parse_from_string(CompileCommand, CompilerOracle::parse_from_line);
 948   CompilerOracle::parse_from_string(CompileOnly, CompilerOracle::parse_compile_only);
 949   if (CompilerOracle::has_command_file()) {
 950     CompilerOracle::parse_from_file();
 951   } else {
 952     struct stat buf;
 953     if (os::stat(default_cc_file, &buf) == 0) {
 954       warning("%s file is present but has been ignored.  "
 955               "Run with -XX:CompileCommandFile=%s to load the file.",
 956               default_cc_file, default_cc_file);
 957     }
 958   }
 959   if (lists[PrintCommand] != NULL) {
 960     if (PrintAssembly) {
 961       warning("CompileCommand and/or %s file contains 'print' commands, but PrintAssembly is also enabled", default_cc_file);
 962     } else if (FLAG_IS_DEFAULT(DebugNonSafepoints)) {
 963       warning("printing of assembly code is enabled; turning on DebugNonSafepoints to gain additional output");
 964       DebugNonSafepoints = true;
 965     }
 966   }
 967 }
 968 
 969 
 970 void CompilerOracle::parse_compile_only(char * line) {
 971   int i;
 972   char name[1024];
 973   const char* className = NULL;
 974   const char* methodName = NULL;
 975 
 976   bool have_colon = (strstr(line, "::") != NULL);
 977   char method_sep = have_colon ? ':' : '.';
 978 
 979   if (Verbose) {
 980     tty->print_cr("%s", line);
 981   }
 982 
 983   ResourceMark rm;
 984   while (*line != '\0') {
 985     MethodMatcher::Mode c_match = MethodMatcher::Exact;
 986     MethodMatcher::Mode m_match = MethodMatcher::Exact;
 987 
 988     for (i = 0;
 989          i < 1024 && *line != '\0' && *line != method_sep && *line != ',' && !isspace(*line);
 990          line++, i++) {
 991       name[i] = *line;
 992       if (name[i] == '.')  name[i] = '/';  // package prefix uses '/'
 993     }
 994 
 995     if (i > 0) {
 996       char* newName = NEW_RESOURCE_ARRAY( char, i + 1);
 997       if (newName == NULL)
 998         return;
 999       strncpy(newName, name, i);
1000       newName[i] = '\0';
1001 
1002       if (className == NULL) {
1003         className = newName;
1004         c_match = MethodMatcher::Prefix;
1005       } else {
1006         methodName = newName;
1007       }
1008     }
1009 
1010     if (*line == method_sep) {
1011       if (className == NULL) {
1012         className = "";
1013         c_match = MethodMatcher::Any;
1014       } else {
1015         // foo/bar.blah is an exact match on foo/bar, bar.blah is a suffix match on bar
1016         if (strchr(className, '/') != NULL) {
1017           c_match = MethodMatcher::Exact;
1018         } else {
1019           c_match = MethodMatcher::Suffix;
1020         }
1021       }
1022     } else {
1023       // got foo or foo/bar
1024       if (className == NULL) {
1025         ShouldNotReachHere();
1026       } else {
1027         // got foo or foo/bar
1028         if (strchr(className, '/') != NULL) {
1029           c_match = MethodMatcher::Prefix;
1030         } else if (className[0] == '\0') {
1031           c_match = MethodMatcher::Any;
1032         } else {
1033           c_match = MethodMatcher::Substring;
1034         }
1035       }
1036     }
1037 
1038     // each directive is terminated by , or NUL or . followed by NUL
1039     if (*line == ',' || *line == '\0' || (line[0] == '.' && line[1] == '\0')) {
1040       if (methodName == NULL) {
1041         methodName = "";
1042         if (*line != method_sep) {
1043           m_match = MethodMatcher::Any;
1044         }
1045       }
1046 
1047       EXCEPTION_MARK;
1048       Symbol* c_name = SymbolTable::new_symbol(className, CHECK);
1049       Symbol* m_name = SymbolTable::new_symbol(methodName, CHECK);
1050       Symbol* signature = NULL;
1051 
1052       add_predicate(CompileOnlyCommand, c_name, c_match, m_name, m_match, signature);
1053       if (PrintVMOptions) {
1054         tty->print("CompileOnly: compileonly ");
1055         lists[CompileOnlyCommand]->print();
1056       }
1057 
1058       className = NULL;
1059       methodName = NULL;
1060     }
1061 
1062     line = *line == '\0' ? line : line + 1;
1063   }
1064 }