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