History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/com/sun/tools/example/debug/bdi/ChildSession.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/tools/example/debug/bdi/ChildSession.java+300−0
@@ -0,0 +1,300 @@
1/*
2 * Copyright (c) 1998, 1999, 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
26package com.sun.tools.example.debug.bdi;
27
28import com.sun.jdi.*;
29import com.sun.jdi.connect.LaunchingConnector;
30import com.sun.jdi.connect.Connector;
31import com.sun.jdi.connect.VMStartException;
32import com.sun.jdi.connect.IllegalConnectorArgumentsException;
33import java.io.*;
34import java.util.Map;
35import javax.swing.SwingUtilities;
36
37
38class ChildSession extends Session {
39
40 private Process process;
41
42 private PrintWriter in;
43 private BufferedReader out;
44 private BufferedReader err;
45
46 private InputListener input;
47 private OutputListener output;
48 private OutputListener error;
49
50 public ChildSession(ExecutionManager runtime,
51 String userVMArgs, String cmdLine,
52 InputListener input,
53 OutputListener output,
54 OutputListener error,
55 OutputListener diagnostics) {
56 this(runtime, getVM(diagnostics, userVMArgs, cmdLine),
57 input, output, error, diagnostics);
58 }
59
60 public ChildSession(ExecutionManager runtime,
61 LaunchingConnector connector,
62 Map<String, Connector.Argument> arguments,
63 InputListener input,
64 OutputListener output,
65 OutputListener error,
66 OutputListener diagnostics) {
67 this(runtime, generalGetVM(diagnostics, connector, arguments),
68 input, output, error, diagnostics);
69 }
70
71 private ChildSession(ExecutionManager runtime,
72 VirtualMachine vm,
73 InputListener input,
74 OutputListener output,
75 OutputListener error,
76 OutputListener diagnostics) {
77 super(vm, runtime, diagnostics);
78 this.input = input;
79 this.output = output;
80 this.error = error;
81 }
82
83 @Override
84 public boolean attach() {
85
86 if (!connectToVMProcess()) {
87 diagnostics.putString("Could not launch VM");
88 return false;
89 }
90
91 /*
92 * Create a Thread that will retrieve and display any output.
93 * Needs to be high priority, else debugger may exit before
94 * it can be displayed.
95 */
96
97 //### Rename InputWriter and OutputReader classes
98 //### Thread priorities cribbed from ttydebug. Think about them.
99
100 OutputReader outputReader =
101 new OutputReader("output reader", "output",
102 out, output, diagnostics);
103 outputReader.setPriority(Thread.MAX_PRIORITY-1);
104 outputReader.start();
105
106 OutputReader errorReader =
107 new OutputReader("error reader", "error",
108 err, error, diagnostics);
109 errorReader.setPriority(Thread.MAX_PRIORITY-1);
110 errorReader.start();
111
112 InputWriter inputWriter =
113 new InputWriter("input writer", in, input);
114 inputWriter.setPriority(Thread.MAX_PRIORITY-1);
115 inputWriter.start();
116
117 if (!super.attach()) {
118 if (process != null) {
119 process.destroy();
120 process = null;
121 }
122 return false;
123 }
124
125 //### debug
126 //System.out.println("IO after attach: "+ inputWriter + " " + outputReader + " "+ errorReader);
127
128 return true;
129 }
130
131 @Override
132 public void detach() {
133
134 //### debug
135 //System.out.println("IO before detach: "+ inputWriter + " " + outputReader + " "+ errorReader);
136
137 super.detach();
138
139 /*
140 inputWriter.quit();
141 outputReader.quit();
142 errorReader.quit();
143 */
144
145 if (process != null) {
146 process.destroy();
147 process = null;
148 }
149
150 }
151
152 /**
153 * Launch child java interpreter, return host:port
154 */
155
156 static private void dumpStream(OutputListener diagnostics,
157 InputStream stream) throws IOException {
158 BufferedReader in =
159 new BufferedReader(new InputStreamReader(stream));
160 String line;
161 while ((line = in.readLine()) != null) {
162 diagnostics.putString(line);
163 }
164 }
165
166 static private void dumpFailedLaunchInfo(OutputListener diagnostics,
167 Process process) {
168 try {
169 dumpStream(diagnostics, process.getErrorStream());
170 dumpStream(diagnostics, process.getInputStream());
171 } catch (IOException e) {
172 diagnostics.putString("Unable to display process output: " +
173 e.getMessage());
174 }
175 }
176
177 static private VirtualMachine getVM(OutputListener diagnostics,
178 String userVMArgs,
179 String cmdLine) {
180 VirtualMachineManager manager = Bootstrap.virtualMachineManager();
181 LaunchingConnector connector = manager.defaultConnector();
182 Map<String, Connector.Argument> arguments = connector.defaultArguments();
183 arguments.get("options").setValue(userVMArgs);
184 arguments.get("main").setValue(cmdLine);
185 return generalGetVM(diagnostics, connector, arguments);
186 }
187
188 static private VirtualMachine generalGetVM(OutputListener diagnostics,
189 LaunchingConnector connector,
190 Map<String, Connector.Argument> arguments) {
191 VirtualMachine vm = null;
192 try {
193 diagnostics.putString("Starting child.");
194 vm = connector.launch(arguments);
195 } catch (IOException ioe) {
196 diagnostics.putString("Unable to start child: " + ioe.getMessage());
197 } catch (IllegalConnectorArgumentsException icae) {
198 diagnostics.putString("Unable to start child: " + icae.getMessage());
199 } catch (VMStartException vmse) {
200 diagnostics.putString("Unable to start child: " + vmse.getMessage() + '\n');
201 dumpFailedLaunchInfo(diagnostics, vmse.process());
202 }
203 return vm;
204 }
205
206 private boolean connectToVMProcess() {
207 if (vm == null) {
208 return false;
209 }
210 process = vm.process();
211 in = new PrintWriter(new OutputStreamWriter(process.getOutputStream()));
212 //### Note small buffer sizes!
213 out = new BufferedReader(new InputStreamReader(process.getInputStream()), 1);
214 err = new BufferedReader(new InputStreamReader(process.getErrorStream()), 1);
215 return true;
216 }
217
218 /**
219 * Threads to handle application input/output.
220 */
221
222 private static class OutputReader extends Thread {
223
224 private String streamName;
225 private BufferedReader stream;
226 private OutputListener output;
227 private OutputListener diagnostics;
228 private boolean running = true;
229 private char[] buffer = new char[512];
230
231 OutputReader(String threadName,
232 String streamName,
233 BufferedReader stream,
234 OutputListener output,
235 OutputListener diagnostics) {
236 super(threadName);
237 this.streamName = streamName;
238 this.stream = stream;
239 this.output = output;
240 this.diagnostics = diagnostics;
241 }
242
243 @Override
244 public void run() {
245 try {
246 int count;
247 while (running && (count = stream.read(buffer, 0, 512)) != -1) {
248 if (count > 0) {
249 // Run in Swing event dispatcher thread.
250 final String chars = new String(buffer, 0, count);
251 SwingUtilities.invokeLater(new Runnable() {
252 @Override
253 public void run() {
254 output.putString(chars);
255 }
256 });
257 }
258 //### Should we sleep briefly here?
259 }
260 } catch (IOException e) {
261 // Run in Swing event dispatcher thread.
262 SwingUtilities.invokeLater(new Runnable() {
263 @Override
264 public void run() {
265 diagnostics.putString("IO error reading " +
266 streamName +
267 " stream of child java interpreter");
268 }
269 });
270 }
271 }
272 }
273
274 private static class InputWriter extends Thread {
275
276 private PrintWriter stream;
277 private InputListener input;
278 private boolean running = true;
279
280 InputWriter(String threadName,
281 PrintWriter stream,
282 InputListener input) {
283 super(threadName);
284 this.stream = stream;
285 this.input = input;
286 }
287
288 @Override
289 public void run() {
290 String line;
291 while (running) {
292 line = input.getLine();
293 stream.println(line);
294 // Should not be needed for println above!
295 stream.flush();
296 }
297 }
298 }
299
300}