author | alanb |
Thu, 17 Mar 2016 19:04:28 +0000 | |
changeset 36526 | 3b41f1c69604 |
parent 36499 | 9d823cc0fe98 |
child 37644 | 33cf53901cac |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
2 |
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle 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 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. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.jshell; |
|
27 |
||
28 |
import static jdk.internal.jshell.remote.RemoteCodes.*; |
|
35002 | 29 |
import java.io.DataInputStream; |
30 |
import java.io.InputStream; |
|
33362 | 31 |
import java.io.IOException; |
32 |
import java.io.ObjectInputStream; |
|
33 |
import java.io.ObjectOutputStream; |
|
35002 | 34 |
import java.io.PrintStream; |
33362 | 35 |
import java.net.ServerSocket; |
36 |
import java.net.Socket; |
|
37 |
import com.sun.jdi.*; |
|
38 |
import java.io.EOFException; |
|
34755
135cde5b66cf
8143952: JShell: space in class path causes remote launch failure
jlahoda
parents:
33362
diff
changeset
|
39 |
import java.util.HashMap; |
33362 | 40 |
import java.util.List; |
41 |
import java.util.Map; |
|
42 |
import jdk.jshell.ClassTracker.ClassInfo; |
|
43 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN; |
|
44 |
||
45 |
/** |
|
46 |
* Controls the remote execution environment. |
|
47 |
* |
|
48 |
* @author Robert Field |
|
49 |
*/ |
|
50 |
class ExecutionControl { |
|
51 |
||
52 |
private final JDIEnv env; |
|
53 |
private final SnippetMaps maps; |
|
54 |
private JDIEventHandler handler; |
|
55 |
private Socket socket; |
|
56 |
private ObjectInputStream in; |
|
57 |
private ObjectOutputStream out; |
|
58 |
private final JShell proc; |
|
59 |
||
60 |
ExecutionControl(JDIEnv env, SnippetMaps maps, JShell proc) { |
|
61 |
this.env = env; |
|
62 |
this.maps = maps; |
|
63 |
this.proc = proc; |
|
64 |
} |
|
65 |
||
66 |
void launch() throws IOException { |
|
67 |
try (ServerSocket listener = new ServerSocket(0)) { |
|
68 |
// timeout after 60 seconds |
|
69 |
listener.setSoTimeout(60000); |
|
70 |
int port = listener.getLocalPort(); |
|
71 |
jdiGo(port); |
|
72 |
socket = listener.accept(); |
|
73 |
// out before in -- match remote creation so we don't hang |
|
74 |
out = new ObjectOutputStream(socket.getOutputStream()); |
|
35002 | 75 |
PipeInputStream commandIn = new PipeInputStream(); |
76 |
new DemultiplexInput(socket.getInputStream(), commandIn, proc.out, proc.err).start(); |
|
77 |
in = new ObjectInputStream(commandIn); |
|
33362 | 78 |
} |
79 |
} |
|
80 |
||
81 |
void commandExit() { |
|
82 |
try { |
|
83 |
if (out != null) { |
|
84 |
out.writeInt(CMD_EXIT); |
|
85 |
out.flush(); |
|
86 |
} |
|
87 |
JDIConnection c = env.connection(); |
|
88 |
if (c != null) { |
|
89 |
c.disposeVM(); |
|
90 |
} |
|
91 |
} catch (IOException ex) { |
|
92 |
proc.debug(DBG_GEN, "Exception on JDI exit: %s\n", ex); |
|
93 |
} |
|
94 |
} |
|
95 |
||
96 |
||
97 |
boolean commandLoad(List<ClassInfo> cil) { |
|
98 |
try { |
|
99 |
out.writeInt(CMD_LOAD); |
|
100 |
out.writeInt(cil.size()); |
|
101 |
for (ClassInfo ci : cil) { |
|
102 |
out.writeUTF(ci.getClassName()); |
|
103 |
out.writeObject(ci.getBytes()); |
|
104 |
} |
|
105 |
out.flush(); |
|
106 |
return readAndReportResult(); |
|
107 |
} catch (IOException ex) { |
|
108 |
proc.debug(DBG_GEN, "IOException on remote load operation: %s\n", ex); |
|
109 |
return false; |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
String commandInvoke(String classname) throws EvalException, UnresolvedReferenceException { |
|
114 |
try { |
|
115 |
synchronized (STOP_LOCK) { |
|
116 |
userCodeRunning = true; |
|
117 |
} |
|
118 |
out.writeInt(CMD_INVOKE); |
|
119 |
out.writeUTF(classname); |
|
120 |
out.flush(); |
|
121 |
if (readAndReportExecutionResult()) { |
|
122 |
String result = in.readUTF(); |
|
123 |
return result; |
|
124 |
} |
|
125 |
} catch (IOException | ClassNotFoundException ex) { |
|
35002 | 126 |
if (!env.connection().isRunning()) { |
127 |
env.shutdown(); |
|
128 |
} else { |
|
129 |
proc.debug(DBG_GEN, "Exception on remote invoke: %s\n", ex); |
|
130 |
return "Execution failure: " + ex.getMessage(); |
|
131 |
} |
|
33362 | 132 |
} finally { |
133 |
synchronized (STOP_LOCK) { |
|
134 |
userCodeRunning = false; |
|
135 |
} |
|
136 |
} |
|
137 |
return ""; |
|
138 |
} |
|
139 |
||
140 |
String commandVarValue(String classname, String varname) { |
|
141 |
try { |
|
142 |
out.writeInt(CMD_VARVALUE); |
|
143 |
out.writeUTF(classname); |
|
144 |
out.writeUTF(varname); |
|
145 |
out.flush(); |
|
146 |
if (readAndReportResult()) { |
|
147 |
String result = in.readUTF(); |
|
148 |
return result; |
|
149 |
} |
|
150 |
} catch (EOFException ex) { |
|
151 |
env.shutdown(); |
|
152 |
} catch (IOException ex) { |
|
153 |
proc.debug(DBG_GEN, "Exception on remote var value: %s\n", ex); |
|
154 |
return "Execution failure: " + ex.getMessage(); |
|
155 |
} |
|
156 |
return ""; |
|
157 |
} |
|
158 |
||
159 |
boolean commandAddToClasspath(String cp) { |
|
160 |
try { |
|
161 |
out.writeInt(CMD_CLASSPATH); |
|
162 |
out.writeUTF(cp); |
|
163 |
out.flush(); |
|
164 |
return readAndReportResult(); |
|
165 |
} catch (IOException ex) { |
|
166 |
throw new InternalError("Classpath addition failed: " + cp, ex); |
|
167 |
} |
|
168 |
} |
|
169 |
||
170 |
boolean commandRedefine(Map<ReferenceType, byte[]> mp) { |
|
171 |
try { |
|
172 |
env.vm().redefineClasses(mp); |
|
173 |
return true; |
|
174 |
} catch (UnsupportedOperationException ex) { |
|
175 |
return false; |
|
176 |
} catch (Exception ex) { |
|
177 |
proc.debug(DBG_GEN, "Exception on JDI redefine: %s\n", ex); |
|
178 |
return false; |
|
179 |
} |
|
180 |
} |
|
181 |
||
182 |
ReferenceType nameToRef(String name) { |
|
183 |
List<ReferenceType> rtl = env.vm().classesByName(name); |
|
184 |
if (rtl.size() != 1) { |
|
185 |
return null; |
|
186 |
} |
|
187 |
return rtl.get(0); |
|
188 |
} |
|
189 |
||
190 |
private boolean readAndReportResult() throws IOException { |
|
191 |
int ok = in.readInt(); |
|
192 |
switch (ok) { |
|
193 |
case RESULT_SUCCESS: |
|
194 |
return true; |
|
195 |
case RESULT_FAIL: { |
|
196 |
String ex = in.readUTF(); |
|
197 |
proc.debug(DBG_GEN, "Exception on remote operation: %s\n", ex); |
|
198 |
return false; |
|
199 |
} |
|
200 |
default: { |
|
201 |
proc.debug(DBG_GEN, "Bad remote result code: %s\n", ok); |
|
202 |
return false; |
|
203 |
} |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
private boolean readAndReportExecutionResult() throws IOException, ClassNotFoundException, EvalException, UnresolvedReferenceException { |
|
208 |
int ok = in.readInt(); |
|
209 |
switch (ok) { |
|
210 |
case RESULT_SUCCESS: |
|
211 |
return true; |
|
212 |
case RESULT_FAIL: { |
|
213 |
String ex = in.readUTF(); |
|
214 |
proc.debug(DBG_GEN, "Exception on remote operation: %s\n", ex); |
|
215 |
return false; |
|
216 |
} |
|
217 |
case RESULT_EXCEPTION: { |
|
218 |
String exceptionClassName = in.readUTF(); |
|
219 |
String message = in.readUTF(); |
|
220 |
StackTraceElement[] elems = readStackTrace(); |
|
221 |
EvalException ee = new EvalException(message, exceptionClassName, elems); |
|
222 |
throw ee; |
|
223 |
} |
|
224 |
case RESULT_CORRALLED: { |
|
225 |
int id = in.readInt(); |
|
226 |
StackTraceElement[] elems = readStackTrace(); |
|
227 |
Snippet si = maps.getSnippet(id); |
|
36499 | 228 |
throw new UnresolvedReferenceException((DeclarationSnippet) si, elems); |
33362 | 229 |
} |
230 |
case RESULT_KILLED: { |
|
231 |
proc.out.println("Killed."); |
|
232 |
return false; |
|
233 |
} |
|
234 |
default: { |
|
235 |
proc.debug(DBG_GEN, "Bad remote result code: %s\n", ok); |
|
236 |
return false; |
|
237 |
} |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
private StackTraceElement[] readStackTrace() throws IOException { |
|
242 |
int elemCount = in.readInt(); |
|
243 |
StackTraceElement[] elems = new StackTraceElement[elemCount]; |
|
244 |
for (int i = 0; i < elemCount; ++i) { |
|
245 |
String className = in.readUTF(); |
|
246 |
String methodName = in.readUTF(); |
|
247 |
String fileName = in.readUTF(); |
|
248 |
int line = in.readInt(); |
|
249 |
elems[i] = new StackTraceElement(className, methodName, fileName, line); |
|
250 |
} |
|
251 |
return elems; |
|
252 |
} |
|
253 |
||
254 |
private void jdiGo(int port) { |
|
255 |
//MessageOutput.textResources = ResourceBundle.getBundle("impl.TTYResources", |
|
256 |
// Locale.getDefault()); |
|
257 |
||
34755
135cde5b66cf
8143952: JShell: space in class path causes remote launch failure
jlahoda
parents:
33362
diff
changeset
|
258 |
String connectorName = "com.sun.jdi.CommandLineLaunch"; |
33362 | 259 |
String classPath = System.getProperty("java.class.path"); |
36526 | 260 |
String javaArgs = "-classpath " + classPath; |
34755
135cde5b66cf
8143952: JShell: space in class path causes remote launch failure
jlahoda
parents:
33362
diff
changeset
|
261 |
Map<String, String> argumentName2Value = new HashMap<>(); |
135cde5b66cf
8143952: JShell: space in class path causes remote launch failure
jlahoda
parents:
33362
diff
changeset
|
262 |
argumentName2Value.put("main", "jdk.internal.jshell.remote.RemoteAgent " + port); |
135cde5b66cf
8143952: JShell: space in class path causes remote launch failure
jlahoda
parents:
33362
diff
changeset
|
263 |
argumentName2Value.put("options", javaArgs); |
33362 | 264 |
|
265 |
boolean launchImmediately = true; |
|
266 |
int traceFlags = 0;// VirtualMachine.TRACE_SENDS | VirtualMachine.TRACE_EVENTS; |
|
267 |
||
34755
135cde5b66cf
8143952: JShell: space in class path causes remote launch failure
jlahoda
parents:
33362
diff
changeset
|
268 |
env.init(connectorName, argumentName2Value, launchImmediately, traceFlags); |
33362 | 269 |
|
270 |
if (env.connection().isOpen() && env.vm().canBeModified()) { |
|
271 |
/* |
|
272 |
* Connection opened on startup. Start event handler |
|
273 |
* immediately, telling it (through arg 2) to stop on the |
|
274 |
* VM start event. |
|
275 |
*/ |
|
276 |
handler = new JDIEventHandler(env); |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
private final Object STOP_LOCK = new Object(); |
|
281 |
private boolean userCodeRunning = false; |
|
282 |
||
283 |
void commandStop() { |
|
284 |
synchronized (STOP_LOCK) { |
|
285 |
if (!userCodeRunning) |
|
286 |
return ; |
|
287 |
||
288 |
VirtualMachine vm = handler.env.vm(); |
|
289 |
vm.suspend(); |
|
290 |
try { |
|
291 |
OUTER: for (ThreadReference thread : vm.allThreads()) { |
|
292 |
// could also tag the thread (e.g. using name), to find it easier |
|
293 |
for (StackFrame frame : thread.frames()) { |
|
294 |
String remoteAgentName = "jdk.internal.jshell.remote.RemoteAgent"; |
|
295 |
if (remoteAgentName.equals(frame.location().declaringType().name()) && |
|
296 |
"commandLoop".equals(frame.location().method().name())) { |
|
297 |
ObjectReference thiz = frame.thisObject(); |
|
298 |
if (((BooleanValue) thiz.getValue(thiz.referenceType().fieldByName("inClientCode"))).value()) { |
|
299 |
thiz.setValue(thiz.referenceType().fieldByName("expectingStop"), vm.mirrorOf(true)); |
|
300 |
ObjectReference stopInstance = (ObjectReference) thiz.getValue(thiz.referenceType().fieldByName("stopException")); |
|
301 |
||
302 |
vm.resume(); |
|
303 |
proc.debug(DBG_GEN, "Attempting to stop the client code...\n"); |
|
304 |
thread.stop(stopInstance); |
|
305 |
thiz.setValue(thiz.referenceType().fieldByName("expectingStop"), vm.mirrorOf(false)); |
|
306 |
} |
|
307 |
||
308 |
break OUTER; |
|
309 |
} |
|
310 |
} |
|
311 |
} |
|
312 |
} catch (ClassNotLoadedException | IncompatibleThreadStateException | InvalidTypeException ex) { |
|
313 |
proc.debug(DBG_GEN, "Exception on remote stop: %s\n", ex); |
|
314 |
} finally { |
|
315 |
vm.resume(); |
|
316 |
} |
|
317 |
} |
|
318 |
} |
|
35002 | 319 |
|
320 |
private final class DemultiplexInput extends Thread { |
|
321 |
||
322 |
private final DataInputStream delegate; |
|
323 |
private final PipeInputStream command; |
|
324 |
private final PrintStream out; |
|
325 |
private final PrintStream err; |
|
326 |
||
327 |
public DemultiplexInput(InputStream input, |
|
328 |
PipeInputStream command, |
|
329 |
PrintStream out, |
|
330 |
PrintStream err) { |
|
331 |
super("output reader"); |
|
332 |
this.delegate = new DataInputStream(input); |
|
333 |
this.command = command; |
|
334 |
this.out = out; |
|
335 |
this.err = err; |
|
336 |
} |
|
337 |
||
338 |
public void run() { |
|
339 |
try { |
|
340 |
while (true) { |
|
341 |
int nameLen = delegate.read(); |
|
342 |
if (nameLen == (-1)) |
|
343 |
break; |
|
344 |
byte[] name = new byte[nameLen]; |
|
345 |
DemultiplexInput.this.delegate.readFully(name); |
|
346 |
int dataLen = delegate.read(); |
|
347 |
byte[] data = new byte[dataLen]; |
|
348 |
DemultiplexInput.this.delegate.readFully(data); |
|
349 |
switch (new String(name, "UTF-8")) { |
|
350 |
case "err": |
|
351 |
err.write(data); |
|
352 |
break; |
|
353 |
case "out": |
|
354 |
out.write(data); |
|
355 |
break; |
|
356 |
case "command": |
|
357 |
for (byte b : data) { |
|
358 |
command.write(Byte.toUnsignedInt(b)); |
|
359 |
} |
|
360 |
break; |
|
361 |
} |
|
362 |
} |
|
363 |
} catch (IOException ex) { |
|
364 |
proc.debug(ex, "Failed reading output"); |
|
365 |
} finally { |
|
366 |
command.close(); |
|
367 |
} |
|
368 |
} |
|
369 |
||
370 |
} |
|
371 |
||
372 |
public static final class PipeInputStream extends InputStream { |
|
373 |
public static final int INITIAL_SIZE = 128; |
|
374 |
||
375 |
private int[] buffer = new int[INITIAL_SIZE]; |
|
376 |
private int start; |
|
377 |
private int end; |
|
378 |
private boolean closed; |
|
379 |
||
380 |
@Override |
|
381 |
public synchronized int read() { |
|
382 |
while (start == end) { |
|
383 |
if (closed) { |
|
384 |
return -1; |
|
385 |
} |
|
386 |
try { |
|
387 |
wait(); |
|
388 |
} catch (InterruptedException ex) { |
|
389 |
//ignore |
|
390 |
} |
|
391 |
} |
|
392 |
try { |
|
393 |
return buffer[start]; |
|
394 |
} finally { |
|
395 |
start = (start + 1) % buffer.length; |
|
396 |
} |
|
397 |
} |
|
398 |
||
399 |
public synchronized void write(int b) { |
|
400 |
if (closed) |
|
401 |
throw new IllegalStateException("Already closed."); |
|
402 |
int newEnd = (end + 1) % buffer.length; |
|
403 |
if (newEnd == start) { |
|
404 |
//overflow: |
|
405 |
int[] newBuffer = new int[buffer.length * 2]; |
|
406 |
int rightPart = (end > start ? end : buffer.length) - start; |
|
407 |
int leftPart = end > start ? 0 : start - 1; |
|
408 |
System.arraycopy(buffer, start, newBuffer, 0, rightPart); |
|
409 |
System.arraycopy(buffer, 0, newBuffer, rightPart, leftPart); |
|
410 |
buffer = newBuffer; |
|
411 |
start = 0; |
|
412 |
end = rightPart + leftPart; |
|
413 |
newEnd = end + 1; |
|
414 |
} |
|
415 |
buffer[end] = b; |
|
416 |
end = newEnd; |
|
417 |
notifyAll(); |
|
418 |
} |
|
419 |
||
420 |
@Override |
|
421 |
public synchronized void close() { |
|
422 |
closed = true; |
|
423 |
notifyAll(); |
|
424 |
} |
|
425 |
||
426 |
} |
|
33362 | 427 |
} |