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/tools/Tool.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.tools;
26
27
import java.io.PrintStream;
28
29
import sun.jvm.hotspot.HotSpotAgent;
30
import sun.jvm.hotspot.debugger.DebuggerException;
31
import sun.jvm.hotspot.debugger.JVMDebugger;
32
import sun.jvm.hotspot.runtime.VM;
33
34
// generic command line or GUI tool.
35
// override run & code main as shown below.
36
37
public abstract class Tool implements Runnable {
38
private HotSpotAgent agent;
39
private JVMDebugger jvmDebugger;
40
private int debugeeType;
41
42
// debugeeType is one of constants below
43
protected static final int DEBUGEE_PID = 0;
44
protected static final int DEBUGEE_CORE = 1;
45
protected static final int DEBUGEE_REMOTE = 2;
46
47
public Tool() {
48
}
49
50
public Tool(JVMDebugger d) {
51
jvmDebugger = d;
52
}
53
54
public Tool(HotSpotAgent agent) {
55
this.agent = agent;
56
if (agent == null) {
57
jvmDebugger = null;
58
debugeeType = -1;
59
} else {
60
jvmDebugger = agent.getDebugger();
61
debugeeType = switch (agent.getStartupMode()) {
62
case HotSpotAgent.PROCESS_MODE -> DEBUGEE_PID;
63
case HotSpotAgent.CORE_FILE_MODE -> DEBUGEE_CORE;
64
case HotSpotAgent.REMOTE_MODE -> DEBUGEE_REMOTE;
65
default -> throw new IllegalStateException("Invalid attach mode");
66
};
67
}
68
}
69
70
public String getName() {
71
return getClass().getName();
72
}
73
74
protected boolean needsJavaPrefix() {
75
return true;
76
}
77
78
protected void setAgent(HotSpotAgent a) {
79
agent = a;
80
}
81
82
protected void setDebugeeType(int dt) {
83
debugeeType = dt;
84
}
85
86
protected HotSpotAgent getAgent() {
87
return agent;
88
}
89
90
protected int getDebugeeType() {
91
return debugeeType;
92
}
93
94
protected void printUsage() {
95
String name = null;
96
if (needsJavaPrefix()) {
97
name = "java " + getName();
98
} else {
99
name = getName();
100
}
101
System.out.println("Usage: " + name + " [option] <pid>");
102
System.out.println("\t\t(to connect to a live java process)");
103
System.out.println(" or " + name + " [option] <executable> <core>");
104
System.out.println("\t\t(to connect to a core file)");
105
System.out.println(" or " + name + " [option] [server_id@]<remote server IP or hostname>");
106
System.out.println("\t\t(to connect to a remote debug server)");
107
System.out.println();
108
System.out.println("where option must be one of:");
109
printFlagsUsage();
110
}
111
112
protected void printFlagsUsage() {
113
System.out.println(" -h | -help\tto print this help message");
114
}
115
116
protected void usage() {
117
printUsage();
118
}
119
120
/*
121
Derived class main should be of the following form:
122
123
public static void main(String[] args) {
124
<derived class> obj = new <derived class>;
125
obj.execute(args);
126
}
127
128
*/
129
130
protected void execute(String[] args) {
131
int returnStatus = 1;
132
133
try {
134
returnStatus = start(args);
135
} catch (Throwable t) {
136
t.printStackTrace(System.err);
137
} finally {
138
stop();
139
}
140
141
// Exit with 0 or 1
142
System.exit(returnStatus);
143
}
144
145
public void stop() {
146
if (agent != null) {
147
agent.detach();
148
}
149
}
150
151
private int start(String[] args) {
152
153
if ((args.length < 1) || (args.length > 2)) {
154
usage();
155
return 1;
156
}
157
158
// Attempt to handle -h or -help or some invalid flag
159
if (args[0].startsWith("-h")) {
160
usage();
161
return 0;
162
} else if (args[0].startsWith("-")) {
163
usage();
164
return 1;
165
}
166
167
PrintStream err = System.err;
168
PrintStream out = System.out;
169
170
int pid = 0;
171
String coreFileName = null;
172
String executableName = null;
173
String remoteServer = null;
174
175
switch (args.length) {
176
case 1:
177
try {
178
pid = Integer.parseInt(args[0]);
179
debugeeType = DEBUGEE_PID;
180
} catch (NumberFormatException e) {
181
// try remote server
182
remoteServer = args[0];
183
debugeeType = DEBUGEE_REMOTE;
184
}
185
break;
186
187
case 2:
188
executableName = args[0];
189
coreFileName = args[1];
190
debugeeType = DEBUGEE_CORE;
191
break;
192
193
default:
194
usage();
195
return 1;
196
}
197
198
agent = new HotSpotAgent();
199
try {
200
switch (debugeeType) {
201
case DEBUGEE_PID:
202
out.println("Attaching to process ID " + pid + ", please wait...");
203
agent.attach(pid);
204
break;
205
206
case DEBUGEE_CORE:
207
out.println("Attaching to core " + coreFileName +
208
" from executable " + executableName + ", please wait...");
209
agent.attach(executableName, coreFileName);
210
break;
211
212
case DEBUGEE_REMOTE:
213
out.println("Attaching to remote server " + remoteServer + ", please wait...");
214
agent.attach(remoteServer);
215
break;
216
}
217
}
218
catch (DebuggerException e) {
219
switch (debugeeType) {
220
case DEBUGEE_PID:
221
err.print("Error attaching to process: ");
222
break;
223
224
case DEBUGEE_CORE:
225
err.print("Error attaching to core file: ");
226
break;
227
228
case DEBUGEE_REMOTE:
229
err.print("Error attaching to remote server: ");
230
break;
231
}
232
if (e.getMessage() != null) {
233
err.println(e.getMessage());
234
e.printStackTrace();
235
}
236
err.println();
237
return 1;
238
}
239
240
out.println("Debugger attached successfully.");
241
startInternal();
242
return 0;
243
}
244
245
// When using an existing JVMDebugger.
246
public void start() {
247
248
if (jvmDebugger == null) {
249
throw new RuntimeException("Tool.start() called with no JVMDebugger set.");
250
}
251
agent = new HotSpotAgent();
252
agent.attach(jvmDebugger);
253
startInternal();
254
}
255
256
// Remains of the start mechanism, common to both start methods.
257
private void startInternal() {
258
259
PrintStream out = System.out;
260
VM vm = VM.getVM();
261
if (vm.isCore()) {
262
out.println("Core build detected.");
263
} else if (vm.isClientCompiler()) {
264
out.println("Client compiler detected.");
265
} else if (vm.isServerCompiler()) {
266
out.println("Server compiler detected.");
267
} else {
268
throw new RuntimeException("Fatal error: "
269
+ "should have been able to detect core/C1/C2 build");
270
}
271
272
String version = vm.getVMRelease();
273
if (version != null) {
274
out.print("JVM version is ");
275
out.println(version);
276
}
277
278
run();
279
}
280
}
281
282