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 "code/codeBlob.hpp"
32 #include "code/nmethod.hpp"
33 #include "compiler/abstractDisassembler.hpp"
34 #include "runtime/globals.hpp"
35 #include "utilities/macros.hpp"
36 
37 class decode_env;
38 
39 // The disassembler prints out assembly code annotated
40 // with Java specific information.
41 
42 // Disassembler inherits from AbstractDisassembler
43 class Disassembler : public AbstractDisassembler {
44   friend class decode_env;
45  private:
46   // this is the type of the dll entry point:
47   typedef void* (*decode_func_virtual)(uintptr_t start_va, uintptr_t end_va,
48                                unsigned char* buffer, uintptr_t length,
49                                void* (*event_callback)(void*, const char*, void*),
50                                void* event_stream,
51                                int (*printf_callback)(void*, const char*, ...),
52                                void* printf_stream,
53                                const char* options,
54                                int newline);
55   // points to the library.
56   static void*    _library;
57   // bailout
58   static bool     _tried_to_load_library;
59   static bool     _library_usable;
60   // points to the decode function.
61   static decode_func_virtual _decode_instructions_virtual;
62 
63   // tries to load library and return whether it succeeded.
64   // Allow (diagnostic) output redirection.
65   // No output at all if stream is NULL. Can be overridden
66   // with -Verbose flag, in which case output goes to tty.
67   static bool load_library(outputStream* st = NULL);
68 
69   // Check if the two addresses are on the same page.
is_same_page(address a1,address a2)70   static bool is_same_page(address a1, address a2) {
71     return (((uintptr_t)a1 ^ (uintptr_t)a2) & (~0x0fffUL)) == 0L;
72   }
73 
74   // Machine dependent stuff
75 #include CPU_HEADER(disassembler)
76 
77  public:
78   // We can always decode code blobs.
79   // Either we have a disassembler library available (successfully loaded)
80   // or we will resort to the abstract disassembler. This method informs
81   // about which decoding format is used.
82   // We can also enforce using the abstract disassembler.
is_abstract()83   static bool is_abstract() {
84     if (!_tried_to_load_library) {
85       load_library();
86     }
87     return ! _library_usable;
88   }
89 
90   // Check out if we are doing a live disassembly or a post-mortem
91   // disassembly where the binary data was loaded from a hs_err file.
is_decode_error_file()92   static bool is_decode_error_file() {
93 // Activate once post-mortem disassembly (from hs-err file) is available.
94 #if 0
95     return DecodeErrorFile && (strlen(DecodeErrorFile) != 0);
96 #else
97     return false;
98 #endif
99   }
100 
101   // Directly disassemble code blob.
102   static void decode(CodeBlob *cb,               outputStream* st = NULL);
103   // Directly disassemble nmethod.
104   static void decode(nmethod* nm,                outputStream* st = NULL);
105   // Disassemble an arbitrary memory range.
106   static void decode(address start, address end, outputStream* st = NULL, const CodeStrings* = NULL);
107 
108   static void _hook(const char* file, int line, class MacroAssembler* masm);
109 
110   // This functions makes it easy to generate comments in the generated
111   // interpreter code, by riding on the customary __ macro in the interpreter generator.
112   // See templateTable_x86.cpp for an example.
hook(const char * file,int line,T * masm)113   template<class T> inline static T* hook(const char* file, int line, T* masm) {
114     if (PrintInterpreter) {
115       _hook(file, line, masm);
116     }
117     return masm;
118   }
119 };
120 
121 #endif // SHARE_COMPILER_DISASSEMBLER_HPP
122