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/CLHSDB.java
41159 views
1
/*
2
* Copyright (c) 2005, 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;
26
27
import sun.jvm.hotspot.*;
28
import sun.jvm.hotspot.debugger.*;
29
30
import java.io.*;
31
import java.util.*;
32
33
public class CLHSDB {
34
35
public CLHSDB(JVMDebugger d) {
36
pid = -1;
37
execPath = null;
38
coreFilename = null;
39
debugServerName = null;
40
jvmDebugger = d;
41
}
42
43
public static void main(String[] args) {
44
new CLHSDB(args).run();
45
}
46
47
public void run() {
48
// If jvmDebugger is already set, we have been given a JVMDebugger.
49
// Otherwise, if pid != -1 we are supposed to attach to it.
50
// If execPath != null, it is the path of a jdk/bin/java
51
// and coreFilename is the pathname of a core file we are
52
// supposed to attach to.
53
// Finally, if debugServerName != null, we are supposed to
54
// connect to remote debug server.
55
56
agent = new HotSpotAgent();
57
58
Runtime.getRuntime().addShutdownHook(new java.lang.Thread() {
59
public void run() {
60
detachDebugger();
61
}
62
});
63
64
if (jvmDebugger != null) {
65
attachDebugger(jvmDebugger);
66
} else if (pid != -1) {
67
attachDebugger(pid);
68
} else if (execPath != null) {
69
attachDebugger(execPath, coreFilename);
70
} else if (debugServerName != null) {
71
connect(debugServerName);
72
}
73
74
75
CommandProcessor.DebuggerInterface di = new CommandProcessor.DebuggerInterface() {
76
public HotSpotAgent getAgent() {
77
return agent;
78
}
79
public boolean isAttached() {
80
return attached;
81
}
82
public void attach(int pid) {
83
attachDebugger(pid);
84
}
85
public void attach(String java, String core) {
86
attachDebugger(java, core);
87
}
88
public void attach(String debugServerName) {
89
connect(debugServerName);
90
}
91
public void detach() {
92
detachDebugger();
93
}
94
public void reattach() {
95
if (attached) {
96
detachDebugger();
97
}
98
if (pid != -1) {
99
attach(pid);
100
} else if (debugServerName != null) {
101
connect(debugServerName);
102
} else {
103
attach(execPath, coreFilename);
104
}
105
}
106
};
107
108
109
BufferedReader in =
110
new BufferedReader(new InputStreamReader(System.in));
111
CommandProcessor cp = new CommandProcessor(di, in, System.out, System.err);
112
cp.run(true);
113
114
}
115
116
//--------------------------------------------------------------------------------
117
// Internals only below this point
118
//
119
private HotSpotAgent agent;
120
private JVMDebugger jvmDebugger;
121
private boolean attached;
122
// These had to be made data members because they are referenced in inner classes.
123
private int pid;
124
private String execPath;
125
private String coreFilename;
126
private String debugServerName;
127
128
private void doUsage() {
129
System.out.println("Usage: java CLHSDB [[pid] | [path-to-java-executable [path-to-corefile]] | help ]");
130
System.out.println(" pid: attach to the process whose id is 'pid'");
131
System.out.println(" path-to-java-executable: Debug a core file produced by this program");
132
System.out.println(" path-to-corefile: Debug this corefile. The default is 'core'");
133
System.out.println(" If no arguments are specified, you can select what to do from the GUI.\n");
134
HotSpotAgent.showUsage();
135
}
136
137
private CLHSDB(String[] args) {
138
pid = -1;
139
execPath = null;
140
coreFilename = null;
141
debugServerName = null;
142
143
switch (args.length) {
144
case (0):
145
break;
146
147
case (1):
148
if (args[0].equals("help") || args[0].equals("-help")) {
149
doUsage();
150
return;
151
}
152
try {
153
// Attempt to attach as a PID
154
pid = Integer.parseInt(args[0]);
155
} catch (NumberFormatException e) {
156
// Attempt to connect to remote debug server
157
debugServerName = args[0];
158
}
159
break;
160
161
case (2):
162
execPath = args[0];
163
coreFilename = args[1];
164
break;
165
166
default:
167
System.out.println("HSDB Error: Too many options specified");
168
doUsage();
169
return;
170
}
171
}
172
173
private void attachDebugger(JVMDebugger d) {
174
agent.attach(d);
175
attached = true;
176
}
177
178
/** NOTE we are in a different thread here than either the main
179
thread or the Swing/AWT event handler thread, so we must be very
180
careful when creating or removing widgets */
181
private void attachDebugger(int pid) {
182
this.pid = pid;
183
try {
184
System.out.println("Attaching to process " + pid + ", please wait...");
185
186
// FIXME: display exec'd debugger's output messages during this
187
// lengthy call
188
agent.attach(pid);
189
attached = true;
190
}
191
catch (DebuggerException e) {
192
final String errMsg = formatMessage(e.getMessage(), 80);
193
System.err.println("Unable to connect to process ID " + pid + ":\n\n" + errMsg);
194
agent.detach();
195
e.printStackTrace();
196
return;
197
}
198
}
199
200
/** NOTE we are in a different thread here than either the main
201
thread or the Swing/AWT event handler thread, so we must be very
202
careful when creating or removing widgets */
203
private void attachDebugger(final String executablePath, final String corePath) {
204
// Try to open this core file
205
try {
206
System.out.println("Opening core file, please wait...");
207
208
// FIXME: display exec'd debugger's output messages during this
209
// lengthy call
210
agent.attach(executablePath, corePath);
211
attached = true;
212
}
213
catch (DebuggerException e) {
214
final String errMsg = formatMessage(e.getMessage(), 80);
215
System.err.println("Unable to open core file\n" + corePath + ":\n\n" + errMsg);
216
agent.detach();
217
e.printStackTrace();
218
return;
219
}
220
}
221
222
/** NOTE we are in a different thread here than either the main
223
thread or the Swing/AWT event handler thread, so we must be very
224
careful when creating or removing widgets */
225
private void connect(final String debugServerName) {
226
// Try to open this core file
227
try {
228
System.out.println("Connecting to debug server, please wait...");
229
agent.attach(debugServerName);
230
this.debugServerName = debugServerName;
231
attached = true;
232
}
233
catch (DebuggerException e) {
234
final String errMsg = formatMessage(e.getMessage(), 80);
235
System.err.println("Unable to connect to debug server \"" + debugServerName + "\":\n\n" + errMsg);
236
agent.detach();
237
e.printStackTrace();
238
return;
239
}
240
}
241
242
private void detachDebugger() {
243
if (!attached) {
244
return;
245
}
246
agent.detach();
247
attached = false;
248
}
249
250
private void detach() {
251
detachDebugger();
252
}
253
254
/** Punctuates the given string with \n's where necessary to not
255
exceed the given number of characters per line. Strips
256
extraneous whitespace. */
257
private String formatMessage(String message, int charsPerLine) {
258
StringBuilder buf = new StringBuilder(message.length());
259
StringTokenizer tokenizer = new StringTokenizer(message);
260
int curLineLength = 0;
261
while (tokenizer.hasMoreTokens()) {
262
String tok = tokenizer.nextToken();
263
if (curLineLength + tok.length() > charsPerLine) {
264
buf.append('\n');
265
curLineLength = 0;
266
} else {
267
if (curLineLength != 0) {
268
buf.append(' ');
269
++curLineLength;
270
}
271
}
272
buf.append(tok);
273
curLineLength += tok.length();
274
}
275
return buf.toString();
276
}
277
}
278
279