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.
is_same_page(address a1,address a2)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.
is_abstract()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.
is_decode_error_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   // Directly disassemble code buffer.
109   static void decode(CodeBuffer* cb, address start, address end, outputStream* st = NULL);
110   // Directly disassemble code blob.
111   static void decode(CodeBlob *cb,               outputStream* st = NULL, CodeStrings c = CodeStrings());
112   // Directly disassemble nmethod.
113   static void decode(nmethod* nm,                outputStream* st = NULL, CodeStrings c = CodeStrings());
114   // Disassemble an arbitrary memory range.
115   static void decode(address start, address end, outputStream* st = NULL, CodeStrings c = CodeStrings() /* , ptrdiff_t offset */);
116 
117   static void _hook(const char* file, int line, class MacroAssembler* masm);
118 
119   // This functions makes it easy to generate comments in the generated
120   // interpreter code, by riding on the customary __ macro in the interpreter generator.
121   // See templateTable_x86.cpp for an example.
hook(const char * file,int line,T * masm)122   template<class T> inline static T* hook(const char* file, int line, T* masm) {
123     if (PrintInterpreter) {
124       _hook(file, line, masm);
125     }
126     return masm;
127   }
128 };
129 
130 #endif // SHARE_COMPILER_DISASSEMBLER_HPP
131