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 // Copy remaining string plus NUL to the beginning 557 memmove(name, name + 1, strlen(name + 1) + 1); 558 } 559 560 if (strcmp(name, "*") == 0) return MethodMatcher::Any; 561 562 size_t len = strlen(name); 563 while (len > 0 && name[len - 1] == '*') { 564 match |= MethodMatcher::Prefix; 565 name[--len] = '\0'; 566 } 567 568 if (strstr(name, "*") != NULL) { 569 error_msg = " Embedded * not allowed"; 570 return MethodMatcher::Unknown; 571 } 572 return (MethodMatcher::Mode)match; 573 } 574 575 static bool scan_line(const char * line, 576 char class_name[], MethodMatcher::Mode* c_mode, 577 char method_name[], MethodMatcher::Mode* m_mode, 578 int* bytes_read, const char*& error_msg) { 579 *bytes_read = 0; 580 error_msg = NULL; 581 if (2 == sscanf(line, "%*[ \t]%255" RANGESLASH "%*[ ]" "%255" RANGE0 "%n", class_name, method_name, bytes_read)) { 582 *c_mode = check_mode(class_name, error_msg); 583 *m_mode = check_mode(method_name, error_msg); 584 return *c_mode != MethodMatcher::Unknown && *m_mode != MethodMatcher::Unknown; 585 } 586 return false; 587 } 588 589 590 591 // Scan next flag and value in line, return MethodMatcher object on success, NULL on failure. 592 // On failure, error_msg contains description for the first error. 593 // For future extensions: set error_msg on first error. 594 static MethodMatcher* scan_flag_and_value(const char* type, const char* line, int& total_bytes_read, 595 Symbol* c_name, MethodMatcher::Mode c_match, 596 Symbol* m_name, MethodMatcher::Mode m_match, 597 Symbol* signature, 598 char* errorbuf, const int buf_size) { 599 total_bytes_read = 0; 600 int bytes_read = 0; 601 char flag[256]; 602 603 // Read flag name. 604 if (sscanf(line, "%*[ \t]%255[a-zA-Z0-9]%n", flag, &bytes_read) == 1) { 605 line += bytes_read; 606 total_bytes_read += bytes_read; 607 608 // Read value. 609 if (strcmp(type, "intx") == 0) { 610 intx value; 611 if (sscanf(line, "%*[ \t]" INTX_FORMAT "%n", &value, &bytes_read) == 1) { 612 total_bytes_read += bytes_read; 613 return add_option_string(c_name, c_match, m_name, m_match, signature, flag, value); 614 } else { 615 jio_snprintf(errorbuf, buf_size, " Value cannot be read for flag %s of type %s ", flag, type); 616 } 617 } else if (strcmp(type, "uintx") == 0) { 618 uintx value; 619 if (sscanf(line, "%*[ \t]" UINTX_FORMAT "%n", &value, &bytes_read) == 1) { 620 total_bytes_read += bytes_read; 621 return add_option_string(c_name, c_match, m_name, m_match, signature, flag, value); 622 } else { 623 jio_snprintf(errorbuf, buf_size, " Value cannot be read for flag %s of type %s", flag, type); 624 } 625 } else if (strcmp(type, "ccstr") == 0) { 626 ResourceMark rm; 627 char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1); 628 if (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", value, &bytes_read) == 1) { 629 total_bytes_read += bytes_read; 630 return add_option_string(c_name, c_match, m_name, m_match, signature, flag, (ccstr)value); 631 } else { 632 jio_snprintf(errorbuf, buf_size, " Value cannot be read for flag %s of type %s", flag, type); 633 } 634 } else if (strcmp(type, "ccstrlist") == 0) { 635 // Accumulates several strings into one. The internal type is ccstr. 636 ResourceMark rm; 637 char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1); 638 char* next_value = value; 639 if (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", next_value, &bytes_read) == 1) { 640 total_bytes_read += bytes_read; 641 line += bytes_read; 642 next_value += bytes_read; 643 char* end_value = next_value-1; 644 while (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9]%n", next_value, &bytes_read) == 1) { 645 total_bytes_read += bytes_read; 646 line += bytes_read; 647 *end_value = ' '; // override '\0' 648 next_value += bytes_read; 649 end_value = next_value-1; 650 } 651 return add_option_string(c_name, c_match, m_name, m_match, signature, flag, (ccstr)value); 652 } else { 653 jio_snprintf(errorbuf, buf_size, " Value cannot be read for flag %s of type %s", flag, type); 654 } 655 } else if (strcmp(type, "bool") == 0) { 656 char value[256]; 657 if (sscanf(line, "%*[ \t]%255[a-zA-Z]%n", value, &bytes_read) == 1) { 658 if (strcmp(value, "true") == 0) { 659 total_bytes_read += bytes_read; 660 return add_option_string(c_name, c_match, m_name, m_match, signature, flag, true); 661 } else if (strcmp(value, "false") == 0) { 662 total_bytes_read += bytes_read; 663 return add_option_string(c_name, c_match, m_name, m_match, signature, flag, false); 664 } else { 665 jio_snprintf(errorbuf, buf_size, " Value cannot be read for flag %s of type %s", flag, type); 666 } 667 } else { 668 jio_snprintf(errorbuf, sizeof(errorbuf), " Value cannot be read for flag %s of type %s", flag, type); 669 } 670 } else if (strcmp(type, "double") == 0) { 671 char buffer[2][256]; 672 // Decimal separator '.' has been replaced with ' ' or '/' earlier, 673 // so read integer and fraction part of double value separately. 674 if (sscanf(line, "%*[ \t]%255[0-9]%*[ /\t]%255[0-9]%n", buffer[0], buffer[1], &bytes_read) == 2) { 675 char value[512] = ""; 676 jio_snprintf(value, sizeof(value), "%s.%s", buffer[0], buffer[1]); 677 total_bytes_read += bytes_read; 678 return add_option_string(c_name, c_match, m_name, m_match, signature, flag, atof(value)); 679 } else { 680 jio_snprintf(errorbuf, buf_size, " Value cannot be read for flag %s of type %s", flag, type); 681 } 682 } else { 683 jio_snprintf(errorbuf, sizeof(errorbuf), " Type %s not supported ", type); 684 } 685 } else { 686 jio_snprintf(errorbuf, sizeof(errorbuf), " Flag name for type %s should be alphanumeric ", type); 687 } 688 return NULL; 689 } 690 691 int skip_whitespace(char* line) { 692 // Skip any leading spaces 693 int whitespace_read = 0; 694 sscanf(line, "%*[ \t]%n", &whitespace_read); 695 return whitespace_read; 696 } 697 698 void CompilerOracle::parse_from_line(char* line) { 699 if (line[0] == '\0') return; 700 if (line[0] == '#') return; 701 702 bool have_colon = (strstr(line, "::") != NULL); 703 for (char* lp = line; *lp != '\0'; lp++) { 704 // Allow '.' to separate the class name from the method name. 705 // This is the preferred spelling of methods: 706 // exclude java/lang/String.indexOf(I)I 707 // Allow ',' for spaces (eases command line quoting). 708 // exclude,java/lang/String.indexOf 709 // For backward compatibility, allow space as separator also. 710 // exclude java/lang/String indexOf 711 // exclude,java/lang/String,indexOf 712 // For easy cut-and-paste of method names, allow VM output format 713 // as produced by Method::print_short_name: 714 // exclude java.lang.String::indexOf 715 // For simple implementation convenience here, convert them all to space. 716 if (have_colon) { 717 if (*lp == '.') *lp = '/'; // dots build the package prefix 718 if (*lp == ':') *lp = ' '; 719 } 720 if (*lp == ',' || *lp == '.') *lp = ' '; 721 } 722 723 char* original_line = line; 724 int bytes_read; 725 OracleCommand command = parse_command_name(line, &bytes_read); 726 line += bytes_read; 727 ResourceMark rm; 728 729 if (command == UnknownCommand) { 730 ttyLocker ttyl; 731 tty->print_cr("CompileCommand: unrecognized command"); 732 tty->print_cr(" \"%s\"", original_line); 733 CompilerOracle::print_tip(); 734 return; 735 } 736 737 if (command == QuietCommand) { 738 _quiet = true; 739 return; 740 } 741 742 if (command == HelpCommand) { 743 usage(); 744 return; 745 } 746 747 MethodMatcher::Mode c_match = MethodMatcher::Exact; 748 MethodMatcher::Mode m_match = MethodMatcher::Exact; 749 char class_name[256]; 750 char method_name[256]; 751 char sig[1024]; 752 char errorbuf[1024]; 753 const char* error_msg = NULL; // description of first error that appears 754 MethodMatcher* match = NULL; 755 756 if (scan_line(line, class_name, &c_match, method_name, &m_match, &bytes_read, error_msg)) { 757 EXCEPTION_MARK; 758 Symbol* c_name = SymbolTable::new_symbol(class_name, CHECK); 759 Symbol* m_name = SymbolTable::new_symbol(method_name, CHECK); 760 Symbol* signature = NULL; 761 762 line += bytes_read; 763 764 // there might be a signature following the method. 765 // signatures always begin with ( so match that by hand 766 line += skip_whitespace(line); 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 790 line += skip_whitespace(line); 791 while (sscanf(line, "%255[a-zA-Z0-9]%n", option, &bytes_read) == 1) { 792 if (match != NULL && !_quiet) { 793 // Print out the last match added 794 ttyLocker ttyl; 795 tty->print("CompileCommand: %s ", command_names[command]); 796 match->print(); 797 } 798 line += bytes_read; 799 800 if (strcmp(option, "intx") == 0 801 || strcmp(option, "uintx") == 0 802 || strcmp(option, "bool") == 0 803 || strcmp(option, "ccstr") == 0 804 || strcmp(option, "ccstrlist") == 0 805 || strcmp(option, "double") == 0 806 ) { 807 808 // Type (2) option: parse flag name and value. 809 match = scan_flag_and_value(option, line, bytes_read, 810 c_name, c_match, m_name, m_match, signature, 811 errorbuf, sizeof(errorbuf)); 812 if (match == NULL) { 813 error_msg = errorbuf; 814 break; 815 } 816 line += bytes_read; 817 } else { 818 // Type (1) option 819 match = add_option_string(c_name, c_match, m_name, m_match, signature, option, true); 820 } 821 line += skip_whitespace(line); 822 } // while( 823 } else { 824 match = add_predicate(command, c_name, c_match, m_name, m_match, signature); 825 } 826 } 827 828 ttyLocker ttyl; 829 if (error_msg != NULL) { 830 // an error has happened 831 tty->print_cr("CompileCommand: An error occured during parsing"); 832 tty->print_cr(" \"%s\"", original_line); 833 if (error_msg != NULL) { 834 tty->print_cr("%s", error_msg); 835 } 836 CompilerOracle::print_tip(); 837 838 } else { 839 // check for remaining characters 840 bytes_read = 0; 841 sscanf(line, "%*[ \t]%n", &bytes_read); 842 if (line[bytes_read] != '\0') { 843 tty->print_cr("CompileCommand: Bad pattern"); 844 tty->print_cr(" \"%s\"", original_line); 845 tty->print_cr(" Unrecognized text %s after command ", line); 846 CompilerOracle::print_tip(); 847 } else if (match != NULL && !_quiet) { 848 tty->print("CompileCommand: %s ", command_names[command]); 849 match->print(); 850 } 851 } 852 } 853 854 void CompilerOracle::print_tip() { 855 tty->cr(); 856 tty->print_cr("Usage: '-XX:CompileCommand=command,\"package/Class.method()\"'"); 857 tty->print_cr("Use: '-XX:CompileCommand=help' for more information."); 858 tty->cr(); 859 } 860 861 static const char* default_cc_file = ".hotspot_compiler"; 862 863 static const char* cc_file() { 864 #ifdef ASSERT 865 if (CompileCommandFile == NULL) 866 return default_cc_file; 867 #endif 868 return CompileCommandFile; 869 } 870 871 bool CompilerOracle::has_command_file() { 872 return cc_file() != NULL; 873 } 874 875 bool CompilerOracle::_quiet = false; 876 877 void CompilerOracle::parse_from_file() { 878 assert(has_command_file(), "command file must be specified"); 879 FILE* stream = fopen(cc_file(), "rt"); 880 if (stream == NULL) return; 881 882 char token[1024]; 883 int pos = 0; 884 int c = getc(stream); 885 while(c != EOF && pos < (int)(sizeof(token)-1)) { 886 if (c == '\n') { 887 token[pos++] = '\0'; 888 parse_from_line(token); 889 pos = 0; 890 } else { 891 token[pos++] = c; 892 } 893 c = getc(stream); 894 } 895 token[pos++] = '\0'; 896 parse_from_line(token); 897 898 fclose(stream); 899 } 900 901 void CompilerOracle::parse_from_string(const char* str, void (*parse_line)(char*)) { 902 char token[1024]; 903 int pos = 0; 904 const char* sp = str; 905 int c = *sp++; 906 while (c != '\0' && pos < (int)(sizeof(token)-1)) { 907 if (c == '\n') { 908 token[pos++] = '\0'; 909 parse_line(token); 910 pos = 0; 911 } else { 912 token[pos++] = c; 913 } 914 c = *sp++; 915 } 916 token[pos++] = '\0'; 917 parse_line(token); 918 } 919 920 void CompilerOracle::append_comment_to_file(const char* message) { 921 assert(has_command_file(), "command file must be specified"); 922 fileStream stream(fopen(cc_file(), "at")); 923 stream.print("# "); 924 for (int index = 0; message[index] != '\0'; index++) { 925 stream.put(message[index]); 926 if (message[index] == '\n') stream.print("# "); 927 } 928 stream.cr(); 929 } 930 931 void CompilerOracle::append_exclude_to_file(methodHandle method) { 932 assert(has_command_file(), "command file must be specified"); 933 fileStream stream(fopen(cc_file(), "at")); 934 stream.print("exclude "); 935 method->method_holder()->name()->print_symbol_on(&stream); 936 stream.print("."); 937 method->name()->print_symbol_on(&stream); 938 method->signature()->print_symbol_on(&stream); 939 stream.cr(); 940 stream.cr(); 941 } 942 943 944 void compilerOracle_init() { 945 CompilerOracle::parse_from_string(CompileCommand, CompilerOracle::parse_from_line); 946 CompilerOracle::parse_from_string(CompileOnly, CompilerOracle::parse_compile_only); 947 if (CompilerOracle::has_command_file()) { 948 CompilerOracle::parse_from_file(); 949 } else { 950 struct stat buf; 951 if (os::stat(default_cc_file, &buf) == 0) { 952 warning("%s file is present but has been ignored. " 953 "Run with -XX:CompileCommandFile=%s to load the file.", 954 default_cc_file, default_cc_file); 955 } 956 } 957 if (lists[PrintCommand] != NULL) { 958 if (PrintAssembly) { 959 warning("CompileCommand and/or %s file contains 'print' commands, but PrintAssembly is also enabled", default_cc_file); 960 } else if (FLAG_IS_DEFAULT(DebugNonSafepoints)) { 961 warning("printing of assembly code is enabled; turning on DebugNonSafepoints to gain additional output"); 962 DebugNonSafepoints = true; 963 } 964 } 965 } 966 967 968 void CompilerOracle::parse_compile_only(char * line) { 969 int i; 970 char name[1024]; 971 const char* className = NULL; 972 const char* methodName = NULL; 973 974 bool have_colon = (strstr(line, "::") != NULL); 975 char method_sep = have_colon ? ':' : '.'; 976 977 if (Verbose) { 978 tty->print_cr("%s", line); 979 } 980 981 ResourceMark rm; 982 while (*line != '\0') { 983 MethodMatcher::Mode c_match = MethodMatcher::Exact; 984 MethodMatcher::Mode m_match = MethodMatcher::Exact; 985 986 for (i = 0; 987 i < 1024 && *line != '\0' && *line != method_sep && *line != ',' && !isspace(*line); 988 line++, i++) { 989 name[i] = *line; 990 if (name[i] == '.') name[i] = '/'; // package prefix uses '/' 991 } 992 993 if (i > 0) { 994 char* newName = NEW_RESOURCE_ARRAY( char, i + 1); 995 if (newName == NULL) 996 return; 997 strncpy(newName, name, i); 998 newName[i] = '\0'; 999 1000 if (className == NULL) { 1001 className = newName; 1002 c_match = MethodMatcher::Prefix; 1003 } else { 1004 methodName = newName; 1005 } 1006 } 1007 1008 if (*line == method_sep) { 1009 if (className == NULL) { 1010 className = ""; 1011 c_match = MethodMatcher::Any; 1012 } else { 1013 // foo/bar.blah is an exact match on foo/bar, bar.blah is a suffix match on bar 1014 if (strchr(className, '/') != NULL) { 1015 c_match = MethodMatcher::Exact; 1016 } else { 1017 c_match = MethodMatcher::Suffix; 1018 } 1019 } 1020 } else { 1021 // got foo or foo/bar 1022 if (className == NULL) { 1023 ShouldNotReachHere(); 1024 } else { 1025 // got foo or foo/bar 1026 if (strchr(className, '/') != NULL) { 1027 c_match = MethodMatcher::Prefix; 1028 } else if (className[0] == '\0') { 1029 c_match = MethodMatcher::Any; 1030 } else { 1031 c_match = MethodMatcher::Substring; 1032 } 1033 } 1034 } 1035 1036 // each directive is terminated by , or NUL or . followed by NUL 1037 if (*line == ',' || *line == '\0' || (line[0] == '.' && line[1] == '\0')) { 1038 if (methodName == NULL) { 1039 methodName = ""; 1040 if (*line != method_sep) { 1041 m_match = MethodMatcher::Any; 1042 } 1043 } 1044 1045 EXCEPTION_MARK; 1046 Symbol* c_name = SymbolTable::new_symbol(className, CHECK); 1047 Symbol* m_name = SymbolTable::new_symbol(methodName, CHECK); 1048 Symbol* signature = NULL; 1049 1050 add_predicate(CompileOnlyCommand, c_name, c_match, m_name, m_match, signature); 1051 if (PrintVMOptions) { 1052 tty->print("CompileOnly: compileonly "); 1053 lists[CompileOnlyCommand]->print(); 1054 } 1055 1056 className = NULL; 1057 methodName = NULL; 1058 } 1059 1060 line = *line == '\0' ? line : line + 1; 1061 } 1062 }