Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/asm/Disassembler.java
41161 views
1
/*
2
* Copyright (c) 2002, 2021, 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
package sun.jvm.hotspot.asm;
26
27
import java.io.PrintStream;
28
import java.nio.file.Path;
29
import java.util.List;
30
import java.util.Iterator;
31
import java.util.Properties;
32
import sun.jvm.hotspot.code.CodeBlob;
33
import sun.jvm.hotspot.code.NMethod;
34
import sun.jvm.hotspot.debugger.Address;
35
import sun.jvm.hotspot.debugger.DebuggerException;
36
import sun.jvm.hotspot.runtime.VM;
37
38
public class Disassembler {
39
private static String options = "";
40
private static long decode_function;
41
42
protected long startPc;
43
protected byte[] code;
44
private CodeBlob blob;
45
private NMethod nmethod;
46
47
public static void decode(InstructionVisitor visitor, CodeBlob blob) {
48
decode(visitor, blob, blob.codeBegin(), blob.codeEnd());
49
}
50
51
public static void decode(InstructionVisitor visitor, CodeBlob blob, Address begin, Address end) {
52
int codeSize = (int)end.minus(begin);
53
long startPc = VM.getAddressValue(begin);
54
byte[] code = new byte[codeSize];
55
for (int i = 0; i < code.length; i++)
56
code[i] = begin.getJByteAt(i);
57
Disassembler dis = new Disassembler(startPc, code);
58
dis.decode(visitor);
59
}
60
61
private Disassembler(long startPc, byte[] code) {
62
this.startPc = startPc;
63
this.code = code;
64
65
// Lazily load hsdis
66
if (decode_function == 0) {
67
// Search for hsdis library in the following 4 locations:
68
// 1. <home>/lib/<vm>/libhsdis-<arch>.so
69
// 2. <home>/lib/<vm>/hsdis-<arch>.so
70
// 3. <home>/lib/hsdis-<arch>.so
71
// 4. hsdis-<arch>.so (using LD_LIBRARY_PATH)
72
Properties targetSysProps = VM.getVM().getSystemProperties();
73
String os = targetSysProps.getProperty("os.name");
74
String ext = ".so";
75
if (os.contains("Windows")) {
76
ext = ".dll";
77
} else if (os.contains("Mac OS")) {
78
ext = ".dylib";
79
}
80
81
// Find the full path to libjvm.so (jvm.dll and libjvm.dylib on Windows and OSX).
82
String jvmPattern = "^(lib)?jvm\\" + ext + "$";
83
Path jvmPath = VM.getVM()
84
.getDebugger()
85
.getCDebugger()
86
.getLoadObjectList()
87
.stream()
88
.map(o -> Path.of(o.getName()))
89
.filter(p -> p.getFileName().toString().matches(jvmPattern))
90
.findAny()
91
.get();
92
93
String arch = targetSysProps.getProperty("os.arch");
94
String libname = "hsdis-" + arch + ext;
95
96
List<String> libs = List.of(
97
// 1. <home>/lib/<vm>/libhsdis-<arch>.so
98
jvmPath.resolveSibling("lib" + libname).toString(),
99
// 2. <home>/lib/<vm>/hsdis-<arch>.so
100
jvmPath.resolveSibling(libname).toString(),
101
// 3. <home>/lib/hsdis-<arch>.so
102
jvmPath.getParent().resolveSibling(libname).toString(),
103
// 4. hsdis-<arch>.so (using LD_LIBRARY_PATH)
104
libname
105
);
106
107
var itr = libs.iterator();
108
while (itr.hasNext() && (decode_function == 0L)) {
109
try {
110
decode_function = load_library(itr.next());
111
} catch (DebuggerException e) {
112
if (!itr.hasNext()) {
113
throw e;
114
}
115
}
116
}
117
}
118
}
119
120
private static native long load_library(String hsdis_library_name);
121
122
private native void decode(InstructionVisitor visitor, long pc, byte[] code,
123
String options, long decode_function);
124
125
private void decode(InstructionVisitor visitor) {
126
visitor.prologue();
127
decode(visitor, startPc, code, options, decode_function);
128
visitor.epilogue();
129
}
130
131
private boolean match(String event, String tag) {
132
if (!event.startsWith(tag))
133
return false;
134
int taglen = tag.length();
135
if (taglen == event.length()) return true;
136
char delim = event.charAt(taglen);
137
return delim == ' ' || delim == '/' || delim == '=';
138
}
139
140
// This is called from the native code to process various markers
141
// in the dissassembly.
142
private long handleEvent(InstructionVisitor visitor, String event, long arg) {
143
if (match(event, "insn")) {
144
try {
145
visitor.beginInstruction(arg);
146
} catch (Throwable e) {
147
e.printStackTrace();
148
}
149
} else if (match(event, "/insn")) {
150
try {
151
visitor.endInstruction(arg);
152
} catch (Throwable e) {
153
e.printStackTrace();
154
}
155
} else if (match(event, "addr")) {
156
if (arg != 0) {
157
visitor.printAddress(arg);
158
}
159
return arg;
160
} else if (match(event, "mach")) {
161
// output().printf("[Disassembling for mach='%s']\n", arg);
162
} else {
163
// ignore unrecognized markup
164
}
165
return 0;
166
}
167
168
// This called from the native code to perform printing
169
private void rawPrint(InstructionVisitor visitor, String s) {
170
visitor.print(s);
171
}
172
}
173
174