zgu@2364: /* mikael@6198: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. zgu@2364: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. zgu@2364: * zgu@2364: * This code is free software; you can redistribute it and/or modify it zgu@2364: * under the terms of the GNU General Public License version 2 only, as zgu@2364: * published by the Free Software Foundation. zgu@2364: * zgu@2364: * This code is distributed in the hope that it will be useful, but WITHOUT zgu@2364: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or zgu@2364: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License zgu@2364: * version 2 for more details (a copy is included in the LICENSE file that zgu@2364: * accompanied this code). zgu@2364: * zgu@2364: * You should have received a copy of the GNU General Public License version zgu@2364: * 2 along with this work; if not, write to the Free Software Foundation, zgu@2364: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. zgu@2364: * zgu@2364: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA zgu@2364: * or visit www.oracle.com if you need additional information or have any zgu@2364: * questions. zgu@2364: * zgu@2364: */ zgu@2364: zgu@2364: #include "precompiled.hpp" zgu@2364: #include "prims/jvm.h" zgu@3544: #include "runtime/os.hpp" zgu@2364: #include "utilities/decoder.hpp" zgu@3544: #include "utilities/vmError.hpp" zgu@2364: zgu@3430: #if defined(_WINDOWS) zgu@3430: #include "decoder_windows.hpp" zgu@3430: #elif defined(__APPLE__) zgu@3430: #include "decoder_machO.hpp" goetz@6461: #elif defined(AIX) goetz@6461: #include "decoder_aix.hpp" zgu@3430: #else zgu@3430: #include "decoder_elf.hpp" zgu@3430: #endif zgu@2364: zgu@3544: AbstractDecoder* Decoder::_shared_decoder = NULL; zgu@3544: AbstractDecoder* Decoder::_error_handler_decoder = NULL; zgu@3544: NullDecoder Decoder::_do_nothing_decoder; zgu@3544: Mutex* Decoder::_shared_decoder_lock = new Mutex(Mutex::native, zgu@3544: "SharedDecoderLock"); zgu@2364: zgu@3544: AbstractDecoder* Decoder::get_shared_instance() { zgu@3544: assert(_shared_decoder_lock != NULL && _shared_decoder_lock->owned_by_self(), zgu@3430: "Require DecoderLock to enter"); zgu@2364: zgu@3544: if (_shared_decoder == NULL) { zgu@3544: _shared_decoder = create_decoder(); zgu@3430: } zgu@3544: return _shared_decoder; zgu@3544: } zgu@3430: zgu@3544: AbstractDecoder* Decoder::get_error_handler_instance() { zgu@3544: if (_error_handler_decoder == NULL) { zgu@3544: _error_handler_decoder = create_decoder(); zgu@3544: } zgu@3544: return _error_handler_decoder; zgu@3544: } zgu@3544: zgu@3544: zgu@3544: AbstractDecoder* Decoder::create_decoder() { zgu@3544: AbstractDecoder* decoder; zgu@3430: #if defined(_WINDOWS) zgu@3544: decoder = new (std::nothrow) WindowsDecoder(); zgu@3430: #elif defined (__APPLE__) zgu@3544: decoder = new (std::nothrow)MachODecoder(); goetz@6461: #elif defined(AIX) goetz@6461: decoder = new (std::nothrow)AIXDecoder(); zgu@3430: #else zgu@3544: decoder = new (std::nothrow)ElfDecoder(); zgu@3430: #endif zgu@3430: zgu@3544: if (decoder == NULL || decoder->has_error()) { zgu@3544: if (decoder != NULL) { zgu@3544: delete decoder; zgu@3430: } zgu@3544: decoder = &_do_nothing_decoder; zgu@3430: } zgu@3544: return decoder; zgu@3430: } zgu@3430: iklam@5667: inline bool DecoderLocker::is_first_error_thread() { iklam@5667: return (os::current_thread_id() == VMError::get_first_error_tid()); iklam@5667: } iklam@5667: iklam@5667: DecoderLocker::DecoderLocker() : iklam@5667: MutexLockerEx(DecoderLocker::is_first_error_thread() ? iklam@5667: NULL : Decoder::shared_decoder_lock(), true) { iklam@5667: _decoder = is_first_error_thread() ? iklam@5667: Decoder::get_error_handler_instance() : Decoder::get_shared_instance(); iklam@5667: assert(_decoder != NULL, "null decoder"); iklam@5667: } iklam@5667: iklam@5667: Mutex* Decoder::shared_decoder_lock() { iklam@5667: assert(_shared_decoder_lock != NULL, "Just check"); iklam@5667: return _shared_decoder_lock; iklam@5667: } iklam@5667: zgu@3430: bool Decoder::decode(address addr, char* buf, int buflen, int* offset, const char* modulepath) { zgu@3544: assert(_shared_decoder_lock != NULL, "Just check"); zgu@3544: bool error_handling_thread = os::current_thread_id() == VMError::first_error_tid; zgu@3544: MutexLockerEx locker(error_handling_thread ? NULL : _shared_decoder_lock, true); zgu@3544: AbstractDecoder* decoder = error_handling_thread ? zgu@3544: get_error_handler_instance(): get_shared_instance(); zgu@3430: assert(decoder != NULL, "null decoder"); zgu@3430: zgu@3430: return decoder->decode(addr, buf, buflen, offset, modulepath); zgu@3430: } zgu@3430: zgu@3961: bool Decoder::decode(address addr, char* buf, int buflen, int* offset, const void* base) { zgu@3961: assert(_shared_decoder_lock != NULL, "Just check"); zgu@3961: bool error_handling_thread = os::current_thread_id() == VMError::first_error_tid; zgu@3961: MutexLockerEx locker(error_handling_thread ? NULL : _shared_decoder_lock, true); zgu@3961: AbstractDecoder* decoder = error_handling_thread ? zgu@3961: get_error_handler_instance(): get_shared_instance(); zgu@3961: assert(decoder != NULL, "null decoder"); zgu@3961: zgu@3961: return decoder->decode(addr, buf, buflen, offset, base); zgu@3961: } zgu@3961: zgu@3961: zgu@3430: bool Decoder::demangle(const char* symbol, char* buf, int buflen) { zgu@3544: assert(_shared_decoder_lock != NULL, "Just check"); zgu@3544: bool error_handling_thread = os::current_thread_id() == VMError::first_error_tid; zgu@3544: MutexLockerEx locker(error_handling_thread ? NULL : _shared_decoder_lock, true); zgu@3544: AbstractDecoder* decoder = error_handling_thread ? zgu@3544: get_error_handler_instance(): get_shared_instance(); zgu@3430: assert(decoder != NULL, "null decoder"); zgu@3430: return decoder->demangle(symbol, buf, buflen); zgu@3430: } zgu@2364: zgu@2364: bool Decoder::can_decode_C_frame_in_vm() { zgu@3544: assert(_shared_decoder_lock != NULL, "Just check"); zgu@3544: bool error_handling_thread = os::current_thread_id() == VMError::first_error_tid; zgu@3544: MutexLockerEx locker(error_handling_thread ? NULL : _shared_decoder_lock, true); zgu@3544: AbstractDecoder* decoder = error_handling_thread ? zgu@3544: get_error_handler_instance(): get_shared_instance(); zgu@3430: assert(decoder != NULL, "null decoder"); zgu@3430: return decoder->can_decode_C_frame_in_vm(); zgu@2364: } zgu@2364: zgu@3544: /* zgu@3544: * Shutdown shared decoder and replace it with zgu@3544: * _do_nothing_decoder. Do nothing with error handler zgu@3544: * instance, since the JVM is going down. zgu@3544: */ zgu@3430: void Decoder::shutdown() { zgu@3544: assert(_shared_decoder_lock != NULL, "Just check"); zgu@3544: MutexLockerEx locker(_shared_decoder_lock, true); zgu@3430: zgu@3544: if (_shared_decoder != NULL && zgu@3544: _shared_decoder != &_do_nothing_decoder) { zgu@3544: delete _shared_decoder; zgu@3430: } zgu@3430: zgu@3544: _shared_decoder = &_do_nothing_decoder; zgu@2364: } zgu@2364: