aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: aoqi@0: #ifndef SHARE_VM_UTILITIES_DECODER_HPP aoqi@0: #define SHARE_VM_UTILITIES_DECODER_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "runtime/mutex.hpp" aoqi@0: #include "runtime/mutexLocker.hpp" aoqi@0: aoqi@0: class AbstractDecoder : public CHeapObj { aoqi@0: public: aoqi@0: // status code for decoding native C frame aoqi@0: enum decoder_status { aoqi@0: not_available = -10, // real decoder is not available aoqi@0: no_error = 0, // successfully decoded frames aoqi@0: out_of_memory, // out of memory aoqi@0: file_invalid, // invalid elf file aoqi@0: file_not_found, // could not found symbol file (on windows), such as jvm.pdb or jvm.map aoqi@0: helper_not_found, // could not load dbghelp.dll (Windows only) aoqi@0: helper_func_error, // decoding functions not found (Windows only) aoqi@0: helper_init_error // SymInitialize failed (Windows only) aoqi@0: }; aoqi@0: aoqi@0: // decode an pc address to corresponding function name and an offset from the beginning of aoqi@0: // the function aoqi@0: virtual bool decode(address pc, char* buf, int buflen, int* offset, aoqi@0: const char* modulepath = NULL) = 0; aoqi@0: virtual bool decode(address pc, char* buf, int buflen, int* offset, const void* base) = 0; aoqi@0: aoqi@0: // demangle a C++ symbol aoqi@0: virtual bool demangle(const char* symbol, char* buf, int buflen) = 0; aoqi@0: // if the decoder can decode symbols in vm aoqi@0: virtual bool can_decode_C_frame_in_vm() const = 0; aoqi@0: aoqi@0: virtual decoder_status status() const { aoqi@0: return _decoder_status; aoqi@0: } aoqi@0: aoqi@0: virtual bool has_error() const { aoqi@0: return is_error(_decoder_status); aoqi@0: } aoqi@0: aoqi@0: static bool is_error(decoder_status status) { aoqi@0: return (status > 0); aoqi@0: } aoqi@0: aoqi@0: protected: aoqi@0: decoder_status _decoder_status; aoqi@0: }; aoqi@0: aoqi@0: // Do nothing decoder aoqi@0: class NullDecoder : public AbstractDecoder { aoqi@0: public: aoqi@0: NullDecoder() { aoqi@0: _decoder_status = not_available; aoqi@0: } aoqi@0: aoqi@0: ~NullDecoder() {}; aoqi@0: aoqi@0: virtual bool decode(address pc, char* buf, int buflen, int* offset, aoqi@0: const char* modulepath = NULL) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: virtual bool decode(address pc, char* buf, int buflen, int* offset, const void* base) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: virtual bool demangle(const char* symbol, char* buf, int buflen) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: virtual bool can_decode_C_frame_in_vm() const { aoqi@0: return false; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: class Decoder : AllStatic { aoqi@0: public: aoqi@0: static bool decode(address pc, char* buf, int buflen, int* offset, const char* modulepath = NULL); aoqi@0: static bool decode(address pc, char* buf, int buflen, int* offset, const void* base); aoqi@0: static bool demangle(const char* symbol, char* buf, int buflen); aoqi@0: static bool can_decode_C_frame_in_vm(); aoqi@0: aoqi@0: // shutdown shared instance aoqi@0: static void shutdown(); aoqi@0: protected: aoqi@0: // shared decoder instance, _shared_instance_lock is needed aoqi@0: static AbstractDecoder* get_shared_instance(); aoqi@0: // a private instance for error handler. Error handler can be aoqi@0: // triggered almost everywhere, including signal handler, where aoqi@0: // no lock can be taken. So the shared decoder can not be used aoqi@0: // in this scenario. aoqi@0: static AbstractDecoder* get_error_handler_instance(); aoqi@0: aoqi@0: static AbstractDecoder* create_decoder(); aoqi@0: private: aoqi@0: static AbstractDecoder* _shared_decoder; aoqi@0: static AbstractDecoder* _error_handler_decoder; aoqi@0: static NullDecoder _do_nothing_decoder; aoqi@0: aoqi@0: protected: aoqi@0: static Mutex* _shared_decoder_lock; aoqi@0: static Mutex* shared_decoder_lock(); aoqi@0: aoqi@0: friend class DecoderLocker; aoqi@0: }; aoqi@0: aoqi@0: class DecoderLocker : public MutexLockerEx { aoqi@0: AbstractDecoder* _decoder; aoqi@0: inline bool is_first_error_thread(); aoqi@0: public: aoqi@0: DecoderLocker(); aoqi@0: AbstractDecoder* decoder() { aoqi@0: return _decoder; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_UTILITIES_DECODER_HPP