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/jfrEvents.hpp" 27 #include "jfr/leakprofiler/leakProfiler.hpp" 28 #include "jfr/recorder/repository/jfrEmergencyDump.hpp" 29 #include "jfr/recorder/service/jfrPostBox.hpp" 30 #include "jfr/recorder/service/jfrRecorderService.hpp" 31 #include "jfr/utilities/jfrTypes.hpp" 32 #include "memory/resourceArea.hpp" 33 #include "runtime/atomic.hpp" 34 #include "runtime/handles.hpp" 35 #include "runtime/globals.hpp" 36 #include "runtime/mutexLocker.hpp" 37 #include "runtime/thread.hpp" 38 39 /* 40 * We are just about to exit the VM, so we will be very aggressive 41 * at this point in order to increase overall success of dumping jfr data: 42 * 43 * 1. if the thread state is not "_thread_in_vm", we will quick transition 44 * it to "_thread_in_vm". 45 * 2. the nesting state for both resource and handle areas are unknown, 46 * so we allocate new fresh arenas, discarding the old ones. 47 * 3. if the thread is the owner of some critical lock(s), unlock them. 48 * 49 * If we end up deadlocking in the attempt of dumping out jfr data, 50 * we rely on the WatcherThread task "is_error_reported()", 51 * to exit the VM after a hard-coded timeout. 52 * This "safety net" somewhat explains the aggressiveness in this attempt. 53 * 54 */ 55 static void prepare_for_emergency_dump(Thread* thread) { 56 if (thread->is_Java_thread()) { 57 ((JavaThread*)thread)->set_thread_state(_thread_in_vm); 58 } 59 60 #ifdef ASSERT 61 Monitor* owned_lock = thread->owned_locks(); 62 while (owned_lock != NULL) { 63 Monitor* next = owned_lock->next(); 64 owned_lock->unlock(); 65 owned_lock = next; 66 } 67 #endif // ASSERT 68 69 if (Threads_lock->owned_by_self()) { 70 Threads_lock->unlock(); 71 } 72 73 // XXX (Module_lock -> PackageTable_lock) 74 if (PackageTable_lock->owned_by_self()) { 75 PackageTable_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 }