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 
  26 #ifndef SHARE_VM_UTILITIES_DECODER_HPP
  27 #define SHARE_VM_UTILITIES_DECODER_HPP
  28 
  29 #include "memory/allocation.hpp"
  30 #include "runtime/mutex.hpp"
  31 #include "runtime/mutexLocker.hpp"
  32 
  33 class AbstractDecoder : public CHeapObj<mtInternal> {
  34 public:
  35   // status code for decoding native C frame
  36   enum decoder_status {
  37          not_available = -10,  // real decoder is not available
  38          no_error = 0,         // successfully decoded frames
  39          out_of_memory,        // out of memory
  40          file_invalid,         // invalid elf file
  41          file_not_found,       // could not found symbol file (on windows), such as jvm.pdb or jvm.map
  42          helper_not_found,     // could not load dbghelp.dll (Windows only)
  43          helper_func_error,    // decoding functions not found (Windows only)
  44          helper_init_error     // SymInitialize failed (Windows only)
  45   };
  46 
  47   // decode an pc address to corresponding function name and an offset from the beginning of
  48   // the function
  49   //
  50   // Note: the 'base' variant does not demangle names. The
  51   // demangling that was done systematically in the 'modulepath' variant
  52   // is now optional.
  53   virtual bool decode(address pc, char* buf, int buflen, int* offset,
  54                       const char* modulepath = NULL, bool demangle = true) = 0;
  55   virtual bool decode(address pc, char* buf, int buflen, int* offset, const void* base) = 0;
  56 
  57   // demangle a C++ symbol
  58   virtual bool demangle(const char* symbol, char* buf, int buflen) = 0;
  59   // if the decoder can decode symbols in vm
  60   virtual bool can_decode_C_frame_in_vm() const = 0;
  61 
  62   virtual decoder_status status() const {
  63     return _decoder_status;
  64   }
  65 
  66   virtual bool has_error() const {
  67     return is_error(_decoder_status);
  68   }
  69 
  70   static bool is_error(decoder_status status) {
  71     return (status > 0);
  72   }
  73 
  74 protected:
  75   decoder_status  _decoder_status;
  76 };
  77 
  78 // Do nothing decoder
  79 class NullDecoder : public AbstractDecoder {
  80 public:
  81   NullDecoder() {
  82     _decoder_status = not_available;
  83   }
  84 
  85   ~NullDecoder() {};
  86 
  87   virtual bool decode(address pc, char* buf, int buflen, int* offset,
  88                       const char* modulepath, bool demangle) {
  89     return false;
  90   }
  91 
  92   virtual bool decode(address pc, char* buf, int buflen, int* offset, const void* base) {
  93     return false;
  94   }
  95 
  96   virtual bool demangle(const char* symbol, char* buf, int buflen) {
  97     return false;
  98   }
  99 
 100   virtual bool can_decode_C_frame_in_vm() const {
 101     return false;
 102   }
 103 };
 104 
 105 
 106 class Decoder : AllStatic {
 107 public:
 108   static bool decode(address pc, char* buf, int buflen, int* offset, const char* modulepath = NULL, bool demangle = true);
 109   static bool decode(address pc, char* buf, int buflen, int* offset, bool demangle) {
 110     return decode(pc, buf, buflen, offset, (const char*) NULL, demangle);
 111   }
 112   static bool decode(address pc, char* buf, int buflen, int* offset, const void* base);
 113   static bool demangle(const char* symbol, char* buf, int buflen);
 114   static bool can_decode_C_frame_in_vm();
 115 
 116   // shutdown shared instance
 117   static void shutdown();
 118 protected:
 119   // shared decoder instance, _shared_instance_lock is needed
 120   static AbstractDecoder* get_shared_instance();
 121   // a private instance for error handler. Error handler can be
 122   // triggered almost everywhere, including signal handler, where
 123   // no lock can be taken. So the shared decoder can not be used
 124   // in this scenario.
 125   static AbstractDecoder* get_error_handler_instance();
 126 
 127   static AbstractDecoder* create_decoder();
 128 private:
 129   static AbstractDecoder*     _shared_decoder;
 130   static AbstractDecoder*     _error_handler_decoder;
 131   static NullDecoder          _do_nothing_decoder;
 132 
 133 protected:
 134   static Mutex*               _shared_decoder_lock;
 135   static Mutex* shared_decoder_lock();
 136 
 137   friend class DecoderLocker;
 138 };
 139 
 140 class DecoderLocker : public MutexLockerEx {
 141   AbstractDecoder* _decoder;
 142   inline bool is_first_error_thread();
 143 public:
 144   DecoderLocker();
 145   AbstractDecoder* decoder() {
 146     return _decoder;
 147   }
 148 };
 149 
 150 #endif // SHARE_VM_UTILITIES_DECODER_HPP