author | rschmelter |
Tue, 16 Jul 2019 13:57:13 +0200 | |
changeset 55685 | 56b96d234f26 |
parent 54065 | f984aca565c1 |
child 57844 | 3bc26ffdf001 |
permissions | -rw-r--r-- |
12047 | 1 |
/* |
53858 | 2 |
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved. |
12047 | 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 |
package sun.tools.attach; |
|
26 |
||
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
12047
diff
changeset
|
27 |
import com.sun.tools.attach.AttachOperationFailedException; |
12047 | 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:
12047
diff
changeset
|
31 |
|
12047 | 32 |
import java.io.InputStream; |
33 |
import java.io.IOException; |
|
34 |
import java.io.File; |
|
35 |
||
36 |
/* |
|
37 |
* Bsd 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 { |
12047 | 40 |
// "tmpdir" is used as a global well-known location for the files |
41 |
// .java_pid<pid>. and .attach_pid<pid>. It is important that this |
|
42 |
// location is the same for all processes, otherwise the tools |
|
43 |
// will not be able to find all Hotspot processes. |
|
44 |
// This is intentionally not the same as java.io.tmpdir, since |
|
45 |
// the latter can be changed by the user. |
|
46 |
// Any changes to this needs to be synchronized with HotSpot. |
|
47 |
private static final String tmpdir; |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
48 |
String socket_path; |
12047 | 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) |
12047 | 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. |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
69 |
File socket_file = new File(tmpdir, ".java_pid" + pid); |
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
70 |
socket_path = socket_file.getPath(); |
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
71 |
if (!socket_file.exists()) { |
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
72 |
File f = createAttachFile(pid); |
12047 | 73 |
try { |
74 |
sendQuitTo(pid); |
|
75 |
||
76 |
// 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
|
77 |
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
|
78 |
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
|
79 |
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
|
80 |
long delay = 0; |
12047 | 81 |
do { |
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
82 |
// 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
|
83 |
delay += delay_step; |
12047 | 84 |
try { |
85 |
Thread.sleep(delay); |
|
86 |
} catch (InterruptedException x) { } |
|
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; |
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
89 |
if (time_spend > timeout/2 && !socket_file.exists()) { |
40685
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 |
} |
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
93 |
} while (time_spend <= timeout && !socket_file.exists()); |
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
94 |
if (!socket_file.exists()) { |
12047 | 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: " + |
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
97 |
"target process %d doesn't respond within %dms " + |
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
98 |
"or HotSpot VM not loaded", socket_path, |
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
99 |
pid, time_spend)); |
12047 | 100 |
} |
101 |
} finally { |
|
102 |
f.delete(); |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
// Check that the file owner/permission to avoid attaching to |
|
107 |
// bogus process |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
108 |
checkPermissions(socket_path); |
12047 | 109 |
|
110 |
// Check that we can connect to the process |
|
111 |
// - this ensures we throw the permission denied error now rather than |
|
112 |
// later when we attempt to enqueue a command. |
|
113 |
int s = socket(); |
|
114 |
try { |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
115 |
connect(s, socket_path); |
12047 | 116 |
} finally { |
117 |
close(s); |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* Detach from the target VM |
|
123 |
*/ |
|
124 |
public void detach() throws IOException { |
|
125 |
synchronized (this) { |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
126 |
if (socket_path != null) { |
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
127 |
socket_path = null; |
12047 | 128 |
} |
129 |
} |
|
130 |
} |
|
131 |
||
132 |
// protocol version |
|
133 |
private final static String PROTOCOL_VERSION = "1"; |
|
134 |
||
135 |
// known errors |
|
136 |
private final static int ATTACH_ERROR_BADVERSION = 101; |
|
137 |
||
138 |
/** |
|
139 |
* Execute the given command in the target VM. |
|
140 |
*/ |
|
141 |
InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException { |
|
54065
f984aca565c1
8219721: jcmd from earlier release will hang attaching to VM with JDK-8215622 applied
dholmes
parents:
53858
diff
changeset
|
142 |
assert args.length <= 3; // includes null |
12047 | 143 |
|
144 |
// did we detach? |
|
145 |
synchronized (this) { |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
146 |
if (socket_path == null) { |
12047 | 147 |
throw new IOException("Detached from target VM"); |
148 |
} |
|
149 |
} |
|
150 |
||
151 |
// create UNIX socket |
|
152 |
int s = socket(); |
|
153 |
||
154 |
// connect to target VM |
|
155 |
try { |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
156 |
connect(s, socket_path); |
12047 | 157 |
} catch (IOException x) { |
158 |
close(s); |
|
159 |
throw x; |
|
160 |
} |
|
161 |
||
162 |
IOException ioe = null; |
|
163 |
||
164 |
// connected - write request |
|
165 |
// <ver> <cmd> <args...> |
|
166 |
try { |
|
167 |
writeString(s, PROTOCOL_VERSION); |
|
168 |
writeString(s, cmd); |
|
169 |
||
54065
f984aca565c1
8219721: jcmd from earlier release will hang attaching to VM with JDK-8215622 applied
dholmes
parents:
53858
diff
changeset
|
170 |
for (int i = 0; i < 3; i++) { |
12047 | 171 |
if (i < args.length && args[i] != null) { |
172 |
writeString(s, (String)args[i]); |
|
173 |
} else { |
|
174 |
writeString(s, ""); |
|
175 |
} |
|
176 |
} |
|
177 |
} catch (IOException x) { |
|
178 |
ioe = x; |
|
179 |
} |
|
180 |
||
181 |
||
182 |
// Create an input stream to read reply |
|
183 |
SocketInputStream sis = new SocketInputStream(s); |
|
184 |
||
185 |
// Read the command completion status |
|
186 |
int completionStatus; |
|
187 |
try { |
|
188 |
completionStatus = readInt(sis); |
|
189 |
} catch (IOException x) { |
|
190 |
sis.close(); |
|
191 |
if (ioe != null) { |
|
192 |
throw ioe; |
|
193 |
} else { |
|
194 |
throw x; |
|
195 |
} |
|
196 |
} |
|
197 |
||
198 |
if (completionStatus != 0) { |
|
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
12047
diff
changeset
|
199 |
// 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:
12047
diff
changeset
|
200 |
String message = readErrorMessage(sis); |
12047 | 201 |
sis.close(); |
202 |
||
203 |
// In the event of a protocol mismatch then the target VM |
|
204 |
// returns a known error so that we can throw a reasonable |
|
205 |
// error. |
|
206 |
if (completionStatus == ATTACH_ERROR_BADVERSION) { |
|
207 |
throw new IOException("Protocol mismatch with target VM"); |
|
208 |
} |
|
209 |
||
210 |
// Special-case the "load" command so that the right exception is |
|
211 |
// thrown. |
|
212 |
if (cmd.equals("load")) { |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
213 |
String msg = "Failed to load agent library"; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
214 |
if (!message.isEmpty()) |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
215 |
msg += ": " + message; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
216 |
throw new AgentLoadException(msg); |
12047 | 217 |
} else { |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
218 |
if (message.isEmpty()) |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
219 |
message = "Command failed in target VM"; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
220 |
throw new AttachOperationFailedException(message); |
12047 | 221 |
} |
222 |
} |
|
223 |
||
224 |
// Return the input stream so that the command output can be read |
|
225 |
return sis; |
|
226 |
} |
|
227 |
||
228 |
/* |
|
229 |
* InputStream for the socket connection to get target VM |
|
230 |
*/ |
|
231 |
private class SocketInputStream extends InputStream { |
|
232 |
int s; |
|
233 |
||
234 |
public SocketInputStream(int s) { |
|
235 |
this.s = s; |
|
236 |
} |
|
237 |
||
238 |
public synchronized int read() throws IOException { |
|
239 |
byte b[] = new byte[1]; |
|
240 |
int n = this.read(b, 0, 1); |
|
241 |
if (n == 1) { |
|
242 |
return b[0] & 0xff; |
|
243 |
} else { |
|
244 |
return -1; |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
public synchronized int read(byte[] bs, int off, int len) throws IOException { |
|
249 |
if ((off < 0) || (off > bs.length) || (len < 0) || |
|
250 |
((off + len) > bs.length) || ((off + len) < 0)) { |
|
251 |
throw new IndexOutOfBoundsException(); |
|
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
12047
diff
changeset
|
252 |
} else if (len == 0) { |
12047 | 253 |
return 0; |
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
12047
diff
changeset
|
254 |
} |
12047 | 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); |
12047 | 257 |
} |
258 |
||
55685
56b96d234f26
8227440: Implement fix to JDK-8224642 on all platforms
rschmelter
parents:
54065
diff
changeset
|
259 |
public synchronized void close() throws IOException { |
56b96d234f26
8227440: Implement fix to JDK-8224642 on all platforms
rschmelter
parents:
54065
diff
changeset
|
260 |
if (s != -1) { |
56b96d234f26
8227440: Implement fix to JDK-8224642 on all platforms
rschmelter
parents:
54065
diff
changeset
|
261 |
int toClose = s; |
56b96d234f26
8227440: Implement fix to JDK-8224642 on all platforms
rschmelter
parents:
54065
diff
changeset
|
262 |
s = -1; |
56b96d234f26
8227440: Implement fix to JDK-8224642 on all platforms
rschmelter
parents:
54065
diff
changeset
|
263 |
VirtualMachineImpl.close(toClose); |
56b96d234f26
8227440: Implement fix to JDK-8224642 on all platforms
rschmelter
parents:
54065
diff
changeset
|
264 |
} |
12047 | 265 |
} |
266 |
} |
|
267 |
||
268 |
/* |
|
269 |
* Write/sends the given to the target VM. String is transmitted in |
|
270 |
* UTF-8 encoding. |
|
271 |
*/ |
|
272 |
private void writeString(int fd, String s) throws IOException { |
|
273 |
if (s.length() > 0) { |
|
274 |
byte b[]; |
|
275 |
try { |
|
276 |
b = s.getBytes("UTF-8"); |
|
277 |
} catch (java.io.UnsupportedEncodingException x) { |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
278 |
throw new InternalError(x); |
12047 | 279 |
} |
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
280 |
VirtualMachineImpl.write(fd, b, 0, b.length); |
12047 | 281 |
} |
282 |
byte b[] = new byte[1]; |
|
283 |
b[0] = 0; |
|
284 |
write(fd, b, 0, 1); |
|
285 |
} |
|
286 |
||
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
287 |
private File createAttachFile(int pid) throws IOException { |
49742
1196aa0be8be
8201247: Various cleanups in the attach framework
clanger
parents:
48485
diff
changeset
|
288 |
File f = new File(tmpdir, ".attach_pid" + pid); |
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
289 |
createAttachFile0(f.getPath()); |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
290 |
return f; |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
291 |
} |
12047 | 292 |
|
293 |
//-- native methods |
|
294 |
||
295 |
static native void sendQuitTo(int pid) throws IOException; |
|
296 |
||
297 |
static native void checkPermissions(String path) throws IOException; |
|
298 |
||
299 |
static native int socket() throws IOException; |
|
300 |
||
301 |
static native void connect(int fd, String path) throws IOException; |
|
302 |
||
303 |
static native void close(int fd) throws IOException; |
|
304 |
||
305 |
static native int read(int fd, byte buf[], int off, int bufLen) throws IOException; |
|
306 |
||
307 |
static native void write(int fd, byte buf[], int off, int bufLen) throws IOException; |
|
308 |
||
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
309 |
static native void createAttachFile0(String path); |
12047 | 310 |
|
311 |
static native String getTempDir(); |
|
312 |
||
313 |
static { |
|
314 |
System.loadLibrary("attach"); |
|
315 |
tmpdir = getTempDir(); |
|
316 |
} |
|
317 |
} |