2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2005, 2006, 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 |
|
|
27 |
import com.sun.tools.attach.VirtualMachine;
|
|
28 |
import com.sun.tools.attach.AgentLoadException;
|
|
29 |
import com.sun.tools.attach.AttachNotSupportedException;
|
|
30 |
import com.sun.tools.attach.spi.AttachProvider;
|
|
31 |
import java.io.InputStream;
|
|
32 |
import java.io.IOException;
|
|
33 |
import java.io.File;
|
|
34 |
import java.util.Properties;
|
|
35 |
|
|
36 |
/*
|
|
37 |
* Linux implementation of HotSpotVirtualMachine
|
|
38 |
*/
|
|
39 |
public class LinuxVirtualMachine extends HotSpotVirtualMachine {
|
|
40 |
|
|
41 |
// Indicates if this machine uses the old LinuxThreads
|
|
42 |
static boolean isLinuxThreads;
|
|
43 |
|
|
44 |
// The patch to the socket file created by the target VM
|
|
45 |
String path;
|
|
46 |
|
|
47 |
/**
|
|
48 |
* Attaches to the target VM
|
|
49 |
*/
|
|
50 |
LinuxVirtualMachine(AttachProvider provider, String vmid)
|
|
51 |
throws AttachNotSupportedException, IOException
|
|
52 |
{
|
|
53 |
super(provider, vmid);
|
|
54 |
|
|
55 |
// This provider only understands pids
|
|
56 |
int pid;
|
|
57 |
try {
|
|
58 |
pid = Integer.parseInt(vmid);
|
|
59 |
} catch (NumberFormatException x) {
|
|
60 |
throw new AttachNotSupportedException("Invalid process identifier");
|
|
61 |
}
|
|
62 |
|
|
63 |
// Find the socket file. If not found then we attempt to start the
|
|
64 |
// attach mechanism in the target VM by sending it a QUIT signal.
|
|
65 |
// Then we attempt to find the socket file again.
|
|
66 |
path = findSocketFile(pid);
|
|
67 |
if (path == null) {
|
|
68 |
File f = createAttachFile(pid);
|
|
69 |
try {
|
|
70 |
// On LinuxThreads each thread is a process and we don't have the
|
|
71 |
// pid of the VMThread which has SIGQUIT unblocked. To workaround
|
|
72 |
// this we get the pid of the "manager thread" that is created
|
|
73 |
// by the first call to pthread_create. This is parent of all
|
|
74 |
// threads (except the initial thread).
|
|
75 |
if (isLinuxThreads) {
|
|
76 |
int mpid;
|
|
77 |
try {
|
|
78 |
mpid = getLinuxThreadsManager(pid);
|
|
79 |
} catch (IOException x) {
|
|
80 |
throw new AttachNotSupportedException(x.getMessage());
|
|
81 |
}
|
|
82 |
assert(mpid >= 1);
|
|
83 |
sendQuitToChildrenOf(mpid);
|
|
84 |
} else {
|
|
85 |
sendQuitTo(pid);
|
|
86 |
}
|
|
87 |
|
|
88 |
// give the target VM time to start the attach mechanism
|
|
89 |
int i = 0;
|
|
90 |
long delay = 200;
|
|
91 |
int retries = (int)(attachTimeout() / delay);
|
|
92 |
do {
|
|
93 |
try {
|
|
94 |
Thread.sleep(delay);
|
|
95 |
} catch (InterruptedException x) { }
|
|
96 |
path = findSocketFile(pid);
|
|
97 |
i++;
|
|
98 |
} while (i <= retries && path == null);
|
|
99 |
if (path == null) {
|
|
100 |
throw new AttachNotSupportedException(
|
|
101 |
"Unable to open socket file: target process not responding " +
|
|
102 |
"or HotSpot VM not loaded");
|
|
103 |
}
|
|
104 |
} finally {
|
|
105 |
f.delete();
|
|
106 |
}
|
|
107 |
}
|
|
108 |
|
|
109 |
// Check that the file owner/permission to avoid attaching to
|
|
110 |
// bogus process
|
|
111 |
checkPermissions(path);
|
|
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 {
|
|
118 |
connect(s, path);
|
|
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) {
|
|
129 |
if (this.path != null) {
|
|
130 |
this.path = null;
|
|
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 {
|
|
145 |
assert args.length <= 3; // includes null
|
|
146 |
|
|
147 |
// did we detach?
|
|
148 |
String p;
|
|
149 |
synchronized (this) {
|
|
150 |
if (this.path == null) {
|
|
151 |
throw new IOException("Detached from target VM");
|
|
152 |
}
|
|
153 |
p = this.path;
|
|
154 |
}
|
|
155 |
|
|
156 |
// create UNIX socket
|
|
157 |
int s = socket();
|
|
158 |
|
|
159 |
// connect to target VM
|
|
160 |
try {
|
|
161 |
connect(s, p);
|
|
162 |
} catch (IOException x) {
|
|
163 |
close(s);
|
|
164 |
throw x;
|
|
165 |
}
|
|
166 |
|
|
167 |
IOException ioe = null;
|
|
168 |
|
|
169 |
// connected - write request
|
|
170 |
// <ver> <cmd> <args...>
|
|
171 |
try {
|
|
172 |
writeString(s, PROTOCOL_VERSION);
|
|
173 |
writeString(s, cmd);
|
|
174 |
|
|
175 |
for (int i=0; i<3; i++) {
|
|
176 |
if (i < args.length && args[i] != null) {
|
|
177 |
writeString(s, (String)args[i]);
|
|
178 |
} else {
|
|
179 |
writeString(s, "");
|
|
180 |
}
|
|
181 |
}
|
|
182 |
} catch (IOException x) {
|
|
183 |
ioe = x;
|
|
184 |
}
|
|
185 |
|
|
186 |
|
|
187 |
// Create an input stream to read reply
|
|
188 |
SocketInputStream sis = new SocketInputStream(s);
|
|
189 |
|
|
190 |
// Read the command completion status
|
|
191 |
int completionStatus;
|
|
192 |
try {
|
|
193 |
completionStatus = readInt(sis);
|
|
194 |
} catch (IOException x) {
|
|
195 |
sis.close();
|
|
196 |
if (ioe != null) {
|
|
197 |
throw ioe;
|
|
198 |
} else {
|
|
199 |
throw x;
|
|
200 |
}
|
|
201 |
}
|
|
202 |
|
|
203 |
if (completionStatus != 0) {
|
|
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")) {
|
|
216 |
throw new AgentLoadException("Failed to load agent library");
|
|
217 |
} else {
|
|
218 |
throw new IOException("Command failed in target VM");
|
|
219 |
}
|
|
220 |
}
|
|
221 |
|
|
222 |
// Return the input stream so that the command output can be read
|
|
223 |
return sis;
|
|
224 |
}
|
|
225 |
|
|
226 |
/*
|
|
227 |
* InputStream for the socket connection to get target VM
|
|
228 |
*/
|
|
229 |
private class SocketInputStream extends InputStream {
|
|
230 |
int s;
|
|
231 |
|
|
232 |
public SocketInputStream(int s) {
|
|
233 |
this.s = s;
|
|
234 |
}
|
|
235 |
|
|
236 |
public synchronized int read() throws IOException {
|
|
237 |
byte b[] = new byte[1];
|
|
238 |
int n = this.read(b, 0, 1);
|
|
239 |
if (n == 1) {
|
|
240 |
return b[0] & 0xff;
|
|
241 |
} else {
|
|
242 |
return -1;
|
|
243 |
}
|
|
244 |
}
|
|
245 |
|
|
246 |
public synchronized int read(byte[] bs, int off, int len) throws IOException {
|
|
247 |
if ((off < 0) || (off > bs.length) || (len < 0) ||
|
|
248 |
((off + len) > bs.length) || ((off + len) < 0)) {
|
|
249 |
throw new IndexOutOfBoundsException();
|
|
250 |
} else if (len == 0)
|
|
251 |
return 0;
|
|
252 |
|
|
253 |
return LinuxVirtualMachine.read(s, bs, off, len);
|
|
254 |
}
|
|
255 |
|
|
256 |
public void close() throws IOException {
|
|
257 |
LinuxVirtualMachine.close(s);
|
|
258 |
}
|
|
259 |
}
|
|
260 |
|
|
261 |
// Return the socket file for the given process.
|
|
262 |
// Checks working directory of process for .java_pid<pid>. If not
|
|
263 |
// found it looks in /tmp.
|
|
264 |
private String findSocketFile(int pid) {
|
|
265 |
// First check for a .java_pid<pid> file in the working directory
|
|
266 |
// of the target process
|
|
267 |
String fn = ".java_pid" + pid;
|
|
268 |
String path = "/proc/" + pid + "/cwd/" + fn;
|
|
269 |
File f = new File(path);
|
|
270 |
if (!f.exists()) {
|
|
271 |
// Not found, so try /tmp
|
|
272 |
path = "/tmp/" + fn;
|
|
273 |
f = new File(path);
|
|
274 |
if (!f.exists()) {
|
|
275 |
return null; // not found
|
|
276 |
}
|
|
277 |
}
|
|
278 |
return path;
|
|
279 |
}
|
|
280 |
|
|
281 |
// On Solaris/Linux a simple handshake is used to start the attach mechanism
|
|
282 |
// if not already started. The client creates a .attach_pid<pid> file in the
|
|
283 |
// target VM's working directory (or /tmp), and the SIGQUIT handler checks
|
|
284 |
// for the file.
|
|
285 |
private File createAttachFile(int pid) throws IOException {
|
|
286 |
String fn = ".attach_pid" + pid;
|
|
287 |
String path = "/proc/" + pid + "/cwd/" + fn;
|
|
288 |
File f = new File(path);
|
|
289 |
try {
|
|
290 |
f.createNewFile();
|
|
291 |
} catch (IOException x) {
|
|
292 |
path = "/tmp/" + fn;
|
|
293 |
f = new File(path);
|
|
294 |
f.createNewFile();
|
|
295 |
}
|
|
296 |
return f;
|
|
297 |
}
|
|
298 |
|
|
299 |
/*
|
|
300 |
* Write/sends the given to the target VM. String is transmitted in
|
|
301 |
* UTF-8 encoding.
|
|
302 |
*/
|
|
303 |
private void writeString(int fd, String s) throws IOException {
|
|
304 |
if (s.length() > 0) {
|
|
305 |
byte b[];
|
|
306 |
try {
|
|
307 |
b = s.getBytes("UTF-8");
|
|
308 |
} catch (java.io.UnsupportedEncodingException x) {
|
|
309 |
throw new InternalError();
|
|
310 |
}
|
|
311 |
LinuxVirtualMachine.write(fd, b, 0, b.length);
|
|
312 |
}
|
|
313 |
byte b[] = new byte[1];
|
|
314 |
b[0] = 0;
|
|
315 |
write(fd, b, 0, 1);
|
|
316 |
}
|
|
317 |
|
|
318 |
|
|
319 |
//-- native methods
|
|
320 |
|
|
321 |
static native boolean isLinuxThreads();
|
|
322 |
|
|
323 |
static native int getLinuxThreadsManager(int pid) throws IOException;
|
|
324 |
|
|
325 |
static native void sendQuitToChildrenOf(int pid) throws IOException;
|
|
326 |
|
|
327 |
static native void sendQuitTo(int pid) throws IOException;
|
|
328 |
|
|
329 |
static native void checkPermissions(String path) throws IOException;
|
|
330 |
|
|
331 |
static native int socket() throws IOException;
|
|
332 |
|
|
333 |
static native void connect(int fd, String path) throws IOException;
|
|
334 |
|
|
335 |
static native void close(int fd) throws IOException;
|
|
336 |
|
|
337 |
static native int read(int fd, byte buf[], int off, int bufLen) throws IOException;
|
|
338 |
|
|
339 |
static native void write(int fd, byte buf[], int off, int bufLen) throws IOException;
|
|
340 |
|
|
341 |
static {
|
|
342 |
System.loadLibrary("attach");
|
|
343 |
isLinuxThreads = isLinuxThreads();
|
|
344 |
}
|
|
345 |
}
|