1 /* 2 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "compiler/compilerOracle.hpp" 27 #include "memory/allocation.inline.hpp" 28 #include "memory/oopFactory.hpp" 29 #include "memory/resourceArea.hpp" 30 #include "oops/klass.hpp" 31 #include "oops/method.hpp" 32 #include "oops/oop.inline.hpp" 33 #include "oops/symbol.hpp" 34 #include "runtime/handles.inline.hpp" 35 #include "runtime/jniHandles.hpp" 36 #include "runtime/os.hpp" 37 38 class MethodMatcher : public CHeapObj<mtCompiler> { 39 public: 40 enum Mode { 41 Exact, 42 Prefix = 1, 43 Suffix = 2, 44 Substring = Prefix | Suffix, 45 Any, 46 Unknown = -1 47 }; 48 49 protected: 50 Symbol* _class_name; 51 Symbol* _method_name; 52 Symbol* _signature; 53 Mode _class_mode; 54 Mode _method_mode; 55 MethodMatcher* _next; 56 57 static bool match(Symbol* candidate, Symbol* match, Mode match_mode); 58 59 Symbol* class_name() const { return _class_name; } 60 Symbol* method_name() const { return _method_name; } 61 Symbol* signature() const { return _signature; } 62 63 public: 64 MethodMatcher(Symbol* class_name, Mode class_mode, 65 Symbol* method_name, Mode method_mode, 66 Symbol* signature, MethodMatcher* next); 67 MethodMatcher(Symbol* class_name, Symbol* method_name, MethodMatcher* next); 68 69 // utility method 70 MethodMatcher* find(methodHandle method) { 71 Symbol* class_name = method->method_holder()->name(); 72 Symbol* method_name = method->name(); 73 for (MethodMatcher* current = this; current != NULL; current = current->_next) { 74 if (match(class_name, current->class_name(), current->_class_mode) && 75 match(method_name, current->method_name(), current->_method_mode) && 76 (current->signature() == NULL || current->signature() == method->signature())) { 77 return current; 78 } 79 } 80 return NULL; 81 } 82 83 bool match(methodHandle method) { 84 return find(method) != NULL; 85 } 86 87 MethodMatcher* next() const { return _next; } 88 89 static void print_symbol(Symbol* h, Mode mode) { 90 ResourceMark rm; 91 92 if (mode == Suffix || mode == Substring || mode == Any) { 93 tty->print("*"); 94 } 95 if (mode != Any) { 96 h->print_symbol_on(tty); 97 } 98 if (mode == Prefix || mode == Substring) { 99 tty->print("*"); 100 } 101 } 102 103 void print_base() { 104 print_symbol(class_name(), _class_mode); 105 tty->print("."); 106 print_symbol(method_name(), _method_mode); 107 if (signature() != NULL) { 108 signature()->print_symbol_on(tty); 109 } 110 } 111 112 virtual void print() { 113 print_base(); 114 tty->cr(); 115 } 116 }; 117 118 MethodMatcher::MethodMatcher(Symbol* class_name, Symbol* method_name, MethodMatcher* next) { 119 _class_name = class_name; 120 _method_name = method_name; 121 _next = next; 122 _class_mode = MethodMatcher::Exact; 123 _method_mode = MethodMatcher::Exact; 124 _signature = NULL; 125 } 126 127 128 MethodMatcher::MethodMatcher(Symbol* class_name, Mode class_mode, 129 Symbol* method_name, Mode method_mode, 130 Symbol* signature, MethodMatcher* next): 131 _class_mode(class_mode) 132 , _method_mode(method_mode) 133 , _next(next) 134 , _class_name(class_name) 135 , _method_name(method_name) 136 , _signature(signature) { 137 } 138 139 bool MethodMatcher::match(Symbol* candidate, Symbol* match, Mode match_mode) { 140 if (match_mode == Any) { 141 return true; 142 } 143 144 if (match_mode == Exact) { 145 return candidate == match; 146 } 147 148 ResourceMark rm; 149 const char * candidate_string = candidate->as_C_string(); 150 const char * match_string = match->as_C_string(); 151 152 switch (match_mode) { 153 case Prefix: 154 return strstr(candidate_string, match_string) == candidate_string; 155 156 case Suffix: { 157 size_t clen = strlen(candidate_string); 158 size_t mlen = strlen(match_string); 159 return clen >= mlen && strcmp(candidate_string + clen - mlen, match_string) == 0; 160 } 161 162 case Substring: 163 return strstr(candidate_string, match_string) != NULL; 164 165 default: 166 return false; 167 } 168 } 169 170 enum OptionType { 171 IntxType, 172 UintxType, 173 BoolType, 174 CcstrType, 175 DoubleType, 176 UnknownType 177 }; 178 179 /* Methods to map real type names to OptionType */ 180 template<typename T> 181 static OptionType get_type_for() { 182 return UnknownType; 183 }; 184 185 template<> OptionType get_type_for<intx>() { 186 return IntxType; 187 } 188 189 template<> OptionType get_type_for<uintx>() { 190 return UintxType; 191 } 192 193 template<> OptionType get_type_for<bool>() { 194 return BoolType; 195 } 196 197 template<> OptionType get_type_for<ccstr>() { 198 return CcstrType; 199 } 200 201 template<> OptionType get_type_for<double>() { 202 return DoubleType; 203 } 204 205 template<typename T> 206 static const T copy_value(const T value) { 207 return value; 208 } 209 210 template<> const ccstr copy_value<ccstr>(const ccstr value) { 211 return (const ccstr)os::strdup_check_oom(value); 212 } 213 214 template <typename T> 215 class TypedMethodOptionMatcher : public MethodMatcher { 216 const char* _option; 217 OptionType _type; 218 const T _value; 219 220 public: 221 TypedMethodOptionMatcher(Symbol* class_name, Mode class_mode, 222 Symbol* method_name, Mode method_mode, 223 Symbol* signature, const char* opt, 224 const T value, MethodMatcher* next) : 225 MethodMatcher(class_name, class_mode, method_name, method_mode, signature, next), 226 _type(get_type_for<T>()), _value(copy_value<T>(value)) { 227 _option = os::strdup_check_oom(opt); 228 } 229 230 ~TypedMethodOptionMatcher() { 231 os::free((void*)_option); 232 } 233 234 TypedMethodOptionMatcher* match(methodHandle method, const char* opt) { 235 TypedMethodOptionMatcher* current = this; 236 while (current != NULL) { 237 current = (TypedMethodOptionMatcher*)current->find(method); 238 if (current == NULL) { 239 return NULL; 240 } 241 if (strcmp(current->_option, opt) == 0) { 242 return current; 243 } 244 current = current->next(); 245 } 246 return NULL; 247 } 248 249 TypedMethodOptionMatcher* next() { 250 return (TypedMethodOptionMatcher*)_next; 251 } 252 253 OptionType get_type(void) { 254 return _type; 255 }; 256 257 T value() { return _value; } 258 259 void print() { 260 ttyLocker ttyl; 261 print_base(); 262 tty->print(" %s", _option); 263 tty->print(" <unknown option type>"); 264 tty->cr(); 265 } 266 }; 267 268 template<> 269 void TypedMethodOptionMatcher<intx>::print() { 270 ttyLocker ttyl; 271 print_base(); 272 tty->print(" intx %s", _option); 273 tty->print(" = " INTX_FORMAT, _value); 274 tty->cr(); 275 }; 276 277 template<> 278 void TypedMethodOptionMatcher<uintx>::print() { 279 ttyLocker ttyl; 280 print_base(); 281 tty->print(" uintx %s", _option); 282 tty->print(" = " UINTX_FORMAT, _value); 283 tty->cr(); 284 }; 285 286 template<> 287 void TypedMethodOptionMatcher<bool>::print() { 288 ttyLocker ttyl; 289 print_base(); 290 tty->print(" bool %s", _option); 291 tty->print(" = %s", _value ? "true" : "false"); 292 tty->cr(); 293 }; 294 295 template<> 296 void TypedMethodOptionMatcher<ccstr>::print() { 297 ttyLocker ttyl; 298 print_base(); 299 tty->print(" const char* %s", _option); 300 tty->print(" = '%s'", _value); 301 tty->cr(); 302 }; 303 304 template<> 305 void TypedMethodOptionMatcher<double>::print() { 306 ttyLocker ttyl; 307 print_base(); 308 tty->print(" double %s", _option); 309 tty->print(" = %f", _value); 310 tty->cr(); 311 }; 312 313 // this must parallel the command_names below 314 enum OracleCommand { 315 UnknownCommand = -1, 316 OracleFirstCommand = 0, 317 BreakCommand = OracleFirstCommand, 318 PrintCommand, 319 ExcludeCommand, 320 InlineCommand, 321 DontInlineCommand, 322 CompileOnlyCommand, 323 LogCommand, 324 OptionCommand, 325 QuietCommand, 326 HelpCommand, 327 OracleCommandCount 328 }; 329 330 // this must parallel the enum OracleCommand 331 static const char * command_names[] = { 332 "break", 333 "print", 334 "exclude", 335 "inline", 336 "dontinline", 337 "compileonly", 338 "log", 339 "option", 340 "quiet", 341 "help" 342 }; 343 344 class MethodMatcher; 345 static MethodMatcher* lists[OracleCommandCount] = { 0, }; 346 347 348 static bool check_predicate(OracleCommand command, methodHandle method) { 349 return ((lists[command] != NULL) && 350 !method.is_null() && 351 lists[command]->match(method)); 352 } 353 354 355 static MethodMatcher* add_predicate(OracleCommand command, 356 Symbol* class_name, MethodMatcher::Mode c_mode, 357 Symbol* method_name, MethodMatcher::Mode m_mode, 358 Symbol* signature) { 359 assert(command != OptionCommand, "must use add_option_string"); 360 if (command == LogCommand && !LogCompilation && lists[LogCommand] == NULL) 361 tty->print_cr("Warning: +LogCompilation must be enabled in order for individual methods to be logged."); 362 lists[command] = new MethodMatcher(class_name, c_mode, method_name, m_mode, signature, lists[command]); 363 return lists[command]; 364 } 365 366 template<typename T> 367 static MethodMatcher* add_option_string(Symbol* class_name, MethodMatcher::Mode c_mode, 368 Symbol* method_name, MethodMatcher::Mode m_mode, 369 Symbol* signature, 370 const char* option, 371 T value) { 372 lists[OptionCommand] = new TypedMethodOptionMatcher<T>(class_name, c_mode, method_name, m_mode, 373 signature, option, value, lists[OptionCommand]); 374 return lists[OptionCommand]; 375 } 376 377 template<typename T> 378 static bool get_option_value(methodHandle method, const char* option, T& value) { 379 TypedMethodOptionMatcher<T>* m; 380 if (lists[OptionCommand] != NULL 381 && (m = ((TypedMethodOptionMatcher<T>*)lists[OptionCommand])->match(method, option)) != NULL 382 && m->get_type() == get_type_for<T>()) { 383 value = m->value(); 384 return true; 385 } else { 386 return false; 387 } 388 } 389 390 bool CompilerOracle::has_option_string(methodHandle method, const char* option) { 391 bool value = false; 392 get_option_value(method, option, value); 393 return value; 394 } 395 396 template<typename T> 397 bool CompilerOracle::has_option_value(methodHandle method, const char* option, T& value) { 398 return ::get_option_value(method, option, value); 399 } 400 401 // Explicit instantiation for all OptionTypes supported. 402 template bool CompilerOracle::has_option_value<intx>(methodHandle method, const char* option, intx& value); 403 template bool CompilerOracle::has_option_value<uintx>(methodHandle method, const char* option, uintx& value); 404 template bool CompilerOracle::has_option_value<bool>(methodHandle method, const char* option, bool& value); 405 template bool CompilerOracle::has_option_value<ccstr>(methodHandle method, const char* option, ccstr& value); 406 template bool CompilerOracle::has_option_value<double>(methodHandle method, const char* option, double& value); 407 408 bool CompilerOracle::should_exclude(methodHandle method, bool& quietly) { 409 quietly = true; 410 if (lists[ExcludeCommand] != NULL) { 411 if (lists[ExcludeCommand]->match(method)) { 412 quietly = _quiet; 413 return true; 414 } 415 } 416 417 if (lists[CompileOnlyCommand] != NULL) { 418 return !lists[CompileOnlyCommand]->match(method); 419 } 420 return false; 421 } 422 423 424 bool CompilerOracle::should_inline(methodHandle method) { 425 return (check_predicate(InlineCommand, method)); 426 } 427 428 429 bool CompilerOracle::should_not_inline(methodHandle method) { 430 return (check_predicate(DontInlineCommand, method)); 431 } 432 433 434 bool CompilerOracle::should_print(methodHandle method) { 435 return (check_predicate(PrintCommand, method)); 436 } 437 438 bool CompilerOracle::should_print_methods() { 439 return lists[PrintCommand] != NULL; 440 } 441 442 bool CompilerOracle::should_log(methodHandle method) { 443 if (!LogCompilation) return false; 444 if (lists[LogCommand] == NULL) return true; // by default, log all 445 return (check_predicate(LogCommand, method)); 446 } 447 448 449 bool CompilerOracle::should_break_at(methodHandle method) { 450 return check_predicate(BreakCommand, method); 451 } 452 453 454 static OracleCommand parse_command_name(const char * line, int* bytes_read) { 455 assert(ARRAY_SIZE(command_names) == OracleCommandCount, 456 "command_names size mismatch"); 457 458 *bytes_read = 0; 459 char command[33]; 460 int result = sscanf(line, "%32[a-z]%n", command, bytes_read); 461 for (uint i = 0; i < ARRAY_SIZE(command_names); i++) { 462 if (strcmp(command, command_names[i]) == 0) { 463 return (OracleCommand)i; 464 } 465 } 466 return UnknownCommand; 467 } 468 469 static void usage() { 470 tty->cr(); 471 tty->print_cr("The CompileCommand option enables the user of the JVM to control specific"); 472 tty->print_cr("behavior of the dynamic compilers. Many commands require a pattern that defines"); 473 tty->print_cr("the set of methods the command shall be applied to. The CompileCommand"); 474 tty->print_cr("option provides the following commands:"); 475 tty->cr(); 476 tty->print_cr(" break,<pattern> - debug breakpoint in compiler and in generated code"); 477 tty->print_cr(" print,<pattern> - print assembly"); 478 tty->print_cr(" exclude,<pattern> - don't compile or inline"); 479 tty->print_cr(" inline,<pattern> - always inline"); 480 tty->print_cr(" dontinline,<pattern> - don't inline"); 481 tty->print_cr(" compileonly,<pattern> - compile only"); 482 tty->print_cr(" log,<pattern> - log compilation"); 483 tty->print_cr(" option,<pattern>,<option type>,<option name>,<value>"); 484 tty->print_cr(" - set value of custom option"); 485 tty->print_cr(" option,<pattern>,<bool option name>"); 486 tty->print_cr(" - shorthand for setting boolean flag"); 487 tty->print_cr(" quiet - silence the compile command output"); 488 tty->print_cr(" help - print this text"); 489 tty->cr(); 490 tty->print_cr("The preferred format for the method matching pattern is:"); 491 tty->print_cr(" package/Class.method()"); 492 tty->cr(); 493 tty->print_cr("For backward compatibility this form is also allowed:"); 494 tty->print_cr(" package.Class::method()"); 495 tty->cr(); 496 tty->print_cr("The signature can be separated by an optional whitespace or comma:"); 497 tty->print_cr(" package/Class.method ()"); 498 tty->cr(); 499 tty->print_cr("The class and method identifier can be used together with leading or"); 500 tty->print_cr("trailing *'s for a small amount of wildcarding:"); 501 tty->print_cr(" *ackage/Clas*.*etho*()"); 502 tty->cr(); 503 tty->print_cr("It is possible to use more than one CompileCommand on the command line:"); 504 tty->print_cr(" -XX:CompileCommand=exclude,java/*.* -XX:CompileCommand=log,java*.*"); 505 tty->cr(); 506 tty->print_cr("The CompileCommands can be loaded from a file with the flag"); 507 tty->print_cr("-XX:CompileCommandFile=<file> or be added to the file '.hotspot_compiler'"); 508 tty->print_cr("Use the same format in the file as the argument to the CompileCommand flag."); 509 tty->print_cr("Add one command on each line."); 510 tty->print_cr(" exclude java/*.*"); 511 tty->print_cr(" option java/*.* ReplayInline"); 512 tty->cr(); 513 tty->print_cr("The following commands have conflicting behavior: 'exclude', 'inline', 'dontinline',"); 514 tty->print_cr("and 'compileonly'. There is no priority of commands. Applying (a subset of) these"); 515 tty->print_cr("commands to the same method results in undefined behavior."); 516 tty->cr(); 517 }; 518 519 // The JVM specification defines the allowed characters. 520 // Tokens that are disallowed by the JVM specification can have 521 // a meaning to the parser so we need to include them here. 522 // The parser does not enforce all rules of the JVMS - a successful parse 523 // does not mean that it is an allowed name. Illegal names will 524 // be ignored since they never can match a class or method. 525 // 526 // '\0' and 0xf0-0xff are disallowed in constant string values 527 // 0x20 ' ', 0x09 '\t' and, 0x2c ',' are used in the matching 528 // 0x5b '[' and 0x5d ']' can not be used because of the matcher 529 // 0x28 '(' and 0x29 ')' are used for the signature 530 // 0x2e '.' is always replaced before the matching 531 // 0x2f '/' is only used in the class name as package separator 532 533 #define RANGEBASE "\x1\x2\x3\x4\x5\x6\x7\x8\xa\xb\xc\xd\xe\xf" \ 534 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \ 535 "\x21\x22\x23\x24\x25\x26\x27\x2a\x2b\x2c\x2d" \ 536 "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" \ 537 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" \ 538 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5c\x5e\x5f" \ 539 "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" \ 540 "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f" \ 541 "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" \ 542 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" \ 543 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" \ 544 "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" \ 545 "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" \ 546 "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" \ 547 "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef" 548 549 #define RANGE0 "[*" RANGEBASE "]" 550 #define RANGESLASH "[*" RANGEBASE "/]" 551 552 static MethodMatcher::Mode check_mode(char name[], const char*& error_msg) { 553 int match = MethodMatcher::Exact; 554 while (name[0] == '*') { 555 match |= MethodMatcher::Suffix; 556 strcpy(name, name + 1); 557 } 558 559 if (strcmp(name, "*") == 0) return MethodMatcher::Any; 560 561 size_t len = strlen(name); 562 while (len > 0 && name[len - 1] == '*') { 563 match |= MethodMatcher::Prefix; 564 name[--len] = '\0'; 565 } 566 567 if (strstr(name, "*") != NULL) { 568 error_msg = " Embedded * not allowed"; 569 return MethodMatcher::Unknown; 570 } 571 return (MethodMatcher::Mode)match; 572 } 573 574 static bool scan_line(const char * line, 575 char class_name[], MethodMatcher::Mode* c_mode, 576 char method_name[], MethodMatcher::Mode* m_mode, 577 int* bytes_read, const char*& error_msg) { 578 *bytes_read = 0; 579 error_msg = NULL; 580 if (2 == sscanf(line, "%*[ \t]%255" RANGESLASH "%*[ ]" "%255" RANGE0 "%n", class_name, method_name, bytes_read)) { 581 *c_mode = check_mode(class_name, error_msg); 582 *m_mode = check_mode(method_name, error_msg); 583 return *c_mode != MethodMatcher::Unknown && *m_mode != MethodMatcher::Unknown; 584 } 585 return false; 586 } 587 588 589 590 // Scan next flag and value in line, return MethodMatcher object on success, NULL on failure. 591 // On failure, error_msg contains description for the first error. 592 // For future extensions: set error_msg on first error. 593 static MethodMatcher* scan_flag_and_value(const char* type, const char* line, int& total_bytes_read, 594 Symbol* c_name, MethodMatcher::Mode c_match, 595 Symbol* m_name, MethodMatcher::Mode m_match, 596 Symbol* signature, 597 char* errorbuf, const int buf_size) { 598 total_bytes_read = 0; 599 int bytes_read = 0; 600 char flag[256]; 601 602 // Read flag name. 603 if (sscanf(line, "%*[ \t]%255[a-zA-Z0-9]%n", flag, &bytes_read) == 1) { 604 line += bytes_read; 605 total_bytes_read += bytes_read; 606 607 // Read value. 608 if (strcmp(type, "intx") == 0) { 609 intx value; 610 if (sscanf(line, "%*[ \t]" INTX_FORMAT "%n", &value, &bytes_read) == 1) { 611 total_bytes_read += bytes_read; 612 return add_option_string(c_name, c_match, m_name, m_match, signature, flag, value); 613 } else { 614 jio_snprintf(errorbuf, buf_size, " Value cannot be read for flag %s of type %s ", flag, type); 615 } 616 } else if (strcmp(type, "uintx") == 0) { 617 uintx value; 618 if (sscanf(line, "%*[ \t]" UINTX_FORMAT "%n", &value, &bytes_read) == 1) { 619 total_bytes_read += bytes_read; 620 return add_option_string(c_name, c_match, m_name, m_match, signature, flag, value); 621 } else { 622 jio_snprintf(errorbuf, buf_size, " Value cannot be read for flag %s of type %s", flag, type); 623 } 624 } else if (strcmp(type, "ccstr") == 0) { 625 ResourceMark rm; 626 char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1); 627 if (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", value, &bytes_read) == 1) { 628 total_bytes_read += bytes_read; 629 return add_option_string(c_name, c_match, m_name, m_match, signature, flag, (ccstr)value); 630 } else { 631 jio_snprintf(errorbuf, buf_size, " Value cannot be read for flag %s of type %s", flag, type); 632 } 633 } else if (strcmp(type, "ccstrlist") == 0) { 634 // Accumulates several strings into one. The internal type is ccstr. 635 ResourceMark rm; 636 char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1); 637 char* next_value = value; 638 if (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", next_value, &bytes_read) == 1) { 639 total_bytes_read += bytes_read; 640 line += bytes_read; 641 next_value += bytes_read; 642 char* end_value = next_value-1; 643 while (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", next_value, &bytes_read) == 1) { 644 total_bytes_read += bytes_read; 645 line += bytes_read; 646 *end_value = ' '; // override '\0' 647 next_value += bytes_read; 648 end_value = next_value-1; 649 } 650 return add_option_string(c_name, c_match, m_name, m_match, signature, flag, (ccstr)value); 651 } else { 652 jio_snprintf(errorbuf, buf_size, " Value cannot be read for flag %s of type %s", flag, type); 653 } 654 } else if (strcmp(type, "bool") == 0) { 655 char value[256]; 656 if (sscanf(line, "%*[ \t]%255[a-zA-Z]%n", value, &bytes_read) == 1) { 657 if (strcmp(value, "true") == 0) { 658 total_bytes_read += bytes_read; 659 return add_option_string(c_name, c_match, m_name, m_match, signature, flag, true); 660 } else if (strcmp(value, "false") == 0) { 661 total_bytes_read += bytes_read; 662 return add_option_string(c_name, c_match, m_name, m_match, signature, flag, false); 663 } else { 664 jio_snprintf(errorbuf, buf_size, " Value cannot be read for flag %s of type %s", flag, type); 665 } 666 } else { 667 jio_snprintf(errorbuf, sizeof(errorbuf), " Value cannot be read for flag %s of type %s", flag, type); 668 } 669 } else if (strcmp(type, "double") == 0) { 670 char buffer[2][256]; 671 // Decimal separator '.' has been replaced with ' ' or '/' earlier, 672 // so read integer and fraction part of double value separately. 673 if (sscanf(line, "%*[ \t]%255[0-9]%*[ /\t]%255[0-9]%n", buffer[0], buffer[1], &bytes_read) == 2) { 674 char value[512] = ""; 675 strncat(value, buffer[0], 255); 676 strcat(value, "."); 677 strncat(value, buffer[1], 255); 678 total_bytes_read += bytes_read; 679 return add_option_string(c_name, c_match, m_name, m_match, signature, flag, atof(value)); 680 } else { 681 jio_snprintf(errorbuf, buf_size, " Value cannot be read for flag %s of type %s", flag, type); 682 } 683 } else { 684 jio_snprintf(errorbuf, sizeof(errorbuf), " Type %s not supported ", type); 685 } 686 } else { 687 jio_snprintf(errorbuf, sizeof(errorbuf), " Flag name for type %s should be alphanumeric ", type); 688 } 689 return NULL; 690 } 691 692 void CompilerOracle::parse_from_line(char* line) { 693 if (line[0] == '\0') return; 694 if (line[0] == '#') return; 695 696 bool have_colon = (strstr(line, "::") != NULL); 697 for (char* lp = line; *lp != '\0'; lp++) { 698 // Allow '.' to separate the class name from the method name. 699 // This is the preferred spelling of methods: 700 // exclude java/lang/String.indexOf(I)I 701 // Allow ',' for spaces (eases command line quoting). 702 // exclude,java/lang/String.indexOf 703 // For backward compatibility, allow space as separator also. 704 // exclude java/lang/String indexOf 705 // exclude,java/lang/String,indexOf 706 // For easy cut-and-paste of method names, allow VM output format 707 // as produced by Method::print_short_name: 708 // exclude java.lang.String::indexOf 709 // For simple implementation convenience here, convert them all to space. 710 if (have_colon) { 711 if (*lp == '.') *lp = '/'; // dots build the package prefix 712 if (*lp == ':') *lp = ' '; 713 } 714 if (*lp == ',' || *lp == '.') *lp = ' '; 715 } 716 717 char* original_line = line; 718 int bytes_read; 719 OracleCommand command = parse_command_name(line, &bytes_read); 720 line += bytes_read; 721 ResourceMark rm; 722 723 if (command == UnknownCommand) { 724 ttyLocker ttyl; 725 tty->print_cr("CompileCommand: unrecognized command"); 726 tty->print_cr(" \"%s\"", original_line); 727 CompilerOracle::print_tip(); 728 return; 729 } 730 731 if (command == QuietCommand) { 732 _quiet = true; 733 return; 734 } 735 736 if (command == HelpCommand) { 737 usage(); 738 return; 739 } 740 741 MethodMatcher::Mode c_match = MethodMatcher::Exact; 742 MethodMatcher::Mode m_match = MethodMatcher::Exact; 743 char class_name[256]; 744 char method_name[256]; 745 char sig[1024]; 746 char errorbuf[1024]; 747 const char* error_msg = NULL; // description of first error that appears 748 MethodMatcher* match = NULL; 749 750 if (scan_line(line, class_name, &c_match, method_name, &m_match, &bytes_read, error_msg)) { 751 EXCEPTION_MARK; 752 Symbol* c_name = SymbolTable::new_symbol(class_name, CHECK); 753 Symbol* m_name = SymbolTable::new_symbol(method_name, CHECK); 754 Symbol* signature = NULL; 755 756 line += bytes_read; 757 758 // Skip any leading spaces before signature 759 int whitespace_read = 0; 760 sscanf(line, "%*[ \t]%n", &whitespace_read); 761 if (whitespace_read > 0) { 762 line += whitespace_read; 763 } 764 765 // there might be a signature following the method. 766 // signatures always begin with ( so match that by hand 767 if (1 == sscanf(line, "(%254[[);/" RANGEBASE "]%n", sig + 1, &bytes_read)) { 768 sig[0] = '('; 769 line += bytes_read; 770 signature = SymbolTable::new_symbol(sig, CHECK); 771 } 772 773 if (command == OptionCommand) { 774 // Look for trailing options. 775 // 776 // Two types of trailing options are 777 // supported: 778 // 779 // (1) CompileCommand=option,Klass::method,flag 780 // (2) CompileCommand=option,Klass::method,type,flag,value 781 // 782 // Type (1) is used to enable a boolean flag for a method. 783 // 784 // Type (2) is used to support options with a value. Values can have the 785 // the following types: intx, uintx, bool, ccstr, ccstrlist, and double. 786 // 787 // For future extensions: extend scan_flag_and_value() 788 char option[256]; // stores flag for Type (1) and type of Type (2) 789 while (sscanf(line, "%*[ \t]%255[a-zA-Z0-9]%n", option, &bytes_read) == 1) { 790 if (match != NULL && !_quiet) { 791 // Print out the last match added 792 ttyLocker ttyl; 793 tty->print("CompileCommand: %s ", command_names[command]); 794 match->print(); 795 } 796 line += bytes_read; 797 798 if (strcmp(option, "intx") == 0 799 || strcmp(option, "uintx") == 0 800 || strcmp(option, "bool") == 0 801 || strcmp(option, "ccstr") == 0 802 || strcmp(option, "ccstrlist") == 0 803 || strcmp(option, "double") == 0 804 ) { 805 806 // Type (2) option: parse flag name and value. 807 match = scan_flag_and_value(option, line, bytes_read, 808 c_name, c_match, m_name, m_match, signature, 809 errorbuf, sizeof(errorbuf)); 810 if (match == NULL) { 811 error_msg = errorbuf; 812 break; 813 } 814 line += bytes_read; 815 } else { 816 // Type (1) option 817 match = add_option_string(c_name, c_match, m_name, m_match, signature, option, true); 818 } 819 } // while( 820 } else { 821 match = add_predicate(command, c_name, c_match, m_name, m_match, signature); 822 } 823 } 824 825 ttyLocker ttyl; 826 if (error_msg != NULL) { 827 // an error has happened 828 tty->print_cr("CompileCommand: An error occured during parsing"); 829 tty->print_cr(" \"%s\"", original_line); 830 if (error_msg != NULL) { 831 tty->print_cr("%s", error_msg); 832 } 833 CompilerOracle::print_tip(); 834 835 } else { 836 // check for remaining characters 837 bytes_read = 0; 838 sscanf(line, "%*[ \t]%n", &bytes_read); 839 if (line[bytes_read] != '\0') { 840 tty->print_cr("CompileCommand: Bad pattern"); 841 tty->print_cr(" \"%s\"", original_line); 842 tty->print_cr(" Unrecognized text %s after command ", line); 843 CompilerOracle::print_tip(); 844 } else if (match != NULL && !_quiet) { 845 tty->print("CompileCommand: %s ", command_names[command]); 846 match->print(); 847 } 848 } 849 } 850 851 void CompilerOracle::print_tip() { 852 tty->cr(); 853 tty->print_cr("Usage: '-XX:CompileCommand=command,\"package/Class.method()\"'"); 854 tty->print_cr("Use: '-XX:CompileCommand=help' for more information."); 855 tty->cr(); 856 } 857 858 static const char* default_cc_file = ".hotspot_compiler"; 859 860 static const char* cc_file() { 861 #ifdef ASSERT 862 if (CompileCommandFile == NULL) 863 return default_cc_file; 864 #endif 865 return CompileCommandFile; 866 } 867 868 bool CompilerOracle::has_command_file() { 869 return cc_file() != NULL; 870 } 871 872 bool CompilerOracle::_quiet = false; 873 874 void CompilerOracle::parse_from_file() { 875 assert(has_command_file(), "command file must be specified"); 876 FILE* stream = fopen(cc_file(), "rt"); 877 if (stream == NULL) return; 878 879 char token[1024]; 880 int pos = 0; 881 int c = getc(stream); 882 while(c != EOF && pos < (int)(sizeof(token)-1)) { 883 if (c == '\n') { 884 token[pos++] = '\0'; 885 parse_from_line(token); 886 pos = 0; 887 } else { 888 token[pos++] = c; 889 } 890 c = getc(stream); 891 } 892 token[pos++] = '\0'; 893 parse_from_line(token); 894 895 fclose(stream); 896 } 897 898 void CompilerOracle::parse_from_string(const char* str, void (*parse_line)(char*)) { 899 char token[1024]; 900 int pos = 0; 901 const char* sp = str; 902 int c = *sp++; 903 while (c != '\0' && pos < (int)(sizeof(token)-1)) { 904 if (c == '\n') { 905 token[pos++] = '\0'; 906 parse_line(token); 907 pos = 0; 908 } else { 909 token[pos++] = c; 910 } 911 c = *sp++; 912 } 913 token[pos++] = '\0'; 914 parse_line(token); 915 } 916 917 void CompilerOracle::append_comment_to_file(const char* message) { 918 assert(has_command_file(), "command file must be specified"); 919 fileStream stream(fopen(cc_file(), "at")); 920 stream.print("# "); 921 for (int index = 0; message[index] != '\0'; index++) { 922 stream.put(message[index]); 923 if (message[index] == '\n') stream.print("# "); 924 } 925 stream.cr(); 926 } 927 928 void CompilerOracle::append_exclude_to_file(methodHandle method) { 929 assert(has_command_file(), "command file must be specified"); 930 fileStream stream(fopen(cc_file(), "at")); 931 stream.print("exclude "); 932 method->method_holder()->name()->print_symbol_on(&stream); 933 stream.print("."); 934 method->name()->print_symbol_on(&stream); 935 method->signature()->print_symbol_on(&stream); 936 stream.cr(); 937 stream.cr(); 938 } 939 940 941 void compilerOracle_init() { 942 CompilerOracle::parse_from_string(CompileCommand, CompilerOracle::parse_from_line); 943 CompilerOracle::parse_from_string(CompileOnly, CompilerOracle::parse_compile_only); 944 if (CompilerOracle::has_command_file()) { 945 CompilerOracle::parse_from_file(); 946 } else { 947 struct stat buf; 948 if (os::stat(default_cc_file, &buf) == 0) { 949 warning("%s file is present but has been ignored. " 950 "Run with -XX:CompileCommandFile=%s to load the file.", 951 default_cc_file, default_cc_file); 952 } 953 } 954 if (lists[PrintCommand] != NULL) { 955 if (PrintAssembly) { 956 warning("CompileCommand and/or %s file contains 'print' commands, but PrintAssembly is also enabled", default_cc_file); 957 } else if (FLAG_IS_DEFAULT(DebugNonSafepoints)) { 958 warning("printing of assembly code is enabled; turning on DebugNonSafepoints to gain additional output"); 959 DebugNonSafepoints = true; 960 } 961 } 962 } 963 964 965 void CompilerOracle::parse_compile_only(char * line) { 966 int i; 967 char name[1024]; 968 const char* className = NULL; 969 const char* methodName = NULL; 970 971 bool have_colon = (strstr(line, "::") != NULL); 972 char method_sep = have_colon ? ':' : '.'; 973 974 if (Verbose) { 975 tty->print_cr("%s", line); 976 } 977 978 ResourceMark rm; 979 while (*line != '\0') { 980 MethodMatcher::Mode c_match = MethodMatcher::Exact; 981 MethodMatcher::Mode m_match = MethodMatcher::Exact; 982 983 for (i = 0; 984 i < 1024 && *line != '\0' && *line != method_sep && *line != ',' && !isspace(*line); 985 line++, i++) { 986 name[i] = *line; 987 if (name[i] == '.') name[i] = '/'; // package prefix uses '/' 988 } 989 990 if (i > 0) { 991 char* newName = NEW_RESOURCE_ARRAY( char, i + 1); 992 if (newName == NULL) 993 return; 994 strncpy(newName, name, i); 995 newName[i] = '\0'; 996 997 if (className == NULL) { 998 className = newName; 999 c_match = MethodMatcher::Prefix; 1000 } else { 1001 methodName = newName; 1002 } 1003 } 1004 1005 if (*line == method_sep) { 1006 if (className == NULL) { 1007 className = ""; 1008 c_match = MethodMatcher::Any; 1009 } else { 1010 // foo/bar.blah is an exact match on foo/bar, bar.blah is a suffix match on bar 1011 if (strchr(className, '/') != NULL) { 1012 c_match = MethodMatcher::Exact; 1013 } else { 1014 c_match = MethodMatcher::Suffix; 1015 } 1016 } 1017 } else { 1018 // got foo or foo/bar 1019 if (className == NULL) { 1020 ShouldNotReachHere(); 1021 } else { 1022 // got foo or foo/bar 1023 if (strchr(className, '/') != NULL) { 1024 c_match = MethodMatcher::Prefix; 1025 } else if (className[0] == '\0') { 1026 c_match = MethodMatcher::Any; 1027 } else { 1028 c_match = MethodMatcher::Substring; 1029 } 1030 } 1031 } 1032 1033 // each directive is terminated by , or NUL or . followed by NUL 1034 if (*line == ',' || *line == '\0' || (line[0] == '.' && line[1] == '\0')) { 1035 if (methodName == NULL) { 1036 methodName = ""; 1037 if (*line != method_sep) { 1038 m_match = MethodMatcher::Any; 1039 } 1040 } 1041 1042 EXCEPTION_MARK; 1043 Symbol* c_name = SymbolTable::new_symbol(className, CHECK); 1044 Symbol* m_name = SymbolTable::new_symbol(methodName, CHECK); 1045 Symbol* signature = NULL; 1046 1047 add_predicate(CompileOnlyCommand, c_name, c_match, m_name, m_match, signature); 1048 if (PrintVMOptions) { 1049 tty->print("CompileOnly: compileonly "); 1050 lists[CompileOnlyCommand]->print(); 1051 } 1052 1053 className = NULL; 1054 methodName = NULL; 1055 } 1056 1057 line = *line == '\0' ? line : line + 1; 1058 } 1059 }