diff -r 11a574b352d0 -r e3618c902d17 src/hotspot/share/classfile/javaClasses.cpp --- a/src/hotspot/share/classfile/javaClasses.cpp Thu Oct 17 10:35:18 2019 +0200 +++ b/src/hotspot/share/classfile/javaClasses.cpp Mon Oct 14 11:36:17 2019 +0200 @@ -1545,7 +1545,7 @@ int java_lang_Class::classRedefinedCount_offset = -1; #define CLASS_FIELDS_DO(macro) \ - macro(classRedefinedCount_offset, k, "classRedefinedCount", int_signature, false) ; \ + macro(classRedefinedCount_offset, k, "classRedefinedCount", int_signature, false); \ macro(_class_loader_offset, k, "classLoader", classloader_signature, false); \ macro(_component_mirror_offset, k, "componentType", class_signature, false); \ macro(_module_offset, k, "module", module_signature, false); \ @@ -1938,7 +1938,6 @@ return method != NULL && (method->constants()->version() == version); } - // This class provides a simple wrapper over the internal structure of // exception backtrace to insulate users of the backtrace from needing // to know what it looks like. @@ -1950,7 +1949,11 @@ typeArrayOop _methods; typeArrayOop _bcis; objArrayOop _mirrors; - typeArrayOop _names; // needed to insulate method name against redefinition + typeArrayOop _names; // Needed to insulate method name against redefinition. + // This is set to a java.lang.Boolean(true) if the top frame + // of the backtrace is omitted because it shall be hidden. + // Else it is null. + oop _has_hidden_top_frame; int _index; NoSafepointVerifier _nsv; @@ -1960,6 +1963,7 @@ trace_mirrors_offset = java_lang_Throwable::trace_mirrors_offset, trace_names_offset = java_lang_Throwable::trace_names_offset, trace_next_offset = java_lang_Throwable::trace_next_offset, + trace_hidden_offset = java_lang_Throwable::trace_hidden_offset, trace_size = java_lang_Throwable::trace_size, trace_chunk_size = java_lang_Throwable::trace_chunk_size }; @@ -1985,11 +1989,15 @@ assert(names != NULL, "names array should be initialized in backtrace"); return names; } + static oop get_has_hidden_top_frame(objArrayHandle chunk) { + oop hidden = chunk->obj_at(trace_hidden_offset); + return hidden; + } public: // constructor for new backtrace - BacktraceBuilder(TRAPS): _head(NULL), _methods(NULL), _bcis(NULL), _mirrors(NULL), _names(NULL) { + BacktraceBuilder(TRAPS): _head(NULL), _methods(NULL), _bcis(NULL), _mirrors(NULL), _names(NULL), _has_hidden_top_frame(NULL) { expand(CHECK); _backtrace = Handle(THREAD, _head); _index = 0; @@ -2000,6 +2008,7 @@ _bcis = get_bcis(backtrace); _mirrors = get_mirrors(backtrace); _names = get_names(backtrace); + _has_hidden_top_frame = get_has_hidden_top_frame(backtrace); assert(_methods->length() == _bcis->length() && _methods->length() == _mirrors->length() && _mirrors->length() == _names->length(), @@ -2037,6 +2046,7 @@ new_head->obj_at_put(trace_bcis_offset, new_bcis()); new_head->obj_at_put(trace_mirrors_offset, new_mirrors()); new_head->obj_at_put(trace_names_offset, new_names()); + new_head->obj_at_put(trace_hidden_offset, NULL); _head = new_head(); _methods = new_methods(); @@ -2077,6 +2087,16 @@ _index++; } + void set_has_hidden_top_frame(TRAPS) { + if (_has_hidden_top_frame == NULL) { + jvalue prim; + prim.z = 1; + PauseNoSafepointVerifier pnsv(&_nsv); + _has_hidden_top_frame = java_lang_boxing_object::create(T_BOOLEAN, &prim, CHECK); + _head->obj_at_put(trace_hidden_offset, _has_hidden_top_frame); + } + } + }; struct BacktraceElement : public StackObj { @@ -2406,7 +2426,13 @@ } } if (method->is_hidden()) { - if (skip_hidden) continue; + if (skip_hidden) { + if (total_count == 0) { + // The top frame will be hidden from the stack trace. + bt.set_has_hidden_top_frame(CHECK); + } + continue; + } } bt.push(method, bci, CHECK); total_count++; @@ -2523,6 +2549,37 @@ } } +bool java_lang_Throwable::get_top_method_and_bci(oop throwable, Method** method, int* bci) { + Thread* THREAD = Thread::current(); + objArrayHandle result(THREAD, objArrayOop(backtrace(throwable))); + BacktraceIterator iter(result, THREAD); + // No backtrace available. + if (!iter.repeat()) return false; + + // If the exception happened in a frame that has been hidden, i.e., + // omitted from the back trace, we can not compute the message. + oop hidden = ((objArrayOop)backtrace(throwable))->obj_at(trace_hidden_offset); + if (hidden != NULL) { + return false; + } + + // Get first backtrace element. + BacktraceElement bte = iter.next(THREAD); + + InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror())); + assert(holder != NULL, "first element should be non-null"); + Method* m = holder->method_with_orig_idnum(bte._method_id, bte._version); + + // Original version is no longer available. + if (m == NULL || !version_matches(m, bte._version)) { + return false; + } + + *method = m; + *bci = bte._bci; + return true; +} + oop java_lang_StackTraceElement::create(const methodHandle& method, int bci, TRAPS) { // Allocate java.lang.StackTraceElement instance InstanceKlass* k = SystemDictionary::StackTraceElement_klass();