author | alanb |
Thu, 04 May 2017 07:26:55 +0000 | |
changeset 45004 | ea3137042a61 |
parent 40943 | 46cd6d4f353f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
2 |
* Copyright (c) 2005, 2014, 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 |
package sun.tools.attach; |
|
26 |
||
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
27 |
import com.sun.tools.attach.AttachOperationFailedException; |
2 | 28 |
import com.sun.tools.attach.AgentLoadException; |
29 |
import com.sun.tools.attach.AttachNotSupportedException; |
|
30 |
import com.sun.tools.attach.spi.AttachProvider; |
|
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
31 |
|
2 | 32 |
import java.io.InputStream; |
33 |
import java.io.IOException; |
|
34 |
import java.io.File; |
|
35 |
||
36 |
/* |
|
37 |
* Linux implementation of HotSpotVirtualMachine |
|
38 |
*/ |
|
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
39 |
public class VirtualMachineImpl extends HotSpotVirtualMachine { |
11692
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
10419
diff
changeset
|
40 |
// "/tmp" is used as a global well-known location for the files |
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
10419
diff
changeset
|
41 |
// .java_pid<pid>. and .attach_pid<pid>. It is important that this |
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
10419
diff
changeset
|
42 |
// location is the same for all processes, otherwise the tools |
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
10419
diff
changeset
|
43 |
// will not be able to find all Hotspot processes. |
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
10419
diff
changeset
|
44 |
// Any changes to this needs to be synchronized with HotSpot. |
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
10419
diff
changeset
|
45 |
private static final String tmpdir = "/tmp"; |
2 | 46 |
|
47 |
// The patch to the socket file created by the target VM |
|
48 |
String path; |
|
49 |
||
50 |
/** |
|
51 |
* Attaches to the target VM |
|
52 |
*/ |
|
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
53 |
VirtualMachineImpl(AttachProvider provider, String vmid) |
2 | 54 |
throws AttachNotSupportedException, IOException |
55 |
{ |
|
56 |
super(provider, vmid); |
|
57 |
||
58 |
// This provider only understands pids |
|
59 |
int pid; |
|
60 |
try { |
|
61 |
pid = Integer.parseInt(vmid); |
|
62 |
} catch (NumberFormatException x) { |
|
63 |
throw new AttachNotSupportedException("Invalid process identifier"); |
|
64 |
} |
|
65 |
||
66 |
// Find the socket file. If not found then we attempt to start the |
|
67 |
// attach mechanism in the target VM by sending it a QUIT signal. |
|
68 |
// Then we attempt to find the socket file again. |
|
69 |
path = findSocketFile(pid); |
|
70 |
if (path == null) { |
|
71 |
File f = createAttachFile(pid); |
|
72 |
try { |
|
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
73 |
sendQuitTo(pid); |
2 | 74 |
|
75 |
// give the target VM time to start the attach mechanism |
|
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
76 |
final int delay_step = 100; |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
77 |
final long timeout = attachTimeout(); |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
78 |
long time_spend = 0; |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
79 |
long delay = 0; |
2 | 80 |
do { |
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
81 |
// Increase timeout on each attempt to reduce polling |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
82 |
delay += delay_step; |
2 | 83 |
try { |
84 |
Thread.sleep(delay); |
|
85 |
} catch (InterruptedException x) { } |
|
86 |
path = findSocketFile(pid); |
|
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
87 |
|
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
88 |
time_spend += delay; |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
89 |
if (time_spend > timeout/2 && path == null) { |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
90 |
// Send QUIT again to give target VM the last chance to react |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
91 |
sendQuitTo(pid); |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
92 |
} |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
93 |
} while (time_spend <= timeout && path == null); |
2 | 94 |
if (path == null) { |
95 |
throw new AttachNotSupportedException( |
|
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
96 |
String.format("Unable to open socket file %s: " + |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
97 |
"target process %d doesn't respond within %dms " + |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
98 |
"or HotSpot VM not loaded", f.getPath(), pid, time_spend)); |
2 | 99 |
} |
100 |
} finally { |
|
101 |
f.delete(); |
|
102 |
} |
|
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
103 |
} |
2 | 104 |
|
105 |
// Check that the file owner/permission to avoid attaching to |
|
106 |
// bogus process |
|
107 |
checkPermissions(path); |
|
108 |
||
109 |
// Check that we can connect to the process |
|
110 |
// - this ensures we throw the permission denied error now rather than |
|
111 |
// later when we attempt to enqueue a command. |
|
112 |
int s = socket(); |
|
113 |
try { |
|
114 |
connect(s, path); |
|
115 |
} finally { |
|
116 |
close(s); |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* Detach from the target VM |
|
122 |
*/ |
|
123 |
public void detach() throws IOException { |
|
124 |
synchronized (this) { |
|
125 |
if (this.path != null) { |
|
126 |
this.path = null; |
|
127 |
} |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
// protocol version |
|
132 |
private final static String PROTOCOL_VERSION = "1"; |
|
133 |
||
134 |
// known errors |
|
135 |
private final static int ATTACH_ERROR_BADVERSION = 101; |
|
136 |
||
137 |
/** |
|
138 |
* Execute the given command in the target VM. |
|
139 |
*/ |
|
140 |
InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException { |
|
141 |
assert args.length <= 3; // includes null |
|
142 |
||
143 |
// did we detach? |
|
144 |
String p; |
|
145 |
synchronized (this) { |
|
146 |
if (this.path == null) { |
|
147 |
throw new IOException("Detached from target VM"); |
|
148 |
} |
|
149 |
p = this.path; |
|
150 |
} |
|
151 |
||
152 |
// create UNIX socket |
|
153 |
int s = socket(); |
|
154 |
||
155 |
// connect to target VM |
|
156 |
try { |
|
157 |
connect(s, p); |
|
158 |
} catch (IOException x) { |
|
159 |
close(s); |
|
160 |
throw x; |
|
161 |
} |
|
162 |
||
163 |
IOException ioe = null; |
|
164 |
||
165 |
// connected - write request |
|
166 |
// <ver> <cmd> <args...> |
|
167 |
try { |
|
168 |
writeString(s, PROTOCOL_VERSION); |
|
169 |
writeString(s, cmd); |
|
170 |
||
171 |
for (int i=0; i<3; i++) { |
|
172 |
if (i < args.length && args[i] != null) { |
|
173 |
writeString(s, (String)args[i]); |
|
174 |
} else { |
|
175 |
writeString(s, ""); |
|
176 |
} |
|
177 |
} |
|
178 |
} catch (IOException x) { |
|
179 |
ioe = x; |
|
180 |
} |
|
181 |
||
182 |
||
183 |
// Create an input stream to read reply |
|
184 |
SocketInputStream sis = new SocketInputStream(s); |
|
185 |
||
186 |
// Read the command completion status |
|
187 |
int completionStatus; |
|
188 |
try { |
|
189 |
completionStatus = readInt(sis); |
|
190 |
} catch (IOException x) { |
|
191 |
sis.close(); |
|
192 |
if (ioe != null) { |
|
193 |
throw ioe; |
|
194 |
} else { |
|
195 |
throw x; |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
if (completionStatus != 0) { |
|
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
200 |
// read from the stream and use that as the error message |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
201 |
String message = readErrorMessage(sis); |
2 | 202 |
sis.close(); |
203 |
||
204 |
// In the event of a protocol mismatch then the target VM |
|
205 |
// returns a known error so that we can throw a reasonable |
|
206 |
// error. |
|
207 |
if (completionStatus == ATTACH_ERROR_BADVERSION) { |
|
208 |
throw new IOException("Protocol mismatch with target VM"); |
|
209 |
} |
|
210 |
||
211 |
// Special-case the "load" command so that the right exception is |
|
212 |
// thrown. |
|
213 |
if (cmd.equals("load")) { |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40943
diff
changeset
|
214 |
String msg = "Failed to load agent library"; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40943
diff
changeset
|
215 |
if (!message.isEmpty()) |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40943
diff
changeset
|
216 |
msg += ": " + message; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40943
diff
changeset
|
217 |
throw new AgentLoadException(msg); |
2 | 218 |
} else { |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40943
diff
changeset
|
219 |
if (message.isEmpty()) |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40943
diff
changeset
|
220 |
message = "Command failed in target VM"; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40943
diff
changeset
|
221 |
throw new AttachOperationFailedException(message); |
2 | 222 |
} |
223 |
} |
|
224 |
||
225 |
// Return the input stream so that the command output can be read |
|
226 |
return sis; |
|
227 |
} |
|
228 |
||
229 |
/* |
|
230 |
* InputStream for the socket connection to get target VM |
|
231 |
*/ |
|
232 |
private class SocketInputStream extends InputStream { |
|
233 |
int s; |
|
234 |
||
235 |
public SocketInputStream(int s) { |
|
236 |
this.s = s; |
|
237 |
} |
|
238 |
||
239 |
public synchronized int read() throws IOException { |
|
240 |
byte b[] = new byte[1]; |
|
241 |
int n = this.read(b, 0, 1); |
|
242 |
if (n == 1) { |
|
243 |
return b[0] & 0xff; |
|
244 |
} else { |
|
245 |
return -1; |
|
246 |
} |
|
247 |
} |
|
248 |
||
249 |
public synchronized int read(byte[] bs, int off, int len) throws IOException { |
|
250 |
if ((off < 0) || (off > bs.length) || (len < 0) || |
|
251 |
((off + len) > bs.length) || ((off + len) < 0)) { |
|
252 |
throw new IndexOutOfBoundsException(); |
|
253 |
} else if (len == 0) |
|
254 |
return 0; |
|
255 |
||
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
256 |
return VirtualMachineImpl.read(s, bs, off, len); |
2 | 257 |
} |
258 |
||
259 |
public void close() throws IOException { |
|
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
260 |
VirtualMachineImpl.close(s); |
2 | 261 |
} |
262 |
} |
|
263 |
||
264 |
// Return the socket file for the given process. |
|
265 |
private String findSocketFile(int pid) { |
|
11692
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
10419
diff
changeset
|
266 |
File f = new File(tmpdir, ".java_pid" + pid); |
2 | 267 |
if (!f.exists()) { |
11692
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
10419
diff
changeset
|
268 |
return null; |
2 | 269 |
} |
11692
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
10419
diff
changeset
|
270 |
return f.getPath(); |
2 | 271 |
} |
272 |
||
273 |
// On Solaris/Linux a simple handshake is used to start the attach mechanism |
|
274 |
// if not already started. The client creates a .attach_pid<pid> file in the |
|
5771
1f415c7e277d
6950927: Testcase failure sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
alanb
parents:
5506
diff
changeset
|
275 |
// target VM's working directory (or temp directory), and the SIGQUIT handler |
1f415c7e277d
6950927: Testcase failure sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
alanb
parents:
5506
diff
changeset
|
276 |
// checks for the file. |
2 | 277 |
private File createAttachFile(int pid) throws IOException { |
278 |
String fn = ".attach_pid" + pid; |
|
279 |
String path = "/proc/" + pid + "/cwd/" + fn; |
|
280 |
File f = new File(path); |
|
281 |
try { |
|
282 |
f.createNewFile(); |
|
283 |
} catch (IOException x) { |
|
5771
1f415c7e277d
6950927: Testcase failure sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
alanb
parents:
5506
diff
changeset
|
284 |
f = new File(tmpdir, fn); |
2 | 285 |
f.createNewFile(); |
286 |
} |
|
287 |
return f; |
|
288 |
} |
|
289 |
||
290 |
/* |
|
291 |
* Write/sends the given to the target VM. String is transmitted in |
|
292 |
* UTF-8 encoding. |
|
293 |
*/ |
|
294 |
private void writeString(int fd, String s) throws IOException { |
|
295 |
if (s.length() > 0) { |
|
296 |
byte b[]; |
|
297 |
try { |
|
298 |
b = s.getBytes("UTF-8"); |
|
299 |
} catch (java.io.UnsupportedEncodingException x) { |
|
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
7668
diff
changeset
|
300 |
throw new InternalError(x); |
2 | 301 |
} |
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
302 |
VirtualMachineImpl.write(fd, b, 0, b.length); |
2 | 303 |
} |
304 |
byte b[] = new byte[1]; |
|
305 |
b[0] = 0; |
|
306 |
write(fd, b, 0, 1); |
|
307 |
} |
|
308 |
||
309 |
||
310 |
//-- native methods |
|
311 |
||
312 |
static native void sendQuitToChildrenOf(int pid) throws IOException; |
|
313 |
||
314 |
static native void sendQuitTo(int pid) throws IOException; |
|
315 |
||
316 |
static native void checkPermissions(String path) throws IOException; |
|
317 |
||
318 |
static native int socket() throws IOException; |
|
319 |
||
320 |
static native void connect(int fd, String path) throws IOException; |
|
321 |
||
322 |
static native void close(int fd) throws IOException; |
|
323 |
||
324 |
static native int read(int fd, byte buf[], int off, int bufLen) throws IOException; |
|
325 |
||
326 |
static native void write(int fd, byte buf[], int off, int bufLen) throws IOException; |
|
327 |
||
328 |
static { |
|
329 |
System.loadLibrary("attach"); |
|
330 |
} |
|
331 |
} |