Path: blob/master/src/hotspot/share/compiler/compilerOracle.cpp
40930 views
/*1* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "jvm.h"26#include "classfile/symbolTable.hpp"27#include "compiler/compilerDirectives.hpp"28#include "compiler/compilerOracle.hpp"29#include "compiler/methodMatcher.hpp"30#include "memory/allocation.inline.hpp"31#include "memory/oopFactory.hpp"32#include "memory/resourceArea.hpp"33#include "oops/klass.hpp"34#include "oops/method.inline.hpp"35#include "oops/symbol.hpp"36#include "runtime/globals_extension.hpp"37#include "runtime/handles.inline.hpp"38#include "runtime/jniHandles.hpp"39#include "runtime/os.hpp"4041static const char* optiontype_names[] = {42#define enum_of_types(type, name) name,43OPTION_TYPES(enum_of_types)44#undef enum_of_types45};4647const char* optiontype2name(enum OptionType type) {48return optiontype_names[static_cast<int>(type)];49}5051static enum OptionType option_types[] = {52#define enum_of_options(option, name, ctype) OptionType::ctype,53COMPILECOMMAND_OPTIONS(enum_of_options)54#undef enum_of_options55};5657enum OptionType option2type(enum CompileCommand option) {58return option_types[static_cast<int>(option)];59}6061static const char* option_names[] = {62#define enum_of_options(option, name, ctype) name,63COMPILECOMMAND_OPTIONS(enum_of_options)64#undef enum_of_options65};6667const char* option2name(enum CompileCommand option) {68return option_names[static_cast<int>(option)];69}7071/* Methods to map real type names to OptionType */72template<typename T>73static OptionType get_type_for() {74return OptionType::Unknown;75};7677template<> OptionType get_type_for<intx>() {78return OptionType::Intx;79}8081template<> OptionType get_type_for<uintx>() {82return OptionType::Uintx;83}8485template<> OptionType get_type_for<bool>() {86return OptionType::Bool;87}8889template<> OptionType get_type_for<ccstr>() {90return OptionType::Ccstr;91}9293template<> OptionType get_type_for<double>() {94return OptionType::Double;95}9697class MethodMatcher;98class TypedMethodOptionMatcher;99100static TypedMethodOptionMatcher* option_list = NULL;101static bool any_set = false;102103// A filter for quick lookup if an option is set104static bool option_filter[static_cast<int>(CompileCommand::Unknown) + 1] = { 0 };105106void command_set_in_filter(enum CompileCommand option) {107assert(option != CompileCommand::Unknown, "sanity");108assert(option2type(option) != OptionType::Unknown, "sanity");109110if ((option != CompileCommand::DontInline) &&111(option != CompileCommand::Inline) &&112(option != CompileCommand::Log)) {113any_set = true;114}115option_filter[static_cast<int>(option)] = true;116}117118bool has_command(enum CompileCommand option) {119return option_filter[static_cast<int>(option)];120}121122class TypedMethodOptionMatcher : public MethodMatcher {123private:124TypedMethodOptionMatcher* _next;125enum CompileCommand _option;126public:127128union {129bool bool_value;130intx intx_value;131uintx uintx_value;132double double_value;133ccstr ccstr_value;134} _u;135136TypedMethodOptionMatcher() : MethodMatcher(),137_next(NULL),138_option(CompileCommand::Unknown) {139memset(&_u, 0, sizeof(_u));140}141142~TypedMethodOptionMatcher();143static TypedMethodOptionMatcher* parse_method_pattern(char*& line, char* errorbuf, const int buf_size);144TypedMethodOptionMatcher* match(const methodHandle &method, enum CompileCommand option);145146void init(enum CompileCommand option, TypedMethodOptionMatcher* next) {147_next = next;148_option = option;149}150151void init_matcher(Symbol* class_name, Mode class_mode,152Symbol* method_name, Mode method_mode,153Symbol* signature) {154MethodMatcher::init(class_name, class_mode, method_name, method_mode, signature);155}156157void set_next(TypedMethodOptionMatcher* next) {_next = next; }158TypedMethodOptionMatcher* next() { return _next; }159enum CompileCommand option() { return _option; }160template<typename T> T value();161template<typename T> void set_value(T value);162void print();163void print_all();164TypedMethodOptionMatcher* clone();165};166167// A few templated accessors instead of a full template class.168template<> intx TypedMethodOptionMatcher::value<intx>() {169return _u.intx_value;170}171172template<> uintx TypedMethodOptionMatcher::value<uintx>() {173return _u.uintx_value;174}175176template<> bool TypedMethodOptionMatcher::value<bool>() {177return _u.bool_value;178}179180template<> double TypedMethodOptionMatcher::value<double>() {181return _u.double_value;182}183184template<> ccstr TypedMethodOptionMatcher::value<ccstr>() {185return _u.ccstr_value;186}187188template<> void TypedMethodOptionMatcher::set_value(intx value) {189_u.intx_value = value;190}191192template<> void TypedMethodOptionMatcher::set_value(uintx value) {193_u.uintx_value = value;194}195196template<> void TypedMethodOptionMatcher::set_value(double value) {197_u.double_value = value;198}199200template<> void TypedMethodOptionMatcher::set_value(bool value) {201_u.bool_value = value;202}203204template<> void TypedMethodOptionMatcher::set_value(ccstr value) {205_u.ccstr_value = (const ccstr)os::strdup_check_oom(value);206}207208void TypedMethodOptionMatcher::print() {209ttyLocker ttyl;210print_base(tty);211const char* name = option2name(_option);212enum OptionType type = option2type(_option);213switch (type) {214case OptionType::Intx:215tty->print_cr(" intx %s = " INTX_FORMAT, name, value<intx>());216break;217case OptionType::Uintx:218tty->print_cr(" uintx %s = " UINTX_FORMAT, name, value<uintx>());219break;220case OptionType::Bool:221tty->print_cr(" bool %s = %s", name, value<bool>() ? "true" : "false");222break;223case OptionType::Double:224tty->print_cr(" double %s = %f", name, value<double>());225break;226case OptionType::Ccstr:227case OptionType::Ccstrlist:228tty->print_cr(" const char* %s = '%s'", name, value<ccstr>());229break;230default:231ShouldNotReachHere();232}233}234235void TypedMethodOptionMatcher::print_all() {236print();237if (_next != NULL) {238tty->print(" ");239_next->print_all();240}241}242243TypedMethodOptionMatcher* TypedMethodOptionMatcher::clone() {244TypedMethodOptionMatcher* m = new TypedMethodOptionMatcher();245m->_class_mode = _class_mode;246m->_class_name = _class_name;247m->_method_mode = _method_mode;248m->_method_name = _method_name;249m->_signature = _signature;250// Need to ref count the symbols251if (_class_name != NULL) {252_class_name->increment_refcount();253}254if (_method_name != NULL) {255_method_name->increment_refcount();256}257if (_signature != NULL) {258_signature->increment_refcount();259}260return m;261}262263TypedMethodOptionMatcher::~TypedMethodOptionMatcher() {264enum OptionType type = option2type(_option);265if (type == OptionType::Ccstr || type == OptionType::Ccstrlist) {266ccstr v = value<ccstr>();267os::free((void*)v);268}269}270271TypedMethodOptionMatcher* TypedMethodOptionMatcher::parse_method_pattern(char*& line, char* errorbuf, const int buf_size) {272assert(*errorbuf == '\0', "Dont call here with error_msg already set");273const char* error_msg = NULL;274TypedMethodOptionMatcher* tom = new TypedMethodOptionMatcher();275MethodMatcher::parse_method_pattern(line, error_msg, tom);276if (error_msg != NULL) {277jio_snprintf(errorbuf, buf_size, error_msg);278delete tom;279return NULL;280}281return tom;282}283284TypedMethodOptionMatcher* TypedMethodOptionMatcher::match(const methodHandle& method, enum CompileCommand option) {285TypedMethodOptionMatcher* current = this;286while (current != NULL) {287if (current->_option == option) {288if (current->matches(method)) {289return current;290}291}292current = current->next();293}294return NULL;295}296297template<typename T>298static void register_command(TypedMethodOptionMatcher* matcher,299enum CompileCommand option,300T value) {301assert(matcher != option_list, "No circular lists please");302if (option == CompileCommand::Log && !LogCompilation) {303tty->print_cr("Warning: +LogCompilation must be enabled in order for individual methods to be logged with ");304tty->print_cr(" CompileCommand=log,<method pattern>");305}306assert(CompilerOracle::option_matches_type(option, value), "Value must match option type");307308if (option == CompileCommand::Blackhole && !UnlockExperimentalVMOptions) {309warning("Blackhole compile option is experimental and must be enabled via -XX:+UnlockExperimentalVMOptions");310return;311}312313matcher->init(option, option_list);314matcher->set_value<T>(value);315option_list = matcher;316command_set_in_filter(option);317318if (!CompilerOracle::be_quiet()) {319// Print out the successful registration of a compile command320ttyLocker ttyl;321tty->print("CompileCommand: %s ", option2name(option));322matcher->print();323}324return;325}326327template<typename T>328bool CompilerOracle::has_option_value(const methodHandle& method, enum CompileCommand option, T& value) {329assert(option_matches_type(option, value), "Value must match option type");330if (!has_command(option)) {331return false;332}333if (option_list != NULL) {334TypedMethodOptionMatcher* m = option_list->match(method, option);335if (m != NULL) {336value = m->value<T>();337return true;338}339}340return false;341}342343static bool check_predicate(enum CompileCommand option, const methodHandle& method) {344bool value = false;345if (CompilerOracle::has_option_value(method, option, value)) {346return value;347}348return false;349}350351bool CompilerOracle::has_any_command_set() {352return any_set;353}354355// Explicit instantiation for all OptionTypes supported.356template bool CompilerOracle::has_option_value<intx>(const methodHandle& method, enum CompileCommand option, intx& value);357template bool CompilerOracle::has_option_value<uintx>(const methodHandle& method, enum CompileCommand option, uintx& value);358template bool CompilerOracle::has_option_value<bool>(const methodHandle& method, enum CompileCommand option, bool& value);359template bool CompilerOracle::has_option_value<ccstr>(const methodHandle& method, enum CompileCommand option, ccstr& value);360template bool CompilerOracle::has_option_value<double>(const methodHandle& method, enum CompileCommand option, double& value);361362template<typename T>363bool CompilerOracle::option_matches_type(enum CompileCommand option, T& value) {364enum OptionType option_type = option2type(option);365if (option_type == OptionType::Unknown) {366return false; // Can't query options with type Unknown.367}368if (option_type == OptionType::Ccstrlist) {369option_type = OptionType::Ccstr; // CCstrList type options are stored as Ccstr370}371return (get_type_for<T>() == option_type);372}373374template bool CompilerOracle::option_matches_type<intx>(enum CompileCommand option, intx& value);375template bool CompilerOracle::option_matches_type<uintx>(enum CompileCommand option, uintx& value);376template bool CompilerOracle::option_matches_type<bool>(enum CompileCommand option, bool& value);377template bool CompilerOracle::option_matches_type<ccstr>(enum CompileCommand option, ccstr& value);378template bool CompilerOracle::option_matches_type<double>(enum CompileCommand option, double& value);379380bool CompilerOracle::has_option(const methodHandle& method, enum CompileCommand option) {381bool value = false;382has_option_value(method, option, value);383return value;384}385386bool CompilerOracle::should_exclude(const methodHandle& method) {387if (check_predicate(CompileCommand::Exclude, method)) {388return true;389}390if (has_command(CompileCommand::CompileOnly)) {391return !check_predicate(CompileCommand::CompileOnly, method);392}393return false;394}395396bool CompilerOracle::should_inline(const methodHandle& method) {397return (check_predicate(CompileCommand::Inline, method));398}399400bool CompilerOracle::should_not_inline(const methodHandle& method) {401return check_predicate(CompileCommand::DontInline, method) || check_predicate(CompileCommand::Exclude, method);402}403404bool CompilerOracle::should_print(const methodHandle& method) {405return check_predicate(CompileCommand::Print, method);406}407408bool CompilerOracle::should_print_methods() {409return has_command(CompileCommand::Print);410}411412bool CompilerOracle::should_log(const methodHandle& method) {413if (!LogCompilation) return false;414if (!has_command(CompileCommand::Log)) {415return true; // by default, log all416}417return (check_predicate(CompileCommand::Log, method));418}419420bool CompilerOracle::should_break_at(const methodHandle& method) {421return check_predicate(CompileCommand::Break, method);422}423424void CompilerOracle::tag_blackhole_if_possible(const methodHandle& method) {425if (!check_predicate(CompileCommand::Blackhole, method)) {426return;427}428guarantee(UnlockExperimentalVMOptions, "Checked during initial parsing");429if (method->result_type() != T_VOID) {430warning("Blackhole compile option only works for methods with void type: %s",431method->name_and_sig_as_C_string());432return;433}434if (!method->is_empty_method()) {435warning("Blackhole compile option only works for empty methods: %s",436method->name_and_sig_as_C_string());437return;438}439if (!method->is_static()) {440warning("Blackhole compile option only works for static methods: %s",441method->name_and_sig_as_C_string());442return;443}444if (method->intrinsic_id() == vmIntrinsics::_blackhole) {445return;446}447if (method->intrinsic_id() != vmIntrinsics::_none) {448warning("Blackhole compile option only works for methods that do not have intrinsic set: %s, %s",449method->name_and_sig_as_C_string(), vmIntrinsics::name_at(method->intrinsic_id()));450return;451}452method->set_intrinsic_id(vmIntrinsics::_blackhole);453}454455static enum CompileCommand match_option_name(const char* line, int* bytes_read, char* errorbuf, int bufsize) {456assert(ARRAY_SIZE(option_names) == static_cast<int>(CompileCommand::Count), "option_names size mismatch");457458*bytes_read = 0;459char option_buf[256];460int matches = sscanf(line, "%255[a-zA-Z0-9]%n", option_buf, bytes_read);461if (matches > 0 && strcasecmp(option_buf, "unknown") != 0) {462for (uint i = 0; i < ARRAY_SIZE(option_names); i++) {463if (strcasecmp(option_buf, option_names[i]) == 0) {464return static_cast<enum CompileCommand>(i);465}466}467}468jio_snprintf(errorbuf, bufsize, "Unrecognized option '%s'", option_buf);469return CompileCommand::Unknown;470}471472// match exactly and don't mess with errorbuf473enum CompileCommand CompilerOracle::parse_option_name(const char* line) {474for (uint i = 0; i < ARRAY_SIZE(option_names); i++) {475if (strcasecmp(line, option_names[i]) == 0) {476return static_cast<enum CompileCommand>(i);477}478}479return CompileCommand::Unknown;480}481482enum OptionType CompilerOracle::parse_option_type(const char* type_str) {483for (uint i = 0; i < ARRAY_SIZE(optiontype_names); i++) {484if (strcasecmp(type_str, optiontype_names[i]) == 0) {485return static_cast<enum OptionType>(i);486}487}488return OptionType::Unknown;489}490491void print_tip() { // CMH Update info492tty->cr();493tty->print_cr("Usage: '-XX:CompileCommand=<option>,<method pattern>' - to set boolean option to true");494tty->print_cr("Usage: '-XX:CompileCommand=<option>,<method pattern>,<value>'");495tty->print_cr("Use: '-XX:CompileCommand=help' for more information and to list all option.");496tty->cr();497}498499void print_option(enum CompileCommand option, const char* name, enum OptionType type) {500if (type != OptionType::Unknown) {501tty->print_cr(" %s (%s)", name, optiontype2name(type));502}503}504505void print_commands() {506tty->cr();507tty->print_cr("All available options:");508#define enum_of_options(option, name, ctype) print_option(CompileCommand::option, name, OptionType::ctype);509COMPILECOMMAND_OPTIONS(enum_of_options)510#undef enum_of_options511tty->cr();512}513514static void usage() {515tty->cr();516tty->print_cr("The CompileCommand option enables the user of the JVM to control specific");517tty->print_cr("behavior of the dynamic compilers.");518tty->cr();519tty->print_cr("Compile commands has this general form:");520tty->print_cr("-XX:CompileCommand=<option><method pattern><value>");521tty->print_cr(" Sets <option> to the specified value for methods matching <method pattern>");522tty->print_cr(" All options are typed");523tty->cr();524tty->print_cr("-XX:CompileCommand=<option><method pattern>");525tty->print_cr(" Sets <option> to true for methods matching <method pattern>");526tty->print_cr(" Only applies to boolean options.");527tty->cr();528tty->print_cr("-XX:CompileCommand=quiet");529tty->print_cr(" Silence the compile command output");530tty->cr();531tty->print_cr("-XX:CompileCommand=help");532tty->print_cr(" Prints this help text");533tty->cr();534print_commands();535tty->cr();536tty->print_cr("Method patterns has the format:");537tty->print_cr(" package/Class.method()");538tty->cr();539tty->print_cr("For backward compatibility this form is also allowed:");540tty->print_cr(" package.Class::method()");541tty->cr();542tty->print_cr("The signature can be separated by an optional whitespace or comma:");543tty->print_cr(" package/Class.method ()");544tty->cr();545tty->print_cr("The class and method identifier can be used together with leading or");546tty->print_cr("trailing *'s for wildcard matching:");547tty->print_cr(" *ackage/Clas*.*etho*()");548tty->cr();549tty->print_cr("It is possible to use more than one CompileCommand on the command line:");550tty->print_cr(" -XX:CompileCommand=exclude,java/*.* -XX:CompileCommand=log,java*.*");551tty->cr();552tty->print_cr("The CompileCommands can be loaded from a file with the flag");553tty->print_cr("-XX:CompileCommandFile=<file> or be added to the file '.hotspot_compiler'");554tty->print_cr("Use the same format in the file as the argument to the CompileCommand flag.");555tty->print_cr("Add one command on each line.");556tty->print_cr(" exclude java/*.*");557tty->print_cr(" option java/*.* ReplayInline");558tty->cr();559tty->print_cr("The following commands have conflicting behavior: 'exclude', 'inline', 'dontinline',");560tty->print_cr("and 'compileonly'. There is no priority of commands. Applying (a subset of) these");561tty->print_cr("commands to the same method results in undefined behavior.");562tty->cr();563};564565int skip_whitespace(char* &line) {566// Skip any leading spaces567int whitespace_read = 0;568sscanf(line, "%*[ \t]%n", &whitespace_read);569line += whitespace_read;570return whitespace_read;571}572573void skip_comma(char* &line) {574// Skip any leading spaces575if (*line == ',') {576line++;577}578}579580static void scan_value(enum OptionType type, char* line, int& total_bytes_read,581TypedMethodOptionMatcher* matcher, enum CompileCommand option, char* errorbuf, const int buf_size) {582int bytes_read = 0;583const char* ccname = option2name(option);584const char* type_str = optiontype2name(type);585int skipped = skip_whitespace(line);586total_bytes_read += skipped;587if (type == OptionType::Intx) {588intx value;589if (sscanf(line, "" INTX_FORMAT "%n", &value, &bytes_read) == 1) {590total_bytes_read += bytes_read;591line += bytes_read;592register_command(matcher, option, value);593return;594} else {595jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);596}597} else if (type == OptionType::Uintx) {598uintx value;599if (sscanf(line, "" UINTX_FORMAT "%n", &value, &bytes_read) == 1) {600total_bytes_read += bytes_read;601line += bytes_read;602register_command(matcher, option, value);603return;604} else {605jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);606}607} else if (type == OptionType::Ccstr) {608ResourceMark rm;609char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);610if (sscanf(line, "%255[_a-zA-Z0-9]%n", value, &bytes_read) == 1) {611total_bytes_read += bytes_read;612line += bytes_read;613register_command(matcher, option, (ccstr) value);614return;615} else {616jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);617}618} else if (type == OptionType::Ccstrlist) {619// Accumulates several strings into one. The internal type is ccstr.620ResourceMark rm;621char* value = NEW_RESOURCE_ARRAY(char, strlen(line) + 1);622char* next_value = value;623if (sscanf(line, "%255[_a-zA-Z0-9+\\-]%n", next_value, &bytes_read) == 1) {624total_bytes_read += bytes_read;625line += bytes_read;626next_value += bytes_read + 1;627char* end_value = next_value - 1;628while (sscanf(line, "%*[ \t]%255[_a-zA-Z0-9+\\-]%n", next_value, &bytes_read) == 1) {629total_bytes_read += bytes_read;630line += bytes_read;631*end_value = ' '; // override '\0'632next_value += bytes_read;633end_value = next_value-1;634}635636if (option == CompileCommand::ControlIntrinsic || option == CompileCommand::DisableIntrinsic) {637ControlIntrinsicValidator validator(value, (option == CompileCommand::DisableIntrinsic));638639if (!validator.is_valid()) {640jio_snprintf(errorbuf, buf_size, "Unrecognized intrinsic detected in %s: %s", option2name(option), validator.what());641}642}643644register_command(matcher, option, (ccstr) value);645return;646} else {647jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);648}649} else if (type == OptionType::Bool) {650char value[256];651if (*line == '\0') {652// Short version of a CompileCommand sets a boolean Option to true653// -XXCompileCommand=<Option>,<method pattern>654register_command(matcher, option, true);655return;656}657if (sscanf(line, "%255[a-zA-Z]%n", value, &bytes_read) == 1) {658if (strcasecmp(value, "true") == 0) {659total_bytes_read += bytes_read;660line += bytes_read;661register_command(matcher, option, true);662return;663} else if (strcasecmp(value, "false") == 0) {664total_bytes_read += bytes_read;665line += bytes_read;666register_command(matcher, option, false);667return;668} else {669jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);670}671} else {672jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);673}674} else if (type == OptionType::Double) {675char buffer[2][256];676// Decimal separator '.' has been replaced with ' ' or '/' earlier,677// so read integer and fraction part of double value separately.678if (sscanf(line, "%255[0-9]%*[ /\t]%255[0-9]%n", buffer[0], buffer[1], &bytes_read) == 2) {679char value[512] = "";680jio_snprintf(value, sizeof(value), "%s.%s", buffer[0], buffer[1]);681total_bytes_read += bytes_read;682line += bytes_read;683register_command(matcher, option, atof(value));684return;685} else {686jio_snprintf(errorbuf, buf_size, "Value cannot be read for option '%s' of type '%s'", ccname, type_str);687}688} else {689jio_snprintf(errorbuf, buf_size, "Type '%s' not supported ", type_str);690}691}692693// Scan next option and value in line, return MethodMatcher object on success, NULL on failure.694// On failure, error_msg contains description for the first error.695// For future extensions: set error_msg on first error.696static void scan_option_and_value(enum OptionType type, char* line, int& total_bytes_read,697TypedMethodOptionMatcher* matcher,698char* errorbuf, const int buf_size) {699total_bytes_read = 0;700int bytes_read = 0;701char option_buf[256];702703// Read option name.704if (sscanf(line, "%*[ \t]%255[a-zA-Z0-9]%n", option_buf, &bytes_read) == 1) {705line += bytes_read;706total_bytes_read += bytes_read;707int bytes_read2 = 0;708total_bytes_read += skip_whitespace(line);709enum CompileCommand option = match_option_name(option_buf, &bytes_read2, errorbuf, buf_size);710if (option == CompileCommand::Unknown) {711assert(*errorbuf != '\0', "error must have been set");712return;713}714enum OptionType optiontype = option2type(option);715if (option2type(option) != type) {716const char* optiontype_name = optiontype2name(optiontype);717const char* type_name = optiontype2name(type);718jio_snprintf(errorbuf, buf_size, "Option '%s' with type '%s' doesn't match supplied type '%s'", option_buf, optiontype_name, type_name);719return;720}721scan_value(type, line, total_bytes_read, matcher, option, errorbuf, buf_size);722} else {723const char* type_str = optiontype2name(type);724jio_snprintf(errorbuf, buf_size, "Option name for type '%s' should be alphanumeric ", type_str);725}726return;727}728729void CompilerOracle::print_parse_error(char* error_msg, char* original_line) {730assert(*error_msg != '\0', "Must have error_message");731ttyLocker ttyl;732tty->print_cr("CompileCommand: An error occurred during parsing");733tty->print_cr("Error: %s", error_msg);734tty->print_cr("Line: '%s'", original_line);735print_tip();736}737738class LineCopy : StackObj {739const char* _copy;740public:741LineCopy(char* line) {742_copy = os::strdup(line, mtInternal);743}744~LineCopy() {745os::free((void*)_copy);746}747char* get() {748return (char*)_copy;749}750};751752void CompilerOracle::parse_from_line(char* line) {753if (line[0] == '\0') return;754if (line[0] == '#') return;755756LineCopy original(line);757int bytes_read;758char error_buf[1024] = {0};759760enum CompileCommand option = match_option_name(line, &bytes_read, error_buf, sizeof(error_buf));761line += bytes_read;762ResourceMark rm;763764if (option == CompileCommand::Unknown) {765print_parse_error(error_buf, original.get());766return;767}768769if (option == CompileCommand::Quiet) {770_quiet = true;771return;772}773774if (option == CompileCommand::Help) {775usage();776return;777}778779if (option == CompileCommand::Option) {780// Look for trailing options.781//782// Two types of trailing options are783// supported:784//785// (1) CompileCommand=option,Klass::method,option786// (2) CompileCommand=option,Klass::method,type,option,value787//788// Type (1) is used to enable a boolean option for a method.789//790// Type (2) is used to support options with a value. Values can have the791// the following types: intx, uintx, bool, ccstr, ccstrlist, and double.792793char option_type[256]; // stores option for Type (1) and type of Type (2)794skip_comma(line);795TypedMethodOptionMatcher* archetype = TypedMethodOptionMatcher::parse_method_pattern(line, error_buf, sizeof(error_buf));796if (archetype == NULL) {797print_parse_error(error_buf, original.get());798return;799}800801skip_whitespace(line);802803// This is unnecessarily complex. Should retire multi-option lines and skip while loop804while (sscanf(line, "%255[a-zA-Z0-9]%n", option_type, &bytes_read) == 1) {805line += bytes_read;806807// typed_matcher is used as a blueprint for each option, deleted at the end808TypedMethodOptionMatcher* typed_matcher = archetype->clone();809enum OptionType type = parse_option_type(option_type);810if (type != OptionType::Unknown) {811// Type (2) option: parse option name and value.812scan_option_and_value(type, line, bytes_read, typed_matcher, error_buf, sizeof(error_buf));813if (*error_buf != '\0') {814print_parse_error(error_buf, original.get());815return;816}817line += bytes_read;818} else {819// Type (1) option - option_type contains the option name -> bool value = true is implied820int bytes_read;821enum CompileCommand option = match_option_name(option_type, &bytes_read, error_buf, sizeof(error_buf));822if (option == CompileCommand::Unknown) {823print_parse_error(error_buf, original.get());824return;825}826if (option2type(option) == OptionType::Bool) {827register_command(typed_matcher, option, true);828} else {829jio_snprintf(error_buf, sizeof(error_buf), " Missing type '%s' before option '%s'",830optiontype2name(option2type(option)), option2name(option));831print_parse_error(error_buf, original.get());832return;833}834}835assert(typed_matcher != NULL, "sanity");836assert(*error_buf == '\0', "No error here");837skip_whitespace(line);838} // while(839delete archetype;840} else { // not an OptionCommand841// Command has the following form:842// CompileCommand=<option>,<method pattern><value>843// CompileCommand=<option>,<method pattern> (implies option is bool and value is true)844assert(*error_buf == '\0', "Don't call here with error_buf already set");845enum OptionType type = option2type(option);846int bytes_read = 0;847skip_comma(line);848TypedMethodOptionMatcher* matcher = TypedMethodOptionMatcher::parse_method_pattern(line, error_buf, sizeof(error_buf));849if (matcher == NULL) {850print_parse_error(error_buf, original.get());851return;852}853skip_whitespace(line);854if (*line == '\0') {855// if this is a bool option this implies true856if (option2type(option) == OptionType::Bool) {857register_command(matcher, option, true);858return;859} else {860jio_snprintf(error_buf, sizeof(error_buf), " Option '%s' is not followed by a value", option2name(option));861print_parse_error(error_buf, original.get());862return;863}864}865scan_value(type, line, bytes_read, matcher, option, error_buf, sizeof(error_buf));866if (*error_buf != '\0') {867print_parse_error(error_buf, original.get());868return;869}870assert(matcher != NULL, "consistency");871}872}873874static const char* default_cc_file = ".hotspot_compiler";875876static const char* cc_file() {877#ifdef ASSERT878if (CompileCommandFile == NULL)879return default_cc_file;880#endif881return CompileCommandFile;882}883884bool CompilerOracle::has_command_file() {885return cc_file() != NULL;886}887888bool CompilerOracle::_quiet = false;889890void CompilerOracle::parse_from_file() {891assert(has_command_file(), "command file must be specified");892FILE* stream = fopen(cc_file(), "rt");893if (stream == NULL) return;894895char token[1024];896int pos = 0;897int c = getc(stream);898while(c != EOF && pos < (int)(sizeof(token)-1)) {899if (c == '\n') {900token[pos++] = '\0';901parse_from_line(token);902pos = 0;903} else {904token[pos++] = c;905}906c = getc(stream);907}908token[pos++] = '\0';909parse_from_line(token);910911fclose(stream);912}913914void CompilerOracle::parse_from_string(const char* str, void (*parse_line)(char*)) {915char token[1024];916int pos = 0;917const char* sp = str;918int c = *sp++;919while (c != '\0' && pos < (int)(sizeof(token)-1)) {920if (c == '\n') {921token[pos++] = '\0';922parse_line(token);923pos = 0;924} else {925token[pos++] = c;926}927c = *sp++;928}929token[pos++] = '\0';930parse_line(token);931}932933void compilerOracle_init() {934CompilerOracle::parse_from_string(CompileCommand, CompilerOracle::parse_from_line);935CompilerOracle::parse_from_string(CompileOnly, CompilerOracle::parse_compile_only);936if (CompilerOracle::has_command_file()) {937CompilerOracle::parse_from_file();938} else {939struct stat buf;940if (os::stat(default_cc_file, &buf) == 0) {941warning("%s file is present but has been ignored. "942"Run with -XX:CompileCommandFile=%s to load the file.",943default_cc_file, default_cc_file);944}945}946if (has_command(CompileCommand::Print)) {947if (PrintAssembly) {948warning("CompileCommand and/or %s file contains 'print' commands, but PrintAssembly is also enabled", default_cc_file);949}950}951}952953void CompilerOracle::parse_compile_only(char* line) {954int i;955char name[1024];956const char* className = NULL;957const char* methodName = NULL;958959bool have_colon = (strstr(line, "::") != NULL);960char method_sep = have_colon ? ':' : '.';961962if (Verbose) {963tty->print_cr("%s", line);964}965966ResourceMark rm;967while (*line != '\0') {968MethodMatcher::Mode c_match = MethodMatcher::Exact;969MethodMatcher::Mode m_match = MethodMatcher::Exact;970971for (i = 0;972i < 1024 && *line != '\0' && *line != method_sep && *line != ',' && !isspace(*line);973line++, i++) {974name[i] = *line;975if (name[i] == '.') name[i] = '/'; // package prefix uses '/'976}977978if (i > 0) {979char* newName = NEW_RESOURCE_ARRAY( char, i + 1);980if (newName == NULL)981return;982strncpy(newName, name, i);983newName[i] = '\0';984985if (className == NULL) {986className = newName;987} else {988methodName = newName;989}990}991992if (*line == method_sep) {993if (className == NULL) {994className = "";995c_match = MethodMatcher::Any;996}997} else {998// got foo or foo/bar999if (className == NULL) {1000ShouldNotReachHere();1001} else {1002// missing class name handled as "Any" class match1003if (className[0] == '\0') {1004c_match = MethodMatcher::Any;1005}1006}1007}10081009// each directive is terminated by , or NUL or . followed by NUL1010if (*line == ',' || *line == '\0' || (line[0] == '.' && line[1] == '\0')) {1011if (methodName == NULL) {1012methodName = "";1013if (*line != method_sep) {1014m_match = MethodMatcher::Any;1015}1016}10171018EXCEPTION_MARK;1019Symbol* c_name = SymbolTable::new_symbol(className);1020Symbol* m_name = SymbolTable::new_symbol(methodName);1021Symbol* signature = NULL;10221023TypedMethodOptionMatcher* tom = new TypedMethodOptionMatcher();1024tom->init_matcher(c_name, c_match, m_name, m_match, signature);1025register_command(tom, CompileCommand::CompileOnly, true);1026if (PrintVMOptions) {1027tty->print("CompileOnly: compileonly ");1028tom->print();1029}10301031className = NULL;1032methodName = NULL;1033}10341035line = *line == '\0' ? line : line + 1;1036}1037}10381039enum CompileCommand CompilerOracle::string_to_option(const char* name) {1040int bytes_read = 0;1041char errorbuf[1024] = {0};1042return match_option_name(name, &bytes_read, errorbuf, sizeof(errorbuf));1043}104410451046