2
|
1 |
/*
|
|
2 |
* Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
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.io.FileNotFoundException;
|
|
35 |
import java.util.Properties;
|
|
36 |
|
|
37 |
/*
|
|
38 |
* Solaris implementation of HotSpotVirtualMachine.
|
|
39 |
*/
|
|
40 |
public class SolarisVirtualMachine extends HotSpotVirtualMachine {
|
|
41 |
|
|
42 |
// door descriptor;
|
|
43 |
private int fd = -1;
|
|
44 |
|
|
45 |
/**
|
|
46 |
* Attaches to the target VM
|
|
47 |
*/
|
|
48 |
SolarisVirtualMachine(AttachProvider provider, String vmid)
|
|
49 |
throws AttachNotSupportedException, IOException
|
|
50 |
{
|
|
51 |
super(provider, vmid);
|
|
52 |
// This provider only understands process-ids (pids).
|
|
53 |
int pid;
|
|
54 |
try {
|
|
55 |
pid = Integer.parseInt(vmid);
|
|
56 |
} catch (NumberFormatException x) {
|
|
57 |
throw new AttachNotSupportedException("invalid process identifier");
|
|
58 |
}
|
|
59 |
|
|
60 |
// Opens the door file to the target VM. If the file is not
|
|
61 |
// found it might mean that the attach mechanism isn't started in the
|
|
62 |
// target VM so we attempt to start it and retry.
|
|
63 |
try {
|
|
64 |
fd = openDoor(pid);
|
|
65 |
} catch (FileNotFoundException fnf1) {
|
|
66 |
File f = createAttachFile(pid);
|
|
67 |
try {
|
|
68 |
// kill -QUIT will tickle target VM to check for the
|
|
69 |
// attach file.
|
|
70 |
sigquit(pid);
|
|
71 |
|
|
72 |
// give the target VM time to start the attach mechanism
|
|
73 |
int i = 0;
|
|
74 |
long delay = 200;
|
|
75 |
int retries = (int)(attachTimeout() / delay);
|
|
76 |
do {
|
|
77 |
try {
|
|
78 |
Thread.sleep(delay);
|
|
79 |
} catch (InterruptedException x) { }
|
|
80 |
try {
|
|
81 |
fd = openDoor(pid);
|
|
82 |
} catch (FileNotFoundException fnf2) { }
|
|
83 |
i++;
|
|
84 |
} while (i <= retries && fd == -1);
|
|
85 |
if (fd == -1) {
|
|
86 |
throw new AttachNotSupportedException(
|
|
87 |
"Unable to open door: target process not responding or " +
|
|
88 |
"HotSpot VM not loaded");
|
|
89 |
}
|
|
90 |
} finally {
|
|
91 |
f.delete();
|
|
92 |
}
|
|
93 |
}
|
|
94 |
assert fd >= 0;
|
|
95 |
}
|
|
96 |
|
|
97 |
/**
|
|
98 |
* Detach from the target VM
|
|
99 |
*/
|
|
100 |
public void detach() throws IOException {
|
|
101 |
synchronized (this) {
|
|
102 |
if (fd != -1) {
|
|
103 |
close(fd);
|
|
104 |
fd = -1;
|
|
105 |
}
|
|
106 |
}
|
|
107 |
}
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Execute the given command in the target VM.
|
|
111 |
*/
|
|
112 |
InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {
|
|
113 |
assert args.length <= 3; // includes null
|
|
114 |
|
|
115 |
// first check that we are still attached
|
|
116 |
int door;
|
|
117 |
synchronized (this) {
|
|
118 |
if (fd == -1) {
|
|
119 |
throw new IOException("Detached from target VM");
|
|
120 |
}
|
|
121 |
door = fd;
|
|
122 |
}
|
|
123 |
|
|
124 |
// enqueue the command via a door call
|
|
125 |
int s = enqueue(door, cmd, args);
|
|
126 |
assert s >= 0; // valid file descriptor
|
|
127 |
|
|
128 |
// The door call returns a file descriptor (one end of a socket pair).
|
|
129 |
// Create an input stream around it.
|
|
130 |
SocketInputStream sis = new SocketInputStream(s);
|
|
131 |
|
|
132 |
// Read the command completion status
|
|
133 |
int completionStatus;
|
|
134 |
try {
|
|
135 |
completionStatus = readInt(sis);
|
|
136 |
} catch (IOException ioe) {
|
|
137 |
sis.close();
|
|
138 |
throw ioe;
|
|
139 |
}
|
|
140 |
|
|
141 |
// If non-0 it means an error but we need to special-case the
|
|
142 |
// "load" command to ensure that the right exception is thrown.
|
|
143 |
if (completionStatus != 0) {
|
|
144 |
sis.close();
|
|
145 |
if (cmd.equals("load")) {
|
|
146 |
throw new AgentLoadException("Failed to load agent library");
|
|
147 |
} else {
|
|
148 |
throw new IOException("Command failed in target VM");
|
|
149 |
}
|
|
150 |
}
|
|
151 |
|
|
152 |
// Return the input stream so that the command output can be read
|
|
153 |
return sis;
|
|
154 |
}
|
|
155 |
|
|
156 |
// InputStream over a socket
|
|
157 |
private class SocketInputStream extends InputStream {
|
|
158 |
int s;
|
|
159 |
|
|
160 |
public SocketInputStream(int s) {
|
|
161 |
this.s = s;
|
|
162 |
}
|
|
163 |
|
|
164 |
public synchronized int read() throws IOException {
|
|
165 |
byte b[] = new byte[1];
|
|
166 |
int n = this.read(b, 0, 1);
|
|
167 |
if (n == 1) {
|
|
168 |
return b[0] & 0xff;
|
|
169 |
} else {
|
|
170 |
return -1;
|
|
171 |
}
|
|
172 |
}
|
|
173 |
|
|
174 |
public synchronized int read(byte[] bs, int off, int len) throws IOException {
|
|
175 |
if ((off < 0) || (off > bs.length) || (len < 0) ||
|
|
176 |
((off + len) > bs.length) || ((off + len) < 0)) {
|
|
177 |
throw new IndexOutOfBoundsException();
|
|
178 |
} else if (len == 0)
|
|
179 |
return 0;
|
|
180 |
|
|
181 |
return SolarisVirtualMachine.read(s, bs, off, len);
|
|
182 |
}
|
|
183 |
|
|
184 |
public void close() throws IOException {
|
|
185 |
SolarisVirtualMachine.close(s);
|
|
186 |
}
|
|
187 |
}
|
|
188 |
|
|
189 |
// The door is attached to .java_pid<pid> in the target VM's working
|
|
190 |
// directory or /tmp.
|
|
191 |
private int openDoor(int pid) throws IOException {
|
|
192 |
// First check for a .java_pid<pid> file in the working directory
|
|
193 |
// of the target process
|
|
194 |
String fn = ".java_pid" + pid;
|
|
195 |
String path = "/proc/" + pid + "/cwd/" + fn;
|
|
196 |
try {
|
|
197 |
fd = open(path);
|
|
198 |
} catch (FileNotFoundException fnf) {
|
|
199 |
path = "/tmp/" + fn;
|
|
200 |
fd = open(path);
|
|
201 |
}
|
|
202 |
|
|
203 |
// Check that the file owner/permission to avoid attaching to
|
|
204 |
// bogus process
|
|
205 |
try {
|
|
206 |
checkPermissions(path);
|
|
207 |
} catch (IOException ioe) {
|
|
208 |
close(fd);
|
|
209 |
throw ioe;
|
|
210 |
}
|
|
211 |
return fd;
|
|
212 |
}
|
|
213 |
|
|
214 |
// On Solaris/Linux a simple handshake is used to start the attach mechanism
|
|
215 |
// if not already started. The client creates a .attach_pid<pid> file in the
|
|
216 |
// target VM's working directory (or /tmp), and the SIGQUIT handler checks
|
|
217 |
// for the file.
|
|
218 |
private File createAttachFile(int pid) throws IOException {
|
|
219 |
String fn = ".attach_pid" + pid;
|
|
220 |
String path = "/proc/" + pid + "/cwd/" + fn;
|
|
221 |
File f = new File(path);
|
|
222 |
try {
|
|
223 |
f.createNewFile();
|
|
224 |
} catch (IOException x) {
|
|
225 |
path = "/tmp/" + fn;
|
|
226 |
f = new File(path);
|
|
227 |
f.createNewFile();
|
|
228 |
}
|
|
229 |
return f;
|
|
230 |
}
|
|
231 |
|
|
232 |
//-- native methods
|
|
233 |
|
|
234 |
static native int open(String path) throws IOException;
|
|
235 |
|
|
236 |
static native void close(int fd) throws IOException;
|
|
237 |
|
|
238 |
static native int read(int fd, byte buf[], int off, int buflen) throws IOException;
|
|
239 |
|
|
240 |
static native void checkPermissions(String path) throws IOException;
|
|
241 |
|
|
242 |
static native void sigquit(int pid) throws IOException;
|
|
243 |
|
|
244 |
// enqueue a command (and arguments) to the given door
|
|
245 |
static native int enqueue(int fd, String cmd, Object ... args)
|
|
246 |
throws IOException;
|
|
247 |
|
|
248 |
static {
|
|
249 |
System.loadLibrary("attach");
|
|
250 |
}
|
|
251 |
}
|