Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java
41161 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.tools.attach;
27
28
import com.sun.tools.attach.AttachNotSupportedException;
29
import com.sun.tools.attach.VirtualMachine;
30
import com.sun.tools.attach.AgentLoadException;
31
import com.sun.tools.attach.AgentInitializationException;
32
import com.sun.tools.attach.spi.AttachProvider;
33
import jdk.internal.misc.VM;
34
35
import java.io.BufferedReader;
36
import java.io.InputStream;
37
import java.io.IOException;
38
import java.io.InputStreamReader;
39
import java.security.AccessController;
40
import java.security.PrivilegedAction;
41
import java.util.Properties;
42
import java.util.stream.Collectors;
43
44
/*
45
* The HotSpot implementation of com.sun.tools.attach.VirtualMachine.
46
*/
47
48
@SuppressWarnings("removal")
49
public abstract class HotSpotVirtualMachine extends VirtualMachine {
50
51
private static final long CURRENT_PID;
52
private static final boolean ALLOW_ATTACH_SELF;
53
static {
54
PrivilegedAction<ProcessHandle> pa = ProcessHandle::current;
55
CURRENT_PID = AccessController.doPrivileged(pa).pid();
56
57
String s = VM.getSavedProperty("jdk.attach.allowAttachSelf");
58
ALLOW_ATTACH_SELF = "".equals(s) || Boolean.parseBoolean(s);
59
}
60
61
HotSpotVirtualMachine(AttachProvider provider, String id)
62
throws AttachNotSupportedException, IOException
63
{
64
super(provider, id);
65
66
int pid;
67
try {
68
pid = Integer.parseInt(id);
69
} catch (NumberFormatException e) {
70
throw new AttachNotSupportedException("Invalid process identifier");
71
}
72
73
// The tool should be a different VM to the target. This check will
74
// eventually be enforced by the target VM.
75
if (!ALLOW_ATTACH_SELF && (pid == 0 || pid == CURRENT_PID)) {
76
throw new IOException("Can not attach to current VM");
77
}
78
}
79
80
/*
81
* Load agent library
82
* If isAbsolute is true then the agent library is the absolute path
83
* to the library and thus will not be expanded in the target VM.
84
* if isAbsolute is false then the agent library is just a library
85
* name and it will be expended in the target VM.
86
*/
87
private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options)
88
throws AgentLoadException, AgentInitializationException, IOException
89
{
90
if (agentLibrary == null) {
91
throw new NullPointerException("agentLibrary cannot be null");
92
}
93
94
String msgPrefix = "return code: ";
95
InputStream in = execute("load",
96
agentLibrary,
97
isAbsolute ? "true" : "false",
98
options);
99
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
100
String result = reader.readLine();
101
if (result == null) {
102
throw new AgentLoadException("Target VM did not respond");
103
} else if (result.startsWith(msgPrefix)) {
104
int retCode = Integer.parseInt(result.substring(msgPrefix.length()));
105
if (retCode != 0) {
106
throw new AgentInitializationException("Agent_OnAttach failed", retCode);
107
}
108
} else {
109
throw new AgentLoadException(result);
110
}
111
}
112
}
113
114
/*
115
* Load agent library - library name will be expanded in target VM
116
*/
117
public void loadAgentLibrary(String agentLibrary, String options)
118
throws AgentLoadException, AgentInitializationException, IOException
119
{
120
loadAgentLibrary(agentLibrary, false, options);
121
}
122
123
/*
124
* Load agent - absolute path of library provided to target VM
125
*/
126
public void loadAgentPath(String agentLibrary, String options)
127
throws AgentLoadException, AgentInitializationException, IOException
128
{
129
loadAgentLibrary(agentLibrary, true, options);
130
}
131
132
/*
133
* Load JPLIS agent which will load the agent JAR file and invoke
134
* the agentmain method.
135
*/
136
public void loadAgent(String agent, String options)
137
throws AgentLoadException, AgentInitializationException, IOException
138
{
139
if (agent == null) {
140
throw new NullPointerException("agent cannot be null");
141
}
142
143
String args = agent;
144
if (options != null) {
145
args = args + "=" + options;
146
}
147
try {
148
loadAgentLibrary("instrument", args);
149
} catch (AgentInitializationException x) {
150
/*
151
* Translate interesting errors into the right exception and
152
* message (FIXME: create a better interface to the instrument
153
* implementation so this isn't necessary)
154
*/
155
int rc = x.returnValue();
156
switch (rc) {
157
case JNI_ENOMEM:
158
throw new AgentLoadException("Insuffient memory");
159
case ATTACH_ERROR_BADJAR:
160
throw new AgentLoadException(
161
"Agent JAR not found or no Agent-Class attribute");
162
case ATTACH_ERROR_NOTONCP:
163
throw new AgentLoadException(
164
"Unable to add JAR file to system class path");
165
case ATTACH_ERROR_STARTFAIL:
166
throw new AgentInitializationException(
167
"Agent JAR loaded but agent failed to initialize");
168
default :
169
throw new AgentLoadException("" +
170
"Failed to load agent - unknown reason: " + rc);
171
}
172
}
173
}
174
175
/*
176
* The possible errors returned by JPLIS's agentmain
177
*/
178
private static final int JNI_ENOMEM = -4;
179
private static final int ATTACH_ERROR_BADJAR = 100;
180
private static final int ATTACH_ERROR_NOTONCP = 101;
181
private static final int ATTACH_ERROR_STARTFAIL = 102;
182
183
184
/*
185
* Send "properties" command to target VM
186
*/
187
public Properties getSystemProperties() throws IOException {
188
InputStream in = null;
189
Properties props = new Properties();
190
try {
191
in = executeCommand("properties");
192
props.load(in);
193
} finally {
194
if (in != null) in.close();
195
}
196
return props;
197
}
198
199
public Properties getAgentProperties() throws IOException {
200
InputStream in = null;
201
Properties props = new Properties();
202
try {
203
in = executeCommand("agentProperties");
204
props.load(in);
205
} finally {
206
if (in != null) in.close();
207
}
208
return props;
209
}
210
211
private static final String MANAGEMENT_PREFIX = "com.sun.management.";
212
213
private static boolean checkedKeyName(Object key) {
214
if (!(key instanceof String)) {
215
throw new IllegalArgumentException("Invalid option (not a String): "+key);
216
}
217
if (!((String)key).startsWith(MANAGEMENT_PREFIX)) {
218
throw new IllegalArgumentException("Invalid option: "+key);
219
}
220
return true;
221
}
222
223
private static String stripKeyName(Object key) {
224
return ((String)key).substring(MANAGEMENT_PREFIX.length());
225
}
226
227
@Override
228
public void startManagementAgent(Properties agentProperties) throws IOException {
229
if (agentProperties == null) {
230
throw new NullPointerException("agentProperties cannot be null");
231
}
232
// Convert the arguments into arguments suitable for the Diagnostic Command:
233
// "ManagementAgent.start jmxremote.port=5555 jmxremote.authenticate=false"
234
String args = agentProperties.entrySet().stream()
235
.filter(entry -> checkedKeyName(entry.getKey()))
236
.map(entry -> stripKeyName(entry.getKey()) + "=" + escape(entry.getValue()))
237
.collect(Collectors.joining(" "));
238
executeJCmd("ManagementAgent.start " + args).close();
239
}
240
241
private String escape(Object arg) {
242
String value = arg.toString();
243
if (value.contains(" ")) {
244
return "'" + value + "'";
245
}
246
return value;
247
}
248
249
@Override
250
public String startLocalManagementAgent() throws IOException {
251
executeJCmd("ManagementAgent.start_local").close();
252
String prop = MANAGEMENT_PREFIX + "jmxremote.localConnectorAddress";
253
return getAgentProperties().getProperty(prop);
254
}
255
256
257
// --- HotSpot specific methods ---
258
259
// same as SIGQUIT
260
public void localDataDump() throws IOException {
261
executeCommand("datadump").close();
262
}
263
264
// Remote ctrl-break. The output of the ctrl-break actions can
265
// be read from the input stream.
266
public InputStream remoteDataDump(Object ... args) throws IOException {
267
return executeCommand("threaddump", args);
268
}
269
270
// Remote heap dump. The output (error message) can be read from the
271
// returned input stream.
272
public InputStream dumpHeap(Object ... args) throws IOException {
273
return executeCommand("dumpheap", args);
274
}
275
276
// Heap histogram (heap inspection in HotSpot)
277
public InputStream heapHisto(Object ... args) throws IOException {
278
return executeCommand("inspectheap", args);
279
}
280
281
// set JVM command line flag
282
public InputStream setFlag(String name, String value) throws IOException {
283
return executeCommand("setflag", name, value);
284
}
285
286
// print command line flag
287
public InputStream printFlag(String name) throws IOException {
288
return executeCommand("printflag", name);
289
}
290
291
public InputStream executeJCmd(String command) throws IOException {
292
return executeCommand("jcmd", command);
293
}
294
295
296
// -- Supporting methods
297
298
/*
299
* Execute the given command in the target VM - specific platform
300
* implementation must implement this.
301
*/
302
abstract InputStream execute(String cmd, Object ... args)
303
throws AgentLoadException, IOException;
304
305
/*
306
* Convenience method for simple commands
307
*/
308
public InputStream executeCommand(String cmd, Object ... args) throws IOException {
309
try {
310
return execute(cmd, args);
311
} catch (AgentLoadException x) {
312
throw new InternalError("Should not get here", x);
313
}
314
}
315
316
317
/*
318
* Utility method to read an 'int' from the input stream. Ideally
319
* we should be using java.util.Scanner here but this implementation
320
* guarantees not to read ahead.
321
*/
322
int readInt(InputStream in) throws IOException {
323
StringBuilder sb = new StringBuilder();
324
325
// read to \n or EOF
326
int n;
327
byte buf[] = new byte[1];
328
do {
329
n = in.read(buf, 0, 1);
330
if (n > 0) {
331
char c = (char)buf[0];
332
if (c == '\n') {
333
break; // EOL found
334
} else {
335
sb.append(c);
336
}
337
}
338
} while (n > 0);
339
340
if (sb.length() == 0) {
341
throw new IOException("Premature EOF");
342
}
343
344
int value;
345
try {
346
value = Integer.parseInt(sb.toString());
347
} catch (NumberFormatException x) {
348
throw new IOException("Non-numeric value found - int expected");
349
}
350
return value;
351
}
352
353
/*
354
* Utility method to read data into a String.
355
*/
356
String readErrorMessage(InputStream in) throws IOException {
357
String s;
358
StringBuilder message = new StringBuilder();
359
BufferedReader br = new BufferedReader(new InputStreamReader(in));
360
while ((s = br.readLine()) != null) {
361
message.append(s);
362
}
363
return message.toString();
364
}
365
366
367
// -- attach timeout support
368
369
private static long defaultAttachTimeout = 10000;
370
private volatile long attachTimeout;
371
372
/*
373
* Return attach timeout based on the value of the sun.tools.attach.attachTimeout
374
* property, or the default timeout if the property is not set to a positive
375
* value.
376
*/
377
long attachTimeout() {
378
if (attachTimeout == 0) {
379
synchronized(this) {
380
if (attachTimeout == 0) {
381
try {
382
String s =
383
System.getProperty("sun.tools.attach.attachTimeout");
384
attachTimeout = Long.parseLong(s);
385
} catch (SecurityException se) {
386
} catch (NumberFormatException ne) {
387
}
388
if (attachTimeout <= 0) {
389
attachTimeout = defaultAttachTimeout;
390
}
391
}
392
}
393
}
394
return attachTimeout;
395
}
396
}
397
398