Skip to content

Commit 109e022

Browse files
martinuyRealCLanger
authored andcommitted
8319851: Improve exception logging
Reviewed-by: mbaesken Backport-of: 87dfeeb14fdd0fa1648a8bec91b5b713cc2c1b83
1 parent 07873cc commit 109e022

File tree

9 files changed

+72
-67
lines changed

9 files changed

+72
-67
lines changed

src/hotspot/share/classfile/javaClasses.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,18 +2076,17 @@ oop java_lang_Throwable::message(oop throwable) {
20762076
return throwable->obj_field(_detailMessage_offset);
20772077
}
20782078

2079-
oop java_lang_Throwable::cause(oop throwable) {
2080-
return throwable->obj_field(_cause_offset);
2079+
const char* java_lang_Throwable::message_as_utf8(oop throwable) {
2080+
oop msg = java_lang_Throwable::message(throwable);
2081+
const char* msg_utf8 = nullptr;
2082+
if (msg != nullptr) {
2083+
msg_utf8 = java_lang_String::as_utf8_string(msg);
2084+
}
2085+
return msg_utf8;
20812086
}
20822087

2083-
// Return Symbol for detailed_message or NULL
2084-
Symbol* java_lang_Throwable::detail_message(oop throwable) {
2085-
PreserveExceptionMark pm(Thread::current());
2086-
oop detailed_message = java_lang_Throwable::message(throwable);
2087-
if (detailed_message != NULL) {
2088-
return java_lang_String::as_symbol(detailed_message);
2089-
}
2090-
return NULL;
2088+
oop java_lang_Throwable::cause(oop throwable) {
2089+
return throwable->obj_field(_cause_offset);
20912090
}
20922091

20932092
void java_lang_Throwable::set_message(oop throwable, oop value) {
@@ -2739,15 +2738,19 @@ Handle java_lang_Throwable::create_initialization_error(JavaThread* current, Han
27392738
assert(throwable.not_null(), "shouldn't be");
27402739

27412740
// Now create the message from the original exception and thread name.
2742-
Symbol* message = java_lang_Throwable::detail_message(throwable());
27432741
ResourceMark rm(current);
27442742
stringStream st;
2743+
const char *message = nullptr;
2744+
oop detailed_message = java_lang_Throwable::message(throwable());
2745+
if (detailed_message != nullptr) {
2746+
message = java_lang_String::as_utf8_string(detailed_message);
2747+
}
27452748
st.print("Exception %s%s ", throwable()->klass()->name()->as_klass_external_name(),
27462749
message == nullptr ? "" : ":");
2747-
if (message == NULL) {
2750+
if (message == nullptr) {
27482751
st.print("[in thread \"%s\"]", current->name());
27492752
} else {
2750-
st.print("%s [in thread \"%s\"]", message->as_C_string(), current->name());
2753+
st.print("%s [in thread \"%s\"]", message, current->name());
27512754
}
27522755

27532756
Symbol* exception_name = vmSymbols::java_lang_ExceptionInInitializerError();

src/hotspot/share/classfile/javaClasses.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,12 +549,14 @@ class java_lang_Throwable: AllStatic {
549549
static void set_backtrace(oop throwable, oop value);
550550
static int depth(oop throwable);
551551
static void set_depth(oop throwable, int value);
552-
static int get_detailMessage_offset() { CHECK_INIT(_detailMessage_offset); }
553552
// Message
553+
static int get_detailMessage_offset() { CHECK_INIT(_detailMessage_offset); }
554554
static oop message(oop throwable);
555-
static oop cause(oop throwable);
555+
static const char* message_as_utf8(oop throwable);
556556
static void set_message(oop throwable, oop value);
557-
static Symbol* detail_message(oop throwable);
557+
558+
static oop cause(oop throwable);
559+
558560
static void print_stack_element(outputStream *st, Method* method, int bci);
559561

560562
static void compute_offsets();

src/hotspot/share/classfile/resolutionErrors.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
// create new error entry
3737
void ResolutionErrorTable::add_entry(int index, unsigned int hash,
3838
const constantPoolHandle& pool, int cp_index,
39-
Symbol* error, Symbol* message,
40-
Symbol* cause, Symbol* cause_msg)
39+
Symbol* error, const char* message,
40+
Symbol* cause, const char* cause_msg)
4141
{
4242
assert_locked_or_safepoint(SystemDictionary_lock);
4343
assert(!pool.is_null() && error != NULL, "adding NULL obj");
@@ -95,11 +95,8 @@ void ResolutionErrorEntry::set_error(Symbol* e) {
9595
}
9696
}
9797

98-
void ResolutionErrorEntry::set_message(Symbol* c) {
99-
_message = c;
100-
if (_message != NULL) {
101-
_message->increment_refcount();
102-
}
98+
void ResolutionErrorEntry::set_message(const char* c) {
99+
_message = c != nullptr ? os::strdup(c) : nullptr;
103100
}
104101

105102
void ResolutionErrorEntry::set_cause(Symbol* c) {
@@ -109,13 +106,11 @@ void ResolutionErrorEntry::set_cause(Symbol* c) {
109106
}
110107
}
111108

112-
void ResolutionErrorEntry::set_cause_msg(Symbol* c) {
113-
_cause_msg = c;
114-
if (_cause_msg != NULL) {
115-
_cause_msg->increment_refcount();
116-
}
109+
void ResolutionErrorEntry::set_cause_msg(const char* c) {
110+
_cause_msg = c != nullptr ? os::strdup(c) : nullptr;
117111
}
118112

113+
// The incoming nest host error message is already in the C-Heap.
119114
void ResolutionErrorEntry::set_nest_host_error(const char* message) {
120115
_nest_host_error = message;
121116
}
@@ -126,13 +121,13 @@ void ResolutionErrorTable::free_entry(ResolutionErrorEntry *entry) {
126121
entry->error()->decrement_refcount();
127122
}
128123
if (entry->message() != NULL) {
129-
entry->message()->decrement_refcount();
124+
FREE_C_HEAP_ARRAY(char, entry->message());
130125
}
131126
if (entry->cause() != NULL) {
132127
entry->cause()->decrement_refcount();
133128
}
134129
if (entry->cause_msg() != NULL) {
135-
entry->cause_msg()->decrement_refcount();
130+
FREE_C_HEAP_ARRAY(char, entry->cause_msg());
136131
}
137132
if (entry->nest_host_error() != NULL) {
138133
FREE_C_HEAP_ARRAY(char, entry->nest_host_error());

src/hotspot/share/classfile/resolutionErrors.hpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ class ResolutionErrorTable : public Hashtable<ConstantPool*, mtClass> {
6161
}
6262

6363
void add_entry(int index, unsigned int hash,
64-
const constantPoolHandle& pool, int which, Symbol* error, Symbol* message,
65-
Symbol* cause, Symbol* cause_msg);
64+
const constantPoolHandle& pool, int cp_index, Symbol* error, const char* error_msg,
65+
Symbol* cause, const char* cause_msg);
6666

6767
void add_entry(int index, unsigned int hash,
68-
const constantPoolHandle& pool, int which, const char* message);
68+
const constantPoolHandle& pool, int cp_index, const char* message);
6969

7070
// find error given the constant pool and constant pool index
7171
ResolutionErrorEntry* find_entry(int index, unsigned int hash,
@@ -96,9 +96,9 @@ class ResolutionErrorEntry : public HashtableEntry<ConstantPool*, mtClass> {
9696
private:
9797
int _cp_index;
9898
Symbol* _error;
99-
Symbol* _message;
99+
const char* _message;
100100
Symbol* _cause;
101-
Symbol* _cause_msg;
101+
const char* _cause_msg;
102102
const char* _nest_host_error;
103103

104104
public:
@@ -110,16 +110,19 @@ class ResolutionErrorEntry : public HashtableEntry<ConstantPool*, mtClass> {
110110
Symbol* error() const { return _error; }
111111
void set_error(Symbol* e);
112112

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

116117
Symbol* cause() const { return _cause; }
117118
void set_cause(Symbol* c);
118119

119-
Symbol* cause_msg() const { return _cause_msg; }
120-
void set_cause_msg(Symbol* c);
120+
const char* cause_msg() const { return _cause_msg; }
121+
// The incoming cause_msg is copied to the C-Heap.
122+
void set_cause_msg(const char* c);
121123

122124
const char* nest_host_error() const { return _nest_host_error; }
125+
// The incoming nest host error message is already in the C-Heap.
123126
void set_nest_host_error(const char* message);
124127

125128
ResolutionErrorEntry* next() const {

src/hotspot/share/classfile/systemDictionary.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,8 +1846,8 @@ bool SystemDictionary::add_loader_constraint(Symbol* class_name,
18461846
// Add entry to resolution error table to record the error when the first
18471847
// attempt to resolve a reference to a class has failed.
18481848
void SystemDictionary::add_resolution_error(const constantPoolHandle& pool, int which,
1849-
Symbol* error, Symbol* message,
1850-
Symbol* cause, Symbol* cause_msg) {
1849+
Symbol* error, const char* message,
1850+
Symbol* cause, const char* cause_msg) {
18511851
unsigned int hash = resolution_errors()->compute_hash(pool, which);
18521852
int index = resolution_errors()->hash_to_index(hash);
18531853
{
@@ -1866,7 +1866,7 @@ void SystemDictionary::delete_resolution_error(ConstantPool* pool) {
18661866

18671867
// Lookup resolution error table. Returns error if found, otherwise NULL.
18681868
Symbol* SystemDictionary::find_resolution_error(const constantPoolHandle& pool, int which,
1869-
Symbol** message, Symbol** cause, Symbol** cause_msg) {
1869+
const char** message, Symbol** cause, const char** cause_msg) {
18701870
unsigned int hash = resolution_errors()->compute_hash(pool, which);
18711871
int index = resolution_errors()->hash_to_index(hash);
18721872
{

src/hotspot/share/classfile/systemDictionary.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,10 @@ class SystemDictionary : AllStatic {
281281
// Record the error when the first attempt to resolve a reference from a constant
282282
// pool entry to a class fails.
283283
static void add_resolution_error(const constantPoolHandle& pool, int which, Symbol* error,
284-
Symbol* message, Symbol* cause = NULL, Symbol* cause_msg = NULL);
284+
const char* message, Symbol* cause = NULL, const char* cause_msg = NULL);
285285
static void delete_resolution_error(ConstantPool* pool);
286286
static Symbol* find_resolution_error(const constantPoolHandle& pool, int which,
287-
Symbol** message, Symbol** cause, Symbol** cause_msg);
287+
const char** message, Symbol** cause, const char** cause_msg);
288288

289289

290290
// Record a nest host resolution/validation error

src/hotspot/share/oops/constantPool.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -767,13 +767,16 @@ void ConstantPool::resolve_string_constants_impl(const constantPoolHandle& this_
767767
}
768768
}
769769

770-
static Symbol* exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) {
770+
static const char* exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) {
771+
// Note: caller needs ResourceMark
772+
771773
// Dig out the detailed message to reuse if possible
772-
Symbol* message = java_lang_Throwable::detail_message(pending_exception);
773-
if (message != NULL) {
774-
return message;
774+
const char* msg = java_lang_Throwable::message_as_utf8(pending_exception);
775+
if (msg != nullptr) {
776+
return msg;
775777
}
776778

779+
Symbol* message = nullptr;
777780
// Return specific message for the tag
778781
switch (tag.value()) {
779782
case JVM_CONSTANT_UnresolvedClass:
@@ -796,49 +799,48 @@ static Symbol* exception_message(const constantPoolHandle& this_cp, int which, c
796799
ShouldNotReachHere();
797800
}
798801

799-
return message;
802+
return message != nullptr ? message->as_C_string() : nullptr;
800803
}
801804

802-
static void add_resolution_error(const constantPoolHandle& this_cp, int which,
805+
static void add_resolution_error(JavaThread* current, const constantPoolHandle& this_cp, int which,
803806
constantTag tag, oop pending_exception) {
807+
ResourceMark rm(current);
804808

805809
Symbol* error = pending_exception->klass()->name();
806810
oop cause = java_lang_Throwable::cause(pending_exception);
807811

808812
// Also dig out the exception cause, if present.
809813
Symbol* cause_sym = NULL;
810-
Symbol* cause_msg = NULL;
814+
const char* cause_msg = nullptr;
811815
if (cause != NULL && cause != pending_exception) {
812816
cause_sym = cause->klass()->name();
813-
cause_msg = java_lang_Throwable::detail_message(cause);
817+
cause_msg = java_lang_Throwable::message_as_utf8(cause);
814818
}
815819

816-
Symbol* message = exception_message(this_cp, which, tag, pending_exception);
820+
const char* message = exception_message(this_cp, which, tag, pending_exception);
817821
SystemDictionary::add_resolution_error(this_cp, which, error, message, cause_sym, cause_msg);
818822
}
819823

820824

821825
void ConstantPool::throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS) {
822826
ResourceMark rm(THREAD);
823-
Symbol* message = NULL;
827+
const char* message = NULL;
824828
Symbol* cause = NULL;
825-
Symbol* cause_msg = NULL;
829+
const char* cause_msg = NULL;
826830
Symbol* error = SystemDictionary::find_resolution_error(this_cp, which, &message, &cause, &cause_msg);
827831
assert(error != NULL, "checking");
828-
const char* cause_str = cause_msg != NULL ? cause_msg->as_C_string() : NULL;
829832

830833
CLEAR_PENDING_EXCEPTION;
831834
if (message != NULL) {
832-
char* msg = message->as_C_string();
833835
if (cause != NULL) {
834-
Handle h_cause = Exceptions::new_exception(THREAD, cause, cause_str);
835-
THROW_MSG_CAUSE(error, msg, h_cause);
836+
Handle h_cause = Exceptions::new_exception(THREAD, cause, cause_msg);
837+
THROW_MSG_CAUSE(error, message, h_cause);
836838
} else {
837-
THROW_MSG(error, msg);
839+
THROW_MSG(error, message);
838840
}
839841
} else {
840842
if (cause != NULL) {
841-
Handle h_cause = Exceptions::new_exception(THREAD, cause, cause_str);
843+
Handle h_cause = Exceptions::new_exception(THREAD, cause, cause_msg);
842844
THROW_CAUSE(error, h_cause);
843845
} else {
844846
THROW(error);
@@ -860,7 +862,7 @@ void ConstantPool::save_and_throw_exception(const constantPoolHandle& this_cp, i
860862
// and OutOfMemoryError, etc, or if the thread was hit by stop()
861863
// Needs clarification to section 5.4.3 of the VM spec (see 6308271)
862864
} else if (this_cp->tag_at(which).value() != error_tag) {
863-
add_resolution_error(this_cp, which, tag, PENDING_EXCEPTION);
865+
add_resolution_error(THREAD, this_cp, which, tag, PENDING_EXCEPTION);
864866
// CAS in the tag. If a thread beat us to registering this error that's fine.
865867
// If another thread resolved the reference, this is a race condition. This
866868
// thread may have had a security manager or something temporary.

src/hotspot/share/oops/cpCache.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,9 @@ bool ConstantPoolCacheEntry::save_and_throw_indy_exc(
497497
CLEAR_PENDING_EXCEPTION;
498498
return false;
499499
}
500-
500+
ResourceMark rm(THREAD);
501501
Symbol* error = PENDING_EXCEPTION->klass()->name();
502-
Symbol* message = java_lang_Throwable::detail_message(PENDING_EXCEPTION);
502+
const char* message = java_lang_Throwable::message_as_utf8(PENDING_EXCEPTION);
503503

504504
SystemDictionary::add_resolution_error(cpool, index, error, message);
505505
set_indy_resolution_failed();

src/hotspot/share/utilities/exceptions.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,11 +563,11 @@ void Exceptions::debug_check_abort_helper(Handle exception, const char* message)
563563
// for logging exceptions
564564
void Exceptions::log_exception(Handle exception, const char* message) {
565565
ResourceMark rm;
566-
Symbol* detail_message = java_lang_Throwable::detail_message(exception());
567-
if (detail_message != NULL) {
566+
const char* detail_message = java_lang_Throwable::message_as_utf8(exception());
567+
if (detail_message != nullptr) {
568568
log_info(exceptions)("Exception <%s: %s>\n thrown in %s",
569569
exception->print_value_string(),
570-
detail_message->as_C_string(),
570+
detail_message,
571571
message);
572572
} else {
573573
log_info(exceptions)("Exception <%s>\n thrown in %s",

0 commit comments

Comments
 (0)