KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > gui > CommandTool


1 /*
2  * @(#)CommandTool.java 1.15 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
11  * modify and redistribute this software in source and binary code form,
12  * provided that i) this copyright notice and license appear on all copies of
13  * the software; and ii) Licensee does not utilize the software in a manner
14  * which is disparaging to Sun.
15  *
16  * This software is provided "AS IS," without a warranty of any kind. ALL
17  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
18  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
20  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
21  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
22  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
23  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
24  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
25  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGES.
27  *
28  * This software is not designed or intended for use in on-line control of
29  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
30  * the design, construction, operation or maintenance of any nuclear
31  * facility. Licensee represents and warrants that it will not use or
32  * redistribute the Software for such purposes.
33  */

34
35 package com.sun.tools.example.debug.gui;
36
37 import java.io.*;
38 import java.util.*;
39
40 import javax.swing.*;
41 import java.awt.BorderLayout JavaDoc;
42 import java.awt.event.*;
43
44 import com.sun.jdi.*;
45 import com.sun.jdi.event.*;
46
47 import com.sun.tools.example.debug.bdi.*;
48 import com.sun.tools.example.debug.event.*;
49
50 public class CommandTool extends JPanel {
51
52     private Environment env;
53
54     private ContextManager context;
55     private ExecutionManager runtime;
56     private SourceManager sourceManager;
57
58     private TypeScript script;
59
60     private static final String JavaDoc DEFAULT_CMD_PROMPT = "Command:";
61
62     public CommandTool(Environment env) {
63
64     super(new BorderLayout JavaDoc());
65
66     this.env = env;
67     this.context = env.getContextManager();
68     this.runtime = env.getExecutionManager();
69     this.sourceManager = env.getSourceManager();
70
71     script = new TypeScript(DEFAULT_CMD_PROMPT, false); //no echo
72
this.add(script);
73
74     final CommandInterpreter interpreter =
75         new CommandInterpreter(env);
76
77     // Establish handler for incoming commands.
78

79     script.addActionListener(new ActionListener() {
80         public void actionPerformed(ActionEvent e) {
81         interpreter.executeCommand(script.readln());
82         }
83     });
84
85     // Establish ourselves as the listener for VM diagnostics.
86

87     OutputListener diagnosticsListener =
88         new TypeScriptOutputListener(script, true);
89         runtime.addDiagnosticsListener(diagnosticsListener);
90
91     // Establish ourselves as the shared debugger typescript.
92

93     env.setTypeScript(new PrintWriter(new TypeScriptWriter(script)));
94
95     // Handle VM events.
96

97         TTYDebugListener listener = new TTYDebugListener(diagnosticsListener);
98     
99         runtime.addJDIListener(listener);
100         runtime.addSessionListener(listener);
101         runtime.addSpecListener(listener);
102         context.addContextListener(listener);
103
104         //### remove listeners on exit!
105

106     }
107
108     private class TTYDebugListener implements
109             JDIListener, SessionListener, SpecListener, ContextListener {
110
111     private OutputListener diagnostics;
112
113         TTYDebugListener(OutputListener diagnostics) {
114         this.diagnostics = diagnostics;
115     }
116
117         // JDIListener
118

119         public void accessWatchpoint(AccessWatchpointEventSet e) {
120             setThread(e);
121             for (EventIterator it = e.eventIterator(); it.hasNext(); ) {
122                 Event evt = it.nextEvent();
123                 diagnostics.putString("Watchpoint hit: " +
124                                       locationString(e));
125             }
126     }
127
128         public void classPrepare(ClassPrepareEventSet e) {
129         if (context.getVerboseFlag()) {
130         String JavaDoc name = e.getReferenceType().name();
131         diagnostics.putString("Class " + name + " loaded");
132         }
133     }
134
135         public void classUnload(ClassUnloadEventSet e) {
136         if (context.getVerboseFlag()) {
137         diagnostics.putString("Class " + e.getClassName() +
138                                       " unloaded.");
139         }
140     }
141
142         public void exception(ExceptionEventSet e) {
143             setThread(e);
144         String JavaDoc name = e.getException().referenceType().name();
145         diagnostics.putString("Exception: " + name);
146     }
147
148         public void locationTrigger(LocationTriggerEventSet e) {
149             String JavaDoc locString = locationString(e);
150             setThread(e);
151             for (EventIterator it = e.eventIterator(); it.hasNext(); ) {
152                 Event evt = it.nextEvent();
153                 if (evt instanceof BreakpointEvent) {
154                     diagnostics.putString("Breakpoint hit: " + locString);
155                 } else if (evt instanceof StepEvent) {
156                     diagnostics.putString("Step completed: " + locString);
157                 } else if (evt instanceof MethodEntryEvent) {
158                     diagnostics.putString("Method entered: " + locString);
159                 } else if (evt instanceof MethodExitEvent) {
160                     diagnostics.putString("Method exited: " + locString);
161                 } else {
162                     diagnostics.putString("UNKNOWN event: " + e);
163                 }
164             }
165     }
166
167         public void modificationWatchpoint(ModificationWatchpointEventSet e) {
168             setThread(e);
169             for (EventIterator it = e.eventIterator(); it.hasNext(); ) {
170                 Event evt = it.nextEvent();
171                 diagnostics.putString("Watchpoint hit: " +
172                                       locationString(e));
173             }
174     }
175
176         public void threadDeath(ThreadDeathEventSet e) {
177         if (context.getVerboseFlag()) {
178         diagnostics.putString("Thread " + e.getThread() +
179                                       " ended.");
180         }
181     }
182
183         public void threadStart(ThreadStartEventSet e) {
184         if (context.getVerboseFlag()) {
185         diagnostics.putString("Thread " + e.getThread() +
186                                       " started.");
187         }
188     }
189
190         public void vmDeath(VMDeathEventSet e) {
191         script.setPrompt(DEFAULT_CMD_PROMPT);
192         diagnostics.putString("VM exited");
193         }
194
195         public void vmDisconnect(VMDisconnectEventSet e) {
196         script.setPrompt(DEFAULT_CMD_PROMPT);
197         diagnostics.putString("Disconnected from VM");
198         }
199
200         public void vmStart(VMStartEventSet e) {
201         script.setPrompt(DEFAULT_CMD_PROMPT);
202         diagnostics.putString("VM started");
203     }
204
205         // SessionListener
206

207         public void sessionStart(EventObject e) {}
208
209         public void sessionInterrupt(EventObject e) {
210             Thread.yield(); // fetch output
211
diagnostics.putString("VM interrupted by user.");
212         script.setPrompt(DEFAULT_CMD_PROMPT);
213     }
214
215         public void sessionContinue(EventObject e) {
216         diagnostics.putString("Execution resumed.");
217         script.setPrompt(DEFAULT_CMD_PROMPT);
218     }
219
220         // SpecListener
221

222         public void breakpointSet(SpecEvent e) {
223             EventRequestSpec spec = e.getEventRequestSpec();
224             diagnostics.putString("Breakpoint set at " + spec + ".");
225         }
226         public void breakpointDeferred(SpecEvent e) {
227             EventRequestSpec spec = e.getEventRequestSpec();
228             diagnostics.putString("Breakpoint will be set at " +
229                                   spec + " when its class is loaded.");
230         }
231         public void breakpointDeleted(SpecEvent e) {
232             EventRequestSpec spec = e.getEventRequestSpec();
233         diagnostics.putString("Breakpoint at " + spec.toString() + " deleted.");
234         }
235         public void breakpointResolved(SpecEvent e) {
236             EventRequestSpec spec = e.getEventRequestSpec();
237         diagnostics.putString("Breakpoint resolved to " + spec.toString() + ".");
238         }
239         public void breakpointError(SpecErrorEvent e) {
240             EventRequestSpec spec = e.getEventRequestSpec();
241         diagnostics.putString("Deferred breakpoint at " +
242                   spec + " could not be resolved:" +
243                                   e.getReason());
244         }
245
246 //### Add info for watchpoints and exceptions
247

248         public void watchpointSet(SpecEvent e) {
249         }
250         public void watchpointDeferred(SpecEvent e) {
251         }
252         public void watchpointDeleted(SpecEvent e) {
253         }
254         public void watchpointResolved(SpecEvent e) {
255         }
256         public void watchpointError(SpecErrorEvent e) {
257         }
258
259         public void exceptionInterceptSet(SpecEvent e) {
260         }
261         public void exceptionInterceptDeferred(SpecEvent e) {
262         }
263         public void exceptionInterceptDeleted(SpecEvent e) {
264         }
265         public void exceptionInterceptResolved(SpecEvent e) {
266         }
267         public void exceptionInterceptError(SpecErrorEvent e) {
268         }
269
270
271     // ContextListener.
272

273     // If the user selects a new current thread or frame, update prompt.
274

275     public void currentFrameChanged(CurrentFrameChangedEvent e) {
276         // Update prompt only if affect thread is current.
277
ThreadReference thread = e.getThread();
278         if (thread == context.getCurrentThread()) {
279         script.setPrompt(promptString(thread, e.getIndex()));
280         }
281         }
282
283     }
284
285     private String JavaDoc locationString(LocatableEventSet e) {
286         Location loc = e.getLocation();
287         return "thread=\"" + e.getThread().name() +
288             "\", " + Utils.locationString(loc);
289     }
290
291     private void setThread(LocatableEventSet e) {
292         if (!e.suspendedNone()) {
293             Thread.yield(); // fetch output
294
script.setPrompt(promptString(e.getThread(), 0));
295             //### Current thread should be set elsewhere, e.g.,
296
//### in ContextManager
297
//### context.setCurrentThread(thread);
298
}
299     }
300                           
301     private String JavaDoc promptString(ThreadReference thread, int frameIndex) {
302         if (thread == null) {
303         return DEFAULT_CMD_PROMPT;
304         } else {
305         // Frame indices are presented to user as indexed from 1.
306
return (thread.name() + "[" + (frameIndex + 1) + "]:");
307     }
308     }
309 }
310
311
312
313
314
Popular Tags