--- old/src/hotspot/share/jfr/jfr.cpp 2019-11-03 00:50:37.937085200 +0900 +++ new/src/hotspot/share/jfr/jfr.cpp 2019-11-03 00:50:37.674141900 +0900 @@ -89,9 +89,9 @@ } } -void Jfr::on_vm_shutdown(bool exception_handler) { +void Jfr::on_vm_shutdown(Thread* thread, bool exception_handler) { if (JfrRecorder::is_recording()) { - JfrEmergencyDump::on_vm_shutdown(exception_handler); + JfrEmergencyDump::on_vm_shutdown(thread, exception_handler); } } --- old/src/hotspot/share/jfr/jfr.hpp 2019-11-03 00:50:40.264464900 +0900 +++ new/src/hotspot/share/jfr/jfr.hpp 2019-11-03 00:50:39.689356800 +0900 @@ -49,7 +49,7 @@ static void on_thread_start(Thread* thread); static void on_thread_exit(Thread* thread); static void on_java_thread_dismantle(JavaThread* jt); - static void on_vm_shutdown(bool exception_handler = false); + static void on_vm_shutdown(Thread* thread, bool exception_handler = false); static bool on_flight_recorder_option(const JavaVMOption** option, char* delimiter); static bool on_start_flight_recording_option(const JavaVMOption** option, char* delimiter); static void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f); --- old/src/hotspot/share/jfr/recorder/repository/jfrEmergencyDump.cpp 2019-11-03 00:50:42.804220400 +0900 +++ new/src/hotspot/share/jfr/recorder/repository/jfrEmergencyDump.cpp 2019-11-03 00:50:42.171262600 +0900 @@ -334,8 +334,7 @@ * We are just about to exit the VM, so we will be very aggressive * at this point in order to increase overall success of dumping jfr data: * -* 1. if the thread state is not "_thread_in_vm", we will quick transition -* it to "_thread_in_vm". +* 1. if current thread is JavaThread, thread state need to be set to "_thread_in_vm". * 2. if the thread is the owner of some critical lock(s), unlock them. * * If we end up deadlocking in the attempt of dumping out jfr data, @@ -355,9 +354,7 @@ return false; } - if (thread->is_Java_thread()) { - ((JavaThread*)thread)->set_thread_state(_thread_in_vm); - } + assert(thread->is_Java_thread() && (((JavaThread*)thread)->thread_state() == _thread_in_vm), "invariant"); #ifdef ASSERT Mutex* owned_lock = thread->owned_locks(); @@ -428,7 +425,37 @@ return Atomic::cmpxchg(1, &jfr_shutdown_lock, 0) == 0; } -void JfrEmergencyDump::on_vm_shutdown(bool exception_handler) { +class ThreadInVMForJFR : public StackObj { + private: + JavaThread* _jt; + JavaThreadState _original_state; + + public: + + ThreadInVMForJFR(Thread* thread) : _jt(NULL), + _original_state(_thread_max_state) { + if ((thread != NULL) && thread->is_Java_thread()) { + _jt = (JavaThread*)thread; + _original_state = _jt->thread_state(); + _jt->set_thread_state(_thread_in_vm); + } + } + + ~ThreadInVMForJFR() { + if (_jt != NULL) { + _jt->set_thread_state(_original_state); + } + } + +}; + +void JfrEmergencyDump::on_vm_shutdown(Thread* thread, bool exception_handler) { + /* + * if the thread state is not "_thread_in_vm", we will quick transition + * it to "_thread_in_vm" via ThreadInVMForJFR. + */ + ThreadInVMForJFR tiv(thread); + if (!(guard_reentrancy() && prepare_for_emergency_dump())) { return; } --- old/src/hotspot/share/jfr/recorder/repository/jfrEmergencyDump.hpp 2019-11-03 00:50:45.324502100 +0900 +++ new/src/hotspot/share/jfr/recorder/repository/jfrEmergencyDump.hpp 2019-11-03 00:50:44.703602400 +0900 @@ -32,7 +32,7 @@ // class JfrEmergencyDump : AllStatic { public: - static void on_vm_shutdown(bool exception_handler); + static void on_vm_shutdown(Thread* thread, bool exception_handler); static void on_vm_error(const char* repository_path); static const char* build_dump_path(const char* repository_path); }; --- old/src/hotspot/share/runtime/java.cpp 2019-11-03 00:50:47.801263500 +0900 +++ new/src/hotspot/share/runtime/java.cpp 2019-11-03 00:50:47.206148900 +0900 @@ -445,7 +445,7 @@ event.commit(); } - JFR_ONLY(Jfr::on_vm_shutdown();) + JFR_ONLY(Jfr::on_vm_shutdown(thread);) // Stop the WatcherThread. We do this before disenrolling various // PeriodicTasks to reduce the likelihood of races. --- old/src/hotspot/share/utilities/vmError.cpp 2019-11-03 00:50:50.288595000 +0900 +++ new/src/hotspot/share/utilities/vmError.cpp 2019-11-03 00:50:49.651202600 +0900 @@ -1415,7 +1415,7 @@ e.commit(); } - JFR_ONLY(Jfr::on_vm_shutdown(true);) + JFR_ONLY(Jfr::on_vm_shutdown(thread, true);) } else { // If UseOsErrorReporting we call this for each level of the call stack