1 /* 2 * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "jfr/leakprofiler/leakProfiler.hpp" 27 #include "jfr/recorder/repository/jfrEmergencyDump.hpp" 28 #include "jfr/recorder/service/jfrPostBox.hpp" 29 #include "jfr/recorder/service/jfrRecorderService.hpp" 30 #include "memory/resourceArea.hpp" 31 #include "runtime/atomic.hpp" 32 #include "runtime/handles.hpp" 33 #include "runtime/globals.hpp" 34 #include "runtime/mutexLocker.hpp" 35 #include "runtime/thread.hpp" 36 #include "trace/traceBackend.hpp" 37 #include "tracefiles/traceEventClasses.hpp" 38 #include "tracefiles/traceTypes.hpp" 39 40 /* 41 * We are just about to exit the VM, so we will be very aggressive 42 * at this point in order to increase overall success of dumping jfr data: 43 * 44 * 1. if the thread state is not "_thread_in_vm", we will quick transition 45 * it to "_thread_in_vm". 46 * 2. the nesting state for both resource and handle areas are unknown, 47 * so we allocate new fresh arenas, discarding the old ones. 48 * 3. if the thread is the owner of some critical lock(s), unlock them. 49 * 50 * If we end up deadlocking in the attempt of dumping out jfr data, 51 * we rely on the WatcherThread task "is_error_reported()", 52 * to exit the VM after a hard-coded timeout. 53 * This "safety net" somewhat explains the aggressiveness in this attempt. 54 * 55 */ 56 static void prepare_for_emergency_dump(Thread* thread) { 57 if (thread->is_Java_thread()) { 58 ((JavaThread*)thread)->set_thread_state(_thread_in_vm); 59 } 60 61 #ifdef ASSERT 62 Monitor* owned_lock = thread->owned_locks(); 63 while (owned_lock != NULL) { 64 Monitor* next = owned_lock->next(); 65 owned_lock->unlock(); 66 owned_lock = next; 67 } 68 #endif // ASSERT 69 70 if (Threads_lock->owned_by_self()) { 71 Threads_lock->unlock(); 72 } 73 74 if (Module_lock->owned_by_self()) { 75 Module_lock->unlock(); 76 } 77 78 if (Heap_lock->owned_by_self()) { 79 Heap_lock->unlock(); 80 } 81 82 if (Safepoint_lock->owned_by_self()) { 83 Safepoint_lock->unlock(); 84 } 85 86 if (VMOperationQueue_lock->owned_by_self()) { 87 VMOperationQueue_lock->unlock(); 88 } 89 90 if (VMOperationRequest_lock->owned_by_self()) { 91 VMOperationRequest_lock->unlock(); 92 } 93 94 95 if (Service_lock->owned_by_self()) { 96 Service_lock->unlock(); 97 } 98 99 if (CodeCache_lock->owned_by_self()) { 100 CodeCache_lock->unlock(); 101 } 102 103 if (PeriodicTask_lock->owned_by_self()) { 104 PeriodicTask_lock->unlock(); 105 } 106 107 if (JfrMsg_lock->owned_by_self()) { 108 JfrMsg_lock->unlock(); 109 } 110 111 if (JfrBuffer_lock->owned_by_self()) { 112 JfrBuffer_lock->unlock(); 113 } 114 115 if (JfrStream_lock->owned_by_self()) { 116 JfrStream_lock->unlock(); 117 } 118 119 if (JfrStacktrace_lock->owned_by_self()) { 120 JfrStacktrace_lock->unlock(); 121 } 122 } 123 124 static volatile int jfr_shutdown_lock = 0; 125 126 static bool guard_reentrancy() { 127 return Atomic::cmpxchg(1, &jfr_shutdown_lock, 0) == 0; 128 } 129 130 void JfrEmergencyDump::on_vm_shutdown(bool exception_handler) { 131 if (!guard_reentrancy()) { 132 return; 133 } 134 // function made non-reentrant 135 Thread* thread = Thread::current(); 136 if (exception_handler) { 137 // we are crashing 138 if (thread->is_Watcher_thread()) { 139 // The Watcher thread runs the periodic thread sampling task. 140 // If it has crashed, it is likely that another thread is 141 // left in a suspended state. This would mean the system 142 // will not be able to ever move to a safepoint. We try 143 // to avoid issuing safepoint operations when attempting 144 // an emergency dump, but a safepoint might be already pending. 145 return; 146 } 147 prepare_for_emergency_dump(thread); 148 } 149 EventDumpReason event; 150 if (event.should_commit()) { 151 event.set_reason(exception_handler ? "Crash" : "Out of Memory"); 152 event.set_recordingId(-1); 153 event.commit(); 154 } 155 if (!exception_handler) { 156 // OOM 157 LeakProfiler::emit_events(max_jlong, false); 158 } 159 const int messages = MSGBIT(MSG_VM_ERROR); 160 ResourceMark rm(thread); 161 HandleMark hm(thread); 162 JfrRecorderService service; 163 service.rotate(messages); 164 }