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