2
|
1 |
/*
|
715
|
2 |
* Copyright 1998-2008 Sun Microsystems, Inc. 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
|
|
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 |
|
|
26 |
package com.sun.tools.example.debug.tty;
|
|
27 |
|
|
28 |
import com.sun.jdi.*;
|
|
29 |
import com.sun.jdi.connect.*;
|
|
30 |
import com.sun.jdi.request.EventRequestManager;
|
|
31 |
import com.sun.jdi.request.ExceptionRequest;
|
|
32 |
import com.sun.jdi.request.ThreadStartRequest;
|
|
33 |
import com.sun.jdi.request.ThreadDeathRequest;
|
|
34 |
|
|
35 |
import java.util.*;
|
|
36 |
import java.util.regex.*;
|
|
37 |
import java.io.*;
|
|
38 |
|
|
39 |
class VMConnection {
|
|
40 |
|
|
41 |
private VirtualMachine vm;
|
|
42 |
private Process process = null;
|
|
43 |
private int outputCompleteCount = 0;
|
|
44 |
|
|
45 |
private final Connector connector;
|
|
46 |
private final Map<String, com.sun.jdi.connect.Connector.Argument> connectorArgs;
|
|
47 |
private final int traceFlags;
|
|
48 |
|
|
49 |
synchronized void notifyOutputComplete() {
|
|
50 |
outputCompleteCount++;
|
|
51 |
notifyAll();
|
|
52 |
}
|
|
53 |
|
|
54 |
synchronized void waitOutputComplete() {
|
|
55 |
// Wait for stderr and stdout
|
|
56 |
if (process != null) {
|
|
57 |
while (outputCompleteCount < 2) {
|
|
58 |
try {wait();} catch (InterruptedException e) {}
|
|
59 |
}
|
|
60 |
}
|
|
61 |
}
|
|
62 |
|
|
63 |
private Connector findConnector(String name) {
|
51
|
64 |
for (Connector connector :
|
|
65 |
Bootstrap.virtualMachineManager().allConnectors()) {
|
2
|
66 |
if (connector.name().equals(name)) {
|
|
67 |
return connector;
|
|
68 |
}
|
|
69 |
}
|
|
70 |
return null;
|
|
71 |
}
|
|
72 |
|
|
73 |
private Map <String, com.sun.jdi.connect.Connector.Argument> parseConnectorArgs(Connector connector, String argString) {
|
|
74 |
Map<String, com.sun.jdi.connect.Connector.Argument> arguments = connector.defaultArguments();
|
|
75 |
|
|
76 |
/*
|
|
77 |
* We are parsing strings of the form:
|
|
78 |
* name1=value1,[name2=value2,...]
|
|
79 |
* However, the value1...valuen substrings may contain
|
|
80 |
* embedded comma(s), so make provision for quoting inside
|
|
81 |
* the value substrings. (Bug ID 4285874)
|
|
82 |
*/
|
|
83 |
String regexPattern =
|
|
84 |
"(quote=[^,]+,)|" + // special case for quote=.,
|
|
85 |
"(\\w+=)" + // name=
|
|
86 |
"(((\"[^\"]*\")|" + // ( "l , ue"
|
|
87 |
"('[^']*')|" + // 'l , ue'
|
|
88 |
"([^,'\"]+))+,)"; // v a l u e )+ ,
|
|
89 |
Pattern p = Pattern.compile(regexPattern);
|
|
90 |
Matcher m = p.matcher(argString);
|
|
91 |
while (m.find()) {
|
|
92 |
int startPosition = m.start();
|
|
93 |
int endPosition = m.end();
|
|
94 |
if (startPosition > 0) {
|
|
95 |
/*
|
|
96 |
* It is an error if parsing skips over any part of argString.
|
|
97 |
*/
|
|
98 |
throw new IllegalArgumentException
|
|
99 |
(MessageOutput.format("Illegal connector argument",
|
|
100 |
argString));
|
|
101 |
}
|
|
102 |
|
|
103 |
String token = argString.substring(startPosition, endPosition);
|
|
104 |
int index = token.indexOf('=');
|
|
105 |
String name = token.substring(0, index);
|
|
106 |
String value = token.substring(index + 1,
|
|
107 |
token.length() - 1); // Remove comma delimiter
|
|
108 |
|
51
|
109 |
Connector.Argument argument = arguments.get(name);
|
2
|
110 |
if (argument == null) {
|
|
111 |
throw new IllegalArgumentException
|
|
112 |
(MessageOutput.format("Argument is not defined for connector:",
|
|
113 |
new Object [] {name, connector.name()}));
|
|
114 |
}
|
|
115 |
argument.setValue(value);
|
|
116 |
|
|
117 |
argString = argString.substring(endPosition); // Remove what was just parsed...
|
|
118 |
m = p.matcher(argString); // and parse again on what is left.
|
|
119 |
}
|
|
120 |
if ((! argString.equals(",")) && (argString.length() > 0)) {
|
|
121 |
/*
|
|
122 |
* It is an error if any part of argString is left over,
|
|
123 |
* unless it was empty to begin with.
|
|
124 |
*/
|
|
125 |
throw new IllegalArgumentException
|
|
126 |
(MessageOutput.format("Illegal connector argument", argString));
|
|
127 |
}
|
|
128 |
return arguments;
|
|
129 |
}
|
|
130 |
|
|
131 |
VMConnection(String connectSpec, int traceFlags) {
|
|
132 |
String nameString;
|
|
133 |
String argString;
|
|
134 |
int index = connectSpec.indexOf(':');
|
|
135 |
if (index == -1) {
|
|
136 |
nameString = connectSpec;
|
|
137 |
argString = "";
|
|
138 |
} else {
|
|
139 |
nameString = connectSpec.substring(0, index);
|
|
140 |
argString = connectSpec.substring(index + 1);
|
|
141 |
}
|
|
142 |
|
|
143 |
connector = findConnector(nameString);
|
|
144 |
if (connector == null) {
|
|
145 |
throw new IllegalArgumentException
|
|
146 |
(MessageOutput.format("No connector named:", nameString));
|
|
147 |
}
|
|
148 |
|
|
149 |
connectorArgs = parseConnectorArgs(connector, argString);
|
|
150 |
this.traceFlags = traceFlags;
|
|
151 |
}
|
|
152 |
|
|
153 |
synchronized VirtualMachine open() {
|
|
154 |
if (connector instanceof LaunchingConnector) {
|
|
155 |
vm = launchTarget();
|
|
156 |
} else if (connector instanceof AttachingConnector) {
|
|
157 |
vm = attachTarget();
|
|
158 |
} else if (connector instanceof ListeningConnector) {
|
|
159 |
vm = listenTarget();
|
|
160 |
} else {
|
|
161 |
throw new InternalError
|
|
162 |
(MessageOutput.format("Invalid connect type"));
|
|
163 |
}
|
|
164 |
vm.setDebugTraceMode(traceFlags);
|
|
165 |
if (vm.canBeModified()){
|
|
166 |
setEventRequests(vm);
|
|
167 |
resolveEventRequests();
|
|
168 |
}
|
|
169 |
/*
|
|
170 |
* Now that the vm connection is open, fetch the debugee
|
|
171 |
* classpath and set up a default sourcepath.
|
|
172 |
* (Unless user supplied a sourcepath on the command line)
|
|
173 |
* (Bug ID 4186582)
|
|
174 |
*/
|
|
175 |
if (Env.getSourcePath().length() == 0) {
|
|
176 |
if (vm instanceof PathSearchingVirtualMachine) {
|
|
177 |
PathSearchingVirtualMachine psvm =
|
|
178 |
(PathSearchingVirtualMachine) vm;
|
|
179 |
Env.setSourcePath(psvm.classPath());
|
|
180 |
} else {
|
|
181 |
Env.setSourcePath(".");
|
|
182 |
}
|
|
183 |
}
|
|
184 |
|
|
185 |
return vm;
|
|
186 |
}
|
|
187 |
|
|
188 |
boolean setConnectorArg(String name, String value) {
|
|
189 |
/*
|
|
190 |
* Too late if the connection already made
|
|
191 |
*/
|
|
192 |
if (vm != null) {
|
|
193 |
return false;
|
|
194 |
}
|
|
195 |
|
51
|
196 |
Connector.Argument argument = connectorArgs.get(name);
|
2
|
197 |
if (argument == null) {
|
|
198 |
return false;
|
|
199 |
}
|
|
200 |
argument.setValue(value);
|
|
201 |
return true;
|
|
202 |
}
|
|
203 |
|
|
204 |
String connectorArg(String name) {
|
51
|
205 |
Connector.Argument argument = connectorArgs.get(name);
|
2
|
206 |
if (argument == null) {
|
|
207 |
return "";
|
|
208 |
}
|
|
209 |
return argument.value();
|
|
210 |
}
|
|
211 |
|
|
212 |
public synchronized VirtualMachine vm() {
|
|
213 |
if (vm == null) {
|
|
214 |
throw new VMNotConnectedException();
|
|
215 |
} else {
|
|
216 |
return vm;
|
|
217 |
}
|
|
218 |
}
|
|
219 |
|
|
220 |
boolean isOpen() {
|
|
221 |
return (vm != null);
|
|
222 |
}
|
|
223 |
|
|
224 |
boolean isLaunch() {
|
|
225 |
return (connector instanceof LaunchingConnector);
|
|
226 |
}
|
|
227 |
|
|
228 |
public void disposeVM() {
|
|
229 |
try {
|
|
230 |
if (vm != null) {
|
|
231 |
vm.dispose();
|
|
232 |
vm = null;
|
|
233 |
}
|
|
234 |
} finally {
|
|
235 |
if (process != null) {
|
|
236 |
process.destroy();
|
|
237 |
process = null;
|
|
238 |
}
|
|
239 |
waitOutputComplete();
|
|
240 |
}
|
|
241 |
}
|
|
242 |
|
|
243 |
private void setEventRequests(VirtualMachine vm) {
|
|
244 |
EventRequestManager erm = vm.eventRequestManager();
|
|
245 |
|
|
246 |
// Normally, we want all uncaught exceptions. We request them
|
|
247 |
// via the same mechanism as Commands.commandCatchException()
|
|
248 |
// so the user can ignore them later if they are not
|
|
249 |
// interested.
|
|
250 |
// FIXME: this works but generates spurious messages on stdout
|
|
251 |
// during startup:
|
|
252 |
// Set uncaught java.lang.Throwable
|
|
253 |
// Set deferred uncaught java.lang.Throwable
|
|
254 |
Commands evaluator = new Commands();
|
|
255 |
evaluator.commandCatchException
|
|
256 |
(new StringTokenizer("uncaught java.lang.Throwable"));
|
|
257 |
|
|
258 |
ThreadStartRequest tsr = erm.createThreadStartRequest();
|
|
259 |
tsr.enable();
|
|
260 |
ThreadDeathRequest tdr = erm.createThreadDeathRequest();
|
|
261 |
tdr.enable();
|
|
262 |
}
|
|
263 |
|
|
264 |
private void resolveEventRequests() {
|
|
265 |
Env.specList.resolveAll();
|
|
266 |
}
|
|
267 |
|
|
268 |
private void dumpStream(InputStream stream) throws IOException {
|
|
269 |
BufferedReader in =
|
|
270 |
new BufferedReader(new InputStreamReader(stream));
|
|
271 |
int i;
|
|
272 |
try {
|
|
273 |
while ((i = in.read()) != -1) {
|
|
274 |
MessageOutput.printDirect((char)i);// Special case: use
|
|
275 |
// printDirect()
|
|
276 |
}
|
|
277 |
} catch (IOException ex) {
|
|
278 |
String s = ex.getMessage();
|
|
279 |
if (!s.startsWith("Bad file number")) {
|
|
280 |
throw ex;
|
|
281 |
}
|
|
282 |
// else we got a Bad file number IOException which just means
|
|
283 |
// that the debuggee has gone away. We'll just treat it the
|
|
284 |
// same as if we got an EOF.
|
|
285 |
}
|
|
286 |
}
|
|
287 |
|
|
288 |
/**
|
|
289 |
* Create a Thread that will retrieve and display any output.
|
|
290 |
* Needs to be high priority, else debugger may exit before
|
|
291 |
* it can be displayed.
|
|
292 |
*/
|
|
293 |
private void displayRemoteOutput(final InputStream stream) {
|
|
294 |
Thread thr = new Thread("output reader") {
|
|
295 |
public void run() {
|
|
296 |
try {
|
|
297 |
dumpStream(stream);
|
|
298 |
} catch (IOException ex) {
|
|
299 |
MessageOutput.fatalError("Failed reading output");
|
|
300 |
} finally {
|
|
301 |
notifyOutputComplete();
|
|
302 |
}
|
|
303 |
}
|
|
304 |
};
|
|
305 |
thr.setPriority(Thread.MAX_PRIORITY-1);
|
|
306 |
thr.start();
|
|
307 |
}
|
|
308 |
|
|
309 |
private void dumpFailedLaunchInfo(Process process) {
|
|
310 |
try {
|
|
311 |
dumpStream(process.getErrorStream());
|
|
312 |
dumpStream(process.getInputStream());
|
|
313 |
} catch (IOException e) {
|
|
314 |
MessageOutput.println("Unable to display process output:",
|
|
315 |
e.getMessage());
|
|
316 |
}
|
|
317 |
}
|
|
318 |
|
|
319 |
/* launch child target vm */
|
|
320 |
private VirtualMachine launchTarget() {
|
|
321 |
LaunchingConnector launcher = (LaunchingConnector)connector;
|
|
322 |
try {
|
|
323 |
VirtualMachine vm = launcher.launch(connectorArgs);
|
|
324 |
process = vm.process();
|
|
325 |
displayRemoteOutput(process.getErrorStream());
|
|
326 |
displayRemoteOutput(process.getInputStream());
|
|
327 |
return vm;
|
|
328 |
} catch (IOException ioe) {
|
|
329 |
ioe.printStackTrace();
|
|
330 |
MessageOutput.fatalError("Unable to launch target VM.");
|
|
331 |
} catch (IllegalConnectorArgumentsException icae) {
|
|
332 |
icae.printStackTrace();
|
|
333 |
MessageOutput.fatalError("Internal debugger error.");
|
|
334 |
} catch (VMStartException vmse) {
|
|
335 |
MessageOutput.println("vmstartexception", vmse.getMessage());
|
|
336 |
MessageOutput.println();
|
|
337 |
dumpFailedLaunchInfo(vmse.process());
|
|
338 |
MessageOutput.fatalError("Target VM failed to initialize.");
|
|
339 |
}
|
|
340 |
return null; // Shuts up the compiler
|
|
341 |
}
|
|
342 |
|
|
343 |
/* attach to running target vm */
|
|
344 |
private VirtualMachine attachTarget() {
|
|
345 |
AttachingConnector attacher = (AttachingConnector)connector;
|
|
346 |
try {
|
|
347 |
return attacher.attach(connectorArgs);
|
|
348 |
} catch (IOException ioe) {
|
|
349 |
ioe.printStackTrace();
|
|
350 |
MessageOutput.fatalError("Unable to attach to target VM.");
|
|
351 |
} catch (IllegalConnectorArgumentsException icae) {
|
|
352 |
icae.printStackTrace();
|
|
353 |
MessageOutput.fatalError("Internal debugger error.");
|
|
354 |
}
|
|
355 |
return null; // Shuts up the compiler
|
|
356 |
}
|
|
357 |
|
|
358 |
/* listen for connection from target vm */
|
|
359 |
private VirtualMachine listenTarget() {
|
|
360 |
ListeningConnector listener = (ListeningConnector)connector;
|
|
361 |
try {
|
|
362 |
String retAddress = listener.startListening(connectorArgs);
|
|
363 |
MessageOutput.println("Listening at address:", retAddress);
|
|
364 |
vm = listener.accept(connectorArgs);
|
|
365 |
listener.stopListening(connectorArgs);
|
|
366 |
return vm;
|
|
367 |
} catch (IOException ioe) {
|
|
368 |
ioe.printStackTrace();
|
|
369 |
MessageOutput.fatalError("Unable to attach to target VM.");
|
|
370 |
} catch (IllegalConnectorArgumentsException icae) {
|
|
371 |
icae.printStackTrace();
|
|
372 |
MessageOutput.fatalError("Internal debugger error.");
|
|
373 |
}
|
|
374 |
return null; // Shuts up the compiler
|
|
375 |
}
|
|
376 |
}
|