author | gromero |
Tue, 04 Sep 2018 11:46:23 -0400 | |
changeset 51629 | 78c7f0c7827d |
parent 48629 | a58c1924e037 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
48629
a58c1924e037
6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents:
48150
diff
changeset
|
2 |
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.tools.attach; |
|
27 |
||
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
28 |
import com.sun.tools.attach.AttachNotSupportedException; |
2 | 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; |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
33 |
import jdk.internal.misc.VM; |
2 | 34 |
|
26460
68a3e0abedfd
8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents:
25859
diff
changeset
|
35 |
import java.io.BufferedReader; |
2 | 36 |
import java.io.InputStream; |
37 |
import java.io.IOException; |
|
26460
68a3e0abedfd
8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents:
25859
diff
changeset
|
38 |
import java.io.InputStreamReader; |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
39 |
import java.security.AccessController; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
40 |
import java.security.PrivilegedAction; |
2 | 41 |
import java.util.Properties; |
24870
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
42 |
import java.util.stream.Collectors; |
2 | 43 |
|
44 |
/* |
|
45 |
* The HotSpot implementation of com.sun.tools.attach.VirtualMachine. |
|
46 |
*/ |
|
47 |
||
48 |
public abstract class HotSpotVirtualMachine extends VirtualMachine { |
|
49 |
||
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
50 |
private static final long CURRENT_PID; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
51 |
private static final boolean ALLOW_ATTACH_SELF; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
52 |
static { |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
53 |
PrivilegedAction<ProcessHandle> pa = ProcessHandle::current; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
54 |
CURRENT_PID = AccessController.doPrivileged(pa).pid(); |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
55 |
|
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
56 |
String s = VM.getSavedProperty("jdk.attach.allowAttachSelf"); |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
57 |
ALLOW_ATTACH_SELF = "".equals(s) || Boolean.parseBoolean(s); |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
58 |
} |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
59 |
|
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
60 |
HotSpotVirtualMachine(AttachProvider provider, String id) |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
61 |
throws AttachNotSupportedException, IOException |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
62 |
{ |
2 | 63 |
super(provider, id); |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
64 |
|
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
65 |
int pid; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
66 |
try { |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
67 |
pid = Integer.parseInt(id); |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
68 |
} catch (NumberFormatException e) { |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
69 |
throw new AttachNotSupportedException("Invalid process identifier"); |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
70 |
} |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
71 |
|
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
72 |
// The tool should be a different VM to the target. This check will |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
73 |
// eventually be enforced by the target VM. |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
74 |
if (!ALLOW_ATTACH_SELF && (pid == 0 || pid == CURRENT_PID)) { |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
75 |
throw new IOException("Can not attach to current VM"); |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
76 |
} |
2 | 77 |
} |
78 |
||
79 |
/* |
|
80 |
* Load agent library |
|
81 |
* If isAbsolute is true then the agent library is the absolute path |
|
82 |
* to the library and thus will not be expanded in the target VM. |
|
83 |
* if isAbsolute is false then the agent library is just a library |
|
84 |
* name and it will be expended in the target VM. |
|
85 |
*/ |
|
86 |
private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options) |
|
87 |
throws AgentLoadException, AgentInitializationException, IOException |
|
88 |
{ |
|
48629
a58c1924e037
6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents:
48150
diff
changeset
|
89 |
if (agentLibrary == null) { |
a58c1924e037
6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents:
48150
diff
changeset
|
90 |
throw new NullPointerException("agentLibrary cannot be null"); |
a58c1924e037
6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents:
48150
diff
changeset
|
91 |
} |
a58c1924e037
6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents:
48150
diff
changeset
|
92 |
|
48150
bc1cffa26561
8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents:
47216
diff
changeset
|
93 |
String msgPrefix = "return code: "; |
2 | 94 |
InputStream in = execute("load", |
95 |
agentLibrary, |
|
96 |
isAbsolute ? "true" : "false", |
|
97 |
options); |
|
48150
bc1cffa26561
8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents:
47216
diff
changeset
|
98 |
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) { |
bc1cffa26561
8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents:
47216
diff
changeset
|
99 |
String result = reader.readLine(); |
bc1cffa26561
8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents:
47216
diff
changeset
|
100 |
if (result == null) { |
bc1cffa26561
8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents:
47216
diff
changeset
|
101 |
throw new AgentLoadException("Target VM did not respond"); |
bc1cffa26561
8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents:
47216
diff
changeset
|
102 |
} else if (result.startsWith(msgPrefix)) { |
bc1cffa26561
8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents:
47216
diff
changeset
|
103 |
int retCode = Integer.parseInt(result.substring(msgPrefix.length())); |
bc1cffa26561
8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents:
47216
diff
changeset
|
104 |
if (retCode != 0) { |
bc1cffa26561
8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents:
47216
diff
changeset
|
105 |
throw new AgentInitializationException("Agent_OnAttach failed", retCode); |
bc1cffa26561
8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents:
47216
diff
changeset
|
106 |
} |
bc1cffa26561
8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents:
47216
diff
changeset
|
107 |
} else { |
bc1cffa26561
8165736: Error message should be shown when JVMTI agent cannot be attached
ysuenaga
parents:
47216
diff
changeset
|
108 |
throw new AgentLoadException(result); |
2 | 109 |
} |
110 |
} |
|
111 |
} |
|
112 |
||
113 |
/* |
|
114 |
* Load agent library - library name will be expanded in target VM |
|
115 |
*/ |
|
116 |
public void loadAgentLibrary(String agentLibrary, String options) |
|
117 |
throws AgentLoadException, AgentInitializationException, IOException |
|
118 |
{ |
|
119 |
loadAgentLibrary(agentLibrary, false, options); |
|
120 |
} |
|
121 |
||
122 |
/* |
|
123 |
* Load agent - absolute path of library provided to target VM |
|
124 |
*/ |
|
125 |
public void loadAgentPath(String agentLibrary, String options) |
|
126 |
throws AgentLoadException, AgentInitializationException, IOException |
|
127 |
{ |
|
128 |
loadAgentLibrary(agentLibrary, true, options); |
|
129 |
} |
|
130 |
||
131 |
/* |
|
132 |
* Load JPLIS agent which will load the agent JAR file and invoke |
|
133 |
* the agentmain method. |
|
134 |
*/ |
|
135 |
public void loadAgent(String agent, String options) |
|
136 |
throws AgentLoadException, AgentInitializationException, IOException |
|
137 |
{ |
|
48629
a58c1924e037
6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents:
48150
diff
changeset
|
138 |
if (agent == null) { |
a58c1924e037
6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents:
48150
diff
changeset
|
139 |
throw new NullPointerException("agent cannot be null"); |
a58c1924e037
6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents:
48150
diff
changeset
|
140 |
} |
a58c1924e037
6640188: Methods com.cun.attach.VirtualMachine.load... don't throw NullPointerxception
gadams
parents:
48150
diff
changeset
|
141 |
|
2 | 142 |
String args = agent; |
143 |
if (options != null) { |
|
144 |
args = args + "=" + options; |
|
145 |
} |
|
146 |
try { |
|
147 |
loadAgentLibrary("instrument", args); |
|
148 |
} catch (AgentInitializationException x) { |
|
149 |
/* |
|
150 |
* Translate interesting errors into the right exception and |
|
151 |
* message (FIXME: create a better interface to the instrument |
|
152 |
* implementation so this isn't necessary) |
|
153 |
*/ |
|
154 |
int rc = x.returnValue(); |
|
155 |
switch (rc) { |
|
156 |
case JNI_ENOMEM: |
|
157 |
throw new AgentLoadException("Insuffient memory"); |
|
158 |
case ATTACH_ERROR_BADJAR: |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
159 |
throw new AgentLoadException( |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
160 |
"Agent JAR not found or no Agent-Class attribute"); |
2 | 161 |
case ATTACH_ERROR_NOTONCP: |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
162 |
throw new AgentLoadException( |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
163 |
"Unable to add JAR file to system class path"); |
2 | 164 |
case ATTACH_ERROR_STARTFAIL: |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
165 |
throw new AgentInitializationException( |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
166 |
"Agent JAR loaded but agent failed to initialize"); |
2 | 167 |
default : |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
168 |
throw new AgentLoadException("" + |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
169 |
"Failed to load agent - unknown reason: " + rc); |
2 | 170 |
} |
171 |
} |
|
172 |
} |
|
173 |
||
174 |
/* |
|
175 |
* The possible errors returned by JPLIS's agentmain |
|
176 |
*/ |
|
177 |
private static final int JNI_ENOMEM = -4; |
|
178 |
private static final int ATTACH_ERROR_BADJAR = 100; |
|
179 |
private static final int ATTACH_ERROR_NOTONCP = 101; |
|
180 |
private static final int ATTACH_ERROR_STARTFAIL = 102; |
|
181 |
||
182 |
||
183 |
/* |
|
184 |
* Send "properties" command to target VM |
|
185 |
*/ |
|
186 |
public Properties getSystemProperties() throws IOException { |
|
187 |
InputStream in = null; |
|
188 |
Properties props = new Properties(); |
|
189 |
try { |
|
190 |
in = executeCommand("properties"); |
|
191 |
props.load(in); |
|
192 |
} finally { |
|
193 |
if (in != null) in.close(); |
|
194 |
} |
|
195 |
return props; |
|
196 |
} |
|
197 |
||
198 |
public Properties getAgentProperties() throws IOException { |
|
199 |
InputStream in = null; |
|
200 |
Properties props = new Properties(); |
|
201 |
try { |
|
202 |
in = executeCommand("agentProperties"); |
|
203 |
props.load(in); |
|
204 |
} finally { |
|
205 |
if (in != null) in.close(); |
|
206 |
} |
|
207 |
return props; |
|
208 |
} |
|
209 |
||
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
210 |
private static final String MANAGEMENT_PREFIX = "com.sun.management."; |
24870
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
211 |
|
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
212 |
private static boolean checkedKeyName(Object key) { |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
213 |
if (!(key instanceof String)) { |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
214 |
throw new IllegalArgumentException("Invalid option (not a String): "+key); |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
215 |
} |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
216 |
if (!((String)key).startsWith(MANAGEMENT_PREFIX)) { |
24870
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
217 |
throw new IllegalArgumentException("Invalid option: "+key); |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
218 |
} |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
219 |
return true; |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
220 |
} |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
221 |
|
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
222 |
private static String stripKeyName(Object key) { |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
223 |
return ((String)key).substring(MANAGEMENT_PREFIX.length()); |
24870
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
224 |
} |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
225 |
|
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
226 |
@Override |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
227 |
public void startManagementAgent(Properties agentProperties) throws IOException { |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
228 |
if (agentProperties == null) { |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
229 |
throw new NullPointerException("agentProperties cannot be null"); |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
230 |
} |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
231 |
// Convert the arguments into arguments suitable for the Diagnostic Command: |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
232 |
// "ManagementAgent.start jmxremote.port=5555 jmxremote.authenticate=false" |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
233 |
String args = agentProperties.entrySet().stream() |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
234 |
.filter(entry -> checkedKeyName(entry.getKey())) |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
235 |
.map(entry -> stripKeyName(entry.getKey()) + "=" + escape(entry.getValue())) |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
236 |
.collect(Collectors.joining(" ")); |
26460
68a3e0abedfd
8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents:
25859
diff
changeset
|
237 |
executeJCmd("ManagementAgent.start " + args).close(); |
24870
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
238 |
} |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
239 |
|
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
240 |
private String escape(Object arg) { |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
241 |
String value = arg.toString(); |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
242 |
if (value.contains(" ")) { |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
243 |
return "'" + value + "'"; |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
244 |
} |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
245 |
return value; |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
246 |
} |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
247 |
|
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
248 |
@Override |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
249 |
public String startLocalManagementAgent() throws IOException { |
26460
68a3e0abedfd
8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents:
25859
diff
changeset
|
250 |
executeJCmd("ManagementAgent.start_local").close(); |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
251 |
String prop = MANAGEMENT_PREFIX + "jmxremote.localConnectorAddress"; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
252 |
return getAgentProperties().getProperty(prop); |
24870
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
253 |
} |
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
24363
diff
changeset
|
254 |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
255 |
|
2 | 256 |
// --- HotSpot specific methods --- |
257 |
||
258 |
// same as SIGQUIT |
|
259 |
public void localDataDump() throws IOException { |
|
260 |
executeCommand("datadump").close(); |
|
261 |
} |
|
262 |
||
263 |
// Remote ctrl-break. The output of the ctrl-break actions can |
|
264 |
// be read from the input stream. |
|
265 |
public InputStream remoteDataDump(Object ... args) throws IOException { |
|
266 |
return executeCommand("threaddump", args); |
|
267 |
} |
|
268 |
||
269 |
// Remote heap dump. The output (error message) can be read from the |
|
270 |
// returned input stream. |
|
271 |
public InputStream dumpHeap(Object ... args) throws IOException { |
|
272 |
return executeCommand("dumpheap", args); |
|
273 |
} |
|
274 |
||
275 |
// Heap histogram (heap inspection in HotSpot) |
|
276 |
public InputStream heapHisto(Object ... args) throws IOException { |
|
277 |
return executeCommand("inspectheap", args); |
|
278 |
} |
|
279 |
||
280 |
// set JVM command line flag |
|
281 |
public InputStream setFlag(String name, String value) throws IOException { |
|
282 |
return executeCommand("setflag", name, value); |
|
283 |
} |
|
284 |
||
285 |
// print command line flag |
|
286 |
public InputStream printFlag(String name) throws IOException { |
|
287 |
return executeCommand("printflag", name); |
|
288 |
} |
|
289 |
||
11365 | 290 |
public InputStream executeJCmd(String command) throws IOException { |
291 |
return executeCommand("jcmd", command); |
|
292 |
} |
|
293 |
||
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
294 |
|
2 | 295 |
// -- Supporting methods |
296 |
||
297 |
/* |
|
298 |
* Execute the given command in the target VM - specific platform |
|
299 |
* implementation must implement this. |
|
300 |
*/ |
|
301 |
abstract InputStream execute(String cmd, Object ... args) |
|
302 |
throws AgentLoadException, IOException; |
|
303 |
||
304 |
/* |
|
305 |
* Convenience method for simple commands |
|
306 |
*/ |
|
38360
fb63be22ffa6
8155091: Remove SA related functions from tmtools
dsamersoff
parents:
26460
diff
changeset
|
307 |
public InputStream executeCommand(String cmd, Object ... args) throws IOException { |
2 | 308 |
try { |
309 |
return execute(cmd, args); |
|
310 |
} catch (AgentLoadException x) { |
|
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
5506
diff
changeset
|
311 |
throw new InternalError("Should not get here", x); |
2 | 312 |
} |
313 |
} |
|
314 |
||
315 |
||
316 |
/* |
|
317 |
* Utility method to read an 'int' from the input stream. Ideally |
|
318 |
* we should be using java.util.Scanner here but this implementation |
|
319 |
* guarantees not to read ahead. |
|
320 |
*/ |
|
321 |
int readInt(InputStream in) throws IOException { |
|
322 |
StringBuilder sb = new StringBuilder(); |
|
323 |
||
324 |
// read to \n or EOF |
|
325 |
int n; |
|
326 |
byte buf[] = new byte[1]; |
|
327 |
do { |
|
328 |
n = in.read(buf, 0, 1); |
|
329 |
if (n > 0) { |
|
330 |
char c = (char)buf[0]; |
|
331 |
if (c == '\n') { |
|
332 |
break; // EOL found |
|
333 |
} else { |
|
334 |
sb.append(c); |
|
335 |
} |
|
336 |
} |
|
337 |
} while (n > 0); |
|
338 |
||
339 |
if (sb.length() == 0) { |
|
340 |
throw new IOException("Premature EOF"); |
|
341 |
} |
|
342 |
||
343 |
int value; |
|
344 |
try { |
|
345 |
value = Integer.parseInt(sb.toString()); |
|
346 |
} catch (NumberFormatException x) { |
|
347 |
throw new IOException("Non-numeric value found - int expected"); |
|
348 |
} |
|
349 |
return value; |
|
350 |
} |
|
351 |
||
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
352 |
/* |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
353 |
* Utility method to read data into a String. |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
354 |
*/ |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
355 |
String readErrorMessage(InputStream in) throws IOException { |
26460
68a3e0abedfd
8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents:
25859
diff
changeset
|
356 |
String s; |
68a3e0abedfd
8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents:
25859
diff
changeset
|
357 |
StringBuilder message = new StringBuilder(); |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
358 |
BufferedReader br = new BufferedReader(new InputStreamReader(in)); |
26460
68a3e0abedfd
8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents:
25859
diff
changeset
|
359 |
while ((s = br.readLine()) != null) { |
68a3e0abedfd
8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents:
25859
diff
changeset
|
360 |
message.append(s); |
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
361 |
} |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
362 |
return message.toString(); |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
363 |
} |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
364 |
|
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
365 |
|
2 | 366 |
// -- attach timeout support |
367 |
||
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
38360
diff
changeset
|
368 |
private static long defaultAttachTimeout = 10000; |
2 | 369 |
private volatile long attachTimeout; |
370 |
||
371 |
/* |
|
372 |
* Return attach timeout based on the value of the sun.tools.attach.attachTimeout |
|
373 |
* property, or the default timeout if the property is not set to a positive |
|
374 |
* value. |
|
375 |
*/ |
|
376 |
long attachTimeout() { |
|
377 |
if (attachTimeout == 0) { |
|
378 |
synchronized(this) { |
|
379 |
if (attachTimeout == 0) { |
|
380 |
try { |
|
381 |
String s = |
|
382 |
System.getProperty("sun.tools.attach.attachTimeout"); |
|
383 |
attachTimeout = Long.parseLong(s); |
|
384 |
} catch (SecurityException se) { |
|
385 |
} catch (NumberFormatException ne) { |
|
386 |
} |
|
387 |
if (attachTimeout <= 0) { |
|
388 |
attachTimeout = defaultAttachTimeout; |
|
389 |
} |
|
390 |
} |
|
391 |
} |
|
392 |
} |
|
393 |
return attachTimeout; |
|
394 |
} |
|
395 |
} |