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 
 496 // The characters allowed in a class or method name.  All characters > 0x7f
 497 // are allowed in order to handle obfuscated class files (e.g. Volano)
 498 #define RANGEBASE "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$_<>" \
 499         "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" \
 500         "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" \
 501         "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" \
 502         "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" \
 503         "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" \
 504         "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" \
 505         "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef" \
 506         "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
 507 
 508 #define RANGE0 "[*" RANGEBASE "]"
 509 #define RANGESLASH "[*" RANGEBASE "/]"
 510 
 511 static MethodMatcher::Mode check_mode(char name[], const char*& error_msg) {
 512   int match = MethodMatcher::Exact;
 513   while (name[0] == '*') {
 514     match |= MethodMatcher::Suffix;
 515     strcpy(name, name + 1);
 516   }
 517 
 518   if (strcmp(name, "*") == 0) return MethodMatcher::Any;
 519 
 520   size_t len = strlen(name);
 521   while (len > 0 && name[len - 1] == '*') {
 522     match |= MethodMatcher::Prefix;
 523     name[--len] = '\0';
 524   }
 525 
 526   if (strstr(name, "*") != NULL) {
 527     error_msg = "  Embedded * not allowed";
 528     return MethodMatcher::Unknown;
 529   }
 530   return (MethodMatcher::Mode)match;
 531 }
 532 
 533 static bool scan_line(const char * line,
 534                       char class_name[],  MethodMatcher::Mode* c_mode,
 535                       char method_name[], MethodMatcher::Mode* m_mode,
 536                       int* bytes_read, const char*& error_msg) {
 537   *bytes_read = 0;
 538   error_msg = NULL;
 539   if (2 == sscanf(line, "%*[ \t]%255" RANGESLASH "%*[ ]" "%255"  RANGE0 "%n", class_name, method_name, bytes_read)) {
 540     *c_mode = check_mode(class_name, error_msg);
 541     *m_mode = check_mode(method_name, error_msg);
 542     return *c_mode != MethodMatcher::Unknown && *m_mode != MethodMatcher::Unknown;
 543   }
 544   return false;
 545 }
 546 
 547 
 548 
 549 // Scan next flag and value in line, return MethodMatcher object on success, NULL on failure.
 550 // On failure, error_msg contains description for the first error.
 551 // For future extensions: set error_msg on first error.
 552 static MethodMatcher* scan_flag_and_value(const char* type, const char* line, int& total_bytes_read,
 553                                           Symbol* c_name, MethodMatcher::Mode c_match,
 554                                           Symbol* m_name, MethodMatcher::Mode m_match,
 555                                           Symbol* signature,
 556                                           char* errorbuf, const int buf_size) {
 557   total_bytes_read = 0;
 558   int bytes_read = 0;
 559   char flag[256];
 560 
 561   // Read flag name.
 562   if (sscanf(line, "%*[ \t]%255[a-zA-Z0-9]%n", flag, &bytes_read) == 1) {
 563     line += bytes_read;
 564     total_bytes_read += bytes_read;
 565 
 566     // Read value.
 567     if (strcmp(type, "intx") == 0) {
 568       intx value;
 569       if (sscanf(line, "%*[ \t]" INTX_FORMAT "%n", &value, &bytes_read) == 1) {
 570         total_bytes_read += bytes_read;
 571         return add_option_string(c_name, c_match, m_name, m_match, signature, flag, value);
 572       } else {
 573         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s ", flag, type);
 574       }
 575     } else if (strcmp(type, "uintx") == 0) {
 576       uintx value;
 577       if (sscanf(line, "%*[ \t]" UINTX_FORMAT "%n", &value, &bytes_read) == 1) {
 578         total_bytes_read += bytes_read;
 579         return add_option_string(c_name, c_match, m_name, m_match, signature, flag, value);
 580       } else {
 581         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 582       }
 583     } else if (strcmp(type, "ccstr") == 0) {
 584       ResourceMark rm;
 585       char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);
 586       if (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", value, &bytes_read) == 1) {
 587         total_bytes_read += bytes_read;
 588         return add_option_string(c_name, c_match, m_name, m_match, signature, flag, (ccstr)value);
 589       } else {
 590         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 591       }
 592     } else if (strcmp(type, "ccstrlist") == 0) {
 593       // Accumulates several strings into one. The internal type is ccstr.
 594       ResourceMark rm;
 595       char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);
 596       char* next_value = value;
 597       if (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", next_value, &bytes_read) == 1) {
 598         total_bytes_read += bytes_read;
 599         line += bytes_read;
 600         next_value += bytes_read;
 601         char* end_value = next_value-1;
 602         while (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", next_value, &bytes_read) == 1) {
 603           total_bytes_read += bytes_read;
 604           line += bytes_read;
 605           *end_value = ' '; // override '\0'
 606           next_value += bytes_read;
 607           end_value = next_value-1;
 608         }
 609         return add_option_string(c_name, c_match, m_name, m_match, signature, flag, (ccstr)value);
 610       } else {
 611         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 612       }
 613     } else if (strcmp(type, "bool") == 0) {
 614       char value[256];
 615       if (sscanf(line, "%*[ \t]%255[a-zA-Z]%n", value, &bytes_read) == 1) {
 616         if (strcmp(value, "true") == 0) {
 617           total_bytes_read += bytes_read;
 618           return add_option_string(c_name, c_match, m_name, m_match, signature, flag, true);
 619         } else if (strcmp(value, "false") == 0) {
 620           total_bytes_read += bytes_read;
 621           return add_option_string(c_name, c_match, m_name, m_match, signature, flag, false);
 622         } else {
 623           jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 624         }
 625       } else {
 626         jio_snprintf(errorbuf, sizeof(errorbuf), "  Value cannot be read for flag %s of type %s", flag, type);
 627       }
 628     } else if (strcmp(type, "double") == 0) {
 629       char buffer[2][256];
 630       // Decimal separator '.' has been replaced with ' ' or '/' earlier,
 631       // so read integer and fraction part of double value separately.
 632       if (sscanf(line, "%*[ \t]%255[0-9]%*[ /\t]%255[0-9]%n", buffer[0], buffer[1], &bytes_read) == 2) {
 633         char value[512] = "";
 634         strncat(value, buffer[0], 255);
 635         strcat(value, ".");
 636         strncat(value, buffer[1], 255);
 637         total_bytes_read += bytes_read;
 638         return add_option_string(c_name, c_match, m_name, m_match, signature, flag, atof(value));
 639       } else {
 640         jio_snprintf(errorbuf, buf_size, "  Value cannot be read for flag %s of type %s", flag, type);
 641       }
 642     } else {
 643       jio_snprintf(errorbuf, sizeof(errorbuf), "  Type %s not supported ", type);
 644     }
 645   } else {
 646     jio_snprintf(errorbuf, sizeof(errorbuf), "  Flag name for type %s should be alphanumeric ", type);
 647   }
 648   return NULL;
 649 }
 650 
 651 void CompilerOracle::parse_from_line(char* line) {
 652   if (line[0] == '\0') return;
 653   if (line[0] == '#')  return;
 654 
 655   bool have_colon = (strstr(line, "::") != NULL);
 656   for (char* lp = line; *lp != '\0'; lp++) {
 657     // Allow '.' to separate the class name from the method name.
 658     // This is the preferred spelling of methods:
 659     //      exclude java/lang/String.indexOf(I)I
 660     // Allow ',' for spaces (eases command line quoting).
 661     //      exclude,java/lang/String.indexOf
 662     // For backward compatibility, allow space as separator also.
 663     //      exclude java/lang/String indexOf
 664     //      exclude,java/lang/String,indexOf
 665     // For easy cut-and-paste of method names, allow VM output format
 666     // as produced by Method::print_short_name:
 667     //      exclude java.lang.String::indexOf
 668     // For simple implementation convenience here, convert them all to space.
 669     if (have_colon) {
 670       if (*lp == '.')  *lp = '/';   // dots build the package prefix
 671       if (*lp == ':')  *lp = ' ';
 672     }
 673     if (*lp == ',' || *lp == '.')  *lp = ' ';
 674   }
 675 
 676   char* original_line = line;
 677   int bytes_read;
 678   OracleCommand command = parse_command_name(line, &bytes_read);
 679   line += bytes_read;
 680   ResourceMark rm;
 681 
 682   if (command == UnknownCommand) {
 683     ttyLocker ttyl;
 684     tty->print_cr("CompilerOracle: unrecognized line");
 685     tty->print_cr("  \"%s\"", original_line);
 686     return;
 687   }
 688 
 689   if (command == QuietCommand) {
 690     _quiet = true;
 691     return;
 692   }
 693 
 694   if (command == HelpCommand) {
 695     usage();
 696     return;
 697   }
 698 
 699   MethodMatcher::Mode c_match = MethodMatcher::Exact;
 700   MethodMatcher::Mode m_match = MethodMatcher::Exact;
 701   char class_name[256];
 702   char method_name[256];
 703   char sig[1024];
 704   char errorbuf[1024];
 705   const char* error_msg = NULL; // description of first error that appears
 706   MethodMatcher* match = NULL;
 707 
 708   if (scan_line(line, class_name, &c_match, method_name, &m_match, &bytes_read, error_msg)) {
 709     EXCEPTION_MARK;
 710     Symbol* c_name = SymbolTable::new_symbol(class_name, CHECK);
 711     Symbol* m_name = SymbolTable::new_symbol(method_name, CHECK);
 712     Symbol* signature = NULL;
 713 
 714     line += bytes_read;
 715     // there might be a signature following the method.
 716     // signatures always begin with ( so match that by hand
 717     if (1 == sscanf(line, "%*[ \t](%254[[);/" RANGEBASE "]%n", sig + 1, &bytes_read)) {
 718       sig[0] = '(';
 719       line += bytes_read;
 720       signature = SymbolTable::new_symbol(sig, CHECK);
 721     }
 722 
 723     if (command == OptionCommand) {
 724       // Look for trailing options.
 725       //
 726       // Two types of trailing options are
 727       // supported:
 728       //
 729       // (1) CompileCommand=option,Klass::method,flag
 730       // (2) CompileCommand=option,Klass::method,type,flag,value
 731       //
 732       // Type (1) is used to enable a boolean flag for a method.
 733       //
 734       // Type (2) is used to support options with a value. Values can have the
 735       // the following types: intx, uintx, bool, ccstr, ccstrlist, and double.
 736       //
 737       // For future extensions: extend scan_flag_and_value()
 738       char option[256]; // stores flag for Type (1) and type of Type (2)
 739       while (sscanf(line, "%*[ \t]%255[a-zA-Z0-9]%n", option, &bytes_read) == 1) {
 740         if (match != NULL && !_quiet) {
 741           // Print out the last match added
 742           ttyLocker ttyl;
 743           tty->print("CompilerOracle: %s ", command_names[command]);
 744           match->print();
 745         }
 746         line += bytes_read;
 747 
 748         if (strcmp(option, "intx") == 0
 749             || strcmp(option, "uintx") == 0
 750             || strcmp(option, "bool") == 0
 751             || strcmp(option, "ccstr") == 0
 752             || strcmp(option, "ccstrlist") == 0
 753             || strcmp(option, "double") == 0
 754             ) {
 755 
 756           // Type (2) option: parse flag name and value.
 757           match = scan_flag_and_value(option, line, bytes_read,
 758                                       c_name, c_match, m_name, m_match, signature,
 759                                       errorbuf, sizeof(errorbuf));
 760           if (match == NULL) {
 761             error_msg = errorbuf;
 762             break;
 763           }
 764           line += bytes_read;
 765         } else {
 766           // Type (1) option
 767           match = add_option_string(c_name, c_match, m_name, m_match, signature, option, true);
 768         }
 769       } // while(
 770     } else {
 771       match = add_predicate(command, c_name, c_match, m_name, m_match, signature);
 772     }
 773   }
 774 
 775   ttyLocker ttyl;
 776   if (error_msg != NULL) {
 777     // an error has happened
 778     tty->print_cr("CompilerOracle: unrecognized line");
 779     tty->print_cr("  \"%s\"", original_line);
 780     if (error_msg != NULL) {
 781       tty->print_cr("%s", error_msg);
 782     }
 783   } else {
 784     // check for remaining characters
 785     bytes_read = 0;
 786     sscanf(line, "%*[ \t]%n", &bytes_read);
 787     if (line[bytes_read] != '\0') {
 788       tty->print_cr("CompilerOracle: unrecognized line");
 789       tty->print_cr("  \"%s\"", original_line);
 790       tty->print_cr("  Unrecognized text %s after command ", line);
 791     } else if (match != NULL && !_quiet) {
 792       tty->print("CompilerOracle: %s ", command_names[command]);
 793       match->print();
 794     }
 795   }
 796 }
 797 
 798 static const char* default_cc_file = ".hotspot_compiler";
 799 
 800 static const char* cc_file() {
 801 #ifdef ASSERT
 802   if (CompileCommandFile == NULL)
 803     return default_cc_file;
 804 #endif
 805   return CompileCommandFile;
 806 }
 807 
 808 bool CompilerOracle::has_command_file() {
 809   return cc_file() != NULL;
 810 }
 811 
 812 bool CompilerOracle::_quiet = false;
 813 
 814 void CompilerOracle::parse_from_file() {
 815   assert(has_command_file(), "command file must be specified");
 816   FILE* stream = fopen(cc_file(), "rt");
 817   if (stream == NULL) return;
 818 
 819   char token[1024];
 820   int  pos = 0;
 821   int  c = getc(stream);
 822   while(c != EOF && pos < (int)(sizeof(token)-1)) {
 823     if (c == '\n') {
 824       token[pos++] = '\0';
 825       parse_from_line(token);
 826       pos = 0;
 827     } else {
 828       token[pos++] = c;
 829     }
 830     c = getc(stream);
 831   }
 832   token[pos++] = '\0';
 833   parse_from_line(token);
 834 
 835   fclose(stream);
 836 }
 837 
 838 void CompilerOracle::parse_from_string(const char* str, void (*parse_line)(char*)) {
 839   char token[1024];
 840   int  pos = 0;
 841   const char* sp = str;
 842   int  c = *sp++;
 843   while (c != '\0' && pos < (int)(sizeof(token)-1)) {
 844     if (c == '\n') {
 845       token[pos++] = '\0';
 846       parse_line(token);
 847       pos = 0;
 848     } else {
 849       token[pos++] = c;
 850     }
 851     c = *sp++;
 852   }
 853   token[pos++] = '\0';
 854   parse_line(token);
 855 }
 856 
 857 void CompilerOracle::append_comment_to_file(const char* message) {
 858   assert(has_command_file(), "command file must be specified");
 859   fileStream stream(fopen(cc_file(), "at"));
 860   stream.print("# ");
 861   for (int index = 0; message[index] != '\0'; index++) {
 862     stream.put(message[index]);
 863     if (message[index] == '\n') stream.print("# ");
 864   }
 865   stream.cr();
 866 }
 867 
 868 void CompilerOracle::append_exclude_to_file(methodHandle method) {
 869   assert(has_command_file(), "command file must be specified");
 870   fileStream stream(fopen(cc_file(), "at"));
 871   stream.print("exclude ");
 872   method->method_holder()->name()->print_symbol_on(&stream);
 873   stream.print(".");
 874   method->name()->print_symbol_on(&stream);
 875   method->signature()->print_symbol_on(&stream);
 876   stream.cr();
 877   stream.cr();
 878 }
 879 
 880 
 881 void compilerOracle_init() {
 882   CompilerOracle::parse_from_string(CompileCommand, CompilerOracle::parse_from_line);
 883   CompilerOracle::parse_from_string(CompileOnly, CompilerOracle::parse_compile_only);
 884   if (CompilerOracle::has_command_file()) {
 885     CompilerOracle::parse_from_file();
 886   } else {
 887     struct stat buf;
 888     if (os::stat(default_cc_file, &buf) == 0) {
 889       warning("%s file is present but has been ignored.  "
 890               "Run with -XX:CompileCommandFile=%s to load the file.",
 891               default_cc_file, default_cc_file);
 892     }
 893   }
 894   if (lists[PrintCommand] != NULL) {
 895     if (PrintAssembly) {
 896       warning("CompileCommand and/or %s file contains 'print' commands, but PrintAssembly is also enabled", default_cc_file);
 897     } else if (FLAG_IS_DEFAULT(DebugNonSafepoints)) {
 898       warning("printing of assembly code is enabled; turning on DebugNonSafepoints to gain additional output");
 899       DebugNonSafepoints = true;
 900     }
 901   }
 902 }
 903 
 904 
 905 void CompilerOracle::parse_compile_only(char * line) {
 906   int i;
 907   char name[1024];
 908   const char* className = NULL;
 909   const char* methodName = NULL;
 910 
 911   bool have_colon = (strstr(line, "::") != NULL);
 912   char method_sep = have_colon ? ':' : '.';
 913 
 914   if (Verbose) {
 915     tty->print_cr("%s", line);
 916   }
 917 
 918   ResourceMark rm;
 919   while (*line != '\0') {
 920     MethodMatcher::Mode c_match = MethodMatcher::Exact;
 921     MethodMatcher::Mode m_match = MethodMatcher::Exact;
 922 
 923     for (i = 0;
 924          i < 1024 && *line != '\0' && *line != method_sep && *line != ',' && !isspace(*line);
 925          line++, i++) {
 926       name[i] = *line;
 927       if (name[i] == '.')  name[i] = '/';  // package prefix uses '/'
 928     }
 929 
 930     if (i > 0) {
 931       char* newName = NEW_RESOURCE_ARRAY( char, i + 1);
 932       if (newName == NULL)
 933         return;
 934       strncpy(newName, name, i);
 935       newName[i] = '\0';
 936 
 937       if (className == NULL) {
 938         className = newName;
 939         c_match = MethodMatcher::Prefix;
 940       } else {
 941         methodName = newName;
 942       }
 943     }
 944 
 945     if (*line == method_sep) {
 946       if (className == NULL) {
 947         className = "";
 948         c_match = MethodMatcher::Any;
 949       } else {
 950         // foo/bar.blah is an exact match on foo/bar, bar.blah is a suffix match on bar
 951         if (strchr(className, '/') != NULL) {
 952           c_match = MethodMatcher::Exact;
 953         } else {
 954           c_match = MethodMatcher::Suffix;
 955         }
 956       }
 957     } else {
 958       // got foo or foo/bar
 959       if (className == NULL) {
 960         ShouldNotReachHere();
 961       } else {
 962         // got foo or foo/bar
 963         if (strchr(className, '/') != NULL) {
 964           c_match = MethodMatcher::Prefix;
 965         } else if (className[0] == '\0') {
 966           c_match = MethodMatcher::Any;
 967         } else {
 968           c_match = MethodMatcher::Substring;
 969         }
 970       }
 971     }
 972 
 973     // each directive is terminated by , or NUL or . followed by NUL
 974     if (*line == ',' || *line == '\0' || (line[0] == '.' && line[1] == '\0')) {
 975       if (methodName == NULL) {
 976         methodName = "";
 977         if (*line != method_sep) {
 978           m_match = MethodMatcher::Any;
 979         }
 980       }
 981 
 982       EXCEPTION_MARK;
 983       Symbol* c_name = SymbolTable::new_symbol(className, CHECK);
 984       Symbol* m_name = SymbolTable::new_symbol(methodName, CHECK);
 985       Symbol* signature = NULL;
 986 
 987       add_predicate(CompileOnlyCommand, c_name, c_match, m_name, m_match, signature);
 988       if (PrintVMOptions) {
 989         tty->print("CompileOnly: compileonly ");
 990         lists[CompileOnlyCommand]->print();
 991       }
 992 
 993       className = NULL;
 994       methodName = NULL;
 995     }
 996 
 997     line = *line == '\0' ? line : line + 1;
 998   }
 999 }