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