1 /*
   2  * Copyright (c) 2008, 2019, 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 #ifndef SHARE_COMPILER_DISASSEMBLER_HPP
  26 #define SHARE_COMPILER_DISASSEMBLER_HPP
  27 
  28 #include "utilities/globalDefinitions.hpp"
  29 
  30 #include "asm/assembler.hpp"
  31 #include "asm/codeBuffer.hpp"
  32 #include "compiler/abstractDisassembler.hpp"
  33 #include "runtime/globals.hpp"
  34 #include "utilities/macros.hpp"
  35 
  36 class decode_env;
  37 
  38 // The disassembler prints out assembly code annotated
  39 // with Java specific information.
  40 
  41 // Disassembler inherits from AbstractDisassembler
  42 class Disassembler : public AbstractDisassembler {
  43   friend class decode_env;
  44  private:
  45   // this is the type of the dll entry point:
  46   typedef void* (*decode_func_virtual)(uintptr_t start_va, uintptr_t end_va,
  47                                unsigned char* buffer, uintptr_t length,
  48                                void* (*event_callback)(void*, const char*, void*),
  49                                void* event_stream,
  50                                int (*printf_callback)(void*, const char*, ...),
  51                                void* printf_stream,
  52                                const char* options,
  53                                int newline);
  54   // this is the type of the dll entry point for old version:
  55   typedef void* (*decode_func)(void* start_va, void* end_va,
  56                                void* (*event_callback)(void*, const char*, void*),
  57                                void* event_stream,
  58                                int (*printf_callback)(void*, const char*, ...),
  59                                void* printf_stream,
  60                                const char* options);
  61   // points to the library.
  62   static void*    _library;
  63   // bailout
  64   static bool     _tried_to_load_library;
  65   static bool     _library_usable;
  66   // points to the decode function.
  67   static decode_func_virtual _decode_instructions_virtual;
  68   static decode_func _decode_instructions;
  69 
  70   // tries to load library and return whether it succeeded.
  71   // Allow (diagnostic) output redirection.
  72   // No output at all if stream is NULL. Can be overridden
  73   // with -Verbose flag, in which case output goes to tty.
  74   static bool load_library(outputStream* st = NULL);
  75 
  76   // Check if the two addresses are on the same page.
  77   static bool is_same_page(address a1, address a2) {
  78     return (((uintptr_t)a1 ^ (uintptr_t)a2) & (~0x0fffUL)) == 0L;
  79   }
  80 
  81   // Machine dependent stuff
  82 #include CPU_HEADER(disassembler)
  83 
  84  public:
  85   // We can always decode code blobs.
  86   // Either we have a disassembler library available (successfully loaded)
  87   // or we will resort to the abstract disassembler. This method informs
  88   // about which decoding format is used.
  89   // We can also enforce using the abstract disassembler.
  90   static bool is_abstract() {
  91     if (!_tried_to_load_library /* && !UseAbstractDisassembler */) {
  92       load_library();
  93     }
  94     return ! _library_usable /* || UseAbstractDisassembler */;  // Not available until DecodeErrorFile is supported.
  95   }
  96 
  97   // Check out if we are doing a live disassembly or a post-mortem
  98   // disassembly where the binary data was loaded from a hs_err file.
  99   static bool is_decode_error_file() {
 100 // Activate once post-mortem disassembly (from hs-err file) is available.
 101 #if 0
 102     return DecodeErrorFile && (strlen(DecodeErrorFile) != 0);
 103 #else
 104     return false;
 105 #endif
 106   }
 107 
 108 #if defined(LUCY_OBSOLETE)  // Used in SAPJVM only
 109   // Have exactly one central method for printing one disassembly line.
 110   // Decode the one instruction @pc and return a pointer to the next instruction.
 111   static address decode_one(address pc, address begin, address end, const char* marker, outputStream* st);
 112 
 113   // Decode one instruction and return a pointer to the next instruction.
 114   // virtual_begin is used when decoding abstract disassemblies.
 115   static address decode_instruction(address begin, outputStream* st = NULL, address virtual_begin = 0);
 116 #endif
 117 
 118 #if defined(LUCY_OBSOLETE)  // Used in SAPJVM only
 119   // Decode a range (#bytes) around a given pc.
 120   // Mark the instruction at pc with '=>'.
 121   static void decode(address pc, unsigned int range, outputStream* st);
 122 #endif
 123 #if defined(LUCY_OBSOLETE)  // Used in SAPJVM only
 124   // Decode a range (#instructions) around a given pc.
 125   // On CICS platforms, it may prove difficult to step back the requested #instructions.
 126   // Mark the instruction at pc with '=>'.
 127   static void decode(address pc, int num_instr_before, int num_instr_after, outputStream* st);
 128 #endif
 129 
 130   // Directly disassemble code buffer.
 131   static void decode(CodeBuffer* cb, address start, address end, outputStream* st = NULL);
 132   // Directly disassemble code blob.
 133   static void decode(CodeBlob *cb,               outputStream* st = NULL, CodeStrings c = CodeStrings());
 134   // Directly disassemble nmethod.
 135   static void decode(nmethod* nm,                outputStream* st = NULL, CodeStrings c = CodeStrings());
 136   // Disassemble an arbitrary memory range.
 137   static void decode(address start, address end, outputStream* st = NULL, CodeStrings c = CodeStrings() /* , ptrdiff_t offset */);
 138 
 139   static void _hook(const char* file, int line, class MacroAssembler* masm);
 140 
 141   // This functions makes it easy to generate comments in the generated
 142   // interpreter code, by riding on the customary __ macro in the interpreter generator.
 143   // See templateTable_x86.cpp for an example.
 144   template<class T> inline static T* hook(const char* file, int line, T* masm) {
 145     if (PrintInterpreter) {
 146       _hook(file, line, masm);
 147     }
 148     return masm;
 149   }
 150 };
 151 
 152 #endif // SHARE_COMPILER_DISASSEMBLER_HPP