# HG changeset patch # User Gilles Duboscq # Date 1431010774 -7200 # Node ID fe4a77bec5b7913d8d0167a05ed21a796bcd7969 # Parent 1ab7802d35c9e2b187612777ec7005bf0dc7aee3 Use GraalRuntime::parse_lines in GraalRuntime::parse_graal_options_file diff -r 1ab7802d35c9 -r fe4a77bec5b7 src/share/vm/graal/graalRuntime.cpp --- a/src/share/vm/graal/graalRuntime.cpp Thu May 07 16:00:42 2015 +0200 +++ b/src/share/vm/graal/graalRuntime.cpp Thu May 07 16:59:34 2015 +0200 @@ -866,51 +866,27 @@ } } +class GraalOptionParseClosure : public ParseClosure { + TRAPS; + KlassHandle _hotSpotOptionsClass; +public: + GraalOptionParseClosure(KlassHandle hotSpotOptionsClass, TRAPS) : THREAD(THREAD), _hotSpotOptionsClass(hotSpotOptionsClass) {} + void do_line(char* line) { + GraalRuntime::parse_argument(_hotSpotOptionsClass, line, THREAD); + if (HAS_PENDING_EXCEPTION) { + warn_and_abort("Exception thrown while parsing argument"); + } + } +}; + void GraalRuntime::parse_graal_options_file(KlassHandle hotSpotOptionsClass, TRAPS) { const char* home = Arguments::get_java_home(); size_t path_len = strlen(home) + strlen("/lib/graal.options") + 1; char* path = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, path_len); char sep = os::file_separator()[0]; sprintf(path, "%s%clib%cgraal.options", home, sep, sep); - - struct stat st; - if (os::stat(path, &st) == 0) { - int file_handle = os::open(path, 0, 0); - if (file_handle != -1) { - char* buffer = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, st.st_size); - int num_read = (int) os::read(file_handle, (char*) buffer, st.st_size); - if (num_read == -1) { - warning("Error reading file %s due to %s", path, strerror(errno)); - } else if (num_read != st.st_size) { - warning("Only read %d of " SIZE_FORMAT " bytes from %s", num_read, (size_t) st.st_size, path); - } - os::close(file_handle); - if (num_read == st.st_size) { - char* line = buffer; - int lineNo = 1; - while (line - buffer < num_read) { - char* nl = strchr(line, '\n'); - if (nl != NULL) { - *nl = '\0'; - } - parse_argument(hotSpotOptionsClass, line, THREAD); - if (HAS_PENDING_EXCEPTION) { - warning("Error in %s:%d", path, lineNo); - return; - } - if (nl != NULL) { - line = nl + 1; - lineNo++; - } else { - // File without newline at the end - break; - } - } - } - } else { - warning("Error opening file %s due to %s", path, strerror(errno)); - } - } + GraalOptionParseClosure closure(hotSpotOptionsClass, THREAD); + parse_lines(path, &closure, false, THREAD); } jlong GraalRuntime::parse_primitive_option_value(char spec, const char* name, size_t name_len, const char* value, TRAPS) { @@ -1076,7 +1052,7 @@ return klass; } -void GraalRuntime::parse_lines(char* path, ParseClosure* closure, TRAPS) { +void GraalRuntime::parse_lines(char* path, ParseClosure* closure, bool warnStatFailure, TRAPS) { struct stat st; if (os::stat(path, &st) == 0) { int file_handle = os::open(path, 0, 0); @@ -1089,11 +1065,12 @@ warning("Only read %d of " SIZE_FORMAT " bytes from %s", num_read, (size_t) st.st_size, path); } os::close(file_handle); + closure->set_filename(path); if (num_read == st.st_size) { buffer[num_read] = '\0'; char* line = buffer; - while (line - buffer < num_read) { + while (line - buffer < num_read && !closure->is_aborted()) { // find line end (\r, \n or \r\n) char* nextline = NULL; char* cr = strchr(line, '\r'); @@ -1121,7 +1098,7 @@ *end = '\0'; // skip comments and empty lines if (*line != '#' && strlen(line) > 0) { - closure->do_line(line); + closure->parse_line(line); } if (nextline != NULL) { line = nextline; @@ -1134,8 +1111,8 @@ } else { warning("Error opening file %s due to %s", path, strerror(errno)); } - } else { - warning("Error opening file %s due to %s", path, strerror(errno)); + } else if (warnStatFailure) { + warning("Could not stat file %s due to %s", path, strerror(errno)); } } @@ -1164,7 +1141,7 @@ char sep = os::file_separator()[0]; sprintf(path, "%s%clib%cgraal%cservices%c%s", home, sep, sep, sep, sep, serviceName); ServiceParseClosure closure; - parse_lines(path, &closure, THREAD); + parse_lines(path, &closure, true, THREAD); GrowableArray* implNames = closure.implNames(); objArrayOop servicesOop = oopFactory::new_objArray(serviceKlass(), implNames->length(), CHECK_NH); diff -r 1ab7802d35c9 -r fe4a77bec5b7 src/share/vm/graal/graalRuntime.hpp --- a/src/share/vm/graal/graalRuntime.hpp Thu May 07 16:00:42 2015 +0200 +++ b/src/share/vm/graal/graalRuntime.hpp Thu May 07 16:59:34 2015 +0200 @@ -29,8 +29,25 @@ #include "runtime/deoptimization.hpp" class ParseClosure : public StackObj { +protected: + int _lineNo; + char* _filename; + bool _abort; + void abort() { _abort = true; } + void warn_and_abort(const char* message) { + warning("Error at line %d while parsing %s: %s", _lineNo, _filename == NULL ? "?" : _filename, message); + abort(); + } public: + ParseClosure() : _lineNo(0), _filename(NULL) {} + void parse_line(char* line) { + _lineNo++; + do_line(line); + } virtual void do_line(char* line) = 0; + int lineNo() { return _lineNo; } + bool is_aborted() { return _abort; } + void set_filename(char* path) {_filename = path; } }; class GraalRuntime: public CHeapObj { @@ -69,13 +86,6 @@ static void parse_graal_options_file(KlassHandle hotSpotOptionsClass, TRAPS); /** - * Parses a given argument and sets the denoted Graal option. - * - * @throws InternalError if there was a problem parsing or setting the option - */ - static void parse_argument(KlassHandle hotSpotOptionsClass, char* arg, TRAPS); - - /** * Searches for a Boolean Graal option denoted by a given name and sets it value. * * The definition of this method is in graalRuntime.inline.hpp @@ -132,6 +142,13 @@ public: /** + * Parses a given argument and sets the denoted Graal option. + * + * @throws InternalError if there was a problem parsing or setting the option + */ + static void parse_argument(KlassHandle hotSpotOptionsClass, char* arg, TRAPS); + + /** * Ensures that the Graal class loader is initialized and the well known Graal classes are loaded. */ static void ensure_graal_class_loader_is_initialized(); @@ -182,7 +199,7 @@ */ static Handle get_service_impls(KlassHandle serviceKlass, TRAPS); - static void parse_lines(char* path, ParseClosure* closure, TRAPS); + static void parse_lines(char* path, ParseClosure* closure, bool warnStatFailure, TRAPS); /** * Aborts the VM due to an unexpected exception.