1 22 23 package org.aspectj.debugger.tty; 24 25 import com.sun.jdi.*; 26 import com.sun.jdi.event.*; 27 import com.sun.jdi.request.*; 28 import com.sun.jdi.connect.*; 29 import org.aspectj.debugger.base.*; 30 import org.aspectj.debugger.request.*; 31 import org.aspectj.tools.ide.SourceLine; 32 import java.awt.*; 33 import java.awt.event.*; 34 import java.io.*; 35 import java.util.*; 36 import java.util.List ; 37 import javax.swing.*; 38 import javax.swing.event.*; 39 import javax.swing.text.*; 40 41 49 50 public class CommandLineDebugger 51 implements 52 DebuggerApp, 53 StopListener, 54 VMListener, 55 DebuggerListener, 56 PromptListener, 57 Runnable { 58 59 protected AJDebugger debugger; private boolean isRunning = true; 61 private BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 62 private static String title = "AspectJ Example Debugger"; 63 private static String version = "v0.1"; 64 private static String copyright = "(C) 2000 Xerox Corporation"; 65 private String lastCommand = ""; 66 private boolean echo = false; 67 private static String [] echoStrings = { 68 69 }; 70 private boolean showPrompt; 71 private String [] cmdLineArgs; 72 private Thread thread; 73 private Options options = new Options(); 74 protected boolean wantsToExit = true; 75 76 private boolean debug = false; private void db(Object o) { 78 if (debug) { 79 System.err.println("<CL DEBUGGER>: " + o); 80 } 81 } 82 83 public CommandLineDebugger(String [] args) { 84 this(args, true); 85 } 86 87 public CommandLineDebugger(AJDebugger debugger, String [] args) { 88 this(debugger, args, true); 89 } 90 91 public CommandLineDebugger(String [] args, String scriptFile) { 92 this(args, false); 93 List commands = new Vector(); 94 try { 95 commands = debugger.readCommand(scriptFile, true); 96 } catch (NoVMException nvme) { 97 } catch (DebuggerException de) { 98 } 99 for (int i = 0; i < commands.size(); i++) { 100 String command = commands.get(i) + ""; 101 executeCommand(command); 102 } 103 while (debugger.isRunning()) {} 104 exit(); 105 } 106 107 public CommandLineDebugger(String args[], boolean showPrompt) { 108 this.debugger = new AJDebugger(this, showPrompt); 109 debugger.addStopListener(this); 110 debugger.addVMListener(this); 111 debugger.addDebuggerListener(this); 112 debugger.addPromptListener(this); 113 this.showPrompt = showPrompt; 114 this.cmdLineArgs = args; 115 options.fill(args); 116 } 117 118 public CommandLineDebugger(AJDebugger db, 119 String args[], 120 boolean showPrompt) { 121 this.debugger = db; 122 debugger.addStopListener(this); 123 debugger.addVMListener(this); 124 debugger.addDebuggerListener(this); 125 debugger.addPromptListener(this); 126 this.showPrompt = showPrompt; 127 this.cmdLineArgs = args; 128 options.fill(args); 129 } 130 131 public void go() { 132 debugger.setOptions(options); 133 String mainClass = options.getClassName(); 134 boolean runNow = false; 135 if (!options.isSet("now") && 136 mainClass != null && 137 !mainClass.equals("")) { 138 debugger.loadMainClass(options); 139 } 140 start(); 141 if (options.isSet("now")) { 142 debugger.runCommand(); 143 } 144 } 145 146 public void start() { 147 if (thread == null) { 148 thread = new Thread (this); 149 thread.start(); 150 } 151 } 152 153 public void run() { 154 if (showPrompt ) { 155 while (true) { 156 prompt(); 157 executeCommand(line()); 158 } 159 } 160 } 161 162 public boolean isGUI() { 163 return false; 164 } 165 166 public AJDebugger getDebugger() { 167 return debugger; 168 } 169 170 171 172 private boolean runNow(String [] args) { 173 return !options.getClassName().equals(""); 174 } 175 176 String className(String [] args) { 177 if (args != null && args.length > 0) { 178 return args[0]; 179 } 180 return ""; 181 } 182 183 String exArgs(String [] args) { 184 String exArgs = ""; 185 if (args != null && args.length > 1) { 186 for (int i = 1; i < args.length; i++) { 187 exArgs += args[i] + " "; 188 } 189 } 190 return exArgs.trim(); 191 } 192 193 194 195 196 197 199 public void runCommand(String className, 200 String exArgs) 201 throws NoVMException, DebuggerException { 202 } 203 204 public void runCommand(String className, 205 String vmArgs, 206 String exArgs, 207 boolean isSuspended, int debugTraceMode) 208 throws NoVMException, DebuggerException { 209 } 210 211 public void clearAtCommand(String className, 212 int line, 213 EventRequest request) 214 throws NoVMException, DebuggerException { 215 } 216 217 public void clearInCommand(String className, 218 String methodProto, 219 EventRequest request) 220 throws NoVMException, DebuggerException { 221 } 222 223 public void clearOnCommand(String sourceName, 224 int line, 225 EventRequest request) 226 throws NoVMException, DebuggerException { 227 } 228 229 public void clearCommand(List breakpoints) 230 throws NoVMException, DebuggerException { 231 result("Breakpoints:\n" + debugger.iter(breakpoints)); 232 } 233 234 public void clearAllCommand(List breakpoints) 235 throws NoVMException, DebuggerException { 236 result("Breakpoint requests cleared:\n" + debugger.iter(breakpoints)); 237 } 238 239 public void stopAtCommand(String className, 240 int line, 241 EventRequest request) 242 throws NoVMException, DebuggerException { 243 } 244 245 public void stopInCommand(String className, 246 String methodProto, 247 EventRequest request) 248 throws NoVMException, DebuggerException { 249 } 250 251 public void stopOnCommand(String sourceName, 252 int line, 253 EventRequest request) 254 throws NoVMException, DebuggerException { 255 } 256 257 public void stopCommand(List breakpoints) 258 throws NoVMException, DebuggerException { 259 clearCommand(breakpoints); 260 } 261 262 public void contCommand() 263 throws NoVMException, DebuggerException { 264 } 265 266 public void threadsCommand(String threadGroupName, List threads) 267 throws NoVMException, DebuggerException { 268 result(debugger.threads(threads)); 269 } 270 271 public void threadGroupsCommand(List threadGroups) 272 throws NoVMException, DebuggerException { 273 result(debugger.threadGroups(threadGroups)); 274 } 275 276 public void fieldsCommand(String className, List fields) 277 throws NoVMException, DebuggerException { 278 result(debugger.fields(fields)); 279 } 280 281 public void methodsCommand(String className, List methods) 282 throws NoVMException, DebuggerException { 283 result(debugger.methods(methods)); 284 } 285 286 public void classesCommand(List classes) 287 throws NoVMException, DebuggerException { 288 result(debugger.classes(classes)); 289 } 290 291 public void classCommand(String className, 292 ReferenceType refType) 293 throws NoVMException, DebuggerException { 294 result(debugger.clazz(refType)); 295 } 296 297 public void threadCommand(String threadName, 298 ThreadReference threadRef) 299 throws NoVMException, DebuggerException { 300 } 301 302 public void threadGroupCommand(String threadGroupName, 303 ThreadGroupReference threadGroupRef) 304 throws NoVMException, DebuggerException { 305 } 306 307 public void suspendCommand(List threadNames, List threads) 308 throws NoVMException, DebuggerException { 309 } 310 311 public void resumeCommand(List threadNames, List threads) 312 throws NoVMException, DebuggerException { 313 } 314 315 public void whereCommand(String threadName, List frames) 316 throws NoVMException, DebuggerException { 317 result(debugger.frames(frames)); 318 } 319 320 public void localsCommand(List locals) 321 throws NoVMException, DebuggerException { 322 result(debugger.locals(locals)); 323 } 324 325 public void interruptCommand(String threadName, 326 ThreadReference threadRef) 327 throws NoVMException, DebuggerException { 328 } 329 330 public void upCommand(int frames, StackFrame currentFrame) 331 throws NoVMException, DebuggerException { 332 } 333 334 public void downCommand(int frames, StackFrame currentFrame) 335 throws NoVMException, DebuggerException { 336 } 337 338 public void stepCommand(StepRequest request) 339 throws NoVMException, DebuggerException { 340 } 341 342 public void stepUpCommand(StepRequest request) 343 throws NoVMException, DebuggerException { 344 } 345 346 public void stepiCommand(StepRequest request) 347 throws NoVMException, DebuggerException { 348 } 349 350 public void nextCommand(StepRequest request) 351 throws NoVMException, DebuggerException { 352 } 353 354 public void printCommand(Object valueRep, Value value) 355 throws NoVMException, DebuggerException { 356 result(" " + valueRep + " = " + getValue(value)); 357 } 358 359 public void dumpCommand(Object valueRep, Value value) 360 throws NoVMException, DebuggerException { 361 result(" " + valueRep + " = " + getValue(value)); 362 } 363 364 public void evalCommand(Object valueRep, Value value) 365 throws NoVMException, DebuggerException { 366 result(" " + valueRep + " = " + getValue(value)); 367 } 368 369 private String getValue(Value value) { 370 String valString = value + ""; 371 if (!debugger.getOptions().isSet("extra")) { 372 return valString; 373 } 374 valString += " = " + value(value); 375 return valString; 376 } 377 378 382 private String value(Value value) { 383 if (value == null) { 384 return "null"; 385 } 386 if (!(value instanceof ObjectReference)) { 387 return value+""; } 389 String valString = ""; 391 try { 392 ObjectReference oRef = (ObjectReference) value; 393 if (oRef instanceof ArrayReference) { 394 String result = "{"; 395 ArrayReference arrayRef = (ArrayReference) oRef; 396 int length = arrayRef.length(); 397 for (int i = 0; i < length; i++) { 398 try { 399 result += value(arrayRef.getValue(i)); 400 } catch (NullPointerException npe) { 401 result += "null"; 402 } 403 result += i < length-1 ? ", " : ""; 404 } 405 result += "}"; 406 return result; 407 } 408 ReferenceType refType = oRef.referenceType(); 409 String name = refType.name(); 410 ThreadReference thread = null; 411 try { 412 if (debugger.getVM().canGetOwnedMonitorInfo()) { 413 try { 414 thread = oRef.owningThread(); 415 } catch (UnsupportedOperationException uoe) { 416 } 417 } 418 if (thread == null) { 419 thread = debugger.getDefaultThread(); 420 } 421 } catch (NoVMException nvme) { 422 return valString; 423 } 424 int options = ObjectReference.INVOKE_NONVIRTUAL; 425 Method method = null; 426 if (name.equals("java.lang.Byte")) { 428 method = (Method)refType.methodsByName("byteValue").get(0); 429 } else if (name.equals("java.lang.Boolean")) { 430 method = (Method)refType.methodsByName("booleanValue").get(0); 431 } else if (name.equals("java.lang.Character")) { 432 method = (Method)refType.methodsByName("charValue").get(0); 433 } else if (name.equals("java.lang.Double")) { 434 method = (Method)refType.methodsByName("doubleValue").get(0); 435 } else if (name.equals("java.lang.Float")) { 436 method = (Method)refType.methodsByName("floatValue").get(0); 437 } else if (name.equals("java.lang.Integer")) { 438 method = (Method)refType.methodsByName("intValue").get(0); 439 } else if (name.equals("java.lang.Long")) { 440 method = (Method)refType.methodsByName("longValue").get(0); 441 } else if (name.equals("java.lang.Short")) { 442 method = (Method)refType.methodsByName("shortValue").get(0); 443 } else { 444 List list = refType.methodsByName("toString"); 445 if (null != list) { 446 for (Iterator it = list.iterator(); it.hasNext();) { 447 Method m = (Method) it.next(); 448 List types = m.argumentTypes(); 449 if ((null == types) || (0 == types.size())) { 450 method = m; 451 System.err.println("method: " + method); 452 break; 453 } 454 } 455 } 456 } 457 if ((null != thread) && (null != method)) { 458 Value newValue = oRef.invokeMethod(thread, method, 459 new Vector(), options); 460 valString += newValue+""; 461 } 462 } catch (InvalidTypeException ite) { } catch (ClassNotLoadedException cnle) { 464 } catch (IncompatibleThreadStateException itse) { 465 } catch (InvocationException ie) { 466 } 467 return valString; 468 } 469 470 public void setWantsToExit(boolean wantsToExit) { 471 this.wantsToExit = wantsToExit; 472 } 473 474 public void setCommand(Object lvalue, 475 Object rvalue, 476 Value oldValue, 477 Value newValue) 478 throws NoVMException, DebuggerException { 479 result("Changed '" + lvalue + "' from '" + 480 oldValue + "' to '" + newValue + "'"); 481 } 482 483 public void classpathCommand(String baseDirectory, List paths) 484 throws NoVMException, DebuggerException { 485 String result = 486 "base directory: " + baseDirectory + "\n" + 487 "classpath: " + paths; 488 result(result); 489 } 490 491 public void lockCommand(Object valueRep, LockInformation lockInfo) 492 throws NoVMException, DebuggerException { 493 result("Lock information for '" + valueRep + "':\n" + lockInfo); 494 } 495 496 public void threadlocksCommand(String threadName, 497 ThreadLockInformation threadLockInfo) 498 throws NoVMException, DebuggerException { 499 result("Thread lock information for '" + 500 threadName + "':\n" + threadLockInfo); 501 } 502 503 public void watchAccessCommand(String className, 504 String fieldName, 505 WatchpointRequest request) 506 throws NoVMException, DebuggerException { 507 } 508 509 public void watchAllCommand(String className, 510 String fieldName, 511 WatchpointRequest request) 512 throws NoVMException, DebuggerException { 513 } 514 515 public void unwatchAccessCommand(String className, 516 String fieldName, 517 WatchpointRequest request) 518 throws NoVMException, DebuggerException { 519 } 520 521 public void unwatchAllCommand(String className, 522 String fieldName, 523 WatchpointRequest request) 524 throws NoVMException, DebuggerException { 525 } 526 527 public void catchCommand(String className, ExceptionRequest request) 528 throws NoVMException, DebuggerException { 529 } 530 531 public void ignoreCommand(String className, ExceptionRequest request) 532 throws NoVMException, DebuggerException { 533 } 534 535 public void traceMethodsCommand(String threadName, 536 TraceMethodsRequest.EntryExitPair pair) 537 throws NoVMException, DebuggerException { 538 } 539 540 public void untraceMethodsCommand(String threadName, 541 UntraceMethodsRequest.EntryExitPair pair) 542 throws NoVMException, DebuggerException { 543 } 544 545 public void excludeCommand(List classNames, List classes) 546 throws NoVMException, DebuggerException { 547 result("Exclude:\n" + debugger.iter(classes)); 548 } 549 550 public void quitCommand() 551 throws NoVMException, DebuggerException { 552 } 553 554 public void helpCommand(String helpString) 555 throws NoVMException, DebuggerException { 556 result(helpString); 557 } 558 559 public void versionCommand(Object version) 560 throws NoVMException, DebuggerException { 561 result(version); 562 } 563 564 public void killCommand(String threadName, 565 String valueRep, 566 ThreadReference threadRef) 567 throws NoVMException, DebuggerException { 568 result("Killed thread '" + threadName + "' = " + 569 threadRef + " with '" + valueRep + "'"); 570 } 571 572 public void useCommand(String sourcePath, String newSourcePath) 573 throws NoVMException, DebuggerException { 574 result("Using sourcepath: " + newSourcePath); 575 } 576 577 public void workingdirCommand(String workingdirPath, String newWorkingdirPath) 578 throws NoVMException, DebuggerException { 579 result("Using workingdir: " + newWorkingdirPath); 580 } 581 582 public void listCommand(List sourceLines) 583 throws NoVMException, DebuggerException { 584 Object o = debugger.sourceLines(sourceLines, true); 585 if (sourceLines.size() == 0) { 586 o = "Source file not found: unkown.source"; 587 } 588 result(o); 589 } 590 591 public void listCommand(String sourceName, List sourceLines) 592 throws NoVMException, DebuggerException { 593 Object o = debugger.sourceLines(sourceLines, true); 594 if (sourceLines.size() == 0) { 595 o = 596 "Source file not found: " + 597 (sourceName != null ? sourceName : "<unkown>"); 598 } 599 result(o); 600 } 601 602 public void listCommand(String sourceName, 603 int lineNumber, 604 SourceManager.SourceLine sl) 605 throws NoVMException, DebuggerException { 606 Object o = debugger.sourceLine(sl); 607 if (sl.getLineNumber() < 0) { 608 o = 609 "<no source available for " + 610 sourceName + ":" + lineNumber + ">"; 611 } 612 result(o); 613 } 614 615 public void listCommand(String sourceName, 616 int startLine, 617 int endLine, 618 List sourceLines) 619 throws NoVMException, DebuggerException { 620 listCommand(sourceLines); 621 } 622 623 public void monitorCommand(String command, String result) 624 throws NoVMException, DebuggerException { 625 } 626 627 public void monitorCommand(List monitors) 628 throws NoVMException, DebuggerException { 629 result(monitors); 630 } 631 632 public void unmonitorCommand(int number, MonitorRequest request) 633 throws NoVMException, DebuggerException { 634 result("Unmonitoring " + request); 635 } 636 637 public void readCommand(String fileName, List commands) 638 throws NoVMException, DebuggerException { 639 } 640 641 public void viewCommand(String sourceName, List sourceLines) 642 throws NoVMException, DebuggerException { 643 listCommand(sourceName, sourceLines); 644 } 645 646 public void tostringCommand(String tostring) 647 throws NoVMException, DebuggerException { 648 result(tostring); 649 } 650 651 public void connectCommand(VirtualMachine vm) 652 throws NoVMException, DebuggerException { 653 throw new DebuggerException("connect is not valid on the command line"); 654 } 655 656 public void pwdCommand(File file) 657 throws NoVMException, DebuggerException { 658 if (file != null) result(file.getAbsolutePath()); 659 } 660 661 662 public void lsCommand(String dir, List files) 663 throws NoVMException, DebuggerException { 664 result(dir + ":\n" + debugger.iter(files)); 665 } 666 667 669 670 671 672 public void handleParseException(ParseException e) { 673 result(e.getMessage()); 674 } 675 676 public void handleNoVMException(NoVMException e) { 677 result("There is no VM: " + nonull(e.getMessage())); 678 } 679 680 public void handleDebuggerException(DebuggerException e) { 681 result(e.getMessage()); 682 } 683 684 public void handleVMDisconnectedException(VMDisconnectedException e) { 685 result("The VM has already disconnected."); 686 } 687 688 public void handleInternalException(Throwable e) { 689 result("Internal error: " + e); 690 e.printStackTrace(getOutputStream()); 691 new ErrorLogger(debugger).log(e); 692 } 693 694 695 696 public PrintStream getOutputStream() { 697 return System.out; 698 } 699 700 701 702 public Object executeCommand(String str) { 703 if (echo(str)) { 704 result(str); 705 } 706 return debugger.execute(str.trim()); 707 } 708 709 private boolean echo(String str) { 710 if (str != null) { 711 str = str.trim(); 712 for (int i = 0; i < echoStrings.length; i++) { 713 if (str.startsWith(echoStrings[i])) { 714 return true; 715 } 716 } 717 } 718 return false; 719 } 720 721 private void repeat() { 722 executeCommand(lastCommand); 723 } 724 725 protected boolean nonull(String str) { 726 return str != null && !"null".equals(str); 727 } 728 729 protected void result(Object str) { 730 if (str != null) outln(str); 731 } 732 733 protected void result(List list) { 734 Iterator iter = list.iterator(); 735 while (iter.hasNext()) { 736 result(iter.next()); 737 } 738 } 739 740 private void warn(String str) { 741 outln(" *** " + str); 742 } 743 744 public String line() { 745 String str = ""; 746 try { db("get line?"); 747 str = in.readLine(); 748 db("got line str=" + str); 749 } catch (IOException e) { 750 } 751 return str; 752 } 753 754 public void exit() { 755 isRunning = false; 756 debugger.shutDown(); 757 if (wantsToExit()) { 758 System.exit(0); 759 } 760 } 761 762 private void prompt() { 763 if (showPrompt) out(debugger.getPrompt() + " "); 764 } 765 766 private void out(Object o) { 767 System.out.print(o); 768 System.out.flush(); 769 } 770 771 public void outln(Object o) { 772 out(o); 773 out("\n"); 774 } 775 776 public void log(Object o) { 777 } 778 779 private VirtualMachine vm() { 780 return debugger.vm(); 781 } 782 783 784 785 786 787 public void resetPrompt() { 788 prompt(); 789 } 790 791 792 public void accessWatchpointEvent(AccessWatchpointEvent e){ 793 } 794 public void breakpointEvent(BreakpointEvent e){ 795 result("\n\nBreakpoint hit: " + debugger.format(e)); 796 } 797 public void exceptionEvent(ExceptionEvent e){ 798 result("\n\nException occured: " + debugger.format(e)); 799 } 800 public void modificationWatchpointEvent(ModificationWatchpointEvent e){ 801 } 802 public void stepEvent(StepEvent e){ 803 result("\n\nStep completed: " + debugger.format(e)); 804 } 805 806 807 private int deaths = 0; 808 public void vmDeathEvent(VMDeathEvent e) { 809 die(); 810 } 811 public void vmDisconnectEvent(VMDisconnectEvent e) { 812 die(); 813 } 814 public void vmStartEvent(VMStartEvent e) { 815 deaths = 0; 816 result("VM Started: "); 817 } 818 private void die() { 819 if (deaths++ == 0) result("\nThe application has exited."); 820 exit(); 821 } 822 823 824 public void requestSetEvent(RequestEvent re) { 825 RequestAction action = (RequestAction) re.getRequest(); 826 result("Set " + 827 (action.isDeferred() ? " deferred " : "") + 828 re.getRequest()); 829 } 830 public void requestClearEvent(RequestEvent re) { 831 result("Clear " + re.getRequest()); 832 } 833 public void requestDeferredEvent(RequestEvent re) { 834 if (!debugger.isRunning()) { 835 result("Deferring " + re.getRequest() + "\n" + 836 "It will be set after the class is loaded."); 837 } 838 } 839 public void requestFailedEvent(RequestEvent re) { 840 result("Unable to set " + re.getRequest() + 841 " : " + re.getErrorMessage()); 842 } 843 844 public boolean wantsToExit() { 845 return wantsToExit; 846 } 847 848 public boolean canRestart() { 849 return false; 850 } 851 852 public VirtualMachine connect(String vmArgs, 853 String className, 854 String commandLine) { 855 return null; 856 } 857 } 858 859 860 | Popular Tags |