Skip to content

Commit 778a6c4

Browse files
martinuyRealCLanger
authored andcommitted
8319851: Improve exception logging
Reviewed-by: mbaesken Backport-of: 87dfeeb14fdd0fa1648a8bec91b5b713cc2c1b83
1 parent 517bd93 commit 778a6c4

9 files changed

+79
-65
lines changed

src/hotspot/share/classfile/javaClasses.cpp

+9-10
Original file line numberDiff line numberDiff line change
@@ -2073,18 +2073,17 @@ oop java_lang_Throwable::message(oop throwable) {
20732073
return throwable->obj_field(_detailMessage_offset);
20742074
}
20752075

2076-
oop java_lang_Throwable::cause(oop throwable) {
2077-
return throwable->obj_field(_cause_offset);
2076+
const char* java_lang_Throwable::message_as_utf8(oop throwable) {
2077+
oop msg = java_lang_Throwable::message(throwable);
2078+
const char* msg_utf8 = nullptr;
2079+
if (msg != nullptr) {
2080+
msg_utf8 = java_lang_String::as_utf8_string(msg);
2081+
}
2082+
return msg_utf8;
20782083
}
20792084

2080-
// Return Symbol for detailed_message or null
2081-
Symbol* java_lang_Throwable::detail_message(oop throwable) {
2082-
PreserveExceptionMark pm(Thread::current());
2083-
oop detailed_message = java_lang_Throwable::message(throwable);
2084-
if (detailed_message != nullptr) {
2085-
return java_lang_String::as_symbol(detailed_message);
2086-
}
2087-
return nullptr;
2085+
oop java_lang_Throwable::cause(oop throwable) {
2086+
return throwable->obj_field(_cause_offset);
20882087
}
20892088

20902089
void java_lang_Throwable::set_message(oop throwable, oop value) {

src/hotspot/share/classfile/javaClasses.hpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -591,12 +591,14 @@ class java_lang_Throwable: AllStatic {
591591
static void set_backtrace(oop throwable, oop value);
592592
static int depth(oop throwable);
593593
static void set_depth(oop throwable, int value);
594-
static int get_detailMessage_offset() { CHECK_INIT(_detailMessage_offset); }
595594
// Message
595+
static int get_detailMessage_offset() { CHECK_INIT(_detailMessage_offset); }
596596
static oop message(oop throwable);
597-
static oop cause(oop throwable);
597+
static const char* message_as_utf8(oop throwable);
598598
static void set_message(oop throwable, oop value);
599-
static Symbol* detail_message(oop throwable);
599+
600+
static oop cause(oop throwable);
601+
600602
static void print_stack_element(outputStream *st, Method* method, int bci);
601603

602604
static void compute_offsets();

src/hotspot/share/classfile/resolutionErrors.cpp

+14-10
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ void ResolutionErrorTable::initialize() {
6666

6767
// create new error entry
6868
void ResolutionErrorTable::add_entry(const constantPoolHandle& pool, int cp_index,
69-
Symbol* error, Symbol* message,
70-
Symbol* cause, Symbol* cause_msg)
69+
Symbol* error, const char* message,
70+
Symbol* cause, const char* cause_msg)
7171
{
7272
assert_locked_or_safepoint(SystemDictionary_lock);
7373
assert(!pool.is_null() && error != nullptr, "adding null obj");
@@ -97,26 +97,30 @@ ResolutionErrorEntry* ResolutionErrorTable::find_entry(const constantPoolHandle&
9797
return entry == nullptr ? nullptr : *entry;
9898
}
9999

100-
ResolutionErrorEntry::ResolutionErrorEntry(Symbol* error, Symbol* message,
101-
Symbol* cause, Symbol* cause_msg):
100+
ResolutionErrorEntry::ResolutionErrorEntry(Symbol* error, const char* message,
101+
Symbol* cause, const char* cause_msg):
102102
_error(error),
103-
_message(message),
103+
_message(message != nullptr ? os::strdup(message) : nullptr),
104104
_cause(cause),
105-
_cause_msg(cause_msg),
105+
_cause_msg(cause_msg != nullptr ? os::strdup(cause_msg) : nullptr),
106106
_nest_host_error(nullptr) {
107107

108108
Symbol::maybe_increment_refcount(_error);
109-
Symbol::maybe_increment_refcount(_message);
110109
Symbol::maybe_increment_refcount(_cause);
111-
Symbol::maybe_increment_refcount(_cause_msg);
112110
}
113111

114112
ResolutionErrorEntry::~ResolutionErrorEntry() {
115113
// decrement error refcount
116114
Symbol::maybe_decrement_refcount(_error);
117-
Symbol::maybe_decrement_refcount(_message);
118115
Symbol::maybe_decrement_refcount(_cause);
119-
Symbol::maybe_decrement_refcount(_cause_msg);
116+
117+
if (_message != nullptr) {
118+
FREE_C_HEAP_ARRAY(char, _message);
119+
}
120+
121+
if (_cause_msg != nullptr) {
122+
FREE_C_HEAP_ARRAY(char, _cause_msg);
123+
}
120124

121125
if (nest_host_error() != nullptr) {
122126
FREE_C_HEAP_ARRAY(char, nest_host_error());

src/hotspot/share/classfile/resolutionErrors.hpp

+18-13
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ class ResolutionErrorTable : AllStatic {
3636

3737
public:
3838
static void initialize();
39-
static void add_entry(const constantPoolHandle& pool, int which, Symbol* error, Symbol* message,
40-
Symbol* cause, Symbol* cause_msg);
39+
static void add_entry(const constantPoolHandle& pool, int cp_index,
40+
Symbol* error, const char* error_msg,
41+
Symbol* cause, const char* cause_msg);
4142

42-
static void add_entry(const constantPoolHandle& pool, int which, const char* message);
43+
static void add_entry(const constantPoolHandle& pool, int cp_index, const char* message);
4344

4445
// find error given the constant pool and constant pool index
4546
static ResolutionErrorEntry* find_entry(const constantPoolHandle& pool, int cp_index);
@@ -69,34 +70,38 @@ class ResolutionErrorTable : AllStatic {
6970
class ResolutionErrorEntry : public CHeapObj<mtClass> {
7071
private:
7172
Symbol* _error;
72-
Symbol* _message;
73+
const char* _message;
7374
Symbol* _cause;
74-
Symbol* _cause_msg;
75+
const char* _cause_msg;
7576
const char* _nest_host_error;
7677

7778
NONCOPYABLE(ResolutionErrorEntry);
7879

7980
public:
80-
ResolutionErrorEntry(Symbol* error, Symbol* message, Symbol* cause, Symbol* cause_msg);
81+
// The incoming message and cause_msg are copied to the C-Heap.
82+
ResolutionErrorEntry(Symbol* error, const char* message,
83+
Symbol* cause, const char* cause_msg);
8184

82-
ResolutionErrorEntry(const char* message):
85+
// The incoming nest host error message is already in the C-Heap.
86+
ResolutionErrorEntry(const char* message):
8387
_error(nullptr),
8488
_message(nullptr),
8589
_cause(nullptr),
8690
_cause_msg(nullptr),
8791
_nest_host_error(message) {}
8892

89-
~ResolutionErrorEntry();
93+
~ResolutionErrorEntry();
9094

91-
void set_nest_host_error(const char* message) {
92-
_nest_host_error = message;
93-
}
95+
// The incoming nest host error message is already in the C-Heap.
96+
void set_nest_host_error(const char* message) {
97+
_nest_host_error = message;
98+
}
9499

95100

96101
Symbol* error() const { return _error; }
97-
Symbol* message() const { return _message; }
102+
const char* message() const { return _message; }
98103
Symbol* cause() const { return _cause; }
99-
Symbol* cause_msg() const { return _cause_msg; }
104+
const char* cause_msg() const { return _cause_msg; }
100105
const char* nest_host_error() const { return _nest_host_error; }
101106
};
102107

src/hotspot/share/classfile/systemDictionary.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1797,8 +1797,8 @@ bool SystemDictionary::add_loader_constraint(Symbol* class_name,
17971797
// Add entry to resolution error table to record the error when the first
17981798
// attempt to resolve a reference to a class has failed.
17991799
void SystemDictionary::add_resolution_error(const constantPoolHandle& pool, int which,
1800-
Symbol* error, Symbol* message,
1801-
Symbol* cause, Symbol* cause_msg) {
1800+
Symbol* error, const char* message,
1801+
Symbol* cause, const char* cause_msg) {
18021802
{
18031803
MutexLocker ml(Thread::current(), SystemDictionary_lock);
18041804
ResolutionErrorEntry* entry = ResolutionErrorTable::find_entry(pool, which);
@@ -1815,7 +1815,8 @@ void SystemDictionary::delete_resolution_error(ConstantPool* pool) {
18151815

18161816
// Lookup resolution error table. Returns error if found, otherwise null.
18171817
Symbol* SystemDictionary::find_resolution_error(const constantPoolHandle& pool, int which,
1818-
Symbol** message, Symbol** cause, Symbol** cause_msg) {
1818+
const char** message,
1819+
Symbol** cause, const char** cause_msg) {
18191820

18201821
{
18211822
MutexLocker ml(Thread::current(), SystemDictionary_lock);

src/hotspot/share/classfile/systemDictionary.hpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,13 @@ class SystemDictionary : AllStatic {
272272

273273
// Record the error when the first attempt to resolve a reference from a constant
274274
// pool entry to a class fails.
275-
static void add_resolution_error(const constantPoolHandle& pool, int which, Symbol* error,
276-
Symbol* message, Symbol* cause = nullptr, Symbol* cause_msg = nullptr);
275+
static void add_resolution_error(const constantPoolHandle& pool, int which,
276+
Symbol* error, const char* message,
277+
Symbol* cause = nullptr, const char* cause_msg = nullptr);
277278
static void delete_resolution_error(ConstantPool* pool);
278279
static Symbol* find_resolution_error(const constantPoolHandle& pool, int which,
279-
Symbol** message, Symbol** cause, Symbol** cause_msg);
280-
280+
const char** message,
281+
Symbol** cause, const char** cause_msg);
281282

282283
// Record a nest host resolution/validation error
283284
static void add_nest_host_error(const constantPoolHandle& pool, int which,

src/hotspot/share/oops/constantPool.cpp

+20-18
Original file line numberDiff line numberDiff line change
@@ -803,13 +803,16 @@ void ConstantPool::resolve_string_constants_impl(const constantPoolHandle& this_
803803
}
804804
}
805805

806-
static Symbol* exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) {
806+
static const char* exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) {
807+
// Note: caller needs ResourceMark
808+
807809
// Dig out the detailed message to reuse if possible
808-
Symbol* message = java_lang_Throwable::detail_message(pending_exception);
809-
if (message != nullptr) {
810-
return message;
810+
const char* msg = java_lang_Throwable::message_as_utf8(pending_exception);
811+
if (msg != nullptr) {
812+
return msg;
811813
}
812814

815+
Symbol* message = nullptr;
813816
// Return specific message for the tag
814817
switch (tag.value()) {
815818
case JVM_CONSTANT_UnresolvedClass:
@@ -832,49 +835,48 @@ static Symbol* exception_message(const constantPoolHandle& this_cp, int which, c
832835
ShouldNotReachHere();
833836
}
834837

835-
return message;
838+
return message != nullptr ? message->as_C_string() : nullptr;
836839
}
837840

838-
static void add_resolution_error(const constantPoolHandle& this_cp, int which,
841+
static void add_resolution_error(JavaThread* current, const constantPoolHandle& this_cp, int which,
839842
constantTag tag, oop pending_exception) {
840843

844+
ResourceMark rm(current);
841845
Symbol* error = pending_exception->klass()->name();
842846
oop cause = java_lang_Throwable::cause(pending_exception);
843847

844848
// Also dig out the exception cause, if present.
845849
Symbol* cause_sym = nullptr;
846-
Symbol* cause_msg = nullptr;
850+
const char* cause_msg = nullptr;
847851
if (cause != nullptr && cause != pending_exception) {
848852
cause_sym = cause->klass()->name();
849-
cause_msg = java_lang_Throwable::detail_message(cause);
853+
cause_msg = java_lang_Throwable::message_as_utf8(cause);
850854
}
851855

852-
Symbol* message = exception_message(this_cp, which, tag, pending_exception);
856+
const char* message = exception_message(this_cp, which, tag, pending_exception);
853857
SystemDictionary::add_resolution_error(this_cp, which, error, message, cause_sym, cause_msg);
854858
}
855859

856860

857861
void ConstantPool::throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS) {
858862
ResourceMark rm(THREAD);
859-
Symbol* message = nullptr;
863+
const char* message = nullptr;
860864
Symbol* cause = nullptr;
861-
Symbol* cause_msg = nullptr;
865+
const char* cause_msg = nullptr;
862866
Symbol* error = SystemDictionary::find_resolution_error(this_cp, which, &message, &cause, &cause_msg);
863867
assert(error != nullptr, "checking");
864-
const char* cause_str = cause_msg != nullptr ? cause_msg->as_C_string() : nullptr;
865868

866869
CLEAR_PENDING_EXCEPTION;
867870
if (message != nullptr) {
868-
char* msg = message->as_C_string();
869871
if (cause != nullptr) {
870-
Handle h_cause = Exceptions::new_exception(THREAD, cause, cause_str);
871-
THROW_MSG_CAUSE(error, msg, h_cause);
872+
Handle h_cause = Exceptions::new_exception(THREAD, cause, cause_msg);
873+
THROW_MSG_CAUSE(error, message, h_cause);
872874
} else {
873-
THROW_MSG(error, msg);
875+
THROW_MSG(error, message);
874876
}
875877
} else {
876878
if (cause != nullptr) {
877-
Handle h_cause = Exceptions::new_exception(THREAD, cause, cause_str);
879+
Handle h_cause = Exceptions::new_exception(THREAD, cause, cause_msg);
878880
THROW_CAUSE(error, h_cause);
879881
} else {
880882
THROW(error);
@@ -896,7 +898,7 @@ void ConstantPool::save_and_throw_exception(const constantPoolHandle& this_cp, i
896898
// and OutOfMemoryError, etc, or if the thread was hit by stop()
897899
// Needs clarification to section 5.4.3 of the VM spec (see 6308271)
898900
} else if (this_cp->tag_at(which).value() != error_tag) {
899-
add_resolution_error(this_cp, which, tag, PENDING_EXCEPTION);
901+
add_resolution_error(THREAD, this_cp, which, tag, PENDING_EXCEPTION);
900902
// CAS in the tag. If a thread beat us to registering this error that's fine.
901903
// If another thread resolved the reference, this is a race condition. This
902904
// thread may have had a security manager or something temporary.

src/hotspot/share/oops/cpCache.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -849,9 +849,9 @@ bool ConstantPoolCache::save_and_throw_indy_exc(
849849
CLEAR_PENDING_EXCEPTION;
850850
return false;
851851
}
852-
852+
ResourceMark rm(THREAD);
853853
Symbol* error = PENDING_EXCEPTION->klass()->name();
854-
Symbol* message = java_lang_Throwable::detail_message(PENDING_EXCEPTION);
854+
const char* message = java_lang_Throwable::message_as_utf8(PENDING_EXCEPTION);
855855

856856
int encoded_index = ResolutionErrorTable::encode_cpcache_index(
857857
ConstantPool::encode_invokedynamic_index(index));

src/hotspot/share/utilities/exceptions.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -565,11 +565,11 @@ void Exceptions::debug_check_abort_helper(Handle exception, const char* message)
565565
// for logging exceptions
566566
void Exceptions::log_exception(Handle exception, const char* message) {
567567
ResourceMark rm;
568-
Symbol* detail_message = java_lang_Throwable::detail_message(exception());
568+
const char* detail_message = java_lang_Throwable::message_as_utf8(exception());
569569
if (detail_message != nullptr) {
570570
log_info(exceptions)("Exception <%s: %s>\n thrown in %s",
571571
exception->print_value_string(),
572-
detail_message->as_C_string(),
572+
detail_message,
573573
message);
574574
} else {
575575
log_info(exceptions)("Exception <%s>\n thrown in %s",

0 commit comments

Comments
 (0)