author | ctornqvi |
Wed, 18 Feb 2015 19:28:08 -0800 | |
changeset 29105 | 3927f8f0bf4b |
parent 26460 | 68a3e0abedfd |
child 45004 | ea3137042a61 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
5506
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:
5506
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:
5506
diff
changeset
|
31 |
|
2 | 32 |
import sun.tools.attach.HotSpotVirtualMachine; |
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
5506
diff
changeset
|
33 |
|
2 | 34 |
import java.io.IOException; |
35 |
import java.io.InputStream; |
|
36 |
import java.util.Random; |
|
37 |
||
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
38 |
public class VirtualMachineImpl extends HotSpotVirtualMachine { |
2 | 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 |
||
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
45 |
VirtualMachineImpl(AttachProvider provider, String id) |
2 | 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) { |
|
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
5506
diff
changeset
|
108 |
// 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:
5506
diff
changeset
|
109 |
String message = readErrorMessage(is); |
26460
68a3e0abedfd
8057558: VirtualMachineImpl.execute on windows should close PipedInputStream before throwing exceptions
sla
parents:
26216
diff
changeset
|
110 |
is.close(); |
2 | 111 |
// special case the load command so that the right exception is thrown |
112 |
if (cmd.equals("load")) { |
|
113 |
throw new AgentLoadException("Failed to load agent library"); |
|
114 |
} else { |
|
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
5506
diff
changeset
|
115 |
if (message == null) { |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
5506
diff
changeset
|
116 |
throw new AttachOperationFailedException("Command failed in target VM"); |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
5506
diff
changeset
|
117 |
} else { |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
5506
diff
changeset
|
118 |
throw new AttachOperationFailedException(message); |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
5506
diff
changeset
|
119 |
} |
2 | 120 |
} |
121 |
} |
|
122 |
||
123 |
// return the input stream |
|
124 |
return is; |
|
125 |
||
126 |
} catch (IOException ioe) { |
|
127 |
closePipe(hPipe); |
|
128 |
throw ioe; |
|
129 |
} |
|
130 |
} |
|
131 |
||
132 |
// An InputStream based on a pipe to the target VM |
|
133 |
private class PipedInputStream extends InputStream { |
|
134 |
||
135 |
private long hPipe; |
|
136 |
||
137 |
public PipedInputStream(long hPipe) { |
|
138 |
this.hPipe = hPipe; |
|
139 |
} |
|
140 |
||
141 |
public synchronized int read() throws IOException { |
|
142 |
byte b[] = new byte[1]; |
|
143 |
int n = this.read(b, 0, 1); |
|
144 |
if (n == 1) { |
|
145 |
return b[0] & 0xff; |
|
146 |
} else { |
|
147 |
return -1; |
|
148 |
} |
|
149 |
} |
|
150 |
||
151 |
public synchronized int read(byte[] bs, int off, int len) throws IOException { |
|
152 |
if ((off < 0) || (off > bs.length) || (len < 0) || |
|
153 |
((off + len) > bs.length) || ((off + len) < 0)) { |
|
154 |
throw new IndexOutOfBoundsException(); |
|
155 |
} else if (len == 0) |
|
156 |
return 0; |
|
157 |
||
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
158 |
return VirtualMachineImpl.readPipe(hPipe, bs, off, len); |
2 | 159 |
} |
160 |
||
161 |
public void close() throws IOException { |
|
162 |
if (hPipe != -1) { |
|
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
163 |
VirtualMachineImpl.closePipe(hPipe); |
2 | 164 |
hPipe = -1; |
165 |
} |
|
166 |
} |
|
167 |
} |
|
168 |
||
169 |
||
170 |
//-- native methods |
|
171 |
||
172 |
static native void init(); |
|
173 |
||
174 |
static native byte[] generateStub(); |
|
175 |
||
176 |
static native long openProcess(int pid) throws IOException; |
|
177 |
||
178 |
static native void closeProcess(long hProcess) throws IOException; |
|
179 |
||
180 |
static native long createPipe(String name) throws IOException; |
|
181 |
||
182 |
static native void closePipe(long hPipe) throws IOException; |
|
183 |
||
184 |
static native void connectPipe(long hPipe) throws IOException; |
|
185 |
||
186 |
static native int readPipe(long hPipe, byte buf[], int off, int buflen) throws IOException; |
|
187 |
||
188 |
static native void enqueue(long hProcess, byte[] stub, |
|
189 |
String cmd, String pipename, Object ... args) throws IOException; |
|
190 |
||
191 |
static { |
|
192 |
System.loadLibrary("attach"); |
|
193 |
init(); // native initialization |
|
194 |
stub = generateStub(); // generate stub to copy into target process |
|
195 |
} |
|
196 |
} |