commandinterpreter.java
来自「jpda例子文件」· Java 代码 · 共 1,488 行 · 第 1/4 页
JAVA
1,488 行
ThreadReference thread = (ThreadReference)it.next(); out.println(thread.name() + ": "); dumpStack(thread, showPC); } } else { ThreadReference thread = findThread(t.nextToken()); //### Do we want to set current thread here? //### Should notify user of change. if (thread != null) { context.setCurrentThread(thread); } dumpStack(thread, showPC); } } } private void dumpStack(ThreadReference thread, boolean showPC) { //### Check for these. //env.failure("Thread no longer exists."); //env.failure("Target VM must be in interrupted state."); //env.failure("Current thread isn't suspended."); //### Should handle extremely long stack traces sensibly for user. List stack = null; try { stack = thread.frames(); } catch (IncompatibleThreadStateException e) { env.failure("Thread is not suspended."); } //### Fix this! //### Previously mishandled cases where thread was not current. //### Now, prints all of the stack regardless of current frame. int frameIndex = 0; //int frameIndex = context.getCurrentFrameIndex(); if (stack == null) { env.failure("Thread is not running (no stack)."); } else { OutputSink out = env.getOutputSink(); int nFrames = stack.size(); for (int i = frameIndex; i < nFrames; i++) { StackFrame frame = (StackFrame)stack.get(i); Location loc = frame.location(); Method meth = loc.method(); out.print(" [" + (i + 1) + "] "); out.print(meth.declaringType().name()); out.print('.'); out.print(meth.name()); out.print(" ("); if (meth instanceof Method && ((Method)meth).isNative()) { out.print("native method"); } else if (loc.lineNumber() != -1) { try { out.print(loc.sourceName()); } catch (AbsentInformationException e) { out.print("<unknown>"); } out.print(':'); out.print(loc.lineNumber()); } out.print(')'); if (showPC) { long pc = loc.codeIndex(); if (pc != -1) { out.print(", pc = " + pc); } } out.println(); } out.show(); } } private void listEventRequests() throws NoSessionException { // Print set breakpoints Iterator iter = runtime.eventRequestSpecs().iterator(); if (!iter.hasNext()) { env.notice("No breakpoints/watchpoints/exceptions set."); } else { OutputSink out = env.getOutputSink(); out.println("Current breakpoints/watchpoints/exceptions set:"); while (iter.hasNext()) { EventRequestSpec bp = (EventRequestSpec)iter.next(); out.println("\t" + bp); } out.show(); } } private BreakpointSpec parseBreakpointSpec(String bptSpec) { StringTokenizer t = new StringTokenizer(bptSpec); BreakpointSpec bpSpec = null;// try { String token = t.nextToken("@:( \t\n\r"); // We can't use hasMoreTokens here because it will cause any leading // paren to be lost. String rest; try { rest = t.nextToken("").trim(); } catch (NoSuchElementException e) { rest = null; } if ((rest != null) && rest.startsWith("@")) { t = new StringTokenizer(rest.substring(1)); String sourceName = token; String lineToken = t.nextToken(); int lineNumber = Integer.valueOf(lineToken).intValue(); if (t.hasMoreTokens()) { return null; } bpSpec = runtime.createSourceLineBreakpoint(sourceName, lineNumber); } else if ((rest != null) && rest.startsWith(":")) { t = new StringTokenizer(rest.substring(1)); String classId = token; String lineToken = t.nextToken(); int lineNumber = Integer.valueOf(lineToken).intValue(); if (t.hasMoreTokens()) { return null; } bpSpec = runtime.createClassLineBreakpoint(classId, lineNumber); } else { // Try stripping method from class.method token. int idot = token.lastIndexOf("."); if ( (idot <= 0) || /* No dot or dot in first char */ (idot >= token.length() - 1) ) { /* dot in last char */ return null; } String methodName = token.substring(idot + 1); String classId = token.substring(0, idot); List argumentList = null; if (rest != null) { if (!rest.startsWith("(") || !rest.endsWith(")")) { //### Should throw exception with error message //out.println("Invalid method specification: " // + methodName + rest); return null; } // Trim the parens //### What about spaces in arglist? rest = rest.substring(1, rest.length() - 1); argumentList = new ArrayList(); t = new StringTokenizer(rest, ","); while (t.hasMoreTokens()) { argumentList.add(t.nextToken()); } } bpSpec = runtime.createMethodBreakpoint(classId, methodName, argumentList); }// } catch (Exception e) {// env.error("Exception attempting to create breakpoint: " + e);// return null;// } return bpSpec; } private void commandStop(StringTokenizer t) throws NoSessionException { Location bploc; String token; if (!t.hasMoreTokens()) { listEventRequests(); } else { token = t.nextToken(); // Ignore optional "at" or "in" token. // Allowed for backward compatibility. if (token.equals("at") || token.equals("in")) { if (t.hasMoreTokens()) { token = t.nextToken(); } else { env.error("Missing breakpoint specification."); return; } } BreakpointSpec bpSpec = parseBreakpointSpec(token); if (bpSpec != null) { //### Add sanity-checks for deferred breakpoint. runtime.install(bpSpec); } else { env.error("Ill-formed breakpoint specification."); } } } private void commandClear(StringTokenizer t) throws NoSessionException { if (!t.hasMoreTokens()) { // Print set breakpoints listEventRequests(); return; } //### need 'clear all' BreakpointSpec bpSpec = parseBreakpointSpec(t.nextToken()); if (bpSpec != null) { Iterator iter = runtime.eventRequestSpecs().iterator(); if (!iter.hasNext()) { env.notice("No breakpoints set."); } else { List toDelete = new ArrayList(); while (iter.hasNext()) { BreakpointSpec spec = (BreakpointSpec)iter.next(); if (spec.equals(bpSpec)) { toDelete.add(spec); } } // The request used for matching should be found if (toDelete.size() <= 1) { env.notice("No matching breakpoint set."); } for (Iterator it = toDelete.iterator(); it.hasNext();) { BreakpointSpec spec = (BreakpointSpec)it.next(); runtime.delete(spec); } } } else { env.error("Ill-formed breakpoint specification."); } } // Command: list private void commandList(StringTokenizer t) throws NoSessionException { ThreadReference current = context.getCurrentThread(); if (current == null) { env.error("No thread specified."); return; } Location loc; try { StackFrame frame = context.getCurrentFrame(current); if (frame == null) { env.failure("Thread has not yet begun execution."); return; } loc = frame.location(); } catch (VMNotInterruptedException e) { env.failure("Target VM must be in interrupted state."); return; } SourceModel source = sourceManager.sourceForLocation(loc); if (source == null) { if (loc.method().isNative()) { env.failure("Current method is native."); return; } env.failure("No source available for " + Utils.locationString(loc) + "."); return; } ReferenceType refType = loc.declaringType(); int lineno = loc.lineNumber(); if (t.hasMoreTokens()) { String id = t.nextToken(); // See if token is a line number. try { lineno = Integer.valueOf(id).intValue(); } catch (NumberFormatException nfe) { // It isn't -- see if it's a method name. List meths = refType.methodsByName(id); if (meths == null || meths.size() == 0) { env.failure(id + " is not a valid line number or " + "method name for class " + refType.name()); return; } else if (meths.size() > 1) { env.failure(id + " is an ambiguous method name in" + refType.name()); return; } loc = ((Method)meths.get(0)).location(); lineno = loc.lineNumber(); } } int startLine = (lineno > 4) ? lineno - 4 : 1; int endLine = startLine + 9; String sourceLine = source.sourceLine(lineno); if (sourceLine == null) { env.failure("" + lineno + " is an invalid line number for " + refType.name()); } else { OutputSink out = env.getOutputSink(); for (int i = startLine; i <= endLine; i++) { sourceLine = source.sourceLine(i); if (sourceLine == null) { break; } out.print(i); out.print("\t"); if (i == lineno) { out.print("=> "); } else { out.print(" "); } out.println(sourceLine); } out.show(); } } // Command: use // Get or set the source file path list. private void commandUse(StringTokenizer t) { if (!t.hasMoreTokens()) { out.println(sourceManager.getSourcePath().asString()); } else { //### Should throw exception for invalid path. //### E.g., vetoable property change. sourceManager.setSourcePath(new SearchPath(t.nextToken())); } } // Command: sourcepath // Get or set the source file path list. (Alternate to 'use'.) private void commandSourcepath(StringTokenizer t) { if (!t.hasMoreTokens()) { out.println(sourceManager.getSourcePath().asString()); } else { //### Should throw exception for invalid path. //### E.g., vetoable property change. sourceManager.setSourcePath(new SearchPath(t.nextToken())); } } // Command: classpath // Get or set the class file path list. private void commandClasspath(StringTokenizer t) { if (!t.hasMoreTokens()) { out.println(classManager.getClassPath().asString()); } else { //### Should throw exception for invalid path. //### E.g., vetoable property change. classManager.setClassPath(new SearchPath(t.nextToken())); } } // Command: view // Display source for source file or class. private void commandView(StringTokenizer t) throws NoSessionException { if (!t.hasMoreTokens()) { env.error("Argument required"); } else { String name = t.nextToken(); if (name.endsWith(".java") || name.indexOf(File.separatorChar) >= 0) { env.viewSource(name); } else { //### JDI crashes taking line number for class. /***** ReferenceType cls = findClass(name); if (cls != null) { env.viewLocation(cls.location()); } else { env.failure("No such class"); } *****/ String fileName = name.replace('.', File.separatorChar) + ".java"; env.viewSource(fileName); } } } // Command: locals // Print all local variables in current stack frame. private void commandLocals() throws NoSessionException { ThreadReference current = context.getCurrentThread();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?