1 7 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 ; 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 DEFAULT_CMD_PROMPT = "Command:"; 61 62 public CommandTool(Environment env) { 63 64 super(new BorderLayout ()); 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); this.add(script); 73 74 final CommandInterpreter interpreter = 75 new CommandInterpreter(env); 76 77 79 script.addActionListener(new ActionListener() { 80 public void actionPerformed(ActionEvent e) { 81 interpreter.executeCommand(script.readln()); 82 } 83 }); 84 85 87 OutputListener diagnosticsListener = 88 new TypeScriptOutputListener(script, true); 89 runtime.addDiagnosticsListener(diagnosticsListener); 90 91 93 env.setTypeScript(new PrintWriter(new TypeScriptWriter(script))); 94 95 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 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 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 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 name = e.getException().referenceType().name(); 145 diagnostics.putString("Exception: " + name); 146 } 147 148 public void locationTrigger(LocationTriggerEventSet e) { 149 String 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 207 public void sessionStart(EventObject e) {} 208 209 public void sessionInterrupt(EventObject e) { 210 Thread.yield(); 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 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 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 273 275 public void currentFrameChanged(CurrentFrameChangedEvent e) { 276 ThreadReference thread = e.getThread(); 278 if (thread == context.getCurrentThread()) { 279 script.setPrompt(promptString(thread, e.getIndex())); 280 } 281 } 282 283 } 284 285 private String 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(); script.setPrompt(promptString(e.getThread(), 0)); 295 } 299 } 300 301 private String promptString(ThreadReference thread, int frameIndex) { 302 if (thread == null) { 303 return DEFAULT_CMD_PROMPT; 304 } else { 305 return (thread.name() + "[" + (frameIndex + 1) + "]:"); 307 } 308 } 309 } 310 311 312 313 314 | Popular Tags |