author | tschatzl |
Thu, 07 Jun 2018 11:20:18 +0200 | |
changeset 50440 | cfdd37095f66 |
parent 49610 | 6790b1077a3f |
child 53858 | e7cf035682e3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49610
6790b1077a3f
8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2005, 2018, 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 |
|
49610
6790b1077a3f
8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents:
47216
diff
changeset
|
83 |
Random rnd = new Random(); |
6790b1077a3f
8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents:
47216
diff
changeset
|
84 |
int r = rnd.nextInt(); |
6790b1077a3f
8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents:
47216
diff
changeset
|
85 |
String pipeprefix = "\\\\.\\pipe\\javatool"; |
6790b1077a3f
8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents:
47216
diff
changeset
|
86 |
String pipename = pipeprefix + r; |
6790b1077a3f
8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents:
47216
diff
changeset
|
87 |
long hPipe; |
6790b1077a3f
8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents:
47216
diff
changeset
|
88 |
try { |
6790b1077a3f
8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents:
47216
diff
changeset
|
89 |
hPipe = createPipe(pipename); |
6790b1077a3f
8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents:
47216
diff
changeset
|
90 |
} catch (IOException ce) { |
6790b1077a3f
8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents:
47216
diff
changeset
|
91 |
// Retry with another random pipe name. |
6790b1077a3f
8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents:
47216
diff
changeset
|
92 |
r = rnd.nextInt(); |
6790b1077a3f
8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents:
47216
diff
changeset
|
93 |
pipename = pipeprefix + r; |
6790b1077a3f
8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents:
47216
diff
changeset
|
94 |
hPipe = createPipe(pipename); |
6790b1077a3f
8057732: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java: Invalid PerfMemory size
gadams
parents:
47216
diff
changeset
|
95 |
} |
2 | 96 |
|
97 |
// check if we are detached - in theory it's possible that detach is invoked |
|
98 |
// after this check but before we enqueue the command. |
|
99 |
if (hProcess == -1) { |
|
100 |
closePipe(hPipe); |
|
101 |
throw new IOException("Detached from target VM"); |
|
102 |
} |
|
103 |
||
104 |
try { |
|
105 |
// enqueue the command to the process |
|
106 |
enqueue(hProcess, stub, cmd, pipename, args); |
|
107 |
||
108 |
// wait for command to complete - process will connect with the |
|
109 |
// completion status |
|
110 |
connectPipe(hPipe); |
|
111 |
||
112 |
// create an input stream for the pipe |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
26460
diff
changeset
|
113 |
PipedInputStream in = new PipedInputStream(hPipe); |
2 | 114 |
|
115 |
// read completion status |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
26460
diff
changeset
|
116 |
int status = readInt(in); |
2 | 117 |
if (status != 0) { |
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
5506
diff
changeset
|
118 |
// read from the stream and use that as the error message |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
26460
diff
changeset
|
119 |
String message = readErrorMessage(in); |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
26460
diff
changeset
|
120 |
in.close(); |
2 | 121 |
// special case the load command so that the right exception is thrown |
122 |
if (cmd.equals("load")) { |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
26460
diff
changeset
|
123 |
String msg = "Failed to load agent library"; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
26460
diff
changeset
|
124 |
if (!message.isEmpty()) |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
26460
diff
changeset
|
125 |
msg += ": " + message; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
26460
diff
changeset
|
126 |
throw new AgentLoadException(msg); |
2 | 127 |
} else { |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
26460
diff
changeset
|
128 |
if (message.isEmpty()) |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
26460
diff
changeset
|
129 |
message = "Command failed in target VM"; |
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
26460
diff
changeset
|
130 |
throw new AttachOperationFailedException(message); |
2 | 131 |
} |
132 |
} |
|
133 |
||
134 |
// return the input stream |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
26460
diff
changeset
|
135 |
return in; |
2 | 136 |
|
137 |
} catch (IOException ioe) { |
|
138 |
closePipe(hPipe); |
|
139 |
throw ioe; |
|
140 |
} |
|
141 |
} |
|
142 |
||
143 |
// An InputStream based on a pipe to the target VM |
|
144 |
private class PipedInputStream extends InputStream { |
|
145 |
||
146 |
private long hPipe; |
|
147 |
||
148 |
public PipedInputStream(long hPipe) { |
|
149 |
this.hPipe = hPipe; |
|
150 |
} |
|
151 |
||
152 |
public synchronized int read() throws IOException { |
|
153 |
byte b[] = new byte[1]; |
|
154 |
int n = this.read(b, 0, 1); |
|
155 |
if (n == 1) { |
|
156 |
return b[0] & 0xff; |
|
157 |
} else { |
|
158 |
return -1; |
|
159 |
} |
|
160 |
} |
|
161 |
||
162 |
public synchronized int read(byte[] bs, int off, int len) throws IOException { |
|
163 |
if ((off < 0) || (off > bs.length) || (len < 0) || |
|
164 |
((off + len) > bs.length) || ((off + len) < 0)) { |
|
165 |
throw new IndexOutOfBoundsException(); |
|
166 |
} else if (len == 0) |
|
167 |
return 0; |
|
168 |
||
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
169 |
return VirtualMachineImpl.readPipe(hPipe, bs, off, len); |
2 | 170 |
} |
171 |
||
172 |
public void close() throws IOException { |
|
173 |
if (hPipe != -1) { |
|
26216
5e46c782b43c
8055230: Rename attach provider implementation class be platform neutral
mchung
parents:
25859
diff
changeset
|
174 |
VirtualMachineImpl.closePipe(hPipe); |
2 | 175 |
hPipe = -1; |
176 |
} |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
||
181 |
//-- native methods |
|
182 |
||
183 |
static native void init(); |
|
184 |
||
185 |
static native byte[] generateStub(); |
|
186 |
||
187 |
static native long openProcess(int pid) throws IOException; |
|
188 |
||
189 |
static native void closeProcess(long hProcess) throws IOException; |
|
190 |
||
191 |
static native long createPipe(String name) throws IOException; |
|
192 |
||
193 |
static native void closePipe(long hPipe) throws IOException; |
|
194 |
||
195 |
static native void connectPipe(long hPipe) throws IOException; |
|
196 |
||
197 |
static native int readPipe(long hPipe, byte buf[], int off, int buflen) throws IOException; |
|
198 |
||
199 |
static native void enqueue(long hProcess, byte[] stub, |
|
200 |
String cmd, String pipename, Object ... args) throws IOException; |
|
201 |
||
202 |
static { |
|
203 |
System.loadLibrary("attach"); |
|
204 |
init(); // native initialization |
|
205 |
stub = generateStub(); // generate stub to copy into target process |
|
206 |
} |
|
207 |
} |