1 /*
   2  * Copyright (c) 1997, 2017, 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 #ifndef _WINDOWS
  32 #if defined(__APPLE__)
  33   #include "decoder_machO.hpp"
  34 #elif defined(AIX)
  35   #include "decoder_aix.hpp"
  36 #else
  37   #include "decoder_elf.hpp"
  38 #endif
  39 
  40 AbstractDecoder*  Decoder::_shared_decoder = NULL;
  41 AbstractDecoder*  Decoder::_error_handler_decoder = NULL;
  42 NullDecoder       Decoder::_do_nothing_decoder;
  43 Mutex*            Decoder::_shared_decoder_lock = new Mutex(Mutex::native,
  44                                 "SharedDecoderLock",
  45                                 false,
  46                                 Monitor::_safepoint_check_never);
  47 
  48 AbstractDecoder* Decoder::get_shared_instance() {
  49   assert(_shared_decoder_lock != NULL && _shared_decoder_lock->owned_by_self(),
  50     "Require DecoderLock to enter");
  51 
  52   if (_shared_decoder == NULL) {
  53     _shared_decoder = create_decoder();
  54   }
  55   return _shared_decoder;
  56 }
  57 
  58 AbstractDecoder* Decoder::get_error_handler_instance() {
  59   if (_error_handler_decoder == NULL) {
  60     _error_handler_decoder = create_decoder();
  61   }
  62   return _error_handler_decoder;
  63 }
  64 
  65 
  66 AbstractDecoder* Decoder::create_decoder() {
  67   AbstractDecoder* decoder;
  68 #if defined (__APPLE__)
  69   decoder = new (std::nothrow)MachODecoder();
  70 #elif defined(AIX)
  71   decoder = new (std::nothrow)AIXDecoder();
  72 #else
  73   decoder = new (std::nothrow)ElfDecoder();
  74 #endif
  75 
  76   if (decoder == NULL || decoder->has_error()) {
  77     if (decoder != NULL) {
  78       delete decoder;
  79     }
  80     decoder = &_do_nothing_decoder;
  81   }
  82   return decoder;
  83 }
  84 
  85 inline bool DecoderLocker::is_first_error_thread() {
  86   return (os::current_thread_id() == VMError::get_first_error_tid());
  87 }
  88 
  89 DecoderLocker::DecoderLocker() :
  90   MutexLockerEx(DecoderLocker::is_first_error_thread() ?
  91                 NULL : Decoder::shared_decoder_lock(), true) {
  92   _decoder = is_first_error_thread() ?
  93     Decoder::get_error_handler_instance() : Decoder::get_shared_instance();
  94   assert(_decoder != NULL, "null decoder");
  95 }
  96 
  97 Mutex* Decoder::shared_decoder_lock() {
  98   assert(_shared_decoder_lock != NULL, "Just check");
  99   return _shared_decoder_lock;
 100 }
 101 
 102 bool Decoder::decode(address addr, char* buf, int buflen, int* offset, const char* modulepath, bool demangle) {
 103   assert(_shared_decoder_lock != NULL, "Just check");
 104   bool error_handling_thread = os::current_thread_id() == VMError::first_error_tid;
 105   MutexLockerEx locker(error_handling_thread ? NULL : _shared_decoder_lock, true);
 106   AbstractDecoder* decoder = error_handling_thread ?
 107     get_error_handler_instance(): get_shared_instance();
 108   assert(decoder != NULL, "null decoder");
 109 
 110   return decoder->decode(addr, buf, buflen, offset, modulepath, demangle);
 111 }
 112 
 113 bool Decoder::decode(address addr, char* buf, int buflen, int* offset, const void* base) {
 114   assert(_shared_decoder_lock != NULL, "Just check");
 115   bool error_handling_thread = os::current_thread_id() == VMError::first_error_tid;
 116   MutexLockerEx locker(error_handling_thread ? NULL : _shared_decoder_lock, true);
 117   AbstractDecoder* decoder = error_handling_thread ?
 118     get_error_handler_instance(): get_shared_instance();
 119   assert(decoder != NULL, "null decoder");
 120 
 121   return decoder->decode(addr, buf, buflen, offset, base);
 122 }
 123 
 124 
 125 bool Decoder::demangle(const char* symbol, char* buf, int buflen) {
 126   assert(_shared_decoder_lock != NULL, "Just check");
 127   bool error_handling_thread = os::current_thread_id() == VMError::first_error_tid;
 128   MutexLockerEx locker(error_handling_thread ? NULL : _shared_decoder_lock, true);
 129   AbstractDecoder* decoder = error_handling_thread ?
 130     get_error_handler_instance(): get_shared_instance();
 131   assert(decoder != NULL, "null decoder");
 132   return decoder->demangle(symbol, buf, buflen);
 133 }
 134 
 135 void Decoder::print_state_on(outputStream* st) {
 136 }
 137 
 138 bool Decoder::get_source_info(address pc, char* buf, size_t buflen, int* line) {
 139   return false;
 140 }
 141 
 142 #endif // !_WINDOWS
 143