annotate agent/test/jdi/VMConnection.java @ 3237:399aa66d375e

Fixed a bug in which the valueEquals method was misused. The method does only check the equality of the node data and not full GVN equality by taking inputs and successors into account.
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Wed, 27 Jul 2011 14:16:38 -0700
parents c18cbe5936b8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
2 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 import com.sun.jdi.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
26 import com.sun.jdi.connect.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import com.sun.jdi.request.EventRequestManager;
a61af66fc99e Initial load
duke
parents:
diff changeset
28
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import java.util.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import java.io.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
34 * Manages a VM conection for the JDI test framework.
a61af66fc99e Initial load
duke
parents:
diff changeset
35 */
a61af66fc99e Initial load
duke
parents:
diff changeset
36 class VMConnection {
a61af66fc99e Initial load
duke
parents:
diff changeset
37 private VirtualMachine vm;
a61af66fc99e Initial load
duke
parents:
diff changeset
38 private Process process = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
39 private int outputCompleteCount = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
40
a61af66fc99e Initial load
duke
parents:
diff changeset
41 private final Connector connector;
a61af66fc99e Initial load
duke
parents:
diff changeset
42 private final Map connectorArgs;
a61af66fc99e Initial load
duke
parents:
diff changeset
43 private final int traceFlags;
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
46 * Return a String containing VM Options to pass to the debugee
a61af66fc99e Initial load
duke
parents:
diff changeset
47 * or an empty string if there are none.
a61af66fc99e Initial load
duke
parents:
diff changeset
48 * These are read from the first non-comment line
a61af66fc99e Initial load
duke
parents:
diff changeset
49 * in file test/com/sun/jdi/@debuggeeVMOptions.
a61af66fc99e Initial load
duke
parents:
diff changeset
50 */
a61af66fc99e Initial load
duke
parents:
diff changeset
51 static public String getDebuggeeVMOptions() {
a61af66fc99e Initial load
duke
parents:
diff changeset
52
a61af66fc99e Initial load
duke
parents:
diff changeset
53 // When we run under jtreg, test.src contains the pathname of
a61af66fc99e Initial load
duke
parents:
diff changeset
54 // the test/com/sun/jdi dir.
a61af66fc99e Initial load
duke
parents:
diff changeset
55 BufferedReader reader;
a61af66fc99e Initial load
duke
parents:
diff changeset
56 final String filename = "@debuggeeVMOptions";
a61af66fc99e Initial load
duke
parents:
diff changeset
57 String srcDir = System.getProperty("test.src");
a61af66fc99e Initial load
duke
parents:
diff changeset
58
a61af66fc99e Initial load
duke
parents:
diff changeset
59 if (srcDir == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
60 srcDir = System.getProperty("user.dir");
a61af66fc99e Initial load
duke
parents:
diff changeset
61 }
a61af66fc99e Initial load
duke
parents:
diff changeset
62 srcDir = srcDir + File.separator;
a61af66fc99e Initial load
duke
parents:
diff changeset
63
a61af66fc99e Initial load
duke
parents:
diff changeset
64 File myDir = new File(srcDir);
a61af66fc99e Initial load
duke
parents:
diff changeset
65
a61af66fc99e Initial load
duke
parents:
diff changeset
66 File myFile = new File(myDir, filename);
a61af66fc99e Initial load
duke
parents:
diff changeset
67 if (!myFile.canRead()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
68 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
69 // We have some subdirs of test/com/sun/jdi so in case we
a61af66fc99e Initial load
duke
parents:
diff changeset
70 // are in one of them, look in our parent dir for the file.
a61af66fc99e Initial load
duke
parents:
diff changeset
71 myFile = new File(myDir.getCanonicalFile().getParent(),
a61af66fc99e Initial load
duke
parents:
diff changeset
72 filename);
a61af66fc99e Initial load
duke
parents:
diff changeset
73 if (!myFile.canRead()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
74 return "";
a61af66fc99e Initial load
duke
parents:
diff changeset
75 }
a61af66fc99e Initial load
duke
parents:
diff changeset
76 } catch (IOException ee) {
a61af66fc99e Initial load
duke
parents:
diff changeset
77 System.out.println("-- Error 1 trying to access file " +
a61af66fc99e Initial load
duke
parents:
diff changeset
78 myFile.getPath() + ": " + ee);
a61af66fc99e Initial load
duke
parents:
diff changeset
79 return "";
a61af66fc99e Initial load
duke
parents:
diff changeset
80 }
a61af66fc99e Initial load
duke
parents:
diff changeset
81 }
a61af66fc99e Initial load
duke
parents:
diff changeset
82 String wholePath = myFile.getPath();
a61af66fc99e Initial load
duke
parents:
diff changeset
83 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
84 reader = new BufferedReader(new FileReader(myFile));
a61af66fc99e Initial load
duke
parents:
diff changeset
85 } catch (FileNotFoundException ee) {
a61af66fc99e Initial load
duke
parents:
diff changeset
86 System.out.println("-- Error 2 trying to access file " +
a61af66fc99e Initial load
duke
parents:
diff changeset
87 wholePath + ": " + ee);
a61af66fc99e Initial load
duke
parents:
diff changeset
88 return "";
a61af66fc99e Initial load
duke
parents:
diff changeset
89 }
a61af66fc99e Initial load
duke
parents:
diff changeset
90
a61af66fc99e Initial load
duke
parents:
diff changeset
91 String line;
a61af66fc99e Initial load
duke
parents:
diff changeset
92 String retVal = "";
a61af66fc99e Initial load
duke
parents:
diff changeset
93 while (true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
94 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 line = reader.readLine();
a61af66fc99e Initial load
duke
parents:
diff changeset
96 } catch (IOException ee) {
a61af66fc99e Initial load
duke
parents:
diff changeset
97 System.out.println("-- Error reading options from file " +
a61af66fc99e Initial load
duke
parents:
diff changeset
98 wholePath + ": " + ee);
a61af66fc99e Initial load
duke
parents:
diff changeset
99 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
100 }
a61af66fc99e Initial load
duke
parents:
diff changeset
101 if (line == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
102 System.out.println("-- No debuggee VM options found in file " +
a61af66fc99e Initial load
duke
parents:
diff changeset
103 wholePath);
a61af66fc99e Initial load
duke
parents:
diff changeset
104 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
105 }
a61af66fc99e Initial load
duke
parents:
diff changeset
106 line = line.trim();
a61af66fc99e Initial load
duke
parents:
diff changeset
107 if (line.length() != 0 && !line.startsWith("#")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
108 System.out.println("-- Added debuggeeVM options from file " +
a61af66fc99e Initial load
duke
parents:
diff changeset
109 wholePath + ": " + line);
a61af66fc99e Initial load
duke
parents:
diff changeset
110 retVal = line;
a61af66fc99e Initial load
duke
parents:
diff changeset
111 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
112 }
a61af66fc99e Initial load
duke
parents:
diff changeset
113 // Else, read he next line.
a61af66fc99e Initial load
duke
parents:
diff changeset
114 }
a61af66fc99e Initial load
duke
parents:
diff changeset
115 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
116 reader.close();
a61af66fc99e Initial load
duke
parents:
diff changeset
117 } catch (IOException ee) {
a61af66fc99e Initial load
duke
parents:
diff changeset
118 }
a61af66fc99e Initial load
duke
parents:
diff changeset
119 return retVal;
a61af66fc99e Initial load
duke
parents:
diff changeset
120 }
a61af66fc99e Initial load
duke
parents:
diff changeset
121
a61af66fc99e Initial load
duke
parents:
diff changeset
122 private Connector findConnector(String name) {
a61af66fc99e Initial load
duke
parents:
diff changeset
123 List connectors = Bootstrap.virtualMachineManager().allConnectors();
a61af66fc99e Initial load
duke
parents:
diff changeset
124 Iterator iter = connectors.iterator();
a61af66fc99e Initial load
duke
parents:
diff changeset
125 while (iter.hasNext()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
126 Connector connector = (Connector)iter.next();
a61af66fc99e Initial load
duke
parents:
diff changeset
127 if (connector.name().equals(name)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
128 return connector;
a61af66fc99e Initial load
duke
parents:
diff changeset
129 }
a61af66fc99e Initial load
duke
parents:
diff changeset
130 }
a61af66fc99e Initial load
duke
parents:
diff changeset
131 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
132 }
a61af66fc99e Initial load
duke
parents:
diff changeset
133
a61af66fc99e Initial load
duke
parents:
diff changeset
134 private Map parseConnectorArgs(Connector connector, String argString) {
a61af66fc99e Initial load
duke
parents:
diff changeset
135 StringTokenizer tokenizer = new StringTokenizer(argString, ",");
a61af66fc99e Initial load
duke
parents:
diff changeset
136 Map arguments = connector.defaultArguments();
a61af66fc99e Initial load
duke
parents:
diff changeset
137
a61af66fc99e Initial load
duke
parents:
diff changeset
138 while (tokenizer.hasMoreTokens()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
139 String token = tokenizer.nextToken();
a61af66fc99e Initial load
duke
parents:
diff changeset
140 int index = token.indexOf('=');
a61af66fc99e Initial load
duke
parents:
diff changeset
141 if (index == -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
142 throw new IllegalArgumentException("Illegal connector argument: " +
a61af66fc99e Initial load
duke
parents:
diff changeset
143 token);
a61af66fc99e Initial load
duke
parents:
diff changeset
144 }
a61af66fc99e Initial load
duke
parents:
diff changeset
145 String name = token.substring(0, index);
a61af66fc99e Initial load
duke
parents:
diff changeset
146 String value = token.substring(index + 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
147 Connector.Argument argument = (Connector.Argument)arguments.get(name);
a61af66fc99e Initial load
duke
parents:
diff changeset
148 if (argument == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
149 throw new IllegalArgumentException("Argument " + name +
a61af66fc99e Initial load
duke
parents:
diff changeset
150 "is not defined for connector: " +
a61af66fc99e Initial load
duke
parents:
diff changeset
151 connector.name());
a61af66fc99e Initial load
duke
parents:
diff changeset
152 }
a61af66fc99e Initial load
duke
parents:
diff changeset
153 argument.setValue(value);
a61af66fc99e Initial load
duke
parents:
diff changeset
154 }
a61af66fc99e Initial load
duke
parents:
diff changeset
155 return arguments;
a61af66fc99e Initial load
duke
parents:
diff changeset
156 }
a61af66fc99e Initial load
duke
parents:
diff changeset
157
a61af66fc99e Initial load
duke
parents:
diff changeset
158 VMConnection(String connectSpec, int traceFlags) {
a61af66fc99e Initial load
duke
parents:
diff changeset
159 String nameString;
a61af66fc99e Initial load
duke
parents:
diff changeset
160 String argString;
a61af66fc99e Initial load
duke
parents:
diff changeset
161 int index = connectSpec.indexOf(':');
a61af66fc99e Initial load
duke
parents:
diff changeset
162 if (index == -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
163 nameString = connectSpec;
a61af66fc99e Initial load
duke
parents:
diff changeset
164 argString = "";
a61af66fc99e Initial load
duke
parents:
diff changeset
165 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
166 nameString = connectSpec.substring(0, index);
a61af66fc99e Initial load
duke
parents:
diff changeset
167 argString = connectSpec.substring(index + 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
168 }
a61af66fc99e Initial load
duke
parents:
diff changeset
169
a61af66fc99e Initial load
duke
parents:
diff changeset
170 connector = findConnector(nameString);
a61af66fc99e Initial load
duke
parents:
diff changeset
171 if (connector == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
172 throw new IllegalArgumentException("No connector named: " +
a61af66fc99e Initial load
duke
parents:
diff changeset
173 nameString);
a61af66fc99e Initial load
duke
parents:
diff changeset
174 }
a61af66fc99e Initial load
duke
parents:
diff changeset
175
a61af66fc99e Initial load
duke
parents:
diff changeset
176 connectorArgs = parseConnectorArgs(connector, argString);
a61af66fc99e Initial load
duke
parents:
diff changeset
177 this.traceFlags = traceFlags;
a61af66fc99e Initial load
duke
parents:
diff changeset
178 }
a61af66fc99e Initial load
duke
parents:
diff changeset
179
a61af66fc99e Initial load
duke
parents:
diff changeset
180 synchronized VirtualMachine open() {
a61af66fc99e Initial load
duke
parents:
diff changeset
181 if (connector instanceof LaunchingConnector) {
a61af66fc99e Initial load
duke
parents:
diff changeset
182 vm = launchTarget();
a61af66fc99e Initial load
duke
parents:
diff changeset
183 } else if (connector instanceof AttachingConnector) {
a61af66fc99e Initial load
duke
parents:
diff changeset
184 vm = attachTarget();
a61af66fc99e Initial load
duke
parents:
diff changeset
185 } else if (connector instanceof ListeningConnector) {
a61af66fc99e Initial load
duke
parents:
diff changeset
186 vm = listenTarget();
a61af66fc99e Initial load
duke
parents:
diff changeset
187 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
188 throw new InternalError("Invalid connect type");
a61af66fc99e Initial load
duke
parents:
diff changeset
189 }
a61af66fc99e Initial load
duke
parents:
diff changeset
190 vm.setDebugTraceMode(traceFlags);
a61af66fc99e Initial load
duke
parents:
diff changeset
191 System.out.println("JVM version:" + vm.version());
a61af66fc99e Initial load
duke
parents:
diff changeset
192 System.out.println("JDI version: " + Bootstrap.virtualMachineManager().majorInterfaceVersion() +
a61af66fc99e Initial load
duke
parents:
diff changeset
193 "." + Bootstrap.virtualMachineManager().minorInterfaceVersion());
a61af66fc99e Initial load
duke
parents:
diff changeset
194 System.out.println("JVM description: " + vm.description());
a61af66fc99e Initial load
duke
parents:
diff changeset
195
a61af66fc99e Initial load
duke
parents:
diff changeset
196 return vm;
a61af66fc99e Initial load
duke
parents:
diff changeset
197 }
a61af66fc99e Initial load
duke
parents:
diff changeset
198
a61af66fc99e Initial load
duke
parents:
diff changeset
199 boolean setConnectorArg(String name, String value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
200 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
201 * Too late if the connection already made
a61af66fc99e Initial load
duke
parents:
diff changeset
202 */
a61af66fc99e Initial load
duke
parents:
diff changeset
203 if (vm != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
204 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
205 }
a61af66fc99e Initial load
duke
parents:
diff changeset
206
a61af66fc99e Initial load
duke
parents:
diff changeset
207 Connector.Argument argument = (Connector.Argument)connectorArgs.get(name);
a61af66fc99e Initial load
duke
parents:
diff changeset
208 if (argument == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
209 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
210 }
a61af66fc99e Initial load
duke
parents:
diff changeset
211 argument.setValue(value);
a61af66fc99e Initial load
duke
parents:
diff changeset
212 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
213 }
a61af66fc99e Initial load
duke
parents:
diff changeset
214
a61af66fc99e Initial load
duke
parents:
diff changeset
215 String connectorArg(String name) {
a61af66fc99e Initial load
duke
parents:
diff changeset
216 Connector.Argument argument = (Connector.Argument)connectorArgs.get(name);
a61af66fc99e Initial load
duke
parents:
diff changeset
217 if (argument == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
218 return "";
a61af66fc99e Initial load
duke
parents:
diff changeset
219 }
a61af66fc99e Initial load
duke
parents:
diff changeset
220 return argument.value();
a61af66fc99e Initial load
duke
parents:
diff changeset
221 }
a61af66fc99e Initial load
duke
parents:
diff changeset
222
a61af66fc99e Initial load
duke
parents:
diff changeset
223 public synchronized VirtualMachine vm() {
a61af66fc99e Initial load
duke
parents:
diff changeset
224 if (vm == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
225 throw new InternalError("VM not connected");
a61af66fc99e Initial load
duke
parents:
diff changeset
226 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
227 return vm;
a61af66fc99e Initial load
duke
parents:
diff changeset
228 }
a61af66fc99e Initial load
duke
parents:
diff changeset
229 }
a61af66fc99e Initial load
duke
parents:
diff changeset
230
a61af66fc99e Initial load
duke
parents:
diff changeset
231 boolean isOpen() {
a61af66fc99e Initial load
duke
parents:
diff changeset
232 return (vm != null);
a61af66fc99e Initial load
duke
parents:
diff changeset
233 }
a61af66fc99e Initial load
duke
parents:
diff changeset
234
a61af66fc99e Initial load
duke
parents:
diff changeset
235 boolean isLaunch() {
a61af66fc99e Initial load
duke
parents:
diff changeset
236 return (connector instanceof LaunchingConnector);
a61af66fc99e Initial load
duke
parents:
diff changeset
237 }
a61af66fc99e Initial load
duke
parents:
diff changeset
238
a61af66fc99e Initial load
duke
parents:
diff changeset
239 Connector connector() {
a61af66fc99e Initial load
duke
parents:
diff changeset
240 return connector;
a61af66fc99e Initial load
duke
parents:
diff changeset
241 }
a61af66fc99e Initial load
duke
parents:
diff changeset
242
a61af66fc99e Initial load
duke
parents:
diff changeset
243 boolean isListen() {
a61af66fc99e Initial load
duke
parents:
diff changeset
244 return (connector instanceof ListeningConnector);
a61af66fc99e Initial load
duke
parents:
diff changeset
245 }
a61af66fc99e Initial load
duke
parents:
diff changeset
246
a61af66fc99e Initial load
duke
parents:
diff changeset
247 boolean isAttach() {
a61af66fc99e Initial load
duke
parents:
diff changeset
248 return (connector instanceof AttachingConnector);
a61af66fc99e Initial load
duke
parents:
diff changeset
249 }
a61af66fc99e Initial load
duke
parents:
diff changeset
250
a61af66fc99e Initial load
duke
parents:
diff changeset
251 private synchronized void notifyOutputComplete() {
a61af66fc99e Initial load
duke
parents:
diff changeset
252 outputCompleteCount++;
a61af66fc99e Initial load
duke
parents:
diff changeset
253 notifyAll();
a61af66fc99e Initial load
duke
parents:
diff changeset
254 }
a61af66fc99e Initial load
duke
parents:
diff changeset
255
a61af66fc99e Initial load
duke
parents:
diff changeset
256 private synchronized void waitOutputComplete() {
a61af66fc99e Initial load
duke
parents:
diff changeset
257 // Wait for stderr and stdout
a61af66fc99e Initial load
duke
parents:
diff changeset
258 if (process != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
259 while (outputCompleteCount < 2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
260 try {wait();} catch (InterruptedException e) {}
a61af66fc99e Initial load
duke
parents:
diff changeset
261 }
a61af66fc99e Initial load
duke
parents:
diff changeset
262 }
a61af66fc99e Initial load
duke
parents:
diff changeset
263 }
a61af66fc99e Initial load
duke
parents:
diff changeset
264
a61af66fc99e Initial load
duke
parents:
diff changeset
265 public void disposeVM() {
a61af66fc99e Initial load
duke
parents:
diff changeset
266 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
267 if (vm != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
268 vm.dispose();
a61af66fc99e Initial load
duke
parents:
diff changeset
269 vm = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
270 }
a61af66fc99e Initial load
duke
parents:
diff changeset
271 } finally {
a61af66fc99e Initial load
duke
parents:
diff changeset
272 if (process != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
273 process.destroy();
a61af66fc99e Initial load
duke
parents:
diff changeset
274 process = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
275 }
a61af66fc99e Initial load
duke
parents:
diff changeset
276 waitOutputComplete();
a61af66fc99e Initial load
duke
parents:
diff changeset
277 }
a61af66fc99e Initial load
duke
parents:
diff changeset
278 }
a61af66fc99e Initial load
duke
parents:
diff changeset
279
a61af66fc99e Initial load
duke
parents:
diff changeset
280 private void dumpStream(InputStream stream) throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
281 PrintStream outStream = System.out;
a61af66fc99e Initial load
duke
parents:
diff changeset
282 BufferedReader in =
a61af66fc99e Initial load
duke
parents:
diff changeset
283 new BufferedReader(new InputStreamReader(stream));
a61af66fc99e Initial load
duke
parents:
diff changeset
284 String line;
a61af66fc99e Initial load
duke
parents:
diff changeset
285 while ((line = in.readLine()) != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
286 outStream.println(line);
a61af66fc99e Initial load
duke
parents:
diff changeset
287 }
a61af66fc99e Initial load
duke
parents:
diff changeset
288 }
a61af66fc99e Initial load
duke
parents:
diff changeset
289
a61af66fc99e Initial load
duke
parents:
diff changeset
290 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
291 * Create a Thread that will retrieve and display any output.
a61af66fc99e Initial load
duke
parents:
diff changeset
292 * Needs to be high priority, else debugger may exit before
a61af66fc99e Initial load
duke
parents:
diff changeset
293 * it can be displayed.
a61af66fc99e Initial load
duke
parents:
diff changeset
294 */
a61af66fc99e Initial load
duke
parents:
diff changeset
295 private void displayRemoteOutput(final InputStream stream) {
a61af66fc99e Initial load
duke
parents:
diff changeset
296 Thread thr = new Thread("output reader") {
a61af66fc99e Initial load
duke
parents:
diff changeset
297 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
298 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
299 dumpStream(stream);
a61af66fc99e Initial load
duke
parents:
diff changeset
300 } catch (IOException ex) {
a61af66fc99e Initial load
duke
parents:
diff changeset
301 System.err.println("IOException reading output of child java interpreter:"
a61af66fc99e Initial load
duke
parents:
diff changeset
302 + ex.getMessage());
a61af66fc99e Initial load
duke
parents:
diff changeset
303 } finally {
a61af66fc99e Initial load
duke
parents:
diff changeset
304 notifyOutputComplete();
a61af66fc99e Initial load
duke
parents:
diff changeset
305 }
a61af66fc99e Initial load
duke
parents:
diff changeset
306 }
a61af66fc99e Initial load
duke
parents:
diff changeset
307 };
a61af66fc99e Initial load
duke
parents:
diff changeset
308 thr.setPriority(Thread.MAX_PRIORITY-1);
a61af66fc99e Initial load
duke
parents:
diff changeset
309 thr.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
310 }
a61af66fc99e Initial load
duke
parents:
diff changeset
311
a61af66fc99e Initial load
duke
parents:
diff changeset
312 private void dumpFailedLaunchInfo(Process process) {
a61af66fc99e Initial load
duke
parents:
diff changeset
313 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
314 dumpStream(process.getErrorStream());
a61af66fc99e Initial load
duke
parents:
diff changeset
315 dumpStream(process.getInputStream());
a61af66fc99e Initial load
duke
parents:
diff changeset
316 } catch (IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
317 System.err.println("Unable to display process output: " +
a61af66fc99e Initial load
duke
parents:
diff changeset
318 e.getMessage());
a61af66fc99e Initial load
duke
parents:
diff changeset
319 }
a61af66fc99e Initial load
duke
parents:
diff changeset
320 }
a61af66fc99e Initial load
duke
parents:
diff changeset
321
a61af66fc99e Initial load
duke
parents:
diff changeset
322 /* launch child target vm */
a61af66fc99e Initial load
duke
parents:
diff changeset
323 private VirtualMachine launchTarget() {
a61af66fc99e Initial load
duke
parents:
diff changeset
324 LaunchingConnector launcher = (LaunchingConnector)connector;
a61af66fc99e Initial load
duke
parents:
diff changeset
325 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
326 VirtualMachine vm = launcher.launch(connectorArgs);
a61af66fc99e Initial load
duke
parents:
diff changeset
327 process = vm.process();
a61af66fc99e Initial load
duke
parents:
diff changeset
328 displayRemoteOutput(process.getErrorStream());
a61af66fc99e Initial load
duke
parents:
diff changeset
329 displayRemoteOutput(process.getInputStream());
a61af66fc99e Initial load
duke
parents:
diff changeset
330 return vm;
a61af66fc99e Initial load
duke
parents:
diff changeset
331 } catch (IOException ioe) {
a61af66fc99e Initial load
duke
parents:
diff changeset
332 ioe.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
333 System.err.println("\n Unable to launch target VM.");
a61af66fc99e Initial load
duke
parents:
diff changeset
334 } catch (IllegalConnectorArgumentsException icae) {
a61af66fc99e Initial load
duke
parents:
diff changeset
335 icae.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
336 System.err.println("\n Internal debugger error.");
a61af66fc99e Initial load
duke
parents:
diff changeset
337 } catch (VMStartException vmse) {
a61af66fc99e Initial load
duke
parents:
diff changeset
338 System.err.println(vmse.getMessage() + "\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
339 dumpFailedLaunchInfo(vmse.process());
a61af66fc99e Initial load
duke
parents:
diff changeset
340 System.err.println("\n Target VM failed to initialize.");
a61af66fc99e Initial load
duke
parents:
diff changeset
341 }
a61af66fc99e Initial load
duke
parents:
diff changeset
342 return null; // Shuts up the compiler
a61af66fc99e Initial load
duke
parents:
diff changeset
343 }
a61af66fc99e Initial load
duke
parents:
diff changeset
344
a61af66fc99e Initial load
duke
parents:
diff changeset
345 /* attach to running target vm */
a61af66fc99e Initial load
duke
parents:
diff changeset
346 private VirtualMachine attachTarget() {
a61af66fc99e Initial load
duke
parents:
diff changeset
347 AttachingConnector attacher = (AttachingConnector)connector;
a61af66fc99e Initial load
duke
parents:
diff changeset
348 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
349 return attacher.attach(connectorArgs);
a61af66fc99e Initial load
duke
parents:
diff changeset
350 } catch (IOException ioe) {
a61af66fc99e Initial load
duke
parents:
diff changeset
351 ioe.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
352 System.err.println("\n Unable to attach to target VM.");
a61af66fc99e Initial load
duke
parents:
diff changeset
353 } catch (IllegalConnectorArgumentsException icae) {
a61af66fc99e Initial load
duke
parents:
diff changeset
354 icae.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
355 System.err.println("\n Internal debugger error.");
a61af66fc99e Initial load
duke
parents:
diff changeset
356 }
a61af66fc99e Initial load
duke
parents:
diff changeset
357 return null; // Shuts up the compiler
a61af66fc99e Initial load
duke
parents:
diff changeset
358 }
a61af66fc99e Initial load
duke
parents:
diff changeset
359
a61af66fc99e Initial load
duke
parents:
diff changeset
360 /* listen for connection from target vm */
a61af66fc99e Initial load
duke
parents:
diff changeset
361 private VirtualMachine listenTarget() {
a61af66fc99e Initial load
duke
parents:
diff changeset
362 ListeningConnector listener = (ListeningConnector)connector;
a61af66fc99e Initial load
duke
parents:
diff changeset
363 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
364 String retAddress = listener.startListening(connectorArgs);
a61af66fc99e Initial load
duke
parents:
diff changeset
365 System.out.println("Listening at address: " + retAddress);
a61af66fc99e Initial load
duke
parents:
diff changeset
366 vm = listener.accept(connectorArgs);
a61af66fc99e Initial load
duke
parents:
diff changeset
367 listener.stopListening(connectorArgs);
a61af66fc99e Initial load
duke
parents:
diff changeset
368 return vm;
a61af66fc99e Initial load
duke
parents:
diff changeset
369 } catch (IOException ioe) {
a61af66fc99e Initial load
duke
parents:
diff changeset
370 ioe.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
371 System.err.println("\n Unable to attach to target VM.");
a61af66fc99e Initial load
duke
parents:
diff changeset
372 } catch (IllegalConnectorArgumentsException icae) {
a61af66fc99e Initial load
duke
parents:
diff changeset
373 icae.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
374 System.err.println("\n Internal debugger error.");
a61af66fc99e Initial load
duke
parents:
diff changeset
375 }
a61af66fc99e Initial load
duke
parents:
diff changeset
376 return null; // Shuts up the compiler
a61af66fc99e Initial load
duke
parents:
diff changeset
377 }
a61af66fc99e Initial load
duke
parents:
diff changeset
378 }