author | dcubed |
Tue, 05 Jul 2016 14:00:21 -0700 | |
changeset 39696 | 80c9894e18f9 |
parent 26216 | 5e46c782b43c |
child 40685 | e6f3a9fff607 |
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 |
// kill -QUIT will tickle target VM to check for the |
|
75 |
// attach file. |
|
76 |
sigquit(pid); |
|
77 |
||
78 |
// give the target VM time to start the attach mechanism |
|
79 |
int i = 0; |
|
80 |
long delay = 200; |
|
81 |
int retries = (int)(attachTimeout() / delay); |
|
82 |
do { |
|
83 |
try { |
|
84 |
Thread.sleep(delay); |
|
85 |
} catch (InterruptedException x) { } |
|
86 |
try { |
|
87 |
fd = openDoor(pid); |
|
88 |
} catch (FileNotFoundException fnf2) { } |
|
89 |
i++; |
|
90 |
} while (i <= retries && fd == -1); |
|
91 |
if (fd == -1) { |
|
92 |
throw new AttachNotSupportedException( |
|
93 |
"Unable to open door: target process not responding or " + |
|
94 |
"HotSpot VM not loaded"); |
|
95 |
} |
|
96 |
} finally { |
|
97 |
f.delete(); |
|
98 |
} |
|
99 |
} |
|
100 |
assert fd >= 0; |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Detach from the target VM |
|
105 |
*/ |
|
106 |
public void detach() throws IOException { |
|
107 |
synchronized (this) { |
|
108 |
if (fd != -1) { |
|
109 |
close(fd); |
|
110 |
fd = -1; |
|
111 |
} |
|
112 |
} |
|
113 |
} |
|
114 |
||
115 |
/** |
|
116 |
* Execute the given command in the target VM. |
|
117 |
*/ |
|
118 |
InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException { |
|
119 |
assert args.length <= 3; // includes null |
|
120 |
||
121 |
// first check that we are still attached |
|
122 |
int door; |
|
123 |
synchronized (this) { |
|
124 |
if (fd == -1) { |
|
125 |
throw new IOException("Detached from target VM"); |
|
126 |
} |
|
127 |
door = fd; |
|
128 |
} |
|
129 |
||
130 |
// enqueue the command via a door call |
|
131 |
int s = enqueue(door, cmd, args); |
|
132 |
assert s >= 0; // valid file descriptor |
|
133 |
||
134 |
// The door call returns a file descriptor (one end of a socket pair). |
|
135 |
// Create an input stream around it. |
|
136 |
SocketInputStream sis = new SocketInputStream(s); |
|
137 |
||
138 |
// Read the command completion status |
|
139 |
int completionStatus; |
|
140 |
try { |
|
141 |
completionStatus = readInt(sis); |
|
142 |
} catch (IOException ioe) { |
|
143 |
sis.close(); |
|
144 |
throw ioe; |
|
145 |
} |
|
146 |
||
147 |
// If non-0 it means an error but we need to special-case the |
|
148 |
// "load" command to ensure that the right exception is thrown. |
|
149 |
if (completionStatus != 0) { |
|
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
150 |
// 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
|
151 |
String message = readErrorMessage(sis); |
2 | 152 |
sis.close(); |
153 |
if (cmd.equals("load")) { |
|
154 |
throw new AgentLoadException("Failed to load agent library"); |
|
155 |
} else { |
|
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
156 |
if (message == null) { |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
157 |
throw new AttachOperationFailedException("Command failed in target VM"); |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
158 |
} else { |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
159 |
throw new AttachOperationFailedException(message); |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14342
diff
changeset
|
160 |
} |
2 | 161 |
} |
162 |
} |
|
163 |
||
164 |
// Return the input stream so that the command output can be read |
|
165 |
return sis; |
|
166 |
} |
|
167 |
||
168 |
// InputStream over a socket |
|
169 |
private class SocketInputStream extends InputStream { |
|
170 |
int s; |
|
171 |
||
172 |
public SocketInputStream(int s) { |
|
173 |
this.s = s; |
|
174 |
} |
|
175 |
||
176 |
public synchronized int read() throws IOException { |
|
177 |
byte b[] = new byte[1]; |
|
178 |
int n = this.read(b, 0, 1); |
|
179 |
if (n == 1) { |
|
180 |
return b[0] & 0xff; |
|
181 |
} else { |
|
182 |
return -1; |
|
183 |
} |
|
184 |
} |
|
185 |
||
186 |
public synchronized int read(byte[] bs, int off, int len) throws IOException { |
|
187 |
if ((off < 0) || (off > bs.length) || (len < 0) || |
|
188 |
((off + len) > bs.length) || ((off + len) < 0)) { |
|
189 |
throw new IndexOutOfBoundsException(); |
|
190 |
} else if (len == 0) |
|
191 |
return 0; |
|
192 |
||
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
193 |
return VirtualMachineImpl.read(s, bs, off, len); |
2 | 194 |
} |
195 |
||
196 |
public void close() throws IOException { |
|
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
197 |
VirtualMachineImpl.close(s); |
2 | 198 |
} |
199 |
} |
|
200 |
||
11692
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
7668
diff
changeset
|
201 |
// The door is attached to .java_pid<pid> in the temporary directory. |
2 | 202 |
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
|
203 |
String path = tmpdir + "/.java_pid" + pid;; |
dd4289e8b9e3
7132199: sun/management/jmxremote/bootstrap/JvmstatCountersTest.java failing on all platforms
sla
parents:
7668
diff
changeset
|
204 |
fd = open(path); |
2 | 205 |
|
206 |
// Check that the file owner/permission to avoid attaching to |
|
207 |
// bogus process |
|
208 |
try { |
|
209 |
checkPermissions(path); |
|
210 |
} catch (IOException ioe) { |
|
211 |
close(fd); |
|
212 |
throw ioe; |
|
213 |
} |
|
214 |
return fd; |
|
215 |
} |
|
216 |
||
217 |
// On Solaris/Linux a simple handshake is used to start the attach mechanism |
|
218 |
// 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
|
219 |
// 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
|
220 |
// handler checks for the file. |
2 | 221 |
private File createAttachFile(int pid) throws IOException { |
222 |
String fn = ".attach_pid" + pid; |
|
223 |
String path = "/proc/" + pid + "/cwd/" + fn; |
|
224 |
File f = new File(path); |
|
225 |
try { |
|
226 |
f.createNewFile(); |
|
227 |
} catch (IOException x) { |
|
5771
1f415c7e277d
6950927: Testcase failure sun/management/jmxremote/bootstrap/JvmstatCountersTest.java
alanb
parents:
5506
diff
changeset
|
228 |
f = new File(tmpdir, fn); |
2 | 229 |
f.createNewFile(); |
230 |
} |
|
231 |
return f; |
|
232 |
} |
|
233 |
||
234 |
//-- native methods |
|
235 |
||
236 |
static native int open(String path) throws IOException; |
|
237 |
||
238 |
static native void close(int fd) throws IOException; |
|
239 |
||
240 |
static native int read(int fd, byte buf[], int off, int buflen) throws IOException; |
|
241 |
||
242 |
static native void checkPermissions(String path) throws IOException; |
|
243 |
||
244 |
static native void sigquit(int pid) throws IOException; |
|
245 |
||
246 |
// enqueue a command (and arguments) to the given door |
|
247 |
static native int enqueue(int fd, String cmd, Object ... args) |
|
248 |
throws IOException; |
|
249 |
||
250 |
static { |
|
251 |
System.loadLibrary("attach"); |
|
252 |
} |
|
253 |
} |