Showing the changes to java/pkg/com/sun/tools/example/debug/gui/CommandTool.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,333 @@ | |||
| 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 | |||
| 26 | package com.sun.tools.example.debug.gui; | ||
| 27 | |||
| 28 | import java.io.*; | ||
| 29 | import java.util.*; | ||
| 30 | |||
| 31 | import javax.swing.*; | ||
| 32 | import java.awt.BorderLayout; | ||
| 33 | import java.awt.event.*; | ||
| 34 | |||
| 35 | import com.sun.jdi.*; | ||
| 36 | import com.sun.jdi.event.*; | ||
| 37 | |||
| 38 | import com.sun.tools.example.debug.bdi.*; | ||
| 39 | import com.sun.tools.example.debug.event.*; | ||
| 40 | |||
| 41 | public class CommandTool extends JPanel { | ||
| 42 | |||
| 43 | private static final long serialVersionUID = 8613516856378346415L; | ||
| 44 | |||
| 45 | private Environment env; | ||
| 46 | |||
| 47 | private ContextManager context; | ||
| 48 | private ExecutionManager runtime; | ||
| 49 | private SourceManager sourceManager; | ||
| 50 | |||
| 51 | private TypeScript script; | ||
| 52 | |||
| 53 | private static final String DEFAULT_CMD_PROMPT = "Command:"; | ||
| 54 | |||
| 55 | public CommandTool(Environment env) { | ||
| 56 | |||
| 57 | super(new BorderLayout()); | ||
| 58 | |||
| 59 | this.env = env; | ||
| 60 | this.context = env.getContextManager(); | ||
| 61 | this.runtime = env.getExecutionManager(); | ||
| 62 | this.sourceManager = env.getSourceManager(); | ||
| 63 | |||
| 64 | script = new TypeScript(DEFAULT_CMD_PROMPT, false); //no echo | ||
| 65 | this.add(script); | ||
| 66 | |||
| 67 | final CommandInterpreter interpreter = | ||
| 68 | new CommandInterpreter(env); | ||
| 69 | |||
| 70 | // Establish handler for incoming commands. | ||
| 71 | |||
| 72 | script.addActionListener(new ActionListener() { | ||
| 73 | @Override | ||
| 74 | public void actionPerformed(ActionEvent e) { | ||
| 75 | interpreter.executeCommand(script.readln()); | ||
| 76 | } | ||
| 77 | }); | ||
| 78 | |||
| 79 | // Establish ourselves as the listener for VM diagnostics. | ||
| 80 | |||
| 81 | OutputListener diagnosticsListener = | ||
| 82 | new TypeScriptOutputListener(script, true); | ||
| 83 | runtime.addDiagnosticsListener(diagnosticsListener); | ||
| 84 | |||
| 85 | // Establish ourselves as the shared debugger typescript. | ||
| 86 | |||
| 87 | env.setTypeScript(new PrintWriter(new TypeScriptWriter(script))); | ||
| 88 | |||
| 89 | // Handle VM events. | ||
| 90 | |||
| 91 | TTYDebugListener listener = new TTYDebugListener(diagnosticsListener); | ||
| 92 | |||
| 93 | runtime.addJDIListener(listener); | ||
| 94 | runtime.addSessionListener(listener); | ||
| 95 | runtime.addSpecListener(listener); | ||
| 96 | context.addContextListener(listener); | ||
| 97 | |||
| 98 | //### remove listeners on exit! | ||
| 99 | |||
| 100 | } | ||
| 101 | |||
| 102 | private class TTYDebugListener implements | ||
| 103 | JDIListener, SessionListener, SpecListener, ContextListener { | ||
| 104 | |||
| 105 | private OutputListener diagnostics; | ||
| 106 | |||
| 107 | TTYDebugListener(OutputListener diagnostics) { | ||
| 108 | this.diagnostics = diagnostics; | ||
| 109 | } | ||
| 110 | |||
| 111 | // JDIListener | ||
| 112 | |||
| 113 | @Override | ||
| 114 | public void accessWatchpoint(AccessWatchpointEventSet e) { | ||
| 115 | setThread(e); | ||
| 116 | for (EventIterator it = e.eventIterator(); it.hasNext(); ) { | ||
| 117 | it.nextEvent(); | ||
| 118 | diagnostics.putString("Watchpoint hit: " + | ||
| 119 | locationString(e)); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | @Override | ||
| 124 | public void classPrepare(ClassPrepareEventSet e) { | ||
| 125 | if (context.getVerboseFlag()) { | ||
| 126 | String name = e.getReferenceType().name(); | ||
| 127 | diagnostics.putString("Class " + name + " loaded"); | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | @Override | ||
| 132 | public void classUnload(ClassUnloadEventSet e) { | ||
| 133 | if (context.getVerboseFlag()) { | ||
| 134 | diagnostics.putString("Class " + e.getClassName() + | ||
| 135 | " unloaded."); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | @Override | ||
| 140 | public void exception(ExceptionEventSet e) { | ||
| 141 | setThread(e); | ||
| 142 | String name = e.getException().referenceType().name(); | ||
| 143 | diagnostics.putString("Exception: " + name); | ||
| 144 | } | ||
| 145 | |||
| 146 | @Override | ||
| 147 | public void locationTrigger(LocationTriggerEventSet e) { | ||
| 148 | String locString = locationString(e); | ||
| 149 | setThread(e); | ||
| 150 | for (EventIterator it = e.eventIterator(); it.hasNext(); ) { | ||
| 151 | Event evt = it.nextEvent(); | ||
| 152 | if (evt instanceof BreakpointEvent) { | ||
| 153 | diagnostics.putString("Breakpoint hit: " + locString); | ||
| 154 | } else if (evt instanceof StepEvent) { | ||
| 155 | diagnostics.putString("Step completed: " + locString); | ||
| 156 | } else if (evt instanceof MethodEntryEvent) { | ||
| 157 | diagnostics.putString("Method entered: " + locString); | ||
| 158 | } else if (evt instanceof MethodExitEvent) { | ||
| 159 | diagnostics.putString("Method exited: " + locString); | ||
| 160 | } else { | ||
| 161 | diagnostics.putString("UNKNOWN event: " + e); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | @Override | ||
| 167 | public void modificationWatchpoint(ModificationWatchpointEventSet e) { | ||
| 168 | setThread(e); | ||
| 169 | for (EventIterator it = e.eventIterator(); it.hasNext(); ) { | ||
| 170 | it.nextEvent(); | ||
| 171 | diagnostics.putString("Watchpoint hit: " + | ||
| 172 | locationString(e)); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | @Override | ||
| 177 | public void threadDeath(ThreadDeathEventSet e) { | ||
| 178 | if (context.getVerboseFlag()) { | ||
| 179 | diagnostics.putString("Thread " + e.getThread() + | ||
| 180 | " ended."); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | @Override | ||
| 185 | public void threadStart(ThreadStartEventSet e) { | ||
| 186 | if (context.getVerboseFlag()) { | ||
| 187 | diagnostics.putString("Thread " + e.getThread() + | ||
| 188 | " started."); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | @Override | ||
| 193 | public void vmDeath(VMDeathEventSet e) { | ||
| 194 | script.setPrompt(DEFAULT_CMD_PROMPT); | ||
| 195 | diagnostics.putString("VM exited"); | ||
| 196 | } | ||
| 197 | |||
| 198 | @Override | ||
| 199 | public void vmDisconnect(VMDisconnectEventSet e) { | ||
| 200 | script.setPrompt(DEFAULT_CMD_PROMPT); | ||
| 201 | diagnostics.putString("Disconnected from VM"); | ||
| 202 | } | ||
| 203 | |||
| 204 | @Override | ||
| 205 | public void vmStart(VMStartEventSet e) { | ||
| 206 | script.setPrompt(DEFAULT_CMD_PROMPT); | ||
| 207 | diagnostics.putString("VM started"); | ||
| 208 | } | ||
| 209 | |||
| 210 | // SessionListener | ||
| 211 | |||
| 212 | @Override | ||
| 213 | public void sessionStart(EventObject e) {} | ||
| 214 | |||
| 215 | @Override | ||
| 216 | public void sessionInterrupt(EventObject e) { | ||
| 217 | Thread.yield(); // fetch output | ||
| 218 | diagnostics.putString("VM interrupted by user."); | ||
| 219 | script.setPrompt(DEFAULT_CMD_PROMPT); | ||
| 220 | } | ||
| 221 | |||
| 222 | @Override | ||
| 223 | public void sessionContinue(EventObject e) { | ||
| 224 | diagnostics.putString("Execution resumed."); | ||
| 225 | script.setPrompt(DEFAULT_CMD_PROMPT); | ||
| 226 | } | ||
| 227 | |||
| 228 | // SpecListener | ||
| 229 | |||
| 230 | @Override | ||
| 231 | public void breakpointSet(SpecEvent e) { | ||
| 232 | EventRequestSpec spec = e.getEventRequestSpec(); | ||
| 233 | diagnostics.putString("Breakpoint set at " + spec + "."); | ||
| 234 | } | ||
| 235 | @Override | ||
| 236 | public void breakpointDeferred(SpecEvent e) { | ||
| 237 | EventRequestSpec spec = e.getEventRequestSpec(); | ||
| 238 | diagnostics.putString("Breakpoint will be set at " + | ||
| 239 | spec + " when its class is loaded."); | ||
| 240 | } | ||
| 241 | @Override | ||
| 242 | public void breakpointDeleted(SpecEvent e) { | ||
| 243 | EventRequestSpec spec = e.getEventRequestSpec(); | ||
| 244 | diagnostics.putString("Breakpoint at " + spec.toString() + " deleted."); | ||
| 245 | } | ||
| 246 | @Override | ||
| 247 | public void breakpointResolved(SpecEvent e) { | ||
| 248 | EventRequestSpec spec = e.getEventRequestSpec(); | ||
| 249 | diagnostics.putString("Breakpoint resolved to " + spec.toString() + "."); | ||
| 250 | } | ||
| 251 | @Override | ||
| 252 | public void breakpointError(SpecErrorEvent e) { | ||
| 253 | EventRequestSpec spec = e.getEventRequestSpec(); | ||
| 254 | diagnostics.putString("Deferred breakpoint at " + | ||
| 255 | spec + " could not be resolved:" + | ||
| 256 | e.getReason()); | ||
| 257 | } | ||
| 258 | |||
| 259 | //### Add info for watchpoints and exceptions | ||
| 260 | |||
| 261 | @Override | ||
| 262 | public void watchpointSet(SpecEvent e) { | ||
| 263 | } | ||
| 264 | @Override | ||
| 265 | public void watchpointDeferred(SpecEvent e) { | ||
| 266 | } | ||
| 267 | @Override | ||
| 268 | public void watchpointDeleted(SpecEvent e) { | ||
| 269 | } | ||
| 270 | @Override | ||
| 271 | public void watchpointResolved(SpecEvent e) { | ||
| 272 | } | ||
| 273 | @Override | ||
| 274 | public void watchpointError(SpecErrorEvent e) { | ||
| 275 | } | ||
| 276 | |||
| 277 | @Override | ||
| 278 | public void exceptionInterceptSet(SpecEvent e) { | ||
| 279 | } | ||
| 280 | @Override | ||
| 281 | public void exceptionInterceptDeferred(SpecEvent e) { | ||
| 282 | } | ||
| 283 | @Override | ||
| 284 | public void exceptionInterceptDeleted(SpecEvent e) { | ||
| 285 | } | ||
| 286 | @Override | ||
| 287 | public void exceptionInterceptResolved(SpecEvent e) { | ||
| 288 | } | ||
| 289 | @Override | ||
| 290 | public void exceptionInterceptError(SpecErrorEvent e) { | ||
| 291 | } | ||
| 292 | |||
| 293 | |||
| 294 | // ContextListener. | ||
| 295 | |||
| 296 | // If the user selects a new current thread or frame, update prompt. | ||
| 297 | |||
| 298 | @Override | ||
| 299 | public void currentFrameChanged(CurrentFrameChangedEvent e) { | ||
| 300 | // Update prompt only if affect thread is current. | ||
| 301 | ThreadReference thread = e.getThread(); | ||
| 302 | if (thread == context.getCurrentThread()) { | ||
| 303 | script.setPrompt(promptString(thread, e.getIndex())); | ||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | } | ||
| 308 | |||
| 309 | private String locationString(LocatableEventSet e) { | ||
| 310 | Location loc = e.getLocation(); | ||
| 311 | return "thread=\"" + e.getThread().name() + | ||
| 312 | "\", " + Utils.locationString(loc); | ||
| 313 | } | ||
| 314 | |||
| 315 | private void setThread(LocatableEventSet e) { | ||
| 316 | if (!e.suspendedNone()) { | ||
| 317 | Thread.yield(); // fetch output | ||
| 318 | script.setPrompt(promptString(e.getThread(), 0)); | ||
| 319 | //### Current thread should be set elsewhere, e.g., | ||
| 320 | //### in ContextManager | ||
| 321 | //### context.setCurrentThread(thread); | ||
| 322 | } | ||
| 323 | } | ||
| 324 | |||
| 325 | private String promptString(ThreadReference thread, int frameIndex) { | ||
| 326 | if (thread == null) { | ||
| 327 | return DEFAULT_CMD_PROMPT; | ||
| 328 | } else { | ||
| 329 | // Frame indices are presented to user as indexed from 1. | ||
| 330 | return (thread.name() + "[" + (frameIndex + 1) + "]:"); | ||
| 331 | } | ||
| 332 | } | ||
| 333 | } | ||