Skip to content

Commit

Permalink
8319851: Improve exception logging
Browse files Browse the repository at this point in the history
Reviewed-by: andrew
Backport-of: 87dfeeb14fdd0fa1648a8bec91b5b713cc2c1b83
  • Loading branch information
martinuy authored and gnu-andrew committed Apr 7, 2024
1 parent 54419d4 commit ce255d6
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 38 deletions.
13 changes: 6 additions & 7 deletions hotspot/src/share/vm/classfile/javaClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,14 +1235,13 @@ oop java_lang_Throwable::message(Handle throwable) {
}


// Return Symbol for detailed_message or NULL
Symbol* java_lang_Throwable::detail_message(oop throwable) {
PRESERVE_EXCEPTION_MARK; // Keep original exception
oop detailed_message = java_lang_Throwable::message(throwable);
if (detailed_message != NULL) {
return java_lang_String::as_symbol(detailed_message, THREAD);
const char* java_lang_Throwable::message_as_utf8(oop throwable) {
oop msg = java_lang_Throwable::message(throwable);
const char* msg_utf8 = NULL;
if (msg != NULL) {
msg_utf8 = java_lang_String::as_utf8_string(msg);
}
return NULL;
return msg_utf8;
}

void java_lang_Throwable::set_message(oop throwable, oop value) {
Expand Down
4 changes: 2 additions & 2 deletions hotspot/src/share/vm/classfile/javaClasses.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,12 +523,12 @@ class java_lang_Throwable: AllStatic {
static void set_backtrace(oop throwable, oop value);
// Needed by JVMTI to filter out this internal field.
static int get_backtrace_offset() { return backtrace_offset;}
static int get_detailMessage_offset() { return detailMessage_offset;}
// Message
static int get_detailMessage_offset() { return detailMessage_offset;}
static oop message(oop throwable);
static oop message(Handle throwable);
static const char* message_as_utf8(oop throwable);
static void set_message(oop throwable, oop value);
static Symbol* detail_message(oop throwable);
static void print_stack_element(outputStream *st, Handle mirror, int method,
int version, int bci, int cpref);
static void print_stack_element(outputStream *st, methodHandle method, int bci);
Expand Down
14 changes: 7 additions & 7 deletions hotspot/src/share/vm/classfile/resolutionErrors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
// add new entry to the table
void ResolutionErrorTable::add_entry(int index, unsigned int hash,
constantPoolHandle pool, int cp_index,
Symbol* error, Symbol* message)
Symbol* error, const char* message)
{
assert_locked_or_safepoint(SystemDictionary_lock);
assert(!pool.is_null() && error != NULL, "adding NULL obj");
Expand Down Expand Up @@ -64,16 +64,14 @@ void ResolutionErrorEntry::set_error(Symbol* e) {
_error->increment_refcount();
}

void ResolutionErrorEntry::set_message(Symbol* c) {
assert(c != NULL, "must set a value");
_message = c;
_message->increment_refcount();
void ResolutionErrorEntry::set_message(const char* c) {
_message = c != NULL ? os::strdup(c) : NULL;
}

// create new error entry
ResolutionErrorEntry* ResolutionErrorTable::new_entry(int hash, ConstantPool* pool,
int cp_index, Symbol* error,
Symbol* message)
const char* message)
{
ResolutionErrorEntry* entry = (ResolutionErrorEntry*)Hashtable<ConstantPool*, mtClass>::new_entry(hash, pool);
entry->set_cp_index(cp_index);
Expand All @@ -87,7 +85,9 @@ void ResolutionErrorTable::free_entry(ResolutionErrorEntry *entry) {
// decrement error refcount
assert(entry->error() != NULL, "error should be set");
entry->error()->decrement_refcount();
entry->message()->decrement_refcount();
if (entry->message() != NULL) {
FREE_C_HEAP_ARRAY(char, entry->message(), mtInternal);
}
Hashtable<ConstantPool*, mtClass>::free_entry(entry);
}

Expand Down
11 changes: 6 additions & 5 deletions hotspot/src/share/vm/classfile/resolutionErrors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ResolutionErrorTable : public Hashtable<ConstantPool*, mtClass> {
ResolutionErrorTable(int table_size);

ResolutionErrorEntry* new_entry(int hash, ConstantPool* pool, int cp_index,
Symbol* error, Symbol* message);
Symbol* error, const char* message);
void free_entry(ResolutionErrorEntry *entry);

ResolutionErrorEntry* bucket(int i) {
Expand All @@ -56,7 +56,7 @@ class ResolutionErrorTable : public Hashtable<ConstantPool*, mtClass> {
}

void add_entry(int index, unsigned int hash,
constantPoolHandle pool, int which, Symbol* error, Symbol* message);
constantPoolHandle pool, int cp_index, Symbol* error, const char* error_msg);


// find error given the constant pool and constant pool index
Expand All @@ -80,7 +80,7 @@ class ResolutionErrorEntry : public HashtableEntry<ConstantPool*, mtClass> {
private:
int _cp_index;
Symbol* _error;
Symbol* _message;
const char* _message;

public:
ConstantPool* pool() const { return literal(); }
Expand All @@ -91,8 +91,9 @@ class ResolutionErrorEntry : public HashtableEntry<ConstantPool*, mtClass> {
Symbol* error() const { return _error; }
void set_error(Symbol* e);

Symbol* message() const { return _message; }
void set_message(Symbol* c);
const char* message() const { return _message; }
// The incoming message is copied to the C-Heap.
void set_message(const char* c);

ResolutionErrorEntry* next() const {
return (ResolutionErrorEntry*)HashtableEntry<ConstantPool*, mtClass>::next();
Expand Down
4 changes: 2 additions & 2 deletions hotspot/src/share/vm/classfile/systemDictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2280,7 +2280,7 @@ bool SystemDictionary::add_loader_constraint(Symbol* class_name,
// Add entry to resolution error table to record the error when the first
// attempt to resolve a reference to a class has failed.
void SystemDictionary::add_resolution_error(constantPoolHandle pool, int which,
Symbol* error, Symbol* message) {
Symbol* error, const char* message) {
unsigned int hash = resolution_errors()->compute_hash(pool, which);
int index = resolution_errors()->hash_to_index(hash);
{
Expand All @@ -2296,7 +2296,7 @@ void SystemDictionary::delete_resolution_error(ConstantPool* pool) {

// Lookup resolution error table. Returns error if found, otherwise NULL.
Symbol* SystemDictionary::find_resolution_error(constantPoolHandle pool, int which,
Symbol** message) {
const char** message) {
unsigned int hash = resolution_errors()->compute_hash(pool, which);
int index = resolution_errors()->hash_to_index(hash);
{
Expand Down
4 changes: 2 additions & 2 deletions hotspot/src/share/vm/classfile/systemDictionary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,10 @@ class SystemDictionary : AllStatic {
// Record the error when the first attempt to resolve a reference from a constant
// pool entry to a class fails.
static void add_resolution_error(constantPoolHandle pool, int which, Symbol* error,
Symbol* message);
const char* message);
static void delete_resolution_error(ConstantPool* pool);
static Symbol* find_resolution_error(constantPoolHandle pool, int which,
Symbol** message);
const char** message);

protected:

Expand Down
6 changes: 3 additions & 3 deletions hotspot/src/share/vm/interpreter/interpreterRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,11 @@ IRT_ENTRY(address, InterpreterRuntime::exception_handler_for_exception(JavaThrea
// tracing
if (TraceExceptions) {
ResourceMark rm(thread);
Symbol* message = java_lang_Throwable::detail_message(h_exception());
const char* detail_message = java_lang_Throwable::message_as_utf8(h_exception());
ttyLocker ttyl; // Lock after getting the detail message
if (message != NULL) {
if (detail_message != NULL) {
tty->print_cr("Exception <%s: %s> (" INTPTR_FORMAT ")",
h_exception->print_value_string(), message->as_C_string(),
h_exception->print_value_string(), detail_message,
(address)h_exception());
} else {
tty->print_cr("Exception <%s> (" INTPTR_FORMAT ")",
Expand Down
23 changes: 14 additions & 9 deletions hotspot/src/share/vm/oops/constantPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,13 +569,16 @@ bool ConstantPool::resolve_class_constants(TRAPS) {
return true;
}

Symbol* ConstantPool::exception_message(constantPoolHandle this_oop, int which, constantTag tag, oop pending_exception) {
const char* ConstantPool::exception_message(constantPoolHandle this_oop, int which, constantTag tag, oop pending_exception) {
// Note: caller needs ResourceMark

// Dig out the detailed message to reuse if possible
Symbol* message = java_lang_Throwable::detail_message(pending_exception);
if (message != NULL) {
return message;
const char* msg = java_lang_Throwable::message_as_utf8(pending_exception);
if (msg != NULL) {
return msg;
}

Symbol* message = NULL;
// Return specific message for the tag
switch (tag.value()) {
case JVM_CONSTANT_UnresolvedClass:
Expand All @@ -594,16 +597,16 @@ Symbol* ConstantPool::exception_message(constantPoolHandle this_oop, int which,
ShouldNotReachHere();
}

return message;
return message != NULL ? message->as_C_string() : NULL;
}

void ConstantPool::throw_resolution_error(constantPoolHandle this_oop, int which, TRAPS) {
Symbol* message = NULL;
ResourceMark rm(THREAD);
const char* message = NULL;
Symbol* error = SystemDictionary::find_resolution_error(this_oop, which, &message);
assert(error != NULL && message != NULL, "checking");
CLEAR_PENDING_EXCEPTION;
ResourceMark rm;
THROW_MSG(error, message->as_C_string());
THROW_MSG(error, message);
}

// If resolution for Class, MethodHandle or MethodType fails, save the exception
Expand All @@ -622,7 +625,9 @@ void ConstantPool::save_and_throw_exception(constantPoolHandle this_oop, int whi
// and OutOfMemoryError, etc, or if the thread was hit by stop()
// Needs clarification to section 5.4.3 of the VM spec (see 6308271)
} else if (this_oop->tag_at(which).value() != error_tag) {
Symbol* message = exception_message(this_oop, which, tag, PENDING_EXCEPTION);
ResourceMark rm(THREAD);

const char* message = exception_message(this_oop, which, tag, PENDING_EXCEPTION);
SystemDictionary::add_resolution_error(this_oop, which, error, message);
this_oop->tag_at_put(which, error_tag);
} else {
Expand Down
2 changes: 1 addition & 1 deletion hotspot/src/share/vm/oops/constantPool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ class ConstantPool : public Metadata {

// Exception handling
static void throw_resolution_error(constantPoolHandle this_oop, int which, TRAPS);
static Symbol* exception_message(constantPoolHandle this_oop, int which, constantTag tag, oop pending_exception);
static const char* exception_message(constantPoolHandle this_oop, int which, constantTag tag, oop pending_exception);
static void save_and_throw_exception(constantPoolHandle this_oop, int which, constantTag tag, TRAPS);

public:
Expand Down

0 comments on commit ce255d6

Please sign in to comment.