1 /*
   2  * Copyright (c) 1997, 2010, 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/mutexLocker.hpp"
  28 #include "utilities/decoder.hpp"
  29 
  30 #if defined(_WINDOWS)
  31   #include "decoder_windows.hpp"
  32 #elif defined(__APPLE__)
  33   #include "decoder_machO.hpp"
  34 #else
  35   #include "decoder_elf.hpp"
  36 #endif
  37 
  38 NullDecoder*  Decoder::_decoder = NULL;
  39 NullDecoder   Decoder::_do_nothing_decoder;
  40 Mutex*           Decoder::_decoder_lock = new Mutex(Mutex::safepoint,
  41                                 "DecoderLock");
  42 
  43 // _decoder_lock should already acquired before enter this method
  44 NullDecoder* Decoder::get_decoder() {
  45   assert(_decoder_lock != NULL && _decoder_lock->owned_by_self(),
  46     "Require DecoderLock to enter");
  47 
  48   if (_decoder != NULL) {
  49     return _decoder;
  50   }
  51 
  52   // Decoder is a secondary service. Although, it is good to have,
  53   // but we can live without it. 
  54 #if defined(_WINDOWS)
  55   _decoder = new (std::nothrow) WindowsDecoder();
  56 #elif defined (__APPLE__) 
  57     _decoder = new (std::nothrow)MachODecoder();
  58 #else
  59     _decoder = new (std::nothrow)ElfDecoder();
  60 #endif
  61 
  62   if (_decoder == NULL || _decoder->has_error()) {
  63     if (_decoder != NULL) {
  64       delete _decoder;
  65     }
  66     _decoder = &_do_nothing_decoder;
  67   }
  68   return _decoder;
  69 }
  70 
  71 bool Decoder::decode(address addr, char* buf, int buflen, int* offset, const char* modulepath) {
  72   assert(_decoder_lock != NULL, "Just check");
  73   MutexLockerEx locker(_decoder_lock, true);
  74   NullDecoder* decoder = get_decoder();
  75   assert(decoder != NULL, "null decoder");
  76 
  77   return decoder->decode(addr, buf, buflen, offset, modulepath);
  78 }
  79 
  80 bool Decoder::demangle(const char* symbol, char* buf, int buflen) {
  81   assert(_decoder_lock != NULL, "Just check");
  82   MutexLockerEx locker(_decoder_lock, true);
  83   NullDecoder* decoder = get_decoder();
  84   assert(decoder != NULL, "null decoder");
  85   return decoder->demangle(symbol, buf, buflen);
  86 }
  87 
  88 bool Decoder::can_decode_C_frame_in_vm() {
  89   assert(_decoder_lock != NULL, "Just check");
  90   MutexLockerEx locker(_decoder_lock, true);
  91   NullDecoder* decoder = get_decoder();
  92   assert(decoder != NULL, "null decoder");
  93   return decoder->can_decode_C_frame_in_vm();
  94 }
  95 
  96 // shutdown real decoder and replace it with
  97 // _do_nothing_decoder
  98 void Decoder::shutdown() {
  99   assert(_decoder_lock != NULL, "Just check");
 100   MutexLockerEx locker(_decoder_lock, true);
 101 
 102   if (_decoder != NULL && _decoder != &_do_nothing_decoder) {
 103     delete _decoder;
 104   }
 105 
 106   _decoder = &_do_nothing_decoder;
 107 }