author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 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); |
|
57844
3bc26ffdf001
8229957: Harden pid verification in attach mechanism
lmesnik
parents:
55685
diff
changeset
|
62 |
if (pid < 1) { |
3bc26ffdf001
8229957: Harden pid verification in attach mechanism
lmesnik
parents:
55685
diff
changeset
|
63 |
throw new NumberFormatException(); |
3bc26ffdf001
8229957: Harden pid verification in attach mechanism
lmesnik
parents:
55685
diff
changeset
|
64 |
} |
12047 | 65 |
} catch (NumberFormatException x) { |
57844
3bc26ffdf001
8229957: Harden pid verification in attach mechanism
lmesnik
parents:
55685
diff
changeset
|
66 |
throw new AttachNotSupportedException("Invalid process identifier: " + vmid); |
12047 | 67 |
} |
68 |
||
69 |
// Find the socket file. If not found then we attempt to start the |
|
70 |
// attach mechanism in the target VM by sending it a QUIT signal. |
|
71 |
// 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
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
File f = createAttachFile(pid); |
12047 | 76 |
try { |
77 |
sendQuitTo(pid); |
|
78 |
||
79 |
// 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
|
80 |
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
|
81 |
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
|
82 |
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
|
83 |
long delay = 0; |
12047 | 84 |
do { |
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
85 |
// 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
|
86 |
delay += delay_step; |
12047 | 87 |
try { |
88 |
Thread.sleep(delay); |
|
89 |
} 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
|
90 |
|
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
91 |
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
|
92 |
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
|
93 |
// 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
|
94 |
sendQuitTo(pid); |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
95 |
} |
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
96 |
} 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
|
97 |
if (!socket_file.exists()) { |
12047 | 98 |
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
|
99 |
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
|
100 |
"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
|
101 |
"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
|
102 |
pid, time_spend)); |
12047 | 103 |
} |
104 |
} finally { |
|
105 |
f.delete(); |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
// Check that the file owner/permission to avoid attaching to |
|
110 |
// 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
|
111 |
checkPermissions(socket_path); |
12047 | 112 |
|
113 |
// Check that we can connect to the process |
|
114 |
// - this ensures we throw the permission denied error now rather than |
|
115 |
// later when we attempt to enqueue a command. |
|
116 |
int s = socket(); |
|
117 |
try { |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
118 |
connect(s, socket_path); |
12047 | 119 |
} finally { |
120 |
close(s); |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Detach from the target VM |
|
126 |
*/ |
|
127 |
public void detach() throws IOException { |
|
128 |
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
|
129 |
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
|
130 |
socket_path = null; |
12047 | 131 |
} |
132 |
} |
|
133 |
} |
|
134 |
||
135 |
// protocol version |
|
136 |
private final static String PROTOCOL_VERSION = "1"; |
|
137 |
||
138 |
// known errors |
|
139 |
private final static int ATTACH_ERROR_BADVERSION = 101; |
|
140 |
||
141 |
/** |
|
142 |
* Execute the given command in the target VM. |
|
143 |
*/ |
|
144 |
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
|
145 |
assert args.length <= 3; // includes null |
12047 | 146 |
|
147 |
// did we detach? |
|
148 |
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
|
149 |
if (socket_path == null) { |
12047 | 150 |
throw new IOException("Detached from target VM"); |
151 |
} |
|
152 |
} |
|
153 |
||
154 |
// create UNIX socket |
|
155 |
int s = socket(); |
|
156 |
||
157 |
// connect to target VM |
|
158 |
try { |
|
48485
258a4dab74a7
8188856: Incorrect file path in an exception message when .java_pid is not accessible on Unix
gadams
parents:
47216
diff
changeset
|
159 |
connect(s, socket_path); |
12047 | 160 |
} catch (IOException x) { |
161 |
close(s); |
|
162 |
throw x; |
|
163 |
} |
|
164 |
||
165 |
IOException ioe = null; |
|
166 |
||
167 |
// connected - write request |
|
168 |
// <ver> <cmd> <args...> |
|
169 |
try { |
|
170 |
writeString(s, PROTOCOL_VERSION); |
|
171 |
writeString(s, cmd); |
|
172 |
||
54065
f984aca565c1
8219721: jcmd from earlier release will hang attaching to VM with JDK-8215622 applied
dholmes
parents:
53858
diff
changeset
|
173 |
for (int i = 0; i < 3; i++) { |
12047 | 174 |
if (i < args.length && args[i] != null) { |
175 |
writeString(s, (String)args[i]); |
|
176 |
} else { |
|
177 |
writeString(s, ""); |
|
178 |
} |
|
179 |
} |
|
180 |
} catch (IOException x) { |
|
181 |
ioe = x; |
|
182 |
} |
|
183 |
||
184 |
||
185 |
// Create an input stream to read reply |
|
186 |
SocketInputStream sis = new SocketInputStream(s); |
|
187 |
||
188 |
// Read the command completion status |
|
189 |
int completionStatus; |
|
190 |
try { |
|
191 |
completionStatus = readInt(sis); |
|
192 |
} catch (IOException x) { |
|
193 |
sis.close(); |
|
194 |
if (ioe != null) { |
|
195 |
throw ioe; |
|
196 |
} else { |
|
197 |
throw x; |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
if (completionStatus != 0) { |
|
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
12047
diff
changeset
|
202 |
// 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
|
203 |
String message = readErrorMessage(sis); |
12047 | 204 |
sis.close(); |
205 |
||
206 |
// In the event of a protocol mismatch then the target VM |
|
207 |
// returns a known error so that we can throw a reasonable |
|
208 |
// error. |
|
209 |
if (completionStatus == ATTACH_ERROR_BADVERSION) { |
|
210 |
throw new IOException("Protocol mismatch with target VM"); |
|
211 |
} |
|
212 |
||
213 |
// Special-case the "load" command so that the right exception is |
|
214 |
// thrown. |
|
215 |
if (cmd.equals("load")) { |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
216 |
String msg = "Failed to load agent library"; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
217 |
if (!message.isEmpty()) |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
218 |
msg += ": " + message; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
219 |
throw new AgentLoadException(msg); |
12047 | 220 |
} else { |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
221 |
if (message.isEmpty()) |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
222 |
message = "Command failed in target VM"; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
223 |
throw new AttachOperationFailedException(message); |
12047 | 224 |
} |
225 |
} |
|
226 |
||
227 |
// Return the input stream so that the command output can be read |
|
228 |
return sis; |
|
229 |
} |
|
230 |
||
231 |
/* |
|
232 |
* InputStream for the socket connection to get target VM |
|
233 |
*/ |
|
234 |
private class SocketInputStream extends InputStream { |
|
235 |
int s; |
|
236 |
||
237 |
public SocketInputStream(int s) { |
|
238 |
this.s = s; |
|
239 |
} |
|
240 |
||
241 |
public synchronized int read() throws IOException { |
|
242 |
byte b[] = new byte[1]; |
|
243 |
int n = this.read(b, 0, 1); |
|
244 |
if (n == 1) { |
|
245 |
return b[0] & 0xff; |
|
246 |
} else { |
|
247 |
return -1; |
|
248 |
} |
|
249 |
} |
|
250 |
||
251 |
public synchronized int read(byte[] bs, int off, int len) throws IOException { |
|
252 |
if ((off < 0) || (off > bs.length) || (len < 0) || |
|
253 |
((off + len) > bs.length) || ((off + len) < 0)) { |
|
254 |
throw new IndexOutOfBoundsException(); |
|
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
12047
diff
changeset
|
255 |
} else if (len == 0) { |
12047 | 256 |
return 0; |
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
12047
diff
changeset
|
257 |
} |
12047 | 258 |
|
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
259 |
return VirtualMachineImpl.read(s, bs, off, len); |
12047 | 260 |
} |
261 |
||
55685
56b96d234f26
8227440: Implement fix to JDK-8224642 on all platforms
rschmelter
parents:
54065
diff
changeset
|
262 |
public synchronized void close() throws IOException { |
56b96d234f26
8227440: Implement fix to JDK-8224642 on all platforms
rschmelter
parents:
54065
diff
changeset
|
263 |
if (s != -1) { |
56b96d234f26
8227440: Implement fix to JDK-8224642 on all platforms
rschmelter
parents:
54065
diff
changeset
|
264 |
int toClose = s; |
56b96d234f26
8227440: Implement fix to JDK-8224642 on all platforms
rschmelter
parents:
54065
diff
changeset
|
265 |
s = -1; |
56b96d234f26
8227440: Implement fix to JDK-8224642 on all platforms
rschmelter
parents:
54065
diff
changeset
|
266 |
VirtualMachineImpl.close(toClose); |
56b96d234f26
8227440: Implement fix to JDK-8224642 on all platforms
rschmelter
parents:
54065
diff
changeset
|
267 |
} |
12047 | 268 |
} |
269 |
} |
|
270 |
||
271 |
/* |
|
272 |
* Write/sends the given to the target VM. String is transmitted in |
|
273 |
* UTF-8 encoding. |
|
274 |
*/ |
|
275 |
private void writeString(int fd, String s) throws IOException { |
|
276 |
if (s.length() > 0) { |
|
277 |
byte b[]; |
|
278 |
try { |
|
279 |
b = s.getBytes("UTF-8"); |
|
280 |
} 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
|
281 |
throw new InternalError(x); |
12047 | 282 |
} |
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
283 |
VirtualMachineImpl.write(fd, b, 0, b.length); |
12047 | 284 |
} |
285 |
byte b[] = new byte[1]; |
|
286 |
b[0] = 0; |
|
287 |
write(fd, b, 0, 1); |
|
288 |
} |
|
289 |
||
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
290 |
private File createAttachFile(int pid) throws IOException { |
49742
1196aa0be8be
8201247: Various cleanups in the attach framework
clanger
parents:
48485
diff
changeset
|
291 |
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
|
292 |
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
|
293 |
return f; |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
294 |
} |
12047 | 295 |
|
296 |
//-- native methods |
|
297 |
||
298 |
static native void sendQuitTo(int pid) throws IOException; |
|
299 |
||
300 |
static native void checkPermissions(String path) throws IOException; |
|
301 |
||
302 |
static native int socket() throws IOException; |
|
303 |
||
304 |
static native void connect(int fd, String path) throws IOException; |
|
305 |
||
306 |
static native void close(int fd) throws IOException; |
|
307 |
||
308 |
static native int read(int fd, byte buf[], int off, int bufLen) throws IOException; |
|
309 |
||
310 |
static native void write(int fd, byte buf[], int off, int bufLen) throws IOException; |
|
311 |
||
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
312 |
static native void createAttachFile0(String path); |
12047 | 313 |
|
314 |
static native String getTempDir(); |
|
315 |
||
316 |
static { |
|
317 |
System.loadLibrary("attach"); |
|
318 |
tmpdir = getTempDir(); |
|
319 |
} |
|
320 |
} |