2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2005, 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 sun.tools.attach.HotSpotVirtualMachine;
|
|
32 |
import java.io.IOException;
|
|
33 |
import java.io.File;
|
|
34 |
import java.io.InputStream;
|
|
35 |
import java.util.Properties;
|
|
36 |
import java.util.Random;
|
|
37 |
|
|
38 |
public class WindowsVirtualMachine extends HotSpotVirtualMachine {
|
|
39 |
|
|
40 |
// the enqueue code stub (copied into each target VM)
|
|
41 |
private static byte[] stub;
|
|
42 |
|
|
43 |
private volatile long hProcess; // handle to the process
|
|
44 |
|
|
45 |
WindowsVirtualMachine(AttachProvider provider, String id)
|
|
46 |
throws AttachNotSupportedException, IOException
|
|
47 |
{
|
|
48 |
super(provider, id);
|
|
49 |
|
|
50 |
int pid;
|
|
51 |
try {
|
|
52 |
pid = Integer.parseInt(id);
|
|
53 |
} catch (NumberFormatException x) {
|
|
54 |
throw new AttachNotSupportedException("Invalid process identifier");
|
|
55 |
}
|
|
56 |
hProcess = openProcess(pid);
|
|
57 |
|
|
58 |
// The target VM might be a pre-6.0 VM so we enqueue a "null" command
|
|
59 |
// which minimally tests that the enqueue function exists in the target
|
|
60 |
// VM.
|
|
61 |
try {
|
|
62 |
enqueue(hProcess, stub, null, null);
|
|
63 |
} catch (IOException x) {
|
|
64 |
throw new AttachNotSupportedException(x.getMessage());
|
|
65 |
}
|
|
66 |
}
|
|
67 |
|
|
68 |
public void detach() throws IOException {
|
|
69 |
synchronized (this) {
|
|
70 |
if (hProcess != -1) {
|
|
71 |
closeProcess(hProcess);
|
|
72 |
hProcess = -1;
|
|
73 |
}
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
InputStream execute(String cmd, Object ... args)
|
|
78 |
throws AgentLoadException, IOException
|
|
79 |
{
|
|
80 |
assert args.length <= 3; // includes null
|
|
81 |
|
|
82 |
// create a pipe using a random name
|
|
83 |
int r = (new Random()).nextInt();
|
|
84 |
String pipename = "\\\\.\\pipe\\javatool" + r;
|
|
85 |
long hPipe = createPipe(pipename);
|
|
86 |
|
|
87 |
// check if we are detached - in theory it's possible that detach is invoked
|
|
88 |
// after this check but before we enqueue the command.
|
|
89 |
if (hProcess == -1) {
|
|
90 |
closePipe(hPipe);
|
|
91 |
throw new IOException("Detached from target VM");
|
|
92 |
}
|
|
93 |
|
|
94 |
try {
|
|
95 |
// enqueue the command to the process
|
|
96 |
enqueue(hProcess, stub, cmd, pipename, args);
|
|
97 |
|
|
98 |
// wait for command to complete - process will connect with the
|
|
99 |
// completion status
|
|
100 |
connectPipe(hPipe);
|
|
101 |
|
|
102 |
// create an input stream for the pipe
|
|
103 |
PipedInputStream is = new PipedInputStream(hPipe);
|
|
104 |
|
|
105 |
// read completion status
|
|
106 |
int status = readInt(is);
|
|
107 |
if (status != 0) {
|
|
108 |
// special case the load command so that the right exception is thrown
|
|
109 |
if (cmd.equals("load")) {
|
|
110 |
throw new AgentLoadException("Failed to load agent library");
|
|
111 |
} else {
|
|
112 |
throw new IOException("Command failed in target VM");
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
// return the input stream
|
|
117 |
return is;
|
|
118 |
|
|
119 |
} catch (IOException ioe) {
|
|
120 |
closePipe(hPipe);
|
|
121 |
throw ioe;
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|
|
125 |
// An InputStream based on a pipe to the target VM
|
|
126 |
private class PipedInputStream extends InputStream {
|
|
127 |
|
|
128 |
private long hPipe;
|
|
129 |
|
|
130 |
public PipedInputStream(long hPipe) {
|
|
131 |
this.hPipe = hPipe;
|
|
132 |
}
|
|
133 |
|
|
134 |
public synchronized int read() throws IOException {
|
|
135 |
byte b[] = new byte[1];
|
|
136 |
int n = this.read(b, 0, 1);
|
|
137 |
if (n == 1) {
|
|
138 |
return b[0] & 0xff;
|
|
139 |
} else {
|
|
140 |
return -1;
|
|
141 |
}
|
|
142 |
}
|
|
143 |
|
|
144 |
public synchronized int read(byte[] bs, int off, int len) throws IOException {
|
|
145 |
if ((off < 0) || (off > bs.length) || (len < 0) ||
|
|
146 |
((off + len) > bs.length) || ((off + len) < 0)) {
|
|
147 |
throw new IndexOutOfBoundsException();
|
|
148 |
} else if (len == 0)
|
|
149 |
return 0;
|
|
150 |
|
|
151 |
return WindowsVirtualMachine.readPipe(hPipe, bs, off, len);
|
|
152 |
}
|
|
153 |
|
|
154 |
public void close() throws IOException {
|
|
155 |
if (hPipe != -1) {
|
|
156 |
WindowsVirtualMachine.closePipe(hPipe);
|
|
157 |
hPipe = -1;
|
|
158 |
}
|
|
159 |
}
|
|
160 |
}
|
|
161 |
|
|
162 |
|
|
163 |
//-- native methods
|
|
164 |
|
|
165 |
static native void init();
|
|
166 |
|
|
167 |
static native byte[] generateStub();
|
|
168 |
|
|
169 |
static native long openProcess(int pid) throws IOException;
|
|
170 |
|
|
171 |
static native void closeProcess(long hProcess) throws IOException;
|
|
172 |
|
|
173 |
static native long createPipe(String name) throws IOException;
|
|
174 |
|
|
175 |
static native void closePipe(long hPipe) throws IOException;
|
|
176 |
|
|
177 |
static native void connectPipe(long hPipe) throws IOException;
|
|
178 |
|
|
179 |
static native int readPipe(long hPipe, byte buf[], int off, int buflen) throws IOException;
|
|
180 |
|
|
181 |
static native void enqueue(long hProcess, byte[] stub,
|
|
182 |
String cmd, String pipename, Object ... args) throws IOException;
|
|
183 |
|
|
184 |
static {
|
|
185 |
System.loadLibrary("attach");
|
|
186 |
init(); // native initialization
|
|
187 |
stub = generateStub(); // generate stub to copy into target process
|
|
188 |
}
|
|
189 |
}
|