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