decoder.cpp revision 8550:b8a3be10a4ed
1/* 2 * Copyright (c) 1997, 2015, 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 "prims/jvm.h" 27#include "runtime/os.hpp" 28#include "utilities/decoder.hpp" 29#include "utilities/vmError.hpp" 30 31#if defined(_WINDOWS) 32 #include "decoder_windows.hpp" 33#elif defined(__APPLE__) 34 #include "decoder_machO.hpp" 35#elif defined(AIX) 36 #include "decoder_aix.hpp" 37#else 38 #include "decoder_elf.hpp" 39#endif 40 41AbstractDecoder* Decoder::_shared_decoder = NULL; 42AbstractDecoder* Decoder::_error_handler_decoder = NULL; 43NullDecoder Decoder::_do_nothing_decoder; 44Mutex* Decoder::_shared_decoder_lock = new Mutex(Mutex::native, 45 "SharedDecoderLock", 46 false, 47 Monitor::_safepoint_check_never); 48 49AbstractDecoder* Decoder::get_shared_instance() { 50 assert(_shared_decoder_lock != NULL && _shared_decoder_lock->owned_by_self(), 51 "Require DecoderLock to enter"); 52 53 if (_shared_decoder == NULL) { 54 _shared_decoder = create_decoder(); 55 } 56 return _shared_decoder; 57} 58 59AbstractDecoder* Decoder::get_error_handler_instance() { 60 if (_error_handler_decoder == NULL) { 61 _error_handler_decoder = create_decoder(); 62 } 63 return _error_handler_decoder; 64} 65 66 67AbstractDecoder* Decoder::create_decoder() { 68 AbstractDecoder* decoder; 69#if defined(_WINDOWS) 70 decoder = new (std::nothrow) WindowsDecoder(); 71#elif defined (__APPLE__) 72 decoder = new (std::nothrow)MachODecoder(); 73#elif defined(AIX) 74 decoder = new (std::nothrow)AIXDecoder(); 75#else 76 decoder = new (std::nothrow)ElfDecoder(); 77#endif 78 79 if (decoder == NULL || decoder->has_error()) { 80 if (decoder != NULL) { 81 delete decoder; 82 } 83 decoder = &_do_nothing_decoder; 84 } 85 return decoder; 86} 87 88inline bool DecoderLocker::is_first_error_thread() { 89 return (os::current_thread_id() == VMError::get_first_error_tid()); 90} 91 92DecoderLocker::DecoderLocker() : 93 MutexLockerEx(DecoderLocker::is_first_error_thread() ? 94 NULL : Decoder::shared_decoder_lock(), true) { 95 _decoder = is_first_error_thread() ? 96 Decoder::get_error_handler_instance() : Decoder::get_shared_instance(); 97 assert(_decoder != NULL, "null decoder"); 98} 99 100Mutex* Decoder::shared_decoder_lock() { 101 assert(_shared_decoder_lock != NULL, "Just check"); 102 return _shared_decoder_lock; 103} 104 105bool Decoder::decode(address addr, char* buf, int buflen, int* offset, const char* modulepath, bool demangle) { 106 assert(_shared_decoder_lock != NULL, "Just check"); 107 bool error_handling_thread = os::current_thread_id() == VMError::first_error_tid; 108 MutexLockerEx locker(error_handling_thread ? NULL : _shared_decoder_lock, true); 109 AbstractDecoder* decoder = error_handling_thread ? 110 get_error_handler_instance(): get_shared_instance(); 111 assert(decoder != NULL, "null decoder"); 112 113 return decoder->decode(addr, buf, buflen, offset, modulepath, demangle); 114} 115 116bool Decoder::decode(address addr, char* buf, int buflen, int* offset, const void* base) { 117 assert(_shared_decoder_lock != NULL, "Just check"); 118 bool error_handling_thread = os::current_thread_id() == VMError::first_error_tid; 119 MutexLockerEx locker(error_handling_thread ? NULL : _shared_decoder_lock, true); 120 AbstractDecoder* decoder = error_handling_thread ? 121 get_error_handler_instance(): get_shared_instance(); 122 assert(decoder != NULL, "null decoder"); 123 124 return decoder->decode(addr, buf, buflen, offset, base); 125} 126 127 128bool Decoder::demangle(const char* symbol, char* buf, int buflen) { 129 assert(_shared_decoder_lock != NULL, "Just check"); 130 bool error_handling_thread = os::current_thread_id() == VMError::first_error_tid; 131 MutexLockerEx locker(error_handling_thread ? NULL : _shared_decoder_lock, true); 132 AbstractDecoder* decoder = error_handling_thread ? 133 get_error_handler_instance(): get_shared_instance(); 134 assert(decoder != NULL, "null decoder"); 135 return decoder->demangle(symbol, buf, buflen); 136} 137 138bool Decoder::can_decode_C_frame_in_vm() { 139 assert(_shared_decoder_lock != NULL, "Just check"); 140 bool error_handling_thread = os::current_thread_id() == VMError::first_error_tid; 141 MutexLockerEx locker(error_handling_thread ? NULL : _shared_decoder_lock, true); 142 AbstractDecoder* decoder = error_handling_thread ? 143 get_error_handler_instance(): get_shared_instance(); 144 assert(decoder != NULL, "null decoder"); 145 return decoder->can_decode_C_frame_in_vm(); 146} 147 148/* 149 * Shutdown shared decoder and replace it with 150 * _do_nothing_decoder. Do nothing with error handler 151 * instance, since the JVM is going down. 152 */ 153void Decoder::shutdown() { 154 assert(_shared_decoder_lock != NULL, "Just check"); 155 MutexLockerEx locker(_shared_decoder_lock, true); 156 157 if (_shared_decoder != NULL && 158 _shared_decoder != &_do_nothing_decoder) { 159 delete _shared_decoder; 160 } 161 162 _shared_decoder = &_do_nothing_decoder; 163} 164 165