|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| package edu.rice.cs.drjava.model.debug.jpda; |
|
38 |
| |
|
39 |
| import edu.rice.cs.util.Log; |
|
40 |
| |
|
41 |
| import com.sun.jdi.*; |
|
42 |
| import com.sun.jdi.event.*; |
|
43 |
| import com.sun.jdi.request.*; |
|
44 |
| |
|
45 |
| import java.io.*; |
|
46 |
| import javax.swing.SwingUtilities; |
|
47 |
| |
|
48 |
| import edu.rice.cs.drjava.model.OpenDefinitionsDocument; |
|
49 |
| import edu.rice.cs.drjava.model.debug.DebugException; |
|
50 |
| import edu.rice.cs.plt.tuple.Pair; |
|
51 |
| import edu.rice.cs.util.UnexpectedException; |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| public class EventHandlerThread extends Thread { |
|
57 |
| |
|
58 |
| |
|
59 |
| private final JPDADebugger _debugger; |
|
60 |
| |
|
61 |
| |
|
62 |
| private final VirtualMachine _vm; |
|
63 |
| |
|
64 |
| |
|
65 |
| private volatile boolean _connected; |
|
66 |
| |
|
67 |
| |
|
68 |
| private static final Log _log = new Log("GlobalModel.txt", false); |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
0
| EventHandlerThread(JPDADebugger debugger, VirtualMachine vm) {
|
|
76 |
0
| super("DrJava Debug Event Handler");
|
|
77 |
0
| _debugger = debugger;
|
|
78 |
0
| _vm = vm;
|
|
79 |
0
| _connected = true;
|
|
80 |
| } |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
0
| private void _log(String message) { _log.log(message); }
|
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
0
| private void _log(String message, Throwable t) { _log.log(message, t); }
|
|
92 |
| |
|
93 |
| |
|
94 |
0
| public void run() {
|
|
95 |
0
| _log.log("Debugger starting");
|
|
96 |
0
| _debugger.notifyDebuggerStarted();
|
|
97 |
| |
|
98 |
0
| EventQueue queue = _vm.eventQueue();
|
|
99 |
0
| while (_connected) {
|
|
100 |
0
| try {
|
|
101 |
0
| try {
|
|
102 |
| |
|
103 |
0
| EventSet eventSet = queue.remove();
|
|
104 |
0
| EventIterator it = eventSet.eventIterator();
|
|
105 |
| |
|
106 |
0
| while (it.hasNext()) handleEvent(it.nextEvent());
|
|
107 |
| } |
|
108 |
| catch (InterruptedException ie) { |
|
109 |
| |
|
110 |
| |
|
111 |
0
| _log("InterruptedException in main loop: " + ie);
|
|
112 |
| } |
|
113 |
| catch (VMDisconnectedException de) { |
|
114 |
| |
|
115 |
0
| handleDisconnectedException();
|
|
116 |
0
| break;
|
|
117 |
| } |
|
118 |
| } |
|
119 |
| catch (Exception e) { |
|
120 |
| |
|
121 |
0
| _log("Exception in main event handler loop.", e);
|
|
122 |
0
| _debugger.eventHandlerError(e);
|
|
123 |
0
| _debugger.printMessage("An exception occurred in the event handler:\n" + e);
|
|
124 |
0
| _debugger.printMessage("The debugger may have become unstable as a result.");
|
|
125 |
0
| ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
126 |
0
| e.printStackTrace(new PrintWriter(baos, true));
|
|
127 |
0
| _debugger.printMessage("Stack trace: " + baos.toString());
|
|
128 |
| } |
|
129 |
| } |
|
130 |
| |
|
131 |
0
| _debugger.notifyDebuggerShutdown();
|
|
132 |
| } |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
0
| private void handleEvent(Event e) throws DebugException {
|
|
138 |
| |
|
139 |
0
| _log("handling event: " + e);
|
|
140 |
| |
|
141 |
0
| if (e instanceof BreakpointEvent) _handleBreakpointEvent((BreakpointEvent) e);
|
|
142 |
0
| else if (e instanceof StepEvent) _handleStepEvent((StepEvent) e);
|
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
0
| else if (e instanceof ClassPrepareEvent) _handleClassPrepareEvent((ClassPrepareEvent) e);
|
|
147 |
0
| else if (e instanceof ThreadStartEvent) _handleThreadStartEvent((ThreadStartEvent) e);
|
|
148 |
0
| else if (e instanceof ThreadDeathEvent) _handleThreadDeathEvent((ThreadDeathEvent) e);
|
|
149 |
0
| else if (e instanceof VMDeathEvent) _handleVMDeathEvent((VMDeathEvent) e);
|
|
150 |
0
| else if (e instanceof VMDisconnectEvent) _handleVMDisconnectEvent((VMDisconnectEvent) e);
|
|
151 |
| else |
|
152 |
0
| throw new DebugException("Unexpected event type: " + e);
|
|
153 |
| } |
|
154 |
| |
|
155 |
| |
|
156 |
0
| private boolean _isSuspendedWithFrames(ThreadReference thread) throws DebugException {
|
|
157 |
| |
|
158 |
0
| try { return thread.isSuspended() && thread.frameCount() > 0; }
|
|
159 |
| catch (IncompatibleThreadStateException itse) { |
|
160 |
0
| throw new DebugException("Could not count frames on a suspended thread: " + itse);
|
|
161 |
| } |
|
162 |
| } |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
0
| private void _handleBreakpointEvent(final BreakpointEvent e) {
|
|
168 |
| |
|
169 |
0
| SwingUtilities.invokeLater(new Runnable() {
|
|
170 |
0
| public void run() {
|
|
171 |
| |
|
172 |
0
| try {
|
|
173 |
0
| if (_isSuspendedWithFrames(e.thread()) && _debugger.setCurrentThread(e.thread())) {
|
|
174 |
| |
|
175 |
0
| _debugger.currThreadSuspended((BreakpointRequest) e.request());
|
|
176 |
0
| _debugger.reachedBreakpoint((BreakpointRequest) e.request());
|
|
177 |
| } |
|
178 |
| } |
|
179 |
0
| catch(DebugException e) { throw new UnexpectedException(e); }
|
|
180 |
| } |
|
181 |
| }); |
|
182 |
| } |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
0
| private void _handleStepEvent(final StepEvent e) {
|
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
0
| SwingUtilities.invokeLater(new Runnable() {
|
|
193 |
0
| public void run() {
|
|
194 |
0
| try {
|
|
195 |
0
| Pair<Location,OpenDefinitionsDocument> locAndDoc = _debugger.preloadDocument(e.location());
|
|
196 |
0
| Location lll = locAndDoc.first();
|
|
197 |
0
| if (_isSuspendedWithFrames(e.thread()) && _debugger.setCurrentThread(e.thread())) {
|
|
198 |
0
| _debugger.printMessage("Stepped to " + lll.declaringType().name() + "." + lll.method().name()
|
|
199 |
| + "(...) [line " + lll.lineNumber() + "]"); |
|
200 |
0
| _debugger.currThreadSuspended();
|
|
201 |
| } |
|
202 |
| |
|
203 |
0
| _debugger.getEventRequestManager().deleteEventRequest(e.request());
|
|
204 |
| } |
|
205 |
0
| catch(DebugException e) { throw new UnexpectedException(e); }
|
|
206 |
| } |
|
207 |
| }); |
|
208 |
| } |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
0
| private void _handleClassPrepareEvent(final ClassPrepareEvent e) {
|
|
225 |
| |
|
226 |
0
| SwingUtilities.invokeLater(new Runnable() {
|
|
227 |
0
| public void run() {
|
|
228 |
0
| try {
|
|
229 |
0
| _debugger.getPendingRequestManager().classPrepared(e);
|
|
230 |
| |
|
231 |
0
| e.thread().resume();
|
|
232 |
| } |
|
233 |
0
| catch(DebugException e) { throw new UnexpectedException(e); }
|
|
234 |
| } |
|
235 |
| }); |
|
236 |
| } |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
0
| private void _handleThreadStartEvent(ThreadStartEvent e) {
|
|
242 |
| |
|
243 |
0
| SwingUtilities.invokeLater(new Runnable() {
|
|
244 |
0
| public void run() { _debugger.threadStarted(); }
|
|
245 |
| }); |
|
246 |
| } |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
0
| private void _handleThreadDeathEvent(final ThreadDeathEvent e) {
|
|
252 |
| |
|
253 |
| |
|
254 |
0
| SwingUtilities.invokeLater(new Runnable() {
|
|
255 |
0
| public void run() {
|
|
256 |
0
| try {
|
|
257 |
0
| ThreadReference running = _debugger.getCurrentRunningThread();
|
|
258 |
0
| if (e.thread().equals(running)) {
|
|
259 |
| |
|
260 |
0
| EventRequestManager erm = _vm.eventRequestManager();
|
|
261 |
0
| for (StepRequest step : erm.stepRequests()) {
|
|
262 |
0
| if (step.thread().equals(e.thread())) {
|
|
263 |
0
| erm.deleteEventRequest(step);
|
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
0
| break;
|
|
268 |
| } |
|
269 |
| } |
|
270 |
0
| _debugger.currThreadDied();
|
|
271 |
| } |
|
272 |
0
| else _debugger.nonCurrThreadDied();
|
|
273 |
| |
|
274 |
| |
|
275 |
0
| e.thread().resume();
|
|
276 |
| } |
|
277 |
0
| catch(DebugException e) { throw new UnexpectedException(e); }
|
|
278 |
| } |
|
279 |
| }); |
|
280 |
| } |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
0
| private void _handleVMDeathEvent(VMDeathEvent e) throws DebugException { _cleanUp(e); }
|
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
0
| private void _handleVMDisconnectEvent(VMDisconnectEvent e) throws DebugException { _cleanUp(e); }
|
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
0
| private void _cleanUp(Event e) throws DebugException {
|
|
297 |
| |
|
298 |
0
| SwingUtilities.invokeLater(new Runnable() {
|
|
299 |
0
| public void run() {
|
|
300 |
0
| _connected = false;
|
|
301 |
0
| if (_debugger.isReady()) {
|
|
302 |
| |
|
303 |
| |
|
304 |
0
| _debugger.shutdown();
|
|
305 |
| } |
|
306 |
| } |
|
307 |
| }); |
|
308 |
| } |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
0
| private void handleDisconnectedException() throws DebugException {
|
|
313 |
0
| EventQueue queue = _vm.eventQueue();
|
|
314 |
0
| while (_connected) {
|
|
315 |
0
| try {
|
|
316 |
0
| EventSet eventSet = queue.remove();
|
|
317 |
0
| EventIterator iter = eventSet.eventIterator();
|
|
318 |
0
| while (iter.hasNext()) {
|
|
319 |
0
| Event event = iter.nextEvent();
|
|
320 |
0
| if (event instanceof VMDeathEvent) _handleVMDeathEvent((VMDeathEvent)event);
|
|
321 |
0
| else if (event instanceof VMDisconnectEvent) _handleVMDisconnectEvent((VMDisconnectEvent)event);
|
|
322 |
| |
|
323 |
| } |
|
324 |
0
| eventSet.resume();
|
|
325 |
| } |
|
326 |
| catch (InterruptedException ie) { |
|
327 |
| |
|
328 |
0
| _log("InterruptedException after a disconnected exception.", ie);
|
|
329 |
| } |
|
330 |
| catch (VMDisconnectedException de) { |
|
331 |
| |
|
332 |
0
| _log("A second VMDisconnectedException.", de);
|
|
333 |
| } |
|
334 |
| } |
|
335 |
| } |
|
336 |
| } |