author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 45004 | jdk/src/jdk.attach/solaris/classes/sun/tools/attach/VirtualMachineImpl.java@ea3137042a61 |
child 48485 | 258a4dab74a7 |
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 |
import java.io.FileNotFoundException; |
|
36 |
||
37 |
/* |
|
38 |
* Solaris implementation of HotSpotVirtualMachine. |
|
39 |
*/ |
|
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
40 |
public class VirtualMachineImpl extends HotSpotVirtualMachine { |
11692
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
7668
diff
changeset
|
41 |
// "/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:
7668
diff
changeset
|
42 |
// .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:
7668
diff
changeset
|
43 |
// location is the same for all processes, otherwise the tools |
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
7668
diff
changeset
|
44 |
// will not be able to find all Hotspot processes. |
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
7668
diff
changeset
|
45 |
// Any changes to this needs to be synchronized with HotSpot. |
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
7668
diff
changeset
|
46 |
private static final String tmpdir = "/tmp"; |
2 | 47 |
|
48 |
// door descriptor; |
|
49 |
private int fd = -1; |
|
50 |
||
51 |
/** |
|
52 |
* Attaches to the target VM |
|
53 |
*/ |
|
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
54 |
VirtualMachineImpl(AttachProvider provider, String vmid) |
2 | 55 |
throws AttachNotSupportedException, IOException |
56 |
{ |
|
57 |
super(provider, vmid); |
|
58 |
// This provider only understands process-ids (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 |
// Opens the door file to the target VM. If the file is not |
|
67 |
// found it might mean that the attach mechanism isn't started in the |
|
68 |
// target VM so we attempt to start it and retry. |
|
69 |
try { |
|
70 |
fd = openDoor(pid); |
|
71 |
} catch (FileNotFoundException fnf1) { |
|
72 |
File f = createAttachFile(pid); |
|
73 |
try { |
|
74 |
sigquit(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; |
2 | 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; |
2 | 84 |
try { |
85 |
Thread.sleep(delay); |
|
86 |
} catch (InterruptedException x) { } |
|
87 |
try { |
|
88 |
fd = openDoor(pid); |
|
40685
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
89 |
} catch (FileNotFoundException fnf2) { |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
90 |
// pass |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
91 |
} |
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 |
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
|
94 |
if (time_spend > timeout/2 && fd == -1) { |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
95 |
// 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
|
96 |
sigquit(pid); |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
97 |
} |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
98 |
} while (time_spend <= timeout && fd == -1); |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
99 |
if (fd == -1) { |
2 | 100 |
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
|
101 |
String.format("Unable to open door %s: " + |
e6f3a9fff607
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
dsamersoff
parents:
26216
diff
changeset
|
102 |
"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
|
103 |
"or HotSpot VM not loaded", f.getPath(), pid, time_spend)); |
2 | 104 |
} |
105 |
} finally { |
|
106 |
f.delete(); |
|
107 |
} |
|
108 |
} |
|
109 |
assert fd >= 0; |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* Detach from the target VM |
|
114 |
*/ |
|
115 |
public void detach() throws IOException { |
|
116 |
synchronized (this) { |
|
117 |
if (fd != -1) { |
|
118 |
close(fd); |
|
119 |
fd = -1; |
|
120 |
} |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Execute the given command in the target VM. |
|
126 |
*/ |
|
127 |
InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException { |
|
128 |
assert args.length <= 3; // includes null |
|
129 |
||
130 |
// first check that we are still attached |
|
131 |
int door; |
|
132 |
synchronized (this) { |
|
133 |
if (fd == -1) { |
|
134 |
throw new IOException("Detached from target VM"); |
|
135 |
} |
|
136 |
door = fd; |
|
137 |
} |
|
138 |
||
139 |
// enqueue the command via a door call |
|
140 |
int s = enqueue(door, cmd, args); |
|
141 |
assert s >= 0; // valid file descriptor |
|
142 |
||
143 |
// The door call returns a file descriptor (one end of a socket pair). |
|
144 |
// Create an input stream around it. |
|
145 |
SocketInputStream sis = new SocketInputStream(s); |
|
146 |
||
147 |
// Read the command completion status |
|
148 |
int completionStatus; |
|
149 |
try { |
|
150 |
completionStatus = readInt(sis); |
|
151 |
} catch (IOException ioe) { |
|
152 |
sis.close(); |
|
153 |
throw ioe; |
|
154 |
} |
|
155 |
||
156 |
// If non-0 it means an error but we need to special-case the |
|
157 |
// "load" command to ensure that the right exception is thrown. |
|
158 |
if (completionStatus != 0) { |
|
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
159 |
// 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
|
160 |
String message = readErrorMessage(sis); |
2 | 161 |
sis.close(); |
162 |
if (cmd.equals("load")) { |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
163 |
String msg = "Failed to load agent library"; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
164 |
if (!message.isEmpty()) |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
165 |
msg += ": " + message; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
166 |
throw new AgentLoadException(msg); |
2 | 167 |
} else { |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
168 |
if (message.isEmpty()) |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
169 |
message = "Command failed in target VM"; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
40685
diff
changeset
|
170 |
throw new AttachOperationFailedException(message); |
2 | 171 |
} |
172 |
} |
|
173 |
||
174 |
// Return the input stream so that the command output can be read |
|
175 |
return sis; |
|
176 |
} |
|
177 |
||
178 |
// InputStream over a socket |
|
179 |
private class SocketInputStream extends InputStream { |
|
180 |
int s; |
|
181 |
||
182 |
public SocketInputStream(int s) { |
|
183 |
this.s = s; |
|
184 |
} |
|
185 |
||
186 |
public synchronized int read() throws IOException { |
|
187 |
byte b[] = new byte[1]; |
|
188 |
int n = this.read(b, 0, 1); |
|
189 |
if (n == 1) { |
|
190 |
return b[0] & 0xff; |
|
191 |
} else { |
|
192 |
return -1; |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
public synchronized int read(byte[] bs, int off, int len) throws IOException { |
|
197 |
if ((off < 0) || (off > bs.length) || (len < 0) || |
|
198 |
((off + len) > bs.length) || ((off + len) < 0)) { |
|
199 |
throw new IndexOutOfBoundsException(); |
|
200 |
} else if (len == 0) |
|
201 |
return 0; |
|
202 |
||
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
203 |
return VirtualMachineImpl.read(s, bs, off, len); |
2 | 204 |
} |
205 |
||
206 |
public void close() throws IOException { |
|
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
207 |
VirtualMachineImpl.close(s); |
2 | 208 |
} |
209 |
} |
|
210 |
||
11692
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
7668
diff
changeset
|
211 |
// The door is attached to .java_pid<pid> in the temporary directory. |
2 | 212 |
private int openDoor(int pid) throws IOException { |
11692
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
7668
diff
changeset
|
213 |
String path = tmpdir + "/.java_pid" + pid;; |
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
7668
diff
changeset
|
214 |
fd = open(path); |
2 | 215 |
|
216 |
// Check that the file owner/permission to avoid attaching to |
|
217 |
// bogus process |
|
218 |
try { |
|
219 |
checkPermissions(path); |
|
220 |
} catch (IOException ioe) { |
|
221 |
close(fd); |
|
222 |
throw ioe; |
|
223 |
} |
|
224 |
return fd; |
|
225 |
} |
|
226 |
||
227 |
// On Solaris/Linux a simple handshake is used to start the attach mechanism |
|
228 |
// 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
|
229 |
// target VM's working directory (or temporary directory), and the SIGQUIT |
1f415c7e277d
6950927: Testcase failure sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
alanb
parents:
5506
diff
changeset
|
230 |
// handler checks for the file. |
2 | 231 |
private File createAttachFile(int pid) throws IOException { |
232 |
String fn = ".attach_pid" + pid; |
|
233 |
String path = "/proc/" + pid + "/cwd/" + fn; |
|
234 |
File f = new File(path); |
|
235 |
try { |
|
236 |
f.createNewFile(); |
|
237 |
} catch (IOException x) { |
|
5771
1f415c7e277d
6950927: Testcase failure sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
alanb
parents:
5506
diff
changeset
|
238 |
f = new File(tmpdir, fn); |
2 | 239 |
f.createNewFile(); |
240 |
} |
|
241 |
return f; |
|
242 |
} |
|
243 |
||
244 |
//-- native methods |
|
245 |
||
246 |
static native int open(String path) throws IOException; |
|
247 |
||
248 |
static native void close(int fd) throws IOException; |
|
249 |
||
250 |
static native int read(int fd, byte buf[], int off, int buflen) throws IOException; |
|
251 |
||
252 |
static native void checkPermissions(String path) throws IOException; |
|
253 |
||
254 |
static native void sigquit(int pid) throws IOException; |
|
255 |
||
256 |
// enqueue a command (and arguments) to the given door |
|
257 |
static native int enqueue(int fd, String cmd, Object ... args) |
|
258 |
throws IOException; |
|
259 |
||
260 |
static { |
|
261 |
System.loadLibrary("attach"); |
|
262 |
} |
|
263 |
} |